Skip to contentSkip to conversation

Support writable CPU ID registers from userspace

PATCH Superseded Jing Zhang 4 participants 25 messages Mar 17, 2023 – Mar 29, 2023

Subsystems (3)

v4 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Jing Zhang

author
#0 lore raw

v4 cover Support writable CPU ID registers from userspace

This patchset refactors/adds code to support writable per guest CPU ID feature registers. Part of the code/ideas are from
https://lore.kernel.org/all/[email protected] . No functional change is intended in this patchset. With the new CPU ID feature registers infrastructure, only writtings of ID_AA64PFR0_EL1.[CSV2|CSV3], ID_AA64DFR0_EL1.PMUVer and ID_DFR0_ELF.PerfMon are allowed as KVM does before.

Writable (Configurable) per guest CPU ID feature registers are useful for creating/migrating guest on ARM CPUs with different kinds of features.

---

* v3 -> v4
- Remove IDREG() macro for ID reg access, use simple array access instead
- Rename kvm_arm_read_id_reg_with_encoding() to kvm_arm_read_id_reg()
- Save perfmon value in ID_DFR0_EL1 instead of pmuver
- Update perfmon in ID_DFR0_EL1 and pmuver in ID_AA64DFR0_EL1 atomically
- Remove kvm_vcpu_has_pmu() in macro kvm_pmu_is_3p5()
- Improve ID register sanity checking in kvm_arm_check_idreg_table()

* v2 -> v3
- Rebased to 96a4627dbbd4 (kvmarm/next)
Merge tag ' https://github.com/oupton/linux tags/kvmarm-6.3' from into kvmarm-master/next
- Add id registere emulation entry point function emulate_id_reg
- Fix consistency for ID_AA64DFR0_EL1.PMUVer and ID_DFR0_EL1.PerfMon
- Improve the checking for id register table by ensuring that every entry has the correct id register encoding.
- Addressed other comments from Reiji and Marc.

* v1 -> v2
- Rebase to 7121a2e1d107 (kvmarm/next) Merge branch kvm-arm64/nv-prefix into kvmarm/next
- Address writing issue for PMUVer

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
[3] https://lore.kernel.org/all/[email protected]

---

Jing Zhang (5):
KVM: arm64: Move CPU ID feature registers emulation into a separate
file
KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]
KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer
KVM: arm64: Introduce ID register specific descriptor
KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

Reiji Watanabe (1):
KVM: arm64: Save ID registers' sanitized value per guest

 arch/arm64/include/asm/cpufeature.h |  25 +
 arch/arm64/include/asm/kvm_host.h   |  24 +-
 arch/arm64/kernel/cpufeature.c      |  26 +-
 arch/arm64/kvm/Makefile             |   2 +-
 arch/arm64/kvm/arm.c                |  24 +-
 arch/arm64/kvm/hyp/nvhe/sys_regs.c  |   7 +-
 arch/arm64/kvm/id_regs.c            | 807 ++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c           | 469 +---------------
 arch/arm64/kvm/sys_regs.h           |  43 ++
 include/kvm/arm_pmu.h               |   5 +-
 10 files changed, 930 insertions(+), 502 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c

base-commit: 96a4627dbbd48144a65af936b321701c70876026

Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
author
#1 lore raw

PATCH v4 1/6 KVM: arm64: Move CPU ID feature registers emulation into a separate file

Create a new file id_regs.c for CPU ID feature registers emulation code, which are moved from sys_regs.c and tweak sys_regs code accordingly.

No functional change intended.

---
 arch/arm64/kvm/Makefile   |   2 +-
 arch/arm64/kvm/id_regs.c  | 506 ++++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c | 464 ++--------------------------------
 arch/arm64/kvm/sys_regs.h |  41 +++
 4 files changed, 575 insertions(+), 438 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c
arch/arm64/kvm/Makefile +1 -1
index c0c050e53157..a6a315fcd81e 100644
@@ -13,7 +13,7 @@ obj-$(CONFIG_KVM) += hyp/
 kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 inject_fault.o va_layout.o handle_exit.o \
 	 guest.o debug.o reset.o sys_regs.o stacktrace.o \
-	 vgic-sys-reg-v3.o fpsimd.o pkvm.o \
+	 vgic-sys-reg-v3.o fpsimd.o pkvm.o id_regs.o \
 	 arch_timer.o trng.o vmid.o emulate-nested.o nested.o \
 	 vgic/vgic.o vgic/vgic-init.o \
 	 vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/id_regs.c new file +506 -0

506 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +27 -437

554 lines not on this page · raw message

arch/arm64/kvm/sys_regs.h +41 -0

53 lines not on this page · raw message

Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
maintainer
#2 lore raw

Re: [PATCH v4 1/6] KVM: arm64: Move CPU ID feature registers emulation into a separate file

Quoted 33 lines · Jing Zhang

Create a new file id_regs.c for CPU ID feature registers emulation code, which are moved from sys_regs.c and tweak sys_regs code accordingly.

No functional change intended.

---
 arch/arm64/kvm/Makefile   |   2 +-
 arch/arm64/kvm/id_regs.c  | 506 ++++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c | 464 ++--------------------------------
 arch/arm64/kvm/sys_regs.h |  41 +++
 4 files changed, 575 insertions(+), 438 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c
arch/arm64/kvm/Makefile +1 -1
index c0c050e53157..a6a315fcd81e 100644
@@ -13,7 +13,7 @@ obj-$(CONFIG_KVM) += hyp/
 kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 inject_fault.o va_layout.o handle_exit.o \
 	 guest.o debug.o reset.o sys_regs.o stacktrace.o \
-	 vgic-sys-reg-v3.o fpsimd.o pkvm.o \
+	 vgic-sys-reg-v3.o fpsimd.o pkvm.o id_regs.o \
 	 arch_timer.o trng.o vmid.o emulate-nested.o nested.o \
 	 vgic/vgic.o vgic/vgic-init.o \
 	 vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/id_regs.c new file +0 -0
new file mode 100644
index 000000000000..08b738852955

[...]

Quoted 24 lines
arch/arm64/kvm/id_regs.c new file +24 -0
+/**
+ * emulate_id_reg - Emulate a guest access to an AArch64 CPU ID feature register
+ * @vcpu: The VCPU pointer
+ * @params: Decoded system register parameters
+ *
+ * Return: true if the ID register access was successful, false otherwise.
+ */
+int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
+{
+	const struct sys_reg_desc *r;
+
+	r = find_reg(params, id_reg_descs, ARRAY_SIZE(id_reg_descs));
+
+	if (likely(r)) {
+		perform_access(vcpu, params, r);
+	} else {
+		print_sys_reg_msg(params,
+				  "Unsupported guest id_reg access at: %lx [%08lx]\n",
+				  *vcpu_pc(vcpu), *vcpu_cpsr(vcpu));
+		kvm_inject_undefined(vcpu);
+	}
+
+	return 1;
+}

arch/arm64/kvm/id_regs.c line 472

Quoted 10 lines
arch/arm64/kvm/id_regs.c new file +10 -0
+
+
+void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu)
+{
+	unsigned long i;
+
+	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++)
+		if (id_reg_descs[i].reset)
+			id_reg_descs[i].reset(vcpu, &id_reg_descs[i]);
+}

What does this mean? None of the idregs have a reset function, given that they are global. Maybe this will make sense in the following patches, but definitely not here.

Quoted 7 lines
arch/arm64/kvm/id_regs.c new file +7 -0
+
+int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
+{
+	return kvm_sys_reg_get_user(vcpu, reg,
+				    id_reg_descs, ARRAY_SIZE(id_reg_descs));
+}
+

arch/arm64/kvm/id_regs.c line 489

Quoted 10 lines
arch/arm64/kvm/id_regs.c new file +10 -0
+int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
+{
+	return kvm_sys_reg_set_user(vcpu, reg,
+				    id_reg_descs, ARRAY_SIZE(id_reg_descs));
+}
+
+bool kvm_arm_check_idreg_table(void)
+{
+	return check_sysreg_table(id_reg_descs, ARRAY_SIZE(id_reg_descs), false);
+}

All these helpers are called from sys_regs.c and directly call back into it. Why not simply have a helper that gets the base and size of the array, and stick to pure common code?

Quoted 7 lines
arch/arm64/kvm/id_regs.c new file +7 -0
+
+int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
+{
+	const struct sys_reg_desc *i2, *end2;
+	unsigned int total = 0;
+	int err;
+

arch/arm64/kvm/id_regs.c line 506

Quoted 10 lines
arch/arm64/kvm/id_regs.c new file +10 -0
+	i2 = id_reg_descs;
+	end2 = id_reg_descs + ARRAY_SIZE(id_reg_descs);
+
+	while (i2 != end2) {
+		err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
+		if (err)
+			return err;
+	}
+	return total;
+}

This is an exact copy of walk_sys_regs. Surely this can be made common code.

[...]

Quoted 20 lines
arch/arm64/kvm/id_regs.c new file +5 -0
@@ -2912,6 +2482,8 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
 {
 	unsigned long i;
 
+	kvm_arm_reset_id_regs(vcpu);
+
 	for (i = 0; i < ARRAY_SIZE(sys_reg_descs); i++)
 		if (sys_reg_descs[i].reset)
 			sys_reg_descs[i].reset(vcpu, &sys_reg_descs[i]);
@@ -2932,6 +2504,9 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
 	params = esr_sys64_to_params(esr);
 	params.regval = vcpu_get_reg(vcpu, Rt);
 
+	if (is_id_reg(reg_to_encoding(&params)))
+		return emulate_id_reg(vcpu, &params);
+
 	if (!emulate_sys_reg(vcpu, &params))
 		return 1;
 

arch/arm64/kvm/id_regs.c int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg · line 2738

Quoted 4 lines
arch/arm64/kvm/id_regs.c new file +1 -0
@@ -3160,6 +2735,10 @@ int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg
 	if (err != -ENOENT)
 		return err;
 
+	err = kvm_arm_get_id_reg(vcpu, reg);

Why not check for the encoding here? or in the helpers? It feels that this is an overhead that would be easy to reduce, given that we have fewer idregs than normal sysregs.

arch/arm64/kvm/id_regs.c int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg · line 2786

Quoted 11 lines
arch/arm64/kvm/id_regs.c new file +4 -0
+	if (err != -ENOENT)
+		return err;
+
 	return kvm_sys_reg_get_user(vcpu, reg,
 				    sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
 }
@@ -3204,6 +2783,10 @@ int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg
 	if (err != -ENOENT)
 		return err;
 
+	err = kvm_arm_set_id_reg(vcpu, reg);

Same here.

Quoted 54 lines
arch/arm64/kvm/id_regs.c new file +14 -4
+	if (err != -ENOENT)
+		return err;
+
 	return kvm_sys_reg_set_user(vcpu, reg,
 				    sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
 }
@@ -3250,10 +2833,10 @@ static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)
 	return true;
 }
 
-static int walk_one_sys_reg(const struct kvm_vcpu *vcpu,
-			    const struct sys_reg_desc *rd,
-			    u64 __user **uind,
-			    unsigned int *total)
+int walk_one_sys_reg(const struct kvm_vcpu *vcpu,
+		     const struct sys_reg_desc *rd,
+		     u64 __user **uind,
+		     unsigned int *total)
 {
 	/*
 	 * Ignore registers we trap but don't save,
@@ -3294,6 +2877,7 @@ unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu)
 {
 	return ARRAY_SIZE(invariant_sys_regs)
 		+ num_demux_regs()
+		+ kvm_arm_walk_id_regs(vcpu, (u64 __user *)NULL)
 		+ walk_sys_regs(vcpu, (u64 __user *)NULL);
 }
 
@@ -3309,6 +2893,11 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 		uindices++;
 	}
 
+	err = kvm_arm_walk_id_regs(vcpu, uindices);
+	if (err < 0)
+		return err;
+	uindices += err;
+
 	err = walk_sys_regs(vcpu, uindices);
 	if (err < 0)
 		return err;
@@ -3323,6 +2912,7 @@ int __init kvm_sys_reg_table_init(void)
 	unsigned int i;
 
 	/* Make sure tables are unique and in order. */
+	valid &= kvm_arm_check_idreg_table();
 	valid &= check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs), false);
 	valid &= check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs), true);
 	valid &= check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs), true);
arch/arm64/kvm/sys_regs.h +0 -0
index 6b11f2cc7146..ad41305348f7 100644

arch/arm64/kvm/sys_regs.h find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[], · line 217

Quoted 8 lines
arch/arm64/kvm/sys_regs.h +5 -0
@@ -210,6 +210,35 @@ find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
 	return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
 }
 
+static inline unsigned int raz_visibility(const struct kvm_vcpu *vcpu,
+					  const struct sys_reg_desc *r)
+{
+	return REG_RAZ;
+}

No, please. This is used as a function pointer. You now potentially force the compiler to emit as many copy of this as there are pointers.

arch/arm64/kvm/sys_regs.h find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[], · line 227

Quoted 10 lines
arch/arm64/kvm/sys_regs.h +10 -0
+
+static inline bool write_to_read_only(struct kvm_vcpu *vcpu,
+				      struct sys_reg_params *params,
+				      const struct sys_reg_desc *r)
+{
+	WARN_ONCE(1, "Unexpected sys_reg write to read-only register\n");
+	print_sys_reg_instr(params);
+	kvm_inject_undefined(vcpu);
+	return false;
+}

Please make this common code, and not an inline function.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#3 lore raw

Re: [PATCH v4 1/6] KVM: arm64: Move CPU ID feature registers emulation into a separate file

Hi Marc,

Quoted 69 lines · Marc Zyngier
Quoted 33 lines · Jing Zhang

Create a new file id_regs.c for CPU ID feature registers emulation code, which are moved from sys_regs.c and tweak sys_regs code accordingly.

No functional change intended.

---
 arch/arm64/kvm/Makefile   |   2 +-
 arch/arm64/kvm/id_regs.c  | 506 ++++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c | 464 ++--------------------------------
 arch/arm64/kvm/sys_regs.h |  41 +++
 4 files changed, 575 insertions(+), 438 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c
arch/arm64/kvm/Makefile +1 -1
index c0c050e53157..a6a315fcd81e 100644
@@ -13,7 +13,7 @@ obj-$(CONFIG_KVM) += hyp/
 kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
       inject_fault.o va_layout.o handle_exit.o \
       guest.o debug.o reset.o sys_regs.o stacktrace.o \
-      vgic-sys-reg-v3.o fpsimd.o pkvm.o \
+      vgic-sys-reg-v3.o fpsimd.o pkvm.o id_regs.o \
       arch_timer.o trng.o vmid.o emulate-nested.o nested.o \
       vgic/vgic.o vgic/vgic-init.o \
       vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/id_regs.c new file +0 -0
new file mode 100644
index 000000000000..08b738852955

[...]

Quoted 30 lines
arch/arm64/kvm/id_regs.c new file +30 -0
+/**
+ * emulate_id_reg - Emulate a guest access to an AArch64 CPU ID feature register
+ * @vcpu: The VCPU pointer
+ * @params: Decoded system register parameters
+ *
+ * Return: true if the ID register access was successful, false otherwise.
+ */
+int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
+{
+     const struct sys_reg_desc *r;
+
+     r = find_reg(params, id_reg_descs, ARRAY_SIZE(id_reg_descs));
+
+     if (likely(r)) {
+             perform_access(vcpu, params, r);
+     } else {
+             print_sys_reg_msg(params,
+                               "Unsupported guest id_reg access at: %lx [%08lx]\n",
+                               *vcpu_pc(vcpu), *vcpu_cpsr(vcpu));
+             kvm_inject_undefined(vcpu);
+     }
+
+     return 1;
+}
+
+
+void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu)
+{
+     unsigned long i;
+
Quoted 9 lines

arch/arm64/kvm/id_regs.c line 472

Quoted 4 lines
arch/arm64/kvm/id_regs.c new file +4 -0
+     for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++)
+             if (id_reg_descs[i].reset)
+                     id_reg_descs[i].reset(vcpu, &id_reg_descs[i]);
+}

What does this mean? None of the idregs have a reset function, given that they are global. Maybe this will make sense in the following patches, but definitely not here.

You are right. It actually does nothing for idregs which have no reset function. Will remove this.

Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/id_regs.c new file +13 -0
+
+int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
+{
+     return kvm_sys_reg_get_user(vcpu, reg,
+                                 id_reg_descs, ARRAY_SIZE(id_reg_descs));
+}
+
+int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
+{
+     return kvm_sys_reg_set_user(vcpu, reg,
+                                 id_reg_descs, ARRAY_SIZE(id_reg_descs));
+}
+
Quoted 9 lines

arch/arm64/kvm/id_regs.c line 489

Quoted 4 lines
arch/arm64/kvm/id_regs.c new file +4 -0
+bool kvm_arm_check_idreg_table(void)
+{
+     return check_sysreg_table(id_reg_descs, ARRAY_SIZE(id_reg_descs), false);
+}

All these helpers are called from sys_regs.c and directly call back into it. Why not simply have a helper that gets the base and size of the array, and stick to pure common code?

As you know from the later patches in this series, a per VM idregs array and an idregs specific structure are used. All these functions would be implemented based on that.

Quoted 12 lines
Quoted 12 lines
arch/arm64/kvm/id_regs.c new file +12 -0
+
+int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
+{
+     const struct sys_reg_desc *i2, *end2;
+     unsigned int total = 0;
+     int err;
+
+     i2 = id_reg_descs;
+     end2 = id_reg_descs + ARRAY_SIZE(id_reg_descs);
+
+     while (i2 != end2) {
+             err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
Quoted 8 lines

arch/arm64/kvm/id_regs.c line 506

Quoted 5 lines
arch/arm64/kvm/id_regs.c new file +5 -0
+             if (err)
+                     return err;
+     }
+     return total;
+}

This is an exact copy of walk_sys_regs. Surely this can be made common code.

The reason for not using common code is the same as last comment. An idregs specific data structure would be used.

Quoted 26 lines

[...]

Quoted 23 lines
arch/arm64/kvm/id_regs.c new file +5 -0
@@ -2912,6 +2482,8 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
 {
      unsigned long i;
 
+     kvm_arm_reset_id_regs(vcpu);
+
      for (i = 0; i < ARRAY_SIZE(sys_reg_descs); i++)
              if (sys_reg_descs[i].reset)
                      sys_reg_descs[i].reset(vcpu, &sys_reg_descs[i]);
@@ -2932,6 +2504,9 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
      params = esr_sys64_to_params(esr);
      params.regval = vcpu_get_reg(vcpu, Rt);
 
+     if (is_id_reg(reg_to_encoding(&params)))
+             return emulate_id_reg(vcpu, &params);
+
      if (!emulate_sys_reg(vcpu, &params))
              return 1;
 
@@ -3160,6 +2735,10 @@ int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg
      if (err != -ENOENT)
              return err;
 
Quoted 5 lines

arch/arm64/kvm/id_regs.c int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg · line 2738

Quoted 1 line
arch/arm64/kvm/id_regs.c new file +1 -0
+     err = kvm_arm_get_id_reg(vcpu, reg);

Why not check for the encoding here? or in the helpers? It feels that this is an overhead that would be easy to reduce, given that we have fewer idregs than normal sysregs.

Sure, will move the encoding check here.

Quoted 11 lines
Quoted 11 lines
arch/arm64/kvm/id_regs.c new file +3 -0
+     if (err != -ENOENT)
+             return err;
+
      return kvm_sys_reg_get_user(vcpu, reg,
                                  sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
 }
@@ -3204,6 +2783,10 @@ int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg
      if (err != -ENOENT)
              return err;
 
Quoted 3 lines

arch/arm64/kvm/id_regs.c int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg · line 2786

Quoted 1 line
arch/arm64/kvm/id_regs.c new file +1 -0
+     err = kvm_arm_set_id_reg(vcpu, reg);

Same here.

Agreed.

Quoted 58 lines
Quoted 58 lines
arch/arm64/kvm/id_regs.c new file +14 -4
+     if (err != -ENOENT)
+             return err;
+
      return kvm_sys_reg_set_user(vcpu, reg,
                                  sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
 }
@@ -3250,10 +2833,10 @@ static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)
      return true;
 }
 
-static int walk_one_sys_reg(const struct kvm_vcpu *vcpu,
-                         const struct sys_reg_desc *rd,
-                         u64 __user **uind,
-                         unsigned int *total)
+int walk_one_sys_reg(const struct kvm_vcpu *vcpu,
+                  const struct sys_reg_desc *rd,
+                  u64 __user **uind,
+                  unsigned int *total)
 {
      /*
       * Ignore registers we trap but don't save,
@@ -3294,6 +2877,7 @@ unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu)
 {
      return ARRAY_SIZE(invariant_sys_regs)
              + num_demux_regs()
+             + kvm_arm_walk_id_regs(vcpu, (u64 __user *)NULL)
              + walk_sys_regs(vcpu, (u64 __user *)NULL);
 }
 
@@ -3309,6 +2893,11 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
              uindices++;
      }
 
+     err = kvm_arm_walk_id_regs(vcpu, uindices);
+     if (err < 0)
+             return err;
+     uindices += err;
+
      err = walk_sys_regs(vcpu, uindices);
      if (err < 0)
              return err;
@@ -3323,6 +2912,7 @@ int __init kvm_sys_reg_table_init(void)
      unsigned int i;
 
      /* Make sure tables are unique and in order. */
+     valid &= kvm_arm_check_idreg_table();
      valid &= check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs), false);
      valid &= check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs), true);
      valid &= check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs), true);
arch/arm64/kvm/sys_regs.h +0 -0
index 6b11f2cc7146..ad41305348f7 100644
@@ -210,6 +210,35 @@ find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
      return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
 }
 
Quoted 9 lines

arch/arm64/kvm/sys_regs.h find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[], · line 217

Quoted 5 lines
arch/arm64/kvm/sys_regs.h +5 -0
+static inline unsigned int raz_visibility(const struct kvm_vcpu *vcpu,
+                                       const struct sys_reg_desc *r)
+{
+     return REG_RAZ;
+}

No, please. This is used as a function pointer. You now potentially force the compiler to emit as many copy of this as there are pointers.

Thanks, will fix this.

Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/sys_regs.h +4 -0
+
+static inline bool write_to_read_only(struct kvm_vcpu *vcpu,
+                                   struct sys_reg_params *params,
+                                   const struct sys_reg_desc *r)
Quoted 8 lines

arch/arm64/kvm/sys_regs.h find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[], · line 227

Quoted 6 lines
arch/arm64/kvm/sys_regs.h +6 -0
+{
+     WARN_ONCE(1, "Unexpected sys_reg write to read-only register\n");
+     print_sys_reg_instr(params);
+     kvm_inject_undefined(vcpu);
+     return false;
+}

Please make this common code, and not an inline function.

Sure, will do.

Quoted 7 lines

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.

Thanks,
Jing

author
#4 lore raw

PATCH v4 2/6 KVM: arm64: Save ID registers' sanitized value per guest

In reply to Jing Zhang

From: Reiji Watanabe <[email protected]>

Introduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 44 ++++++++++++++++++++++++-------
 arch/arm64/kvm/sys_regs.c         |  2 +-
 arch/arm64/kvm/sys_regs.h         |  1 +
 5 files changed, 49 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_host.h +11 -0
index a1892a8f6032..fb6b50b1f111 100644
@@ -245,6 +245,15 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */
 	struct kvm_protected_vm pkvm;
+
+	/*
+	 * Save ID registers for the guest in id_regs[].
+	 * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
+	 * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+	 */
+#define KVM_ARM_ID_REG_NUM	56
+#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+	u64 id_regs[KVM_ARM_ID_REG_NUM];
 };
 
 struct kvm_vcpu_fault_info {
@@ -1005,6 +1014,8 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 				struct kvm_arm_copy_mte_tags *copy_tags);
 
+void kvm_arm_set_default_id_regs(struct kvm *kvm);
+
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +1 -0
index 3bd732eaf087..4579c878ab30 100644
@@ -153,6 +153,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	set_default_spectre(kvm);
 	kvm_arm_init_hypercalls(kvm);
+	kvm_arm_set_default_id_regs(kvm);
 
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +35 -9

60 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +1 -1
index 22b60474fcab..3243c924527e 100644
@@ -354,7 +354,7 @@ static bool trap_loregion(struct kvm_vcpu *vcpu,
 			  struct sys_reg_params *p,
 			  const struct sys_reg_desc *r)
 {
-	u64 val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
+	u64 val = kvm_arm_read_id_reg(vcpu, SYS_ID_AA64MMFR1_EL1);
 	u32 sr = reg_to_encoding(r);
 
 	if (!(val & (0xfUL << ID_AA64MMFR1_EL1_LO_SHIFT))) {
arch/arm64/kvm/sys_regs.h +1 -0
index ad41305348f7..ee136ba28fa5 100644
@@ -261,6 +261,7 @@ int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
 bool kvm_arm_check_idreg_table(void);
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind);
+u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id);
 
 #define AA32(_x)	.aarch32_map = AA32_##_x
 #define Op0(_x) 	.Op0 = _x
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
maintainer
#5 lore raw

Re: [PATCH v4 2/6] KVM: arm64: Save ID registers' sanitized value per guest

Quoted 28 lines · Jing Zhang

From: Reiji Watanabe <[email protected]>

Introduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 44 ++++++++++++++++++++++++-------
 arch/arm64/kvm/sys_regs.c         |  2 +-
 arch/arm64/kvm/sys_regs.h         |  1 +
 5 files changed, 49 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index a1892a8f6032..fb6b50b1f111 100644
@@ -245,6 +245,15 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */

arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 256

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +9 -0
 	struct kvm_protected_vm pkvm;
+
+	/*
+	 * Save ID registers for the guest in id_regs[].
+	 * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
+	 * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+	 */
+#define KVM_ARM_ID_REG_NUM	56
+#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+	u64 id_regs[KVM_ARM_ID_REG_NUM];

Place these registers in their own structure, and place this structure *before* the pvm structure. Document what guards these registers when updated (my hunch is that this should rely on Oliver's locking fixes if the update comes from a vcpu).

Quoted 76 lines
arch/arm64/include/asm/kvm_host.h +2 -0
 };
 
 struct kvm_vcpu_fault_info {
@@ -1005,6 +1014,8 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 				struct kvm_arm_copy_mte_tags *copy_tags);
 
+void kvm_arm_set_default_id_regs(struct kvm *kvm);
+
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +1 -0
index 3bd732eaf087..4579c878ab30 100644
@@ -153,6 +153,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	set_default_spectre(kvm);
 	kvm_arm_init_hypercalls(kvm);
+	kvm_arm_set_default_id_regs(kvm);
 
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +20 -9
index 08b738852955..e393b5730557 100644
@@ -52,16 +52,9 @@ static u8 pmuver_to_perfmon(u8 pmuver)
 	}
 }
 
-/* Read a sanitised cpufeature ID register by sys_reg_desc */
-static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 {
-	u32 id = reg_to_encoding(r);
-	u64 val;
-
-	if (sysreg_visible_as_raz(vcpu, r))
-		return 0;
-
-	val = read_sanitised_ftr_reg(id);
+	u64 val = vcpu->kvm->arch.id_regs[IDREG_IDX(id)];
 
 	switch (id) {
 	case SYS_ID_AA64PFR0_EL1:
@@ -126,6 +119,14 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r
 	return val;
 }
 
+static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+{
+	if (sysreg_visible_as_raz(vcpu, r))
+		return 0;
+
+	return kvm_arm_read_id_reg(vcpu, reg_to_encoding(r));
+}
+
 /* cpufeature ID register access trap handlers */
 
 static bool access_id_reg(struct kvm_vcpu *vcpu,
@@ -504,3 +505,28 @@ int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 	}
 	return total;
 }
+
+/*
+ * Set the guest's ID registers that are defined in id_reg_descs[]
+ * with ID_SANITISED() to the host's sanitized value.
+ */
+void kvm_arm_set_default_id_regs(struct kvm *kvm)
+{
+	int i;
+	u32 id;
+	u64 val;

arch/arm64/kvm/id_regs.c int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 527

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+
+	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+		id = reg_to_encoding(&id_reg_descs[i]);
+		if (WARN_ON_ONCE(!is_id_reg(id)))
+			/* Shouldn't happen */
+			continue;
+
+		if (id_reg_descs[i].visibility == raz_visibility)
+			/* Hidden or reserved ID register */
+			continue;

Relying on function pointer comparison is really fragile. If I wrap raz_visibility() in another function, this won't catch it. It also doesn't bode well with your 'inline' definition of this function.

More importantly, why do we care about checking for visibility at all? We can happily populate the array and rely on the runtime visibility.

arch/arm64/kvm/id_regs.c int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 532

Quoted 5 lines
arch/arm64/kvm/id_regs.c +5 -0
+
+		val = read_sanitised_ftr_reg(id);
+		kvm->arch.id_regs[IDREG_IDX(id)] = val;
+	}
+}

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#6 lore raw

Re: [PATCH v4 2/6] KVM: arm64: Save ID registers' sanitized value per guest

Hi Marc,

Quoted 38 lines · Marc Zyngier
Quoted 35 lines · Jing Zhang

From: Reiji Watanabe <[email protected]>

Introduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 44 ++++++++++++++++++++++++-------
 arch/arm64/kvm/sys_regs.c         |  2 +-
 arch/arm64/kvm/sys_regs.h         |  1 +
 5 files changed, 49 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -0
index a1892a8f6032..fb6b50b1f111 100644
@@ -245,6 +245,15 @@ struct kvm_arch {
       * the associated pKVM instance in the hypervisor.
       */
      struct kvm_protected_vm pkvm;
+
+     /*
+      * Save ID registers for the guest in id_regs[].
+      * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
+      * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+      */
Quoted 8 lines

arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 256

Quoted 3 lines
arch/arm64/include/asm/kvm_host.h +3 -0
+#define KVM_ARM_ID_REG_NUM   56
+#define IDREG_IDX(id)                (((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+     u64 id_regs[KVM_ARM_ID_REG_NUM];

Place these registers in their own structure, and place this structure *before* the pvm structure. Document what guards these registers when updated (my hunch is that this should rely on Oliver's locking fixes if the update comes from a vcpu).

Sure, I will put them in their own structure and place it before the pkvm structure.
IIUC, usually we don't need a specific locking to update idregs here. All idregs are 64 bit and can be read/written atomically. The only case that may need a locking is to keep the consistency for PMUVer in AA64DFR0_EL1 and PerfMon in DFR0_EL1. If there is no use case for two VCPU threads in a VM to update PMUVer and PerfMon concurrently, then we don't need the locking as in later patch by using the kvm lock. WDTY?

Quoted 92 lines
Quoted 77 lines
arch/arm64/include/asm/kvm_host.h +2 -0
 };
 
 struct kvm_vcpu_fault_info {
@@ -1005,6 +1014,8 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
                              struct kvm_arm_copy_mte_tags *copy_tags);
 
+void kvm_arm_set_default_id_regs(struct kvm *kvm);
+
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +1 -0
index 3bd732eaf087..4579c878ab30 100644
@@ -153,6 +153,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
      set_default_spectre(kvm);
      kvm_arm_init_hypercalls(kvm);
+     kvm_arm_set_default_id_regs(kvm);
 
      /*
       * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +20 -9
index 08b738852955..e393b5730557 100644
@@ -52,16 +52,9 @@ static u8 pmuver_to_perfmon(u8 pmuver)
      }
 }
 
-/* Read a sanitised cpufeature ID register by sys_reg_desc */
-static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 {
-     u32 id = reg_to_encoding(r);
-     u64 val;
-
-     if (sysreg_visible_as_raz(vcpu, r))
-             return 0;
-
-     val = read_sanitised_ftr_reg(id);
+     u64 val = vcpu->kvm->arch.id_regs[IDREG_IDX(id)];
 
      switch (id) {
      case SYS_ID_AA64PFR0_EL1:
@@ -126,6 +119,14 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r
      return val;
 }
 
+static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+{
+     if (sysreg_visible_as_raz(vcpu, r))
+             return 0;
+
+     return kvm_arm_read_id_reg(vcpu, reg_to_encoding(r));
+}
+
 /* cpufeature ID register access trap handlers */
 
 static bool access_id_reg(struct kvm_vcpu *vcpu,
@@ -504,3 +505,28 @@ int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
      }
      return total;
 }
+
+/*
+ * Set the guest's ID registers that are defined in id_reg_descs[]
+ * with ID_SANITISED() to the host's sanitized value.
+ */
+void kvm_arm_set_default_id_regs(struct kvm *kvm)
+{
+     int i;
+     u32 id;
+     u64 val;

arch/arm64/kvm/id_regs.c int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 527

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+
+     for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+             id = reg_to_encoding(&id_reg_descs[i]);
+             if (WARN_ON_ONCE(!is_id_reg(id)))
+                     /* Shouldn't happen */
+                     continue;
+
+             if (id_reg_descs[i].visibility == raz_visibility)
+                     /* Hidden or reserved ID register */
+                     continue;

Relying on function pointer comparison is really fragile. If I wrap raz_visibility() in another function, this won't catch it. It also doesn't bode well with your 'inline' definition of this function.

Quoted 2 lines

More importantly, why do we care about checking for visibility at all? We can happily populate the array and rely on the runtime visibility.

Right. I'll remove this checking.

Quoted 9 lines

arch/arm64/kvm/id_regs.c int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 532

Quoted 6 lines
arch/arm64/kvm/id_regs.c +5 -0
+
+             val = read_sanitised_ftr_reg(id);
+             kvm->arch.id_regs[IDREG_IDX(id)] = val;
+     }
+}

Thanks,

Quoted 4 lines
        M.

--
Without deviation from the norm, progress is not possible.

Thanks,
Jing

maintainer
#7 lore raw

Re: [PATCH v4 2/6] KVM: arm64: Save ID registers' sanitized value per guest

Quoted 2 lines · Jing Zhang

Hi Marc,

[...]

Quoted 7 lines

IIUC, usually we don't need a specific locking to update idregs here. All idregs are 64 bit and can be read/written atomically. The only case that may need a locking is to keep the consistency for PMUVer in AA64DFR0_EL1 and PerfMon in DFR0_EL1. If there is no use case for two VCPU threads in a VM to update PMUVer and PerfMon concurrently, then we don't need the locking as in later patch by using the kvm lock. WDTY?

I think we generally need locking for any writable id-reg, the goal being that they will ultimately *all* be writable. As you found out, there is this need for the PMU fields, and I'm willing to bet that there will be more of those.

And given that the locking you have used in some of the later patches violates the locking order (don't worry, you're not alone!), we need to use something else. Which is where Oliver's series comes into play.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#8 lore raw

Re: [PATCH v4 2/6] KVM: arm64: Save ID registers' sanitized value per guest

Quoted 16 lines · Marc Zyngier
Quoted 2 lines · Jing Zhang

Hi Marc,

[...]

Quoted 7 lines

IIUC, usually we don't need a specific locking to update idregs here. All idregs are 64 bit and can be read/written atomically. The only case that may need a locking is to keep the consistency for PMUVer in AA64DFR0_EL1 and PerfMon in DFR0_EL1. If there is no use case for two VCPU threads in a VM to update PMUVer and PerfMon concurrently, then we don't need the locking as in later patch by using the kvm lock. WDTY?

Quoted 8 lines

I think we generally need locking for any writable id-reg, the goal being that they will ultimately *all* be writable. As you found out, there is this need for the PMU fields, and I'm willing to bet that there will be more of those.

And given that the locking you have used in some of the later patches violates the locking order (don't worry, you're not alone!), we need to use something else. Which is where Oliver's series comes into play.

Got it. Thanks for the details. I'll add locking based on Oliver's series.

Quoted 7 lines

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.

Thanks,
Jing

#9 lore raw

Re: [PATCH v4 2/6] KVM: arm64: Save ID registers' sanitized value per guest

In reply to Jing Zhang
Quoted 24 lines
Quoted 24 lines
Quoted 9 lines
arch/arm64/kvm/id_regs.c +9 -0
+/*
+ * Set the guest's ID registers that are defined in id_reg_descs[]
+ * with ID_SANITISED() to the host's sanitized value.
+ */
+void kvm_arm_set_default_id_regs(struct kvm *kvm)
+{
+     int i;
+     u32 id;
+     u64 val;

arch/arm64/kvm/id_regs.c int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 527

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+
+     for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+             id = reg_to_encoding(&id_reg_descs[i]);
+             if (WARN_ON_ONCE(!is_id_reg(id)))
+                     /* Shouldn't happen */
+                     continue;
+
+             if (id_reg_descs[i].visibility == raz_visibility)
+                     /* Hidden or reserved ID register */
+                     continue;

Relying on function pointer comparison is really fragile. If I wrap raz_visibility() in another function, this won't catch it. It also doesn't bode well with your 'inline' definition of this function.

Quoted 3 lines
Quoted 2 lines

More importantly, why do we care about checking for visibility at all? We can happily populate the array and rely on the runtime visibility.

Right. I'll remove this checking.

Without the check, calling read_sanitised_ftr_reg() for some hidden ID registers will show a warning as some of them are not in arm64_ftr_regs[] (e.g. reserved ones). This checking is needed temporarily to avoid the warning (the check is removed in the following patches of this series). It would be much less fragile to call the visibility function instead, but I don't think this is a also good way to check the availability of the sanitized values for ID registers either. I didn't found a good (proper) way to check that without making changes in cpufeature.c, and I'm not sure if it is worth it for this temporary purpose.

Thank you,
Reiji

Quoted 16 lines
Quoted 9 lines

arch/arm64/kvm/id_regs.c int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 532

Quoted 6 lines
arch/arm64/kvm/id_regs.c +5 -0
+
+             val = read_sanitised_ftr_reg(id);
+             kvm->arch.id_regs[IDREG_IDX(id)] = val;
+     }
+}

Thanks,

Quoted 4 lines
        M.

--
Without deviation from the norm, progress is not possible.

Thanks,
Jing

author
#10 lore raw

PATCH v4 3/6 KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]

In reply to Jing Zhang

With per guest ID registers, ID_AA64PFR0_EL1.[CSV2|CSV3] settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h  |  2 --
 arch/arm64/kvm/arm.c               | 19 +------------------
 arch/arm64/kvm/hyp/nvhe/sys_regs.c |  7 +++----
 arch/arm64/kvm/id_regs.c           | 30 ++++++++++++++++++++++--------
 4 files changed, 26 insertions(+), 32 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
index fb6b50b1f111..e926ea91a73c 100644
@@ -230,8 +230,6 @@ struct kvm_arch {
 
 	cpumask_var_t supported_cpus;
 
-	u8 pfr0_csv2;
-	u8 pfr0_csv3;
 	struct {
 		u8 imp:4;
 		u8 unimp:4;
arch/arm64/kvm/arm.c +1 -18
index 4579c878ab30..c78d68d011cb 100644
@@ -104,22 +104,6 @@ static int kvm_arm_default_max_vcpus(void)
 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
-static void set_default_spectre(struct kvm *kvm)
-{
-	/*
-	 * The default is to expose CSV2 == 1 if the HW isn't affected.
-	 * Although this is a per-CPU feature, we make it global because
-	 * asymmetric systems are just a nuisance.
-	 *
-	 * Userspace can override this as long as it doesn't promise
-	 * the impossible.
-	 */
-	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
-		kvm->arch.pfr0_csv2 = 1;
-	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
-		kvm->arch.pfr0_csv3 = 1;
-}
-
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:	pointer to the KVM struct
@@ -151,9 +135,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	/* The maximum number of VCPUs is limited by the host's GIC model */
 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-	set_default_spectre(kvm);
-	kvm_arm_init_hypercalls(kvm);
 	kvm_arm_set_default_id_regs(kvm);
+	kvm_arm_init_hypercalls(kvm);
 
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/hyp/nvhe/sys_regs.c +3 -4
index 08d2b004f4b7..0e1988740a65 100644
@@ -93,10 +93,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
 		PVM_ID_AA64PFR0_RESTRICT_UNSIGNED);
 
 	/* Spectre and Meltdown mitigation in KVM */
-	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-			       (u64)kvm->arch.pfr0_csv2);
-	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-			       (u64)kvm->arch.pfr0_csv3);
+	set_mask |= vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] &
+		(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
+			ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
 
 	return (id_aa64pfr0_el1_sys_val & allow_mask) | set_mask;
 }
arch/arm64/kvm/id_regs.c +22 -8
index e393b5730557..b60ca1058301 100644
@@ -61,12 +61,6 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 		if (!vcpu_has_sve(vcpu))
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
 		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
-		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
-		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-				  (u64)vcpu->kvm->arch.pfr0_csv2);
-		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
-		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-				  (u64)vcpu->kvm->arch.pfr0_csv3);
 		if (kvm_vgic_global_state.type == VGIC_V3) {
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
 			val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -201,6 +195,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 			       u64 val)
 {
 	u8 csv2, csv3;
+	u64 sval = val;
 
 	/*
 	 * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
@@ -225,8 +220,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 	if (val)
 		return -EINVAL;
 
-	vcpu->kvm->arch.pfr0_csv2 = csv2;
-	vcpu->kvm->arch.pfr0_csv3 = csv3;
+	vcpu->kvm->arch.id_regs[IDREG_IDX(reg_to_encoding(rd))] = sval;
 
 	return 0;
 }
@@ -529,4 +523,24 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
 		val = read_sanitised_ftr_reg(id);
 		kvm->arch.id_regs[IDREG_IDX(id)] = val;
 	}
+	/*
+	 * The default is to expose CSV2 == 1 if the HW isn't affected.
+	 * Although this is a per-CPU feature, we make it global because
+	 * asymmetric systems are just a nuisance.
+	 *
+	 * Userspace can override this as long as it doesn't promise
+	 * the impossible.
+	 */
+	val = kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)];
+
+	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);
+	}
+	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
+	}
+
+	kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = val;
 }
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
maintainer
#11 lore raw

Re: [PATCH v4 3/6] KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]

Quoted 55 lines · Jing Zhang

With per guest ID registers, ID_AA64PFR0_EL1.[CSV2|CSV3] settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h  |  2 --
 arch/arm64/kvm/arm.c               | 19 +------------------
 arch/arm64/kvm/hyp/nvhe/sys_regs.c |  7 +++----
 arch/arm64/kvm/id_regs.c           | 30 ++++++++++++++++++++++--------
 4 files changed, 26 insertions(+), 32 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
index fb6b50b1f111..e926ea91a73c 100644
@@ -230,8 +230,6 @@ struct kvm_arch {
 
 	cpumask_var_t supported_cpus;
 
-	u8 pfr0_csv2;
-	u8 pfr0_csv3;
 	struct {
 		u8 imp:4;
 		u8 unimp:4;
arch/arm64/kvm/arm.c +0 -16
index 4579c878ab30..c78d68d011cb 100644
@@ -104,22 +104,6 @@ static int kvm_arm_default_max_vcpus(void)
 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
-static void set_default_spectre(struct kvm *kvm)
-{
-	/*
-	 * The default is to expose CSV2 == 1 if the HW isn't affected.
-	 * Although this is a per-CPU feature, we make it global because
-	 * asymmetric systems are just a nuisance.
-	 *
-	 * Userspace can override this as long as it doesn't promise
-	 * the impossible.
-	 */
-	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
-		kvm->arch.pfr0_csv2 = 1;
-	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
-		kvm->arch.pfr0_csv3 = 1;
-}
-
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:	pointer to the KVM struct

arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 139

Quoted 7 lines
arch/arm64/kvm/arm.c +1 -2
@@ -151,9 +135,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	/* The maximum number of VCPUs is limited by the host's GIC model */
 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-	set_default_spectre(kvm);
-	kvm_arm_init_hypercalls(kvm);
 	kvm_arm_set_default_id_regs(kvm);
+	kvm_arm_init_hypercalls(kvm);

Please document the ordering dependency between idregs and hypercalls.

Quoted 8 lines
arch/arm64/kvm/arm.c +0 -0
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
index 08d2b004f4b7..0e1988740a65 100644

arch/arm64/kvm/hyp/nvhe/sys_regs.c static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu) · line 96

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +1 -4
@@ -93,10 +93,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
 		PVM_ID_AA64PFR0_RESTRICT_UNSIGNED);
 
 	/* Spectre and Meltdown mitigation in KVM */
-	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-			       (u64)kvm->arch.pfr0_csv2);
-	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-			       (u64)kvm->arch.pfr0_csv3);
+	set_mask |= vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] &

This really want an accessor.

Quoted 31 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +2 -0
+		(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
+			ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
 
 	return (id_aa64pfr0_el1_sys_val & allow_mask) | set_mask;
 }
arch/arm64/kvm/id_regs.c +1 -6
index e393b5730557..b60ca1058301 100644
@@ -61,12 +61,6 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 		if (!vcpu_has_sve(vcpu))
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
 		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
-		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
-		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-				  (u64)vcpu->kvm->arch.pfr0_csv2);
-		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
-		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-				  (u64)vcpu->kvm->arch.pfr0_csv3);
 		if (kvm_vgic_global_state.type == VGIC_V3) {
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
 			val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -201,6 +195,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 			       u64 val)
 {
 	u8 csv2, csv3;
+	u64 sval = val;
 
 	/*
 	 * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as

arch/arm64/kvm/id_regs.c static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, · line 223

Quoted 6 lines
arch/arm64/kvm/id_regs.c +1 -2
@@ -225,8 +220,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 	if (val)
 		return -EINVAL;
 
-	vcpu->kvm->arch.pfr0_csv2 = csv2;
-	vcpu->kvm->arch.pfr0_csv3 = csv3;
+	vcpu->kvm->arch.id_regs[IDREG_IDX(reg_to_encoding(rd))] = sval;

Accessor needed here to.

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 526

Quoted 8 lines
arch/arm64/kvm/id_regs.c +1 -0
 	return 0;
 }
@@ -529,4 +523,24 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
 		val = read_sanitised_ftr_reg(id);
 		kvm->arch.id_regs[IDREG_IDX(id)] = val;
 	}
+	/*

Add a blank line after the closing bracket.

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 534

Quoted 8 lines
arch/arm64/kvm/id_regs.c +8 -0
+	 * The default is to expose CSV2 == 1 if the HW isn't affected.
+	 * Although this is a per-CPU feature, we make it global because
+	 * asymmetric systems are just a nuisance.
+	 *
+	 * Userspace can override this as long as it doesn't promise
+	 * the impossible.
+	 */
+	val = kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)];

Accessor.

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 545

Quoted 11 lines
arch/arm64/kvm/id_regs.c +11 -0
+
+	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);
+	}
+	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
+	}
+
+	kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = val;

Accessor.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#12 lore raw

Re: [PATCH v4 3/6] KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]

Hi Marc,

Quoted 61 lines · Marc Zyngier
Quoted 58 lines · Jing Zhang

With per guest ID registers, ID_AA64PFR0_EL1.[CSV2|CSV3] settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h  |  2 --
 arch/arm64/kvm/arm.c               | 19 +------------------
 arch/arm64/kvm/hyp/nvhe/sys_regs.c |  7 +++----
 arch/arm64/kvm/id_regs.c           | 30 ++++++++++++++++++++++--------
 4 files changed, 26 insertions(+), 32 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
index fb6b50b1f111..e926ea91a73c 100644
@@ -230,8 +230,6 @@ struct kvm_arch {
 
      cpumask_var_t supported_cpus;
 
-     u8 pfr0_csv2;
-     u8 pfr0_csv3;
      struct {
              u8 imp:4;
              u8 unimp:4;
arch/arm64/kvm/arm.c +0 -16
index 4579c878ab30..c78d68d011cb 100644
@@ -104,22 +104,6 @@ static int kvm_arm_default_max_vcpus(void)
      return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
-static void set_default_spectre(struct kvm *kvm)
-{
-     /*
-      * The default is to expose CSV2 == 1 if the HW isn't affected.
-      * Although this is a per-CPU feature, we make it global because
-      * asymmetric systems are just a nuisance.
-      *
-      * Userspace can override this as long as it doesn't promise
-      * the impossible.
-      */
-     if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
-             kvm->arch.pfr0_csv2 = 1;
-     if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
-             kvm->arch.pfr0_csv3 = 1;
-}
-
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:     pointer to the KVM struct
@@ -151,9 +135,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
      /* The maximum number of VCPUs is limited by the host's GIC model */
      kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
Quoted 6 lines

arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 139

Quoted 4 lines
arch/arm64/kvm/arm.c +1 -2
-     set_default_spectre(kvm);
-     kvm_arm_init_hypercalls(kvm);
      kvm_arm_set_default_id_regs(kvm);
+     kvm_arm_init_hypercalls(kvm);

Please document the ordering dependency between idregs and hypercalls.

I didn't see an ordering dependency here. The reason I put kvm_arm_set_default_id_regs before kvm_arm_init_hypercalls is that kvm_arm_set_default_id_regs includes the code in set_default_spectre.

Quoted 61 lines
Quoted 9 lines
arch/arm64/kvm/arm.c +0 -0
      /*
       * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
index 08d2b004f4b7..0e1988740a65 100644

arch/arm64/kvm/hyp/nvhe/sys_regs.c static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu) · line 96

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +1 -4
@@ -93,10 +93,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
              PVM_ID_AA64PFR0_RESTRICT_UNSIGNED);
 
      /* Spectre and Meltdown mitigation in KVM */
-     set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-                            (u64)kvm->arch.pfr0_csv2);
-     set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-                            (u64)kvm->arch.pfr0_csv3);
+     set_mask |= vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] &

This really want an accessor.

Quoted 31 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +2 -0
+             (ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
+                     ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
 
      return (id_aa64pfr0_el1_sys_val & allow_mask) | set_mask;
 }
arch/arm64/kvm/id_regs.c +1 -6
index e393b5730557..b60ca1058301 100644
@@ -61,12 +61,6 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
              if (!vcpu_has_sve(vcpu))
                      val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
              val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
-             val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
-             val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-                               (u64)vcpu->kvm->arch.pfr0_csv2);
-             val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
-             val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-                               (u64)vcpu->kvm->arch.pfr0_csv3);
              if (kvm_vgic_global_state.type == VGIC_V3) {
                      val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
                      val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -201,6 +195,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
                             u64 val)
 {
      u8 csv2, csv3;
+     u64 sval = val;
 
      /*
       * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as

arch/arm64/kvm/id_regs.c static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, · line 223

Quoted 6 lines
arch/arm64/kvm/id_regs.c +1 -2
@@ -225,8 +220,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
      if (val)
              return -EINVAL;
 
-     vcpu->kvm->arch.pfr0_csv2 = csv2;
-     vcpu->kvm->arch.pfr0_csv3 = csv3;
+     vcpu->kvm->arch.id_regs[IDREG_IDX(reg_to_encoding(rd))] = sval;

Accessor needed here to.

Quoted 1 line
Quoted 9 lines

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 526

Quoted 7 lines
      return 0;

}

arch/arm64/kvm/id_regs.c +1 -0
@@ -529,4 +523,24 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
              val = read_sanitised_ftr_reg(id);
              kvm->arch.id_regs[IDREG_IDX(id)] = val;
      }
+     /*

Add a blank line after the closing bracket.

Will do.

Quoted 12 lines

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 534

Quoted 9 lines
arch/arm64/kvm/id_regs.c +8 -0
+      * The default is to expose CSV2 == 1 if the HW isn't affected.
+      * Although this is a per-CPU feature, we make it global because
+      * asymmetric systems are just a nuisance.
+      *
+      * Userspace can override this as long as it doesn't promise
+      * the impossible.
+      */
+     val = kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)];

Accessor.

Quoted 13 lines

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 545

Quoted 11 lines
arch/arm64/kvm/id_regs.c +11 -0
+
+     if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+             val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+             val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);
+     }
+     if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+             val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
+             val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
+     }
+
+     kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = val;

Accessor.

Sure, I'll use the macro IDREG() as in the last version of the patch series.

Quoted 7 lines

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.

Thanks,
Jing

#13 lore raw

Re: [PATCH v4 3/6] KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]

In reply to Jing Zhang

Hi,

Quoted 70 lines · Jing Zhang

With per guest ID registers, ID_AA64PFR0_EL1.[CSV2|CSV3] settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h  |  2 --
 arch/arm64/kvm/arm.c               | 19 +------------------
 arch/arm64/kvm/hyp/nvhe/sys_regs.c |  7 +++----
 arch/arm64/kvm/id_regs.c           | 30 ++++++++++++++++++++++--------
 4 files changed, 26 insertions(+), 32 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
index fb6b50b1f111..e926ea91a73c 100644
@@ -230,8 +230,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       u8 pfr0_csv2;
-       u8 pfr0_csv3;
        struct {
                u8 imp:4;
                u8 unimp:4;
arch/arm64/kvm/arm.c +1 -18
index 4579c878ab30..c78d68d011cb 100644
@@ -104,22 +104,6 @@ static int kvm_arm_default_max_vcpus(void)
        return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
-static void set_default_spectre(struct kvm *kvm)
-{
-       /*
-        * The default is to expose CSV2 == 1 if the HW isn't affected.
-        * Although this is a per-CPU feature, we make it global because
-        * asymmetric systems are just a nuisance.
-        *
-        * Userspace can override this as long as it doesn't promise
-        * the impossible.
-        */
-       if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
-               kvm->arch.pfr0_csv2 = 1;
-       if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
-               kvm->arch.pfr0_csv3 = 1;
-}
-
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:       pointer to the KVM struct
@@ -151,9 +135,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        /* The maximum number of VCPUs is limited by the host's GIC model */
        kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-       set_default_spectre(kvm);
-       kvm_arm_init_hypercalls(kvm);
        kvm_arm_set_default_id_regs(kvm);
+       kvm_arm_init_hypercalls(kvm);
 
        /*
         * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
index 08d2b004f4b7..0e1988740a65 100644

arch/arm64/kvm/hyp/nvhe/sys_regs.c static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu) · line 98

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +3 -4
@@ -93,10 +93,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
                PVM_ID_AA64PFR0_RESTRICT_UNSIGNED);
 
        /* Spectre and Meltdown mitigation in KVM */
-       set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-                              (u64)kvm->arch.pfr0_csv2);
-       set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-                              (u64)kvm->arch.pfr0_csv3);
+       set_mask |= vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] &
+               (ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
+                       ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));

This triggers a compiler warning now since the variable struct kvm *kvm isn't used anymore, this, however, isn't the main issue.

The main issue is that struct kvm here (vcpu->kvm) is the hypervisor's version for protected vms, and not the host's. Therefore, reading that value is wrong. That said, this is an existing bug in pKVM since kvm->arch.pfr0_csv2 and kvm->arch.pfr0_csv3 are not initialized.

The solution would be to track the spectre/meltown state at hyp and use that. I'll submit a patch that does that. In the meantime, I think that it would be better not to set the CSV bits for protected VMs, which is the current behavior in practice.

Non-protected VMs in protected mode go back to the host on id register traps, and use the host's struct kvm, so they should behave as expected.

Thanks,
/fuad

Quoted 66 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
        return (id_aa64pfr0_el1_sys_val & allow_mask) | set_mask;
 }
arch/arm64/kvm/id_regs.c +22 -9
index e393b5730557..b60ca1058301 100644
@@ -61,12 +61,6 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
                if (!vcpu_has_sve(vcpu))
                        val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
                val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
-               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
-               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-                                 (u64)vcpu->kvm->arch.pfr0_csv2);
-               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
-               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-                                 (u64)vcpu->kvm->arch.pfr0_csv3);
                if (kvm_vgic_global_state.type == VGIC_V3) {
                        val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
                        val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -201,6 +195,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
                               u64 val)
 {
        u8 csv2, csv3;
+       u64 sval = val;
 
        /*
         * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
@@ -225,8 +220,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
-       vcpu->kvm->arch.pfr0_csv2 = csv2;
-       vcpu->kvm->arch.pfr0_csv3 = csv3;
+       vcpu->kvm->arch.id_regs[IDREG_IDX(reg_to_encoding(rd))] = sval;
 
        return 0;
 }
@@ -529,4 +523,24 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
                val = read_sanitised_ftr_reg(id);
                kvm->arch.id_regs[IDREG_IDX(id)] = val;
        }
+       /*
+        * The default is to expose CSV2 == 1 if the HW isn't affected.
+        * Although this is a per-CPU feature, we make it global because
+        * asymmetric systems are just a nuisance.
+        *
+        * Userspace can override this as long as it doesn't promise
+        * the impossible.
+        */
+       val = kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)];
+
+       if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);
+       }
+       if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
+               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
+       }
+
+       kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = val;
 }
--

2.40.0.rc1.284.g88254d51c5-goog

author
#14 lore raw

Re: [PATCH v4 3/6] KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]

Hi Faud,

Quoted 94 lines · Fuad Tabba

Hi,

Quoted 70 lines · Jing Zhang

With per guest ID registers, ID_AA64PFR0_EL1.[CSV2|CSV3] settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h  |  2 --
 arch/arm64/kvm/arm.c               | 19 +------------------
 arch/arm64/kvm/hyp/nvhe/sys_regs.c |  7 +++----
 arch/arm64/kvm/id_regs.c           | 30 ++++++++++++++++++++++--------
 4 files changed, 26 insertions(+), 32 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
index fb6b50b1f111..e926ea91a73c 100644
@@ -230,8 +230,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       u8 pfr0_csv2;
-       u8 pfr0_csv3;
        struct {
                u8 imp:4;
                u8 unimp:4;
arch/arm64/kvm/arm.c +1 -18
index 4579c878ab30..c78d68d011cb 100644
@@ -104,22 +104,6 @@ static int kvm_arm_default_max_vcpus(void)
        return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
-static void set_default_spectre(struct kvm *kvm)
-{
-       /*
-        * The default is to expose CSV2 == 1 if the HW isn't affected.
-        * Although this is a per-CPU feature, we make it global because
-        * asymmetric systems are just a nuisance.
-        *
-        * Userspace can override this as long as it doesn't promise
-        * the impossible.
-        */
-       if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
-               kvm->arch.pfr0_csv2 = 1;
-       if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
-               kvm->arch.pfr0_csv3 = 1;
-}
-
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:       pointer to the KVM struct
@@ -151,9 +135,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        /* The maximum number of VCPUs is limited by the host's GIC model */
        kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-       set_default_spectre(kvm);
-       kvm_arm_init_hypercalls(kvm);
        kvm_arm_set_default_id_regs(kvm);
+       kvm_arm_init_hypercalls(kvm);
 
        /*
         * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
index 08d2b004f4b7..0e1988740a65 100644

arch/arm64/kvm/hyp/nvhe/sys_regs.c static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu) · line 98

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +3 -4
@@ -93,10 +93,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
                PVM_ID_AA64PFR0_RESTRICT_UNSIGNED);
 
        /* Spectre and Meltdown mitigation in KVM */
-       set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-                              (u64)kvm->arch.pfr0_csv2);
-       set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-                              (u64)kvm->arch.pfr0_csv3);
+       set_mask |= vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] &
+               (ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
+                       ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));

This triggers a compiler warning now since the variable struct kvm *kvm isn't used anymore, this, however, isn't the main issue.

The main issue is that struct kvm here (vcpu->kvm) is the hypervisor's version for protected vms, and not the host's. Therefore, reading that value is wrong. That said, this is an existing bug in pKVM since kvm->arch.pfr0_csv2 and kvm->arch.pfr0_csv3 are not initialized.

Quoted 8 lines

The solution would be to track the spectre/meltown state at hyp and use that. I'll submit a patch that does that. In the meantime, I think that it would be better not to set the CSV bits for protected VMs, which is the current behavior in practice.

Non-protected VMs in protected mode go back to the host on id register traps, and use the host's struct kvm, so they should behave as expected.

You mean just remove these two lines:
/* Spectre and Meltdown mitigation in KVM */
set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
(u64)kvm->arch.pfr0_csv2);
set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), (u64)kvm->arch.pfr0_csv3);

Will it cause any problem for pKVM without your incoming patch?

Quoted 63 lines

Thanks,
/fuad

Quoted 58 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
        return (id_aa64pfr0_el1_sys_val & allow_mask) | set_mask;
 }
arch/arm64/kvm/id_regs.c +18 -8
index e393b5730557..b60ca1058301 100644
@@ -61,12 +61,6 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
                if (!vcpu_has_sve(vcpu))
                        val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
                val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
-               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
-               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-                                 (u64)vcpu->kvm->arch.pfr0_csv2);
-               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
-               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-                                 (u64)vcpu->kvm->arch.pfr0_csv3);
                if (kvm_vgic_global_state.type == VGIC_V3) {
                        val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
                        val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -201,6 +195,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
                               u64 val)
 {
        u8 csv2, csv3;
+       u64 sval = val;
 
        /*
         * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
@@ -225,8 +220,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
-       vcpu->kvm->arch.pfr0_csv2 = csv2;
-       vcpu->kvm->arch.pfr0_csv3 = csv3;
+       vcpu->kvm->arch.id_regs[IDREG_IDX(reg_to_encoding(rd))] = sval;
 
        return 0;
 }
@@ -529,4 +523,24 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
                val = read_sanitised_ftr_reg(id);
                kvm->arch.id_regs[IDREG_IDX(id)] = val;
        }
+       /*
+        * The default is to expose CSV2 == 1 if the HW isn't affected.
+        * Although this is a per-CPU feature, we make it global because
+        * asymmetric systems are just a nuisance.
+        *
+        * Userspace can override this as long as it doesn't promise
+        * the impossible.
+        */
+       val = kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)];
+
+       if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);
+       }
+       if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
Quoted 8 lines
Quoted 8 lines
arch/arm64/kvm/id_regs.c +4 -1
+               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
+       }
+
+       kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = val;
 }
--

2.40.0.rc1.284.g88254d51c5-goog

Thanks,
Jing

#15 lore raw

Re: [PATCH v4 3/6] KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]

Hi Jing,

Quoted 106 lines · Jing Zhang

Hi Faud,

Quoted 94 lines · Fuad Tabba

Hi,

Quoted 70 lines · Jing Zhang

With per guest ID registers, ID_AA64PFR0_EL1.[CSV2|CSV3] settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h  |  2 --
 arch/arm64/kvm/arm.c               | 19 +------------------
 arch/arm64/kvm/hyp/nvhe/sys_regs.c |  7 +++----
 arch/arm64/kvm/id_regs.c           | 30 ++++++++++++++++++++++--------
 4 files changed, 26 insertions(+), 32 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
index fb6b50b1f111..e926ea91a73c 100644
@@ -230,8 +230,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       u8 pfr0_csv2;
-       u8 pfr0_csv3;
        struct {
                u8 imp:4;
                u8 unimp:4;
arch/arm64/kvm/arm.c +1 -18
index 4579c878ab30..c78d68d011cb 100644
@@ -104,22 +104,6 @@ static int kvm_arm_default_max_vcpus(void)
        return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
-static void set_default_spectre(struct kvm *kvm)
-{
-       /*
-        * The default is to expose CSV2 == 1 if the HW isn't affected.
-        * Although this is a per-CPU feature, we make it global because
-        * asymmetric systems are just a nuisance.
-        *
-        * Userspace can override this as long as it doesn't promise
-        * the impossible.
-        */
-       if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
-               kvm->arch.pfr0_csv2 = 1;
-       if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
-               kvm->arch.pfr0_csv3 = 1;
-}
-
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:       pointer to the KVM struct
@@ -151,9 +135,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        /* The maximum number of VCPUs is limited by the host's GIC model */
        kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-       set_default_spectre(kvm);
-       kvm_arm_init_hypercalls(kvm);
        kvm_arm_set_default_id_regs(kvm);
+       kvm_arm_init_hypercalls(kvm);
 
        /*
         * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
index 08d2b004f4b7..0e1988740a65 100644

arch/arm64/kvm/hyp/nvhe/sys_regs.c static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu) · line 98

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +3 -4
@@ -93,10 +93,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
                PVM_ID_AA64PFR0_RESTRICT_UNSIGNED);
 
        /* Spectre and Meltdown mitigation in KVM */
-       set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-                              (u64)kvm->arch.pfr0_csv2);
-       set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-                              (u64)kvm->arch.pfr0_csv3);
+       set_mask |= vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] &
+               (ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
+                       ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));

This triggers a compiler warning now since the variable struct kvm *kvm isn't used anymore, this, however, isn't the main issue.

The main issue is that struct kvm here (vcpu->kvm) is the hypervisor's version for protected vms, and not the host's. Therefore, reading that value is wrong. That said, this is an existing bug in pKVM since kvm->arch.pfr0_csv2 and kvm->arch.pfr0_csv3 are not initialized.

Quoted 8 lines

The solution would be to track the spectre/meltown state at hyp and use that. I'll submit a patch that does that. In the meantime, I think that it would be better not to set the CSV bits for protected VMs, which is the current behavior in practice.

Non-protected VMs in protected mode go back to the host on id register traps, and use the host's struct kvm, so they should behave as expected.

Quoted 8 lines

You mean just remove these two lines:
/* Spectre and Meltdown mitigation in KVM */
set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
(u64)kvm->arch.pfr0_csv2);
set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), (u64)kvm->arch.pfr0_csv3);

Will it cause any problem for pKVM without your incoming patch?

Yes, just remove those lines and maybe write a comment in the commit message referring to this thread please.

It won't cause any problems because in pKVM, these values are never set (initialized to zero). This means that a protected guest always needs to use spectre/meltdown mitigations, which could be a performance problem, but not a security one. This is still a bug of course, hence why I need to fix it.

Thanks,
/fuad

Quoted 73 lines
Quoted 63 lines

Thanks,
/fuad

Quoted 58 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
        return (id_aa64pfr0_el1_sys_val & allow_mask) | set_mask;
 }
arch/arm64/kvm/id_regs.c +18 -8
index e393b5730557..b60ca1058301 100644
@@ -61,12 +61,6 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
                if (!vcpu_has_sve(vcpu))
                        val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
                val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
-               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
-               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-                                 (u64)vcpu->kvm->arch.pfr0_csv2);
-               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
-               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-                                 (u64)vcpu->kvm->arch.pfr0_csv3);
                if (kvm_vgic_global_state.type == VGIC_V3) {
                        val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
                        val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -201,6 +195,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
                               u64 val)
 {
        u8 csv2, csv3;
+       u64 sval = val;
 
        /*
         * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
@@ -225,8 +220,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
-       vcpu->kvm->arch.pfr0_csv2 = csv2;
-       vcpu->kvm->arch.pfr0_csv3 = csv3;
+       vcpu->kvm->arch.id_regs[IDREG_IDX(reg_to_encoding(rd))] = sval;
 
        return 0;
 }
@@ -529,4 +523,24 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
                val = read_sanitised_ftr_reg(id);
                kvm->arch.id_regs[IDREG_IDX(id)] = val;
        }
+       /*
+        * The default is to expose CSV2 == 1 if the HW isn't affected.
+        * Although this is a per-CPU feature, we make it global because
+        * asymmetric systems are just a nuisance.
+        *
+        * Userspace can override this as long as it doesn't promise
+        * the impossible.
+        */
+       val = kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)];
+
+       if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);
+       }
+       if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+               val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
Quoted 8 lines
Quoted 8 lines
arch/arm64/kvm/id_regs.c +4 -1
+               val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
+       }
+
+       kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = val;
 }
--

2.40.0.rc1.284.g88254d51c5-goog

Thanks,
Jing

author
#16 lore raw

PATCH v4 4/6 KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

In reply to Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 +++---
 arch/arm64/kvm/arm.c              |  6 ---
 arch/arm64/kvm/id_regs.c          | 61 +++++++++++++++++++++++++------
 include/kvm/arm_pmu.h             |  5 ++-
 4 files changed, 59 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index e926ea91a73c..102860ba896d 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT				4
 	/* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED		5
+	/*
+	 * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+	 * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+	 * userspace for VCPUs without PMU.
+	 */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU		6
 
 	unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
 	cpumask_var_t supported_cpus;
 
-	struct {
-		u8 imp:4;
-		u8 unimp:4;
-	} dfr0_pmuver;
-
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	kvm_arm_set_default_id_regs(kvm);
 	kvm_arm_init_hypercalls(kvm);
 
-	/*
-	 * Initialise the default PMUver before there is a chance to
-	 * create an actual PMU.
-	 */
-	kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
 	return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +50 -11

83 lines not on this page · raw message

include/kvm/arm_pmu.h +3 -2
index 628775334d5e..51c7f3e7bdde 100644
@@ -92,8 +92,9 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
  */
-#define kvm_pmu_is_3p5(vcpu)						\
-	(vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)									\
+	 (FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),					\
+		 vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)]) >= ID_AA64DFR0_EL1_PMUVer_V3P5)
 
 u8 kvm_arm_pmu_get_pmuver_limit(void);
 
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
maintainer
#17 lore raw

Re: [PATCH v4 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

Quoted 67 lines · Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 +++---
 arch/arm64/kvm/arm.c              |  6 ---
 arch/arm64/kvm/id_regs.c          | 61 +++++++++++++++++++++++++------
 include/kvm/arm_pmu.h             |  5 ++-
 4 files changed, 59 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index e926ea91a73c..102860ba896d 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT				4
 	/* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED		5
+	/*
+	 * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+	 * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+	 * userspace for VCPUs without PMU.
+	 */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU		6
 
 	unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
 	cpumask_var_t supported_cpus;
 
-	struct {
-		u8 imp:4;
-		u8 unimp:4;
-	} dfr0_pmuver;
-
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	kvm_arm_set_default_id_regs(kvm);
 	kvm_arm_init_hypercalls(kvm);
 
-	/*
-	 * Initialise the default PMUver before there is a chance to
-	 * create an actual PMU.
-	 */
-	kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
 	return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +0 -0
index b60ca1058301..3a87a3d2390d 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {

arch/arm64/kvm/id_regs.c line 29

Quoted 10 lines
arch/arm64/kvm/id_regs.c +6 -3
 	if (kvm_vcpu_has_pmu(vcpu))
-		return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-	return vcpu->kvm->arch.dfr0_pmuver.unimp;
+		return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+				vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)]);
+	else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+		return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+	else
+		return 0;

Drop the pointless elses.

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 263

Quoted 13 lines
arch/arm64/kvm/id_regs.c +2 -4
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,23 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 	if (val)
 		return -EINVAL;
 
-	if (valid_pmu)
-		vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
-	else
-		vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+	if (valid_pmu) {
+		mutex_lock(&vcpu->kvm->lock);

Bingo!

Quoted 5 lines
arch/arm64/kvm/id_regs.c +5 -0
+		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
+			~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |=
+			FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 278

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] &=
+			~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] |= FIELD_PREP(
+				ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver_to_perfmon(pmuver));
+		mutex_unlock(&vcpu->kvm->lock);
+	} else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+		set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+	} else {
+		clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+	}

The last two cases are better written as:

	assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags,
		   pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF);

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 316

Quoted 13 lines
arch/arm64/kvm/id_regs.c +2 -4
 	return 0;
 }
@@ -296,10 +312,23 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	if (val)
 		return -EINVAL;
 
-	if (valid_pmu)
-		vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-	else
-		vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+	if (valid_pmu) {
+		mutex_lock(&vcpu->kvm->lock);

Same here (lock inversion)

Quoted 5 lines
arch/arm64/kvm/id_regs.c +5 -0
+		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] &=
+			~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] |= FIELD_PREP(
+			ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon);
+

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 331

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
+			~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |= FIELD_PREP(
+			ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+		mutex_unlock(&vcpu->kvm->lock);
+	} else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+		set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+	} else {
+		clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+	}

Same here (assign_bit).

Quoted 7 lines
arch/arm64/kvm/id_regs.c +0 -0
 	return 0;
 }
@@ -543,4 +572,14 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
 	}
 
 	kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = val;

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 584

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+
+	/*
+	 * Initialise the default PMUver before there is a chance to
+	 * create an actual PMU.
+	 */
+	kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
+		~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+	kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |=
+		FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+			   kvm_arm_pmu_get_pmuver_limit());

Please put these assignments on a single line...

Quoted 6 lines
arch/arm64/kvm/id_regs.c +0 -0
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..51c7f3e7bdde 100644

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 97

Quoted 8 lines
include/kvm/arm_pmu.h +3 -2
@@ -92,8 +92,9 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
  */
-#define kvm_pmu_is_3p5(vcpu)						\
-	(vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)									\
+	 (FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),					\
+		 vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)]) >= ID_AA64DFR0_EL1_PMUVer_V3P5)

I'll stop mentioning the need for accessors...

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#18 lore raw

Re: [PATCH v4 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

Hi Marc,

Quoted 74 lines · Marc Zyngier
Quoted 71 lines · Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 +++---
 arch/arm64/kvm/arm.c              |  6 ---
 arch/arm64/kvm/id_regs.c          | 61 +++++++++++++++++++++++++------
 include/kvm/arm_pmu.h             |  5 ++-
 4 files changed, 59 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index e926ea91a73c..102860ba896d 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT                              4
      /* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED         5
+     /*
+      * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+      * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+      * userspace for VCPUs without PMU.
+      */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU           6
 
      unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
      cpumask_var_t supported_cpus;
 
-     struct {
-             u8 imp:4;
-             u8 unimp:4;
-     } dfr0_pmuver;
-
      /* Hypercall features firmware registers' descriptor */
      struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
      kvm_arm_set_default_id_regs(kvm);
      kvm_arm_init_hypercalls(kvm);
 
-     /*
-      * Initialise the default PMUver before there is a chance to
-      * create an actual PMU.
-      */
-     kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
      return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +0 -3
index b60ca1058301..3a87a3d2390d 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
      if (kvm_vcpu_has_pmu(vcpu))
-             return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-     return vcpu->kvm->arch.dfr0_pmuver.unimp;
Quoted 8 lines

arch/arm64/kvm/id_regs.c line 29

Quoted 6 lines
arch/arm64/kvm/id_regs.c +6 -0
+             return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)]);
+     else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+             return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+     else
+             return 0;

Drop the pointless elses.

Will do.

Quoted 8 lines
Quoted 8 lines
arch/arm64/kvm/id_regs.c +0 -0
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,23 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
      if (val)
              return -EINVAL;
 
Quoted 9 lines

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 263

Quoted 6 lines
arch/arm64/kvm/id_regs.c +2 -4
-     if (valid_pmu)
-             vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
-     else
-             vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+     if (valid_pmu) {
+             mutex_lock(&vcpu->kvm->lock);

Bingo!

Will fix it.

Quoted 16 lines
Quoted 5 lines
arch/arm64/kvm/id_regs.c +5 -0
+             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
+                     ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |=
+                     FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 278

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] &=
+                     ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] |= FIELD_PREP(
+                             ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver_to_perfmon(pmuver));
+             mutex_unlock(&vcpu->kvm->lock);
+     } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+             set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+     } else {
+             clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+     }
Quoted 5 lines

The last two cases are better written as:

        assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags,
                   pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF);

Will do.

Quoted 7 lines
Quoted 7 lines
arch/arm64/kvm/id_regs.c +0 -0
      return 0;
 }
@@ -296,10 +312,23 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
      if (val)
              return -EINVAL;
 
Quoted 8 lines

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 316

Quoted 6 lines
arch/arm64/kvm/id_regs.c +2 -4
-     if (valid_pmu)
-             vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-     else
-             vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+     if (valid_pmu) {
+             mutex_lock(&vcpu->kvm->lock);

Same here (lock inversion)

Will fix it.

Quoted 17 lines
Quoted 6 lines
arch/arm64/kvm/id_regs.c +5 -0
+             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] &=
+                     ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] |= FIELD_PREP(
+                     ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon);
+

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 331

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
+                     ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |= FIELD_PREP(
+                     ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+             mutex_unlock(&vcpu->kvm->lock);
+     } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+             set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+     } else {
+             clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+     }
Quoted 1 line

Same here (assign_bit).

Will do.

Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/id_regs.c +0 -0
      return 0;
 }
@@ -543,4 +572,14 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
      }
 
Quoted 13 lines

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 584

Quoted 11 lines
arch/arm64/kvm/id_regs.c +10 -0
      kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = val;
+
+     /*
+      * Initialise the default PMUver before there is a chance to
+      * create an actual PMU.
+      */
+     kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
+             ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+     kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |=
+             FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                        kvm_arm_pmu_get_pmuver_limit());

Please put these assignments on a single line...

Will do.

Quoted 17 lines
Quoted 7 lines
arch/arm64/kvm/id_regs.c +0 -0
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..51c7f3e7bdde 100644

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 97

Quoted 8 lines
include/kvm/arm_pmu.h +3 -2
@@ -92,8 +92,9 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
  */
-#define kvm_pmu_is_3p5(vcpu)                                         \
-     (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)                                                                 \
+      (FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),                                 \
+              vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)]) >= ID_AA64DFR0_EL1_PMUVer_V3P5)

I'll stop mentioning the need for accessors...

Will fix it.

Quoted 7 lines

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.

Thanks,
Jing

author
#19 lore raw

PATCH v4 5/6 KVM: arm64: Introduce ID register specific descriptor

In reply to Jing Zhang

Introduce an ID feature register specific descriptor to include ID register specific fields and callbacks besides its corresponding general system register descriptor.
New fields for ID register descriptor would be added later when it is necessary to support a writable ID register.

No functional change intended.

---
 arch/arm64/kvm/id_regs.c  | 187 +++++++++++++++++++++++++++-----------
 arch/arm64/kvm/sys_regs.c |   2 +-
 arch/arm64/kvm/sys_regs.h |   1 +
 3 files changed, 138 insertions(+), 52 deletions(-)
arch/arm64/kvm/id_regs.c +136 -51
index 3a87a3d2390d..9956c99d20f7 100644
@@ -18,6 +18,10 @@
 
 #include "sys_regs.h"
 
+struct id_reg_desc {
+	const struct sys_reg_desc	reg_desc;
+};
+
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
 	if (kvm_vcpu_has_pmu(vcpu))
@@ -334,21 +338,25 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define ID_SANITISED(name) {			\
-	SYS_DESC(SYS_##name),			\
-	.access	= access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = id_visibility,		\
+#define ID_SANITISED(name) {				\
+	.reg_desc = {					\
+		SYS_DESC(SYS_##name),			\
+		.access	= access_id_reg,		\
+		.get_user = get_id_reg,			\
+		.set_user = set_id_reg,			\
+		.visibility = id_visibility,		\
+	},						\
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define AA32_ID_SANITISED(name) {		\
-	SYS_DESC(SYS_##name),			\
-	.access	= access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = aa32_id_visibility,	\
+#define AA32_ID_SANITISED(name) {			\
+	.reg_desc = {					\
+		SYS_DESC(SYS_##name),			\
+		.access	= access_id_reg,		\
+		.get_user = get_id_reg,			\
+		.set_user = set_id_reg,			\
+		.visibility = aa32_id_visibility,	\
+	},						\
 }
 
 /*
@@ -356,12 +364,14 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2
  * (1 <= crm < 8, 0 <= Op2 < 8).
  */
-#define ID_UNALLOCATED(crm, op2) {			\
-	Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),	\
-	.access = access_id_reg,			\
-	.get_user = get_id_reg,				\
-	.set_user = set_id_reg,				\
-	.visibility = raz_visibility			\
+#define ID_UNALLOCATED(crm, op2) {				\
+	.reg_desc = {						\
+		Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),	\
+		.access = access_id_reg,			\
+		.get_user = get_id_reg,				\
+		.set_user = set_id_reg,				\
+		.visibility = raz_visibility			\
+	},							\
 }
 
 /*
@@ -369,15 +379,17 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * For now, these are exposed just like unallocated ID regs: they appear
  * RAZ for the guest.
  */
-#define ID_HIDDEN(name) {			\
-	SYS_DESC(SYS_##name),			\
-	.access = access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = raz_visibility,		\
+#define ID_HIDDEN(name) {				\
+	.reg_desc = {					\
+		SYS_DESC(SYS_##name),			\
+		.access = access_id_reg,		\
+		.get_user = get_id_reg,			\
+		.set_user = set_id_reg,			\
+		.visibility = raz_visibility,		\
+	},						\
 }
 
-static const struct sys_reg_desc id_reg_descs[] = {
+static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
 	/*
 	 * ID regs: all ID_SANITISED() entries here must have corresponding
 	 * entries in arm64_ftr_regs[].
@@ -387,9 +399,13 @@ static const struct sys_reg_desc id_reg_descs[] = {
 	/* CRm=1 */
 	AA32_ID_SANITISED(ID_PFR0_EL1),
 	AA32_ID_SANITISED(ID_PFR1_EL1),
-	{ SYS_DESC(SYS_ID_DFR0_EL1), .access = access_id_reg,
-	  .get_user = get_id_reg, .set_user = set_id_dfr0_el1,
-	  .visibility = aa32_id_visibility, },
+	{ .reg_desc = {
+		SYS_DESC(SYS_ID_DFR0_EL1),
+		.access = access_id_reg,
+		.get_user = get_id_reg,
+		.set_user = set_id_dfr0_el1,
+		.visibility = aa32_id_visibility, },
+	},
 	ID_HIDDEN(ID_AFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -418,8 +434,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
 
 	/* AArch64 ID registers */
 	/* CRm=4 */
-	{ SYS_DESC(SYS_ID_AA64PFR0_EL1), .access = access_id_reg,
-	  .get_user = get_id_reg, .set_user = set_id_aa64pfr0_el1, },
+	{ .reg_desc = {
+		SYS_DESC(SYS_ID_AA64PFR0_EL1),
+		.access = access_id_reg,
+		.get_user = get_id_reg,
+		.set_user = set_id_aa64pfr0_el1, },
+	},
 	ID_SANITISED(ID_AA64PFR1_EL1),
 	ID_UNALLOCATED(4, 2),
 	ID_UNALLOCATED(4, 3),
@@ -429,8 +449,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
 	ID_UNALLOCATED(4, 7),
 
 	/* CRm=5 */
-	{ SYS_DESC(SYS_ID_AA64DFR0_EL1), .access = access_id_reg,
-	  .get_user = get_id_reg, .set_user = set_id_aa64dfr0_el1, },
+	{ .reg_desc = {
+		SYS_DESC(SYS_ID_AA64DFR0_EL1),
+		.access = access_id_reg,
+		.get_user = get_id_reg,
+		.set_user = set_id_aa64dfr0_el1, },
+	},
 	ID_SANITISED(ID_AA64DFR1_EL1),
 	ID_UNALLOCATED(5, 2),
 	ID_UNALLOCATED(5, 3),
@@ -469,12 +493,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
  */
 int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 {
-	const struct sys_reg_desc *r;
+	u32 id;
 
-	r = find_reg(params, id_reg_descs, ARRAY_SIZE(id_reg_descs));
+	id = reg_to_encoding(params);
 
-	if (likely(r)) {
-		perform_access(vcpu, params, r);
+	if (likely(is_id_reg(id))) {
+		perform_access(vcpu, params, &id_reg_descs[IDREG_IDX(id)].reg_desc);
 	} else {
 		print_sys_reg_msg(params,
 				  "Unsupported guest id_reg access at: %lx [%08lx]\n",
@@ -491,38 +515,102 @@ void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu)
 	unsigned long i;
 
 	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++)
-		if (id_reg_descs[i].reset)
-			id_reg_descs[i].reset(vcpu, &id_reg_descs[i]);
+		if (id_reg_descs[i].reg_desc.reset)
+			id_reg_descs[i].reg_desc.reset(vcpu, &id_reg_descs[i].reg_desc);
 }
 
 int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-	return kvm_sys_reg_get_user(vcpu, reg,
-				    id_reg_descs, ARRAY_SIZE(id_reg_descs));
+	u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+	const struct sys_reg_desc *r;
+	struct sys_reg_params params;
+	u64 val;
+	int ret;
+	u32 id;
+
+	if (!index_to_params(reg->id, &params))
+		return -ENOENT;
+	id = reg_to_encoding(&params);
+
+	if (!is_id_reg(id))
+		return -ENOENT;
+
+	r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+	if (r->get_user) {
+		ret = (r->get_user)(vcpu, r, &val);
+	} else {
+		ret = 0;
+		val = vcpu->kvm->arch.id_regs[IDREG_IDX(id)];
+	}
+
+	if (!ret)
+		ret = put_user(val, uaddr);
+
+	return ret;
 }
 
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-	return kvm_sys_reg_set_user(vcpu, reg,
-				    id_reg_descs, ARRAY_SIZE(id_reg_descs));
+	u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+	const struct sys_reg_desc *r;
+	struct sys_reg_params params;
+	u64 val;
+	int ret;
+	u32 id;
+
+	if (!index_to_params(reg->id, &params))
+		return -ENOENT;
+	id = reg_to_encoding(&params);
+
+	if (!is_id_reg(id))
+		return -ENOENT;
+
+	if (get_user(val, uaddr))
+		return -EFAULT;
+
+	r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+
+	if (sysreg_user_write_ignore(vcpu, r))
+		return 0;
+
+	if (r->set_user) {
+		ret = (r->set_user)(vcpu, r, val);
+	} else {
+		WARN_ONCE(1, "ID register set_user callback is NULL\n");
+		ret = 0;
+	}
+
+	return ret;
 }
 
 bool kvm_arm_check_idreg_table(void)
 {
-	return check_sysreg_table(id_reg_descs, ARRAY_SIZE(id_reg_descs), false);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+		const struct sys_reg_desc *r = &id_reg_descs[i].reg_desc;
+
+		if (!is_id_reg(reg_to_encoding(r))) {
+			kvm_err("id_reg table %pS entry %d not set correctly\n",
+				&id_reg_descs[i].reg_desc, i);
+			return false;
+		}
+	}
+
+	return true;
 }
 
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 {
-	const struct sys_reg_desc *i2, *end2;
+	const struct id_reg_desc *i2, *end2;
 	unsigned int total = 0;
 	int err;
 
 	i2 = id_reg_descs;
 	end2 = id_reg_descs + ARRAY_SIZE(id_reg_descs);
 
-	while (i2 != end2) {
-		err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
+	for (; i2 != end2; i2++) {
+		err = walk_one_sys_reg(vcpu, &(i2->reg_desc), &uind, &total);
 		if (err)
 			return err;
 	}
@@ -540,12 +628,9 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
 	u64 val;
 
 	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
-		id = reg_to_encoding(&id_reg_descs[i]);
-		if (WARN_ON_ONCE(!is_id_reg(id)))
-			/* Shouldn't happen */
-			continue;
+		id = reg_to_encoding(&id_reg_descs[i].reg_desc);
 
-		if (id_reg_descs[i].visibility == raz_visibility)
+		if (id_reg_descs[i].reg_desc.visibility == raz_visibility)
 			/* Hidden or reserved ID register */
 			continue;
 
arch/arm64/kvm/sys_regs.c +1 -1
index 3243c924527e..608a0378bdae 100644
@@ -2519,7 +2519,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
  * Userspace API
  *****************************************************************************/
 
-static bool index_to_params(u64 id, struct sys_reg_params *params)
+bool index_to_params(u64 id, struct sys_reg_params *params)
 {
 	switch (id & KVM_REG_SIZE_MASK) {
 	case KVM_REG_SIZE_U64:
arch/arm64/kvm/sys_regs.h +1 -0
index ee136ba28fa5..8fd0020c67ca 100644
@@ -239,6 +239,7 @@ static inline bool is_id_reg(u32 id)
 
 void perform_access(struct kvm_vcpu *vcpu, struct sys_reg_params *params,
 		    const struct sys_reg_desc *r);
+bool index_to_params(u64 id, struct sys_reg_params *params);
 const struct sys_reg_desc *get_reg_by_id(u64 id,
 					 const struct sys_reg_desc table[],
 					 unsigned int num);
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
maintainer
#20 lore raw

Re: [PATCH v4 5/6] KVM: arm64: Introduce ID register specific descriptor

Quoted 6 lines · Jing Zhang

Introduce an ID feature register specific descriptor to include ID register specific fields and callbacks besides its corresponding general system register descriptor.
New fields for ID register descriptor would be added later when it is necessary to support a writable ID register.

What would these be? Could they make sense for "normal" sysregs as well?

Quoted 17 lines

No functional change intended.

---
 arch/arm64/kvm/id_regs.c  | 187 +++++++++++++++++++++++++++-----------
 arch/arm64/kvm/sys_regs.c |   2 +-
 arch/arm64/kvm/sys_regs.h |   1 +
 3 files changed, 138 insertions(+), 52 deletions(-)
arch/arm64/kvm/id_regs.c +0 -0
index 3a87a3d2390d..9956c99d20f7 100644

arch/arm64/kvm/id_regs.c line 24

Quoted 7 lines
arch/arm64/kvm/id_regs.c +4 -0
@@ -18,6 +18,10 @@
 
 #include "sys_regs.h"
 
+struct id_reg_desc {
+	const struct sys_reg_desc	reg_desc;
+};
+

What is the advantage in having this wrapping structure that forces us to reinvent the wheel (the structure is different) over an additional pointer or even a side table?

Quoted 138 lines
arch/arm64/kvm/id_regs.c +52 -32
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
 	if (kvm_vcpu_has_pmu(vcpu))
@@ -334,21 +338,25 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define ID_SANITISED(name) {			\
-	SYS_DESC(SYS_##name),			\
-	.access	= access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = id_visibility,		\
+#define ID_SANITISED(name) {				\
+	.reg_desc = {					\
+		SYS_DESC(SYS_##name),			\
+		.access	= access_id_reg,		\
+		.get_user = get_id_reg,			\
+		.set_user = set_id_reg,			\
+		.visibility = id_visibility,		\
+	},						\
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define AA32_ID_SANITISED(name) {		\
-	SYS_DESC(SYS_##name),			\
-	.access	= access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = aa32_id_visibility,	\
+#define AA32_ID_SANITISED(name) {			\
+	.reg_desc = {					\
+		SYS_DESC(SYS_##name),			\
+		.access	= access_id_reg,		\
+		.get_user = get_id_reg,			\
+		.set_user = set_id_reg,			\
+		.visibility = aa32_id_visibility,	\
+	},						\
 }
 
 /*
@@ -356,12 +364,14 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2
  * (1 <= crm < 8, 0 <= Op2 < 8).
  */
-#define ID_UNALLOCATED(crm, op2) {			\
-	Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),	\
-	.access = access_id_reg,			\
-	.get_user = get_id_reg,				\
-	.set_user = set_id_reg,				\
-	.visibility = raz_visibility			\
+#define ID_UNALLOCATED(crm, op2) {				\
+	.reg_desc = {						\
+		Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),	\
+		.access = access_id_reg,			\
+		.get_user = get_id_reg,				\
+		.set_user = set_id_reg,				\
+		.visibility = raz_visibility			\
+	},							\
 }
 
 /*
@@ -369,15 +379,17 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * For now, these are exposed just like unallocated ID regs: they appear
  * RAZ for the guest.
  */
-#define ID_HIDDEN(name) {			\
-	SYS_DESC(SYS_##name),			\
-	.access = access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = raz_visibility,		\
+#define ID_HIDDEN(name) {				\
+	.reg_desc = {					\
+		SYS_DESC(SYS_##name),			\
+		.access = access_id_reg,		\
+		.get_user = get_id_reg,			\
+		.set_user = set_id_reg,			\
+		.visibility = raz_visibility,		\
+	},						\
 }
 
-static const struct sys_reg_desc id_reg_descs[] = {
+static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
 	/*
 	 * ID regs: all ID_SANITISED() entries here must have corresponding
 	 * entries in arm64_ftr_regs[].
@@ -387,9 +399,13 @@ static const struct sys_reg_desc id_reg_descs[] = {
 	/* CRm=1 */
 	AA32_ID_SANITISED(ID_PFR0_EL1),
 	AA32_ID_SANITISED(ID_PFR1_EL1),
-	{ SYS_DESC(SYS_ID_DFR0_EL1), .access = access_id_reg,
-	  .get_user = get_id_reg, .set_user = set_id_dfr0_el1,
-	  .visibility = aa32_id_visibility, },
+	{ .reg_desc = {
+		SYS_DESC(SYS_ID_DFR0_EL1),
+		.access = access_id_reg,
+		.get_user = get_id_reg,
+		.set_user = set_id_dfr0_el1,
+		.visibility = aa32_id_visibility, },
+	},
 	ID_HIDDEN(ID_AFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -418,8 +434,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
 
 	/* AArch64 ID registers */
 	/* CRm=4 */
-	{ SYS_DESC(SYS_ID_AA64PFR0_EL1), .access = access_id_reg,
-	  .get_user = get_id_reg, .set_user = set_id_aa64pfr0_el1, },
+	{ .reg_desc = {
+		SYS_DESC(SYS_ID_AA64PFR0_EL1),
+		.access = access_id_reg,
+		.get_user = get_id_reg,
+		.set_user = set_id_aa64pfr0_el1, },
+	},
 	ID_SANITISED(ID_AA64PFR1_EL1),
 	ID_UNALLOCATED(4, 2),
 	ID_UNALLOCATED(4, 3),
@@ -429,8 +449,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
 	ID_UNALLOCATED(4, 7),
 
 	/* CRm=5 */
-	{ SYS_DESC(SYS_ID_AA64DFR0_EL1), .access = access_id_reg,
-	  .get_user = get_id_reg, .set_user = set_id_aa64dfr0_el1, },
+	{ .reg_desc = {
+		SYS_DESC(SYS_ID_AA64DFR0_EL1),
+		.access = access_id_reg,
+		.get_user = get_id_reg,
+		.set_user = set_id_aa64dfr0_el1, },
+	},
 	ID_SANITISED(ID_AA64DFR1_EL1),
 	ID_UNALLOCATED(5, 2),
 	ID_UNALLOCATED(5, 3),
@@ -469,12 +493,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
  */
 int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 {

arch/arm64/kvm/id_regs.c static const struct sys_reg_desc id_reg_descs[] = { · line 501

Quoted 10 lines
arch/arm64/kvm/id_regs.c +4 -4
-	const struct sys_reg_desc *r;
+	u32 id;
 
-	r = find_reg(params, id_reg_descs, ARRAY_SIZE(id_reg_descs));
+	id = reg_to_encoding(params);
 
-	if (likely(r)) {
-		perform_access(vcpu, params, r);
+	if (likely(is_id_reg(id))) {
+		perform_access(vcpu, params, &id_reg_descs[IDREG_IDX(id)].reg_desc);

How about minimising the diff and making the whole thing less verbose?

static const struct sys_reg_desc *id_to_id_reg_desc(struct sys_reg_params *params)

{
	u32 id;

	id = reg_to_encoding(params);
	if (is_id_reg(id))
		return &id_reg_descs[IDREG_IDX(id)].reg_desc;

	return NULL;
}

int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)

{
	const struct sys_reg_desc *r;

	r = id_to_id_reg_desc(params);
	[...]
}

And use the helper everywhere?

Quoted 31 lines
arch/arm64/kvm/id_regs.c +16 -4
 	} else {
 		print_sys_reg_msg(params,
 				  "Unsupported guest id_reg access at: %lx [%08lx]\n",
@@ -491,38 +515,102 @@ void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu)
 	unsigned long i;
 
 	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++)
-		if (id_reg_descs[i].reset)
-			id_reg_descs[i].reset(vcpu, &id_reg_descs[i]);
+		if (id_reg_descs[i].reg_desc.reset)
+			id_reg_descs[i].reg_desc.reset(vcpu, &id_reg_descs[i].reg_desc);
 }
 
 int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-	return kvm_sys_reg_get_user(vcpu, reg,
-				    id_reg_descs, ARRAY_SIZE(id_reg_descs));
+	u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+	const struct sys_reg_desc *r;
+	struct sys_reg_params params;
+	u64 val;
+	int ret;
+	u32 id;
+
+	if (!index_to_params(reg->id, &params))
+		return -ENOENT;
+	id = reg_to_encoding(&params);
+
+	if (!is_id_reg(id))
+		return -ENOENT;
+

arch/arm64/kvm/id_regs.c void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu) · line 547

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+	r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+	if (r->get_user) {
+		ret = (r->get_user)(vcpu, r, &val);
+	} else {
+		ret = 0;
+		val = vcpu->kvm->arch.id_regs[IDREG_IDX(id)];
+	}
+
+	if (!ret)
+		ret = put_user(val, uaddr);

How about the visibility? Why isn't it checked?

Quoted 24 lines
arch/arm64/kvm/id_regs.c +18 -2
+
+	return ret;
 }
 
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-	return kvm_sys_reg_set_user(vcpu, reg,
-				    id_reg_descs, ARRAY_SIZE(id_reg_descs));
+	u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+	const struct sys_reg_desc *r;
+	struct sys_reg_params params;
+	u64 val;
+	int ret;
+	u32 id;
+
+	if (!index_to_params(reg->id, &params))
+		return -ENOENT;
+	id = reg_to_encoding(&params);
+
+	if (!is_id_reg(id))
+		return -ENOENT;
+
+	if (get_user(val, uaddr))
+		return -EFAULT;

arch/arm64/kvm/id_regs.c void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu) · line 579

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+
+	r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+
+	if (sysreg_user_write_ignore(vcpu, r))
+		return 0;
+
+	if (r->set_user) {
+		ret = (r->set_user)(vcpu, r, val);
+	} else {
+		WARN_ONCE(1, "ID register set_user callback is NULL\n");

Why the shouting? We didn't do that before. What's changed?

Quoted 42 lines
arch/arm64/kvm/id_regs.c +20 -4
+		ret = 0;
+	}
+
+	return ret;
 }
 
 bool kvm_arm_check_idreg_table(void)
 {
-	return check_sysreg_table(id_reg_descs, ARRAY_SIZE(id_reg_descs), false);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+		const struct sys_reg_desc *r = &id_reg_descs[i].reg_desc;
+
+		if (!is_id_reg(reg_to_encoding(r))) {
+			kvm_err("id_reg table %pS entry %d not set correctly\n",
+				&id_reg_descs[i].reg_desc, i);
+			return false;
+		}
+	}
+
+	return true;
 }
 
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 {
-	const struct sys_reg_desc *i2, *end2;
+	const struct id_reg_desc *i2, *end2;
 	unsigned int total = 0;
 	int err;
 
 	i2 = id_reg_descs;
 	end2 = id_reg_descs + ARRAY_SIZE(id_reg_descs);
 
-	while (i2 != end2) {
-		err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
+	for (; i2 != end2; i2++) {
+		err = walk_one_sys_reg(vcpu, &(i2->reg_desc), &uind, &total);
 		if (err)
 			return err;
 	}

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 631

Quoted 8 lines
arch/arm64/kvm/id_regs.c +1 -4
@@ -540,12 +628,9 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
 	u64 val;
 
 	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
-		id = reg_to_encoding(&id_reg_descs[i]);
-		if (WARN_ON_ONCE(!is_id_reg(id)))
-			/* Shouldn't happen */
-			continue;
+		id = reg_to_encoding(&id_reg_descs[i].reg_desc);

Why have you dropped that check? If it shouldn't happen before, it still shouldn't happen.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#21 lore raw

Re: [PATCH v4 5/6] KVM: arm64: Introduce ID register specific descriptor

Hi Marc,

Quoted 4 lines · Marc Zyngier
Quoted 1 line · Jing Zhang
Quoted 8 lines
Quoted 5 lines

Introduce an ID feature register specific descriptor to include ID register specific fields and callbacks besides its corresponding general system register descriptor.
New fields for ID register descriptor would be added later when it is necessary to support a writable ID register.

What would these be? Could they make sense for "normal" sysregs as well?

As you know from the later patch, some fields are added only applicable to idregs.
As you suggested, some of those fields may not be necessary, I'll try to improve the data idregs specific data structures based on your comments or even don't use a new structure if it is possible.

Quoted 21 lines
Quoted 21 lines

No functional change intended.

---
 arch/arm64/kvm/id_regs.c  | 187 +++++++++++++++++++++++++++-----------
 arch/arm64/kvm/sys_regs.c |   2 +-
 arch/arm64/kvm/sys_regs.h |   1 +
 3 files changed, 138 insertions(+), 52 deletions(-)
arch/arm64/kvm/id_regs.c +0 -0
index 3a87a3d2390d..9956c99d20f7 100644
@@ -18,6 +18,10 @@
 
 #include "sys_regs.h"
 
Quoted 8 lines

arch/arm64/kvm/id_regs.c line 24

Quoted 4 lines
arch/arm64/kvm/id_regs.c +4 -0
+struct id_reg_desc {
+     const struct sys_reg_desc       reg_desc;
+};
+

What is the advantage in having this wrapping structure that forces us to reinvent the wheel (the structure is different) over an additional pointer or even a side table?

As stated in the last comment, I'll try to improve the data structure or don't use it at all if possible.

Quoted 164 lines
Quoted 139 lines
arch/arm64/kvm/id_regs.c +52 -32
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
      if (kvm_vcpu_has_pmu(vcpu))
@@ -334,21 +338,25 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define ID_SANITISED(name) {                 \
-     SYS_DESC(SYS_##name),                   \
-     .access = access_id_reg,                \
-     .get_user = get_id_reg,                 \
-     .set_user = set_id_reg,                 \
-     .visibility = id_visibility,            \
+#define ID_SANITISED(name) {                         \
+     .reg_desc = {                                   \
+             SYS_DESC(SYS_##name),                   \
+             .access = access_id_reg,                \
+             .get_user = get_id_reg,                 \
+             .set_user = set_id_reg,                 \
+             .visibility = id_visibility,            \
+     },                                              \
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define AA32_ID_SANITISED(name) {            \
-     SYS_DESC(SYS_##name),                   \
-     .access = access_id_reg,                \
-     .get_user = get_id_reg,                 \
-     .set_user = set_id_reg,                 \
-     .visibility = aa32_id_visibility,       \
+#define AA32_ID_SANITISED(name) {                    \
+     .reg_desc = {                                   \
+             SYS_DESC(SYS_##name),                   \
+             .access = access_id_reg,                \
+             .get_user = get_id_reg,                 \
+             .set_user = set_id_reg,                 \
+             .visibility = aa32_id_visibility,       \
+     },                                              \
 }
 
 /*
@@ -356,12 +364,14 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2
  * (1 <= crm < 8, 0 <= Op2 < 8).
  */
-#define ID_UNALLOCATED(crm, op2) {                   \
-     Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),     \
-     .access = access_id_reg,                        \
-     .get_user = get_id_reg,                         \
-     .set_user = set_id_reg,                         \
-     .visibility = raz_visibility                    \
+#define ID_UNALLOCATED(crm, op2) {                           \
+     .reg_desc = {                                           \
+             Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),     \
+             .access = access_id_reg,                        \
+             .get_user = get_id_reg,                         \
+             .set_user = set_id_reg,                         \
+             .visibility = raz_visibility                    \
+     },                                                      \
 }
 
 /*
@@ -369,15 +379,17 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * For now, these are exposed just like unallocated ID regs: they appear
  * RAZ for the guest.
  */
-#define ID_HIDDEN(name) {                    \
-     SYS_DESC(SYS_##name),                   \
-     .access = access_id_reg,                \
-     .get_user = get_id_reg,                 \
-     .set_user = set_id_reg,                 \
-     .visibility = raz_visibility,           \
+#define ID_HIDDEN(name) {                            \
+     .reg_desc = {                                   \
+             SYS_DESC(SYS_##name),                   \
+             .access = access_id_reg,                \
+             .get_user = get_id_reg,                 \
+             .set_user = set_id_reg,                 \
+             .visibility = raz_visibility,           \
+     },                                              \
 }
 
-static const struct sys_reg_desc id_reg_descs[] = {
+static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
      /*
       * ID regs: all ID_SANITISED() entries here must have corresponding
       * entries in arm64_ftr_regs[].
@@ -387,9 +399,13 @@ static const struct sys_reg_desc id_reg_descs[] = {
      /* CRm=1 */
      AA32_ID_SANITISED(ID_PFR0_EL1),
      AA32_ID_SANITISED(ID_PFR1_EL1),
-     { SYS_DESC(SYS_ID_DFR0_EL1), .access = access_id_reg,
-       .get_user = get_id_reg, .set_user = set_id_dfr0_el1,
-       .visibility = aa32_id_visibility, },
+     { .reg_desc = {
+             SYS_DESC(SYS_ID_DFR0_EL1),
+             .access = access_id_reg,
+             .get_user = get_id_reg,
+             .set_user = set_id_dfr0_el1,
+             .visibility = aa32_id_visibility, },
+     },
      ID_HIDDEN(ID_AFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -418,8 +434,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
 
      /* AArch64 ID registers */
      /* CRm=4 */
-     { SYS_DESC(SYS_ID_AA64PFR0_EL1), .access = access_id_reg,
-       .get_user = get_id_reg, .set_user = set_id_aa64pfr0_el1, },
+     { .reg_desc = {
+             SYS_DESC(SYS_ID_AA64PFR0_EL1),
+             .access = access_id_reg,
+             .get_user = get_id_reg,
+             .set_user = set_id_aa64pfr0_el1, },
+     },
      ID_SANITISED(ID_AA64PFR1_EL1),
      ID_UNALLOCATED(4, 2),
      ID_UNALLOCATED(4, 3),
@@ -429,8 +449,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
      ID_UNALLOCATED(4, 7),
 
      /* CRm=5 */
-     { SYS_DESC(SYS_ID_AA64DFR0_EL1), .access = access_id_reg,
-       .get_user = get_id_reg, .set_user = set_id_aa64dfr0_el1, },
+     { .reg_desc = {
+             SYS_DESC(SYS_ID_AA64DFR0_EL1),
+             .access = access_id_reg,
+             .get_user = get_id_reg,
+             .set_user = set_id_aa64dfr0_el1, },
+     },
      ID_SANITISED(ID_AA64DFR1_EL1),
      ID_UNALLOCATED(5, 2),
      ID_UNALLOCATED(5, 3),
@@ -469,12 +493,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
  */
 int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 {

arch/arm64/kvm/id_regs.c static const struct sys_reg_desc id_reg_descs[] = { · line 501

Quoted 10 lines
arch/arm64/kvm/id_regs.c +4 -4
-     const struct sys_reg_desc *r;
+     u32 id;
 
-     r = find_reg(params, id_reg_descs, ARRAY_SIZE(id_reg_descs));
+     id = reg_to_encoding(params);
 
-     if (likely(r)) {
-             perform_access(vcpu, params, r);
+     if (likely(is_id_reg(id))) {
+             perform_access(vcpu, params, &id_reg_descs[IDREG_IDX(id)].reg_desc);

How about minimising the diff and making the whole thing less verbose?

static const struct sys_reg_desc *id_to_id_reg_desc(struct sys_reg_params *params)

{
        u32 id;

        id = reg_to_encoding(params);
        if (is_id_reg(id))
                return &id_reg_descs[IDREG_IDX(id)].reg_desc;

        return NULL;
}

int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)

Quoted 8 lines
{
        const struct sys_reg_desc *r;

        r = id_to_id_reg_desc(params);
        [...]
}

And use the helper everywhere?

Sure, will do.

Quoted 36 lines
Quoted 36 lines
arch/arm64/kvm/id_regs.c +20 -4
      } else {
              print_sys_reg_msg(params,
                                "Unsupported guest id_reg access at: %lx [%08lx]\n",
@@ -491,38 +515,102 @@ void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu)
      unsigned long i;
 
      for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++)
-             if (id_reg_descs[i].reset)
-                     id_reg_descs[i].reset(vcpu, &id_reg_descs[i]);
+             if (id_reg_descs[i].reg_desc.reset)
+                     id_reg_descs[i].reg_desc.reset(vcpu, &id_reg_descs[i].reg_desc);
 }
 
 int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-     return kvm_sys_reg_get_user(vcpu, reg,
-                                 id_reg_descs, ARRAY_SIZE(id_reg_descs));
+     u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+     const struct sys_reg_desc *r;
+     struct sys_reg_params params;
+     u64 val;
+     int ret;
+     u32 id;
+
+     if (!index_to_params(reg->id, &params))
+             return -ENOENT;
+     id = reg_to_encoding(&params);
+
+     if (!is_id_reg(id))
+             return -ENOENT;
+
+     r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+     if (r->get_user) {
+             ret = (r->get_user)(vcpu, r, &val);
+     } else {
Quoted 8 lines

arch/arm64/kvm/id_regs.c void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu) · line 547

Quoted 6 lines
arch/arm64/kvm/id_regs.c +6 -0
+             ret = 0;
+             val = vcpu->kvm->arch.id_regs[IDREG_IDX(id)];
+     }
+
+     if (!ret)
+             ret = put_user(val, uaddr);

How about the visibility? Why isn't it checked?

The visibility check is done in get_user()->get_id_reg()->read_id_reg().

Quoted 29 lines
Quoted 29 lines
arch/arm64/kvm/id_regs.c +22 -2
+
+     return ret;
 }
 
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-     return kvm_sys_reg_set_user(vcpu, reg,
-                                 id_reg_descs, ARRAY_SIZE(id_reg_descs));
+     u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+     const struct sys_reg_desc *r;
+     struct sys_reg_params params;
+     u64 val;
+     int ret;
+     u32 id;
+
+     if (!index_to_params(reg->id, &params))
+             return -ENOENT;
+     id = reg_to_encoding(&params);
+
+     if (!is_id_reg(id))
+             return -ENOENT;
+
+     if (get_user(val, uaddr))
+             return -EFAULT;
+
+     r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+
+     if (sysreg_user_write_ignore(vcpu, r))
Quoted 8 lines

arch/arm64/kvm/id_regs.c void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu) · line 579

Quoted 6 lines
arch/arm64/kvm/id_regs.c +6 -0
+             return 0;
+
+     if (r->set_user) {
+             ret = (r->set_user)(vcpu, r, val);
+     } else {
+             WARN_ONCE(1, "ID register set_user callback is NULL\n");

Why the shouting? We didn't do that before. What's changed?

It was added according to one of Reiji's comments from
https://lore.kernel.org/all/CAAeT=Fz-G_EUmh=Pj3UHA7pnKKYi7UyYuedziJxfmSoKpntw3Q@mail.gmail.com.
WDYT?

Quoted 45 lines
Quoted 45 lines
arch/arm64/kvm/id_regs.c +20 -4
+             ret = 0;
+     }
+
+     return ret;
 }
 
 bool kvm_arm_check_idreg_table(void)
 {
-     return check_sysreg_table(id_reg_descs, ARRAY_SIZE(id_reg_descs), false);
+     unsigned int i;
+
+     for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+             const struct sys_reg_desc *r = &id_reg_descs[i].reg_desc;
+
+             if (!is_id_reg(reg_to_encoding(r))) {
+                     kvm_err("id_reg table %pS entry %d not set correctly\n",
+                             &id_reg_descs[i].reg_desc, i);
+                     return false;
+             }
+     }
+
+     return true;
 }
 
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 {
-     const struct sys_reg_desc *i2, *end2;
+     const struct id_reg_desc *i2, *end2;
      unsigned int total = 0;
      int err;
 
      i2 = id_reg_descs;
      end2 = id_reg_descs + ARRAY_SIZE(id_reg_descs);
 
-     while (i2 != end2) {
-             err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
+     for (; i2 != end2; i2++) {
+             err = walk_one_sys_reg(vcpu, &(i2->reg_desc), &uind, &total);
              if (err)
                      return err;
      }
@@ -540,12 +628,9 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
      u64 val;
 
Quoted 9 lines

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 631

Quoted 6 lines
arch/arm64/kvm/id_regs.c +1 -4
      for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
-             id = reg_to_encoding(&id_reg_descs[i]);
-             if (WARN_ON_ONCE(!is_id_reg(id)))
-                     /* Shouldn't happen */
-                     continue;
+             id = reg_to_encoding(&id_reg_descs[i].reg_desc);

Why have you dropped that check? If it shouldn't happen before, it still shouldn't happen.

Since the check was done in kvm_arm_check_idreg_table() now.

Quoted 7 lines

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.

author
#22 lore raw

PATCH v4 6/6 KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

In reply to Jing Zhang

Save KVM sanitised ID register value in ID descriptor (kvm_sys_val). Add an init callback for every ID register to setup kvm_sys_val. All per VCPU sanitizations are still handled on the fly during ID register read and write from userspace.
An arm64_ftr_bits array is used to indicate writable feature fields.

Refactor writings for ID_AA64PFR0_EL1.[CSV2|CSV3], ID_AA64DFR0_EL1.PMUVer and ID_DFR0_ELF.PerfMon based on utilities introduced by ID register descriptor.

No functional change intended.

---
 arch/arm64/include/asm/cpufeature.h |  25 +++
 arch/arm64/include/asm/kvm_host.h   |   2 +-
 arch/arm64/kernel/cpufeature.c      |  26 +--
 arch/arm64/kvm/arm.c                |   2 +-
 arch/arm64/kvm/id_regs.c            | 325 ++++++++++++++++++++--------
 arch/arm64/kvm/sys_regs.c           |   3 +-
 arch/arm64/kvm/sys_regs.h           |   2 +-
 7 files changed, 261 insertions(+), 124 deletions(-)
arch/arm64/include/asm/cpufeature.h +25 -0
index fc2c739f48f1..493ec530eefc 100644
@@ -64,6 +64,30 @@ struct arm64_ftr_bits {
 	s64		safe_val; /* safe value for FTR_EXACT features */
 };
 
+#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+	{						\
+		.sign = SIGNED,				\
+		.visible = VISIBLE,			\
+		.strict = STRICT,			\
+		.type = TYPE,				\
+		.shift = SHIFT,				\
+		.width = WIDTH,				\
+		.safe_val = SAFE_VAL,			\
+	}
+
+/* Define a feature with unsigned values */
+#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+	__ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
+
+/* Define a feature with a signed value */
+#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+	__ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
+
+#define ARM64_FTR_END					\
+	{						\
+		.width = 0,				\
+	}
+
 /*
  * Describe the early feature override to the core override code:
  *
@@ -911,6 +935,7 @@ static inline unsigned int get_vmid_bits(u64 mmfr1)
 	return 8;
 }
 
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
 struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
 
 extern struct arm64_ftr_override id_aa64mmfr1_override;
arch/arm64/include/asm/kvm_host.h +1 -1
index 102860ba896d..aa83dd79e7ff 100644
@@ -1013,7 +1013,7 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 				struct kvm_arm_copy_mte_tags *copy_tags);
 
-void kvm_arm_set_default_id_regs(struct kvm *kvm);
+void kvm_arm_init_id_regs(struct kvm *kvm);
 
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
arch/arm64/kernel/cpufeature.c +1 -25
index 23bd2a926b74..e18848ee4b98 100644
@@ -139,30 +139,6 @@ void dump_cpu_features(void)
 	pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
 }
 
-#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-	{						\
-		.sign = SIGNED,				\
-		.visible = VISIBLE,			\
-		.strict = STRICT,			\
-		.type = TYPE,				\
-		.shift = SHIFT,				\
-		.width = WIDTH,				\
-		.safe_val = SAFE_VAL,			\
-	}
-
-/* Define a feature with unsigned values */
-#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-	__ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
-
-/* Define a feature with a signed value */
-#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-	__ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
-
-#define ARM64_FTR_END					\
-	{						\
-		.width = 0,				\
-	}
-
 static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
 
 static bool __system_matches_cap(unsigned int n);
@@ -790,7 +766,7 @@ static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
 	return reg;
 }
 
-static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
 				s64 cur)
 {
 	s64 ret = 0;
arch/arm64/kvm/arm.c +1 -1
index fb2de2cb98cb..e539d9ca9d01 100644
@@ -135,7 +135,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	/* The maximum number of VCPUs is limited by the host's GIC model */
 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-	kvm_arm_set_default_id_regs(kvm);
+	kvm_arm_init_id_regs(kvm);
 	kvm_arm_init_hypercalls(kvm);
 
 	return 0;
arch/arm64/kvm/id_regs.c +231 -94

492 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +1 -2
index 608a0378bdae..61b9adfd5d5e 100644
@@ -2912,14 +2912,13 @@ int __init kvm_sys_reg_table_init(void)
 	unsigned int i;
 
 	/* Make sure tables are unique and in order. */
-	valid &= kvm_arm_check_idreg_table();
 	valid &= check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs), false);
 	valid &= check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs), true);
 	valid &= check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs), true);
 	valid &= check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs), true);
 	valid &= check_sysreg_table(cp15_64_regs, ARRAY_SIZE(cp15_64_regs), true);
 	valid &= check_sysreg_table(invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs), false);
-
+	valid &= kvm_arm_idreg_table_init();
 	if (!valid)
 		return -EINVAL;
 
arch/arm64/kvm/sys_regs.h +1 -1
index 8fd0020c67ca..55484d1615be 100644
@@ -260,7 +260,7 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params);
 void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu);
 int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
-bool kvm_arm_check_idreg_table(void);
+bool kvm_arm_idreg_table_init(void);
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind);
 u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id);
 
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
maintainer
#23 lore raw

Re: [PATCH v4 6/6] KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

Quoted 2 lines · Jing Zhang

Save KVM sanitised ID register value in ID descriptor (kvm_sys_val).

Why do we need to store a separate value *beside* the sanitised value the kernel already holds?

Quoted 1 line

Add an init callback for every ID register to setup kvm_sys_val.

Same question.

Quoted 128 lines

All per VCPU sanitizations are still handled on the fly during ID register read and write from userspace.
An arm64_ftr_bits array is used to indicate writable feature fields.

Refactor writings for ID_AA64PFR0_EL1.[CSV2|CSV3], ID_AA64DFR0_EL1.PMUVer and ID_DFR0_ELF.PerfMon based on utilities introduced by ID register descriptor.

No functional change intended.

---
 arch/arm64/include/asm/cpufeature.h |  25 +++
 arch/arm64/include/asm/kvm_host.h   |   2 +-
 arch/arm64/kernel/cpufeature.c      |  26 +--
 arch/arm64/kvm/arm.c                |   2 +-
 arch/arm64/kvm/id_regs.c            | 325 ++++++++++++++++++++--------
 arch/arm64/kvm/sys_regs.c           |   3 +-
 arch/arm64/kvm/sys_regs.h           |   2 +-
 7 files changed, 261 insertions(+), 124 deletions(-)
arch/arm64/include/asm/cpufeature.h +25 -0
index fc2c739f48f1..493ec530eefc 100644
@@ -64,6 +64,30 @@ struct arm64_ftr_bits {
 	s64		safe_val; /* safe value for FTR_EXACT features */
 };
 
+#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+	{						\
+		.sign = SIGNED,				\
+		.visible = VISIBLE,			\
+		.strict = STRICT,			\
+		.type = TYPE,				\
+		.shift = SHIFT,				\
+		.width = WIDTH,				\
+		.safe_val = SAFE_VAL,			\
+	}
+
+/* Define a feature with unsigned values */
+#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+	__ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
+
+/* Define a feature with a signed value */
+#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+	__ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
+
+#define ARM64_FTR_END					\
+	{						\
+		.width = 0,				\
+	}
+
 /*
  * Describe the early feature override to the core override code:
  *
@@ -911,6 +935,7 @@ static inline unsigned int get_vmid_bits(u64 mmfr1)
 	return 8;
 }
 
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
 struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
 
 extern struct arm64_ftr_override id_aa64mmfr1_override;
arch/arm64/include/asm/kvm_host.h +1 -1
index 102860ba896d..aa83dd79e7ff 100644
@@ -1013,7 +1013,7 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 				struct kvm_arm_copy_mte_tags *copy_tags);
 
-void kvm_arm_set_default_id_regs(struct kvm *kvm);
+void kvm_arm_init_id_regs(struct kvm *kvm);
 
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
arch/arm64/kernel/cpufeature.c +1 -25
index 23bd2a926b74..e18848ee4b98 100644
@@ -139,30 +139,6 @@ void dump_cpu_features(void)
 	pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
 }
 
-#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-	{						\
-		.sign = SIGNED,				\
-		.visible = VISIBLE,			\
-		.strict = STRICT,			\
-		.type = TYPE,				\
-		.shift = SHIFT,				\
-		.width = WIDTH,				\
-		.safe_val = SAFE_VAL,			\
-	}
-
-/* Define a feature with unsigned values */
-#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-	__ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
-
-/* Define a feature with a signed value */
-#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-	__ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
-
-#define ARM64_FTR_END					\
-	{						\
-		.width = 0,				\
-	}
-
 static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
 
 static bool __system_matches_cap(unsigned int n);
@@ -790,7 +766,7 @@ static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
 	return reg;
 }
 
-static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
 				s64 cur)
 {
 	s64 ret = 0;
arch/arm64/kvm/arm.c +0 -0
index fb2de2cb98cb..e539d9ca9d01 100644

arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 138

Quoted 5 lines
arch/arm64/kvm/arm.c +1 -1
@@ -135,7 +135,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	/* The maximum number of VCPUs is limited by the host's GIC model */
 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-	kvm_arm_set_default_id_regs(kvm);
+	kvm_arm_init_id_regs(kvm);

How about picking the name once and for all from the first patch?

Quoted 8 lines
arch/arm64/kvm/arm.c +0 -0
 	kvm_arm_init_hypercalls(kvm);
 
 	return 0;
arch/arm64/kvm/id_regs.c +0 -0
index 9956c99d20f7..726b810b6e06 100644

arch/arm64/kvm/id_regs.c line 25

Quoted 8 lines
arch/arm64/kvm/id_regs.c +5 -0
@@ -18,10 +18,88 @@
 
 #include "sys_regs.h"
 
+/*
+ * Number of entries in id_reg_desc's ftr_bits[] (Number of 4 bits fields
+ * in 64 bit register + 1 entry for a terminator entry).
+ */
+#define	FTR_FIELDS_NUM	17

Please see SMFR0_EL1 for an example of a sysreg that doesn't follow the 4bits-per-field format. I expect to see more of those in the future.

And given that this is always a variable set of fields, why do we need to define this as a fixed array that only bloats the structure? I'd rather see a variable array in a side structure.

Quoted 11 lines
arch/arm64/kvm/id_regs.c +9 -0
+
 struct id_reg_desc {
 	const struct sys_reg_desc	reg_desc;
+	/*
+	 * KVM sanitised ID register value.
+	 * It is the default value for per VM emulated ID register.
+	 */
+	u64 kvm_sys_val;
+	/*
+	 * Used to validate the ID register values with arm64_check_features().
+	 * The last item in the array must be terminated by an item whose

arch/arm64/kvm/id_regs.c line 46

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+	 * width field is zero as that is expected by arm64_check_features().
+	 * Only feature bits defined in this array are writable.
+	 */
+	struct arm64_ftr_bits	ftr_bits[FTR_FIELDS_NUM];
+
+	/*
+	 * Basically init() is used to setup the KVM sanitised value
+	 * stored in kvm_sys_val.
+	 */
+	void (*init)(struct id_reg_desc *idr);

Given that this callback only builds the value from the sanitised view, and that it is very cheap (only a set of masking), why do we bother keeping the value around? It would also allow this structure to be kept *const*, something that is extremely desirable.

Also, why do we need an init() method when each sysreg already have a reset() method? Surely this should be the same thing...

My gut feeling is that we should only have a callback returning the limit value computed on the fly.

Quoted 45 lines
arch/arm64/kvm/id_regs.c +43 -0
 };
 
+static struct id_reg_desc id_reg_descs[];
+
+/**
+ * arm64_check_features() - Check if a feature register value constitutes
+ * a subset of features indicated by @limit.
+ *
+ * @ftrp: Pointer to an array of arm64_ftr_bits. It must be terminated by
+ * an item whose width field is zero.
+ * @val: The feature register value to check
+ * @limit: The limit value of the feature register
+ *
+ * This function will check if each feature field of @val is the "safe" value
+ * against @limit based on @ftrp[], each of which specifies the target field
+ * (shift, width), whether or not the field is for a signed value (sign),
+ * how the field is determined to be "safe" (type), and the safe value
+ * (safe_val) when type == FTR_EXACT (safe_val won't be used by this
+ * function when type != FTR_EXACT). Any other fields in arm64_ftr_bits
+ * won't be used by this function. If a field value in @val is the same
+ * as the one in @limit, it is always considered the safe value regardless
+ * of the type. For register fields that are not in @ftrp[], only the value
+ * in @limit is considered the safe value.
+ *
+ * Return: 0 if all the fields are safe. Otherwise, return negative errno.
+ */
+static int arm64_check_features(const struct arm64_ftr_bits *ftrp, u64 val, u64 limit)
+{
+	u64 mask = 0;
+
+	for (; ftrp->width; ftrp++) {
+		s64 f_val, f_lim, safe_val;
+
+		f_val = arm64_ftr_value(ftrp, val);
+		f_lim = arm64_ftr_value(ftrp, limit);
+		mask |= arm64_ftr_mask(ftrp);
+
+		if (f_val == f_lim)
+			safe_val = f_val;
+		else
+			safe_val = arm64_ftr_safe_value(ftrp, f_val, f_lim);
+
+		if (safe_val != f_val)
+			return -E2BIG;
+	}

arch/arm64/kvm/id_regs.c line 101

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+
+	/*
+	 * For fields that are not indicated in ftrp, values in limit are the
+	 * safe values.
+	 */
+	if ((val & ~mask) != (limit & ~mask))
+		return -E2BIG;
+
+	return 0;
+}

I have the feeling that the core code already implements something similar...

Quoted 73 lines
arch/arm64/kvm/id_regs.c +32 -9
+
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
 	if (kvm_vcpu_has_pmu(vcpu))
@@ -67,7 +145,6 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 	case SYS_ID_AA64PFR0_EL1:
 		if (!vcpu_has_sve(vcpu))
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
-		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
 		if (kvm_vgic_global_state.type == VGIC_V3) {
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
 			val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -94,15 +171,10 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 			val &= ~ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_WFxT);
 		break;
 	case SYS_ID_AA64DFR0_EL1:
-		/* Limit debug to ARMv8.0 */
-		val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer);
-		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer), 6);
 		/* Set PMUver to the required version */
 		val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
 		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
 				  vcpu_pmuver(vcpu));
-		/* Hide SPE from guests */
-		val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMSVer);
 		break;
 	case SYS_ID_DFR0_EL1:
 		val &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
@@ -161,9 +233,15 @@ static int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 		      u64 val)
 {
-	/* This is what we mean by invariant: you can't change it. */
-	if (val != read_id_reg(vcpu, rd))
-		return -EINVAL;
+	int ret;
+	int id = reg_to_encoding(rd);
+
+	ret = arm64_check_features(id_reg_descs[IDREG_IDX(id)].ftr_bits, val,
+				   id_reg_descs[IDREG_IDX(id)].kvm_sys_val);
+	if (ret)
+		return ret;
+
+	vcpu->kvm->arch.id_regs[IDREG_IDX(id)] = val;
 
 	return 0;
 }
@@ -197,12 +275,47 @@ static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
 	return id_visibility(vcpu, r);
 }
 
+static void init_id_reg(struct id_reg_desc *idr)
+{
+	idr->kvm_sys_val = read_sanitised_ftr_reg(reg_to_encoding(&idr->reg_desc));
+}
+
+static void init_id_aa64pfr0_el1(struct id_reg_desc *idr)
+{
+	u64 val;
+	u32 id = reg_to_encoding(&idr->reg_desc);
+
+	val = read_sanitised_ftr_reg(id);
+	/*
+	 * The default is to expose CSV2 == 1 if the HW isn't affected.
+	 * Although this is a per-CPU feature, we make it global because
+	 * asymmetric systems are just a nuisance.
+	 *
+	 * Userspace can override this as long as it doesn't promise
+	 * the impossible.
+	 */
+	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);

arch/arm64/kvm/id_regs.c static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu, · line 309

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+	}
+	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
+	}
+
+	val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
+
+	val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
+	val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);

What? Why? What if I have a GICv2? What if I have no GIC?

arch/arm64/kvm/id_regs.c static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu, · line 312

Quoted 3 lines
arch/arm64/kvm/id_regs.c +3 -0
+
+	idr->kvm_sys_val = val;
+}

How does this compose with the runtime feature reduction that takes place in access_nested_id_reg()?

Quoted 65 lines
arch/arm64/kvm/id_regs.c +23 -14
+
 static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 			       const struct sys_reg_desc *rd,
 			       u64 val)
 {
 	u8 csv2, csv3;
-	u64 sval = val;
 
 	/*
 	 * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
@@ -220,16 +333,29 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 	    (csv3 && arm64_get_meltdown_state() != SPECTRE_UNAFFECTED))
 		return -EINVAL;
 
-	/* We can only differ with CSV[23], and anything else is an error */
-	val ^= read_id_reg(vcpu, rd);
-	val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
-		 ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
-	if (val)
-		return -EINVAL;
+	return set_id_reg(vcpu, rd, val);
+}
 
-	vcpu->kvm->arch.id_regs[IDREG_IDX(reg_to_encoding(rd))] = sval;
+static void init_id_aa64dfr0_el1(struct id_reg_desc *idr)
+{
+	u64 val;
+	u32 id = reg_to_encoding(&idr->reg_desc);
 
-	return 0;
+	val = read_sanitised_ftr_reg(id);
+	/* Limit debug to ARMv8.0 */
+	val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer);
+	val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer), 6);
+	/*
+	 * Initialise the default PMUver before there is a chance to
+	 * create an actual PMU.
+	 */
+	val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+	val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+			  kvm_arm_pmu_get_pmuver_limit());
+	/* Hide SPE from guests */
+	val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMSVer);
+
+	idr->kvm_sys_val = val;
 }
 
 static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
@@ -238,6 +364,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 {
 	u8 pmuver, host_pmuver;
 	bool valid_pmu;
+	int ret;
 
 	host_pmuver = kvm_arm_pmu_get_pmuver_limit();
 
@@ -257,39 +384,58 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 	if (kvm_vcpu_has_pmu(vcpu) != valid_pmu)
 		return -EINVAL;
 
-	/* We can only differ with PMUver, and anything else is an error */
-	val ^= read_id_reg(vcpu, rd);
-	val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
-	if (val)
-		return -EINVAL;

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 391

Quoted 10 lines
arch/arm64/kvm/id_regs.c +3 -5
-
 	if (valid_pmu) {
 		mutex_lock(&vcpu->kvm->lock);
-		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
-			~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
-		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |=
-			FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+		ret = set_id_reg(vcpu, rd, val);
+		if (ret)
+			return ret;

Next stop, Deadlock City, our final destination.

Quoted 5 lines
arch/arm64/kvm/id_regs.c +0 -0
 		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] &=
 			~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
 		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] |= FIELD_PREP(
 				ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver_to_perfmon(pmuver));

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 403

Quoted 10 lines
arch/arm64/kvm/id_regs.c +5 -3
 		mutex_unlock(&vcpu->kvm->lock);
-	} else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
-		set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
 	} else {
-		clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+		/* We can only differ with PMUver, and anything else is an error */
+		val ^= read_id_reg(vcpu, rd);
+		val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+		if (val)
+			return -EINVAL;

I find it very odd that you add all this infrastructure to check for writable fields, and yet have to keep this comparison. It makes me thing that the data structures are not necessarily the right ones.

Quoted 47 lines
arch/arm64/kvm/id_regs.c +24 -5
+
+		if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF)
+			set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+		else
+			clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+
 	}
 
 	return 0;
 }
 
+static void init_id_dfr0_el1(struct id_reg_desc *idr)
+{
+	u64 val;
+	u32 id = reg_to_encoding(&idr->reg_desc);
+
+	val = read_sanitised_ftr_reg(id);
+	/*
+	 * Initialise the default PMUver before there is a chance to
+	 * create an actual PMU.
+	 */
+	val &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+	val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon),
+			  kvm_arm_pmu_get_pmuver_limit());
+
+	idr->kvm_sys_val = val;
+}
+
 static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 			   const struct sys_reg_desc *rd,
 			   u64 val)
 {
 	u8 perfmon, host_perfmon;
 	bool valid_pmu;
+	int ret;
 
 	host_perfmon = pmuver_to_perfmon(kvm_arm_pmu_get_pmuver_limit());
 
@@ -310,42 +456,46 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	if (kvm_vcpu_has_pmu(vcpu) != valid_pmu)
 		return -EINVAL;
 
-	/* We can only differ with PerfMon, and anything else is an error */
-	val ^= read_id_reg(vcpu, rd);
-	val &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
-	if (val)
-		return -EINVAL;

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 463

Quoted 10 lines
arch/arm64/kvm/id_regs.c +3 -5
-
 	if (valid_pmu) {
 		mutex_lock(&vcpu->kvm->lock);
-		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] &=
-			~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
-		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] |= FIELD_PREP(
-			ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon);
+		ret = set_id_reg(vcpu, rd, val);
+		if (ret)
+			return ret;

Same player, shoot again.

Quoted 11 lines
arch/arm64/kvm/id_regs.c +1 -3
 		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
 			~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
 		vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |= FIELD_PREP(
 			ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
 		mutex_unlock(&vcpu->kvm->lock);
-	} else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
-		set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
 	} else {
-		clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+		/* We can only differ with PerfMon, and anything else is an error */

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 481

Quoted 10 lines
arch/arm64/kvm/id_regs.c +9 -0
+		val ^= read_id_reg(vcpu, rd);
+		val &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+		if (val)
+			return -EINVAL;
+
+		if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF)
+			set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+		else
+			clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
 	}

Same remarks.

Quoted 69 lines
arch/arm64/kvm/id_regs.c +21 -8
 	return 0;
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
+#define SYS_DESC_SANITISED(name) {			\
+	SYS_DESC(SYS_##name),				\
+	.access	= access_id_reg,			\
+	.get_user = get_id_reg,				\
+	.set_user = set_id_reg,				\
+	.visibility = id_visibility,			\
+}
+
 #define ID_SANITISED(name) {				\
-	.reg_desc = {					\
-		SYS_DESC(SYS_##name),			\
-		.access	= access_id_reg,		\
-		.get_user = get_id_reg,			\
-		.set_user = set_id_reg,			\
-		.visibility = id_visibility,		\
-	},						\
+	.reg_desc = SYS_DESC_SANITISED(name),		\
+	.ftr_bits = { ARM64_FTR_END, },			\
+	.init = init_id_reg,				\
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
@@ -357,6 +507,8 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 		.set_user = set_id_reg,			\
 		.visibility = aa32_id_visibility,	\
 	},						\
+	.ftr_bits = { ARM64_FTR_END, },			\
+	.init = init_id_reg,				\
 }
 
 /*
@@ -372,6 +524,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 		.set_user = set_id_reg,				\
 		.visibility = raz_visibility			\
 	},							\
+	.ftr_bits = { ARM64_FTR_END, },				\
 }
 
 /*
@@ -387,9 +540,10 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 		.set_user = set_id_reg,			\
 		.visibility = raz_visibility,		\
 	},						\
+	.ftr_bits = { ARM64_FTR_END, },			\
 }
 
-static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
+static struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
 	/*
 	 * ID regs: all ID_SANITISED() entries here must have corresponding
 	 * entries in arm64_ftr_regs[].
@@ -405,6 +559,11 @@ static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
 		.get_user = get_id_reg,
 		.set_user = set_id_dfr0_el1,
 		.visibility = aa32_id_visibility, },
+	  .ftr_bits = {
+		ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE,
+			ID_DFR0_EL1_PerfMon_SHIFT, ID_DFR0_EL1_PerfMon_WIDTH, 0),
+		ARM64_FTR_END, },
+	  .init = init_id_dfr0_el1,
 	},
 	ID_HIDDEN(ID_AFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR0_EL1),

arch/arm64/kvm/id_regs.c static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = { · line 606

Quoted 9 lines
arch/arm64/kvm/id_regs.c +6 -0
@@ -439,6 +598,13 @@ static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
 		.access = access_id_reg,
 		.get_user = get_id_reg,
 		.set_user = set_id_aa64pfr0_el1, },
+	  .ftr_bits = {
+		ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE,
+			ID_AA64PFR0_EL1_CSV2_SHIFT, ID_AA64PFR0_EL1_CSV2_WIDTH, 0),
+		ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE,
+			ID_AA64PFR0_EL1_CSV3_SHIFT, ID_AA64PFR0_EL1_CSV3_WIDTH, 0),
+		ARM64_FTR_END, },

It really strikes me that you are 100% duplicating data that is already in ftr_id_aa64pfr0[]. Only that this is a subset of the existing data.

You could instead have your 'init()' callback return a pair of values:
the default value based on the sanitised one, and a 64bit mask. At this stage, you'll realise that this looks a lot like the feature override, and that you should be able to reuse some of the existing infrastructure.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#24 lore raw

Re: [PATCH v4 6/6] KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

Hi Marc,

Quoted 4 lines · Marc Zyngier
Quoted 1 line · Jing Zhang
Quoted 8 lines
Quoted 1 line

Save KVM sanitised ID register value in ID descriptor (kvm_sys_val).

Why do we need to store a separate value *beside* the sanitised value the kernel already holds?

Quoted 1 line

Add an init callback for every ID register to setup kvm_sys_val.

Same question.

It is used to store the value further sanitised by KVM, which might be different from the one held by kernel. But as you suggested later, this isn't necessary, we can create KVM sanitised value on the fly since it is cheap.

Quoted 132 lines
Quoted 132 lines

All per VCPU sanitizations are still handled on the fly during ID register read and write from userspace.
An arm64_ftr_bits array is used to indicate writable feature fields.

Refactor writings for ID_AA64PFR0_EL1.[CSV2|CSV3], ID_AA64DFR0_EL1.PMUVer and ID_DFR0_ELF.PerfMon based on utilities introduced by ID register descriptor.

No functional change intended.

---
 arch/arm64/include/asm/cpufeature.h |  25 +++
 arch/arm64/include/asm/kvm_host.h   |   2 +-
 arch/arm64/kernel/cpufeature.c      |  26 +--
 arch/arm64/kvm/arm.c                |   2 +-
 arch/arm64/kvm/id_regs.c            | 325 ++++++++++++++++++++--------
 arch/arm64/kvm/sys_regs.c           |   3 +-
 arch/arm64/kvm/sys_regs.h           |   2 +-
 7 files changed, 261 insertions(+), 124 deletions(-)
arch/arm64/include/asm/cpufeature.h +25 -0
index fc2c739f48f1..493ec530eefc 100644
@@ -64,6 +64,30 @@ struct arm64_ftr_bits {
      s64             safe_val; /* safe value for FTR_EXACT features */
 };
 
+#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+     {                                               \
+             .sign = SIGNED,                         \
+             .visible = VISIBLE,                     \
+             .strict = STRICT,                       \
+             .type = TYPE,                           \
+             .shift = SHIFT,                         \
+             .width = WIDTH,                         \
+             .safe_val = SAFE_VAL,                   \
+     }
+
+/* Define a feature with unsigned values */
+#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+     __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
+
+/* Define a feature with a signed value */
+#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+     __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
+
+#define ARM64_FTR_END                                        \
+     {                                               \
+             .width = 0,                             \
+     }
+
 /*
  * Describe the early feature override to the core override code:
  *
@@ -911,6 +935,7 @@ static inline unsigned int get_vmid_bits(u64 mmfr1)
      return 8;
 }
 
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
 struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
 
 extern struct arm64_ftr_override id_aa64mmfr1_override;
arch/arm64/include/asm/kvm_host.h +1 -1
index 102860ba896d..aa83dd79e7ff 100644
@@ -1013,7 +1013,7 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
                              struct kvm_arm_copy_mte_tags *copy_tags);
 
-void kvm_arm_set_default_id_regs(struct kvm *kvm);
+void kvm_arm_init_id_regs(struct kvm *kvm);
 
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
arch/arm64/kernel/cpufeature.c +1 -25
index 23bd2a926b74..e18848ee4b98 100644
@@ -139,30 +139,6 @@ void dump_cpu_features(void)
      pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
 }
 
-#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-     {                                               \
-             .sign = SIGNED,                         \
-             .visible = VISIBLE,                     \
-             .strict = STRICT,                       \
-             .type = TYPE,                           \
-             .shift = SHIFT,                         \
-             .width = WIDTH,                         \
-             .safe_val = SAFE_VAL,                   \
-     }
-
-/* Define a feature with unsigned values */
-#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-     __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
-
-/* Define a feature with a signed value */
-#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-     __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
-
-#define ARM64_FTR_END                                        \
-     {                                               \
-             .width = 0,                             \
-     }
-
 static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
 
 static bool __system_matches_cap(unsigned int n);
@@ -790,7 +766,7 @@ static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
      return reg;
 }
 
-static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
                              s64 cur)
 {
      s64 ret = 0;
arch/arm64/kvm/arm.c +0 -0
index fb2de2cb98cb..e539d9ca9d01 100644
@@ -135,7 +135,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
      /* The maximum number of VCPUs is limited by the host's GIC model */
      kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
Quoted 4 lines

arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 138

Quoted 2 lines
arch/arm64/kvm/arm.c +1 -1
-     kvm_arm_set_default_id_regs(kvm);
+     kvm_arm_init_id_regs(kvm);

How about picking the name once and for all from the first patch?

Sure, will do.

Quoted 22 lines
Quoted 9 lines
arch/arm64/kvm/arm.c +0 -0
      kvm_arm_init_hypercalls(kvm);
 
      return 0;
arch/arm64/kvm/id_regs.c +0 -0
index 9956c99d20f7..726b810b6e06 100644

arch/arm64/kvm/id_regs.c line 25

Quoted 8 lines
arch/arm64/kvm/id_regs.c +5 -0
@@ -18,10 +18,88 @@
 
 #include "sys_regs.h"
 
+/*
+ * Number of entries in id_reg_desc's ftr_bits[] (Number of 4 bits fields
+ * in 64 bit register + 1 entry for a terminator entry).
+ */
+#define      FTR_FIELDS_NUM  17

Please see SMFR0_EL1 for an example of a sysreg that doesn't follow the 4bits-per-field format. I expect to see more of those in the future.

Quoted 4 lines

And given that this is always a variable set of fields, why do we need to define this as a fixed array that only bloats the structure? I'd rather see a variable array in a side structure.

Yes, it makes more sense to use a variable array here. Do you have any suggestions? xarray?

Quoted 27 lines
Quoted 11 lines
arch/arm64/kvm/id_regs.c +9 -0
+
 struct id_reg_desc {
      const struct sys_reg_desc       reg_desc;
+     /*
+      * KVM sanitised ID register value.
+      * It is the default value for per VM emulated ID register.
+      */
+     u64 kvm_sys_val;
+     /*
+      * Used to validate the ID register values with arm64_check_features().
+      * The last item in the array must be terminated by an item whose

arch/arm64/kvm/id_regs.c line 46

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+      * width field is zero as that is expected by arm64_check_features().
+      * Only feature bits defined in this array are writable.
+      */
+     struct arm64_ftr_bits   ftr_bits[FTR_FIELDS_NUM];
+
+     /*
+      * Basically init() is used to setup the KVM sanitised value
+      * stored in kvm_sys_val.
+      */
+     void (*init)(struct id_reg_desc *idr);

Given that this callback only builds the value from the sanitised view, and that it is very cheap (only a set of masking), why do we bother keeping the value around? It would also allow this structure to be kept *const*, something that is extremely desirable.

Quoted 5 lines

Also, why do we need an init() method when each sysreg already have a reset() method? Surely this should be the same thing...

My gut feeling is that we should only have a callback returning the limit value computed on the fly.

Sure, will use a callback to return the limit value on the fly.

Quoted 51 lines
Quoted 51 lines
arch/arm64/kvm/id_regs.c +48 -0
 };
 
+static struct id_reg_desc id_reg_descs[];
+
+/**
+ * arm64_check_features() - Check if a feature register value constitutes
+ * a subset of features indicated by @limit.
+ *
+ * @ftrp: Pointer to an array of arm64_ftr_bits. It must be terminated by
+ * an item whose width field is zero.
+ * @val: The feature register value to check
+ * @limit: The limit value of the feature register
+ *
+ * This function will check if each feature field of @val is the "safe" value
+ * against @limit based on @ftrp[], each of which specifies the target field
+ * (shift, width), whether or not the field is for a signed value (sign),
+ * how the field is determined to be "safe" (type), and the safe value
+ * (safe_val) when type == FTR_EXACT (safe_val won't be used by this
+ * function when type != FTR_EXACT). Any other fields in arm64_ftr_bits
+ * won't be used by this function. If a field value in @val is the same
+ * as the one in @limit, it is always considered the safe value regardless
+ * of the type. For register fields that are not in @ftrp[], only the value
+ * in @limit is considered the safe value.
+ *
+ * Return: 0 if all the fields are safe. Otherwise, return negative errno.
+ */
+static int arm64_check_features(const struct arm64_ftr_bits *ftrp, u64 val, u64 limit)
+{
+     u64 mask = 0;
+
+     for (; ftrp->width; ftrp++) {
+             s64 f_val, f_lim, safe_val;
+
+             f_val = arm64_ftr_value(ftrp, val);
+             f_lim = arm64_ftr_value(ftrp, limit);
+             mask |= arm64_ftr_mask(ftrp);
+
+             if (f_val == f_lim)
+                     safe_val = f_val;
+             else
+                     safe_val =  arm64_ftr_safe_value(ftrp, f_val, f_lim);
+
+             if (safe_val != f_val)
+                     return -E2BIG;
+     }
+
+     /*
+      * For fields that are not indicated in ftrp, values in limit are the
+      * safe values.
+      */
Quoted 8 lines

arch/arm64/kvm/id_regs.c line 101

Quoted 5 lines
arch/arm64/kvm/id_regs.c +5 -0
+     if ((val & ~mask) != (limit & ~mask))
+             return -E2BIG;
+
+     return 0;
+}

I have the feeling that the core code already implements something similar...

Right, it is similar to update_cpu_ftr_reg() in cpufeature.c. But that function can't meet the needs here.

Quoted 78 lines
Quoted 78 lines
arch/arm64/kvm/id_regs.c +36 -9
+
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
      if (kvm_vcpu_has_pmu(vcpu))
@@ -67,7 +145,6 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
      case SYS_ID_AA64PFR0_EL1:
              if (!vcpu_has_sve(vcpu))
                      val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
-             val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
              if (kvm_vgic_global_state.type == VGIC_V3) {
                      val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
                      val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -94,15 +171,10 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
                      val &= ~ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_WFxT);
              break;
      case SYS_ID_AA64DFR0_EL1:
-             /* Limit debug to ARMv8.0 */
-             val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer);
-             val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer), 6);
              /* Set PMUver to the required version */
              val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
              val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
                                vcpu_pmuver(vcpu));
-             /* Hide SPE from guests */
-             val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMSVer);
              break;
      case SYS_ID_DFR0_EL1:
              val &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
@@ -161,9 +233,15 @@ static int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
                    u64 val)
 {
-     /* This is what we mean by invariant: you can't change it. */
-     if (val != read_id_reg(vcpu, rd))
-             return -EINVAL;
+     int ret;
+     int id = reg_to_encoding(rd);
+
+     ret = arm64_check_features(id_reg_descs[IDREG_IDX(id)].ftr_bits, val,
+                                id_reg_descs[IDREG_IDX(id)].kvm_sys_val);
+     if (ret)
+             return ret;
+
+     vcpu->kvm->arch.id_regs[IDREG_IDX(id)] = val;
 
      return 0;
 }
@@ -197,12 +275,47 @@ static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
      return id_visibility(vcpu, r);
 }
 
+static void init_id_reg(struct id_reg_desc *idr)
+{
+     idr->kvm_sys_val = read_sanitised_ftr_reg(reg_to_encoding(&idr->reg_desc));
+}
+
+static void init_id_aa64pfr0_el1(struct id_reg_desc *idr)
+{
+     u64 val;
+     u32 id = reg_to_encoding(&idr->reg_desc);
+
+     val = read_sanitised_ftr_reg(id);
+     /*
+      * The default is to expose CSV2 == 1 if the HW isn't affected.
+      * Although this is a per-CPU feature, we make it global because
+      * asymmetric systems are just a nuisance.
+      *
+      * Userspace can override this as long as it doesn't promise
+      * the impossible.
+      */
+     if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+             val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+             val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);
+     }
+     if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+             val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
+             val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
Quoted 8 lines

arch/arm64/kvm/id_regs.c static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu, · line 309

Quoted 6 lines
arch/arm64/kvm/id_regs.c +6 -0
+     }
+
+     val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
+
+     val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
+     val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);

What? Why? What if I have a GICv2? What if I have no GIC?

:-) Forgot how these two lines got in here. Will remove them.

Quoted 7 lines

arch/arm64/kvm/id_regs.c static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu, · line 312

Quoted 4 lines
arch/arm64/kvm/id_regs.c +3 -0
+
+     idr->kvm_sys_val = val;
+}

How does this compose with the runtime feature reduction that takes place in access_nested_id_reg()?

kvm_sys_val is used as the initial value for the per VM idregs, which is passed into access_nested_id_reg in function access_id_reg().

Quoted 70 lines
Quoted 70 lines
arch/arm64/kvm/id_regs.c +23 -16
+
 static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
                             const struct sys_reg_desc *rd,
                             u64 val)
 {
      u8 csv2, csv3;
-     u64 sval = val;
 
      /*
       * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
@@ -220,16 +333,29 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
          (csv3 && arm64_get_meltdown_state() != SPECTRE_UNAFFECTED))
              return -EINVAL;
 
-     /* We can only differ with CSV[23], and anything else is an error */
-     val ^= read_id_reg(vcpu, rd);
-     val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
-              ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
-     if (val)
-             return -EINVAL;
+     return set_id_reg(vcpu, rd, val);
+}
 
-     vcpu->kvm->arch.id_regs[IDREG_IDX(reg_to_encoding(rd))] = sval;
+static void init_id_aa64dfr0_el1(struct id_reg_desc *idr)
+{
+     u64 val;
+     u32 id = reg_to_encoding(&idr->reg_desc);
 
-     return 0;
+     val = read_sanitised_ftr_reg(id);
+     /* Limit debug to ARMv8.0 */
+     val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer);
+     val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer), 6);
+     /*
+      * Initialise the default PMUver before there is a chance to
+      * create an actual PMU.
+      */
+     val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+     val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                       kvm_arm_pmu_get_pmuver_limit());
+     /* Hide SPE from guests */
+     val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMSVer);
+
+     idr->kvm_sys_val = val;
 }
 
 static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
@@ -238,6 +364,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 {
      u8 pmuver, host_pmuver;
      bool valid_pmu;
+     int ret;
 
      host_pmuver = kvm_arm_pmu_get_pmuver_limit();
 
@@ -257,39 +384,58 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
      if (kvm_vcpu_has_pmu(vcpu) != valid_pmu)
              return -EINVAL;
 
-     /* We can only differ with PMUver, and anything else is an error */
-     val ^= read_id_reg(vcpu, rd);
-     val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
-     if (val)
-             return -EINVAL;
-
      if (valid_pmu) {
              mutex_lock(&vcpu->kvm->lock);
-             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
Quoted 8 lines

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 391

Quoted 6 lines
arch/arm64/kvm/id_regs.c +3 -3
-                     ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
-             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |=
-                     FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+             ret = set_id_reg(vcpu, rd, val);
+             if (ret)
+                     return ret;

Next stop, Deadlock City, our final destination.

Will fix it.

Quoted 17 lines
Quoted 6 lines
arch/arm64/kvm/id_regs.c +0 -0
              vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] &=
                      ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
              vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] |= FIELD_PREP(
                              ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver_to_perfmon(pmuver));

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 403

Quoted 10 lines
arch/arm64/kvm/id_regs.c +5 -3
              mutex_unlock(&vcpu->kvm->lock);
-     } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
-             set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
      } else {
-             clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+             /* We can only differ with PMUver, and anything else is an error */
+             val ^= read_id_reg(vcpu, rd);
+             val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+             if (val)
+                     return -EINVAL;
Quoted 3 lines

I find it very odd that you add all this infrastructure to check for writable fields, and yet have to keep this comparison. It makes me thing that the data structures are not necessarily the right ones.

This comparison is still here because for this patch, we don't allow the writable for the whole ID register and in the path of invalid pmu, the set_id_reg() (This function will do all the checks) is not called. This comparison can be removed as long as the whole ID reg is enabled writable.

Quoted 52 lines
Quoted 52 lines
arch/arm64/kvm/id_regs.c +24 -7
+
+             if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF)
+                     set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+             else
+                     clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+
      }
 
      return 0;
 }
 
+static void init_id_dfr0_el1(struct id_reg_desc *idr)
+{
+     u64 val;
+     u32 id = reg_to_encoding(&idr->reg_desc);
+
+     val = read_sanitised_ftr_reg(id);
+     /*
+      * Initialise the default PMUver before there is a chance to
+      * create an actual PMU.
+      */
+     val &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+     val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon),
+                       kvm_arm_pmu_get_pmuver_limit());
+
+     idr->kvm_sys_val = val;
+}
+
 static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
                         const struct sys_reg_desc *rd,
                         u64 val)
 {
      u8 perfmon, host_perfmon;
      bool valid_pmu;
+     int ret;
 
      host_perfmon = pmuver_to_perfmon(kvm_arm_pmu_get_pmuver_limit());
 
@@ -310,42 +456,46 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
      if (kvm_vcpu_has_pmu(vcpu) != valid_pmu)
              return -EINVAL;
 
-     /* We can only differ with PerfMon, and anything else is an error */
-     val ^= read_id_reg(vcpu, rd);
-     val &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
-     if (val)
-             return -EINVAL;
-
      if (valid_pmu) {
              mutex_lock(&vcpu->kvm->lock);
-             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] &=
Quoted 8 lines

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 463

Quoted 6 lines
arch/arm64/kvm/id_regs.c +3 -3
-                     ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
-             vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_DFR0_EL1)] |= FIELD_PREP(
-                     ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon);
+             ret = set_id_reg(vcpu, rd, val);
+             if (ret)
+                     return ret;

Same player, shoot again.

Will fix it.

Quoted 108 lines
Quoted 12 lines
arch/arm64/kvm/id_regs.c +1 -3
              vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] &=
                      ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
              vcpu->kvm->arch.id_regs[IDREG_IDX(SYS_ID_AA64DFR0_EL1)] |= FIELD_PREP(
                      ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
              mutex_unlock(&vcpu->kvm->lock);
-     } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
-             set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
      } else {
-             clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+             /* We can only differ with PerfMon, and anything else is an error */

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 481

Quoted 10 lines
arch/arm64/kvm/id_regs.c +9 -0
+             val ^= read_id_reg(vcpu, rd);
+             val &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+             if (val)
+                     return -EINVAL;
+
+             if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF)
+                     set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+             else
+                     clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
      }

Same remarks.

Quoted 69 lines
arch/arm64/kvm/id_regs.c +21 -8
      return 0;
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
+#define SYS_DESC_SANITISED(name) {                   \
+     SYS_DESC(SYS_##name),                           \
+     .access = access_id_reg,                        \
+     .get_user = get_id_reg,                         \
+     .set_user = set_id_reg,                         \
+     .visibility = id_visibility,                    \
+}
+
 #define ID_SANITISED(name) {                         \
-     .reg_desc = {                                   \
-             SYS_DESC(SYS_##name),                   \
-             .access = access_id_reg,                \
-             .get_user = get_id_reg,                 \
-             .set_user = set_id_reg,                 \
-             .visibility = id_visibility,            \
-     },                                              \
+     .reg_desc = SYS_DESC_SANITISED(name),           \
+     .ftr_bits = { ARM64_FTR_END, },                 \
+     .init = init_id_reg,                            \
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
@@ -357,6 +507,8 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
              .set_user = set_id_reg,                 \
              .visibility = aa32_id_visibility,       \
      },                                              \
+     .ftr_bits = { ARM64_FTR_END, },                 \
+     .init = init_id_reg,                            \
 }
 
 /*
@@ -372,6 +524,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
              .set_user = set_id_reg,                         \
              .visibility = raz_visibility                    \
      },                                                      \
+     .ftr_bits = { ARM64_FTR_END, },                         \
 }
 
 /*
@@ -387,9 +540,10 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
              .set_user = set_id_reg,                 \
              .visibility = raz_visibility,           \
      },                                              \
+     .ftr_bits = { ARM64_FTR_END, },                 \
 }
 
-static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
+static struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
      /*
       * ID regs: all ID_SANITISED() entries here must have corresponding
       * entries in arm64_ftr_regs[].
@@ -405,6 +559,11 @@ static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
              .get_user = get_id_reg,
              .set_user = set_id_dfr0_el1,
              .visibility = aa32_id_visibility, },
+       .ftr_bits = {
+             ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE,
+                     ID_DFR0_EL1_PerfMon_SHIFT, ID_DFR0_EL1_PerfMon_WIDTH, 0),
+             ARM64_FTR_END, },
+       .init = init_id_dfr0_el1,
      },
      ID_HIDDEN(ID_AFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR0_EL1),

arch/arm64/kvm/id_regs.c static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = { · line 606

Quoted 9 lines
arch/arm64/kvm/id_regs.c +6 -0
@@ -439,6 +598,13 @@ static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
              .access = access_id_reg,
              .get_user = get_id_reg,
              .set_user = set_id_aa64pfr0_el1, },
+       .ftr_bits = {
+             ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE,
+                     ID_AA64PFR0_EL1_CSV2_SHIFT, ID_AA64PFR0_EL1_CSV2_WIDTH, 0),
+             ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE,
+                     ID_AA64PFR0_EL1_CSV3_SHIFT, ID_AA64PFR0_EL1_CSV3_WIDTH, 0),
+             ARM64_FTR_END, },

It really strikes me that you are 100% duplicating data that is already in ftr_id_aa64pfr0[]. Only that this is a subset of the existing data.

Quoted 5 lines

You could instead have your 'init()' callback return a pair of values:
the default value based on the sanitised one, and a 64bit mask. At this stage, you'll realise that this looks a lot like the feature override, and that you should be able to reuse some of the existing infrastructure.

Sure, will try to improve this by your suggestion.

Quoted 7 lines

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.

Thanks,
Jing