* src/vm/jit/mips/Makefile.am (libarch_la_SOURCES): Added md-trap.h.
[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 #include "vm/jit/trap.h"
49
50
51 /* md_init *********************************************************************
52
53    Do some machine dependent initialization.
54
55 *******************************************************************************/
56
57 void md_init(void)
58 {
59         /* The Boehm GC initialization blocks the SIGSEGV signal. So we do
60            a dummy allocation here to ensure that the GC is
61            initialized. */
62
63 #if defined(ENABLE_GC_BOEHM)
64         (void) GCNEW(int);
65 #endif
66
67 #if 0
68         /* Turn off flush-to-zero */
69
70         {
71                 union fpc_csr n;
72                 n.fc_word = get_fpc_csr();
73                 n.fc_struct.flush = 0;
74                 set_fpc_csr(n.fc_word);
75         }
76 #endif
77 }
78
79
80 /* md_signal_handler_sigsegv ***************************************************
81
82    NullPointerException signal handler for hardware null pointer
83    check.
84
85 *******************************************************************************/
86
87 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
88 {
89         ucontext_t     *_uc;
90         mcontext_t     *_mc;
91         greg_t         *_gregs;
92         u1             *pv;
93         u1             *sp;
94         u1             *ra;
95         u1             *xpc;
96         unsigned int    cause;
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         _uc    = (struct ucontext *) _p;
107         _mc    = &_uc->uc_mcontext;
108
109 #if defined(__UCLIBC__)
110         _gregs = _mc->gpregs;
111 #else   
112         _gregs = _mc->gregs;
113 #endif
114
115         /* In glibc's ucontext.h the registers are defined as long long,
116            even for MIPS32, so we cast them.  This is not the case for
117            uClibc. */
118
119         pv  = (u1 *) (ptrint) _gregs[REG_PV];
120         sp  = (u1 *) (ptrint) _gregs[REG_SP];
121         ra  = (u1 *) (ptrint) _gregs[REG_RA];        /* this is correct for leafs */
122
123 #if !defined(__UCLIBC__) && ((__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) _gregs[CTX_EPC];
148 #endif
149
150         /* get exception-throwing instruction */
151
152         mcode = *((u4 *) xpc);
153
154         d    = M_ITYPE_GET_RT(mcode);
155         s1   = M_ITYPE_GET_RS(mcode);
156         disp = M_ITYPE_GET_IMM(mcode);
157
158         /* check for special-load */
159
160         if (s1 == REG_ZERO) {
161                 /* we use the exception type as load displacement */
162
163                 type = disp;
164                 val  = _gregs[d];
165
166                 if (type == TRAP_COMPILER) {
167                         /* The XPC is the RA minus 4, because the RA points to the
168                            instruction after the call. */
169
170                         xpc = ra - 4;
171                 }
172         }
173         else {
174                 /* This is a normal NPE: addr must be NULL and the NPE-type
175                    define is 0. */
176
177                 addr = _gregs[s1];
178                 type = (int) addr;
179                 val  = 0;
180         }
181
182         /* Handle the trap. */
183
184         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
185
186         /* Set registers. */
187
188         switch (type) {
189         case TRAP_COMPILER:
190                 if (p != NULL) {
191                         _gregs[REG_PV]  = (uintptr_t) p;
192 #if defined(__UCLIBC__)
193                         _gregs[CTX_EPC] = (uintptr_t) p;
194 #else
195                         _mc->pc         = (uintptr_t) p;
196 #endif
197                         break;
198                 }
199
200                 /* Get and set the PV from the parent Java method. */
201
202                 pv = md_codegen_get_pv_from_pc(ra);
203
204                 _gregs[REG_PV] = (uintptr_t) pv;
205
206                 /* Get the exception object. */
207
208                 p = builtin_retrieve_exception();
209
210                 assert(p != NULL);
211
212                 /* fall-through */
213
214         case TRAP_PATCHER:
215                 if (p == NULL) {
216                         /* We set the PC again because the cause may have changed
217                            the XPC. */
218
219 #if defined(__UCLIBC__)
220                         _gregs[CTX_EPC] = (uintptr_t) xpc;
221 #else
222                         _mc->pc         = (uintptr_t) xpc;
223 #endif
224                         break;
225                 }
226
227                 /* fall-through */
228                 
229         default:
230                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
231                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
232 #if defined(__UCLIBC__)
233                 _gregs[CTX_EPC]        = (uintptr_t) asm_handle_exception;
234 #else
235                 _mc->pc                = (uintptr_t) asm_handle_exception;
236 #endif
237         }
238 }
239
240
241 /* md_signal_handler_sigusr2 ***************************************************
242
243    DOCUMENT ME
244
245 *******************************************************************************/
246
247 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
248 {
249 }
250
251
252 /* md_critical_section_restart *************************************************
253
254    Search the critical sections tree for a matching section and set
255    the PC to the restart point, if necessary.
256
257 *******************************************************************************/
258
259 #if defined(ENABLE_THREADS)
260 void md_critical_section_restart(ucontext_t *_uc)
261 {
262         mcontext_t *_mc;
263         u1         *pc;
264         u1         *npc;
265
266         _mc = &_uc->uc_mcontext;
267
268 #if defined(__UCLIBC__)
269         pc = (u1 *) (ptrint) _mc->gpregs[CTX_EPC];
270 #else
271         pc = (u1 *) (ptrint) _mc->pc;
272 #endif
273
274         npc = critical_find_restart_point(pc);
275
276         if (npc != NULL) {
277 #if defined(__UCLIBC__)
278                 _mc->gpregs[CTX_EPC] = (ptrint) npc;
279 #else
280                 _mc->pc              = (ptrint) npc;
281 #endif
282         }
283 }
284 #endif
285
286
287 /*
288  * These are local overrides for various environment variables in Emacs.
289  * Please do not remove this and leave it at the end of the file, where
290  * Emacs will automagically detect them.
291  * ---------------------------------------------------------------------
292  * Local variables:
293  * mode: c
294  * indent-tabs-mode: t
295  * c-basic-offset: 4
296  * tab-width: 4
297  * End:
298  * vim:noexpandtab:sw=4:ts=4:
299  */