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"
44 #include "vm/jit/asmpart.h"
46 #if defined(ENABLE_PROFILING)
47 # include "vm/jit/optimizing/profile.h"
50 #include "vm/jit/stacktrace.h"
53 /* md_signal_handler_sigsegv ***************************************************
55 Signal handler for hardware-exceptions.
57 *******************************************************************************/
59 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
63 unsigned long *_gregs;
77 _uc = (ucontext_t *) _p;
79 #if defined(__UCLIBC__)
80 _mc = &(_uc->uc_mcontext);
81 _gregs = _mc->regs->gpr;
83 _mc = _uc->uc_mcontext.uc_regs;
87 pv = (u1 *) _gregs[REG_PV];
88 sp = (u1 *) _gregs[REG_SP];
89 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
90 xpc = (u1 *) _gregs[PT_NIP];
92 /* get exception-throwing instruction */
94 mcode = *((u4 *) xpc);
96 s1 = M_INSTR_OP2_IMM_A(mcode);
97 disp = M_INSTR_OP2_IMM_I(mcode);
98 d = M_INSTR_OP2_IMM_D(mcode);
102 /* check for special-load */
104 if (s1 == REG_ZERO) {
105 /* we use the exception type as load displacement */
109 if (type == EXCEPTION_HARDWARE_COMPILER) {
110 /* The XPC is the RA minus 4, because the RA points to the
111 instruction after the call. */
117 /* This is a normal NPE: addr must be NULL and the NPE-type
121 type = EXCEPTION_HARDWARE_NULLPOINTER;
124 vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
127 /* Handle the type. */
129 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
134 case EXCEPTION_HARDWARE_COMPILER:
136 _gregs[REG_PV] = (uintptr_t) p;
137 _gregs[PT_NIP] = (uintptr_t) p;
141 /* Get and set the PV from the parent Java method. */
143 pv = md_codegen_get_pv_from_pc(ra);
145 _gregs[REG_PV] = (uintptr_t) pv;
147 /* Get the exception object. */
149 p = builtin_retrieve_exception();
155 case EXCEPTION_HARDWARE_PATCHER:
162 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
163 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
164 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
169 /* md_signal_handler_sigtrap ***************************************************
171 Signal handler for hardware-traps.
173 *******************************************************************************/
175 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
179 unsigned long *_gregs;
190 _uc = (ucontext_t *) _p;
192 #if defined(__UCLIBC__)
193 _mc = &(_uc->uc_mcontext);
194 _gregs = _mc->regs->gpr;
196 _mc = _uc->uc_mcontext.uc_regs;
200 pv = (u1 *) _gregs[REG_PV];
201 sp = (u1 *) _gregs[REG_SP];
202 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
203 xpc = (u1 *) _gregs[PT_NIP];
205 /* get exception-throwing instruction */
207 mcode = *((u4 *) xpc);
209 s1 = M_OP3_GET_A(mcode);
211 /* for now we only handle ArrayIndexOutOfBoundsException */
213 type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
216 /* Handle the type. */
218 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
222 _gregs[REG_ITMP1_XPTR] = (intptr_t) p;
223 _gregs[REG_ITMP2_XPC] = (intptr_t) xpc;
224 _gregs[PT_NIP] = (intptr_t) asm_handle_exception;
228 /* md_signal_handler_sigusr1 ***************************************************
230 Signal handler for suspending threads.
232 *******************************************************************************/
234 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
235 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
239 unsigned long *_gregs;
243 _uc = (ucontext_t *) _p;
245 #if defined(__UCLIBC__)
246 _mc = &(_uc->uc_mcontext);
247 _gregs = _mc->regs->gpr;
249 _mc = _uc->uc_mcontext.uc_regs;
253 /* get the PC and SP for this thread */
255 pc = (u1 *) _gregs[PT_NIP];
256 sp = (u1 *) _gregs[REG_SP];
258 /* now suspend the current thread */
260 threads_suspend_ack(pc, sp);
265 /* md_signal_handler_sigusr2 ***************************************************
267 Signal handler for profiling sampling.
269 *******************************************************************************/
271 #if defined(ENABLE_THREADS)
272 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
277 unsigned long *_gregs;
282 _uc = (ucontext_t *) _p;
284 #if defined(__UCLIBC__)
285 _mc = &(_uc->uc_mcontext);
286 _gregs = _mc->regs->gpr;
288 _mc = _uc->uc_mcontext.uc_regs;
292 pc = (u1 *) _gregs[PT_NIP];
299 /* md_replace_executionstate_read **********************************************
301 Read the given context into an executionstate for Replacement.
303 *******************************************************************************/
305 #if defined(ENABLE_REPLACEMENT)
306 void md_replace_executionstate_read(executionstate_t *es, void *context)
310 unsigned long *_gregs;
313 _uc = (ucontext_t *) context;
315 #if defined(__UCLIBC__)
316 #error Please port md_replace_executionstate_read to __UCLIBC__
318 _mc = _uc->uc_mcontext.uc_regs;
322 /* read special registers */
323 es->pc = (u1 *) _gregs[PT_NIP];
324 es->sp = (u1 *) _gregs[REG_SP];
325 es->pv = (u1 *) _gregs[REG_PV];
326 es->ra = (u1 *) _gregs[PT_LNK];
328 /* read integer registers */
329 for (i = 0; i < INT_REG_CNT; i++)
330 es->intregs[i] = _gregs[i];
332 /* read float registers */
333 /* Do not use the assignment operator '=', as the type of
334 * the _mc->fpregs[i] can cause invalid conversions. */
336 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
337 memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
342 /* md_replace_executionstate_write *********************************************
344 Write the given executionstate back to the context for Replacement.
346 *******************************************************************************/
348 #if defined(ENABLE_REPLACEMENT)
349 void md_replace_executionstate_write(executionstate_t *es, void *context)
353 unsigned long *_gregs;
356 _uc = (ucontext_t *) context;
358 #if defined(__UCLIBC__)
359 #error Please port md_replace_executionstate_read to __UCLIBC__
361 _mc = _uc->uc_mcontext.uc_regs;
365 /* write integer registers */
366 for (i = 0; i < INT_REG_CNT; i++)
367 _gregs[i] = es->intregs[i];
369 /* write float registers */
370 /* Do not use the assignment operator '=', as the type of
371 * the _mc->fpregs[i] can cause invalid conversions. */
373 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
374 memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
376 /* write special registers */
377 _gregs[PT_NIP] = (ptrint) es->pc;
378 _gregs[REG_SP] = (ptrint) es->sp;
379 _gregs[REG_PV] = (ptrint) es->pv;
380 _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