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