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