5f50a10b2df345512a31c81eea9d6732833ba824
[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 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 7363 2007-02-15 14:57:04Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <signal.h>
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 #include "vm/jit/asmpart.h"
44 #include "vm/jit/stacktrace.h"
45
46
47 typedef struct sigcontext sigcontext;
48
49 ptrint md_get_reg_from_context(sigcontext *ctx, 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                 
65                 val = ctx->sigc_regs.u_regs[rindex];
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 = ctx->sigc_regs.u_regs[REG_SP] + BIAS;
73                 val = window[rindex - 16];
74         }
75         
76         return val;
77 }
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 *info , void *_p)
89 {
90         /*
91         ucontext_t  *_uc;
92         mcontext_t  *_mc;
93         */
94         sigcontext *ctx;
95         u1          *pv;
96         u1          *sp;
97         u1          *ra;
98         u1          *xpc;
99         u4          mcode;
100         s4                 d;
101         s4                 s1;
102         s4                 disp;
103         ptrint             val;
104         ptrint             addr;
105         s4                 type;
106         java_objectheader *e;
107
108         ctx = (sigcontext *) info;
109
110
111         pv  = (u1 *) md_get_reg_from_context(ctx, REG_PV_CALLEE);
112         sp  = (u1 *) md_get_reg_from_context(ctx, REG_SP);
113         ra  = (u1 *) md_get_reg_from_context(ctx, REG_RA_CALLEE);  /* this is correct for leafs */
114         xpc = (u1 *) ctx->sigc_regs.tpc;
115
116         /* get exception-throwing instruction */        
117
118         mcode = *((u4 *) xpc);
119
120         d    = M_OP3_GET_RD(mcode);
121         s1   = M_OP3_GET_RS(mcode);
122         disp = M_OP3_GET_IMM(mcode);
123
124         /* flush register windows? */
125         
126         val   = md_get_reg_from_context(ctx, d);
127
128         /* check for special-load */
129
130         if (s1 == REG_ZERO) {
131                 /* we use the exception type as load displacement */
132
133                 type = disp;
134         }
135         else {
136                 /* This is a normal NPE: addr must be NULL and the NPE-type
137                    define is 0. */
138
139                 addr  = md_get_reg_from_context(ctx, s1);
140                 type = (s4) addr;
141         }
142
143
144         e = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
145
146         /* set registers */
147
148         ctx->sigc_regs.u_regs[REG_ITMP2_XPTR] = (ptrint) e;
149         ctx->sigc_regs.u_regs[REG_ITMP3_XPC]  = (ptrint) xpc;
150         ctx->sigc_regs.tpc                    = (ptrint) asm_handle_exception;
151         ctx->sigc_regs.tnpc                   = (ptrint) asm_handle_exception + 4;
152 }
153
154
155 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
156 void thread_restartcriticalsection(ucontext_t *_uc)
157 {
158         mcontext_t *_mc;
159         void       *critical;
160
161         _mc = &_uc->uc_mcontext;
162
163         critical = thread_checkcritical((void *) _mc->sc_pc);
164
165         if (critical)
166                 _mc->sc_pc = (ptrint) critical;
167 }
168 #endif
169
170
171 /* md_icacheflush **************************************************************
172
173    Calls the system's function to flush the instruction cache.
174
175 *******************************************************************************/
176
177 void md_icacheflush(u1 *addr, s4 nbytes)
178 {
179         u1* end;
180         
181         end = addr + nbytes;
182         
183         /* zero the least significant 3 bits to align on a 64-bit boundary */
184         addr = (u1 *) (((ptrint) addr) & -8l);
185         
186         while (addr < end) {
187                 __asm__ (
188                         "flush %0"
189                         :
190                         : "r"(addr)
191                         );
192                 addr += 8;
193         }
194 }
195
196
197
198
199 /*
200  * These are local overrides for various environment variables in Emacs.
201  * Please do not remove this and leave it at the end of the file, where
202  * Emacs will automagically detect them.
203  * ---------------------------------------------------------------------
204  * Local variables:
205  * mode: c
206  * indent-tabs-mode: t
207  * c-basic-offset: 4
208  * tab-width: 4
209  * End:
210  * vim:noexpandtab:sw=4:ts=4:
211  */