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