1 /* src/vm/jit/trap.c - hardware traps
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
30 /* Include machine dependent trap stuff. */
35 #include "mm/memory.h"
37 #include "native/llni.h"
39 #include "toolbox/logging.h"
41 #include "vm/exceptions.hpp"
42 #include "vm/options.h"
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/code.hpp"
48 #include "vm/jit/disass.h"
49 #include "vm/jit/executionstate.h"
50 #include "vm/jit/jit.hpp"
51 #include "vm/jit/methodtree.h"
52 #include "vm/jit/patcher-common.hpp"
53 #include "vm/jit/replace.hpp"
54 #include "vm/jit/stacktrace.hpp"
58 * Mmap the first memory page to support hardware exceptions and check
59 * the maximum hardware trap displacement on the architectures where
60 * it is required (TRAP_INSTRUCTION_IS_LOAD defined to 1).
64 #if !(defined(__ARM__) && defined(__LINUX__))
65 /* On arm-linux the first memory page can't be mmap'ed, as it
66 contains the exception vectors. */
70 /* mmap a memory page at address 0x0, so our hardware-exceptions
73 pagesize = os_getpagesize();
75 (void) os_mmap_anonymous(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
78 TRACESUBSYSTEMINITIALIZATION("trap_init");
80 #if !defined(TRAP_INSTRUCTION_IS_LOAD)
81 # error TRAP_INSTRUCTION_IS_LOAD is not defined in your md-trap.h
84 #if TRAP_INSTRUCTION_IS_LOAD == 1
85 /* Check if we get into trouble with our hardware-exceptions. */
87 if (TRAP_END > OFFSET(java_bytearray_t, data))
88 vm_abort("trap_init: maximum hardware trap displacement is greater than the array-data offset: %d > %d", TRAP_END, OFFSET(java_bytearray_t, data));
94 * Handles the signal which is generated by trap instructions, caught
95 * by a signal handler and calls the correct function.
97 * @param type trap number
100 void* trap_handle(int type, intptr_t val, void *pv, void *sp, void *ra, void *xpc, void *context)
103 stackframeinfo_t sfi;
111 log_println("[signal_handle: trap %d]", type);
114 #if defined(ENABLE_VMLOG)
115 vmlog_cacao_signl_type(type);
118 /* Prevent compiler warnings. */
123 #if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__POWERPC__) || defined(__POWERPC64__) || defined(__X86_64__)
124 # if !defined(NDEBUG)
125 /* Perform a sanity check on our execution state functions. */
127 executionstate_sanity_check(context);
130 /* Read execution state from current context. */
133 md_executionstate_read(&es, context);
136 /* wrap the value into a handle if it is a reference */
137 /* BEFORE: creating stackframeinfo */
140 case TRAP_ClassCastException:
141 o = LLNI_WRAP((java_object_t *) val);
145 /* In this case the passed PV points to the compiler stub. We
146 get the methodinfo pointer here and set PV to NULL so
147 stacktrace_stackframeinfo_add determines the PV for the
148 parent Java method. */
150 m = code_get_methodinfo_for_pv(pv);
159 /* Fill and add a stackframeinfo. */
161 stacktrace_stackframeinfo_add(&sfi, pv, sp, ra, xpc);
164 case TRAP_NullPointerException:
165 p = exceptions_new_nullpointerexception();
168 case TRAP_ArithmeticException:
169 p = exceptions_new_arithmeticexception();
172 case TRAP_ArrayIndexOutOfBoundsException:
174 p = exceptions_new_arrayindexoutofboundsexception(index);
177 case TRAP_ArrayStoreException:
178 p = exceptions_new_arraystoreexception();
181 case TRAP_ClassCastException:
182 p = exceptions_new_classcastexception(o);
185 case TRAP_CHECK_EXCEPTION:
186 p = exceptions_fillinstacktrace();
190 #if defined(ENABLE_REPLACEMENT)
191 if (replace_me_wrapper(xpc, context)) {
196 p = patcher_handler(xpc);
200 p = jit_compile_handle(m, sfi.pv, ra, (void *) val);
203 #if defined(ENABLE_REPLACEMENT)
205 #if defined(__I386__)
206 replace_me_wrapper((char*)xpc - 13, context);
213 /* Let's try to get a backtrace. */
215 (void) methodtree_find(xpc);
217 /* If that does not work, print more debug info. */
219 log_println("signal_handle: unknown hardware exception type %d", type);
221 #if SIZEOF_VOID_P == 8
222 log_println("PC=0x%016lx", xpc);
224 log_println("PC=0x%08x", xpc);
227 #if defined(ENABLE_DISASSEMBLER)
228 log_println("machine instruction at PC:");
232 vm_abort("Exiting...");
234 /* keep compiler happy */
239 /* Remove stackframeinfo. */
241 stacktrace_stackframeinfo_remove(&sfi);
243 #if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__POWERPC__) || defined(__POWERPC64__) || defined(__X86_64__)
244 /* Update execution state and set registers. */
245 /* AFTER: removing stackframeinfo */
249 // The normal case for a compiler trap is to jump directly to
250 // the newly compiled method.
253 es.pc = (uint8_t *) (uintptr_t) p;
254 es.pv = (uint8_t *) (uintptr_t) p;
258 // In case of an exception during JIT compilation, we fetch
259 // the exception here and proceed with exception handling.
261 java_handle_t *e = exceptions_get_and_clear_exception();
264 // Get and set the PV from the parent Java method.
266 es.pv = md_codegen_get_pv_from_pc(ra);
268 // XXX: Make the code below a fall-through to default case!
270 es.intregs[REG_ITMP1_XPTR] = (uintptr_t) LLNI_DIRECT(e);
271 es.intregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
272 es.pc = (uint8_t *) (uintptr_t) asm_handle_exception;
276 // The normal case for a patcher trap is to continue execution at
277 // the trap instruction. On some archs the PC may point after the
278 // trap instruction, so we reset it here.
281 es.pc = (uint8_t *) (uintptr_t) xpc;
289 es.intregs[REG_ITMP1_XPTR] = (uintptr_t) LLNI_DIRECT(p);
290 es.intregs[REG_ITMP2_XPC] = (uintptr_t) xpc;
291 es.pc = (uint8_t *) (uintptr_t) asm_handle_exception;
295 /* Write back execution state to current context. */
297 md_executionstate_write(&es, context);
300 /* unwrap and return the exception object */
301 /* AFTER: removing stackframeinfo */
303 if (type == TRAP_COMPILER)
306 return LLNI_UNWRAP(p);
311 * These are local overrides for various environment variables in Emacs.
312 * Please do not remove this and leave it at the end of the file, where
313 * Emacs will automagically detect them.
314 * ---------------------------------------------------------------------
317 * indent-tabs-mode: t