* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / src / vm / jit / i386 / freebsd / md-os.c
1 /* src/vm/jit/i386/freebsd/md-os.c - machine dependent i386 FreeBSD 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 #include "config.h"
27
28 #include <stdint.h>
29 #include <ucontext.h>
30
31 #include "vm/types.h"
32
33 #include "vm/jit/i386/codegen.h"
34 #include "vm/jit/i386/md.h"
35
36 #include "threads/thread.hpp"
37
38 #include "vm/jit/builtin.hpp"
39 #include "vm/signallocal.hpp"
40
41 #include "vm/jit/asmpart.h"
42 #include "vm/jit/executionstate.h"
43 #include "vm/jit/stacktrace.hpp"
44 #include "vm/jit/trap.hpp"
45
46
47 /* md_signal_handler_sigsegv ***************************************************
48
49    Signal handler for hardware exceptions.
50
51 *******************************************************************************/
52
53 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
54 {
55         ucontext_t     *_uc;
56         mcontext_t     *_mc;
57         u1             *pv;
58         u1             *sp;
59         u1             *ra;
60         u1             *xpc;
61         u1              opc;
62         u1              mod;
63         u1              rm;
64         s4              d;
65         s4              disp;
66         ptrint          val;
67         s4              type;
68         void           *p;
69
70         _uc = (ucontext_t *) _p;
71         _mc = &_uc->uc_mcontext;
72
73         pv  = NULL;                 /* is resolved during stackframeinfo creation */
74         sp  = (u1 *) _mc->mc_esp;
75         xpc = (u1 *) _mc->mc_eip;
76         ra  = xpc;                              /* return address is equal to XPC */
77
78         /* get exception-throwing instruction */
79
80         opc = M_ALD_MEM_GET_OPC(xpc);
81         mod = M_ALD_MEM_GET_MOD(xpc);
82         rm  = M_ALD_MEM_GET_RM(xpc);
83
84         /* for values see emit_mov_mem_reg and emit_mem */
85
86         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
87                 /* this was a hardware-exception */
88
89                 d    = M_ALD_MEM_GET_REG(xpc);
90                 disp = M_ALD_MEM_GET_DISP(xpc);
91
92                 /* we use the exception type as load displacement */
93
94                 type = disp;
95
96                 switch (d) {
97                 case EAX:
98                         val = _mc->mc_eax;
99                         break;
100                 case ECX:
101                         val = _mc->mc_ecx;
102                         break;
103                 case EDX:
104                         val = _mc->mc_edx;
105                         break;
106                 case EBX:
107                         val = _mc->mc_ebx;
108                         break;
109                 case ESP:
110                         val = _mc->mc_esp;
111                         break;
112                 case EBP:
113                         val = _mc->mc_ebp;
114                         break;
115                 case ESI:
116                         val = _mc->mc_esi;
117                         break;
118                 case EDI:
119                         val = _mc->mc_edi;
120                         break;
121                 default:
122                         vm_abort("md_signal_handler_sigsegv: Unkown register %d", d);
123                 }
124
125                 if (type == TRAP_COMPILER) {
126                         /* The PV from the compiler stub is equal to the XPC. */
127
128                         pv = xpc;
129
130                         /* We use a framesize of zero here because the call pushed
131                            the return addres onto the stack. */
132
133                         ra = md_stacktrace_get_returnaddress(sp, 0);
134
135                         /* Skip the RA on the stack. */
136
137                         sp = sp + 1 * SIZEOF_VOID_P;
138
139                         /* The XPC is the RA minus 2, because the RA points to the
140                            instruction after the call. */
141
142                         xpc = ra - 2;
143                 }
144         }
145         else {
146                 /* this was a normal NPE */
147
148                 type = TRAP_NullPointerException;
149                 val  = 0;
150         }
151
152         /* Handle the trap. */
153
154         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
155
156         /* Set registers. */
157
158         if (type == TRAP_COMPILER) {
159                 if (p == NULL) {
160                         _mc->mc_esp = (uintptr_t) sp; // Remove RA from stack.
161                 }
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
184         _uc = (ucontext_t *) _p;
185         _mc = &_uc->uc_mcontext;
186
187         pv  = NULL;                 /* is resolved during stackframeinfo creation */
188         sp  = (u1 *) _mc->mc_esp;
189         xpc = (u1 *) _mc->mc_eip;
190         ra  = xpc;                          /* return address is equal to xpc     */
191
192         /* This is an ArithmeticException. */
193
194         type = TRAP_ArithmeticException;
195         val  = 0;
196
197         /* Handle the trap. */
198
199         trap_handle(type, val, pv, sp, ra, xpc, _p);
200 }
201
202
203 /* md_signal_handler_sigill ****************************************************
204
205    Signal handler for hardware patcher traps (ud2).
206
207 *******************************************************************************/
208
209 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
210 {
211         ucontext_t        *_uc;
212         mcontext_t        *_mc;
213         u1                *pv;
214         u1                *sp;
215         u1                *ra;
216         u1                *xpc;
217         s4                 type;
218         ptrint             val;
219
220         _uc = (ucontext_t *) _p;
221         _mc = &_uc->uc_mcontext;
222
223         pv  = NULL;                 /* is resolved during stackframeinfo creation */
224         sp  = (u1 *) _mc->mc_esp;
225         xpc = (u1 *) _mc->mc_eip;
226         ra  = xpc;                            /* return address is equal to xpc   */
227
228         // Check if the trap instruction is valid.
229         // TODO Move this into patcher_handler.
230         if (patcher_is_valid_trap_instruction_at(xpc) == false) {
231                 // Check if the PC has been patched during our way to this
232                 // signal handler (see PR85).
233                 if (patcher_is_patched_at(xpc) == true)
234                         return;
235
236                 // We have a problem...
237                 log_println("md_signal_handler_sigill: Unknown illegal instruction at 0x%lx", xpc);
238 #if defined(ENABLE_DISASSEMBLER)
239                 (void) disassinstr(xpc);
240 #endif
241                 vm_abort("Aborting...");
242         }
243
244         type = TRAP_PATCHER;
245         val  = 0;
246
247         /* Handle the trap. */
248
249         trap_handle(type, val, pv, sp, ra, xpc, _p);
250 }
251
252
253 /* md_signal_handler_sigusr2 ***************************************************
254
255    Signal handler for profiling sampling.
256
257 *******************************************************************************/
258
259 #if defined(ENABLE_THREADS)
260 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
261 {
262         threadobject *t;
263         ucontext_t   *_uc;
264         mcontext_t   *_mc;
265         u1           *pc;
266
267         t = THREADOBJECT;
268
269         _uc = (ucontext_t *) _p;
270         _mc = &_uc->uc_mcontext;
271
272         pc = (u1 *) _mc->mc_eip;
273
274         t->pc = pc;
275 }
276 #endif
277
278
279 /* md_executionstate_read ******************************************************
280
281    Read the given context into an executionstate for Replacement.
282
283 *******************************************************************************/
284
285 void md_executionstate_read(executionstate_t* es, void* context)
286 {
287         ucontext_t* _uc = (ucontext_t*) context;
288         mcontext_t* _mc = &_uc->uc_mcontext;
289
290         // Read special registers.
291         es->pc = (u1 *) _mc->mc_eip;
292         es->sp = (u1 *) _mc->mc_esp;
293         es->pv = NULL;                   /* pv must be looked up via AVL tree */
294
295         // Read integer registers.
296         es->intregs[EAX] = _mc->mc_eax;
297         es->intregs[ECX] = _mc->mc_ecx;
298         es->intregs[EDX] = _mc->mc_edx;
299         es->intregs[EBX] = _mc->mc_ebx;
300         es->intregs[ESP] = _mc->mc_esp;
301         es->intregs[EBP] = _mc->mc_ebp;
302         es->intregs[ESI] = _mc->mc_esi;
303         es->intregs[EDI] = _mc->mc_edi;
304
305         // Read float registers.
306         for (int i = 0; i < FLT_REG_CNT; i++)
307                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
308 }
309
310
311 /* md_executionstate_write *****************************************************
312
313    Write the given executionstate back to the context for Replacement.
314
315 *******************************************************************************/
316
317 void md_executionstate_write(executionstate_t* es, void* context)
318 {
319         ucontext_t* _uc = (ucontext_t*) context;
320         mcontext_t* _mc = &_uc->uc_mcontext;
321
322         // Write integer registers.
323         _mc->mc_eax = es->intregs[EAX];
324         _mc->mc_ecx = es->intregs[ECX];
325         _mc->mc_edx = es->intregs[EDX];
326         _mc->mc_ebx = es->intregs[EBX];
327         _mc->mc_esp = es->intregs[ESP];
328         _mc->mc_ebp = es->intregs[EBP];
329         _mc->mc_esi = es->intregs[ESI];
330         _mc->mc_edi = es->intregs[EDI];
331
332         // Write special registers.
333         _mc->mc_eip = (uintptr_t) es->pc;
334         _mc->mc_esp = (uintptr_t) es->sp;
335 }
336
337
338 /*
339  * These are local overrides for various environment variables in Emacs.
340  * Please do not remove this and leave it at the end of the file, where
341  * Emacs will automagically detect them.
342  * ---------------------------------------------------------------------
343  * Local variables:
344  * mode: c
345  * indent-tabs-mode: t
346  * c-basic-offset: 4
347  * tab-width: 4
348  * End:
349  */