* src/vm/jit/trap.c (trap_handle): Update executionstates PC register in case
[cacao.git] / src / vm / jit / alpha / linux / md-os.c
index 8bc0aec27f2dc5e739a67ca4b0d2a4d04736f5f6..8e127a9fab4e3bae10487216f640ecdda617c28a 100644 (file)
@@ -1,9 +1,8 @@
-/* src/vm/jit/alpha/linux/md.c - machine dependent Alpha Linux functions
+/* src/vm/jit/alpha/linux/md-os.c - machine dependent Alpha 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
+   Copyright (C) 2008 Theobroma Systems Ltd.
 
    This file is part of CACAO.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: md-os.c 7251 2007-01-29 20:24:53Z twisti $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 #include <ucontext.h>
 
 #include "vm/types.h"
 
+#include "vm/jit/alpha/codegen.h"
+#include "vm/jit/alpha/md.h"
 #include "vm/jit/alpha/md-abi.h"
 
-#include "vm/exceptions.h"
+#include "threads/thread.hpp"
+
+#include "vm/jit/builtin.hpp"
 #include "vm/signallocal.h"
+#include "vm/os.hpp"
 
 #include "vm/jit/asmpart.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/disass.h"
+#include "vm/jit/executionstate.h"
+#include "vm/jit/trap.h"
 
 
 /* md_signal_handler_sigsegv ***************************************************
 
 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 {
-       ucontext_t  *_uc;
-       mcontext_t  *_mc;
-       u4           instr;
-       ptrint       addr;
-       u1          *pv;
-       u1          *sp;
-       u1          *ra;
-       u1          *xpc;
+       ucontext_t     *_uc;
+       mcontext_t     *_mc;
+       u1             *pv;
+       u1             *sp;
+       u1             *ra;
+       u1             *xpc;
+       u4              mcode;
+       s4              d;
+       s4              s1;
+       s4              disp;
+       intptr_t        val;
+       intptr_t        addr;
+       int             type;
 
        _uc = (ucontext_t *) _p;
        _mc = &_uc->uc_mcontext;
@@ -69,32 +79,121 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
        ra  = (u1 *) _mc->sc_regs[REG_RA];           /* this is correct for leafs */
        xpc = (u1 *) _mc->sc_pc;
 
-       instr = *((s4 *) (_mc->sc_pc));
-       addr = _mc->sc_regs[(instr >> 16) & 0x1f];
+       /* get exception-throwing instruction */
+
+       mcode = *((u4 *) xpc);
+
+       d    = M_MEM_GET_Ra(mcode);
+       s1   = M_MEM_GET_Rb(mcode);
+       disp = M_MEM_GET_Memory_disp(mcode);
+
+       val  = _mc->sc_regs[d];
 
-       if (addr == 0) {
-               _mc->sc_regs[REG_ITMP1_XPTR] =
-                       (ptrint) stacktrace_hardware_nullpointerexception(pv, sp, ra, xpc);
+       /* check for special-load */
 
-               _mc->sc_regs[REG_ITMP2_XPC] = (ptrint) xpc;
-               _mc->sc_pc = (ptrint) asm_handle_exception;
+       if (s1 == REG_ZERO) {
+               /* we use the exception type as load displacement */
+
+               type = disp;
+
+               if (type == TRAP_COMPILER) {
+                       /* The XPC is the RA minus 1, because the RA points to the
+                          instruction after the call. */
+
+                       xpc = ra - 4;
+               }
        }
        else {
-               codegen_get_pv_from_pc(xpc);
+               /* This is a normal NPE: addr must be NULL and the NPE-type
+                  define is 0. */
+
+               addr = _mc->sc_regs[s1];
+               type = (int) addr;
+       }
+
+       /* Handle the trap. */
+
+       trap_handle(type, val, pv, sp, ra, xpc, _p);
+}
+
 
-               /* this should not happen */
+/* md_signal_handler_sigill ****************************************************
 
-               assert(0);
+   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;
+       mcontext_t* _mc = &_uc->uc_mcontext;
+
+       void* pv  = (u1 *) _mc->sc_regs[REG_PV];
+       void* sp  = (u1 *) _mc->sc_regs[REG_SP];
+       void* ra  = (u1 *) _mc->sc_regs[REG_RA]; // RA is correct for leaf methods.
+       void* xpc = (u1 *) _mc->sc_pc;
+
+       // The PC points to the instruction after the illegal instruction.
+       xpc = (void*) (((uintptr_t) xpc) - 4);
+
+       // Get the exception-throwing instruction.
+       uint32_t mcode = *((uint32_t*) xpc);
+
+       int opcode = M_OP3_GET_Opcode(mcode);
+
+       // Check for undefined instruction we use.
+       // TODO Check the whole instruction.
+       if (opcode != 0x4) {
+               log_println("md_signal_handler_sigill: Unknown illegal instruction %x at %p", mcode, xpc);
+#if defined(ENABLE_DISASSEMBLER)
+               (void) disassinstr(xpc);
+#endif
+               vm_abort("Aborting...");
        }
+
+       // This signal is always a patcher.
+       int      type = TRAP_PATCHER;
+       intptr_t val  = 0;
+
+       // Handle the trap.
+       trap_handle(type, val, pv, sp, ra, xpc, _p);
 }
 
 
+/* 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;
+       mcontext_t *_mc;
+       u1         *pc;
+       u1         *sp;
+
+       _uc = (ucontext_t *) _p;
+       _mc = &_uc->uc_mcontext;
+
+       /* get the PC and SP for this thread */
+       pc = (u1 *) _mc->sc_pc;
+       sp = (u1 *) _mc->sc_regs[REG_SP];
+
+       /* now suspend the current thread */
+       threads_suspend_ack(pc, sp);
+}
+#endif
+
+
 /* md_signal_handler_sigusr2 ***************************************************
 
    Signal handler for profiling sampling.
 
 *******************************************************************************/
 
+#if defined(ENABLE_THREADS)
 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 {
        threadobject *tobj;
@@ -111,25 +210,75 @@ void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 
        tobj->pc = pc;
 }
+#endif
 
 
-#if defined(ENABLE_THREADS)
-void thread_restartcriticalsection(ucontext_t *_uc)
+/* 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;
-       u1         *pc;
-       u1         *npc;
+       int         i;
 
+       _uc = (ucontext_t *) context;
        _mc = &_uc->uc_mcontext;
 
-       pc = (u1 *) _mc->sc_pc;
+       /* 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];
 
-       npc = critical_find_restart_point(pc);
+       /* read integer registers */
+       for (i = 0; i < INT_REG_CNT; i++)
+               es->intregs[i] = _mc->sc_regs[i];
 
-       if (npc != NULL)
-               _mc->sc_pc = (ptrint) npc;
+       /* read 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(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
+}
+
+
+/* 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;
+       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
 
 
 /*