731fad10e47c5207fc25ce1300e13937012644ef
[cacao.git] / src / vm / jit / i386 / linux / md-os.c
1 /* src/vm/jit/i386/linux/md-os.c - machine dependent i386 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 */
26
27
28 #define _GNU_SOURCE                   /* include REG_ defines from ucontext.h */
29
30 #include "config.h"
31
32 #include <stdint.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/i386/codegen.h"
38
39 #include "threads/threads-common.h"
40
41 #include "vm/exceptions.h"
42 #include "vm/signallocal.h"
43 #include "vm/stringlocal.h"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/stacktrace.h"
47
48
49 /* md_signal_handler_sigsegv ***************************************************
50
51    Signal handler for hardware exceptions.
52
53 *******************************************************************************/
54
55 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
56 {
57         ucontext_t     *_uc;
58         mcontext_t     *_mc;
59         u1             *pv;
60         u1             *sp;
61         u1             *ra;
62         u1             *xpc;
63         u1              opc;
64         u1              mod;
65         u1              rm;
66         s4              d;
67         s4              disp;
68         ptrint          val;
69         s4              type;
70         void           *p;
71         java_object_t  *o;
72
73         _uc = (ucontext_t *) _p;
74         _mc = &_uc->uc_mcontext;
75
76         pv  = NULL;                 /* is resolved during stackframeinfo creation */
77         sp  = (u1 *) _mc->gregs[REG_ESP];
78         xpc = (u1 *) _mc->gregs[REG_EIP];
79         ra  = xpc;                              /* return address is equal to XPC */
80
81         /* get exception-throwing instruction */
82
83         opc = M_ALD_MEM_GET_OPC(xpc);
84         mod = M_ALD_MEM_GET_MOD(xpc);
85         rm  = M_ALD_MEM_GET_RM(xpc);
86
87         /* for values see emit_mov_mem_reg and emit_mem */
88
89         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
90                 /* this was a hardware-exception */
91
92                 d    = M_ALD_MEM_GET_REG(xpc);
93                 disp = M_ALD_MEM_GET_DISP(xpc);
94
95                 /* we use the exception type as load displacement */
96
97                 type = disp;
98
99                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
100                    registers are reversed starting with number 4 for REG_EDI
101                    (see /usr/include/sys/ucontext.h).  We have to convert that
102                    here. */
103
104                 val = _mc->gregs[REG_EAX - d];
105
106                 if (type == EXCEPTION_HARDWARE_COMPILER) {
107                         /* We use a framesize of zero here because the call pushed
108                            the return addres onto the stack. */
109
110                         ra = md_stacktrace_get_returnaddress(sp, 0);
111
112                         /* And remove the RA from the stack. */
113
114                         sp = sp + 1 * SIZEOF_VOID_P;
115                 }
116         }
117         else {
118                 /* this was a normal NPE */
119
120                 type = EXCEPTION_HARDWARE_NULLPOINTER;
121                 val  = 0;
122         }
123
124         /* Handle the type. */
125
126         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
127
128         /* Set registers. */
129
130         if (type == EXCEPTION_HARDWARE_COMPILER) {
131                 if (p == NULL) {
132                         o = exceptions_get_and_clear_exception();
133
134                         ra = ra - 2;                     /* XPC is before the actual call */
135
136                         _mc->gregs[REG_ESP] = (uintptr_t) sp;    /* Remove RA from stack. */
137
138                         _mc->gregs[REG_EAX] = (uintptr_t) o;
139                         _mc->gregs[REG_ECX] = (uintptr_t) ra;            /* REG_ITMP2_XPC */
140                         _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
141                 }
142                 else {
143                         _mc->gregs[REG_EIP] = (uintptr_t) p;
144                 }
145         }
146         else {
147                 _mc->gregs[REG_EAX] = (intptr_t) p;
148                 _mc->gregs[REG_ECX] = (intptr_t) xpc;                /* REG_ITMP2_XPC */
149                 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
150         }
151 }
152
153
154 /* md_signal_handler_sigfpe ****************************************************
155
156    ArithmeticException signal handler for hardware divide by zero
157    check.
158
159 *******************************************************************************/
160
161 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
162 {
163         ucontext_t     *_uc;
164         mcontext_t     *_mc;
165         u1             *pv;
166         u1             *sp;
167         u1             *ra;
168         u1             *xpc;
169         s4              type;
170         ptrint          val;
171         void           *p;
172
173         _uc = (ucontext_t *) _p;
174         _mc = &_uc->uc_mcontext;
175
176         pv  = NULL;                 /* is resolved during stackframeinfo creation */
177         sp  = (u1 *) _mc->gregs[REG_ESP];
178         xpc = (u1 *) _mc->gregs[REG_EIP];
179         ra  = xpc;                          /* return address is equal to xpc     */
180
181         /* this is an ArithmeticException */
182
183         type = EXCEPTION_HARDWARE_ARITHMETIC;
184         val  = 0;
185
186         /* Handle the type. */
187
188         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
189
190         /* set registers */
191
192         _mc->gregs[REG_EAX] = (intptr_t) p;
193         _mc->gregs[REG_ECX] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
194         _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
195 }
196
197
198 /* md_signal_handler_sigill ****************************************************
199
200    Signal handler for hardware patcher traps (ud2).
201
202 *******************************************************************************/
203
204 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
205 {
206         ucontext_t        *_uc;
207         mcontext_t        *_mc;
208         u1                *pv;
209         u1                *sp;
210         u1                *ra;
211         u1                *xpc;
212         s4                 type;
213         ptrint             val;
214         void              *p;
215
216         _uc = (ucontext_t *) _p;
217         _mc = &_uc->uc_mcontext;
218
219         pv  = NULL;                 /* is resolved during stackframeinfo creation */
220         sp  = (u1 *) _mc->gregs[REG_ESP];
221         xpc = (u1 *) _mc->gregs[REG_EIP];
222         ra  = xpc;                            /* return address is equal to xpc   */
223
224         /* this is an ArithmeticException */
225
226         type = EXCEPTION_HARDWARE_PATCHER;
227         val  = 0;
228
229         /* generate appropriate exception */
230
231         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
232
233         /* set registers (only if exception object ready) */
234
235         if (p != NULL) {
236                 _mc->gregs[REG_EAX] = (ptrint) p;
237                 _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
238                 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
239         }
240 }
241
242
243 /* md_signal_handler_sigusr1 ***************************************************
244
245    Signal handler for suspending threads.
246
247 *******************************************************************************/
248
249 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
250 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
251 {
252         ucontext_t *_uc;
253         mcontext_t *_mc;
254         u1         *pc;
255         u1         *sp;
256
257         _uc = (ucontext_t *) _p;
258         _mc = &_uc->uc_mcontext;
259
260         /* get the PC and SP for this thread */
261         pc = (u1 *) _mc->gregs[REG_EIP];
262         sp = (u1 *) _mc->gregs[REG_ESP];
263
264         /* now suspend the current thread */
265         threads_suspend_ack(pc, sp);
266 }
267 #endif
268
269
270 /* md_signal_handler_sigusr2 ***************************************************
271
272    Signal handler for profiling sampling.
273
274 *******************************************************************************/
275
276 #if defined(ENABLE_THREADS)
277 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
278 {
279         threadobject *t;
280         ucontext_t   *_uc;
281         mcontext_t   *_mc;
282         u1           *pc;
283
284         t = THREADOBJECT;
285
286         _uc = (ucontext_t *) _p;
287         _mc = &_uc->uc_mcontext;
288
289         pc = (u1 *) _mc->gregs[REG_EIP];
290
291         t->pc = pc;
292 }
293 #endif
294
295
296 /* md_critical_section_restart *************************************************
297
298    Search the critical sections tree for a matching section and set
299    the PC to the restart point, if necessary.
300
301 *******************************************************************************/
302
303 #if defined(ENABLE_THREADS)
304 void md_critical_section_restart(ucontext_t *_uc)
305 {
306         mcontext_t *_mc;
307         u1         *pc;
308         u1         *npc;
309
310         _mc = &_uc->uc_mcontext;
311
312         pc = (u1 *) _mc->gregs[REG_EIP];
313
314         npc = critical_find_restart_point(pc);
315
316         if (npc != NULL)
317                 _mc->gregs[REG_EIP] = (ptrint) npc;
318 }
319 #endif
320
321
322 /*
323  * These are local overrides for various environment variables in Emacs.
324  * Please do not remove this and leave it at the end of the file, where
325  * Emacs will automagically detect them.
326  * ---------------------------------------------------------------------
327  * Local variables:
328  * mode: c
329  * indent-tabs-mode: t
330  * c-basic-offset: 4
331  * tab-width: 4
332  * End:
333  */