2007-05-27 Zoltan Varga <vargaz@gmail.com>
[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 static gboolean arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only);
29
30 /*
31
32 struct sigcontext {
33         unsigned long trap_no;
34         unsigned long error_code;
35         unsigned long oldmask;
36         unsigned long arm_r0;
37         unsigned long arm_r1;
38         unsigned long arm_r2;
39         unsigned long arm_r3;
40         unsigned long arm_r4;
41         unsigned long arm_r5;
42         unsigned long arm_r6;
43         unsigned long arm_r7;
44         unsigned long arm_r8;
45         unsigned long arm_r9;
46         unsigned long arm_r10;
47         unsigned long arm_fp;
48         unsigned long arm_ip;
49         unsigned long arm_sp;
50         unsigned long arm_lr;
51         unsigned long arm_pc;
52         unsigned long arm_cpsr;
53         unsigned long fault_address;
54 };
55
56 gregs below is this struct
57 struct user_regs {
58         unsigned long int uregs[18];
59 };
60
61 the companion user_fpregs has just 8 double registers
62 (it's valid for FPA mode, will need changes for VFP)
63
64 typedef struct {
65         gregset_t gregs;
66         fpregset_t fpregs;
67 } mcontext_t;
68             
69 typedef struct ucontext {
70         unsigned long int uc_flags;
71         struct ucontext *uc_link;
72         __sigset_t uc_sigmask;
73         stack_t uc_stack;
74         mcontext_t uc_mcontext;
75         long int uc_filler[5];
76 } ucontext_t;
77
78 */
79
80 /*
81  * So, it turns out that the ucontext struct defined by libc is incorrect.
82  * We define our own version here and use it instead.
83  */
84
85 typedef struct my_ucontext {
86         unsigned long       uc_flags;
87         struct my_ucontext *uc_link;
88         struct {
89                 void *p;
90                 int flags;
91                 size_t size;
92         } sstack_data;
93         struct sigcontext sig_ctx;
94         /* some 2.6.x kernel has fp data here after a few other fields
95          * we don't use them for now...
96          */
97 } my_ucontext;
98
99 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do {  \
100                 int reg;        \
101                 ARM_LDR_IMM (code, ip_reg, ctx_reg, G_STRUCT_OFFSET (MonoContext, eip));        \
102                 ARM_ADD_REG_IMM8 (code, tmp_reg, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs));  \
103                 ARM_LDMIA (code, tmp_reg, MONO_ARM_REGSAVE_MASK);       \
104         } while (0)
105
106 /* nothing to do */
107 #define setup_context(ctx)
108
109 /*
110  * arch_get_restore_context:
111  *
112  * Returns a pointer to a method which restores a previously saved sigcontext.
113  * The first argument in r0 is the pointer to the context.
114  */
115 gpointer
116 mono_arch_get_restore_context (void)
117 {
118         guint8 *code;
119         static guint8 start [128];
120         static int inited = 0;
121
122         if (inited)
123                 return start;
124         inited = 1;
125
126         code = start;
127         restore_regs_from_context (ARMREG_R0, ARMREG_R1, ARMREG_R2);
128         /* restore also the stack pointer, FIXME: handle sp != fp */
129         ARM_LDR_IMM (code, ARMREG_SP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, ebp));
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_IP (&ctx, eip);
200         memcpy (&ctx.regs, int_regs, sizeof (gulong) * 8);
201         /* memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS); */
202
203         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
204                 MonoException *mono_ex = (MonoException*)exc;
205                 if (!rethrow)
206                         mono_ex->stack_trace = NULL;
207         }
208         mono_handle_exception (&ctx, exc, (gpointer)(eip + 4), FALSE);
209         restore_context (&ctx);
210         g_assert_not_reached ();
211 }
212
213 /**
214  * arch_get_throw_exception_generic:
215  *
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) (char *exc_name);
220  *
221  */
222 static gpointer 
223 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gboolean rethrow)
224 {
225         guint8 *code;
226         int alloc_size, pos, i;
227
228         code = start;
229
230         /* save all the regs on the stack */
231         ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
232         ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
233
234         if (by_name) {
235                 /* r0 has the name of the exception: get the object */
236                 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R0);
237                 code = mono_arm_emit_load_imm (code, ARMREG_R0, GPOINTER_TO_UINT (mono_defaults.corlib));
238                 code = mono_arm_emit_load_imm (code, ARMREG_R1, GPOINTER_TO_UINT ("System"));
239                 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (mono_exception_from_name));
240                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
241                 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
242         }
243
244         /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
245         /* caller sp */
246         ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, 10 * 4); /* 10 saved regs */
247         /* exc is already in place in r0 */
248         if (by_name)
249                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_SP, 9 * 4); /* pos on the stack were lr was saved */
250         else
251                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
252         /* FIXME: pointer to the saved fp regs */
253         /*pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
254         ppc_addi (code, ppc_r7, ppc_sp, pos);*/
255         /* pointer to the saved int regs */
256         ARM_MOV_REG_REG (code, ARMREG_R3, ARMREG_SP); /* the pushed regs */
257         /* we encode rethrow in the ip, so we avoid args on the stack */
258         ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
259
260         code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (throw_exception));
261         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
262         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
263         /* we should never reach this breakpoint */
264         ARM_DBRK (code);
265         g_assert ((code - start) < size);
266         mono_arch_flush_icache (start, code - start);
267         return start;
268 }
269
270 /**
271  * mono_arch_get_rethrow_exception:
272  *
273  * Returns a function pointer which can be used to rethrow 
274  * exceptions. The returned function has the following 
275  * signature: void (*func) (MonoException *exc); 
276  *
277  */
278 gpointer
279 mono_arch_get_rethrow_exception (void)
280 {
281         static guint8 start [132];
282         static int inited = 0;
283
284         if (inited)
285                 return start;
286         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
287         inited = 1;
288         return start;
289 }
290 /**
291  * arch_get_throw_exception:
292  *
293  * Returns a function pointer which can be used to raise 
294  * exceptions. The returned function has the following 
295  * signature: void (*func) (MonoException *exc); 
296  * For example to raise an arithmetic exception you can use:
297  *
298  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
299  * x86_call_code (code, arch_get_throw_exception ()); 
300  *
301  */
302 gpointer 
303 mono_arch_get_throw_exception (void)
304 {
305         static guint8 start [132];
306         static int inited = 0;
307
308         if (inited)
309                 return start;
310         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
311         inited = 1;
312         return start;
313 }
314
315 /**
316  * arch_get_throw_exception_by_name:
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) (char *exc_name); 
321  * For example to raise an arithmetic exception you can use:
322  *
323  * x86_push_imm (code, "ArithmeticException"); 
324  * x86_call_code (code, arch_get_throw_exception_by_name ()); 
325  *
326  */
327 gpointer 
328 mono_arch_get_throw_exception_by_name (void)
329 {
330         static guint8 start [168];
331         static int inited = 0;
332
333         if (inited)
334                 return start;
335         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
336         inited = 1;
337         return start;
338 }       
339
340 static MonoArray *
341 glist_to_array (GList *list, MonoClass *eclass) 
342 {
343         MonoDomain *domain = mono_domain_get ();
344         MonoArray *res;
345         int len, i;
346
347         if (!list)
348                 return NULL;
349
350         len = g_list_length (list);
351         res = mono_array_new (domain, eclass, len);
352
353         for (i = 0; list; list = list->next, i++)
354                 mono_array_set (res, gpointer, i, list->data);
355
356         return res;
357 }
358
359 /* mono_arch_find_jit_info:
360  *
361  * This function is used to gather information from @ctx. It return the 
362  * MonoJitInfo of the corresponding function, unwinds one stack frame and
363  * stores the resulting context into @new_ctx. It also stores a string 
364  * describing the stack location into @trace (if not NULL), and modifies
365  * the @lmf if necessary. @native_offset return the IP offset from the 
366  * start of the function or -1 if that info is not available.
367  */
368 MonoJitInfo *
369 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
370                          MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf,
371                          int *native_offset, gboolean *managed)
372 {
373         MonoJitInfo *ji;
374         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
375
376         /* Avoid costly table lookup during stack overflow */
377         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
378                 ji = prev_ji;
379         else
380                 ji = mono_jit_info_table_find (domain, ip);
381
382         if (managed)
383                 *managed = FALSE;
384
385         if (ji != NULL) {
386                 int offset;
387
388                 *new_ctx = *ctx;
389
390                 if (managed)
391                         if (!ji->method->wrapper_type)
392                                 *managed = TRUE;
393
394                 /*
395                  * Some managed methods like pinvoke wrappers might have save_lmf set.
396                  * In this case, register save/restore code is not generated by the 
397                  * JIT, so we have to restore callee saved registers from the lmf.
398                  */
399                 if (ji->method->save_lmf) {
400                         /* 
401                          * We only need to do this if the exception was raised in managed
402                          * code, since otherwise the lmf was already popped of the stack.
403                          */
404                         if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
405                                 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
406                         }
407                         new_ctx->esp = (*lmf)->iregs [12];
408                         new_ctx->eip = (*lmf)->iregs [13];
409                         new_ctx->ebp = new_ctx->esp;
410                 } else {
411                         int i;
412                         char* sp;
413                         offset = ji->used_regs >> 16;
414                         offset <<= 2;
415                         /* the saved regs are at sp + offset */
416                         /* restore caller saved registers */
417                         sp = (char*)ctx->ebp;
418                         sp += offset;
419                         for (i = 4; i < 16; ++i) {
420                                 if (ji->used_regs & (1 << i)) {
421                                         new_ctx->regs [i - 4] = *(gulong*)sp;
422                                         sp += sizeof (gulong);
423                                 }
424                         }
425                         /* IP and LR */
426                         new_ctx->esp = *(gulong*)sp;
427                         sp += sizeof (gulong);
428                         new_ctx->eip = *(gulong*)sp;
429                         sp += sizeof (gulong);
430                         new_ctx->ebp = new_ctx->esp;
431                 }
432
433                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
434                         /* remove any unused lmf */
435                         *lmf = (*lmf)->previous_lmf;
436                 }
437
438                 /* we substract 1, so that the IP points into the call instruction */
439                 new_ctx->eip--;
440
441                 return ji;
442         } else if (*lmf) {
443                 
444                 *new_ctx = *ctx;
445
446                 if (!(*lmf)->method)
447                         return (gpointer)-1;
448
449                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
450                 } else {
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 /*
469  * This is the function called from the signal handler
470  */
471 gboolean
472 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
473 {
474 #if BROKEN_LINUX
475         struct ucontext *uc = ctx;
476 #else
477         my_ucontext *my_uc = ctx;
478 #endif
479         MonoContext mctx;
480         gboolean result;
481
482 #if BROKEN_LINUX
483         mctx.eip = uc->uc_mcontext.gregs [ARMREG_PC];
484         mctx.ebp = uc->uc_mcontext.gregs [ARMREG_SP];
485         memcpy (&mctx.regs, &uc->uc_mcontext.gregs [ARMREG_R4], sizeof (gulong) * 8);
486         /* memcpy (&mctx.fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);*/
487 #else
488         mctx.eip = my_uc->sig_ctx.arm_pc;
489         mctx.ebp = my_uc->sig_ctx.arm_sp;
490         memcpy (&mctx.regs, &my_uc->sig_ctx.arm_r4, sizeof (gulong) * 8);
491 #endif
492
493         result = mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
494         /* restore the context so that returning from the signal handler will invoke
495          * the catch clause 
496          */
497 #if BROKEN_LINUX
498         uc->uc_mcontext.gregs [ARMREG_PC] = mctx.eip;
499         uc->uc_mcontext.gregs [ARMREG_SP] = mctx.ebp;
500         memcpy (&uc->uc_mcontext.gregs [ARMREG_R4], &mctx.regs, sizeof (gulong) * 8);
501         /* memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx.fregs, sizeof (double) * MONO_SAVED_FREGS);*/
502 #else
503         my_uc->sig_ctx.arm_pc = mctx.eip;
504         my_uc->sig_ctx.arm_sp = mctx.ebp;
505         memcpy (&my_uc->sig_ctx.arm_r4, &mctx.regs, sizeof (gulong) * 8);
506 #endif
507         return result;
508 }
509
510 gpointer
511 mono_arch_ip_from_context (void *sigctx)
512 {
513 #if BROKEN_LINUX
514         struct ucontext *uc = sigctx;
515         return (gpointer)uc->uc_mcontext.gregs [ARMREG_PC];
516 #else
517         my_ucontext *my_uc = sigctx;
518         return (void*)my_uc->sig_ctx.arm_pc;
519 #endif
520 }
521
522 gboolean
523 mono_arch_has_unwind_info (gconstpointer addr)
524 {
525         return FALSE;
526 }
527