d37c191c477d0bbc8dff8cf40da857bbf73a7099
[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         ucontext_t        *_uc;
75         scontext_t        *_sc;
76         u1                *pv;
77         u1                *sp;
78         u1                *ra;
79         u1                *xpc;
80         u4                 mcode;
81         ptrint             addr;
82         s4                 type;
83         ptrint             val;
84         java_objectheader *o;
85
86         _uc = (ucontext_t*) _p;
87         _sc = &_uc->uc_mcontext;
88
89         /* ATTENTION: glibc included messed up kernel headers we needed a
90            workaround for the ucontext structure. */
91
92         pv  = (u1 *) _sc->arm_ip;
93         sp  = (u1 *) _sc->arm_sp;
94         ra  = (u1 *) _sc->arm_lr;                    /* this is correct for leafs */
95         xpc = (u1 *) _sc->arm_pc;
96
97         /* get exception-throwing instruction */
98
99         mcode = *((s4 *) xpc);
100
101         /* this is a NullPointerException */
102
103 /*      addr = _mc->gregs[s1]; */
104         addr = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + ((mcode >> 16) & 0x0f));
105         type = EXCEPTION_HARDWARE_NULLPOINTER;
106         val  = 0;
107
108         if (addr != 0)
109                 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
110
111         /* generate appropriate exception */
112
113         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
114
115         /* set registers */
116
117         _sc->arm_r10 = (ptrint) o;
118         _sc->arm_fp  = (ptrint) xpc;
119         _sc->arm_pc  = (ptrint) asm_handle_exception;
120 }
121
122
123 /* md_signal_handler_sigill ****************************************************
124
125    Illegal Instruction signal handler for hardware exception checks.
126
127 *******************************************************************************/
128
129 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
130 {
131         ucontext_t        *_uc;
132         scontext_t        *_sc;
133         u1                *pv;
134         u1                *sp;
135         u1                *ra;
136         u1                *xpc;
137         u4                 mcode;
138         s4                 type;
139         ptrint             val;
140         java_objectheader *o;
141
142         _uc = (ucontext_t*) _p;
143         _sc = &_uc->uc_mcontext;
144
145         /* ATTENTION: glibc included messed up kernel headers we needed a
146            workaround for the ucontext structure. */
147
148         pv  = (u1 *) _sc->arm_ip;
149         sp  = (u1 *) _sc->arm_sp;
150         ra  = (u1 *) _sc->arm_lr;                    /* this is correct for leafs */
151         xpc = (u1 *) _sc->arm_pc;
152
153         /* get exception-throwing instruction */
154
155         mcode = *((u4 *) xpc);
156
157         /* check for undefined instruction we use */
158
159         if ((mcode & 0x0ff000f0) != 0x07f000f0)
160                 vm_abort("md_signal_handler_sigill: unknown illegal instruction");
161
162         type = (mcode >> 8) & 0x0fff;
163         val  = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + (mcode & 0x0f));
164
165         /* generate appropriate exception */
166
167         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
168
169         /* set registers */
170
171         _sc->arm_r10 = (ptrint) o;
172         _sc->arm_fp  = (ptrint) xpc;
173         _sc->arm_pc  = (ptrint) asm_handle_exception;
174 }
175
176
177 /* md_signal_handler_sigusr2 ***************************************************
178
179    Signal handler for profiling sampling.
180
181 *******************************************************************************/
182
183 #if defined(ENABLE_THREADS)
184 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
185 {
186         threadobject *thread;
187         ucontext_t   *_uc;
188         scontext_t   *_sc;
189         u1           *pc;
190
191         thread = THREADOBJECT;
192
193         _uc = (ucontext_t*) _p;
194         _sc = &_uc->uc_mcontext;
195
196         pc = (u1 *) _sc->arm_pc;
197
198         thread->pc = pc;
199 }
200 #endif
201
202
203 /* thread_restartcriticalsection ***********************************************
204
205    TODO: document me
206
207 *******************************************************************************/
208
209 #if defined(ENABLE_THREADS)
210 void thread_restartcriticalsection(ucontext_t *_uc)
211 {
212         scontext_t *_sc;
213         void       *critical;
214
215         _sc = &_uc->uc_mcontext;
216
217         critical = critical_find_restart_point((void *) _sc->arm_pc);
218
219         if (critical)
220                 _sc->arm_pc = (ptrint) critical;
221 }
222 #endif
223
224
225 /*
226  * These are local overrides for various environment variables in Emacs.
227  * Please do not remove this and leave it at the end of the file, where
228  * Emacs will automagically detect them.
229  * ---------------------------------------------------------------------
230  * Local variables:
231  * mode: c
232  * indent-tabs-mode: t
233  * c-basic-offset: 4
234  * tab-width: 4
235  * End:
236  */
237