1 /* src/vm/jit/powerpc/linux/md-os.c - machine dependent PowerPC Linux functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
36 #include "vm/jit/powerpc/codegen.h"
37 #include "vm/jit/powerpc/md.h"
38 #include "vm/jit/powerpc/linux/md-abi.h"
40 #if defined(ENABLE_THREADS)
41 # include "threads/native/threads.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/signallocal.h"
47 #include "vm/stringlocal.h"
48 #include "vm/jit/asmpart.h"
50 #if defined(ENABLE_PROFILING)
51 # include "vm/jit/optimizing/profile.h"
54 #include "vm/jit/stacktrace.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_replace_executionstate_read **********************************************
305 Read the given context into an executionstate for Replacement.
307 *******************************************************************************/
309 #if defined(ENABLE_REPLACEMENT)
310 void md_replace_executionstate_read(executionstate_t *es, void *context)
314 unsigned long *_gregs;
317 _uc = (ucontext_t *) context;
319 #if defined(__UCLIBC__)
320 #error Please port md_replace_executionstate_read to __UCLIBC__
322 _mc = _uc->uc_mcontext.uc_regs;
326 /* read special registers */
327 es->pc = (u1 *) _gregs[PT_NIP];
328 es->sp = (u1 *) _gregs[REG_SP];
329 es->pv = (u1 *) _gregs[REG_PV];
330 es->ra = (u1 *) _gregs[PT_LNK];
332 /* read integer registers */
333 for (i = 0; i < INT_REG_CNT; i++)
334 es->intregs[i] = _gregs[i];
336 /* read float registers */
337 /* Do not use the assignment operator '=', as the type of
338 * the _mc->fpregs[i] can cause invalid conversions. */
340 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
341 memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
346 /* md_replace_executionstate_write *********************************************
348 Write the given executionstate back to the context for Replacement.
350 *******************************************************************************/
352 #if defined(ENABLE_REPLACEMENT)
353 void md_replace_executionstate_write(executionstate_t *es, void *context)
357 unsigned long *_gregs;
360 _uc = (ucontext_t *) context;
362 #if defined(__UCLIBC__)
363 #error Please port md_replace_executionstate_read to __UCLIBC__
365 _mc = _uc->uc_mcontext.uc_regs;
369 /* write integer registers */
370 for (i = 0; i < INT_REG_CNT; i++)
371 _gregs[i] = es->intregs[i];
373 /* write float registers */
374 /* Do not use the assignment operator '=', as the type of
375 * the _mc->fpregs[i] can cause invalid conversions. */
377 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
378 memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
380 /* write special registers */
381 _gregs[PT_NIP] = (ptrint) es->pc;
382 _gregs[REG_SP] = (ptrint) es->sp;
383 _gregs[REG_PV] = (ptrint) es->pv;
384 _gregs[PT_LNK] = (ptrint) es->ra;
389 /* md_critical_section_restart *************************************************
391 Search the critical sections tree for a matching section and set
392 the PC to the restart point, if necessary.
394 *******************************************************************************/
396 #if defined(ENABLE_THREADS)
397 void md_critical_section_restart(ucontext_t *_uc)
400 unsigned long *_gregs;
404 #if defined(__UCLIBC__)
405 _mc = &(_uc->uc_mcontext);
406 _gregs = _mc->regs->gpr;
408 _mc = _uc->uc_mcontext.uc_regs;
412 pc = (u1 *) _gregs[PT_NIP];
414 npc = critical_find_restart_point(pc);
417 _gregs[PT_NIP] = (ptrint) npc;
423 * These are local overrides for various environment variables in Emacs.
424 * Please do not remove this and leave it at the end of the file, where
425 * Emacs will automagically detect them.
426 * ---------------------------------------------------------------------
429 * indent-tabs-mode: t