1 /* src/vm/jit/mips/linux/md-os.c - machine dependent MIPS 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
29 #include <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
36 #include "vm/jit/mips/codegen.h"
37 #include "vm/jit/mips/md.h"
38 #include "vm/jit/mips/md-abi.h"
41 #include "mm/memory.hpp"
43 #include "vm/signallocal.hpp"
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/trap.hpp"
51 /* md_init *********************************************************************
53 Do some machine dependent initialization.
55 *******************************************************************************/
59 /* The Boehm GC initialization blocks the SIGSEGV signal. So we do
60 a dummy allocation here to ensure that the GC is
63 #if defined(ENABLE_GC_BOEHM)
68 /* Turn off flush-to-zero */
72 n.fc_word = get_fpc_csr();
73 n.fc_struct.flush = 0;
74 set_fpc_csr(n.fc_word);
80 /* md_signal_handler_sigsegv ***************************************************
82 NullPointerException signal handler for hardware null pointer
85 *******************************************************************************/
87 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
105 _uc = (struct ucontext *) _p;
106 _mc = &_uc->uc_mcontext;
108 #if defined(__UCLIBC__)
109 _gregs = _mc->gpregs;
114 /* In glibc's ucontext.h the registers are defined as long long,
115 even for MIPS32, so we cast them. This is not the case for
118 pv = (u1 *) (ptrint) _gregs[REG_PV];
119 sp = (u1 *) (ptrint) _gregs[REG_SP];
120 ra = (u1 *) (ptrint) _gregs[REG_RA]; /* this is correct for leafs */
122 #if !defined(__UCLIBC__)
123 # if ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 5))
124 /* NOTE: We only need this for pre glibc-2.5. */
126 xpc = (u1 *) (ptrint) _mc->pc;
128 /* get the cause of this exception */
132 /* check the cause to find the faulting instruction */
134 /* TODO: use defines for that stuff */
136 switch (cause & 0x0000003c) {
138 /* TLBL: XPC is ok */
142 /* AdEL: XPC is of the following instruction */
147 xpc = (u1 *) (ptrint) _mc->pc;
150 xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
153 /* get exception-throwing instruction */
155 mcode = *((u4 *) xpc);
157 d = M_ITYPE_GET_RT(mcode);
158 s1 = M_ITYPE_GET_RS(mcode);
159 disp = M_ITYPE_GET_IMM(mcode);
161 /* check for special-load */
163 if (s1 == REG_ZERO) {
164 /* we use the exception type as load displacement */
169 if (type == TRAP_COMPILER) {
170 /* The XPC is the RA minus 4, because the RA points to the
171 instruction after the call. */
177 /* This is a normal NPE: addr must be NULL and the NPE-type
185 /* Handle the trap. */
187 trap_handle(type, val, pv, sp, ra, xpc, _p);
192 * Signal handler for patcher calls.
194 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
196 ucontext_t* _uc = (struct ucontext *) _p;
197 mcontext_t* _mc = &_uc->uc_mcontext;
200 #if defined(__UCLIBC__)
201 _gregs = _mc->gpregs;
206 // In glibc's ucontext.h the registers are defined as long long
207 // int, even for MIPS32, so we cast them. This is not the case
209 void* pv = (void*) (uintptr_t) _gregs[REG_PV];
210 void* sp = (void*) (uintptr_t) _gregs[REG_SP];
211 void* ra = (void*) (uintptr_t) _gregs[REG_RA]; // The RA is correct for leaf methods.
213 #if defined(__UCLIBC__)
214 void* xpc = (void*) (uintptr_t) _gregs[CTX_EPC];
216 void* xpc = (void*) (uintptr_t) _mc->pc;
219 // This signal is always a patcher.
220 int type = TRAP_PATCHER;
224 void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
228 // We set the PC again because the cause may have changed the
230 #if defined(__UCLIBC__)
231 _gregs[CTX_EPC] = (uintptr_t) xpc;
233 _mc->pc = (uintptr_t) xpc;
239 /* md_signal_handler_sigusr2 ***************************************************
243 *******************************************************************************/
245 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
251 * Read the given context into an executionstate.
253 * @param es execution state
254 * @param context machine context
256 void md_executionstate_read(executionstate_t* es, void* context)
263 vm_abort("md_executionstate_read: PLEASE REVISE ME!");
265 _uc = (ucontext_t*) context;
266 _mc = &_uc->uc_mcontext;
268 #if defined(__UCLIBC__)
269 _gregs = _mc->gpregs;
274 /* Read special registers. */
276 /* In glibc's ucontext.h the registers are defined as long long,
277 even for MIPS32, so we cast them. This is not the case for
280 #if defined(__UCLIBC__)
281 es->pc = _gregs[CTX_EPC];
283 es->pc = (void*) (uintptr_t) _mc->pc;
286 es->sp = (void*) (uintptr_t) _gregs[REG_SP];
287 es->pv = (void*) (uintptr_t) _gregs[REG_PV];
288 es->ra = (void*) (uintptr_t) _gregs[REG_RA];
290 /* Read integer registers. */
292 for (i = 0; i < INT_REG_CNT; i++)
293 es->intregs[i] = _gregs[i];
295 /* Read float registers. */
297 /* Do not use the assignment operator '=', as the type of the
298 _mc->fpregs[i] can cause invalid conversions. */
300 assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
301 os_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
306 * Write the given executionstate back to the context.
308 * @param es execution state
309 * @param context machine context
311 void md_executionstate_write(executionstate_t* es, void* context)
318 vm_abort("md_executionstate_write: PLEASE REVISE ME!");
320 _uc = (ucontext_t *) context;
321 _mc = &_uc->uc_mcontext;
323 /* Write integer registers. */
325 for (i = 0; i < INT_REG_CNT; i++)
326 _gregs[i] = es->intregs[i];
328 /* Write float registers. */
330 /* Do not use the assignment operator '=', as the type of the
331 _mc->fpregs[i] can cause invalid conversions. */
333 assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
334 os_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
336 /* Write special registers. */
338 #if defined(__UCLIBC__)
339 _gregs[CTX_EPC] = es->pc;
341 _mc->pc = (uintptr_t) es->pc;
344 _gregs[REG_SP] = (uintptr_t) es->sp;
345 _gregs[REG_PV] = (uintptr_t) es->pv;
346 _gregs[REG_RA] = (uintptr_t) es->ra;
351 * These are local overrides for various environment variables in Emacs.
352 * Please do not remove this and leave it at the end of the file, where
353 * Emacs will automagically detect them.
354 * ---------------------------------------------------------------------
357 * indent-tabs-mode: t
361 * vim:noexpandtab:sw=4:ts=4: