* src/vm/jit/trap.c: Moved to C++
[cacao.git] / src / vm / jit / x86_64 / linux / md-os.c
index 1a2e7dbf008eac1732af273ddeeccdd158e9731d..ca42a1926749c1b32914fee6f36e6f4fe16db3be 100644 (file)
 #include "vm/jit/x86_64/codegen.h"
 #include "vm/jit/x86_64/md.h"
 
-#include "threads/thread.h"
+#include "threads/thread.hpp"
 
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
-#include "vm/signallocal.h"
+#include "vm/jit/builtin.hpp"
+#include "vm/signallocal.hpp"
 
 #include "vm/jit/asmpart.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/executionstate.h"
+#include "vm/jit/trap.hpp"
+#include "vm/jit/stacktrace.hpp"
 
 
 /* md_signal_handler_sigsegv ***************************************************
@@ -69,7 +70,6 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
        int             type;
        intptr_t        val;
        void           *p;
-       java_object_t  *o;
 
        _uc = (ucontext_t *) _p;
        _mc = &_uc->uc_mcontext;
@@ -151,7 +151,7 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 
                val = _mc->gregs[d];
 
-               if (type == EXCEPTION_HARDWARE_COMPILER) {
+               if (type == TRAP_COMPILER) {
                        /* The PV from the compiler stub is equal to the XPC. */
 
                        pv = xpc;
@@ -174,35 +174,21 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
        else {
                /* this was a normal NPE */
 
-               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);
 
        /* Set registers. */
 
-       if (type == EXCEPTION_HARDWARE_COMPILER) {
+       if (type == TRAP_COMPILER) {
                if (p == NULL) {
-                       o = builtin_retrieve_exception();
-
                        _mc->gregs[REG_RSP] = (uintptr_t) sp;    /* Remove RA from stack. */
-
-                       _mc->gregs[REG_RAX] = (uintptr_t) o;
-                       _mc->gregs[REG_R10] = (uintptr_t) xpc;           /* REG_ITMP2_XPC */
-                       _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
-               }
-               else {
-                       _mc->gregs[REG_RIP] = (uintptr_t) p;
                }
        }
-       else {
-               _mc->gregs[REG_RAX] = (uintptr_t) p;
-               _mc->gregs[REG_R10] = (uintptr_t) xpc;               /* REG_ITMP2_XPC */
-               _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
-       }
 }
 
 
@@ -223,7 +209,6 @@ void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
        u1             *xpc;
        int             type;
        intptr_t        val;
-       void           *p;
 
        _uc = (ucontext_t *) _p;
        _mc = &_uc->uc_mcontext;
@@ -236,20 +221,14 @@ void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
        xpc = (u1 *) _mc->gregs[REG_RIP];
        ra  = xpc;                          /* return address is equal to xpc     */
 
-       /* this is an ArithmeticException */
+       /* This is an ArithmeticException. */
 
-       type = EXCEPTION_HARDWARE_ARITHMETIC;
+       type = TRAP_ArithmeticException;
        val  = 0;
 
-       /* Handle the type. */
-
-       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
-
-       /* set registers */
+       /* Handle the trap. */
 
-       _mc->gregs[REG_RAX] = (intptr_t) p;
-       _mc->gregs[REG_R10] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
-       _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
+       trap_handle(type, val, pv, sp, ra, xpc, _p);
 }
 
 
@@ -269,7 +248,6 @@ void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
        u1             *xpc;
        int             type;
        intptr_t        val;
-       void           *p;
 
        _uc = (ucontext_t *) _p;
        _mc = &_uc->uc_mcontext;
@@ -282,22 +260,30 @@ void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
        xpc = (u1 *) _mc->gregs[REG_RIP];
        ra  = xpc;                          /* return address is equal to xpc     */
 
+       // Check if the trap instruction is valid.
+       // TODO Move this into patcher_handler.
+       if (patcher_is_valid_trap_instruction_at(xpc) == false) {
+               // Check if the PC has been patched during our way to this
+               // signal handler (see PR85).
+               if (patcher_is_patched_at(xpc) == true)
+                       return;
+
+               // We have a problem...
+               log_println("md_signal_handler_sigill: Unknown illegal instruction at 0x%lx", xpc);
+#if defined(ENABLE_DISASSEMBLER)
+               (void) disassinstr(xpc);
+#endif
+               vm_abort("Aborting...");
+       }
+
        /* This is a patcher. */
 
-       type = EXCEPTION_HARDWARE_PATCHER;
+       type = TRAP_PATCHER;
        val  = 0;
 
-       /* Handle the type. */
-
-       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
+       /* Handle the trap. */
 
-       /* set registers */
-
-       if (p != NULL) {
-               _mc->gregs[REG_RAX] = (intptr_t) p;
-               _mc->gregs[REG_R10] = (intptr_t) xpc;                /* REG_ITMP2_XPC */
-               _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
-       }
+       trap_handle(type, val, pv, sp, ra, xpc, _p);
 }
 
 
@@ -360,14 +346,13 @@ void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 #endif
 
 
-/* md_replace_executionstate_read **********************************************
+/* md_executionstate_read ******************************************************
 
-   Read the given context into an executionstate for Replacement.
+   Read the given context into an executionstate.
 
 *******************************************************************************/
 
-#if defined(ENABLE_REPLACEMENT)
-void md_replace_executionstate_read(executionstate_t *es, void *context)
+void md_executionstate_read(executionstate_t *es, void *context)
 {
        ucontext_t *_uc;
        mcontext_t *_mc;
@@ -430,17 +415,15 @@ void md_replace_executionstate_read(executionstate_t *es, void *context)
        for (i = 0; i < FLT_REG_CNT; i++)
                es->fltregs[i] = 0xdeadbeefdeadbeefL;
 }
-#endif
 
 
-/* md_replace_executionstate_write *********************************************
+/* md_executionstate_write *****************************************************
 
-   Write the given executionstate back to the context for Replacement.
+   Write the given executionstate back to the context.
 
 *******************************************************************************/
 
-#if defined(ENABLE_REPLACEMENT)
-void md_replace_executionstate_write(executionstate_t *es, void *context)
+void md_executionstate_write(executionstate_t *es, void *context)
 {
        ucontext_t *_uc;
        mcontext_t *_mc;
@@ -498,36 +481,6 @@ void md_replace_executionstate_write(executionstate_t *es, void *context)
        _mc->gregs[REG_RIP] = (ptrint) es->pc;
        _mc->gregs[REG_RSP] = (ptrint) es->sp;
 }
-#endif
-
-
-/* md_critical_section_restart *************************************************
-
-   Search the critical sections tree for a matching section and set
-   the PC to the restart point, if necessary.
-
-*******************************************************************************/
-
-#if defined(ENABLE_THREADS)
-void md_critical_section_restart(ucontext_t *_uc)
-{
-       mcontext_t *_mc;
-       u1         *pc;
-       u1         *npc;
-
-       _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];
-
-       npc = critical_find_restart_point(pc);
-
-       if (npc != NULL)
-               _mc->gregs[REG_RIP] = (ptrint) npc;
-}
-#endif
 
 
 /*