* Removed all Id tags.
[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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32 #include <signal.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/sparc64/codegen.h"
37 #include "vm/jit/sparc64/md-abi.h"
38
39 #include "vm/signallocal.h"
40 #include "vm/stringlocal.h"
41 #include "vm/jit/asmpart.h"
42 #include "vm/jit/stacktrace.h"
43
44
45 typedef struct sigcontext sigcontext;
46
47 ptrint md_get_reg_from_context(sigcontext *ctx, u4 rindex)
48 {
49         ptrint val;     
50         s8     *window;
51         
52         
53         /* return 0 for REG_ZERO */
54         
55         if (rindex == 0)
56                 return 0;
57                 
58         
59         if (rindex <= 15) {
60                 
61                 /* register is in global or out range, available in context */
62                 
63                 val = ctx->sigc_regs.u_regs[rindex];
64         }
65         else {
66                 assert(rindex <= 31);
67                 
68                 /* register is local or in, need to fetch from regsave area on stack */
69                 
70                 window = ctx->sigc_regs.u_regs[REG_SP] + BIAS;
71                 val = window[rindex - 16];
72         }
73         
74         return val;
75 }
76         
77         
78
79 /* md_signal_handler_sigsegv ***************************************************
80
81    NullPointerException signal handler for hardware null pointer
82    check.
83
84 *******************************************************************************/
85
86 void md_signal_handler_sigsegv(int sig, siginfo_t *info , void *_p)
87 {
88         stackframeinfo     sfi;
89         /*
90         ucontext_t  *_uc;
91         mcontext_t  *_mc;
92         */
93         sigcontext *ctx;
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         intptr_t    addr;
104         int         type;
105         void       *p;
106
107         ctx = (sigcontext *) info;
108
109
110         pv  = (u1 *) md_get_reg_from_context(ctx, REG_PV_CALLEE);
111         sp  = (u1 *) md_get_reg_from_context(ctx, REG_SP);
112         ra  = (u1 *) md_get_reg_from_context(ctx, REG_RA_CALLEE);  /* this is correct for leafs */
113         xpc = (u1 *) ctx->sigc_regs.tpc;
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(ctx, 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(ctx, 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         ctx->sigc_regs.u_regs[REG_ITMP2_XPTR] = (intptr_t) p;
157         ctx->sigc_regs.u_regs[REG_ITMP3_XPC]  = (intptr_t) xpc;
158         ctx->sigc_regs.tpc                    = (intptr_t) asm_handle_exception;
159         ctx->sigc_regs.tnpc                   = (intptr_t) asm_handle_exception + 4;
160 }
161
162
163 /* md_icacheflush **************************************************************
164
165    Calls the system's function to flush the instruction cache.
166
167 *******************************************************************************/
168
169 void md_icacheflush(u1 *addr, s4 nbytes)
170 {
171         u1* end;
172         
173         end = addr + nbytes;
174         
175         /* zero the least significant 3 bits to align on a 64-bit boundary */
176         addr = (u1 *) (((ptrint) addr) & -8l);
177         
178         while (addr < end) {
179                 __asm__ (
180                         "flush %0"
181                         :
182                         : "r"(addr)
183                         );
184                 addr += 8;
185         }
186 }
187
188 #if defined(ENABLE_THREADS)
189 /* md_critical_section_restart ************************************************
190  
191    Search the critical sections tree for a matching section and set
192    the NPC to the restart point, if necessary.
193
194    Reads PC and modifies NPC.
195
196 ******************************************************************************/
197
198 void md_critical_section_restart(ucontext_t *_uc)
199 {
200         /* mcontext_t *_mc; */
201         sigcontext *ctx;
202         u1         *pc;
203         u1         *npc;
204
205         printf("ignoring md_critical_section_restart\n");
206         return;
207
208         /* again, we are getting sigcontext instead of ucontext */
209         ctx = (sigcontext *) _uc;
210         
211         pc = (u1 *) ctx->sigc_regs.tpc;
212
213         npc = critical_find_restart_point(pc);
214
215         if (npc != NULL) {
216                 log_println("md_critical_section_restart: pc=%p, npc=%p", pc, npc);
217                 ctx->sigc_regs.tnpc = (ptrint) npc;
218         }
219
220 }
221 #endif
222         
223 /*
224  * These are local overrides for various environment variables in Emacs.
225  * Please do not remove this and leave it at the end of the file, where
226  * Emacs will automagically detect them.
227  * ---------------------------------------------------------------------
228  * Local variables:
229  * mode: c
230  * indent-tabs-mode: t
231  * c-basic-offset: 4
232  * tab-width: 4
233  * End:
234  * vim:noexpandtab:sw=4:ts=4:
235  */