c2848c7891cd857ac3b9f1c0977741300b8f71fc
[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 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 8283 2007-08-09 15:10:05Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
34 #include <signal.h>
35 #include <stdint.h>
36 #include <ucontext.h>
37
38 #include "vm/types.h"
39
40 #include "vm/jit/mips/codegen.h"
41 #include "vm/jit/mips/md-abi.h"
42
43 #include "mm/gc-common.h"
44
45 #include "vm/signallocal.h"
46
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.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 a      */
60         /* dummy allocation here to ensure that the GC is initialized.            */
61
62 #if defined(ENABLE_GC_BOEHM)
63         heap_allocate(1, 0, NULL);
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         stackframeinfo  sfi;
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         else {
167                 /* This is a normal NPE: addr must be NULL and the NPE-type
168                    define is 0. */
169
170                 addr = _gregs[s1];
171                 type = (s4) addr;
172                 val  = 0;
173         }
174
175         /* create stackframeinfo */
176
177         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
178
179         /* Handle the type. */
180
181         p = signal_handle(xpc, type, val);
182
183         /* remove stackframeinfo */
184
185         stacktrace_remove_stackframeinfo(&sfi);
186
187         /* set registers (only if exception object ready) */
188
189         if (p != NULL) {
190                 _gregs[REG_ITMP1_XPTR] = (intptr_t) p;
191                 _gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
192
193 #if defined(__UCLIBC__)
194                 _gregs[CTX_EPC]        = (intptr_t) asm_handle_exception;
195 #else
196                 _mc->pc                = (intptr_t) asm_handle_exception;
197 #endif
198         }
199         else {
200 #if defined(__UCLIBC__)
201                 _gregs[CTX_EPC]        = (intptr_t) xpc;
202 #else
203                 _mc->pc                = (intptr_t) xpc;
204 #endif
205         }
206 }
207
208
209 /* md_signal_handler_sigusr2 ***************************************************
210
211    DOCUMENT ME
212
213 *******************************************************************************/
214
215 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
216 {
217 }
218
219
220 /* md_critical_section_restart *************************************************
221
222    Search the critical sections tree for a matching section and set
223    the PC to the restart point, if necessary.
224
225 *******************************************************************************/
226
227 #if defined(ENABLE_THREADS)
228 void md_critical_section_restart(ucontext_t *_uc)
229 {
230         mcontext_t *_mc;
231         u1         *pc;
232         u1         *npc;
233
234         _mc = &_uc->uc_mcontext;
235
236 #if defined(__UCLIBC__)
237         pc = (u1 *) (ptrint) _mc->gpregs[CTX_EPC];
238 #else
239         pc = (u1 *) (ptrint) _mc->pc;
240 #endif
241
242         npc = critical_find_restart_point(pc);
243
244         if (npc != NULL) {
245 #if defined(__UCLIBC__)
246                 _mc->gpregs[CTX_EPC] = (ptrint) npc;
247 #else
248                 _mc->pc              = (ptrint) npc;
249 #endif
250         }
251 }
252 #endif
253
254
255 /*
256  * These are local overrides for various environment variables in Emacs.
257  * Please do not remove this and leave it at the end of the file, where
258  * Emacs will automagically detect them.
259  * ---------------------------------------------------------------------
260  * Local variables:
261  * mode: c
262  * indent-tabs-mode: t
263  * c-basic-offset: 4
264  * tab-width: 4
265  * End:
266  * vim:noexpandtab:sw=4:ts=4:
267  */