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