1 /* src/vm/jit/powerpc64/linux/md-os.c - machine dependent PowerPC64 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/powerpc64/codegen.h"
36 #include "vm/jit/powerpc64/md.h"
37 #include "vm/jit/powerpc64/linux/md-abi.h"
39 #include "threads/thread.hpp"
41 #include "vm/jit/builtin.hpp"
42 #include "vm/signallocal.hpp"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/executionstate.h"
48 #if defined(ENABLE_PROFILING)
49 # include "vm/jit/optimizing/profile.h"
52 #include "vm/jit/disass.h"
53 #include "vm/jit/trap.hpp"
56 /* md_signal_handler_sigsegv ***************************************************
58 Signal handler for hardware-exceptions.
60 *******************************************************************************/
62 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
78 _uc = (ucontext_t *) _p;
79 _mc = &(_uc->uc_mcontext);
81 /* get register values */
83 pv = (u1*) _mc->gp_regs[REG_PV];
84 sp = (u1*) _mc->gp_regs[REG_SP];
85 ra = (u1*) _mc->gp_regs[PT_LNK]; /* correct for leafs */
86 xpc =(u1*) _mc->gp_regs[PT_NIP];
88 /* get the throwing instruction */
92 s1 = M_INSTR_OP2_IMM_A(mcode);
93 disp = M_INSTR_OP2_IMM_I(mcode);
94 d = M_INSTR_OP2_IMM_D(mcode);
96 val = _mc->gp_regs[d];
99 /* We use the exception type as load displacement. */
102 if (type == TRAP_COMPILER) {
103 /* The XPC is the RA minus 1, because the RA points to the
104 instruction after the call. */
111 addr = _mc->gp_regs[s1];
115 /* Handle the trap. */
117 trap_handle(type, val, pv, sp, ra, xpc, _p);
122 * Signal handler for patcher calls.
124 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
126 ucontext_t* _uc = (ucontext_t*) _p;
127 mcontext_t* _mc = &(_uc->uc_mcontext);
129 /* get register values */
131 void* pv = (void*) _mc->gp_regs[REG_PV];
132 void* sp = (void*) _mc->gp_regs[REG_SP];
133 void* ra = (void*) _mc->gp_regs[PT_LNK]; // The RA is correct for leag methods.
134 void* xpc =(void*) _mc->gp_regs[PT_NIP];
136 // Get the illegal-instruction.
137 uint32_t mcode = *((uint32_t*) xpc);
139 // Check if the trap instruction is valid.
140 // TODO Move this into patcher_handler.
141 if (patcher_is_valid_trap_instruction_at(xpc) == false) {
142 // Check if the PC has been patched during our way to this
143 // signal handler (see PR85).
144 if (patcher_is_patched_at(xpc) == true)
147 // We have a problem...
148 log_println("md_signal_handler_sigill: Unknown illegal instruction 0x%x at 0x%lx", mcode, xpc);
149 #if defined(ENABLE_DISASSEMBLER)
150 (void) disassinstr(xpc);
152 vm_abort("Aborting...");
155 // This signal is always a patcher.
156 int type = TRAP_PATCHER;
160 trap_handle(type, val, pv, sp, ra, xpc, _p);
164 /* md_signal_handler_sigusr2 ***************************************************
166 Signal handler for profiling sampling.
168 *******************************************************************************/
170 #if defined(ENABLE_THREADS)
171 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
180 _uc = (ucontext_t *) _p;
181 _mc = &(_uc->uc_mcontext);
183 pc = (u1 *) _mc->gp_regs[PT_NIP];
190 /* md_executionstate_read ******************************************************
192 Read the given context into an executionstate.
194 *******************************************************************************/
196 void md_executionstate_read(executionstate_t *es, void *context)
202 _uc = (ucontext_t *) context;
203 _mc = &(_uc->uc_mcontext);
205 /* read special registers */
206 es->pc = (u1 *) _mc->gp_regs[PT_NIP];
207 es->sp = (u1 *) _mc->gp_regs[REG_SP];
208 es->pv = (u1 *) _mc->gp_regs[REG_PV];
209 es->ra = (u1 *) _mc->gp_regs[PT_LNK];
211 /* read integer registers */
212 for (i = 0; i < INT_REG_CNT; i++)
213 es->intregs[i] = _mc->gp_regs[i];
215 /* read float registers */
216 /* Do not use the assignment operator '=', as the type of
217 * the _mc->fpregs[i] can cause invalid conversions. */
219 // The assertion below will fail because _mc->fp_regs[] also
220 // contains the "fpscr" register.
221 //assert(sizeof(_mc->fp_regs) == sizeof(es->fltregs));
222 os_memcpy(&es->fltregs, &_mc->fp_regs, sizeof(es->fltregs));
226 /* md_executionstate_write *****************************************************
228 Write the given executionstate back to the context.
230 *******************************************************************************/
232 void md_executionstate_write(executionstate_t *es, void *context)
238 _uc = (ucontext_t *) context;
239 _mc = &(_uc->uc_mcontext);
241 /* write integer registers */
242 for (i = 0; i < INT_REG_CNT; i++)
243 _mc->gp_regs[i] = es->intregs[i];
245 /* write float registers */
246 /* Do not use the assignment operator '=', as the type of
247 * the _mc->fpregs[i] can cause invalid conversions. */
249 // The assertion below will fail because _mc->fp_regs[] also
250 // contains the "fpscr" register.
251 //assert(sizeof(_mc->fp_regs) == sizeof(es->fltregs));
252 os_memcpy(&_mc->fp_regs, &es->fltregs, sizeof(es->fltregs));
254 /* write special registers */
255 _mc->gp_regs[PT_NIP] = (ptrint) es->pc;
256 _mc->gp_regs[REG_SP] = (ptrint) es->sp;
257 _mc->gp_regs[REG_PV] = (ptrint) es->pv;
258 _mc->gp_regs[PT_LNK] = (ptrint) es->ra;
263 * These are local overrides for various environment variables in Emacs.
264 * Please do not remove this and leave it at the end of the file, where
265 * Emacs will automagically detect them.
266 * ---------------------------------------------------------------------
269 * indent-tabs-mode: t