* Merged executionstate branch.
[cacao.git] / src / vm / jit / i386 / darwin / md-os.c
index 0c593d052cf217d9ccf55b7a6985acf256385681..a58cbb937c1a0da8d048e4deb5c9164caf6ebe28 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.h"
 
+#include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/global.h"
 #include "vm/signallocal.h"
 #include "vm/stringlocal.h"
+
 #include "vm/jit/asmpart.h"
+#include "vm/jit/executionstate.h"
 #include "vm/jit/stacktrace.h"
 
 #include "vm/jit/i386/codegen.h"
@@ -56,7 +58,6 @@
 
 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 {
-       stackframeinfo       sfi;
        ucontext_t          *_uc;
        mcontext_t           _mc;
        u1                  *pv;
@@ -72,6 +73,7 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
        intptr_t             val;
     int                  type;
     void                *p;
+       java_object_t       *o;
 
        _uc = (ucontext_t *) _p;
        _mc = _uc->uc_mcontext;
@@ -107,30 +109,58 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
             ((d == 4) ? _ss->esp :
             ((d == 5) ? _ss->ebp :
             ((d == 6) ? _ss->esi : _ss->edi))))));
-    }
-    else {
-        /* this was a normal NPE */
 
-        type = EXCEPTION_HARDWARE_NULLPOINTER;
-    }
+               if (type == EXCEPTION_HARDWARE_COMPILER) {
+                       /* The PV from the compiler stub is equal to the XPC. */
 
-       /* create stackframeinfo */
+                       pv = xpc;
 
-       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
+                       /* We use a framesize of zero here because the call pushed
+                          the return addres onto the stack. */
 
-       /* Handle the type. */
+                       ra = md_stacktrace_get_returnaddress(sp, 0);
+
+                       /* Skip the RA on the stack. */
 
-       p = signal_handle(xpc, type, val);
+                       sp = sp + 1 * SIZEOF_VOID_P;
 
-       /* remove stackframeinfo */
+                       /* The XPC is the RA minus 2, because the RA points to the
+                          instruction after the call. */
+
+                       xpc = ra - 2;
+               } 
+    }
+    else {
+        /* this was a normal NPE */
 
-       stacktrace_remove_stackframeinfo(&sfi);
+        type = EXCEPTION_HARDWARE_NULLPOINTER;
+    }
 
-    /* set registers */
+       /* Handle the type. */
 
-    _ss->eax = (intptr_t) p;
-       _ss->ecx = (intptr_t) xpc;
-       _ss->eip = (intptr_t) asm_handle_exception;
+       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
+
+       /* Set registers. */
+
+       if (type == EXCEPTION_HARDWARE_COMPILER) {
+               if (p == NULL) { 
+                       o = builtin_retrieve_exception();
+
+                       _ss->esp = (uintptr_t) sp;    /* Remove RA from stack. */
+
+                       _ss->eax = (uintptr_t) o;
+                       _ss->ecx = (uintptr_t) xpc;            /* REG_ITMP2_XPC */
+                       _ss->eip = (uintptr_t) asm_handle_exception;
+               }
+               else {
+                       _ss->eip = (uintptr_t) p;
+               }
+       }
+       else {
+               _ss->eax = (intptr_t) p;
+               _ss->ecx = (intptr_t) xpc;
+               _ss->eip = (intptr_t) asm_handle_exception;
+       }
 }
 
 
@@ -143,7 +173,6 @@ 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;
@@ -170,17 +199,9 @@ void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
     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);
-
-       /* remove stackframeinfo */
-
-       stacktrace_remove_stackframeinfo(&sfi);
+       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
 
     _ss->eax = (intptr_t) p;
        _ss->ecx = (intptr_t) xpc;
@@ -214,6 +235,124 @@ void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 }
 
 
+/* md_signal_handler_sigill ****************************************************
+
+   Signal handler for hardware patcher traps (ud2).
+
+*******************************************************************************/
+
+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                  *sp;
+       u1                  *ra;
+       u1                  *xpc;
+       int                  type;
+       intptr_t             val;
+       void                *p;
+
+
+       _uc = (ucontext_t *) _p;
+       _mc = _uc->uc_mcontext;
+       _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     */
+
+       /* this is an ArithmeticException */
+
+       type = EXCEPTION_HARDWARE_PATCHER;
+       val  = 0;
+
+       /* generate appropriate exception */
+
+       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
+
+       /* set registers (only if exception object ready) */
+
+       if (p != NULL) {
+               _ss->eax = (intptr_t) p;
+               _ss->ecx = (intptr_t) xpc;
+               _ss->eip = (intptr_t) asm_handle_exception;
+       }
+}
+
+/* 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;
+}
+
+
 /* md_critical_section_restart *************************************************
 
    Search the critical sections tree for a matching section and set