* src/vm/jit/trap.c: Moved to C++
[cacao.git] / src / vm / jit / sparc64 / linux / md-os.c
1 /* src/vm/jit/sparc64/linux/md-os.c - machine dependent SPARC Linux functions
2
3    Copyright (C) 1996-2005, 2006, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30 #include <signal.h>
31
32 #include "vm/types.h"
33
34 #include "vm/jit/sparc64/codegen.h"
35 #include "vm/jit/sparc64/md-abi.h"
36
37 #include "vm/signallocal.hpp"
38
39 #include "vm/jit/asmpart.h"
40 #include "vm/jit/stacktrace.hpp"
41 #include "vm/jit/trap.hpp"
42
43
44 typedef struct sigcontext sigcontext;
45
46 ptrint md_get_reg_from_context(sigcontext *ctx, u4 rindex)
47 {
48         ptrint val;     
49         s8     *window;
50         
51         
52         /* return 0 for REG_ZERO */
53         
54         if (rindex == 0)
55                 return 0;
56                 
57         
58         if (rindex <= 15) {
59                 
60                 /* register is in global or out range, available in context */
61                 
62                 val = ctx->sigc_regs.u_regs[rindex];
63         }
64         else {
65                 assert(rindex <= 31);
66                 
67                 /* register is local or in, need to fetch from regsave area on stack */
68                 
69                 window = ctx->sigc_regs.u_regs[REG_SP] + BIAS;
70                 val = window[rindex - 16];
71         }
72         
73         return val;
74 }
75         
76         
77
78 /* md_signal_handler_sigsegv ***************************************************
79
80    NullPointerException signal handler for hardware null pointer
81    check.
82
83 *******************************************************************************/
84
85 void md_signal_handler_sigsegv(int sig, siginfo_t *info , void *_p)
86 {
87         /*
88         ucontext_t  *_uc;
89         mcontext_t  *_mc;
90         */
91         sigcontext *ctx;
92         u1          *pv;
93         u1          *sp;
94         u1          *ra;
95         u1          *xpc;
96         u4          mcode;
97         int         d;
98         int         s1;
99         int16_t     disp;
100         intptr_t    val;
101         intptr_t    addr;
102         int         type;
103         void       *p;
104
105         ctx = (sigcontext *) info;
106
107
108         pv  = (u1 *) md_get_reg_from_context(ctx, REG_PV_CALLEE);
109         sp  = (u1 *) md_get_reg_from_context(ctx, REG_SP);
110         ra  = (u1 *) md_get_reg_from_context(ctx, REG_RA_CALLEE);  /* this is correct for leafs */
111         xpc = (u1 *) ctx->sigc_regs.tpc;
112
113         /* get exception-throwing instruction */        
114
115         mcode = *((u4 *) xpc);
116
117         d    = M_OP3_GET_RD(mcode);
118         s1   = M_OP3_GET_RS(mcode);
119         disp = M_OP3_GET_IMM(mcode);
120
121         /* flush register windows? */
122         
123         val  = md_get_reg_from_context(ctx, d);
124
125         /* check for special-load */
126
127         if (s1 == REG_ZERO) {
128                 /* we use the exception type as load displacement */
129
130                 type = disp;
131         }
132         else {
133                 /* This is a normal NPE: addr must be NULL and the NPE-type
134                    define is 0. */
135
136                 addr = md_get_reg_from_context(ctx, s1);
137                 type = (int) addr;
138         }
139
140         /* Handle the trap. */
141
142         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
143
144         /* set registers */
145
146         ctx->sigc_regs.u_regs[REG_ITMP2_XPTR] = (uintptr_t) p;
147         ctx->sigc_regs.u_regs[REG_ITMP3_XPC]  = (uintptr_t) xpc;
148         ctx->sigc_regs.tpc                    = (uintptr_t) asm_handle_exception;
149         ctx->sigc_regs.tnpc                   = (uintptr_t) asm_handle_exception + 4;
150 }
151
152
153 /* md_icacheflush **************************************************************
154
155    Calls the system's function to flush the instruction cache.
156
157 *******************************************************************************/
158
159 void md_icacheflush(u1 *addr, s4 nbytes)
160 {
161         u1* end;
162         
163         end = addr + nbytes;
164         
165         /* zero the least significant 3 bits to align on a 64-bit boundary */
166         addr = (u1 *) (((ptrint) addr) & -8l);
167         
168         while (addr < end) {
169                 __asm__ (
170                         "flush %0"
171                         :
172                         : "r"(addr)
173                         );
174                 addr += 8;
175         }
176 }
177
178         
179 /*
180  * These are local overrides for various environment variables in Emacs.
181  * Please do not remove this and leave it at the end of the file, where
182  * Emacs will automagically detect them.
183  * ---------------------------------------------------------------------
184  * Local variables:
185  * mode: c
186  * indent-tabs-mode: t
187  * c-basic-offset: 4
188  * tab-width: 4
189  * End:
190  * vim:noexpandtab:sw=4:ts=4:
191  */