* src/vm/jit/replace.h (executionstate_t): Added 'ra' field for return
[cacao.git] / src / vm / jit / alpha / linux / md-os.c
index 4a559964e33d2fc312f1646c7cf1397450c90d98..1cbe4d26e4e0f7158f6b4ec6c0097f7c07634815 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: md-os.c 7638 2007-04-02 21:24:59Z 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"
 
 #if defined(ENABLE_THREADS)
 # include "threads/native/threads.h"
 #endif
 
+#include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/signallocal.h"
 
 
 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 {
-       ucontext_t        *_uc;
-       mcontext_t        *_mc;
-       u1                *pv;
-       u1                *sp;
-       u1                *ra;
-       u1                *xpc;
-       u4                 mcode;
-       s4                 d;
-       s4                 s1;
-       s4                 disp;
-       ptrint             val;
-       ptrint             addr;
-       s4                 type;
-       java_objectheader *e;
+       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;
+       void           *p;
 
        _uc = (ucontext_t *) _p;
        _mc = &_uc->uc_mcontext;
@@ -84,11 +85,11 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
 
        mcode = *((u4 *) xpc);
 
-       d    = M_MEM_GET_A(mcode);
-       s1   = M_MEM_GET_B(mcode);
-       disp = M_MEM_GET_DISP(mcode);
+       d    = M_MEM_GET_Ra(mcode);
+       s1   = M_MEM_GET_Rb(mcode);
+       disp = M_MEM_GET_Memory_disp(mcode);
 
-       val   = _mc->sc_regs[d];
+       val  = _mc->sc_regs[d];
 
        /* check for special-load */
 
@@ -96,27 +97,91 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
                /* we use the exception type as load displacement */
 
                type = disp;
+
+               if (type == EXCEPTION_HARDWARE_COMPILER) {
+                       /* The XPC is the RA minus 1, 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 = _mc->sc_regs[s1];
-               type = (s4) addr;
+               type = (int) addr;
        }
 
-       /* generate appropriate exception */
+       /* Handle the type. */
+
+       p = signal_handle(type, val, pv, sp, ra, xpc, _p);
+
+       /* Set registers. */
+
+       switch (type) {
+       case EXCEPTION_HARDWARE_COMPILER:
+               if (p != NULL) {
+                       _mc->sc_regs[REG_PV] = (uintptr_t) p;
+                       _mc->sc_pc           = (uintptr_t) p;
+                       break;
+               }
+
+               /* Get and set the PV from the parent Java method. */
+
+               pv = md_codegen_get_pv_from_pc(ra);
+
+               _mc->sc_regs[REG_PV] = (uintptr_t) pv;
 
-       e = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
+               /* Get the exception object. */
 
-       /* set registers */
+               p = builtin_retrieve_exception();
 
-       _mc->sc_regs[REG_ITMP1_XPTR] = (ptrint) e;
-       _mc->sc_regs[REG_ITMP2_XPC]  = (ptrint) xpc;
-       _mc->sc_pc                   = (ptrint) asm_handle_exception;
+               assert(p != NULL);
+
+               /* fall-through */
+
+       case EXCEPTION_HARDWARE_PATCHER:
+               if (p == NULL)
+                       break;
+
+               /* fall-through */
+               
+       default:
+               _mc->sc_regs[REG_ITMP1_XPTR] = (uintptr_t) p;
+               _mc->sc_regs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
+               _mc->sc_pc                   = (uintptr_t) asm_handle_exception;
+       }
 }
 
 
+/* 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.
@@ -143,8 +208,87 @@ void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 #endif
 
 
+/* md_replace_executionstate_read **********************************************
+
+   Read the given context into an executionstate for Replacement.
+
+*******************************************************************************/
+
+#if defined(ENABLE_REPLACEMENT)
+void md_replace_executionstate_read(executionstate_t *es, void *context)
+{
+       ucontext_t *_uc;
+       mcontext_t *_mc;
+       s4          i;
+
+       _uc = (ucontext_t *) context;
+       _mc = &_uc->uc_mcontext;
+
+       /* 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];
+
+       /* read integer registers */
+       for (i = 0; i < INT_REG_CNT; i++)
+               es->intregs[i] = _mc->sc_regs[i];
+
+       /* 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));
+       memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
+}
+#endif
+
+
+/* md_replace_executionstate_write *********************************************
+
+   Write the given executionstate back to the context for Replacement.
+
+*******************************************************************************/
+
+#if defined(ENABLE_REPLACEMENT)
+void md_replace_executionstate_write(executionstate_t *es, void *context)
+{
+       ucontext_t *_uc;
+       mcontext_t *_mc;
+       s4          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));
+       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
+
+
+/* md_critical_section_restart *************************************************
+
+   Search the critical sections tree for a matching section and set
+   the PC to the restart point, if necessary.
+
+*******************************************************************************/
+
 #if defined(ENABLE_THREADS)
-void thread_restartcriticalsection(ucontext_t *_uc)
+void md_critical_section_restart(ucontext_t *_uc)
 {
        mcontext_t *_mc;
        u1         *pc;