Merge pull request #1668 from alexanderkyte/bug1856
[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         MonoContext ctx;
141         gboolean rethrow = sp & 1;
142
143         sp &= ~1; /* clear the optional rethrow bit */
144         pc &= ~1; /* clear the thumb bit */
145         /* adjust eip so that it point into the call instruction */
146         pc -= 4;
147
148         /*printf ("stack in throw: %p\n", esp);*/
149         MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
150         MONO_CONTEXT_SET_SP (&ctx, sp);
151         MONO_CONTEXT_SET_IP (&ctx, pc);
152         memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
153         memcpy (&ctx.fregs, fp_regs, sizeof (double) * 16);
154
155         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
156                 MonoException *mono_ex = (MonoException*)exc;
157                 if (!rethrow) {
158                         mono_ex->stack_trace = NULL;
159                         mono_ex->trace_ips = NULL;
160                 }
161         }
162         mono_handle_exception (&ctx, exc);
163         mono_restore_context (&ctx);
164         g_assert_not_reached ();
165 }
166
167 void
168 mono_arm_throw_exception_by_token (guint32 type_token, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
169 {
170         /* Clear thumb bit */
171         pc &= ~1;
172
173         mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, type_token), pc, sp, int_regs, fp_regs);
174 }
175
176 void
177 mono_arm_resume_unwind (guint32 dummy1, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
178 {
179         MonoContext ctx;
180
181         pc &= ~1; /* clear the optional rethrow bit */
182         /* adjust eip so that it point into the call instruction */
183         pc -= 4;
184
185         MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
186         MONO_CONTEXT_SET_SP (&ctx, sp);
187         MONO_CONTEXT_SET_IP (&ctx, pc);
188         memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
189
190         mono_resume_unwind (&ctx);
191 }
192
193 /**
194  * get_throw_trampoline:
195  *
196  * Returns a function pointer which can be used to raise 
197  * exceptions. The returned function has the following 
198  * signature: void (*func) (MonoException *exc); or
199  * void (*func) (guint32 ex_token, guint8* ip);
200  *
201  */
202 static gpointer 
203 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
204 {
205         guint8 *start;
206         guint8 *code;
207         MonoJumpInfo *ji = NULL;
208         GSList *unwind_ops = NULL;
209         int cfa_offset;
210
211         code = start = mono_global_codeman_reserve (size);
212
213         mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, 0);
214
215         /* save all the regs on the stack */
216         ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
217         ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
218
219         cfa_offset = MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t);
220         mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, cfa_offset);
221         mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_LR, - sizeof (mgreg_t));
222
223         /* Save fp regs */
224         if (!mono_arch_is_soft_float ()) {
225                 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (double) * 16);
226                 cfa_offset += sizeof (double) * 16;
227                 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
228                 ARM_FSTMD (code, ARM_VFP_D0, 16, ARMREG_SP);
229         }
230
231         /* Param area */
232         ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
233         cfa_offset += 8;
234         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
235
236         /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
237         /* caller sp */
238         ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
239         /* we encode rethrow in sp */
240         if (rethrow) {
241                 g_assert (!resume_unwind);
242                 g_assert (!corlib);
243                 ARM_ORR_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, rethrow);
244         }
245         /* exc is already in place in r0 */
246         if (corlib) {
247                 /* The caller ip is already in R1 */
248                 if (llvm)
249                         /* Negate the ip adjustment done in mono_arm_throw_exception */
250                         ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, 4);
251         } else {
252                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
253         }
254         /* int regs */
255         ARM_ADD_REG_IMM8 (code, ARMREG_R3, ARMREG_SP, (cfa_offset - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t))));
256         /* fp regs */
257         ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_SP, 8);
258         ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, 0);
259
260         if (aot) {
261                 const char *icall_name;
262
263                 if (resume_unwind)
264                         icall_name = "mono_arm_resume_unwind";
265                 else if (corlib)
266                         icall_name = "mono_arm_throw_exception_by_token";
267                 else
268                         icall_name = "mono_arm_throw_exception";
269
270                 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
271                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
272                 ARM_B (code, 0);
273                 *(gpointer*)(gpointer)code = NULL;
274                 code += 4;
275                 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
276         } else {
277                 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)));
278         }
279         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
280         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
281         /* we should never reach this breakpoint */
282         ARM_DBRK (code);
283         g_assert ((code - start) < size);
284         mono_arch_flush_icache (start, code - start);
285
286         if (info)
287                 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
288
289         return start;
290 }
291
292 /**
293  * arch_get_throw_exception:
294  *
295  * Returns a function pointer which can be used to raise 
296  * exceptions. The returned function has the following 
297  * signature: void (*func) (MonoException *exc); 
298  * For example to raise an arithmetic exception you can use:
299  *
300  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
301  * x86_call_code (code, arch_get_throw_exception ()); 
302  *
303  */
304 gpointer 
305 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
306 {
307         return get_throw_trampoline (132, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
308 }
309
310 /**
311  * mono_arch_get_rethrow_exception:
312  *
313  * Returns a function pointer which can be used to rethrow 
314  * exceptions. The returned function has the following 
315  * signature: void (*func) (MonoException *exc); 
316  *
317  */
318 gpointer
319 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
320 {
321         return get_throw_trampoline (132, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
322 }
323
324 /**
325  * mono_arch_get_throw_corlib_exception:
326  *
327  * Returns a function pointer which can be used to raise 
328  * corlib exceptions. The returned function has the following 
329  * signature: void (*func) (guint32 ex_token, guint32 offset); 
330  * Here, offset is the offset which needs to be substracted from the caller IP 
331  * to get the IP of the throw. Passing the offset has the advantage that it 
332  * needs no relocations in the caller.
333  * On ARM, the ip is passed instead of an offset.
334  */
335 gpointer 
336 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
337 {
338         return get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
339 }       
340
341 GSList*
342 mono_arm_get_exception_trampolines (gboolean aot)
343 {
344         MonoTrampInfo *info;
345         GSList *tramps = NULL;
346
347         /* LLVM uses the normal trampolines, but with a different name */
348         get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
349         tramps = g_slist_prepend (tramps, info);
350         
351         get_throw_trampoline (168, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
352         tramps = g_slist_prepend (tramps, info);
353
354         get_throw_trampoline (168, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
355         tramps = g_slist_prepend (tramps, info);
356
357         return tramps;
358 }
359
360 void
361 mono_arch_exceptions_init (void)
362 {
363         guint8 *tramp;
364         GSList *tramps, *l;
365         
366         if (mono_aot_only) {
367                 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
368                 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
369                 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
370                 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
371                 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
372                 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
373         } else {
374                 tramps = mono_arm_get_exception_trampolines (FALSE);
375                 for (l = tramps; l; l = l->next) {
376                         MonoTrampInfo *info = l->data;
377
378                         mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
379                         mono_tramp_info_register (info);
380                 }
381                 g_slist_free (tramps);
382         }
383 }
384
385 /* 
386  * mono_arch_find_jit_info:
387  *
388  * See exceptions-amd64.c for docs;
389  */
390 gboolean
391 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
392                                                          MonoJitInfo *ji, MonoContext *ctx, 
393                                                          MonoContext *new_ctx, MonoLMF **lmf,
394                                                          mgreg_t **save_locations,
395                                                          StackFrameInfo *frame)
396 {
397         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
398
399         memset (frame, 0, sizeof (StackFrameInfo));
400         frame->ji = ji;
401
402         *new_ctx = *ctx;
403
404         if (ji != NULL) {
405                 int i;
406                 gssize regs [MONO_MAX_IREGS + 1 + 8];
407                 guint8 *cfa;
408                 guint32 unwind_info_len;
409                 guint8 *unwind_info;
410
411                 frame->type = FRAME_TYPE_MANAGED;
412
413                 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
414
415                 /*
416                 printf ("%s %p %p\n", ji->d.method->name, ji->code_start, ip);
417                 mono_print_unwind_info (unwind_info, unwind_info_len);
418                 */
419
420                 for (i = 0; i < 16; ++i)
421                         regs [i] = new_ctx->regs [i];
422 #ifdef TARGET_IOS
423                 /* On IOS, d8..d15 are callee saved. They are mapped to 8..15 in unwind.c */
424                 for (i = 0; i < 8; ++i)
425                         regs [MONO_MAX_IREGS + i] = new_ctx->fregs [8 + i];
426 #endif
427
428                 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
429                                                    (guint8*)ji->code_start + ji->code_size,
430                                                    ip, NULL, regs, MONO_MAX_IREGS + 8,
431                                                    save_locations, MONO_MAX_IREGS, &cfa);
432
433                 for (i = 0; i < 16; ++i)
434                         new_ctx->regs [i] = regs [i];
435                 new_ctx->pc = regs [ARMREG_LR];
436                 new_ctx->regs [ARMREG_SP] = (gsize)cfa;
437 #ifdef TARGET_IOS
438                 for (i = 0; i < 8; ++i)
439                         new_ctx->fregs [8 + i] = regs [MONO_MAX_IREGS + i];
440 #endif
441
442                 /* Clear thumb bit */
443                 new_ctx->pc &= ~1;
444
445                 /* we substract 1, so that the IP points into the call instruction */
446                 new_ctx->pc--;
447
448                 return TRUE;
449         } else if (*lmf) {
450
451                 if (((gsize)(*lmf)->previous_lmf) & 2) {
452                         /* 
453                          * This LMF entry is created by the soft debug code to mark transitions to
454                          * managed code done during invokes.
455                          */
456                         MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
457
458                         g_assert (ext->debugger_invoke);
459
460                         memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
461
462                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
463
464                         frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
465
466                         return TRUE;
467                 }
468
469                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
470                 
471                 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL))) {
472                         frame->ji = ji;
473                 } else {
474                         if (!(*lmf)->method)
475                                 return FALSE;
476                         frame->method = (*lmf)->method;
477                 }
478
479                 /*
480                  * The LMF is saved at the start of the method using:
481                  * ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP)
482                  * ARM_PUSH (code, 0x5ff0);
483                  * So it stores the register state as it existed at the caller. We need to
484                  * produce the register state which existed at the time of the call which
485                  * transitioned to native call, so we save the sp/fp/ip in the LMF.
486                  */
487                 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [0], sizeof (mgreg_t) * 13);
488                 new_ctx->pc = (*lmf)->ip;
489                 new_ctx->regs [ARMREG_SP] = (*lmf)->sp;
490                 new_ctx->regs [ARMREG_FP] = (*lmf)->fp;
491
492                 /* Clear thumb bit */
493                 new_ctx->pc &= ~1;
494
495                 /* we substract 1, so that the IP points into the call instruction */
496                 new_ctx->pc--;
497
498                 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
499
500                 return TRUE;
501         }
502
503         return FALSE;
504 }
505
506 /*
507  * handle_exception:
508  *
509  *   Called by resuming from a signal handler.
510  */
511 static void
512 handle_signal_exception (gpointer obj)
513 {
514         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
515         MonoContext ctx;
516
517         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
518
519         mono_handle_exception (&ctx, obj);
520
521         mono_restore_context (&ctx);
522 }
523
524 /*
525  * This works around a gcc 4.5 bug:
526  * https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/721531
527  */
528 static MONO_NEVER_INLINE gpointer
529 get_handle_signal_exception_addr (void)
530 {
531         return handle_signal_exception;
532 }
533
534 /*
535  * This is the function called from the signal handler
536  */
537 gboolean
538 mono_arch_handle_exception (void *ctx, gpointer obj)
539 {
540 #if defined(MONO_CROSS_COMPILE) || !defined(MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX)
541         g_assert_not_reached ();
542 #elif defined(MONO_ARCH_USE_SIGACTION)
543         arm_ucontext *sigctx = ctx;
544         /*
545          * Handling the exception in the signal handler is problematic, since the original
546          * signal is disabled, and we could run arbitrary code though the debugger. So
547          * resume into the normal stack and do most work there if possible.
548          */
549         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
550         guint64 sp = UCONTEXT_REG_SP (sigctx);
551
552         /* Pass the ctx parameter in TLS */
553         mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
554         /* The others in registers */
555         UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
556
557         /* Allocate a stack frame */
558         sp -= 16;
559         UCONTEXT_REG_SP (sigctx) = sp;
560
561         UCONTEXT_REG_PC (sigctx) = (gsize)get_handle_signal_exception_addr ();
562 #ifdef UCONTEXT_REG_CPSR
563         if ((gsize)UCONTEXT_REG_PC (sigctx) & 1)
564                 /* Transition to thumb */
565                 UCONTEXT_REG_CPSR (sigctx) |= (1 << 5);
566         else
567                 /* Transition to ARM */
568                 UCONTEXT_REG_CPSR (sigctx) &= ~(1 << 5);
569 #endif
570
571         return TRUE;
572 #else
573         MonoContext mctx;
574         gboolean result;
575
576         mono_sigctx_to_monoctx (ctx, &mctx);
577
578         result = mono_handle_exception (&mctx, obj);
579         /* restore the context so that returning from the signal handler will invoke
580          * the catch clause 
581          */
582         mono_monoctx_to_sigctx (&mctx, ctx);
583         return result;
584 #endif
585 }
586
587 gpointer
588 mono_arch_ip_from_context (void *sigctx)
589 {
590 #ifdef MONO_CROSS_COMPILE
591         g_assert_not_reached ();
592 #elif defined(__native_client__)
593         g_assert_not_reached ();
594 #else
595         arm_ucontext *my_uc = sigctx;
596         return (void*) UCONTEXT_REG_PC (my_uc);
597 #endif
598 }
599
600 void
601 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
602 {
603         mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
604
605         // FIXME:
606         g_assert (!user_data);
607
608         /* Allocate a stack frame */
609         sp -= 16;
610         MONO_CONTEXT_SET_SP (ctx, sp);
611
612         mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
613 }
614
615 /*
616  * mono_arch_setup_resume_sighandler_ctx:
617  *
618  *   Setup CTX so execution continues at FUNC.
619  */
620 void
621 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
622 {
623         MONO_CONTEXT_SET_IP (ctx,func);
624         if ((mgreg_t)MONO_CONTEXT_GET_IP (ctx) & 1)
625                 /* Transition to thumb */
626                 ctx->cpsr |= (1 << 5);
627         else
628                 /* Transition to ARM */
629                 ctx->cpsr &= ~(1 << 5);
630 }