1 /* src/vm/jit/x86_64/linux/md-os.c - machine dependent x86_64 Linux functions
3 Copyright (C) 2007, 2008
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
37 #include "vm/jit/x86_64/codegen.h"
38 #include "vm/jit/x86_64/md.h"
40 #include "threads/thread.hpp"
42 #include "vm/signallocal.hpp"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/trap.hpp"
50 * Signal handler for hardware exception.
52 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
54 ucontext_t* _uc = (ucontext_t *) _p;
55 mcontext_t* _mc = &_uc->uc_mcontext;
57 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
58 different to the ones in <ucontext.h>. */
60 void* xpc = (void*) _mc->gregs[REG_RIP];
63 trap_handle(TRAP_SIGSEGV, xpc, _p);
68 * ArithmeticException signal handler for hardware divide by zero
71 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
73 ucontext_t* _uc = (ucontext_t *) _p;
74 mcontext_t* _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 void* xpc = (void*) _mc->gregs[REG_RIP];
82 trap_handle(TRAP_SIGFPE, xpc, _p);
87 * Signal handler for patchers.
89 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
91 ucontext_t* _uc = (ucontext_t *) _p;
92 mcontext_t* _mc = &_uc->uc_mcontext;
94 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
95 different to the ones in <ucontext.h>. */
97 void* xpc = (void*) _mc->gregs[REG_RIP];
100 trap_handle(TRAP_SIGILL, xpc, _p);
104 /* md_signal_handler_sigusr1 ***************************************************
106 Signal handler for suspending threads.
108 *******************************************************************************/
110 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
111 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
118 _uc = (ucontext_t *) _p;
119 _mc = &_uc->uc_mcontext;
121 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
122 different to the ones in <ucontext.h>. */
124 /* get the PC and SP for this thread */
125 pc = (u1 *) _mc->gregs[REG_RIP];
126 sp = (u1 *) _mc->gregs[REG_RSP];
128 /* now suspend the current thread */
129 threads_suspend_ack(pc, sp);
134 /* md_signal_handler_sigusr2 ***************************************************
136 Signal handler for profiling sampling.
138 *******************************************************************************/
140 #if defined(ENABLE_THREADS)
141 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
150 _uc = (ucontext_t *) _p;
151 _mc = &_uc->uc_mcontext;
153 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
154 different to the ones in <ucontext.h>. */
156 pc = (u1 *) _mc->gregs[REG_RIP];
163 /* md_executionstate_read ******************************************************
165 Read the given context into an executionstate.
167 *******************************************************************************/
169 void md_executionstate_read(executionstate_t *es, void *context)
176 _uc = (ucontext_t *) context;
177 _mc = &_uc->uc_mcontext;
179 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
180 different to the ones in <ucontext.h>. */
182 /* read special registers */
183 es->pc = (u1 *) _mc->gregs[REG_RIP];
184 es->sp = (u1 *) _mc->gregs[REG_RSP];
187 /* read integer registers */
188 for (i = 0; i < INT_REG_CNT; i++) {
192 case 0: /* REG_RAX == 13 */
195 case 1: /* REG_RCX == 14 */
198 case 2: /* REG_RDX == 12 */
201 case 3: /* REG_RBX == 11 */
204 case 4: /* REG_RSP == 15 */
207 case 5: /* REG_RBP == 10 */
210 case 6: /* REG_RSI == 9 */
213 case 7: /* REG_RDI == 8 */
216 case 8: /* REG_R8 == 0 */
217 case 9: /* REG_R9 == 1 */
218 case 10: /* REG_R10 == 2 */
219 case 11: /* REG_R11 == 3 */
220 case 12: /* REG_R12 == 4 */
221 case 13: /* REG_R13 == 5 */
222 case 14: /* REG_R14 == 6 */
223 case 15: /* REG_R15 == 7 */
228 es->intregs[i] = _mc->gregs[d];
231 /* read float registers */
232 for (i = 0; i < FLT_REG_CNT; i++)
233 es->fltregs[i] = 0xdeadbeefdeadbeefL;
237 /* md_executionstate_write *****************************************************
239 Write the given executionstate back to the context.
241 *******************************************************************************/
243 void md_executionstate_write(executionstate_t *es, void *context)
250 _uc = (ucontext_t *) context;
251 _mc = &_uc->uc_mcontext;
253 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
254 different to the ones in <ucontext.h>. */
256 /* write integer registers */
257 for (i = 0; i < INT_REG_CNT; i++) {
261 case 0: /* REG_RAX == 13 */
264 case 1: /* REG_RCX == 14 */
267 case 2: /* REG_RDX == 12 */
270 case 3: /* REG_RBX == 11 */
273 case 4: /* REG_RSP == 15 */
276 case 5: /* REG_RBP == 10 */
279 case 6: /* REG_RSI == 9 */
282 case 7: /* REG_RDI == 8 */
285 case 8: /* REG_R8 == 0 */
286 case 9: /* REG_R9 == 1 */
287 case 10: /* REG_R10 == 2 */
288 case 11: /* REG_R11 == 3 */
289 case 12: /* REG_R12 == 4 */
290 case 13: /* REG_R13 == 5 */
291 case 14: /* REG_R14 == 6 */
292 case 15: /* REG_R15 == 7 */
297 _mc->gregs[d] = es->intregs[i];
300 /* write special registers */
301 _mc->gregs[REG_RIP] = (ptrint) es->pc;
302 _mc->gregs[REG_RSP] = (ptrint) es->sp;
307 * These are local overrides for various environment variables in Emacs.
308 * Please do not remove this and leave it at the end of the file, where
309 * Emacs will automagically detect them.
310 * ---------------------------------------------------------------------
313 * indent-tabs-mode: t
317 * vim:noexpandtab:sw=4:ts=4: