1 /* src/vm/jit/powerpc/linux/md-os.c - machine dependent PowerPC Linux functions
3 Copyright (C) 1996-2005, 2006, 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
34 #include "vm/jit/powerpc/codegen.h"
35 #include "vm/jit/powerpc/md.h"
36 #include "vm/jit/powerpc/linux/md-abi.h"
38 #include "threads/thread.hpp"
40 #include "vm/jit/builtin.hpp"
41 #include "vm/signallocal.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
47 #if defined(ENABLE_PROFILING)
48 # include "vm/jit/optimizing/profile.h"
51 #include "vm/jit/trap.h"
54 /* md_signal_handler_sigsegv ***************************************************
56 Signal handler for hardware-exceptions.
58 *******************************************************************************/
60 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
64 unsigned long *_gregs;
78 _uc = (ucontext_t *) _p;
80 #if defined(__UCLIBC__)
81 _mc = &(_uc->uc_mcontext);
82 _gregs = _mc->regs->gpr;
84 _mc = _uc->uc_mcontext.uc_regs;
88 pv = (u1 *) _gregs[REG_PV];
89 sp = (u1 *) _gregs[REG_SP];
90 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
91 xpc = (u1 *) _gregs[PT_NIP];
93 /* get exception-throwing instruction */
95 mcode = *((u4 *) xpc);
97 s1 = M_INSTR_OP2_IMM_A(mcode);
98 disp = M_INSTR_OP2_IMM_I(mcode);
99 d = M_INSTR_OP2_IMM_D(mcode);
103 /* check for special-load */
105 if (s1 == REG_ZERO) {
106 /* we use the exception type as load displacement */
110 if (type == TRAP_COMPILER) {
111 /* The XPC is the RA minus 4, because the RA points to the
112 instruction after the call. */
118 /* This is a normal NPE: addr must be NULL and the NPE-type
125 /* Handle the trap. */
127 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
134 _gregs[REG_PV] = (uintptr_t) p;
135 _gregs[PT_NIP] = (uintptr_t) p;
139 /* Get and set the PV from the parent Java method. */
141 pv = md_codegen_get_pv_from_pc(ra);
143 _gregs[REG_PV] = (uintptr_t) pv;
145 /* Get the exception object. */
147 p = builtin_retrieve_exception();
160 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
161 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
162 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
167 /* md_signal_handler_sigtrap ***************************************************
169 Signal handler for hardware-traps.
171 *******************************************************************************/
173 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
177 unsigned long *_gregs;
188 _uc = (ucontext_t *) _p;
190 #if defined(__UCLIBC__)
191 _mc = &(_uc->uc_mcontext);
192 _gregs = _mc->regs->gpr;
194 _mc = _uc->uc_mcontext.uc_regs;
198 pv = (u1 *) _gregs[REG_PV];
199 sp = (u1 *) _gregs[REG_SP];
200 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
201 xpc = (u1 *) _gregs[PT_NIP];
203 /* get exception-throwing instruction */
205 mcode = *((u4 *) xpc);
207 s1 = M_OP3_GET_A(mcode);
209 /* For now we only handle ArrayIndexOutOfBoundsException. */
211 type = TRAP_ArrayIndexOutOfBoundsException;
214 /* Handle the trap. */
216 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
220 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
221 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
222 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
226 /* md_signal_handler_sigusr1 ***************************************************
228 Signal handler for suspending threads.
230 *******************************************************************************/
232 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
233 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
237 unsigned long *_gregs;
241 _uc = (ucontext_t *) _p;
243 #if defined(__UCLIBC__)
244 _mc = &(_uc->uc_mcontext);
245 _gregs = _mc->regs->gpr;
247 _mc = _uc->uc_mcontext.uc_regs;
251 /* get the PC and SP for this thread */
253 pc = (u1 *) _gregs[PT_NIP];
254 sp = (u1 *) _gregs[REG_SP];
256 /* now suspend the current thread */
258 threads_suspend_ack(pc, sp);
263 /* md_signal_handler_sigusr2 ***************************************************
265 Signal handler for profiling sampling.
267 *******************************************************************************/
269 #if defined(ENABLE_THREADS)
270 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
275 unsigned long *_gregs;
280 _uc = (ucontext_t *) _p;
282 #if defined(__UCLIBC__)
283 _mc = &(_uc->uc_mcontext);
284 _gregs = _mc->regs->gpr;
286 _mc = _uc->uc_mcontext.uc_regs;
290 pc = (u1 *) _gregs[PT_NIP];
297 /* md_executionstate_read ******************************************************
299 Read the given context into an executionstate.
301 *******************************************************************************/
303 void md_executionstate_read(executionstate_t *es, void *context)
307 unsigned long *_gregs;
310 _uc = (ucontext_t *) context;
312 #if defined(__UCLIBC__)
313 #error Please port md_executionstate_read to __UCLIBC__
315 _mc = _uc->uc_mcontext.uc_regs;
319 /* read special registers */
320 es->pc = (u1 *) _gregs[PT_NIP];
321 es->sp = (u1 *) _gregs[REG_SP];
322 es->pv = (u1 *) _gregs[REG_PV];
323 es->ra = (u1 *) _gregs[PT_LNK];
325 /* read integer registers */
326 for (i = 0; i < INT_REG_CNT; i++)
327 es->intregs[i] = _gregs[i];
329 /* read float registers */
330 /* Do not use the assignment operator '=', as the type of
331 * the _mc->fpregs[i] can cause invalid conversions. */
333 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
334 os_memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
338 /* md_executionstate_write *****************************************************
340 Write the given executionstate back to the context.
342 *******************************************************************************/
344 void md_executionstate_write(executionstate_t *es, void *context)
348 unsigned long *_gregs;
351 _uc = (ucontext_t *) context;
353 #if defined(__UCLIBC__)
354 #error Please port md_executionstate_write to __UCLIBC__
356 _mc = _uc->uc_mcontext.uc_regs;
360 /* write integer registers */
361 for (i = 0; i < INT_REG_CNT; i++)
362 _gregs[i] = es->intregs[i];
364 /* write float registers */
365 /* Do not use the assignment operator '=', as the type of
366 * the _mc->fpregs[i] can cause invalid conversions. */
368 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
369 os_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
371 /* write special registers */
372 _gregs[PT_NIP] = (ptrint) es->pc;
373 _gregs[REG_SP] = (ptrint) es->sp;
374 _gregs[REG_PV] = (ptrint) es->pv;
375 _gregs[PT_LNK] = (ptrint) es->ra;
380 * These are local overrides for various environment variables in Emacs.
381 * Please do not remove this and leave it at the end of the file, where
382 * Emacs will automagically detect them.
383 * ---------------------------------------------------------------------
386 * indent-tabs-mode: t