* src/threads/critical.c: Removed.
[cacao.git] / src / vm / jit / s390 / md.c
index 36b81f242acd63f556a9e36830192fd388a0208a..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.
 
 
 #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);
@@ -95,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.",
-                       m->class->name->text, m->name->text, m->descriptor->text
+                       m->clazz->name->text, m->name->text, m->descriptor->text
                );
        }
 
@@ -124,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 ***************************************************
@@ -187,20 +185,20 @@ 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;
 
-       /* Handle the type. */
+       /* Handle the trap. */
 
-       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
+       p = trap_handle(type, val, pv, sp, ra, xpc, _p);
 
        if (p != NULL) {
-               _mc->gregs[REG_ITMP3_XPTR] = (intptr_t) p;
-               _mc->gregs[REG_ITMP1_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;
        }
 }
 
@@ -235,17 +233,41 @@ void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
                sp = (u1 *)_mc->gregs[REG_SP];
                val = (ptrint)_mc->gregs[reg];
 
-               /* Handle the type. */
+               if (TRAP_COMPILER == type) {
+                       /* The PV from the compiler stub is equal to the XPC. */
+
+                       pv = xpc;
+
+                       /* The return address in is REG_RA */
 
-               p = signal_handle(type, val, pv, sp, ra, xpc, _p);
+                       ra = (u1 *)_mc->gregs[REG_RA];
 
-               if (p != NULL) {
-                       _mc->gregs[REG_ITMP3_XPTR] = (intptr_t) p;
-                       _mc->gregs[REG_ITMP1_XPC]  = (intptr_t) xpc;
-                       _mc->psw.addr              = (intptr_t) asm_handle_exception;
+                       xpc = ra - 2;
                }
-               else {
-                       _mc->psw.addr              = (intptr_t) xpc;
+
+               /* Handle the trap. */
+
+               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)
@@ -313,16 +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;
 
-                       /* Handle the type. */
+                       /* Handle the trap. */
 
-                       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
+                       p = trap_handle(type, val, pv, sp, ra, xpc, _p);
 
-                       _mc->gregs[REG_ITMP3_XPTR] = (intptr_t) p;
-                       _mc->gregs[REG_ITMP1_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;
                }
@@ -366,73 +388,27 @@ 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)
-{
-       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;
-       }
-}
-#endif
-
-
-/* md_codegen_patch_branch *****************************************************
-
-   Back-patches a branch instruction.
-
-*******************************************************************************/
-
-void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
+/**
+ * Read the given context into an executionstate.
+ *
+ * @param es      execution state
+ * @param context machine context
+ */
+void md_executionstate_read(executionstate_t* es, void* context)
 {
-
-       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);
+       vm_abort("md_executionstate_read: IMPLEMENT ME!");
 }
 
 
-/* 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!");
 }
 
 
@@ -520,7 +496,7 @@ void *md_jit_method_patch_address(void* pv, void *ra, void *mptr)
 
                        /* add offset to method pointer */
                        
-                       pa = mptr + offset;
+                       pa = (uint8_t *)mptr + offset;
                        break;
 
                default:
@@ -533,63 +509,6 @@ void *md_jit_method_patch_address(void* pv, void *ra, void *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.
@@ -635,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) {
 
@@ -687,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;