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