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