* Merged with default branch at rev 16f3633aaa5a.
[cacao.git] / src / vm / jit / arm / linux / md-os.c
index 3bc9af9fe5f5be21a0b6cc6623ef35cb4978065e..da889e87379581d30ac90c908a4a40b5f8e98eea 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: md.c 166 2006-01-22 23:38:44Z twisti $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 
 #include "vm/types.h"
 
@@ -51,10 +50,14 @@ typedef struct ucontext {
 
 #define scontext_t struct sigcontext
 
-#include "mm/memory.h"
+#if defined(ENABLE_THREADS)
+# include "threads/native/threads.h"
+#endif
+
 #include "vm/exceptions.h"
 #include "vm/signallocal.h"
 #include "vm/stringlocal.h"
+
 #include "vm/jit/asmpart.h"
 #include "vm/jit/stacktrace.h"
 
@@ -67,17 +70,18 @@ typedef struct ucontext {
 
 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 {
-       ucontext_t        *_uc;
-       scontext_t        *_sc;
-       u1                *pv;
-       u1                *sp;
-       u1                *ra;
-       u1                *xpc;
-       u4                 mcode;
-       ptrint             addr;
-       s4                 type;
-       ptrint             val;
-       java_objectheader *o;
+       stackframeinfo  sfi;
+       ucontext_t     *_uc;
+       scontext_t     *_sc;
+       u1             *pv;
+       u1             *sp;
+       u1             *ra;
+       u1             *xpc;
+       u4              mcode;
+       intptr_t        addr;
+       int             type;
+       intptr_t        val;
+       void           *p;
 
        _uc = (ucontext_t*) _p;
        _sc = &_uc->uc_mcontext;
@@ -92,11 +96,13 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 
        /* get exception-throwing instruction */
 
+       if (xpc == NULL)
+               vm_abort("md_signal_handler_sigsegv: the program counter is NULL");
+
        mcode = *((s4 *) xpc);
 
        /* this is a NullPointerException */
 
-/*     addr = _mc->gregs[s1]; */
        addr = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + ((mcode >> 16) & 0x0f));
        type = EXCEPTION_HARDWARE_NULLPOINTER;
        val  = 0;
@@ -104,15 +110,23 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
        if (addr != 0)
                vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
 
-       /* generate appropriate exception */
+       /* create stackframeinfo */
+
+       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
+
+       /* Handle the type. */
 
-       o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
+       p = signal_handle(xpc, type, val);
+
+       /* remove stackframeinfo */
+
+       stacktrace_remove_stackframeinfo(&sfi);
 
        /* set registers */
 
-       _sc->arm_r10 = (ptrint) o;
-       _sc->arm_fp  = (ptrint) xpc;
-       _sc->arm_pc  = (ptrint) asm_handle_exception;
+       _sc->arm_r10 = (intptr_t) p;
+       _sc->arm_fp  = (intptr_t) xpc;
+       _sc->arm_pc  = (intptr_t) asm_handle_exception;
 }
 
 
@@ -124,16 +138,17 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 
 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
 {
-       ucontext_t        *_uc;
-       scontext_t        *_sc;
-       u1                *pv;
-       u1                *sp;
-       u1                *ra;
-       u1                *xpc;
-       u4                 mcode;
-       s4                 type;
-       ptrint             val;
-       java_objectheader *o;
+       stackframeinfo  sfi;
+       ucontext_t     *_uc;
+       scontext_t     *_sc;
+       u1             *pv;
+       u1             *sp;
+       u1             *ra;
+       u1             *xpc;
+       u4              mcode;
+       int             type;
+       intptr_t        val;
+       void           *p;
 
        _uc = (ucontext_t*) _p;
        _sc = &_uc->uc_mcontext;
@@ -158,16 +173,54 @@ void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
        type = (mcode >> 8) & 0x0fff;
        val  = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + (mcode & 0x0f));
 
-       /* generate appropriate exception */
+       /* create stackframeinfo */
 
-       o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
+       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
 
-       /* set registers */
+       /* Handle the type. */
+
+       p = signal_handle(xpc, type, val);
+
+       /* remove stackframeinfo */
+
+       stacktrace_remove_stackframeinfo(&sfi);
+
+       /* set registers if we have an exception, return continue execution
+          otherwise (this is needed for patchers to work) */
+
+       if (p != NULL) {
+               _sc->arm_r10 = (intptr_t) p;
+               _sc->arm_fp  = (intptr_t) xpc;
+               _sc->arm_pc  = (intptr_t) asm_handle_exception;
+       }
+}
+
+
+/* md_signal_handler_sigusr1 ***************************************************
+
+   Signal handler for suspending threads.
+
+*******************************************************************************/
+
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
+{
+       ucontext_t *_uc;
+       scontext_t *_sc;
+       u1         *pc;
+       u1         *sp;
+
+       _uc = (ucontext_t *) _p;
+       _sc = &_uc->uc_mcontext;
+
+       /* get the PC and SP for this thread */
+       pc = (u1 *) _sc->arm_pc;
+       sp = (u1 *) _sc->arm_sp;
 
-       _sc->arm_r10 = (ptrint) o;
-       _sc->arm_fp  = (ptrint) xpc;
-       _sc->arm_pc  = (ptrint) asm_handle_exception;
+       /* now suspend the current thread */
+       threads_suspend_ack(pc, sp);
 }
+#endif
 
 
 /* md_signal_handler_sigusr2 ***************************************************
@@ -196,24 +249,28 @@ void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 #endif
 
 
-/* thread_restartcriticalsection ***********************************************
+/* md_critical_section_restart *************************************************
 
-   TODO: document me
+   Search the critical sections tree for a matching section and set
+   the PC to the restart point, if necessary.
 
 *******************************************************************************/
 
 #if defined(ENABLE_THREADS)
-void thread_restartcriticalsection(ucontext_t *_uc)
+void md_critical_section_restart(ucontext_t *_uc)
 {
        scontext_t *_sc;
-       void       *critical;
+       u1         *pc;
+       u1         *npc;
 
        _sc = &_uc->uc_mcontext;
 
-       critical = critical_find_restart_point((void *) _sc->arm_pc);
+       pc = (u1 *) _sc->arm_pc;
+
+       npc = critical_find_restart_point(pc);
 
-       if (critical)
-               _sc->arm_pc = (ptrint) critical;
+       if (npc != NULL)
+               _sc->arm_pc = (ptrint) npc;
 }
 #endif