Merge pull request #301 from directhex/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         static void (*restore_context) (MonoContext *);
178         MonoContext ctx;
179
180 #ifdef DEBUG_EXCEPTIONS
181         g_print ("throw_exception: exc=%p eip=%p esp=%p rethrow=%d\n",
182                  exc, (void *)eip, (void *) esp, rethrow);
183 #endif
184
185         if (!restore_context)
186                 restore_context = mono_get_restore_context ();
187
188         /* adjust eip so that it point into the call instruction */
189         eip -= 8;
190
191         memset (&ctx, 0, sizeof (MonoContext));
192
193         /*g_print  ("stack in throw: %p\n", esp);*/
194         memcpy (&ctx.sc_regs, (void *)(esp + MIPS_STACK_PARAM_OFFSET),
195                 sizeof (gulong) * MONO_SAVED_GREGS);
196         memset (&ctx.sc_fpregs, 0, sizeof (mips_freg) * MONO_SAVED_FREGS);
197         MONO_CONTEXT_SET_IP (&ctx, eip);
198
199         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
200                 MonoException *mono_ex = (MonoException*)exc;
201                 if (!rethrow)
202                         mono_ex->stack_trace = NULL;
203         }
204         mono_handle_exception (&ctx, exc);
205 #ifdef DEBUG_EXCEPTIONS
206         g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
207                  (void *) ctx.sc_pc, (void *) ctx.sc_regs[mips_sp],
208                  (void *) ctx.sc_regs[mips_fp], &ctx);
209 #endif
210         restore_context (&ctx);
211
212         g_assert_not_reached ();
213 }
214
215 /**
216  * arch_get_throw_exception_generic:
217  *
218  * Returns a function pointer which can be used to raise 
219  * exceptions. The returned function has the following 
220  * signature: void (*func) (MonoException *exc); or
221  * void (*func) (char *exc_name);
222  *
223  */
224 static gpointer 
225 mono_arch_get_throw_exception_generic (guint8 *start, int size, int corlib, gboolean rethrow)
226 {
227         guint8 *code;
228         int alloc_size, pos, i;
229
230         code = start;
231
232         //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
233
234         pos = 0;
235         /* XXX - save all the FP regs on the stack ? */
236
237         pos += MONO_MAX_IREGS * sizeof(guint32);
238
239         alloc_size = MIPS_MINIMAL_STACK_SIZE + pos + 64;
240         // align to MIPS_STACK_ALIGNMENT bytes
241         alloc_size += MIPS_STACK_ALIGNMENT - 1;
242         alloc_size &= ~(MIPS_STACK_ALIGNMENT - 1);
243
244         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
245         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
246         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
247
248         /* Save all the regs on the stack */
249         for (i = 0; i < MONO_MAX_IREGS; i++) {
250                 if (i != mips_sp)
251                         MIPS_SW (code, i, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
252                 else {
253                         mips_addiu (code, mips_at, mips_sp, alloc_size);
254                         MIPS_SW (code, mips_at, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
255                 }
256         }
257
258         if (corlib) {
259                 mips_move (code, mips_a1, mips_a0);
260                 mips_load (code, mips_a0, mono_defaults.corlib);
261                 mips_load (code, mips_t9, mono_exception_from_token);
262                 mips_jalr (code, mips_t9, mips_ra);
263                 mips_nop (code);
264                 mips_move (code, mips_a0, mips_v0);
265         }
266         /* call throw_exception (exc, ip, sp, rethrow) */
267
268         /* exc is already in place in a0 */
269
270         /* pointer to ip */
271         if (corlib)
272                 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
273         else
274                 mips_move (code, mips_a1, mips_ra);
275
276         /* current sp & rethrow */
277         mips_move (code, mips_a2, mips_sp);
278         mips_addiu (code, mips_a3, mips_zero, rethrow);
279
280         mips_load (code, mips_t9, throw_exception);
281         mips_jr (code, mips_t9);
282         mips_nop (code);
283         /* we should never reach this breakpoint */
284         mips_break (code, 0xfe);
285
286         g_assert ((code - start) < size);
287         mono_arch_flush_icache (start, code - start);
288         return start;
289 }
290
291 /**
292  * mono_arch_get_rethrow_exception:
293  *
294  * Returns a function pointer which can be used to rethrow 
295  * exceptions. The returned function has the following 
296  * signature: void (*func) (MonoException *exc); 
297  *
298  */
299 gpointer
300 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
301 {
302         static guint8 start [GENERIC_EXCEPTION_SIZE];
303         static int inited = 0;
304
305         g_assert (!aot);
306         if (info)
307                 *info = NULL;
308
309         if (inited)
310                 return start;
311         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
312         inited = 1;
313         return start;
314 }
315
316 /**
317  * arch_get_throw_exception:
318  *
319  * Returns a function pointer which can be used to raise 
320  * exceptions. The returned function has the following 
321  * signature: void (*func) (MonoException *exc); 
322  * For example to raise an arithmetic exception you can use:
323  *
324  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
325  * x86_call_code (code, arch_get_throw_exception ()); 
326  *
327  */
328 gpointer
329 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
330 {
331         static guint8 start [GENERIC_EXCEPTION_SIZE];
332         static int inited = 0;
333
334         g_assert (!aot);
335         if (info)
336                 *info = NULL;
337
338         if (inited)
339                 return start;
340         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
341         inited = 1;
342         return start;
343 }
344
345 gpointer 
346 mono_arch_get_throw_exception_by_name (void)
347 {
348         guint8 *start, *code;
349         int size = 64;
350
351         /* Not used on MIPS */  
352         start = code = mono_global_codeman_reserve (size);
353         mips_break (code, 0xfd);
354         mono_arch_flush_icache (start, code - start);
355         return start;
356 }
357
358 /**
359  * mono_arch_get_throw_corlib_exception:
360  *
361  * Returns a function pointer which can be used to raise 
362  * corlib exceptions. The returned function has the following 
363  * signature: void (*func) (guint32 ex_token, guint32 offset); 
364  * On MIPS, the offset argument is missing.
365  */
366 gpointer
367 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
368 {
369         static guint8 start [GENERIC_EXCEPTION_SIZE];
370         static int inited = 0;
371
372         g_assert (!aot);
373         if (info)
374                 *info = NULL;
375
376         if (inited)
377                 return start;
378         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
379         inited = 1;
380         return start;
381 }
382
383 /*
384  * mono_arch_find_jit_info:
385  *
386  * This function is used to gather information from @ctx, and store it in @frame_info.
387  * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
388  * is modified if needed.
389  * Returns TRUE on success, FALSE otherwise.
390  */
391 gboolean
392 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
393                                                          MonoJitInfo *ji, MonoContext *ctx, 
394                                                          MonoContext *new_ctx, MonoLMF **lmf, 
395                                                          mgreg_t **save_locations,
396                                                          StackFrameInfo *frame)
397 {
398         memset (frame, 0, sizeof (StackFrameInfo));
399         frame->ji = ji;
400
401         *new_ctx = *ctx;
402
403         if (ji != NULL) {
404                 int i;
405                 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
406                 mgreg_t regs [MONO_MAX_IREGS + 1];
407                 guint8 *cfa;
408                 guint32 unwind_info_len;
409                 guint8 *unwind_info;
410
411                 frame->type = FRAME_TYPE_MANAGED;
412
413                 if (ji->from_aot)
414                         unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
415                 else
416                         unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
417
418                 for (i = 0; i < MONO_MAX_IREGS; ++i)
419                         regs [i] = new_ctx->sc_regs [i];
420
421                 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
422                                                    (guint8*)ji->code_start + ji->code_size,
423                                                    ip, regs, MONO_MAX_IREGS,
424                                                    save_locations, MONO_MAX_IREGS, &cfa);
425
426                 for (i = 0; i < MONO_MAX_IREGS; ++i)
427                         new_ctx->sc_regs [i] = regs [i];
428                 new_ctx->sc_pc = regs [mips_ra];
429                 new_ctx->sc_regs [mips_sp] = (mgreg_t)cfa;
430
431                 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->iregs [mips_sp])) {
432                         /* remove any unused lmf */
433                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
434                 }
435
436                 /* we substract 8, so that the IP points into the call instruction */
437                 MONO_CONTEXT_SET_IP (new_ctx, new_ctx->sc_pc - 8);
438
439                 /* Sanity check -- we should have made progress here */
440                 g_assert (MONO_CONTEXT_GET_SP (new_ctx) != MONO_CONTEXT_GET_SP (ctx));
441                 return TRUE;
442         } else if (*lmf) {
443
444                 if (((mgreg_t)(*lmf)->previous_lmf) & 2) {
445                         /* 
446                          * This LMF entry is created by the soft debug code to mark transitions to
447                          * managed code done during invokes.
448                          */
449                         MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
450
451                         g_assert (ext->debugger_invoke);
452
453                         memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
454
455                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
456
457                         frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
458
459                         return TRUE;
460                 }
461
462                 if (!(*lmf)->method) {
463 #ifdef DEBUG_EXCEPTIONS
464                         g_print ("mono_arch_find_jit_info: bad lmf @ %p\n", (void *) *lmf);
465 #endif
466                         return FALSE;
467                 }
468                 g_assert (((*lmf)->magic == MIPS_LMF_MAGIC1) || ((*lmf)->magic == MIPS_LMF_MAGIC2));
469
470                 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
471                 if (!ji) {
472                         // FIXME: This can happen with multiple appdomains (bug #444383)
473                         return FALSE;
474                 }
475
476                 frame->ji = ji;
477                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
478
479                 memcpy (&new_ctx->sc_regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
480                 memcpy (&new_ctx->sc_fpregs, (*lmf)->fregs, sizeof (float) * MONO_SAVED_FREGS);
481                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
482                 /* ensure that we've made progress */
483                 g_assert (new_ctx->sc_pc != ctx->sc_pc);
484
485                 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
486
487                 return TRUE;
488         }
489
490         return FALSE;
491 }
492
493 void
494 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
495 {
496         mono_sigctx_to_monoctx (sigctx, mctx);
497 }
498
499 void
500 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
501 {
502         mono_monoctx_to_sigctx (mctx, sigctx);
503 }
504
505 gpointer
506 mono_arch_ip_from_context (void *sigctx)
507 {
508         return (gpointer)UCONTEXT_REG_PC (sigctx);
509 }
510
511 /*
512  * handle_exception:
513  *
514  *   Called by resuming from a signal handler.
515  */
516 static void
517 handle_signal_exception (gpointer obj)
518 {
519         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
520         MonoContext ctx;
521         static void (*restore_context) (MonoContext *);
522
523         if (!restore_context)
524                 restore_context = mono_get_restore_context ();
525
526         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
527
528         mono_handle_exception (&ctx, obj);
529
530         restore_context (&ctx);
531 }
532
533 /*
534  * This is the function called from the signal handler
535  */
536 gboolean
537 mono_arch_handle_exception (void *ctx, gpointer obj)
538 {
539 #if defined(MONO_CROSS_COMPILE)
540         g_assert_not_reached ();
541 #elif defined(MONO_ARCH_USE_SIGACTION)
542         void *sigctx = ctx;
543
544         /*
545          * Handling the exception in the signal handler is problematic, since the original
546          * signal is disabled, and we could run arbitrary code though the debugger. So
547          * resume into the normal stack and do most work there if possible.
548          */
549         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
550         guint64 sp = UCONTEXT_GREGS (sigctx) [mips_sp];
551
552         /* Pass the ctx parameter in TLS */
553         mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
554         /* The others in registers */
555         UCONTEXT_GREGS (sigctx)[mips_a0] = (gsize)obj;
556
557         /* Allocate a stack frame */
558         sp -= 256;
559         UCONTEXT_GREGS (sigctx)[mips_sp] = sp;
560
561         UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
562
563         return TRUE;
564 #else
565         MonoContext mctx;
566         gboolean result;
567
568         mono_arch_sigctx_to_monoctx (ctx, &mctx);
569
570         result = mono_handle_exception (&mctx, obj);
571         /* restore the context so that returning from the signal handler will invoke
572          * the catch clause 
573          */
574         mono_arch_monoctx_to_sigctx (&mctx, ctx);
575         return result;
576 #endif
577 }
578
579 /*
580  * mono_arch_setup_resume_sighandler_ctx:
581  *
582  *   Setup CTX so execution continues at FUNC.
583  */
584 void
585 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
586 {
587         MONO_CONTEXT_SET_IP (ctx,func);
588 }