📄 cpu-exec.c
字号:
/* * 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)/* XXX: unify with i386 target */void cpu_loop_exit(void){ longjmp(env->jmp_env, 1);}#endif#ifndef TARGET_SPARC#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 flags = (env->pstate << 2) | ((env->lsu & (DMMU_E | IMMU_E)) >> 2);#else flags = env->psrs | ((env->mmuregs[0] & (MMU_E | MMU_NF)) << 1);#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_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){ int saved_T0, saved_T1;#if defined(reg_T2) int saved_T2;#endif CPUState *saved_env;#if defined(TARGET_I386)#ifdef reg_EAX int saved_EAX;#endif#ifdef reg_ECX int saved_ECX;#endif#ifdef reg_EDX int saved_EDX;#endif#ifdef reg_EBX int saved_EBX;#endif#ifdef reg_ESP int saved_ESP;#endif#ifdef reg_EBP int saved_EBP;#endif#ifdef reg_ESI int saved_ESI;#endif#ifdef reg_EDI int saved_EDI;#endif#elif defined(TARGET_SPARC)#if defined(reg_REGWPTR) uint32_t *saved_regwptr;#endif#endif#ifdef __sparc__ int saved_i7, 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 */ saved_env = env; env = env1; saved_T0 = T0; saved_T1 = T1;#if defined(reg_T2) saved_T2 = T2;#endif#ifdef __sparc__ /* we also save i7 because longjmp may not restore it */ asm volatile ("mov %%i7, %0" : "=r" (saved_i7));#endif#if defined(TARGET_I386)#ifdef reg_EAX saved_EAX = EAX;#endif#ifdef reg_ECX saved_ECX = ECX;#endif#ifdef reg_EDX saved_EDX = EDX;#endif#ifdef reg_EBX saved_EBX = EBX;#endif#ifdef reg_ESP saved_ESP = ESP;#endif#ifdef reg_EBP saved_EBP = EBP;#endif#ifdef reg_ESI saved_ESI = ESI;#endif#ifdef reg_EDI saved_EDI = EDI;#endif 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_MIPS)#elif defined(TARGET_SH4) /* XXXXX */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -