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 trap_handle(type, val, pv, sp, ra, xpc, _p);
228 /* md_signal_handler_sigusr2 ***************************************************
232 *******************************************************************************/
234 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
240 * Read the given context into an executionstate.
242 * @param es execution state
243 * @param context machine context
245 void md_executionstate_read(executionstate_t* es, void* context)
252 vm_abort("md_executionstate_read: PLEASE REVISE ME!");
254 _uc = (ucontext_t*) context;
255 _mc = &_uc->uc_mcontext;
257 #if defined(__UCLIBC__)
258 _gregs = _mc->gpregs;
263 /* Read special registers. */
265 /* In glibc's ucontext.h the registers are defined as long long,
266 even for MIPS32, so we cast them. This is not the case for
269 #if defined(__UCLIBC__)
270 es->pc = _gregs[CTX_EPC];
272 es->pc = (void*) (uintptr_t) _mc->pc;
275 es->sp = (void*) (uintptr_t) _gregs[REG_SP];
276 es->pv = (void*) (uintptr_t) _gregs[REG_PV];
277 es->ra = (void*) (uintptr_t) _gregs[REG_RA];
279 /* Read integer registers. */
281 for (i = 0; i < INT_REG_CNT; i++)
282 es->intregs[i] = _gregs[i];
284 /* Read float registers. */
286 /* Do not use the assignment operator '=', as the type of the
287 _mc->fpregs[i] can cause invalid conversions. */
289 assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
290 os_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
295 * Write the given executionstate back to the context.
297 * @param es execution state
298 * @param context machine context
300 void md_executionstate_write(executionstate_t* es, void* context)
307 vm_abort("md_executionstate_write: PLEASE REVISE ME!");
309 _uc = (ucontext_t *) context;
310 _mc = &_uc->uc_mcontext;
312 /* Write integer registers. */
314 for (i = 0; i < INT_REG_CNT; i++)
315 _gregs[i] = es->intregs[i];
317 /* Write float registers. */
319 /* Do not use the assignment operator '=', as the type of the
320 _mc->fpregs[i] can cause invalid conversions. */
322 assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
323 os_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
325 /* Write special registers. */
327 #if defined(__UCLIBC__)
328 _gregs[CTX_EPC] = es->pc;
330 _mc->pc = (uintptr_t) es->pc;
333 _gregs[REG_SP] = (uintptr_t) es->sp;
334 _gregs[REG_PV] = (uintptr_t) es->pv;
335 _gregs[REG_RA] = (uintptr_t) es->ra;
340 * These are local overrides for various environment variables in Emacs.
341 * Please do not remove this and leave it at the end of the file, where
342 * Emacs will automagically detect them.
343 * ---------------------------------------------------------------------
346 * indent-tabs-mode: t
350 * vim:noexpandtab:sw=4:ts=4: