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 #if defined(ENABLE_THREADS)
39 # include "threads/native/threads.h"
42 #include "vm/builtin.h"
43 #include "vm/exceptions.h"
44 #include "vm/signallocal.h"
45 #include "vm/stringlocal.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/executionstate.h"
50 #if defined(ENABLE_PROFILING)
51 # include "vm/jit/optimizing/profile.h"
54 #include "vm/jit/stacktrace.h"
56 #include "vmcore/system.h"
59 /* md_signal_handler_sigsegv ***************************************************
61 Signal handler for hardware-exceptions.
63 *******************************************************************************/
65 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
69 unsigned long *_gregs;
83 _uc = (ucontext_t *) _p;
85 #if defined(__UCLIBC__)
86 _mc = &(_uc->uc_mcontext);
87 _gregs = _mc->regs->gpr;
89 _mc = _uc->uc_mcontext.uc_regs;
93 pv = (u1 *) _gregs[REG_PV];
94 sp = (u1 *) _gregs[REG_SP];
95 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
96 xpc = (u1 *) _gregs[PT_NIP];
98 /* get exception-throwing instruction */
100 mcode = *((u4 *) xpc);
102 s1 = M_INSTR_OP2_IMM_A(mcode);
103 disp = M_INSTR_OP2_IMM_I(mcode);
104 d = M_INSTR_OP2_IMM_D(mcode);
108 /* check for special-load */
110 if (s1 == REG_ZERO) {
111 /* we use the exception type as load displacement */
115 if (type == EXCEPTION_HARDWARE_COMPILER) {
116 /* The XPC is the RA minus 4, because the RA points to the
117 instruction after the call. */
123 /* This is a normal NPE: addr must be NULL and the NPE-type
127 type = EXCEPTION_HARDWARE_NULLPOINTER;
130 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
133 /* Handle the type. */
135 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
140 case EXCEPTION_HARDWARE_COMPILER:
142 _gregs[REG_PV] = (uintptr_t) p;
143 _gregs[PT_NIP] = (uintptr_t) p;
147 /* Get and set the PV from the parent Java method. */
149 pv = md_codegen_get_pv_from_pc(ra);
151 _gregs[REG_PV] = (uintptr_t) pv;
153 /* Get the exception object. */
155 p = builtin_retrieve_exception();
161 case EXCEPTION_HARDWARE_PATCHER:
168 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
169 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
170 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
175 /* md_signal_handler_sigtrap ***************************************************
177 Signal handler for hardware-traps.
179 *******************************************************************************/
181 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
185 unsigned long *_gregs;
196 _uc = (ucontext_t *) _p;
198 #if defined(__UCLIBC__)
199 _mc = &(_uc->uc_mcontext);
200 _gregs = _mc->regs->gpr;
202 _mc = _uc->uc_mcontext.uc_regs;
206 pv = (u1 *) _gregs[REG_PV];
207 sp = (u1 *) _gregs[REG_SP];
208 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
209 xpc = (u1 *) _gregs[PT_NIP];
211 /* get exception-throwing instruction */
213 mcode = *((u4 *) xpc);
215 s1 = M_OP3_GET_A(mcode);
217 /* for now we only handle ArrayIndexOutOfBoundsException */
219 type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
222 /* Handle the type. */
224 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
228 _gregs[REG_ITMP1_XPTR] = (intptr_t) p;
229 _gregs[REG_ITMP2_XPC] = (intptr_t) xpc;
230 _gregs[PT_NIP] = (intptr_t) asm_handle_exception;
234 /* md_signal_handler_sigusr1 ***************************************************
236 Signal handler for suspending threads.
238 *******************************************************************************/
240 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
241 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
245 unsigned long *_gregs;
249 _uc = (ucontext_t *) _p;
251 #if defined(__UCLIBC__)
252 _mc = &(_uc->uc_mcontext);
253 _gregs = _mc->regs->gpr;
255 _mc = _uc->uc_mcontext.uc_regs;
259 /* get the PC and SP for this thread */
261 pc = (u1 *) _gregs[PT_NIP];
262 sp = (u1 *) _gregs[REG_SP];
264 /* now suspend the current thread */
266 threads_suspend_ack(pc, sp);
271 /* md_signal_handler_sigusr2 ***************************************************
273 Signal handler for profiling sampling.
275 *******************************************************************************/
277 #if defined(ENABLE_THREADS)
278 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
283 unsigned long *_gregs;
288 _uc = (ucontext_t *) _p;
290 #if defined(__UCLIBC__)
291 _mc = &(_uc->uc_mcontext);
292 _gregs = _mc->regs->gpr;
294 _mc = _uc->uc_mcontext.uc_regs;
298 pc = (u1 *) _gregs[PT_NIP];
305 /* md_executionstate_read ******************************************************
307 Read the given context into an executionstate.
309 *******************************************************************************/
311 void md_executionstate_read(executionstate_t *es, void *context)
315 unsigned long *_gregs;
318 _uc = (ucontext_t *) context;
320 #if defined(__UCLIBC__)
321 #error Please port md_executionstate_read to __UCLIBC__
323 _mc = _uc->uc_mcontext.uc_regs;
327 /* read special registers */
328 es->pc = (u1 *) _gregs[PT_NIP];
329 es->sp = (u1 *) _gregs[REG_SP];
330 es->pv = (u1 *) _gregs[REG_PV];
331 es->ra = (u1 *) _gregs[PT_LNK];
333 /* read integer registers */
334 for (i = 0; i < INT_REG_CNT; i++)
335 es->intregs[i] = _gregs[i];
337 /* read float registers */
338 /* Do not use the assignment operator '=', as the type of
339 * the _mc->fpregs[i] can cause invalid conversions. */
341 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
342 system_memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
346 /* md_executionstate_write *****************************************************
348 Write the given executionstate back to the context.
350 *******************************************************************************/
352 void md_executionstate_write(executionstate_t *es, void *context)
356 unsigned long *_gregs;
359 _uc = (ucontext_t *) context;
361 #if defined(__UCLIBC__)
362 #error Please port md_executionstate_write to __UCLIBC__
364 _mc = _uc->uc_mcontext.uc_regs;
368 /* write integer registers */
369 for (i = 0; i < INT_REG_CNT; i++)
370 _gregs[i] = es->intregs[i];
372 /* write float registers */
373 /* Do not use the assignment operator '=', as the type of
374 * the _mc->fpregs[i] can cause invalid conversions. */
376 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
377 system_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
379 /* write special registers */
380 _gregs[PT_NIP] = (ptrint) es->pc;
381 _gregs[REG_SP] = (ptrint) es->sp;
382 _gregs[REG_PV] = (ptrint) es->pv;
383 _gregs[PT_LNK] = (ptrint) es->ra;
387 /* md_critical_section_restart *************************************************
389 Search the critical sections tree for a matching section and set
390 the PC to the restart point, if necessary.
392 *******************************************************************************/
394 #if defined(ENABLE_THREADS)
395 void md_critical_section_restart(ucontext_t *_uc)
398 unsigned long *_gregs;
402 #if defined(__UCLIBC__)
403 _mc = &(_uc->uc_mcontext);
404 _gregs = _mc->regs->gpr;
406 _mc = _uc->uc_mcontext.uc_regs;
410 pc = (u1 *) _gregs[PT_NIP];
412 npc = critical_find_restart_point(pc);
415 _gregs[PT_NIP] = (ptrint) npc;
421 * These are local overrides for various environment variables in Emacs.
422 * Please do not remove this and leave it at the end of the file, where
423 * Emacs will automagically detect them.
424 * ---------------------------------------------------------------------
427 * indent-tabs-mode: t