KVM: PPC: Emulator: clean up instruction parsing
Subsystems (3)
PATCH 1/2 KVM: PPC: Emulator: clean up instruction parsing
Instructions on PPC are pretty similarly encoded. So instead of every instruction emulation code decoding the instruction fields itself, we can move that code to more generic places and rely on the compiler to optimize the unused bits away.
This has 2 advantages. It makes the code smaller and it makes the code less error prone, as the instruction fields are always available, so accidental misusage is reduced.
Functionally, this patch doesn't change anything.
--- arch/powerpc/kvm/44x_emulate.c | 27 ++++---------- arch/powerpc/kvm/book3s_emulate.c | 72 +++++++++++++++++++----------------- arch/powerpc/kvm/booke_emulate.c | 7 +--- arch/powerpc/kvm/e500_emulate.c | 16 +++------ arch/powerpc/kvm/emulate.c | 71 ++---------------------------------- 5 files changed, 56 insertions(+), 137 deletions(-)
arch/powerpc/kvm/44x_emulate.c +7 -20
@@ -37,22 +37,19 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, unsigned int inst, int *advance) { int emulated = EMULATE_DONE; -int dcrn; -int ra; -int rb; -int rc; -int rs; -int rt; -int ws; +int dcrn =get_dcrn (inst); +int ra =get_ra (inst); +int rb =get_rb (inst); +int rc =get_rc (inst); +int rs =get_rs (inst); +int rt =get_rt (inst); +int ws =get_ws (inst);switch (get_op (inst)) {case 31 :switch (get_xop (inst)) {case XOP_MFDCR : - dcrn =get_dcrn (inst); - rt =get_rt (inst); -/* The guest may access CPR0 registers to determine the timebase * frequency, and it must know the real host frequency because it * can directly access the timebase registers.
@@ -88,9 +85,6 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,break ;case XOP_MTDCR : - dcrn =get_dcrn (inst); - rs =get_rs (inst); -/* emulate some access in kernel */ switch (dcrn) {case DCRN_CPR0_CONFIG_ADDR :
@@ -108,17 +102,10 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,break ;case XOP_TLBWE : - ra =get_ra (inst); - rs =get_rs (inst); - ws =get_ws (inst); emulated =kvmppc_44x_emul_tlbwe (vcpu, ra, rs, ws);break ;case XOP_TLBSX : - rt =get_rt (inst); - ra =get_ra (inst); - rb =get_rb (inst); - rc =get_rc (inst); emulated =kvmppc_44x_emul_tlbsx (vcpu, rt, ra, rb, rc);break ;
arch/powerpc/kvm/book3s_emulate.c +38 -34
178 lines not on this page · raw message
arch/powerpc/kvm/booke_emulate.c +2 -5
@@ -40,8 +40,8 @@ int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, unsigned int inst, int *advance) { int emulated = EMULATE_DONE; -int rs; -int rt; +int rs =get_rs (inst); +int rt =get_rt (inst);switch (get_op (inst)) {case 19 :
@@ -62,19 +62,16 @@ int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,switch (get_xop (inst)) {case OP_31_XOP_MFMSR : - rt =get_rt (inst);kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->msr);kvmppc_set_exit_type (vcpu,EMULATED_MFMSR_EXITS );break ;case OP_31_XOP_MTMSR : - rs =get_rs (inst);kvmppc_set_exit_type (vcpu,EMULATED_MTMSR_EXITS );kvmppc_set_msr (vcpu,kvmppc_get_gpr (vcpu, rs));break ;case OP_31_XOP_WRTEE : - rs =get_rs (inst); vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE ) | (kvmppc_get_gpr (vcpu, rs) &MSR_EE );kvmppc_set_exit_type (vcpu,EMULATED_WRTEE_EXITS );
arch/powerpc/kvm/e500_emulate.c +5 -11
@@ -86,9 +86,9 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, unsigned int inst, int *advance) { int emulated = EMULATE_DONE; -int ra; -int rb; -int rt; +int ra =get_ra (inst); +int rb =get_rb (inst); +int rt =get_rt (inst);switch (get_op (inst)) {case 31 :
@@ -96,11 +96,11 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,#ifdef CONFIG_KVM_E500MC case XOP_MSGSND : - emulated =kvmppc_e500_emul_msgsnd (vcpu,get_rb (inst)); + emulated =kvmppc_e500_emul_msgsnd (vcpu, rb);break ;case XOP_MSGCLR : - emulated =kvmppc_e500_emul_msgclr (vcpu,get_rb (inst)); + emulated =kvmppc_e500_emul_msgclr (vcpu, rb);break ;#endif
@@ -113,20 +113,14 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,break ;case XOP_TLBSX : - rb =get_rb (inst); emulated =kvmppc_e500_emul_tlbsx (vcpu,rb);break ;case XOP_TLBILX : - ra =get_ra (inst); - rb =get_rb (inst); - rt =get_rt (inst); emulated =kvmppc_e500_emul_tlbilx (vcpu, rt, ra, rb);break ;case XOP_TLBIVAX : - ra =get_ra (inst); - rb =get_rb (inst); emulated =kvmppc_e500_emul_tlbivax (vcpu, ra, rb);break ;
arch/powerpc/kvm/emulate.c +4 -67
239 lines not on this page · raw message
Signature · 1 line
1.6.0.2
PATCH 2/2 KVM: PPC: Emulator: clean up SPR reads and writes
When reading and writing SPRs, every SPR emulation piece had to read or write the respective GPR the value was read from or stored in itself.
This approach is pretty prone to failure. What if we accidentally implement mfspr emulation where we just do "break" and nothing else? Suddenly we would get a random value in the return register - which is always a bad idea.
So let's consolidate the generic code paths and only give the core specific SPR handling code readily made variables to read/write from/to.
Functionally, this patch doesn't change anything, but it increases the readability of the code and makes is less prone to bugs.
--- arch/powerpc/include/asm/kvm_ppc.h | 6 +- arch/powerpc/kvm/44x_emulate.c | 24 ++++---- arch/powerpc/kvm/book3s_emulate.c | 34 +++++------ arch/powerpc/kvm/book3s_hv.c | 4 +- arch/powerpc/kvm/booke.h | 4 +- arch/powerpc/kvm/booke_emulate.c | 88 +++++++++++++++++------------ arch/powerpc/kvm/e500_emulate.c | 108 ++++++++++++++++++++++-------------- arch/powerpc/kvm/emulate.c | 64 ++++++++++++--------- 8 files changed, 190 insertions(+), 142 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +4 -2
@@ -107,8 +107,10 @@ extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,extern int kvmppc_core_emulate_op (struct kvm_run *run,struct kvm_vcpu *vcpu,unsigned int op,int *advance); -extern int kvmppc_core_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn,int rs); -extern int kvmppc_core_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn,int rt); +extern int kvmppc_core_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn, +ulong val); +extern int kvmppc_core_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn, +ulong *val);extern int kvmppc_booke_init (void );extern void kvmppc_booke_exit (void );
arch/powerpc/kvm/44x_emulate.c +12 -12
@@ -128,41 +128,41 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,return emulated; } -int kvmppc_core_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn,int rs) +int kvmppc_core_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn,ulong spr_val) {int emulated =EMULATE_DONE ;switch (sprn) {case SPRN_PID : -kvmppc_set_pid (vcpu,kvmppc_get_gpr (vcpu, rs));break ; +kvmppc_set_pid (vcpu, spr_val);break ;case SPRN_MMUCR : - vcpu->arch.mmucr =kvmppc_get_gpr (vcpu, rs);break ; + vcpu->arch.mmucr = spr_val;break ;case SPRN_CCR0 : - vcpu->arch.ccr0 =kvmppc_get_gpr (vcpu, rs);break ; + vcpu->arch.ccr0 = spr_val;break ;case SPRN_CCR1 : - vcpu->arch.ccr1 =kvmppc_get_gpr (vcpu, rs);break ; + vcpu->arch.ccr1 = spr_val;break ;default : - emulated =kvmppc_booke_emulate_mtspr (vcpu, sprn, rs); + emulated =kvmppc_booke_emulate_mtspr (vcpu, sprn, spr_val); }return emulated; } -int kvmppc_core_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn,int rt) +int kvmppc_core_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn,ulong *spr_val) {int emulated =EMULATE_DONE ;switch (sprn) {case SPRN_PID : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.pid);break ; + *spr_val = vcpu->arch.pid;break ;case SPRN_MMUCR : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.mmucr);break ; + *spr_val = vcpu->arch.mmucr;break ;case SPRN_CCR0 : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.ccr0);break ; + *spr_val = vcpu->arch.ccr0;break ;case SPRN_CCR1 : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.ccr1);break ; + *spr_val = vcpu->arch.ccr1;break ;default : - emulated =kvmppc_booke_emulate_mfspr (vcpu, sprn, rt); + emulated =kvmppc_booke_emulate_mfspr (vcpu, sprn, spr_val); }return emulated;
arch/powerpc/kvm/book3s_emulate.c +16 -18
@@ -318,10 +318,9 @@ static struct kvmppc_bat *kvmppc_find_bat(struct kvm_vcpu *vcpu, int sprn)return bat; } -int kvmppc_core_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn,int rs) +int kvmppc_core_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn,ulong spr_val) {int emulated =EMULATE_DONE ; -ulong spr_val =kvmppc_get_gpr (vcpu, rs);switch (sprn) {case SPRN_SDR1 :
@@ -433,7 +432,7 @@ unprivileged:return emulated; } -int kvmppc_core_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn,int rt) +int kvmppc_core_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn,ulong *spr_val) {int emulated =EMULATE_DONE ;
@@ -446,46 +445,46 @@ int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)struct kvmppc_bat *bat =kvmppc_find_bat (vcpu, sprn);if (sprn %2 ) -kvmppc_set_gpr (vcpu, rt, bat->raw >>32 ); + *spr_val = bat->raw >>32 ;else -kvmppc_set_gpr (vcpu, rt, bat->raw); + *spr_val = bat->raw;break ; }case SPRN_SDR1 :if (!spr_allowed (vcpu,PRIV_HYPER )) goto unprivileged; -kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->sdr1); + *spr_val =to_book3s (vcpu)->sdr1;break ;case SPRN_DSISR : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->dsisr); + *spr_val = vcpu->arch.shared->dsisr;break ;case SPRN_DAR : -kvmppc_set_gpr (vcpu, rt, vcpu->arch.shared->dar); + *spr_val = vcpu->arch.shared->dar;break ;case SPRN_HIOR : -kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->hior); + *spr_val =to_book3s (vcpu)->hior;break ;case SPRN_HID0 : -kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->hid[0 ]); + *spr_val =to_book3s (vcpu)->hid[0 ];break ;case SPRN_HID1 : -kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->hid[1 ]); + *spr_val =to_book3s (vcpu)->hid[1 ];break ;case SPRN_HID2 :case SPRN_HID2_GEKKO : -kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->hid[2 ]); + *spr_val =to_book3s (vcpu)->hid[2 ];break ;case SPRN_HID4 :case SPRN_HID4_GEKKO : -kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->hid[4 ]); + *spr_val =to_book3s (vcpu)->hid[4 ];break ;case SPRN_HID5 : -kvmppc_set_gpr (vcpu, rt,to_book3s (vcpu)->hid[5 ]); + *spr_val =to_book3s (vcpu)->hid[5 ];break ;case SPRN_CFAR :case SPRN_PURR : -kvmppc_set_gpr (vcpu, rt,0 ); + *spr_val =0 ;break ;case SPRN_GQR0 :case SPRN_GQR1 :
@@ -495,8 +494,7 @@ int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)case SPRN_GQR5 :case SPRN_GQR6 :case SPRN_GQR7 : -kvmppc_set_gpr (vcpu, rt, -to_book3s (vcpu)->gqr[sprn -SPRN_GQR0 ]); + *spr_val =to_book3s (vcpu)->gqr[sprn -SPRN_GQR0 ];break ;case SPRN_THRM1 :case SPRN_THRM2 :
@@ -511,7 +509,7 @@ int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)case SPRN_PMC3_GEKKO :case SPRN_PMC4_GEKKO :case SPRN_WPAR_GEKKO : -kvmppc_set_gpr (vcpu, rt,0 ); + *spr_val =0 ;break ;default : unprivileged:
arch/powerpc/kvm/book3s_hv.c +2 -2
@@ -1505,12 +1505,12 @@ int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,return EMULATE_FAIL ; } -int kvmppc_core_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn,int rs) +int kvmppc_core_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn,ulong spr_val) {return EMULATE_FAIL ; } -int kvmppc_core_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn,int rt) +int kvmppc_core_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn,ulong *spr_val) {return EMULATE_FAIL ; }
arch/powerpc/kvm/booke.h +2 -2
@@ -75,8 +75,8 @@ void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits);int kvmppc_booke_emulate_op (struct kvm_run *run,struct kvm_vcpu *vcpu,unsigned int inst,int *advance); -int kvmppc_booke_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn,int rt); -int kvmppc_booke_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn,int rs); +int kvmppc_booke_emulate_mfspr (struct kvm_vcpu *vcpu,int sprn,ulong *spr_val); +int kvmppc_booke_emulate_mtspr (struct kvm_vcpu *vcpu,int sprn,ulong spr_val);/* low-level asm code to transfer guest state */ void kvmppc_load_guest_spe (struct kvm_vcpu *vcpu);
arch/powerpc/kvm/booke_emulate.c +52 -36
162 lines not on this page · raw message
arch/powerpc/kvm/e500_emulate.c +66 -42
200 lines not on this page · raw message
arch/powerpc/kvm/emulate.c +36 -28
136 lines not on this page · raw message
Signature · 1 line
1.6.0.2