2004-11-07 Zoltan Varga <vargaz@freemail.hu>
[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
26 #include "mini.h"
27 #include "mini-sparc.h"
28
29 #ifndef REG_SP
30 #define REG_SP REG_O6
31 #endif
32
33 #define MONO_SPARC_WINDOW_ADDR(sp) ((gpointer*)(((guint8*)(sp)) + MONO_SPARC_STACK_BIAS))
34
35 /*
36  * mono_arch_get_restore_context:
37  *
38  * Returns a pointer to a method which restores a previously saved sigcontext.
39  */
40 gpointer
41 mono_arch_get_restore_context (void)
42 {
43         static guint32 start [32];
44         static int inited = 0;
45         guint32 *code;
46
47         if (inited)
48                 return start;
49
50         code = start;
51
52         sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, ip), sparc_i7);
53         sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, sp), sparc_i6);
54
55         sparc_jmpl_imm (code, sparc_i7, 0, sparc_g0);
56         /* FIXME: This does not return to the correct window */
57         sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
58
59         g_assert ((code - start) < 32);
60
61         inited = 1;
62
63         return start;
64 }
65
66 /*
67  * mono_arch_get_call_filter:
68  *
69  * Returns a pointer to a method which calls an exception filter. We
70  * also use this function to call finally handlers (we pass NULL as 
71  * @exc object in this case).
72  *
73  * call_filter (MonoContext *ctx, gpointer ip)
74  */
75 gpointer
76 mono_arch_get_call_filter (void)
77 {
78         static guint32 start [64];
79         static int inited = 0;
80         guint32 *code;
81         int i;
82
83         if (inited)
84                 return start;
85
86         code = start;
87
88         /*
89          * There are two frames here:
90          * - the first frame is used by call_filter
91          * - the second frame is used to run the filter code
92          */
93
94         /* Create first frame */
95         sparc_save_imm (code, sparc_sp, -256, sparc_sp);
96
97         sparc_mov_reg_reg (code, sparc_i1, sparc_o0);
98         sparc_ldi_imm (code, sparc_i0, G_STRUCT_OFFSET (MonoContext, sp), sparc_o1);
99
100         /* Create second frame */
101         sparc_save_imm (code, sparc_sp, -256, sparc_sp);
102
103         sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
104         sparc_mov_reg_reg (code, sparc_i1, sparc_o1);
105
106         /*
107          * We need to change %fp to point to the stack frame of the method
108          * containing the filter. But changing %fp also changes the %sp of
109          * the parent frame (the first frame), so if the OS saves the first frame,
110          * it saves it to the stack frame of the method, which is not good.
111          * So flush all register windows to memory before changing %fp.
112          */
113         sparc_flushw (code);
114
115         sparc_mov_reg_reg (code, sparc_fp, sparc_o7);
116
117         /* 
118          * Modify the second frame so it is identical to the one used in the
119          * method containing the filter.
120          */
121         for (i = 0; i < 16; ++i)
122                 sparc_ldi_imm (code, sparc_o1, MONO_SPARC_STACK_BIAS + i * sizeof (gpointer), sparc_l0 + i);
123
124         /* Save %fp to a location reserved in mono_arch_allocate_vars */
125         sparc_sti_imm (code, sparc_o7, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer));
126
127         /* Call the filter code, after this returns, %o0 will hold the result */
128         sparc_call_imm (code, sparc_o0, 0);
129         sparc_nop (code);
130
131         /* Restore original %fp */
132         sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer), sparc_fp);
133
134         sparc_mov_reg_reg (code, sparc_o0, sparc_i0);
135
136         /* Return to first frame */
137         sparc_restore (code, sparc_g0, sparc_g0, sparc_g0);
138
139         /* FIXME: Save locals to the stack */
140
141         /* Return to caller */
142         sparc_ret (code);
143         /* Return result in delay slot */
144         sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
145
146         g_assert ((code - start) < 64);
147
148         inited = 1;
149
150         return start;
151 }
152
153 static void
154 throw_exception (MonoObject *exc, gpointer sp, gpointer ip, gboolean rethrow)
155 {
156         MonoContext ctx;
157         static void (*restore_context) (MonoContext *);
158         gpointer *window;
159         
160         if (!restore_context)
161                 restore_context = mono_arch_get_restore_context ();
162
163         window = MONO_SPARC_WINDOW_ADDR (sp);
164         ctx.sp = (gpointer*)sp;
165         ctx.ip = ip;
166         ctx.fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (sp) [sparc_i6 - 16]);
167
168         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
169                 MonoException *mono_ex = (MonoException*)exc;
170                 if (!rethrow)
171                         mono_ex->stack_trace = NULL;
172         }
173         mono_handle_exception (&ctx, exc, ip, FALSE);
174         restore_context (&ctx);
175
176         g_assert_not_reached ();
177 }
178
179 gpointer 
180 get_throw_exception (gboolean rethrow)
181 {
182         guint32 start [36];
183         guint32 *code;
184
185         if (inited)
186                 return start;
187
188         code = start;
189
190         sparc_save_imm (code, sparc_sp, -512, sparc_sp);
191
192         sparc_flushw (code);
193         sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
194         sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
195         sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
196         sparc_set (code, rethrow, sparc_o3);
197         sparc_set (code, throw_exception, sparc_o7);
198         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
199         sparc_nop (code);
200
201         g_assert ((code - start) < 36);
202
203         return start;
204 }
205
206 /**
207  * mono_arch_get_throw_exception:
208  *
209  * Returns a function pointer which can be used to raise exceptions.
210  * The returned function has the following 
211  * signature: void (*func) (MonoException *exc); 
212  */
213 gpointer 
214 mono_arch_get_throw_exception (void)
215 {
216         static guint32* start;
217         static int inited = 0;
218
219         if (inited)
220                 return start;
221
222         inited = 1;
223
224         start = get_throw_exception (FALSE);
225
226         return start;
227 }
228
229 gpointer 
230 mono_arch_get_rethrow_exception (void)
231 {
232         static guint32* start;
233         static int inited = 0;
234
235         if (inited)
236                 return start;
237
238         inited = 1;
239
240         start = get_throw_exception (TRUE);
241
242         return start;
243 }
244
245 /**
246  * mono_arch_get_throw_exception_by_name:
247  *
248  * Returns a function pointer which can be used to raise 
249  * corlib exceptions. The returned function has the following 
250  * signature: void (*func) (char *exc_name, gpointer ip); 
251  */
252 gpointer 
253 mono_arch_get_throw_exception_by_name (void)
254 {
255         static guint32 start [64];
256         static int inited = 0;
257         guint32 *code;
258         int reg;
259
260         if (inited)
261                 return start;
262
263         inited = 1;
264         code = start;
265
266 #ifdef SPARCV9
267         reg = sparc_g4;
268 #else
269         reg = sparc_g1;
270 #endif
271
272         sparc_save_imm (code, sparc_sp, -160, sparc_sp);
273
274         sparc_mov_reg_reg (code, sparc_i0, sparc_o2);
275         sparc_set (code, mono_defaults.corlib, sparc_o0);
276         sparc_set (code, "System", sparc_o1);
277         sparc_set (code, mono_exception_from_name, sparc_o7);
278         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
279         sparc_nop (code);
280
281         /* Return to the caller, so exception handling does not see this frame */
282         sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
283
284         /* Put original return address into %o7 */
285         sparc_mov_reg_reg (code, sparc_o1, sparc_o7);
286         sparc_set (code, mono_arch_get_throw_exception (), reg);
287         /* Use a jmp instead of a call so o7 is preserved */
288         sparc_jmpl_imm (code, reg, 0, sparc_g0);
289         sparc_nop (code);
290
291         g_assert ((code - start) < 32);
292
293         return start;
294 }       
295
296 /* mono_arch_find_jit_info:
297  *
298  * This function is used to gather information from @ctx. It return the 
299  * MonoJitInfo of the corresponding function, unwinds one stack frame and
300  * stores the resulting context into @new_ctx. It also stores a string 
301  * describing the stack location into @trace (if not NULL), and modifies
302  * the @lmf if necessary. @native_offset return the IP offset from the 
303  * start of the function or -1 if that info is not available.
304  */
305 MonoJitInfo *
306 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
307                          MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
308                          gboolean *managed)
309 {
310         MonoJitInfo *ji;
311         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
312         gpointer *window;
313
314         /* Avoid costly table lookup during stack overflow */
315         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
316                 ji = prev_ji;
317         else
318                 ji = mono_jit_info_table_find (domain, ip);
319
320         if (managed)
321                 *managed = FALSE;
322
323         if (ji != NULL) {
324                 *new_ctx = *ctx;
325
326                 if (managed)
327                         if (!ji->method->wrapper_type)
328                                 *managed = TRUE;
329
330                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
331                         /* remove any unused lmf */
332                         *lmf = (*lmf)->previous_lmf;
333                 }
334
335                 /* Restore ip and sp from the saved register window */
336                 window = MONO_SPARC_WINDOW_ADDR (ctx->sp);
337                 new_ctx->ip = window [sparc_i7 - 16];
338                 new_ctx->sp = (gpointer*)(window [sparc_i6 - 16]);
339                 new_ctx->fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (new_ctx->sp) [sparc_i6 - 16]);
340
341                 *res = *ji;
342                 return res;
343         }
344         else {
345                 if (!(*lmf))
346                         return NULL;
347
348                 *new_ctx = *ctx;
349
350                 if (!(*lmf)->method)
351                         return (gpointer)-1;
352
353                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->ip))) {
354                         *res = *ji;
355                 } else {
356                         memset (res, 0, sizeof (MonoJitInfo));
357                         res->method = (*lmf)->method;
358                 }
359
360                 new_ctx->ip = (*lmf)->ip;
361                 new_ctx->sp = (*lmf)->sp;
362                 new_ctx->fp = (*lmf)->ebp;
363
364                 *lmf = (*lmf)->previous_lmf;
365
366                 return res;
367         }
368 }
369
370 gboolean
371 mono_arch_has_unwind_info (gconstpointer addr)
372 {
373         return FALSE;
374 }
375
376 gboolean
377 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
378 {
379         MonoContext mctx;
380         ucontext_t *ctx = (ucontext_t*)sigctx;
381         gpointer *window;
382
383         /*
384          * Access to the machine state using the ucontext_t parameter is somewhat
385          * under documented under solaris. The code below seems to work under
386          * Solaris 9.
387          */
388 #ifndef __linux__
389         g_assert (!ctx->uc_mcontext.gwins);
390 #else
391         /* better, but doesn't work all the time.  need to rethink! */
392         g_assert (!ctx->uc_mcontext.gregs);
393 #endif
394
395         mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
396         mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
397         window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
398         mctx.fp = window [sparc_fp - 16];
399
400         mono_handle_exception (&mctx, obj, mctx.ip, test_only);
401         
402         /* We can't use restore_context to return from a signal handler */
403         ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
404         ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
405         ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
406         window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
407         window [sparc_fp - 16] = mctx.fp;
408
409         return TRUE;
410 }
411
412 gpointer
413 mono_arch_ip_from_context (void *sigctx)
414 {
415         ucontext_t *ctx = (ucontext_t*)sigctx;
416         return (gpointer)ctx->uc_mcontext.gregs [REG_PC];
417 }
418