7e47210ec18db390d8770a0943b20247c0714288
[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 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: md-os.c 4357 2006-01-22 23:33:38Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34 #include <ucontext.h>
35
36 /* work around name clash */
37 #undef REG_SP
38
39 #include "vm/types.h"
40
41 #include "vm/jit/sparc64/codegen.h"
42 #include "vm/jit/sparc64/md-abi.h"
43
44 #include "vm/exceptions.h"
45 #include "vm/signallocal.h"
46 #include "vm/stringlocal.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.h"
49
50
51 ptrint md_get_reg_from_context(mcontext_t *_mc, u4 rindex)
52 {
53         ptrint val;     
54         s8     *window;
55         
56         
57         /* return 0 for REG_ZERO */
58         
59         if (rindex == 0)
60                 return 0;
61                 
62         
63         if (rindex <= 15) {
64                 
65                 /* register is in global or out range, available in context */
66                 val = _mc->gregs[rindex + 3];
67                 
68         }
69         else {
70                 assert(rindex <= 31);
71                 
72                 /* register is local or in, need to fetch from regsave area on stack */
73
74                 window = (s8 *) (_mc->gregs[REG_O6] + BIAS);
75                 val = window[rindex - 16];
76
77         }
78         
79         return val;
80 }
81
82
83 /* md_signal_handler_sigsegv ***************************************************
84
85    NullPointerException signal handler for hardware null pointer
86    check.
87
88 *******************************************************************************/
89
90 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
91 {
92         stackframeinfo  sfi;
93         ucontext_t     *_uc;
94         mcontext_t     *_mc;
95         ptrint          addr;
96         u1             *pv;
97         u1             *sp;
98         u1             *ra;
99         u1             *xpc;
100         u4              mcode;
101         int             d;
102         int             s1;
103         int16_t         disp;
104         intptr_t        val;
105         int             type;
106         void           *p;
107
108         _uc = (ucontext_t *) _p;
109         _mc = &_uc->uc_mcontext;
110
111         pv  = (u1 *) md_get_reg_from_context(_mc, REG_PV_CALLEE);
112         sp  = (u1 *) _mc->gregs[REG_O6];
113         ra  = (u1 *) md_get_reg_from_context(_mc, REG_RA_CALLEE);
114         xpc = (u1 *) _mc->gregs[REG_PC];
115
116
117         /* get exception-throwing instruction */        
118
119         mcode = *((u4 *) xpc);
120
121         d    = M_OP3_GET_RD(mcode);
122         s1   = M_OP3_GET_RS(mcode);
123         disp = M_OP3_GET_IMM(mcode);
124
125         /* flush register windows? */
126         
127         val  = md_get_reg_from_context(_mc, d);
128
129         /* check for special-load */
130
131         if (s1 == REG_ZERO) {
132                 /* we use the exception type as load displacement */
133
134                 type = disp;
135         }
136         else {
137                 /* This is a normal NPE: addr must be NULL and the NPE-type
138                    define is 0. */
139
140                 addr  = md_get_reg_from_context(_mc, s1);
141                 type = (int) addr;
142         }
143
144         /* create stackframeinfo */
145
146         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
147
148         /* Handle the type. */
149
150         p = signal_handle(xpc, type, val);
151
152         /* remove stackframeinfo */
153
154         stacktrace_remove_stackframeinfo(&sfi);
155
156         /* set registers */
157
158         _mc->gregs[REG_G2]  = (intptr_t) p;                     /* REG_ITMP2_XPTR */
159         _mc->gregs[REG_G3]  = (intptr_t) xpc;                    /* REG_ITMP3_XPC */
160         _mc->gregs[REG_PC]  = (intptr_t) asm_handle_exception;
161         _mc->gregs[REG_nPC] = (intptr_t) asm_handle_exception + 4;      
162 }
163
164
165 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
166 void thread_restartcriticalsection(ucontext_t *_uc)
167 {
168         mcontext_t *_mc;
169         void       *critical;
170
171         _mc = &_uc->uc_mcontext;
172
173         critical = thread_checkcritical((void *) _mc->sc_pc);
174
175         if (critical)
176                 _mc->sc_pc = (ptrint) critical;
177 }
178 #endif
179
180
181 /* md_icacheflush **************************************************************
182
183    Calls the system's function to flush the instruction cache.
184
185 *******************************************************************************/
186
187 void md_icacheflush(u1 *addr, s4 nbytes)
188 {
189         u1* end;
190         
191         end = addr + nbytes;
192         
193         /* zero the least significant 3 bits to align on a 64-bit boundary */
194         addr = (u1 *) (((ptrint) addr) & -8l);
195         
196         while (addr < end) {
197                 __asm__ (
198                         "flush %0"
199                         :
200                         : "r"(addr)
201                         );
202                 addr += 8;
203         }
204 }
205
206
207
208
209 /*
210  * These are local overrides for various environment variables in Emacs.
211  * Please do not remove this and leave it at the end of the file, where
212  * Emacs will automagically detect them.
213  * ---------------------------------------------------------------------
214  * Local variables:
215  * mode: c
216  * indent-tabs-mode: t
217  * c-basic-offset: 4
218  * tab-width: 4
219  * End:
220  */