KVM: arm64: Support SVE in protected VMs
Subsystems (4)
Fuad Tabba
reviewercover KVM: arm64: Support SVE in protected VMs
Hi folks,
This series adds SVE support for protected VMs. EL2 allocates a protected vCPU's SVE state from its own heap rather than pinning the host's buffer, so the host never sees the guest's vector registers. It then advertises ID_AA64ZFR0_EL1 to protected VMs, and routes the SVE trap to the lazy FP switch. The first patch is a small tidy-up of the pKVM FP sync code that saves a guest's SVE state.
v2 of the pKVM selftests series [1] will add a selftest for this.
Based on Linux 7.4-rc1 (6628e7d4c6b25).
Cheers,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
Fuad Tabba (4):
KVM: arm64: Remove redundant kern_hyp_va() calls from the pKVM FP sync
KVM: arm64: Allocate a protected vCPU's SVE state at EL2
KVM: arm64: Advertise ID_AA64ZFR0_EL1 to protected VMs
KVM: arm64: Allow protected VMs to use SVE
Documentation/virt/kvm/arm/pkvm.rst | 6 ++--- arch/arm64/include/asm/kvm_hyp.h | 2 ++ arch/arm64/include/asm/kvm_pkvm.h | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 +-- arch/arm64/kvm/hyp/nvhe/pkvm.c | 41 +++++++++++++++++++---------- arch/arm64/kvm/hyp/nvhe/switch.c | 1 + arch/arm64/kvm/hyp/nvhe/sys_regs.c | 22 ++++++++++++++-- arch/arm64/kvm/reset.c | 2 ++ 9 files changed, 59 insertions(+), 21 deletions(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.47.3
Fuad Tabba
reviewerPATCH 1/4 KVM: arm64: Remove redundant kern_hyp_va() calls from the pKVM FP sync
With pKVM, EL2 saves a guest's FP and SVE state on exit into the hyp vCPU, whose kvm is the hyp VM's and whose sve_state is already a hyp address. kern_hyp_va() returns both unchanged.
Remove the two calls, in fpsimd_sve_sync() and __hyp_sve_save_guest(). The one in __hyp_sve_restore_guest() stays, as nVHE without pKVM reaches it with the host's pointer.
No functional change intended.
--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -2
@@ -482,7 +482,7 @@ static void __hyp_sve_save_guest(struct kvm_vcpu *vcpu)* on the VL, so use a consistent (i.e., the maximum) guest VL. */ sve_cond_update_zcr_vq (vcpu_sve_max_vq (vcpu) -1 ,SYS_ZCR_EL2 ); -sve_save_state (kern_hyp_va (vcpu->arch.sve_state), true); +sve_save_state (vcpu->arch.sve_state, true);fpsimd_save_common (&vcpu->arch.ctxt.fp_regs);write_sysreg_s (sve_vq_from_vl (kvm_host_sve_max_vl) -1 ,SYS_ZCR_EL2 ); }
@@ -531,7 +531,7 @@ static void fpsimd_sve_sync(struct kvm_vcpu *vcpu)else fpsimd_save_state (&vcpu->arch.ctxt.fp_regs); - has_fpmr =kvm_has_fpmr (kern_hyp_va (vcpu->kvm)); + has_fpmr =kvm_has_fpmr (vcpu->kvm);if (has_fpmr)__vcpu_assign_sys_reg (vcpu,FPMR ,read_sysreg_s (SYS_FPMR ));
Signature · 1 line
2.47.3
Fuad Tabba
reviewerPATCH 2/4 KVM: arm64: Allocate a protected vCPU's SVE state at EL2
In reply to Fuad TabbaFor a vCPU with SVE, EL2 pins the buffer the host allocated at KVM_ARM_VCPU_FINALIZE and saves the guest's SVE registers there. The host can still read and write that buffer, so it can't hold a protected vCPU's state.
Allocate a protected vCPU's SVE state from the hyp heap instead, and free it with the vCPU. When EL2 resets a protected vCPU, zero its SVE state and ZCR_EL1, as KVM does for a non-protected one. The host still allocates its own buffer, which KVM_{GET,SET}_ONE_REG access before the first run, but EL2 doesn't use it.
For a protected vCPU, the host's sve_max_vl now sizes an EL2 allocation and is the guest's maximum vector length on every CPU. Give EL2 a copy of kvm_sve_max_vl, the largest length all CPUs can give a guest, and check every vCPU's sve_max_vl against it rather than clamping it to the host's own maximum. set_sve_vls() already keeps the host's value within that bound.
--- arch/arm64/include/asm/kvm_hyp.h | 1 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 41 ++++++++++++++++++++---------- arch/arm64/kvm/hyp/nvhe/sys_regs.c | 1 + arch/arm64/kvm/reset.c | 2 ++ 4 files changed, 31 insertions(+), 14 deletions(-)
arch/arm64/include/asm/kvm_hyp.h +1 -0
@@ -157,6 +157,7 @@ extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);extern enum mitigation_state kvm_nvhe_sym (spectre_v2_state );extern enum mitigation_state kvm_nvhe_sym (spectre_v4_state );extern enum mitigation_state kvm_nvhe_sym (spectre_bhb_state ); +extern unsigned int kvm_nvhe_sym (kvm_sve_max_vl );extern unsigned int kvm_nvhe_sym (kvm_host_sve_max_vl );extern unsigned long kvm_nvhe_sym (hyp_nr_cpus );extern unsigned int kvm_nvhe_sym (hyp_gicv3_nr_lr );
arch/arm64/kvm/hyp/nvhe/pkvm.c +27 -14
93 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/sys_regs.c +1 -0
@@ -570,6 +570,7 @@ static const struct sys_reg_desc_reset pvm_sys_reg_reset_vals[] = {RESET_UNKNOWN (MAIR_EL1 ),RESET_ZERO (AMAIR_EL1 ),RESET_ZERO (MDSCR_EL1 ), +RESET_ZERO (ZCR_EL1 ),RESET_ZERO (CNTV_CTL_EL0 ),RESET_UNKNOWN (TTBR0_EL1 ), RESET_UNKNOWN(TTBR1_EL1),
arch/arm64/kvm/reset.c +2 -0
@@ -52,6 +52,8 @@ int __init kvm_arm_init_sve(void)if (WARN_ON (kvm_sve_max_vl >VL_ARCH_MAX )) kvm_sve_max_vl =VL_ARCH_MAX ; +kvm_nvhe_sym (kvm_sve_max_vl) = kvm_sve_max_vl; +/* * Don't even try to make use of vector lengths that * aren't available on all CPUs, for now:
Signature · 1 line
2.47.3
Fuad Tabba
reviewerPATCH 3/4 KVM: arm64: Advertise ID_AA64ZFR0_EL1 to protected VMs
In reply to Fuad TabbaEL2 builds a protected guest's ID registers from the host's sanitised values, most of them capped per field. It has no case for ID_AA64ZFR0_EL1, so the register reads as 0, and a protected guest with SVE wouldn't see any of the SVE extensions the CPUs implement.
Keep a copy of the host's sanitised ID_AA64ZFR0_EL1 at EL2, and advertise it when the guest has SVE, as KVM does for a non-protected guest. Cap each field at the highest value the sysreg file defines.
--- arch/arm64/include/asm/kvm_hyp.h | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/hyp/nvhe/sys_regs.c | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+)
arch/arm64/include/asm/kvm_hyp.h +1 -0
@@ -144,6 +144,7 @@ void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt);extern u64 kvm_nvhe_sym (id_aa64pfr0_el1_sys_val );extern u64 kvm_nvhe_sym (id_aa64pfr1_el1_sys_val );extern u64 kvm_nvhe_sym (id_aa64pfr2_el1_sys_val ); +extern u64 kvm_nvhe_sym (id_aa64zfr0_el1_sys_val );extern u64 kvm_nvhe_sym (id_aa64isar0_el1_sys_val );extern u64 kvm_nvhe_sym (id_aa64isar1_el1_sys_val );extern u64 kvm_nvhe_sym (id_aa64isar2_el1_sys_val );
arch/arm64/kvm/arm.c +1 -0
@@ -2827,6 +2827,7 @@ static void kvm_hyp_init_symbols(void)kvm_nvhe_sym (id_aa64pfr0_el1_sys_val) =get_hyp_id_aa64pfr0_el1 ();kvm_nvhe_sym (id_aa64pfr1_el1_sys_val) =read_sanitised_ftr_reg (SYS_ID_AA64PFR1_EL1 );kvm_nvhe_sym (id_aa64pfr2_el1_sys_val) =read_sanitised_ftr_reg (SYS_ID_AA64PFR2_EL1 ); +kvm_nvhe_sym (id_aa64zfr0_el1_sys_val) =read_sanitised_ftr_reg (SYS_ID_AA64ZFR0_EL1 );kvm_nvhe_sym (id_aa64isar0_el1_sys_val) =read_sanitised_ftr_reg (SYS_ID_AA64ISAR0_EL1 );kvm_nvhe_sym (id_aa64isar1_el1_sys_val) =read_sanitised_ftr_reg (SYS_ID_AA64ISAR1_EL1 );kvm_nvhe_sym (id_aa64isar2_el1_sys_val) =read_sanitised_ftr_reg (SYS_ID_AA64ISAR2_EL1 );
arch/arm64/kvm/hyp/nvhe/sys_regs.c +19 -0
@@ -21,6 +21,7 @@u64 id_aa64pfr0_el1_sys_val;u64 id_aa64pfr1_el1_sys_val;u64 id_aa64pfr2_el1_sys_val; +u64 id_aa64zfr0_el1_sys_val;u64 id_aa64isar0_el1_sys_val;u64 id_aa64isar1_el1_sys_val;u64 id_aa64isar2_el1_sys_val;
@@ -114,6 +115,22 @@ static const struct pvm_ftr_bits pvmid_aa64pfr2[] = {FEAT_END }; +static const struct pvm_ftr_bits pvmid_aa64zfr0[] = { +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 , SVEver, SVE2p3, vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 ,AES ,AES2 , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 , EltPerm,IMP , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 , BitPerm,IMP , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 ,BF16 ,EBF16 , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 ,B16B16 ,B16MM , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 ,SHA3 ,IMP , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 ,SM4 ,IMP , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 ,I8MM ,IMP , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 ,F16MM ,IMP , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 ,F32MM ,IMP , vm_has_sve), +MAX_FEAT_FUNC (ID_AA64ZFR0_EL1 ,F64MM ,IMP , vm_has_sve), +FEAT_END +}; +static const struct pvm_ftr_bits pvmid_aa64mmfr0[] = {MAX_FEAT_ENUM (ID_AA64MMFR0_EL1 ,PARANGE ,40 ),MAX_FEAT_ENUM (ID_AA64MMFR0_EL1 ,ASIDBITS ,16 ),
@@ -232,6 +249,8 @@ static u64 pvm_calc_id_reg(const struct kvm_vcpu *vcpu, u32 id)return get_restricted_features (vcpu, id_aa64pfr1_el1_sys_val, pvmid_aa64pfr1);case SYS_ID_AA64PFR2_EL1 :return get_restricted_features (vcpu, id_aa64pfr2_el1_sys_val, pvmid_aa64pfr2); +case SYS_ID_AA64ZFR0_EL1 : +return get_restricted_features (vcpu, id_aa64zfr0_el1_sys_val, pvmid_aa64zfr0);case SYS_ID_AA64ISAR0_EL1 :return id_aa64isar0_el1_sys_val;case SYS_ID_AA64ISAR1_EL1 :
Signature · 1 line
2.47.3
Fuad Tabba
reviewerPATCH 4/4 KVM: arm64: Allow protected VMs to use SVE
In reply to Fuad TabbaNow that EL2 owns a protected vCPU's SVE state and advertises ID_AA64ZFR0_EL1, allow KVM_CAP_ARM_SVE for a protected VM, which lets KVM_ARM_VCPU_INIT take KVM_ARM_VCPU_SVE.
A protected guest's SVE access trap injects an UNDEF. An SVE instruction takes the SVE trap rather than the FP one, so a guest whose first vector instruction is an SVE one would get an UNDEF instead of the lazy FP switch. Handle the SVE trap as the FP trap is:kvm_handle_pvm_fpsimd() switches the state for a vCPU with SVE and still injects the UNDEF for one without.
Update the documentation to match.
--- Documentation/virt/kvm/arm/pkvm.rst | 6 +++--- arch/arm64/include/asm/kvm_pkvm.h | 1 + arch/arm64/kvm/hyp/nvhe/switch.c | 1 + arch/arm64/kvm/hyp/nvhe/sys_regs.c | 2 -- 4 files changed, 5 insertions(+), 5 deletions(-)
Documentation/virt/kvm/arm/pkvm.rst +3 -3
@@ -134,9 +134,9 @@ A protected VM boots from a single primary vCPU. Before the first * Set the primary vCPU's boot state with ``KVM_SET_ONE_REG``: the kernel entry address in ``PC`` and the DTB pointer in ``x0``. -``PC`` and ``x0`` are the only registers EL2 takes from the host. Other -pre-run writes are accepted, but the guest starts from the state the -hypervisor resets it to. +``PC``, ``x0`` and the SVE vector lengths (``KVM_REG_ARM64_SVE_VLS``) are +the only registers EL2 takes from the host. Other pre-run writes are +accepted, but the guest starts from the state the hypervisor resets it to. Secondary vCPUs are started by the guest itself through PSCI ``CPU_ON`` (see `Power state`_), which supplies their entry point and context ID.
arch/arm64/include/asm/kvm_pkvm.h +1 -0
@@ -40,6 +40,7 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)case KVM_CAP_MAX_VCPU_ID :case KVM_CAP_MSI_DEVID :case KVM_CAP_ARM_VM_IPA_SIZE : +case KVM_CAP_ARM_SVE :case KVM_CAP_ARM_PTRAUTH_ADDRESS :case KVM_CAP_ARM_PTRAUTH_GENERIC :case KVM_CAP_ONE_REG :
arch/arm64/kvm/hyp/nvhe/switch.c +1 -0
@@ -230,6 +230,7 @@ static const exit_handler_fn pvm_exit_handlers[] = { [ESR_ELx_EC_WFx] = kvm_handle_pvm_wfx, [ESR_ELx_EC_HVC64] = kvm_handle_pvm_hvc64, [ESR_ELx_EC_SYS64] = kvm_handle_pvm_sys64, + [ESR_ELx_EC_SVE] = kvm_handle_pvm_fpsimd, [ESR_ELx_EC_FP_ASIMD] = kvm_handle_pvm_fpsimd, [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low, [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low,
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -2
@@ -502,8 +502,6 @@ static const struct sys_reg_desc pvm_sys_reg_descs[] = {ID_UNALLOCATED (7 ,6 ),ID_UNALLOCATED (7 ,7 ), -/* Scalable Vector Registers are restricted. */ -HOST_HANDLED (SYS_ICC_PMR_EL1 ), RAZ_WI(SYS_ERRIDR_EL1),
Signature · 1 line
2.47.3
Re: [PATCH 2/4] KVM: arm64: Allocate a protected vCPU's SVE state at EL2
In reply to Fuad Tabbaarch/arm64/kvm/hyp/nvhe/pkvm.c int pkvm_reset_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) · line 1258
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +3
-0@@ -1243,6 +1253,9 @@ int pkvm_reset_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) PSCI_0_2_AFFINITY_LEVEL_ON) != PSCI_0_2_AFFINITY_LEVEL_ON_PENDING)return -ECANCELED ; +if (vcpu_has_sve (&hyp_vcpu->vcpu)) +memset (hyp_vcpu->vcpu.arch.sve_state,0 ,vcpu_sve_state_size (&hyp_vcpu->vcpu)); +
Why wasn't this line around before?
Quoted 31 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0kvm_reset_vcpu_core (&hyp_vcpu->vcpu);kvm_reset_pvm_sys_regs (&hyp_vcpu->vcpu);arch/arm64/kvm/hyp/nvhe/sys_regs.c +1
-0@@ -570,6 +570,7 @@ static const struct sys_reg_desc_reset pvm_sys_reg_reset_vals[] = {RESET_UNKNOWN (MAIR_EL1 ),RESET_ZERO (AMAIR_EL1 ),RESET_ZERO (MDSCR_EL1 ), +RESET_ZERO (ZCR_EL1 ),RESET_ZERO (CNTV_CTL_EL0 ),RESET_UNKNOWN (TTBR0_EL1 ), RESET_UNKNOWN(TTBR1_EL1),arch/arm64/kvm/reset.c +2
-0@@ -52,6 +52,8 @@ int __init kvm_arm_init_sve(void)if (WARN_ON (kvm_sve_max_vl >VL_ARCH_MAX )) kvm_sve_max_vl =VL_ARCH_MAX ; +kvm_nvhe_sym (kvm_sve_max_vl) = kvm_sve_max_vl; +/* * Don't even try to make use of vector lengths that * aren't available on all CPUs, for now: Signature · 1 line
2.47.3
Signature · 1 line
Vincent
Quoted 36 lines · Fuad Tabba
Hi folks,
This series adds SVE support for protected VMs. EL2 allocates a protected vCPU's SVE state from its own heap rather than pinning the host's buffer, so the host never sees the guest's vector registers. It then advertises
ID_AA64ZFR0_EL1to protected VMs, and routes the SVE trap to the lazy FP switch. The first patch is a small tidy-up of the pKVM FP sync code that saves a guest's SVE state.v2 of the pKVM selftests series [1] will add a selftest for this.
Based on Linux 7.4-rc1 (
6628e7d4c6b25).Cheers,
/fuad[1] https://lore.kernel.org/all/[email protected]/
Fuad Tabba (4):
KVM: arm64: Remove redundantkern_hyp_va()calls from the pKVM FP sync
KVM: arm64: Allocate a protected vCPU's SVE state at EL2
KVM: arm64: AdvertiseID_AA64ZFR0_EL1to protected VMs
KVM: arm64: Allow protected VMs to use SVEDocumentation/virt/kvm/arm/pkvm.rst | 6 ++--- arch/arm64/include/asm/kvm_hyp.h | 2 ++ arch/arm64/include/asm/kvm_pkvm.h | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 +-- arch/arm64/kvm/hyp/nvhe/pkvm.c | 41 +++++++++++++++++++---------- arch/arm64/kvm/hyp/nvhe/switch.c | 1 + arch/arm64/kvm/hyp/nvhe/sys_regs.c | 22 ++++++++++++++-- arch/arm64/kvm/reset.c | 2 ++ 9 files changed, 59 insertions(+), 21 deletions(-)
Quoted 4 lines
base-commit:
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4Signature · 1 line
2.47.3
For the whole series
Signature · 1 line
Vincent
Oliver Upton
maintainerRe: [PATCH 2/4] KVM: arm64: Allocate a protected vCPU's SVE state at EL2
In reply to Vincent DonnefortQuoted 9 lines · Vincent Donnefort
arch/arm64/kvm/hyp/nvhe/pkvm.c int pkvm_reset_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) · line 1258
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +3
-0@@ -1243,6 +1253,9 @@ int pkvm_reset_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) PSCI_0_2_AFFINITY_LEVEL_ON) != PSCI_0_2_AFFINITY_LEVEL_ON_PENDING)return -ECANCELED ; +if (vcpu_has_sve (&hyp_vcpu->vcpu)) +memset (hyp_vcpu->vcpu.arch.sve_state,0 ,vcpu_sve_state_size (&hyp_vcpu->vcpu)); +Why wasn't this line around before?
Prior to this series SVE was only supported in non-protected guests, so the host was responsible for resetting SVE state.
Now that we have pVM support we need to reset state for protected vCPUs.
Thanks,
Oliver
Quoted 34 lines
Quoted 31 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0kvm_reset_vcpu_core (&hyp_vcpu->vcpu);kvm_reset_pvm_sys_regs (&hyp_vcpu->vcpu);arch/arm64/kvm/hyp/nvhe/sys_regs.c +1
-0@@ -570,6 +570,7 @@ static const struct sys_reg_desc_reset pvm_sys_reg_reset_vals[] = {RESET_UNKNOWN (MAIR_EL1 ),RESET_ZERO (AMAIR_EL1 ),RESET_ZERO (MDSCR_EL1 ), +RESET_ZERO (ZCR_EL1 ),RESET_ZERO (CNTV_CTL_EL0 ),RESET_UNKNOWN (TTBR0_EL1 ), RESET_UNKNOWN(TTBR1_EL1),arch/arm64/kvm/reset.c +2
-0@@ -52,6 +52,8 @@ int __init kvm_arm_init_sve(void)if (WARN_ON (kvm_sve_max_vl >VL_ARCH_MAX )) kvm_sve_max_vl =VL_ARCH_MAX ; +kvm_nvhe_sym (kvm_sve_max_vl) = kvm_sve_max_vl; +/* * Don't even try to make use of vector lengths that * aren't available on all CPUs, for now: Signature · 1 line
2.47.3Signature · 1 line
Vincent
Fuad Tabba
reviewerHi folks,
I've folded this into the series that advertises the rest of the protected VM features [1], so please consider this posting superseded.
Cheers,
/fuad