]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: arm64: Add helper for writing ID regs
authorOliver Upton <oliver.upton@linux.dev>
Wed, 19 Jun 2024 17:40:30 +0000 (17:40 +0000)
committerOliver Upton <oliver.upton@linux.dev>
Thu, 20 Jun 2024 17:16:44 +0000 (17:16 +0000)
Replace the remaining usage of IDREG() with a new helper for setting the
value of a feature ID register, with the benefit of cramming in some
extra sanity checks.

Reviewed-by: Sebastian Ott <sebott@redhat.com>
Link: https://lore.kernel.org/r/20240619174036.483943-5-oliver.upton@linux.dev
Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
arch/arm64/include/asm/kvm_host.h
arch/arm64/kvm/nested.c
arch/arm64/kvm/sys_regs.c

index 1201af636551b1d27b3ea949560678584e5a2209..74e7c29364ee1d8da1f1f3300156944d71b5fe38 100644 (file)
@@ -327,7 +327,6 @@ struct kvm_arch {
         */
 #define IDREG_IDX(id)          (((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
 #define IDX_IDREG(idx)         sys_reg(3, 0, 0, ((idx) >> 3) + 1, (idx) & Op2_mask)
-#define IDREG(kvm, id)         ((kvm)->arch.id_regs[IDREG_IDX(id)])
 #define KVM_ARM_ID_REG_NUM     (IDREG_IDX(sys_reg(3, 0, 0, 7, 7)) + 1)
        u64 id_regs[KVM_ARM_ID_REG_NUM];
 
@@ -1346,6 +1345,8 @@ static inline u64 *__vm_id_reg(struct kvm_arch *ka, u32 reg)
 #define kvm_read_vm_id_reg(kvm, reg)                                   \
        ({ u64 __val = *__vm_id_reg(&(kvm)->arch, reg); __val; })
 
+void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
+
 #define __expand_field_sign_unsigned(id, fld, val)                     \
        ((u64)SYS_FIELD_VALUE(id, fld, val))
 
index 6813c7c7f00ab2eedf9d04a9a84bfb5f4858ca77..5db5bc9dd2905d47b626af5c4dc2b42bc25b138f 100644 (file)
@@ -203,8 +203,8 @@ int kvm_init_nv_sysregs(struct kvm *kvm)
        }
 
        for (int i = 0; i < KVM_ARM_ID_REG_NUM; i++)
-               kvm->arch.id_regs[i] = limit_nv_id_reg(IDX_IDREG(i),
-                                                      kvm->arch.id_regs[i]);
+               kvm_set_vm_id_reg(kvm, IDX_IDREG(i), limit_nv_id_reg(IDX_IDREG(i),
+                                                                    kvm->arch.id_regs[i]));
 
        /* VTTBR_EL2 */
        res0 = res1 = 0;
index 0692a109fd4d3858545f404744656f038fbd86b0..8e33589053717b3d900e9d9bfabf0e54c81789b5 100644 (file)
@@ -1851,7 +1851,7 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 
        ret = arm64_check_features(vcpu, rd, val);
        if (!ret)
-               IDREG(vcpu->kvm, id) = val;
+               kvm_set_vm_id_reg(vcpu->kvm, id, val);
 
        mutex_unlock(&vcpu->kvm->arch.config_lock);
 
@@ -1867,6 +1867,18 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
        return ret;
 }
 
+void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val)
+{
+       u64 *p = __vm_id_reg(&kvm->arch, reg);
+
+       lockdep_assert_held(&kvm->arch.config_lock);
+
+       if (KVM_BUG_ON(kvm_vm_has_ran_once(kvm) || !p, kvm))
+               return;
+
+       *p = val;
+}
+
 static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
                       u64 *val)
 {
@@ -3549,8 +3561,7 @@ static void reset_vm_ftr_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc
        if (test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))
                return;
 
-       lockdep_assert_held(&kvm->arch.config_lock);
-       IDREG(kvm, id) = reg->reset(vcpu, reg);
+       kvm_set_vm_id_reg(kvm, id, reg->reset(vcpu, reg));
 }
 
 static void reset_vcpu_ftr_id_reg(struct kvm_vcpu *vcpu,