Merge branch 'BigIntegerParse'
[mono.git] / mono / mini / exceptions-s390x.c
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 /*------------------------------------------------------------------*/
18
19 /*------------------------------------------------------------------*/
20 /*                 D e f i n e s                                    */
21 /*------------------------------------------------------------------*/
22
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)))
27
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)))
35
36 #define S390_REG_SAVE_R13               (S390_REG_SAVE_OFFSET+(7*sizeof(gulong)))
37
38 #define SZ_THROW        384
39
40 #define setup_context(ctx)
41
42 /*========================= End of Defines =========================*/
43
44 /*------------------------------------------------------------------*/
45 /*                 I n c l u d e s                                  */
46 /*------------------------------------------------------------------*/
47
48 #include <config.h>
49 #include <glib.h>
50 #include <signal.h>
51 #include <string.h>
52 #include <ucontext.h>
53
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>
61
62 #include "mini.h"
63 #include "mini-s390x.h"
64
65 /*========================= End of Includes ========================*/
66
67 /*------------------------------------------------------------------*/
68 /*                   P r o t o t y p e s                            */
69 /*------------------------------------------------------------------*/
70
71 gboolean mono_arch_handle_exception (void     *ctx,
72                                      gpointer obj);
73
74 /*========================= End of Prototypes ======================*/
75
76 /*------------------------------------------------------------------*/
77 /*                 G l o b a l   V a r i a b l e s                  */
78 /*------------------------------------------------------------------*/
79
80 typedef enum {
81         by_none,
82         by_token
83 } throwType;
84
85 /*====================== End of Global Variables ===================*/
86
87 /*------------------------------------------------------------------*/
88 /*                                                                  */
89 /* Name         - mono_arch_get_call_filter                         */
90 /*                                                                  */
91 /* Function     - Return a pointer to a method which calls an       */
92 /*                exception filter. We also use this function to    */
93 /*                call finally handlers (we pass NULL as @exc       */
94 /*                object in this case).                             */
95 /*                                                                  */
96 /*------------------------------------------------------------------*/
97
98 gpointer
99 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
100 {
101         static guint8 *start;
102         static int inited = 0;
103         guint8 *code;
104         int alloc_size, pos, i;
105         GSList *unwind_ops = NULL;
106         MonoJumpInfo *ji = NULL;
107
108         g_assert (!aot);
109
110         if (inited)
111                 return start;
112
113         inited = 1;
114         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
115         code = start = mono_global_codeman_reserve (512);
116
117         s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
118         s390_lgr  (code, s390_r14, STK_BASE);
119         alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
120         s390_aghi (code, STK_BASE, -alloc_size);
121         s390_stg  (code, s390_r14, 0, STK_BASE, 0);
122
123         /*------------------------------------------------------*/
124         /* save general registers on stack                      */
125         /*------------------------------------------------------*/
126         s390_stmg (code, s390_r0, STK_BASE, STK_BASE, S390_CALLFILTER_INTREGS);
127
128         /*------------------------------------------------------*/
129         /* save floating point registers on stack               */
130         /*------------------------------------------------------*/
131         pos = S390_CALLFILTER_FLTREGS;
132         for (i = 0; i < 16; ++i) {
133                 s390_std (code, i, 0, STK_BASE, pos);
134                 pos += sizeof (gdouble);
135         }
136
137         /*------------------------------------------------------*/
138         /* save access registers on stack                       */
139         /*------------------------------------------------------*/
140         s390_stam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
141
142         /*------------------------------------------------------*/
143         /* Get A(Context)                                       */
144         /*------------------------------------------------------*/
145         s390_lgr  (code, s390_r13, s390_r2);
146
147         /*------------------------------------------------------*/
148         /* Get A(Handler Entry Point)                           */
149         /*------------------------------------------------------*/
150         s390_lgr  (code, s390_r0, s390_r3);
151
152         /*------------------------------------------------------*/
153         /* Set parameter register with Exception                */
154         /*------------------------------------------------------*/
155         s390_lgr  (code, s390_r2, s390_r4);
156
157         /*------------------------------------------------------*/
158         /* Load all registers with values from the context      */
159         /*------------------------------------------------------*/
160         s390_lmg  (code, s390_r3, s390_r12, s390_r13, 
161                    G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[3]));
162         pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
163         for (i = 0; i < 16; ++i) {
164                 s390_ld  (code, i, 0, s390_r13, pos);
165                 pos += sizeof(gdouble);
166         }
167
168 #if 0
169         /*------------------------------------------------------*/
170         /* We need to preserve current SP before calling filter */
171         /* with SP from the context                             */
172         /*------------------------------------------------------*/
173         s390_lgr  (code, s390_r14, STK_BASE);
174         s390_lg   (code, STK_BASE, 0, s390_r13,
175                    G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[15]));
176         s390_lgr  (code, s390_r13, s390_r14);
177 #endif
178
179         /*------------------------------------------------------*/
180         /* Go call filter                                       */
181         /*------------------------------------------------------*/
182         s390_lgr  (code, s390_r1, s390_r0);
183         s390_basr (code, s390_r14, s390_r1);
184
185         /*------------------------------------------------------*/
186         /* Save return value                                    */
187         /*------------------------------------------------------*/
188         s390_lgr  (code, s390_r14, s390_r2);
189
190 #if 0
191         /*------------------------------------------------------*/
192         /* Reload our stack register with value saved in context*/
193         /*------------------------------------------------------*/
194         s390_lgr  (code, STK_BASE, s390_r13);
195 #endif
196
197         /*------------------------------------------------------*/
198         /* Restore all the regs from the stack                  */
199         /*------------------------------------------------------*/
200         s390_lmg  (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
201         pos = S390_CALLFILTER_FLTREGS;
202         for (i = 0; i < 16; ++i) {
203                 s390_ld (code, i, 0, STK_BASE, pos);
204                 pos += sizeof (gdouble);
205         }
206
207         s390_lgr  (code, s390_r2, s390_r14);
208         s390_lam  (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
209         s390_aghi (code, s390_r15, alloc_size);
210         s390_lmg  (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
211         s390_br   (code, s390_r14);
212
213         g_assert ((code - start) < SZ_THROW); 
214
215         if (info)
216                 *info = mono_tramp_info_create ("call_filter",
217                                                 start, code - start, ji,
218                                                 unwind_ops);
219
220         return start;
221 }
222
223 /*========================= End of Function ========================*/
224
225 /*------------------------------------------------------------------*/
226 /*                                                                  */
227 /* Name         - throw_exception.                                  */
228 /*                                                                  */
229 /* Function     - Raise an exception based on the parameters passed.*/
230 /*                                                                  */
231 /*------------------------------------------------------------------*/
232
233 static void
234 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp, 
235                  gulong *int_regs, gdouble *fp_regs, gint32 *acc_regs, 
236                  guint fpc, gboolean rethrow)
237 {
238         MonoContext ctx;
239         int iReg;
240
241         memset(&ctx, 0, sizeof(ctx));
242
243         setup_context(&ctx);
244
245         /* adjust eip so that it point into the call instruction */
246         ip -= 2;
247
248         for (iReg = 0; iReg < 16; iReg++) {
249                 ctx.uc_mcontext.gregs[iReg]         = int_regs[iReg];
250                 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
251                 ctx.uc_mcontext.aregs[iReg]         = acc_regs[iReg];
252         }
253
254         ctx.uc_mcontext.fpregs.fpc = fpc;
255
256         MONO_CONTEXT_SET_BP (&ctx, sp);
257         MONO_CONTEXT_SET_IP (&ctx, ip);
258         
259         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
260                 MonoException *mono_ex = (MonoException*)exc;
261                 if (!rethrow)
262                         mono_ex->stack_trace = NULL;
263         }
264 //      mono_arch_handle_exception (&ctx, exc, FALSE);
265         mono_handle_exception (&ctx, exc);
266         mono_restore_context(&ctx);
267
268         g_assert_not_reached ();
269 }
270
271 /*========================= End of Function ========================*/
272
273 /*------------------------------------------------------------------*/
274 /*                                                                  */
275 /* Name         - get_throw_exception_generic                       */
276 /*                                                                  */
277 /* Function     - Return a function pointer which can be used to    */
278 /*                raise exceptions. The returned function has the   */
279 /*                following signature:                              */
280 /*                void (*func) (MonoException *exc); or,            */
281 /*                void (*func) (char *exc_name);                    */
282 /*                                                                  */
283 /*------------------------------------------------------------------*/
284
285 static gpointer 
286 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info, 
287                                 int corlib, gboolean rethrow, gboolean aot)
288 {
289         guint8 *code, *start;
290         int alloc_size, pos, i;
291         MonoJumpInfo *ji = NULL;
292         GSList *unwind_ops = NULL;
293
294         code = start = mono_global_codeman_reserve(size);
295
296         s390_stmg (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
297         alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
298         s390_lgr  (code, s390_r14, STK_BASE);
299         s390_aghi (code, STK_BASE, -alloc_size);
300         s390_stg  (code, s390_r14, 0, STK_BASE, 0);
301         s390_lgr  (code, s390_r3, s390_r2);
302         if (corlib) {
303                 s390_basr (code, s390_r13, 0);
304                 s390_j    (code, 10);
305                 s390_llong(code, mono_defaults.exception_class->image);
306                 s390_llong(code, mono_exception_from_token);
307                 s390_lg   (code, s390_r2, 0, s390_r13, 4);
308                 s390_lg   (code, s390_r1, 0, s390_r13, 12);
309                 s390_basr (code, s390_r14, s390_r1);
310         }
311
312         /*------------------------------------------------------*/
313         /* save the general registers on the stack              */
314         /*------------------------------------------------------*/
315         s390_stmg (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
316
317         s390_lgr  (code, s390_r1, STK_BASE);
318         s390_aghi (code, s390_r1, alloc_size);
319         /*------------------------------------------------------*/
320         /* save the return address in the parameter register    */
321         /*------------------------------------------------------*/
322         s390_lg   (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
323
324         /*------------------------------------------------------*/
325         /* save the floating point registers                    */
326         /*------------------------------------------------------*/
327         pos = S390_THROWSTACK_FLTREGS;
328         for (i = 0; i < 16; ++i) {
329                 s390_std (code, i, 0, STK_BASE, pos);
330                 pos += sizeof (gdouble);
331         }
332         /*------------------------------------------------------*/
333         /* save the access registers                            */
334         /*------------------------------------------------------*/
335         s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
336
337         /*------------------------------------------------------*/
338         /* call throw_exception (tkn, ip, sp, gr, fr, ar, re)   */
339         /* - r2 already contains *exc                           */
340         /*------------------------------------------------------*/
341         s390_lgr  (code, s390_r4, s390_r1);        /* caller sp */
342
343         /*------------------------------------------------------*/
344         /* pointer to the saved int regs                        */
345         /*------------------------------------------------------*/
346         s390_la   (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
347         s390_la   (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
348         s390_la   (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
349         s390_stg  (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
350         s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM+4);
351         s390_lghi (code, s390_r7, rethrow);
352         s390_stg  (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
353         s390_basr (code, s390_r13, 0);
354         s390_j    (code, 6);
355         s390_llong(code, throw_exception);
356         s390_lg   (code, s390_r1, 0, s390_r13, 4);
357         s390_basr (code, s390_r14, s390_r1);
358         /* we should never reach this breakpoint */
359         s390_break (code);
360         g_assert ((code - start) < size);
361
362         if (info)
363                 *info = mono_tramp_info_create (corlib ? "throw_corlib_exception" 
364                                                                        : (rethrow ? "rethrow_exception" 
365                                                                        : "throw_exception"), 
366                                                 start, code - start, ji, unwind_ops);
367
368         return start;
369 }
370
371 /*========================= End of Function ========================*/
372
373 /*------------------------------------------------------------------*/
374 /*                                                                  */
375 /* Name         - arch_get_throw_exception                          */
376 /*                                                                  */
377 /* Function     - Return a function pointer which can be used to    */
378 /*                raise exceptions. The returned function has the   */
379 /*                following signature:                              */
380 /*                void (*func) (MonoException *exc);                */
381 /*                                                                  */
382 /*------------------------------------------------------------------*/
383
384 gpointer
385 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
386 {
387
388         g_assert (!aot);
389         if (info)
390                 *info = NULL;
391
392         return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
393 }
394
395 /*========================= End of Function ========================*/
396
397 /*------------------------------------------------------------------*/
398 /*                                                                  */
399 /* Name         - arch_get_rethrow_exception                        */
400 /*                                                                  */
401 /* Function     - Return a function pointer which can be used to    */
402 /*                raise exceptions. The returned function has the   */
403 /*                following signature:                              */
404 /*                void (*func) (MonoException *exc);                */
405 /*                                                                  */
406 /*------------------------------------------------------------------*/
407
408 gpointer 
409 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
410 {
411         g_assert (!aot);
412         if (info)
413                 *info = NULL;
414
415         return (mono_arch_get_throw_exception_generic (SZ_THROW, info, FALSE, FALSE, aot));
416 }
417
418 /*========================= End of Function ========================*/
419
420 /*------------------------------------------------------------------*/
421 /*                                                                  */
422 /* Name         - arch_get_corlib_exception                         */
423 /*                                                                  */
424 /* Function     - Return a function pointer which can be used to    */
425 /*                raise corlib exceptions. The return function has  */
426 /*                the following signature:                          */
427 /*                void (*func) (guint32 token, guint32 offset)      */
428 /*                                                                  */
429 /*------------------------------------------------------------------*/
430
431 gpointer
432 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
433 {
434         g_assert (!aot);
435         if (info)
436                 *info = NULL;
437
438         return (mono_arch_get_throw_exception_generic (SZ_THROW, info, TRUE, FALSE, aot));
439 }       
440
441 /*========================= End of Function ========================*/
442
443 /*------------------------------------------------------------------*/
444 /*                                                                  */
445 /* Name         - mono_arch_find_jit_info                           */
446 /*                                                                  */
447 /* Function     - See exceptions-amd64.c for docs.                  */
448 /*                                                                  */
449 /*------------------------------------------------------------------*/
450
451 gboolean
452 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
453                          MonoJitInfo *ji, MonoContext *ctx, 
454                          MonoContext *new_ctx, MonoLMF **lmf,
455                          mgreg_t **save_locations,
456                          StackFrameInfo *frame)
457 {
458         gpointer ip = (gpointer) MONO_CONTEXT_GET_IP (ctx);
459         MonoS390StackFrame *sframe;
460
461         memset (frame, 0, sizeof (StackFrameInfo));
462         frame->ji = ji;
463
464         *new_ctx = *ctx;
465
466         if (ji != NULL) {
467                 gint64 address;
468                 guint8 *cfa;
469                 guint32 unwind_info_len;
470                 guint8 *unwind_info;
471                 mgreg_t regs[16];
472
473                 frame->type = FRAME_TYPE_MANAGED;
474
475                 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
476
477                 if (*lmf && ((*lmf) != jit_tls->first_lmf) && 
478                     (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
479                         /* remove any unused lmf */
480                         *lmf = (*lmf)->previous_lmf;
481                 }
482
483                 address = (char *)ip - (char *)ji->code_start;
484
485                 memcpy(&regs, &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, regs, 16, save_locations, 
489                                 MONO_MAX_IREGS, &cfa);
490                 memcpy (&new_ctx->uc_mcontext.gregs, &regs, sizeof(regs));
491                 MONO_CONTEXT_SET_IP(new_ctx, regs[14] - 2);
492                 MONO_CONTEXT_SET_BP(new_ctx, cfa);
493         
494                 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
495                         /* remove any unused lmf */
496                         *lmf = (*lmf)->previous_lmf;
497                 }
498                 return TRUE;
499         } else if (*lmf) {
500
501                 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
502                 if (!ji) {
503                         if (!(*lmf)->method)
504                                 return FALSE;
505                 
506                         frame->method = (*lmf)->method;
507                 }
508
509                 frame->ji = ji;
510                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
511
512                 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
513                 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
514                 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
515                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip - 2);
516                 *lmf = (*lmf)->previous_lmf;
517
518                 return TRUE;
519         }
520
521         return FALSE;
522 }
523
524 /*========================= End of Function ========================*/
525
526 /*------------------------------------------------------------------*/
527 /*                                                                  */
528 /* Name         - mono_arch_handle_exception                        */
529 /*                                                                  */
530 /* Function     - Handle an exception raised by the JIT code.       */
531 /*                                                                  */
532 /* Parameters   - ctx       - Saved processor state                 */
533 /*                obj       - The exception object                  */
534 /*                                                                  */
535 /*------------------------------------------------------------------*/
536
537 gboolean
538 mono_arch_handle_exception (void *uc, gpointer obj)
539 {
540         return mono_handle_exception (uc, obj);
541 }
542
543 /*========================= End of Function ========================*/
544
545 /*------------------------------------------------------------------*/
546 /*                                                                  */
547 /* Name         - mono_arch_sigctx_to_monoctx.                      */
548 /*                                                                  */
549 /* Function     - Called from the signal handler to convert signal  */
550 /*                context to MonoContext.                           */
551 /*                                                                  */
552 /*------------------------------------------------------------------*/
553
554 void
555 mono_arch_sigctx_to_monoctx (void *ctx, MonoContext *mctx)
556 {
557         mono_sigctx_to_monoctx(ctx, mctx);
558 }
559
560 /*========================= End of Function ========================*/
561
562 /*------------------------------------------------------------------*/
563 /*                                                                  */
564 /* Name         - mono_arch_monoctx_to_sigctx.                      */
565 /*                                                                  */
566 /* Function     - Convert MonoContext structure to signal context.  */
567 /*                                                                  */
568 /*------------------------------------------------------------------*/
569
570 void
571 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
572 {
573         mono_monoctx_to_sigctx(mctx, ctx);
574 }
575
576 /*========================= End of Function ========================*/
577
578 /*------------------------------------------------------------------*/
579 /*                                                                  */
580 /* Name         - mono_arch_ip_from_context                         */
581 /*                                                                  */
582 /* Function     - Return the instruction pointer from the context.  */
583 /*                                                                  */
584 /* Parameters   - sigctx    - Saved processor state                 */
585 /*                                                                  */
586 /*------------------------------------------------------------------*/
587
588 gpointer
589 mono_arch_ip_from_context (void *sigctx)
590 {
591         return ((gpointer) MONO_CONTEXT_GET_IP(((MonoContext *) sigctx)));
592 }
593
594
595 /*========================= End of Function ========================*/
596
597 /*------------------------------------------------------------------*/
598 /*                                                                  */
599 /* Name         - mono_arch_get_restore_context                    */
600 /*                                                                  */
601 /* Function     - Return the address of the routine that will rest- */
602 /*                ore the context.                                  */
603 /*                                                                  */
604 /*------------------------------------------------------------------*/
605
606 gpointer
607 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
608 {
609         g_assert (!aot);
610         if (info)
611                 *info = NULL;
612
613         return setcontext;
614 }
615
616 /*========================= End of Function ========================*/
617
618 /*------------------------------------------------------------------*/
619 /*                                                                  */
620 /* Name         - mono_arch_is_int_overflow                         */
621 /*                                                                  */
622 /* Function     - Inspect the code that raised the SIGFPE signal    */
623 /*                to see if the DivideByZero or Arithmetic exception*/
624 /*                should be raised.                                 */
625 /*                                                                  */
626 /*------------------------------------------------------------------*/
627
628 gboolean
629 mono_arch_is_int_overflow (void *uc, void *info)
630 {
631         MonoContext *ctx;
632         guint8      *code;
633         guint64     *operand;
634         gboolean    arithExc = TRUE;
635         gint        regNo,
636                     idxNo,
637                     offset;
638
639         ctx  = (MonoContext *) uc;
640         code =  (guint8 *) ((siginfo_t *)info)->si_addr;
641         /*----------------------------------------------------------*/
642         /* Divide operations are the only ones that will give the   */
643         /* divide by zero exception so just check for these ops.    */
644         /*----------------------------------------------------------*/
645         switch (code[0]) {
646                 case 0x1d :             /* Divide Register          */
647                         regNo = code[1] & 0x0f; 
648                         if (ctx->uc_mcontext.gregs[regNo] == 0)
649                                 arithExc = FALSE;
650                 break;
651                 case 0x5d :             /* Divide                   */
652                         regNo   = (code[2] & 0xf0 >> 8);        
653                         idxNo   = (code[1] & 0x0f);
654                         offset  = *((guint16 *) code+2) & 0x0fff;
655                         operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
656                         if (idxNo != 0)
657                                 operand += ctx->uc_mcontext.gregs[idxNo];
658                         if (*operand == 0)
659                                 arithExc = FALSE; 
660                 break;
661                 case 0xb9 :             /* DL[GR] or DS[GR]         */
662                         if ((code[1] == 0x97) || (code[1] == 0x87) ||
663                             (code[1] == 0x0d) || (code[1] == 0x1d)) {
664                                 regNo = (code[3] & 0x0f);
665                                 if (ctx->uc_mcontext.gregs[regNo] == 0)
666                                         arithExc = FALSE;
667                         }
668                 break;
669                 case 0xe3 :             /* DL[G] | DS[G]            */
670                         if ((code[5] == 0x97) || (code[5] == 0x87) ||
671                             (code[5] == 0x0d) || (code[5] == 0x1d)) {
672                                 regNo   = (code[2] & 0xf0 >> 8);        
673                                 idxNo   = (code[1] & 0x0f);
674                                 offset  = (code[2] & 0x0f << 8) + 
675                                           code[3] + (code[4] << 12);
676                                 operand = (guint64*)(ctx->uc_mcontext.gregs[regNo] + offset);
677                                 if (idxNo != 0)
678                                         operand += ctx->uc_mcontext.gregs[idxNo];
679                                 if (*operand == 0)
680                                         arithExc = FALSE; 
681                         }
682                 break;
683                 default:
684                         arithExc = TRUE;
685         }
686         ctx->uc_mcontext.psw.addr = (guint64)code;
687         return (arithExc);
688 }
689
690 /*========================= End of Function ========================*/