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(gint32)))
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(gint32)))
38 /*========================= End of Defines =========================*/
40 /*------------------------------------------------------------------*/
42 /*------------------------------------------------------------------*/
50 #include <mono/arch/s390x/s390x-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-s390x.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 /*------------------------------------------------------------------*/
82 /*====================== End of Global Variables ===================*/
84 /*------------------------------------------------------------------*/
86 /* Name - mono_arch_has_unwind_info */
88 /* Function - Tests if a function has a DWARF exception table */
89 /* that is able to restore all caller saved registers*/
91 /*------------------------------------------------------------------*/
94 mono_arch_has_unwind_info (gconstpointer addr)
99 /*========================= End of Function ========================*/
101 /*------------------------------------------------------------------*/
103 /* Name - mono_arch_get_call_filter */
105 /* Function - Return a pointer to a method which calls an */
106 /* exception filter. We also use this function to */
107 /* call finally handlers (we pass NULL as @exc */
108 /* object in this case). */
110 /*------------------------------------------------------------------*/
113 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
115 static guint8 *start;
116 static int inited = 0;
118 int alloc_size, pos, i;
128 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
129 code = start = mono_global_codeman_reserve (512);
131 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
132 s390_lgr (code, s390_r14, STK_BASE);
133 alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
134 s390_aghi (code, STK_BASE, -alloc_size);
135 s390_stg (code, s390_r14, 0, STK_BASE, 0);
137 /*------------------------------------------------------*/
138 /* save general registers on stack */
139 /*------------------------------------------------------*/
140 s390_stmg (code, s390_r0, STK_BASE, STK_BASE, S390_CALLFILTER_INTREGS);
142 /*------------------------------------------------------*/
143 /* save floating point registers on stack */
144 /*------------------------------------------------------*/
145 pos = S390_CALLFILTER_FLTREGS;
146 for (i = 0; i < 16; ++i) {
147 s390_std (code, i, 0, STK_BASE, pos);
148 pos += sizeof (gdouble);
151 /*------------------------------------------------------*/
152 /* save access registers on stack */
153 /*------------------------------------------------------*/
154 s390_stam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
156 /*------------------------------------------------------*/
158 /*------------------------------------------------------*/
159 s390_lgr (code, s390_r13, s390_r2);
161 /*------------------------------------------------------*/
162 /* Get A(Handler Entry Point) */
163 /*------------------------------------------------------*/
164 s390_lgr (code, s390_r0, s390_r3);
166 /*------------------------------------------------------*/
167 /* Set parameter register with Exception */
168 /*------------------------------------------------------*/
169 s390_lgr (code, s390_r2, s390_r4);
171 /*------------------------------------------------------*/
172 /* Load all registers with values from the context */
173 /*------------------------------------------------------*/
174 s390_lmg (code, s390_r3, s390_r12, s390_r13,
175 G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[3]));
176 pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
177 for (i = 0; i < 16; ++i) {
178 s390_ld (code, i, 0, s390_r13, pos);
179 pos += sizeof(gdouble);
182 /*------------------------------------------------------*/
184 /*------------------------------------------------------*/
185 s390_lgr (code, s390_r1, s390_r0);
186 s390_basr (code, s390_r14, s390_r1);
188 /*------------------------------------------------------*/
189 /* Save return value */
190 /*------------------------------------------------------*/
191 s390_lgr (code, s390_r14, s390_r2);
193 /*------------------------------------------------------*/
194 /* Restore all the regs from the stack */
195 /*------------------------------------------------------*/
196 s390_lmg (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
197 pos = S390_CALLFILTER_FLTREGS;
198 for (i = 0; i < 16; ++i) {
199 s390_ld (code, i, 0, STK_BASE, pos);
200 pos += sizeof (gdouble);
203 s390_lgr (code, s390_r2, s390_r14);
204 s390_lam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
205 s390_aghi (code, s390_r15, alloc_size);
206 s390_lmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
207 s390_br (code, s390_r14);
209 g_assert ((code - start) < SZ_THROW);
213 /*========================= End of Function ========================*/
215 /*------------------------------------------------------------------*/
217 /* Name - throw_exception. */
219 /* Function - Raise an exception based on the parameters passed.*/
221 /*------------------------------------------------------------------*/
224 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
225 gulong *int_regs, gdouble *fp_regs, gint32 *acc_regs,
226 guint fpc, gboolean rethrow)
230 static void (*restore_context) (MonoContext *);
232 if (!restore_context)
233 restore_context = mono_get_restore_context();
235 memset(&ctx, 0, sizeof(ctx));
239 /* adjust eip so that it point into the call instruction */
242 for (iReg = 0; iReg < 16; iReg++) {
243 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
244 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
245 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
248 ctx.uc_mcontext.fpregs.fpc = fpc;
250 MONO_CONTEXT_SET_BP (&ctx, sp);
251 MONO_CONTEXT_SET_IP (&ctx, ip);
253 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
254 MonoException *mono_ex = (MonoException*)exc;
256 mono_ex->stack_trace = NULL;
258 mono_arch_handle_exception (&ctx, exc, FALSE);
259 restore_context(&ctx);
261 g_assert_not_reached ();
264 /*========================= End of Function ========================*/
266 /*------------------------------------------------------------------*/
268 /* Name - get_throw_exception_generic */
270 /* Function - Return a function pointer which can be used to */
271 /* raise exceptions. The returned function has the */
272 /* following signature: */
273 /* void (*func) (MonoException *exc); or, */
274 /* void (*func) (char *exc_name); */
276 /*------------------------------------------------------------------*/
279 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info,
280 int corlib, gboolean rethrow, gboolean aot)
282 guint8 *code, *start;
283 int alloc_size, pos, i;
285 code = start = mono_global_codeman_reserve(size);
287 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
288 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
289 s390_lgr (code, s390_r14, STK_BASE);
290 s390_aghi (code, STK_BASE, -alloc_size);
291 s390_stg (code, s390_r14, 0, STK_BASE, 0);
292 s390_lgr (code, s390_r3, s390_r2);
294 s390_basr (code, s390_r13, 0);
296 s390_llong(code, mono_defaults.exception_class->image);
297 s390_llong(code, mono_exception_from_token);
298 s390_lg (code, s390_r2, 0, s390_r13, 4);
299 s390_lg (code, s390_r1, 0, s390_r13, 12);
300 s390_basr (code, s390_r14, s390_r1);
303 /*------------------------------------------------------*/
304 /* save the general registers on the stack */
305 /*------------------------------------------------------*/
306 s390_stmg (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
308 s390_lgr (code, s390_r1, STK_BASE);
309 s390_aghi (code, s390_r1, alloc_size);
310 /*------------------------------------------------------*/
311 /* save the return address in the parameter register */
312 /*------------------------------------------------------*/
313 s390_lg (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
315 /*------------------------------------------------------*/
316 /* save the floating point registers */
317 /*------------------------------------------------------*/
318 pos = S390_THROWSTACK_FLTREGS;
319 for (i = 0; i < 16; ++i) {
320 s390_std (code, i, 0, STK_BASE, pos);
321 pos += sizeof (gdouble);
323 /*------------------------------------------------------*/
324 /* save the access registers */
325 /*------------------------------------------------------*/
326 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
328 /*------------------------------------------------------*/
329 /* call throw_exception (tkn, ip, sp, gr, fr, ar, re) */
330 /* - r2 already contains *exc */
331 /*------------------------------------------------------*/
332 s390_lgr (code, s390_r4, s390_r1); /* caller sp */
334 /*------------------------------------------------------*/
335 /* pointer to the saved int regs */
336 /*------------------------------------------------------*/
337 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
338 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
339 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
340 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
341 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM+4);
342 s390_lghi (code, s390_r7, rethrow);
343 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
344 s390_basr (code, s390_r13, 0);
346 s390_llong(code, throw_exception);
347 s390_lg (code, s390_r1, 0, s390_r13, 4);
348 s390_basr (code, s390_r14, s390_r1);
349 /* we should never reach this breakpoint */
351 g_assert ((code - start) < size);
355 /*========================= End of Function ========================*/
357 /*------------------------------------------------------------------*/
359 /* Name - arch_get_throw_exception */
361 /* Function - Return a function pointer which can be used to */
362 /* raise exceptions. The returned function has the */
363 /* following signature: */
364 /* void (*func) (MonoException *exc); */
366 /*------------------------------------------------------------------*/
369 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
376 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
379 /*========================= End of Function ========================*/
381 /*------------------------------------------------------------------*/
383 /* Name - arch_get_rethrow_exception */
385 /* Function - Return a function pointer which can be used to */
386 /* raise exceptions. The returned function has the */
387 /* following signature: */
388 /* void (*func) (MonoException *exc); */
390 /*------------------------------------------------------------------*/
393 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
399 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
402 /*========================= End of Function ========================*/
404 /*------------------------------------------------------------------*/
406 /* Name - arch_get_corlib_exception */
408 /* Function - Return a function pointer which can be used to */
409 /* raise corlib exceptions. The return function has */
410 /* the following signature: */
411 /* void (*func) (guint32 token, guint32 offset) */
413 /*------------------------------------------------------------------*/
416 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
422 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, TRUE, FALSE, aot));
425 /*========================= End of Function ========================*/
427 /*------------------------------------------------------------------*/
429 /* Name - mono_arch_find_jit_info */
431 /* Function - This function is used to gather information from */
432 /* @ctx. It returns the MonoJitInfo of the corres- */
433 /* ponding function, unwinds one stack frame and */
434 /* stores the resulting context into @new_ctx. It */
435 /* also stores a string describing the stack location*/
436 /* into @trace (if not NULL), and modifies the @lmf */
437 /* if necessary. @native_offset returns the IP off- */
438 /* set from the start of the function or -1 if that */
439 /* information is not available. */
441 /*------------------------------------------------------------------*/
444 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
445 MonoJitInfo *ji, MonoContext *ctx,
446 MonoContext *new_ctx, MonoLMF **lmf,
447 StackFrameInfo *frame)
449 gpointer ip = (gpointer) MONO_CONTEXT_GET_IP (ctx);
450 MonoS390StackFrame *sframe;
452 memset (frame, 0, sizeof (StackFrameInfo));
454 frame->managed = FALSE;
461 frame->type = FRAME_TYPE_MANAGED;
463 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
464 frame->managed = TRUE;
466 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
467 /* remove any unused lmf */
468 *lmf = (*lmf)->previous_lmf;
471 address = (char *)ip - (char *)ji->code_start;
473 sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_SP (ctx);
474 MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
475 sframe = (MonoS390StackFrame *) sframe->prev;
476 MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
477 memcpy (&new_ctx->uc_mcontext.gregs[6], sframe->regs, (8*sizeof(gint64)));
484 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
486 // FIXME: This can happen with multiple appdomains (bug #444383)
491 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
493 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
494 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
496 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
497 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
498 *lmf = (*lmf)->previous_lmf;
506 /*========================= End of Function ========================*/
508 /*------------------------------------------------------------------*/
510 /* Name - mono_arch_handle_exception */
512 /* Function - Handle an exception raised by the JIT code. */
514 /* Parameters - ctx - Saved processor state */
515 /* obj - The exception object */
516 /* test_only - Only test if the exception is caught, */
517 /* but don't call handlers */
519 /*------------------------------------------------------------------*/
522 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
524 return mono_handle_exception (uc, obj, mono_arch_ip_from_context(uc), test_only);
527 /*========================= End of Function ========================*/
529 /*------------------------------------------------------------------*/
531 /* Name - mono_arch_sigctx_to_monoctx. */
533 /* Function - Called from the signal handler to convert signal */
534 /* context to MonoContext. */
536 /*------------------------------------------------------------------*/
539 mono_arch_sigctx_to_monoctx (void *ctx, MonoContext *mctx)
541 memcpy (mctx, ctx, sizeof(MonoContext));
544 /*========================= End of Function ========================*/
546 /*------------------------------------------------------------------*/
548 /* Name - mono_arch_monoctx_to_sigctx. */
550 /* Function - Convert MonoContext structure to signal context. */
552 /*------------------------------------------------------------------*/
555 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
557 memcpy (ctx, mctx, sizeof(MonoContext));
560 /*========================= End of Function ========================*/
562 /*------------------------------------------------------------------*/
564 /* Name - mono_arch_ip_from_context */
566 /* Function - Return the instruction pointer from the context. */
568 /* Parameters - sigctx - Saved processor state */
570 /*------------------------------------------------------------------*/
573 mono_arch_ip_from_context (void *sigctx)
575 return ((gpointer) MONO_CONTEXT_GET_IP(((MonoContext *) sigctx)));
579 /*========================= End of Function ========================*/
581 /*------------------------------------------------------------------*/
583 /* Name - mono_arch_get_restore_context */
585 /* Function - Return the address of the routine that will rest- */
586 /* ore the context. */
588 /*------------------------------------------------------------------*/
591 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
600 /*========================= End of Function ========================*/
602 /*------------------------------------------------------------------*/
604 /* Name - mono_arch_is_int_overflow */
606 /* Function - Inspect the code that raised the SIGFPE signal */
607 /* to see if the DivideByZero or Arithmetic exception*/
608 /* should be raised. */
610 /*------------------------------------------------------------------*/
613 mono_arch_is_int_overflow (void *uc, void *info)
618 gboolean arithExc = TRUE;
623 ctx = (MonoContext *) uc;
624 code = (guint8 *) ((siginfo_t *)info)->si_addr;
625 /*----------------------------------------------------------*/
626 /* Divide operations are the only ones that will give the */
627 /* divide by zero exception so just check for these ops. */
628 /*----------------------------------------------------------*/
630 case 0x1d : /* Divide Register */
631 regNo = code[1] & 0x0f;
632 if (ctx->uc_mcontext.gregs[regNo] == 0)
635 case 0x5d : /* Divide */
636 regNo = (code[2] & 0xf0 >> 8);
637 idxNo = (code[1] & 0x0f);
638 offset = *((guint16 *) code+2) & 0x0fff;
639 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
641 operand += ctx->uc_mcontext.gregs[idxNo];
645 case 0xb9 : /* DL[GR] or DS[GR] */
646 if ((code[1] == 0x97) || (code[1] == 0x87) ||
647 (code[1] == 0x0d) || (code[1] == 0x1d)) {
648 regNo = (code[3] & 0x0f);
649 if (ctx->uc_mcontext.gregs[regNo] == 0)
653 case 0xe3 : /* DL[G] | DS[G] */
654 if ((code[5] == 0x97) || (code[5] == 0x87) ||
655 (code[5] == 0x0d) || (code[5] == 0x1d)) {
656 regNo = (code[2] & 0xf0 >> 8);
657 idxNo = (code[1] & 0x0f);
658 offset = (code[2] & 0x0f << 8) +
659 code[3] + (code[4] << 12);
660 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
662 operand += ctx->uc_mcontext.gregs[idxNo];
670 ctx->uc_mcontext.psw.addr = (guint64)code;
674 /*========================= End of Function ========================*/