⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpu-exec.c

📁 qemu性能直逼VMware的仿真器QEMU 的模擬速度約為實機的 25%;約為 Bochs 的 60 倍。Plex86、User-Mode-Linux、VMware 和 Virtual PC 則比
💻 C
📖 第 1 页 / 共 4 页
字号:
/* *  i386 emulator main execution loop *  *  Copyright (c) 2003-2005 Fabrice Bellard * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include "config.h"#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>#endifint tb_invalidated_flag;//#define DEBUG_EXEC//#define DEBUG_SIGNAL#if defined(TARGET_ARM) || defined(TARGET_SPARC) || defined(TARGET_M68K)/* XXX: unify with i386 target */void cpu_loop_exit(void){    longjmp(env->jmp_env, 1);}#endif#if !(defined(TARGET_SPARC) || defined(TARGET_SH4) || defined(TARGET_M68K))#define reg_T2#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);}static TranslationBlock *tb_find_slow(target_ulong pc,                                      target_ulong cs_base,                                      unsigned int flags){    TranslationBlock *tb, **ptb1;    int code_gen_size;    unsigned int h;    target_ulong phys_pc, phys_page1, phys_page2, virt_page2;    uint8_t *tc_ptr;        spin_lock(&tb_lock);    tb_invalidated_flag = 0;        regs_to_env(); /* XXX: do it just before cpu_gen_code() */        /* find translated block using physical mappings */    phys_pc = get_phys_addr_code(env, pc);    phys_page1 = phys_pc & TARGET_PAGE_MASK;    phys_page2 = -1;    h = tb_phys_hash_func(phys_pc);    ptb1 = &tb_phys_hash[h];    for(;;) {        tb = *ptb1;        if (!tb)            goto not_found;        if (tb->pc == pc &&             tb->page_addr[0] == phys_page1 &&            tb->cs_base == cs_base &&             tb->flags == flags) {            /* check next page if needed */            if (tb->page_addr[1] != -1) {                virt_page2 = (pc & TARGET_PAGE_MASK) +                     TARGET_PAGE_SIZE;                phys_page2 = get_phys_addr_code(env, virt_page2);                if (tb->page_addr[1] == phys_page2)                    goto found;            } else {                goto found;            }        }        ptb1 = &tb->phys_hash_next;    } not_found:    /* if no translated code available, then translate it now */    tb = tb_alloc(pc);    if (!tb) {        /* flush must be done */        tb_flush(env);        /* cannot fail at this point */        tb = tb_alloc(pc);        /* don't forget to invalidate previous TB info */        tb_invalidated_flag = 1;    }    tc_ptr = code_gen_ptr;    tb->tc_ptr = tc_ptr;    tb->cs_base = cs_base;    tb->flags = flags;    cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);    code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));        /* check next page if needed */    virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;    phys_page2 = -1;    if ((pc & TARGET_PAGE_MASK) != virt_page2) {        phys_page2 = get_phys_addr_code(env, virt_page2);    }    tb_link_phys(tb, phys_pc, phys_page2);     found:    /* we add the TB in the virtual pc hash table */    env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)] = tb;    spin_unlock(&tb_lock);    return tb;}static inline TranslationBlock *tb_find_fast(void){    TranslationBlock *tb;    target_ulong cs_base, pc;    unsigned int flags;    /* we record a subset of the CPU state. It will       always be the same before a given translated block       is executed. */#if defined(TARGET_I386)    flags = env->hflags;    flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));    cs_base = env->segs[R_CS].base;    pc = cs_base + env->eip;#elif defined(TARGET_ARM)    flags = env->thumb | (env->vfp.vec_len << 1)            | (env->vfp.vec_stride << 4);    if ((env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR)        flags |= (1 << 6);    if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30))        flags |= (1 << 7);    cs_base = 0;    pc = env->regs[15];#elif defined(TARGET_SPARC)#ifdef TARGET_SPARC64    // Combined FPU enable bits . PRIV . DMMU enabled . IMMU enabled    flags = (((env->pstate & PS_PEF) >> 1) | ((env->fprs & FPRS_FEF) << 2))        | (env->pstate & PS_PRIV) | ((env->lsu & (DMMU_E | IMMU_E)) >> 2);#else    // FPU enable . MMU enabled . MMU no-fault . Supervisor    flags = (env->psref << 3) | ((env->mmuregs[0] & (MMU_E | MMU_NF)) << 1)        | env->psrs;#endif    cs_base = env->npc;    pc = env->pc;#elif defined(TARGET_PPC)    flags = (msr_pr << MSR_PR) | (msr_fp << MSR_FP) |        (msr_se << MSR_SE) | (msr_le << MSR_LE);    cs_base = 0;    pc = env->nip;#elif defined(TARGET_MIPS)    flags = env->hflags & (MIPS_HFLAG_TMASK | MIPS_HFLAG_BMASK);    cs_base = 0;    pc = env->PC;#elif defined(TARGET_M68K)    flags = env->fpcr & M68K_FPCR_PREC;    cs_base = 0;    pc = env->pc;#elif defined(TARGET_SH4)    flags = env->sr & (SR_MD | SR_RB);    cs_base = 0;         /* XXXXX */    pc = env->pc;#else#error unsupported CPU#endif    tb = env->tb_jmp_cache[tb_jmp_cache_hash_func(pc)];    if (__builtin_expect(!tb || tb->pc != pc || tb->cs_base != cs_base ||                         tb->flags != flags, 0)) {        tb = tb_find_slow(pc, cs_base, flags);        /* Note: we do it here to avoid a gcc bug on Mac OS X when           doing it in tb_find_slow */        if (tb_invalidated_flag) {            /* as some TB could have been invalidated because               of memory exceptions while generating the code, we               must recompute the hash index here */            T0 = 0;        }    }    return tb;}/* main execution loop */int cpu_exec(CPUState *env1){#define DECLARE_HOST_REGS 1#include "hostregs_helper.h"#if defined(TARGET_SPARC)#if defined(reg_REGWPTR)    uint32_t *saved_regwptr;#endif#endif#if defined(__sparc__) && !defined(HOST_SOLARIS)    int saved_i7;    target_ulong tmp_T0;#endif    int ret, interrupt_request;    void (*gen_func)(void);    TranslationBlock *tb;    uint8_t *tc_ptr;#if defined(TARGET_I386)    /* handle exit of HALTED state */    if (env1->hflags & HF_HALTED_MASK) {        /* disable halt condition */        if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&            (env1->eflags & IF_MASK)) {            env1->hflags &= ~HF_HALTED_MASK;        } else {            return EXCP_HALTED;        }    }#elif defined(TARGET_PPC)    if (env1->halted) {        if (env1->msr[MSR_EE] &&             (env1->interrupt_request &              (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER))) {            env1->halted = 0;        } else {            return EXCP_HALTED;        }    }#elif defined(TARGET_SPARC)    if (env1->halted) {        if ((env1->interrupt_request & CPU_INTERRUPT_HARD) &&            (env1->psret != 0)) {            env1->halted = 0;        } else {            return EXCP_HALTED;        }    }#elif defined(TARGET_ARM)    if (env1->halted) {        /* An interrupt wakes the CPU even if the I and F CPSR bits are           set.  */        if (env1->interrupt_request            & (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD)) {            env1->halted = 0;        } else {            return EXCP_HALTED;        }    }#elif defined(TARGET_MIPS)    if (env1->halted) {        if (env1->interrupt_request &            (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER)) {            env1->halted = 0;        } else {            return EXCP_HALTED;        }    }#endif    cpu_single_env = env1;     /* first we save global registers */#define SAVE_HOST_REGS 1#include "hostregs_helper.h"    env = env1;#if defined(__sparc__) && !defined(HOST_SOLARIS)    /* we also save i7 because longjmp may not restore it */    asm volatile ("mov %%i7, %0" : "=r" (saved_i7));#endif#if defined(TARGET_I386)    env_to_regs();    /* put eflags in CPU temporary format */    CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);    DF = 1 - (2 * ((env->eflags >> 10) & 1));    CC_OP = CC_OP_EFLAGS;    env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);#elif defined(TARGET_ARM)#elif defined(TARGET_SPARC)#if defined(reg_REGWPTR)    saved_regwptr = REGWPTR;#endif#elif defined(TARGET_PPC)#elif defined(TARGET_M68K)    env->cc_op = CC_OP_FLAGS;    env->cc_dest = env->sr & 0xf;    env->cc_x = (env->sr >> 4) & 1;#elif defined(TARGET_MIPS)#elif defined(TARGET_SH4)    /* XXXXX */#else#error unsupported target CPU#endif    env->exception_index = -1;    /* prepare setjmp context for exception handling */    for(;;) {        if (setjmp(env->jmp_env) == 0) {            env->current_tb = NULL;            /* if an exception is pending, we execute it here */            if (env->exception_index >= 0) {                if (env->exception_index >= EXCP_INTERRUPT) {                    /* exit request from the cpu execution loop */                    ret = env->exception_index;                    break;                } else if (env->user_mode_only) {                    /* if user mode only, we simulate a fake exception                       which will be handled outside the cpu execution                       loop */#if defined(TARGET_I386)                    do_interrupt_user(env->exception_index,                                       env->exception_is_int,                                       env->error_code,                                       env->exception_next_eip);#endif                    ret = env->exception_index;                    break;                } else {#if defined(TARGET_I386)                    /* simulate a real cpu exception. On i386, it can                       trigger new exceptions, but we do not handle                       double or triple faults yet. */                    do_interrupt(env->exception_index,                                  env->exception_is_int,                                  env->error_code,                                  env->exception_next_eip, 0);#elif defined(TARGET_PPC)                    do_interrupt(env);#elif defined(TARGET_MIPS)                    do_interrupt(env);#elif defined(TARGET_SPARC)                    do_interrupt(env->exception_index);#elif defined(TARGET_ARM)                    do_interrupt(env);#elif defined(TARGET_SH4)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -