* TextControl.cs: Make PageUp and PageDown more like the
[mono-project.git] / mono / mini / exceptions-s390.c
blob1aad39a1b33a70247bf2eb456682d3e336cc4cb0
1 /*------------------------------------------------------------------*/
2 /* */
3 /* Name - exceptions-s390.c */
4 /* */
5 /* Function - Exception support for S/390. */
6 /* */
7 /* Name - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
8 /* */
9 /* Date - January, 2004 */
10 /* */
11 /* Derivation - From exceptions-x86 & exceptions-ppc */
12 /* Paolo Molaro (lupus@ximian.com) */
13 /* Dietmar Maurer (dietmar@ximian.com) */
14 /* */
15 /* Copyright - 2001 Ximian, Inc. */
16 /* */
17 /*------------------------------------------------------------------*/
19 /*------------------------------------------------------------------*/
20 /* D e f i n e s */
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(gulong)))
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(gulong)))
36 #define SZ_THROW 384
38 /*========================= End of Defines =========================*/
40 /*------------------------------------------------------------------*/
41 /* I n c l u d e s */
42 /*------------------------------------------------------------------*/
44 #include <config.h>
45 #include <glib.h>
46 #include <signal.h>
47 #include <string.h>
48 #include <ucontext.h>
50 #include <mono/arch/s390/s390-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>
58 #include "mini.h"
59 #include "mini-s390.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,
68 gpointer obj,
69 gboolean test_only);
71 /*========================= End of Prototypes ======================*/
73 /*------------------------------------------------------------------*/
74 /* G l o b a l V a r i a b l e s */
75 /*------------------------------------------------------------------*/
77 /*====================== End of Global Variables ===================*/
79 /*------------------------------------------------------------------*/
80 /* */
81 /* Name - mono_arch_has_unwind_info */
82 /* */
83 /* Function - Tests if a function has a DWARF exception table */
84 /* that is able to restore all caller saved registers*/
85 /* */
86 /*------------------------------------------------------------------*/
88 gboolean
89 mono_arch_has_unwind_info (gconstpointer addr)
91 return FALSE;
94 /*========================= End of Function ========================*/
96 /*------------------------------------------------------------------*/
97 /* */
98 /* Name - mono_arch_get_call_filter */
99 /* */
100 /* Function - Return a pointer to a method which calls an */
101 /* exception filter. We also use this function to */
102 /* call finally handlers (we pass NULL as @exc */
103 /* object in this case). */
104 /* */
105 /*------------------------------------------------------------------*/
107 gpointer
108 mono_arch_get_call_filter (void)
110 static guint8 *start;
111 static int inited = 0;
112 guint8 *code;
113 int alloc_size, pos, i;
115 if (inited)
116 return start;
118 inited = 1;
119 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
120 code = start = mono_global_codeman_reserve (512);
122 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
123 s390_lr (code, s390_r14, STK_BASE);
124 alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
125 s390_ahi (code, STK_BASE, -alloc_size);
126 s390_st (code, s390_r14, 0, STK_BASE, 0);
128 /*------------------------------------------------------*/
129 /* save general registers on stack */
130 /*------------------------------------------------------*/
131 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
133 /*------------------------------------------------------*/
134 /* save floating point registers on stack */
135 /*------------------------------------------------------*/
136 // pos = S390_CALLFILTER_FLTREGS;
137 // for (i = 0; i < 16; ++i) {
138 // s390_std (code, i, 0, STK_BASE, pos);
139 // pos += sizeof (gdouble);
140 // }
142 /*------------------------------------------------------*/
143 /* save access registers on stack */
144 /*------------------------------------------------------*/
145 // s390_stam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
147 /*------------------------------------------------------*/
148 /* Get A(Context) */
149 /*------------------------------------------------------*/
150 s390_lr (code, s390_r13, s390_r2);
152 /*------------------------------------------------------*/
153 /* Get A(Handler Entry Point) */
154 /*------------------------------------------------------*/
155 s390_lr (code, s390_r0, s390_r3);
157 /*------------------------------------------------------*/
158 /* Set parameter register with Exception */
159 /*------------------------------------------------------*/
160 s390_lr (code, s390_r2, s390_r4);
162 /*------------------------------------------------------*/
163 /* Load all registers with values from the context */
164 /*------------------------------------------------------*/
165 s390_lm (code, s390_r3, s390_r12, s390_r13,
166 G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[3]));
167 pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
168 for (i = 0; i < 16; ++i) {
169 s390_ld (code, i, 0, s390_r13, pos);
170 pos += sizeof(gdouble);
173 /*------------------------------------------------------*/
174 /* Point at the copied stack frame and call the filter */
175 /*------------------------------------------------------*/
176 s390_lr (code, s390_r1, s390_r0);
177 s390_basr (code, s390_r14, s390_r1);
179 /*------------------------------------------------------*/
180 /* Save return value */
181 /*------------------------------------------------------*/
182 s390_lr (code, s390_r14, s390_r2);
184 /*------------------------------------------------------*/
185 /* Restore all the regs from the stack */
186 /*------------------------------------------------------*/
187 s390_lm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
188 // pos = S390_CALLFILTER_FLTREGS;
189 // for (i = 0; i < 16; ++i) {
190 // s390_ld (code, i, 0, STK_BASE, pos);
191 // pos += sizeof (gdouble);
192 // }
194 s390_lr (code, s390_r2, s390_r14);
195 // s390_lam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
196 s390_ahi (code, s390_r15, alloc_size);
197 s390_lm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
198 s390_br (code, s390_r14);
200 g_assert ((code - start) < SZ_THROW);
201 return start;
204 /*========================= End of Function ========================*/
206 /*------------------------------------------------------------------*/
207 /* */
208 /* Name - throw_exception. */
209 /* */
210 /* Function - Raise an exception based on the parameters passed.*/
211 /* */
212 /*------------------------------------------------------------------*/
214 static void
215 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
216 gulong *int_regs, gdouble *fp_regs, gulong *acc_regs,
217 guint fpc, gboolean rethrow)
219 MonoContext ctx;
220 int iReg;
222 memset(&ctx, 0, sizeof(ctx));
224 getcontext(&ctx);
226 /* adjust eip so that it point into the call instruction */
227 ip -= 6;
229 for (iReg = 0; iReg < 16; iReg++) {
230 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
231 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
232 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
235 ctx.uc_mcontext.fpregs.fpc = fpc;
237 MONO_CONTEXT_SET_BP (&ctx, sp);
238 MONO_CONTEXT_SET_IP (&ctx, ip);
240 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
241 MonoException *mono_ex = (MonoException*)exc;
242 if (!rethrow)
243 mono_ex->stack_trace = NULL;
245 mono_arch_handle_exception (&ctx, exc, FALSE);
246 setcontext(&ctx);
248 g_assert_not_reached ();
251 /*========================= End of Function ========================*/
253 /*------------------------------------------------------------------*/
254 /* */
255 /* Name - get_throw_exception_generic */
256 /* */
257 /* Function - Return a function pointer which can be used to */
258 /* raise exceptions. The returned function has the */
259 /* following signature: */
260 /* void (*func) (MonoException *exc); or, */
261 /* void (*func) (char *exc_name); */
262 /* */
263 /*------------------------------------------------------------------*/
265 static gpointer
266 get_throw_exception_generic (guint8 *start, int size,
267 int by_name, gboolean rethrow)
269 guint8 *code;
270 int alloc_size, pos, i, offset;
272 code = start;
274 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
275 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
276 s390_lr (code, s390_r14, STK_BASE);
277 s390_ahi (code, STK_BASE, -alloc_size);
278 s390_st (code, s390_r14, 0, STK_BASE, 0);
279 if (by_name) {
280 s390_lr (code, s390_r4, s390_r2);
281 s390_bras (code, s390_r13, 6);
282 s390_word (code, mono_defaults.corlib);
283 s390_word (code, "System");
284 s390_l (code, s390_r2, 0, s390_r13, 0);
285 s390_l (code, s390_r3, 0, s390_r13, 4);
286 offset = (guint32) S390_RELATIVE(mono_exception_from_name, code);
287 s390_brasl(code, s390_r14, offset);
289 /*------------------------------------------------------*/
290 /* save the general registers on the stack */
291 /*------------------------------------------------------*/
292 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
294 s390_lr (code, s390_r1, STK_BASE);
295 s390_ahi (code, s390_r1, alloc_size);
296 /*------------------------------------------------------*/
297 /* save the return address in the parameter register */
298 /*------------------------------------------------------*/
299 s390_l (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
301 /*------------------------------------------------------*/
302 /* save the floating point registers */
303 /*------------------------------------------------------*/
304 pos = S390_THROWSTACK_FLTREGS;
305 for (i = 0; i < 16; ++i) {
306 s390_std (code, i, 0,STK_BASE, pos);
307 pos += sizeof (gdouble);
309 /*------------------------------------------------------*/
310 /* save the access registers */
311 /*------------------------------------------------------*/
312 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
314 /*------------------------------------------------------*/
315 /* call throw_exception (exc, ip, sp, gr, fr, ar) */
316 /* exc is already in place in r2 */
317 /*------------------------------------------------------*/
318 s390_lr (code, s390_r4, s390_r1); /* caller sp */
319 /*------------------------------------------------------*/
320 /* pointer to the saved int regs */
321 /*------------------------------------------------------*/
322 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
323 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
324 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
325 s390_st (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
326 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM);
327 s390_lhi (code, s390_r7, rethrow);
328 s390_st (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
329 offset = (guint32) S390_RELATIVE(throw_exception, code);
330 s390_brasl(code, s390_r14, offset);
331 /* we should never reach this breakpoint */
332 s390_break (code);
333 g_assert ((code - start) < size);
334 return start;
337 /*========================= End of Function ========================*/
339 /*------------------------------------------------------------------*/
340 /* */
341 /* Name - arch_get_throw_exception */
342 /* */
343 /* Function - Return a function pointer which can be used to */
344 /* raise exceptions. The returned function has the */
345 /* following signature: */
346 /* void (*func) (MonoException *exc); */
347 /* */
348 /*------------------------------------------------------------------*/
350 gpointer
351 mono_arch_get_throw_exception (void)
353 static guint8 *start;
354 static int inited = 0;
356 if (inited)
357 return start;
358 start = mono_global_codeman_reserve (SZ_THROW);
359 get_throw_exception_generic (start, SZ_THROW, FALSE, FALSE);
360 inited = 1;
361 return start;
364 /*========================= End of Function ========================*/
366 /*------------------------------------------------------------------*/
367 /* */
368 /* Name - arch_get_rethrow_exception */
369 /* */
370 /* Function - Return a function pointer which can be used to */
371 /* raise exceptions. The returned function has the */
372 /* following signature: */
373 /* void (*func) (MonoException *exc); */
374 /* */
375 /*------------------------------------------------------------------*/
377 gpointer
378 mono_arch_get_rethrow_exception (void)
380 static guint8 *start;
381 static int inited = 0;
383 if (inited)
384 return start;
385 start = mono_global_codeman_reserve (SZ_THROW);
386 get_throw_exception_generic (start, SZ_THROW, FALSE, TRUE);
387 inited = 1;
388 return start;
391 /*========================= End of Function ========================*/
393 /*------------------------------------------------------------------*/
394 /* */
395 /* Name - arch_get_throw_exception_by_name */
396 /* */
397 /* Function - Return a function pointer which can be used to */
398 /* raise corlib exceptions. The return function has */
399 /* the following signature: */
400 /* void (*func) (char *exc_name); */
401 /* */
402 /*------------------------------------------------------------------*/
404 gpointer
405 mono_arch_get_throw_exception_by_name (void)
407 static guint8 *start;
408 static int inited = 0;
410 if (inited)
411 return start;
412 start = mono_global_codeman_reserve (SZ_THROW);
413 get_throw_exception_generic (start, SZ_THROW, TRUE, FALSE);
414 inited = 1;
415 return start;
418 /*========================= End of Function ========================*/
420 /*------------------------------------------------------------------*/
421 /* */
422 /* Name - mono_arch_find_jit_info */
423 /* */
424 /* Function - This function is used to gather informatoin from */
425 /* @ctx. It returns the MonoJitInfo of the corres- */
426 /* ponding function, unwinds one stack frame and */
427 /* stores the resulting context into @new_ctx. It */
428 /* also stores a string describing the stack location*/
429 /* into @trace (if not NULL), and modifies the @lmf */
430 /* if necessary. @native_offset returns the IP off- */
431 /* set from the start of the function or -1 if that */
432 /* informatoin is not available. */
433 /* */
434 /*------------------------------------------------------------------*/
436 MonoJitInfo *
437 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
438 MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
439 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
440 int *native_offset, gboolean *managed)
442 MonoJitInfo *ji;
443 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
444 MonoS390StackFrame *sframe;
446 if (prev_ji &&
447 (ip >= prev_ji->code_start &&
448 ((guint8 *) ip <= ((guint8 *) prev_ji->code_start) + prev_ji->code_size)))
449 ji = prev_ji;
450 else
451 ji = mono_jit_info_table_find (domain, ip);
453 if (trace)
454 *trace = NULL;
456 if (native_offset)
457 *native_offset = -1;
459 if (managed)
460 *managed = FALSE;
462 if (ji != NULL) {
463 gint32 address;
465 *new_ctx = *ctx;
467 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
468 /* remove any unused lmf */
469 *lmf = (*lmf)->previous_lmf;
472 address = (char *)ip - (char *)ji->code_start;
474 if (native_offset)
475 *native_offset = address;
477 if (managed)
478 if (!ji->method->wrapper_type)
479 *managed = TRUE;
481 sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_SP (ctx);
482 MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
483 sframe = (MonoS390StackFrame *) sframe->prev;
484 MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
485 memcpy (&new_ctx->uc_mcontext.gregs[6], sframe->regs, (8*sizeof(gint32)));
486 return ji;
487 } else if (*lmf) {
489 *new_ctx = *ctx;
491 if (!(*lmf)->method)
492 return (gpointer)-1;
494 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
495 } else {
496 memset (res, 0, sizeof (MonoJitInfo));
497 res->method = (*lmf)->method;
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));
503 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
504 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
505 *lmf = (*lmf)->previous_lmf;
507 return ji ? ji : res;
510 return NULL;
513 /*========================= End of Function ========================*/
515 /*------------------------------------------------------------------*/
516 /* */
517 /* Name - mono_arch_handle_exception */
518 /* */
519 /* Function - Handle an exception raised by the JIT code. */
520 /* */
521 /* Parameters - ctx - Saved processor state */
522 /* obj - The exception object */
523 /* test_only - Only test if the exception is caught, */
524 /* but don't call handlers */
525 /* */
526 /*------------------------------------------------------------------*/
528 gboolean
529 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
531 return mono_handle_exception (uc, obj, mono_arch_ip_from_context(uc), test_only);
534 /*========================= End of Function ========================*/
536 /*------------------------------------------------------------------*/
537 /* */
538 /* Name - mono_arch_ip_from_context */
539 /* */
540 /* Function - Return the instruction pointer from the context. */
541 /* */
542 /* Parameters - sigctx - Saved processor state */
543 /* */
544 /*------------------------------------------------------------------*/
546 gpointer
547 mono_arch_ip_from_context (void *sigctx)
549 return context_get_ip (sigctx);
553 /*========================= End of Function ========================*/
555 /*------------------------------------------------------------------*/
556 /* */
557 /* Name - mono_arch_get_restore_context */
558 /* */
559 /* Function - Return the address of the routine that will rest- */
560 /* ore the context. */
561 /* */
562 /*------------------------------------------------------------------*/
564 gpointer
565 mono_arch_get_restore_context ()
567 return setcontext;
571 /*========================= End of Function ========================*/
573 /*------------------------------------------------------------------*/
574 /* */
575 /* Name - mono_arch_is_int_overflow */
576 /* */
577 /* Function - Inspect the code that raised the SIGFPE signal */
578 /* to see if the DivideByZero or Arithmetic exception*/
579 /* should be raised. */
580 /* */
581 /*------------------------------------------------------------------*/
583 gboolean
584 mono_arch_is_int_overflow (void *uc, void *info)
586 MonoContext *ctx;
587 guint8 *code;
588 guint32 *operand;
589 gboolean arithExc = TRUE;
590 gint regNo,
591 offset;
593 ctx = (MonoContext *) uc;
594 code = (guint8 *) ((siginfo_t *)info)->si_addr;
595 /*----------------------------------------------------------*/
596 /* Divide operations are the only ones that will give the */
597 /* divide by zero exception so just check for these ops. */
598 /*----------------------------------------------------------*/
599 switch (code[0]) {
600 case 0x1d : /* Divide Register */
601 regNo = code[1] & 0x0f;
602 if (ctx->uc_mcontext.gregs[regNo] == 0)
603 arithExc = FALSE;
604 break;
605 case 0x5d : /* Divide */
606 regNo = (code[2] & 0xf0 >> 8);
607 offset = *((guint16 *) code+2) & 0x0fff;
608 operand = (guint32*)(ctx->uc_mcontext.gregs[regNo] + offset);
609 if (*operand == 0)
610 arithExc = FALSE;
611 break;
612 case 0xb9 : /* Divide logical Register? */
613 if (code[1] == 0x97) {
614 regNo = (code[2] & 0xf0 >> 8);
615 if (ctx->uc_mcontext.gregs[regNo] == 0)
616 arithExc = FALSE;
618 break;
619 case 0xe3 : /* Divide logical? */
620 if (code[1] == 0x97) {
621 regNo = (code[2] & 0xf0 >> 8);
622 offset = *((guint32 *) code+1) & 0x000fffff;
623 operand = (guint32*)(ctx->uc_mcontext.gregs[regNo] + offset);
624 if (*operand == 0)
625 arithExc = FALSE;
627 break;
628 default:
629 arithExc = TRUE;
631 ctx->uc_mcontext.psw.addr = (guint32)code;
632 return (arithExc);
635 /*========================= End of Function ========================*/