Merged trunk and subtype.
[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.hpp"
39
40 #include "vm/jit/builtin.hpp"
41 #include "vm/signallocal.h"
42
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/executionstate.h"
45 #include "vm/jit/stacktrace.hpp"
46 #include "vm/jit/trap.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 == TRAP_COMPILER) {
107                         /* The PV from the compiler stub is equal to the XPC. */
108
109                         pv = xpc;
110
111                         /* We use a framesize of zero here because the call pushed
112                            the return addres onto the stack. */
113
114                         ra = md_stacktrace_get_returnaddress(sp, 0);
115
116                         /* Skip the RA on the stack. */
117
118                         sp = sp + 1 * SIZEOF_VOID_P;
119
120                         /* The XPC is the RA minus 2, because the RA points to the
121                            instruction after the call. */
122
123                         xpc = ra - 2;
124                 }
125         }
126         else {
127                 /* this was a normal NPE */
128
129                 type = TRAP_NullPointerException;
130                 val  = 0;
131         }
132
133         /* Handle the trap. */
134
135         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
136
137         /* Set registers. */
138
139         if (type == TRAP_COMPILER) {
140                 if (p == NULL) {
141                         o = builtin_retrieve_exception();
142
143                         _mc->gregs[REG_ESP] = (uintptr_t) sp;    /* Remove RA from stack. */
144
145                         _mc->gregs[REG_EAX] = (uintptr_t) o;
146                         _mc->gregs[REG_ECX] = (uintptr_t) xpc;           /* REG_ITMP2_XPC */
147                         _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
148                 }
149                 else {
150                         _mc->gregs[REG_EIP] = (uintptr_t) p;
151                 }
152         }
153 #if defined(ENABLE_REPLACEMENT)
154         else if (type == TRAP_COUNTDOWN) {
155                 /* context has been written by md_replace_executionstate_write */
156         }
157 #endif
158         else {
159                 _mc->gregs[REG_EAX] = (uintptr_t) p;
160                 _mc->gregs[REG_ECX] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
161                 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
162         }
163 }
164
165
166 /* md_signal_handler_sigfpe ****************************************************
167
168    ArithmeticException signal handler for hardware divide by zero
169    check.
170
171 *******************************************************************************/
172
173 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
174 {
175         ucontext_t     *_uc;
176         mcontext_t     *_mc;
177         u1             *pv;
178         u1             *sp;
179         u1             *ra;
180         u1             *xpc;
181         s4              type;
182         ptrint          val;
183         void           *p;
184
185         _uc = (ucontext_t *) _p;
186         _mc = &_uc->uc_mcontext;
187
188         pv  = NULL;                 /* is resolved during stackframeinfo creation */
189         sp  = (u1 *) _mc->gregs[REG_ESP];
190         xpc = (u1 *) _mc->gregs[REG_EIP];
191         ra  = xpc;                          /* return address is equal to xpc     */
192
193         /* This is an ArithmeticException. */
194
195         type = TRAP_ArithmeticException;
196         val  = 0;
197
198         /* Handle the trap. */
199
200         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
201
202         /* Set registers. */
203
204         _mc->gregs[REG_EAX] = (uintptr_t) p;
205         _mc->gregs[REG_ECX] = (uintptr_t) xpc;                   /* REG_ITMP2_XPC */
206         _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
207 }
208
209
210 /* md_signal_handler_sigill ****************************************************
211
212    Signal handler for hardware patcher traps (ud2).
213
214 *******************************************************************************/
215
216 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
217 {
218         ucontext_t        *_uc;
219         mcontext_t        *_mc;
220         u1                *pv;
221         u1                *sp;
222         u1                *ra;
223         u1                *xpc;
224         s4                 type;
225         ptrint             val;
226         void              *p;
227
228         _uc = (ucontext_t *) _p;
229         _mc = &_uc->uc_mcontext;
230
231         pv  = NULL;                 /* is resolved during stackframeinfo creation */
232         sp  = (u1 *) _mc->gregs[REG_ESP];
233         xpc = (u1 *) _mc->gregs[REG_EIP];
234         ra  = xpc;                            /* return address is equal to xpc   */
235
236         type = TRAP_PATCHER;
237         val  = 0;
238
239         /* Handle the trap. */
240
241         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
242
243         /* Set registers. */
244
245         if (p != NULL) {
246                 _mc->gregs[REG_EAX] = (uintptr_t) p;
247                 _mc->gregs[REG_ECX] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
248                 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
249         }
250 }
251
252
253 /* md_signal_handler_sigusr1 ***************************************************
254
255    Signal handler for suspending threads.
256
257 *******************************************************************************/
258
259 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
260 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
261 {
262         ucontext_t *_uc;
263         mcontext_t *_mc;
264         u1         *pc;
265         u1         *sp;
266
267         _uc = (ucontext_t *) _p;
268         _mc = &_uc->uc_mcontext;
269
270         /* get the PC and SP for this thread */
271         pc = (u1 *) _mc->gregs[REG_EIP];
272         sp = (u1 *) _mc->gregs[REG_ESP];
273
274         /* now suspend the current thread */
275         threads_suspend_ack(pc, sp);
276 }
277 #endif
278
279
280 /* md_signal_handler_sigusr2 ***************************************************
281
282    Signal handler for profiling sampling.
283
284 *******************************************************************************/
285
286 #if defined(ENABLE_THREADS)
287 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
288 {
289         threadobject *t;
290         ucontext_t   *_uc;
291         mcontext_t   *_mc;
292         u1           *pc;
293
294         t = THREADOBJECT;
295
296         _uc = (ucontext_t *) _p;
297         _mc = &_uc->uc_mcontext;
298
299         pc = (u1 *) _mc->gregs[REG_EIP];
300
301         t->pc = pc;
302 }
303 #endif
304
305
306 /* md_executionstate_read ******************************************************
307
308    Read the given context into an executionstate for Replacement.
309
310 *******************************************************************************/
311
312 void md_executionstate_read(executionstate_t *es, void *context)
313 {
314         ucontext_t *_uc;
315         mcontext_t *_mc;
316         s4          i;
317
318         _uc = (ucontext_t *) context;
319         _mc = &_uc->uc_mcontext;
320
321         /* read special registers */
322         es->pc = (u1 *) _mc->gregs[REG_EIP];
323         es->sp = (u1 *) _mc->gregs[REG_ESP];
324         es->pv = NULL;                   /* pv must be looked up via AVL tree */
325
326         /* read integer registers */
327         for (i = 0; i < INT_REG_CNT; i++)
328                 es->intregs[i] = _mc->gregs[REG_EAX - i];
329
330         /* read float registers */
331         for (i = 0; i < FLT_REG_CNT; i++)
332                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
333 }
334
335
336 /* md_executionstate_write *****************************************************
337
338    Write the given executionstate back to the context for Replacement.
339
340 *******************************************************************************/
341
342 void md_executionstate_write(executionstate_t *es, void *context)
343 {
344         ucontext_t *_uc;
345         mcontext_t *_mc;
346         s4          i;
347
348         _uc = (ucontext_t *) context;
349         _mc = &_uc->uc_mcontext;
350
351         /* write integer registers */
352         for (i = 0; i < INT_REG_CNT; i++)
353                 _mc->gregs[REG_EAX - i] = es->intregs[i];
354
355         /* write special registers */
356         _mc->gregs[REG_EIP] = (ptrint) es->pc;
357         _mc->gregs[REG_ESP] = (ptrint) es->sp;
358 }
359
360
361 /*
362  * These are local overrides for various environment variables in Emacs.
363  * Please do not remove this and leave it at the end of the file, where
364  * Emacs will automagically detect them.
365  * ---------------------------------------------------------------------
366  * Local variables:
367  * mode: c
368  * indent-tabs-mode: t
369  * c-basic-offset: 4
370  * tab-width: 4
371  * End:
372  */