Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / exceptions-mips.c
1 /**
2  * \file
3  * exception support for MIPS
4  *
5  * Authors:
6  *   Mark Mason (mason@broadcom.com)
7  *
8  * Based on exceptions-ppc.c by:
9  *   Dietmar Maurer (dietmar@ximian.com)
10  *   Paolo Molaro (lupus@ximian.com)
11  *
12  * (C) 2006 Broadcom
13  * (C) 2001 Ximian, Inc.
14  */
15
16 #include <config.h>
17 #include <glib.h>
18 #include <signal.h>
19 #include <string.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         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
82         return start;
83 }
84
85 /*
86  * mono_arch_get_call_filter:
87  *
88  * Returns a pointer to a method which calls an exception filter. We
89  * also use this function to call finally handlers (we pass NULL as 
90  * @exc object in this case).
91  *
92  * This function is invoked as
93  *      call_handler (MonoContext *ctx, handler)
94  *
95  * Where 'handler' is a function to be invoked as:
96  *      handler (void)
97  */
98 gpointer
99 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
100 {
101         static guint8 start [320];
102         static int inited = 0;
103         guint8 *code;
104         int alloc_size;
105         int offset;
106
107         g_assert (!aot);
108         if (info)
109                 *info = NULL;
110
111         if (inited)
112                 return start;
113
114         inited = 1;
115         code = start;
116
117         alloc_size = 64;
118         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
119
120         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
121         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
122
123         /* Save global registers on stack (s0 - s7) */
124         offset = 16;
125         MIPS_SW (code, mips_s0, mips_sp, offset); offset += IREG_SIZE;
126         MIPS_SW (code, mips_s1, mips_sp, offset); offset += IREG_SIZE;
127         MIPS_SW (code, mips_s2, mips_sp, offset); offset += IREG_SIZE;
128         MIPS_SW (code, mips_s3, mips_sp, offset); offset += IREG_SIZE;
129         MIPS_SW (code, mips_s4, mips_sp, offset); offset += IREG_SIZE;
130         MIPS_SW (code, mips_s5, mips_sp, offset); offset += IREG_SIZE;
131         MIPS_SW (code, mips_s6, mips_sp, offset); offset += IREG_SIZE;
132         MIPS_SW (code, mips_s7, mips_sp, offset); offset += IREG_SIZE;
133         MIPS_SW (code, mips_fp, mips_sp, offset); offset += IREG_SIZE;
134
135         /* Restore global registers from MonoContext, including the frame pointer */
136         MIPS_LW (code, mips_s0, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s0]));
137         MIPS_LW (code, mips_s1, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s1]));
138         MIPS_LW (code, mips_s2, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s2]));
139         MIPS_LW (code, mips_s3, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s3]));
140         MIPS_LW (code, mips_s4, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s4]));
141         MIPS_LW (code, mips_s5, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s5]));
142         MIPS_LW (code, mips_s6, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s6]));
143         MIPS_LW (code, mips_s7, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s7]));
144         MIPS_LW (code, mips_fp, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_fp]));
145
146         /* a1 is the handler to call */
147         mips_move (code, mips_t9, mips_a1);
148
149         /* jump to the saved IP */
150         mips_jalr (code, mips_t9, mips_ra);
151         mips_nop (code);
152
153         /* restore all regs from the stack */
154         offset = 16;
155         MIPS_LW (code, mips_s0, mips_sp, offset); offset += IREG_SIZE;
156         MIPS_LW (code, mips_s1, mips_sp, offset); offset += IREG_SIZE;
157         MIPS_LW (code, mips_s2, mips_sp, offset); offset += IREG_SIZE;
158         MIPS_LW (code, mips_s3, mips_sp, offset); offset += IREG_SIZE;
159         MIPS_LW (code, mips_s4, mips_sp, offset); offset += IREG_SIZE;
160         MIPS_LW (code, mips_s5, mips_sp, offset); offset += IREG_SIZE;
161         MIPS_LW (code, mips_s6, mips_sp, offset); offset += IREG_SIZE;
162         MIPS_LW (code, mips_s7, mips_sp, offset); offset += IREG_SIZE;
163         MIPS_LW (code, mips_fp, mips_sp, offset); offset += IREG_SIZE;
164
165         /* epilog */
166         mips_lw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
167         mips_addiu (code, mips_sp, mips_sp, alloc_size);
168         mips_jr (code, mips_ra);
169         mips_nop (code);
170
171         g_assert ((code - start) < sizeof(start));
172         mono_arch_flush_icache (start, code - start);
173         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
174         return start;
175 }
176
177 static void
178 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gboolean rethrow)
179 {
180         MonoError error;
181         MonoContext ctx;
182
183 #ifdef DEBUG_EXCEPTIONS
184         g_print ("throw_exception: exc=%p eip=%p esp=%p rethrow=%d\n",
185                  exc, (void *)eip, (void *) esp, rethrow);
186 #endif
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_checked (exc, mono_defaults.exception_class, &error)) {
200                 MonoException *mono_ex = (MonoException*)exc;
201                 if (!rethrow) {
202                         mono_ex->stack_trace = NULL;
203                         mono_ex->trace_ips = NULL;
204                 }
205         }
206         mono_error_assert_ok (&error);
207         mono_handle_exception (&ctx, exc);
208 #ifdef DEBUG_EXCEPTIONS
209         g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
210                  (void *) ctx.sc_pc, (void *) ctx.sc_regs[mips_sp],
211                  (void *) ctx.sc_regs[mips_fp], &ctx);
212 #endif
213         mono_restore_context (&ctx);
214
215         g_assert_not_reached ();
216 }
217
218 /**
219  * arch_get_throw_exception_generic:
220  *
221  * Returns a function pointer which can be used to raise 
222  * exceptions. The returned function has the following 
223  * signature: void (*func) (MonoException *exc); or
224  * void (*func) (char *exc_name);
225  *
226  */
227 static gpointer 
228 mono_arch_get_throw_exception_generic (guint8 *start, int size, int corlib, gboolean rethrow)
229 {
230         guint8 *code;
231         int alloc_size, pos, i;
232
233         code = start;
234
235         //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
236
237         pos = 0;
238         /* XXX - save all the FP regs on the stack ? */
239
240         pos += MONO_MAX_IREGS * sizeof(guint32);
241
242         alloc_size = MIPS_MINIMAL_STACK_SIZE + pos + 64;
243         // align to MIPS_STACK_ALIGNMENT bytes
244         alloc_size += MIPS_STACK_ALIGNMENT - 1;
245         alloc_size &= ~(MIPS_STACK_ALIGNMENT - 1);
246
247         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
248         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
249         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
250
251         /* Save all the regs on the stack */
252         for (i = 0; i < MONO_MAX_IREGS; i++) {
253                 if (i != mips_sp)
254                         MIPS_SW (code, i, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
255                 else {
256                         mips_addiu (code, mips_at, mips_sp, alloc_size);
257                         MIPS_SW (code, mips_at, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
258                 }
259         }
260
261         if (corlib) {
262                 mips_move (code, mips_a1, mips_a0);
263                 mips_load (code, mips_a0, mono_defaults.corlib);
264                 mips_load (code, mips_t9, mono_exception_from_token);
265                 mips_jalr (code, mips_t9, mips_ra);
266                 mips_nop (code);
267                 mips_move (code, mips_a0, mips_v0);
268         }
269         /* call throw_exception (exc, ip, sp, rethrow) */
270
271         /* exc is already in place in a0 */
272
273         /* pointer to ip */
274         if (corlib)
275                 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
276         else
277                 mips_move (code, mips_a1, mips_ra);
278
279         /* current sp & rethrow */
280         mips_move (code, mips_a2, mips_sp);
281         mips_addiu (code, mips_a3, mips_zero, rethrow);
282
283         mips_load (code, mips_t9, throw_exception);
284         mips_jr (code, mips_t9);
285         mips_nop (code);
286         /* we should never reach this breakpoint */
287         mips_break (code, 0xfe);
288
289         g_assert ((code - start) < size);
290         mono_arch_flush_icache (start, code - start);
291         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
292         return start;
293 }
294
295 /**
296  * mono_arch_get_rethrow_exception:
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 gpointer
302 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
303 {
304         static guint8 start [GENERIC_EXCEPTION_SIZE];
305         static int inited = 0;
306
307         g_assert (!aot);
308         if (info)
309                 *info = NULL;
310
311         if (inited)
312                 return start;
313         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
314         inited = 1;
315         return start;
316 }
317
318 /**
319  * arch_get_throw_exception:
320  *
321  * Returns a function pointer which can be used to raise 
322  * exceptions. The returned function has the following 
323  * signature: void (*func) (MonoException *exc); 
324  * For example to raise an arithmetic exception you can use:
325  *
326  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
327  * x86_call_code (code, arch_get_throw_exception ()); 
328  *
329  */
330 gpointer
331 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
332 {
333         static guint8 start [GENERIC_EXCEPTION_SIZE];
334         static int inited = 0;
335
336         g_assert (!aot);
337         if (info)
338                 *info = NULL;
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         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
358         return start;
359 }
360
361 /**
362  * mono_arch_get_throw_corlib_exception:
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 (MonoTrampInfo **info, gboolean aot)
370 {
371         static guint8 start [GENERIC_EXCEPTION_SIZE];
372         static int inited = 0;
373
374         g_assert (!aot);
375         if (info)
376                 *info = NULL;
377
378         if (inited)
379                 return start;
380         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
381         inited = 1;
382         return start;
383 }
384
385 /*
386  * mono_arch_unwind_frame:
387  *
388  * This function is used to gather information from @ctx, and store it in @frame_info.
389  * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
390  * is modified if needed.
391  * Returns TRUE on success, FALSE otherwise.
392  */
393 gboolean
394 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, 
395                                                          MonoJitInfo *ji, MonoContext *ctx, 
396                                                          MonoContext *new_ctx, MonoLMF **lmf, 
397                                                          mgreg_t **save_locations,
398                                                          StackFrameInfo *frame)
399 {
400         memset (frame, 0, sizeof (StackFrameInfo));
401         frame->ji = ji;
402
403         *new_ctx = *ctx;
404
405         if (ji != NULL) {
406                 int i;
407                 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
408                 mgreg_t regs [MONO_MAX_IREGS + 1];
409                 guint8 *cfa;
410                 guint32 unwind_info_len;
411                 guint8 *unwind_info;
412
413                 if (ji->is_trampoline)
414                         frame->type = FRAME_TYPE_TRAMPOLINE;
415                 else
416                         frame->type = FRAME_TYPE_MANAGED;
417
418                 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
419
420                 for (i = 0; i < MONO_MAX_IREGS; ++i)
421                         regs [i] = new_ctx->sc_regs [i];
422
423                 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
424                                                    (guint8*)ji->code_start + ji->code_size,
425                                                    ip, NULL, regs, MONO_MAX_IREGS,
426                                                    save_locations, MONO_MAX_IREGS, &cfa);
427
428                 for (i = 0; i < MONO_MAX_IREGS; ++i)
429                         new_ctx->sc_regs [i] = regs [i];
430                 new_ctx->sc_pc = regs [mips_ra];
431                 new_ctx->sc_regs [mips_sp] = (mgreg_t)cfa;
432
433                 /* we substract 8, so that the IP points into the call instruction */
434                 MONO_CONTEXT_SET_IP (new_ctx, new_ctx->sc_pc - 8);
435
436                 /* Sanity check -- we should have made progress here */
437                 g_assert (MONO_CONTEXT_GET_SP (new_ctx) != MONO_CONTEXT_GET_SP (ctx));
438                 return TRUE;
439         } else if (*lmf) {
440
441                 if (((mgreg_t)(*lmf)->previous_lmf) & 2) {
442                         /* 
443                          * This LMF entry is created by the soft debug code to mark transitions to
444                          * managed code done during invokes.
445                          */
446                         MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
447
448                         g_assert (ext->debugger_invoke);
449
450                         memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
451
452                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
453
454                         frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
455
456                         return TRUE;
457                 }
458
459                 if (!(*lmf)->method) {
460 #ifdef DEBUG_EXCEPTIONS
461                         g_print ("mono_arch_unwind_frame: bad lmf @ %p\n", (void *) *lmf);
462 #endif
463                         return FALSE;
464                 }
465                 g_assert (((*lmf)->magic == MIPS_LMF_MAGIC1) || ((*lmf)->magic == MIPS_LMF_MAGIC2));
466
467                 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
468                 if (!ji) {
469                         // FIXME: This can happen with multiple appdomains (bug #444383)
470                         return FALSE;
471                 }
472
473                 frame->ji = ji;
474                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
475
476                 memcpy (&new_ctx->sc_regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
477                 memcpy (&new_ctx->sc_fpregs, (*lmf)->fregs, sizeof (float) * MONO_SAVED_FREGS);
478                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
479                 /* ensure that we've made progress */
480                 g_assert (new_ctx->sc_pc != ctx->sc_pc);
481
482                 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
483
484                 return TRUE;
485         }
486
487         return FALSE;
488 }
489
490 gpointer
491 mono_arch_ip_from_context (void *sigctx)
492 {
493         return (gpointer)(gsize)UCONTEXT_REG_PC (sigctx);
494 }
495
496 /*
497  * handle_exception:
498  *
499  *   Called by resuming from a signal handler.
500  */
501 static void
502 handle_signal_exception (gpointer obj)
503 {
504         MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
505         MonoContext ctx;
506
507         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
508
509         mono_handle_exception (&ctx, obj);
510
511         mono_restore_context (&ctx);
512 }
513
514 /*
515  * This is the function called from the signal handler
516  */
517 gboolean
518 mono_arch_handle_exception (void *ctx, gpointer obj)
519 {
520 #if defined(MONO_CROSS_COMPILE)
521         g_assert_not_reached ();
522 #elif defined(MONO_ARCH_USE_SIGACTION)
523         void *sigctx = ctx;
524
525         /*
526          * Handling the exception in the signal handler is problematic, since the original
527          * signal is disabled, and we could run arbitrary code though the debugger. So
528          * resume into the normal stack and do most work there if possible.
529          */
530         MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
531         guint64 sp = UCONTEXT_GREGS (sigctx) [mips_sp];
532
533         /* Pass the ctx parameter in TLS */
534         mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
535         /* The others in registers */
536         UCONTEXT_GREGS (sigctx)[mips_a0] = (gsize)obj;
537
538         /* Allocate a stack frame */
539         sp -= 256;
540         UCONTEXT_GREGS (sigctx)[mips_sp] = sp;
541
542         UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
543
544         return TRUE;
545 #else
546         MonoContext mctx;
547         gboolean result;
548
549         mono_sigctx_to_monoctx (ctx, &mctx);
550
551         result = mono_handle_exception (&mctx, obj);
552         /* restore the context so that returning from the signal handler will invoke
553          * the catch clause 
554          */
555         mono_monoctx_to_sigctx (&mctx, ctx);
556         return result;
557 #endif
558 }
559
560 /*
561  * mono_arch_setup_resume_sighandler_ctx:
562  *
563  *   Setup CTX so execution continues at FUNC.
564  */
565 void
566 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
567 {
568         MONO_CONTEXT_SET_IP (ctx,func);
569 }