13aff78f9de3f0b47a2541b7d32e58a4059ecfdb
[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         memset (&ctx.sc_fpregs, 0, sizeof (mips_freg) * MONO_SAVED_FREGS);
207         MONO_CONTEXT_SET_IP (&ctx, eip);
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, (void *)eip, FALSE);
215 #ifdef DEBUG_EXCEPTIONS
216         g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
217                  (void *) ctx.sc_pc, (void *) ctx.sc_regs[mips_sp],
218                  (void *) ctx.sc_regs[mips_fp], &ctx);
219 #endif
220         restore_context (&ctx);
221
222         g_assert_not_reached ();
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) (char *exc_name);
232  *
233  */
234 static gpointer 
235 mono_arch_get_throw_exception_generic (guint8 *start, int size, int corlib, gboolean rethrow)
236 {
237         guint8 *code;
238         int alloc_size, pos, i;
239
240         code = start;
241
242         //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
243
244         pos = 0;
245         /* XXX - save all the FP regs on the stack ? */
246
247         pos += MONO_MAX_IREGS * sizeof(guint32);
248
249         alloc_size = MIPS_MINIMAL_STACK_SIZE + pos + 64;
250         // align to MIPS_STACK_ALIGNMENT bytes
251         alloc_size += MIPS_STACK_ALIGNMENT - 1;
252         alloc_size &= ~(MIPS_STACK_ALIGNMENT - 1);
253
254         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
255         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
256         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
257
258         /* Save all the regs on the stack */
259         for (i = 0; i < MONO_MAX_IREGS; i++) {
260                 if (i != mips_sp)
261                         MIPS_SW (code, i, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
262                 else {
263                         mips_addiu (code, mips_at, mips_sp, alloc_size);
264                         MIPS_SW (code, mips_at, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
265                 }
266         }
267
268         if (corlib) {
269                 mips_move (code, mips_a1, mips_a0);
270                 mips_load (code, mips_a0, mono_defaults.corlib);
271                 mips_load (code, mips_t9, mono_exception_from_token);
272                 mips_jalr (code, mips_t9, mips_ra);
273                 mips_nop (code);
274                 mips_move (code, mips_a0, mips_v0);
275         }
276         /* call throw_exception (exc, ip, sp, rethrow) */
277
278         /* exc is already in place in a0 */
279
280         /* pointer to ip */
281         if (corlib)
282                 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
283         else
284                 mips_move (code, mips_a1, mips_ra);
285
286         /* current sp & rethrow */
287         mips_move (code, mips_a2, mips_sp);
288         mips_addiu (code, mips_a3, mips_zero, rethrow);
289
290         mips_load (code, mips_t9, throw_exception);
291         mips_jr (code, mips_t9);
292         mips_nop (code);
293         /* we should never reach this breakpoint */
294         mips_break (code, 0xfe);
295
296         g_assert ((code - start) < size);
297         mono_arch_flush_icache (start, code - start);
298         return start;
299 }
300
301 /**
302  * mono_arch_get_rethrow_exception:
303  *
304  * Returns a function pointer which can be used to rethrow 
305  * exceptions. The returned function has the following 
306  * signature: void (*func) (MonoException *exc); 
307  *
308  */
309 gpointer
310 mono_arch_get_rethrow_exception (void)
311 {
312         static guint8 start [GENERIC_EXCEPTION_SIZE];
313         static int inited = 0;
314
315         if (inited)
316                 return start;
317         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
318         inited = 1;
319         return start;
320 }
321
322 /**
323  * arch_get_throw_exception:
324  *
325  * Returns a function pointer which can be used to raise 
326  * exceptions. The returned function has the following 
327  * signature: void (*func) (MonoException *exc); 
328  * For example to raise an arithmetic exception you can use:
329  *
330  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
331  * x86_call_code (code, arch_get_throw_exception ()); 
332  *
333  */
334 gpointer 
335 mono_arch_get_throw_exception (void)
336 {
337         static guint8 start [GENERIC_EXCEPTION_SIZE];
338         static int inited = 0;
339
340         if (inited)
341                 return start;
342         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
343         inited = 1;
344         return start;
345 }
346
347 gpointer 
348 mono_arch_get_throw_exception_by_name (void)
349 {
350         guint8 *start, *code;
351         int size = 64;
352
353         /* Not used on MIPS */  
354         start = code = mono_global_codeman_reserve (size);
355         mips_break (code, 0xfd);
356         mono_arch_flush_icache (start, code - start);
357         return start;
358 }
359
360 /**
361  * mono_arch_get_throw_corlib_exception:
362  *
363  * Returns a function pointer which can be used to raise 
364  * corlib exceptions. The returned function has the following 
365  * signature: void (*func) (guint32 ex_token, guint32 offset); 
366  * On MIPS, the offset argument is missing.
367  */
368 gpointer 
369 mono_arch_get_throw_corlib_exception (void)
370 {
371         static guint8 start [GENERIC_EXCEPTION_SIZE];
372         static int inited = 0;
373
374         if (inited)
375                 return start;
376         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
377         inited = 1;
378         return start;
379 }       
380
381 static MonoArray *
382 glist_to_array (GList *list, MonoClass *eclass) 
383 {
384         MonoDomain *domain = mono_domain_get ();
385         MonoArray *res;
386         int len, i;
387
388         if (!list)
389                 return NULL;
390
391         len = g_list_length (list);
392         res = mono_array_new (domain, eclass, len);
393
394         for (i = 0; list; list = list->next, i++)
395                 mono_array_set (res, gpointer, i, list->data);
396
397         return res;
398 }
399
400 /* mono_arch_find_jit_info:
401  *
402  * This function is used to gather information from @ctx. It returns the 
403  * MonoJitInfo of the corresponding function, unwinds one stack frame and
404  * stores the resulting context into @new_ctx. It also stores a string 
405  * describing the stack location into @trace (if not NULL), and modifies
406  * the @lmf if necessary. @native_offset return the IP offset from the 
407  * start of the function or -1 if that info is not available.
408  */
409 MonoJitInfo *
410 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
411                          MonoJitInfo *res, MonoJitInfo *prev_ji,
412                          MonoContext *ctx, MonoContext *new_ctx,
413                          MonoLMF **lmf, gboolean *managed)
414 {
415         MonoJitInfo *ji;
416         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
417         gpointer fp = MONO_CONTEXT_GET_BP (ctx);
418         guint32 sp;
419
420         /* Avoid costly table lookup during stack overflow */
421         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
422                 ji = prev_ji;
423         else
424                 ji = mono_jit_info_table_find (domain, ip);
425
426         if (managed)
427                 *managed = FALSE;
428
429         memcpy (new_ctx, ctx, sizeof (MonoContext));
430
431         if (ji != NULL) {
432                 int i;
433                 gint32 address;
434                 int offset = 0;
435
436                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
437                         /* remove any unused lmf */
438                         *lmf = (*lmf)->previous_lmf;
439                 }
440
441                 address = (char *)ip - (char *)ji->code_start;
442
443                 if (managed)
444                         if (!ji->method->wrapper_type)
445                                 *managed = TRUE;
446
447                 /* My stack frame */
448                 fp = MONO_CONTEXT_GET_BP (ctx);
449
450                 /* Compute the previous stack frame */
451                 sp = (guint32)(fp) - (short)(*(guint32 *)(ji->code_start));
452
453                 /* Sanity check the frame */
454                 if (!sp || (sp == 0xffffffff)
455                     || (sp & 0x07) || (sp < 64*1024)) {
456 #ifdef DEBUG_EXCEPTIONS
457                         g_print ("mono_arch_find_jit_info: bad stack sp=%p\n", (void *) sp);
458 #endif
459                         return (gpointer)-1;
460                 }
461
462                 if (ji->method->save_lmf && 0) {
463                         /* only enable this when prologue stops emitting
464                          * normal save of s-regs when save_lmf is true.
465                          * Will have to sync with prologue code at that point.
466                          */
467                         memcpy (&new_ctx->sc_fpregs,
468                                 (char*)sp - sizeof (float) * MONO_SAVED_FREGS,
469                                 sizeof (float) * MONO_SAVED_FREGS);
470                         memcpy (&new_ctx->sc_regs,
471                                 (char*)sp - sizeof (float) * MONO_SAVED_FREGS - sizeof (gulong) * MONO_SAVED_GREGS,
472                                 sizeof (gulong) * MONO_SAVED_GREGS);
473                 } else if (ji->used_regs) {
474                         guint32 *insn;
475                         guint32 mask = ji->used_regs;
476
477                         /* these all happen before adjustment of fp */
478                         /* Look for sw ??, ????(sp) */
479                         insn = ((guint32 *)ji->code_start) + 1;
480                         while (!*insn || ((*insn & 0xffe00000) == 0xafa00000) || ((*insn & 0xffe00000) == 0xffa00000)) {
481                                 int reg = (*insn >> 16) & 0x1f;
482                                 guint32 addr = (((guint32)fp) + (short)(*insn & 0x0000ffff));
483
484                                 mask &= ~(1 << reg);
485                                 if ((*insn & 0xffe00000) == 0xafa00000)
486                                         new_ctx->sc_regs [reg] = *(guint32 *)addr;
487                                 else
488                                         new_ctx->sc_regs [reg] = *(guint64 *)addr;
489                                 insn++;
490                         }
491                         MONO_CONTEXT_SET_SP (new_ctx, sp);
492                         MONO_CONTEXT_SET_BP (new_ctx, sp);
493                         /* assert that we found all registers we were supposed to */
494                         g_assert (!mask);
495                 }
496                 /* we substract 8, so that the IP points into the call instruction */
497                 MONO_CONTEXT_SET_IP (new_ctx, new_ctx->sc_regs[mips_ra] - 8);
498
499                 /* Sanity check -- we should have made progress here */
500                 g_assert (new_ctx->sc_pc != ctx->sc_pc);
501                 return ji;
502         } else if (*lmf) {
503                 if (!(*lmf)->method) {
504 #ifdef DEBUG_EXCEPTIONS
505                         g_print ("mono_arch_find_jit_info: bad lmf @ %p\n", (void *) *lmf);
506 #endif
507                         return (gpointer)-1;
508                 }
509                 g_assert (((*lmf)->magic == MIPS_LMF_MAGIC1) || ((*lmf)->magic == MIPS_LMF_MAGIC2));
510
511                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
512                 } else {
513                         memset (res, 0, MONO_SIZEOF_JIT_INFO);
514                         res->method = (*lmf)->method;
515                 }
516                 memcpy (&new_ctx->sc_regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
517                 memcpy (&new_ctx->sc_fpregs, (*lmf)->fregs, sizeof (float) * MONO_SAVED_FREGS);
518                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
519                 /* ensure that we've made progress */
520                 g_assert (new_ctx->sc_pc != ctx->sc_pc);
521                 *lmf = (*lmf)->previous_lmf;
522
523                 return ji ? ji : res;
524         }
525
526         return NULL;
527 }
528
529 void
530 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
531 {
532         int i;
533         struct sigcontext *ctx = (struct sigcontext *)sigctx;
534
535         mctx->sc_pc = ctx->sc_pc;
536         for (i = 0; i < 32; ++i) {
537                 mctx->sc_regs[i] = ctx->sc_regs[i];
538                 mctx->sc_fpregs[i] = ctx->sc_fpregs[i];
539         }
540 }
541
542 void
543 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
544 {
545         int i;
546         struct sigcontext *ctx = (struct sigcontext *)sigctx;
547
548         ctx->sc_pc = mctx->sc_pc;
549         for (i = 0; i < 32; ++i) {
550                 ctx->sc_regs[i] = mctx->sc_regs[i];
551                 ctx->sc_fpregs[i] = mctx->sc_fpregs[i];
552         }
553 }       
554
555 gpointer
556 mono_arch_ip_from_context (void *sigctx)
557 {
558         struct sigcontext *ctx = (struct sigcontext *)sigctx;
559         return (gpointer)(guint32)ctx->sc_pc;
560 }
561
562 /*
563  * This is the function called from the signal handler
564  */
565 gboolean
566 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
567 {
568         MonoContext mctx;
569         gboolean result;
570         
571         mono_arch_sigctx_to_monoctx (ctx, &mctx);
572 #ifdef DEBUG_EXCEPTIONS
573         g_print ("mono_arch_handle_exception: pc=%p\n", (void *) mctx.sc_pc);
574 #endif
575         mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_pc, test_only);
576         result = TRUE;
577
578 #ifdef DEBUG_EXCEPTIONS
579         g_print ("mono_arch_handle_exception: restore pc=%p\n", (void *)mctx.sc_pc);
580 #endif
581         /* restore the context so that returning from the signal handler
582          * will invoke the catch clause 
583          */
584         mono_arch_monoctx_to_sigctx (&mctx, ctx);
585
586         return result;
587 }
588
589
590 gboolean
591 mono_arch_has_unwind_info (gconstpointer addr)
592 {
593         return FALSE;
594 }
595