minor KVM fixes and cleanups
Subsystems (2)
Nicholas Piggin
reviewercover minor KVM fixes and cleanups
Here is the first batch of patches are extracted from the patches of the KVM C conversion series, plus one new fix (host CTRL not restored) since v6 was posted.
Please consider for merging.
Thanks,
Nick
Nicholas Piggin (12):
KVM: PPC: Book3S HV P9: Restore host CTRL SPR after guest exit
KVM: PPC: Book3S HV: Nested move LPCR sanitising to sanitise_hv_regs
KVM: PPC: Book3S HV: Add a function to filter guest LPCR bits
KVM: PPC: Book3S HV: Disallow LPCR[AIL] to be set to 1 or 2
KVM: PPC: Book3S HV: Prevent radix guests setting LPCR[TC]
KVM: PPC: Book3S HV: Remove redundant mtspr PSPB
KVM: PPC: Book3S HV: remove unused kvmppc_h_protect argument
KVM: PPC: Book3S HV: Fix CONFIG_SPAPR_TCE_IOMMU=n default hcalls powerpc/64s: Remove KVM handler support from CBE_RAS interrupts powerpc/64s: remove KVM SKIP test from instruction breakpoint handler
KVM: PPC: Book3S HV: Ensure MSR[ME] is always set in guest MSR
KVM: PPC: Book3S HV: Ensure MSR[HV] is always clear in guest MSR
arch/powerpc/include/asm/kvm_book3s.h | 2 + arch/powerpc/include/asm/kvm_ppc.h | 3 +- arch/powerpc/kernel/exceptions-64s.S | 15 +++-- arch/powerpc/kvm/book3s_hv.c | 85 ++++++++++++++++++++------- arch/powerpc/kvm/book3s_hv_builtin.c | 3 + arch/powerpc/kvm/book3s_hv_nested.c | 37 +++++++++--- arch/powerpc/kvm/book3s_hv_rm_mmu.c | 3 +- 7 files changed, 109 insertions(+), 39 deletions(-)
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 1/12 KVM: PPC: Book3S HV P9: Restore host CTRL SPR after guest exit
The host CTRL (runlatch) value is not restored after guest exit. The host CTRL should always be 1 except in CPU idle code, so this can result in the host running with runlatch clear, and potentially switching to a different vCPU which then runs with runlatch clear as well.
This has little effect on P9 machines, CTRL is only responsible for some PMU counter logic in the host and so other than corner cases of software relying on that, or explicitly reading the runlatch value (Linux does not appear to be affected but it's possible non-Linux guests could be), there should be no execution correctness problem, though it could be used as a covert channel between guests.
There may be microcontrollers, firmware or monitoring tools that sample the runlatch value out-of-band, however since the register is writable by guests, these values would (should) not be relied upon for correct operation of the host, so suboptimal performance or incorrect reporting should be the worst problem.
--- arch/powerpc/kvm/book3s_hv.c | 3 +++ 1 file changed, 3 insertions(+)
arch/powerpc/kvm/book3s_hv.c +3 -0
@@ -3728,7 +3728,10 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit, vcpu->arch.dec_expires = dec + tb; vcpu->cpu =-1 ; vcpu->arch.thread_cpu =-1 ; +/* Save guest CTRL register, set runlatch to 1 */ vcpu->arch.ctrl =mfspr (SPRN_CTRLF ); +if (!(vcpu->arch.ctrl &1 )) +mtspr (SPRN_CTRLT , vcpu->arch.ctrl |1 ); vcpu->arch.iamr =mfspr (SPRN_IAMR ); vcpu->arch.pspb =mfspr (SPRN_PSPB );
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 2/12 KVM: PPC: Book3S HV: Nested move LPCR sanitising to sanitise_hv_regs
In reply to Nicholas PigginThis will get a bit more complicated in future patches. Move it into the helper function.
This change allows the L1 hypervisor to determine some of the LPCR bits that the L0 is using to run it, which could be a privilege violation (LPCR is HV-privileged), although the same problem exists now for HFSCR for example. Discussion of the HV privilege issue is ongoing and can be resolved with a later change.
--- arch/powerpc/kvm/book3s_hv_nested.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-)
arch/powerpc/kvm/book3s_hv_nested.c +21 -6
@@ -132,8 +132,27 @@ static void save_hv_return_state(struct kvm_vcpu *vcpu, int trap, } } +/* +* This can result in some L0 HV register state being leaked to an L1 +* hypervisor when the hv_guest_state is copied back to the guest after +* being modified here. +* +* There is no known problem with such a leak, and in many cases these +* register settings could be derived by the guest by observing behaviour +* and timing, interrupts, etc., but it is an issue to consider. +*/ static void sanitise_hv_regs (struct kvm_vcpu *vcpu,struct hv_guest_state *hr) { +struct kvmppc_vcore *vc = vcpu->arch.vcore; +u64 mask; + +/* +* Don't let L1 change LPCR bits for the L2 except these: +*/ + mask =LPCR_DPFD |LPCR_ILE |LPCR_TC |LPCR_AIL |LPCR_LD | +LPCR_LPES |LPCR_MER ; + hr->lpcr = (vc->lpcr & ~mask) | (hr->lpcr & mask); +/* * Don't let L1 enable features for L2 which we've disabled for L1, * but preserve the interrupt cause field.
@@ -271,8 +290,6 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)u64 hv_ptr, regs_ptr;u64 hdec_exp;s64 delta_purr, delta_spurr, delta_ic, delta_vtb; -u64 mask; -unsigned long lpcr;if (vcpu->kvm->arch.l1_ptcr =0 )return H_NOT_AVAILABLE ;
@@ -321,9 +338,7 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token; vcpu->arch.regs = l2_regs; vcpu->arch.shregs.msr = vcpu->arch.regs.msr; - mask =LPCR_DPFD |LPCR_ILE |LPCR_TC |LPCR_AIL |LPCR_LD | -LPCR_LPES |LPCR_MER ; - lpcr = (vc->lpcr & ~mask) | (l2_hv.lpcr & mask); +sanitise_hv_regs (vcpu, &l2_hv);restore_hv_regs (vcpu, &l2_hv);
@@ -335,7 +350,7 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) r =RESUME_HOST ;break ; } - r =kvmhv_run_single_vcpu (vcpu, hdec_exp, lpcr); + r =kvmhv_run_single_vcpu (vcpu, hdec_exp, l2_hv.lpcr); }while (is_kvmppc_resume_guest (r));/* save L2 state for return */
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 3/12 KVM: PPC: Book3S HV: Add a function to filter guest LPCR bits
In reply to Nicholas PigginGuest LPCR depends on hardware type, and future changes will add restrictions based on errata and guest MMU mode. Move this logic to a common function and use it for the cases where the guest wants to update its LPCR (or the LPCR of a nested guest).
This also adds a warning in other places that set or update LPCR if we try to set something that would have been disallowed by the filter, as a sanity check.
--- arch/powerpc/include/asm/kvm_book3s.h | 2 + arch/powerpc/kvm/book3s_hv.c | 68 ++++++++++++++++++++------- arch/powerpc/kvm/book3s_hv_nested.c | 8 +++- 3 files changed, 59 insertions(+), 19 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +2 -0
@@ -258,6 +258,8 @@ extern long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm,extern void kvmppc_harvest_vpa_dirty (struct kvmppc_vpa *vpa,struct kvm_memory_slot *memslot,unsigned long *map); +extern unsigned long kvmppc_filter_lpcr_hv (struct kvm *kvm, +unsigned long lpcr);extern void kvmppc_update_lpcr (struct kvm *kvm,unsigned long lpcr,unsigned long mask);extern void kvmppc_set_fscr (struct kvm_vcpu *vcpu,u64 fscr);
arch/powerpc/kvm/book3s_hv.c +50 -18
101 lines not on this page · raw message
arch/powerpc/kvm/book3s_hv_nested.c +7 -1
@@ -151,7 +151,13 @@ static void sanitise_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)*/ mask =LPCR_DPFD |LPCR_ILE |LPCR_TC |LPCR_AIL |LPCR_LD |LPCR_LPES |LPCR_MER ; - hr->lpcr = (vc->lpcr & ~mask) | (hr->lpcr & mask); + +/* +* Additional filtering is required depending on hardware +* and configuration. +*/ + hr->lpcr =kvmppc_filter_lpcr_hv (vcpu->kvm, + (vc->lpcr & ~mask) | (hr->lpcr & mask));/* * Don't let L1 enable features for L2 which we've disabled for L1,
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 4/12 KVM: PPC: Book3S HV: Disallow LPCR[AIL] to be set to 1 or 2
In reply to Nicholas PigginThese are already disallowed by H_SET_MODE from the guest, also disallow these by updating LPCR directly.
AIL modes can affect the host interrupt behaviour while the guest LPCR value is set, so filter it here too.
--- arch/powerpc/kvm/book3s_hv.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_hv.c +6 -1
@@ -803,7 +803,10 @@ static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags, vcpu->arch.dawrx1 = value2;return H_SUCCESS ;case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE : -/* KVM does not support mflags=2 (AIL=2) */ +/* +* KVM does not support mflags=2 (AIL=2) and AIL=1 is reserved. +* Keep this in synch with kvmppc_filter_guest_lpcr_hv. +*/ if (mflags !=0 && mflags !=3 )return H_UNSUPPORTED_FLAG_START ;return H_TOO_HARD ;
@@ -1645,6 +1648,8 @@ unsigned long kvmppc_filter_lpcr_hv(struct kvm *kvm, unsigned long lpcr)/* On POWER8 and above, userspace can modify AIL */ if (!cpu_has_feature (CPU_FTR_ARCH_207S )) lpcr &= ~LPCR_AIL ; +if ((lpcr &LPCR_AIL ) !=LPCR_AIL_3 ) + lpcr &= ~LPCR_AIL ;/* LPCR[AIL]=1/2 is disallowed */ /* * On POWER9, allow userspace to enable large decrementer for the
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 5/12 KVM: PPC: Book3S HV: Prevent radix guests setting LPCR[TC]
In reply to Nicholas PigginPrevent radix guests setting LPCR[TC]. This bit only applies to hash partitions.
--- arch/powerpc/kvm/book3s_hv.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/powerpc/kvm/book3s_hv.c +4 -0
@@ -1645,6 +1645,10 @@ static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,*/ unsigned long kvmppc_filter_lpcr_hv (struct kvm *kvm,unsigned long lpcr) { +/* LPCR_TC only applies to HPT guests */ +if (kvm_is_radix (kvm)) + lpcr &= ~LPCR_TC ; +/* On POWER8 and above, userspace can modify AIL */ if (!cpu_has_feature (CPU_FTR_ARCH_207S )) lpcr &= ~LPCR_AIL ;
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 6/12 KVM: PPC: Book3S HV: Remove redundant mtspr PSPB
In reply to Nicholas PigginThis SPR is set to 0 twice when exiting the guest.
--- arch/powerpc/kvm/book3s_hv.c | 1 - 1 file changed, 1 deletion(-)
arch/powerpc/kvm/book3s_hv.c +0 -1
@@ -3790,7 +3790,6 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,mtspr (SPRN_DSCR , host_dscr);mtspr (SPRN_TIDR , host_tidr);mtspr (SPRN_IAMR , host_iamr); -mtspr (SPRN_PSPB ,0 );if (host_amr != vcpu->arch.amr)mtspr (SPRN_AMR , host_amr);
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 7/12 KVM: PPC: Book3S HV: remove unused kvmppc_h_protect argument
In reply to Nicholas PigginThe va argument is not used in the function or set by its asm caller, so remove it to be safe.
--- arch/powerpc/include/asm/kvm_ppc.h | 3 +-- arch/powerpc/kvm/book3s_hv_rm_mmu.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +1 -2
@@ -767,8 +767,7 @@ long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags, unsigned long pte_index, unsigned long avpn);long kvmppc_h_bulk_remove (struct kvm_vcpu *vcpu);long kvmppc_h_protect (struct kvm_vcpu *vcpu,unsigned long flags, -unsigned long pte_index,unsigned long avpn, -unsigned long va); +unsigned long pte_index,unsigned long avpn);long kvmppc_h_read (struct kvm_vcpu *vcpu,unsigned long flags,unsigned long pte_index); long kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
arch/powerpc/kvm/book3s_hv_rm_mmu.c +1 -2
@@ -673,8 +673,7 @@ long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu) }long kvmppc_h_protect (struct kvm_vcpu *vcpu,unsigned long flags, -unsigned long pte_index,unsigned long avpn, -unsigned long va) +unsigned long pte_index,unsigned long avpn) {struct kvm *kvm = vcpu->kvm;__be64 *hpte;
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 8/12 KVM: PPC: Book3S HV: Fix CONFIG_SPAPR_TCE_IOMMU=n default hcalls
In reply to Nicholas PigginThis config option causes the warning in init_default_hcalls to fire because the TCE handlers are in the default hcall list but not implemented.
--- arch/powerpc/kvm/book3s_hv.c | 2 ++ 1 file changed, 2 insertions(+)
arch/powerpc/kvm/book3s_hv.c +2 -0
@@ -5412,8 +5412,10 @@ static unsigned int default_hcall_list[] = {H_READ ,H_PROTECT ,H_BULK_REMOVE , +#ifdef CONFIG_SPAPR_TCE_IOMMUH_GET_TCE ,H_PUT_TCE , +#endifH_SET_DABR ,H_SET_XDABR , H_CEDE,
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 9/12 powerpc/64s: Remove KVM handler support from CBE_RAS interrupts
In reply to Nicholas PigginCell does not support KVM.
--- arch/powerpc/kernel/exceptions-64s.S | 6 ------ 1 file changed, 6 deletions(-)
arch/powerpc/kernel/exceptions-64s.S +0 -6
@@ -2530,8 +2530,6 @@ EXC_VIRT_NONE(0x5100, 0x100) INT_DEFINE_BEGIN(cbe_system_error) IVEC=0x1200 IHSRR=1 - IKVM_SKIP=1 - IKVM_REAL=1 INT_DEFINE_END(cbe_system_error) EXC_REAL_BEGIN(cbe_system_error, 0x1200, 0x100)
@@ -2701,8 +2699,6 @@ EXC_COMMON_BEGIN(denorm_exception_common) INT_DEFINE_BEGIN(cbe_maintenance) IVEC=0x1600 IHSRR=1 - IKVM_SKIP=1 - IKVM_REAL=1 INT_DEFINE_END(cbe_maintenance) EXC_REAL_BEGIN(cbe_maintenance, 0x1600, 0x100)
@@ -2754,8 +2750,6 @@ EXC_COMMON_BEGIN(altivec_assist_common) INT_DEFINE_BEGIN(cbe_thermal) IVEC=0x1800 IHSRR=1 - IKVM_SKIP=1 - IKVM_REAL=1 INT_DEFINE_END(cbe_thermal) EXC_REAL_BEGIN(cbe_thermal, 0x1800, 0x100)
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 10/12 powerpc/64s: remove KVM SKIP test from instruction breakpoint handler
In reply to Nicholas PigginThe code being executed in KVM_GUEST_MODE_SKIP is hypervisor code with MSR[IR]=0, so the faults of concern are the d-side ones caused by access to guest context by the hypervisor.
Instruction breakpoint interrupts are not a concern here. It's unlikely any good would come of causing breaks in this code, but skipping the instruction that caused it won't help matters (e.g., skip the mtmsr that sets MSR[DR]=0 or clears KVM_GUEST_MODE_SKIP).
[Paul notes: "the 0x1300 interrupt was dropped from the architecture a
long time ago and is not generated by P7, P8, P9 or P10." So add a comment about this in the handler code while we're here. ]
--- arch/powerpc/kernel/exceptions-64s.S | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/powerpc/kernel/exceptions-64s.S +7 -2
@@ -2549,11 +2549,16 @@ EXC_REAL_NONE(0x1200, 0x100) EXC_VIRT_NONE(0x5200, 0x100) #endif - +/** + * Interrupt 0x1300 - Instruction Address Breakpoint Interrupt. + * This has been removed from the ISA before 2.01, which is the earliest + * 64-bit BookS ISA supported, however the G5 / 970 implements this + * interrupt with a non-architected feature available through the support + * processor interface. + */ INT_DEFINE_BEGIN(instruction_breakpoint) IVEC=0x1300 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE - IKVM_SKIP=1 IKVM_REAL=1 #endif INT_DEFINE_END(instruction_breakpoint)
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 11/12 KVM: PPC: Book3S HV: Ensure MSR[ME] is always set in guest MSR
In reply to Nicholas PigginRather than add the ME bit to the MSR at guest entry, make it clear that the hypervisor does not allow the guest to clear the bit.
The ME set is kept in guest entry for now, but a future patch will warn if it's not present.
--- arch/powerpc/kvm/book3s_hv_builtin.c | 3 +++ arch/powerpc/kvm/book3s_hv_nested.c | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_hv_builtin.c +3 -0
@@ -662,6 +662,9 @@ static void kvmppc_end_cede(struct kvm_vcpu *vcpu)void kvmppc_set_msr_hv (struct kvm_vcpu *vcpu,u64 msr) { +/* Guest must always run with ME enabled. */ + msr = msr |MSR_ME ; +/* * Check for illegal transactional state bit combination * and if we find it, force the TS field to a safe state.
arch/powerpc/kvm/book3s_hv_nested.c +3 -1
@@ -343,7 +343,9 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) vcpu->arch.nested = l2; vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token; vcpu->arch.regs = l2_regs; - vcpu->arch.shregs.msr = vcpu->arch.regs.msr; + +/* Guest must always run with ME enabled. */ + vcpu->arch.shregs.msr = vcpu->arch.regs.msr |MSR_ME ;sanitise_hv_regs (vcpu, &l2_hv);restore_hv_regs (vcpu, &l2_hv);
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerPATCH 12/12 KVM: PPC: Book3S HV: Ensure MSR[HV] is always clear in guest MSR
In reply to Nicholas PigginRather than clear the HV bit from the MSR at guest entry, make it clear that the hypervisor does not allow the guest to set the bit.
The HV clear is kept in guest entry for now, but a future patch will warn if it is set.
--- arch/powerpc/kvm/book3s_hv_builtin.c | 4 ++-- arch/powerpc/kvm/book3s_hv_nested.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/kvm/book3s_hv_builtin.c +2 -2
@@ -662,8 +662,8 @@ static void kvmppc_end_cede(struct kvm_vcpu *vcpu)void kvmppc_set_msr_hv (struct kvm_vcpu *vcpu,u64 msr) { -/* Guest must always run with ME enabled. */ - msr = msr |MSR_ME ; +/* Guest must always run with ME enabled, HV disabled. */ + msr = (msr |MSR_ME ) & ~MSR_HV ;/* * Check for illegal transactional state bit combination
arch/powerpc/kvm/book3s_hv_nested.c +2 -2
@@ -344,8 +344,8 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token; vcpu->arch.regs = l2_regs; -/* Guest must always run with ME enabled. */ - vcpu->arch.shregs.msr = vcpu->arch.regs.msr |MSR_ME ; +/* Guest must always run with ME enabled, HV disabled. */ + vcpu->arch.shregs.msr = (vcpu->arch.regs.msr |MSR_ME ) & ~MSR_HV ;sanitise_hv_regs (vcpu, &l2_hv);restore_hv_regs (vcpu, &l2_hv);
Signature · 1 line
2.23.0
Re: [PATCH v1 01/12] KVM: PPC: Book3S HV P9: Restore host CTRL SPR after guest exit
In reply to Nicholas PigginQuoted 29 lines · Nicholas Piggin
The host CTRL (runlatch) value is not restored after guest exit. The host CTRL should always be 1 except in CPU idle code, so this can result in the host running with runlatch clear, and potentially switching to a different vCPU which then runs with runlatch clear as well.
This has little effect on P9 machines, CTRL is only responsible for some PMU counter logic in the host and so other than corner cases of software relying on that, or explicitly reading the runlatch value (Linux does not appear to be affected but it's possible non-Linux guests could be), there should be no execution correctness problem, though it could be used as a covert channel between guests.
There may be microcontrollers, firmware or monitoring tools that sample the runlatch value out-of-band, however since the register is writable by guests, these values would (should) not be relied upon for correct operation of the host, so suboptimal performance or incorrect reporting should be the worst problem.
Fixes 95a6432ce9038 ("KVM: PPC: Book3S HV: Streamlined guest entry/exit path on P9 for radix guests")Signed-off-by Nicholas Piggin <[email protected]>--- arch/powerpc/kvm/book3s_hv.c | 3 +++ 1 file changed, 3 insertions(+)arch/powerpc/kvm/book3s_hv.c +0
-0
arch/powerpc/kvm/book3s_hv.c static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit, · line 3734
Quoted 7 lines
arch/powerpc/kvm/book3s_hv.c +3
-0@@ -3728,7 +3728,10 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit, vcpu->arch.dec_expires = dec + tb; vcpu->cpu =-1 ; vcpu->arch.thread_cpu =-1 ; +/* Save guest CTRL register, set runlatch to 1 */ vcpu->arch.ctrl =mfspr (SPRN_CTRLF ); +if (!(vcpu->arch.ctrl &1 )) +mtspr (SPRN_CTRLT , vcpu->arch.ctrl |1 );
Maybe ditch the comment and use the already defined CTRL_RUNLATCH?
Quoted 3 lines
arch/powerpc/kvm/book3s_hv.c +0
-0vcpu->arch.iamr =mfspr (SPRN_IAMR ); vcpu->arch.pspb =mfspr (SPRN_PSPB );
Nicholas Piggin
reviewerRe: [PATCH v1 01/12] KVM: PPC: Book3S HV P9: Restore host CTRL SPR after guest exit
Excerpts from Fabiano Rosas's message of April 13, 2021 12:06 am:
Quoted 26 lines
Quoted 24 lines · Nicholas Piggin
The host CTRL (runlatch) value is not restored after guest exit. The host CTRL should always be 1 except in CPU idle code, so this can result in the host running with runlatch clear, and potentially switching to a different vCPU which then runs with runlatch clear as well.
This has little effect on P9 machines, CTRL is only responsible for some PMU counter logic in the host and so other than corner cases of software relying on that, or explicitly reading the runlatch value (Linux does not appear to be affected but it's possible non-Linux guests could be), there should be no execution correctness problem, though it could be used as a covert channel between guests.
There may be microcontrollers, firmware or monitoring tools that sample the runlatch value out-of-band, however since the register is writable by guests, these values would (should) not be relied upon for correct operation of the host, so suboptimal performance or incorrect reporting should be the worst problem.
Fixes 95a6432ce9038 ("KVM: PPC: Book3S HV: Streamlined guest entry/exit path on P9 for radix guests")Signed-off-by Nicholas Piggin <[email protected]>--- arch/powerpc/kvm/book3s_hv.c | 3 +++ 1 file changed, 3 insertions(+)
Quoted 14 lines
arch/powerpc/kvm/book3s_hv.c static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit, · line 3734
Quoted 12 lines
arch/powerpc/kvm/book3s_hv.c +3
-0@@ -3728,7 +3728,10 @@ static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit, vcpu->arch.dec_expires = dec + tb; vcpu->cpu =-1 ; vcpu->arch.thread_cpu =-1 ; +/* Save guest CTRL register, set runlatch to 1 */ vcpu->arch.ctrl =mfspr (SPRN_CTRLF ); +if (!(vcpu->arch.ctrl &1 )) +mtspr (SPRN_CTRLT , vcpu->arch.ctrl |1 );Maybe ditch the comment and use the already defined
CTRL_RUNLATCH?
I did it this way so you can more easily match up the C with the existing asm version.
I have a later patch to clean up CTRL handling a bit (in both C and asm).
Thanks,
Nick
Re: [PATCH v1 12/12] KVM: PPC: Book3S HV: Ensure MSR[HV] is always clear in guest MSR
In reply to Nicholas PigginQuoted 8 lines · Nicholas Piggin
Rather than clear the HV bit from the MSR at guest entry, make it clear that the hypervisor does not allow the guest to set the bit.
The HV clear is kept in guest entry for now, but a future patch will warn if it is set.
Acked-by Paul Mackerras <[email protected]>Signed-off-by Nicholas Piggin <[email protected]>
Quoted 35 lines
--- arch/powerpc/kvm/book3s_hv_builtin.c | 4 ++-- arch/powerpc/kvm/book3s_hv_nested.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)arch/powerpc/kvm/book3s_hv_builtin.c +2
-2@@ -662,8 +662,8 @@ static void kvmppc_end_cede(struct kvm_vcpu *vcpu)void kvmppc_set_msr_hv (struct kvm_vcpu *vcpu,u64 msr) { -/* Guest must always run with ME enabled. */ - msr = msr |MSR_ME ; +/* Guest must always run with ME enabled, HV disabled. */ + msr = (msr |MSR_ME ) & ~MSR_HV ;/* * Check for illegal transactional state bit combination arch/powerpc/kvm/book3s_hv_nested.c +2
-2@@ -344,8 +344,8 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token; vcpu->arch.regs = l2_regs; -/* Guest must always run with ME enabled. */ - vcpu->arch.shregs.msr = vcpu->arch.regs.msr |MSR_ME ; +/* Guest must always run with ME enabled, HV disabled. */ + vcpu->arch.shregs.msr = (vcpu->arch.regs.msr |MSR_ME ) & ~MSR_HV ;sanitise_hv_regs (vcpu, &l2_hv);restore_hv_regs (vcpu, &l2_hv);
Michael Ellerman
AppliedQuoted 10 lines · Nicholas Piggin
Here is the first batch of patches are extracted from the patches of the KVM C conversion series, plus one new fix (host CTRL not restored) since v6 was posted.
Please consider for merging.
Thanks,
Nick[...]
Applied to powerpc/next.
[01/12] KVM: PPC: Book3S HV P9: Restore host CTRL SPR after guest exit
https://git.kernel.org/powerpc/c/5088eb4092df12d701af8e0e92860b7186365279 [02/12] KVM: PPC: Book3S HV: Nested move LPCR sanitising to sanitise_hv_regs https://git.kernel.org/powerpc/c/a19b70abc69aea8ea5974c57e1c3457d9df6aff2 [03/12] KVM: PPC: Book3S HV: Add a function to filter guest LPCR bits https://git.kernel.org/powerpc/c/67145ef4960f55923b9e404c0b184944bfeded4d [04/12] KVM: PPC: Book3S HV: Disallow LPCR[AIL] to be set to 1 or 2 https://git.kernel.org/powerpc/c/bcc92a0d6d6eae1e7b34a88f58ae69c081d85f97 [05/12] KVM: PPC: Book3S HV: Prevent radix guests setting LPCR[TC] https://git.kernel.org/powerpc/c/72c15287210f7433f5fcb55452b05e4b6ccc6c15 [06/12] KVM: PPC: Book3S HV: Remove redundant mtspr PSPB https://git.kernel.org/powerpc/c/4b5f0a0d49e663adf1c7c6f2dd05cb18dd53db8c [07/12] KVM: PPC: Book3S HV: remove unused kvmppc_h_protect argument https://git.kernel.org/powerpc/c/6c12c4376bbbc89fc84480096ba838e07ab7c405 [08/12] KVM: PPC: Book3S HV: Fix CONFIG_SPAPR_TCE_IOMMU=n default hcalls https://git.kernel.org/powerpc/c/0fd85cb83fbd7048d8a024ba1338924349e26fd5 [09/12] powerpc/64s: Remove KVM handler support from CBE_RAS interrupts https://git.kernel.org/powerpc/c/5eee8371828a92a2620453907d6b2b6dc819ab3a [10/12] powerpc/64s: remove KVM SKIP test from instruction breakpoint handler https://git.kernel.org/powerpc/c/da487a5d1bee6a30798a8db15986d3d028c8ac92 [11/12] KVM: PPC: Book3S HV: Ensure MSR[ME] is always set in guest MSR https://git.kernel.org/powerpc/c/946cf44ac6ce61378ea02386d39394a06d502f28 [12/12] KVM: PPC: Book3S HV: Ensure MSR[HV] is always clear in guest MSR https://git.kernel.org/powerpc/c/732f21a3053cf279eb6b85d19b7818a8f1dd2071
cheers