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;
264 mono_ex->trace_ips = NULL;
267 // mono_arch_handle_exception (&ctx, exc, FALSE);
268 mono_handle_exception (&ctx, exc);
269 mono_restore_context(&ctx);
271 g_assert_not_reached ();
274 /*========================= End of Function ========================*/
276 /*------------------------------------------------------------------*/
278 /* Name - get_throw_exception_generic */
280 /* Function - Return a function pointer which can be used to */
281 /* raise exceptions. The returned function has the */
282 /* following signature: */
283 /* void (*func) (MonoException *exc); or, */
284 /* void (*func) (char *exc_name); */
286 /*------------------------------------------------------------------*/
289 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info,
290 int corlib, gboolean rethrow, gboolean aot)
292 guint8 *code, *start;
293 int alloc_size, pos, i;
294 MonoJumpInfo *ji = NULL;
295 GSList *unwind_ops = NULL;
297 code = start = mono_global_codeman_reserve(size);
299 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
300 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
301 s390_lgr (code, s390_r14, STK_BASE);
302 s390_aghi (code, STK_BASE, -alloc_size);
303 s390_stg (code, s390_r14, 0, STK_BASE, 0);
304 s390_lgr (code, s390_r3, s390_r2);
306 s390_basr (code, s390_r13, 0);
308 s390_llong(code, mono_defaults.exception_class->image);
309 s390_llong(code, mono_exception_from_token);
310 s390_lg (code, s390_r2, 0, s390_r13, 4);
311 s390_lg (code, s390_r1, 0, s390_r13, 12);
312 s390_basr (code, s390_r14, s390_r1);
315 /*------------------------------------------------------*/
316 /* save the general registers on the stack */
317 /*------------------------------------------------------*/
318 s390_stmg (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
320 s390_lgr (code, s390_r1, STK_BASE);
321 s390_aghi (code, s390_r1, alloc_size);
322 /*------------------------------------------------------*/
323 /* save the return address in the parameter register */
324 /*------------------------------------------------------*/
325 s390_lg (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
327 /*------------------------------------------------------*/
328 /* save the floating point registers */
329 /*------------------------------------------------------*/
330 pos = S390_THROWSTACK_FLTREGS;
331 for (i = 0; i < 16; ++i) {
332 s390_std (code, i, 0, STK_BASE, pos);
333 pos += sizeof (gdouble);
335 /*------------------------------------------------------*/
336 /* save the access registers */
337 /*------------------------------------------------------*/
338 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
340 /*------------------------------------------------------*/
341 /* call throw_exception (tkn, ip, sp, gr, fr, ar, re) */
342 /* - r2 already contains *exc */
343 /*------------------------------------------------------*/
344 s390_lgr (code, s390_r4, s390_r1); /* caller sp */
346 /*------------------------------------------------------*/
347 /* pointer to the saved int regs */
348 /*------------------------------------------------------*/
349 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
350 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
351 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
352 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
353 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM+4);
354 s390_lghi (code, s390_r7, rethrow);
355 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
356 s390_basr (code, s390_r13, 0);
358 s390_llong(code, throw_exception);
359 s390_lg (code, s390_r1, 0, s390_r13, 4);
360 s390_basr (code, s390_r14, s390_r1);
361 /* we should never reach this breakpoint */
363 g_assert ((code - start) < size);
365 mono_arch_flush_icache (start, code - start);
366 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
369 *info = mono_tramp_info_create (corlib ? "throw_corlib_exception"
370 : (rethrow ? "rethrow_exception"
371 : "throw_exception"),
372 start, code - start, ji, unwind_ops);
377 /*========================= End of Function ========================*/
379 /*------------------------------------------------------------------*/
381 /* Name - arch_get_throw_exception */
383 /* Function - Return a function pointer which can be used to */
384 /* raise exceptions. The returned function has the */
385 /* following signature: */
386 /* void (*func) (MonoException *exc); */
388 /*------------------------------------------------------------------*/
391 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
398 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
401 /*========================= End of Function ========================*/
403 /*------------------------------------------------------------------*/
405 /* Name - arch_get_rethrow_exception */
407 /* Function - Return a function pointer which can be used to */
408 /* raise exceptions. The returned function has the */
409 /* following signature: */
410 /* void (*func) (MonoException *exc); */
412 /*------------------------------------------------------------------*/
415 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
421 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
424 /*========================= End of Function ========================*/
426 /*------------------------------------------------------------------*/
428 /* Name - arch_get_corlib_exception */
430 /* Function - Return a function pointer which can be used to */
431 /* raise corlib exceptions. The return function has */
432 /* the following signature: */
433 /* void (*func) (guint32 token, guint32 offset) */
435 /*------------------------------------------------------------------*/
438 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
444 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, TRUE, FALSE, aot));
447 /*========================= End of Function ========================*/
449 /*------------------------------------------------------------------*/
451 /* Name - mono_arch_find_jit_info */
453 /* Function - See exceptions-amd64.c for docs. */
455 /*------------------------------------------------------------------*/
458 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
459 MonoJitInfo *ji, MonoContext *ctx,
460 MonoContext *new_ctx, MonoLMF **lmf,
461 mgreg_t **save_locations,
462 StackFrameInfo *frame)
464 gpointer ip = (gpointer) MONO_CONTEXT_GET_IP (ctx);
465 MonoS390StackFrame *sframe;
467 memset (frame, 0, sizeof (StackFrameInfo));
475 guint32 unwind_info_len;
479 frame->type = FRAME_TYPE_MANAGED;
481 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
483 address = (char *)ip - (char *)ji->code_start;
485 memcpy(®s, &ctx->uc_mcontext.gregs, sizeof(regs));
486 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
487 (guint8 *) ji->code_start + ji->code_size,
488 ip, NULL, regs, 16, save_locations,
489 MONO_MAX_IREGS, &cfa);
490 memcpy (&new_ctx->uc_mcontext.gregs, ®s, sizeof(regs));
491 MONO_CONTEXT_SET_IP(new_ctx, regs[14] - 2);
492 MONO_CONTEXT_SET_BP(new_ctx, cfa);
497 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
502 frame->method = (*lmf)->method;
506 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
508 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
509 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
510 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
511 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip - 2);
512 *lmf = (*lmf)->previous_lmf;
520 /*========================= End of Function ========================*/
522 /*------------------------------------------------------------------*/
524 /* Name - mono_arch_handle_exception */
526 /* Function - Handle an exception raised by the JIT code. */
528 /* Parameters - ctx - Saved processor state */
529 /* obj - The exception object */
531 /*------------------------------------------------------------------*/
534 mono_arch_handle_exception (void *uc, gpointer obj)
536 return mono_handle_exception (uc, obj);
539 /*========================= End of Function ========================*/
541 /*------------------------------------------------------------------*/
543 /* Name - mono_arch_ip_from_context */
545 /* Function - Return the instruction pointer from the context. */
547 /* Parameters - sigctx - Saved processor state */
549 /*------------------------------------------------------------------*/
552 mono_arch_ip_from_context (void *sigctx)
554 return ((gpointer) MONO_CONTEXT_GET_IP(((MonoContext *) sigctx)));
558 /*========================= End of Function ========================*/
560 /*------------------------------------------------------------------*/
562 /* Name - mono_arch_get_restore_context */
564 /* Function - Return the address of the routine that will rest- */
565 /* ore the context. */
567 /*------------------------------------------------------------------*/
570 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
579 /*========================= End of Function ========================*/
581 /*------------------------------------------------------------------*/
583 /* Name - mono_arch_is_int_overflow */
585 /* Function - Inspect the code that raised the SIGFPE signal */
586 /* to see if the DivideByZero or Arithmetic exception*/
587 /* should be raised. */
589 /*------------------------------------------------------------------*/
592 mono_arch_is_int_overflow (void *uc, void *info)
597 gboolean arithExc = TRUE;
602 ctx = (MonoContext *) uc;
603 code = (guint8 *) ((siginfo_t *)info)->si_addr;
604 /*----------------------------------------------------------*/
605 /* Divide operations are the only ones that will give the */
606 /* divide by zero exception so just check for these ops. */
607 /*----------------------------------------------------------*/
609 case 0x1d : /* Divide Register */
610 regNo = code[1] & 0x0f;
611 if (ctx->uc_mcontext.gregs[regNo] == 0)
614 case 0x5d : /* Divide */
615 regNo = (code[2] & 0xf0 >> 8);
616 idxNo = (code[1] & 0x0f);
617 offset = *((guint16 *) code+2) & 0x0fff;
618 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
620 operand += ctx->uc_mcontext.gregs[idxNo];
624 case 0xb9 : /* DL[GR] or DS[GR] */
625 if ((code[1] == 0x97) || (code[1] == 0x87) ||
626 (code[1] == 0x0d) || (code[1] == 0x1d)) {
627 regNo = (code[3] & 0x0f);
628 if (ctx->uc_mcontext.gregs[regNo] == 0)
632 case 0xe3 : /* DL[G] | DS[G] */
633 if ((code[5] == 0x97) || (code[5] == 0x87) ||
634 (code[5] == 0x0d) || (code[5] == 0x1d)) {
635 regNo = (code[2] & 0xf0 >> 8);
636 idxNo = (code[1] & 0x0f);
637 offset = (code[2] & 0x0f << 8) +
638 code[3] + (code[4] << 12);
639 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
641 operand += ctx->uc_mcontext.gregs[idxNo];
649 ctx->uc_mcontext.psw.addr = (guint64)code;
653 /*========================= End of Function ========================*/