Merge pull request #2798 from BrzVlad/fix-sgen-timestamp
[mono.git] / mono / mini / exceptions-arm.c
1 /*
2  * exceptions-arm.c: exception support for ARM
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc.
9  */
10
11 #include <config.h>
12 #include <glib.h>
13 #include <signal.h>
14 #include <string.h>
15
16 #ifndef MONO_CROSS_COMPILE
17 #ifdef HAVE_ASM_SIGCONTEXT_H
18 #include <asm/sigcontext.h>
19 #endif  /* def HAVE_ASM_SIGCONTEXT_H */
20 #endif
21
22 #ifdef HAVE_UCONTEXT_H
23 #include <ucontext.h>
24 #endif  /* def HAVE_UCONTEXT_H */
25
26 #include <mono/arch/arm/arm-codegen.h>
27 #include <mono/arch/arm/arm-vfp-codegen.h>
28 #include <mono/metadata/abi-details.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/threads.h>
32 #include <mono/metadata/debug-helpers.h>
33 #include <mono/metadata/exception.h>
34 #include <mono/metadata/mono-debug.h>
35
36 #include "mini.h"
37 #include "mini-arm.h"
38 #include "mono/utils/mono-sigcontext.h"
39 #include "mono/utils/mono-compiler.h"
40
41 /*
42  * arch_get_restore_context:
43  *
44  * Returns a pointer to a method which restores a previously saved sigcontext.
45  * The first argument in r0 is the pointer to the context.
46  */
47 gpointer
48 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
49 {
50         guint8 *code;
51         guint8 *start;
52         int ctx_reg;
53         MonoJumpInfo *ji = NULL;
54         GSList *unwind_ops = NULL;
55
56         start = code = mono_global_codeman_reserve (128);
57
58         /* 
59          * Move things to their proper place so we can restore all the registers with
60          * one instruction.
61          */
62
63         ctx_reg = ARMREG_R0;
64
65         if (!mono_arch_is_soft_float ()) {
66                 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs));
67                 ARM_FLDMD (code, ARM_VFP_D0, 16, ARMREG_IP);
68         }
69
70         /* move pc to PC */
71         ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
72         ARM_STR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_PC * sizeof (mgreg_t)));
73
74         /* restore everything */
75         ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs));
76         ARM_LDM (code, ARMREG_IP, 0xffff);
77
78         /* never reached */
79         ARM_DBRK (code);
80
81         g_assert ((code - start) < 128);
82
83         mono_arch_flush_icache (start, code - start);
84
85         if (info)
86                 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
87
88         return start;
89 }
90
91 /*
92  * arch_get_call_filter:
93  *
94  * Returns a pointer to a method which calls an exception filter. We
95  * also use this function to call finally handlers (we pass NULL as 
96  * @exc object in this case).
97  */
98 gpointer
99 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
100 {
101         guint8 *code;
102         guint8* start;
103         int ctx_reg;
104         MonoJumpInfo *ji = NULL;
105         GSList *unwind_ops = NULL;
106
107         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
108         start = code = mono_global_codeman_reserve (320);
109
110         /* save all the regs on the stack */
111         ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
112         ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
113
114         /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
115         ctx_reg = ARMREG_R0;
116         ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
117         ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs) + (MONO_ARM_FIRST_SAVED_REG * sizeof (mgreg_t)));
118         ARM_LDM (code, ARMREG_LR, MONO_ARM_REGSAVE_MASK);
119         /* call handler at eip (r1) and set the first arg with the exception (r2) */
120         ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
121         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
122         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
123
124         /* epilog */
125         ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
126
127         g_assert ((code - start) < 320);
128
129         mono_arch_flush_icache (start, code - start);
130
131         if (info)
132                 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
133
134         return start;
135 }
136
137 void
138 mono_arm_throw_exception (MonoObject *exc, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
139 {
140         MonoError error;
141         MonoContext ctx;
142         gboolean rethrow = sp & 1;
143
144         sp &= ~1; /* clear the optional rethrow bit */
145         pc &= ~1; /* clear the thumb bit */
146         /* adjust eip so that it point into the call instruction */
147         pc -= 4;
148
149         /*printf ("stack in throw: %p\n", esp);*/
150         MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
151         MONO_CONTEXT_SET_SP (&ctx, sp);
152         MONO_CONTEXT_SET_IP (&ctx, pc);
153         memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
154         memcpy (&ctx.fregs, fp_regs, sizeof (double) * 16);
155
156         if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
157                 MonoException *mono_ex = (MonoException*)exc;
158                 if (!rethrow) {
159                         mono_ex->stack_trace = NULL;
160                         mono_ex->trace_ips = NULL;
161                 }
162         }
163         mono_error_assert_ok (&error);
164         mono_handle_exception (&ctx, exc);
165         mono_restore_context (&ctx);
166         g_assert_not_reached ();
167 }
168
169 void
170 mono_arm_throw_exception_by_token (guint32 type_token, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
171 {
172         /* Clear thumb bit */
173         pc &= ~1;
174
175         mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, type_token), pc, sp, int_regs, fp_regs);
176 }
177
178 void
179 mono_arm_resume_unwind (guint32 dummy1, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
180 {
181         MonoContext ctx;
182
183         pc &= ~1; /* clear the optional rethrow bit */
184         /* adjust eip so that it point into the call instruction */
185         pc -= 4;
186
187         MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
188         MONO_CONTEXT_SET_SP (&ctx, sp);
189         MONO_CONTEXT_SET_IP (&ctx, pc);
190         memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
191
192         mono_resume_unwind (&ctx);
193 }
194
195 /**
196  * get_throw_trampoline:
197  *
198  * Returns a function pointer which can be used to raise 
199  * exceptions. The returned function has the following 
200  * signature: void (*func) (MonoException *exc); or
201  * void (*func) (guint32 ex_token, guint8* ip);
202  *
203  */
204 static gpointer 
205 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
206 {
207         guint8 *start;
208         guint8 *code;
209         MonoJumpInfo *ji = NULL;
210         GSList *unwind_ops = NULL;
211         int cfa_offset;
212
213         code = start = mono_global_codeman_reserve (size);
214
215         mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, 0);
216
217         /* save all the regs on the stack */
218         ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
219         ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
220
221         cfa_offset = MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t);
222         mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, cfa_offset);
223         mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_LR, - sizeof (mgreg_t));
224
225         /* Save fp regs */
226         if (!mono_arch_is_soft_float ()) {
227                 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (double) * 16);
228                 cfa_offset += sizeof (double) * 16;
229                 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
230                 ARM_FSTMD (code, ARM_VFP_D0, 16, ARMREG_SP);
231         }
232
233         /* Param area */
234         ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
235         cfa_offset += 8;
236         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
237
238         /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
239         /* caller sp */
240         ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
241         /* we encode rethrow in sp */
242         if (rethrow) {
243                 g_assert (!resume_unwind);
244                 g_assert (!corlib);
245                 ARM_ORR_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, rethrow);
246         }
247         /* exc is already in place in r0 */
248         if (corlib) {
249                 /* The caller ip is already in R1 */
250                 if (llvm) {
251                         /*
252                          * The address passed by llvm might point to before the call,
253                          * thus outside the eh range recorded by llvm. Use the return
254                          * address instead.
255                          * FIXME: Do this on more platforms.
256                          */
257                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
258                 }
259         } else {
260                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
261         }
262         /* int regs */
263         ARM_ADD_REG_IMM8 (code, ARMREG_R3, ARMREG_SP, (cfa_offset - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t))));
264         /* fp regs */
265         ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_SP, 8);
266         ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, 0);
267
268         if (aot) {
269                 const char *icall_name;
270
271                 if (resume_unwind)
272                         icall_name = "mono_arm_resume_unwind";
273                 else if (corlib)
274                         icall_name = "mono_arm_throw_exception_by_token";
275                 else
276                         icall_name = "mono_arm_throw_exception";
277
278                 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
279                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
280                 ARM_B (code, 0);
281                 *(gpointer*)(gpointer)code = NULL;
282                 code += 4;
283                 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
284         } else {
285                 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (resume_unwind ? (gpointer)mono_arm_resume_unwind : (corlib ? (gpointer)mono_arm_throw_exception_by_token : (gpointer)mono_arm_throw_exception)));
286         }
287         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
288         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
289         /* we should never reach this breakpoint */
290         ARM_DBRK (code);
291         g_assert ((code - start) < size);
292         mono_arch_flush_icache (start, code - start);
293
294         if (info)
295                 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
296
297         return start;
298 }
299
300 /**
301  * arch_get_throw_exception:
302  *
303  * Returns a function pointer which can be used to raise 
304  * exceptions. The returned function has the following 
305  * signature: void (*func) (MonoException *exc); 
306  * For example to raise an arithmetic exception you can use:
307  *
308  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
309  * x86_call_code (code, arch_get_throw_exception ()); 
310  *
311  */
312 gpointer 
313 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
314 {
315         return get_throw_trampoline (132, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
316 }
317
318 /**
319  * mono_arch_get_rethrow_exception:
320  *
321  * Returns a function pointer which can be used to rethrow 
322  * exceptions. The returned function has the following 
323  * signature: void (*func) (MonoException *exc); 
324  *
325  */
326 gpointer
327 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
328 {
329         return get_throw_trampoline (132, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
330 }
331
332 /**
333  * mono_arch_get_throw_corlib_exception:
334  *
335  * Returns a function pointer which can be used to raise 
336  * corlib exceptions. The returned function has the following 
337  * signature: void (*func) (guint32 ex_token, guint32 offset); 
338  * Here, offset is the offset which needs to be substracted from the caller IP 
339  * to get the IP of the throw. Passing the offset has the advantage that it 
340  * needs no relocations in the caller.
341  * On ARM, the ip is passed instead of an offset.
342  */
343 gpointer 
344 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
345 {
346         return get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
347 }       
348
349 GSList*
350 mono_arm_get_exception_trampolines (gboolean aot)
351 {
352         MonoTrampInfo *info;
353         GSList *tramps = NULL;
354
355         /* LLVM uses the normal trampolines, but with a different name */
356         get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
357         tramps = g_slist_prepend (tramps, info);
358         
359         get_throw_trampoline (168, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
360         tramps = g_slist_prepend (tramps, info);
361
362         get_throw_trampoline (168, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
363         tramps = g_slist_prepend (tramps, info);
364
365         return tramps;
366 }
367
368 void
369 mono_arch_exceptions_init (void)
370 {
371         guint8 *tramp;
372         GSList *tramps, *l;
373         
374         if (mono_aot_only) {
375                 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
376                 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
377                 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
378                 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
379                 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
380                 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
381         } else {
382                 tramps = mono_arm_get_exception_trampolines (FALSE);
383                 for (l = tramps; l; l = l->next) {
384                         MonoTrampInfo *info = l->data;
385
386                         mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
387                         mono_tramp_info_register (info, NULL);
388                 }
389                 g_slist_free (tramps);
390         }
391 }
392
393 /* 
394  * mono_arch_unwind_frame:
395  *
396  * See exceptions-amd64.c for docs;
397  */
398 gboolean
399 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, 
400                                                          MonoJitInfo *ji, MonoContext *ctx, 
401                                                          MonoContext *new_ctx, MonoLMF **lmf,
402                                                          mgreg_t **save_locations,
403                                                          StackFrameInfo *frame)
404 {
405         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
406
407         memset (frame, 0, sizeof (StackFrameInfo));
408         frame->ji = ji;
409
410         *new_ctx = *ctx;
411
412         if (ji != NULL) {
413                 int i;
414                 mono_unwind_reg_t regs [MONO_MAX_IREGS + 1 + 8];
415                 guint8 *cfa;
416                 guint32 unwind_info_len;
417                 guint8 *unwind_info;
418
419                 if (ji->is_trampoline)
420                         frame->type = FRAME_TYPE_TRAMPOLINE;
421                 else
422                         frame->type = FRAME_TYPE_MANAGED;
423
424                 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
425
426                 /*
427                 printf ("%s %p %p\n", ji->d.method->name, ji->code_start, ip);
428                 mono_print_unwind_info (unwind_info, unwind_info_len);
429                 */
430
431                 for (i = 0; i < 16; ++i)
432                         regs [i] = new_ctx->regs [i];
433 #ifdef TARGET_IOS
434                 /* On IOS, d8..d15 are callee saved. They are mapped to 8..15 in unwind.c */
435                 for (i = 0; i < 8; ++i)
436                         regs [MONO_MAX_IREGS + i] = *(guint64*)&(new_ctx->fregs [8 + i]);
437 #endif
438
439                 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
440                                                    (guint8*)ji->code_start + ji->code_size,
441                                                    ip, NULL, regs, MONO_MAX_IREGS + 8,
442                                                    save_locations, MONO_MAX_IREGS, &cfa);
443
444                 for (i = 0; i < 16; ++i)
445                         new_ctx->regs [i] = regs [i];
446                 new_ctx->pc = regs [ARMREG_LR];
447                 new_ctx->regs [ARMREG_SP] = (gsize)cfa;
448 #ifdef TARGET_IOS
449                 for (i = 0; i < 8; ++i)
450                         new_ctx->fregs [8 + i] = *(double*)&(regs [MONO_MAX_IREGS + i]);
451 #endif
452
453                 /* Clear thumb bit */
454                 new_ctx->pc &= ~1;
455
456                 /* we substract 1, so that the IP points into the call instruction */
457                 new_ctx->pc--;
458
459                 return TRUE;
460         } else if (*lmf) {
461
462                 if (((gsize)(*lmf)->previous_lmf) & 2) {
463                         /* 
464                          * This LMF entry is created by the soft debug code to mark transitions to
465                          * managed code done during invokes.
466                          */
467                         MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
468
469                         g_assert (ext->debugger_invoke);
470
471                         memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
472
473                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
474
475                         frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
476
477                         return TRUE;
478                 }
479
480                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
481                 
482                 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL))) {
483                         frame->ji = ji;
484                 } else {
485                         if (!(*lmf)->method)
486                                 return FALSE;
487                         frame->method = (*lmf)->method;
488                 }
489
490                 /*
491                  * The LMF is saved at the start of the method using:
492                  * ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP)
493                  * ARM_PUSH (code, 0x5ff0);
494                  * So it stores the register state as it existed at the caller. We need to
495                  * produce the register state which existed at the time of the call which
496                  * transitioned to native call, so we save the sp/fp/ip in the LMF.
497                  */
498                 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [0], sizeof (mgreg_t) * 13);
499                 new_ctx->pc = (*lmf)->ip;
500                 new_ctx->regs [ARMREG_SP] = (*lmf)->sp;
501                 new_ctx->regs [ARMREG_FP] = (*lmf)->fp;
502
503                 /* Clear thumb bit */
504                 new_ctx->pc &= ~1;
505
506                 /* we substract 1, so that the IP points into the call instruction */
507                 new_ctx->pc--;
508
509                 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
510
511                 return TRUE;
512         }
513
514         return FALSE;
515 }
516
517 /*
518  * handle_exception:
519  *
520  *   Called by resuming from a signal handler.
521  */
522 static void
523 handle_signal_exception (gpointer obj)
524 {
525         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
526         MonoContext ctx;
527
528         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
529
530         mono_handle_exception (&ctx, obj);
531
532         mono_restore_context (&ctx);
533 }
534
535 /*
536  * This works around a gcc 4.5 bug:
537  * https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/721531
538  */
539 static MONO_NEVER_INLINE gpointer
540 get_handle_signal_exception_addr (void)
541 {
542         return handle_signal_exception;
543 }
544
545 /*
546  * This is the function called from the signal handler
547  */
548 gboolean
549 mono_arch_handle_exception (void *ctx, gpointer obj)
550 {
551 #if defined(MONO_CROSS_COMPILE) || !defined(MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX)
552         g_assert_not_reached ();
553 #elif defined(MONO_ARCH_USE_SIGACTION)
554         arm_ucontext *sigctx = ctx;
555         /*
556          * Handling the exception in the signal handler is problematic, since the original
557          * signal is disabled, and we could run arbitrary code though the debugger. So
558          * resume into the normal stack and do most work there if possible.
559          */
560         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
561         guint64 sp = UCONTEXT_REG_SP (sigctx);
562
563         /* Pass the ctx parameter in TLS */
564         mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
565         /* The others in registers */
566         UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
567
568         /* Allocate a stack frame */
569         sp -= 16;
570         UCONTEXT_REG_SP (sigctx) = sp;
571
572         UCONTEXT_REG_PC (sigctx) = (gsize)get_handle_signal_exception_addr ();
573 #ifdef UCONTEXT_REG_CPSR
574         if ((gsize)UCONTEXT_REG_PC (sigctx) & 1)
575                 /* Transition to thumb */
576                 UCONTEXT_REG_CPSR (sigctx) |= (1 << 5);
577         else
578                 /* Transition to ARM */
579                 UCONTEXT_REG_CPSR (sigctx) &= ~(1 << 5);
580 #endif
581
582         return TRUE;
583 #else
584         MonoContext mctx;
585         gboolean result;
586
587         mono_sigctx_to_monoctx (ctx, &mctx);
588
589         result = mono_handle_exception (&mctx, obj);
590         /* restore the context so that returning from the signal handler will invoke
591          * the catch clause 
592          */
593         mono_monoctx_to_sigctx (&mctx, ctx);
594         return result;
595 #endif
596 }
597
598 gpointer
599 mono_arch_ip_from_context (void *sigctx)
600 {
601 #ifdef MONO_CROSS_COMPILE
602         g_assert_not_reached ();
603 #elif defined(__native_client__)
604         g_assert_not_reached ();
605 #else
606         arm_ucontext *my_uc = sigctx;
607         return (void*) UCONTEXT_REG_PC (my_uc);
608 #endif
609 }
610
611 void
612 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
613 {
614         mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
615
616         // FIXME:
617         g_assert (!user_data);
618
619         /* Allocate a stack frame */
620         sp -= 16;
621         MONO_CONTEXT_SET_SP (ctx, sp);
622
623         mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
624 }
625
626 /*
627  * mono_arch_setup_resume_sighandler_ctx:
628  *
629  *   Setup CTX so execution continues at FUNC.
630  */
631 void
632 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
633 {
634         MONO_CONTEXT_SET_IP (ctx,func);
635         if ((mgreg_t)MONO_CONTEXT_GET_IP (ctx) & 1)
636                 /* Transition to thumb */
637                 ctx->cpsr |= (1 << 5);
638         else
639                 /* Transition to ARM */
640                 ctx->cpsr &= ~(1 << 5);
641 }