Skip to content

Commit 23711a5

Browse files
author
Marc Zyngier
committed
KVM: arm64: Allow setting of ID_AA64PFR0_EL1.CSV2 from userspace
We now expose ID_AA64PFR0_EL1.CSV2=1 to guests running on hosts that are immune to Spectre-v2, but that don't have this field set, most likely because they predate the specification. However, this prevents the migration of guests that have started on a host the doesn't fake this CSV2 setting to one that does, as KVM rejects the write to ID_AA64PFR0_EL2 on the grounds that it isn't what is already there. In order to fix this, allow userspace to set this field as long as this doesn't result in a promising more than what is already there (setting CSV2 to 0 is acceptable, but setting it to 1 when it is already set to 0 isn't). Fixes: e102623 ("KVM: arm64: Set CSV2 for guests on hardware unaffected by Spectre-v2") Reported-by: Peng Liang <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Acked-by: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4f6b838 commit 23711a5

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ struct kvm_arch {
118118
*/
119119
unsigned long *pmu_filter;
120120
unsigned int pmuver;
121+
122+
u8 pfr0_csv2;
121123
};
122124

123125
struct kvm_vcpu_fault_info {

arch/arm64/kvm/arm.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ static int kvm_arm_default_max_vcpus(void)
102102
return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
103103
}
104104

105+
static void set_default_csv2(struct kvm *kvm)
106+
{
107+
/*
108+
* The default is to expose CSV2 == 1 if the HW isn't affected.
109+
* Although this is a per-CPU feature, we make it global because
110+
* asymmetric systems are just a nuisance.
111+
*
112+
* Userspace can override this as long as it doesn't promise
113+
* the impossible.
114+
*/
115+
if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
116+
kvm->arch.pfr0_csv2 = 1;
117+
}
118+
105119
/**
106120
* kvm_arch_init_vm - initializes a VM data structure
107121
* @kvm: pointer to the KVM struct
@@ -127,6 +141,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
127141
/* The maximum number of VCPUs is limited by the host's GIC model */
128142
kvm->arch.max_vcpus = kvm_arm_default_max_vcpus();
129143

144+
set_default_csv2(kvm);
145+
130146
return ret;
131147
out_free_stage2_pgd:
132148
kvm_free_stage2_pgd(&kvm->arch.mmu);

arch/arm64/kvm/sys_regs.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,9 +1128,8 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu,
11281128
if (!vcpu_has_sve(vcpu))
11291129
val &= ~(0xfUL << ID_AA64PFR0_SVE_SHIFT);
11301130
val &= ~(0xfUL << ID_AA64PFR0_AMU_SHIFT);
1131-
if (!(val & (0xfUL << ID_AA64PFR0_CSV2_SHIFT)) &&
1132-
arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
1133-
val |= (1UL << ID_AA64PFR0_CSV2_SHIFT);
1131+
val &= ~(0xfUL << ID_AA64PFR0_CSV2_SHIFT);
1132+
val |= ((u64)vcpu->kvm->arch.pfr0_csv2 << ID_AA64PFR0_CSV2_SHIFT);
11341133
} else if (id == SYS_ID_AA64PFR1_EL1) {
11351134
val &= ~(0xfUL << ID_AA64PFR1_MTE_SHIFT);
11361135
} else if (id == SYS_ID_AA64ISAR1_EL1 && !vcpu_has_ptrauth(vcpu)) {
@@ -1213,6 +1212,40 @@ static unsigned int sve_visibility(const struct kvm_vcpu *vcpu,
12131212
return REG_HIDDEN;
12141213
}
12151214

1215+
static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
1216+
const struct sys_reg_desc *rd,
1217+
const struct kvm_one_reg *reg, void __user *uaddr)
1218+
{
1219+
const u64 id = sys_reg_to_index(rd);
1220+
int err;
1221+
u64 val;
1222+
u8 csv2;
1223+
1224+
err = reg_from_user(&val, uaddr, id);
1225+
if (err)
1226+
return err;
1227+
1228+
/*
1229+
* Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
1230+
* it doesn't promise more than what is actually provided (the
1231+
* guest could otherwise be covered in ectoplasmic residue).
1232+
*/
1233+
csv2 = cpuid_feature_extract_unsigned_field(val, ID_AA64PFR0_CSV2_SHIFT);
1234+
if (csv2 > 1 ||
1235+
(csv2 && arm64_get_spectre_v2_state() != SPECTRE_UNAFFECTED))
1236+
return -EINVAL;
1237+
1238+
/* We can only differ with CSV2, and anything else is an error */
1239+
val ^= read_id_reg(vcpu, rd, false);
1240+
val &= ~(0xFUL << ID_AA64PFR0_CSV2_SHIFT);
1241+
if (val)
1242+
return -EINVAL;
1243+
1244+
vcpu->kvm->arch.pfr0_csv2 = csv2;
1245+
1246+
return 0;
1247+
}
1248+
12161249
/*
12171250
* cpufeature ID register user accessors
12181251
*
@@ -1472,7 +1505,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
14721505

14731506
/* AArch64 ID registers */
14741507
/* CRm=4 */
1475-
ID_SANITISED(ID_AA64PFR0_EL1),
1508+
{ SYS_DESC(SYS_ID_AA64PFR0_EL1), .access = access_id_reg,
1509+
.get_user = get_id_reg, .set_user = set_id_aa64pfr0_el1, },
14761510
ID_SANITISED(ID_AA64PFR1_EL1),
14771511
ID_UNALLOCATED(4,2),
14781512
ID_UNALLOCATED(4,3),

0 commit comments

Comments
 (0)