Fix typo in GC_compare_and_exchange
[mono.git] / mono / mini / exceptions-arm.c
blob94a0f3a34a2694cb63622f0c2879c0568dac1cd2
1 /*
2 * exceptions-arm.c: exception support for ARM
4 * Authors:
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001 Ximian, Inc.
9 */
11 #include <config.h>
12 #include <glib.h>
13 #include <signal.h>
14 #include <string.h>
15 #include <ucontext.h>
17 #include <mono/arch/arm/arm-codegen.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/mono-debug.h>
25 #include "mini.h"
26 #include "mini-arm.h"
30 struct sigcontext {
31 unsigned long trap_no;
32 unsigned long error_code;
33 unsigned long oldmask;
34 unsigned long arm_r0;
35 unsigned long arm_r1;
36 unsigned long arm_r2;
37 unsigned long arm_r3;
38 unsigned long arm_r4;
39 unsigned long arm_r5;
40 unsigned long arm_r6;
41 unsigned long arm_r7;
42 unsigned long arm_r8;
43 unsigned long arm_r9;
44 unsigned long arm_r10;
45 unsigned long arm_fp;
46 unsigned long arm_ip;
47 unsigned long arm_sp;
48 unsigned long arm_lr;
49 unsigned long arm_pc;
50 unsigned long arm_cpsr;
51 unsigned long fault_address;
54 gregs below is this struct
55 struct user_regs {
56 unsigned long int uregs[18];
59 the companion user_fpregs has just 8 double registers
60 (it's valid for FPA mode, will need changes for VFP)
62 typedef struct {
63 gregset_t gregs;
64 fpregset_t fpregs;
65 } mcontext_t;
67 typedef struct ucontext {
68 unsigned long int uc_flags;
69 struct ucontext *uc_link;
70 __sigset_t uc_sigmask;
71 stack_t uc_stack;
72 mcontext_t uc_mcontext;
73 long int uc_filler[5];
74 } ucontext_t;
79 * So, it turns out that the ucontext struct defined by libc is incorrect.
80 * We define our own version here and use it instead.
83 #if __APPLE__
84 #define my_ucontext ucontext_t
85 #else
86 typedef struct my_ucontext {
87 unsigned long uc_flags;
88 struct my_ucontext *uc_link;
89 struct {
90 void *p;
91 int flags;
92 size_t size;
93 } sstack_data;
94 struct sigcontext sig_ctx;
95 /* some 2.6.x kernel has fp data here after a few other fields
96 * we don't use them for now...
98 } my_ucontext;
99 #endif
101 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
102 ARM_LDR_IMM (code, ip_reg, ctx_reg, G_STRUCT_OFFSET (MonoContext, eip)); \
103 ARM_ADD_REG_IMM8 (code, tmp_reg, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs)); \
104 ARM_LDMIA (code, tmp_reg, MONO_ARM_REGSAVE_MASK); \
105 } while (0)
107 /* nothing to do */
108 #define setup_context(ctx)
111 * arch_get_restore_context:
113 * Returns a pointer to a method which restores a previously saved sigcontext.
114 * The first argument in r0 is the pointer to the context.
116 gpointer
117 mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
119 guint8 *code;
120 guint8 *start;
122 *ji = NULL;
124 start = code = mono_global_codeman_reserve (128);
126 restore_regs_from_context (ARMREG_R0, ARMREG_R1, ARMREG_R2);
127 /* restore also the stack pointer, FIXME: handle sp != fp */
128 ARM_LDR_IMM (code, ARMREG_SP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, ebp));
129 ARM_LDR_IMM (code, ARMREG_FP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, esp));
131 /* jump to the saved IP */
132 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
133 /* never reached */
134 ARM_DBRK (code);
136 g_assert ((code - start) < 128);
138 mono_arch_flush_icache (start, code - start);
140 *code_size = code - start;
142 return start;
146 * arch_get_call_filter:
148 * Returns a pointer to a method which calls an exception filter. We
149 * also use this function to call finally handlers (we pass NULL as
150 * @exc object in this case).
152 gpointer
153 mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
155 guint8 *code;
156 guint8* start;
158 *ji = NULL;
160 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
161 start = code = mono_global_codeman_reserve (320);
163 /* save all the regs on the stack */
164 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
165 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
167 /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
168 restore_regs_from_context (ARMREG_R0, ARMREG_IP, ARMREG_LR);
169 /* call handler at eip (r1) and set the first arg with the exception (r2) */
170 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
171 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
172 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
174 /* epilog */
175 ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
177 g_assert ((code - start) < 320);
179 mono_arch_flush_icache (start, code - start);
181 *code_size = code - start;
183 return start;
186 void
187 mono_arm_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
189 static void (*restore_context) (MonoContext *);
190 MonoContext ctx;
191 gboolean rethrow = eip & 1;
193 if (!restore_context)
194 restore_context = mono_get_restore_context ();
196 eip &= ~1; /* clear the optional rethrow bit */
197 /* adjust eip so that it point into the call instruction */
198 eip -= 4;
200 setup_context (&ctx);
202 /*printf ("stack in throw: %p\n", esp);*/
203 MONO_CONTEXT_SET_BP (&ctx, esp);
204 MONO_CONTEXT_SET_SP (&ctx, esp);
205 MONO_CONTEXT_SET_IP (&ctx, eip);
206 memcpy (&ctx.regs, int_regs, sizeof (gulong) * 8);
207 /* memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS); */
209 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
210 MonoException *mono_ex = (MonoException*)exc;
211 if (!rethrow)
212 mono_ex->stack_trace = NULL;
214 mono_handle_exception (&ctx, exc, (gpointer)(eip + 4), FALSE);
215 restore_context (&ctx);
216 g_assert_not_reached ();
219 void
220 mono_arm_throw_exception_by_token (guint32 type_token, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
222 mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, type_token), eip, esp, int_regs, fp_regs);
226 * arch_get_throw_exception_generic:
228 * Returns a function pointer which can be used to raise
229 * exceptions. The returned function has the following
230 * signature: void (*func) (MonoException *exc); or
231 * void (*func) (guint32 ex_token, guint8* ip);
234 static gpointer
235 mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
237 guint8 *start;
238 guint8 *code;
240 *ji = NULL;
242 code = start = mono_global_codeman_reserve (size);
244 /* save all the regs on the stack */
245 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
246 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
248 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
249 /* caller sp */
250 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, 10 * 4); /* 10 saved regs */
251 /* exc is already in place in r0 */
252 if (by_token) {
253 /* The caller ip is already in R1 */
254 } else {
255 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
257 /* FIXME: pointer to the saved fp regs */
258 /*pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
259 ppc_addi (code, ppc_r7, ppc_sp, pos);*/
260 /* pointer to the saved int regs */
261 ARM_MOV_REG_REG (code, ARMREG_R3, ARMREG_SP); /* the pushed regs */
262 /* we encode rethrow in the ip, so we avoid args on the stack */
263 ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
265 if (aot) {
266 *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, by_token ? "mono_arm_throw_exception_by_token" : "mono_arm_throw_exception");
267 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
268 ARM_B (code, 0);
269 *(gpointer*)(gpointer)code = NULL;
270 code += 4;
271 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
272 } else {
273 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (by_token ? (gpointer)mono_arm_throw_exception_by_token : (gpointer)mono_arm_throw_exception));
275 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
276 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
277 /* we should never reach this breakpoint */
278 ARM_DBRK (code);
279 g_assert ((code - start) < size);
280 mono_arch_flush_icache (start, code - start);
282 *code_size = code - start;
284 return start;
288 * mono_arch_get_rethrow_exception:
290 * Returns a function pointer which can be used to rethrow
291 * exceptions. The returned function has the following
292 * signature: void (*func) (MonoException *exc);
295 gpointer
296 mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
298 return mono_arch_get_throw_exception_generic (132, FALSE, TRUE, code_size, ji, aot);
302 * arch_get_throw_exception:
304 * Returns a function pointer which can be used to raise
305 * exceptions. The returned function has the following
306 * signature: void (*func) (MonoException *exc);
307 * For example to raise an arithmetic exception you can use:
309 * x86_push_imm (code, mono_get_exception_arithmetic ());
310 * x86_call_code (code, arch_get_throw_exception ());
313 gpointer
314 mono_arch_get_throw_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
316 return mono_arch_get_throw_exception_generic (132, FALSE, FALSE, code_size, ji, aot);
319 gpointer
320 mono_arch_get_throw_exception_by_name_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
322 guint8* start;
323 guint8 *code;
325 *ji = NULL;
327 start = code = mono_global_codeman_reserve (64);
329 /* Not used on ARM */
330 ARM_DBRK (code);
332 *code_size = code - start;
334 return start;
338 * 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, 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.
348 gpointer
349 mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
351 return mono_arch_get_throw_exception_generic (168, TRUE, FALSE, code_size, ji, aot);
354 /* mono_arch_find_jit_info:
356 * This function is used to gather information from @ctx. It return the
357 * MonoJitInfo of the corresponding function, unwinds one stack frame and
358 * stores the resulting context into @new_ctx. It also stores a string
359 * describing the stack location into @trace (if not NULL), and modifies
360 * the @lmf if necessary. @native_offset return the IP offset from the
361 * start of the function or -1 if that info is not available.
363 MonoJitInfo *
364 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
365 MonoContext *ctx, MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
367 MonoJitInfo *ji;
368 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
370 /* Avoid costly table lookup during stack overflow */
371 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
372 ji = prev_ji;
373 else
374 ji = mono_jit_info_table_find (domain, ip);
376 if (managed)
377 *managed = FALSE;
379 if (ji != NULL) {
380 int i;
381 gssize regs [MONO_MAX_IREGS + 1];
382 guint8 *cfa;
383 guint32 unwind_info_len;
384 guint8 *unwind_info;
386 *new_ctx = *ctx;
388 if (managed)
389 if (!ji->method->wrapper_type)
390 *managed = TRUE;
392 if (ji->from_aot)
393 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
394 else
395 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
397 /* Beautiful numbers */
398 for (i = 4; i < 12; ++i)
399 regs [i] = new_ctx->regs [i - 4];
400 regs [ARMREG_IP] = new_ctx->regs [8];
401 regs [ARMREG_LR] = new_ctx->regs [9];
402 regs [ARMREG_SP] = new_ctx->esp;
404 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
405 (guint8*)ji->code_start + ji->code_size,
406 ip, regs, MONO_MAX_IREGS, &cfa);
408 for (i = 4; i < 12; ++i)
409 new_ctx->regs [i - 4] = regs [i];
410 new_ctx->regs [8] = regs [ARMREG_IP];
411 new_ctx->regs [9] = regs [ARMREG_LR];
412 new_ctx->eip = regs [ARMREG_LR];
413 new_ctx->esp = (gsize)cfa;
414 new_ctx->ebp = new_ctx->esp;
416 /* Needed by get_exception_catch_class () */
417 // FIXME: Is this neded ?
418 new_ctx->regs [ARMREG_R11] = new_ctx->esp;
420 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
421 /* remove any unused lmf */
422 *lmf = (*lmf)->previous_lmf;
425 /* we substract 1, so that the IP points into the call instruction */
426 new_ctx->eip--;
428 return ji;
429 } else if (*lmf) {
431 *new_ctx = *ctx;
433 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
434 } else {
435 if (!(*lmf)->method)
436 return (gpointer)-1;
437 memset (res, 0, sizeof (MonoJitInfo));
438 res->method = (*lmf)->method;
441 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
442 /* This is the sp for the current frame */
443 new_ctx->esp = (*lmf)->iregs [ARMREG_FP];
444 new_ctx->eip = (*lmf)->iregs [13];
445 new_ctx->ebp = new_ctx->esp;
447 *lmf = (*lmf)->previous_lmf;
449 return ji ? ji : res;
452 return NULL;
455 void
456 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
458 #if BROKEN_LINUX
459 struct ucontext *uc = sigctx;
461 mctx->eip = uc->uc_mcontext.gregs [ARMREG_PC];
462 mctx->ebp = uc->uc_mcontext.gregs [ARMREG_SP];
463 memcpy (&mctx->regs, &uc->uc_mcontext.gregs [ARMREG_R4], sizeof (gulong) * 8);
464 /* memcpy (&mctx->fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);*/
465 #else
466 my_ucontext *my_uc = sigctx;
468 mctx->eip = UCONTEXT_REG_PC (my_uc);
469 mctx->ebp = UCONTEXT_REG_SP (my_uc);
470 memcpy (&mctx->regs, &UCONTEXT_REG_R4 (my_uc), sizeof (gulong) * 8);
471 #endif
474 void
475 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
477 #if BROKEN_LINUX
478 struct ucontext *uc = ctx;
480 uc->uc_mcontext.gregs [ARMREG_PC] = mctx->eip;
481 uc->uc_mcontext.gregs [ARMREG_SP] = mctx->ebp;
482 memcpy (&uc->uc_mcontext.gregs [ARMREG_R4], &mctx->regs, sizeof (gulong) * 8);
483 /* memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx->fregs, sizeof (double) * MONO_SAVED_FREGS);*/
484 #else
485 my_ucontext *my_uc = ctx;
487 UCONTEXT_REG_PC (my_uc) = mctx->eip;
488 UCONTEXT_REG_SP (my_uc) = mctx->ebp;
489 memcpy (&UCONTEXT_REG_R4 (my_uc), &mctx->regs, sizeof (gulong) * 8);
490 #endif
494 * This is the function called from the signal handler
496 gboolean
497 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
499 MonoContext mctx;
500 gboolean result;
502 mono_arch_sigctx_to_monoctx (ctx, &mctx);
504 result = mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
505 /* restore the context so that returning from the signal handler will invoke
506 * the catch clause
508 mono_arch_monoctx_to_sigctx (&mctx, ctx);
509 return result;
512 gpointer
513 mono_arch_ip_from_context (void *sigctx)
515 #if BROKEN_LINUX
516 struct ucontext *uc = sigctx;
517 return (gpointer)uc->uc_mcontext.gregs [ARMREG_PC];
518 #else
519 my_ucontext *my_uc = sigctx;
520 return (void*) UCONTEXT_REG_PC (my_uc);
521 #endif
524 gboolean
525 mono_arch_has_unwind_info (gconstpointer addr)
527 return FALSE;