e80cac2ded7194a1401cc449a138884fe87db285
[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 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    $Id: md.c 166 2006-01-22 23:38:44Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/arm/md-abi.h"
38
39 #define ucontext broken_glibc_ucontext
40 #define ucontext_t broken_glibc_ucontext_t
41 #include <ucontext.h>
42 #undef ucontext
43 #undef ucontext_t
44
45 typedef struct ucontext {
46    unsigned long     uc_flags;
47    struct ucontext  *uc_link;
48    stack_t           uc_stack;
49    struct sigcontext uc_mcontext;
50    sigset_t          uc_sigmask;
51 } ucontext_t;
52
53 #define scontext_t struct sigcontext
54
55 #if defined(ENABLE_THREADS)
56 # include "threads/native/threads.h"
57 #endif
58
59 #include "vm/exceptions.h"
60 #include "vm/signallocal.h"
61 #include "vm/stringlocal.h"
62
63 #include "vm/jit/asmpart.h"
64 #include "vm/jit/stacktrace.h"
65
66
67 /* md_signal_handler_sigsegv ***************************************************
68
69    Signal handler for hardware exceptions.
70
71 *******************************************************************************/
72
73 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
74 {
75         stackframeinfo  sfi;
76         ucontext_t     *_uc;
77         scontext_t     *_sc;
78         u1             *pv;
79         u1             *sp;
80         u1             *ra;
81         u1             *xpc;
82         u4              mcode;
83         intptr_t        addr;
84         int             type;
85         intptr_t        val;
86         void           *p;
87
88         _uc = (ucontext_t*) _p;
89         _sc = &_uc->uc_mcontext;
90
91         /* ATTENTION: glibc included messed up kernel headers we needed a
92            workaround for the ucontext structure. */
93
94         pv  = (u1 *) _sc->arm_ip;
95         sp  = (u1 *) _sc->arm_sp;
96         ra  = (u1 *) _sc->arm_lr;                    /* this is correct for leafs */
97         xpc = (u1 *) _sc->arm_pc;
98
99         /* get exception-throwing instruction */
100
101         if (xpc == NULL)
102                 vm_abort("md_signal_handler_sigsegv: the program counter is NULL");
103
104         mcode = *((s4 *) xpc);
105
106         /* this is a NullPointerException */
107
108         addr = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + ((mcode >> 16) & 0x0f));
109         type = EXCEPTION_HARDWARE_NULLPOINTER;
110         val  = 0;
111
112         if (addr != 0)
113                 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
114
115         /* create stackframeinfo */
116
117         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
118
119         /* Handle the type. */
120
121         p = signal_handle(xpc, type, val);
122
123         /* remove stackframeinfo */
124
125         stacktrace_remove_stackframeinfo(&sfi);
126
127         /* set registers */
128
129         _sc->arm_r10 = (intptr_t) p;
130         _sc->arm_fp  = (intptr_t) xpc;
131         _sc->arm_pc  = (intptr_t) asm_handle_exception;
132 }
133
134
135 /* md_signal_handler_sigill ****************************************************
136
137    Illegal Instruction signal handler for hardware exception checks.
138
139 *******************************************************************************/
140
141 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
142 {
143         stackframeinfo  sfi;
144         ucontext_t     *_uc;
145         scontext_t     *_sc;
146         u1             *pv;
147         u1             *sp;
148         u1             *ra;
149         u1             *xpc;
150         u4              mcode;
151         int             type;
152         intptr_t        val;
153         void           *p;
154
155         _uc = (ucontext_t*) _p;
156         _sc = &_uc->uc_mcontext;
157
158         /* ATTENTION: glibc included messed up kernel headers we needed a
159            workaround for the ucontext structure. */
160
161         pv  = (u1 *) _sc->arm_ip;
162         sp  = (u1 *) _sc->arm_sp;
163         ra  = (u1 *) _sc->arm_lr;                    /* this is correct for leafs */
164         xpc = (u1 *) _sc->arm_pc;
165
166         /* get exception-throwing instruction */
167
168         mcode = *((u4 *) xpc);
169
170         /* check for undefined instruction we use */
171
172         if ((mcode & 0x0ff000f0) != 0x07f000f0)
173                 vm_abort("md_signal_handler_sigill: unknown illegal instruction");
174
175         type = (mcode >> 8) & 0x0fff;
176         val  = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + (mcode & 0x0f));
177
178         /* create stackframeinfo */
179
180         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
181
182         /* Handle the type. */
183
184         p = signal_handle(xpc, type, val);
185
186         /* remove stackframeinfo */
187
188         stacktrace_remove_stackframeinfo(&sfi);
189
190         /* set registers if we have an exception, return continue execution
191            otherwise (this is needed for patchers to work) */
192
193         if (p != NULL) {
194                 _sc->arm_r10 = (intptr_t) p;
195                 _sc->arm_fp  = (intptr_t) xpc;
196                 _sc->arm_pc  = (intptr_t) asm_handle_exception;
197         }
198 }
199
200
201 /* md_signal_handler_sigusr2 ***************************************************
202
203    Signal handler for profiling sampling.
204
205 *******************************************************************************/
206
207 #if defined(ENABLE_THREADS)
208 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
209 {
210         threadobject *thread;
211         ucontext_t   *_uc;
212         scontext_t   *_sc;
213         u1           *pc;
214
215         thread = THREADOBJECT;
216
217         _uc = (ucontext_t*) _p;
218         _sc = &_uc->uc_mcontext;
219
220         pc = (u1 *) _sc->arm_pc;
221
222         thread->pc = pc;
223 }
224 #endif
225
226
227 /* md_critical_section_restart *************************************************
228
229    Search the critical sections tree for a matching section and set
230    the PC to the restart point, if necessary.
231
232 *******************************************************************************/
233
234 #if defined(ENABLE_THREADS)
235 void md_critical_section_restart(ucontext_t *_uc)
236 {
237         scontext_t *_sc;
238         u1         *pc;
239         u1         *npc;
240
241         _sc = &_uc->uc_mcontext;
242
243         pc = (u1 *) _sc->arm_pc;
244
245         npc = critical_find_restart_point(pc);
246
247         if (npc != NULL)
248                 _sc->arm_pc = (ptrint) npc;
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  */
265