merged volatile memory barriers
[cacao.git] / src / vm / jit / m68k / linux / md-os.c
index b3e47e16d80fe3d473f8ffbbf51c5359e4ebc7b5..359541fb2e34cb922e21b769a2962a1f6a4facd7 100644 (file)
@@ -1,9 +1,7 @@
-/* src/vm/jit/m68k/linux/md-os.c - linux specific functions
+/* src/vm/jit/m68k/linux/md-os.c - m68k linux specific 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: arch.h 5330 2006-09-05 18:43:12Z edwin $
-
 */
 
+
 #include "config.h"
 
-#include "md-os.h"
-#include "md-abi.h"
+#include "vm/jit/m68k/md.h"
+#include "vm/jit/m68k/linux/md-abi.h"
+
+#include "vm/signallocal.hpp"
+#include "vm/vm.hpp"
 
-#include "vm/vm.h"
-#include "vm/exceptions.h"
 #include "vm/jit/asmpart.h"
+#include "vm/jit/trap.hpp"
 
 #include <assert.h>
 #include <stdlib.h>
-#include <signal.h>
+/*#include <signal.h>*/
 #include <stdint.h>
 #include <sys/ucontext.h> /* has br0ken ucontext_t*/
 
@@ -68,35 +67,25 @@ typedef struct actual_ucontext {
 } actual_ucontext_t;
 
 
-/*
- *     linux specific initializations
- */
-void md_init_linux()
-{
-       struct sigaction act;
-       
-       act.sa_sigaction = md_signal_handler_sigill;
-       act.sa_flags     = SA_NODEFER | SA_SIGINFO;
-
-       if (sigaction(SIGILL, &act, NULL) == -1)        {
-               vm_abort("md_linux_init: Error registering signal handler");
-       }
-}
 
 /* md_signal_handler_sigsegv ******************************************
  *
- * Invoked when a Nullpointerexception occured, or when the c
+ * Invoked when a Nullpointerexception occured, or when the v
  * crashes, hard to tell the difference. 
  **********************************************************************/
-void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, actual_ucontext_t *_uc) 
+
+void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 {      
        uint32_t        xpc, sp;
        uint16_t        opc;
        uint32_t        val, regval, off;
        bool            adrreg;
-       java_objectheader *e;
-       mcontext_t      *_mc;
+       void        *p;
+       actual_mcontext_t       *_mc;
+       actual_ucontext_t       *_uc;
+       int type;
 
+       _uc = (actual_ucontext_t*)_p;
        _mc = &_uc->uc_mcontext;
        sp = _mc->gregs[R_SP];
        xpc = _mc->gregs[R_PC];
@@ -115,7 +104,9 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, actual_ucontext_t *_
                val = (opc >> 9) & 0x0007;
                off = *(uint16_t*)(xpc+1);
        } else {
-               fprintf(stderr, "SEGV: short instructions %x\n", opc);
+               
+               /*fprintf(stderr, "SEGV: short instructions %x\n", opc);
+               */
                /* now check the 32 bit sized instructions */
                if ((opc & (2<<3)) == (2<<3))   {
                        if (opc & (1<<6)) adrreg = true;                /* M_L*X */
@@ -130,20 +121,18 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, actual_ucontext_t *_
 
        /* val is now register number, adreg == true if it is an address regsiter */
        regval = _mc->gregs[adrreg ? GREGS_ADRREG_OFF + val : val];
-       /*
-       if (regval != 0)        {
-               vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", regval);
-       }*/
+       type   = regval;
 
+       /* Handle the type. */
 
-       fprintf(stderr, "SEGV: sp=%x, xpc=%x, regval=%x\n", sp, xpc, regval);
-       e = exceptions_new_hardware_exception(0, sp, xpc, xpc, EXCEPTION_HARDWARE_NULLPOINTER, regval);
+       p = trap_handle(type, regval, NULL, (void*)sp, (void*)xpc, (void*)xpc, _p);
 
-       _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (ptrint) e;
-       _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (ptrint) xpc;
-       _mc->gregs[R_PC]          = (ptrint) asm_handle_exception;
+       _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (intptr_t) p;
+       _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (intptr_t) xpc;
+       _mc->gregs[R_PC]                             = (intptr_t) asm_handle_exception;
 }
 
+
 /* md_signal_handler_sigill *******************************************
  *
  * This handler is used to generate hardware exceptions.
@@ -152,16 +141,22 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, actual_ucontext_t *_
  * been created directly before the trap instruction (2 bytes long).
  * the last 3 bit of this tst instruction contain the register number.
  **********************************************************************/
-void md_signal_handler_sigill(int sig, siginfo_t *siginfo, actual_ucontext_t *_uc) 
+
+void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
 {
-       uint32_t        xpc, sp;
+       uint32_t        xpc, sp, ra, pv;
        uint16_t        opc;
        uint32_t        type;
-       uint32_t        val, regval;
-       java_objectheader *e;
-       mcontext_t      *_mc;
+       uint32_t        regval;
+       void        *p;
+       actual_mcontext_t       *_mc;
+       actual_ucontext_t       *_uc;
 
-       xpc = siginfo->si_addr;
+       _uc = (actual_ucontext_t*)_p;
+       xpc = (uint32_t)siginfo->si_addr;
+
+       pv = NULL;      /* we have no pv */
+       ra = xpc;       /* that is ture for most cases */
 
        if (siginfo->si_code == ILL_ILLOPC)     {
                vm_abort("md_signal_handler_sigill: the illegal instruction @ 0x%x, aborting", xpc);
@@ -181,35 +176,80 @@ void md_signal_handler_sigill(int sig, siginfo_t *siginfo, actual_ucontext_t *_u
        /* Figure out in which register the object causing the exception resides for appropiate exceptions
         */
        switch (type)   {
-               case EXCEPTION_HARDWARE_EXCEPTION:
+               case TRAP_NullPointerException:
+               case TRAP_ArithmeticException:
+               case TRAP_CHECK_EXCEPTION:
                        /* nothing */
                        break;
-               case EXCEPTION_HARDWARE_CLASSCAST: 
+
+               case TRAP_ClassCastException: 
                        regval = *(uint16_t*)(xpc-4);
                        assert( (regval&0xfff0) == 0x4a00 );
                        /* was in a address register */
                        regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
                        break;
-               case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
+
+               case TRAP_ArrayIndexOutOfBoundsException:
                        regval = *(uint16_t*)(xpc-4);
                        assert( (regval&0xfff0) == 0x4a00 );
                        /* was a data register */
                        regval = _mc->gregs[regval & 0x7];
                        break;
-               case M68K_EXCEPTION_HARDWARE_NULLPOINTER:
-                       type = EXCEPTION_HARDWARE_NULLPOINTER;
+
+               case TRAP_COMPILER:
+                       regval = *(uint16_t*)(xpc-4);
+                       assert( (regval&0xfff0) == 0x4a00 );
+                       /* was in a address register */
+                       regval = _mc->gregs[ GREGS_ADRREG_OFF + (regval & 0x7) ];
+
+                       pv = xpc-4;     /* the compiler stub consists of 2 instructions */
+                       ra = md_stacktrace_get_returnaddress(sp, 0);
+                       sp = sp + SIZEOF_VOID_P;
+                       xpc = ra - 2;
                        break;
 
-               default: assert(0);
+               case TRAP_PATCHER:
+                       xpc -= 2;
+                       ra = xpc;
+                       break;
        }
 
-       /*fprintf(stderr, "NEW HWE: sp=%x, xpc=%x, tpye=%x, regval=%x\n", sp, xpc, type, regval);
-       */
-       e = exceptions_new_hardware_exception(0, sp, xpc, xpc, type, regval);
+       /* Handle the trap. */
+
+       p = trap_handle(type, regval, pv, (void*)sp, (void*)ra, (void*)xpc, _p);
+
+       switch (type)   {
+       case TRAP_COMPILER:
+               if (p == NULL) {
+                       /* exception when compiling the method */
+                       java_object_t *o = builtin_retrieve_exception();
+
+                       _mc->gregs[R_SP] = sp;  /* remove RA from stack */
+
+                       _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (uintptr_t) o;
+                       _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (uintptr_t) xpc;
+                       _mc->gregs[R_PC]                             = (uintptr_t) asm_handle_exception;
+               }
+               else {
+                       /* compilation ok, execute */
+                       _mc->gregs[R_PC] = p;
+               }
+               break;
+
+       case TRAP_PATCHER:
+               if (p == NULL) { 
+                       /* No exception while patching, continue. */
+                       _mc->gregs[R_PC] = xpc; 
+                       return; 
+               }
+               /* fall-through in case of exception */
 
-       _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (ptrint) e;
-       _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (ptrint) xpc;
-       _mc->gregs[R_PC]          = (ptrint) asm_handle_exception;
+       default:
+               /* a normal exception with normal expcetion handling */
+               _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP1]     = (uintptr_t) p;
+               _mc->gregs[GREGS_ADRREG_OFF + REG_ATMP2_XPC] = (uintptr_t) xpc;
+               _mc->gregs[R_PC]                             = (uintptr_t) asm_handle_exception;
+       }
 }