Merged branch subtype-trunk into default.
[cacao.git] / src / vm / jit / i386 / darwin / md-os.c
index 0c593d052cf217d9ccf55b7a6985acf256385681..b9f3afd9de575a9b1697f2dc96553ee273ff33eb 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/i386/darwin/md-os.c - machine dependent i386 Darwin functions
 
-   Copyright (C) 1996-2005, 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) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
 #include "vm/types.h"
 
 #include "vm/jit/i386/codegen.h"
+#include "vm/jit/i386/md.h"
 
-#include "threads/threads-common.h"
+#include "threads/thread.hpp"
 
-#include "vm/exceptions.h"
+#include "vm/jit/builtin.hpp"
 #include "vm/global.h"
-#include "vm/signallocal.h"
-#include "vm/stringlocal.h"
+#include "vm/signallocal.hpp"
+
 #include "vm/jit/asmpart.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/executionstate.h"
+#include "vm/jit/stacktrace.hpp"
+#include "vm/jit/trap.h"
 
 #include "vm/jit/i386/codegen.h"
 
@@ -56,7 +57,6 @@
 
 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 {
-       stackframeinfo       sfi;
        ucontext_t          *_uc;
        mcontext_t           _mc;
        u1                  *pv;
@@ -64,73 +64,87 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
        u1                  *sp;
        u1                  *ra;
        u1                  *xpc;
-    u1                   opc;
-    u1                   mod;
-    u1                   rm;
-    int                  d;
-    int32_t              disp;
+       u1                   opc;
+       u1                   mod;
+       u1                   rm;
+       int                  d;
+       int32_t              disp;
        intptr_t             val;
-    int                  type;
-    void                *p;
+       int                  type;
+       void                *p;
 
        _uc = (ucontext_t *) _p;
        _mc = _uc->uc_mcontext;
-       _ss = &_mc->ss;
+       _ss = &_mc->__ss;
+
+       pv  = NULL;                 /* is resolved during stackframeinfo creation */
+       sp  = (u1 *) _ss->__esp;
+       xpc = (u1 *) _ss->__eip;
+       ra  = xpc;                              /* return address is equal to XPC */
+
+       /* get exception-throwing instruction */
 
-    pv  = NULL;                 /* is resolved during stackframeinfo creation */
-       sp  = (u1 *) _ss->esp;
-       xpc = (u1 *) _ss->eip;
-    ra  = xpc;                              /* return address is equal to XPC */
+       opc = M_ALD_MEM_GET_OPC(xpc);
+       mod = M_ALD_MEM_GET_MOD(xpc);
+       rm  = M_ALD_MEM_GET_RM(xpc);
 
-    /* get exception-throwing instruction */
+       /* for values see emit_mov_mem_reg and emit_mem */
 
-    opc = M_ALD_MEM_GET_OPC(xpc);
-    mod = M_ALD_MEM_GET_MOD(xpc);
-    rm  = M_ALD_MEM_GET_RM(xpc);
+       if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
+               /* this was a hardware-exception */
 
-    /* for values see emit_mov_mem_reg and emit_mem */
+               d    = M_ALD_MEM_GET_REG(xpc);
+               disp = M_ALD_MEM_GET_DISP(xpc);
 
-    if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
-        /* this was a hardware-exception */
+               /* we use the exception type as load displacement */
 
-        d    = M_ALD_MEM_GET_REG(xpc);
-        disp = M_ALD_MEM_GET_DISP(xpc);
+               type = disp;
 
-        /* we use the exception type as load displacement */
+               val = (d == 0) ? _ss->__eax :
+                       ((d == 1) ? _ss->__ecx :
+                       ((d == 2) ? _ss->__edx :
+                       ((d == 3) ? _ss->__ebx :
+                       ((d == 4) ? _ss->__esp :
+                       ((d == 5) ? _ss->__ebp :
+                       ((d == 6) ? _ss->__esi : _ss->__edi))))));
 
-        type = disp;
+               if (type == TRAP_COMPILER) {
+                       /* The PV from the compiler stub is equal to the XPC. */
 
-        val = (d == 0) ? _ss->eax :
-            ((d == 1) ? _ss->ecx :
-            ((d == 2) ? _ss->edx :
-            ((d == 3) ? _ss->ebx :
-            ((d == 4) ? _ss->esp :
-            ((d == 5) ? _ss->ebp :
-            ((d == 6) ? _ss->esi : _ss->edi))))));
-    }
-    else {
-        /* this was a normal NPE */
+                       pv = xpc;
 
-        type = EXCEPTION_HARDWARE_NULLPOINTER;
-    }
+                       /* We use a framesize of zero here because the call pushed
+                          the return addres onto the stack. */
 
-       /* create stackframeinfo */
+                       ra = md_stacktrace_get_returnaddress(sp, 0);
 
-       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
+                       /* Skip the RA on the stack. */
 
-       /* Handle the type. */
+                       sp = sp + 1 * SIZEOF_VOID_P;
 
-       p = signal_handle(xpc, type, val);
+                       /* The XPC is the RA minus 2, because the RA points to the
+                          instruction after the call. */
 
-       /* remove stackframeinfo */
+                       xpc = ra - 2;
+               } 
+       }
+       else {
+               /* this was a normal NPE */
 
-       stacktrace_remove_stackframeinfo(&sfi);
+               type = TRAP_NullPointerException;
+       }
 
-    /* set registers */
+       /* Handle the trap. */
 
-    _ss->eax = (intptr_t) p;
-       _ss->ecx = (intptr_t) xpc;
-       _ss->eip = (intptr_t) asm_handle_exception;
+       p = trap_handle(type, val, pv, sp, ra, xpc, _p);
+
+       /* Set registers. */
+
+       if (type == TRAP_COMPILER) {
+               if (p == NULL) {
+                       _ss->__esp = (uintptr_t) sp;  /* Remove RA from stack. */
+               }
+       }
 }
 
 
@@ -143,48 +157,34 @@ void md_signal_handler_sigsegv(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;
+       u1                  *pv;
        i386_thread_state_t *_ss;
        u1                  *sp;
        u1                  *ra;
        u1                  *xpc;
-    int                  type;
-    intptr_t             val;
-    void                *p;
+       int                  type;
+       intptr_t             val;
 
 
        _uc = (ucontext_t *) _p;
        _mc = _uc->uc_mcontext;
-       _ss = &_mc->ss;
+       _ss = &_mc->__ss;
 
-    pv  = NULL;                 /* is resolved during stackframeinfo creation */
-       sp  = (u1 *) _ss->esp;
-       xpc = (u1 *) _ss->eip;
+       pv  = NULL;                 /* is resolved during stackframeinfo creation */
+       sp  = (u1 *) _ss->__esp;
+       xpc = (u1 *) _ss->__eip;
        ra  = xpc;                          /* return address is equal to xpc     */
 
-    /* this is an ArithmeticException */
-
-    type = EXCEPTION_HARDWARE_ARITHMETIC;
-    val  = 0;
-
-       /* create stackframeinfo */
-
-       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
-
-       /* Handle the type. */
-
-       p = signal_handle(xpc, type, val);
+       /* This is an ArithmeticException */
 
-       /* remove stackframeinfo */
+       type = TRAP_ArithmeticException;
+       val  = 0;
 
-       stacktrace_remove_stackframeinfo(&sfi);
+       /* Handle the trap. */
 
-    _ss->eax = (intptr_t) p;
-       _ss->ecx = (intptr_t) xpc;
-       _ss->eip = (intptr_t) asm_handle_exception;
+       trap_handle(type, val, pv, sp, ra, xpc, _p);
 }
 
 
@@ -206,40 +206,119 @@ void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 
        _uc = (ucontext_t *) _p;
        _mc = _uc->uc_mcontext;
-       _ss = &_mc->ss;
+       _ss = &_mc->__ss;
 
-       pc = (u1 *) _ss->eip;
+       pc = (u1 *) _ss->__eip;
 
        t->pc = pc;
 }
 
 
-/* md_critical_section_restart *************************************************
+/* md_signal_handler_sigill ****************************************************
 
-   Search the critical sections tree for a matching section and set
-   the PC to the restart point, if necessary.
+   Signal handler for hardware patcher traps (ud2).
 
 *******************************************************************************/
 
-#if defined(ENABLE_THREADS)
-void thread_restartcriticalsection(ucontext_t *_uc)
+void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
 {
+       ucontext_t          *_uc;
        mcontext_t           _mc;
+       u1                  *pv;
        i386_thread_state_t *_ss;
-       u1                  *pc;
-       void                *rpc;
+       u1                  *sp;
+       u1                  *ra;
+       u1                  *xpc;
+       int                  type;
+       intptr_t             val;
+
 
+       _uc = (ucontext_t *) _p;
        _mc = _uc->uc_mcontext;
-       _ss = &_mc->ss;
+       _ss = &_mc->__ss;
+
+       pv  = NULL;                 /* is resolved during stackframeinfo creation */
+       sp  = (u1 *) _ss->__esp;
+       xpc = (u1 *) _ss->__eip;
+       ra  = xpc;                          /* return address is equal to xpc     */
 
-       pc = (u1 *) _ss->eip;
+       type = TRAP_PATCHER;
+       val  = 0;
 
-       rpc = critical_find_restart_point(pc);
+       /* Handle the trap. */
 
-       if (rpc != NULL)
-               _ss->eip = (ptrint) rpc;
+       trap_handle(type, val, pv, sp, ra, xpc, _p);
+}
+
+/* md_executionstate_read ******************************************************
+
+   Read the given context into an executionstate.
+
+*******************************************************************************/
+
+void md_executionstate_read(executionstate_t *es, void *context)
+{
+       ucontext_t          *_uc;
+       mcontext_t           _mc; 
+       i386_thread_state_t *_ss;
+       int                  i;
+
+       _uc = (ucontext_t *) context;
+       _mc = _uc->uc_mcontext;
+       _ss = &_mc->__ss;
+
+       /* read special registers */
+       es->pc = (u1 *) _ss->__eip;
+       es->sp = (u1 *) _ss->__esp;
+       es->pv = NULL;                   /* pv must be looked up via AVL tree */
+
+       /* read integer registers */
+       for (i = 0; i < INT_REG_CNT; i++)
+               es->intregs[i] = (i == 0) ? _ss->__eax :
+                       ((i == 1) ? _ss->__ecx :
+                       ((i == 2) ? _ss->__edx :
+                       ((i == 3) ? _ss->__ebx :
+                       ((i == 4) ? _ss->__esp :
+                       ((i == 5) ? _ss->__ebp :
+                       ((i == 6) ? _ss->__esi : _ss->__edi))))));
+
+       /* read float registers */
+       for (i = 0; i < FLT_REG_CNT; i++)
+               es->fltregs[i] = 0xdeadbeefdeadbeefULL;
+}
+
+
+/* md_executionstate_write *****************************************************
+
+   Write the given executionstate back to the context.
+
+*******************************************************************************/
+
+void md_executionstate_write(executionstate_t *es, void *context)
+{
+       ucontext_t*          _uc;
+       mcontext_t           _mc;
+       i386_thread_state_t* _ss;
+       int                  i;
+
+       _uc = (ucontext_t *) context;
+       _mc = _uc->uc_mcontext;
+       _ss = &_mc->__ss;
+
+       /* write integer registers */
+       for (i = 0; i < INT_REG_CNT; i++)
+               *((i == 0) ? &_ss->__eax :
+                ((i == 1) ? &_ss->__ecx :
+                ((i == 2) ? &_ss->__edx :
+                ((i == 3) ? &_ss->__ebx :
+                ((i == 4) ? &_ss->__esp :
+                ((i == 5) ? &_ss->__ebp :
+                ((i == 6) ? &_ss->__esi : &_ss->__edi))))))) = es->intregs[i];
+
+       /* write special registers */
+       _ss->__eip = (ptrint) es->pc;
+       _ss->__esp = (ptrint) es->sp;
 }
-#endif
 
 
 /*