0788aa26a9ecc9341dd3625a4b8a2c56c9903216
[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.hpp"
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.hpp"
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         void* 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 = (void*) (((uintptr_t) ra) - 4);
162         }
163
164         // Handle the trap.
165         trap_handle(type, val, pv, sp, ra, xpc, _p);
166 }
167
168
169 /* md_signal_handler_sigusr1 ***************************************************
170
171    Signal handler for suspending threads.
172
173 *******************************************************************************/
174
175 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
176 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
177 {
178         ucontext_t *_uc;
179         scontext_t *_sc;
180         u1         *pc;
181         u1         *sp;
182
183         _uc = (ucontext_t *) _p;
184         _sc = &_uc->uc_mcontext;
185
186         /* get the PC and SP for this thread */
187         pc = (u1 *) _sc->arm_pc;
188         sp = (u1 *) _sc->arm_sp;
189
190         /* now suspend the current thread */
191         threads_suspend_ack(pc, sp);
192 }
193 #endif
194
195
196 /* md_signal_handler_sigusr2 ***************************************************
197
198    Signal handler for profiling sampling.
199
200 *******************************************************************************/
201
202 #if defined(ENABLE_THREADS)
203 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
204 {
205         threadobject *thread;
206         ucontext_t   *_uc;
207         scontext_t   *_sc;
208         u1           *pc;
209
210         thread = THREADOBJECT;
211
212         _uc = (ucontext_t*) _p;
213         _sc = &_uc->uc_mcontext;
214
215         pc = (u1 *) _sc->arm_pc;
216
217         thread->pc = pc;
218 }
219 #endif
220
221
222 /**
223  * Read the given context into an executionstate.
224  *
225  * @param es      execution state
226  * @param context machine context
227  */
228 void md_executionstate_read(executionstate_t *es, void *context)
229 {
230         ucontext_t *_uc;
231         scontext_t *_sc;
232         int         i;
233
234         _uc = (ucontext_t *) context;
235         _sc = &_uc->uc_mcontext;
236
237         /* ATTENTION: glibc included messed up kernel headers we needed a
238            workaround for the ucontext structure. */
239
240         /* read special registers */
241
242         es->pc = (u1 *) _sc->arm_pc;
243         es->sp = (u1 *) _sc->arm_sp;
244         es->pv = (u1 *) _sc->arm_ip;
245         es->ra = (u1 *) _sc->arm_lr;
246
247         /* read integer registers */
248
249         for (i = 0; i < INT_REG_CNT; i++)
250                 es->intregs[i] = *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i);
251
252         /* read float registers */
253
254         for (i = 0; i < FLT_REG_CNT; i++)
255                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
256 }
257
258
259 /**
260  * Write the given executionstate back to the context.
261  *
262  * @param es      execution state
263  * @param context machine context
264  */
265 void md_executionstate_write(executionstate_t *es, void *context)
266 {
267         ucontext_t *_uc;
268         scontext_t *_sc;
269         int         i;
270
271         _uc = (ucontext_t *) context;
272         _sc = &_uc->uc_mcontext;
273
274         /* ATTENTION: glibc included messed up kernel headers we needed a
275            workaround for the ucontext structure. */
276
277         /* write integer registers */
278
279         for (i = 0; i < INT_REG_CNT; i++)
280                 *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i) = es->intregs[i];
281
282         /* write special registers */
283
284         _sc->arm_pc = (ptrint) es->pc;
285         _sc->arm_sp = (ptrint) es->sp;
286         _sc->arm_ip = (ptrint) es->pv;
287         _sc->arm_lr = (ptrint) es->ra;
288 }
289
290
291 /*
292  * These are local overrides for various environment variables in Emacs.
293  * Please do not remove this and leave it at the end of the file, where
294  * Emacs will automagically detect them.
295  * ---------------------------------------------------------------------
296  * Local variables:
297  * mode: c
298  * indent-tabs-mode: t
299  * c-basic-offset: 4
300  * tab-width: 4
301  * End:
302  */
303