Merged trunk and subtype.
[cacao.git] / src / vm / jit / alpha / linux / md-os.c
1 /* src/vm/jit/alpha/linux/md-os.c - machine dependent Alpha Linux functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5    Copyright (C) 2008 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include <stdint.h>
31 #include <ucontext.h>
32
33 #include "vm/types.h"
34
35 #include "vm/jit/alpha/codegen.h"
36 #include "vm/jit/alpha/md.h"
37 #include "vm/jit/alpha/md-abi.h"
38
39 #include "threads/thread.hpp"
40
41 #include "vm/jit/builtin.hpp"
42 #include "vm/signallocal.h"
43 #include "vm/os.hpp"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/disass.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/trap.h"
49
50
51 /* md_signal_handler_sigsegv ***************************************************
52
53    NullPointerException signal handler for hardware null pointer
54    check.
55
56 *******************************************************************************/
57
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
59 {
60         ucontext_t     *_uc;
61         mcontext_t     *_mc;
62         u1             *pv;
63         u1             *sp;
64         u1             *ra;
65         u1             *xpc;
66         u4              mcode;
67         s4              d;
68         s4              s1;
69         s4              disp;
70         intptr_t        val;
71         intptr_t        addr;
72         int             type;
73         void           *p;
74
75         _uc = (ucontext_t *) _p;
76         _mc = &_uc->uc_mcontext;
77
78         pv  = (u1 *) _mc->sc_regs[REG_PV];
79         sp  = (u1 *) _mc->sc_regs[REG_SP];
80         ra  = (u1 *) _mc->sc_regs[REG_RA];           /* this is correct for leafs */
81         xpc = (u1 *) _mc->sc_pc;
82
83         /* get exception-throwing instruction */
84
85         mcode = *((u4 *) xpc);
86
87         d    = M_MEM_GET_Ra(mcode);
88         s1   = M_MEM_GET_Rb(mcode);
89         disp = M_MEM_GET_Memory_disp(mcode);
90
91         val  = _mc->sc_regs[d];
92
93         /* check for special-load */
94
95         if (s1 == REG_ZERO) {
96                 /* we use the exception type as load displacement */
97
98                 type = disp;
99
100                 if (type == TRAP_COMPILER) {
101                         /* The XPC is the RA minus 1, because the RA points to the
102                            instruction after the call. */
103
104                         xpc = ra - 4;
105                 }
106         }
107         else {
108                 /* This is a normal NPE: addr must be NULL and the NPE-type
109                    define is 0. */
110
111                 addr = _mc->sc_regs[s1];
112                 type = (int) addr;
113         }
114
115         /* Handle the trap. */
116
117         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
118
119         /* Set registers. */
120
121         switch (type) {
122         case TRAP_COMPILER:
123                 if (p != NULL) {
124                         _mc->sc_regs[REG_PV] = (uintptr_t) p;
125                         _mc->sc_pc           = (uintptr_t) p;
126                         break;
127                 }
128
129                 /* Get and set the PV from the parent Java method. */
130
131                 pv = md_codegen_get_pv_from_pc(ra);
132
133                 _mc->sc_regs[REG_PV] = (uintptr_t) pv;
134
135                 /* Get the exception object. */
136
137                 p = builtin_retrieve_exception();
138
139                 assert(p != NULL);
140
141                 /* fall-through */
142
143         default:
144                 _mc->sc_regs[REG_ITMP1_XPTR] = (uintptr_t) p;
145                 _mc->sc_regs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
146                 _mc->sc_pc                   = (uintptr_t) asm_handle_exception;
147         }
148 }
149
150
151 /* md_signal_handler_sigill ****************************************************
152
153    Illegal Instruction signal handler for hardware exception checks.
154
155 *******************************************************************************/
156
157 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
158 {
159         ucontext_t* _uc = (ucontext_t*) _p;
160         mcontext_t* _mc = &_uc->uc_mcontext;
161
162         void* pv  = (u1 *) _mc->sc_regs[REG_PV];
163         void* sp  = (u1 *) _mc->sc_regs[REG_SP];
164         void* ra  = (u1 *) _mc->sc_regs[REG_RA]; // RA is correct for leaf methods.
165         void* xpc = (u1 *) _mc->sc_pc;
166
167         // The PC points to the instruction after the illegal instruction.
168         xpc = (void*) (((uintptr_t) xpc) - 4);
169
170         // Get the exception-throwing instruction.
171         uint32_t mcode = *((uint32_t*) xpc);
172
173         int opcode = M_OP3_GET_Opcode(mcode);
174
175         // Check for undefined instruction we use.
176         // TODO Check the whole instruction.
177         if (opcode != 0x4) {
178                 log_println("md_signal_handler_sigill: Unknown illegal instruction %x at %p", mcode, xpc);
179 #if defined(ENABLE_DISASSEMBLER)
180                 (void) disassinstr(xpc);
181 #endif
182                 vm_abort("Aborting...");
183         }
184
185         // This signal is always a patcher.
186         int      type = TRAP_PATCHER;
187         intptr_t val  = 0;
188
189         // Handle the trap.
190         void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
191
192         // Set registers if we have an exception, continue execution
193         // otherwise.
194         if (p != NULL) {
195                 _mc->sc_regs[REG_ITMP1_XPTR] = (uintptr_t) p;
196                 _mc->sc_regs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
197                 _mc->sc_pc                   = (uintptr_t) asm_handle_exception;
198         }
199         else {
200                 // We need to set the PC because we adjusted it above.
201                 _mc->sc_pc                   = (uintptr_t) xpc;
202         }
203 }
204
205
206 /* md_signal_handler_sigusr1 ***************************************************
207
208    Signal handler for suspending threads.
209
210 *******************************************************************************/
211
212 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
213 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
214 {
215         ucontext_t *_uc;
216         mcontext_t *_mc;
217         u1         *pc;
218         u1         *sp;
219
220         _uc = (ucontext_t *) _p;
221         _mc = &_uc->uc_mcontext;
222
223         /* get the PC and SP for this thread */
224         pc = (u1 *) _mc->sc_pc;
225         sp = (u1 *) _mc->sc_regs[REG_SP];
226
227         /* now suspend the current thread */
228         threads_suspend_ack(pc, sp);
229 }
230 #endif
231
232
233 /* md_signal_handler_sigusr2 ***************************************************
234
235    Signal handler for profiling sampling.
236
237 *******************************************************************************/
238
239 #if defined(ENABLE_THREADS)
240 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
241 {
242         threadobject *tobj;
243         ucontext_t   *_uc;
244         mcontext_t   *_mc;
245         u1           *pc;
246
247         tobj = THREADOBJECT;
248
249         _uc = (ucontext_t *) _p;
250         _mc = &_uc->uc_mcontext;
251
252         pc = (u1 *) _mc->sc_pc;
253
254         tobj->pc = pc;
255 }
256 #endif
257
258
259 /* md_executionstate_read ******************************************************
260
261    Read the given context into an executionstate.
262
263 *******************************************************************************/
264
265 void md_executionstate_read(executionstate_t *es, void *context)
266 {
267         ucontext_t *_uc;
268         mcontext_t *_mc;
269         int         i;
270
271         _uc = (ucontext_t *) context;
272         _mc = &_uc->uc_mcontext;
273
274         /* read special registers */
275         es->pc = (u1 *) _mc->sc_pc;
276         es->sp = (u1 *) _mc->sc_regs[REG_SP];
277         es->pv = (u1 *) _mc->sc_regs[REG_PV];
278         es->ra = (u1 *) _mc->sc_regs[REG_RA];
279
280         /* read integer registers */
281         for (i = 0; i < INT_REG_CNT; i++)
282                 es->intregs[i] = _mc->sc_regs[i];
283
284         /* read float registers */
285         /* Do not use the assignment operator '=', as the type of
286          * the _mc->sc_fpregs[i] can cause invalid conversions. */
287
288         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
289         os_memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
290 }
291
292
293 /* md_executionstate_write *****************************************************
294
295    Write the given executionstate back to the context.
296
297 *******************************************************************************/
298
299 void md_executionstate_write(executionstate_t *es, void *context)
300 {
301         ucontext_t *_uc;
302         mcontext_t *_mc;
303         int         i;
304
305         _uc = (ucontext_t *) context;
306         _mc = &_uc->uc_mcontext;
307
308         /* write integer registers */
309         for (i = 0; i < INT_REG_CNT; i++)
310                 _mc->sc_regs[i] = es->intregs[i];
311
312         /* write float registers */
313         /* Do not use the assignment operator '=', as the type of
314          * the _mc->sc_fpregs[i] can cause invalid conversions. */
315
316         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
317         os_memcpy(&_mc->sc_fpregs, &es->fltregs, sizeof(_mc->sc_fpregs));
318
319         /* write special registers */
320         _mc->sc_pc           = (ptrint) es->pc;
321         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
322         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
323         _mc->sc_regs[REG_RA] = (ptrint) es->ra;
324 }
325
326
327 /*
328  * These are local overrides for various environment variables in Emacs.
329  * Please do not remove this and leave it at the end of the file, where
330  * Emacs will automagically detect them.
331  * ---------------------------------------------------------------------
332  * Local variables:
333  * mode: c
334  * indent-tabs-mode: t
335  * c-basic-offset: 4
336  * tab-width: 4
337  * End:
338  */