1 /* src/vm/jit/x86_64/solaris/md-os.c - machine dependent x86_64 Solaris functions
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
35 #include "vm/jit/x86_64/codegen.h"
36 #include "vm/jit/x86_64/md.h"
38 #include "threads/thread.hpp"
40 #include "vm/jit/builtin.hpp"
41 #include "vm/signallocal.hpp"
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/executionstate.h"
45 #include "vm/jit/trap.hpp"
46 #include "vm/jit/stacktrace.hpp"
49 /* md_signal_handler_sigsegv ***************************************************
51 Signal handler for hardware exception.
53 *******************************************************************************/
55 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
73 _uc = (ucontext_t *) _p;
74 _mc = &_uc->uc_mcontext;
76 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
77 different to the ones in <ucontext.h>. */
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 */
85 /* check for StackOverflowException */
87 threads_check_stackoverflow(sp);
90 /* get exception-throwing instruction */
92 opc = M_ALD_MEM_GET_OPC(xpc);
93 mod = M_ALD_MEM_GET_MOD(xpc);
94 rm = M_ALD_MEM_GET_RM(xpc);
96 /* for values see emit_mov_mem_reg and emit_mem */
98 if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
99 /* this was a hardware-exception */
101 d = M_ALD_MEM_GET_REG(xpc);
102 disp = M_ALD_MEM_GET_DISP(xpc);
104 /* we use the exception type as load displacement */
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... */
115 case 0: /* REG_RAX == 14 */
118 case 1: /* REG_RCX == 13 */
121 case 2: /* REG_RDX == 12 */
124 case 3: /* REG_RBX == 11 */
127 case 4: /* REG_RSP == 20 */
130 case 5: /* REG_RBP == 10 */
133 case 6: /* REG_RSI == 9 */
136 case 7: /* REG_RDI == 8 */
139 case 8: /* REG_R8 == 7 */
142 case 9: /* REG_R9 == 6 */
145 case 10: /* REG_R10 == 5 */
148 case 11: /* REG_R11 == 4 */
151 case 12: /* REG_R12 == 3 */
154 case 13: /* REG_R13 == 2 */
157 case 14: /* REG_R14 == 1 */
160 case 15: /* REG_R15 == 0 */
167 if (type == TRAP_COMPILER) {
168 /* The PV from the compiler stub is equal to the XPC. */
172 /* We use a framesize of zero here because the call pushed
173 the return addres onto the stack. */
175 ra = md_stacktrace_get_returnaddress(sp, 0);
177 /* Skip the RA on the stack. */
179 sp = sp + 1 * SIZEOF_VOID_P;
181 /* The XPC is the RA minus 1, because the RA points to the
182 instruction after the call. */
188 /* this was a normal NPE */
190 type = TRAP_NullPointerException;
194 /* Handle the trap. */
196 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
200 if (type == TRAP_COMPILER) {
202 o = builtin_retrieve_exception();
204 _mc->gregs[REG_RSP] = (uintptr_t) sp; /* Remove RA from stack. */
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;
211 _mc->gregs[REG_RIP] = (uintptr_t) p;
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;
222 /* md_signal_handler_sigfpe ****************************************************
224 ArithmeticException signal handler for hardware divide by zero
227 *******************************************************************************/
229 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
241 _uc = (ucontext_t *) _p;
242 _mc = &_uc->uc_mcontext;
244 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
245 different to the ones in <ucontext.h>. */
248 sp = (u1 *) _mc->gregs[REG_RSP];
249 xpc = (u1 *) _mc->gregs[REG_RIP];
250 ra = xpc; /* return address is equal to xpc */
252 /* This is an ArithmeticException. */
254 type = TRAP_ArithmeticException;
257 /* Handle the trap. */
259 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
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;
269 /* md_signal_handler_sigill ****************************************************
271 Signal handler for patchers.
273 *******************************************************************************/
275 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
287 _uc = (ucontext_t *) _p;
288 _mc = &_uc->uc_mcontext;
290 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
291 different to the ones in <ucontext.h>. */
294 sp = (u1 *) _mc->gregs[REG_RSP];
295 xpc = (u1 *) _mc->gregs[REG_RIP];
296 ra = xpc; /* return address is equal to xpc */
298 /* This is a patcher. */
303 /* Handle the trap. */
305 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
310 _mc->gregs[REG_RAX] = (intptr_t) p;
311 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
312 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
317 /* md_signal_handler_sigusr1 ***************************************************
319 Signal handler for suspending threads.
321 *******************************************************************************/
323 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
324 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
331 _uc = (ucontext_t *) _p;
332 _mc = &_uc->uc_mcontext;
334 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
335 different to the ones in <ucontext.h>. */
337 /* get the PC and SP for this thread */
338 pc = (u1 *) _mc->gregs[REG_RIP];
339 sp = (u1 *) _mc->gregs[REG_RSP];
341 /* now suspend the current thread */
342 threads_suspend_ack(pc, sp);
347 /* md_signal_handler_sigusr2 ***************************************************
349 Signal handler for profiling sampling.
351 *******************************************************************************/
353 #if defined(ENABLE_THREADS)
354 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
363 _uc = (ucontext_t *) _p;
364 _mc = &_uc->uc_mcontext;
366 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
367 different to the ones in <ucontext.h>. */
369 pc = (u1 *) _mc->gregs[REG_RIP];
376 /* md_executionstate_read ******************************************************
378 Read the given context into an executionstate.
380 *******************************************************************************/
382 void md_executionstate_read(executionstate_t *es, void *context)
389 _uc = (ucontext_t *) context;
390 _mc = &_uc->uc_mcontext;
392 /* read special registers */
393 es->pc = (u1 *) _mc->gregs[REG_RIP];
394 es->sp = (u1 *) _mc->gregs[REG_RSP];
397 /* read integer registers */
398 for (i = 0; i < INT_REG_CNT; i++) {
402 case 0: /* REG_RAX == 13 */
405 case 1: /* REG_RCX == 14 */
408 case 2: /* REG_RDX == 12 */
411 case 3: /* REG_RBX == 11 */
414 case 4: /* REG_RSP == 15 */
417 case 5: /* REG_RBP == 10 */
420 case 6: /* REG_RSI == 9 */
423 case 7: /* REG_RDI == 8 */
426 case 8: /* REG_R8 == 0 */
427 case 9: /* REG_R9 == 1 */
428 case 10: /* REG_R10 == 2 */
429 case 11: /* REG_R11 == 3 */
430 case 12: /* REG_R12 == 4 */
431 case 13: /* REG_R13 == 5 */
432 case 14: /* REG_R14 == 6 */
433 case 15: /* REG_R15 == 7 */
438 es->intregs[i] = _mc->gregs[d];
441 /* read float registers */
442 for (i = 0; i < FLT_REG_CNT; i++)
443 es->fltregs[i] = 0xdeadbeefdeadbeefL;
447 /* md_executionstate_write *****************************************************
449 Write the given executionstate back to the context.
451 *******************************************************************************/
453 void md_executionstate_write(executionstate_t *es, void *context)
460 _uc = (ucontext_t *) context;
461 _mc = &_uc->uc_mcontext;
463 /* write integer registers */
464 for (i = 0; i < INT_REG_CNT; i++) {
468 case 0: /* REG_RAX == 13 */
471 case 1: /* REG_RCX == 14 */
474 case 2: /* REG_RDX == 12 */
477 case 3: /* REG_RBX == 11 */
480 case 4: /* REG_RSP == 15 */
483 case 5: /* REG_RBP == 10 */
486 case 6: /* REG_RSI == 9 */
489 case 7: /* REG_RDI == 8 */
492 case 8: /* REG_R8 == 0 */
493 case 9: /* REG_R9 == 1 */
494 case 10: /* REG_R10 == 2 */
495 case 11: /* REG_R11 == 3 */
496 case 12: /* REG_R12 == 4 */
497 case 13: /* REG_R13 == 5 */
498 case 14: /* REG_R14 == 6 */
499 case 15: /* REG_R15 == 7 */
504 _mc->gregs[d] = es->intregs[i];
507 /* write special registers */
508 _mc->gregs[REG_RIP] = (ptrint) es->pc;
509 _mc->gregs[REG_RSP] = (ptrint) es->sp;
514 * These are local overrides for various environment variables in Emacs.
515 * Please do not remove this and leave it at the end of the file, where
516 * Emacs will automagically detect them.
517 * ---------------------------------------------------------------------
520 * indent-tabs-mode: t
524 * vim:noexpandtab:sw=4:ts=4: