* src/vm/jit/arm/arch.h: Defined JIT_COMPILER_VIA_SIGNAL.
[cacao.git] / src / vm / jit / arm / linux / md-os.c
1 /* src/vm/jit/arm/linux/md-os.c - machine dependent ARM 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 <stdint.h>
30
31 #define ucontext broken_glibc_ucontext
32 #define ucontext_t broken_glibc_ucontext_t
33 #include <ucontext.h>
34 #undef ucontext
35 #undef ucontext_t
36
37 typedef struct ucontext {
38    unsigned long     uc_flags;
39    struct ucontext  *uc_link;
40    stack_t           uc_stack;
41    struct sigcontext uc_mcontext;
42    sigset_t          uc_sigmask;
43 } ucontext_t;
44
45 #define scontext_t struct sigcontext
46
47 #include "vm/types.h"
48
49 #include "vm/jit/arm/md.h"
50 #include "vm/jit/arm/md-abi.h"
51
52 #include "threads/thread.hpp"
53
54 #include "vm/os.hpp"
55 #include "vm/signallocal.h"
56 #include "vm/vm.hpp"
57
58 #include "vm/jit/asmpart.h"
59 #include "vm/jit/disass.h"
60 #include "vm/jit/executionstate.h"
61 #include "vm/jit/patcher-common.hpp"
62 #include "vm/jit/trap.h"
63
64
65 /* md_signal_handler_sigsegv ***************************************************
66
67    Signal handler for hardware exceptions.
68
69 *******************************************************************************/
70
71 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
72 {
73         ucontext_t     *_uc;
74         scontext_t     *_sc;
75         u1             *pv;
76         u1             *sp;
77         u1             *ra;
78         u1             *xpc;
79         u4              mcode;
80         intptr_t        addr;
81         int             type;
82         intptr_t        val;
83
84         _uc = (ucontext_t*) _p;
85         _sc = &_uc->uc_mcontext;
86
87         /* ATTENTION: glibc included messed up kernel headers we needed a
88            workaround for the ucontext structure. */
89
90         pv  = (u1 *) _sc->arm_ip;
91         sp  = (u1 *) _sc->arm_sp;
92         ra  = (u1 *) _sc->arm_lr;                    /* this is correct for leafs */
93         xpc = (u1 *) _sc->arm_pc;
94
95         /* get exception-throwing instruction */
96
97         if (xpc == NULL)
98                 vm_abort("md_signal_handler_sigsegv: the program counter is NULL");
99
100         mcode = *((s4 *) xpc);
101
102         /* This is a NullPointerException. */
103
104         addr = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + ((mcode >> 16) & 0x0f));
105         type = addr;
106         val  = 0;
107
108         /* Handle the trap. */
109
110         trap_handle(type, val, pv, sp, ra, xpc, _p);
111 }
112
113
114 /* md_signal_handler_sigill ****************************************************
115
116    Illegal Instruction signal handler for hardware exception checks.
117
118 *******************************************************************************/
119
120 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
121 {
122         ucontext_t* _uc = (ucontext_t*) _p;
123         scontext_t* _sc = &_uc->uc_mcontext;
124
125         /* ATTENTION: glibc included messed up kernel headers we needed a
126            workaround for the ucontext structure. */
127
128         void* pv  = (void*) _sc->arm_ip;
129         void* sp  = (void*) _sc->arm_sp;
130         u1*   ra  = (void*) _sc->arm_lr; // The RA is correct for leaf methods.
131         void* xpc = (void*) _sc->arm_pc;
132
133         // Get the exception-throwing instruction.
134         uint32_t mcode = *((uint32_t*) xpc);
135
136         // Check if the trap instruction is valid.
137         // TODO Move this into patcher_handler.
138         if (patcher_is_valid_trap_instruction_at(xpc) == false) {
139                 // Check if the PC has been patched during our way to this
140                 // signal handler (see PR85).
141                 // NOTE: ARM uses SIGILL for other traps too, but it's OK to
142                 // do this check anyway because it will fail.
143                 if (patcher_is_patched_at(xpc) == true)
144                         return;
145
146                 // We have a problem...
147                 log_println("md_signal_handler_sigill: Unknown illegal instruction 0x%x at 0x%x", mcode, xpc);
148 #if defined(ENABLE_DISASSEMBLER)
149                 (void) disassinstr(xpc);
150 #endif
151                 vm_abort("Aborting...");
152         }
153
154         int      type = (mcode >> 8) & 0x0fff;
155         intptr_t val  = *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + (mcode & 0x0f));
156
157         if (type == TRAP_COMPILER) {
158                 /* The XPC is the RA minus 4, because the RA points to the
159                    instruction after the call. */
160
161                 xpc = ra - 4;
162         }
163
164         // Handle the trap.
165         void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
166
167         if (type == TRAP_COMPILER) {
168                 if (p != NULL) {
169                         _sc->arm_ip = (uintptr_t) p; // set REG_PV correctly
170                 }
171                 else {
172                         /* Get and set the PV from the parent Java method. */
173
174                         pv = md_codegen_get_pv_from_pc(ra);
175
176                         _sc->arm_ip = (uintptr_t) pv;
177                 }
178         }
179 }
180
181
182 /* md_signal_handler_sigusr1 ***************************************************
183
184    Signal handler for suspending threads.
185
186 *******************************************************************************/
187
188 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
189 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
190 {
191         ucontext_t *_uc;
192         scontext_t *_sc;
193         u1         *pc;
194         u1         *sp;
195
196         _uc = (ucontext_t *) _p;
197         _sc = &_uc->uc_mcontext;
198
199         /* get the PC and SP for this thread */
200         pc = (u1 *) _sc->arm_pc;
201         sp = (u1 *) _sc->arm_sp;
202
203         /* now suspend the current thread */
204         threads_suspend_ack(pc, sp);
205 }
206 #endif
207
208
209 /* md_signal_handler_sigusr2 ***************************************************
210
211    Signal handler for profiling sampling.
212
213 *******************************************************************************/
214
215 #if defined(ENABLE_THREADS)
216 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
217 {
218         threadobject *thread;
219         ucontext_t   *_uc;
220         scontext_t   *_sc;
221         u1           *pc;
222
223         thread = THREADOBJECT;
224
225         _uc = (ucontext_t*) _p;
226         _sc = &_uc->uc_mcontext;
227
228         pc = (u1 *) _sc->arm_pc;
229
230         thread->pc = pc;
231 }
232 #endif
233
234
235 /**
236  * Read the given context into an executionstate.
237  *
238  * @param es      execution state
239  * @param context machine context
240  */
241 void md_executionstate_read(executionstate_t *es, void *context)
242 {
243         ucontext_t *_uc;
244         scontext_t *_sc;
245         int         i;
246
247         _uc = (ucontext_t *) context;
248         _sc = &_uc->uc_mcontext;
249
250         /* ATTENTION: glibc included messed up kernel headers we needed a
251            workaround for the ucontext structure. */
252
253         /* read special registers */
254
255         es->pc = (u1 *) _sc->arm_pc;
256         es->sp = (u1 *) _sc->arm_sp;
257         es->pv = (u1 *) _sc->arm_ip;
258         es->ra = (u1 *) _sc->arm_lr;
259
260         /* read integer registers */
261
262         for (i = 0; i < INT_REG_CNT; i++)
263                 es->intregs[i] = *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i);
264
265         /* read float registers */
266
267         for (i = 0; i < FLT_REG_CNT; i++)
268                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
269 }
270
271
272 /**
273  * Write the given executionstate back to the context.
274  *
275  * @param es      execution state
276  * @param context machine context
277  */
278 void md_executionstate_write(executionstate_t *es, void *context)
279 {
280         ucontext_t *_uc;
281         scontext_t *_sc;
282         int         i;
283
284         _uc = (ucontext_t *) context;
285         _sc = &_uc->uc_mcontext;
286
287         /* ATTENTION: glibc included messed up kernel headers we needed a
288            workaround for the ucontext structure. */
289
290         /* write integer registers */
291
292         for (i = 0; i < INT_REG_CNT; i++)
293                 *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i) = es->intregs[i];
294
295         /* write special registers */
296
297         _sc->arm_pc = (ptrint) es->pc;
298         _sc->arm_sp = (ptrint) es->sp;
299         _sc->arm_ip = (ptrint) es->pv;
300         _sc->arm_lr = (ptrint) es->ra;
301 }
302
303
304 /*
305  * These are local overrides for various environment variables in Emacs.
306  * Please do not remove this and leave it at the end of the file, where
307  * Emacs will automagically detect them.
308  * ---------------------------------------------------------------------
309  * Local variables:
310  * mode: c
311  * indent-tabs-mode: t
312  * c-basic-offset: 4
313  * tab-width: 4
314  * End:
315  */
316