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.hpp"
40 #include "vm/jit/builtin.hpp"
41 #include "vm/signallocal.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
47 #if defined(ENABLE_PROFILING)
48 # include "vm/jit/optimizing/profile.h"
51 #include "vm/jit/trap.h"
54 /* md_signal_handler_sigsegv ***************************************************
56 Signal handler for hardware-exceptions.
58 *******************************************************************************/
60 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
64 unsigned long *_gregs;
78 _uc = (ucontext_t *) _p;
80 #if defined(__UCLIBC__)
81 _mc = &(_uc->uc_mcontext);
82 _gregs = _mc->regs->gpr;
84 _mc = _uc->uc_mcontext.uc_regs;
88 pv = (u1 *) _gregs[REG_PV];
89 sp = (u1 *) _gregs[REG_SP];
90 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
91 xpc = (u1 *) _gregs[PT_NIP];
93 /* get exception-throwing instruction */
95 mcode = *((u4 *) xpc);
97 s1 = M_INSTR_OP2_IMM_A(mcode);
98 disp = M_INSTR_OP2_IMM_I(mcode);
99 d = M_INSTR_OP2_IMM_D(mcode);
103 /* check for special-load */
105 if (s1 == REG_ZERO) {
106 /* we use the exception type as load displacement */
110 if (type == TRAP_COMPILER) {
111 /* The XPC is the RA minus 4, because the RA points to the
112 instruction after the call. */
118 /* This is a normal NPE: addr must be NULL and the NPE-type
125 /* Handle the trap. */
127 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
134 _gregs[REG_PV] = (uintptr_t) p;
135 _gregs[PT_NIP] = (uintptr_t) p;
139 /* Get and set the PV from the parent Java method. */
141 pv = md_codegen_get_pv_from_pc(ra);
143 _gregs[REG_PV] = (uintptr_t) pv;
145 /* Get the exception object. */
147 p = builtin_retrieve_exception();
154 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
155 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
156 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
162 * Signal handler for patcher calls.
164 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
166 ucontext_t* _uc = (ucontext_t*) _p;
168 unsigned long* _gregs;
170 #if defined(__UCLIBC__)
171 _mc = &(_uc->uc_mcontext);
172 _gregs = _mc->regs->gpr;
174 _mc = _uc->uc_mcontext.uc_regs;
178 /* get register values */
180 void* pv = (void*) _gregs[REG_PV];
181 void* sp = (void*) _gregs[REG_SP];
182 void* ra = (void*) _gregs[PT_LNK]; // The RA is correct for leag methods.
183 void* xpc =(void*) _gregs[PT_NIP];
185 // This signal is always a patcher.
186 int type = TRAP_PATCHER;
190 void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
192 // Set registers if we have an exception, continue execution
195 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
196 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
197 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
202 /* md_signal_handler_sigtrap ***************************************************
204 Signal handler for hardware-traps.
206 *******************************************************************************/
208 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
212 unsigned long *_gregs;
223 _uc = (ucontext_t *) _p;
225 #if defined(__UCLIBC__)
226 _mc = &(_uc->uc_mcontext);
227 _gregs = _mc->regs->gpr;
229 _mc = _uc->uc_mcontext.uc_regs;
233 pv = (u1 *) _gregs[REG_PV];
234 sp = (u1 *) _gregs[REG_SP];
235 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
236 xpc = (u1 *) _gregs[PT_NIP];
238 /* get exception-throwing instruction */
240 mcode = *((u4 *) xpc);
242 s1 = M_OP3_GET_A(mcode);
244 /* For now we only handle ArrayIndexOutOfBoundsException. */
246 type = TRAP_ArrayIndexOutOfBoundsException;
249 /* Handle the trap. */
251 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
255 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
256 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
257 _gregs[PT_NIP] = (uintptr_t) asm_handle_exception;
261 /* md_signal_handler_sigusr1 ***************************************************
263 Signal handler for suspending threads.
265 *******************************************************************************/
267 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
268 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
272 unsigned long *_gregs;
276 _uc = (ucontext_t *) _p;
278 #if defined(__UCLIBC__)
279 _mc = &(_uc->uc_mcontext);
280 _gregs = _mc->regs->gpr;
282 _mc = _uc->uc_mcontext.uc_regs;
286 /* get the PC and SP for this thread */
288 pc = (u1 *) _gregs[PT_NIP];
289 sp = (u1 *) _gregs[REG_SP];
291 /* now suspend the current thread */
293 threads_suspend_ack(pc, sp);
298 /* md_signal_handler_sigusr2 ***************************************************
300 Signal handler for profiling sampling.
302 *******************************************************************************/
304 #if defined(ENABLE_THREADS)
305 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
310 unsigned long *_gregs;
315 _uc = (ucontext_t *) _p;
317 #if defined(__UCLIBC__)
318 _mc = &(_uc->uc_mcontext);
319 _gregs = _mc->regs->gpr;
321 _mc = _uc->uc_mcontext.uc_regs;
325 pc = (u1 *) _gregs[PT_NIP];
332 /* md_executionstate_read ******************************************************
334 Read the given context into an executionstate.
336 *******************************************************************************/
338 void md_executionstate_read(executionstate_t *es, void *context)
342 unsigned long *_gregs;
345 _uc = (ucontext_t *) context;
347 #if defined(__UCLIBC__)
348 #error Please port md_executionstate_read to __UCLIBC__
350 _mc = _uc->uc_mcontext.uc_regs;
354 /* read special registers */
355 es->pc = (u1 *) _gregs[PT_NIP];
356 es->sp = (u1 *) _gregs[REG_SP];
357 es->pv = (u1 *) _gregs[REG_PV];
358 es->ra = (u1 *) _gregs[PT_LNK];
360 /* read integer registers */
361 for (i = 0; i < INT_REG_CNT; i++)
362 es->intregs[i] = _gregs[i];
364 /* read float registers */
365 /* Do not use the assignment operator '=', as the type of
366 * the _mc->fpregs[i] can cause invalid conversions. */
368 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
369 os_memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
373 /* md_executionstate_write *****************************************************
375 Write the given executionstate back to the context.
377 *******************************************************************************/
379 void md_executionstate_write(executionstate_t *es, void *context)
383 unsigned long *_gregs;
386 _uc = (ucontext_t *) context;
388 #if defined(__UCLIBC__)
389 #error Please port md_executionstate_write to __UCLIBC__
391 _mc = _uc->uc_mcontext.uc_regs;
395 /* write integer registers */
396 for (i = 0; i < INT_REG_CNT; i++)
397 _gregs[i] = es->intregs[i];
399 /* write float registers */
400 /* Do not use the assignment operator '=', as the type of
401 * the _mc->fpregs[i] can cause invalid conversions. */
403 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
404 os_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
406 /* write special registers */
407 _gregs[PT_NIP] = (ptrint) es->pc;
408 _gregs[REG_SP] = (ptrint) es->sp;
409 _gregs[REG_PV] = (ptrint) es->pv;
410 _gregs[PT_LNK] = (ptrint) es->ra;
415 * These are local overrides for various environment variables in Emacs.
416 * Please do not remove this and leave it at the end of the file, where
417 * Emacs will automagically detect them.
418 * ---------------------------------------------------------------------
421 * indent-tabs-mode: t