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