Merged?
[cacao.git] / src / vm / jit / powerpc / linux / md-os.c
index 737439741e45ed69d7cb037c87c68926f37b5ec8..6628953d6290968ab9e55ea16747087a3eda6b60 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: md-os.c 8243 2007-07-31 08:57:54Z michi $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 #include <ucontext.h>
 
 #include "vm/types.h"
 
 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 {
-       stackframeinfo     sfi;
-       ucontext_t        *_uc;
-       mcontext_t        *_mc;
-       unsigned long     *_gregs;
-       u1                *pv;
-       u1                *sp;
-       u1                *ra;
-       u1                *xpc;
-       u4                 mcode;
-       s4                 s1;
-       s4                 disp;
-       s4                 d;
-       ptrint             addr;
-       ptrint             val;
-       s4                 type;
-       java_objectheader *e;
+       ucontext_t     *_uc;
+       mcontext_t     *_mc;
+       unsigned long  *_gregs;
+       u1             *pv;
+       u1             *sp;
+       u1             *ra;
+       u1             *xpc;
+       u4              mcode;
+       int             s1;
+       int16_t         disp;
+       int             d;
+       intptr_t        addr;
+       intptr_t        val;
+       int             type;
+       void           *p;
 
        _uc = (ucontext_t *) _p;
 
@@ -109,6 +107,13 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
                /* we use the exception type as load displacement */
 
                type = disp;
+
+               if (type == EXCEPTION_HARDWARE_COMPILER) {
+                       /* The XPC is the RA minus 4, because the RA points to the
+                          instruction after the call. */
+
+                       xpc = ra - 4;
+               }
        }
        else {
                /* This is a normal NPE: addr must be NULL and the NPE-type
@@ -121,24 +126,44 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
                        vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
        }
 
-       /* create stackframeinfo */
+       /* Handle the type. */
+
+       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
+
+       /* Set registers. */
 
-       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
+       switch (type) {
+       case EXCEPTION_HARDWARE_COMPILER:
+               if (p != NULL) {
+                       _gregs[REG_PV] = (uintptr_t) p;
+                       _gregs[PT_NIP] = (uintptr_t) p;
+                       break;
+               }
 
-       /* generate appropriate exception */
+               /* Get and set the PV from the parent Java method. */
 
-       e = exceptions_new_hardware_exception(xpc, type, val);
+               pv = md_codegen_get_pv_from_pc(ra);
 
-       /* remove stackframeinfo */
+               _gregs[REG_PV] = (uintptr_t) pv;
 
-       stacktrace_remove_stackframeinfo(&sfi);
+               /* Get the exception object. */
 
-       /* set registers (only if exception object ready) */
+               p = exceptions_get_and_clear_exception();
 
-       if (e != NULL) {
-               _gregs[REG_ITMP1_XPTR] = (ptrint) e;
-               _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
-               _gregs[PT_NIP]         = (ptrint) asm_handle_exception;
+               assert(p != NULL);
+
+               /* fall-through */
+
+       case EXCEPTION_HARDWARE_PATCHER:
+               if (p == NULL)
+                       break;
+
+               /* fall-through */
+               
+       default:
+               _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
+               _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
+               _gregs[PT_NIP]         = (uintptr_t) asm_handle_exception;
        }
 }
 
@@ -151,19 +176,18 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 
 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
 {
-       stackframeinfo     sfi;
-       ucontext_t        *_uc;
-       mcontext_t        *_mc;
-       unsigned long     *_gregs;
-       u1                *pv;
-       u1                *sp;
-       u1                *ra;
-       u1                *xpc;
-       u4                 mcode;
-       s4                 s1;
-       ptrint             val;
-       s4                 type;
-       java_objectheader *e;
+       ucontext_t     *_uc;
+       mcontext_t     *_mc;
+       unsigned long  *_gregs;
+       u1             *pv;
+       u1             *sp;
+       u1             *ra;
+       u1             *xpc;
+       u4              mcode;
+       int             s1;
+       intptr_t        val;
+       int             type;
+       void           *p;
 
        _uc = (ucontext_t *) _p;
 
@@ -191,24 +215,53 @@ void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
        type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
        val  = _gregs[s1];
 
-       /* create stackframeinfo */
+       /* Handle the type. */
 
-       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
+       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
 
-       /* generate appropriate exception */
+       /* set registers */
 
-       e = exceptions_new_hardware_exception(xpc, type, val);
+       _gregs[REG_ITMP1_XPTR] = (intptr_t) p;
+       _gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
+       _gregs[PT_NIP]         = (intptr_t) asm_handle_exception;
+}
 
-       /* remove stackframeinfo */
 
-       stacktrace_remove_stackframeinfo(&sfi);
+/* md_signal_handler_sigusr1 ***************************************************
 
-       /* set registers */
+   Signal handler for suspending threads.
+
+*******************************************************************************/
 
-       _gregs[REG_ITMP1_XPTR] = (ptrint) e;
-       _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
-       _gregs[PT_NIP]         = (ptrint) asm_handle_exception;
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
+{
+       ucontext_t    *_uc;
+       mcontext_t    *_mc;
+       unsigned long *_gregs;
+       u1            *pc;
+       u1            *sp;
+
+       _uc = (ucontext_t *) _p;
+
+#if defined(__UCLIBC__)
+       _mc    = &(_uc->uc_mcontext);
+       _gregs = _mc->regs->gpr;
+#else
+       _mc    = _uc->uc_mcontext.uc_regs;
+       _gregs = _mc->gregs;
+#endif
+
+       /* get the PC and SP for this thread */
+
+       pc = (u1 *) _gregs[PT_NIP];
+       sp = (u1 *) _gregs[REG_SP];
+
+       /* now suspend the current thread */
+
+       threads_suspend_ack(pc, sp);
 }
+#endif
 
 
 /* md_signal_handler_sigusr2 ***************************************************