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"
40 #include "mm/gc-common.h"
41 #include "mm/memory.h"
43 #include "vm/exceptions.h"
44 #include "vm/signallocal.h"
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/stacktrace.h"
49 #include "vm/jit/trap.h"
52 /* md_init *********************************************************************
54 Do some machine dependent initialization.
56 *******************************************************************************/
60 /* The Boehm GC initialization blocks the SIGSEGV signal. So we do
61 a dummy allocation here to ensure that the GC is
64 #if defined(ENABLE_GC_BOEHM)
69 /* Turn off flush-to-zero */
73 n.fc_word = get_fpc_csr();
74 n.fc_struct.flush = 0;
75 set_fpc_csr(n.fc_word);
81 /* md_signal_handler_sigsegv ***************************************************
83 NullPointerException signal handler for hardware null pointer
86 *******************************************************************************/
88 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
107 _uc = (struct ucontext *) _p;
108 _mc = &_uc->uc_mcontext;
110 #if defined(__UCLIBC__)
111 _gregs = _mc->gpregs;
116 /* In glibc's ucontext.h the registers are defined as long long,
117 even for MIPS32, so we cast them. This is not the case for
120 pv = (u1 *) (ptrint) _gregs[REG_PV];
121 sp = (u1 *) (ptrint) _gregs[REG_SP];
122 ra = (u1 *) (ptrint) _gregs[REG_RA]; /* this is correct for leafs */
124 #if !defined(__UCLIBC__)
125 # if ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 5))
126 /* NOTE: We only need this for pre glibc-2.5. */
128 xpc = (u1 *) (ptrint) _mc->pc;
130 /* get the cause of this exception */
134 /* check the cause to find the faulting instruction */
136 /* TODO: use defines for that stuff */
138 switch (cause & 0x0000003c) {
140 /* TLBL: XPC is ok */
144 /* AdEL: XPC is of the following instruction */
149 xpc = (u1 *) (ptrint) _mc->pc;
152 xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
155 /* get exception-throwing instruction */
157 mcode = *((u4 *) xpc);
159 d = M_ITYPE_GET_RT(mcode);
160 s1 = M_ITYPE_GET_RS(mcode);
161 disp = M_ITYPE_GET_IMM(mcode);
163 /* check for special-load */
165 if (s1 == REG_ZERO) {
166 /* we use the exception type as load displacement */
171 if (type == TRAP_COMPILER) {
172 /* The XPC is the RA minus 4, because the RA points to the
173 instruction after the call. */
179 /* This is a normal NPE: addr must be NULL and the NPE-type
187 /* Handle the trap. */
189 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
196 _gregs[REG_PV] = (uintptr_t) p;
197 #if defined(__UCLIBC__)
198 _gregs[CTX_EPC] = (uintptr_t) p;
200 _mc->pc = (uintptr_t) p;
205 /* Get and set the PV from the parent Java method. */
207 pv = md_codegen_get_pv_from_pc(ra);
209 _gregs[REG_PV] = (uintptr_t) pv;
211 /* Get the exception object. */
213 p = builtin_retrieve_exception();
221 /* We set the PC again because the cause may have changed
224 #if defined(__UCLIBC__)
225 _gregs[CTX_EPC] = (uintptr_t) xpc;
227 _mc->pc = (uintptr_t) xpc;
235 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
236 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
237 #if defined(__UCLIBC__)
238 _gregs[CTX_EPC] = (uintptr_t) asm_handle_exception;
240 _mc->pc = (uintptr_t) asm_handle_exception;
246 /* md_signal_handler_sigusr2 ***************************************************
250 *******************************************************************************/
252 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
258 * Read the given context into an executionstate.
260 * @param es execution state
261 * @param context machine context
263 void md_executionstate_read(executionstate_t* es, void* context)
270 vm_abort("md_executionstate_read: PLEASE REVISE ME!");
272 _uc = (ucontext_t*) context;
273 _mc = &_uc->uc_mcontext;
275 #if defined(__UCLIBC__)
276 _gregs = _mc->gpregs;
281 /* Read special registers. */
283 /* In glibc's ucontext.h the registers are defined as long long,
284 even for MIPS32, so we cast them. This is not the case for
287 #if defined(__UCLIBC__)
288 es->pc = _gregs[CTX_EPC];
290 es->pc = (void*) (uintptr_t) _mc->pc;
293 es->sp = (void*) (uintptr_t) _gregs[REG_SP];
294 es->pv = (void*) (uintptr_t) _gregs[REG_PV];
295 es->ra = (void*) (uintptr_t) _gregs[REG_RA];
297 /* Read integer registers. */
299 for (i = 0; i < INT_REG_CNT; i++)
300 es->intregs[i] = _gregs[i];
302 /* Read float registers. */
304 /* Do not use the assignment operator '=', as the type of the
305 _mc->fpregs[i] can cause invalid conversions. */
307 assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
308 system_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
313 * Write the given executionstate back to the context.
315 * @param es execution state
316 * @param context machine context
318 void md_executionstate_write(executionstate_t* es, void* context)
325 vm_abort("md_executionstate_write: PLEASE REVISE ME!");
327 _uc = (ucontext_t *) context;
328 _mc = &_uc->uc_mcontext;
330 /* Write integer registers. */
332 for (i = 0; i < INT_REG_CNT; i++)
333 _gregs[i] = es->intregs[i];
335 /* Write float registers. */
337 /* Do not use the assignment operator '=', as the type of the
338 _mc->fpregs[i] can cause invalid conversions. */
340 assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
341 system_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
343 /* Write special registers. */
345 #if defined(__UCLIBC__)
346 _gregs[CTX_EPC] = es->pc;
348 _mc->pc = (uintptr_t) es->pc;
351 _gregs[REG_SP] = (uintptr_t) es->sp;
352 _gregs[REG_PV] = (uintptr_t) es->pv;
353 _gregs[REG_RA] = (uintptr_t) es->ra;
357 /* md_critical_section_restart *************************************************
359 Search the critical sections tree for a matching section and set
360 the PC to the restart point, if necessary.
362 *******************************************************************************/
364 #if defined(ENABLE_THREADS)
365 void md_critical_section_restart(ucontext_t *_uc)
371 _mc = &_uc->uc_mcontext;
373 #if defined(__UCLIBC__)
374 pc = (u1 *) (ptrint) _mc->gpregs[CTX_EPC];
376 pc = (u1 *) (ptrint) _mc->pc;
379 npc = critical_find_restart_point(pc);
382 #if defined(__UCLIBC__)
383 _mc->gpregs[CTX_EPC] = (ptrint) npc;
385 _mc->pc = (ptrint) npc;
393 * These are local overrides for various environment variables in Emacs.
394 * Please do not remove this and leave it at the end of the file, where
395 * Emacs will automagically detect them.
396 * ---------------------------------------------------------------------
399 * indent-tabs-mode: t
403 * vim:noexpandtab:sw=4:ts=4: