From: Zhang Wei Date: Fri, 25 Jan 2008 07:12:59 +0000 Subject: [kvm-ppc-devel] [PATCH 5/5] Extract instruction emulate codes for Message-Id: <12012451822275-git-send-email-wei.zhang@freescale.com> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: kvm-ppc@vger.kernel.org Some instruction emulation need implemented for individual processor. I extract general code from emulate.c to header file inst.h. A new header 44x_inst.h is created for special 44x emulation functions. Signed-off-by: Zhang Wei --- arch/powerpc/kvm/44x_inst.h | 192 +++++++++++++++++++++++++++++++++ arch/powerpc/kvm/emulate.c | 245 +------------------------------------------ arch/powerpc/kvm/inst.h | 132 +++++++++++++++++++++++ 3 files changed, 329 insertions(+), 240 deletions(-) create mode 100644 arch/powerpc/kvm/44x_inst.h create mode 100644 arch/powerpc/kvm/inst.h diff --git a/arch/powerpc/kvm/44x_inst.h b/arch/powerpc/kvm/44x_inst.h new file mode 100644 index 0000000..7529618 --- /dev/null +++ b/arch/powerpc/kvm/44x_inst.h @@ -0,0 +1,192 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Copyright IBM Corp. 2007 + * + * Authors: Hollis Blanchard + * + * Functions of the file are extrcated from emulate.c. + * The file only includes special implementation for PowerPC 44x. + * + * Copyright (C) 2008 Freescale Semiconductor. + * + * Zhang Wei + */ + +#ifndef __ARCH_POWERPC_KVM_44X_INST_H__ +#define __ARCH_POWERPC_KVM_44X_INST_H__ + +#define DCRN_CPR0_CFGADDR 0xc +#define DCRN_CPR0_CFGDATA 0xd + +static int emul_tlbwe(struct kvm_vcpu *vcpu, u32 inst) +{ + u64 eaddr; + u64 raddr; + u64 asid; + u32 flags; + struct tlbe *tlbe; + unsigned int ra; + unsigned int rs; + unsigned int ws; + unsigned int index; + + ra = get_ra(inst); + rs = get_rs(inst); + ws = get_ws(inst); + + index = vcpu->arch.gpr[ra]; + if (index > PPC44x_TLB_SIZE) { + printk("%s: index %d\n", __func__, index); + kvmppc_dump_vcpu(vcpu); + return EMULATE_FAIL; + } + + tlbe = &vcpu->arch.guest_tlb[index]; + + /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */ + if (tlbe->word0 & PPC44x_TLB_VALID) { + eaddr = get_tlb_eaddr(tlbe); + asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid; + kvmppc_mmu_invalidate(vcpu, eaddr, asid); + } + + switch (ws) { + case PPC44x_TLB_PAGEID: + tlbe->tid = vcpu->arch.mmucr & 0xff; + tlbe->word0 = vcpu->arch.gpr[rs]; + break; + + case PPC44x_TLB_XLAT: + tlbe->word1 = vcpu->arch.gpr[rs]; + break; + + case PPC44x_TLB_ATTRIB: + tlbe->word2 = vcpu->arch.gpr[rs]; + break; + + default: + return EMULATE_FAIL; + } + +#if 0 + printk("gtlbe %02d: %08x %08x %08x %08x\n", index, + tlbe->tid, tlbe->word0, tlbe->word1, tlbe->word2); +#endif + + if (tlbe_is_host_safe(vcpu, tlbe)) { + eaddr = get_tlb_eaddr(tlbe); + raddr = get_tlb_raddr(tlbe); + asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid; + flags = tlbe->word2 & 0xffff; + + /* Create a 4KB mapping on the host. If the guest wanted a + * large page, only the first 4KB is mapped here and the rest + * are mapped on the fly. */ + kvmppc_mmu_map(vcpu, eaddr, raddr >> PAGE_SHIFT, asid, flags); + } + + return EMULATE_DONE; +} + +static int emul_mtdcr(struct kvm_vcpu *vcpu, u32 inst) +{ + struct kvm_run *run = vcpu->run; + int dcrn; + int rs; + + dcrn = get_dcrn(inst); + rs = get_rs(inst); + + /* emulate some access in kernel */ + switch (dcrn) { + case DCRN_CPR0_CFGADDR: + vcpu->arch.cpr0_cfgaddr = vcpu->arch.gpr[rs]; + break; + default: + run->dcr.dcrn = dcrn; + run->dcr.data = vcpu->arch.gpr[rs]; + run->dcr.is_write = 1; + vcpu->arch.dcr_needed = 1; + return EMULATE_DO_DCR; + } + + return EMULATE_DONE; +} + +static int emul_mfdcr(struct kvm_vcpu *vcpu, u32 inst) +{ + struct kvm_run *run = vcpu->run; + int dcrn; + int rt; + + dcrn = get_dcrn(inst); + rt = get_rt(inst); + + /* emulate some access in kernel */ + switch (dcrn) { + case DCRN_CPR0_CFGADDR: + vcpu->arch.gpr[rt] = vcpu->arch.cpr0_cfgaddr; + break; + case DCRN_CPR0_CFGDATA: + local_irq_disable(); + mtdcr(DCRN_CPR0_CFGADDR, + vcpu->arch.cpr0_cfgaddr); + vcpu->arch.gpr[rt] = mfdcr(DCRN_CPR0_CFGDATA); + local_irq_enable(); + break; + default: + run->dcr.dcrn = dcrn; + run->dcr.data = 0; + run->dcr.is_write = 0; + vcpu->arch.io_gpr = rt; + vcpu->arch.dcr_needed = 1; + return EMULATE_DO_DCR; + } + + return EMULATE_DONE; +} + +static int emul_tlbsx(struct kvm_vcpu *vcpu, u32 inst) +{ + int index; + unsigned int as = get_mmucr_sts(vcpu); + unsigned int pid = get_mmucr_stid(vcpu); + unsigned int ra; + unsigned int rb; + unsigned int rc; + unsigned int rt; + unsigned int ea; + + rt = get_rt(inst); + ra = get_ra(inst); + rb = get_rb(inst); + rc = get_rc(inst); + + ea = vcpu->arch.gpr[rb]; + if (ra) + ea += vcpu->arch.gpr[ra]; + + index = kvmppc_tlb_index(vcpu, ea, pid, as); + if (rc) { + if (index < 0) + vcpu->arch.cr &= ~0x20000000; + else + vcpu->arch.cr |= 0x20000000; + } + vcpu->arch.gpr[rt] = index; + + return EMULATE_DONE; +} +#endif /* __ARCH_POWERPC_KVM_44X_INST_H__ */ diff --git a/arch/powerpc/kvm/emulate.c b/arch/powerpc/kvm/emulate.c index ad4a26a..8eb2f31 100644 --- a/arch/powerpc/kvm/emulate.c +++ b/arch/powerpc/kvm/emulate.c @@ -28,156 +28,7 @@ #include #include "tlb.h" - -#define DCRN_CPR0_CFGADDR 0xc -#define DCRN_CPR0_CFGDATA 0xd - -/* Instruction decoding */ -static inline unsigned int get_op(u32 inst) -{ - return inst >> 26; -} - -static inline unsigned int get_xop(u32 inst) -{ - return (inst >> 1) & 0x3ff; -} - -static inline unsigned int get_sprn(u32 inst) -{ - return ((inst >> 16) & 0x1f) | ((inst >> 6) & 0x3e0); -} - -static inline unsigned int get_dcrn(u32 inst) -{ - return ((inst >> 16) & 0x1f) | ((inst >> 6) & 0x3e0); -} - -static inline unsigned int get_rt(u32 inst) -{ - return (inst >> 21) & 0x1f; -} - -static inline unsigned int get_rs(u32 inst) -{ - return (inst >> 21) & 0x1f; -} - -static inline unsigned int get_ra(u32 inst) -{ - return (inst >> 16) & 0x1f; -} - -static inline unsigned int get_rb(u32 inst) -{ - return (inst >> 11) & 0x1f; -} - -static inline unsigned int get_rc(u32 inst) -{ - return inst & 0x1; -} - -static inline unsigned int get_ws(u32 inst) -{ - return (inst >> 11) & 0x1f; -} - -static inline unsigned int get_d(u32 inst) -{ - return inst & 0xffff; -} - -static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu, - const struct tlbe *tlbe) -{ - gpa_t gpa; - - if (!get_tlb_v(tlbe)) - return 0; - - /* Does it match current guest AS? */ - /* XXX what about IS != DS? */ - if (get_tlb_ts(tlbe) != !!(vcpu->arch.guest_msr & MSR_IS)) - return 0; - - gpa = get_tlb_raddr(tlbe); - if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT)) - /* Mapping is not for RAM. */ - return 0; - - return 1; -} - -static int emul_tlbwe(struct kvm_vcpu *vcpu, u32 inst) -{ - u64 eaddr; - u64 raddr; - u64 asid; - u32 flags; - struct tlbe *tlbe; - unsigned int ra; - unsigned int rs; - unsigned int ws; - unsigned int index; - - ra = get_ra(inst); - rs = get_rs(inst); - ws = get_ws(inst); - - index = vcpu->arch.gpr[ra]; - if (index > PPC44x_TLB_SIZE) { - printk("%s: index %d\n", __func__, index); - kvmppc_dump_vcpu(vcpu); - return EMULATE_FAIL; - } - - tlbe = &vcpu->arch.guest_tlb[index]; - - /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */ - if (tlbe->word0 & PPC44x_TLB_VALID) { - eaddr = get_tlb_eaddr(tlbe); - asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid; - kvmppc_mmu_invalidate(vcpu, eaddr, asid); - } - - switch (ws) { - case PPC44x_TLB_PAGEID: - tlbe->tid = vcpu->arch.mmucr & 0xff; - tlbe->word0 = vcpu->arch.gpr[rs]; - break; - - case PPC44x_TLB_XLAT: - tlbe->word1 = vcpu->arch.gpr[rs]; - break; - - case PPC44x_TLB_ATTRIB: - tlbe->word2 = vcpu->arch.gpr[rs]; - break; - - default: - return EMULATE_FAIL; - } - -#if 0 - printk("gtlbe %02d: %08x %08x %08x %08x\n", index, - tlbe->tid, tlbe->word0, tlbe->word1, tlbe->word2); -#endif - - if (tlbe_is_host_safe(vcpu, tlbe)) { - eaddr = get_tlb_eaddr(tlbe); - raddr = get_tlb_raddr(tlbe); - asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid; - flags = tlbe->word2 & 0xffff; - - /* Create a 4KB mapping on the host. If the guest wanted a - * large page, only the first 4KB is mapped here and the rest - * are mapped on the fly. */ - kvmppc_mmu_map(vcpu, eaddr, raddr >> PAGE_SHIFT, asid, flags); - } - - return EMULATE_DONE; -} +#include "inst.h" static void kvm_emulate_dec(struct kvm_vcpu *vcpu) { @@ -198,36 +49,12 @@ static void kvm_emulate_dec(struct kvm_vcpu *vcpu) } } -static void emul_rfi(struct kvm_vcpu *vcpu) -{ - if ((vcpu->arch.srr1 & MSR_PR) != (vcpu->arch.guest_msr & MSR_PR)) { - /* Switch MMU between kernel and user modes. */ - kvmppc_mmu_priv_switch(vcpu, vcpu->arch.srr1 & MSR_PR); - } - - vcpu->arch.pc = vcpu->arch.srr0; - vcpu->arch.guest_msr = vcpu->arch.srr1; - - vcpu->arch.shadow_msr |= vcpu->arch.guest_msr & GUEST_MSR_MASK; - vcpu->arch.shadow_msr &= ~(vcpu->arch.guest_msr & GUEST_MSR_MASK); - -#if 0 - printk("rfi: pc %x msr %x (%x)\n", vcpu->arch.pc, - vcpu->arch.guest_msr, vcpu->arch.shadow_msr); -#endif -} - int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) { u32 inst = vcpu->arch.last_inst; - u32 ea; - int ra; - int rb; - int rc; int rs; int rt; int sprn; - int dcrn; enum emulation_result emulated = EMULATE_DONE; int advance = 1; @@ -294,32 +121,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) break; case 323: /* mfdcr */ - dcrn = get_dcrn(inst); - rt = get_rt(inst); - - /* emulate some access in kernel */ - switch (dcrn) { - case DCRN_CPR0_CFGADDR: - vcpu->arch.gpr[rt] = vcpu->arch.cpr0_cfgaddr; - emulated = EMULATE_DONE; - break; - case DCRN_CPR0_CFGDATA: - local_irq_disable(); - mtdcr(DCRN_CPR0_CFGADDR, - vcpu->arch.cpr0_cfgaddr); - vcpu->arch.gpr[rt] = mfdcr(DCRN_CPR0_CFGDATA); - local_irq_enable(); - emulated = EMULATE_DONE; - break; - default: - run->dcr.dcrn = dcrn; - run->dcr.data = 0; - run->dcr.is_write = 0; - vcpu->arch.io_gpr = rt; - vcpu->arch.dcr_needed = 1; - emulated = EMULATE_DO_DCR; - } - + emulated = emul_mfdcr(vcpu, inst); break; case 339: /* mfspr */ @@ -416,22 +218,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) break; case 451: /* mtdcr */ - dcrn = get_dcrn(inst); - rs = get_rs(inst); - - /* emulate some access in kernel */ - switch (dcrn) { - case DCRN_CPR0_CFGADDR: - vcpu->arch.cpr0_cfgaddr = vcpu->arch.gpr[rs]; - break; - default: - run->dcr.dcrn = dcrn; - run->dcr.data = vcpu->arch.gpr[rs]; - run->dcr.is_write = 1; - vcpu->arch.dcr_needed = 1; - emulated = EMULATE_DO_DCR; - } - + emulated = emul_mtdcr(vcpu, inst); break; case 467: /* mtspr */ @@ -547,30 +334,8 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) emulated = emul_tlbwe(vcpu, inst); break; - case 914: { /* tlbsx */ - int index; - unsigned int as = get_mmucr_sts(vcpu); - unsigned int pid = get_mmucr_stid(vcpu); - - rt = get_rt(inst); - ra = get_ra(inst); - rb = get_rb(inst); - rc = get_rc(inst); - - ea = vcpu->arch.gpr[rb]; - if (ra) - ea += vcpu->arch.gpr[ra]; - - index = kvmppc_tlb_index(vcpu, ea, pid, as); - if (rc) { - if (index < 0) - vcpu->arch.cr &= ~0x20000000; - else - vcpu->arch.cr |= 0x20000000; - } - vcpu->arch.gpr[rt] = index; - - } + case 914: /* tlbsx */ + emulated = emul_tlbsx(vcpu, inst); break; case 966: /* iccci */ diff --git a/arch/powerpc/kvm/inst.h b/arch/powerpc/kvm/inst.h new file mode 100644 index 0000000..4bcd533 --- /dev/null +++ b/arch/powerpc/kvm/inst.h @@ -0,0 +1,132 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Copyright IBM Corp. 2007 + * + * Authors: Hollis Blanchard + * + * Functions of the file are extrcated from emulate.c. + * The file includes some generial instruction implementation + * and decoding for PowerPC. + * + * Copyright (C) 2008 Freescale Semiconductor. + * + * Zhang Wei + */ + +#ifndef __ARCH_POWERPC_KVM_INST_H__ +#define __ARCH_POWERPC_KVM_INST_H__ + +/* Instruction decoding */ +static inline unsigned int get_op(u32 inst) +{ + return inst >> 26; +} + +static inline unsigned int get_xop(u32 inst) +{ + return (inst >> 1) & 0x3ff; +} + +static inline unsigned int get_sprn(u32 inst) +{ + return ((inst >> 16) & 0x1f) | ((inst >> 6) & 0x3e0); +} + +static inline unsigned int get_dcrn(u32 inst) +{ + return ((inst >> 16) & 0x1f) | ((inst >> 6) & 0x3e0); +} + +static inline unsigned int get_rt(u32 inst) +{ + return (inst >> 21) & 0x1f; +} + +static inline unsigned int get_rs(u32 inst) +{ + return (inst >> 21) & 0x1f; +} + +static inline unsigned int get_ra(u32 inst) +{ + return (inst >> 16) & 0x1f; +} + +static inline unsigned int get_rb(u32 inst) +{ + return (inst >> 11) & 0x1f; +} + +static inline unsigned int get_rc(u32 inst) +{ + return inst & 0x1; +} + +static inline unsigned int get_ws(u32 inst) +{ + return (inst >> 11) & 0x1f; +} + +static inline unsigned int get_d(u32 inst) +{ + return inst & 0xffff; +} + +static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu, + const struct tlbe *tlbe) +{ + gpa_t gpa; + + if (!get_tlb_v(tlbe)) + return 0; + + /* Does it match current guest AS? */ + /* XXX what about IS != DS? */ + if (get_tlb_ts(tlbe) != !!(vcpu->arch.guest_msr & MSR_IS)) + return 0; + + gpa = get_tlb_raddr(tlbe); + if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT)) + /* Mapping is not for RAM. */ + return 0; + + return 1; +} + +static void emul_rfi(struct kvm_vcpu *vcpu) +{ + if ((vcpu->arch.srr1 & MSR_PR) != (vcpu->arch.guest_msr & MSR_PR)) { + /* Switch MMU between kernel and user modes. */ + kvmppc_mmu_priv_switch(vcpu, vcpu->arch.srr1 & MSR_PR); + } + + vcpu->arch.pc = vcpu->arch.srr0; + vcpu->arch.guest_msr = vcpu->arch.srr1; + + vcpu->arch.shadow_msr |= vcpu->arch.guest_msr & GUEST_MSR_MASK; + vcpu->arch.shadow_msr &= ~(vcpu->arch.guest_msr & GUEST_MSR_MASK); + +#if 0 + printk("rfi: pc %x msr %x (%x)\n", vcpu->arch.pc, + vcpu->arch.guest_msr, vcpu->arch.shadow_msr); +#endif +} + +/* Include the processor special instruction emulation header file */ +#if defined(CONFIG_KVM_POWERPC_440) +#include "44x_inst.h" +#endif + +#endif /* __ARCH_POWERPC_KVM_INST_H__ */ -- 1.5.2 ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ kvm-ppc-devel mailing list kvm-ppc-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/kvm-ppc-devel