0b1d7ca4b8b062a07f7395d3d3e596f66e4e809c
[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 #include <ucontext.h>
16
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>
24
25 #include "mini.h"
26 #include "mini-arm.h"
27
28 /*
29
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;
52 };
53
54 gregs below is this struct
55 struct user_regs {
56         unsigned long int uregs[18];
57 };
58
59 the companion user_fpregs has just 8 double registers
60 (it's valid for FPA mode, will need changes for VFP)
61
62 typedef struct {
63         gregset_t gregs;
64         fpregset_t fpregs;
65 } mcontext_t;
66             
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;
75
76 */
77
78 /*
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.
81  */
82
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...
97          */
98 } my_ucontext;
99 #endif
100
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)
106
107 /* nothing to do */
108 #define setup_context(ctx)
109
110 /*
111  * arch_get_restore_context:
112  *
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.
115  */
116 gpointer
117 mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
118 {
119         guint8 *code;
120         guint8 *start;
121
122         *ji = NULL;
123
124         start = code = mono_global_codeman_reserve (128);
125
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));
130
131         /* jump to the saved IP */
132         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
133         /* never reached */
134         ARM_DBRK (code);
135
136         g_assert ((code - start) < 128);
137
138         mono_arch_flush_icache (start, code - start);
139
140         *code_size = code - start;
141
142         return start;
143 }
144
145 /*
146  * arch_get_call_filter:
147  *
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).
151  */
152 gpointer
153 mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
154 {
155         guint8 *code;
156         guint8* start;
157
158         *ji = NULL;
159
160         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
161         start = code = mono_global_codeman_reserve (320);
162
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);
166
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);
173
174         /* epilog */
175         ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
176
177         g_assert ((code - start) < 320);
178
179         mono_arch_flush_icache (start, code - start);
180
181         *code_size = code - start;
182
183         return start;
184 }
185
186 void
187 mono_arm_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
188 {
189         static void (*restore_context) (MonoContext *);
190         MonoContext ctx;
191         gboolean rethrow = eip & 1;
192
193         if (!restore_context)
194                 restore_context = mono_get_restore_context ();
195
196         eip &= ~1; /* clear the optional rethrow bit */
197         /* adjust eip so that it point into the call instruction */
198         eip -= 4;
199
200         setup_context (&ctx);
201
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); */
208
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;
213         }
214         mono_handle_exception (&ctx, exc, (gpointer)(eip + 4), FALSE);
215         restore_context (&ctx);
216         g_assert_not_reached ();
217 }
218
219 void
220 mono_arm_throw_exception_by_token (guint32 type_token, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
221 {
222         mono_arm_throw_exception (mono_exception_from_token (mono_defaults.corlib, type_token), eip, esp, int_regs, fp_regs);
223 }
224
225 /**
226  * arch_get_throw_exception_generic:
227  *
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);
232  *
233  */
234 static gpointer 
235 mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
236 {
237         guint8 *start;
238         guint8 *code;
239
240         *ji = NULL;
241
242         code = start = mono_global_codeman_reserve (size);
243
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);
247
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         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
253         /* FIXME: pointer to the saved fp regs */
254         /*pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
255         ppc_addi (code, ppc_r7, ppc_sp, pos);*/
256         /* pointer to the saved int regs */
257         ARM_MOV_REG_REG (code, ARMREG_R3, ARMREG_SP); /* the pushed regs */
258         /* we encode rethrow in the ip, so we avoid args on the stack */
259         ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
260
261         if (aot) {
262                 *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");
263                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
264                 ARM_B (code, 0);
265                 *(gpointer*)(gpointer)code = NULL;
266                 code += 4;
267                 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
268         } else {
269                 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (by_token ? mono_arm_throw_exception_by_token : mono_arm_throw_exception));
270         }
271         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
272         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
273         /* we should never reach this breakpoint */
274         ARM_DBRK (code);
275         g_assert ((code - start) < size);
276         mono_arch_flush_icache (start, code - start);
277
278         *code_size = code - start;
279
280         return start;
281 }
282
283 /**
284  * mono_arch_get_rethrow_exception:
285  *
286  * Returns a function pointer which can be used to rethrow 
287  * exceptions. The returned function has the following 
288  * signature: void (*func) (MonoException *exc); 
289  *
290  */
291 gpointer
292 mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
293 {
294         return mono_arch_get_throw_exception_generic (132, FALSE, TRUE, code_size, ji, aot);
295 }
296
297 /**
298  * arch_get_throw_exception:
299  *
300  * Returns a function pointer which can be used to raise 
301  * exceptions. The returned function has the following 
302  * signature: void (*func) (MonoException *exc); 
303  * For example to raise an arithmetic exception you can use:
304  *
305  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
306  * x86_call_code (code, arch_get_throw_exception ()); 
307  *
308  */
309 gpointer 
310 mono_arch_get_throw_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
311 {
312         return mono_arch_get_throw_exception_generic (132, FALSE, FALSE, code_size, ji, aot);
313 }
314
315 gpointer 
316 mono_arch_get_throw_exception_by_name_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
317 {       
318         guint8* start;
319         guint8 *code;
320
321         *ji = NULL;
322
323         start = code = mono_global_codeman_reserve (64);
324
325         /* Not used on ARM */
326         ARM_DBRK (code);
327
328         *code_size = code - start;
329
330         return start;
331 }
332
333 /**
334  * mono_arch_get_throw_corlib_exception:
335  *
336  * Returns a function pointer which can be used to raise 
337  * corlib exceptions. The returned function has the following 
338  * signature: void (*func) (guint32 ex_token, guint32 offset); 
339  * Here, offset is the offset which needs to be substracted from the caller IP 
340  * to get the IP of the throw. Passing the offset has the advantage that it 
341  * needs no relocations in the caller.
342  * On ARM, the ip is passed instead of an offset.
343  */
344 gpointer 
345 mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
346 {
347         return mono_arch_get_throw_exception_generic (168, TRUE, FALSE, code_size, ji, aot);
348 }       
349
350 /* mono_arch_find_jit_info:
351  *
352  * This function is used to gather information from @ctx. It return the 
353  * MonoJitInfo of the corresponding function, unwinds one stack frame and
354  * stores the resulting context into @new_ctx. It also stores a string 
355  * describing the stack location into @trace (if not NULL), and modifies
356  * the @lmf if necessary. @native_offset return the IP offset from the 
357  * start of the function or -1 if that info is not available.
358  */
359 MonoJitInfo *
360 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
361                          MonoContext *ctx, MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
362 {
363         MonoJitInfo *ji;
364         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
365
366         /* Avoid costly table lookup during stack overflow */
367         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
368                 ji = prev_ji;
369         else
370                 ji = mono_jit_info_table_find (domain, ip);
371
372         if (managed)
373                 *managed = FALSE;
374
375         if (ji != NULL) {
376                 int offset;
377
378                 *new_ctx = *ctx;
379
380                 if (managed)
381                         if (!ji->method->wrapper_type)
382                                 *managed = TRUE;
383
384                 /*
385                  * Some managed methods like pinvoke wrappers might have save_lmf set.
386                  * In this case, register save/restore code is not generated by the 
387                  * JIT, so we have to restore callee saved registers from the lmf.
388                  */
389                 if (ji->method->save_lmf) {
390                         /* 
391                          * We only need to do this if the exception was raised in managed
392                          * code, since otherwise the lmf was already popped of the stack.
393                          */
394                         if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
395                                 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
396                         }
397                         new_ctx->esp = (*lmf)->iregs [12];
398                         new_ctx->eip = (*lmf)->iregs [13];
399                         new_ctx->ebp = new_ctx->esp;
400                 } else {
401                         int i;
402                         char* sp;
403                         offset = ji->used_regs >> 16;
404                         offset <<= 2;
405                         /* the saved regs are at sp + offset */
406                         /* restore caller saved registers */
407                         sp = (char*)ctx->ebp;
408                         sp += offset;
409                         for (i = 4; i < 16; ++i) {
410                                 if (ji->used_regs & (1 << i)) {
411                                         new_ctx->regs [i - 4] = *(gulong*)sp;
412                                         sp += sizeof (gulong);
413                                 }
414                         }
415                         /* IP and LR */
416                         new_ctx->esp = *(gulong*)sp;
417                         sp += sizeof (gulong);
418                         new_ctx->eip = *(gulong*)sp;
419                         sp += sizeof (gulong);
420                         new_ctx->ebp = new_ctx->esp;
421                         /* Needed by get_exception_catch_class () */
422                         new_ctx->regs [ARMREG_R11] = new_ctx->esp;
423                 }
424
425                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
426                         /* remove any unused lmf */
427                         *lmf = (*lmf)->previous_lmf;
428                 }
429
430                 /* we substract 1, so that the IP points into the call instruction */
431                 new_ctx->eip--;
432
433                 return ji;
434         } else if (*lmf) {
435                 
436                 *new_ctx = *ctx;
437
438                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
439                 } else {
440                         if (!(*lmf)->method)
441                                 return (gpointer)-1;
442                         memset (res, 0, sizeof (MonoJitInfo));
443                         res->method = (*lmf)->method;
444                 }
445
446                 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
447                 new_ctx->esp = (*lmf)->iregs [12];
448                 new_ctx->eip = (*lmf)->iregs [13];
449                 new_ctx->ebp = new_ctx->esp;
450
451                 *lmf = (*lmf)->previous_lmf;
452
453                 return ji ? ji : res;
454         }
455
456         return NULL;
457 }
458
459 void
460 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
461 {
462 #if BROKEN_LINUX
463         struct ucontext *uc = sigctx;
464
465         mctx->eip = uc->uc_mcontext.gregs [ARMREG_PC];
466         mctx->ebp = uc->uc_mcontext.gregs [ARMREG_SP];
467         memcpy (&mctx->regs, &uc->uc_mcontext.gregs [ARMREG_R4], sizeof (gulong) * 8);
468         /* memcpy (&mctx->fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);*/
469 #else
470         my_ucontext *my_uc = sigctx;
471
472         mctx->eip = UCONTEXT_REG_PC (my_uc);
473         mctx->ebp = UCONTEXT_REG_SP (my_uc);
474         memcpy (&mctx->regs, &UCONTEXT_REG_R4 (my_uc), sizeof (gulong) * 8);
475 #endif
476 }
477
478 void
479 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
480 {
481 #if BROKEN_LINUX
482         struct ucontext *uc = ctx;
483
484         uc->uc_mcontext.gregs [ARMREG_PC] = mctx->eip;
485         uc->uc_mcontext.gregs [ARMREG_SP] = mctx->ebp;
486         memcpy (&uc->uc_mcontext.gregs [ARMREG_R4], &mctx->regs, sizeof (gulong) * 8);
487         /* memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx->fregs, sizeof (double) * MONO_SAVED_FREGS);*/
488 #else
489         my_ucontext *my_uc = ctx;
490
491         UCONTEXT_REG_PC (my_uc) = mctx->eip;
492         UCONTEXT_REG_SP (my_uc) = mctx->ebp;
493         memcpy (&UCONTEXT_REG_R4 (my_uc), &mctx->regs, sizeof (gulong) * 8);
494 #endif
495 }
496
497 /*
498  * This is the function called from the signal handler
499  */
500 gboolean
501 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
502 {
503         MonoContext mctx;
504         gboolean result;
505
506         mono_arch_sigctx_to_monoctx (ctx, &mctx);
507
508         result = mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
509         /* restore the context so that returning from the signal handler will invoke
510          * the catch clause 
511          */
512         mono_arch_monoctx_to_sigctx (&mctx, ctx);
513         return result;
514 }
515
516 gpointer
517 mono_arch_ip_from_context (void *sigctx)
518 {
519 #if BROKEN_LINUX
520         struct ucontext *uc = sigctx;
521         return (gpointer)uc->uc_mcontext.gregs [ARMREG_PC];
522 #else
523         my_ucontext *my_uc = sigctx;
524         return (void*) UCONTEXT_REG_PC (my_uc);
525 #endif
526 }
527
528 gboolean
529 mono_arch_has_unwind_info (gconstpointer addr)
530 {
531         return FALSE;
532 }
533