* Moved all files from vmcore/ to vm/.
[cacao.git] / src / vm / jit / arm / linux / md-os.c
index d37c191c477d0bbc8dff8cf40da857bbf73a7099..191605bb7bb79a3f34ba71e57bd8c48c7745a919 100644 (file)
@@ -1,9 +1,7 @@
-/* src/vm/jit/arm/linux/md-os.c - machine dependent arm linux functions
+/* src/vm/jit/arm/linux/md-os.c - machine dependent ARM Linux 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.
 
    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"
 
+#include "vm/jit/disass.h"
+
 #include "vm/jit/arm/md-abi.h"
 
 #define ucontext broken_glibc_ucontext
@@ -51,16 +49,15 @@ typedef struct ucontext {
 
 #define scontext_t struct sigcontext
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
-#endif
+#include "threads/thread.hpp"
 
-#include "vm/exceptions.h"
+#include "vm/os.hpp"
 #include "vm/signallocal.h"
-#include "vm/stringlocal.h"
+#include "vm/vm.hpp"
 
 #include "vm/jit/asmpart.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/executionstate.h"
+#include "vm/jit/trap.h"
 
 
 /* md_signal_handler_sigsegv ***************************************************
@@ -71,17 +68,17 @@ 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;
+       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;
@@ -96,27 +93,26 @@ 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 */
+       /* This is a NullPointerException. */
 
-/*     addr = _mc->gregs[s1]; */
        addr = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + ((mcode >> 16) & 0x0f));
-       type = EXCEPTION_HARDWARE_NULLPOINTER;
+       type = addr;
        val  = 0;
 
-       if (addr != 0)
-               vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
-
-       /* generate appropriate exception */
+       /* Handle the trap. */
 
-       o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
+       p = trap_handle(type, val, pv, sp, ra, xpc, _p);
 
        /* set registers */
 
-       _sc->arm_r10 = (ptrint) o;
-       _sc->arm_fp  = (ptrint) xpc;
-       _sc->arm_pc  = (ptrint) asm_handle_exception;
+       _sc->arm_r10 = (uintptr_t) p;
+       _sc->arm_fp  = (uintptr_t) xpc;
+       _sc->arm_pc  = (uintptr_t) asm_handle_exception;
 }
 
 
@@ -128,16 +124,16 @@ 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;
+       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;
@@ -156,22 +152,57 @@ void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
 
        /* check for undefined instruction we use */
 
-       if ((mcode & 0x0ff000f0) != 0x07f000f0)
-               vm_abort("md_signal_handler_sigill: unknown illegal instruction");
+       if ((mcode & 0x0ff000f0) != 0x07f000f0) {
+               log_println("md_signal_handler_sigill: unknown illegal instruction: inst=%x", mcode);
+#if defined(ENABLE_DISASSEMBLER)
+               DISASSINSTR(xpc);
+#endif
+               vm_abort("Aborting...");
+       }
 
        type = (mcode >> 8) & 0x0fff;
        val  = *((s4 *) _sc + OFFSET(scontext_t, arm_r0)/4 + (mcode & 0x0f));
 
-       /* generate appropriate exception */
+       /* Handle the trap. */
 
-       o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
+       p = trap_handle(type, val, pv, sp, ra, xpc, _p);
 
-       /* set registers */
+       /* set registers if we have an exception, continue execution
+          otherwise (this is needed for patchers to work) */
+
+       if (p != NULL) {
+               _sc->arm_r10 = (uintptr_t) p;
+               _sc->arm_fp  = (uintptr_t) xpc;
+               _sc->arm_pc  = (uintptr_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 ***************************************************
@@ -200,26 +231,80 @@ void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 #endif
 
 
-/* thread_restartcriticalsection ***********************************************
+/**
+ * Read the given context into an executionstate.
+ *
+ * @param es      execution state
+ * @param context machine context
+ */
+void md_executionstate_read(executionstate_t *es, void *context)
+{
+       vm_abort("md_executionstate_read: IMPLEMENT ME!");
 
-   TODO: document me
+#if 0
+       ucontext_t *_uc;
+       mcontext_t *_mc;
+       int         i;
 
-*******************************************************************************/
+       _uc = (ucontext_t *) context;
+       _mc = &_uc->uc_mcontext;
 
-#if defined(ENABLE_THREADS)
-void thread_restartcriticalsection(ucontext_t *_uc)
-{
-       scontext_t *_sc;
-       void       *critical;
+       /* read special registers */
+       es->pc = (u1 *) _mc->sc_pc;
+       es->sp = (u1 *) _mc->sc_regs[REG_SP];
+       es->pv = (u1 *) _mc->sc_regs[REG_PV];
+       es->ra = (u1 *) _mc->sc_regs[REG_RA];
 
-       _sc = &_uc->uc_mcontext;
+       /* read integer registers */
+       for (i = 0; i < INT_REG_CNT; i++)
+               es->intregs[i] = _mc->sc_regs[i];
 
-       critical = critical_find_restart_point((void *) _sc->arm_pc);
+       /* read float registers */
+       /* Do not use the assignment operator '=', as the type of
+        * the _mc->sc_fpregs[i] can cause invalid conversions. */
 
-       if (critical)
-               _sc->arm_pc = (ptrint) critical;
+       assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
+       os_memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
+#endif
 }
+
+
+/**
+ * Write the given executionstate back to the context.
+ *
+ * @param es      execution state
+ * @param context machine context
+ */
+void md_executionstate_write(executionstate_t *es, void *context)
+{
+       vm_abort("md_executionstate_write: IMPLEMENT ME!");
+
+#if 0
+       ucontext_t *_uc;
+       mcontext_t *_mc;
+       int         i;
+
+       _uc = (ucontext_t *) context;
+       _mc = &_uc->uc_mcontext;
+
+       /* write integer registers */
+       for (i = 0; i < INT_REG_CNT; i++)
+               _mc->sc_regs[i] = es->intregs[i];
+
+       /* write float registers */
+       /* Do not use the assignment operator '=', as the type of
+        * the _mc->sc_fpregs[i] can cause invalid conversions. */
+
+       assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
+       os_memcpy(&_mc->sc_fpregs, &es->fltregs, sizeof(_mc->sc_fpregs));
+
+       /* write special registers */
+       _mc->sc_pc           = (ptrint) es->pc;
+       _mc->sc_regs[REG_SP] = (ptrint) es->sp;
+       _mc->sc_regs[REG_PV] = (ptrint) es->pv;
+       _mc->sc_regs[REG_RA] = (ptrint) es->ra;
 #endif
+}
 
 
 /*