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.h"
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)
106 _uc = (struct ucontext *) _p;
107 _mc = &_uc->uc_mcontext;
109 #if defined(__UCLIBC__)
110 _gregs = _mc->gpregs;
115 /* In glibc's ucontext.h the registers are defined as long long,
116 even for MIPS32, so we cast them. This is not the case for
119 pv = (u1 *) (ptrint) _gregs[REG_PV];
120 sp = (u1 *) (ptrint) _gregs[REG_SP];
121 ra = (u1 *) (ptrint) _gregs[REG_RA]; /* this is correct for leafs */
123 #if !defined(__UCLIBC__)
124 # if ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 5))
125 /* NOTE: We only need this for pre glibc-2.5. */
127 xpc = (u1 *) (ptrint) _mc->pc;
129 /* get the cause of this exception */
133 /* check the cause to find the faulting instruction */
135 /* TODO: use defines for that stuff */
137 switch (cause & 0x0000003c) {
139 /* TLBL: XPC is ok */
143 /* AdEL: XPC is of the following instruction */
148 xpc = (u1 *) (ptrint) _mc->pc;
151 xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
154 /* get exception-throwing instruction */
156 mcode = *((u4 *) xpc);
158 d = M_ITYPE_GET_RT(mcode);
159 s1 = M_ITYPE_GET_RS(mcode);
160 disp = M_ITYPE_GET_IMM(mcode);
162 /* check for special-load */
164 if (s1 == REG_ZERO) {
165 /* we use the exception type as load displacement */
170 if (type == TRAP_COMPILER) {
171 /* The XPC is the RA minus 4, because the RA points to the
172 instruction after the call. */
178 /* This is a normal NPE: addr must be NULL and the NPE-type
186 /* Handle the trap. */
188 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
195 _gregs[REG_PV] = (uintptr_t) p;
196 #if defined(__UCLIBC__)
197 _gregs[CTX_EPC] = (uintptr_t) p;
199 _mc->pc = (uintptr_t) p;
204 /* Get and set the PV from the parent Java method. */
206 pv = md_codegen_get_pv_from_pc(ra);
208 _gregs[REG_PV] = (uintptr_t) pv;
210 /* Get the exception object. */
212 p = builtin_retrieve_exception();
219 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
220 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
221 #if defined(__UCLIBC__)
222 _gregs[CTX_EPC] = (uintptr_t) asm_handle_exception;
224 _mc->pc = (uintptr_t) asm_handle_exception;
231 * Signal handler for patcher calls.
233 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
235 ucontext_t* _uc = (struct ucontext *) _p;
236 mcontext_t* _mc = &_uc->uc_mcontext;
239 #if defined(__UCLIBC__)
240 _gregs = _mc->gpregs;
245 // In glibc's ucontext.h the registers are defined as long long
246 // int, even for MIPS32, so we cast them. This is not the case
248 void* pv = (void*) (uintptr_t) _gregs[REG_PV];
249 void* sp = (void*) (uintptr_t) _gregs[REG_SP];
250 void* ra = (void*) (uintptr_t) _gregs[REG_RA]; // The RA is correct for leaf methods.
252 #if defined(__UCLIBC__)
253 void* xpc = (void*) (uintptr_t) _gregs[CTX_EPC];
255 void* xpc = (void*) (uintptr_t) _mc->pc;
258 // This signal is always a patcher.
259 int type = TRAP_PATCHER;
263 void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
265 // Set registers if we have an exception, continue execution
268 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
269 _gregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
270 #if defined(__UCLIBC__)
271 _gregs[CTX_EPC] = (uintptr_t) asm_handle_exception;
273 _mc->pc = (uintptr_t) asm_handle_exception;
277 // We set the PC again because the cause may have changed the
279 #if defined(__UCLIBC__)
280 _gregs[CTX_EPC] = (uintptr_t) xpc;
282 _mc->pc = (uintptr_t) xpc;
288 /* md_signal_handler_sigusr2 ***************************************************
292 *******************************************************************************/
294 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
300 * Read the given context into an executionstate.
302 * @param es execution state
303 * @param context machine context
305 void md_executionstate_read(executionstate_t* es, void* context)
312 vm_abort("md_executionstate_read: PLEASE REVISE ME!");
314 _uc = (ucontext_t*) context;
315 _mc = &_uc->uc_mcontext;
317 #if defined(__UCLIBC__)
318 _gregs = _mc->gpregs;
323 /* Read special registers. */
325 /* In glibc's ucontext.h the registers are defined as long long,
326 even for MIPS32, so we cast them. This is not the case for
329 #if defined(__UCLIBC__)
330 es->pc = _gregs[CTX_EPC];
332 es->pc = (void*) (uintptr_t) _mc->pc;
335 es->sp = (void*) (uintptr_t) _gregs[REG_SP];
336 es->pv = (void*) (uintptr_t) _gregs[REG_PV];
337 es->ra = (void*) (uintptr_t) _gregs[REG_RA];
339 /* Read integer registers. */
341 for (i = 0; i < INT_REG_CNT; i++)
342 es->intregs[i] = _gregs[i];
344 /* Read float registers. */
346 /* Do not use the assignment operator '=', as the type of the
347 _mc->fpregs[i] can cause invalid conversions. */
349 assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
350 os_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
355 * Write the given executionstate back to the context.
357 * @param es execution state
358 * @param context machine context
360 void md_executionstate_write(executionstate_t* es, void* context)
367 vm_abort("md_executionstate_write: PLEASE REVISE ME!");
369 _uc = (ucontext_t *) context;
370 _mc = &_uc->uc_mcontext;
372 /* Write integer registers. */
374 for (i = 0; i < INT_REG_CNT; i++)
375 _gregs[i] = es->intregs[i];
377 /* Write float registers. */
379 /* Do not use the assignment operator '=', as the type of the
380 _mc->fpregs[i] can cause invalid conversions. */
382 assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
383 os_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
385 /* Write special registers. */
387 #if defined(__UCLIBC__)
388 _gregs[CTX_EPC] = es->pc;
390 _mc->pc = (uintptr_t) es->pc;
393 _gregs[REG_SP] = (uintptr_t) es->sp;
394 _gregs[REG_PV] = (uintptr_t) es->pv;
395 _gregs[REG_RA] = (uintptr_t) es->ra;
400 * These are local overrides for various environment variables in Emacs.
401 * Please do not remove this and leave it at the end of the file, where
402 * Emacs will automagically detect them.
403 * ---------------------------------------------------------------------
406 * indent-tabs-mode: t
410 * vim:noexpandtab:sw=4:ts=4: