2008-06-24 Robert Schuster <robertschuster@fsfe.org>
[cacao.git] / src / vm / jit / sparc64 / solaris / md-os.c
1 /* src/vm/jit/sparc64/solaris/md-os.c - machine dependent SPARC Solaris functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 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 <ucontext.h>
31
32 /* work around name clash */
33 #undef REG_SP
34
35 #include "vm/types.h"
36
37 #include "vm/jit/sparc64/codegen.h"
38 #include "vm/jit/sparc64/md-abi.h"
39
40 #include "vm/exceptions.h"
41 #include "vm/signallocal.h"
42 #include "vm/stringlocal.h"
43
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/stacktrace.h"
46 #include "vm/jit/trap.h"
47
48
49 ptrint md_get_reg_from_context(mcontext_t *_mc, u4 rindex)
50 {
51         ptrint val;     
52         s8     *window;
53         
54         
55         /* return 0 for REG_ZERO */
56         
57         if (rindex == 0)
58                 return 0;
59                 
60         
61         if (rindex <= 15) {
62                 
63                 /* register is in global or out range, available in context */
64                 val = _mc->gregs[rindex + 3];
65                 
66         }
67         else {
68                 assert(rindex <= 31);
69                 
70                 /* register is local or in, need to fetch from regsave area on stack */
71
72                 window = (s8 *) (_mc->gregs[REG_O6] + BIAS);
73                 val = window[rindex - 16];
74
75         }
76         
77         return val;
78 }
79
80
81 /* md_signal_handler_sigsegv ***************************************************
82
83    NullPointerException signal handler for hardware null pointer
84    check.
85
86 *******************************************************************************/
87
88 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
89 {
90         ucontext_t     *_uc;
91         mcontext_t     *_mc;
92         ptrint          addr;
93         u1             *pv;
94         u1             *sp;
95         u1             *ra;
96         u1             *xpc;
97         u4              mcode;
98         int             d;
99         int             s1;
100         int16_t         disp;
101         intptr_t        val;
102         int             type;
103         void           *p;
104
105         _uc = (ucontext_t *) _p;
106         _mc = &_uc->uc_mcontext;
107
108         pv  = (u1 *) md_get_reg_from_context(_mc, REG_PV_CALLEE);
109         sp  = (u1 *) _mc->gregs[REG_O6];
110         ra  = (u1 *) md_get_reg_from_context(_mc, REG_RA_CALLEE);
111         xpc = (u1 *) _mc->gregs[REG_PC];
112
113
114         /* get exception-throwing instruction */        
115
116         mcode = *((u4 *) xpc);
117
118         d    = M_OP3_GET_RD(mcode);
119         s1   = M_OP3_GET_RS(mcode);
120         disp = M_OP3_GET_IMM(mcode);
121
122         /* flush register windows? */
123         
124         val  = md_get_reg_from_context(_mc, d);
125
126         /* check for special-load */
127
128         if (s1 == REG_ZERO) {
129                 /* we use the exception type as load displacement */
130
131                 type = disp;
132         }
133         else {
134                 /* This is a normal NPE: addr must be NULL and the NPE-type
135                    define is 0. */
136
137                 addr = md_get_reg_from_context(_mc, s1);
138                 type = (int) addr;
139         }
140
141         /* Handle the trap. */
142
143         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
144
145         /* Set registers. */
146
147         _mc->gregs[REG_G2]  = (uintptr_t) p;                    /* REG_ITMP2_XPTR */
148         _mc->gregs[REG_G3]  = (uintptr_t) xpc;                   /* REG_ITMP3_XPC */
149         _mc->gregs[REG_PC]  = (uintptr_t) asm_handle_exception;
150         _mc->gregs[REG_nPC] = (uintptr_t) asm_handle_exception + 4;     
151 }
152
153
154 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
155 void thread_restartcriticalsection(ucontext_t *_uc)
156 {
157         mcontext_t *_mc;
158         void       *critical;
159
160         _mc = &_uc->uc_mcontext;
161
162         critical = thread_checkcritical((void *) _mc->sc_pc);
163
164         if (critical)
165                 _mc->sc_pc = (ptrint) critical;
166 }
167 #endif
168
169
170 /* md_icacheflush **************************************************************
171
172    Calls the system's function to flush the instruction cache.
173
174 *******************************************************************************/
175
176 void md_icacheflush(u1 *addr, s4 nbytes)
177 {
178         u1* end;
179         
180         end = addr + nbytes;
181         
182         /* zero the least significant 3 bits to align on a 64-bit boundary */
183         addr = (u1 *) (((ptrint) addr) & -8l);
184         
185         while (addr < end) {
186                 __asm__ (
187                         "flush %0"
188                         :
189                         : "r"(addr)
190                         );
191                 addr += 8;
192         }
193 }
194
195
196
197
198 /*
199  * These are local overrides for various environment variables in Emacs.
200  * Please do not remove this and leave it at the end of the file, where
201  * Emacs will automagically detect them.
202  * ---------------------------------------------------------------------
203  * Local variables:
204  * mode: c
205  * indent-tabs-mode: t
206  * c-basic-offset: 4
207  * tab-width: 4
208  * End:
209  */