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