Proper x86_64 mnemonics
[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, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30 #include <signal.h>
31
32 #include "vm/types.h"
33
34 #include "vm/jit/sparc64/codegen.h"
35 #include "vm/jit/sparc64/md-abi.h"
36
37 #include "vm/signallocal.h"
38 #include "vm/stringlocal.h"
39
40 #include "vm/jit/asmpart.h"
41 #include "vm/jit/stacktrace.h"
42 #include "vm/jit/trap.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         /*
89         ucontext_t  *_uc;
90         mcontext_t  *_mc;
91         */
92         sigcontext *ctx;
93         u1          *pv;
94         u1          *sp;
95         u1          *ra;
96         u1          *xpc;
97         u4          mcode;
98         int         d;
99         int         s1;
100         int16_t     disp;
101         intptr_t    val;
102         intptr_t    addr;
103         int         type;
104         void       *p;
105
106         ctx = (sigcontext *) info;
107
108
109         pv  = (u1 *) md_get_reg_from_context(ctx, REG_PV_CALLEE);
110         sp  = (u1 *) md_get_reg_from_context(ctx, REG_SP);
111         ra  = (u1 *) md_get_reg_from_context(ctx, REG_RA_CALLEE);  /* this is correct for leafs */
112         xpc = (u1 *) ctx->sigc_regs.tpc;
113
114         /* get exception-throwing instruction */        
115
116         mcode = *((u4 *) xpc);
117
118         d    = M_OP3_GET_RD(mcode);
119         s1   = M_OP3_GET_RS(mcode);
120         disp = M_OP3_GET_IMM(mcode);
121
122         /* flush register windows? */
123         
124         val  = md_get_reg_from_context(ctx, d);
125
126         /* check for special-load */
127
128         if (s1 == REG_ZERO) {
129                 /* we use the exception type as load displacement */
130
131                 type = disp;
132         }
133         else {
134                 /* This is a normal NPE: addr must be NULL and the NPE-type
135                    define is 0. */
136
137                 addr = md_get_reg_from_context(ctx, s1);
138                 type = (int) addr;
139         }
140
141         /* Handle the trap. */
142
143         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
144
145         /* set registers */
146
147         ctx->sigc_regs.u_regs[REG_ITMP2_XPTR] = (uintptr_t) p;
148         ctx->sigc_regs.u_regs[REG_ITMP3_XPC]  = (uintptr_t) xpc;
149         ctx->sigc_regs.tpc                    = (uintptr_t) asm_handle_exception;
150         ctx->sigc_regs.tnpc                   = (uintptr_t) asm_handle_exception + 4;
151 }
152
153
154 /* md_icacheflush **************************************************************
155
156    Calls the system's function to flush the instruction cache.
157
158 *******************************************************************************/
159
160 void md_icacheflush(u1 *addr, s4 nbytes)
161 {
162         u1* end;
163         
164         end = addr + nbytes;
165         
166         /* zero the least significant 3 bits to align on a 64-bit boundary */
167         addr = (u1 *) (((ptrint) addr) & -8l);
168         
169         while (addr < end) {
170                 __asm__ (
171                         "flush %0"
172                         :
173                         : "r"(addr)
174                         );
175                 addr += 8;
176         }
177 }
178
179 #if defined(ENABLE_THREADS)
180 /* md_critical_section_restart ************************************************
181  
182    Search the critical sections tree for a matching section and set
183    the NPC to the restart point, if necessary.
184
185    Reads PC and modifies NPC.
186
187 ******************************************************************************/
188
189 void md_critical_section_restart(ucontext_t *_uc)
190 {
191         /* mcontext_t *_mc; */
192         sigcontext *ctx;
193         u1         *pc;
194         u1         *npc;
195
196         printf("ignoring md_critical_section_restart\n");
197         return;
198
199         /* again, we are getting sigcontext instead of ucontext */
200         ctx = (sigcontext *) _uc;
201         
202         pc = (u1 *) ctx->sigc_regs.tpc;
203
204         npc = critical_find_restart_point(pc);
205
206         if (npc != NULL) {
207                 log_println("md_critical_section_restart: pc=%p, npc=%p", pc, npc);
208                 ctx->sigc_regs.tnpc = (ptrint) npc;
209         }
210
211 }
212 #endif
213         
214 /*
215  * These are local overrides for various environment variables in Emacs.
216  * Please do not remove this and leave it at the end of the file, where
217  * Emacs will automagically detect them.
218  * ---------------------------------------------------------------------
219  * Local variables:
220  * mode: c
221  * indent-tabs-mode: t
222  * c-basic-offset: 4
223  * tab-width: 4
224  * End:
225  * vim:noexpandtab:sw=4:ts=4:
226  */