Merge from 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
72         _uc = (ucontext_t *) _p;
73         _mc = &_uc->uc_mcontext;
74
75         pv  = NULL;                 /* is resolved during stackframeinfo creation */
76         sp  = (u1 *) _mc->gregs[REG_ESP];
77         xpc = (u1 *) _mc->gregs[REG_EIP];
78         ra  = xpc;                              /* return address is equal to XPC */
79
80         /* get exception-throwing instruction */
81
82         opc = M_ALD_MEM_GET_OPC(xpc);
83         mod = M_ALD_MEM_GET_MOD(xpc);
84         rm  = M_ALD_MEM_GET_RM(xpc);
85
86         /* for values see emit_mov_mem_reg and emit_mem */
87
88         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
89                 /* this was a hardware-exception */
90
91                 d    = M_ALD_MEM_GET_REG(xpc);
92                 disp = M_ALD_MEM_GET_DISP(xpc);
93
94                 /* we use the exception type as load displacement */
95
96                 type = disp;
97
98                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
99                    registers are reversed starting with number 4 for REG_EDI
100                    (see /usr/include/sys/ucontext.h).  We have to convert that
101                    here. */
102
103                 val = _mc->gregs[REG_EAX - d];
104
105                 if (type == TRAP_COMPILER) {
106                         /* The PV from the compiler stub is equal to the XPC. */
107
108                         pv = xpc;
109
110                         /* We use a framesize of zero here because the call pushed
111                            the return addres onto the stack. */
112
113                         ra = md_stacktrace_get_returnaddress(sp, 0);
114
115                         /* Skip the RA on the stack. */
116
117                         sp = sp + 1 * SIZEOF_VOID_P;
118
119                         /* The XPC is the RA minus 2, because the RA points to the
120                            instruction after the call. */
121
122                         xpc = ra - 2;
123                 }
124         }
125         else {
126                 /* this was a normal NPE */
127
128                 type = TRAP_NullPointerException;
129                 val  = 0;
130         }
131
132         /* Handle the trap. */
133
134         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
135
136         /* Set registers. */
137
138         if (type == TRAP_COMPILER) {
139                 if (p == NULL) {
140                         _mc->gregs[REG_ESP] = (uintptr_t) sp;    /* Remove RA from stack. */
141                 }
142         }
143 }
144
145
146 /* md_signal_handler_sigfpe ****************************************************
147
148    ArithmeticException signal handler for hardware divide by zero
149    check.
150
151 *******************************************************************************/
152
153 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
154 {
155         ucontext_t     *_uc;
156         mcontext_t     *_mc;
157         u1             *pv;
158         u1             *sp;
159         u1             *ra;
160         u1             *xpc;
161         s4              type;
162         ptrint          val;
163
164         _uc = (ucontext_t *) _p;
165         _mc = &_uc->uc_mcontext;
166
167         pv  = NULL;                 /* is resolved during stackframeinfo creation */
168         sp  = (u1 *) _mc->gregs[REG_ESP];
169         xpc = (u1 *) _mc->gregs[REG_EIP];
170         ra  = xpc;                          /* return address is equal to xpc     */
171
172         /* This is an ArithmeticException. */
173
174         type = TRAP_ArithmeticException;
175         val  = 0;
176
177         /* Handle the trap. */
178
179         trap_handle(type, val, pv, sp, ra, xpc, _p);
180 }
181
182
183 /* md_signal_handler_sigill ****************************************************
184
185    Signal handler for hardware patcher traps (ud2).
186
187 *******************************************************************************/
188
189 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
190 {
191         ucontext_t        *_uc;
192         mcontext_t        *_mc;
193         u1                *pv;
194         u1                *sp;
195         u1                *ra;
196         u1                *xpc;
197         s4                 type;
198         ptrint             val;
199
200         _uc = (ucontext_t *) _p;
201         _mc = &_uc->uc_mcontext;
202
203         pv  = NULL;                 /* is resolved during stackframeinfo creation */
204         sp  = (u1 *) _mc->gregs[REG_ESP];
205         xpc = (u1 *) _mc->gregs[REG_EIP];
206         ra  = xpc;                            /* return address is equal to xpc   */
207
208         type = TRAP_PATCHER;
209         val  = 0;
210
211         /* Handle the trap. */
212
213         trap_handle(type, val, pv, sp, ra, xpc, _p);
214 }
215
216
217 /* md_signal_handler_sigusr1 ***************************************************
218
219    Signal handler for suspending threads.
220
221 *******************************************************************************/
222
223 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
224 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
225 {
226         ucontext_t *_uc;
227         mcontext_t *_mc;
228         u1         *pc;
229         u1         *sp;
230
231         _uc = (ucontext_t *) _p;
232         _mc = &_uc->uc_mcontext;
233
234         /* get the PC and SP for this thread */
235         pc = (u1 *) _mc->gregs[REG_EIP];
236         sp = (u1 *) _mc->gregs[REG_ESP];
237
238         /* now suspend the current thread */
239         threads_suspend_ack(pc, sp);
240 }
241 #endif
242
243
244 /* md_signal_handler_sigusr2 ***************************************************
245
246    Signal handler for profiling sampling.
247
248 *******************************************************************************/
249
250 #if defined(ENABLE_THREADS)
251 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
252 {
253         threadobject *t;
254         ucontext_t   *_uc;
255         mcontext_t   *_mc;
256         u1           *pc;
257
258         t = THREADOBJECT;
259
260         _uc = (ucontext_t *) _p;
261         _mc = &_uc->uc_mcontext;
262
263         pc = (u1 *) _mc->gregs[REG_EIP];
264
265         t->pc = pc;
266 }
267 #endif
268
269
270 /* md_executionstate_read ******************************************************
271
272    Read the given context into an executionstate for Replacement.
273
274 *******************************************************************************/
275
276 void md_executionstate_read(executionstate_t *es, void *context)
277 {
278         ucontext_t *_uc;
279         mcontext_t *_mc;
280         s4          i;
281
282         _uc = (ucontext_t *) context;
283         _mc = &_uc->uc_mcontext;
284
285         /* read special registers */
286         es->pc = (u1 *) _mc->gregs[REG_EIP];
287         es->sp = (u1 *) _mc->gregs[REG_ESP];
288         es->pv = NULL;                   /* pv must be looked up via AVL tree */
289
290         /* read integer registers */
291         for (i = 0; i < INT_REG_CNT; i++)
292                 es->intregs[i] = _mc->gregs[REG_EAX - i];
293
294         /* read float registers */
295         for (i = 0; i < FLT_REG_CNT; i++)
296                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
297 }
298
299
300 /* md_executionstate_write *****************************************************
301
302    Write the given executionstate back to the context for Replacement.
303
304 *******************************************************************************/
305
306 void md_executionstate_write(executionstate_t *es, void *context)
307 {
308         ucontext_t *_uc;
309         mcontext_t *_mc;
310         s4          i;
311
312         _uc = (ucontext_t *) context;
313         _mc = &_uc->uc_mcontext;
314
315         /* write integer registers */
316         for (i = 0; i < INT_REG_CNT; i++)
317                 _mc->gregs[REG_EAX - i] = es->intregs[i];
318
319         /* write special registers */
320         _mc->gregs[REG_EIP] = (ptrint) es->pc;
321         _mc->gregs[REG_ESP] = (ptrint) es->sp;
322 }
323
324
325 /*
326  * These are local overrides for various environment variables in Emacs.
327  * Please do not remove this and leave it at the end of the file, where
328  * Emacs will automagically detect them.
329  * ---------------------------------------------------------------------
330  * Local variables:
331  * mode: c
332  * indent-tabs-mode: t
333  * c-basic-offset: 4
334  * tab-width: 4
335  * End:
336  */