1f72d58d2ffca4f21a73fbd7508651312c9abb18
[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.hpp"
42
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/executionstate.h"
45 #include "vm/jit/stacktrace.hpp"
46 #include "vm/jit/trap.hpp"
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         // Check if the trap instruction is valid.
209         // TODO Move this into patcher_handler.
210         if (patcher_is_valid_trap_instruction_at(xpc) == false) {
211                 // Check if the PC has been patched during our way to this
212                 // signal handler (see PR85).
213                 if (patcher_is_patched_at(xpc) == true)
214                         return;
215
216                 // We have a problem...
217                 log_println("md_signal_handler_sigill: Unknown illegal instruction at 0x%lx", xpc);
218 #if defined(ENABLE_DISASSEMBLER)
219                 (void) disassinstr(xpc);
220 #endif
221                 vm_abort("Aborting...");
222         }
223
224         type = TRAP_PATCHER;
225         val  = 0;
226
227         /* Handle the trap. */
228
229         trap_handle(type, val, pv, sp, ra, xpc, _p);
230 }
231
232
233 /* md_signal_handler_sigusr1 ***************************************************
234
235    Signal handler for suspending threads.
236
237 *******************************************************************************/
238
239 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
240 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
241 {
242         ucontext_t *_uc;
243         mcontext_t *_mc;
244         u1         *pc;
245         u1         *sp;
246
247         _uc = (ucontext_t *) _p;
248         _mc = &_uc->uc_mcontext;
249
250         /* get the PC and SP for this thread */
251         pc = (u1 *) _mc->gregs[REG_EIP];
252         sp = (u1 *) _mc->gregs[REG_ESP];
253
254         /* now suspend the current thread */
255         threads_suspend_ack(pc, sp);
256 }
257 #endif
258
259
260 /* md_signal_handler_sigusr2 ***************************************************
261
262    Signal handler for profiling sampling.
263
264 *******************************************************************************/
265
266 #if defined(ENABLE_THREADS)
267 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
268 {
269         threadobject *t;
270         ucontext_t   *_uc;
271         mcontext_t   *_mc;
272         u1           *pc;
273
274         t = THREADOBJECT;
275
276         _uc = (ucontext_t *) _p;
277         _mc = &_uc->uc_mcontext;
278
279         pc = (u1 *) _mc->gregs[REG_EIP];
280
281         t->pc = pc;
282 }
283 #endif
284
285
286 /* md_executionstate_read ******************************************************
287
288    Read the given context into an executionstate for Replacement.
289
290 *******************************************************************************/
291
292 void md_executionstate_read(executionstate_t *es, void *context)
293 {
294         ucontext_t *_uc;
295         mcontext_t *_mc;
296         s4          i;
297
298         _uc = (ucontext_t *) context;
299         _mc = &_uc->uc_mcontext;
300
301         /* read special registers */
302         es->pc = (u1 *) _mc->gregs[REG_EIP];
303         es->sp = (u1 *) _mc->gregs[REG_ESP];
304         es->pv = NULL;                   /* pv must be looked up via AVL tree */
305
306         /* read integer registers */
307         for (i = 0; i < INT_REG_CNT; i++)
308                 es->intregs[i] = _mc->gregs[REG_EAX - i];
309
310         /* read float registers */
311         for (i = 0; i < FLT_REG_CNT; i++)
312                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
313 }
314
315
316 /* md_executionstate_write *****************************************************
317
318    Write the given executionstate back to the context for Replacement.
319
320 *******************************************************************************/
321
322 void md_executionstate_write(executionstate_t *es, void *context)
323 {
324         ucontext_t *_uc;
325         mcontext_t *_mc;
326         s4          i;
327
328         _uc = (ucontext_t *) context;
329         _mc = &_uc->uc_mcontext;
330
331         /* write integer registers */
332         for (i = 0; i < INT_REG_CNT; i++)
333                 _mc->gregs[REG_EAX - i] = es->intregs[i];
334
335         /* write special registers */
336         _mc->gregs[REG_EIP] = (ptrint) es->pc;
337         _mc->gregs[REG_ESP] = (ptrint) es->sp;
338 }
339
340
341 /*
342  * These are local overrides for various environment variables in Emacs.
343  * Please do not remove this and leave it at the end of the file, where
344  * Emacs will automagically detect them.
345  * ---------------------------------------------------------------------
346  * Local variables:
347  * mode: c
348  * indent-tabs-mode: t
349  * c-basic-offset: 4
350  * tab-width: 4
351  * End:
352  */