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 #define setup_context(ctx)
40 /*========================= End of Defines =========================*/
42 /*------------------------------------------------------------------*/
44 /*------------------------------------------------------------------*/
52 #include <mono/arch/s390x/s390x-codegen.h>
53 #include <mono/metadata/appdomain.h>
54 #include <mono/metadata/tabledefs.h>
55 #include <mono/metadata/threads.h>
56 #include <mono/metadata/debug-helpers.h>
57 #include <mono/metadata/exception.h>
58 #include <mono/metadata/mono-debug.h>
61 #include "mini-s390x.h"
63 /*========================= End of Includes ========================*/
65 /*------------------------------------------------------------------*/
66 /* P r o t o t y p e s */
67 /*------------------------------------------------------------------*/
69 gboolean mono_arch_handle_exception (void *ctx,
72 /*========================= End of Prototypes ======================*/
74 /*------------------------------------------------------------------*/
75 /* G l o b a l V a r i a b l e s */
76 /*------------------------------------------------------------------*/
83 /*====================== End of Global Variables ===================*/
85 /*------------------------------------------------------------------*/
87 /* Name - mono_arch_get_call_filter */
89 /* Function - Return a pointer to a method which calls an */
90 /* exception filter. We also use this function to */
91 /* call finally handlers (we pass NULL as @exc */
92 /* object in this case). */
94 /*------------------------------------------------------------------*/
97 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
100 static int inited = 0;
102 int alloc_size, pos, i;
103 GSList *unwind_ops = NULL;
104 MonoJumpInfo *ji = NULL;
112 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
113 code = start = mono_global_codeman_reserve (512);
115 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
116 s390_lgr (code, s390_r14, STK_BASE);
117 alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
118 s390_aghi (code, STK_BASE, -alloc_size);
119 s390_stg (code, s390_r14, 0, STK_BASE, 0);
121 /*------------------------------------------------------*/
122 /* save general registers on stack */
123 /*------------------------------------------------------*/
124 s390_stmg (code, s390_r0, STK_BASE, STK_BASE, S390_CALLFILTER_INTREGS);
126 /*------------------------------------------------------*/
127 /* save floating point registers on stack */
128 /*------------------------------------------------------*/
129 pos = S390_CALLFILTER_FLTREGS;
130 for (i = 0; i < 16; ++i) {
131 s390_std (code, i, 0, STK_BASE, pos);
132 pos += sizeof (gdouble);
135 /*------------------------------------------------------*/
136 /* save access registers on stack */
137 /*------------------------------------------------------*/
138 s390_stam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
140 /*------------------------------------------------------*/
142 /*------------------------------------------------------*/
143 s390_lgr (code, s390_r13, s390_r2);
145 /*------------------------------------------------------*/
146 /* Get A(Handler Entry Point) */
147 /*------------------------------------------------------*/
148 s390_lgr (code, s390_r0, s390_r3);
150 /*------------------------------------------------------*/
151 /* Set parameter register with Exception */
152 /*------------------------------------------------------*/
153 s390_lgr (code, s390_r2, s390_r4);
155 /*------------------------------------------------------*/
156 /* Load all registers with values from the context */
157 /*------------------------------------------------------*/
158 s390_lmg (code, s390_r3, s390_r12, s390_r13,
159 G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[3]));
160 pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
161 for (i = 0; i < 16; ++i) {
162 s390_ld (code, i, 0, s390_r13, pos);
163 pos += sizeof(gdouble);
167 /*------------------------------------------------------*/
168 /* We need to preserve current SP before calling filter */
169 /* with SP from the context */
170 /*------------------------------------------------------*/
171 s390_lgr (code, s390_r14, STK_BASE);
172 s390_lg (code, STK_BASE, 0, s390_r13,
173 G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[15]));
174 s390_lgr (code, s390_r13, s390_r14);
177 /*------------------------------------------------------*/
179 /*------------------------------------------------------*/
180 s390_lgr (code, s390_r1, s390_r0);
181 s390_basr (code, s390_r14, s390_r1);
183 /*------------------------------------------------------*/
184 /* Save return value */
185 /*------------------------------------------------------*/
186 s390_lgr (code, s390_r14, s390_r2);
189 /*------------------------------------------------------*/
190 /* Reload our stack register with value saved in context*/
191 /*------------------------------------------------------*/
192 s390_lgr (code, STK_BASE, s390_r13);
195 /*------------------------------------------------------*/
196 /* Restore all the regs from the stack */
197 /*------------------------------------------------------*/
198 s390_lmg (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
199 pos = S390_CALLFILTER_FLTREGS;
200 for (i = 0; i < 16; ++i) {
201 s390_ld (code, i, 0, STK_BASE, pos);
202 pos += sizeof (gdouble);
205 s390_lgr (code, s390_r2, s390_r14);
206 s390_lam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
207 s390_aghi (code, s390_r15, alloc_size);
208 s390_lmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
209 s390_br (code, s390_r14);
211 g_assert ((code - start) < SZ_THROW);
213 mono_arch_flush_icache(start, code - start);
214 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
217 *info = mono_tramp_info_create ("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)
242 memset(&ctx, 0, sizeof(ctx));
246 /* adjust eip so that it point into the call instruction */
249 for (iReg = 0; iReg < 16; iReg++) {
250 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
251 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
252 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
255 ctx.uc_mcontext.fpregs.fpc = fpc;
257 MONO_CONTEXT_SET_BP (&ctx, sp);
258 MONO_CONTEXT_SET_IP (&ctx, ip);
260 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
261 MonoException *mono_ex = (MonoException*)exc;
263 mono_ex->stack_trace = NULL;
265 // mono_arch_handle_exception (&ctx, exc, FALSE);
266 mono_handle_exception (&ctx, exc);
267 mono_restore_context(&ctx);
269 g_assert_not_reached ();
272 /*========================= End of Function ========================*/
274 /*------------------------------------------------------------------*/
276 /* Name - get_throw_exception_generic */
278 /* Function - Return a function pointer which can be used to */
279 /* raise exceptions. The returned function has the */
280 /* following signature: */
281 /* void (*func) (MonoException *exc); or, */
282 /* void (*func) (char *exc_name); */
284 /*------------------------------------------------------------------*/
287 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info,
288 int corlib, gboolean rethrow, gboolean aot)
290 guint8 *code, *start;
291 int alloc_size, pos, i;
292 MonoJumpInfo *ji = NULL;
293 GSList *unwind_ops = NULL;
295 code = start = mono_global_codeman_reserve(size);
297 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
298 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
299 s390_lgr (code, s390_r14, STK_BASE);
300 s390_aghi (code, STK_BASE, -alloc_size);
301 s390_stg (code, s390_r14, 0, STK_BASE, 0);
302 s390_lgr (code, s390_r3, s390_r2);
304 s390_basr (code, s390_r13, 0);
306 s390_llong(code, mono_defaults.exception_class->image);
307 s390_llong(code, mono_exception_from_token);
308 s390_lg (code, s390_r2, 0, s390_r13, 4);
309 s390_lg (code, s390_r1, 0, s390_r13, 12);
310 s390_basr (code, s390_r14, s390_r1);
313 /*------------------------------------------------------*/
314 /* save the general registers on the stack */
315 /*------------------------------------------------------*/
316 s390_stmg (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
318 s390_lgr (code, s390_r1, STK_BASE);
319 s390_aghi (code, s390_r1, alloc_size);
320 /*------------------------------------------------------*/
321 /* save the return address in the parameter register */
322 /*------------------------------------------------------*/
323 s390_lg (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
325 /*------------------------------------------------------*/
326 /* save the floating point registers */
327 /*------------------------------------------------------*/
328 pos = S390_THROWSTACK_FLTREGS;
329 for (i = 0; i < 16; ++i) {
330 s390_std (code, i, 0, STK_BASE, pos);
331 pos += sizeof (gdouble);
333 /*------------------------------------------------------*/
334 /* save the access registers */
335 /*------------------------------------------------------*/
336 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
338 /*------------------------------------------------------*/
339 /* call throw_exception (tkn, ip, sp, gr, fr, ar, re) */
340 /* - r2 already contains *exc */
341 /*------------------------------------------------------*/
342 s390_lgr (code, s390_r4, s390_r1); /* caller sp */
344 /*------------------------------------------------------*/
345 /* pointer to the saved int regs */
346 /*------------------------------------------------------*/
347 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
348 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
349 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
350 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
351 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM+4);
352 s390_lghi (code, s390_r7, rethrow);
353 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
354 s390_basr (code, s390_r13, 0);
356 s390_llong(code, throw_exception);
357 s390_lg (code, s390_r1, 0, s390_r13, 4);
358 s390_basr (code, s390_r14, s390_r1);
359 /* we should never reach this breakpoint */
361 g_assert ((code - start) < size);
363 mono_arch_flush_icache (start, code - start);
364 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
367 *info = mono_tramp_info_create (corlib ? "throw_corlib_exception"
368 : (rethrow ? "rethrow_exception"
369 : "throw_exception"),
370 start, code - start, ji, unwind_ops);
375 /*========================= End of Function ========================*/
377 /*------------------------------------------------------------------*/
379 /* Name - arch_get_throw_exception */
381 /* Function - Return a function pointer which can be used to */
382 /* raise exceptions. The returned function has the */
383 /* following signature: */
384 /* void (*func) (MonoException *exc); */
386 /*------------------------------------------------------------------*/
389 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
396 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
399 /*========================= End of Function ========================*/
401 /*------------------------------------------------------------------*/
403 /* Name - arch_get_rethrow_exception */
405 /* Function - Return a function pointer which can be used to */
406 /* raise exceptions. The returned function has the */
407 /* following signature: */
408 /* void (*func) (MonoException *exc); */
410 /*------------------------------------------------------------------*/
413 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
419 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
422 /*========================= End of Function ========================*/
424 /*------------------------------------------------------------------*/
426 /* Name - arch_get_corlib_exception */
428 /* Function - Return a function pointer which can be used to */
429 /* raise corlib exceptions. The return function has */
430 /* the following signature: */
431 /* void (*func) (guint32 token, guint32 offset) */
433 /*------------------------------------------------------------------*/
436 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
442 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, TRUE, FALSE, aot));
445 /*========================= End of Function ========================*/
447 /*------------------------------------------------------------------*/
449 /* Name - mono_arch_find_jit_info */
451 /* Function - See exceptions-amd64.c for docs. */
453 /*------------------------------------------------------------------*/
456 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
457 MonoJitInfo *ji, MonoContext *ctx,
458 MonoContext *new_ctx, MonoLMF **lmf,
459 mgreg_t **save_locations,
460 StackFrameInfo *frame)
462 gpointer ip = (gpointer) MONO_CONTEXT_GET_IP (ctx);
463 MonoS390StackFrame *sframe;
465 memset (frame, 0, sizeof (StackFrameInfo));
473 guint32 unwind_info_len;
477 frame->type = FRAME_TYPE_MANAGED;
479 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
481 address = (char *)ip - (char *)ji->code_start;
483 memcpy(®s, &ctx->uc_mcontext.gregs, sizeof(regs));
484 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
485 (guint8 *) ji->code_start + ji->code_size,
486 ip, NULL, regs, 16, save_locations,
487 MONO_MAX_IREGS, &cfa);
488 memcpy (&new_ctx->uc_mcontext.gregs, ®s, sizeof(regs));
489 MONO_CONTEXT_SET_IP(new_ctx, regs[14] - 2);
490 MONO_CONTEXT_SET_BP(new_ctx, cfa);
495 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
500 frame->method = (*lmf)->method;
504 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
506 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
507 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
508 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
509 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip - 2);
510 *lmf = (*lmf)->previous_lmf;
518 /*========================= End of Function ========================*/
520 /*------------------------------------------------------------------*/
522 /* Name - mono_arch_handle_exception */
524 /* Function - Handle an exception raised by the JIT code. */
526 /* Parameters - ctx - Saved processor state */
527 /* obj - The exception object */
529 /*------------------------------------------------------------------*/
532 mono_arch_handle_exception (void *uc, gpointer obj)
534 return mono_handle_exception (uc, obj);
537 /*========================= End of Function ========================*/
539 /*------------------------------------------------------------------*/
541 /* Name - mono_arch_ip_from_context */
543 /* Function - Return the instruction pointer from the context. */
545 /* Parameters - sigctx - Saved processor state */
547 /*------------------------------------------------------------------*/
550 mono_arch_ip_from_context (void *sigctx)
552 return ((gpointer) MONO_CONTEXT_GET_IP(((MonoContext *) sigctx)));
556 /*========================= End of Function ========================*/
558 /*------------------------------------------------------------------*/
560 /* Name - mono_arch_get_restore_context */
562 /* Function - Return the address of the routine that will rest- */
563 /* ore the context. */
565 /*------------------------------------------------------------------*/
568 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;
600 ctx = (MonoContext *) uc;
601 code = (guint8 *) ((siginfo_t *)info)->si_addr;
602 /*----------------------------------------------------------*/
603 /* Divide operations are the only ones that will give the */
604 /* divide by zero exception so just check for these ops. */
605 /*----------------------------------------------------------*/
607 case 0x1d : /* Divide Register */
608 regNo = code[1] & 0x0f;
609 if (ctx->uc_mcontext.gregs[regNo] == 0)
612 case 0x5d : /* Divide */
613 regNo = (code[2] & 0xf0 >> 8);
614 idxNo = (code[1] & 0x0f);
615 offset = *((guint16 *) code+2) & 0x0fff;
616 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
618 operand += ctx->uc_mcontext.gregs[idxNo];
622 case 0xb9 : /* DL[GR] or DS[GR] */
623 if ((code[1] == 0x97) || (code[1] == 0x87) ||
624 (code[1] == 0x0d) || (code[1] == 0x1d)) {
625 regNo = (code[3] & 0x0f);
626 if (ctx->uc_mcontext.gregs[regNo] == 0)
630 case 0xe3 : /* DL[G] | DS[G] */
631 if ((code[5] == 0x97) || (code[5] == 0x87) ||
632 (code[5] == 0x0d) || (code[5] == 0x1d)) {
633 regNo = (code[2] & 0xf0 >> 8);
634 idxNo = (code[1] & 0x0f);
635 offset = (code[2] & 0x0f << 8) +
636 code[3] + (code[4] << 12);
637 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
639 operand += ctx->uc_mcontext.gregs[idxNo];
647 ctx->uc_mcontext.psw.addr = (guint64)code;
651 /*========================= End of Function ========================*/