ffa0b449524781baa07c6082e922cedc27b24b76
[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 <stdint.h>
34 #include <signal.h>
35
36 #include "vm/types.h"
37
38 #include "vm/jit/sparc64/codegen.h"
39 #include "vm/jit/sparc64/md-abi.h"
40
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         stackframeinfo     sfi;
91         /*
92         ucontext_t  *_uc;
93         mcontext_t  *_mc;
94         */
95         sigcontext *ctx;
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         intptr_t    addr;
106         int         type;
107         void       *p;
108
109         ctx = (sigcontext *) info;
110
111
112         pv  = (u1 *) md_get_reg_from_context(ctx, REG_PV_CALLEE);
113         sp  = (u1 *) md_get_reg_from_context(ctx, REG_SP);
114         ra  = (u1 *) md_get_reg_from_context(ctx, REG_RA_CALLEE);  /* this is correct for leafs */
115         xpc = (u1 *) ctx->sigc_regs.tpc;
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(ctx, 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(ctx, 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         ctx->sigc_regs.u_regs[REG_ITMP2_XPTR] = (intptr_t) p;
159         ctx->sigc_regs.u_regs[REG_ITMP3_XPC]  = (intptr_t) xpc;
160         ctx->sigc_regs.tpc                    = (intptr_t) asm_handle_exception;
161         ctx->sigc_regs.tnpc                   = (intptr_t) asm_handle_exception + 4;
162 }
163
164
165 /* md_icacheflush **************************************************************
166
167    Calls the system's function to flush the instruction cache.
168
169 *******************************************************************************/
170
171 void md_icacheflush(u1 *addr, s4 nbytes)
172 {
173         u1* end;
174         
175         end = addr + nbytes;
176         
177         /* zero the least significant 3 bits to align on a 64-bit boundary */
178         addr = (u1 *) (((ptrint) addr) & -8l);
179         
180         while (addr < end) {
181                 __asm__ (
182                         "flush %0"
183                         :
184                         : "r"(addr)
185                         );
186                 addr += 8;
187         }
188 }
189
190 #if defined(ENABLE_THREADS)
191 /* md_critical_section_restart ************************************************
192  
193    Search the critical sections tree for a matching section and set
194    the NPC to the restart point, if necessary.
195
196    Reads PC and modifies NPC.
197
198 ******************************************************************************/
199
200 void md_critical_section_restart(ucontext_t *_uc)
201 {
202         /* mcontext_t *_mc; */
203         sigcontext *ctx;
204         u1         *pc;
205         u1         *npc;
206
207         printf("ignoring md_critical_section_restart\n");
208         return;
209
210         /* again, we are getting sigcontext instead of ucontext */
211         ctx = (sigcontext *) _uc;
212         
213         pc = (u1 *) ctx->sigc_regs.tpc;
214
215         npc = critical_find_restart_point(pc);
216
217         if (npc != NULL) {
218                 log_println("md_critical_section_restart: pc=%p, npc=%p", pc, npc);
219                 ctx->sigc_regs.tnpc = (ptrint) npc;
220         }
221
222 }
223 #endif
224         
225 /*
226  * These are local overrides for various environment variables in Emacs.
227  * Please do not remove this and leave it at the end of the file, where
228  * Emacs will automagically detect them.
229  * ---------------------------------------------------------------------
230  * Local variables:
231  * mode: c
232  * indent-tabs-mode: t
233  * c-basic-offset: 4
234  * tab-width: 4
235  * End:
236  * vim:noexpandtab:sw=4:ts=4:
237  */