]> git.proxmox.com Git - qemu.git/blobdiff - cpu-exec.c
added qemu_strdup()
[qemu.git] / cpu-exec.c
index 44a0f73a58c56f81f64c3783b0be2a8d143467e1..053c0cc5dd0fdc27f6f1c70313e8dedd05de8a8c 100644 (file)
 #include "exec.h"
 #include "disas.h"
 
+#if !defined(CONFIG_SOFTMMU)
+#undef EAX
+#undef ECX
+#undef EDX
+#undef EBX
+#undef ESP
+#undef EBP
+#undef ESI
+#undef EDI
+#undef EIP
+#include <signal.h>
+#include <sys/ucontext.h>
+#endif
+
 int tb_invalidated_flag;
 
 //#define DEBUG_EXEC
@@ -34,6 +48,28 @@ void cpu_loop_exit(void)
 }
 #endif
 
+/* exit the current TB from a signal handler. The host registers are
+   restored in a state compatible with the CPU emulator
+ */
+void cpu_resume_from_signal(CPUState *env1, void *puc) 
+{
+#if !defined(CONFIG_SOFTMMU)
+    struct ucontext *uc = puc;
+#endif
+
+    env = env1;
+
+    /* XXX: restore cpu registers saved in host registers */
+
+#if !defined(CONFIG_SOFTMMU)
+    if (puc) {
+        /* XXX: use siglongjmp ? */
+        sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
+    }
+#endif
+    longjmp(env->jmp_env, 1);
+}
+
 /* main execution loop */
 
 int cpu_exec(CPUState *env1)
@@ -190,12 +226,12 @@ int cpu_exec(CPUState *env1)
                         (env->eflags & IF_MASK) && 
                         !(env->hflags & HF_INHIBIT_IRQ_MASK)) {
                         int intno;
-                        intno = cpu_x86_get_pic_interrupt(env);
-                        if (loglevel) {
+                        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
+                        intno = cpu_get_pic_interrupt(env);
+                        if (loglevel & CPU_LOG_TB_IN_ASM) {
                             fprintf(logfile, "Servicing hardware INT=0x%02x\n", intno);
                         }
                         do_interrupt(intno, 0, 0, 0, 1);
-                        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
                         /* ensure that no TB jump will be modified as
                            the program flow was changed */
 #ifdef __sparc__
@@ -205,11 +241,25 @@ int cpu_exec(CPUState *env1)
 #endif
                     }
 #elif defined(TARGET_PPC)
+#if 0
+                    if ((interrupt_request & CPU_INTERRUPT_RESET)) {
+                        cpu_ppc_reset(env);
+                    }
+#endif
+                    if (msr_ee != 0) {
                     if ((interrupt_request & CPU_INTERRUPT_HARD)) {
-                        do_queue_exception(EXCP_EXTERNAL);
-                        if (check_exception_state(env))
+                           /* Raise it */
+                           env->exception_index = EXCP_EXTERNAL;
+                           env->error_code = 0;
                             do_interrupt(env);
                         env->interrupt_request &= ~CPU_INTERRUPT_HARD;
+                       } else if ((interrupt_request & CPU_INTERRUPT_TIMER)) {
+                           /* Raise it */
+                           env->exception_index = EXCP_DECR;
+                           env->error_code = 0;
+                           do_interrupt(env);
+                            env->interrupt_request &= ~CPU_INTERRUPT_TIMER;
+                       }
                     }
 #endif
                     if (interrupt_request & CPU_INTERRUPT_EXITTB) {
@@ -229,7 +279,7 @@ int cpu_exec(CPUState *env1)
                     }
                 }
 #ifdef DEBUG_EXEC
-                if (loglevel) {
+                if (loglevel & CPU_LOG_EXEC) {
 #if defined(TARGET_I386)
                     /* restore flags in standard format */
                     env->regs[R_EAX] = EAX;
@@ -362,7 +412,7 @@ int cpu_exec(CPUState *env1)
                     spin_unlock(&tb_lock);
                 }
 #ifdef DEBUG_EXEC
-                if (loglevel) {
+                if (loglevel & CPU_LOG_EXEC) {
                     fprintf(logfile, "Trace 0x%08lx [0x%08lx] %s\n",
                             (long)tb->tc_ptr, (long)tb->pc,
                             lookup_symbol((void *)tb->pc));
@@ -380,6 +430,11 @@ int cpu_exec(CPUState *env1)
                     ) {
                     spin_lock(&tb_lock);
                     tb_add_jump((TranslationBlock *)(T0 & ~3), T0 & 3, tb);
+#if defined(USE_CODE_COPY)
+                    /* propagates the FP use info */
+                    ((TranslationBlock *)(T0 & ~3))->cflags |= 
+                        (tb->cflags & CF_FP_USED);
+#endif
                     spin_unlock(&tb_lock);
                 }
                 tc_ptr = tb->tc_ptr;
@@ -402,8 +457,14 @@ int cpu_exec(CPUState *env1)
 #elif defined(TARGET_I386) && defined(USE_CODE_COPY)
 {
     if (!(tb->cflags & CF_CODE_COPY)) {
+        if ((tb->cflags & CF_FP_USED) && env->native_fp_regs) {
+            save_native_fp_state(env);
+        }
         gen_func();
     } else {
+        if ((tb->cflags & CF_FP_USED) && !env->native_fp_regs) {
+            restore_native_fp_state(env);
+        }
         /* we work with native eflags */
         CC_SRC = cc_table[CC_OP].compute_all();
         CC_OP = CC_OP_EFLAGS;
@@ -487,6 +548,11 @@ int cpu_exec(CPUState *env1)
 
 
 #if defined(TARGET_I386)
+#if defined(USE_CODE_COPY)
+    if (env->native_fp_regs) {
+        save_native_fp_state(env);
+    }
+#endif
     /* restore flags in standard format */
     env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
 
@@ -532,6 +598,19 @@ int cpu_exec(CPUState *env1)
     return ret;
 }
 
+/* must only be called from the generated code as an exception can be
+   generated */
+void tb_invalidate_page_range(target_ulong start, target_ulong end)
+{
+    /* XXX: cannot enable it yet because it yields to MMU exception
+       where NIP != read address on PowerPC */
+#if 0
+    target_ulong phys_addr;
+    phys_addr = get_phys_addr_code(env, start);
+    tb_invalidate_phys_page_range(phys_addr, phys_addr + end - start, 0);
+#endif
+}
+
 #if defined(TARGET_I386) && defined(CONFIG_USER_ONLY)
 
 void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
@@ -576,17 +655,7 @@ void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
 
 #endif /* TARGET_I386 */
 
-#undef EAX
-#undef ECX
-#undef EDX
-#undef EBX
-#undef ESP
-#undef EBP
-#undef ESI
-#undef EDI
-#undef EIP
-#include <signal.h>
-#include <sys/ucontext.h>
+#if !defined(CONFIG_SOFTMMU)
 
 #if defined(TARGET_I386)
 
@@ -608,9 +677,10 @@ static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
                 pc, address, is_write, *(unsigned long *)old_set);
 #endif
     /* XXX: locking issue */
-    if (is_write && page_unprotect(address)) {
+    if (is_write && page_unprotect(address, pc, puc)) {
         return 1;
     }
+
     /* see if it is an MMU fault */
     ret = cpu_x86_handle_mmu_fault(env, address, is_write, 
                                    ((env->hflags & HF_CPL_MASK) == 3), 0);
@@ -637,8 +707,7 @@ static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
     } else {
         /* activate soft MMU for this block */
         env->hflags |= HF_SOFTMMU_MASK;
-        sigprocmask(SIG_SETMASK, old_set, NULL);
-        cpu_loop_exit();
+        cpu_resume_from_signal(env, puc);
     }
     /* never comes here */
     return 1;
@@ -658,7 +727,7 @@ static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
                                     void *puc)
 {
     /* XXX: locking issue */
-    if (is_write && page_unprotect(address)) {
+    if (is_write && page_unprotect(address, pc, puc)) {
         return 1;
     }
     return 0;
@@ -680,7 +749,7 @@ static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
            pc, address, is_write, *(unsigned long *)old_set);
 #endif
     /* XXX: locking issue */
-    if (is_write && page_unprotect(address)) {
+    if (is_write && page_unprotect(address, pc, puc)) {
         return 1;
     }
 
@@ -706,11 +775,10 @@ static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
     /* we restore the process signal mask as the sigreturn should
        do it (XXX: use sigsetjmp) */
         sigprocmask(SIG_SETMASK, old_set, NULL);
-        do_queue_exception_err(env->exception_index, env->error_code);
+        do_raise_exception_err(env->exception_index, env->error_code);
     } else {
         /* activate soft MMU for this block */
-        sigprocmask(SIG_SETMASK, old_set, NULL);
-        cpu_loop_exit();
+        cpu_resume_from_signal(env, puc);
     }
     /* never comes here */
     return 1;
@@ -747,7 +815,7 @@ int cpu_signal_handler(int host_signum, struct siginfo *info,
     struct ucontext *uc = puc;
     unsigned long pc;
     int trapno;
-    
+
 #ifndef REG_EIP
 /* for glibc 2.1 */
 #define REG_EIP    EIP
@@ -769,24 +837,87 @@ int cpu_signal_handler(int host_signum, struct siginfo *info,
                                  &uc->uc_sigmask, puc);
 }
 
-#elif defined(__powerpc)
+#elif defined(__x86_64__)
+
+int cpu_signal_handler(int host_signum, struct siginfo *info,
+                       void *puc)
+{
+    struct ucontext *uc = puc;
+    unsigned long pc;
+
+    pc = uc->uc_mcontext.gregs[REG_RIP];
+    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
+                             uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ? 
+                             (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
+                             &uc->uc_sigmask, puc);
+}
+
+#elif defined(__powerpc__)
+
+/***********************************************************************
+ * signal context platform-specific definitions
+ * From Wine
+ */
+#ifdef linux
+/* All Registers access - only for local access */
+# define REG_sig(reg_name, context)            ((context)->uc_mcontext.regs->reg_name)
+/* Gpr Registers access  */
+# define GPR_sig(reg_num, context)             REG_sig(gpr[reg_num], context)
+# define IAR_sig(context)                      REG_sig(nip, context)   /* Program counter */
+# define MSR_sig(context)                      REG_sig(msr, context)   /* Machine State Register (Supervisor) */
+# define CTR_sig(context)                      REG_sig(ctr, context)   /* Count register */
+# define XER_sig(context)                      REG_sig(xer, context) /* User's integer exception register */
+# define LR_sig(context)                       REG_sig(link, context) /* Link register */
+# define CR_sig(context)                       REG_sig(ccr, context) /* Condition register */
+/* Float Registers access  */
+# define FLOAT_sig(reg_num, context)           (((double*)((char*)((context)->uc_mcontext.regs+48*4)))[reg_num])
+# define FPSCR_sig(context)                    (*(int*)((char*)((context)->uc_mcontext.regs+(48+32*2)*4)))
+/* Exception Registers access */
+# define DAR_sig(context)                      REG_sig(dar, context)
+# define DSISR_sig(context)                    REG_sig(dsisr, context)
+# define TRAP_sig(context)                     REG_sig(trap, context)
+#endif /* linux */
+
+#ifdef __APPLE__
+# include <sys/ucontext.h>
+typedef struct ucontext SIGCONTEXT;
+/* All Registers access - only for local access */
+# define REG_sig(reg_name, context)            ((context)->uc_mcontext->ss.reg_name)
+# define FLOATREG_sig(reg_name, context)       ((context)->uc_mcontext->fs.reg_name)
+# define EXCEPREG_sig(reg_name, context)       ((context)->uc_mcontext->es.reg_name)
+# define VECREG_sig(reg_name, context)         ((context)->uc_mcontext->vs.reg_name)
+/* Gpr Registers access */
+# define GPR_sig(reg_num, context)             REG_sig(r##reg_num, context)
+# define IAR_sig(context)                      REG_sig(srr0, context)  /* Program counter */
+# define MSR_sig(context)                      REG_sig(srr1, context)  /* Machine State Register (Supervisor) */
+# define CTR_sig(context)                      REG_sig(ctr, context)
+# define XER_sig(context)                      REG_sig(xer, context) /* Link register */
+# define LR_sig(context)                       REG_sig(lr, context)  /* User's integer exception register */
+# define CR_sig(context)                       REG_sig(cr, context)  /* Condition register */
+/* Float Registers access */
+# define FLOAT_sig(reg_num, context)           FLOATREG_sig(fpregs[reg_num], context)
+# define FPSCR_sig(context)                    ((double)FLOATREG_sig(fpscr, context))
+/* Exception Registers access */
+# define DAR_sig(context)                      EXCEPREG_sig(dar, context)     /* Fault registers for coredump */
+# define DSISR_sig(context)                    EXCEPREG_sig(dsisr, context)
+# define TRAP_sig(context)                     EXCEPREG_sig(exception, context) /* number of powerpc exception taken */
+#endif /* __APPLE__ */
 
 int cpu_signal_handler(int host_signum, struct siginfo *info, 
                        void *puc)
 {
     struct ucontext *uc = puc;
-    struct pt_regs *regs = uc->uc_mcontext.regs;
     unsigned long pc;
     int is_write;
 
-    pc = regs->nip;
+    pc = IAR_sig(uc);
     is_write = 0;
 #if 0
     /* ppc 4xx case */
-    if (regs->dsisr & 0x00800000)
+    if (DSISR_sig(uc) & 0x00800000)
         is_write = 1;
 #else
-    if (regs->trap != 0x400 && (regs->dsisr & 0x02000000))
+    if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000))
         is_write = 1;
 #endif
     return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
@@ -894,3 +1025,5 @@ int cpu_signal_handler(int host_signum, struct siginfo *info,
 #error host CPU specific signal handler needed
 
 #endif
+
+#endif /* !defined(CONFIG_SOFTMMU) */