Skip to content

Commit 41d6028

Browse files
Fuad TabbaMarc Zyngier
authored andcommitted
KVM: arm64: Convert the SVE guest vcpu flag to a vm flag
The vcpu flag GUEST_HAS_SVE is per-vcpu, but it is based on what is now a per-vm feature. Make the flag per-vm. Signed-off-by: Fuad Tabba <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Marc Zyngier <[email protected]>
1 parent c5c1763 commit 41d6028

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

arch/arm64/include/asm/kvm_emulate.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ static __always_inline void kvm_write_cptr_el2(u64 val)
620620
}
621621

622622
/* Resets the value of cptr_el2 when returning to the host. */
623-
static __always_inline void kvm_reset_cptr_el2(struct kvm_vcpu *vcpu)
623+
static __always_inline void __kvm_reset_cptr_el2(struct kvm *kvm)
624624
{
625625
u64 val;
626626

@@ -631,14 +631,14 @@ static __always_inline void kvm_reset_cptr_el2(struct kvm_vcpu *vcpu)
631631
} else if (has_hvhe()) {
632632
val = CPACR_ELx_FPEN;
633633

634-
if (!vcpu_has_sve(vcpu) || !guest_owns_fp_regs())
634+
if (!kvm_has_sve(kvm) || !guest_owns_fp_regs())
635635
val |= CPACR_ELx_ZEN;
636636
if (cpus_have_final_cap(ARM64_SME))
637637
val |= CPACR_ELx_SMEN;
638638
} else {
639639
val = CPTR_NVHE_EL2_RES1;
640640

641-
if (vcpu_has_sve(vcpu) && guest_owns_fp_regs())
641+
if (kvm_has_sve(kvm) && guest_owns_fp_regs())
642642
val |= CPTR_EL2_TZ;
643643
if (!cpus_have_final_cap(ARM64_SME))
644644
val |= CPTR_EL2_TSM;
@@ -647,6 +647,12 @@ static __always_inline void kvm_reset_cptr_el2(struct kvm_vcpu *vcpu)
647647
kvm_write_cptr_el2(val);
648648
}
649649

650+
#ifdef __KVM_NVHE_HYPERVISOR__
651+
#define kvm_reset_cptr_el2(v) __kvm_reset_cptr_el2(kern_hyp_va((v)->kvm))
652+
#else
653+
#define kvm_reset_cptr_el2(v) __kvm_reset_cptr_el2((v)->kvm)
654+
#endif
655+
650656
/*
651657
* Returns a 'sanitised' view of CPTR_EL2, translating from nVHE to the VHE
652658
* format if E2H isn't set.

arch/arm64/include/asm/kvm_host.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ struct kvm_arch {
331331
#define KVM_ARCH_FLAG_ID_REGS_INITIALIZED 7
332332
/* Fine-Grained UNDEF initialised */
333333
#define KVM_ARCH_FLAG_FGU_INITIALIZED 8
334+
/* SVE exposed to guest */
335+
#define KVM_ARCH_FLAG_GUEST_HAS_SVE 9
334336
unsigned long flags;
335337

336338
/* VM-wide vCPU feature set */
@@ -862,12 +864,10 @@ struct kvm_vcpu_arch {
862864
#define vcpu_set_flag(v, ...) __vcpu_set_flag((v), __VA_ARGS__)
863865
#define vcpu_clear_flag(v, ...) __vcpu_clear_flag((v), __VA_ARGS__)
864866

865-
/* SVE exposed to guest */
866-
#define GUEST_HAS_SVE __vcpu_single_flag(cflags, BIT(0))
867+
/* KVM_ARM_VCPU_INIT completed */
868+
#define VCPU_INITIALIZED __vcpu_single_flag(cflags, BIT(0))
867869
/* SVE config completed */
868870
#define VCPU_SVE_FINALIZED __vcpu_single_flag(cflags, BIT(1))
869-
/* KVM_ARM_VCPU_INIT completed */
870-
#define VCPU_INITIALIZED __vcpu_single_flag(cflags, BIT(2))
871871

872872
/* Exception pending */
873873
#define PENDING_EXCEPTION __vcpu_single_flag(iflags, BIT(0))
@@ -956,8 +956,14 @@ struct kvm_vcpu_arch {
956956
KVM_GUESTDBG_USE_HW | \
957957
KVM_GUESTDBG_SINGLESTEP)
958958

959-
#define vcpu_has_sve(vcpu) (system_supports_sve() && \
960-
vcpu_get_flag(vcpu, GUEST_HAS_SVE))
959+
#define kvm_has_sve(kvm) (system_supports_sve() && \
960+
test_bit(KVM_ARCH_FLAG_GUEST_HAS_SVE, &(kvm)->arch.flags))
961+
962+
#ifdef __KVM_NVHE_HYPERVISOR__
963+
#define vcpu_has_sve(vcpu) kvm_has_sve(kern_hyp_va((vcpu)->kvm))
964+
#else
965+
#define vcpu_has_sve(vcpu) kvm_has_sve((vcpu)->kvm)
966+
#endif
961967

962968
#ifdef CONFIG_ARM64_PTR_AUTH
963969
#define vcpu_has_ptrauth(vcpu) \

arch/arm64/kvm/hyp/nvhe/pkvm.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,13 @@ void pkvm_put_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
248248
static void pkvm_init_features_from_host(struct pkvm_hyp_vm *hyp_vm, const struct kvm *host_kvm)
249249
{
250250
struct kvm *kvm = &hyp_vm->kvm;
251+
unsigned long host_arch_flags = READ_ONCE(host_kvm->arch.flags);
251252
DECLARE_BITMAP(allowed_features, KVM_VCPU_MAX_FEATURES);
252253

253254
/* No restrictions for non-protected VMs. */
254255
if (!kvm_vm_is_protected(kvm)) {
256+
hyp_vm->kvm.arch.flags = host_arch_flags;
257+
255258
bitmap_copy(kvm->arch.vcpu_features,
256259
host_kvm->arch.vcpu_features,
257260
KVM_VCPU_MAX_FEATURES);
@@ -271,8 +274,10 @@ static void pkvm_init_features_from_host(struct pkvm_hyp_vm *hyp_vm, const struc
271274
if (kvm_pvm_ext_allowed(KVM_CAP_ARM_PTRAUTH_GENERIC))
272275
set_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, allowed_features);
273276

274-
if (kvm_pvm_ext_allowed(KVM_CAP_ARM_SVE))
277+
if (kvm_pvm_ext_allowed(KVM_CAP_ARM_SVE)) {
275278
set_bit(KVM_ARM_VCPU_SVE, allowed_features);
279+
kvm->arch.flags |= host_arch_flags & BIT(KVM_ARCH_FLAG_GUEST_HAS_SVE);
280+
}
276281

277282
bitmap_and(kvm->arch.vcpu_features, host_kvm->arch.vcpu_features,
278283
allowed_features, KVM_VCPU_MAX_FEATURES);
@@ -308,10 +313,8 @@ static void pkvm_vcpu_init_sve(struct pkvm_hyp_vcpu *hyp_vcpu, struct kvm_vcpu *
308313
{
309314
struct kvm_vcpu *vcpu = &hyp_vcpu->vcpu;
310315

311-
if (!vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE)) {
312-
vcpu_clear_flag(vcpu, GUEST_HAS_SVE);
316+
if (!vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE))
313317
vcpu_clear_flag(vcpu, VCPU_SVE_FINALIZED);
314-
}
315318
}
316319

317320
static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu,

arch/arm64/kvm/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static void kvm_vcpu_enable_sve(struct kvm_vcpu *vcpu)
8585
* KVM_REG_ARM64_SVE_VLS. Allocation is deferred until
8686
* kvm_arm_vcpu_finalize(), which freezes the configuration.
8787
*/
88-
vcpu_set_flag(vcpu, GUEST_HAS_SVE);
88+
set_bit(KVM_ARCH_FLAG_GUEST_HAS_SVE, &vcpu->kvm->arch.flags);
8989
}
9090

9191
/*

0 commit comments

Comments
 (0)