* src/vm/jit/trap.c: Moved to C++
[cacao.git] / src / vm / jit / x86_64 / linux / md-os.c
index 30b7e4cf474dcfb49a0c247071885574d7b092c7..ca42a1926749c1b32914fee6f36e6f4fe16db3be 100644 (file)
 #include "vm/jit/x86_64/codegen.h"
 #include "vm/jit/x86_64/md.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
-#endif
+#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/executionstate.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/trap.hpp"
+#include "vm/jit/stacktrace.hpp"
 
 
 /* md_signal_handler_sigsegv ***************************************************
@@ -72,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;
@@ -154,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;
@@ -177,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;
-       }
 }
 
 
@@ -226,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;
@@ -239,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);
 }
 
 
@@ -272,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;
@@ -285,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);
 }
 
 
@@ -500,35 +483,6 @@ void md_executionstate_write(executionstate_t *es, void *context)
 }
 
 
-/* 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
-
-
 /*
  * 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