Merge from subtype.
[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, 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 <ucontext.h>
31
32 /* work around name clash */
33 #undef REG_SP
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/signallocal.h"
41
42 #include "vm/jit/asmpart.h"
43 #include "vm/jit/stacktrace.hpp"
44 #include "vm/jit/trap.h"
45
46
47 ptrint md_get_reg_from_context(mcontext_t *_mc, 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                 val = _mc->gregs[rindex + 3];
63                 
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 = (s8 *) (_mc->gregs[REG_O6] + BIAS);
71                 val = window[rindex - 16];
72
73         }
74         
75         return val;
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 *siginfo, void *_p)
87 {
88         ucontext_t     *_uc;
89         mcontext_t     *_mc;
90         ptrint          addr;
91         u1             *pv;
92         u1             *sp;
93         u1             *ra;
94         u1             *xpc;
95         u4              mcode;
96         int             d;
97         int             s1;
98         int16_t         disp;
99         intptr_t        val;
100         int             type;
101         void           *p;
102
103         _uc = (ucontext_t *) _p;
104         _mc = &_uc->uc_mcontext;
105
106         pv  = (u1 *) md_get_reg_from_context(_mc, REG_PV_CALLEE);
107         sp  = (u1 *) _mc->gregs[REG_O6];
108         ra  = (u1 *) md_get_reg_from_context(_mc, REG_RA_CALLEE);
109         xpc = (u1 *) _mc->gregs[REG_PC];
110
111
112         /* get exception-throwing instruction */        
113
114         mcode = *((u4 *) xpc);
115
116         d    = M_OP3_GET_RD(mcode);
117         s1   = M_OP3_GET_RS(mcode);
118         disp = M_OP3_GET_IMM(mcode);
119
120         /* flush register windows? */
121         
122         val  = md_get_reg_from_context(_mc, d);
123
124         /* check for special-load */
125
126         if (s1 == REG_ZERO) {
127                 /* we use the exception type as load displacement */
128
129                 type = disp;
130         }
131         else {
132                 /* This is a normal NPE: addr must be NULL and the NPE-type
133                    define is 0. */
134
135                 addr = md_get_reg_from_context(_mc, s1);
136                 type = (int) addr;
137         }
138
139         /* Handle the trap. */
140
141         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
142
143         /* Set registers. */
144
145         _mc->gregs[REG_G2]  = (uintptr_t) p;                    /* REG_ITMP2_XPTR */
146         _mc->gregs[REG_G3]  = (uintptr_t) xpc;                   /* REG_ITMP3_XPC */
147         _mc->gregs[REG_PC]  = (uintptr_t) asm_handle_exception;
148         _mc->gregs[REG_nPC] = (uintptr_t) asm_handle_exception + 4;     
149 }
150
151
152 /* md_icacheflush **************************************************************
153
154    Calls the system's function to flush the instruction cache.
155
156 *******************************************************************************/
157
158 void md_icacheflush(u1 *addr, s4 nbytes)
159 {
160         u1* end;
161         
162         end = addr + nbytes;
163         
164         /* zero the least significant 3 bits to align on a 64-bit boundary */
165         addr = (u1 *) (((ptrint) addr) & -8l);
166         
167         while (addr < end) {
168                 __asm__ (
169                         "flush %0"
170                         :
171                         : "r"(addr)
172                         );
173                 addr += 8;
174         }
175 }
176
177
178
179
180 /*
181  * These are local overrides for various environment variables in Emacs.
182  * Please do not remove this and leave it at the end of the file, where
183  * Emacs will automagically detect them.
184  * ---------------------------------------------------------------------
185  * Local variables:
186  * mode: c
187  * indent-tabs-mode: t
188  * c-basic-offset: 4
189  * tab-width: 4
190  * End:
191  */