Merged with tip.
[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 #if defined(ENABLE_REPLACEMENT)
155         else if (type == TRAP_COUNTDOWN) {
156                 /* context has been written by md_replace_executionstate_write */
157         }
158 #endif
159         else {
160                 _mc->gregs[REG_EAX] = (uintptr_t) p;
161                 _mc->gregs[REG_ECX] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
162                 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
163         }
164 }
165
166
167 /* md_signal_handler_sigfpe ****************************************************
168
169    ArithmeticException signal handler for hardware divide by zero
170    check.
171
172 *******************************************************************************/
173
174 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
175 {
176         ucontext_t     *_uc;
177         mcontext_t     *_mc;
178         u1             *pv;
179         u1             *sp;
180         u1             *ra;
181         u1             *xpc;
182         s4              type;
183         ptrint          val;
184         void           *p;
185
186         _uc = (ucontext_t *) _p;
187         _mc = &_uc->uc_mcontext;
188
189         pv  = NULL;                 /* is resolved during stackframeinfo creation */
190         sp  = (u1 *) _mc->gregs[REG_ESP];
191         xpc = (u1 *) _mc->gregs[REG_EIP];
192         ra  = xpc;                          /* return address is equal to xpc     */
193
194         /* This is an ArithmeticException. */
195
196         type = TRAP_ArithmeticException;
197         val  = 0;
198
199         /* Handle the trap. */
200
201         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
202
203         /* Set registers. */
204
205         _mc->gregs[REG_EAX] = (uintptr_t) p;
206         _mc->gregs[REG_ECX] = (uintptr_t) xpc;                   /* REG_ITMP2_XPC */
207         _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
208 }
209
210
211 /* md_signal_handler_sigill ****************************************************
212
213    Signal handler for hardware patcher traps (ud2).
214
215 *******************************************************************************/
216
217 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
218 {
219         ucontext_t        *_uc;
220         mcontext_t        *_mc;
221         u1                *pv;
222         u1                *sp;
223         u1                *ra;
224         u1                *xpc;
225         s4                 type;
226         ptrint             val;
227         void              *p;
228
229         _uc = (ucontext_t *) _p;
230         _mc = &_uc->uc_mcontext;
231
232         pv  = NULL;                 /* is resolved during stackframeinfo creation */
233         sp  = (u1 *) _mc->gregs[REG_ESP];
234         xpc = (u1 *) _mc->gregs[REG_EIP];
235         ra  = xpc;                            /* return address is equal to xpc   */
236
237         type = TRAP_PATCHER;
238         val  = 0;
239
240         /* Handle the trap. */
241
242         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
243
244         /* Set registers. */
245
246         if (p != NULL) {
247                 _mc->gregs[REG_EAX] = (uintptr_t) p;
248                 _mc->gregs[REG_ECX] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
249                 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
250         }
251 }
252
253
254 /* md_signal_handler_sigusr1 ***************************************************
255
256    Signal handler for suspending threads.
257
258 *******************************************************************************/
259
260 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
261 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
262 {
263         ucontext_t *_uc;
264         mcontext_t *_mc;
265         u1         *pc;
266         u1         *sp;
267
268         _uc = (ucontext_t *) _p;
269         _mc = &_uc->uc_mcontext;
270
271         /* get the PC and SP for this thread */
272         pc = (u1 *) _mc->gregs[REG_EIP];
273         sp = (u1 *) _mc->gregs[REG_ESP];
274
275         /* now suspend the current thread */
276         threads_suspend_ack(pc, sp);
277 }
278 #endif
279
280
281 /* md_signal_handler_sigusr2 ***************************************************
282
283    Signal handler for profiling sampling.
284
285 *******************************************************************************/
286
287 #if defined(ENABLE_THREADS)
288 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
289 {
290         threadobject *t;
291         ucontext_t   *_uc;
292         mcontext_t   *_mc;
293         u1           *pc;
294
295         t = THREADOBJECT;
296
297         _uc = (ucontext_t *) _p;
298         _mc = &_uc->uc_mcontext;
299
300         pc = (u1 *) _mc->gregs[REG_EIP];
301
302         t->pc = pc;
303 }
304 #endif
305
306
307 /* md_executionstate_read ******************************************************
308
309    Read the given context into an executionstate for Replacement.
310
311 *******************************************************************************/
312
313 void md_executionstate_read(executionstate_t *es, void *context)
314 {
315         ucontext_t *_uc;
316         mcontext_t *_mc;
317         s4          i;
318
319         _uc = (ucontext_t *) context;
320         _mc = &_uc->uc_mcontext;
321
322         /* read special registers */
323         es->pc = (u1 *) _mc->gregs[REG_EIP];
324         es->sp = (u1 *) _mc->gregs[REG_ESP];
325         es->pv = NULL;                   /* pv must be looked up via AVL tree */
326
327         /* read integer registers */
328         for (i = 0; i < INT_REG_CNT; i++)
329                 es->intregs[i] = _mc->gregs[REG_EAX - i];
330
331         /* read float registers */
332         for (i = 0; i < FLT_REG_CNT; i++)
333                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
334 }
335
336
337 /* md_executionstate_write *****************************************************
338
339    Write the given executionstate back to the context for Replacement.
340
341 *******************************************************************************/
342
343 void md_executionstate_write(executionstate_t *es, void *context)
344 {
345         ucontext_t *_uc;
346         mcontext_t *_mc;
347         s4          i;
348
349         _uc = (ucontext_t *) context;
350         _mc = &_uc->uc_mcontext;
351
352         /* write integer registers */
353         for (i = 0; i < INT_REG_CNT; i++)
354                 _mc->gregs[REG_EAX - i] = es->intregs[i];
355
356         /* write special registers */
357         _mc->gregs[REG_EIP] = (ptrint) es->pc;
358         _mc->gregs[REG_ESP] = (ptrint) es->sp;
359 }
360
361
362 /*
363  * These are local overrides for various environment variables in Emacs.
364  * Please do not remove this and leave it at the end of the file, where
365  * Emacs will automagically detect them.
366  * ---------------------------------------------------------------------
367  * Local variables:
368  * mode: c
369  * indent-tabs-mode: t
370  * c-basic-offset: 4
371  * tab-width: 4
372  * End:
373  */