1 /* src/vm/jit/alpha/linux/md-os.c - machine dependent Alpha Linux functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2008 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "vm/jit/alpha/codegen.h"
36 #include "vm/jit/alpha/md.h"
37 #include "vm/jit/alpha/md-abi.h"
39 #include "threads/thread.hpp"
41 #include "vm/jit/builtin.hpp"
42 #include "vm/signallocal.h"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/disass.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/trap.h"
51 /* md_signal_handler_sigsegv ***************************************************
53 NullPointerException signal handler for hardware null pointer
56 *******************************************************************************/
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
75 _uc = (ucontext_t *) _p;
76 _mc = &_uc->uc_mcontext;
78 pv = (u1 *) _mc->sc_regs[REG_PV];
79 sp = (u1 *) _mc->sc_regs[REG_SP];
80 ra = (u1 *) _mc->sc_regs[REG_RA]; /* this is correct for leafs */
81 xpc = (u1 *) _mc->sc_pc;
83 /* get exception-throwing instruction */
85 mcode = *((u4 *) xpc);
87 d = M_MEM_GET_Ra(mcode);
88 s1 = M_MEM_GET_Rb(mcode);
89 disp = M_MEM_GET_Memory_disp(mcode);
91 val = _mc->sc_regs[d];
93 /* check for special-load */
96 /* we use the exception type as load displacement */
100 if (type == TRAP_COMPILER) {
101 /* The XPC is the RA minus 1, because the RA points to the
102 instruction after the call. */
108 /* This is a normal NPE: addr must be NULL and the NPE-type
111 addr = _mc->sc_regs[s1];
115 /* Handle the trap. */
117 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
124 _mc->sc_regs[REG_PV] = (uintptr_t) p;
125 _mc->sc_pc = (uintptr_t) p;
129 /* Get and set the PV from the parent Java method. */
131 pv = md_codegen_get_pv_from_pc(ra);
133 _mc->sc_regs[REG_PV] = (uintptr_t) pv;
135 /* Get the exception object. */
137 p = builtin_retrieve_exception();
144 _mc->sc_regs[REG_ITMP1_XPTR] = (uintptr_t) p;
145 _mc->sc_regs[REG_ITMP2_XPC] = (uintptr_t) xpc;
146 _mc->sc_pc = (uintptr_t) asm_handle_exception;
151 /* md_signal_handler_sigill ****************************************************
153 Illegal Instruction signal handler for hardware exception checks.
155 *******************************************************************************/
157 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
159 ucontext_t* _uc = (ucontext_t*) _p;
160 mcontext_t* _mc = &_uc->uc_mcontext;
162 void* pv = (u1 *) _mc->sc_regs[REG_PV];
163 void* sp = (u1 *) _mc->sc_regs[REG_SP];
164 void* ra = (u1 *) _mc->sc_regs[REG_RA]; // RA is correct for leaf methods.
165 void* xpc = (u1 *) _mc->sc_pc;
167 // The PC points to the instruction after the illegal instruction.
168 xpc = (void*) (((uintptr_t) xpc) - 4);
170 // Get the exception-throwing instruction.
171 uint32_t mcode = *((uint32_t*) xpc);
173 int opcode = M_OP3_GET_Opcode(mcode);
175 // Check for undefined instruction we use.
176 // TODO Check the whole instruction.
178 log_println("md_signal_handler_sigill: Unknown illegal instruction %x at %p", mcode, xpc);
179 #if defined(ENABLE_DISASSEMBLER)
180 (void) disassinstr(xpc);
182 vm_abort("Aborting...");
185 // This signal is always a patcher.
186 int type = TRAP_PATCHER;
190 void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
192 // Set registers if we have an exception, continue execution
195 _mc->sc_regs[REG_ITMP1_XPTR] = (uintptr_t) p;
196 _mc->sc_regs[REG_ITMP2_XPC] = (uintptr_t) xpc;
197 _mc->sc_pc = (uintptr_t) asm_handle_exception;
200 // We need to set the PC because we adjusted it above.
201 _mc->sc_pc = (uintptr_t) xpc;
206 /* md_signal_handler_sigusr1 ***************************************************
208 Signal handler for suspending threads.
210 *******************************************************************************/
212 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
213 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
220 _uc = (ucontext_t *) _p;
221 _mc = &_uc->uc_mcontext;
223 /* get the PC and SP for this thread */
224 pc = (u1 *) _mc->sc_pc;
225 sp = (u1 *) _mc->sc_regs[REG_SP];
227 /* now suspend the current thread */
228 threads_suspend_ack(pc, sp);
233 /* md_signal_handler_sigusr2 ***************************************************
235 Signal handler for profiling sampling.
237 *******************************************************************************/
239 #if defined(ENABLE_THREADS)
240 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
249 _uc = (ucontext_t *) _p;
250 _mc = &_uc->uc_mcontext;
252 pc = (u1 *) _mc->sc_pc;
259 /* md_executionstate_read ******************************************************
261 Read the given context into an executionstate.
263 *******************************************************************************/
265 void md_executionstate_read(executionstate_t *es, void *context)
271 _uc = (ucontext_t *) context;
272 _mc = &_uc->uc_mcontext;
274 /* read special registers */
275 es->pc = (u1 *) _mc->sc_pc;
276 es->sp = (u1 *) _mc->sc_regs[REG_SP];
277 es->pv = (u1 *) _mc->sc_regs[REG_PV];
278 es->ra = (u1 *) _mc->sc_regs[REG_RA];
280 /* read integer registers */
281 for (i = 0; i < INT_REG_CNT; i++)
282 es->intregs[i] = _mc->sc_regs[i];
284 /* read float registers */
285 /* Do not use the assignment operator '=', as the type of
286 * the _mc->sc_fpregs[i] can cause invalid conversions. */
288 assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
289 os_memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
293 /* md_executionstate_write *****************************************************
295 Write the given executionstate back to the context.
297 *******************************************************************************/
299 void md_executionstate_write(executionstate_t *es, void *context)
305 _uc = (ucontext_t *) context;
306 _mc = &_uc->uc_mcontext;
308 /* write integer registers */
309 for (i = 0; i < INT_REG_CNT; i++)
310 _mc->sc_regs[i] = es->intregs[i];
312 /* write float registers */
313 /* Do not use the assignment operator '=', as the type of
314 * the _mc->sc_fpregs[i] can cause invalid conversions. */
316 assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
317 os_memcpy(&_mc->sc_fpregs, &es->fltregs, sizeof(_mc->sc_fpregs));
319 /* write special registers */
320 _mc->sc_pc = (ptrint) es->pc;
321 _mc->sc_regs[REG_SP] = (ptrint) es->sp;
322 _mc->sc_regs[REG_PV] = (ptrint) es->pv;
323 _mc->sc_regs[REG_RA] = (ptrint) es->ra;
328 * These are local overrides for various environment variables in Emacs.
329 * Please do not remove this and leave it at the end of the file, where
330 * Emacs will automagically detect them.
331 * ---------------------------------------------------------------------
334 * indent-tabs-mode: t