* src/threads/critical.c: Removed.
[cacao.git] / src / vm / jit / s390 / md.c
index 9f9331869609ae8f48efcf58dfa5df5529e0c566..b21bbb87190bebee9e8eeb6eb98d1f99a09e7f3d 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/s390/md.c - machine dependent s390 Linux functions
 
-   Copyright (C) 2006, 2007 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
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
@@ -22,8 +20,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: md.c 8299 2007-08-13 08:41:18Z michi $
-
 */
 
 
 
 #include "vm/jit/s390/md-abi.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/threads-common.h"
-# include "threads/native/threads.h"
-#endif
+#include "threads/thread.h"
 
 #include "vm/exceptions.h"
 #include "vm/signallocal.h"
+
 #include "vm/jit/asmpart.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.h"
+#include "vm/jit/trap.h"
 
 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
 #include "vmcore/options.h" /* XXX debug */
 
 #include "vm/jit/codegen-common.h"
 #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);
@@ -97,14 +95,15 @@ void md_dump_context(u1 *pc, mcontext_t *mc) {
 
        log_println("Program counter: 0x%08X", pc);
 
-       pv = codegen_get_pv_from_pc_nocheck(pc);
+       pv = methodtree_find_nocheck(pc);
+
        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.",
-                       utf_bytes(m->class->name), utf_bytes(m->name), utf_bytes(m->descriptor)
+                       m->clazz->name->text, m->name->text, m->descriptor->text
                );
        }
 
@@ -126,11 +125,8 @@ void md_dump_context(u1 *pc, mcontext_t *mc) {
                log_println("\tf%d\t0x%016llX\t(double)%e\t(float)%f", i, freg.l, freg.fr.d, freg.fr.f);
        }
 
-#if defined(ENABLE_THREADS)
        log_println("Dumping the current stacktrace:");
-       threads_print_stacktrace();
-#endif
-
+       stacktrace_print_current();
 }
 
 /* md_signal_handler_sigsegv ***************************************************
@@ -142,7 +138,6 @@ void md_dump_context(u1 *pc, mcontext_t *mc) {
 
 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 {
-       stackframeinfo  sfi;
        ucontext_t     *_uc;
        mcontext_t     *_mc;
        u1             *pv;
@@ -162,11 +157,11 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 
        /* Check opcodes and verify it is a null pointer exception */
 
-       switch (xpc[0]) {
-               case 0x58: /* L */
-               case 0x50: /* ST */
-               case 0x55: /* CL (array size check on NULL array) */
-                       base = (xpc[2] >> 4) & 0xF;
+       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 (_mc->gregs[base] == 0) {
@@ -175,6 +170,9 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
                                is_null = 0;
                        }
                        break;
+               default:
+                       is_null = 0;
+                       break;
        }
 
        if (! is_null) {
@@ -187,34 +185,25 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
        pv = (u1 *)_mc->gregs[REG_PV] - N_PV_OFFSET;
        sp = (u1 *)_mc->gregs[REG_SP];
        ra = xpc;
-       type = EXCEPTION_HARDWARE_NULLPOINTER;
+       type = TRAP_NullPointerException;
        val = 0;
 
-       /* create stackframeinfo */
-
-       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
+       /* Handle the trap. */
 
-       /* Handle the type. */
-
-       p = signal_handle(xpc, type, val);
-
-       /* remove stackframeinfo */
-
-       stacktrace_remove_stackframeinfo(&sfi);
+       p = trap_handle(type, val, pv, sp, ra, xpc, _p);
 
        if (p != NULL) {
-               _mc->gregs[REG_ITMP1_XPTR] = (intptr_t) p;
-               _mc->gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
-               _mc->psw.addr              = (intptr_t) asm_handle_exception;
+               _mc->gregs[REG_ITMP3_XPTR] = (uintptr_t) p;
+               _mc->gregs[REG_ITMP1_XPC]  = (uintptr_t) xpc;
+               _mc->psw.addr              = (uintptr_t) asm_handle_exception;
        }
        else {
-               _mc->psw.addr              = (intptr_t) xpc;
+               _mc->psw.addr              = (uintptr_t) xpc;
        }
 }
 
 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
 {
-       stackframeinfo  sfi;
        ucontext_t     *_uc;
        mcontext_t     *_mc;
        u1             *xpc;
@@ -232,37 +221,53 @@ void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
 
        /* Our trap instruction has the format: { 0x02, one_byte_of_data }. */
 
-       if ((siginfo->si_code == ILL_ILLOPC) && (xpc[0] == 0x02)) {
+       if ((siginfo->si_code == ILL_ILLOPC) && (N_RR_GET_OPC(xpc) == OPC_ILL)) {
 
                /* bits 7-4 contain a register holding a value */
-               reg = (xpc[1] >> 4) & 0xF;
+               reg = N_ILL_GET_REG(xpc);
 
                /* bits 3-0 designate the exception type */
-               type = xpc[1] & 0xF;  
+               type = N_ILL_GET_TYPE(xpc);
 
                pv = (u1 *)_mc->gregs[REG_PV] - N_PV_OFFSET;
                sp = (u1 *)_mc->gregs[REG_SP];
                val = (ptrint)_mc->gregs[reg];
 
-               /* create stackframeinfo */
+               if (TRAP_COMPILER == type) {
+                       /* The PV from the compiler stub is equal to the XPC. */
 
-               stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
+                       pv = xpc;
 
-               /* Handle the type. */
+                       /* The return address in is REG_RA */
 
-               p = signal_handle(xpc, type, val);
+                       ra = (u1 *)_mc->gregs[REG_RA];
 
-               /* remove stackframeinfo */
+                       xpc = ra - 2;
+               }
 
-               stacktrace_remove_stackframeinfo(&sfi);
+               /* Handle the trap. */
 
-               if (p != NULL) {
-                       _mc->gregs[REG_ITMP1_XPTR] = (intptr_t) p;
-                       _mc->gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
-                       _mc->psw.addr              = (intptr_t) asm_handle_exception;
-               }
-               else {
-                       _mc->psw.addr              = (intptr_t) xpc;
+               p = trap_handle(type, val, pv, sp, ra, xpc, _p);
+
+               if (TRAP_COMPILER == type) {
+                       if (NULL == p) {
+                               _mc->gregs[REG_ITMP3_XPTR] = (uintptr_t) builtin_retrieve_exception();
+                               _mc->gregs[REG_ITMP1_XPC]  = (uintptr_t) ra - 2;
+                               _mc->gregs[REG_PV]         = (uintptr_t) md_codegen_get_pv_from_pc(ra);
+                               _mc->psw.addr              = (uintptr_t) asm_handle_exception;
+                       } else {
+                               _mc->gregs[REG_PV]         = (uintptr_t) p;
+                               _mc->psw.addr              = (uintptr_t) p;
+                       }
+               } else {
+                       if (p != NULL) {
+                               _mc->gregs[REG_ITMP3_XPTR] = (uintptr_t) p;
+                               _mc->gregs[REG_ITMP1_XPC]  = (uintptr_t) xpc;
+                               _mc->psw.addr              = (uintptr_t) asm_handle_exception;
+                       }
+                       else {
+                               _mc->psw.addr              = (uintptr_t) xpc;
+                       }
                }
        } else {
 #if !defined(NDEBUG)
@@ -281,7 +286,6 @@ void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
 
 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
 {
-       stackframeinfo  sfi;
        ucontext_t     *_uc;
        mcontext_t     *_mc;
        u1             *pv;
@@ -302,10 +306,10 @@ void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
 
        /* Check opcodes */
 
-       if (xpc[0] == 0x1D) { /* DR */
+       if (N_RR_GET_OPC(xpc) == OPC_DR) { /* DR */
 
-               r1 = (xpc[1] >> 4) & 0xF;
-               r2 = xpc[1] & 0xF;
+               r1 = N_RR_GET_REG1(xpc);
+               r2 = N_RR_GET_REG2(xpc);
 
                if (
                        (_mc->gregs[r1] == 0xFFFFFFFF) &&
@@ -331,24 +335,16 @@ void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
                        sp = (u1 *)_mc->gregs[REG_SP];
                        ra = xpc;
 
-                       type = EXCEPTION_HARDWARE_ARITHMETIC;
+                       type = TRAP_ArithmeticException;
                        val = 0;
 
-                       /* create stackframeinfo */
-
-                       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
+                       /* Handle the trap. */
 
-                       /* Handle the type. */
+                       p = trap_handle(type, val, pv, sp, ra, xpc, _p);
 
-                       p = signal_handle(xpc, type, val);
-
-                       /* remove stackframeinfo */
-
-                       stacktrace_remove_stackframeinfo(&sfi);
-
-                       _mc->gregs[REG_ITMP1_XPTR] = (intptr_t) p;
-                       _mc->gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
-                       _mc->psw.addr              = (intptr_t) asm_handle_exception;
+                       _mc->gregs[REG_ITMP3_XPTR] = (uintptr_t) p;
+                       _mc->gregs[REG_ITMP1_XPC]  = (uintptr_t) xpc;
+                       _mc->psw.addr              = (uintptr_t) asm_handle_exception;
 
                        return;
                }
@@ -392,77 +388,31 @@ void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 #endif
 
 
-#if defined(ENABLE_THREADS)
-void md_critical_section_restart(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)
 {
-       mcontext_t *_mc;
-       u1         *pc;
-       void       *npc;
-
-       _mc = &_uc->uc_mcontext;
-
-       pc = (u1 *)_mc->psw.addr;
-
-       npc = critical_find_restart_point(pc);
-
-       if (npc != NULL) {
-               log_println("%s: pc=%p, npc=%p", __FUNCTION__, pc, npc);
-               _mc->psw.addr = (ptrint) npc;
-       }
+       vm_abort("md_executionstate_read: IMPLEMENT ME!");
 }
-#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);
-
-       /* Calculate the branch displacement. */
-
-       disp = targetmpc - branchmpc;
-       disp += 4; /* size of branch */
-       disp /= 2; /* specified in halfwords */
-
-       ASSERT_VALID_BRANCH(disp);      
 
-       /* patch the branch instruction before the mcodeptr */
 
-       mcodeptr[-1] |= (disp & 0xFFFF);
-}
-
-
-/* md_stacktrace_get_returnaddress *********************************************
-
-   Returns the return address of the current stackframe, specified by
-   the passed stack pointer and the stack frame size.
-
-*******************************************************************************/
-
-u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
+/**
+ * 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)
 {
-       u1 *ra;
-
-       /* on S390 the return address is located on the top of the stackframe */
-
-       ra = *((u1 **) (sp + framesize - 8));
-
-       return ra;
+       vm_abort("md_executionstate_write: IMPLEMENT ME!");
 }
 
 
-/* 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
@@ -493,36 +443,36 @@ last 2 instructions the same as in invokevirtual
 
 *******************************************************************************/
 
-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, index;
-       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;
-       index = ra[1] & 0xF;
+       base  = N_RX_GET_BASE(pc);
+       index = N_RX_GET_INDEX(pc);
 
        /* check for the different calls */
 
        switch (base) {
-               case 0xd:
+               case REG_PV:
                        /* INVOKESTATIC/SPECIAL */
 
-               
                        switch (index) {
-                               case 0x0:
+                               case R0:
                                        /* the offset is in the load instruction */
-                                       offset = ((*(u2 *)(ra + 2)) & 0xFFF) + N_PV_OFFSET;
+                                       offset = N_RX_GET_DISP(pc) + N_PV_OFFSET;
                                        break;
-                               case 0x1:
+                               case REG_ITMP1:
                                        /* the offset is in the immediate load before the load */
-                                       offset = *((s2 *) (ra - 2));
+                                       offset = N_RI_GET_IMM(pc - SZ_L);
                                        break;
                                default:
                                        assert(0);
@@ -530,15 +480,14 @@ u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
 
                        /* add the offset to the procedure vector */
 
-                       pa = sfi->pv + offset;
-
+                       pa = ((uint8_t *) pv) + offset;
                        break;
 
-               case 0xc:
+               case REG_METHODPTR:
                        /* mptr relative */
                        /* INVOKEVIRTUAL/INTERFACE */
 
-                       offset = *((u2 *)(ra + 2)) & 0xFFF;
+                       offset = N_RX_GET_DISP(pc);
 
                        /* return NULL if no mptr was specified (used for replacement) */
 
@@ -547,11 +496,12 @@ u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
 
                        /* add offset to method pointer */
                        
-                       pa = mptr + offset;
+                       pa = (uint8_t *)mptr + offset;
                        break;
+
                default:
                        /* catch any problems */
-                       assert(0); 
+                       vm_abort("md_jit_method_patch_address");
                        break;
        }
 
@@ -559,70 +509,13 @@ u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
 }
 
 
-/* md_codegen_get_pv_from_pc ***************************************************
-
-   On this architecture just a wrapper function to
-   codegen_get_pv_from_pc.
-
-*******************************************************************************/
-
-u1 *md_codegen_get_pv_from_pc(u1 *ra)
-{
-       u1 *pv;
-
-       /* Get the start address of the function which contains this
-       address from the method table. */
-
-       pv = codegen_get_pv_from_pc(ra);
-
-       return pv;
-}
-
-
-/* md_cacheflush ***************************************************************
-
-   Calls the system's function to flush the instruction and data
-   cache.
-
-*******************************************************************************/
-
-void md_cacheflush(u1 *addr, s4 nbytes)
-{
-       /* do nothing */
-}
-
-
-/* md_icacheflush **************************************************************
-
-   Calls the system's function to flush the instruction cache.
-
-*******************************************************************************/
-
-void md_icacheflush(u1 *addr, s4 nbytes)
-{
-       /* do nothing */
-}
-
-
-/* md_dcacheflush **************************************************************
-
-   Calls the system's function to flush the data cache.
-
-*******************************************************************************/
-
-void md_dcacheflush(u1 *addr, s4 nbytes)
-{
-       /* do nothing */
-}
-
-
 /* md_patch_replacement_point **************************************************
 
    Patch the given replacement point.
 
 *******************************************************************************/
 #if defined(ENABLE_REPLACEMENT)
-void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
+void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
 {
        assert(0);
 }
@@ -646,8 +539,8 @@ void md_handle_exception(int32_t *regs, int64_t *fregs, int32_t *out) {
 
        /* get registers */
 
-       xptr = *(uint8_t **)(regs + REG_ITMP1_XPTR);
-       xpc = *(uint8_t **)(regs + REG_ITMP2_XPC);
+       xptr = *(uint8_t **)(regs + REG_ITMP3_XPTR);
+       xpc = *(uint8_t **)(regs + REG_ITMP1_XPC);
        sp = *(uint8_t **)(regs + REG_SP);
 
 
@@ -661,9 +554,9 @@ void md_handle_exception(int32_t *regs, int64_t *fregs, int32_t *out) {
 
                ++loops;
 
-               pv = codegen_get_pv_from_pc(xpc);
+               pv = methodtree_find(xpc);
 
-               handler = exceptions_handle_exception(xptr, xpc, pv, sp);
+               handler = exceptions_handle_exception((java_object_t *)xptr, xpc, pv, sp);
 
                if (handler == NULL) {
 
@@ -713,7 +606,7 @@ void md_handle_exception(int32_t *regs, int64_t *fregs, int32_t *out) {
 
                        /* new xpc is call before return address */
 
-                       xpc = ra;
+                       xpc = ra - 2;
 
                } else {
                        xpc = handler;
@@ -722,8 +615,8 @@ void md_handle_exception(int32_t *regs, int64_t *fregs, int32_t *out) {
 
        /* write new values for registers */
 
-       *(uint8_t **)(regs + REG_ITMP1_XPTR) = xptr;
-       *(uint8_t **)(regs + REG_ITMP2_XPC) = xpc;
+       *(uint8_t **)(regs + REG_ITMP3_XPTR) = xptr;
+       *(uint8_t **)(regs + REG_ITMP1_XPC) = xpc;
        *(uint8_t **)(regs + REG_SP) = sp;
        *(uint8_t **)(regs + REG_PV) = pv - 0XFFC;