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)))
36 #define S390_REG_SAVE_R13 (S390_REG_SAVE_OFFSET+(7*sizeof(gulong)))
40 #define setup_context(ctx)
42 /*========================= End of Defines =========================*/
44 /*------------------------------------------------------------------*/
46 /*------------------------------------------------------------------*/
54 #include <mono/arch/s390x/s390x-codegen.h>
55 #include <mono/metadata/appdomain.h>
56 #include <mono/metadata/tabledefs.h>
57 #include <mono/metadata/threads.h>
58 #include <mono/metadata/debug-helpers.h>
59 #include <mono/metadata/exception.h>
60 #include <mono/metadata/mono-debug.h>
63 #include "mini-s390x.h"
65 /*========================= End of Includes ========================*/
67 /*------------------------------------------------------------------*/
68 /* P r o t o t y p e s */
69 /*------------------------------------------------------------------*/
71 gboolean mono_arch_handle_exception (void *ctx,
75 /*========================= End of Prototypes ======================*/
77 /*------------------------------------------------------------------*/
78 /* G l o b a l V a r i a b l e s */
79 /*------------------------------------------------------------------*/
86 /*====================== End of Global Variables ===================*/
88 /*------------------------------------------------------------------*/
90 /* Name - mono_arch_get_call_filter */
92 /* Function - Return a pointer to a method which calls an */
93 /* exception filter. We also use this function to */
94 /* call finally handlers (we pass NULL as @exc */
95 /* object in this case). */
97 /*------------------------------------------------------------------*/
100 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
102 static guint8 *start;
103 static int inited = 0;
105 int alloc_size, pos, i;
106 GSList *unwind_ops = NULL;
107 MonoJumpInfo *ji = NULL;
115 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
116 code = start = mono_global_codeman_reserve (512);
118 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
119 s390_lgr (code, s390_r14, STK_BASE);
120 alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
121 s390_aghi (code, STK_BASE, -alloc_size);
122 s390_stg (code, s390_r14, 0, STK_BASE, 0);
124 /*------------------------------------------------------*/
125 /* save general registers on stack */
126 /*------------------------------------------------------*/
127 s390_stmg (code, s390_r0, STK_BASE, STK_BASE, S390_CALLFILTER_INTREGS);
129 /*------------------------------------------------------*/
130 /* save floating point registers on stack */
131 /*------------------------------------------------------*/
132 pos = S390_CALLFILTER_FLTREGS;
133 for (i = 0; i < 16; ++i) {
134 s390_std (code, i, 0, STK_BASE, pos);
135 pos += sizeof (gdouble);
138 /*------------------------------------------------------*/
139 /* save access registers on stack */
140 /*------------------------------------------------------*/
141 s390_stam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
143 /*------------------------------------------------------*/
145 /*------------------------------------------------------*/
146 s390_lgr (code, s390_r13, s390_r2);
148 /*------------------------------------------------------*/
149 /* Get A(Handler Entry Point) */
150 /*------------------------------------------------------*/
151 s390_lgr (code, s390_r0, s390_r3);
153 /*------------------------------------------------------*/
154 /* Set parameter register with Exception */
155 /*------------------------------------------------------*/
156 s390_lgr (code, s390_r2, s390_r4);
158 /*------------------------------------------------------*/
159 /* Load all registers with values from the context */
160 /*------------------------------------------------------*/
161 s390_lmg (code, s390_r3, s390_r12, s390_r13,
162 G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[3]));
163 pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
164 for (i = 0; i < 16; ++i) {
165 s390_ld (code, i, 0, s390_r13, pos);
166 pos += sizeof(gdouble);
170 /*------------------------------------------------------*/
171 /* We need to preserve current SP before calling filter */
172 /* with SP from the context */
173 /*------------------------------------------------------*/
174 s390_lgr (code, s390_r14, STK_BASE);
175 s390_lg (code, STK_BASE, 0, s390_r13,
176 G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[15]));
177 s390_lgr (code, s390_r13, s390_r14);
180 /*------------------------------------------------------*/
182 /*------------------------------------------------------*/
183 s390_lgr (code, s390_r1, s390_r0);
184 s390_basr (code, s390_r14, s390_r1);
186 /*------------------------------------------------------*/
187 /* Save return value */
188 /*------------------------------------------------------*/
189 s390_lgr (code, s390_r14, s390_r2);
192 /*------------------------------------------------------*/
193 /* Reload our stack register with value saved in context*/
194 /*------------------------------------------------------*/
195 s390_lgr (code, STK_BASE, s390_r13);
198 /*------------------------------------------------------*/
199 /* Restore all the regs from the stack */
200 /*------------------------------------------------------*/
201 s390_lmg (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
202 pos = S390_CALLFILTER_FLTREGS;
203 for (i = 0; i < 16; ++i) {
204 s390_ld (code, i, 0, STK_BASE, pos);
205 pos += sizeof (gdouble);
208 s390_lgr (code, s390_r2, s390_r14);
209 s390_lam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
210 s390_aghi (code, s390_r15, alloc_size);
211 s390_lmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
212 s390_br (code, s390_r14);
214 g_assert ((code - start) < SZ_THROW);
217 *info = mono_tramp_info_create (g_strdup_printf("call_filter"),
218 start, code - start, ji,
224 /*========================= End of Function ========================*/
226 /*------------------------------------------------------------------*/
228 /* Name - throw_exception. */
230 /* Function - Raise an exception based on the parameters passed.*/
232 /*------------------------------------------------------------------*/
235 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
236 gulong *int_regs, gdouble *fp_regs, gint32 *acc_regs,
237 guint fpc, gboolean rethrow)
241 static void (*restore_context) (MonoContext *);
243 if (!restore_context)
244 restore_context = mono_get_restore_context();
246 memset(&ctx, 0, sizeof(ctx));
250 /* adjust eip so that it point into the call instruction */
253 for (iReg = 0; iReg < 16; iReg++) {
254 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
255 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
256 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
259 ctx.uc_mcontext.fpregs.fpc = fpc;
261 MONO_CONTEXT_SET_BP (&ctx, sp);
262 MONO_CONTEXT_SET_IP (&ctx, ip);
264 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
265 MonoException *mono_ex = (MonoException*)exc;
267 mono_ex->stack_trace = NULL;
269 // mono_arch_handle_exception (&ctx, exc, FALSE);
270 mono_handle_exception (&ctx, exc, (gpointer) ip, FALSE);
271 restore_context(&ctx);
273 g_assert_not_reached ();
276 /*========================= End of Function ========================*/
278 /*------------------------------------------------------------------*/
280 /* Name - get_throw_exception_generic */
282 /* Function - Return a function pointer which can be used to */
283 /* raise exceptions. The returned function has the */
284 /* following signature: */
285 /* void (*func) (MonoException *exc); or, */
286 /* void (*func) (char *exc_name); */
288 /*------------------------------------------------------------------*/
291 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info,
292 int corlib, gboolean rethrow, gboolean aot)
294 guint8 *code, *start;
295 int alloc_size, pos, i;
296 MonoJumpInfo *ji = NULL;
297 GSList *unwind_ops = NULL;
299 code = start = mono_global_codeman_reserve(size);
301 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
302 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
303 s390_lgr (code, s390_r14, STK_BASE);
304 s390_aghi (code, STK_BASE, -alloc_size);
305 s390_stg (code, s390_r14, 0, STK_BASE, 0);
306 s390_lgr (code, s390_r3, s390_r2);
308 s390_basr (code, s390_r13, 0);
310 s390_llong(code, mono_defaults.exception_class->image);
311 s390_llong(code, mono_exception_from_token);
312 s390_lg (code, s390_r2, 0, s390_r13, 4);
313 s390_lg (code, s390_r1, 0, s390_r13, 12);
314 s390_basr (code, s390_r14, s390_r1);
317 /*------------------------------------------------------*/
318 /* save the general registers on the stack */
319 /*------------------------------------------------------*/
320 s390_stmg (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
322 s390_lgr (code, s390_r1, STK_BASE);
323 s390_aghi (code, s390_r1, alloc_size);
324 /*------------------------------------------------------*/
325 /* save the return address in the parameter register */
326 /*------------------------------------------------------*/
327 s390_lg (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
329 /*------------------------------------------------------*/
330 /* save the floating point registers */
331 /*------------------------------------------------------*/
332 pos = S390_THROWSTACK_FLTREGS;
333 for (i = 0; i < 16; ++i) {
334 s390_std (code, i, 0, STK_BASE, pos);
335 pos += sizeof (gdouble);
337 /*------------------------------------------------------*/
338 /* save the access registers */
339 /*------------------------------------------------------*/
340 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
342 /*------------------------------------------------------*/
343 /* call throw_exception (tkn, ip, sp, gr, fr, ar, re) */
344 /* - r2 already contains *exc */
345 /*------------------------------------------------------*/
346 s390_lgr (code, s390_r4, s390_r1); /* caller sp */
348 /*------------------------------------------------------*/
349 /* pointer to the saved int regs */
350 /*------------------------------------------------------*/
351 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
352 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
353 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
354 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
355 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM+4);
356 s390_lghi (code, s390_r7, rethrow);
357 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
358 s390_basr (code, s390_r13, 0);
360 s390_llong(code, throw_exception);
361 s390_lg (code, s390_r1, 0, s390_r13, 4);
362 s390_basr (code, s390_r14, s390_r1);
363 /* we should never reach this breakpoint */
365 g_assert ((code - start) < size);
368 *info = mono_tramp_info_create (g_strdup_printf(corlib ? "throw_corlib_exception"
369 : (rethrow ? "rethrow_exception"
370 : "throw_exception")),
371 start, code - start, ji, unwind_ops);
376 /*========================= End of Function ========================*/
378 /*------------------------------------------------------------------*/
380 /* Name - arch_get_throw_exception */
382 /* Function - Return a function pointer which can be used to */
383 /* raise exceptions. The returned function has the */
384 /* following signature: */
385 /* void (*func) (MonoException *exc); */
387 /*------------------------------------------------------------------*/
390 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
397 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
400 /*========================= End of Function ========================*/
402 /*------------------------------------------------------------------*/
404 /* Name - arch_get_rethrow_exception */
406 /* Function - Return a function pointer which can be used to */
407 /* raise exceptions. The returned function has the */
408 /* following signature: */
409 /* void (*func) (MonoException *exc); */
411 /*------------------------------------------------------------------*/
414 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
420 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
423 /*========================= End of Function ========================*/
425 /*------------------------------------------------------------------*/
427 /* Name - arch_get_corlib_exception */
429 /* Function - Return a function pointer which can be used to */
430 /* raise corlib exceptions. The return function has */
431 /* the following signature: */
432 /* void (*func) (guint32 token, guint32 offset) */
434 /*------------------------------------------------------------------*/
437 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
443 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, TRUE, FALSE, aot));
446 /*========================= End of Function ========================*/
448 /*------------------------------------------------------------------*/
450 /* Name - mono_arch_find_jit_info */
452 /* Function - See exceptions-amd64.c for docs. */
454 /*------------------------------------------------------------------*/
457 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
458 MonoJitInfo *ji, MonoContext *ctx,
459 MonoContext *new_ctx, MonoLMF **lmf,
460 mgreg_t **save_locations,
461 StackFrameInfo *frame)
463 gpointer ip = (gpointer) MONO_CONTEXT_GET_IP (ctx);
464 MonoS390StackFrame *sframe;
466 memset (frame, 0, sizeof (StackFrameInfo));
474 guint32 unwind_info_len;
478 frame->type = FRAME_TYPE_MANAGED;
481 unwind_info = mono_aot_get_unwind_info(ji, &unwind_info_len);
483 unwind_info = mono_get_cached_unwind_info(ji->used_regs, &unwind_info_len);
485 if (*lmf && ((*lmf) != jit_tls->first_lmf) &&
486 (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
487 /* remove any unused lmf */
488 *lmf = (*lmf)->previous_lmf;
491 address = (char *)ip - (char *)ji->code_start;
493 memcpy(®s, &ctx->uc_mcontext.gregs, sizeof(regs));
494 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
495 (guint8 *) ji->code_start + ji->code_size,
496 ip, regs, 16, save_locations,
497 MONO_MAX_IREGS, &cfa);
498 memcpy (&new_ctx->uc_mcontext.gregs, ®s, sizeof(regs));
499 MONO_CONTEXT_SET_IP(new_ctx, regs[14] - 2);
500 MONO_CONTEXT_SET_BP(new_ctx, cfa);
502 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
503 /* remove any unused lmf */
504 *lmf = (*lmf)->previous_lmf;
509 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
514 frame->method = (*lmf)->method;
518 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
520 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
521 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
522 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
523 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip - 2);
524 *lmf = (*lmf)->previous_lmf;
532 /*========================= End of Function ========================*/
534 /*------------------------------------------------------------------*/
536 /* Name - mono_arch_handle_exception */
538 /* Function - Handle an exception raised by the JIT code. */
540 /* Parameters - ctx - Saved processor state */
541 /* obj - The exception object */
542 /* test_only - Only test if the exception is caught, */
543 /* but don't call handlers */
545 /*------------------------------------------------------------------*/
548 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
550 return mono_handle_exception (uc, obj, mono_arch_ip_from_context(uc), test_only);
553 /*========================= End of Function ========================*/
555 /*------------------------------------------------------------------*/
557 /* Name - mono_arch_sigctx_to_monoctx. */
559 /* Function - Called from the signal handler to convert signal */
560 /* context to MonoContext. */
562 /*------------------------------------------------------------------*/
565 mono_arch_sigctx_to_monoctx (void *ctx, MonoContext *mctx)
567 memcpy (mctx, ctx, sizeof(MonoContext));
570 /*========================= End of Function ========================*/
572 /*------------------------------------------------------------------*/
574 /* Name - mono_arch_monoctx_to_sigctx. */
576 /* Function - Convert MonoContext structure to signal context. */
578 /*------------------------------------------------------------------*/
581 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
583 memcpy (ctx, mctx, sizeof(MonoContext));
586 /*========================= End of Function ========================*/
588 /*------------------------------------------------------------------*/
590 /* Name - mono_arch_ip_from_context */
592 /* Function - Return the instruction pointer from the context. */
594 /* Parameters - sigctx - Saved processor state */
596 /*------------------------------------------------------------------*/
599 mono_arch_ip_from_context (void *sigctx)
601 return ((gpointer) MONO_CONTEXT_GET_IP(((MonoContext *) sigctx)));
605 /*========================= End of Function ========================*/
607 /*------------------------------------------------------------------*/
609 /* Name - mono_arch_get_restore_context */
611 /* Function - Return the address of the routine that will rest- */
612 /* ore the context. */
614 /*------------------------------------------------------------------*/
617 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
626 /*========================= End of Function ========================*/
628 /*------------------------------------------------------------------*/
630 /* Name - mono_arch_is_int_overflow */
632 /* Function - Inspect the code that raised the SIGFPE signal */
633 /* to see if the DivideByZero or Arithmetic exception*/
634 /* should be raised. */
636 /*------------------------------------------------------------------*/
639 mono_arch_is_int_overflow (void *uc, void *info)
644 gboolean arithExc = TRUE;
649 ctx = (MonoContext *) uc;
650 code = (guint8 *) ((siginfo_t *)info)->si_addr;
651 /*----------------------------------------------------------*/
652 /* Divide operations are the only ones that will give the */
653 /* divide by zero exception so just check for these ops. */
654 /*----------------------------------------------------------*/
656 case 0x1d : /* Divide Register */
657 regNo = code[1] & 0x0f;
658 if (ctx->uc_mcontext.gregs[regNo] == 0)
661 case 0x5d : /* Divide */
662 regNo = (code[2] & 0xf0 >> 8);
663 idxNo = (code[1] & 0x0f);
664 offset = *((guint16 *) code+2) & 0x0fff;
665 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
667 operand += ctx->uc_mcontext.gregs[idxNo];
671 case 0xb9 : /* DL[GR] or DS[GR] */
672 if ((code[1] == 0x97) || (code[1] == 0x87) ||
673 (code[1] == 0x0d) || (code[1] == 0x1d)) {
674 regNo = (code[3] & 0x0f);
675 if (ctx->uc_mcontext.gregs[regNo] == 0)
679 case 0xe3 : /* DL[G] | DS[G] */
680 if ((code[5] == 0x97) || (code[5] == 0x87) ||
681 (code[5] == 0x0d) || (code[5] == 0x1d)) {
682 regNo = (code[2] & 0xf0 >> 8);
683 idxNo = (code[1] & 0x0f);
684 offset = (code[2] & 0x0f << 8) +
685 code[3] + (code[4] << 12);
686 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
688 operand += ctx->uc_mcontext.gregs[idxNo];
696 ctx->uc_mcontext.psw.addr = (guint64)code;
700 /*========================= End of Function ========================*/