* Merged executionstate branch.
[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
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "vm/types.h"
31
32 #include "vm/jit/disass.h"
33
34 #include "vm/jit/arm/md-abi.h"
35
36 #define ucontext broken_glibc_ucontext
37 #define ucontext_t broken_glibc_ucontext_t
38 #include <ucontext.h>
39 #undef ucontext
40 #undef ucontext_t
41
42 typedef struct ucontext {
43    unsigned long     uc_flags;
44    struct ucontext  *uc_link;
45    stack_t           uc_stack;
46    struct sigcontext uc_mcontext;
47    sigset_t          uc_sigmask;
48 } ucontext_t;
49
50 #define scontext_t struct sigcontext
51
52 #include "threads/thread.h"
53
54 #include "vm/exceptions.h"
55 #include "vm/signallocal.h"
56 #include "vm/stringlocal.h"
57 #include "vm/vm.h"
58
59 #include "vm/jit/asmpart.h"
60 #include "vm/jit/stacktrace.h"
61
62
63 /* md_signal_handler_sigsegv ***************************************************
64
65    Signal handler for hardware exceptions.
66
67 *******************************************************************************/
68
69 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
70 {
71         ucontext_t     *_uc;
72         scontext_t     *_sc;
73         u1             *pv;
74         u1             *sp;
75         u1             *ra;
76         u1             *xpc;
77         u4              mcode;
78         intptr_t        addr;
79         int             type;
80         intptr_t        val;
81         void           *p;
82
83         _uc = (ucontext_t*) _p;
84         _sc = &_uc->uc_mcontext;
85
86         /* ATTENTION: glibc included messed up kernel headers we needed a
87            workaround for the ucontext structure. */
88
89         pv  = (u1 *) _sc->arm_ip;
90         sp  = (u1 *) _sc->arm_sp;
91         ra  = (u1 *) _sc->arm_lr;                    /* this is correct for leafs */
92         xpc = (u1 *) _sc->arm_pc;
93
94         /* get exception-throwing instruction */
95
96         if (xpc == NULL)
97                 vm_abort("md_signal_handler_sigsegv: the program counter is NULL");
98
99         mcode = *((s4 *) xpc);
100
101         /* this is a NullPointerException */
102
103         addr = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + ((mcode >> 16) & 0x0f));
104         type = EXCEPTION_HARDWARE_NULLPOINTER;
105         val  = 0;
106
107         if (addr != 0)
108                 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
109
110         /* Handle the type. */
111
112         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
113
114         /* set registers */
115
116         _sc->arm_r10 = (intptr_t) p;
117         _sc->arm_fp  = (intptr_t) xpc;
118         _sc->arm_pc  = (intptr_t) asm_handle_exception;
119 }
120
121
122 /* md_signal_handler_sigill ****************************************************
123
124    Illegal Instruction signal handler for hardware exception checks.
125
126 *******************************************************************************/
127
128 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
129 {
130         ucontext_t     *_uc;
131         scontext_t     *_sc;
132         u1             *pv;
133         u1             *sp;
134         u1             *ra;
135         u1             *xpc;
136         u4              mcode;
137         int             type;
138         intptr_t        val;
139         void           *p;
140
141         _uc = (ucontext_t*) _p;
142         _sc = &_uc->uc_mcontext;
143
144         /* ATTENTION: glibc included messed up kernel headers we needed a
145            workaround for the ucontext structure. */
146
147         pv  = (u1 *) _sc->arm_ip;
148         sp  = (u1 *) _sc->arm_sp;
149         ra  = (u1 *) _sc->arm_lr;                    /* this is correct for leafs */
150         xpc = (u1 *) _sc->arm_pc;
151
152         /* get exception-throwing instruction */
153
154         mcode = *((u4 *) xpc);
155
156         /* check for undefined instruction we use */
157
158         if ((mcode & 0x0ff000f0) != 0x07f000f0) {
159                 log_println("md_signal_handler_sigill: unknown illegal instruction: inst=%x", mcode);
160 #if defined(ENABLE_DISASSEMBLER)
161                 DISASSINSTR(xpc);
162 #endif
163                 vm_abort("Aborting...");
164         }
165
166         type = (mcode >> 8) & 0x0fff;
167         val  = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + (mcode & 0x0f));
168
169         /* Handle the type. */
170
171         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
172
173         /* set registers if we have an exception, continue execution
174            otherwise (this is needed for patchers to work) */
175
176         if (p != NULL) {
177                 _sc->arm_r10 = (intptr_t) p;
178                 _sc->arm_fp  = (intptr_t) xpc;
179                 _sc->arm_pc  = (intptr_t) asm_handle_exception;
180         }
181 }
182
183
184 /* md_signal_handler_sigusr1 ***************************************************
185
186    Signal handler for suspending threads.
187
188 *******************************************************************************/
189
190 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
191 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
192 {
193         ucontext_t *_uc;
194         scontext_t *_sc;
195         u1         *pc;
196         u1         *sp;
197
198         _uc = (ucontext_t *) _p;
199         _sc = &_uc->uc_mcontext;
200
201         /* get the PC and SP for this thread */
202         pc = (u1 *) _sc->arm_pc;
203         sp = (u1 *) _sc->arm_sp;
204
205         /* now suspend the current thread */
206         threads_suspend_ack(pc, sp);
207 }
208 #endif
209
210
211 /* md_signal_handler_sigusr2 ***************************************************
212
213    Signal handler for profiling sampling.
214
215 *******************************************************************************/
216
217 #if defined(ENABLE_THREADS)
218 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
219 {
220         threadobject *thread;
221         ucontext_t   *_uc;
222         scontext_t   *_sc;
223         u1           *pc;
224
225         thread = THREADOBJECT;
226
227         _uc = (ucontext_t*) _p;
228         _sc = &_uc->uc_mcontext;
229
230         pc = (u1 *) _sc->arm_pc;
231
232         thread->pc = pc;
233 }
234 #endif
235
236
237 /* md_critical_section_restart *************************************************
238
239    Search the critical sections tree for a matching section and set
240    the PC to the restart point, if necessary.
241
242 *******************************************************************************/
243
244 #if defined(ENABLE_THREADS)
245 void md_critical_section_restart(ucontext_t *_uc)
246 {
247         scontext_t *_sc;
248         u1         *pc;
249         u1         *npc;
250
251         _sc = &_uc->uc_mcontext;
252
253         pc = (u1 *) _sc->arm_pc;
254
255         npc = critical_find_restart_point(pc);
256
257         if (npc != NULL)
258                 _sc->arm_pc = (ptrint) npc;
259 }
260 #endif
261
262
263 /*
264  * These are local overrides for various environment variables in Emacs.
265  * Please do not remove this and leave it at the end of the file, where
266  * Emacs will automagically detect them.
267  * ---------------------------------------------------------------------
268  * Local variables:
269  * mode: c
270  * indent-tabs-mode: t
271  * c-basic-offset: 4
272  * tab-width: 4
273  * End:
274  */
275