-/* src/vm/jit/x86_64/md.c - machine dependent x86_64 Linux functions
+/* src/vm/jit/s390/md.c - machine dependent s390 Linux functions
- Copyright (C) 1996-2005, 2006 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) 2006, 2007, 2008, 2010
+ CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
This file is part of CACAO.
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
- Contact: cacao@cacaojvm.org
-
- Authors: Christian Thalinger
-
- Changes: Edwin Steiner
-
- $Id: md.c 7534 2007-03-16 23:00:18Z pm $
-
*/
-#define REG_RSP 0
-#define REG_RIP 0
-#define REG_RAX 0
-#define REG_R10 0
-#define REG_RIP 0
-#define REG_RSP 0
-#define REG_RIP 0
-#define REG_RAX 0
-#define REG_R10 0
-#define REG_RIP 0
-
#define _GNU_SOURCE
#include "config.h"
#include <assert.h>
+#include <stdint.h>
#include <stdlib.h>
#include <ucontext.h>
#include "vm/jit/s390/md-abi.h"
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
-#endif
+#include "threads/thread.hpp"
+
+#include "vm/exceptions.hpp"
+#include "vm/signallocal.hpp"
-#include "vm/exceptions.h"
-#include "vm/signallocal.h"
-#include "vm/jit/asmpart.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/abi.h"
+#include "vm/jit/executionstate.h"
+#include "vm/jit/methodheader.h"
+#include "vm/jit/methodtree.h"
+#include "vm/jit/stacktrace.hpp"
+#include "vm/jit/trap.hpp"
#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
-#include "vmcore/options.h" /* XXX debug */
-#include "vm/jit/disass.h" /* XXX debug */
+#include "vm/options.h" /* XXX debug */
#endif
-#include "vm/jit/codegen-common.h"
+#include "vm/jit/codegen-common.hpp"
+#include "vm/jit/s390/codegen.h"
+#include "vm/jit/s390/md.h"
-#include <assert.h>
-#define OOPS() assert(0);
+
+/* prototypes *****************************************************************/
+
+u1 *exceptions_handle_exception(java_object_t *xptro, u1 *xpc, u1 *pv, u1 *sp);
+
+void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p);
+
+void md_dump_context(u1 *pc, mcontext_t *mc);
/* md_init *********************************************************************
void md_init(void)
{
- /* nothing to do */
}
+/* md_dump_context ************************************************************
+
+ Logs the machine context
+
+*******************************************************************************/
-/* md_signal_handler_sigsegv ***************************************************
+void md_dump_context(u1 *pc, mcontext_t *mc) {
+ int i;
+ u1 *pv;
+ methodinfo *m;
- NullPointerException signal handler for hardware null pointer
- check.
+ union {
+ u8 l;
+ fpreg_t fr;
+ } freg;
-*******************************************************************************/
+ log_println("Dumping context.");
-void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
-{
- ucontext_t *_uc;
- mcontext_t *_mc;
- u1 *sp;
- u1 *ra;
- u1 *xpc;
+ log_println("Program counter: 0x%08X", pc);
- _uc = (ucontext_t *) _p;
- _mc = &_uc->uc_mcontext;
+ pv = methodtree_find_nocheck(pc);
- /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
- different to the ones in <ucontext.h>. */
+ if (pv == NULL) {
+ log_println("No java method found at location.");
+ } else {
+ m = (*(codeinfo **)(pv + CodeinfoPointer))->m;
+ log_println(
+ "Java method: class %s, method %s, descriptor %s.",
+ m->clazz->name->text, m->name->text, m->descriptor->text
+ );
+ }
- sp = (u1 *) _mc->gregs[REG_RSP];
- xpc = (u1 *) _mc->gregs[REG_RIP];
- ra = xpc; /* return address is equal to xpc */
+#if defined(ENABLE_DISASSEMBLER)
+ log_println("Printing instruction at program counter:");
+ disassinstr(pc);
+#endif
-#if 0
- /* check for StackOverflowException */
+ log_println("General purpose registers:");
- threads_check_stackoverflow(sp);
-#endif
+ for (i = 0; i < 16; i++) {
+ log_println("\tr%d:\t0x%08X\t%d", i, mc->gregs[i], mc->gregs[i]);
+ }
+
+ log_println("Floating point registers:");
- _mc->gregs[REG_RAX] =
- (ptrint) stacktrace_hardware_nullpointerexception(NULL, sp, ra, xpc);
+ for (i = 0; i < 16; i++) {
+ freg.fr.d = mc->fpregs.fprs[i].d;
+ log_println("\tf%d\t0x%016llX\t(double)%e\t(float)%f", i, freg.l, freg.fr.d, freg.fr.f);
+ }
- _mc->gregs[REG_R10] = (ptrint) xpc; /* REG_ITMP2_XPC */
- _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
+ log_println("Dumping the current stacktrace:");
+ stacktrace_print_current();
}
+/**
+ * NullPointerException signal handler for hardware null pointer check.
+ */
+void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
+{
+ ucontext_t* _uc = (ucontext_t *) _p;
+ mcontext_t* _mc = &_uc->uc_mcontext;
+
+ void* xpc = (u1 *) _mc->psw.addr;
+
+ // Handle the trap.
+ trap_handle(TRAP_SIGSEGV, xpc, _p);
+}
+
+/**
+ * Illegal Instruction signal handler for hardware exception checks.
+ */
+void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
+{
+ ucontext_t* _uc = (ucontext_t *) _p;
+ mcontext_t* _mc = &_uc->uc_mcontext;
+
+ void* xpc = siginfo->si_addr;
+
+ // Handle the trap.
+ trap_handle(TRAP_SIGILL, xpc, _p);
+}
/* md_signal_handler_sigfpe ****************************************************
void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
{
- ucontext_t *_uc;
- mcontext_t *_mc;
- u1 *sp;
- u1 *ra;
- u1 *xpc;
-
- _uc = (ucontext_t *) _p;
- _mc = &_uc->uc_mcontext;
-
- /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
- different to the ones in <ucontext.h>. */
-
- sp = (u1 *) _mc->gregs[REG_RSP];
- xpc = (u1 *) _mc->gregs[REG_RIP];
- ra = xpc; /* return address is equal to xpc */
-
- _mc->gregs[REG_RAX] =
- (ptrint) stacktrace_hardware_arithmeticexception(NULL, sp, ra, xpc);
+ ucontext_t* _uc = (ucontext_t *) _p;
+ mcontext_t* _mc = &_uc->uc_mcontext;
+
+ void* xpc = siginfo->si_addr;
+
+ if (N_RR_GET_OPC(xpc) == OPC_DR) { /* DR */
+
+ int r1 = N_RR_GET_REG1(xpc);
+ int r2 = N_RR_GET_REG2(xpc);
+
+ if (
+ (_mc->gregs[r1] == 0xFFFFFFFF) &&
+ (_mc->gregs[r1 + 1] == 0x80000000) &&
+ (_mc->gregs[r2] == 0xFFFFFFFF)
+ ) {
+ /* handle special case 0x80000000 / 0xFFFFFFFF that fails on hardware */
+ /* next instruction */
+ u1 *pc = (u1 *)_mc->psw.addr;
+ /* remainder */
+ _mc->gregs[r1] = 0;
+ /* quotient */
+ _mc->gregs[r1 + 1] = 0x80000000;
+ /* continue at next instruction */
+ _mc->psw.addr = (ptrint) pc;
+
+ return;
+ }
+ }
- _mc->gregs[REG_R10] = (ptrint) xpc; /* REG_ITMP2_XPC */
- _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
+ // Handle the trap.
+ trap_handle(TRAP_SIGFPE, xpc, _p);
}
/* ATTENTION: Don't use CACAO's internal REG_* defines as they are
different to the ones in <ucontext.h>. */
- pc = (u1 *) _mc->gregs[REG_RIP];
+ pc = (u1 *) _mc->psw.addr;
t->pc = pc;
}
#endif
-#if defined(ENABLE_THREADS)
-void thread_restartcriticalsection(ucontext_t *_uc)
+/**
+ * Read the given context into an executionstate.
+ *
+ * @param es execution state
+ * @param context machine context
+ */
+void md_executionstate_read(executionstate_t* es, void* context)
{
+ ucontext_t *_uc;
mcontext_t *_mc;
- void *pc;
+ int i;
+ _uc = (ucontext_t *) context;
_mc = &_uc->uc_mcontext;
- pc = critical_find_restart_point((void *) _mc->gregs[REG_RIP]);
-
- if (pc != NULL)
- _mc->gregs[REG_RIP] = (ptrint) pc;
-}
-#endif
-
-
-/* md_codegen_patch_branch *****************************************************
-
- Back-patches a branch instruction.
-
-*******************************************************************************/
-
-void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
-{
-
- s4 *mcodeptr;
- s4 disp; /* branch displacement */
-
- /* calculate the patch position */
-
- mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
+ /* read special registers */
+ es->pc = (u1 *) _mc->psw.addr;
+ es->sp = (u1 *) _mc->gregs[REG_SP];
+ es->pv = (u1 *) _mc->gregs[REG_PV] - N_PV_OFFSET;
+ es->ra = (u1 *) _mc->gregs[REG_RA];
- /* Calculate the branch displacement. */
+ /* read integer registers */
+ for (i = 0; i < INT_REG_CNT; i++)
+ es->intregs[i] = _mc->gregs[i];
- disp = targetmpc - branchmpc;
- disp += 4; /* size of branch */
- disp /= 2; /* specified in halfwords */
+ /* read float registers */
+ /* Do not use the assignment operator '=', as the type of
+ * the _mc->sc_fpregs[i] can cause invalid conversions. */
- /* TODO check for overflow */
-
- /* patch the branch instruction before the mcodeptr */
-
- mcodeptr[-1] |= (disp & 0xFFFF);
+ assert(sizeof(_mc->fpregs.fprs) == sizeof(es->fltregs));
+ os_memcpy(&es->fltregs, &_mc->fpregs.fprs, sizeof(_mc->fpregs.fprs));
}
-/* md_stacktrace_get_returnaddress *********************************************
-
- Returns the return address of the current stackframe, specified by
- the passed stack pointer and the stack frame size.
+/**
+ * Write the given executionstate back to the context.
+ *
+ * @param es execution state
+ * @param context machine context
+ */
+void md_executionstate_write(executionstate_t* es, void* context)
+{
+ ucontext_t *_uc;
+ mcontext_t *_mc;
+ int i;
-*******************************************************************************/
+ _uc = (ucontext_t *) context;
+ _mc = &_uc->uc_mcontext;
-u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
-{
- u1 *ra;
+ /* write integer registers */
+ for (i = 0; i < INT_REG_CNT; i++)
+ _mc->gregs[i] = es->intregs[i];
- /* on S390 the return address is located on the top of the stackframe */
+ /* write float registers */
+ /* Do not use the assignment operator '=', as the type of
+ * the _mc->sc_fpregs[i] can cause invalid conversions. */
- ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P));
+ assert(sizeof(_mc->fpregs.fprs) == sizeof(es->fltregs));
+ os_memcpy(&_mc->fpregs.fprs, &es->fltregs, sizeof(_mc->fpregs.fprs));
- return ra;
+ /* write special registers */
+ _mc->psw.addr = (ptrint) es->pc;
+ _mc->gregs[REG_SP] = (ptrint) es->sp;
+ _mc->gregs[REG_PV] = (ptrint) es->pv + N_PV_OFFSET;
+ _mc->gregs[REG_RA] = (ptrint) es->ra;
}
-/* md_get_method_patch_address *************************************************
+/* md_jit_method_patch_address *************************************************
Gets the patch address of the currently compiled method. The offset
is extracted from the load instruction(s) before the jump and added
*******************************************************************************/
-u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
+void *md_jit_method_patch_address(void* pv, void *ra, void *mptr)
{
- u1 base;
- s4 offset;
- u1 *pa; /* patch address */
+ uint8_t *pc;
+ uint8_t base, index;
+ int32_t offset;
+ void *pa; /* patch address */
/* go back to the load before the call instruction */
- ra = ra - 2 /* sizeof bcr */ - 4 /* sizeof l */;
+ pc = ((uint8_t *) ra) - SZ_BCR - SZ_L;
/* get the base register of the load */
- base = ra[2] >> 4;
+ base = N_RX_GET_BASE(pc);
+ index = N_RX_GET_INDEX(pc);
/* check for the different calls */
- if (base == 0xd) { /* pv relative */
- /* INVOKESTATIC/SPECIAL */
+ switch (base) {
+ case REG_PV:
+ /* INVOKESTATIC/SPECIAL */
+
+ switch (index) {
+ case R0:
+ /* the offset is in the load instruction */
+ offset = N_RX_GET_DISP(pc) + N_PV_OFFSET;
+ break;
+ case REG_ITMP1:
+ /* the offset is in the immediate load before the load */
+ offset = N_RI_GET_IMM(pc - SZ_L);
+ break;
+ default:
+ assert(0);
+ }
+
+ /* add the offset to the procedure vector */
- /* the offset is in the load before the load */
+ pa = ((uint8_t *) pv) + offset;
+ break;
- offset = *((s2 *) (ra - 2));
+ case REG_METHODPTR:
+ /* mptr relative */
+ /* INVOKEVIRTUAL/INTERFACE */
- /* add the offset to the procedure vector */
+ offset = N_RX_GET_DISP(pc);
- pa = sfi->pv + offset;
+ /* return NULL if no mptr was specified (used for replacement) */
+
+ if (mptr == NULL)
+ return NULL;
+
+ /* add offset to method pointer */
+
+ pa = (uint8_t *)mptr + offset;
+ break;
+
+ default:
+ /* catch any problems */
+ vm_abort("md_jit_method_patch_address");
+ break;
}
- else if (base == 0xc) { /* mptr relative */
- /* INVOKEVIRTUAL/INTERFACE */
- offset = *((u2 *)(ra + 2)) & 0xFFF;
+ return pa;
+}
- /* add offset to method pointer */
-
- pa = mptr + offset;
+
+/**
+ * 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)
+{
+ switch (sig) {
+ case TRAP_SIGILL:
+ if (N_RR_GET_OPC(xpc) == OPC_ILL) {
+ int32_t reg = N_ILL_GET_REG(xpc);
+ trp->type = N_ILL_GET_TYPE(xpc);
+ trp->value = es->intregs[reg];
+ return true;
+ }
+ return false;
+
+ case TRAP_SIGSEGV:
+ {
+ int is_null;
+ int32_t base;
+ switch (N_RX_GET_OPC(xpc)) {
+ case OPC_L:
+ case OPC_ST:
+ case OPC_CL: /* array size check on NULL array */
+ base = N_RX_GET_BASE(xpc);
+ if (base == 0) {
+ is_null = 1;
+ } else if (es->intregs[base] == 0) {
+ is_null = 1;
+ } else {
+ is_null = 0;
+ }
+ break;
+ default:
+ is_null = 0;
+ break;
+ }
+
+ // Check for implicit NullPointerException.
+ if (is_null) {
+ trp->type = TRAP_NullPointerException;
+ trp->value = 0;
+ return true;
+ }
+
+ return false;
}
- else {
- /* catch any problems */
- assert(0);
+
+ case TRAP_SIGFPE:
+ {
+ if (N_RR_GET_OPC(xpc) == OPC_DR) {
+ int r2 = N_RR_GET_REG2(xpc);
+ if (es->intregs[r2] == 0) {
+ trp->type = TRAP_ArithmeticException;
+ trp->value = 0;
+ return true;
+ }
+ }
+
+ return false;
}
- return pa;
+ default:
+ return false;
+ }
}
-/* md_codegen_get_pv_from_pc ***************************************************
- On this architecture just a wrapper function to
- codegen_get_pv_from_pc.
+/* md_patch_replacement_point **************************************************
-*******************************************************************************/
+ Patch the given replacement point.
-u1 *md_codegen_get_pv_from_pc(u1 *ra)
+*******************************************************************************/
+#if defined(ENABLE_REPLACEMENT)
+void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
{
- u1 *pv;
+ assert(0);
+}
+#endif
- /* Get the start address of the function which contains this
- address from the method table. */
+void md_handle_exception(int32_t *regs, int64_t *fregs, int32_t *out) {
- pv = codegen_get_pv_from_pc(ra);
+ uint8_t *xptr;
+ uint8_t *xpc;
+ uint8_t *sp;
+ uint8_t *pv;
+ uint8_t *ra;
+ uint8_t *handler;
+ int32_t framesize;
+ int32_t intsave;
+ int32_t fltsave;
+ int64_t *savearea;
+ int i;
+ int reg;
+ int loops = 0;
- return pv;
-}
+ /* get registers */
+ xptr = *(uint8_t **)(regs + REG_ITMP1_XPTR);
+ xpc = *(uint8_t **)(regs + REG_ITMP2_XPC);
+ sp = *(uint8_t **)(regs + REG_SP);
-/* md_cacheflush ***************************************************************
- Calls the system's function to flush the instruction and data
- cache.
+ /* initialize number of calle saved int regs to restore to 0 */
+ out[0] = 0;
-*******************************************************************************/
+ /* initialize number of calle saved flt regs to restore to 0 */
+ out[1] = 0;
-void md_cacheflush(u1 *addr, s4 nbytes)
-{
- /* do nothing */
-}
+ do {
+ ++loops;
-/* md_icacheflush **************************************************************
+ pv = methodtree_find(xpc);
- Calls the system's function to flush the instruction cache.
+ handler = exceptions_handle_exception((java_object_t *)xptr, xpc, pv, sp);
-*******************************************************************************/
+ if (handler == NULL) {
-void md_icacheflush(u1 *addr, s4 nbytes)
-{
- /* do nothing */
-}
+ /* exception was not handled
+ * get values of calee saved registers and remove stack frame
+ */
+ /* read stuff from data segment */
-/* md_dcacheflush **************************************************************
+ framesize = *(int32_t *)(pv + FrameSize);
- Calls the system's function to flush the data cache.
+ intsave = *(int32_t *)(pv + IntSave);
+ if (intsave > out[0]) {
+ out[0] = intsave;
+ }
-*******************************************************************************/
+ fltsave = *(int32_t *)(pv + FltSave);
+ if (fltsave > out[1]) {
+ out[1] = fltsave;
+ }
-void md_dcacheflush(u1 *addr, s4 nbytes)
-{
- /* do nothing */
-}
+ /* pointer to register save area */
+ savearea = (int64_t *)(sp + framesize - 8);
-/* md_patch_replacement_point **************************************************
+ /* return address */
- Patch the given replacement point.
+ ra = *(uint8_t **)(sp + framesize - 8);
-*******************************************************************************/
-#if 0
-void md_patch_replacement_point(rplpoint *rp)
-{
- u8 mcode;
+ /* restore saved registers */
- /* XXX this is probably unsafe! */
+ for (i = 0; i < intsave; ++i) {
+ --savearea;
+ reg = abi_registers_integer_saved[INT_SAV_CNT - 1 - i];
+ regs[reg] = *(int32_t *)(savearea);
+ }
- /* save the current machine code */
- mcode = *(u8*)rp->pc;
+ for (i = 0; i < fltsave; ++i) {
+ --savearea;
+ reg = abi_registers_float_saved[FLT_SAV_CNT - 1 - i];
+ fregs[reg] = *savearea;
+ }
- /* write spinning instruction */
- *(u2*)(rp->pc) = 0xebfe;
+ /* remove stack frame */
- /* write 5th byte */
- rp->pc[4] = (rp->mcode >> 32);
+ sp += framesize;
- /* write first word */
- *(u4*)(rp->pc) = (u4) rp->mcode;
+ /* new xpc is call before return address */
- /* store saved mcode */
- rp->mcode = mcode;
-
-#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
- {
- u1* u1ptr = rp->pc;
- DISASSINSTR(u1ptr);
- fflush(stdout);
- }
-#endif
-
- /* XXX if required asm_cacheflush(rp->pc,8); */
+ xpc = ra - 2;
+
+ } else {
+ xpc = handler;
+ }
+ } while (handler == NULL);
+
+ /* write new values for registers */
+
+ *(uint8_t **)(regs + REG_ITMP1_XPTR) = xptr;
+ *(uint8_t **)(regs + REG_ITMP2_XPC) = xpc;
+ *(uint8_t **)(regs + REG_SP) = sp;
+ *(uint8_t **)(regs + REG_PV) = pv - 0XFFC;
+
+ /* maybe leaf flag */
+
+ out[2] = (loops == 1);
}
-#endif
+
/*
* These are local overrides for various environment variables in Emacs.
* Please do not remove this and leave it at the end of the file, where