-/* src/vm/jit/x86_64/md.c - machine dependent x86_64 Linux functions
+/* src/vm/jit/x86_64/md.c - machine dependent x86_64 functions
- Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
- R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
- C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
- Institut f. Computersprachen - TU Wien
+ Copyright (C) 1996-2005, 2006, 2007, 2008
+ CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+ Copyright (C) 2009 Theobroma Systems Ltd.
This file is part of CACAO.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- 02111-1307, USA.
-
- Contact: cacao@complang.tuwien.ac.at
-
- Authors: Christian Thalinger
-
- Changes:
-
- $Id: md.c 2654 2005-06-13 14:10:45Z twisti $
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA.
*/
-#define _GNU_SOURCE
+#include "config.h"
+#include <assert.h>
+#include <stdint.h>
#include <stdlib.h>
-#include <ucontext.h>
-#include "config.h"
+#include "vm/jit/x86_64/codegen.h"
#include "vm/jit/x86_64/md-abi.h"
-#include "vm/options.h"
-#include "vm/stringlocal.h"
-#include "vm/jit/asmpart.h"
+#include "vm/vm.hpp"
+#include "vm/jit/codegen-common.hpp"
+#include "vm/jit/executionstate.h"
+#include "vm/jit/jit.hpp"
+#include "vm/jit/trap.hpp"
-/* catch_NullPointerException **************************************************
- NullPointerException signal handler for hardware null pointer check.
+/* md_init *********************************************************************
+
+ Do some machine dependent initialization.
*******************************************************************************/
-void catch_NullPointerException(int sig, siginfo_t *siginfo, void *_p)
+void md_init(void)
{
- ucontext_t *_uc;
- mcontext_t *_mc;
- struct sigaction act;
- sigset_t nsig;
+ /* nothing to do */
+}
- _uc = (ucontext_t *) _p;
- _mc = &_uc->uc_mcontext;
- /* Reset signal handler - necessary for SysV, does no harm for BSD */
-
- act.sa_sigaction = catch_NullPointerException; /* reinstall handler */
- act.sa_flags = SA_SIGINFO;
- sigaction(sig, &act, NULL);
-
- sigemptyset(&nsig);
- sigaddset(&nsig, sig);
- sigprocmask(SIG_UNBLOCK, &nsig, NULL); /* unblock signal */
+/* md_jit_method_patch_address *************************************************
- _mc->gregs[REG_RAX] = (ptrint) string_java_lang_NullPointerException;
- _mc->gregs[REG_R10] = _mc->gregs[REG_RIP]; /* REG_ITMP2_XPC */
- _mc->gregs[REG_RIP] = (ptrint) asm_throw_and_handle_exception;
-}
+ Gets the patch address of the currently compiled method. The offset
+ is extracted from the load instruction(s) before the jump and added
+ to the right base address (PV or REG_METHODPTR).
+
+ INVOKESTATIC/SPECIAL:
+ 4d 8b 15 e2 fe ff ff mov -286(%rip),%r10
+ 49 ff d2 rex64Z callq *%r10
-/* catch_ArithmeticException ***************************************************
+ INVOKEVIRTUAL:
- ArithmeticException signal handler for hardware divide by zero check.
+ 4c 8b 17 mov (%rdi),%r10
+ 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
+ 48 ff d3 rex64 callq *%rax
+
+ INVOKEINTERFACE:
+
+ 4c 8b 17 mov (%rdi),%r10
+ 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
+ 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
+ 48 ff d3 rex64 callq *%r11
*******************************************************************************/
-void catch_ArithmeticException(int sig, siginfo_t *siginfo, void *_p)
+void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
{
- ucontext_t *_uc;
- mcontext_t *_mc;
- struct sigaction act;
- sigset_t nsig;
+ uint8_t *pc;
+ uint8_t mcode;
+ int32_t offset;
+ void *pa; /* patch address */
- _uc = (ucontext_t *) _p;
- _mc = &_uc->uc_mcontext;
+ /* go back to the actual call instruction (3-bytes) */
- /* Reset signal handler - necessary for SysV, does no harm for BSD */
+ pc = ((uint8_t *) ra) - 3;
- act.sa_sigaction = catch_ArithmeticException; /* reinstall handler */
- act.sa_flags = SA_SIGINFO;
- sigaction(sig, &act, NULL);
+ /* get the last byte of the call */
- sigemptyset(&nsig);
- sigaddset(&nsig, sig);
- sigprocmask(SIG_UNBLOCK, &nsig, NULL); /* unblock signal */
+ mcode = pc[2];
- _mc->gregs[REG_R10] = _mc->gregs[REG_RIP]; /* REG_ITMP2_XPC */
- _mc->gregs[REG_RIP] =
- (ptrint) asm_throw_and_handle_hardware_arithmetic_exception;
-}
+ /* check for the different calls */
+ if (mcode == 0xd2) {
+ /* INVOKESTATIC/SPECIAL */
-void init_exceptions(void)
-{
- struct sigaction act;
+ /* Get the offset from the instruction (the offset address is
+ 4-bytes before the call instruction). */
- /* install signal handlers we need to convert to exceptions */
+ offset = *((int32_t *) (pc - 4));
- sigemptyset(&act.sa_mask);
+ /* add the offset to the return address (IP-relative addressing) */
- if (!checknull) {
-#if defined(SIGSEGV)
- act.sa_sigaction = catch_NullPointerException;
- act.sa_flags = SA_SIGINFO;
- sigaction(SIGSEGV, &act, NULL);
-#endif
+ pa = pc + offset;
+ }
+ else if (mcode == 0xd3) {
+ /* INVOKEVIRTUAL/INTERFACE */
-#if defined(SIGBUS)
- act.sa_sigaction = catch_NullPointerException;
- act.sa_flags = SA_SIGINFO;
- sigaction(SIGBUS, &act, NULL);
-#endif
+ /* return NULL if no mptr was specified (used for replacement) */
+
+ if (mptr == NULL)
+ return NULL;
+
+ /* Get the offset from the instruction (the offset address is
+ 4-bytes before the call instruction). */
+
+ offset = *((int32_t *) (pc - 4));
+
+ /* add the offset to the method pointer */
+
+ pa = ((uint8_t *) mptr) + offset;
}
+ else {
+ /* catch any problems */
- act.sa_sigaction = catch_ArithmeticException;
- act.sa_flags = SA_SIGINFO;
- sigaction(SIGFPE, &act, NULL);
+ vm_abort("md_jit_method_patch_address: unknown instruction %x", mcode);
+
+ /* keep compiler happy */
+
+ pa = NULL;
+ }
+
+ return pa;
}
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
-void thread_restartcriticalsection(ucontext_t *uc)
+/**
+ * Decode the trap instruction at the given PC.
+ *
+ * @param trp information about trap to be filled
+ * @param sig signal number
+ * @param xpc exception PC
+ * @param es execution state of the machine
+ * @return true if trap was decoded successfully, false otherwise.
+ */
+bool md_trap_decode(trapinfo_t* trp, int sig, void* _xpc, executionstate_t* es)
{
- void *critical;
+ uint8_t* xpc = (uint8_t*) _xpc;
- critical = thread_checkcritical((void *) uc->uc_mcontext.gregs[REG_RIP]);
+#if 0
+ // Check for StackOverflowException.
+ threads_check_stackoverflow(sp);
+#endif
- if (critical)
- uc->uc_mcontext.gregs[REG_RIP] = (ptrint) critical;
+ switch (sig) {
+ case TRAP_SIGFPE:
+ // Check for ArithmeticException.
+ trp->type = TRAP_ArithmeticException;
+ trp->value = 0;
+ return true;
+
+ case TRAP_SIGILL:
+ // Check for valid trap instruction.
+ if (patcher_is_valid_trap_instruction_at(xpc)) {
+ trp->type = TRAP_PATCHER;
+ trp->value = 0;
+ return true;
+ }
+ return false;
+
+ case TRAP_SIGSEGV:
+ {
+ // Get exception-throwing instruction.
+ uint8_t opc = M_ALD_MEM_GET_OPC(xpc);
+ uint8_t mod = M_ALD_MEM_GET_MOD(xpc);
+ uint8_t rm = M_ALD_MEM_GET_RM(xpc);
+
+ // Check for hardware exception, for values
+ // see emit_mov_mem_reg and emit_mem.
+ if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
+ int32_t d = M_ALD_MEM_GET_REG(xpc);
+ int32_t disp = M_ALD_MEM_GET_DISP(xpc);
+
+ // We use the exception type as load displacement.
+ trp->type = disp;
+ trp->value = es->intregs[d];
+ return true;
+ }
+
+ // Default case is a normal NullPointerException.
+ else {
+ trp->type = TRAP_NullPointerException;
+ trp->value = 0;
+ return true;
+ }
+ }
+
+ default:
+ return false;
+ }
}
-#endif
+
+
+/* md_patch_replacement_point **************************************************
+
+ Patch the given replacement point.
+
+*******************************************************************************/
+
+#if defined(ENABLE_REPLACEMENT)
+void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
+{
+ u2 mcode;
+
+ if (revert) {
+ /* write saved machine code */
+ *(u2*)(pc) = *(u2*)(savedmcode);
+ }
+ else {
+ /* save the current machine code */
+ *(u2*)(savedmcode) = *(u2*)(pc);
+
+ /* build the machine code for the patch */
+ mcode = 0x0b0f;
+
+ /* write new machine code */
+ *(u2*)(pc) = (u2) mcode;
+ }
+
+ /* XXX if required asm_cacheflush(pc,8); */
+}
+#endif /* defined(ENABLE_REPLACEMENT) */
/*
* c-basic-offset: 4
* tab-width: 4
* End:
+ * vim:noexpandtab:sw=4:ts=4:
*/