1 /* src/vm/jit/trap.cpp - hardware traps
3 Copyright (C) 2008, 2009
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2009 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
31 /* Include machine dependent trap stuff. */
36 #include "mm/memory.hpp"
38 #include "native/llni.h"
40 #include "toolbox/logging.hpp"
42 #include "vm/exceptions.hpp"
43 #include "vm/options.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/code.hpp"
49 #include "vm/jit/disass.h"
50 #include "vm/jit/executionstate.h"
51 #include "vm/jit/jit.hpp"
52 #include "vm/jit/methodtree.h"
53 #include "vm/jit/patcher-common.hpp"
54 #include "vm/jit/replace.hpp"
55 #include "vm/jit/stacktrace.hpp"
56 #include "vm/jit/trap.hpp"
63 * Mmap the first memory page to support hardware exceptions and check
64 * the maximum hardware trap displacement on the architectures where
65 * it is required (TRAP_INSTRUCTION_IS_LOAD defined to 1).
69 TRACESUBSYSTEMINITIALIZATION("trap_init");
71 /* If requested we mmap a memory page at address 0x0,
72 so our hardware-exceptions work. */
74 if (opt_AlwaysMmapFirstPage) {
75 int pagesize = os::getpagesize();
76 (void) os::mmap_anonymous(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
79 #if !defined(TRAP_INSTRUCTION_IS_LOAD)
80 # error TRAP_INSTRUCTION_IS_LOAD is not defined in your md-trap.h
83 #if TRAP_INSTRUCTION_IS_LOAD == 1
84 /* Check if we get into trouble with our hardware-exceptions. */
86 if (TRAP_END > OFFSET(java_bytearray_t, data))
87 vm_abort("trap_init: maximum hardware trap displacement is greater than the array-data offset: %d > %d", TRAP_END, OFFSET(java_bytearray_t, data));
93 * Handles the signal which is generated by trap instructions, caught
94 * by a signal handler and calls the correct function.
96 * @param sig signal number
97 * @param xpc exception PC
98 * @param context pointer to OS dependent machine context
100 void trap_handle(int sig, void *xpc, void *context)
103 stackframeinfo_t sfi;
106 // Prevent compiler warnings.
107 java_handle_t* o = NULL;
108 methodinfo* m = NULL;
111 // Sanity checking the XPC.
113 vm_abort("trap_handle: The program counter is NULL!");
116 #if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__POWERPC64__) || defined(__X86_64__)
117 # if !defined(NDEBUG)
118 /* Perform a sanity check on our execution state functions. */
120 executionstate_sanity_check(context);
123 /* Read execution state from current context. */
126 md_executionstate_read(&es, context);
128 //# define TRAP_TRACE_VERBOSE
129 # if !defined(NDEBUG) && defined(TRAP_TRACE_VERBOSE)
130 /* Dump contents of execution state */
132 if (opt_TraceTraps) {
133 log_println("[trap_handle: dumping execution state BEFORE ...]");
134 executionstate_println(&es);
139 // Extract information from executionstate
140 void* pv = es.pv; // Maybe null, resolved during stackframeinfo creation.
142 #if defined(__I386__) || defined(__X86_64__)
143 void* ra = xpc; // Return address is equal to XPC.
145 void* ra = es.ra; // This is correct for leafs.
148 // Decode machine-dependent trap instruction.
149 bool decode_result = md_trap_decode(&trp, sig, xpc, &es);
151 // Check if the trap instruction is valid and was decoded
153 if (!decode_result) {
154 // Check if the PC has been patched during our way to this
155 // trap handler (see PR85).
156 // NOTE: Some archs use SIGILL for other traps too, but it's OK to
157 // do this check anyway because it will fail.
158 if (patcher_is_patched_at(xpc) == true) {
159 if (opt_PrintWarnings)
160 log_println("trap_handle: Detected patcher race condition (PR85) at %p", xpc);
164 // We have a problem...
165 vm_abort_disassemble(xpc, 1, "trap_handle: Unknown trap instruction at %p", xpc);
168 // For convenience only.
170 intptr_t val = trp.value;
172 /* Do some preparations before we enter the nativeworld. */
173 /* BEFORE: creating stackframeinfo */
176 case TRAP_ClassCastException:
177 /* Wrap the value into a handle, as it is a reference. */
179 o = LLNI_WRAP((java_object_t *) val);
183 /* We need to fixup the XPC, SP and RA here because they
184 all might point into the compiler stub instead of the
187 MD_TRAP_COMPILER_FIXUP(xpc, ra, sp, pv);
189 /* In this case the passed PV points to the compiler stub. We
190 get the methodinfo pointer here and set PV to NULL so
191 stacktrace_stackframeinfo_add determines the PV for the
192 parent Java method. */
194 m = code_get_methodinfo_for_pv(pv);
207 log_println("[trap_handle: sig=%d, type=%d, val=%p, pv=%p, sp=%p, ra=%p, xpc=%p]", sig, type, val, pv, sp, ra, xpc);
210 #if defined(ENABLE_VMLOG)
211 vmlog_cacao_signl_type(type);
214 /* Fill and add a stackframeinfo. */
216 stacktrace_stackframeinfo_add(&sfi, pv, sp, ra, xpc);
218 /* Get resulting exception (or pointer to compiled method). */
225 case TRAP_NullPointerException:
226 p = exceptions_new_nullpointerexception();
229 case TRAP_ArithmeticException:
230 p = exceptions_new_arithmeticexception();
233 case TRAP_ArrayIndexOutOfBoundsException:
234 index = (int32_t) val;
235 p = exceptions_new_arrayindexoutofboundsexception(index);
238 case TRAP_ArrayStoreException:
239 p = exceptions_new_arraystoreexception();
242 case TRAP_ClassCastException:
243 p = exceptions_new_classcastexception(o);
246 case TRAP_CHECK_EXCEPTION:
247 p = exceptions_fillinstacktrace();
251 #if defined(ENABLE_REPLACEMENT)
252 if (replace_me_wrapper((uint8_t*) xpc, context)) {
257 p = patcher_handler((uint8_t*) xpc);
261 entry = jit_compile_handle(m, sfi.pv, ra, (void*) val);
265 #if defined(ENABLE_REPLACEMENT)
267 # if defined(__I386__)
268 replace_me_wrapper((uint8_t*) xpc - 13, context);
275 /* Let's try to get a backtrace. */
277 (void) methodtree_find(xpc);
279 /* If that does not work, print more debug info. */
281 vm_abort_disassemble(xpc, 1, "trap_handle: Unknown hardware exception type %d", type);
283 /* keep compiler happy */
288 /* Remove stackframeinfo. */
290 stacktrace_stackframeinfo_remove(&sfi);
292 #if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__POWERPC64__) || defined(__X86_64__)
293 /* Update execution state and set registers. */
294 /* AFTER: removing stackframeinfo */
298 // The normal case for a compiler trap is to jump directly to
299 // the newly compiled method.
302 es.pc = (uint8_t *) (uintptr_t) entry;
303 es.pv = (uint8_t *) (uintptr_t) entry;
307 // In case of an exception during JIT compilation, we fetch
308 // the exception here and proceed with exception handling.
310 p = exceptions_get_and_clear_exception();
313 // Remove RA from stack on some archs.
315 es.sp = (uint8_t*) sp;
317 // Get and set the PV from the parent Java method.
319 es.pv = (uint8_t*) md_codegen_get_pv_from_pc(ra);
321 // Now fall-through to default exception handling.
323 goto trap_handle_exception;
326 // The normal case for a patcher trap is to continue execution at
327 // the trap instruction. On some archs the PC may point after the
328 // trap instruction, so we reset it here.
331 es.pc = (uint8_t *) (uintptr_t) xpc;
335 // Fall-through to default exception handling.
337 trap_handle_exception:
340 es.intregs[REG_ITMP1_XPTR] = (uintptr_t) LLNI_DIRECT(p);
341 es.intregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
342 es.pc = (uint8_t *) (uintptr_t) asm_handle_exception;
346 /* Write back execution state to current context. */
348 md_executionstate_write(&es, context);
350 # if !defined(NDEBUG) && defined(TRAP_TRACE_VERBOSE)
351 /* Dump contents of execution state */
353 if (opt_TraceTraps) {
354 log_println("[trap_handle: dumping execution state AFTER ...]");
355 executionstate_println(&es);
367 * These are local overrides for various environment variables in Emacs.
368 * Please do not remove this and leave it at the end of the file, where
369 * Emacs will automagically detect them.
370 * ---------------------------------------------------------------------
373 * indent-tabs-mode: t
377 * vim:noexpandtab:sw=4:ts=4: