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