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
5 Copyright (C) 2008 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "vm/jit/powerpc/codegen.h"
36 #include "vm/jit/powerpc/md.h"
37 #include "vm/jit/powerpc/linux/md-abi.h"
39 #include "threads/thread.hpp"
41 #include "vm/jit/builtin.hpp"
42 #include "vm/signallocal.hpp"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/disass.h"
47 #include "vm/jit/executionstate.h"
49 #if defined(ENABLE_PROFILING)
50 # include "vm/jit/optimizing/profile.h"
53 #include "vm/jit/patcher-common.hpp"
54 #include "vm/jit/trap.hpp"
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;
80 _uc = (ucontext_t *) _p;
82 #if defined(__UCLIBC__)
83 _mc = &(_uc->uc_mcontext);
84 _gregs = _mc->regs->gpr;
86 _mc = _uc->uc_mcontext.uc_regs;
90 pv = (u1 *) _gregs[REG_PV];
91 sp = (u1 *) _gregs[REG_SP];
92 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
93 xpc = (u1 *) _gregs[PT_NIP];
95 /* get exception-throwing instruction */
97 mcode = *((u4 *) xpc);
99 s1 = M_INSTR_OP2_IMM_A(mcode);
100 disp = M_INSTR_OP2_IMM_I(mcode);
101 d = M_INSTR_OP2_IMM_D(mcode);
105 /* check for special-load */
107 if (s1 == REG_ZERO) {
108 /* we use the exception type as load displacement */
112 if (type == TRAP_COMPILER) {
113 /* The XPC is the RA minus 4, because the RA points to the
114 instruction after the call. */
120 /* This is a normal NPE: addr must be NULL and the NPE-type
127 /* Handle the trap. */
129 trap_handle(type, val, pv, sp, ra, xpc, _p);
134 * Signal handler for patcher calls.
136 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
138 ucontext_t* _uc = (ucontext_t*) _p;
140 unsigned long* _gregs;
142 #if defined(__UCLIBC__)
143 _mc = &(_uc->uc_mcontext);
144 _gregs = _mc->regs->gpr;
146 _mc = _uc->uc_mcontext.uc_regs;
150 /* get register values */
152 void* pv = (void*) _gregs[REG_PV];
153 void* sp = (void*) _gregs[REG_SP];
154 void* ra = (void*) _gregs[PT_LNK]; // The RA is correct for leag methods.
155 void* xpc =(void*) _gregs[PT_NIP];
157 // Get the illegal-instruction.
158 uint32_t mcode = *((uint32_t*) xpc);
160 // Check if the trap instruction is valid.
161 // TODO Move this into patcher_handler.
162 if (patcher_is_valid_trap_instruction_at(xpc) == false) {
163 // Check if the PC has been patched during our way to this
164 // signal handler (see PR85).
165 if (patcher_is_patched_at(xpc) == true)
168 // We have a problem...
169 log_println("md_signal_handler_sigill: Unknown illegal instruction 0x%x at 0x%lx", mcode, xpc);
170 #if defined(ENABLE_DISASSEMBLER)
171 (void) disassinstr(xpc);
173 vm_abort("Aborting...");
176 // This signal is always a patcher.
177 int type = TRAP_PATCHER;
181 trap_handle(type, val, pv, sp, ra, xpc, _p);
185 /* md_signal_handler_sigtrap ***************************************************
187 Signal handler for hardware-traps.
189 *******************************************************************************/
191 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
195 unsigned long *_gregs;
205 _uc = (ucontext_t *) _p;
207 #if defined(__UCLIBC__)
208 _mc = &(_uc->uc_mcontext);
209 _gregs = _mc->regs->gpr;
211 _mc = _uc->uc_mcontext.uc_regs;
215 pv = (u1 *) _gregs[REG_PV];
216 sp = (u1 *) _gregs[REG_SP];
217 ra = (u1 *) _gregs[PT_LNK]; /* this is correct for leafs */
218 xpc = (u1 *) _gregs[PT_NIP];
220 /* get exception-throwing instruction */
222 mcode = *((u4 *) xpc);
224 s1 = M_OP3_GET_A(mcode);
226 /* For now we only handle ArrayIndexOutOfBoundsException. */
228 type = TRAP_ArrayIndexOutOfBoundsException;
231 /* Handle the trap. */
233 trap_handle(type, val, pv, sp, ra, xpc, _p);
237 /* md_signal_handler_sigusr1 ***************************************************
239 Signal handler for suspending threads.
241 *******************************************************************************/
243 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
244 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
248 unsigned long *_gregs;
252 _uc = (ucontext_t *) _p;
254 #if defined(__UCLIBC__)
255 _mc = &(_uc->uc_mcontext);
256 _gregs = _mc->regs->gpr;
258 _mc = _uc->uc_mcontext.uc_regs;
262 /* get the PC and SP for this thread */
264 pc = (u1 *) _gregs[PT_NIP];
265 sp = (u1 *) _gregs[REG_SP];
267 /* now suspend the current thread */
269 threads_suspend_ack(pc, sp);
274 /* md_signal_handler_sigusr2 ***************************************************
276 Signal handler for profiling sampling.
278 *******************************************************************************/
280 #if defined(ENABLE_THREADS)
281 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
286 unsigned long *_gregs;
291 _uc = (ucontext_t *) _p;
293 #if defined(__UCLIBC__)
294 _mc = &(_uc->uc_mcontext);
295 _gregs = _mc->regs->gpr;
297 _mc = _uc->uc_mcontext.uc_regs;
301 pc = (u1 *) _gregs[PT_NIP];
308 /* md_executionstate_read ******************************************************
310 Read the given context into an executionstate.
312 *******************************************************************************/
314 void md_executionstate_read(executionstate_t *es, void *context)
318 unsigned long *_gregs;
321 _uc = (ucontext_t *) context;
323 #if defined(__UCLIBC__)
324 #error Please port md_executionstate_read to __UCLIBC__
326 _mc = _uc->uc_mcontext.uc_regs;
330 /* read special registers */
331 es->pc = (u1 *) _gregs[PT_NIP];
332 es->sp = (u1 *) _gregs[REG_SP];
333 es->pv = (u1 *) _gregs[REG_PV];
334 es->ra = (u1 *) _gregs[PT_LNK];
336 /* read integer registers */
337 for (i = 0; i < INT_REG_CNT; i++)
338 es->intregs[i] = _gregs[i];
340 /* read float registers */
341 /* Do not use the assignment operator '=', as the type of
342 * the _mc->fpregs[i] can cause invalid conversions. */
344 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
345 os_memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
349 /* md_executionstate_write *****************************************************
351 Write the given executionstate back to the context.
353 *******************************************************************************/
355 void md_executionstate_write(executionstate_t *es, void *context)
359 unsigned long *_gregs;
362 _uc = (ucontext_t *) context;
364 #if defined(__UCLIBC__)
365 #error Please port md_executionstate_write to __UCLIBC__
367 _mc = _uc->uc_mcontext.uc_regs;
371 /* write integer registers */
372 for (i = 0; i < INT_REG_CNT; i++)
373 _gregs[i] = es->intregs[i];
375 /* write float registers */
376 /* Do not use the assignment operator '=', as the type of
377 * the _mc->fpregs[i] can cause invalid conversions. */
379 assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
380 os_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
382 /* write special registers */
383 _gregs[PT_NIP] = (ptrint) es->pc;
384 _gregs[REG_SP] = (ptrint) es->sp;
385 _gregs[REG_PV] = (ptrint) es->pv;
386 _gregs[PT_LNK] = (ptrint) es->ra;
391 * These are local overrides for various environment variables in Emacs.
392 * Please do not remove this and leave it at the end of the file, where
393 * Emacs will automagically detect them.
394 * ---------------------------------------------------------------------
397 * indent-tabs-mode: t