* Merged in twisti-branch.
[cacao.git] / src / vm / jit / mips / linux / md-os.c
index cb8e2639e0aa3dc7d3184e4ac0fd3691c969b918..71b4f0c25d0ab158b4bb0182dc2de471fc8f1395 100644 (file)
@@ -22,7 +22,7 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: md-os.c 7252 2007-01-29 21:09:01Z twisti $
+   $Id: md-os.c 7570 2007-03-24 22:20:09Z twisti $
 
 */
 
@@ -36,6 +36,7 @@
 
 #include "vm/types.h"
 
+#include "vm/jit/mips/codegen.h"
 #include "vm/jit/mips/md-abi.h"
 
 #include "mm/gc-common.h"
@@ -84,15 +85,22 @@ void md_init(void)
 
 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 {
-       ucontext_t  *_uc;
-       mcontext_t  *_mc;
-       greg_t      *_gregs;
-       u4           instr;
-       ptrint       addr;
-       u1          *pv;
-       u1          *sp;
-       u1          *ra;
-       u1          *xpc;
+       ucontext_t        *_uc;
+       mcontext_t        *_mc;
+       greg_t            *_gregs;
+       u1                *pv;
+       u1                *sp;
+       u1                *ra;
+       u1                *xpc;
+       unsigned int       cause;
+       u4                 mcode;
+       s4                 d;
+       s4                 s1;
+       s4                 disp;
+       ptrint             val;
+       ptrint             addr;
+       s4                 type;
+       java_objectheader *o;
 
        _uc    = (struct ucontext *) _p;
        _mc    = &_uc->uc_mcontext;
@@ -113,32 +121,69 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 
 #if defined(__UCLIBC__)
        xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
+
+#error how to get the cause?
 #else
        xpc = (u1 *) (ptrint) _mc->pc;
+
+       /* get the cause of this exception */
+
+       cause = _mc->cause;
 #endif
 
-       instr = *((u4 *) xpc);
-       addr  = _gregs[(instr >> 21) & 0x1f];
+       /* check the cause to find the faulting instruction */
 
-       if (addr == 0) {
-               _gregs[REG_ITMP1_XPTR] =
-                       (ptrint) stacktrace_hardware_nullpointerexception(pv, sp, ra, xpc);
+       /* TODO: use defines for that stuff */
 
-               _gregs[REG_ITMP2_XPC] = (ptrint) xpc;
+       switch (cause & 0x0000003c) {
+       case 0x00000008:
+               /* TLBL: XPC is ok */
+               break;
 
-#if defined(__UCLIBC__)
-               _gregs[CTX_EPC] = (ptrint) asm_handle_exception;
-#else
-               _mc->pc         = (ptrint) asm_handle_exception;
-#endif
+       case 0x00000010:
+               /* AdEL: XPC is of the following instruction */
+               xpc = xpc - 4;
        }
-       else {
-               codegen_get_pv_from_pc(xpc);
 
-               /* this should not happen */
+       /* 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);
+
+       val   = _gregs[d];
+
+       /* check for special-load */
+
+       if (s1 == REG_ZERO) {
+               /* we use the exception type as load displacement */
 
-               assert(0);
+               type = disp;
        }
+       else {
+               /* This is a normal NPE: addr must be NULL and the NPE-type
+                  define is 0. */
+
+               addr = _gregs[s1];
+               type = (s4) addr;
+       }
+
+       /* generate appropriate exception */
+
+       o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
+
+       /* set registers */
+
+       _gregs[REG_ITMP1_XPTR] = (ptrint) o;
+       _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
+
+#if defined(__UCLIBC__)
+       _gregs[CTX_EPC]        = (ptrint) asm_handle_exception;
+#else
+       _mc->pc                = (ptrint) asm_handle_exception;
+#endif
 }