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