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