Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / exceptions-sparc.c
1 /**
2  * \file
3  * exception support for sparc
4  *
5  * Authors:
6  *   Mark Crichton (crichton@gimp.org)
7  *   Dietmar Maurer (dietmar@ximian.com)
8  *
9  * (C) 2003 Ximian, Inc.
10  */
11
12 #include <config.h>
13 #include <glib.h>
14 #include <signal.h>
15 #include <string.h>
16 #include <sys/ucontext.h>
17
18 #include <mono/arch/sparc/sparc-codegen.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/threads.h>
22 #include <mono/metadata/debug-helpers.h>
23 #include <mono/metadata/exception.h>
24 #include <mono/metadata/mono-debug.h>
25 #include <mono/metadata/gc-internals.h>
26 #include <mono/metadata/tokentype.h>
27
28 #include "mini.h"
29 #include "mini-sparc.h"
30
31 #ifndef REG_SP
32 #define REG_SP REG_O6
33 #endif
34
35 #define MONO_SPARC_WINDOW_ADDR(sp) ((gpointer*)(((guint8*)(sp)) + MONO_SPARC_STACK_BIAS))
36
37 /*
38  * mono_arch_get_restore_context:
39  *
40  * Returns a pointer to a method which restores a previously saved sigcontext.
41  */
42 gpointer
43 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
44 {
45         static guint32 *start;
46         static int inited = 0;
47         guint32 *code;
48
49         g_assert (!aot);
50         if (info)
51                 *info = NULL;
52
53         if (inited)
54                 return start;
55
56         code = start = mono_global_codeman_reserve (32 * sizeof (guint32));
57
58         sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, ip), sparc_i7);
59         sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, sp), sparc_i6);
60
61         sparc_jmpl_imm (code, sparc_i7, 0, sparc_g0);
62         /* FIXME: This does not return to the correct window */
63         sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
64
65         g_assert ((code - start) < 32);
66
67         mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
68
69         inited = 1;
70
71         return start;
72 }
73
74 /*
75  * mono_arch_get_call_filter:
76  *
77  * Returns a pointer to a method which calls an exception filter. We
78  * also use this function to call finally handlers (we pass NULL as 
79  * @exc object in this case).
80  *
81  * call_filter (MonoContext *ctx, gpointer ip)
82  */
83 gpointer
84 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
85 {
86         static guint32 *start;
87         static int inited = 0;
88         guint32 *code;
89         int i;
90
91         g_assert (!aot);
92         if (info)
93                 *info = NULL;
94
95         if (inited)
96                 return start;
97
98         code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
99
100         /*
101          * There are two frames here:
102          * - the first frame is used by call_filter
103          * - the second frame is used to run the filter code
104          */
105
106         /* Create first frame */
107         sparc_save_imm (code, sparc_sp, -256, sparc_sp);
108
109         sparc_mov_reg_reg (code, sparc_i1, sparc_o0);
110         sparc_ldi_imm (code, sparc_i0, G_STRUCT_OFFSET (MonoContext, sp), sparc_o1);
111
112         /* Create second frame */
113         sparc_save_imm (code, sparc_sp, -256, sparc_sp);
114
115         sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
116         sparc_mov_reg_reg (code, sparc_i1, sparc_o1);
117
118         /*
119          * We need to change %fp to point to the stack frame of the method
120          * containing the filter. But changing %fp also changes the %sp of
121          * the parent frame (the first frame), so if the OS saves the first frame,
122          * it saves it to the stack frame of the method, which is not good.
123          * So flush all register windows to memory before changing %fp.
124          */
125         sparc_flushw (code);
126
127         sparc_mov_reg_reg (code, sparc_fp, sparc_o7);
128
129         /* 
130          * Modify the second frame so it is identical to the one used in the
131          * method containing the filter.
132          */
133         for (i = 0; i < 16; ++i)
134                 sparc_ldi_imm (code, sparc_o1, MONO_SPARC_STACK_BIAS + i * sizeof (gpointer), sparc_l0 + i);
135
136         /* Save %fp to a location reserved in mono_arch_allocate_vars */
137         sparc_sti_imm (code, sparc_o7, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer));
138
139         /* Call the filter code, after this returns, %o0 will hold the result */
140         sparc_call_imm (code, sparc_o0, 0);
141         sparc_nop (code);
142
143         /* Restore original %fp */
144         sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer), sparc_fp);
145
146         sparc_mov_reg_reg (code, sparc_o0, sparc_i0);
147
148         /* Return to first frame */
149         sparc_restore (code, sparc_g0, sparc_g0, sparc_g0);
150
151         /* FIXME: Save locals to the stack */
152
153         /* Return to caller */
154         sparc_ret (code);
155         /* Return result in delay slot */
156         sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
157
158         g_assert ((code - start) < 64);
159
160         mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
161
162         inited = 1;
163
164         return start;
165 }
166
167 static void
168 throw_exception (MonoObject *exc, gpointer sp, gpointer ip, gboolean rethrow)
169 {
170         MonoError error;
171         MonoContext ctx;
172         static void (*restore_context) (MonoContext *);
173         gpointer *window;
174         
175         if (!restore_context)
176                 restore_context = mono_get_restore_context ();
177
178         window = MONO_SPARC_WINDOW_ADDR (sp);
179         ctx.sp = (gpointer*)sp;
180         ctx.ip = ip;
181         ctx.fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (sp) [sparc_i6 - 16]);
182
183         if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
184                 MonoException *mono_ex = (MonoException*)exc;
185                 if (!rethrow) {
186                         mono_ex->stack_trace = NULL;
187                         mono_ex->trace_ips = NULL;
188                 }
189         }
190         mono_error_assert_ok (&error);
191         mono_handle_exception (&ctx, exc);
192         restore_context (&ctx);
193
194         g_assert_not_reached ();
195 }
196
197 static gpointer 
198 get_throw_exception (gboolean rethrow)
199 {
200         guint32 *start, *code;
201
202         code = start = mono_global_codeman_reserve (16 * sizeof (guint32));
203
204         sparc_save_imm (code, sparc_sp, -512, sparc_sp);
205
206         sparc_flushw (code);
207         sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
208         sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
209         sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
210         sparc_set (code, rethrow, sparc_o3);
211         sparc_set (code, throw_exception, sparc_o7);
212         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
213         sparc_nop (code);
214
215         g_assert ((code - start) <= 16);
216
217         mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
218
219         return start;
220 }
221
222 /**
223  * mono_arch_get_throw_exception:
224  * \returns a function pointer which can be used to raise exceptions.
225  * The returned function has the following 
226  * signature: void (*func) (MonoException *exc); 
227  */
228 gpointer
229 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
230 {
231         static guint32* start;
232         static int inited = 0;
233
234         g_assert (!aot);
235         if (info)
236                 *info = NULL;
237
238         if (inited)
239                 return start;
240
241         inited = 1;
242
243         start = get_throw_exception (FALSE);
244
245         return start;
246 }
247
248 gpointer
249 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
250 {
251         static guint32* start;
252         static int inited = 0;
253
254         g_assert (!aot);
255         if (info)
256                 *info = NULL;
257
258         if (inited)
259                 return start;
260
261         inited = 1;
262
263         start = get_throw_exception (TRUE);
264
265         return start;
266 }
267
268 /**
269  * mono_arch_get_throw_corlib_exception:
270  * \returns a function pointer which can be used to raise 
271  * corlib exceptions. The returned function has the following 
272  * signature: void (*func) (guint32 ex_token, guint32 offset); 
273  * Here, offset is the offset which needs to be substracted from the caller IP 
274  * to get the IP of the throw. Passing the offset has the advantage that it 
275  * needs no relocations in the caller.
276  */
277 gpointer
278 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
279 {
280         static guint32 *start;
281         static int inited = 0;
282         guint32 *code;
283         int reg;
284
285         g_assert (!aot);
286         if (info)
287                 *info = NULL;
288
289         if (inited)
290                 return start;
291
292         inited = 1;
293         code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
294
295 #ifdef SPARCV9
296         reg = sparc_g4;
297 #else
298         reg = sparc_g1;
299 #endif
300
301         sparc_mov_reg_reg (code, sparc_o7, sparc_o2);
302         sparc_save_imm (code, sparc_sp, -160, sparc_sp);
303
304         sparc_set (code, MONO_TOKEN_TYPE_DEF, sparc_o7);
305         sparc_add (code, FALSE, sparc_i0, sparc_o7, sparc_o1);
306         sparc_set (code, mono_defaults.exception_class->image, sparc_o0);
307         sparc_set (code, mono_exception_from_token, sparc_o7);
308         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
309         sparc_nop (code);
310
311         /* Return to the caller, so exception handling does not see this frame */
312         sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
313
314         /* Compute throw ip */
315         sparc_sll_imm (code, sparc_o1, 2, sparc_o1);
316         sparc_sub (code, 0, sparc_o2, sparc_o1, sparc_o7);
317
318         sparc_set (code, mono_arch_get_throw_exception (NULL, FALSE), reg);
319         /* Use a jmp instead of a call so o7 is preserved */
320         sparc_jmpl_imm (code, reg, 0, sparc_g0);
321         sparc_nop (code);
322
323         g_assert ((code - start) < 32);
324
325         mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
326
327         return start;
328 }
329
330 /* mono_arch_unwind_frame:
331  *
332  * This function is used to gather information from @ctx. It return the 
333  * MonoJitInfo of the corresponding function, unwinds one stack frame and
334  * stores the resulting context into @new_ctx. It also stores a string 
335  * describing the stack location into @trace (if not NULL), and modifies
336  * the @lmf if necessary. @native_offset return the IP offset from the 
337  * start of the function or -1 if that info is not available.
338  */
339 gboolean
340 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, 
341                                                          MonoJitInfo *ji, MonoContext *ctx, 
342                                                          MonoContext *new_ctx, MonoLMF **lmf,
343                                                          mgreg_t **save_locations,
344                                                          StackFrameInfo *frame)
345 {
346         gpointer *window;
347
348         memset (frame, 0, sizeof (StackFrameInfo));
349         frame->ji = ji;
350
351         *new_ctx = *ctx;
352
353         if (ji != NULL) {
354                 if (ji->is_trampoline)
355                         frame->type = FRAME_TYPE_TRAMPOLINE;
356                 else
357                         frame->type = FRAME_TYPE_MANAGED;
358
359                 /* Restore ip and sp from the saved register window */
360                 window = MONO_SPARC_WINDOW_ADDR (ctx->sp);
361                 new_ctx->ip = window [sparc_i7 - 16];
362                 new_ctx->sp = (gpointer*)(window [sparc_i6 - 16]);
363                 new_ctx->fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (new_ctx->sp) [sparc_i6 - 16]);
364
365                 return TRUE;
366         }
367         else {
368                 if (!(*lmf))
369                         return FALSE;
370
371                 if (!(*lmf)->method)
372                         return FALSE;
373
374                 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL);
375                 if (!ji)
376                         return FALSE;
377
378                 frame->ji = ji;
379                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
380
381                 new_ctx->ip = (*lmf)->ip;
382                 new_ctx->sp = (*lmf)->sp;
383                 new_ctx->fp = (*lmf)->ebp;
384
385                 *lmf = (*lmf)->previous_lmf;
386
387                 return TRUE;
388         }
389 }
390
391 #ifdef __linux__
392
393 gboolean
394 mono_arch_handle_exception (void *sigctx, gpointer obj)
395 {
396        MonoContext mctx;
397        struct sigcontext *sc = sigctx;
398        gpointer *window;
399
400 #ifdef SPARCV9
401        mctx.ip = (gpointer) sc->sigc_regs.tpc;
402        mctx.sp = (gpointer) sc->sigc_regs.u_regs[14];
403 #else
404        mctx.ip = (gpointer) sc->si_regs.pc;
405        mctx.sp = (gpointer) sc->si_regs.u_regs[14];
406 #endif
407
408        window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
409        mctx.fp = window [sparc_fp - 16];
410
411        mono_handle_exception (&mctx, obj);
412
413 #ifdef SPARCV9
414        sc->sigc_regs.tpc = (unsigned long) mctx.ip;
415        sc->sigc_regs.tnpc = (unsigned long) (mctx.ip + 4);
416        sc->sigc_regs.u_regs[14] = (unsigned long) mctx.sp;
417 #else
418        sc->si_regs.pc = (unsigned long) mctx.ip;
419        sc->si_regs.npc = (unsigned long) (mctx.ip + 4);
420        sc->si_regs.u_regs[14] = (unsigned long) mctx.sp;
421 #endif
422
423        window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
424        window [sparc_fp - 16] = mctx.fp;
425
426        return TRUE;
427 }
428
429 gpointer
430 mono_arch_ip_from_context (void *sigctx)
431 {
432        struct sigcontext *sc = sigctx;
433        gpointer *ret;
434
435 #ifdef SPARCV9
436        ret = (gpointer) sc->sigc_regs.tpc;
437 #else
438        ret = (gpointer) sc->si_regs.pc;
439 #endif
440
441        return ret;
442 }
443
444 #else /* !__linux__ */
445
446 gboolean
447 mono_arch_handle_exception (void *sigctx, gpointer obj)
448 {
449         MonoContext mctx;
450         ucontext_t *ctx = (ucontext_t*)sigctx;
451         gpointer *window;
452
453         /*
454          * Access to the machine state using the ucontext_t parameter is somewhat
455          * under documented under solaris. The code below seems to work under
456          * Solaris 9.
457          */
458         g_assert (!ctx->uc_mcontext.gwins);
459
460         mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
461         mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
462         window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
463         mctx.fp = window [sparc_fp - 16];
464
465         mono_handle_exception (&mctx, obj);
466         
467         /* We can't use restore_context to return from a signal handler */
468         ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
469         ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
470         ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
471         window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
472         window [sparc_fp - 16] = mctx.fp;
473
474         return TRUE;
475 }
476
477 gpointer
478 mono_arch_ip_from_context (void *sigctx)
479 {
480         ucontext_t *ctx = (ucontext_t*)sigctx;
481         return (gpointer)ctx->uc_mcontext.gregs [REG_PC];
482 }
483
484 #endif