* Merged with tip.
[cacao.git] / src / vm / jit / mips / linux / md-os.c
1 /* src/vm/jit/mips/linux/md-os.c - machine dependent MIPS Linux 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 <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
30 #include <signal.h>
31 #include <stdint.h>
32 #include <ucontext.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/mips/codegen.h"
37 #include "vm/jit/mips/md.h"
38 #include "vm/jit/mips/md-abi.h"
39
40 #include "mm/gc-common.h"
41 #include "mm/memory.h"
42
43 #include "vm/exceptions.h"
44 #include "vm/signallocal.h"
45
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/stacktrace.h"
48
49
50 /* md_init *********************************************************************
51
52    Do some machine dependent initialization.
53
54 *******************************************************************************/
55
56 void md_init(void)
57 {
58         /* The Boehm GC initialization blocks the SIGSEGV signal. So we do
59            a dummy allocation here to ensure that the GC is
60            initialized. */
61
62 #if defined(ENABLE_GC_BOEHM)
63         (void) GCNEW(int);
64 #endif
65
66 #if 0
67         /* Turn off flush-to-zero */
68
69         {
70                 union fpc_csr n;
71                 n.fc_word = get_fpc_csr();
72                 n.fc_struct.flush = 0;
73                 set_fpc_csr(n.fc_word);
74         }
75 #endif
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         greg_t         *_gregs;
91         u1             *pv;
92         u1             *sp;
93         u1             *ra;
94         u1             *xpc;
95         unsigned int    cause;
96         u4              mcode;
97         int             d;
98         int             s1;
99         int16_t         disp;
100         intptr_t        val;
101         intptr_t        addr;
102         int             type;
103         void           *p;
104
105         _uc    = (struct ucontext *) _p;
106         _mc    = &_uc->uc_mcontext;
107
108 #if defined(__UCLIBC__)
109         _gregs = _mc->gpregs;
110 #else   
111         _gregs = _mc->gregs;
112 #endif
113
114         /* In glibc's ucontext.h the registers are defined as long long,
115            even for MIPS32, so we cast them.  This is not the case for
116            uClibc. */
117
118         pv  = (u1 *) (ptrint) _gregs[REG_PV];
119         sp  = (u1 *) (ptrint) _gregs[REG_SP];
120         ra  = (u1 *) (ptrint) _gregs[REG_RA];        /* this is correct for leafs */
121
122 #if !defined(__UCLIBC__)
123 # if ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 5))
124         /* NOTE: We only need this for pre glibc-2.5. */
125
126         xpc = (u1 *) (ptrint) _mc->pc;
127
128         /* get the cause of this exception */
129
130         cause = _mc->cause;
131
132         /* check the cause to find the faulting instruction */
133
134         /* TODO: use defines for that stuff */
135
136         switch (cause & 0x0000003c) {
137         case 0x00000008:
138                 /* TLBL: XPC is ok */
139                 break;
140
141         case 0x00000010:
142                 /* AdEL: XPC is of the following instruction */
143                 xpc = xpc - 4;
144                 break;
145         }
146 # else
147         xpc = (u1 *) (ptrint) _mc->pc;
148 # endif
149 #else
150         xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
151 #endif
152
153         /* get exception-throwing instruction */
154
155         mcode = *((u4 *) xpc);
156
157         d    = M_ITYPE_GET_RT(mcode);
158         s1   = M_ITYPE_GET_RS(mcode);
159         disp = M_ITYPE_GET_IMM(mcode);
160
161         /* check for special-load */
162
163         if (s1 == REG_ZERO) {
164                 /* we use the exception type as load displacement */
165
166                 type = disp;
167                 val  = _gregs[d];
168
169                 if (type == EXCEPTION_HARDWARE_COMPILER) {
170                         /* The XPC is the RA minus 4, because the RA points to the
171                            instruction after the call. */
172
173                         xpc = ra - 4;
174                 }
175         }
176         else {
177                 /* This is a normal NPE: addr must be NULL and the NPE-type
178                    define is 0. */
179
180                 addr = _gregs[s1];
181                 type = (s4) addr;
182                 val  = 0;
183         }
184
185         /* Handle the type. */
186
187         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
188
189         /* Set registers. */
190
191         switch (type) {
192         case EXCEPTION_HARDWARE_COMPILER:
193                 if (p != NULL) {
194                         _gregs[REG_PV]  = (uintptr_t) p;
195 #if defined(__UCLIBC__)
196                         _gregs[CTX_EPC] = (uintptr_t) p;
197 #else
198                         _mc->pc         = (uintptr_t) p;
199 #endif
200                         break;
201                 }
202
203                 /* Get and set the PV from the parent Java method. */
204
205                 pv = md_codegen_get_pv_from_pc(ra);
206
207                 _gregs[REG_PV] = (uintptr_t) pv;
208
209                 /* Get the exception object. */
210
211                 p = builtin_retrieve_exception();
212
213                 assert(p != NULL);
214
215                 /* fall-through */
216
217         case EXCEPTION_HARDWARE_PATCHER:
218                 if (p == NULL) {
219                         /* We set the PC again because the cause may have changed
220                            the XPC. */
221
222 #if defined(__UCLIBC__)
223                         _gregs[CTX_EPC] = (uintptr_t) xpc;
224 #else
225                         _mc->pc         = (uintptr_t) xpc;
226 #endif
227                         break;
228                 }
229
230                 /* fall-through */
231                 
232         default:
233                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
234                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
235 #if defined(__UCLIBC__)
236                 _gregs[CTX_EPC]        = (uintptr_t) asm_handle_exception;
237 #else
238                 _mc->pc                = (uintptr_t) asm_handle_exception;
239 #endif
240         }
241 }
242
243
244 /* md_signal_handler_sigusr2 ***************************************************
245
246    DOCUMENT ME
247
248 *******************************************************************************/
249
250 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
251 {
252 }
253
254
255 /* md_critical_section_restart *************************************************
256
257    Search the critical sections tree for a matching section and set
258    the PC to the restart point, if necessary.
259
260 *******************************************************************************/
261
262 #if defined(ENABLE_THREADS)
263 void md_critical_section_restart(ucontext_t *_uc)
264 {
265         mcontext_t *_mc;
266         u1         *pc;
267         u1         *npc;
268
269         _mc = &_uc->uc_mcontext;
270
271 #if defined(__UCLIBC__)
272         pc = (u1 *) (ptrint) _mc->gpregs[CTX_EPC];
273 #else
274         pc = (u1 *) (ptrint) _mc->pc;
275 #endif
276
277         npc = critical_find_restart_point(pc);
278
279         if (npc != NULL) {
280 #if defined(__UCLIBC__)
281                 _mc->gpregs[CTX_EPC] = (ptrint) npc;
282 #else
283                 _mc->pc              = (ptrint) npc;
284 #endif
285         }
286 }
287 #endif
288
289
290 /*
291  * These are local overrides for various environment variables in Emacs.
292  * Please do not remove this and leave it at the end of the file, where
293  * Emacs will automagically detect them.
294  * ---------------------------------------------------------------------
295  * Local variables:
296  * mode: c
297  * indent-tabs-mode: t
298  * c-basic-offset: 4
299  * tab-width: 4
300  * End:
301  * vim:noexpandtab:sw=4:ts=4:
302  */