* src/threads/critical.c: Removed.
[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, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #define _GNU_SOURCE                   /* include REG_ defines from ucontext.h */
27
28 #include "config.h"
29
30 #include <stdint.h>
31 #include <ucontext.h>
32
33 #include "vm/types.h"
34
35 #include "vm/jit/i386/codegen.h"
36 #include "vm/jit/i386/md.h"
37
38 #include "threads/thread.h"
39
40 #include "vm/builtin.h"
41 #include "vm/signallocal.h"
42 #include "vm/stringlocal.h"
43
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/stacktrace.h"
47 #include "vm/jit/trap.h"
48
49
50 /* md_signal_handler_sigsegv ***************************************************
51
52    Signal handler for hardware exceptions.
53
54 *******************************************************************************/
55
56 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
57 {
58         ucontext_t     *_uc;
59         mcontext_t     *_mc;
60         u1             *pv;
61         u1             *sp;
62         u1             *ra;
63         u1             *xpc;
64         u1              opc;
65         u1              mod;
66         u1              rm;
67         s4              d;
68         s4              disp;
69         ptrint          val;
70         s4              type;
71         void           *p;
72         java_object_t  *o;
73
74         _uc = (ucontext_t *) _p;
75         _mc = &_uc->uc_mcontext;
76
77         pv  = NULL;                 /* is resolved during stackframeinfo creation */
78         sp  = (u1 *) _mc->gregs[REG_ESP];
79         xpc = (u1 *) _mc->gregs[REG_EIP];
80         ra  = xpc;                              /* return address is equal to XPC */
81
82         /* get exception-throwing instruction */
83
84         opc = M_ALD_MEM_GET_OPC(xpc);
85         mod = M_ALD_MEM_GET_MOD(xpc);
86         rm  = M_ALD_MEM_GET_RM(xpc);
87
88         /* for values see emit_mov_mem_reg and emit_mem */
89
90         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
91                 /* this was a hardware-exception */
92
93                 d    = M_ALD_MEM_GET_REG(xpc);
94                 disp = M_ALD_MEM_GET_DISP(xpc);
95
96                 /* we use the exception type as load displacement */
97
98                 type = disp;
99
100                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
101                    registers are reversed starting with number 4 for REG_EDI
102                    (see /usr/include/sys/ucontext.h).  We have to convert that
103                    here. */
104
105                 val = _mc->gregs[REG_EAX - d];
106
107                 if (type == TRAP_COMPILER) {
108                         /* The PV from the compiler stub is equal to the XPC. */
109
110                         pv = xpc;
111
112                         /* We use a framesize of zero here because the call pushed
113                            the return addres onto the stack. */
114
115                         ra = md_stacktrace_get_returnaddress(sp, 0);
116
117                         /* Skip the RA on the stack. */
118
119                         sp = sp + 1 * SIZEOF_VOID_P;
120
121                         /* The XPC is the RA minus 2, because the RA points to the
122                            instruction after the call. */
123
124                         xpc = ra - 2;
125                 }
126         }
127         else {
128                 /* this was a normal NPE */
129
130                 type = TRAP_NullPointerException;
131                 val  = 0;
132         }
133
134         /* Handle the trap. */
135
136         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
137
138         /* Set registers. */
139
140         if (type == TRAP_COMPILER) {
141                 if (p == NULL) {
142                         o = builtin_retrieve_exception();
143
144                         _mc->gregs[REG_ESP] = (uintptr_t) sp;    /* Remove RA from stack. */
145
146                         _mc->gregs[REG_EAX] = (uintptr_t) o;
147                         _mc->gregs[REG_ECX] = (uintptr_t) xpc;           /* REG_ITMP2_XPC */
148                         _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
149                 }
150                 else {
151                         _mc->gregs[REG_EIP] = (uintptr_t) p;
152                 }
153         }
154         else {
155                 _mc->gregs[REG_EAX] = (uintptr_t) p;
156                 _mc->gregs[REG_ECX] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
157                 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
158         }
159 }
160
161
162 /* md_signal_handler_sigfpe ****************************************************
163
164    ArithmeticException signal handler for hardware divide by zero
165    check.
166
167 *******************************************************************************/
168
169 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
170 {
171         ucontext_t     *_uc;
172         mcontext_t     *_mc;
173         u1             *pv;
174         u1             *sp;
175         u1             *ra;
176         u1             *xpc;
177         s4              type;
178         ptrint          val;
179         void           *p;
180
181         _uc = (ucontext_t *) _p;
182         _mc = &_uc->uc_mcontext;
183
184         pv  = NULL;                 /* is resolved during stackframeinfo creation */
185         sp  = (u1 *) _mc->gregs[REG_ESP];
186         xpc = (u1 *) _mc->gregs[REG_EIP];
187         ra  = xpc;                          /* return address is equal to xpc     */
188
189         /* This is an ArithmeticException. */
190
191         type = TRAP_ArithmeticException;
192         val  = 0;
193
194         /* Handle the trap. */
195
196         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
197
198         /* Set registers. */
199
200         _mc->gregs[REG_EAX] = (uintptr_t) p;
201         _mc->gregs[REG_ECX] = (uintptr_t) xpc;                   /* REG_ITMP2_XPC */
202         _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
203 }
204
205
206 /* md_signal_handler_sigill ****************************************************
207
208    Signal handler for hardware patcher traps (ud2).
209
210 *******************************************************************************/
211
212 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
213 {
214         ucontext_t        *_uc;
215         mcontext_t        *_mc;
216         u1                *pv;
217         u1                *sp;
218         u1                *ra;
219         u1                *xpc;
220         s4                 type;
221         ptrint             val;
222         void              *p;
223
224         _uc = (ucontext_t *) _p;
225         _mc = &_uc->uc_mcontext;
226
227         pv  = NULL;                 /* is resolved during stackframeinfo creation */
228         sp  = (u1 *) _mc->gregs[REG_ESP];
229         xpc = (u1 *) _mc->gregs[REG_EIP];
230         ra  = xpc;                            /* return address is equal to xpc   */
231
232         type = TRAP_PATCHER;
233         val  = 0;
234
235         /* Handle the trap. */
236
237         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
238
239         /* Set registers. */
240
241         if (p != NULL) {
242                 _mc->gregs[REG_EAX] = (uintptr_t) p;
243                 _mc->gregs[REG_ECX] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
244                 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
245         }
246 }
247
248
249 /* md_signal_handler_sigusr1 ***************************************************
250
251    Signal handler for suspending threads.
252
253 *******************************************************************************/
254
255 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
256 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
257 {
258         ucontext_t *_uc;
259         mcontext_t *_mc;
260         u1         *pc;
261         u1         *sp;
262
263         _uc = (ucontext_t *) _p;
264         _mc = &_uc->uc_mcontext;
265
266         /* get the PC and SP for this thread */
267         pc = (u1 *) _mc->gregs[REG_EIP];
268         sp = (u1 *) _mc->gregs[REG_ESP];
269
270         /* now suspend the current thread */
271         threads_suspend_ack(pc, sp);
272 }
273 #endif
274
275
276 /* md_signal_handler_sigusr2 ***************************************************
277
278    Signal handler for profiling sampling.
279
280 *******************************************************************************/
281
282 #if defined(ENABLE_THREADS)
283 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
284 {
285         threadobject *t;
286         ucontext_t   *_uc;
287         mcontext_t   *_mc;
288         u1           *pc;
289
290         t = THREADOBJECT;
291
292         _uc = (ucontext_t *) _p;
293         _mc = &_uc->uc_mcontext;
294
295         pc = (u1 *) _mc->gregs[REG_EIP];
296
297         t->pc = pc;
298 }
299 #endif
300
301
302 /* md_executionstate_read ******************************************************
303
304    Read the given context into an executionstate for Replacement.
305
306 *******************************************************************************/
307
308 void md_executionstate_read(executionstate_t *es, void *context)
309 {
310         ucontext_t *_uc;
311         mcontext_t *_mc;
312         s4          i;
313
314         _uc = (ucontext_t *) context;
315         _mc = &_uc->uc_mcontext;
316
317         /* read special registers */
318         es->pc = (u1 *) _mc->gregs[REG_EIP];
319         es->sp = (u1 *) _mc->gregs[REG_ESP];
320         es->pv = NULL;                   /* pv must be looked up via AVL tree */
321
322         /* read integer registers */
323         for (i = 0; i < INT_REG_CNT; i++)
324                 es->intregs[i] = _mc->gregs[REG_EAX - i];
325
326         /* read float registers */
327         for (i = 0; i < FLT_REG_CNT; i++)
328                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
329 }
330
331
332 /* md_executionstate_write *****************************************************
333
334    Write the given executionstate back to the context for Replacement.
335
336 *******************************************************************************/
337
338 void md_executionstate_write(executionstate_t *es, void *context)
339 {
340         ucontext_t *_uc;
341         mcontext_t *_mc;
342         s4          i;
343
344         _uc = (ucontext_t *) context;
345         _mc = &_uc->uc_mcontext;
346
347         /* write integer registers */
348         for (i = 0; i < INT_REG_CNT; i++)
349                 _mc->gregs[REG_EAX - i] = es->intregs[i];
350
351         /* write special registers */
352         _mc->gregs[REG_EIP] = (ptrint) es->pc;
353         _mc->gregs[REG_ESP] = (ptrint) es->sp;
354 }
355
356
357 /*
358  * These are local overrides for various environment variables in Emacs.
359  * Please do not remove this and leave it at the end of the file, where
360  * Emacs will automagically detect them.
361  * ---------------------------------------------------------------------
362  * Local variables:
363  * mode: c
364  * indent-tabs-mode: t
365  * c-basic-offset: 4
366  * tab-width: 4
367  * End:
368  */