Merge pull request #200 from ch5oh/master
[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 /*
35  * mono_arch_get_restore_context:
36  *
37  * Returns a pointer to a method which restores a previously saved MonoContext.
38  * The first argument in a0 is the pointer to the MonoContext.
39  */
40 gpointer
41 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
42 {
43         int i;
44         guint8 *code;
45         static guint8 start [512];
46         static int inited = 0;
47         guint32 iregs_to_restore;
48
49         g_assert (!aot);
50         if (info)
51                 *info = NULL;
52
53         if (inited)
54                 return start;
55         inited = 1;
56         code = start;
57
58         mips_move (code, mips_at, mips_a0);
59
60         iregs_to_restore = (MONO_ARCH_CALLEE_SAVED_REGS \
61                             | (1 << mips_sp) | (1 << mips_ra));
62         for (i = 0; i < MONO_SAVED_GREGS; ++i) {
63                 //if (iregs_to_restore & (1 << i)) {
64                 if (i != mips_zero && i != mips_at) {
65                         MIPS_LW (code, i, mips_at, G_STRUCT_OFFSET (MonoContext, sc_regs[i]));
66                 }
67         }
68
69         /* Get the address to return to */
70         mips_lw (code, mips_t9, mips_at, G_STRUCT_OFFSET (MonoContext, sc_pc));
71
72         /* jump to the saved IP */
73         mips_jr (code, mips_t9);
74         mips_nop (code);
75
76         /* never reached */
77         mips_break (code, 0xff);
78
79         g_assert ((code - start) < sizeof(start));
80         mono_arch_flush_icache (start, code - start);
81         return start;
82 }
83
84 /*
85  * mono_arch_get_call_filter:
86  *
87  * Returns a pointer to a method which calls an exception filter. We
88  * also use this function to call finally handlers (we pass NULL as 
89  * @exc object in this case).
90  *
91  * This function is invoked as
92  *      call_handler (MonoContext *ctx, handler)
93  *
94  * Where 'handler' is a function to be invoked as:
95  *      handler (void)
96  */
97 gpointer
98 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
99 {
100         static guint8 start [320];
101         static int inited = 0;
102         guint8 *code;
103         int alloc_size;
104         int offset;
105
106         g_assert (!aot);
107         if (info)
108                 *info = NULL;
109
110         if (inited)
111                 return start;
112
113         inited = 1;
114         code = start;
115
116         alloc_size = 64;
117         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
118
119         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
120         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
121
122         /* Save global registers on stack (s0 - s7) */
123         offset = 16;
124         MIPS_SW (code, mips_s0, mips_sp, offset); offset += IREG_SIZE;
125         MIPS_SW (code, mips_s1, mips_sp, offset); offset += IREG_SIZE;
126         MIPS_SW (code, mips_s2, mips_sp, offset); offset += IREG_SIZE;
127         MIPS_SW (code, mips_s3, mips_sp, offset); offset += IREG_SIZE;
128         MIPS_SW (code, mips_s4, mips_sp, offset); offset += IREG_SIZE;
129         MIPS_SW (code, mips_s5, mips_sp, offset); offset += IREG_SIZE;
130         MIPS_SW (code, mips_s6, mips_sp, offset); offset += IREG_SIZE;
131         MIPS_SW (code, mips_s7, mips_sp, offset); offset += IREG_SIZE;
132         MIPS_SW (code, mips_fp, mips_sp, offset); offset += IREG_SIZE;
133
134         /* Restore global registers from MonoContext, including the frame pointer */
135         MIPS_LW (code, mips_s0, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s0]));
136         MIPS_LW (code, mips_s1, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s1]));
137         MIPS_LW (code, mips_s2, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s2]));
138         MIPS_LW (code, mips_s3, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s3]));
139         MIPS_LW (code, mips_s4, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s4]));
140         MIPS_LW (code, mips_s5, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s5]));
141         MIPS_LW (code, mips_s6, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s6]));
142         MIPS_LW (code, mips_s7, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s7]));
143         MIPS_LW (code, mips_fp, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_fp]));
144
145         /* a1 is the handler to call */
146         mips_move (code, mips_t9, mips_a1);
147
148         /* jump to the saved IP */
149         mips_jalr (code, mips_t9, mips_ra);
150         mips_nop (code);
151
152         /* restore all regs from the stack */
153         offset = 16;
154         MIPS_LW (code, mips_s0, mips_sp, offset); offset += IREG_SIZE;
155         MIPS_LW (code, mips_s1, mips_sp, offset); offset += IREG_SIZE;
156         MIPS_LW (code, mips_s2, mips_sp, offset); offset += IREG_SIZE;
157         MIPS_LW (code, mips_s3, mips_sp, offset); offset += IREG_SIZE;
158         MIPS_LW (code, mips_s4, mips_sp, offset); offset += IREG_SIZE;
159         MIPS_LW (code, mips_s5, mips_sp, offset); offset += IREG_SIZE;
160         MIPS_LW (code, mips_s6, mips_sp, offset); offset += IREG_SIZE;
161         MIPS_LW (code, mips_s7, mips_sp, offset); offset += IREG_SIZE;
162         MIPS_LW (code, mips_fp, mips_sp, offset); offset += IREG_SIZE;
163
164         /* epilog */
165         mips_lw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
166         mips_addiu (code, mips_sp, mips_sp, alloc_size);
167         mips_jr (code, mips_ra);
168         mips_nop (code);
169
170         g_assert ((code - start) < sizeof(start));
171         mono_arch_flush_icache (start, code - start);
172         return start;
173 }
174
175 static void
176 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gboolean rethrow)
177 {
178         static void (*restore_context) (MonoContext *);
179         MonoContext ctx;
180
181 #ifdef DEBUG_EXCEPTIONS
182         g_print ("throw_exception: exc=%p eip=%p esp=%p rethrow=%d\n",
183                  exc, (void *)eip, (void *) esp, rethrow);
184 #endif
185
186         if (!restore_context)
187                 restore_context = mono_get_restore_context ();
188
189         /* adjust eip so that it point into the call instruction */
190         eip -= 8;
191
192         memset (&ctx, 0, sizeof (MonoContext));
193
194         /*g_print  ("stack in throw: %p\n", esp);*/
195         memcpy (&ctx.sc_regs, (void *)(esp + MIPS_STACK_PARAM_OFFSET),
196                 sizeof (gulong) * MONO_SAVED_GREGS);
197         memset (&ctx.sc_fpregs, 0, sizeof (mips_freg) * MONO_SAVED_FREGS);
198         MONO_CONTEXT_SET_IP (&ctx, eip);
199
200         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
201                 MonoException *mono_ex = (MonoException*)exc;
202                 if (!rethrow)
203                         mono_ex->stack_trace = NULL;
204         }
205         mono_handle_exception (&ctx, exc);
206 #ifdef DEBUG_EXCEPTIONS
207         g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
208                  (void *) ctx.sc_pc, (void *) ctx.sc_regs[mips_sp],
209                  (void *) ctx.sc_regs[mips_fp], &ctx);
210 #endif
211         restore_context (&ctx);
212
213         g_assert_not_reached ();
214 }
215
216 /**
217  * arch_get_throw_exception_generic:
218  *
219  * Returns a function pointer which can be used to raise 
220  * exceptions. The returned function has the following 
221  * signature: void (*func) (MonoException *exc); or
222  * void (*func) (char *exc_name);
223  *
224  */
225 static gpointer 
226 mono_arch_get_throw_exception_generic (guint8 *start, int size, int corlib, gboolean rethrow)
227 {
228         guint8 *code;
229         int alloc_size, pos, i;
230
231         code = start;
232
233         //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
234
235         pos = 0;
236         /* XXX - save all the FP regs on the stack ? */
237
238         pos += MONO_MAX_IREGS * sizeof(guint32);
239
240         alloc_size = MIPS_MINIMAL_STACK_SIZE + pos + 64;
241         // align to MIPS_STACK_ALIGNMENT bytes
242         alloc_size += MIPS_STACK_ALIGNMENT - 1;
243         alloc_size &= ~(MIPS_STACK_ALIGNMENT - 1);
244
245         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
246         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
247         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
248
249         /* Save all the regs on the stack */
250         for (i = 0; i < MONO_MAX_IREGS; i++) {
251                 if (i != mips_sp)
252                         MIPS_SW (code, i, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
253                 else {
254                         mips_addiu (code, mips_at, mips_sp, alloc_size);
255                         MIPS_SW (code, mips_at, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
256                 }
257         }
258
259         if (corlib) {
260                 mips_move (code, mips_a1, mips_a0);
261                 mips_load (code, mips_a0, mono_defaults.corlib);
262                 mips_load (code, mips_t9, mono_exception_from_token);
263                 mips_jalr (code, mips_t9, mips_ra);
264                 mips_nop (code);
265                 mips_move (code, mips_a0, mips_v0);
266         }
267         /* call throw_exception (exc, ip, sp, rethrow) */
268
269         /* exc is already in place in a0 */
270
271         /* pointer to ip */
272         if (corlib)
273                 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
274         else
275                 mips_move (code, mips_a1, mips_ra);
276
277         /* current sp & rethrow */
278         mips_move (code, mips_a2, mips_sp);
279         mips_addiu (code, mips_a3, mips_zero, rethrow);
280
281         mips_load (code, mips_t9, throw_exception);
282         mips_jr (code, mips_t9);
283         mips_nop (code);
284         /* we should never reach this breakpoint */
285         mips_break (code, 0xfe);
286
287         g_assert ((code - start) < size);
288         mono_arch_flush_icache (start, code - start);
289         return start;
290 }
291
292 /**
293  * mono_arch_get_rethrow_exception:
294  *
295  * Returns a function pointer which can be used to rethrow 
296  * exceptions. The returned function has the following 
297  * signature: void (*func) (MonoException *exc); 
298  *
299  */
300 gpointer
301 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
302 {
303         static guint8 start [GENERIC_EXCEPTION_SIZE];
304         static int inited = 0;
305
306         g_assert (!aot);
307         if (info)
308                 *info = NULL;
309
310         if (inited)
311                 return start;
312         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
313         inited = 1;
314         return start;
315 }
316
317 /**
318  * arch_get_throw_exception:
319  *
320  * Returns a function pointer which can be used to raise 
321  * exceptions. The returned function has the following 
322  * signature: void (*func) (MonoException *exc); 
323  * For example to raise an arithmetic exception you can use:
324  *
325  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
326  * x86_call_code (code, arch_get_throw_exception ()); 
327  *
328  */
329 gpointer
330 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
331 {
332         static guint8 start [GENERIC_EXCEPTION_SIZE];
333         static int inited = 0;
334
335         g_assert (!aot);
336         if (info)
337                 *info = NULL;
338
339         if (inited)
340                 return start;
341         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
342         inited = 1;
343         return start;
344 }
345
346 gpointer 
347 mono_arch_get_throw_exception_by_name (void)
348 {
349         guint8 *start, *code;
350         int size = 64;
351
352         /* Not used on MIPS */  
353         start = code = mono_global_codeman_reserve (size);
354         mips_break (code, 0xfd);
355         mono_arch_flush_icache (start, code - start);
356         return start;
357 }
358
359 /**
360  * mono_arch_get_throw_corlib_exception:
361  *
362  * Returns a function pointer which can be used to raise 
363  * corlib exceptions. The returned function has the following 
364  * signature: void (*func) (guint32 ex_token, guint32 offset); 
365  * On MIPS, the offset argument is missing.
366  */
367 gpointer
368 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
369 {
370         static guint8 start [GENERIC_EXCEPTION_SIZE];
371         static int inited = 0;
372
373         g_assert (!aot);
374         if (info)
375                 *info = NULL;
376
377         if (inited)
378                 return start;
379         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
380         inited = 1;
381         return start;
382 }
383
384 /*
385  * mono_arch_find_jit_info:
386  *
387  * This function is used to gather information from @ctx, and store it in @frame_info.
388  * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
389  * is modified if needed.
390  * Returns TRUE on success, FALSE otherwise.
391  */
392 gboolean
393 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
394                                                          MonoJitInfo *ji, MonoContext *ctx, 
395                                                          MonoContext *new_ctx, MonoLMF **lmf, 
396                                                          mgreg_t **save_locations,
397                                                          StackFrameInfo *frame)
398 {
399         memset (frame, 0, sizeof (StackFrameInfo));
400         frame->ji = ji;
401
402         *new_ctx = *ctx;
403
404         if (ji != NULL) {
405                 int i;
406                 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
407                 mgreg_t regs [MONO_MAX_IREGS + 1];
408                 guint8 *cfa;
409                 guint32 unwind_info_len;
410                 guint8 *unwind_info;
411
412                 frame->type = FRAME_TYPE_MANAGED;
413
414                 if (ji->from_aot)
415                         unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
416                 else
417                         unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
418
419                 for (i = 0; i < MONO_MAX_IREGS; ++i)
420                         regs [i] = new_ctx->sc_regs [i];
421
422                 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
423                                                    (guint8*)ji->code_start + ji->code_size,
424                                                    ip, regs, MONO_MAX_IREGS,
425                                                    save_locations, MONO_MAX_IREGS, &cfa);
426
427                 for (i = 0; i < MONO_MAX_IREGS; ++i)
428                         new_ctx->sc_regs [i] = regs [i];
429                 new_ctx->sc_pc = regs [mips_ra];
430                 new_ctx->sc_regs [mips_sp] = (mgreg_t)cfa;
431
432                 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->iregs [mips_sp])) {
433                         /* remove any unused lmf */
434                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
435                 }
436
437                 /* we substract 8, so that the IP points into the call instruction */
438                 MONO_CONTEXT_SET_IP (new_ctx, new_ctx->sc_pc - 8);
439
440                 /* Sanity check -- we should have made progress here */
441                 g_assert (MONO_CONTEXT_GET_SP (new_ctx) != MONO_CONTEXT_GET_SP (ctx));
442                 return TRUE;
443         } else if (*lmf) {
444
445                 if (((mgreg_t)(*lmf)->previous_lmf) & 2) {
446                         /* 
447                          * This LMF entry is created by the soft debug code to mark transitions to
448                          * managed code done during invokes.
449                          */
450                         MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
451
452                         g_assert (ext->debugger_invoke);
453
454                         memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
455
456                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
457
458                         frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
459
460                         return TRUE;
461                 }
462
463                 if (!(*lmf)->method) {
464 #ifdef DEBUG_EXCEPTIONS
465                         g_print ("mono_arch_find_jit_info: bad lmf @ %p\n", (void *) *lmf);
466 #endif
467                         return FALSE;
468                 }
469                 g_assert (((*lmf)->magic == MIPS_LMF_MAGIC1) || ((*lmf)->magic == MIPS_LMF_MAGIC2));
470
471                 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
472                 if (!ji) {
473                         // FIXME: This can happen with multiple appdomains (bug #444383)
474                         return FALSE;
475                 }
476
477                 frame->ji = ji;
478                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
479
480                 memcpy (&new_ctx->sc_regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
481                 memcpy (&new_ctx->sc_fpregs, (*lmf)->fregs, sizeof (float) * MONO_SAVED_FREGS);
482                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
483                 /* ensure that we've made progress */
484                 g_assert (new_ctx->sc_pc != ctx->sc_pc);
485
486                 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
487
488                 return TRUE;
489         }
490
491         return FALSE;
492 }
493
494 void
495 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
496 {
497         int i;
498         struct sigcontext *ctx = (struct sigcontext *)sigctx;
499
500         mctx->sc_pc = ctx->sc_pc;
501         for (i = 0; i < 32; ++i) {
502                 mctx->sc_regs[i] = ctx->sc_regs[i];
503                 mctx->sc_fpregs[i] = ctx->sc_fpregs[i];
504         }
505 }
506
507 void
508 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
509 {
510         int i;
511         struct sigcontext *ctx = (struct sigcontext *)sigctx;
512
513         ctx->sc_pc = mctx->sc_pc;
514         for (i = 0; i < 32; ++i) {
515                 ctx->sc_regs[i] = mctx->sc_regs[i];
516                 ctx->sc_fpregs[i] = mctx->sc_fpregs[i];
517         }
518 }       
519
520 gpointer
521 mono_arch_ip_from_context (void *sigctx)
522 {
523         struct sigcontext *ctx = (struct sigcontext *)sigctx;
524         return (gpointer)(guint32)ctx->sc_pc;
525 }
526
527 /*
528  * handle_exception:
529  *
530  *   Called by resuming from a signal handler.
531  */
532 static void
533 handle_signal_exception (gpointer obj)
534 {
535         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
536         MonoContext ctx;
537         static void (*restore_context) (MonoContext *);
538
539         if (!restore_context)
540                 restore_context = mono_get_restore_context ();
541
542         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
543
544         mono_handle_exception (&ctx, obj);
545
546         restore_context (&ctx);
547 }
548
549 /*
550  * This is the function called from the signal handler
551  */
552 gboolean
553 mono_arch_handle_exception (void *ctx, gpointer obj)
554 {
555 #if defined(MONO_CROSS_COMPILE)
556         g_assert_not_reached ();
557 #elif defined(MONO_ARCH_USE_SIGACTION)
558         void *sigctx = ctx;
559
560         /*
561          * Handling the exception in the signal handler is problematic, since the original
562          * signal is disabled, and we could run arbitrary code though the debugger. So
563          * resume into the normal stack and do most work there if possible.
564          */
565         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
566         guint64 sp = UCONTEXT_GREGS (sigctx) [mips_sp];
567
568         /* Pass the ctx parameter in TLS */
569         mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
570         /* The others in registers */
571         UCONTEXT_GREGS (sigctx)[mips_a0] = (gsize)obj;
572
573         /* Allocate a stack frame */
574         sp -= 256;
575         UCONTEXT_GREGS (sigctx)[mips_sp] = sp;
576
577         UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
578
579         return TRUE;
580 #else
581         MonoContext mctx;
582         gboolean result;
583
584         mono_arch_sigctx_to_monoctx (ctx, &mctx);
585
586         result = mono_handle_exception (&mctx, obj);
587         /* restore the context so that returning from the signal handler will invoke
588          * the catch clause 
589          */
590         mono_arch_monoctx_to_sigctx (&mctx, ctx);
591         return result;
592 #endif
593 }
594
595 /*
596  * mono_arch_setup_resume_sighandler_ctx:
597  *
598  *   Setup CTX so execution continues at FUNC.
599  */
600 void
601 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
602 {
603         MONO_CONTEXT_SET_IP (ctx,func);
604 }