* Removed all Id tags.
[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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32 #include <ucontext.h>
33
34 /* work around name clash */
35 #undef REG_SP
36
37 #include "vm/types.h"
38
39 #include "vm/jit/sparc64/codegen.h"
40 #include "vm/jit/sparc64/md-abi.h"
41
42 #include "vm/exceptions.h"
43 #include "vm/signallocal.h"
44 #include "vm/stringlocal.h"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/stacktrace.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         stackframeinfo  sfi;
91         ucontext_t     *_uc;
92         mcontext_t     *_mc;
93         ptrint          addr;
94         u1             *pv;
95         u1             *sp;
96         u1             *ra;
97         u1             *xpc;
98         u4              mcode;
99         int             d;
100         int             s1;
101         int16_t         disp;
102         intptr_t        val;
103         int             type;
104         void           *p;
105
106         _uc = (ucontext_t *) _p;
107         _mc = &_uc->uc_mcontext;
108
109         pv  = (u1 *) md_get_reg_from_context(_mc, REG_PV_CALLEE);
110         sp  = (u1 *) _mc->gregs[REG_O6];
111         ra  = (u1 *) md_get_reg_from_context(_mc, REG_RA_CALLEE);
112         xpc = (u1 *) _mc->gregs[REG_PC];
113
114
115         /* get exception-throwing instruction */        
116
117         mcode = *((u4 *) xpc);
118
119         d    = M_OP3_GET_RD(mcode);
120         s1   = M_OP3_GET_RS(mcode);
121         disp = M_OP3_GET_IMM(mcode);
122
123         /* flush register windows? */
124         
125         val  = md_get_reg_from_context(_mc, d);
126
127         /* check for special-load */
128
129         if (s1 == REG_ZERO) {
130                 /* we use the exception type as load displacement */
131
132                 type = disp;
133         }
134         else {
135                 /* This is a normal NPE: addr must be NULL and the NPE-type
136                    define is 0. */
137
138                 addr  = md_get_reg_from_context(_mc, s1);
139                 type = (int) addr;
140         }
141
142         /* create stackframeinfo */
143
144         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
145
146         /* Handle the type. */
147
148         p = signal_handle(xpc, type, val);
149
150         /* remove stackframeinfo */
151
152         stacktrace_remove_stackframeinfo(&sfi);
153
154         /* set registers */
155
156         _mc->gregs[REG_G2]  = (intptr_t) p;                     /* REG_ITMP2_XPTR */
157         _mc->gregs[REG_G3]  = (intptr_t) xpc;                    /* REG_ITMP3_XPC */
158         _mc->gregs[REG_PC]  = (intptr_t) asm_handle_exception;
159         _mc->gregs[REG_nPC] = (intptr_t) asm_handle_exception + 4;      
160 }
161
162
163 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
164 void thread_restartcriticalsection(ucontext_t *_uc)
165 {
166         mcontext_t *_mc;
167         void       *critical;
168
169         _mc = &_uc->uc_mcontext;
170
171         critical = thread_checkcritical((void *) _mc->sc_pc);
172
173         if (critical)
174                 _mc->sc_pc = (ptrint) critical;
175 }
176 #endif
177
178
179 /* md_icacheflush **************************************************************
180
181    Calls the system's function to flush the instruction cache.
182
183 *******************************************************************************/
184
185 void md_icacheflush(u1 *addr, s4 nbytes)
186 {
187         u1* end;
188         
189         end = addr + nbytes;
190         
191         /* zero the least significant 3 bits to align on a 64-bit boundary */
192         addr = (u1 *) (((ptrint) addr) & -8l);
193         
194         while (addr < end) {
195                 __asm__ (
196                         "flush %0"
197                         :
198                         : "r"(addr)
199                         );
200                 addr += 8;
201         }
202 }
203
204
205
206
207 /*
208  * These are local overrides for various environment variables in Emacs.
209  * Please do not remove this and leave it at the end of the file, where
210  * Emacs will automagically detect them.
211  * ---------------------------------------------------------------------
212  * Local variables:
213  * mode: c
214  * indent-tabs-mode: t
215  * c-basic-offset: 4
216  * tab-width: 4
217  * End:
218  */