* src/native/vm/sun/jvm.c (JVM_FindLibraryEntry): Use HPI.
[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/stacktrace.h"
48
49
50 /* md_signal_handler_sigsegv ***************************************************
51
52    Signal handler for hardware exception.
53
54 *******************************************************************************/
55
56 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
57 {
58         ucontext_t     *_uc;
59         mcontext_t     *_mc;
60         void           *pv;
61         u1             *sp;
62         u1             *ra;
63         u1             *xpc;
64         u1              opc;
65         u1              mod;
66         u1              rm;
67         s4              d;
68         s4              disp;
69         int             type;
70         intptr_t        val;
71         void           *p;
72         java_object_t  *o;
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 == EXCEPTION_HARDWARE_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 = EXCEPTION_HARDWARE_NULLPOINTER;
178                 val  = 0;
179         }
180
181         /* Handle the type. */
182
183         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
184
185         /* Set registers. */
186
187         if (type == EXCEPTION_HARDWARE_COMPILER) {
188                 if (p == NULL) {
189                         o = builtin_retrieve_exception();
190
191                         _mc->gregs[REG_RSP] = (uintptr_t) sp;    /* Remove RA from stack. */
192
193                         _mc->gregs[REG_RAX] = (uintptr_t) o;
194                         _mc->gregs[REG_R10] = (uintptr_t) xpc;           /* REG_ITMP2_XPC */
195                         _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
196                 }
197                 else {
198                         _mc->gregs[REG_RIP] = (uintptr_t) p;
199                 }
200         }
201         else {
202                 _mc->gregs[REG_RAX] = (uintptr_t) p;
203                 _mc->gregs[REG_R10] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
204                 _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
205         }
206 }
207
208
209 /* md_signal_handler_sigfpe ****************************************************
210
211    ArithmeticException signal handler for hardware divide by zero
212    check.
213
214 *******************************************************************************/
215
216 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
217 {
218         ucontext_t     *_uc;
219         mcontext_t     *_mc;
220         u1             *pv;
221         u1             *sp;
222         u1             *ra;
223         u1             *xpc;
224         int             type;
225         intptr_t        val;
226         void           *p;
227
228         _uc = (ucontext_t *) _p;
229         _mc = &_uc->uc_mcontext;
230
231         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
232            different to the ones in <ucontext.h>. */
233
234         pv  = NULL;
235         sp  = (u1 *) _mc->gregs[REG_RSP];
236         xpc = (u1 *) _mc->gregs[REG_RIP];
237         ra  = xpc;                          /* return address is equal to xpc     */
238
239         /* this is an ArithmeticException */
240
241         type = EXCEPTION_HARDWARE_ARITHMETIC;
242         val  = 0;
243
244         /* Handle the type. */
245
246         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
247
248         /* set registers */
249
250         _mc->gregs[REG_RAX] = (intptr_t) p;
251         _mc->gregs[REG_R10] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
252         _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
253 }
254
255
256 /* md_signal_handler_sigill ****************************************************
257
258    Signal handler for patchers.
259
260 *******************************************************************************/
261
262 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
263 {
264         ucontext_t     *_uc;
265         mcontext_t     *_mc;
266         u1             *pv;
267         u1             *sp;
268         u1             *ra;
269         u1             *xpc;
270         int             type;
271         intptr_t        val;
272         void           *p;
273
274         _uc = (ucontext_t *) _p;
275         _mc = &_uc->uc_mcontext;
276
277         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
278            different to the ones in <ucontext.h>. */
279
280         pv  = NULL;
281         sp  = (u1 *) _mc->gregs[REG_RSP];
282         xpc = (u1 *) _mc->gregs[REG_RIP];
283         ra  = xpc;                          /* return address is equal to xpc     */
284
285         /* This is a patcher. */
286
287         type = EXCEPTION_HARDWARE_PATCHER;
288         val  = 0;
289
290         /* Handle the type. */
291
292         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
293
294         /* set registers */
295
296         if (p != NULL) {
297                 _mc->gregs[REG_RAX] = (intptr_t) p;
298                 _mc->gregs[REG_R10] = (intptr_t) xpc;                /* REG_ITMP2_XPC */
299                 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
300         }
301 }
302
303
304 /* md_signal_handler_sigusr1 ***************************************************
305
306    Signal handler for suspending threads.
307
308 *******************************************************************************/
309
310 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
311 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
312 {
313         ucontext_t *_uc;
314         mcontext_t *_mc;
315         u1         *pc;
316         u1         *sp;
317
318         _uc = (ucontext_t *) _p;
319         _mc = &_uc->uc_mcontext;
320
321         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
322            different to the ones in <ucontext.h>. */
323
324         /* get the PC and SP for this thread */
325         pc = (u1 *) _mc->gregs[REG_RIP];
326         sp = (u1 *) _mc->gregs[REG_RSP];
327
328         /* now suspend the current thread */
329         threads_suspend_ack(pc, sp);
330 }
331 #endif
332
333
334 /* md_signal_handler_sigusr2 ***************************************************
335
336    Signal handler for profiling sampling.
337
338 *******************************************************************************/
339
340 #if defined(ENABLE_THREADS)
341 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
342 {
343         threadobject *t;
344         ucontext_t   *_uc;
345         mcontext_t   *_mc;
346         u1           *pc;
347
348         t = THREADOBJECT;
349
350         _uc = (ucontext_t *) _p;
351         _mc = &_uc->uc_mcontext;
352
353         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
354            different to the ones in <ucontext.h>. */
355
356         pc = (u1 *) _mc->gregs[REG_RIP];
357
358         t->pc = pc;
359 }
360 #endif
361
362
363 /* md_replace_executionstate_read **********************************************
364
365    Read the given context into an executionstate for Replacement.
366
367 *******************************************************************************/
368
369 #if defined(ENABLE_REPLACEMENT)
370 void md_replace_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 #endif
434
435
436 /* md_replace_executionstate_write *********************************************
437
438    Write the given executionstate back to the context for Replacement.
439
440 *******************************************************************************/
441
442 #if defined(ENABLE_REPLACEMENT)
443 void md_replace_executionstate_write(executionstate_t *es, void *context)
444 {
445         ucontext_t *_uc;
446         mcontext_t *_mc;
447         s4          i;
448         s4          d;
449
450         _uc = (ucontext_t *) context;
451         _mc = &_uc->uc_mcontext;
452
453         /* write integer registers */
454         for (i = 0; i < INT_REG_CNT; i++) {
455                 /* XXX FIX ME! */
456
457                 switch (i) {
458                 case 0:  /* REG_RAX == 13 */
459                         d = REG_RAX;
460                         break;
461                 case 1:  /* REG_RCX == 14 */
462                         d = REG_RCX;
463                         break;
464                 case 2:  /* REG_RDX == 12 */
465                         d = REG_RDX;
466                         break;
467                 case 3:  /* REG_RBX == 11 */
468                         d = REG_RBX;
469                         break;
470                 case 4:  /* REG_RSP == 15 */
471                         d = REG_RSP;
472                         break;
473                 case 5:  /* REG_RBP == 10 */
474                         d = REG_RBP;
475                         break;
476                 case 6:  /* REG_RSI == 9  */
477                         d = REG_RSI;
478                         break;
479                 case 7:  /* REG_RDI == 8  */
480                         d = REG_RDI;
481                         break;
482                 case 8:  /* REG_R8  == 0  */
483                 case 9:  /* REG_R9  == 1  */
484                 case 10: /* REG_R10 == 2  */
485                 case 11: /* REG_R11 == 3  */
486                 case 12: /* REG_R12 == 4  */
487                 case 13: /* REG_R13 == 5  */
488                 case 14: /* REG_R14 == 6  */
489                 case 15: /* REG_R15 == 7  */
490                         d = i - 8;
491                         break;
492                 }
493
494                 _mc->gregs[d] = es->intregs[i];
495         }
496
497         /* write special registers */
498         _mc->gregs[REG_RIP] = (ptrint) es->pc;
499         _mc->gregs[REG_RSP] = (ptrint) es->sp;
500 }
501 #endif
502
503
504 /* md_critical_section_restart *************************************************
505
506    Search the critical sections tree for a matching section and set
507    the PC to the restart point, if necessary.
508
509 *******************************************************************************/
510
511 #if defined(ENABLE_THREADS)
512 void md_critical_section_restart(ucontext_t *_uc)
513 {
514         mcontext_t *_mc;
515         u1         *pc;
516         u1         *npc;
517
518         _mc = &_uc->uc_mcontext;
519
520         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
521            different to the ones in <ucontext.h>. */
522
523         pc = (u1 *) _mc->gregs[REG_RIP];
524
525         npc = critical_find_restart_point(pc);
526
527         if (npc != NULL)
528                 _mc->gregs[REG_RIP] = (ptrint) npc;
529 }
530 #endif
531
532
533 /*
534  * These are local overrides for various environment variables in Emacs.
535  * Please do not remove this and leave it at the end of the file, where
536  * Emacs will automagically detect them.
537  * ---------------------------------------------------------------------
538  * Local variables:
539  * mode: c
540  * indent-tabs-mode: t
541  * c-basic-offset: 4
542  * tab-width: 4
543  * End:
544  * vim:noexpandtab:sw=4:ts=4:
545  */