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.h"
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/signallocal.h"
43 #include "vm/stringlocal.h"
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/stacktrace.h"
53 #include "vm/jit/trap.h"
55 #include "vmcore/system.h"
58 /* md_signal_handler_sigsegv ***************************************************
60 Signal handler for hardware-exceptions.
62 *******************************************************************************/
64 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
68 unsigned long *_gregs;
82 _uc = (ucontext_t *) _p;
84 #if defined(__UCLIBC__)
85 _mc = &(_uc->uc_mcontext);
86 _gregs = _mc->regs->gpr;
88 _mc = _uc->uc_mcontext.uc_regs;
92 pv = (u1 *) _gregs[REG_PV];
93 sp = (u1 *) _gregs[REG_SP];
94 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
95 xpc = (u1 *) _gregs[PT_NIP];
97 /* get exception-throwing instruction */
99 mcode = *((u4 *) xpc);
101 s1 = M_INSTR_OP2_IMM_A(mcode);
102 disp = M_INSTR_OP2_IMM_I(mcode);
103 d = M_INSTR_OP2_IMM_D(mcode);
107 /* check for special-load */
109 if (s1 == REG_ZERO) {
110 /* we use the exception type as load displacement */
114 if (type == TRAP_COMPILER) {
115 /* The XPC is the RA minus 4, because the RA points to the
116 instruction after the call. */
122 /* This is a normal NPE: addr must be NULL and the NPE-type
129 /* Handle the trap. */
131 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
138 _gregs[REG_PV] = (uintptr_t) p;
139 _gregs[PT_NIP] = (uintptr_t) p;
143 /* Get and set the PV from the parent Java method. */
145 pv = md_codegen_get_pv_from_pc(ra);
147 _gregs[REG_PV] = (uintptr_t) pv;
149 /* Get the exception object. */
151 p = builtin_retrieve_exception();
164 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
165 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
166 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
171 /* md_signal_handler_sigtrap ***************************************************
173 Signal handler for hardware-traps.
175 *******************************************************************************/
177 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
181 unsigned long *_gregs;
192 _uc = (ucontext_t *) _p;
194 #if defined(__UCLIBC__)
195 _mc = &(_uc->uc_mcontext);
196 _gregs = _mc->regs->gpr;
198 _mc = _uc->uc_mcontext.uc_regs;
202 pv = (u1 *) _gregs[REG_PV];
203 sp = (u1 *) _gregs[REG_SP];
204 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
205 xpc = (u1 *) _gregs[PT_NIP];
207 /* get exception-throwing instruction */
209 mcode = *((u4 *) xpc);
211 s1 = M_OP3_GET_A(mcode);
213 /* For now we only handle ArrayIndexOutOfBoundsException. */
215 type = TRAP_ArrayIndexOutOfBoundsException;
218 /* Handle the trap. */
220 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
224 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
225 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
226 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
230 /* md_signal_handler_sigusr1 ***************************************************
232 Signal handler for suspending threads.
234 *******************************************************************************/
236 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
237 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
241 unsigned long *_gregs;
245 _uc = (ucontext_t *) _p;
247 #if defined(__UCLIBC__)
248 _mc = &(_uc->uc_mcontext);
249 _gregs = _mc->regs->gpr;
251 _mc = _uc->uc_mcontext.uc_regs;
255 /* get the PC and SP for this thread */
257 pc = (u1 *) _gregs[PT_NIP];
258 sp = (u1 *) _gregs[REG_SP];
260 /* now suspend the current thread */
262 threads_suspend_ack(pc, sp);
267 /* md_signal_handler_sigusr2 ***************************************************
269 Signal handler for profiling sampling.
271 *******************************************************************************/
273 #if defined(ENABLE_THREADS)
274 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
279 unsigned long *_gregs;
284 _uc = (ucontext_t *) _p;
286 #if defined(__UCLIBC__)
287 _mc = &(_uc->uc_mcontext);
288 _gregs = _mc->regs->gpr;
290 _mc = _uc->uc_mcontext.uc_regs;
294 pc = (u1 *) _gregs[PT_NIP];
301 /* md_executionstate_read ******************************************************
303 Read the given context into an executionstate.
305 *******************************************************************************/
307 void md_executionstate_read(executionstate_t *es, void *context)
311 unsigned long *_gregs;
314 _uc = (ucontext_t *) context;
316 #if defined(__UCLIBC__)
317 #error Please port md_executionstate_read to __UCLIBC__
319 _mc = _uc->uc_mcontext.uc_regs;
323 /* read special registers */
324 es->pc = (u1 *) _gregs[PT_NIP];
325 es->sp = (u1 *) _gregs[REG_SP];
326 es->pv = (u1 *) _gregs[REG_PV];
327 es->ra = (u1 *) _gregs[PT_LNK];
329 /* read integer registers */
330 for (i = 0; i < INT_REG_CNT; i++)
331 es->intregs[i] = _gregs[i];
333 /* read float registers */
334 /* Do not use the assignment operator '=', as the type of
335 * the _mc->fpregs[i] can cause invalid conversions. */
337 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
338 system_memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
342 /* md_executionstate_write *****************************************************
344 Write the given executionstate back to the context.
346 *******************************************************************************/
348 void md_executionstate_write(executionstate_t *es, void *context)
352 unsigned long *_gregs;
355 _uc = (ucontext_t *) context;
357 #if defined(__UCLIBC__)
358 #error Please port md_executionstate_write to __UCLIBC__
360 _mc = _uc->uc_mcontext.uc_regs;
364 /* write integer registers */
365 for (i = 0; i < INT_REG_CNT; i++)
366 _gregs[i] = es->intregs[i];
368 /* write float registers */
369 /* Do not use the assignment operator '=', as the type of
370 * the _mc->fpregs[i] can cause invalid conversions. */
372 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
373 system_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
375 /* write special registers */
376 _gregs[PT_NIP] = (ptrint) es->pc;
377 _gregs[REG_SP] = (ptrint) es->sp;
378 _gregs[REG_PV] = (ptrint) es->pv;
379 _gregs[PT_LNK] = (ptrint) es->ra;
384 * These are local overrides for various environment variables in Emacs.
385 * Please do not remove this and leave it at the end of the file, where
386 * Emacs will automagically detect them.
387 * ---------------------------------------------------------------------
390 * indent-tabs-mode: t