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