[jit] Move a few DISABLE_JIT stubs to arch-stubs.c. (#10991)
[mono-project.git] / mono / mini / exceptions-arm.c
blobc0b709f54c46051cde4e7417da417347d24db698
1 /**
2 * \file
3 * exception support for ARM
5 * Authors:
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Paolo Molaro (lupus@ximian.com)
9 * (C) 2001 Ximian, Inc.
12 #include <config.h>
13 #include <glib.h>
14 #include <signal.h>
15 #include <string.h>
17 #ifndef MONO_CROSS_COMPILE
18 #ifdef HOST_ANDROID
19 #include <asm/sigcontext.h>
20 #endif /* def HOST_ANDROID */
21 #endif
23 #ifdef HAVE_UCONTEXT_H
24 #include <ucontext.h>
25 #endif /* def HAVE_UCONTEXT_H */
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>
37 #include "mini.h"
38 #include "mini-arm.h"
39 #include "mini-runtime.h"
40 #include "aot-runtime.h"
41 #include "mono/utils/mono-sigcontext.h"
42 #include "mono/utils/mono-compiler.h"
44 #ifndef DISABLE_JIT
47 * arch_get_restore_context:
49 * Returns a pointer to a method which restores a previously saved sigcontext.
50 * The first argument in r0 is the pointer to the context.
52 gpointer
53 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
55 guint8 *code;
56 guint8 *start;
57 int ctx_reg;
58 MonoJumpInfo *ji = NULL;
59 GSList *unwind_ops = NULL;
61 start = code = mono_global_codeman_reserve (128);
63 /*
64 * Move things to their proper place so we can restore all the registers with
65 * one instruction.
68 ctx_reg = ARMREG_R0;
70 if (!mono_arch_is_soft_float ()) {
71 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs));
72 ARM_FLDMD (code, ARM_VFP_D0, 16, ARMREG_IP);
75 /* move pc to PC */
76 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
77 ARM_STR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_PC * sizeof (mgreg_t)));
79 /* restore everything */
80 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs));
81 ARM_LDM (code, ARMREG_IP, 0xffff);
83 /* never reached */
84 ARM_DBRK (code);
86 g_assert ((code - start) < 128);
88 mono_arch_flush_icache (start, code - start);
89 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
91 if (info)
92 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
94 return start;
98 * arch_get_call_filter:
100 * Returns a pointer to a method which calls an exception filter. We
101 * also use this function to call finally handlers (we pass NULL as
102 * @exc object in this case).
104 gpointer
105 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
107 guint8 *code;
108 guint8* start;
109 int ctx_reg;
110 MonoJumpInfo *ji = NULL;
111 GSList *unwind_ops = NULL;
113 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
114 start = code = mono_global_codeman_reserve (320);
116 /* save all the regs on the stack */
117 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
118 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
120 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
122 /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
123 ctx_reg = ARMREG_R0;
124 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
125 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs) + (MONO_ARM_FIRST_SAVED_REG * sizeof (mgreg_t)));
126 ARM_LDM (code, ARMREG_LR, MONO_ARM_REGSAVE_MASK);
127 /* call handler at eip (r1) and set the first arg with the exception (r2) */
128 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
129 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
130 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
132 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
134 /* epilog */
135 ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
137 g_assert ((code - start) < 320);
139 mono_arch_flush_icache (start, code - start);
140 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
142 if (info)
143 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
145 return start;
148 #endif /* DISABLE_JIT */
150 void
151 mono_arm_throw_exception (MonoObject *exc, host_mgreg_t pc, host_mgreg_t sp, host_mgreg_t *int_regs, gdouble *fp_regs, gboolean preserve_ips)
153 ERROR_DECL (error);
154 MonoContext ctx;
155 gboolean rethrow = sp & 1;
157 sp &= ~1; /* clear the optional rethrow bit */
158 pc &= ~1; /* clear the thumb bit */
159 /* adjust eip so that it point into the call instruction */
160 pc -= 4;
162 /*printf ("stack in throw: %p\n", esp);*/
163 MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
164 MONO_CONTEXT_SET_SP (&ctx, sp);
165 MONO_CONTEXT_SET_IP (&ctx, pc);
166 memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (host_mgreg_t)), int_regs, 8 * sizeof (host_mgreg_t));
167 memcpy (&ctx.fregs, fp_regs, sizeof (double) * 16);
169 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, error)) {
170 MonoException *mono_ex = (MonoException*)exc;
171 if (!rethrow) {
172 mono_ex->stack_trace = NULL;
173 mono_ex->trace_ips = NULL;
174 } else if (preserve_ips) {
175 mono_ex->caught_in_unmanaged = TRUE;
178 mono_error_assert_ok (error);
179 mono_handle_exception (&ctx, exc);
180 mono_restore_context (&ctx);
181 g_assert_not_reached ();
184 void
185 mono_arm_throw_exception_by_token (guint32 ex_token_index, host_mgreg_t pc, host_mgreg_t sp, host_mgreg_t *int_regs, gdouble *fp_regs)
187 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
188 /* Clear thumb bit */
189 pc &= ~1;
191 mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token), pc, sp, int_regs, fp_regs, FALSE);
194 void
195 mono_arm_resume_unwind (guint32 dummy1, host_mgreg_t pc, host_mgreg_t sp, host_mgreg_t *int_regs, gdouble *fp_regs)
197 MonoContext ctx;
199 pc &= ~1; /* clear the optional rethrow bit */
200 /* adjust eip so that it point into the call instruction */
201 pc -= 4;
203 MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
204 MONO_CONTEXT_SET_SP (&ctx, sp);
205 MONO_CONTEXT_SET_IP (&ctx, pc);
206 memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (host_mgreg_t)), int_regs, 8 * sizeof (host_mgreg_t));
208 mono_resume_unwind (&ctx);
211 #ifndef DISABLE_JIT
214 * get_throw_trampoline:
216 * Returns a function pointer which can be used to raise
217 * exceptions. The returned function has the following
218 * signature: void (*func) (MonoException *exc); or
219 * void (*func) (guint32 ex_token, guint8* ip);
222 static gpointer
223 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot, gboolean preserve_ips)
225 guint8 *start;
226 guint8 *code;
227 MonoJumpInfo *ji = NULL;
228 GSList *unwind_ops = NULL;
229 int cfa_offset;
231 code = start = mono_global_codeman_reserve (size);
233 mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, 0);
235 /* save all the regs on the stack */
236 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
237 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
239 cfa_offset = MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t);
240 mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, cfa_offset);
241 mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_LR, - sizeof (mgreg_t));
243 /* Save fp regs */
244 if (!mono_arch_is_soft_float ()) {
245 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (double) * 16);
246 cfa_offset += sizeof (double) * 16;
247 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
248 ARM_FSTMD (code, ARM_VFP_D0, 16, ARMREG_SP);
251 /* Param area */
252 int param_size = 8;
253 if (!resume_unwind && !corlib)
254 param_size += 4; // Extra arg
255 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, param_size);
256 cfa_offset += param_size;
257 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
259 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
260 /* caller sp */
261 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
262 /* we encode rethrow in sp */
263 if (rethrow) {
264 g_assert (!resume_unwind);
265 g_assert (!corlib);
266 ARM_ORR_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, rethrow);
268 /* exc is already in place in r0 */
269 if (corlib) {
270 /* The caller ip is already in R1 */
271 if (llvm) {
273 * The address passed by llvm might point to before the call,
274 * thus outside the eh range recorded by llvm. Use the return
275 * address instead.
276 * FIXME: Do this on more platforms.
278 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
280 } else {
281 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
283 /* int regs */
284 ARM_ADD_REG_IMM8 (code, ARMREG_R3, ARMREG_SP, (cfa_offset - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t))));
285 if (resume_unwind || corlib) {
286 /* fp regs */
287 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_SP, 8);
288 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, 0);
289 } else {
290 /* preserve_ips */
291 ARM_MOV_REG_IMM8 (code, ARMREG_R5, preserve_ips);
292 ARM_STR_IMM (code, ARMREG_R5, ARMREG_SP, 4);
294 /* fp regs */
295 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_SP, 8);
296 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, 0);
299 if (aot) {
300 const char *icall_name;
302 if (resume_unwind)
303 icall_name = "mono_arm_resume_unwind";
304 else if (corlib)
305 icall_name = "mono_arm_throw_exception_by_token";
306 else
307 icall_name = "mono_arm_throw_exception";
309 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
310 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
311 ARM_B (code, 0);
312 *(gpointer*)(gpointer)code = NULL;
313 code += 4;
314 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
315 } else {
316 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)));
318 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
319 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
320 /* we should never reach this breakpoint */
321 ARM_DBRK (code);
322 g_assert ((code - start) < size);
323 mono_arch_flush_icache (start, code - start);
324 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
326 if (info)
327 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
329 return start;
333 * arch_get_throw_exception:
335 * Returns a function pointer which can be used to raise
336 * exceptions. The returned function has the following
337 * signature: void (*func) (MonoException *exc);
338 * For example to raise an arithmetic exception you can use:
340 * x86_push_imm (code, mono_get_exception_arithmetic ());
341 * x86_call_code (code, arch_get_throw_exception ());
344 gpointer
345 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
347 return get_throw_trampoline (132, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot, FALSE);
351 * mono_arch_get_rethrow_exception:
353 * Returns a function pointer which can be used to rethrow
354 * exceptions. The returned function has the following
355 * signature: void (*func) (MonoException *exc);
358 gpointer
359 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
361 return get_throw_trampoline (132, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot, FALSE);
364 gpointer
365 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo **info, gboolean aot)
367 return get_throw_trampoline (132, FALSE, TRUE, FALSE, FALSE, "rethrow_preserve_exception", info, aot, TRUE);
371 * mono_arch_get_throw_corlib_exception:
372 * \returns a function pointer which can be used to raise
373 * corlib exceptions. The returned function has the following
374 * signature: void (*func) (guint32 ex_token, guint32 offset);
375 * Here, \c offset is the offset which needs to be substracted from the caller IP
376 * to get the IP of the throw. Passing the offset has the advantage that it
377 * needs no relocations in the caller.
378 * On ARM, the ip is passed instead of an offset.
380 gpointer
381 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
383 return get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot, FALSE);
386 GSList*
387 mono_arm_get_exception_trampolines (gboolean aot)
389 MonoTrampInfo *info;
390 GSList *tramps = NULL;
392 /* LLVM uses the normal trampolines, but with a different name */
393 get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot, FALSE);
394 tramps = g_slist_prepend (tramps, info);
396 get_throw_trampoline (168, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot, FALSE);
397 tramps = g_slist_prepend (tramps, info);
399 get_throw_trampoline (168, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot, FALSE);
400 tramps = g_slist_prepend (tramps, info);
402 return tramps;
405 #else
407 GSList*
408 mono_arm_get_exception_trampolines (gboolean aot)
410 g_assert_not_reached ();
411 return NULL;
414 #endif
416 void
417 mono_arch_exceptions_init (void)
419 gpointer tramp;
420 GSList *tramps, *l;
422 if (mono_aot_only) {
423 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
424 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
425 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
426 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
427 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
428 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
429 } else {
430 tramps = mono_arm_get_exception_trampolines (FALSE);
431 for (l = tramps; l; l = l->next) {
432 MonoTrampInfo *info = (MonoTrampInfo*)l->data;
434 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
435 mono_tramp_info_register (info, NULL);
437 g_slist_free (tramps);
442 * mono_arch_unwind_frame:
444 * See exceptions-amd64.c for docs;
446 gboolean
447 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
448 MonoJitInfo *ji, MonoContext *ctx,
449 MonoContext *new_ctx, MonoLMF **lmf,
450 host_mgreg_t **save_locations,
451 StackFrameInfo *frame)
453 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
455 memset (frame, 0, sizeof (StackFrameInfo));
456 frame->ji = ji;
458 *new_ctx = *ctx;
460 if (ji != NULL) {
461 int i;
462 mono_unwind_reg_t regs [MONO_MAX_IREGS + 1 + 8];
463 guint8 *cfa;
464 guint32 unwind_info_len;
465 guint8 *unwind_info;
467 if (ji->is_trampoline)
468 frame->type = FRAME_TYPE_TRAMPOLINE;
469 else
470 frame->type = FRAME_TYPE_MANAGED;
472 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
475 printf ("%s %p %p\n", ji->d.method->name, ji->code_start, ip);
476 mono_print_unwind_info (unwind_info, unwind_info_len);
479 for (i = 0; i < 16; ++i)
480 regs [i] = new_ctx->regs [i];
481 #ifdef TARGET_IOS
482 /* On IOS, d8..d15 are callee saved. They are mapped to 8..15 in unwind.c */
483 for (i = 0; i < 8; ++i)
484 regs [MONO_MAX_IREGS + i] = *(guint64*)&(new_ctx->fregs [8 + i]);
485 #endif
487 mono_unwind_frame (unwind_info, unwind_info_len, (guint8*)ji->code_start,
488 (guint8*)ji->code_start + ji->code_size,
489 (guint8*)ip, NULL, regs, MONO_MAX_IREGS + 8,
490 save_locations, MONO_MAX_IREGS, &cfa);
492 for (i = 0; i < 16; ++i)
493 new_ctx->regs [i] = regs [i];
494 new_ctx->pc = regs [ARMREG_LR];
495 new_ctx->regs [ARMREG_SP] = (gsize)cfa;
496 #ifdef TARGET_IOS
497 for (i = 0; i < 8; ++i)
498 new_ctx->fregs [8 + i] = *(double*)&(regs [MONO_MAX_IREGS + i]);
499 #endif
501 /* Clear thumb bit */
502 new_ctx->pc &= ~1;
504 /* we substract 1, so that the IP points into the call instruction */
505 new_ctx->pc--;
507 return TRUE;
508 } else if (*lmf) {
509 g_assert ((((guint64)(*lmf)->previous_lmf) & 2) == 0);
511 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
513 if ((ji = mini_jit_info_table_find (domain, (gpointer)(gsize)(*lmf)->ip, NULL))) {
514 frame->ji = ji;
515 } else {
516 if (!(*lmf)->method)
517 return FALSE;
518 frame->method = (*lmf)->method;
522 * The LMF is saved at the start of the method using:
523 * ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP)
524 * ARM_PUSH (code, 0x5ff0);
525 * So it stores the register state as it existed at the caller. We need to
526 * produce the register state which existed at the time of the call which
527 * transitioned to native call, so we save the sp/fp/ip in the LMF.
529 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [0], sizeof (host_mgreg_t) * 13);
530 new_ctx->pc = (*lmf)->ip;
531 new_ctx->regs [ARMREG_SP] = (*lmf)->sp;
532 new_ctx->regs [ARMREG_FP] = (*lmf)->fp;
534 /* Clear thumb bit */
535 new_ctx->pc &= ~1;
537 /* we substract 1, so that the IP points into the call instruction */
538 new_ctx->pc--;
540 *lmf = (MonoLMF*)(((gsize)(*lmf)->previous_lmf) & ~3);
542 return TRUE;
545 return FALSE;
549 * handle_exception:
551 * Called by resuming from a signal handler.
553 static void
554 handle_signal_exception (gpointer obj)
556 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
557 MonoContext ctx;
559 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
561 mono_handle_exception (&ctx, (MonoObject*)obj);
563 mono_restore_context (&ctx);
567 * This works around a gcc 4.5 bug:
568 * https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/721531
570 static MONO_NEVER_INLINE gpointer
571 get_handle_signal_exception_addr (void)
573 return (gpointer)handle_signal_exception;
577 * This is the function called from the signal handler
579 gboolean
580 mono_arch_handle_exception (void *ctx, gpointer obj)
582 #if defined(MONO_CROSS_COMPILE) || !defined(MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX)
583 g_assert_not_reached ();
584 #elif defined(MONO_ARCH_USE_SIGACTION)
585 arm_ucontext *sigctx = (arm_ucontext*)ctx;
587 * Handling the exception in the signal handler is problematic, since the original
588 * signal is disabled, and we could run arbitrary code though the debugger. So
589 * resume into the normal stack and do most work there if possible.
591 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
592 guint64 sp = UCONTEXT_REG_SP (sigctx);
594 /* Pass the ctx parameter in TLS */
595 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
596 /* The others in registers */
597 UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
599 /* Allocate a stack frame */
600 sp -= 16;
601 UCONTEXT_REG_SP (sigctx) = sp;
603 UCONTEXT_REG_PC (sigctx) = (gsize)get_handle_signal_exception_addr ();
604 #ifdef UCONTEXT_REG_CPSR
605 if ((gsize)UCONTEXT_REG_PC (sigctx) & 1)
606 /* Transition to thumb */
607 UCONTEXT_REG_CPSR (sigctx) |= (1 << 5);
608 else
609 /* Transition to ARM */
610 UCONTEXT_REG_CPSR (sigctx) &= ~(1 << 5);
611 #endif
613 return TRUE;
614 #else
615 MonoContext mctx;
616 gboolean result;
618 mono_sigctx_to_monoctx (ctx, &mctx);
620 result = mono_handle_exception (&mctx, obj);
621 /* restore the context so that returning from the signal handler will invoke
622 * the catch clause
624 mono_monoctx_to_sigctx (&mctx, ctx);
625 return result;
626 #endif
629 gpointer
630 mono_arch_ip_from_context (void *sigctx)
632 #ifdef MONO_CROSS_COMPILE
633 g_assert_not_reached ();
634 #else
635 arm_ucontext *my_uc = (arm_ucontext*)sigctx;
636 return (void*) UCONTEXT_REG_PC (my_uc);
637 #endif
640 void
641 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
643 host_mgreg_t sp = (host_mgreg_t)MONO_CONTEXT_GET_SP (ctx);
645 // FIXME:
646 g_assert (!user_data);
648 /* Allocate a stack frame */
649 sp -= 16;
650 MONO_CONTEXT_SET_SP (ctx, sp);
652 mono_arch_setup_resume_sighandler_ctx (ctx, (gpointer)async_cb);
656 * mono_arch_setup_resume_sighandler_ctx:
658 * Setup CTX so execution continues at FUNC.
660 void
661 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
663 MONO_CONTEXT_SET_IP (ctx,func);
664 if ((host_mgreg_t)MONO_CONTEXT_GET_IP (ctx) & 1)
665 /* Transition to thumb */
666 ctx->cpsr |= (1 << 5);
667 else
668 /* Transition to ARM */
669 ctx->cpsr &= ~(1 << 5);
672 void
673 mono_arch_undo_ip_adjustment (MonoContext *ctx)
675 ctx->pc++;
677 if (mono_arm_thumb_supported ())
678 ctx->pc |= 1;
681 void
682 mono_arch_do_ip_adjustment (MonoContext *ctx)
684 /* Clear thumb bit */
685 ctx->pc &= ~1;
687 ctx->pc--;