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