27f4e433435b292b76d806af169fc8c78d2b7cb8
[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 typedef struct my_ucontext {
84         unsigned long       uc_flags;
85         struct my_ucontext *uc_link;
86         struct {
87                 void *p;
88                 int flags;
89                 size_t size;
90         } sstack_data;
91         struct sigcontext sig_ctx;
92         /* some 2.6.x kernel has fp data here after a few other fields
93          * we don't use them for now...
94          */
95 } my_ucontext;
96
97 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do {  \
98                 int reg;        \
99                 ARM_LDR_IMM (code, ip_reg, ctx_reg, G_STRUCT_OFFSET (MonoContext, eip));        \
100                 ARM_ADD_REG_IMM8 (code, tmp_reg, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs));  \
101                 ARM_LDMIA (code, tmp_reg, MONO_ARM_REGSAVE_MASK);       \
102         } while (0)
103
104 /* nothing to do */
105 #define setup_context(ctx)
106
107 /*
108  * arch_get_restore_context:
109  *
110  * Returns a pointer to a method which restores a previously saved sigcontext.
111  * The first argument in r0 is the pointer to the context.
112  */
113 gpointer
114 mono_arch_get_restore_context (void)
115 {
116         guint8 *code;
117         static guint8 start [128];
118         static int inited = 0;
119
120         if (inited)
121                 return start;
122         inited = 1;
123
124         code = start;
125         restore_regs_from_context (ARMREG_R0, ARMREG_R1, ARMREG_R2);
126         /* restore also the stack pointer, FIXME: handle sp != fp */
127         ARM_LDR_IMM (code, ARMREG_SP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, ebp));
128         ARM_LDR_IMM (code, ARMREG_FP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, esp));
129
130         /* jump to the saved IP */
131         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
132         /* never reached */
133         ARM_DBRK (code);
134
135         g_assert ((code - start) < sizeof(start));
136         mono_arch_flush_icache (start, code - start);
137         return start;
138 }
139
140 /*
141  * arch_get_call_filter:
142  *
143  * Returns a pointer to a method which calls an exception filter. We
144  * also use this function to call finally handlers (we pass NULL as 
145  * @exc object in this case).
146  */
147 gpointer
148 mono_arch_get_call_filter (void)
149 {
150         static guint8 start [320];
151         static int inited = 0;
152         guint8 *code;
153         int alloc_size, pos, i;
154
155         if (inited)
156                 return start;
157
158         inited = 1;
159         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
160         code = start;
161
162         /* save all the regs on the stack */
163         ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
164         ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
165
166         /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
167         restore_regs_from_context (ARMREG_R0, ARMREG_IP, ARMREG_LR);
168         /* call handler at eip (r1) and set the first arg with the exception (r2) */
169         ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
170         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
171         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
172
173         /* epilog */
174         ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
175
176         g_assert ((code - start) < sizeof(start));
177         mono_arch_flush_icache (start, code - start);
178         return start;
179 }
180
181 static void
182 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
183 {
184         static void (*restore_context) (MonoContext *);
185         MonoContext ctx;
186         gboolean rethrow = eip & 1;
187
188         if (!restore_context)
189                 restore_context = mono_arch_get_restore_context ();
190
191         eip &= ~1; /* clear the optional rethrow bit */
192         /* adjust eip so that it point into the call instruction */
193         eip -= 4;
194
195         setup_context (&ctx);
196
197         /*printf ("stack in throw: %p\n", esp);*/
198         MONO_CONTEXT_SET_BP (&ctx, esp);
199         MONO_CONTEXT_SET_SP (&ctx, esp);
200         MONO_CONTEXT_SET_IP (&ctx, eip);
201         memcpy (&ctx.regs, int_regs, sizeof (gulong) * 8);
202         /* memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS); */
203
204         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
205                 MonoException *mono_ex = (MonoException*)exc;
206                 if (!rethrow)
207                         mono_ex->stack_trace = NULL;
208         }
209         mono_handle_exception (&ctx, exc, (gpointer)(eip + 4), FALSE);
210         restore_context (&ctx);
211         g_assert_not_reached ();
212 }
213
214 /**
215  * arch_get_throw_exception_generic:
216  *
217  * Returns a function pointer which can be used to raise 
218  * exceptions. The returned function has the following 
219  * signature: void (*func) (MonoException *exc); or
220  * void (*func) (guint32 ex_token, guint8* ip);
221  *
222  */
223 static gpointer 
224 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_token, gboolean rethrow)
225 {
226         guint8 *code;
227         int alloc_size, pos, i;
228
229         code = start;
230
231         /* save all the regs on the stack */
232         ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
233         ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
234
235         if (by_token) {
236                 /* r0 has the type token of the exception: get the object */
237                 ARM_PUSH1 (code, ARMREG_R1);
238                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
239                 code = mono_arm_emit_load_imm (code, ARMREG_R0, GPOINTER_TO_UINT (mono_defaults.corlib));
240                 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (mono_exception_from_token));
241                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
242                 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
243                 ARM_POP1 (code, ARMREG_R1);
244         }
245
246         /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
247         /* caller sp */
248         ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, 10 * 4); /* 10 saved regs */
249         /* exc is already in place in r0 */
250         if (by_token) {
251                 /* The caller ip is already in R1 */
252         } else {
253                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
254         }
255         /* FIXME: pointer to the saved fp regs */
256         /*pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
257         ppc_addi (code, ppc_r7, ppc_sp, pos);*/
258         /* pointer to the saved int regs */
259         ARM_MOV_REG_REG (code, ARMREG_R3, ARMREG_SP); /* the pushed regs */
260         /* we encode rethrow in the ip, so we avoid args on the stack */
261         ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
262
263         code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (throw_exception));
264         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
265         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
266         /* we should never reach this breakpoint */
267         ARM_DBRK (code);
268         g_assert ((code - start) < size);
269         mono_arch_flush_icache (start, code - start);
270         return start;
271 }
272
273 /**
274  * mono_arch_get_rethrow_exception:
275  *
276  * Returns a function pointer which can be used to rethrow 
277  * exceptions. The returned function has the following 
278  * signature: void (*func) (MonoException *exc); 
279  *
280  */
281 gpointer
282 mono_arch_get_rethrow_exception (void)
283 {
284         static guint8 start [132];
285         static int inited = 0;
286
287         if (inited)
288                 return start;
289         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
290         inited = 1;
291         return start;
292 }
293 /**
294  * arch_get_throw_exception:
295  *
296  * Returns a function pointer which can be used to raise 
297  * exceptions. The returned function has the following 
298  * signature: void (*func) (MonoException *exc); 
299  * For example to raise an arithmetic exception you can use:
300  *
301  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
302  * x86_call_code (code, arch_get_throw_exception ()); 
303  *
304  */
305 gpointer 
306 mono_arch_get_throw_exception (void)
307 {
308         static guint8 start [132];
309         static int inited = 0;
310
311         if (inited)
312                 return start;
313         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
314         inited = 1;
315         return start;
316 }
317
318 gpointer 
319 mono_arch_get_throw_exception_by_name (void)
320 {       
321         static guint8* start;
322         static gboolean inited = FALSE;
323         guint8 *code;
324
325         if (inited)
326                 return start;
327
328         start = code = mono_global_codeman_reserve (64);
329
330         /* Not used on ARM */
331         ARM_DBRK (code);
332
333         return start;
334 }
335
336 /**
337  * mono_arch_get_throw_corlib_exception:
338  *
339  * Returns a function pointer which can be used to raise 
340  * corlib exceptions. The returned function has the following 
341  * signature: void (*func) (guint32 ex_token, guint32 offset); 
342  * Here, offset is the offset which needs to be substracted from the caller IP 
343  * to get the IP of the throw. Passing the offset has the advantage that it 
344  * needs no relocations in the caller.
345  * On ARM, the ip is passed instead of an offset.
346  */
347 gpointer 
348 mono_arch_get_throw_corlib_exception (void)
349 {
350         static guint8 start [168];
351         static int inited = 0;
352
353         if (inited)
354                 return start;
355         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
356         inited = 1;
357         return start;
358 }       
359
360 /* mono_arch_find_jit_info:
361  *
362  * This function is used to gather information from @ctx. It return the 
363  * MonoJitInfo of the corresponding function, unwinds one stack frame and
364  * stores the resulting context into @new_ctx. It also stores a string 
365  * describing the stack location into @trace (if not NULL), and modifies
366  * the @lmf if necessary. @native_offset return the IP offset from the 
367  * start of the function or -1 if that info is not available.
368  */
369 MonoJitInfo *
370 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
371                          MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf,
372                          int *native_offset, gboolean *managed)
373 {
374         MonoJitInfo *ji;
375         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
376
377         /* Avoid costly table lookup during stack overflow */
378         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
379                 ji = prev_ji;
380         else
381                 ji = mono_jit_info_table_find (domain, ip);
382
383         if (managed)
384                 *managed = FALSE;
385
386         if (ji != NULL) {
387                 int offset;
388
389                 *new_ctx = *ctx;
390
391                 if (managed)
392                         if (!ji->method->wrapper_type)
393                                 *managed = TRUE;
394
395                 /*
396                  * Some managed methods like pinvoke wrappers might have save_lmf set.
397                  * In this case, register save/restore code is not generated by the 
398                  * JIT, so we have to restore callee saved registers from the lmf.
399                  */
400                 if (ji->method->save_lmf) {
401                         /* 
402                          * We only need to do this if the exception was raised in managed
403                          * code, since otherwise the lmf was already popped of the stack.
404                          */
405                         if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
406                                 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
407                         }
408                         new_ctx->esp = (*lmf)->iregs [12];
409                         new_ctx->eip = (*lmf)->iregs [13];
410                         new_ctx->ebp = new_ctx->esp;
411                 } else {
412                         int i;
413                         char* sp;
414                         offset = ji->used_regs >> 16;
415                         offset <<= 2;
416                         /* the saved regs are at sp + offset */
417                         /* restore caller saved registers */
418                         sp = (char*)ctx->ebp;
419                         sp += offset;
420                         for (i = 4; i < 16; ++i) {
421                                 if (ji->used_regs & (1 << i)) {
422                                         new_ctx->regs [i - 4] = *(gulong*)sp;
423                                         sp += sizeof (gulong);
424                                 }
425                         }
426                         /* IP and LR */
427                         new_ctx->esp = *(gulong*)sp;
428                         sp += sizeof (gulong);
429                         new_ctx->eip = *(gulong*)sp;
430                         sp += sizeof (gulong);
431                         new_ctx->ebp = new_ctx->esp;
432                 }
433
434                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
435                         /* remove any unused lmf */
436                         *lmf = (*lmf)->previous_lmf;
437                 }
438
439                 /* we substract 1, so that the IP points into the call instruction */
440                 new_ctx->eip--;
441
442                 return ji;
443         } else if (*lmf) {
444                 
445                 *new_ctx = *ctx;
446
447                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
448                 } else {
449                         if (!(*lmf)->method)
450                                 return (gpointer)-1;
451                         memset (res, 0, sizeof (MonoJitInfo));
452                         res->method = (*lmf)->method;
453                 }
454
455                 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
456                 new_ctx->esp = (*lmf)->iregs [12];
457                 new_ctx->eip = (*lmf)->iregs [13];
458                 new_ctx->ebp = new_ctx->esp;
459
460                 *lmf = (*lmf)->previous_lmf;
461
462                 return ji ? ji : res;
463         }
464
465         return NULL;
466 }
467
468 void
469 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
470 {
471 #if BROKEN_LINUX
472         struct ucontext *uc = sigctx;
473
474         mctx->eip = uc->uc_mcontext.gregs [ARMREG_PC];
475         mctx->ebp = uc->uc_mcontext.gregs [ARMREG_SP];
476         memcpy (&mctx->regs, &uc->uc_mcontext.gregs [ARMREG_R4], sizeof (gulong) * 8);
477         /* memcpy (&mctx->fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);*/
478 #else
479         my_ucontext *my_uc = sigctx;
480
481         mctx->eip = my_uc->sig_ctx.arm_pc;
482         mctx->ebp = my_uc->sig_ctx.arm_sp;
483         memcpy (&mctx->regs, &my_uc->sig_ctx.arm_r4, sizeof (gulong) * 8);
484 #endif
485 }
486
487 void
488 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
489 {
490 #if BROKEN_LINUX
491         struct ucontext *uc = ctx;
492
493         uc->uc_mcontext.gregs [ARMREG_PC] = mctx->eip;
494         uc->uc_mcontext.gregs [ARMREG_SP] = mctx->ebp;
495         memcpy (&uc->uc_mcontext.gregs [ARMREG_R4], &mctx->regs, sizeof (gulong) * 8);
496         /* memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx->fregs, sizeof (double) * MONO_SAVED_FREGS);*/
497 #else
498         my_ucontext *my_uc = ctx;
499
500         my_uc->sig_ctx.arm_pc = mctx->eip;
501         my_uc->sig_ctx.arm_sp = mctx->ebp;
502         memcpy (&my_uc->sig_ctx.arm_r4, &mctx->regs, sizeof (gulong) * 8);
503 #endif
504 }
505
506 /*
507  * This is the function called from the signal handler
508  */
509 gboolean
510 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
511 {
512         MonoContext mctx;
513         gboolean result;
514
515         mono_arch_sigctx_to_monoctx (ctx, &mctx);
516
517         result = mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
518         /* restore the context so that returning from the signal handler will invoke
519          * the catch clause 
520          */
521         mono_arch_monoctx_to_sigctx (&mctx, ctx);
522         return result;
523 }
524
525 gpointer
526 mono_arch_ip_from_context (void *sigctx)
527 {
528 #if BROKEN_LINUX
529         struct ucontext *uc = sigctx;
530         return (gpointer)uc->uc_mcontext.gregs [ARMREG_PC];
531 #else
532         my_ucontext *my_uc = sigctx;
533         return (void*)my_uc->sig_ctx.arm_pc;
534 #endif
535 }
536
537 gboolean
538 mono_arch_has_unwind_info (gconstpointer addr)
539 {
540         return FALSE;
541 }
542