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