PR85 for x86_64.
[cacao.git] / src / vm / jit / x86_64 / linux / md-os.c
1 /* src/vm/jit/x86_64/linux/md-os.c - machine dependent x86_64 Linux functions
2
3    Copyright (C) 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
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/x86_64/codegen.h"
38 #include "vm/jit/x86_64/md.h"
39
40 #include "threads/thread.hpp"
41
42 #include "vm/jit/builtin.hpp"
43 #include "vm/signallocal.hpp"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/executionstate.h"
47 #include "vm/jit/trap.h"
48 #include "vm/jit/stacktrace.hpp"
49
50
51 /* md_signal_handler_sigsegv ***************************************************
52
53    Signal handler for hardware exception.
54
55 *******************************************************************************/
56
57 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
58 {
59         ucontext_t     *_uc;
60         mcontext_t     *_mc;
61         void           *pv;
62         u1             *sp;
63         u1             *ra;
64         u1             *xpc;
65         u1              opc;
66         u1              mod;
67         u1              rm;
68         s4              d;
69         s4              disp;
70         int             type;
71         intptr_t        val;
72         void           *p;
73
74         _uc = (ucontext_t *) _p;
75         _mc = &_uc->uc_mcontext;
76
77         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
78            different to the ones in <ucontext.h>. */
79
80         pv  = NULL;                 /* is resolved during stackframeinfo creation */
81         sp  = (u1 *) _mc->gregs[REG_RSP];
82         xpc = (u1 *) _mc->gregs[REG_RIP];
83         ra  = xpc;                              /* return address is equal to XPC */
84
85 #if 0
86         /* check for StackOverflowException */
87
88         threads_check_stackoverflow(sp);
89 #endif
90
91         /* get exception-throwing instruction */
92
93         opc = M_ALD_MEM_GET_OPC(xpc);
94         mod = M_ALD_MEM_GET_MOD(xpc);
95         rm  = M_ALD_MEM_GET_RM(xpc);
96
97         /* for values see emit_mov_mem_reg and emit_mem */
98
99         if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
100                 /* this was a hardware-exception */
101
102                 d    = M_ALD_MEM_GET_REG(xpc);
103                 disp = M_ALD_MEM_GET_DISP(xpc);
104
105                 /* we use the exception type as load displacement */
106
107                 type = disp;
108
109                 /* XXX FIX ME! */
110
111                 /* ATTENTION: The _mc->gregs layout is even worse than on
112                    i386! See /usr/include/sys/ucontext.h.  We need a
113                    switch-case here... */
114
115                 switch (d) {
116                 case 0:  /* REG_RAX == 13 */
117                         d = REG_RAX;
118                         break;
119                 case 1:  /* REG_RCX == 14 */
120                         d = REG_RCX;
121                         break;
122                 case 2:  /* REG_RDX == 12 */
123                         d = REG_RDX;
124                         break;
125                 case 3:  /* REG_RBX == 11 */
126                         d = REG_RBX;
127                         break;
128                 case 4:  /* REG_RSP == 15 */
129                         d = REG_RSP;
130                         break;
131                 case 5:  /* REG_RBP == 10 */
132                         d = REG_RBP;
133                         break;
134                 case 6:  /* REG_RSI == 9  */
135                         d = REG_RSI;
136                         break;
137                 case 7:  /* REG_RDI == 8  */
138                         d = REG_RDI;
139                         break;
140                 case 8:  /* REG_R8  == 0  */
141                 case 9:  /* REG_R9  == 1  */
142                 case 10: /* REG_R10 == 2  */
143                 case 11: /* REG_R11 == 3  */
144                 case 12: /* REG_R12 == 4  */
145                 case 13: /* REG_R13 == 5  */
146                 case 14: /* REG_R14 == 6  */
147                 case 15: /* REG_R15 == 7  */
148                         d = d - 8;
149                         break;
150                 }
151
152                 val = _mc->gregs[d];
153
154                 if (type == TRAP_COMPILER) {
155                         /* The PV from the compiler stub is equal to the XPC. */
156
157                         pv = xpc;
158
159                         /* We use a framesize of zero here because the call pushed
160                            the return addres onto the stack. */
161
162                         ra = md_stacktrace_get_returnaddress(sp, 0);
163
164                         /* Skip the RA on the stack. */
165
166                         sp = sp + 1 * SIZEOF_VOID_P;
167
168                         /* The XPC is the RA minus 1, because the RA points to the
169                            instruction after the call. */
170
171                         xpc = ra - 3;
172                 }
173         }
174         else {
175                 /* this was a normal NPE */
176
177                 type = TRAP_NullPointerException;
178                 val  = 0;
179         }
180
181         /* Handle the trap. */
182
183         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
184
185         /* Set registers. */
186
187         if (type == TRAP_COMPILER) {
188                 if (p == NULL) {
189                         _mc->gregs[REG_RSP] = (uintptr_t) sp;    /* Remove RA from stack. */
190                 }
191         }
192 }
193
194
195 /* md_signal_handler_sigfpe ****************************************************
196
197    ArithmeticException signal handler for hardware divide by zero
198    check.
199
200 *******************************************************************************/
201
202 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
203 {
204         ucontext_t     *_uc;
205         mcontext_t     *_mc;
206         u1             *pv;
207         u1             *sp;
208         u1             *ra;
209         u1             *xpc;
210         int             type;
211         intptr_t        val;
212
213         _uc = (ucontext_t *) _p;
214         _mc = &_uc->uc_mcontext;
215
216         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
217            different to the ones in <ucontext.h>. */
218
219         pv  = NULL;
220         sp  = (u1 *) _mc->gregs[REG_RSP];
221         xpc = (u1 *) _mc->gregs[REG_RIP];
222         ra  = xpc;                          /* return address is equal to xpc     */
223
224         /* This is an ArithmeticException. */
225
226         type = TRAP_ArithmeticException;
227         val  = 0;
228
229         /* Handle the trap. */
230
231         trap_handle(type, val, pv, sp, ra, xpc, _p);
232 }
233
234
235 /* md_signal_handler_sigill ****************************************************
236
237    Signal handler for patchers.
238
239 *******************************************************************************/
240
241 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
242 {
243         ucontext_t     *_uc;
244         mcontext_t     *_mc;
245         u1             *pv;
246         u1             *sp;
247         u1             *ra;
248         u1             *xpc;
249         int             type;
250         intptr_t        val;
251
252         _uc = (ucontext_t *) _p;
253         _mc = &_uc->uc_mcontext;
254
255         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
256            different to the ones in <ucontext.h>. */
257
258         pv  = NULL;
259         sp  = (u1 *) _mc->gregs[REG_RSP];
260         xpc = (u1 *) _mc->gregs[REG_RIP];
261         ra  = xpc;                          /* return address is equal to xpc     */
262
263         // Check if the trap instruction is valid.
264         // TODO Move this into patcher_handler.
265         if (patcher_is_valid_trap_instruction_at(xpc) == false) {
266                 // Check if the PC has been patched during our way to this
267                 // signal handler (see PR85).
268                 if (patcher_is_patched_at(xpc) == true)
269                         return;
270
271                 // We have a problem...
272                 log_println("md_signal_handler_sigill: Unknown illegal instruction at 0x%lx", xpc);
273 #if defined(ENABLE_DISASSEMBLER)
274                 (void) disassinstr(xpc);
275 #endif
276                 vm_abort("Aborting...");
277         }
278
279         /* This is a patcher. */
280
281         type = TRAP_PATCHER;
282         val  = 0;
283
284         /* Handle the trap. */
285
286         trap_handle(type, val, pv, sp, ra, xpc, _p);
287 }
288
289
290 /* md_signal_handler_sigusr1 ***************************************************
291
292    Signal handler for suspending threads.
293
294 *******************************************************************************/
295
296 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
297 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
298 {
299         ucontext_t *_uc;
300         mcontext_t *_mc;
301         u1         *pc;
302         u1         *sp;
303
304         _uc = (ucontext_t *) _p;
305         _mc = &_uc->uc_mcontext;
306
307         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
308            different to the ones in <ucontext.h>. */
309
310         /* get the PC and SP for this thread */
311         pc = (u1 *) _mc->gregs[REG_RIP];
312         sp = (u1 *) _mc->gregs[REG_RSP];
313
314         /* now suspend the current thread */
315         threads_suspend_ack(pc, sp);
316 }
317 #endif
318
319
320 /* md_signal_handler_sigusr2 ***************************************************
321
322    Signal handler for profiling sampling.
323
324 *******************************************************************************/
325
326 #if defined(ENABLE_THREADS)
327 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
328 {
329         threadobject *t;
330         ucontext_t   *_uc;
331         mcontext_t   *_mc;
332         u1           *pc;
333
334         t = THREADOBJECT;
335
336         _uc = (ucontext_t *) _p;
337         _mc = &_uc->uc_mcontext;
338
339         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
340            different to the ones in <ucontext.h>. */
341
342         pc = (u1 *) _mc->gregs[REG_RIP];
343
344         t->pc = pc;
345 }
346 #endif
347
348
349 /* md_executionstate_read ******************************************************
350
351    Read the given context into an executionstate.
352
353 *******************************************************************************/
354
355 void md_executionstate_read(executionstate_t *es, void *context)
356 {
357         ucontext_t *_uc;
358         mcontext_t *_mc;
359         s4          i;
360         s4          d;
361
362         _uc = (ucontext_t *) context;
363         _mc = &_uc->uc_mcontext;
364
365         /* read special registers */
366         es->pc = (u1 *) _mc->gregs[REG_RIP];
367         es->sp = (u1 *) _mc->gregs[REG_RSP];
368         es->pv = NULL;
369
370         /* read integer registers */
371         for (i = 0; i < INT_REG_CNT; i++) {
372                 /* XXX FIX ME! */
373
374                 switch (i) {
375                 case 0:  /* REG_RAX == 13 */
376                         d = REG_RAX;
377                         break;
378                 case 1:  /* REG_RCX == 14 */
379                         d = REG_RCX;
380                         break;
381                 case 2:  /* REG_RDX == 12 */
382                         d = REG_RDX;
383                         break;
384                 case 3:  /* REG_RBX == 11 */
385                         d = REG_RBX;
386                         break;
387                 case 4:  /* REG_RSP == 15 */
388                         d = REG_RSP;
389                         break;
390                 case 5:  /* REG_RBP == 10 */
391                         d = REG_RBP;
392                         break;
393                 case 6:  /* REG_RSI == 9  */
394                         d = REG_RSI;
395                         break;
396                 case 7:  /* REG_RDI == 8  */
397                         d = REG_RDI;
398                         break;
399                 case 8:  /* REG_R8  == 0  */
400                 case 9:  /* REG_R9  == 1  */
401                 case 10: /* REG_R10 == 2  */
402                 case 11: /* REG_R11 == 3  */
403                 case 12: /* REG_R12 == 4  */
404                 case 13: /* REG_R13 == 5  */
405                 case 14: /* REG_R14 == 6  */
406                 case 15: /* REG_R15 == 7  */
407                         d = i - 8;
408                         break;
409                 }
410
411                 es->intregs[i] = _mc->gregs[d];
412         }
413
414         /* read float registers */
415         for (i = 0; i < FLT_REG_CNT; i++)
416                 es->fltregs[i] = 0xdeadbeefdeadbeefL;
417 }
418
419
420 /* md_executionstate_write *****************************************************
421
422    Write the given executionstate back to the context.
423
424 *******************************************************************************/
425
426 void md_executionstate_write(executionstate_t *es, void *context)
427 {
428         ucontext_t *_uc;
429         mcontext_t *_mc;
430         s4          i;
431         s4          d;
432
433         _uc = (ucontext_t *) context;
434         _mc = &_uc->uc_mcontext;
435
436         /* write integer registers */
437         for (i = 0; i < INT_REG_CNT; i++) {
438                 /* XXX FIX ME! */
439
440                 switch (i) {
441                 case 0:  /* REG_RAX == 13 */
442                         d = REG_RAX;
443                         break;
444                 case 1:  /* REG_RCX == 14 */
445                         d = REG_RCX;
446                         break;
447                 case 2:  /* REG_RDX == 12 */
448                         d = REG_RDX;
449                         break;
450                 case 3:  /* REG_RBX == 11 */
451                         d = REG_RBX;
452                         break;
453                 case 4:  /* REG_RSP == 15 */
454                         d = REG_RSP;
455                         break;
456                 case 5:  /* REG_RBP == 10 */
457                         d = REG_RBP;
458                         break;
459                 case 6:  /* REG_RSI == 9  */
460                         d = REG_RSI;
461                         break;
462                 case 7:  /* REG_RDI == 8  */
463                         d = REG_RDI;
464                         break;
465                 case 8:  /* REG_R8  == 0  */
466                 case 9:  /* REG_R9  == 1  */
467                 case 10: /* REG_R10 == 2  */
468                 case 11: /* REG_R11 == 3  */
469                 case 12: /* REG_R12 == 4  */
470                 case 13: /* REG_R13 == 5  */
471                 case 14: /* REG_R14 == 6  */
472                 case 15: /* REG_R15 == 7  */
473                         d = i - 8;
474                         break;
475                 }
476
477                 _mc->gregs[d] = es->intregs[i];
478         }
479
480         /* write special registers */
481         _mc->gregs[REG_RIP] = (ptrint) es->pc;
482         _mc->gregs[REG_RSP] = (ptrint) es->sp;
483 }
484
485
486 /*
487  * These are local overrides for various environment variables in Emacs.
488  * Please do not remove this and leave it at the end of the file, where
489  * Emacs will automagically detect them.
490  * ---------------------------------------------------------------------
491  * Local variables:
492  * mode: c
493  * indent-tabs-mode: t
494  * c-basic-offset: 4
495  * tab-width: 4
496  * End:
497  * vim:noexpandtab:sw=4:ts=4:
498  */