* src/threads/posix/thread-posix.cpp: Implemented thread suspension mechanism.
[cacao.git] / src / vm / jit / arm / linux / md-os.c
index 1b7ef06f230b299b4c99e0c2c2f7d9995a39ba73..bd1475985669790e6c93e192877a40644fd284c3 100644 (file)
@@ -1,9 +1,8 @@
-/* src/vm/jit/arm/linux/md.c - machine dependent arm linux functions
+/* src/vm/jit/arm/linux/md-os.c - machine dependent ARM Linux functions
 
-   Copyright (C) 1996-2005, 2006 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
+   Copyright (C) 2008, 2009 Theobroma Systems Ltd.
 
    This file is part of CACAO.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Michael Starzinger
-
-   Changes: Christian Thalinger
-
-   $Id: md.c 166 2006-01-22 23:38:44Z twisti $
-
 */
 
 
 #include "config.h"
 
-#include "vm/types.h"
-
-#include "vm/jit/arm/md-abi.h"
+#include <stdint.h>
 
 #define ucontext broken_glibc_ucontext
 #define ucontext_t broken_glibc_ucontext_t
@@ -55,83 +44,149 @@ typedef struct ucontext {
 
 #define scontext_t struct sigcontext
 
-#include "mm/memory.h"
-#include "vm/exceptions.h"
-#include "vm/signallocal.h"
-#include "vm/stringlocal.h"
+#include "vm/types.h"
+
+#include "vm/jit/arm/md.h"
+#include "vm/jit/arm/md-abi.h"
+
+#include "threads/thread.hpp"
+
+#include "vm/signallocal.hpp"
+
 #include "vm/jit/asmpart.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/executionstate.h"
+#include "vm/jit/trap.hpp"
+
+
+/**
+ * Signal handler for hardware exceptions.
+ */
+void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
+{
+       ucontext_t* _uc = (ucontext_t*) _p;
+       scontext_t* _sc = &_uc->uc_mcontext;
+
+       /* ATTENTION: glibc included messed up kernel headers we needed a
+          workaround for the ucontext structure. */
+
+       void* xpc = (u1 *) _sc->arm_pc;
+
+       // Handle the trap.
+       trap_handle(TRAP_SIGSEGV, xpc, _p);
+}
+
+
+/**
+ * Illegal instruction signal handler for hardware exception checks.
+ */
+void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
+{
+       ucontext_t* _uc = (ucontext_t*) _p;
+       scontext_t* _sc = &_uc->uc_mcontext;
+
+       /* ATTENTION: glibc included messed up kernel headers we needed a
+          workaround for the ucontext structure. */
+
+       void* xpc = (void*) _sc->arm_pc;
+
+       // Handle the trap.
+       trap_handle(TRAP_SIGILL, xpc, _p);
+}
 
 
-/* md_signal_handler_sigsegv ***************************************************
+/* md_signal_handler_sigusr2 ***************************************************
 
-   NullPointerException signal handler for hardware null pointer
-   check.
+   Signal handler for profiling sampling.
 
 *******************************************************************************/
 
-void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
+#if defined(ENABLE_THREADS)
+void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 {
-       ucontext_t *_uc;
-       /*mcontext_t *_mc;*/
-       scontext_t *_sc;
-       u4          instr;
-       ptrint      addr;
-       ptrint      base;
-       u1          *pv;
-       u1          *sp;
-       u1          *ra;
-       u1          *xpc;
+       threadobject *thread;
+       ucontext_t   *_uc;
+       scontext_t   *_sc;
+       u1           *pc;
+
+       thread = THREADOBJECT;
 
        _uc = (ucontext_t*) _p;
        _sc = &_uc->uc_mcontext;
 
-       /* ATTENTION: glibc included messed up kernel headers */
-       /* we needed a workaround for the ucontext structure */
-
-       addr = (ptrint) siginfo->si_addr;
-       /*xpc = (u1*) _mc->gregs[REG_PC];*/
-       xpc = (u1*) _sc->arm_pc;
-
-       instr = *((s4*) xpc);
-       base = *((s4*) _sc + OFFSET(scontext_t, arm_r0)/4 + ((instr >> 16) & 0x0f));
-
-       if (base == 0) {
-               pv  = (u1*) _sc->arm_ip;
-               sp  = (u1*) _sc->arm_sp;
-               ra  = (u1*) _sc->arm_lr; /* this is correct for leafs */
-
-               _sc->arm_r10 = (ptrint) stacktrace_hardware_nullpointerexception(pv, sp, ra, xpc);
-               _sc->arm_fp = (ptrint) xpc;
-               _sc->arm_pc = (ptrint) asm_handle_exception;
-       } else {
-               throw_cacao_exception_exit(string_java_lang_InternalError,
-                  "Segmentation fault: %p (pc=%p, instr=%x, base=%p)\n",
-                  addr, xpc, instr, base);
-       }
+       pc = (u1 *) _sc->arm_pc;
+
+       thread->pc = pc;
 }
+#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)
+{
+       ucontext_t *_uc;
+       scontext_t *_sc;
+       int         i;
 
-   TODO: document me
+       _uc = (ucontext_t *) context;
+       _sc = &_uc->uc_mcontext;
 
-*******************************************************************************/
+       /* ATTENTION: glibc included messed up kernel headers we needed a
+          workaround for the ucontext structure. */
 
-#if defined(ENABLE_THREADS)
-void thread_restartcriticalsection(ucontext_t *_uc)
+       /* read special registers */
+
+       es->pc = (u1 *) _sc->arm_pc;
+       es->sp = (u1 *) _sc->arm_sp;
+       es->pv = (u1 *) _sc->arm_ip;
+       es->ra = (u1 *) _sc->arm_lr;
+
+       /* read integer registers */
+
+       for (i = 0; i < INT_REG_CNT; i++)
+               es->intregs[i] = *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i);
+
+       /* read float registers */
+
+       for (i = 0; i < FLT_REG_CNT; i++)
+               es->fltregs[i] = 0xdeadbeefdeadbeefULL;
+}
+
+
+/**
+ * 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)
 {
+       ucontext_t *_uc;
        scontext_t *_sc;
-       void       *critical;
+       int         i;
 
+       _uc = (ucontext_t *) context;
        _sc = &_uc->uc_mcontext;
 
-       critical = critical_find_restart_point((void *) _sc->arm_pc);
+       /* ATTENTION: glibc included messed up kernel headers we needed a
+          workaround for the ucontext structure. */
+
+       /* write integer registers */
 
-       if (critical)
-               _sc->arm_pc = (ptrint) critical;
+       for (i = 0; i < INT_REG_CNT; i++)
+               *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i) = es->intregs[i];
+
+       /* write special registers */
+
+       _sc->arm_pc = (ptrint) es->pc;
+       _sc->arm_sp = (ptrint) es->sp;
+       _sc->arm_ip = (ptrint) es->pv;
+       _sc->arm_lr = (ptrint) es->ra;
 }
-#endif
 
 
 /*