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