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