1 /* src/vm/jit/trap.cpp - hardware traps
3 Copyright (C) 2008, 2009, 2010
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"
59 #include "vm/jit/s390/codegen.h"
69 * Mmap the first memory page to support hardware exceptions and check
70 * the maximum hardware trap displacement on the architectures where
71 * it is required (TRAP_INSTRUCTION_IS_LOAD defined to 1).
75 TRACESUBSYSTEMINITIALIZATION("trap_init");
77 /* If requested we mmap a memory page at address 0x0,
78 so our hardware-exceptions work. */
80 if (opt_AlwaysMmapFirstPage) {
81 int pagesize = os::getpagesize();
82 (void) os::mmap_anonymous(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
85 #if !defined(TRAP_INSTRUCTION_IS_LOAD)
86 # error TRAP_INSTRUCTION_IS_LOAD is not defined in your md-trap.h
89 #if TRAP_INSTRUCTION_IS_LOAD == 1
90 /* Check if we get into trouble with our hardware-exceptions. */
92 if (TRAP_END > OFFSET(java_bytearray_t, data))
93 vm_abort("trap_init: maximum hardware trap displacement is greater than the array-data offset: %d > %d", TRAP_END, OFFSET(java_bytearray_t, data));
99 * Handles the signal which is generated by trap instructions, caught
100 * by a signal handler and calls the correct function.
102 * @param sig signal number
103 * @param xpc exception PC
104 * @param context pointer to OS dependent machine context
106 void trap_handle(int sig, void *xpc, void *context)
109 stackframeinfo_t sfi;
113 // Sanity checking the XPC.
115 vm_abort("trap_handle: The program counter is NULL!");
118 #if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__POWERPC64__) || defined(__S390__) || defined(__X86_64__)
119 # if !defined(NDEBUG)
120 /* Perform a sanity check on our execution state functions. */
122 executionstate_sanity_check(context);
125 /* Read execution state from current context. */
128 md_executionstate_read(&es, context);
130 //# define TRAP_TRACE_VERBOSE
131 # if !defined(NDEBUG) && defined(TRAP_TRACE_VERBOSE)
132 /* Dump contents of execution state */
134 if (opt_TraceTraps) {
135 log_println("[trap_handle: dumping execution state BEFORE ...]");
136 executionstate_println(&es);
141 // Extract information from executionstate
142 void* pv = es.pv; // Maybe null, resolved during stackframeinfo creation.
144 #if defined(__I386__) || defined(__X86_64__)
145 void* ra = xpc; // Return address is equal to XPC.
147 void* ra = es.ra; // This is correct for leafs.
150 // Decode machine-dependent trap instruction.
151 bool decode_result = md_trap_decode(&trp, sig, xpc, &es);
153 // Check if the trap instruction is valid and was decoded
155 if (!decode_result) {
156 // Check if the PC has been patched during our way to this
157 // trap handler (see PR85).
158 // NOTE: Some archs use SIGILL for other traps too, but it's OK to
159 // do this check anyway because it will fail.
160 if (patcher_is_patched_at(xpc) == true) {
161 if (opt_PrintWarnings)
162 log_println("trap_handle: Detected patcher race condition (PR85) at %p", xpc);
166 // We have a problem...
167 vm_abort_disassemble(xpc, 1, "trap_handle: Unknown trap instruction at %p", xpc);
170 // For convenience only.
172 intptr_t val = trp.value;
174 /* Do some preparations before we enter the nativeworld. */
175 /* BEFORE: creating stackframeinfo */
177 // Prevent compiler warnings.
179 java_handle_t* o = NULL;
180 methodinfo* m = NULL;
183 case TRAP_ArrayIndexOutOfBoundsException:
184 /* Get the index into the array causing the exception. */
186 index = (int32_t) val;
189 case TRAP_ClassCastException:
190 /* Wrap the value into a handle, as it is a reference. */
192 o = LLNI_WRAP((java_object_t *) val);
196 /* We need to fixup the XPC, SP and RA here because they
197 all might point into the compiler stub instead of the
200 MD_TRAP_COMPILER_FIXUP(xpc, ra, sp, pv);
202 /* In this case the passed PV points to the compiler stub. We
203 get the methodinfo pointer here and set PV to NULL so
204 stacktrace_stackframeinfo_add determines the PV for the
205 parent Java method. */
207 m = code_get_methodinfo_for_pv(pv);
220 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);
223 #if defined(ENABLE_VMLOG)
224 vmlog_cacao_signl_type(type);
227 /* Fill and add a stackframeinfo. */
229 stacktrace_stackframeinfo_add(&sfi, pv, sp, ra, xpc);
231 /* Get resulting exception (or pointer to compiled method). */
236 #if defined(ENABLE_REPLACEMENT)
241 case TRAP_NullPointerException:
242 p = exceptions_new_nullpointerexception();
245 case TRAP_ArithmeticException:
246 p = exceptions_new_arithmeticexception();
249 case TRAP_ArrayIndexOutOfBoundsException:
250 p = exceptions_new_arrayindexoutofboundsexception(index);
253 case TRAP_ArrayStoreException:
254 p = exceptions_new_arraystoreexception();
257 case TRAP_ClassCastException:
258 p = exceptions_new_classcastexception(o);
261 case TRAP_CHECK_EXCEPTION:
262 p = exceptions_fillinstacktrace();
267 #if defined(ENABLE_REPLACEMENT)
268 was_replaced = replace_handler((uint8_t*) xpc, &es);
272 was_patched = patcher_handler((uint8_t*) xpc);
277 entry = jit_compile_handle(m, sfi.pv, ra, (void*) val);
280 #if defined(__I386__) && defined(ENABLE_REPLACEMENT)
281 # warning Port the below stuff to use the patching subsystem.
284 (void) replace_handler((uint8_t*) xpc - 13, &es);
289 /* Let's try to get a backtrace. */
291 (void) methodtree_find(xpc);
293 /* If that does not work, print more debug info. */
295 vm_abort_disassemble(xpc, 1, "trap_handle: Unknown hardware exception type %d", type);
297 /* keep compiler happy */
302 /* Remove stackframeinfo. */
304 stacktrace_stackframeinfo_remove(&sfi);
306 #if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__POWERPC64__) || defined(__S390__) || defined(__X86_64__)
307 /* Update execution state and set registers. */
308 /* AFTER: removing stackframeinfo */
312 // The normal case for a compiler trap is to jump directly to
313 // the newly compiled method.
316 es.pc = (uint8_t *) (uintptr_t) entry;
317 // The s390 executionstate offsets pv, so we need to
319 es.pv = (uint8_t *) (uintptr_t) entry - N_PV_OFFSET;
323 // In case of an exception during JIT compilation, we fetch
324 // the exception here and proceed with exception handling.
326 p = exceptions_get_and_clear_exception();
329 // Remove RA from stack on some archs.
331 es.sp = (uint8_t*) sp;
333 // Get and set the PV from the parent Java method.
335 es.pv = (uint8_t*) md_codegen_get_pv_from_pc(ra) - N_PV_OFFSET;
337 // Now fall-through to default exception handling.
339 goto trap_handle_exception;
342 #if defined(ENABLE_REPLACEMENT)
343 // If on-stack-replacement suceeded, we are not allowed to touch
344 // the execution state. We assume that there was no exception.
347 assert(exceptions_get_exception() == NULL);
352 // The normal case for a patcher trap is to continue execution at
353 // the trap instruction. On some archs the PC may point after the
354 // trap instruction, so we reset it here.
357 assert(exceptions_get_exception() == NULL);
358 es.pc = (uint8_t *) (uintptr_t) xpc;
362 // In case patching was not successful, we try to fetch the pending
365 p = exceptions_get_and_clear_exception();
367 // If there is no pending exception, we continue execution behind
368 // the position still in need of patching. Normally this would
369 // indicate an error in the patching subsystem, but others might
370 // want to piggyback patchers and we want to be able to provide
371 // "reusable trap points" and avoid inifinite loops here. This is
372 // especially useful to implement breakpoints or profiling points
373 // of any kind. So before changing the trap logic, think about
374 // utilizing the patching subsystem on your quest. :)
378 if (opt_PrintWarnings)
379 log_println("trap_handle: Detected reusable trap at %p", xpc);
381 es.pc = (uint8_t *) (uintptr_t) xpc;
382 es.pc += REPLACEMENT_PATCH_SIZE;
386 // Fall-through to default exception handling.
388 trap_handle_exception:
391 #if defined(__ALPHA__) || defined(__I386__) || defined(__X86_64__)
392 // Perform stack unwinding for exceptions on execution state.
393 es.pc = (uint8_t *) (uintptr_t) xpc;
394 es.pv = (uint8_t *) (uintptr_t) sfi.pv;
395 executionstate_unwind_exception(&es, p);
397 // Pass the exception object to the exception handler.
398 es.intregs[REG_ITMP1_XPTR] = (uintptr_t) LLNI_DIRECT(p);
400 es.intregs[REG_ITMP1_XPTR] = (uintptr_t) LLNI_DIRECT(p);
401 es.intregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
402 es.pc = (uint8_t *) (uintptr_t) asm_handle_exception;
407 /* Write back execution state to current context. */
409 md_executionstate_write(&es, context);
411 # if !defined(NDEBUG) && defined(TRAP_TRACE_VERBOSE)
412 /* Dump contents of execution state */
414 if (opt_TraceTraps) {
415 log_println("[trap_handle: dumping execution state AFTER ...]");
416 executionstate_println(&es);
428 * These are local overrides for various environment variables in Emacs.
429 * Please do not remove this and leave it at the end of the file, where
430 * Emacs will automagically detect them.
431 * ---------------------------------------------------------------------
434 * indent-tabs-mode: t
438 * vim:noexpandtab:sw=4:ts=4: