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);
214 *info = mono_tramp_info_create ("call_filter",
215 start, code - start, ji,
221 /*========================= End of Function ========================*/
223 /*------------------------------------------------------------------*/
225 /* Name - throw_exception. */
227 /* Function - Raise an exception based on the parameters passed.*/
229 /*------------------------------------------------------------------*/
232 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
233 gulong *int_regs, gdouble *fp_regs, gint32 *acc_regs,
234 guint fpc, gboolean rethrow)
239 memset(&ctx, 0, sizeof(ctx));
243 /* adjust eip so that it point into the call instruction */
246 for (iReg = 0; iReg < 16; iReg++) {
247 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
248 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
249 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
252 ctx.uc_mcontext.fpregs.fpc = fpc;
254 MONO_CONTEXT_SET_BP (&ctx, sp);
255 MONO_CONTEXT_SET_IP (&ctx, ip);
257 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
258 MonoException *mono_ex = (MonoException*)exc;
260 mono_ex->stack_trace = NULL;
262 // mono_arch_handle_exception (&ctx, exc, FALSE);
263 mono_handle_exception (&ctx, exc);
264 mono_restore_context(&ctx);
266 g_assert_not_reached ();
269 /*========================= End of Function ========================*/
271 /*------------------------------------------------------------------*/
273 /* Name - get_throw_exception_generic */
275 /* Function - Return a function pointer which can be used to */
276 /* raise exceptions. The returned function has the */
277 /* following signature: */
278 /* void (*func) (MonoException *exc); or, */
279 /* void (*func) (char *exc_name); */
281 /*------------------------------------------------------------------*/
284 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info,
285 int corlib, gboolean rethrow, gboolean aot)
287 guint8 *code, *start;
288 int alloc_size, pos, i;
289 MonoJumpInfo *ji = NULL;
290 GSList *unwind_ops = NULL;
292 code = start = mono_global_codeman_reserve(size);
294 s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
295 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
296 s390_lgr (code, s390_r14, STK_BASE);
297 s390_aghi (code, STK_BASE, -alloc_size);
298 s390_stg (code, s390_r14, 0, STK_BASE, 0);
299 s390_lgr (code, s390_r3, s390_r2);
301 s390_basr (code, s390_r13, 0);
303 s390_llong(code, mono_defaults.exception_class->image);
304 s390_llong(code, mono_exception_from_token);
305 s390_lg (code, s390_r2, 0, s390_r13, 4);
306 s390_lg (code, s390_r1, 0, s390_r13, 12);
307 s390_basr (code, s390_r14, s390_r1);
310 /*------------------------------------------------------*/
311 /* save the general registers on the stack */
312 /*------------------------------------------------------*/
313 s390_stmg (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
315 s390_lgr (code, s390_r1, STK_BASE);
316 s390_aghi (code, s390_r1, alloc_size);
317 /*------------------------------------------------------*/
318 /* save the return address in the parameter register */
319 /*------------------------------------------------------*/
320 s390_lg (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
322 /*------------------------------------------------------*/
323 /* save the floating point registers */
324 /*------------------------------------------------------*/
325 pos = S390_THROWSTACK_FLTREGS;
326 for (i = 0; i < 16; ++i) {
327 s390_std (code, i, 0, STK_BASE, pos);
328 pos += sizeof (gdouble);
330 /*------------------------------------------------------*/
331 /* save the access registers */
332 /*------------------------------------------------------*/
333 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
335 /*------------------------------------------------------*/
336 /* call throw_exception (tkn, ip, sp, gr, fr, ar, re) */
337 /* - r2 already contains *exc */
338 /*------------------------------------------------------*/
339 s390_lgr (code, s390_r4, s390_r1); /* caller sp */
341 /*------------------------------------------------------*/
342 /* pointer to the saved int regs */
343 /*------------------------------------------------------*/
344 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
345 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
346 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
347 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
348 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM+4);
349 s390_lghi (code, s390_r7, rethrow);
350 s390_stg (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
351 s390_basr (code, s390_r13, 0);
353 s390_llong(code, throw_exception);
354 s390_lg (code, s390_r1, 0, s390_r13, 4);
355 s390_basr (code, s390_r14, s390_r1);
356 /* we should never reach this breakpoint */
358 g_assert ((code - start) < size);
361 *info = mono_tramp_info_create (corlib ? "throw_corlib_exception"
362 : (rethrow ? "rethrow_exception"
363 : "throw_exception"),
364 start, code - start, ji, unwind_ops);
369 /*========================= End of Function ========================*/
371 /*------------------------------------------------------------------*/
373 /* Name - arch_get_throw_exception */
375 /* Function - Return a function pointer which can be used to */
376 /* raise exceptions. The returned function has the */
377 /* following signature: */
378 /* void (*func) (MonoException *exc); */
380 /*------------------------------------------------------------------*/
383 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
390 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
393 /*========================= End of Function ========================*/
395 /*------------------------------------------------------------------*/
397 /* Name - arch_get_rethrow_exception */
399 /* Function - Return a function pointer which can be used to */
400 /* raise exceptions. The returned function has the */
401 /* following signature: */
402 /* void (*func) (MonoException *exc); */
404 /*------------------------------------------------------------------*/
407 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
413 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
416 /*========================= End of Function ========================*/
418 /*------------------------------------------------------------------*/
420 /* Name - arch_get_corlib_exception */
422 /* Function - Return a function pointer which can be used to */
423 /* raise corlib exceptions. The return function has */
424 /* the following signature: */
425 /* void (*func) (guint32 token, guint32 offset) */
427 /*------------------------------------------------------------------*/
430 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
436 return (mono_arch_get_throw_exception_generic (SZ_THROW, info, TRUE, FALSE, aot));
439 /*========================= End of Function ========================*/
441 /*------------------------------------------------------------------*/
443 /* Name - mono_arch_find_jit_info */
445 /* Function - See exceptions-amd64.c for docs. */
447 /*------------------------------------------------------------------*/
450 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
451 MonoJitInfo *ji, MonoContext *ctx,
452 MonoContext *new_ctx, MonoLMF **lmf,
453 mgreg_t **save_locations,
454 StackFrameInfo *frame)
456 gpointer ip = (gpointer) MONO_CONTEXT_GET_IP (ctx);
457 MonoS390StackFrame *sframe;
459 memset (frame, 0, sizeof (StackFrameInfo));
467 guint32 unwind_info_len;
471 frame->type = FRAME_TYPE_MANAGED;
473 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
475 address = (char *)ip - (char *)ji->code_start;
477 memcpy(®s, &ctx->uc_mcontext.gregs, sizeof(regs));
478 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
479 (guint8 *) ji->code_start + ji->code_size,
480 ip, NULL, regs, 16, save_locations,
481 MONO_MAX_IREGS, &cfa);
482 memcpy (&new_ctx->uc_mcontext.gregs, ®s, sizeof(regs));
483 MONO_CONTEXT_SET_IP(new_ctx, regs[14] - 2);
484 MONO_CONTEXT_SET_BP(new_ctx, cfa);
489 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
494 frame->method = (*lmf)->method;
498 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
500 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
501 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
502 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
503 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip - 2);
504 *lmf = (*lmf)->previous_lmf;
512 /*========================= End of Function ========================*/
514 /*------------------------------------------------------------------*/
516 /* Name - mono_arch_handle_exception */
518 /* Function - Handle an exception raised by the JIT code. */
520 /* Parameters - ctx - Saved processor state */
521 /* obj - The exception object */
523 /*------------------------------------------------------------------*/
526 mono_arch_handle_exception (void *uc, gpointer obj)
528 return mono_handle_exception (uc, obj);
531 /*========================= End of Function ========================*/
533 /*------------------------------------------------------------------*/
535 /* Name - mono_arch_ip_from_context */
537 /* Function - Return the instruction pointer from the context. */
539 /* Parameters - sigctx - Saved processor state */
541 /*------------------------------------------------------------------*/
544 mono_arch_ip_from_context (void *sigctx)
546 return ((gpointer) MONO_CONTEXT_GET_IP(((MonoContext *) sigctx)));
550 /*========================= End of Function ========================*/
552 /*------------------------------------------------------------------*/
554 /* Name - mono_arch_get_restore_context */
556 /* Function - Return the address of the routine that will rest- */
557 /* ore the context. */
559 /*------------------------------------------------------------------*/
562 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
571 /*========================= End of Function ========================*/
573 /*------------------------------------------------------------------*/
575 /* Name - mono_arch_is_int_overflow */
577 /* Function - Inspect the code that raised the SIGFPE signal */
578 /* to see if the DivideByZero or Arithmetic exception*/
579 /* should be raised. */
581 /*------------------------------------------------------------------*/
584 mono_arch_is_int_overflow (void *uc, void *info)
589 gboolean arithExc = TRUE;
594 ctx = (MonoContext *) uc;
595 code = (guint8 *) ((siginfo_t *)info)->si_addr;
596 /*----------------------------------------------------------*/
597 /* Divide operations are the only ones that will give the */
598 /* divide by zero exception so just check for these ops. */
599 /*----------------------------------------------------------*/
601 case 0x1d : /* Divide Register */
602 regNo = code[1] & 0x0f;
603 if (ctx->uc_mcontext.gregs[regNo] == 0)
606 case 0x5d : /* Divide */
607 regNo = (code[2] & 0xf0 >> 8);
608 idxNo = (code[1] & 0x0f);
609 offset = *((guint16 *) code+2) & 0x0fff;
610 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
612 operand += ctx->uc_mcontext.gregs[idxNo];
616 case 0xb9 : /* DL[GR] or DS[GR] */
617 if ((code[1] == 0x97) || (code[1] == 0x87) ||
618 (code[1] == 0x0d) || (code[1] == 0x1d)) {
619 regNo = (code[3] & 0x0f);
620 if (ctx->uc_mcontext.gregs[regNo] == 0)
624 case 0xe3 : /* DL[G] | DS[G] */
625 if ((code[5] == 0x97) || (code[5] == 0x87) ||
626 (code[5] == 0x0d) || (code[5] == 0x1d)) {
627 regNo = (code[2] & 0xf0 >> 8);
628 idxNo = (code[1] & 0x0f);
629 offset = (code[2] & 0x0f << 8) +
630 code[3] + (code[4] << 12);
631 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
633 operand += ctx->uc_mcontext.gregs[idxNo];
641 ctx->uc_mcontext.psw.addr = (guint64)code;
645 /*========================= End of Function ========================*/