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)))
38 /*========================= End of Defines =========================*/
40 /*------------------------------------------------------------------*/
42 /*------------------------------------------------------------------*/
50 #include <mono/arch/s390/s390-codegen.h>
51 #include <mono/metadata/appdomain.h>
52 #include <mono/metadata/tabledefs.h>
53 #include <mono/metadata/threads.h>
54 #include <mono/metadata/debug-helpers.h>
55 #include <mono/metadata/exception.h>
56 #include <mono/metadata/mono-debug.h>
59 #include "mini-s390.h"
61 /*========================= End of Includes ========================*/
63 /*------------------------------------------------------------------*/
64 /* P r o t o t y p e s */
65 /*------------------------------------------------------------------*/
67 gboolean mono_arch_handle_exception (void *ctx,
71 /*========================= End of Prototypes ======================*/
73 /*------------------------------------------------------------------*/
74 /* G l o b a l V a r i a b l e s */
75 /*------------------------------------------------------------------*/
77 /*====================== End of Global Variables ===================*/
79 /*------------------------------------------------------------------*/
81 /* Name - mono_arch_has_unwind_info */
83 /* Function - Tests if a function has a DWARF exception table */
84 /* that is able to restore all caller saved registers*/
86 /*------------------------------------------------------------------*/
89 mono_arch_has_unwind_info (gconstpointer addr)
94 /*========================= End of Function ========================*/
96 /*------------------------------------------------------------------*/
98 /* Name - mono_arch_get_call_filter */
100 /* Function - Return a pointer to a method which calls an */
101 /* exception filter. We also use this function to */
102 /* call finally handlers (we pass NULL as @exc */
103 /* object in this case). */
105 /*------------------------------------------------------------------*/
108 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
110 static guint8 *start;
111 static int inited = 0;
113 int alloc_size, pos, i;
123 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
124 code = start = mono_global_codeman_reserve (512);
126 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
127 s390_lr (code, s390_r14, STK_BASE);
128 alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
129 s390_ahi (code, STK_BASE, -alloc_size);
130 s390_st (code, s390_r14, 0, STK_BASE, 0);
132 /*------------------------------------------------------*/
133 /* save general registers on stack */
134 /*------------------------------------------------------*/
135 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
137 /*------------------------------------------------------*/
138 /* save floating point registers on stack */
139 /*------------------------------------------------------*/
140 // pos = S390_CALLFILTER_FLTREGS;
141 // for (i = 0; i < 16; ++i) {
142 // s390_std (code, i, 0, STK_BASE, pos);
143 // pos += sizeof (gdouble);
146 /*------------------------------------------------------*/
147 /* save access registers on stack */
148 /*------------------------------------------------------*/
149 // s390_stam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
151 /*------------------------------------------------------*/
153 /*------------------------------------------------------*/
154 s390_lr (code, s390_r13, s390_r2);
156 /*------------------------------------------------------*/
157 /* Get A(Handler Entry Point) */
158 /*------------------------------------------------------*/
159 s390_lr (code, s390_r0, s390_r3);
161 /*------------------------------------------------------*/
162 /* Set parameter register with Exception */
163 /*------------------------------------------------------*/
164 s390_lr (code, s390_r2, s390_r4);
166 /*------------------------------------------------------*/
167 /* Load all registers with values from the context */
168 /*------------------------------------------------------*/
169 s390_lm (code, s390_r3, s390_r12, s390_r13,
170 G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[3]));
171 pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
172 for (i = 0; i < 16; ++i) {
173 s390_ld (code, i, 0, s390_r13, pos);
174 pos += sizeof(gdouble);
177 /*------------------------------------------------------*/
178 /* Point at the copied stack frame and call the filter */
179 /*------------------------------------------------------*/
180 s390_lr (code, s390_r1, s390_r0);
181 s390_basr (code, s390_r14, s390_r1);
183 /*------------------------------------------------------*/
184 /* Save return value */
185 /*------------------------------------------------------*/
186 s390_lr (code, s390_r14, s390_r2);
188 /*------------------------------------------------------*/
189 /* Restore all the regs from the stack */
190 /*------------------------------------------------------*/
191 s390_lm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
192 // pos = S390_CALLFILTER_FLTREGS;
193 // for (i = 0; i < 16; ++i) {
194 // s390_ld (code, i, 0, STK_BASE, pos);
195 // pos += sizeof (gdouble);
198 s390_lr (code, s390_r2, s390_r14);
199 // s390_lam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
200 s390_ahi (code, s390_r15, alloc_size);
201 s390_lm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
202 s390_br (code, s390_r14);
204 g_assert ((code - start) < SZ_THROW);
208 /*========================= End of Function ========================*/
210 /*------------------------------------------------------------------*/
212 /* Name - throw_exception. */
214 /* Function - Raise an exception based on the parameters passed.*/
216 /*------------------------------------------------------------------*/
219 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
220 gulong *int_regs, gdouble *fp_regs, gulong *acc_regs,
221 guint fpc, gboolean rethrow)
226 memset(&ctx, 0, sizeof(ctx));
230 /* adjust eip so that it point into the call instruction */
233 for (iReg = 0; iReg < 16; iReg++) {
234 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
235 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
236 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
239 ctx.uc_mcontext.fpregs.fpc = fpc;
241 MONO_CONTEXT_SET_BP (&ctx, sp);
242 MONO_CONTEXT_SET_IP (&ctx, ip);
244 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
245 MonoException *mono_ex = (MonoException*)exc;
247 mono_ex->stack_trace = NULL;
249 mono_arch_handle_exception (&ctx, exc, FALSE);
252 g_assert_not_reached ();
255 /*========================= End of Function ========================*/
257 /*------------------------------------------------------------------*/
259 /* Name - get_throw_exception_generic */
261 /* Function - Return a function pointer which can be used to */
262 /* raise exceptions. The returned function has the */
263 /* following signature: */
264 /* void (*func) (MonoException *exc); or, */
265 /* void (*func) (char *exc_name); */
267 /*------------------------------------------------------------------*/
270 get_throw_exception_generic (guint8 *start, int size,
271 int by_name, gboolean rethrow)
274 int alloc_size, pos, i, offset;
278 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
279 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
280 s390_lr (code, s390_r14, STK_BASE);
281 s390_ahi (code, STK_BASE, -alloc_size);
282 s390_st (code, s390_r14, 0, STK_BASE, 0);
284 s390_lr (code, s390_r4, s390_r2);
285 s390_bras (code, s390_r13, 6);
286 s390_word (code, mono_defaults.corlib);
287 s390_word (code, "System");
288 s390_l (code, s390_r2, 0, s390_r13, 0);
289 s390_l (code, s390_r3, 0, s390_r13, 4);
290 offset = (guint32) S390_RELATIVE(mono_exception_from_name, code);
291 s390_brasl(code, s390_r14, offset);
293 /*------------------------------------------------------*/
294 /* save the general registers on the stack */
295 /*------------------------------------------------------*/
296 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
298 s390_lr (code, s390_r1, STK_BASE);
299 s390_ahi (code, s390_r1, alloc_size);
300 /*------------------------------------------------------*/
301 /* save the return address in the parameter register */
302 /*------------------------------------------------------*/
303 s390_l (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
305 /*------------------------------------------------------*/
306 /* save the floating point registers */
307 /*------------------------------------------------------*/
308 pos = S390_THROWSTACK_FLTREGS;
309 for (i = 0; i < 16; ++i) {
310 s390_std (code, i, 0,STK_BASE, pos);
311 pos += sizeof (gdouble);
313 /*------------------------------------------------------*/
314 /* save the access registers */
315 /*------------------------------------------------------*/
316 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
318 /*------------------------------------------------------*/
319 /* call throw_exception (exc, ip, sp, gr, fr, ar) */
320 /* exc is already in place in r2 */
321 /*------------------------------------------------------*/
322 s390_lr (code, s390_r4, s390_r1); /* caller sp */
323 /*------------------------------------------------------*/
324 /* pointer to the saved int regs */
325 /*------------------------------------------------------*/
326 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
327 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
328 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
329 s390_st (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
330 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM);
331 s390_lhi (code, s390_r7, rethrow);
332 s390_st (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
333 offset = (guint32) S390_RELATIVE(throw_exception, code);
334 s390_brasl(code, s390_r14, offset);
335 /* we should never reach this breakpoint */
337 g_assert ((code - start) < size);
341 /*========================= End of Function ========================*/
343 /*------------------------------------------------------------------*/
345 /* Name - arch_get_throw_exception */
347 /* Function - Return a function pointer which can be used to */
348 /* raise exceptions. The returned function has the */
349 /* following signature: */
350 /* void (*func) (MonoException *exc); */
352 /*------------------------------------------------------------------*/
355 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
357 static guint8 *start;
358 static int inited = 0;
366 start = mono_global_codeman_reserve (SZ_THROW);
367 get_throw_exception_generic (start, SZ_THROW, FALSE, FALSE);
372 /*========================= End of Function ========================*/
374 /*------------------------------------------------------------------*/
376 /* Name - arch_get_rethrow_exception */
378 /* Function - Return a function pointer which can be used to */
379 /* raise exceptions. The returned function has the */
380 /* following signature: */
381 /* void (*func) (MonoException *exc); */
383 /*------------------------------------------------------------------*/
386 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
388 static guint8 *start;
389 static int inited = 0;
397 start = mono_global_codeman_reserve (SZ_THROW);
398 get_throw_exception_generic (start, SZ_THROW, FALSE, TRUE);
403 /*========================= End of Function ========================*/
405 /*------------------------------------------------------------------*/
407 /* Name - arch_get_throw_exception_by_name */
409 /* Function - Return a function pointer which can be used to */
410 /* raise corlib exceptions. The return function has */
411 /* the following signature: */
412 /* void (*func) (char *exc_name); */
414 /*------------------------------------------------------------------*/
417 mono_arch_get_throw_exception_by_name (void)
419 static guint8 *start;
420 static int inited = 0;
424 start = mono_global_codeman_reserve (SZ_THROW);
425 get_throw_exception_generic (start, SZ_THROW, TRUE, FALSE);
430 /*========================= End of Function ========================*/
432 /*------------------------------------------------------------------*/
434 /* Name - mono_arch_find_jit_info */
436 /* Function - This function is used to gather informatoin from */
437 /* @ctx. It returns the MonoJitInfo of the corres- */
438 /* ponding function, unwinds one stack frame and */
439 /* stores the resulting context into @new_ctx. It */
440 /* also stores a string describing the stack location*/
441 /* into @trace (if not NULL), and modifies the @lmf */
442 /* if necessary. @native_offset returns the IP off- */
443 /* set from the start of the function or -1 if that */
444 /* informatoin is not available. */
446 /*------------------------------------------------------------------*/
449 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
450 MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
451 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
454 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
455 MonoS390StackFrame *sframe;
458 (ip >= prev_ji->code_start &&
459 ((guint8 *) ip <= ((guint8 *) prev_ji->code_start) + prev_ji->code_size)))
462 ji = mini_jit_info_table_find (domain, ip, NULL);
472 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
473 /* remove any unused lmf */
474 *lmf = (*lmf)->previous_lmf;
477 address = (char *)ip - (char *)ji->code_start;
480 if (!ji->method->wrapper_type)
483 sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_SP (ctx);
484 MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
485 sframe = (MonoS390StackFrame *) sframe->prev;
486 MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
487 memcpy (&new_ctx->uc_mcontext.gregs[6], sframe->regs, (8*sizeof(gint32)));
496 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
498 memset (res, 0, MONO_SIZEOF_JIT_INFO);
499 res->method = (*lmf)->method;
502 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
503 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
505 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
506 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
507 *lmf = (*lmf)->previous_lmf;
509 return ji ? ji : res;
515 /*========================= End of Function ========================*/
517 /*------------------------------------------------------------------*/
519 /* Name - mono_arch_handle_exception */
521 /* Function - Handle an exception raised by the JIT code. */
523 /* Parameters - ctx - Saved processor state */
524 /* obj - The exception object */
525 /* test_only - Only test if the exception is caught, */
526 /* but don't call handlers */
528 /*------------------------------------------------------------------*/
531 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
533 return mono_handle_exception (uc, obj, mono_arch_ip_from_context(uc), test_only);
536 /*========================= End of Function ========================*/
538 /*------------------------------------------------------------------*/
540 /* Name - mono_arch_ip_from_context */
542 /* Function - Return the instruction pointer from the context. */
544 /* Parameters - sigctx - Saved processor state */
546 /*------------------------------------------------------------------*/
549 mono_arch_ip_from_context (void *sigctx)
551 return context_get_ip (sigctx);
555 /*========================= End of Function ========================*/
557 /*------------------------------------------------------------------*/
559 /* Name - mono_arch_get_restore_context */
561 /* Function - Return the address of the routine that will rest- */
562 /* ore the context. */
564 /*------------------------------------------------------------------*/
567 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
577 /*========================= End of Function ========================*/
579 /*------------------------------------------------------------------*/
581 /* Name - mono_arch_is_int_overflow */
583 /* Function - Inspect the code that raised the SIGFPE signal */
584 /* to see if the DivideByZero or Arithmetic exception*/
585 /* should be raised. */
587 /*------------------------------------------------------------------*/
590 mono_arch_is_int_overflow (void *uc, void *info)
595 gboolean arithExc = TRUE;
599 ctx = (MonoContext *) uc;
600 code = (guint8 *) ((siginfo_t *)info)->si_addr;
601 /*----------------------------------------------------------*/
602 /* Divide operations are the only ones that will give the */
603 /* divide by zero exception so just check for these ops. */
604 /*----------------------------------------------------------*/
606 case 0x1d : /* Divide Register */
607 regNo = code[1] & 0x0f;
608 if (ctx->uc_mcontext.gregs[regNo] == 0)
611 case 0x5d : /* Divide */
612 regNo = (code[2] & 0xf0 >> 8);
613 offset = *((guint16 *) code+2) & 0x0fff;
614 operand = (guint32*)(ctx->uc_mcontext.gregs[regNo] + offset);
618 case 0xb9 : /* Divide logical Register? */
619 if (code[1] == 0x97) {
620 regNo = (code[2] & 0xf0 >> 8);
621 if (ctx->uc_mcontext.gregs[regNo] == 0)
625 case 0xe3 : /* Divide logical? */
626 if (code[1] == 0x97) {
627 regNo = (code[2] & 0xf0 >> 8);
628 offset = *((guint32 *) code+1) & 0x000fffff;
629 operand = (guint32*)(ctx->uc_mcontext.gregs[regNo] + offset);
637 ctx->uc_mcontext.psw.addr = (guint32)code;
641 /*========================= End of Function ========================*/