* src/vm/jit/trap.cpp (trap_handle) [__MIPS__]: Use executionstate for trap
[cacao.git] / src / vm / jit / mips / linux / md-os.c
index b1db3e1e486d2c6fb475a6adf2025037b51b3766..e96052f300272ed85144f5b279a3b085220c1cdf 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/mips/linux/md-os.c - machine dependent MIPS Linux functions
 
-   Copyright (C) 1996-2005 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.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
-
-   Contact: cacao@complang.tuwien.ac.at
-
-   Authors: Andreas Krall
-            Reinhard Grafl
-
-   Changes: Christian Thalinger
-
-   $Id: md-os.c 3932 2005-12-09 15:10:04Z twisti $
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
 */
 
 
+#include "config.h"
+
 #include <assert.h>
+#include <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
 #include <signal.h>
+#include <stdint.h>
 #include <ucontext.h>
 
-#include "config.h"
 #include "vm/types.h"
 
+#include "vm/jit/mips/codegen.h"
+#include "vm/jit/mips/md.h"
 #include "vm/jit/mips/md-abi.h"
 
-#include "mm/boehm.h"
-#include "vm/exceptions.h"
-#include "vm/stringlocal.h"
+#include "mm/gc.hpp"
+#include "mm/memory.hpp"
+
+#include "vm/signallocal.hpp"
+#include "vm/os.hpp"
+
 #include "vm/jit/asmpart.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/executionstate.h"
+#include "vm/jit/trap.hpp"
 
 
 /* md_init *********************************************************************
 
 void md_init(void)
 {
-       /* The Boehm GC initialization blocks the SIGSEGV signal. So we do a      */
-       /* dummy allocation here to ensure that the GC is initialized.            */
+       /* The Boehm GC initialization blocks the SIGSEGV signal. So we do
+          a dummy allocation here to ensure that the GC is
+          initialized. */
 
-       heap_allocate(1, 0, NULL);
+#if defined(ENABLE_GC_BOEHM)
+       (void) GCNEW(int);
+#endif
 
 #if 0
        /* Turn off flush-to-zero */
@@ -76,69 +77,275 @@ void md_init(void)
 }
 
 
-/* signal_handler_sigsegv ******************************************************
+/* md_signal_handler_sigsegv ***************************************************
 
-   NullPointerException signal handler for hardware null pointer check.
+   NullPointerException signal handler for hardware null pointer
+   check.
 
 *******************************************************************************/
 
-void signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
+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;
-
-       _uc = (struct ucontext *) _p;
-       _mc = &_uc->uc_mcontext;
+       ucontext_t     *_uc;
+       mcontext_t     *_mc;
+       greg_t         *_gregs;
+       u1             *pv;
+       u1             *sp;
+       u1             *ra;
+       u1             *xpc;
+       unsigned int    cause;
+       u4              mcode;
+       int             d;
+       int             s1;
+       int16_t         disp;
+       intptr_t        val;
+       intptr_t        addr;
+       int             type;
+
+       _uc    = (struct ucontext *) _p;
+       _mc    = &_uc->uc_mcontext;
+
+#if defined(__UCLIBC__)
+       _gregs = _mc->gpregs;
+#else  
+       _gregs = _mc->gregs;
+#endif
+
+       /* In glibc's ucontext.h the registers are defined as long long,
+          even for MIPS32, so we cast them.  This is not the case for
+          uClibc. */
+
+       pv  = (u1 *) (ptrint) _gregs[REG_PV];
+       sp  = (u1 *) (ptrint) _gregs[REG_SP];
+       ra  = (u1 *) (ptrint) _gregs[REG_RA];        /* this is correct for leafs */
+
+#if !defined(__UCLIBC__)
+# if ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 5))
+       /* NOTE: We only need this for pre glibc-2.5. */
 
-       /* in ucontext.h the registers are defined as long long, even for
-          MIPS32, so we cast them */
-       
-       instr = *((u4 *) ((ptrint) _mc->pc));
-       addr = _mc->gregs[(instr >> 21) & 0x1f];
+       xpc = (u1 *) (ptrint) _mc->pc;
 
-       if (addr == 0) {
-               pv  = (u1 *) (ptrint) _mc->gregs[REG_PV];
-               sp  = (u1 *) (ptrint) _mc->gregs[REG_SP];
-               ra  = (u1 *) (ptrint) _mc->gregs[REG_RA]; /* this is correct for leafs*/
-               xpc = (u1 *) (ptrint) _mc->pc;
+       /* get the cause of this exception */
 
-               _mc->gregs[REG_ITMP1_XPTR] =
-                       (ptrint) stacktrace_hardware_nullpointerexception(pv, sp, ra, xpc);
+       cause = _mc->cause;
 
-               _mc->gregs[REG_ITMP2_XPC] = (ptrint) xpc;
-               _mc->pc = (ptrint) asm_handle_exception;
+       /* check the cause to find the faulting instruction */
 
-       } else {
-               addr += (long) ((instr << 16) >> 16);
+       /* TODO: use defines for that stuff */
 
-               throw_cacao_exception_exit(string_java_lang_InternalError,
-                                                                  "faulting address: 0x%lx at 0x%lx\n",
-                                                                  addr, _mc->pc);
+       switch (cause & 0x0000003c) {
+       case 0x00000008:
+               /* TLBL: XPC is ok */
+               break;
+
+       case 0x00000010:
+               /* AdEL: XPC is of the following instruction */
+               xpc = xpc - 4;
+               break;
        }
+# else
+       xpc = (u1 *) (ptrint) _mc->pc;
+# endif
+#else
+       xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
+#endif
+
+       /* get exception-throwing instruction */
+
+       mcode = *((u4 *) xpc);
+
+       d    = M_ITYPE_GET_RT(mcode);
+       s1   = M_ITYPE_GET_RS(mcode);
+       disp = M_ITYPE_GET_IMM(mcode);
+
+       /* check for special-load */
+
+       if (s1 == REG_ZERO) {
+               /* we use the exception type as load displacement */
+
+               type = disp;
+               val  = _gregs[d];
+
+               if (type == TRAP_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
+                  define is 0. */
+
+               addr = _gregs[s1];
+               type = (int) addr;
+               val  = 0;
+       }
+
+       /* Handle the trap. */
+
+       trap_handle(type, val, pv, sp, ra, xpc, _p);
 }
 
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
-void thread_restartcriticalsection(ucontext_t *_uc)
+/**
+ * Signal handler for patcher calls.
+ */
+void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
 {
-       mcontext_t *_mc;
-       void *critical;
+       ucontext_t* _uc = (struct ucontext *) _p;
+       mcontext_t* _mc = &_uc->uc_mcontext;
+       greg_t* _gregs;
+
+#if defined(__UCLIBC__)
+       _gregs = _mc->gpregs;
+#else  
+       _gregs = _mc->gregs;
+#endif
+
+       // In glibc's ucontext.h the registers are defined as long long
+       // int, even for MIPS32, so we cast them.  This is not the case
+       // for uClibc.
+       void* pv  = (void*) (uintptr_t) _gregs[REG_PV];
+       void* sp  = (void*) (uintptr_t) _gregs[REG_SP];
+       void* ra  = (void*) (uintptr_t) _gregs[REG_RA]; // The RA is correct for leaf methods.
+
+#if defined(__UCLIBC__)
+       void* xpc = (void*) (uintptr_t) _gregs[CTX_EPC];
+#else
+       void* xpc = (void*) (uintptr_t) _mc->pc;
+#endif
+
+       // This signal is always a patcher.
+       int      type = TRAP_PATCHER;
+       intptr_t val  = 0;
+
+       // Handle the trap.
+       void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
+
+       // Set registers
+       if (p == NULL) {
+               // We set the PC again because the cause may have changed the
+               // XPC.
+#if defined(__UCLIBC__)
+               _gregs[CTX_EPC] = (uintptr_t) xpc;
+#else
+               _mc->pc         = (uintptr_t) xpc;
+#endif
+       }
+}
+
 
+/* md_signal_handler_sigusr2 ***************************************************
+
+   DOCUMENT ME
+
+*******************************************************************************/
+
+void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
+{
+}
+
+
+/**
+ * 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;
+       mcontext_t* _mc;
+       greg_t*     _gregs;
+       int         i;
+
+       vm_abort("md_executionstate_read: PLEASE REVISE ME!");
+
+       _uc = (ucontext_t*) context;
        _mc = &_uc->uc_mcontext;
 
-       critical = thread_checkcritical((void *) (ptrint) _mc->pc);
+#if defined(__UCLIBC__)
+       _gregs = _mc->gpregs;
+#else  
+       _gregs = _mc->gregs;
+#endif
+
+       /* Read special registers. */
+
+       /* In glibc's ucontext.h the registers are defined as long long,
+          even for MIPS32, so we cast them.  This is not the case for
+          uClibc. */
+
+#if defined(__UCLIBC__)
+       es->pc = _gregs[CTX_EPC];
+#else
+       es->pc = (void*) (uintptr_t) _mc->pc;
+#endif
+
+       es->sp = (void*) (uintptr_t) _gregs[REG_SP];
+       es->pv = (void*) (uintptr_t) _gregs[REG_PV];
+       es->ra = (void*) (uintptr_t) _gregs[REG_RA];
+
+       /* Read integer registers. */
 
-       if (critical)
-               _mc->pc = (ptrint) critical;
+       for (i = 0; i < INT_REG_CNT; i++)
+               es->intregs[i] = _gregs[i];
+
+       /* Read float registers. */
+
+       /* Do not use the assignment operator '=', as the type of the
+          _mc->fpregs[i] can cause invalid conversions. */
+
+       assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
+       os_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
 }
+
+
+/**
+ * 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;
+       mcontext_t* _mc;
+       greg_t*     _gregs;
+       int         i;
+
+       vm_abort("md_executionstate_write: PLEASE REVISE ME!");
+
+       _uc = (ucontext_t *) context;
+       _mc = &_uc->uc_mcontext;
+
+       /* Write integer registers. */
+
+       for (i = 0; i < INT_REG_CNT; i++)
+               _gregs[i] = es->intregs[i];
+
+       /* Write float registers. */
+
+       /* Do not use the assignment operator '=', as the type of the
+          _mc->fpregs[i] can cause invalid conversions. */
+
+       assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
+       os_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
+
+       /* Write special registers. */
+
+#if defined(__UCLIBC__)
+       _gregs[CTX_EPC] = es->pc;
+#else
+       _mc->pc         = (uintptr_t) es->pc;
 #endif
 
+       _gregs[REG_SP]  = (uintptr_t) es->sp;
+       _gregs[REG_PV]  = (uintptr_t) es->pv;
+       _gregs[REG_RA]  = (uintptr_t) es->ra;
+}
+
 
 /*
  * These are local overrides for various environment variables in Emacs.
@@ -151,4 +358,5 @@ void thread_restartcriticalsection(ucontext_t *_uc)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */