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"
54 #include "vmcore/system.h"
57 /* md_signal_handler_sigsegv ***************************************************
59 Signal handler for hardware-exceptions.
61 *******************************************************************************/
63 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
67 unsigned long *_gregs;
81 _uc = (ucontext_t *) _p;
83 #if defined(__UCLIBC__)
84 _mc = &(_uc->uc_mcontext);
85 _gregs = _mc->regs->gpr;
87 _mc = _uc->uc_mcontext.uc_regs;
91 pv = (u1 *) _gregs[REG_PV];
92 sp = (u1 *) _gregs[REG_SP];
93 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
94 xpc = (u1 *) _gregs[PT_NIP];
96 /* get exception-throwing instruction */
98 mcode = *((u4 *) xpc);
100 s1 = M_INSTR_OP2_IMM_A(mcode);
101 disp = M_INSTR_OP2_IMM_I(mcode);
102 d = M_INSTR_OP2_IMM_D(mcode);
106 /* check for special-load */
108 if (s1 == REG_ZERO) {
109 /* we use the exception type as load displacement */
113 if (type == EXCEPTION_HARDWARE_COMPILER) {
114 /* The XPC is the RA minus 4, because the RA points to the
115 instruction after the call. */
121 /* This is a normal NPE: addr must be NULL and the NPE-type
125 type = EXCEPTION_HARDWARE_NULLPOINTER;
128 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
131 /* Handle the type. */
133 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
138 case EXCEPTION_HARDWARE_COMPILER:
140 _gregs[REG_PV] = (uintptr_t) p;
141 _gregs[PT_NIP] = (uintptr_t) p;
145 /* Get and set the PV from the parent Java method. */
147 pv = md_codegen_get_pv_from_pc(ra);
149 _gregs[REG_PV] = (uintptr_t) pv;
151 /* Get the exception object. */
153 p = builtin_retrieve_exception();
159 case EXCEPTION_HARDWARE_PATCHER:
166 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
167 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
168 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
173 /* md_signal_handler_sigtrap ***************************************************
175 Signal handler for hardware-traps.
177 *******************************************************************************/
179 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
183 unsigned long *_gregs;
194 _uc = (ucontext_t *) _p;
196 #if defined(__UCLIBC__)
197 _mc = &(_uc->uc_mcontext);
198 _gregs = _mc->regs->gpr;
200 _mc = _uc->uc_mcontext.uc_regs;
204 pv = (u1 *) _gregs[REG_PV];
205 sp = (u1 *) _gregs[REG_SP];
206 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
207 xpc = (u1 *) _gregs[PT_NIP];
209 /* get exception-throwing instruction */
211 mcode = *((u4 *) xpc);
213 s1 = M_OP3_GET_A(mcode);
215 /* for now we only handle ArrayIndexOutOfBoundsException */
217 type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
220 /* Handle the type. */
222 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
226 _gregs[REG_ITMP1_XPTR] = (intptr_t) p;
227 _gregs[REG_ITMP2_XPC] = (intptr_t) xpc;
228 _gregs[PT_NIP] = (intptr_t) asm_handle_exception;
232 /* md_signal_handler_sigusr1 ***************************************************
234 Signal handler for suspending threads.
236 *******************************************************************************/
238 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
239 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
243 unsigned long *_gregs;
247 _uc = (ucontext_t *) _p;
249 #if defined(__UCLIBC__)
250 _mc = &(_uc->uc_mcontext);
251 _gregs = _mc->regs->gpr;
253 _mc = _uc->uc_mcontext.uc_regs;
257 /* get the PC and SP for this thread */
259 pc = (u1 *) _gregs[PT_NIP];
260 sp = (u1 *) _gregs[REG_SP];
262 /* now suspend the current thread */
264 threads_suspend_ack(pc, sp);
269 /* md_signal_handler_sigusr2 ***************************************************
271 Signal handler for profiling sampling.
273 *******************************************************************************/
275 #if defined(ENABLE_THREADS)
276 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
281 unsigned long *_gregs;
286 _uc = (ucontext_t *) _p;
288 #if defined(__UCLIBC__)
289 _mc = &(_uc->uc_mcontext);
290 _gregs = _mc->regs->gpr;
292 _mc = _uc->uc_mcontext.uc_regs;
296 pc = (u1 *) _gregs[PT_NIP];
303 /* md_executionstate_read ******************************************************
305 Read the given context into an executionstate.
307 *******************************************************************************/
309 void md_executionstate_read(executionstate_t *es, void *context)
313 unsigned long *_gregs;
316 _uc = (ucontext_t *) context;
318 #if defined(__UCLIBC__)
319 #error Please port md_executionstate_read to __UCLIBC__
321 _mc = _uc->uc_mcontext.uc_regs;
325 /* read special registers */
326 es->pc = (u1 *) _gregs[PT_NIP];
327 es->sp = (u1 *) _gregs[REG_SP];
328 es->pv = (u1 *) _gregs[REG_PV];
329 es->ra = (u1 *) _gregs[PT_LNK];
331 /* read integer registers */
332 for (i = 0; i < INT_REG_CNT; i++)
333 es->intregs[i] = _gregs[i];
335 /* read float registers */
336 /* Do not use the assignment operator '=', as the type of
337 * the _mc->fpregs[i] can cause invalid conversions. */
339 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
340 system_memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
344 /* md_executionstate_write *****************************************************
346 Write the given executionstate back to the context.
348 *******************************************************************************/
350 void md_executionstate_write(executionstate_t *es, void *context)
354 unsigned long *_gregs;
357 _uc = (ucontext_t *) context;
359 #if defined(__UCLIBC__)
360 #error Please port md_executionstate_write to __UCLIBC__
362 _mc = _uc->uc_mcontext.uc_regs;
366 /* write integer registers */
367 for (i = 0; i < INT_REG_CNT; i++)
368 _gregs[i] = es->intregs[i];
370 /* write float registers */
371 /* Do not use the assignment operator '=', as the type of
372 * the _mc->fpregs[i] can cause invalid conversions. */
374 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
375 system_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
377 /* write special registers */
378 _gregs[PT_NIP] = (ptrint) es->pc;
379 _gregs[REG_SP] = (ptrint) es->sp;
380 _gregs[REG_PV] = (ptrint) es->pv;
381 _gregs[PT_LNK] = (ptrint) es->ra;
385 /* md_critical_section_restart *************************************************
387 Search the critical sections tree for a matching section and set
388 the PC to the restart point, if necessary.
390 *******************************************************************************/
392 #if defined(ENABLE_THREADS)
393 void md_critical_section_restart(ucontext_t *_uc)
396 unsigned long *_gregs;
400 #if defined(__UCLIBC__)
401 _mc = &(_uc->uc_mcontext);
402 _gregs = _mc->regs->gpr;
404 _mc = _uc->uc_mcontext.uc_regs;
408 pc = (u1 *) _gregs[PT_NIP];
410 npc = critical_find_restart_point(pc);
413 _gregs[PT_NIP] = (ptrint) npc;
419 * These are local overrides for various environment variables in Emacs.
420 * Please do not remove this and leave it at the end of the file, where
421 * Emacs will automagically detect them.
422 * ---------------------------------------------------------------------
425 * indent-tabs-mode: t