* src/vm/jit/x86_64/codegen.c (codegen): Use code variables.
[cacao.git] / src / vm / jit / x86_64 / md.c
index af77eeaa1b0499ffeb9cbb07c257511d48101a73..e2c68ac5ee755011b6e8babe6e545e77e447e094 100644 (file)
@@ -26,9 +26,9 @@
 
    Authors: Christian Thalinger
 
-   Changes:
+   Changes: Edwin Steiner
 
-   $Id: md.c 4357 2006-01-22 23:33:38Z twisti $
+   $Id: md.c 5051 2006-06-28 15:39:22Z twisti $
 
 */
 
 
 #include "config.h"
 
+#include <assert.h>
 #include <stdlib.h>
 #include <ucontext.h>
 
 #include "vm/jit/x86_64/md-abi.h"
 
+#if defined(ENABLE_THREADS)
+# include "threads/native/threads.h"
+#endif
+
 #include "vm/exceptions.h"
 #include "vm/signallocal.h"
 #include "vm/jit/asmpart.h"
 #include "vm/jit/stacktrace.h"
 
+#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
+#include "vm/options.h" /* XXX debug */
+#include "vm/jit/disass.h" /* XXX debug */
+#endif
+
 
 /* md_init *********************************************************************
 
@@ -78,8 +88,8 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
        _uc = (ucontext_t *) _p;
        _mc = &_uc->uc_mcontext;
 
-       /* ATTENTION: don't use CACAO internal REG_* defines as they are          */
-       /* different to the ones in <ucontext.h>                                  */
+       /* 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];
@@ -111,8 +121,8 @@ void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
        _uc = (ucontext_t *) _p;
        _mc = &_uc->uc_mcontext;
 
-       /* ATTENTION: don't use CACAO internal REG_* defines as they are          */
-       /* different to the ones in <ucontext.h>                                  */
+       /* 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];
@@ -126,15 +136,46 @@ void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
 }
 
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
-void thread_restartcriticalsection(ucontext_t *uc)
+/* md_signal_handler_sigusr2 ***************************************************
+
+   NullPointerException signal handler for hardware null pointer
+   check.
+
+*******************************************************************************/
+
+void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 {
-       void *critical;
+       threadobject *t;
+       ucontext_t   *_uc;
+       mcontext_t   *_mc;
+       u1           *pc;
+
+       t = THREADOBJECT;
+
+       _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>. */
+
+       pc = (u1 *) _mc->gregs[REG_RIP];
+
+       t->pc = pc;
+}
+
+
+#if defined(ENABLE_THREADS)
+void thread_restartcriticalsection(ucontext_t *_uc)
+{
+       mcontext_t *_mc;
+       void       *pc;
+
+       _mc = &_uc->uc_mcontext;
 
-       critical = thread_checkcritical((void *) uc->uc_mcontext.gregs[REG_RIP]);
+       pc = critical_find_restart_point((void *) _mc->gregs[REG_RIP]);
 
-       if (critical)
-               uc->uc_mcontext.gregs[REG_RIP] = (ptrint) critical;
+       if (pc != NULL)
+               _mc->gregs[REG_RIP] = (ptrint) pc;
 }
 #endif
 
@@ -158,6 +199,77 @@ u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
 }
 
 
+/* md_get_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
+   to the right base address (PV or REG_METHODPTR).
+
+   INVOKESTATIC/SPECIAL:
+
+   49 ba 98 3a ed ab aa 2a 00 00    mov    $0x2aaaabed3a98,%r10
+   49 ff d2                         rex64Z callq  *%r10
+
+   INVOKEVIRTUAL:
+
+   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
+
+*******************************************************************************/
+
+u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
+{
+       u1  mcode;
+       s4  offset;
+       u1 *pa;                             /* patch address                      */
+
+       /* go back to the actual call instruction (3-bytes) */
+
+       ra = ra - 3;
+
+       /* get the last byte of the call */
+
+       mcode = ra[2];
+
+       /* check for the different calls */
+
+       /* INVOKESTATIC/SPECIAL */
+
+       if (mcode == 0xd2) {
+               /* patch address is 8-bytes before the call instruction */
+
+               pa = ra - 8;
+
+       } else if (mcode == 0xd3) {
+               /* INVOKEVIRTUAL/INTERFACE */
+
+               /* Get the offset from the instruction (the offset address is
+                  4-bytes before the call instruction). */
+
+               offset = *((s4 *) (ra - 4));
+
+               /* add the offset to the method pointer */
+
+               pa = mptr + offset;
+
+       } else {
+               /* catch any problems */
+
+               assert(0);
+       }
+
+       return pa;
+}
+
+
 /* md_codegen_findmethod *******************************************************
 
    On this architecture just a wrapper function to codegen_findmethod.
@@ -177,6 +289,81 @@ u1 *md_codegen_findmethod(u1 *ra)
 }
 
 
+/* 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.
+
+*******************************************************************************/
+
+void md_patch_replacement_point(rplpoint *rp)
+{
+    u8 mcode;
+
+       /* XXX this is probably unsafe! */
+
+       /* save the current machine code */
+       mcode = *(u8*)rp->pc;
+
+       /* write spinning instruction */
+       *(u2*)(rp->pc) = 0xebfe;
+
+       /* write 5th byte */
+       rp->pc[4] = (rp->mcode >> 32);
+
+       /* write first word */
+    *(u4*)(rp->pc) = (u4) rp->mcode;
+
+       /* 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); */
+}
+
 /*
  * 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
@@ -188,4 +375,5 @@ u1 *md_codegen_findmethod(u1 *ra)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */