1 /*------------------------------------------------------------------*/
3 /* Name - exceptions-s390.c */
5 /* Function - Exception support for S/390. */
7 /* Name - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
9 /* Date - January, 2004 */
11 /* Derivation - From exceptions-x86 & exceptions-ppc */
12 /* Paolo Molaro (lupus@ximian.com) */
13 /* Dietmar Maurer (dietmar@ximian.com) */
15 /* Copyright - 2001 Ximian, Inc. */
17 /*------------------------------------------------------------------*/
19 /*------------------------------------------------------------------*/
21 /*------------------------------------------------------------------*/
23 #define S390_CALLFILTER_INTREGS S390_MINIMAL_STACK_SIZE
24 #define S390_CALLFILTER_FLTREGS S390_CALLFILTER_INTREGS+(16*sizeof(gulong))
25 #define S390_CALLFILTER_ACCREGS S390_CALLFILTER_FLTREGS+(16*sizeof(gdouble))
26 #define S390_CALLFILTER_SIZE (S390_CALLFILTER_ACCREGS+(16*sizeof(gulong)))
28 #define S390_THROWSTACK_ACCPRM S390_MINIMAL_STACK_SIZE
29 #define S390_THROWSTACK_FPCPRM S390_THROWSTACK_ACCPRM+sizeof(gpointer)
30 #define S390_THROWSTACK_RETHROW S390_THROWSTACK_FPCPRM+sizeof(gulong)
31 #define S390_THROWSTACK_INTREGS S390_THROWSTACK_RETHROW+sizeof(gboolean)
32 #define S390_THROWSTACK_FLTREGS S390_THROWSTACK_INTREGS+(16*sizeof(gulong))
33 #define S390_THROWSTACK_ACCREGS S390_THROWSTACK_FLTREGS+(16*sizeof(gdouble))
34 #define S390_THROWSTACK_SIZE (S390_THROWSTACK_ACCREGS+(16*sizeof(gulong)))
36 /*========================= End of Defines =========================*/
38 /*------------------------------------------------------------------*/
40 /*------------------------------------------------------------------*/
48 #include <mono/arch/s390/s390-codegen.h>
49 #include <mono/metadata/appdomain.h>
50 #include <mono/metadata/tabledefs.h>
51 #include <mono/metadata/threads.h>
52 #include <mono/metadata/debug-helpers.h>
53 #include <mono/metadata/exception.h>
54 #include <mono/metadata/mono-debug.h>
57 #include "mini-s390.h"
59 /*========================= End of Includes ========================*/
61 /*------------------------------------------------------------------*/
62 /* P r o t o t y p e s */
63 /*------------------------------------------------------------------*/
65 gboolean mono_arch_handle_exception (void *ctx,
69 /*========================= End of Prototypes ======================*/
71 /*------------------------------------------------------------------*/
72 /* G l o b a l V a r i a b l e s */
73 /*------------------------------------------------------------------*/
75 /*====================== End of Global Variables ===================*/
77 /*------------------------------------------------------------------*/
79 /* Name - mono_arch_has_unwind_info */
81 /* Function - Tests if a function has a DWARF exception table */
82 /* that is able to restore all caller saved registers*/
84 /*------------------------------------------------------------------*/
87 mono_arch_has_unwind_info (gconstpointer addr)
92 /*========================= End of Function ========================*/
94 /*------------------------------------------------------------------*/
96 /* Name - mono_arch_get_call_filter */
98 /* Function - Return a pointer to a method which calls an */
99 /* exception filter. We also use this function to */
100 /* call finally handlers (we pass NULL as @exc */
101 /* object in this case). */
103 /*------------------------------------------------------------------*/
106 mono_arch_get_call_filter (void)
108 static guint8 start [512];
109 static int inited = 0;
111 int alloc_size, pos, i;
117 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
120 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
121 s390_lr (code, s390_r14, STK_BASE);
122 alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
123 s390_ahi (code, STK_BASE, -alloc_size);
124 s390_st (code, s390_r14, 0, STK_BASE, 0);
126 /*------------------------------------------------------*/
127 /* save general registers on stack */
128 /*------------------------------------------------------*/
129 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
131 /*------------------------------------------------------*/
132 /* save floating point registers on stack */
133 /*------------------------------------------------------*/
134 // pos = S390_CALLFILTER_FLTREGS;
135 // for (i = 0; i < 16; ++i) {
136 // s390_std (code, i, 0, STK_BASE, pos);
137 // pos += sizeof (gdouble);
140 /*------------------------------------------------------*/
141 /* save access registers on stack */
142 /*------------------------------------------------------*/
143 // s390_stam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
145 /*------------------------------------------------------*/
147 /*------------------------------------------------------*/
148 s390_lr (code, s390_r13, s390_r2);
150 /*------------------------------------------------------*/
151 /* Get A(Handler Entry Point) */
152 /*------------------------------------------------------*/
153 s390_lr (code, s390_r0, s390_r3);
155 /*------------------------------------------------------*/
156 /* Set parameter register with Exception */
157 /*------------------------------------------------------*/
158 s390_lr (code, s390_r2, s390_r4);
160 /*------------------------------------------------------*/
161 /* Load all registers with values from the context */
162 /*------------------------------------------------------*/
163 s390_lm (code, s390_r3, s390_r12, s390_r13,
164 G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[3]));
165 pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
166 for (i = 0; i < 16; ++i) {
167 s390_ld (code, i, 0, s390_r13, pos);
168 pos += sizeof(gdouble);
171 /*------------------------------------------------------*/
172 /* Point at the copied stack frame and call the filter */
173 /*------------------------------------------------------*/
174 s390_lr (code, s390_r1, s390_r0);
175 s390_basr (code, s390_r14, s390_r1);
177 /*------------------------------------------------------*/
178 /* Save return value */
179 /*------------------------------------------------------*/
180 s390_lr (code, s390_r14, s390_r2);
182 /*------------------------------------------------------*/
183 /* Restore all the regs from the stack */
184 /*------------------------------------------------------*/
185 s390_lm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
186 // pos = S390_CALLFILTER_FLTREGS;
187 // for (i = 0; i < 16; ++i) {
188 // s390_ld (code, i, 0, STK_BASE, pos);
189 // pos += sizeof (gdouble);
192 s390_lr (code, s390_r2, s390_r14);
193 // s390_lam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
194 s390_ahi (code, s390_r15, alloc_size);
195 s390_lm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
196 s390_br (code, s390_r14);
198 g_assert ((code - start) < sizeof(start));
202 /*========================= End of Function ========================*/
204 /*------------------------------------------------------------------*/
206 /* Name - throw_exception. */
208 /* Function - Raise an exception based on the parameters passed.*/
210 /*------------------------------------------------------------------*/
213 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
214 gulong *int_regs, gdouble *fp_regs, gulong *acc_regs,
215 guint fpc, gboolean rethrow)
220 memset(&ctx, 0, sizeof(ctx));
224 /* adjust eip so that it point into the call instruction */
227 for (iReg = 0; iReg < 16; iReg++) {
228 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
229 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
230 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
233 ctx.uc_mcontext.fpregs.fpc = fpc;
235 MONO_CONTEXT_SET_BP (&ctx, sp);
236 MONO_CONTEXT_SET_IP (&ctx, ip);
238 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
239 MonoException *mono_ex = (MonoException*)exc;
241 mono_ex->stack_trace = NULL;
243 mono_arch_handle_exception (&ctx, exc, FALSE);
246 g_assert_not_reached ();
249 /*========================= End of Function ========================*/
251 /*------------------------------------------------------------------*/
253 /* Name - get_throw_exception_generic */
255 /* Function - Return a function pointer which can be used to */
256 /* raise exceptions. The returned function has the */
257 /* following signature: */
258 /* void (*func) (MonoException *exc); or, */
259 /* void (*func) (char *exc_name); */
261 /*------------------------------------------------------------------*/
264 get_throw_exception_generic (guint8 *start, int size,
265 int by_name, gboolean rethrow)
268 int alloc_size, pos, i, offset;
272 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
273 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
274 s390_lr (code, s390_r14, STK_BASE);
275 s390_ahi (code, STK_BASE, -alloc_size);
276 s390_st (code, s390_r14, 0, STK_BASE, 0);
278 s390_lr (code, s390_r4, s390_r2);
279 s390_bras (code, s390_r13, 6);
280 s390_word (code, mono_defaults.corlib);
281 s390_word (code, "System");
282 s390_l (code, s390_r2, 0, s390_r13, 0);
283 s390_l (code, s390_r3, 0, s390_r13, 4);
284 offset = (guint32) S390_RELATIVE(mono_exception_from_name, code);
285 s390_brasl(code, s390_r14, offset);
287 /*------------------------------------------------------*/
288 /* save the general registers on the stack */
289 /*------------------------------------------------------*/
290 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
292 s390_lr (code, s390_r1, STK_BASE);
293 s390_ahi (code, s390_r1, alloc_size);
294 /*------------------------------------------------------*/
295 /* save the return address in the parameter register */
296 /*------------------------------------------------------*/
297 s390_l (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
299 /*------------------------------------------------------*/
300 /* save the floating point registers */
301 /*------------------------------------------------------*/
302 pos = S390_THROWSTACK_FLTREGS;
303 for (i = 0; i < 16; ++i) {
304 s390_std (code, i, 0,STK_BASE, pos);
305 pos += sizeof (gdouble);
307 /*------------------------------------------------------*/
308 /* save the access registers */
309 /*------------------------------------------------------*/
310 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
312 /*------------------------------------------------------*/
313 /* call throw_exception (exc, ip, sp, gr, fr, ar) */
314 /* exc is already in place in r2 */
315 /*------------------------------------------------------*/
316 s390_lr (code, s390_r4, s390_r1); /* caller sp */
317 /*------------------------------------------------------*/
318 /* pointer to the saved int regs */
319 /*------------------------------------------------------*/
320 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
321 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
322 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
323 s390_st (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
324 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM);
325 s390_lhi (code, s390_r7, rethrow);
326 s390_st (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
327 offset = (guint32) S390_RELATIVE(throw_exception, code);
328 s390_brasl(code, s390_r14, offset);
329 /* we should never reach this breakpoint */
331 g_assert ((code - start) < size);
335 /*========================= End of Function ========================*/
337 /*------------------------------------------------------------------*/
339 /* Name - arch_get_throw_exception */
341 /* Function - Return a function pointer which can be used to */
342 /* raise exceptions. The returned function has the */
343 /* following signature: */
344 /* void (*func) (MonoException *exc); */
346 /*------------------------------------------------------------------*/
349 mono_arch_get_throw_exception (void)
351 static guint8 start [384];
352 static int inited = 0;
356 get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
361 /*========================= End of Function ========================*/
363 /*------------------------------------------------------------------*/
365 /* Name - arch_get_rethrow_exception */
367 /* Function - Return a function pointer which can be used to */
368 /* raise exceptions. The returned function has the */
369 /* following signature: */
370 /* void (*func) (MonoException *exc); */
372 /*------------------------------------------------------------------*/
375 mono_arch_get_rethrow_exception (void)
377 static guint8 start [384];
378 static int inited = 0;
382 get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
387 /*========================= End of Function ========================*/
389 /*------------------------------------------------------------------*/
391 /* Name - arch_get_throw_exception_by_name */
393 /* Function - Return a function pointer which can be used to */
394 /* raise corlib exceptions. The return function has */
395 /* the following signature: */
396 /* void (*func) (char *exc_name); */
398 /*------------------------------------------------------------------*/
401 mono_arch_get_throw_exception_by_name (void)
403 static guint8 start [384];
404 static int inited = 0;
408 get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
413 /*========================= End of Function ========================*/
415 /*------------------------------------------------------------------*/
417 /* Name - mono_arch_find_jit_info */
419 /* Function - This function is used to gather informatoin from */
420 /* @ctx. It returns the MonoJitInfo of the corres- */
421 /* ponding function, unwinds one stack frame and */
422 /* stores the resulting context into @new_ctx. It */
423 /* also stores a string describing the stack location*/
424 /* into @trace (if not NULL), and modifies the @lmf */
425 /* if necessary. @native_offset returns the IP off- */
426 /* set from the start of the function or -1 if that */
427 /* informatoin is not available. */
429 /*------------------------------------------------------------------*/
432 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
433 MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
434 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
435 int *native_offset, gboolean *managed)
438 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
439 MonoS390StackFrame *sframe;
442 (ip >= prev_ji->code_start &&
443 ((guint8 *) ip <= ((guint8 *) prev_ji->code_start) + prev_ji->code_size)))
446 ji = mono_jit_info_table_find (domain, ip);
458 char *source_location, *tmpaddr, *fname;
459 gint32 address, iloffset;
463 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
464 /* remove any unused lmf */
465 *lmf = (*lmf)->previous_lmf;
468 address = (char *)ip - (char *)ji->code_start;
471 *native_offset = address;
474 if (!ji->method->wrapper_type)
478 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
479 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
482 tmpaddr = g_strdup_printf ("<0x%08x>", address);
484 tmpaddr = g_strdup_printf ("[0x%08x]", iloffset);
486 fname = mono_method_full_name (ji->method, TRUE);
489 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
491 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
494 g_free (source_location);
497 sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_SP (ctx);
498 MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
499 sframe = (MonoS390StackFrame *) sframe->prev;
500 MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
501 memcpy (&new_ctx->uc_mcontext.gregs[6], sframe->regs, (8*sizeof(gint32)));
512 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
514 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
517 memset (res, 0, sizeof (MonoJitInfo));
518 res->method = (*lmf)->method;
521 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
522 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
524 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
525 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
526 *lmf = (*lmf)->previous_lmf;
535 /*========================= End of Function ========================*/
537 /*------------------------------------------------------------------*/
539 /* Name - mono_arch_handle_exception */
541 /* Function - Handle an exception raised by the JIT code. */
543 /* Parameters - ctx - Saved processor state */
544 /* obj - The exception object */
545 /* test_only - Only test if the exception is caught, */
546 /* but don't call handlers */
548 /*------------------------------------------------------------------*/
551 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
553 return mono_handle_exception (uc, obj, mono_arch_ip_from_context(uc), test_only);
556 /*========================= End of Function ========================*/
558 /*------------------------------------------------------------------*/
560 /* Name - mono_arch_ip_from_context */
562 /* Function - Return the instruction pointer from the context. */
564 /* Parameters - sigctx - Saved processor state */
566 /*------------------------------------------------------------------*/
569 mono_arch_ip_from_context (void *sigctx)
571 return context_get_ip (sigctx);
575 /*========================= End of Function ========================*/
577 /*------------------------------------------------------------------*/
579 /* Name - mono_arch_get_restore_context */
581 /* Function - Return the address of the routine that will rest- */
582 /* ore the context. */
584 /*------------------------------------------------------------------*/
587 mono_arch_get_restore_context ()
593 /*========================= End of Function ========================*/
595 /*------------------------------------------------------------------*/
597 /* Name - mono_arch_is_int_overflow */
599 /* Function - Inspect the code that raised the SIGFPE signal */
600 /* to see if the DivideByZero or Arithmetic exception*/
601 /* should be raised. */
603 /*------------------------------------------------------------------*/
606 mono_arch_is_int_overflow (void *uc, void *info)
611 gboolean arithExc = TRUE;
615 ctx = (MonoContext *) uc;
616 code = (guint8 *) ((siginfo_t *)info)->si_addr;
617 /*----------------------------------------------------------*/
618 /* Divide operations are the only ones that will give the */
619 /* divide by zero exception so just check for these ops. */
620 /*----------------------------------------------------------*/
622 case 0x1d : /* Divide Register */
623 regNo = code[1] & 0x0f;
624 if (ctx->uc_mcontext.gregs[regNo] == 0)
627 case 0x5d : /* Divide */
628 regNo = (code[2] & 0xf0 >> 8);
629 offset = *((guint16 *) code+2) & 0x0fff;
630 operand = ctx->uc_mcontext.gregs[regNo] + offset;
634 case 0xb9 : /* Divide logical Register? */
635 if (code[1] == 0x97) {
636 regNo = (code[2] & 0xf0 >> 8);
637 if (ctx->uc_mcontext.gregs[regNo] == 0)
641 case 0xe3 : /* Divide logical? */
642 if (code[1] == 0x97) {
643 regNo = (code[2] & 0xf0 >> 8);
644 offset = *((guint32 *) code+1) & 0x000fffff;
645 operand = ctx->uc_mcontext.gregs[regNo] + offset;
653 ctx->uc_mcontext.psw.addr = code;
657 /*========================= End of Function ========================*/