Skip to content

Commit f5b29db

Browse files
paulusmackgregkh
authored andcommitted
KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit
commit 4c3bb4c upstream. This restores several special-purpose registers (SPRs) to sane values on guest exit that were missed before. TAR and VRSAVE are readable and writable by userspace, and we need to save and restore them to prevent the guest from potentially affecting userspace execution (not that TAR or VRSAVE are used by any known program that run uses the KVM_RUN ioctl). We save/restore these in kvmppc_vcpu_run_hv() rather than on every guest entry/exit. FSCR affects userspace execution in that it can prohibit access to certain facilities by userspace. We restore it to the normal value for the task on exit from the KVM_RUN ioctl. IAMR is normally 0, and is restored to 0 on guest exit. However, with a radix host on POWER9, it is set to a value that prevents the kernel from executing user-accessible memory. On POWER9, we save IAMR on guest entry and restore it on guest exit to the saved value rather than 0. On POWER8 we continue to set it to 0 on guest exit. PSPB is normally 0. We restore it to 0 on guest exit to prevent userspace taking advantage of the guest having set it non-zero (which would allow userspace to set its SMT priority to high). UAMOR is normally 0. We restore it to 0 on guest exit to prevent the AMR from being used as a covert channel between userspace processes, since the AMR is not context-switched at present. [[email protected] - removed IAMR bits that are only needed on POWER9; adjusted FSCR save/restore for lack of fscr field in thread_struct.] Fixes: b005255 ("KVM: PPC: Book3S HV: Context-switch new POWER8 SPRs", 2014-01-08) Signed-off-by: Paul Mackerras <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 339fa4a commit f5b29db

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

arch/powerpc/kvm/book3s_hv.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,6 +2688,9 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
26882688
int r;
26892689
int srcu_idx;
26902690
unsigned long ebb_regs[3] = {}; /* shut up GCC */
2691+
unsigned long user_tar = 0;
2692+
unsigned long proc_fscr = 0;
2693+
unsigned int user_vrsave;
26912694

26922695
if (!vcpu->arch.sane) {
26932696
run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
@@ -2738,12 +2741,15 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
27382741
flush_altivec_to_thread(current);
27392742
flush_vsx_to_thread(current);
27402743

2741-
/* Save userspace EBB register values */
2744+
/* Save userspace EBB and other register values */
27422745
if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
27432746
ebb_regs[0] = mfspr(SPRN_EBBHR);
27442747
ebb_regs[1] = mfspr(SPRN_EBBRR);
27452748
ebb_regs[2] = mfspr(SPRN_BESCR);
2749+
user_tar = mfspr(SPRN_TAR);
2750+
proc_fscr = mfspr(SPRN_FSCR);
27462751
}
2752+
user_vrsave = mfspr(SPRN_VRSAVE);
27472753

27482754
vcpu->arch.wqp = &vcpu->arch.vcore->wq;
27492755
vcpu->arch.pgdir = current->mm->pgd;
@@ -2766,12 +2772,15 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
27662772
}
27672773
} while (is_kvmppc_resume_guest(r));
27682774

2769-
/* Restore userspace EBB register values */
2775+
/* Restore userspace EBB and other register values */
27702776
if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
27712777
mtspr(SPRN_EBBHR, ebb_regs[0]);
27722778
mtspr(SPRN_EBBRR, ebb_regs[1]);
27732779
mtspr(SPRN_BESCR, ebb_regs[2]);
2780+
mtspr(SPRN_TAR, user_tar);
2781+
mtspr(SPRN_FSCR, proc_fscr);
27742782
}
2783+
mtspr(SPRN_VRSAVE, user_vrsave);
27752784

27762785
out:
27772786
vcpu->arch.state = KVMPPC_VCPU_NOTREADY;

arch/powerpc/kvm/book3s_hv_rmhandlers.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
12681268
mtspr SPRN_IAMR, r0
12691269
mtspr SPRN_CIABR, r0
12701270
mtspr SPRN_DAWRX, r0
1271+
mtspr SPRN_PSPB, r0
12711272
mtspr SPRN_TCSCR, r0
12721273
mtspr SPRN_WORT, r0
12731274
/* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
@@ -1283,6 +1284,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
12831284
std r6,VCPU_UAMOR(r9)
12841285
li r6,0
12851286
mtspr SPRN_AMR,r6
1287+
mtspr SPRN_UAMOR, r6
12861288

12871289
/* Switch DSCR back to host value */
12881290
mfspr r8, SPRN_DSCR

0 commit comments

Comments
 (0)