2008-12-09 Mark Mason <mmason@upwardaccess.com>
[mono.git] / mono / mini / exceptions-mips.c
1 /*
2  * exceptions-mips.c: exception support for MIPS
3  *
4  * Authors:
5  *   Mark Mason (mason@broadcom.com)
6  *
7  * Based on exceptions-ppc.c by:
8  *   Dietmar Maurer (dietmar@ximian.com)
9  *   Paolo Molaro (lupus@ximian.com)
10  *
11  * (C) 2006 Broadcom
12  * (C) 2001 Ximian, Inc.
13  */
14
15 #include <config.h>
16 #include <glib.h>
17 #include <signal.h>
18 #include <string.h>
19 #include <ucontext.h>
20
21 #include <mono/arch/mips/mips-codegen.h>
22 #include <mono/metadata/appdomain.h>
23 #include <mono/metadata/tabledefs.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/debug-helpers.h>
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/mono-debug.h>
28
29 #include "mini.h"
30 #include "mini-mips.h"
31
32 #define GENERIC_EXCEPTION_SIZE 256
33
34 /* XXX */
35 #if 1
36 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do {  \
37         } while (0)
38 #else
39 #define restore_regs_from_context(ctx_reg,pc,tmp_reg) do {      \
40                 int reg;        \
41                 ppc_lwz (code, pc, G_STRUCT_OFFSET (MonoContext, sc_pc), ctx_reg);      \
42                 ppc_lmw (code, ppc_r13, ctx_reg, G_STRUCT_OFFSET (MonoContext, sc_regs));       \
43                 for (reg = 0; reg < MONO_SAVED_FREGS; ++reg) {  \
44                         ppc_lfd (code, (14 + reg), G_STRUCT_OFFSET(MonoLMF, sc_fpregs) + reg * sizeof (gdouble), ctx_reg);      \
45                 }       \
46         } while (0)
47 #endif
48
49 /* nothing to do */
50 #define setup_context(ctx) do { \
51                 memset ((ctx), 0, sizeof(*(ctx)));      \
52         } while (0);
53
54 /*
55  * mono_arch_get_restore_context:
56  *
57  * Returns a pointer to a method which restores a previously saved MonoContext.
58  * The first argument in a0 is the pointer to the MonoContext.
59  */
60 gpointer
61 mono_arch_get_restore_context (void)
62 {
63         int i;
64         guint8 *code;
65         static guint8 start [128];
66         static int inited = 0;
67         guint32 iregs_to_restore;
68
69         if (inited)
70                 return start;
71         inited = 1;
72         code = start;
73
74         iregs_to_restore = (MONO_ARCH_CALLEE_SAVED_REGS \
75                             | (1 << mips_sp) | (1 << mips_ra));
76         for (i = 0; i < MONO_SAVED_GREGS; ++i) {
77                 if (iregs_to_restore & (1 << i)) {
78                         mips_lw (code, i, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[i]));
79                 }
80         }
81
82         /* Get the address to return to */
83         mips_lw (code, mips_t9, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_pc));
84
85         /* jump to the saved IP */
86         mips_jr (code, mips_t9);
87         mips_nop (code);
88
89         /* never reached */
90         mips_break (code, 0xff);
91
92         g_assert ((code - start) < sizeof(start));
93         mono_arch_flush_icache (start, code - start);
94         return start;
95 }
96
97 /*
98  * mono_arch_get_call_filter:
99  *
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).
103  *
104  * This function is invoked as
105  *      call_handler (MonoContext *ctx, handler)
106  *
107  * Where 'handler' is a function to be invoked as:
108  *      handler (void)
109  */
110 gpointer
111 mono_arch_get_call_filter (void)
112 {
113         static guint8 start [320];
114         static int inited = 0;
115         guint8 *code;
116         int alloc_size;
117         int offset;
118
119         if (inited)
120                 return start;
121
122         inited = 1;
123         code = start;
124
125         alloc_size = 64;
126         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
127
128         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
129         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
130
131         /* Save global registers on stack (s0 - s7) */
132         offset = 16;
133         mips_sw (code, mips_s0, mips_sp, offset); offset += IREG_SIZE;
134         mips_sw (code, mips_s1, mips_sp, offset); offset += IREG_SIZE;
135         mips_sw (code, mips_s2, mips_sp, offset); offset += IREG_SIZE;
136         mips_sw (code, mips_s3, mips_sp, offset); offset += IREG_SIZE;
137         mips_sw (code, mips_s4, mips_sp, offset); offset += IREG_SIZE;
138         mips_sw (code, mips_s5, mips_sp, offset); offset += IREG_SIZE;
139         mips_sw (code, mips_s6, mips_sp, offset); offset += IREG_SIZE;
140         mips_sw (code, mips_s7, mips_sp, offset); offset += IREG_SIZE;
141         mips_sw (code, mips_fp, mips_sp, offset); offset += IREG_SIZE;
142
143         /* Restore global registers from MonoContext, including the frame pointer */
144         mips_lw (code, mips_s0, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s0]));
145         mips_lw (code, mips_s1, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s1]));
146         mips_lw (code, mips_s2, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s2]));
147         mips_lw (code, mips_s3, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s3]));
148         mips_lw (code, mips_s4, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s4]));
149         mips_lw (code, mips_s5, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s5]));
150         mips_lw (code, mips_s6, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s6]));
151         mips_lw (code, mips_s7, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s7]));
152         mips_lw (code, mips_fp, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_fp]));
153
154         /* a1 is the handler to call */
155         mips_move (code, mips_t9, mips_a1);
156
157         /* jump to the saved IP */
158         mips_jalr (code, mips_t9, mips_ra);
159         mips_nop (code);
160
161         /* restore all regs from the stack */
162         offset = 16;
163         mips_lw (code, mips_s0, mips_sp, offset); offset += IREG_SIZE;
164         mips_lw (code, mips_s1, mips_sp, offset); offset += IREG_SIZE;
165         mips_lw (code, mips_s2, mips_sp, offset); offset += IREG_SIZE;
166         mips_lw (code, mips_s3, mips_sp, offset); offset += IREG_SIZE;
167         mips_lw (code, mips_s4, mips_sp, offset); offset += IREG_SIZE;
168         mips_lw (code, mips_s5, mips_sp, offset); offset += IREG_SIZE;
169         mips_lw (code, mips_s6, mips_sp, offset); offset += IREG_SIZE;
170         mips_lw (code, mips_s7, mips_sp, offset); offset += IREG_SIZE;
171         mips_lw (code, mips_fp, mips_sp, offset); offset += IREG_SIZE;
172
173         /* epilog */
174         mips_lw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
175         mips_addiu (code, mips_sp, mips_sp, alloc_size);
176         mips_jr (code, mips_ra);
177         mips_nop (code);
178
179         g_assert ((code - start) < sizeof(start));
180         mono_arch_flush_icache (start, code - start);
181         return start;
182 }
183
184 static void
185 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gboolean rethrow)
186 {
187         static void (*restore_context) (MonoContext *);
188         MonoContext ctx;
189
190 #ifdef DEBUG_EXCEPTIONS
191         g_print ("throw_exception: exc=%p eip=%p esp=%p rethrow=%d\n",
192                  exc, (void *)eip, (void *) esp, rethrow);
193 #endif
194
195         if (!restore_context)
196                 restore_context = mono_arch_get_restore_context ();
197
198         /* adjust eip so that it point into the call instruction */
199         eip -= 8;
200
201         setup_context (&ctx);
202
203         /*g_print  ("stack in throw: %p\n", esp);*/
204         memcpy (&ctx.sc_regs, (void *)(esp + MIPS_STACK_PARAM_OFFSET),
205                 sizeof (gulong) * MONO_SAVED_GREGS);
206 #if 0
207         memcpy (&ctx.sc_fpregs, fp_regs, sizeof (float) * MONO_SAVED_FREGS);
208 #else
209         memset (&ctx.sc_fpregs, 0, sizeof (float) * MONO_SAVED_FREGS);
210 #endif
211         MONO_CONTEXT_SET_IP (&ctx, eip);
212
213         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
214                 MonoException *mono_ex = (MonoException*)exc;
215                 if (!rethrow)
216                         mono_ex->stack_trace = NULL;
217         }
218         mono_handle_exception (&ctx, exc, (void *)eip, FALSE);
219 #ifdef DEBUG_EXCEPTIONS
220         g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
221                  (void *) ctx.sc_pc, (void *) ctx.sc_regs[mips_sp],
222                  (void *) ctx.sc_regs[mips_fp], &ctx);
223 #endif
224         restore_context (&ctx);
225
226         g_assert_not_reached ();
227 }
228
229 /**
230  * arch_get_throw_exception_generic:
231  *
232  * Returns a function pointer which can be used to raise 
233  * exceptions. The returned function has the following 
234  * signature: void (*func) (MonoException *exc); or
235  * void (*func) (char *exc_name);
236  *
237  */
238 static gpointer 
239 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gboolean rethrow)
240 {
241         guint8 *code;
242         int alloc_size, pos, i;
243
244         code = start;
245
246         //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
247
248         pos = 0;
249         /* XXX - save all the FP regs on the stack ? */
250
251         pos += MONO_MAX_IREGS * sizeof(guint32);
252
253         alloc_size = MIPS_MINIMAL_STACK_SIZE + pos + 64;
254         // align to MIPS_STACK_ALIGNMENT bytes
255         alloc_size += MIPS_STACK_ALIGNMENT - 1;
256         alloc_size &= ~(MIPS_STACK_ALIGNMENT - 1);
257
258         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
259         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
260         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
261
262         /* Save all the regs on the stack */
263         for (i = 0; i < MONO_MAX_IREGS; i++) {
264                 if (i != mips_sp)
265                         mips_sw (code, i, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
266                 else {
267                         mips_addiu (code, mips_at, mips_sp, alloc_size);
268                         mips_sw (code, mips_at, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
269                 }
270         }
271
272         if (by_name) {
273                 mips_move (code, mips_a2, mips_a0);
274                 mips_load (code, mips_a0, mono_defaults.corlib);
275                 mips_load (code, mips_a1, "System");
276                 mips_load (code, mips_t9, mono_exception_from_name);
277                 mips_jalr (code, mips_t9, mips_ra);
278                 mips_nop (code);
279                 mips_move (code, mips_a0, mips_v0);
280         }
281         /* call throw_exception (exc, ip, sp, rethrow) */
282
283         /* exc is already in place in a0 */
284
285         /* pointer to ip */
286         if (by_name)
287                 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
288         else
289                 mips_move (code, mips_a1, mips_ra);
290
291         /* current sp & rethrow */
292         mips_move (code, mips_a2, mips_sp);
293         mips_addiu (code, mips_a3, mips_zero, rethrow);
294
295         mips_load (code, mips_t9, throw_exception);
296         mips_jr (code, mips_t9);
297         mips_nop (code);
298         /* we should never reach this breakpoint */
299         mips_break (code, 0xfe);
300
301         g_assert ((code - start) < size);
302         mono_arch_flush_icache (start, code - start);
303         return start;
304 }
305
306 /**
307  * mono_arch_get_rethrow_exception:
308  *
309  * Returns a function pointer which can be used to rethrow 
310  * exceptions. The returned function has the following 
311  * signature: void (*func) (MonoException *exc); 
312  *
313  */
314 gpointer
315 mono_arch_get_rethrow_exception (void)
316 {
317         static guint8 start [GENERIC_EXCEPTION_SIZE];
318         static int inited = 0;
319
320         if (inited)
321                 return start;
322         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
323         inited = 1;
324         return start;
325 }
326
327 /**
328  * arch_get_throw_exception:
329  *
330  * Returns a function pointer which can be used to raise 
331  * exceptions. The returned function has the following 
332  * signature: void (*func) (MonoException *exc); 
333  * For example to raise an arithmetic exception you can use:
334  *
335  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
336  * x86_call_code (code, arch_get_throw_exception ()); 
337  *
338  */
339 gpointer 
340 mono_arch_get_throw_exception (void)
341 {
342         static guint8 start [GENERIC_EXCEPTION_SIZE];
343         static int inited = 0;
344
345         if (inited)
346                 return start;
347         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
348         inited = 1;
349         return start;
350 }
351
352 /**
353  * arch_get_throw_exception_by_name:
354  *
355  * Returns a function pointer which can be used to raise 
356  * corlib exceptions. The returned function has the following 
357  * signature: void (*func) (char *exc_name); 
358  * For example to raise an arithmetic exception you can use:
359  *
360  * x86_push_imm (code, "ArithmeticException"); 
361  * x86_call_code (code, arch_get_throw_exception_by_name ()); 
362  *
363  */
364 gpointer 
365 mono_arch_get_throw_exception_by_name (void)
366 {
367         static guint8 start [GENERIC_EXCEPTION_SIZE];
368         static int inited = 0;
369
370         if (inited)
371                 return start;
372         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
373         inited = 1;
374         return start;
375 }       
376
377 static MonoArray *
378 glist_to_array (GList *list, MonoClass *eclass) 
379 {
380         MonoDomain *domain = mono_domain_get ();
381         MonoArray *res;
382         int len, i;
383
384         if (!list)
385                 return NULL;
386
387         len = g_list_length (list);
388         res = mono_array_new (domain, eclass, len);
389
390         for (i = 0; list; list = list->next, i++)
391                 mono_array_set (res, gpointer, i, list->data);
392
393         return res;
394 }
395
396 /* mono_arch_find_jit_info:
397  *
398  * This function is used to gather information from @ctx. It returns the 
399  * MonoJitInfo of the corresponding function, unwinds one stack frame and
400  * stores the resulting context into @new_ctx. It also stores a string 
401  * describing the stack location into @trace (if not NULL), and modifies
402  * the @lmf if necessary. @native_offset return the IP offset from the 
403  * start of the function or -1 if that info is not available.
404  */
405 MonoJitInfo *
406 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
407                          MonoJitInfo *res, MonoJitInfo *prev_ji,
408                          MonoContext *ctx, MonoContext *new_ctx,
409                          MonoLMF **lmf, gboolean *managed)
410 {
411         MonoJitInfo *ji;
412         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
413         gpointer fp = MONO_CONTEXT_GET_BP (ctx);
414         guint32 sp;
415
416         /* Avoid costly table lookup during stack overflow */
417         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
418                 ji = prev_ji;
419         else
420                 ji = mono_jit_info_table_find (domain, ip);
421
422         if (managed)
423                 *managed = FALSE;
424
425         memcpy (new_ctx, ctx, sizeof (MonoContext));
426
427         if (ji != NULL) {
428                 int i;
429                 gint32 address;
430                 int offset = 0;
431
432                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
433                         /* remove any unused lmf */
434                         *lmf = (*lmf)->previous_lmf;
435                 }
436
437                 address = (char *)ip - (char *)ji->code_start;
438
439                 if (managed)
440                         if (!ji->method->wrapper_type)
441                                 *managed = TRUE;
442
443                 /* My stack frame */
444                 fp = MONO_CONTEXT_GET_BP (ctx);
445
446                 /* Compute the previous stack frame */
447                 sp = (guint32)(fp) - (short)(*(guint32 *)(ji->code_start));
448
449                 /* Sanity check the frame */
450                 if (!sp || (sp == 0xffffffff)
451                     || (sp & 0x07) || (sp < 64*1024)) {
452 #ifdef DEBUG_EXCEPTIONS
453                         g_print ("mono_arch_find_jit_info: bad stack sp=%p\n", (void *) sp);
454 #endif
455                         return (gpointer)-1;
456                 }
457
458                 if (ji->method->save_lmf && 0) {
459                         /* only enable this when prologue stops emitting
460                          * normal save of s-regs when save_lmf is true.
461                          * Will have to sync with prologue code at that point.
462                          */
463                         memcpy (&new_ctx->sc_fpregs,
464                                 (char*)sp - sizeof (float) * MONO_SAVED_FREGS,
465                                 sizeof (float) * MONO_SAVED_FREGS);
466                         memcpy (&new_ctx->sc_regs,
467                                 (char*)sp - sizeof (float) * MONO_SAVED_FREGS - sizeof (gulong) * MONO_SAVED_GREGS,
468                                 sizeof (gulong) * MONO_SAVED_GREGS);
469                 } else if (ji->used_regs) {
470                         guint32 *insn;
471                         guint32 mask = ji->used_regs;
472
473                         /* these all happen before adjustment of fp */
474                         /* Look for sw ??, ????(sp) */
475                         insn = ((guint32 *)ji->code_start) + 1;
476                         while (!*insn || (*insn & 0xffe00000) == 0xafa00000) {
477                                 int reg = (*insn >> 16) & 0x1f;
478                                 mask &= ~(1 << reg);
479                                 new_ctx->sc_regs [reg] = *(guint32 *)(((guint32)fp) + (short)(*insn & 0x0000ffff));
480                                 insn++;
481                         }
482                         MONO_CONTEXT_SET_SP (new_ctx, sp);
483                         MONO_CONTEXT_SET_BP (new_ctx, sp);
484                         /* assert that we found all registers we were supposed to */
485                         g_assert (!mask);
486                 }
487                 /* we substract 8, so that the IP points into the call instruction */
488                 MONO_CONTEXT_SET_IP (new_ctx, new_ctx->sc_regs[mips_ra] - 8);
489
490                 /* Sanity check -- we should have made progress here */
491                 g_assert (new_ctx->sc_pc != ctx->sc_pc);
492                 return ji;
493         } else if (*lmf) {
494                 if (!(*lmf)->method) {
495 #ifdef DEBUG_EXCEPTIONS
496                         g_print ("mono_arch_find_jit_info: bad lmf @ %p\n", (void *) *lmf);
497 #endif
498                         return (gpointer)-1;
499                 }
500                 g_assert (((*lmf)->magic == MIPS_LMF_MAGIC1) || ((*lmf)->magic == MIPS_LMF_MAGIC2));
501
502                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
503                 } else {
504                         memset (res, 0, sizeof (MonoJitInfo));
505                         res->method = (*lmf)->method;
506                 }
507                 memcpy (&new_ctx->sc_regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
508                 memcpy (&new_ctx->sc_fpregs, (*lmf)->fregs, sizeof (float) * MONO_SAVED_FREGS);
509                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
510                 /* ensure that we've made progress */
511                 g_assert (new_ctx->sc_pc != ctx->sc_pc);
512                 *lmf = (*lmf)->previous_lmf;
513
514                 return ji ? ji : res;
515         }
516
517         return NULL;
518 }
519
520 void
521 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
522 {
523         int i;
524         struct sigcontext *ctx = (struct sigcontext *)sigctx;
525
526         mctx->sc_pc = ctx->sc_pc;
527         for (i = 0; i < 32; ++i) {
528                 mctx->sc_regs[i] = ctx->sc_regs[i];
529                 mctx->sc_fpregs[i] = ctx->sc_fpregs[i];
530         }
531 }
532
533 void
534 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
535 {
536         int i;
537         struct sigcontext *ctx = (struct sigcontext *)sigctx;
538
539         ctx->sc_pc = mctx->sc_pc;
540         for (i = 0; i < 32; ++i) {
541                 ctx->sc_regs[i] = mctx->sc_regs[i];
542                 ctx->sc_fpregs[i] = mctx->sc_fpregs[i];
543         }
544 }       
545
546 gpointer
547 mono_arch_ip_from_context (void *sigctx)
548 {
549         struct sigcontext *ctx = (struct sigcontext *)sigctx;
550         return (gpointer)(guint32)ctx->sc_pc;
551 }
552
553 /*
554  * This is the function called from the signal handler
555  */
556 gboolean
557 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
558 {
559         MonoContext mctx;
560         gboolean result;
561         
562         mono_arch_sigctx_to_monoctx (ctx, &mctx);
563 #ifdef DEBUG_EXCEPTIONS
564         g_print ("mono_arch_handle_exception: pc=%p\n", (void *) mctx.sc_pc);
565 #endif
566         mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_pc, test_only);
567         result = TRUE;
568
569 #ifdef DEBUG_EXCEPTIONS
570         g_print ("mono_arch_handle_exception: restore pc=%p\n", (void *)mctx.sc_pc);
571 #endif
572         /* restore the context so that returning from the signal handler
573          * will invoke the catch clause 
574          */
575         mono_arch_monoctx_to_sigctx (&mctx, ctx);
576
577         return result;
578 }
579
580
581 gboolean
582 mono_arch_has_unwind_info (gconstpointer addr)
583 {
584         return FALSE;
585 }
586