Skip to contentSkip to conversation

Support writable CPU ID registers from userspace

PATCH Superseded Jing Zhang 8 participants 35 messages May 3, 2023 – May 20, 2023

Subsystems (3)

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Jing Zhang

author
#0 lore raw

v8 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.

This patchset uses kvm->arch.config_lock from Oliver's lock inversion fixes at
https://lore.kernel.org/linux-arm-kernel/[email protected]/

---

* v7 -> v8
- Move idregs table sanity check to kvm_sys_reg_table_init.
- Only allow userspace writing before VM running.
- No lock is hold for guest access to idregs.
- Addressed some other comments from Reiji and Oliver.

* v6 -> v7
- Rebased to v6.3-rc7.
- Add helpers for idregs read/write.
- Guard all idregs reads/writes.
- Add code to fix features' safe value type which is different for KVM than for the host.

* v5 -> v6
- Rebased to v6.3-rc5.
- Reuse struct sys_reg_desc's reset() callback and field val for KVM. sanitisation function and writable mask instead of creating a new data structure for idregs.
- Use get_arm64_ftr_reg() instead of exposing idregs ftr_bits array.

* v4 -> v5
- Rebased to 2fad20ae05cb (kvmarm/next)
Merge branch kvm-arm64/selftest/misc-6,4 into kvmarm-master/next
- Use kvm->arch.config_lock to guard update to multiple VM scope idregs to avoid lock inversion
- Add back IDREG() macro for idregs access
- Refactor struct id_reg_desc by using existing infrastructure.
- Addressed many other comments from Marc.

* 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]
[4] https://lore.kernel.org/all/[email protected]
[5] https://lore.kernel.org/all/[email protected]
[6] https://lore.kernel.org/all/[email protected]
[7] https://lore.kernel.org/all/[email protected]

---

Jing Zhang (6):
KVM: arm64: Move CPU ID feature registers emulation into a separate
file
KVM: arm64: Save ID registers' sanitized value per guest
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: Reuse fields of sys_reg_desc for idreg
KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

 arch/arm64/include/asm/cpufeature.h |   1 +
 arch/arm64/include/asm/kvm_host.h   |  33 +-
 arch/arm64/kernel/cpufeature.c      |   2 +-
 arch/arm64/kvm/Makefile             |   2 +-
 arch/arm64/kvm/arm.c                |  24 +-
 arch/arm64/kvm/id_regs.c            | 717 ++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c           | 534 ++++-----------------
 arch/arm64/kvm/sys_regs.h           |  28 +-
 include/kvm/arm_pmu.h               |   5 +-
 9 files changed, 864 insertions(+), 482 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c

base-commit: 6a8f57ae2eb07ab39a6f0ccad60c760743051026

Signature · 1 line
2.40.1.495.gc816e09b53d-goog
author
#1 lore raw

PATCH v8 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  | 460 +++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c | 464 ++++----------------------------------
 arch/arm64/kvm/sys_regs.h |  19 ++
 4 files changed, 519 insertions(+), 426 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 +460 -0

460 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +39 -425

569 lines not on this page · raw message

arch/arm64/kvm/sys_regs.h +19 -0
index 6b11f2cc7146..7ce546a8be60 100644
@@ -210,6 +210,19 @@ 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);
 }
 
+/*
+ * Return true if the register's (Op0, Op1, CRn, CRm, Op2) is
+ * (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+ */
+static inline bool is_id_reg(u32 id)
+{
+	return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 &&
+		sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 &&
+		sys_reg_CRm(id) < 8);
+}
+
+void perform_access(struct kvm_vcpu *vcpu, struct sys_reg_params *params,
+		    const struct sys_reg_desc *r);
 const struct sys_reg_desc *get_reg_by_id(u64 id,
 					 const struct sys_reg_desc table[],
 					 unsigned int num);
@@ -220,6 +233,10 @@ int kvm_sys_reg_get_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
 			 const struct sys_reg_desc table[], unsigned int num);
 int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
 			 const struct sys_reg_desc table[], unsigned int num);
+bool write_to_read_only(struct kvm_vcpu *vcpu,
+			struct sys_reg_params *params, const struct sys_reg_desc *r);
+unsigned int raz_visibility(const struct kvm_vcpu *vcpu, const struct sys_reg_desc *r);
+int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params);
 
 #define AA32(_x)	.aarch32_map = AA32_##_x
 #define Op0(_x) 	.Op0 = _x
@@ -234,4 +251,6 @@ int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
 	CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)),	\
 	Op2(sys_reg_Op2(reg))
 
+#define KVM_ARM_ID_REG_NUM	56
+
 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */
Signature · 1 line
2.40.1.495.gc816e09b53d-goog
maintainer
#2 lore raw

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

arch/arm64/kvm/id_regs.c

Quoted 34 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  | 460 +++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c | 464 ++++----------------------------------
 arch/arm64/kvm/sys_regs.h |  19 ++
 4 files changed, 519 insertions(+), 426 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..96b4c43a5100
@@ -0,0 +1,460 @@

[...]

I really wonder why we move this to another file. It only creates noise, and doesn't add much. Yes, the file is big. But now you need to expose all the internal machinery that was private until now.

If you look at the global diffstat, this is "only" another 200 lines. I don't think this is worth the churn.

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

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

Hi Marc,

Quoted 40 lines · Marc Zyngier
Quoted 34 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  | 460 +++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c | 464 ++++----------------------------------
 arch/arm64/kvm/sys_regs.h |  19 ++
 4 files changed, 519 insertions(+), 426 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..96b4c43a5100
@@ -0,0 +1,460 @@

[...]

Quoted 6 lines

I really wonder why we move this to another file. It only creates noise, and doesn't add much. Yes, the file is big. But now you need to expose all the internal machinery that was private until now.

If you look at the global diffstat, this is "only" another 200 lines. I don't think this is worth the churn.

Yes, the global diff is not that big as in the first patch series now. Will do all the changes in the same file in the next version.

Quoted 5 lines
        M.

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

Thanks,
Jing

author
#4 lore raw

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

In reply to Jing Zhang

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 | 20 ++++++++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 46 +++++++++++++++++++++++++------
 arch/arm64/kvm/sys_regs.c         | 11 +++++++-
 arch/arm64/kvm/sys_regs.h         |  3 +-
 5 files changed, 69 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_host.h +20 -0
index bcd774d74f34..a7d4d9e093e3 100644
@@ -177,6 +177,21 @@ struct kvm_smccc_features {
 	unsigned long vendor_hyp_bmap;
 };
 
+/*
+ * Emulated CPU ID registers per VM
+ * (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.
+ *
+ * These emulated idregs are VM-wide, but accessed from the context of a vCPU.
+ * Access to id regs are guarded by kvm_arch.config_lock.
+ */
+#define KVM_ARM_ID_REG_NUM	56
+#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+#define IDREG(kvm, id)		kvm->arch.idregs.regs[IDREG_IDX(id)]
+struct kvm_idregs {
+	u64 regs[KVM_ARM_ID_REG_NUM];
+};
+
 typedef unsigned int pkvm_handle_t;
 
 struct kvm_protected_vm {
@@ -243,6 +258,9 @@ struct kvm_arch {
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 
+	/* Emulated CPU ID registers */
+	struct kvm_idregs idregs;
+
 	/*
 	 * For an untrusted host VM, 'pkvm.handle' is used to lookup
 	 * the associated pKVM instance in the hypervisor.
@@ -1008,6 +1026,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_init_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 4b2e16e696a8..e34744c36406 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_init_id_regs(kvm);
 
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +37 -9

62 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +10 -1
index 8dba10696dac..2e7b17d14b4f 100644
@@ -364,7 +364,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))) {
@@ -2967,5 +2967,14 @@ int __init kvm_sys_reg_table_init(void)
 	for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++)
 		invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]);
 
+	/* Sanity check for ID registers */
+	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+		if (!is_id_reg(reg_to_encoding(&id_reg_descs[i]))) {
+			kvm_err("Non-ID register(index:%d, name: %s) found in idregs table!\n",
+				i, id_reg_descs[i].name);
+			return -EINVAL;
+		}
+	}
+
 	return 0;
 }
arch/arm64/kvm/sys_regs.h +1 -2
index 7ce546a8be60..e88fd77309b2 100644
@@ -237,6 +237,7 @@ bool write_to_read_only(struct kvm_vcpu *vcpu,
 			struct sys_reg_params *params, const struct sys_reg_desc *r);
 unsigned int raz_visibility(const struct kvm_vcpu *vcpu, const struct sys_reg_desc *r);
 int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params);
+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
@@ -251,6 +252,4 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params);
 	CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)),	\
 	Op2(sys_reg_Op2(reg))
 
-#define KVM_ARM_ID_REG_NUM	56
-
 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */
Signature · 1 line
2.40.1.495.gc816e09b53d-goog
maintainer
#5 lore raw

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

Quoted 26 lines · Jing Zhang

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 | 20 ++++++++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 46 +++++++++++++++++++++++++------
 arch/arm64/kvm/sys_regs.c         | 11 +++++++-
 arch/arm64/kvm/sys_regs.h         |  3 +-
 5 files changed, 69 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index bcd774d74f34..a7d4d9e093e3 100644
@@ -177,6 +177,21 @@ struct kvm_smccc_features {
 	unsigned long vendor_hyp_bmap;
 };

arch/arm64/include/asm/kvm_host.h struct kvm_smccc_features { · line 188

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +9 -0
 
+/*
+ * Emulated CPU ID registers per VM
+ * (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.
+ *
+ * These emulated idregs are VM-wide, but accessed from the context of a vCPU.
+ * Access to id regs are guarded by kvm_arch.config_lock.
+ */
+#define KVM_ARM_ID_REG_NUM	56

You already have this as part of patch #1 in another include file, and then move it here. Surely you can do that in one go. I'd also like it to be defined in terms of encodings, and not as a raw value.

arch/arm64/include/asm/kvm_host.h struct kvm_smccc_features { · line 190

Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +2 -0
+#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+#define IDREG(kvm, id)		kvm->arch.idregs.regs[IDREG_IDX(id)]

Missing brackets around 'kvm'.

Quoted 43 lines
arch/arm64/include/asm/kvm_host.h +9 -0
+struct kvm_idregs {
+	u64 regs[KVM_ARM_ID_REG_NUM];
+};
+
 typedef unsigned int pkvm_handle_t;
 
 struct kvm_protected_vm {
@@ -243,6 +258,9 @@ struct kvm_arch {
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 
+	/* Emulated CPU ID registers */
+	struct kvm_idregs idregs;
+
 	/*
 	 * For an untrusted host VM, 'pkvm.handle' is used to lookup
 	 * the associated pKVM instance in the hypervisor.
@@ -1008,6 +1026,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_init_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 4b2e16e696a8..e34744c36406 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_init_id_regs(kvm);
 
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +0 -0
index 96b4c43a5100..e769223bcee2 100644

arch/arm64/kvm/id_regs.c static u8 pmuver_to_perfmon(u8 pmuver) · line 55

Quoted 4 lines
arch/arm64/kvm/id_regs.c +0 -1
@@ -52,16 +52,9 @@ static u8 pmuver_to_perfmon(u8 pmuver)
 	}
 }
 
-/* Read a sanitised cpufeature ID register by sys_reg_desc */

Why getting rid of this comment instead of moving it next to the (re-implemented) function?

Quoted 45 lines
arch/arm64/kvm/id_regs.c +22 -8
-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 = IDREG(vcpu->kvm, 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,
@@ -458,3 +459,30 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 
 	return 1;
 }
+
+/*
+ * 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_init_id_regs(struct kvm *kvm)
+{
+	u64 val;
+	u32 id;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {

arch/arm64/kvm/id_regs.c int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params) · line 483

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+		id = reg_to_encoding(&id_reg_descs[i]);
+
+		/*
+		 * Some hidden ID registers which are not in arm64_ftr_regs[]
+		 * would cause warnings from read_sanitised_ftr_reg().
+		 * Skip those ID registers to avoid the warnings.
+		 */
+		if (id_reg_descs[i].visibility == raz_visibility)
+			/* Hidden or reserved ID register */
+			continue;

Are you sure? What about other visibility attributes that are normally evaluated at runtime? This may work as a short term hack, but I'm not sure this is the correct long-term solution...

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

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

Hi Marc,

Quoted 30 lines · Marc Zyngier
Quoted 27 lines · Jing Zhang

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 | 20 ++++++++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 46 +++++++++++++++++++++++++------
 arch/arm64/kvm/sys_regs.c         | 11 +++++++-
 arch/arm64/kvm/sys_regs.h         |  3 +-
 5 files changed, 69 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index bcd774d74f34..a7d4d9e093e3 100644
@@ -177,6 +177,21 @@ struct kvm_smccc_features {
      unsigned long vendor_hyp_bmap;
 };
 
Quoted 13 lines

arch/arm64/include/asm/kvm_host.h struct kvm_smccc_features { · line 188

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +9 -0
+/*
+ * Emulated CPU ID registers per VM
+ * (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.
+ *
+ * These emulated idregs are VM-wide, but accessed from the context of a vCPU.
+ * Access to id regs are guarded by kvm_arch.config_lock.
+ */
+#define KVM_ARM_ID_REG_NUM   56

You already have this as part of patch #1 in another include file, and then move it here. Surely you can do that in one go. I'd also like it to be defined in terms of encodings, and not as a raw value.

Sure, will do.

Quoted 5 lines

arch/arm64/include/asm/kvm_host.h struct kvm_smccc_features { · line 190

Quoted 3 lines
arch/arm64/include/asm/kvm_host.h +2 -0
+#define IDREG_IDX(id)                (((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+#define IDREG(kvm, id)               kvm->arch.idregs.regs[IDREG_IDX(id)]

Missing brackets around 'kvm'.

Thanks, will fix.

Quoted 47 lines
Quoted 47 lines
arch/arm64/include/asm/kvm_host.h +9 -0
+struct kvm_idregs {
+     u64 regs[KVM_ARM_ID_REG_NUM];
+};
+
 typedef unsigned int pkvm_handle_t;
 
 struct kvm_protected_vm {
@@ -243,6 +258,9 @@ struct kvm_arch {
      /* Hypercall features firmware registers' descriptor */
      struct kvm_smccc_features smccc_feat;
 
+     /* Emulated CPU ID registers */
+     struct kvm_idregs idregs;
+
      /*
       * For an untrusted host VM, 'pkvm.handle' is used to lookup
       * the associated pKVM instance in the hypervisor.
@@ -1008,6 +1026,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_init_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 4b2e16e696a8..e34744c36406 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_init_id_regs(kvm);
 
      /*
       * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +0 -0
index 96b4c43a5100..e769223bcee2 100644
@@ -52,16 +52,9 @@ static u8 pmuver_to_perfmon(u8 pmuver)
      }
 }
 
Quoted 5 lines

arch/arm64/kvm/id_regs.c static u8 pmuver_to_perfmon(u8 pmuver) · line 55

Quoted 1 line
arch/arm64/kvm/id_regs.c +0 -1
-/* Read a sanitised cpufeature ID register by sys_reg_desc */

Why getting rid of this comment instead of moving it next to the (re-implemented) function?

Right, will move it to the re-implemented function.

Quoted 51 lines
Quoted 51 lines
arch/arm64/kvm/id_regs.c +28 -8
-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 = IDREG(vcpu->kvm, 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,
@@ -458,3 +459,30 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 
      return 1;
 }
+
+/*
+ * 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_init_id_regs(struct kvm *kvm)
+{
+     u64 val;
+     u32 id;
+     int i;
+
+     for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+             id = reg_to_encoding(&id_reg_descs[i]);
+
+             /*
+              * Some hidden ID registers which are not in arm64_ftr_regs[]
+              * would cause warnings from read_sanitised_ftr_reg().
+              * Skip those ID registers to avoid the warnings.
Quoted 8 lines

arch/arm64/kvm/id_regs.c int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params) · line 483

Quoted 4 lines
arch/arm64/kvm/id_regs.c +4 -0
+              */
+             if (id_reg_descs[i].visibility == raz_visibility)
+                     /* Hidden or reserved ID register */
+                     continue;

Are you sure? What about other visibility attributes that are normally evaluated at runtime? This may work as a short term hack, but I'm not sure this is the correct long-term solution...

Yes, this is a short term hack. It would be replaced by checking the reset() function of idregs in patch #5 in this series.

Quoted 5 lines
        M.

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

Thanks,
Jing

author
#7 lore raw

PATCH v8 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.

The setting of CSV bits for protected VMs are removed according to the discussion from Fuad below:
https://lore.kernel.org/all/CA+EHjTwXA9TprX4jeG+-D+c8v9XG+oFdU1o6TSkvVye145_OvA@mail.gmail.com

Besides the removal of CSV bits setting for protected VMs, No other functional change intended.

---
 arch/arm64/include/asm/kvm_host.h |  2 --
 arch/arm64/kvm/arm.c              | 17 ----------
 arch/arm64/kvm/id_regs.c          | 56 ++++++++++++++++++++++++-------
 3 files changed, 44 insertions(+), 31 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
index a7d4d9e093e3..4699f6b829b2 100644
@@ -248,8 +248,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 -17
index e34744c36406..0f71b10a2f05 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,7 +135,6 @@ 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_init_id_regs(kvm);
 
arch/arm64/kvm/id_regs.c +44 -12

91 lines not on this page · raw message

Signature · 1 line
2.40.1.495.gc816e09b53d-goog
#8 lore raw

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

kernel test robot noticed the following build errors:

Hi Jing,

kernel test robot noticed the following build errors:

[auto build test ERROR on 6a8f57ae2eb07ab39a6f0ccad60c760743051026]

url:    https://github.com/intel-lab-lkp/linux/commits/Jing-Zhang/KVM-arm64-Move-CPU-ID-feature-registers-emulation-into-a-separate-file/20230504-011759
base:   6a8f57ae2eb07ab39a6f0ccad60c760743051026
patch link:    https://lore.kernel.org/r/20230503171618.2020461-4-jingzhangos%40google.com
patch subject: [PATCH v8 3/6] KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]
config: arm64-allyesconfig (https://download.01.org/0day-ci/archive/20230504/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/5c887874c5459c9690cf3eac8b68022d72789533
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Jing-Zhang/KVM-arm64-Move-CPU-ID-feature-registers-emulation-into-a-separate-file/20230504-011759 git checkout 5c887874c5459c9690cf3eac8b68022d72789533
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash arch/arm64/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/linux/rhashtable-types.h:14,

                    from include/linux/ipc.h:7,
                    from include/uapi/linux/sem.h:5,
                    from include/linux/sem.h:5,
                    from include/linux/sched.h:15,
                    from include/linux/hardirq.h:9,
                    from include/linux/kvm_host.h:7,
                    from arch/arm64/kvm/id_regs.c:13:
   arch/arm64/kvm/id_regs.c: In function 'get_id_reg':
>> arch/arm64/kvm/id_regs.c:152:25: error: 'struct kvm_arch' has no member named 'config_lock'
     152 |         mutex_lock(&arch->config_lock);
         |                         ^~
   include/linux/mutex.h:187:44: note: in definition of macro 'mutex_lock'
     187 | #define mutex_lock(lock) mutex_lock_nested(lock, 0)
         |                                            ^~~~
   arch/arm64/kvm/id_regs.c:154:27: error: 'struct kvm_arch' has no member named 'config_lock'
     154 |         mutex_unlock(&arch->config_lock);
         |                           ^~
   arch/arm64/kvm/id_regs.c: In function 'set_id_aa64pfr0_el1':
   arch/arm64/kvm/id_regs.c:221:25: error: 'struct kvm_arch' has no member named 'config_lock'
     221 |         mutex_lock(&arch->config_lock);
         |                         ^~
   include/linux/mutex.h:187:44: note: in definition of macro 'mutex_lock'
     187 | #define mutex_lock(lock) mutex_lock_nested(lock, 0)
         |                                            ^~~~
   arch/arm64/kvm/id_regs.c:239:27: error: 'struct kvm_arch' has no member named 'config_lock'
     239 |         mutex_unlock(&arch->config_lock);
         |                           ^~

vim +152 arch/arm64/kvm/id_regs.c

139
140 /*
141 * cpufeature ID register user accessors
142 *
143 * For now, these registers are immutable for userspace, so no values
144 * are stored, and for set_id_reg() we don't allow the effective value
145 * to be changed.
146 */
147 static int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
148 u64 *val)
149 {
150 struct kvm_arch *arch = &vcpu->kvm->arch;
151

 > 152		mutex_lock(&arch->config_lock);

153 *val = read_id_reg(vcpu, rd);
154 mutex_unlock(&arch->config_lock);
155
156 return 0;
157 }
158

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
author
#9 lore raw

PATCH v8 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          | 94 +++++++++++++++++++++++++------
 include/kvm/arm_pmu.h             |  5 +-
 4 files changed, 87 insertions(+), 29 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index 4699f6b829b2..009f6ff41078 100644
@@ -236,6 +236,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;
 
@@ -248,11 +254,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 0f71b10a2f05..9ecd0c5d0754 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	kvm_arm_init_hypercalls(kvm);
 	kvm_arm_init_id_regs(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 +78 -16

143 lines not on this page · raw message

include/kvm/arm_pmu.h +3 -2
index 628775334d5e..e486347b297d 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),					\
+		    IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1)) >= ID_AA64DFR0_EL1_PMUVer_V3P5)
 
 u8 kvm_arm_pmu_get_pmuver_limit(void);
 
Signature · 1 line
2.40.1.495.gc816e09b53d-goog
author
#10 lore raw

PATCH v8 5/6 KVM: arm64: Reuse fields of sys_reg_desc for idreg

In reply to Jing Zhang

Since reset() and val are not used for idreg in sys_reg_desc, they would be used with other purposes for idregs.
The callback reset() would be used to return KVM sanitised id register values. The u64 val would be used as mask for writable fields in idregs. Only bits with 1 in val are writable from userspace.

---
 arch/arm64/kvm/id_regs.c  | 44 +++++++++++++++++++----------
 arch/arm64/kvm/sys_regs.c | 59 +++++++++++++++++++++++++++------------
 arch/arm64/kvm/sys_regs.h | 10 ++++---
 3 files changed, 77 insertions(+), 36 deletions(-)
arch/arm64/kvm/id_regs.c +30 -14
index 0a04a90a8676..64d40aa395be 100644
@@ -55,6 +55,11 @@ static u8 pmuver_to_perfmon(u8 pmuver)
 	}
 }
 
+static u64 general_read_kvm_sanitised_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd)
+{
+	return read_sanitised_ftr_reg(reg_to_encoding(rd));
+}
+
 u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 {
 	u64 val = IDREG(vcpu->kvm, id);
@@ -370,6 +375,17 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	return ret;
 }
 
+/*
+ * Since reset() callback and field val are not used for idregs, they will be
+ * used for specific purposes for idregs.
+ * The reset() would return KVM sanitised register value. The value would be the
+ * same as the host kernel sanitised value if there is no KVM sanitisation.
+ * The val would be used as a mask indicating writable fields for the idreg.
+ * Only bits with 1 are writable from userspace. This mask might not be
+ * necessary in the future whenever all ID registers are enabled as writable
+ * from userspace.
+ */
+
 /* sys_reg_desc initialiser for known cpufeature ID registers */
 #define ID_SANITISED(name) {			\
 	SYS_DESC(SYS_##name),			\
@@ -377,6 +393,8 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	.get_user = get_id_reg,			\
 	.set_user = set_id_reg,			\
 	.visibility = id_visibility,		\
+	.reset = general_read_kvm_sanitised_reg,\
+	.val = 0,				\
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
@@ -386,6 +404,8 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	.get_user = get_id_reg,			\
 	.set_user = set_id_reg,			\
 	.visibility = aa32_id_visibility,	\
+	.reset = general_read_kvm_sanitised_reg,\
+	.val = 0,				\
 }
 
 /*
@@ -398,7 +418,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	.access = access_id_reg,			\
 	.get_user = get_id_reg,				\
 	.set_user = set_id_reg,				\
-	.visibility = raz_visibility			\
+	.visibility = raz_visibility,			\
+	.reset = NULL,					\
+	.val = 0,					\
 }
 
 /*
@@ -412,6 +434,8 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	.get_user = get_id_reg,			\
 	.set_user = set_id_reg,			\
 	.visibility = raz_visibility,		\
+	.reset = NULL,				\
+	.val = 0,				\
 }
 
 const struct sys_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
@@ -522,10 +546,7 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 	return 1;
 }
 
-/*
- * Set the guest's ID registers that are defined in id_reg_descs[]
- * with ID_SANITISED() to the host's sanitized value.
- */
+/* Initialize the guest's ID registers with KVM sanitised values. */
 void kvm_arm_init_id_regs(struct kvm *kvm)
 {
 	u64 val;
@@ -535,16 +556,11 @@ void kvm_arm_init_id_regs(struct kvm *kvm)
 	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
 		id = reg_to_encoding(&id_reg_descs[i]);
 
-		/*
-		 * Some hidden ID registers which are not in arm64_ftr_regs[]
-		 * would cause warnings from read_sanitised_ftr_reg().
-		 * Skip those ID registers to avoid the warnings.
-		 */
-		if (id_reg_descs[i].visibility == raz_visibility)
-			/* Hidden or reserved ID register */
-			continue;
+		val = 0;
+		/* Read KVM sanitised register value if available */
+		if (id_reg_descs[i].reset)
+			val = id_reg_descs[i].reset(NULL, &id_reg_descs[i]);
 
-		val = read_sanitised_ftr_reg(id);
 		IDREG(kvm, id) = val;
 	}
 
arch/arm64/kvm/sys_regs.c +41 -18

187 lines not on this page · raw message

arch/arm64/kvm/sys_regs.h +6 -4
index e88fd77309b2..21869319f6e1 100644
@@ -65,12 +65,12 @@ struct sys_reg_desc {
 		       const struct sys_reg_desc *);
 
 	/* Initialization for vcpu. */
-	void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
+	u64 (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
 
 	/* Index into sys_reg[], or 0 if we don't need to save it. */
 	int reg;
 
-	/* Value (usually reset value) */
+	/* Value (usually reset value), or write mask for idregs */
 	u64 val;
 
 	/* Custom get/set_user functions, fallback to generic if NULL */
@@ -123,19 +123,21 @@ static inline bool read_zero(struct kvm_vcpu *vcpu,
 }
 
 /* Reset functions */
-static inline void reset_unknown(struct kvm_vcpu *vcpu,
+static inline u64 reset_unknown(struct kvm_vcpu *vcpu,
 				 const struct sys_reg_desc *r)
 {
 	BUG_ON(!r->reg);
 	BUG_ON(r->reg >= NR_SYS_REGS);
 	__vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
+	return __vcpu_sys_reg(vcpu, r->reg);
 }
 
-static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
+static inline u64 reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	BUG_ON(!r->reg);
 	BUG_ON(r->reg >= NR_SYS_REGS);
 	__vcpu_sys_reg(vcpu, r->reg) = r->val;
+	return __vcpu_sys_reg(vcpu, r->reg);
 }
 
 static inline unsigned int sysreg_visibility(const struct kvm_vcpu *vcpu,
Signature · 1 line
2.40.1.495.gc816e09b53d-goog
#11 lore raw

Re: [PATCH v8 5/6] KVM: arm64: Reuse fields of sys_reg_desc for idreg

Quoted 13 lines · Jing Zhang

Since reset() and val are not used for idreg in sys_reg_desc, they would be used with other purposes for idregs.
The callback reset() would be used to return KVM sanitised id register values. The u64 val would be used as mask for writable fields in idregs. Only bits with 1 in val are writable from userspace.

---
 arch/arm64/kvm/id_regs.c  | 44 +++++++++++++++++++----------
 arch/arm64/kvm/sys_regs.c | 59 +++++++++++++++++++++++++++------------
 arch/arm64/kvm/sys_regs.h | 10 ++++---
 3 files changed, 77 insertions(+), 36 deletions(-)

(...)

arch/arm64/kvm/sys_regs.h struct sys_reg_desc { · line 67

Quoted 8 lines
arch/arm64/kvm/sys_regs.h +0 -0
index e88fd77309b2..21869319f6e1 100644
@@ -65,12 +65,12 @@ struct sys_reg_desc {
 		       const struct sys_reg_desc *);
 
 	/* Initialization for vcpu. */

Maybe be a bit more verbose here?

/* Initialization for vcpu. Return initialized value, or KVM sanitized
value for id registers. */

Quoted 11 lines
arch/arm64/kvm/sys_regs.h +2 -2
-	void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
+	u64 (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
 
 	/* Index into sys_reg[], or 0 if we don't need to save it. */
 	int reg;
 
-	/* Value (usually reset value) */
+	/* Value (usually reset value), or write mask for idregs */
 	u64 val;
 
 	/* Custom get/set_user functions, fallback to generic if NULL */
author
#12 lore raw

Re: [PATCH v8 5/6] KVM: arm64: Reuse fields of sys_reg_desc for idreg

Hi Cornelia,

Quoted 26 lines · Cornelia Huck
Quoted 13 lines · Jing Zhang

Since reset() and val are not used for idreg in sys_reg_desc, they would be used with other purposes for idregs.
The callback reset() would be used to return KVM sanitised id register values. The u64 val would be used as mask for writable fields in idregs. Only bits with 1 in val are writable from userspace.

---
 arch/arm64/kvm/id_regs.c  | 44 +++++++++++++++++++----------
 arch/arm64/kvm/sys_regs.c | 59 +++++++++++++++++++++++++++------------
 arch/arm64/kvm/sys_regs.h | 10 ++++---
 3 files changed, 77 insertions(+), 36 deletions(-)

(...)

Quoted 7 lines
arch/arm64/kvm/sys_regs.h +0 -0
index e88fd77309b2..21869319f6e1 100644
@@ -65,12 +65,12 @@ struct sys_reg_desc {
                     const struct sys_reg_desc *);
 
Quoted 6 lines

arch/arm64/kvm/sys_regs.h struct sys_reg_desc { · line 67

Quoted 1 line
arch/arm64/kvm/sys_regs.h +0 -0
      /* Initialization for vcpu. */

Maybe be a bit more verbose here?

/* Initialization for vcpu. Return initialized value, or KVM sanitized
value for id registers. */

Sure. Thanks.

Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/sys_regs.h +1 -1
-     void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
+     u64 (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
 
Quoted 9 lines

arch/arm64/kvm/sys_regs.h struct sys_reg_desc { · line 76

Quoted 8 lines
arch/arm64/kvm/sys_regs.h +1 -1
      /* Index into sys_reg[], or 0 if we don't need to save it. */
      int reg;
 
-     /* Value (usually reset value) */
+     /* Value (usually reset value), or write mask for idregs */
      u64 val;
 
      /* Custom get/set_user functions, fallback to generic if NULL */

Jing

author
#13 lore raw

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

In reply to Jing Zhang

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 array.

---
 arch/arm64/include/asm/cpufeature.h |   1 +
 arch/arm64/kernel/cpufeature.c      |   2 +-
 arch/arm64/kvm/id_regs.c            | 361 ++++++++++++++++++----------
 3 files changed, 242 insertions(+), 122 deletions(-)
arch/arm64/include/asm/cpufeature.h +1 -0
index 6bf013fb110d..dc769c2eb7a4 100644
@@ -915,6 +915,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/kernel/cpufeature.c +1 -1
index 2e3e55139777..677ec4fe9f6b 100644
@@ -791,7 +791,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/id_regs.c +240 -121

469 lines not on this page · raw message

Signature · 1 line
2.40.1.495.gc816e09b53d-goog
#14 lore raw

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

Quoted 13 lines · Jing Zhang

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 array.

---
 arch/arm64/include/asm/cpufeature.h |   1 + arch/arm64/kernel/cpufeature.c      |   2 +- arch/arm64/kvm/id_regs.c            | 361 ++++++++++++++++++-------- --

 3 files changed, 242 insertions(+), 122 deletions(-)

[ SNIP ]

Quoted 49 lines


+static u64 read_sanitised_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, +                                         const struct sys_reg_desc *rd)

arch/arm64/kvm/id_regs.c +7 -0
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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); +       }

arch/arm64/kvm/id_regs.c +6 -0
+
+       val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
+
+       return val;
+}
+

 static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 val)
 {
-       struct kvm_arch *arch = &vcpu->kvm->arch;
-       u64 sval = val;
        u8 csv2, csv3;
-       int ret = 0;

        /*
         * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as

(quoted diff) +0 -0
@@ -226,26 +338,30 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu
 *vcpu,
Quoted 3 lines

        if (csv3 > 1 || (csv3 && arm64_get_meltdown_state() != SPECTRE_UNAFFECTED))
                return -EINVAL;

Can't we remove the checking of csv[23] here as it will be checked by arm64_check_features()?

i.e. in arm64_check_features() we will load the "limit" value from the "reset" function (read_sanitised_id_aa64pfr0_el1()) which has csv[23] set appropriately and limit it to a safe value basically performing the same check as we are here.

Quoted 100 lines


-       mutex_lock(&arch->config_lock);
-       /* 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) {
-               ret = -EINVAL;
-               goto out;
-       }
+       return set_id_reg(vcpu, rd, val);
+}

-       /* Only allow userspace to change the idregs before VM running */
-       if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &vcpu->kvm-

Quoted 1 line

arch.flags)) {

-               if (sval != read_id_reg(vcpu, rd)) -                       ret = -EBUSY;
-       } else {
-               IDREG(vcpu->kvm, reg_to_encoding(rd)) = sval; -       }

arch/arm64/kvm/id_regs.c +2 -3
-out:
-       mutex_unlock(&arch->config_lock);
-       return ret;
+static u64 read_sanitised_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
+                                         const struct sys_reg_desc

*rd)

arch/arm64/kvm/id_regs.c +8 -0
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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); +
+       return val;
 }

 static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,

arch/arm64/kvm/id_regs.c +0 -1
@@ -255,7 +371,6 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu
 *vcpu,
         struct kvm_arch *arch = &vcpu->kvm->arch;
         u8 pmuver, host_pmuver;
         bool valid_pmu;
-       u64 sval = val;
         int ret = 0;


        host_pmuver = kvm_arm_pmu_get_pmuver_limit();

(quoted diff) +0 -0
@@ -277,40 +392,61 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu
 *vcpu,
                 return -EINVAL;


        mutex_lock(&arch->config_lock);
-       /* 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) {
-               ret = -EINVAL;
-               goto out;
-       }
-
        /* Only allow userspace to change the idregs before VM running */
        if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &vcpu->kvm-

Quoted 1 line

arch.flags)) {

-               if (sval != read_id_reg(vcpu, rd))
+               if (val != read_id_reg(vcpu, rd))
                        ret = -EBUSY;
-       } else {
-               if (valid_pmu) {
-                       val = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1); -                       val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; -                       val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK, pmuver);
-                       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val; -
-                       val = IDREG(vcpu->kvm, SYS_ID_DFR0_EL1); -                       val &= ~ID_DFR0_EL1_PerfMon_MASK; -                       val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK, pmuver_to_perfmon(pmuver));
-                       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val; -               } else {
-
                       assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags,
-                                  pmuver ==

Quoted 8 lines

ID_AA64DFR0_EL1_PMUVer_IMP_DEF);
-               }
+               goto out;
+       }
+
+       if (!valid_pmu) {
+               /*
+                * Ignore the PMUVer filed in @val. The PMUVer would

Nit s/filed/field

Quoted 103 lines

be determined
+                * by arch flags bit KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU,
+                */
+               pmuver = FIELD_GET(ID_AA64DFR0_EL1_PMUVer_MASK, +                                  IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+               val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; +               val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK, pmuver);
        }

+       ret = arm64_check_features(vcpu, rd, val);
+       if (ret)
+               goto out;

arch/arm64/kvm/id_regs.c +6 -0
+
+       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val;
+
+       val = IDREG(vcpu->kvm, SYS_ID_DFR0_EL1);
+       val &= ~ID_DFR0_EL1_PerfMon_MASK;
+       val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK,

pmuver_to_perfmon(pmuver));
+       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val;
+
+       if (!valid_pmu)
+               assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu-

Quoted 1 line

kvm->arch.flags,

+                          pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF); +
 out:
        mutex_unlock(&arch->config_lock);
        return ret;
 }

arch/arm64/kvm/id_regs.c +13 -0
+static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu,
+                                     const struct sys_reg_desc *rd)
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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());

arch/arm64/kvm/id_regs.c +4 -0
+
+       return val;
+}
+

 static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 val)

arch/arm64/kvm/id_regs.c +0 -1
@@ -318,7 +454,6 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
         struct kvm_arch *arch = &vcpu->kvm->arch;
         u8 perfmon, host_perfmon;
         bool valid_pmu;
-       u64 sval = val;
         int ret = 0;


        host_perfmon = pmuver_to_perfmon(kvm_arm_pmu_get_pmuver_limit());

(quoted diff) +0 -0
@@ -341,35 +476,39 @@ static int set_id_dfr0_el1(struct kvm_vcpu
 *vcpu,
                 return -EINVAL;


        mutex_lock(&arch->config_lock);
-       /* 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) {
-               ret = -EINVAL;
-               goto out;
-       }
-
        /* Only allow userspace to change the idregs before VM running */
        if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &vcpu->kvm-

Quoted 1 line

arch.flags)) {

-               if (sval != read_id_reg(vcpu, rd))
+               if (val != read_id_reg(vcpu, rd))
                        ret = -EBUSY;
-       } else {
-               if (valid_pmu) {
-                       val = IDREG(vcpu->kvm, SYS_ID_DFR0_EL1); -                       val &= ~ID_DFR0_EL1_PerfMon_MASK; -                       val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK, perfmon);
-                       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val; -
-                       val = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1); -                       val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; -                       val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK, perfmon_to_pmuver(perfmon)); -                       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val; -               } else {
-
                       assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags,
-                                  perfmon ==

Quoted 8 lines

ID_DFR0_EL1_PerfMon_IMPDEF);
-               }
+               goto out;
+       }
+
+       if (!valid_pmu) {
+               /*
+                * Ignore the PerfMon filed in @val. The PerfMon

Nit s/filed/field

Quoted 25 lines

would be determined
+                * by arch flags bit KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU,
+                */
+               perfmon = FIELD_GET(ID_DFR0_EL1_PerfMon_MASK, +                                   IDREG(vcpu->kvm, SYS_ID_DFR0_EL1));
+               val &= ~ID_DFR0_EL1_PerfMon_MASK;
+               val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK, perfmon); }

+       ret = arm64_check_features(vcpu, rd, val);
+       if (ret)
+               goto out;

arch/arm64/kvm/id_regs.c +6 -0
+
+       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val;
+
+       val = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1);
+       val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
+       val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK,

perfmon_to_pmuver(perfmon));
+       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val;
+
+       if (!valid_pmu)
+               assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu-

Quoted 6 lines
Quoted 1 line

kvm->arch.flags,

+                          perfmon == ID_DFR0_EL1_PerfMon_IMPDEF); +
 out:
        mutex_unlock(&arch->config_lock);
        return ret;

Otherwise looks good!

Thanks,
Suraj

author
#15 lore raw

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

Hi Suraj,

Quoted 74 lines · Suraj
Quoted 13 lines · Jing Zhang

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 array.

---
 arch/arm64/include/asm/cpufeature.h |   1 +
 arch/arm64/kernel/cpufeature.c      |   2 +-
 arch/arm64/kvm/id_regs.c            | 361 ++++++++++++++++++--------
--
 3 files changed, 242 insertions(+), 122 deletions(-)

[ SNIP ]

Quoted 48 lines
(quoted diff) +2 -0
+static u64 read_sanitised_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
+                                         const struct sys_reg_desc

*rd)

arch/arm64/kvm/id_regs.c +7 -0
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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

arch/arm64/kvm/id_regs.c +8 -0
+        * 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 +4 -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);

arch/arm64/kvm/id_regs.c +7 -3
+       }
+
+       val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
+
+       return val;
+}
+
 static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
                               const struct sys_reg_desc *rd,
                               u64 val)
 {
-       struct kvm_arch *arch = &vcpu->kvm->arch;
-       u64 sval = val;
        u8 csv2, csv3;
-       int ret = 0;
 
        /*
         * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long

as

Review static int set_id_aa64pfr0_el1(struct kvm_vcpu · line 341

Quoted 4 lines
(quoted diff) +0 -0
@@ -226,26 +338,30 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu
 *vcpu,
        if (csv3 > 1 || (csv3 && arm64_get_meltdown_state() !=
 SPECTRE_UNAFFECTED))
                return -EINVAL;

Can't we remove the checking of csv[23] here as it will be checked by arm64_check_features()?

Quoted 4 lines

i.e. in arm64_check_features() we will load the "limit" value from the "reset" function (read_sanitised_id_aa64pfr0_el1()) which has csv[23] set appropriately and limit it to a safe value basically performing the same check as we are here.

The limit and the check here might be different if like arm64_get_meltdown_state() is not SPECTRE_UNAFFECTED.
i.e. if we remove the check here, theoretically, the csv3 can be set a value greater 1 if arm64_get_meltdown_state() is not SPECTRE_UNAFFECTED.

Quoted 103 lines
Quoted 103 lines
(quoted diff) +0 -2
-       mutex_lock(&arch->config_lock);
-       /* We can only differ with CSV[23], and anything else is an

error */

arch/arm64/kvm/id_regs.c +2 -8
-       val ^= read_id_reg(vcpu, rd);
-       val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
-                ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
-       if (val) {
-               ret = -EINVAL;
-               goto out;
-       }
+       return set_id_reg(vcpu, rd, val);
+}
 
-       /* Only allow userspace to change the idregs before VM

running */
- if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &vcpu->kvm-

Quoted 1 line

arch.flags)) {

arch/arm64/kvm/id_regs.c +2 -8
-               if (sval != read_id_reg(vcpu, rd))
-                       ret = -EBUSY;
-       } else {
-               IDREG(vcpu->kvm, reg_to_encoding(rd)) = sval;
-       }
-out:
-       mutex_unlock(&arch->config_lock);
-       return ret;
+static u64 read_sanitised_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
+                                         const struct sys_reg_desc

*rd)

arch/arm64/kvm/id_regs.c +8 -0
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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);

arch/arm64/kvm/id_regs.c +11 -1
+       /*
+        * 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);
+
+       return val;
 }
 
 static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
@@ -255,7 +371,6 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu
 *vcpu,
        struct kvm_arch *arch = &vcpu->kvm->arch;
        u8 pmuver, host_pmuver;
        bool valid_pmu;
-       u64 sval = val;
        int ret = 0;
 
        host_pmuver = kvm_arm_pmu_get_pmuver_limit();
arch/arm64/kvm/id_regs.c +0 -1
@@ -277,40 +392,61 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu
 *vcpu,
                return -EINVAL;
 
        mutex_lock(&arch->config_lock);
-       /* We can only differ with PMUver, and anything else is an

error */

arch/arm64/kvm/id_regs.c +0 -7
-       val ^= read_id_reg(vcpu, rd);
-       val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
-       if (val) {
-               ret = -EINVAL;
-               goto out;
-       }
-
        /* Only allow userspace to change the idregs before VM

running */

        if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &vcpu->kvm-
Quoted 1 line

arch.flags)) {

arch/arm64/kvm/id_regs.c +1 -6
-               if (sval != read_id_reg(vcpu, rd))
+               if (val != read_id_reg(vcpu, rd))
                        ret = -EBUSY;
-       } else {
-               if (valid_pmu) {
-                       val = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1);
-                       val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
-                       val |=

FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK, pmuver);

arch/arm64/kvm/id_regs.c +0 -5
-                       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val;
-
-                       val = IDREG(vcpu->kvm, SYS_ID_DFR0_EL1);
-                       val &= ~ID_DFR0_EL1_PerfMon_MASK;
-                       val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK,

pmuver_to_perfmon(pmuver));

arch/arm64/kvm/id_regs.c +0 -3
-                       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val;
-               } else {
-
                       assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU,

&vcpu->kvm->arch.flags,
- pmuver ==
ID_AA64DFR0_EL1_PMUVer_IMP_DEF);
- }

Quoted 8 lines

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

Quoted 6 lines
arch/arm64/kvm/id_regs.c +6 -0
+               goto out;
+       }
+
+       if (!valid_pmu) {
+               /*
+                * Ignore the PMUVer filed in @val. The PMUVer would

Nit s/filed/field

Will fix.

Quoted 106 lines
Quoted 106 lines

be determined
+ * by arch flags bit
KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU,

arch/arm64/kvm/id_regs.c +3 -0
+                */
+               pmuver = FIELD_GET(ID_AA64DFR0_EL1_PMUVer_MASK,
+                                  IDREG(vcpu->kvm,

SYS_ID_AA64DFR0_EL1));

(quoted diff) +2 -0
+               val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
+               val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK,
pmuver);
        }
arch/arm64/kvm/id_regs.c +9 -0
+       ret = arm64_check_features(vcpu, rd, val);
+       if (ret)
+               goto out;
+
+       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val;
+
+       val = IDREG(vcpu->kvm, SYS_ID_DFR0_EL1);
+       val &= ~ID_DFR0_EL1_PerfMon_MASK;
+       val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK,

pmuver_to_perfmon(pmuver));

arch/arm64/kvm/id_regs.c +4 -0
+       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val;
+
+       if (!valid_pmu)
+               assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu-
Quoted 1 line

kvm->arch.flags,

arch/arm64/kvm/id_regs.c +15 -0
+                          pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF);
+
 out:
        mutex_unlock(&arch->config_lock);
        return ret;
 }
 
+static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu,
+                                     const struct sys_reg_desc *rd)
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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());

arch/arm64/kvm/id_regs.c +4 -1
+
+       return val;
+}
+
 static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
                           const struct sys_reg_desc *rd,
                           u64 val)
@@ -318,7 +454,6 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
        struct kvm_arch *arch = &vcpu->kvm->arch;
        u8 perfmon, host_perfmon;
        bool valid_pmu;
-       u64 sval = val;
        int ret = 0;
 
        host_perfmon =

pmuver_to_perfmon(kvm_arm_pmu_get_pmuver_limit());

arch/arm64/kvm/id_regs.c +0 -1
@@ -341,35 +476,39 @@ static int set_id_dfr0_el1(struct kvm_vcpu
 *vcpu,
                return -EINVAL;
 
        mutex_lock(&arch->config_lock);
-       /* We can only differ with PerfMon, and anything else is an

error */

arch/arm64/kvm/id_regs.c +0 -7
-       val ^= read_id_reg(vcpu, rd);
-       val &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
-       if (val) {
-               ret = -EINVAL;
-               goto out;
-       }
-
        /* Only allow userspace to change the idregs before VM

running */

        if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &vcpu->kvm-
Quoted 1 line

arch.flags)) {

arch/arm64/kvm/id_regs.c +1 -6
-               if (sval != read_id_reg(vcpu, rd))
+               if (val != read_id_reg(vcpu, rd))
                        ret = -EBUSY;
-       } else {
-               if (valid_pmu) {
-                       val = IDREG(vcpu->kvm, SYS_ID_DFR0_EL1);
-                       val &= ~ID_DFR0_EL1_PerfMon_MASK;
-                       val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK,

perfmon);

arch/arm64/kvm/id_regs.c +0 -5
-                       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val;
-
-                       val = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1);
-                       val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
-                       val |=

FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK, perfmon_to_pmuver(perfmon));

arch/arm64/kvm/id_regs.c +0 -3
-                       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val;
-               } else {
-
                       assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU,

&vcpu->kvm->arch.flags,
- perfmon ==
ID_DFR0_EL1_PerfMon_IMPDEF);
- }

Quoted 8 lines

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

Quoted 6 lines
arch/arm64/kvm/id_regs.c +6 -0
+               goto out;
+       }
+
+       if (!valid_pmu) {
+               /*
+                * Ignore the PerfMon filed in @val. The PerfMon

Nit s/filed/field

Thanks, will fix it.

Quoted 33 lines
Quoted 27 lines

would be determined
+ * by arch flags bit
KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU,

arch/arm64/kvm/id_regs.c +3 -0
+                */
+               perfmon = FIELD_GET(ID_DFR0_EL1_PerfMon_MASK,
+                                   IDREG(vcpu->kvm,

SYS_ID_DFR0_EL1));

arch/arm64/kvm/id_regs.c +11 -0
+               val &= ~ID_DFR0_EL1_PerfMon_MASK;
+               val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK, perfmon);
        }
 
+       ret = arm64_check_features(vcpu, rd, val);
+       if (ret)
+               goto out;
+
+       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val;
+
+       val = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1);
+       val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
+       val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK,

perfmon_to_pmuver(perfmon));

arch/arm64/kvm/id_regs.c +4 -0
+       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val;
+
+       if (!valid_pmu)
+               assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu-
Quoted 1 line

kvm->arch.flags,

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

Quoted 5 lines
arch/arm64/kvm/id_regs.c +2 -0
+                          perfmon == ID_DFR0_EL1_PerfMon_IMPDEF);
+
 out:
        mutex_unlock(&arch->config_lock);
        return ret;
Quoted 4 lines

Otherwise looks good!

Thanks,
Suraj

Thanks,
Jing

#16 lore raw

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

Quoted 97 lines · Jing Zhang

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

Hi Suraj,

Quoted 80 lines · Suraj
Quoted 14 lines · Jing Zhang

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 array.

---
 arch/arm64/include/asm/cpufeature.h |   1 + arch/arm64/kernel/cpufeature.c      |   2 +- arch/arm64/kvm/id_regs.c            | 361 ++++++++++++++++++----
----
--

 3 files changed, 242 insertions(+), 122 deletions(-)

[ SNIP ]

Quoted 51 lines

+static u64 read_sanitised_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, +                                         const struct sys_reg_desc
*rd)

arch/arm64/kvm/id_regs.c +7 -0
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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); +       }

arch/arm64/kvm/id_regs.c +6 -0
+
+       val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
+
+       return val;
+}
+

 static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 val)
 {
-       struct kvm_arch *arch = &vcpu->kvm->arch;
-       u64 sval = val;
        u8 csv2, csv3;
-       int ret = 0;

        /*
         * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long
as

(quoted diff) +0 -0
@@ -226,26 +338,30 @@ static int set_id_aa64pfr0_el1(struct
Quoted 5 lines

kvm_vcpu
*vcpu,
        if (csv3 > 1 || (csv3 && arm64_get_meltdown_state() != SPECTRE_UNAFFECTED))
                return -EINVAL;

Can't we remove the checking of csv[23] here as it will be checked by
arm64_check_features()?

Quoted 7 lines

i.e. in arm64_check_features() we will load the "limit" value from the
"reset" function (read_sanitised_id_aa64pfr0_el1()) which has csv[23]
set appropriately and limit it to a safe value basically performing the
same check as we are here.

Quoted 6 lines

The limit and the check here might be different if like arm64_get_meltdown_state() is not SPECTRE_UNAFFECTED. i.e. if we remove the check here, theoretically, the csv3 can be set a
value greater 1 if arm64_get_meltdown_state() is not SPECTRE_UNAFFECTED.

Reading init_cpu_ftr_reg() is hurting my head :p but don't we have basically 2 cases here?

1. We are unaffected by spectre|meltdown i.e. arm64_get_[spectre|meltdown]_v2_state() will return SPECTRE_UNAFFECTED and we will set the limit to 1 for the csv[1|2] bit fields in read_sanitised_id_aa64pfr0_el1()

or

2. We ARE affected by spectre|meltdown in which case arm64_get_[spectre|meltdown]_v2_state() will be SPECTRE_VULNERABLE|SPECTRE_MITIGATED in which case read_sanitised_ftr_reg() will return a value with csv[1|2] set to 0 essentially setting the limit for either of these bit fields to 0 in read_sanitised_id_aa64pfr0_el1().

Are we trying to catch the case where csv[1|2] is 0 on the host but we are unaffected as detected by e.g. cpuid and that cpu happens to incorrectly not set csv[1|2] in the ID register but we still want to allow the guest to see those bits as set?

This isn't really related to the CR as I know this is existing code which has just been moved around and sorry if I'm missing something obvious.

Thanks,
Suraj

Quoted 294 lines
Quoted 115 lines
Quoted 115 lines

-       mutex_lock(&arch->config_lock);
-       /* 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) {
-               ret = -EINVAL;
-               goto out;
-       }
+       return set_id_reg(vcpu, rd, val);
+}

-       /* Only allow userspace to change the idregs before VM running */
-       if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &vcpu->kvm-

Quoted 1 line

arch.flags)) {

-               if (sval != read_id_reg(vcpu, rd)) -                       ret = -EBUSY;
-       } else {
-               IDREG(vcpu->kvm, reg_to_encoding(rd)) = sval; -       }

arch/arm64/kvm/id_regs.c +2 -3
-out:
-       mutex_unlock(&arch->config_lock);
-       return ret;
+static u64 read_sanitised_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
+                                         const struct

sys_reg_desc
*rd)

arch/arm64/kvm/id_regs.c +8 -0
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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); +
+       return val;
 }

 static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,

(quoted diff) +0 -0
@@ -255,7 +371,6 @@ static int set_id_aa64dfr0_el1(struct

kvm_vcpu
*vcpu,
        struct kvm_arch *arch = &vcpu->kvm->arch;
        u8 pmuver, host_pmuver;
        bool valid_pmu;
-       u64 sval = val;
        int ret = 0;

        host_pmuver = kvm_arm_pmu_get_pmuver_limit();

(quoted diff) +0 -0
@@ -277,40 +392,61 @@ static int set_id_aa64dfr0_el1(struct

kvm_vcpu
*vcpu,
                return -EINVAL;

        mutex_lock(&arch->config_lock);
-       /* 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) {
-               ret = -EINVAL;
-               goto out;
-       }
-
        /* Only allow userspace to change the idregs before VM running */
        if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &vcpu->kvm-

Quoted 1 line

arch.flags)) {

-               if (sval != read_id_reg(vcpu, rd))
+               if (val != read_id_reg(vcpu, rd))
                        ret = -EBUSY;
-       } else {
-               if (valid_pmu) {
-                       val = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1);
-                       val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; -                       val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK, pmuver);
-                       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val;
-
-                       val = IDREG(vcpu->kvm, SYS_ID_DFR0_EL1); -                       val &= ~ID_DFR0_EL1_PerfMon_MASK; -                       val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK, pmuver_to_perfmon(pmuver));
-                       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val; -               } else {
-

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

Quoted 8 lines
Quoted 6 lines

+       }
+
+       if (!valid_pmu) {
+               /*
+                * Ignore the PMUVer filed in @val. The PMUVer would

Nit s/filed/field

Will fix.

Quoted 118 lines
Quoted 118 lines

be determined
+                * by arch flags bit KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU,
+                */
+               pmuver = FIELD_GET(ID_AA64DFR0_EL1_PMUVer_MASK, +                                  IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+               val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; +               val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK, pmuver);
        }

+       ret = arm64_check_features(vcpu, rd, val); +       if (ret)
+               goto out;

arch/arm64/kvm/id_regs.c +6 -0
+
+       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val;
+
+       val = IDREG(vcpu->kvm, SYS_ID_DFR0_EL1);
+       val &= ~ID_DFR0_EL1_PerfMon_MASK;
+       val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK,

pmuver_to_perfmon(pmuver));
+       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val;
+
+       if (!valid_pmu)
+               assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu-

Quoted 1 line

kvm->arch.flags,

+                          pmuver ==
ID_AA64DFR0_EL1_PMUVer_IMP_DEF);
+
 out:
        mutex_unlock(&arch->config_lock);
        return ret;
 }

+static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu, +                                     const struct sys_reg_desc *rd)

arch/arm64/kvm/id_regs.c +7 -0
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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());

arch/arm64/kvm/id_regs.c +4 -0
+
+       return val;
+}
+

 static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 val)

arch/arm64/kvm/id_regs.c +0 -1
@@ -318,7 +454,6 @@ static int set_id_dfr0_el1(struct kvm_vcpu
 *vcpu,
         struct kvm_arch *arch = &vcpu->kvm->arch;
         u8 perfmon, host_perfmon;
         bool valid_pmu;
-       u64 sval = val;
         int ret = 0;
 

        host_perfmon = pmuver_to_perfmon(kvm_arm_pmu_get_pmuver_limit());

arch/arm64/kvm/id_regs.c +0 -1
@@ -341,35 +476,39 @@ static int set_id_dfr0_el1(struct kvm_vcpu
 *vcpu,
                 return -EINVAL;
 
         mutex_lock(&arch->config_lock);
-       /* 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) {
-               ret = -EINVAL;
-               goto out;
-       }
-
        /* Only allow userspace to change the idregs before VM running */
        if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &vcpu->kvm-

Quoted 1 line

arch.flags)) {

-               if (sval != read_id_reg(vcpu, rd))
+               if (val != read_id_reg(vcpu, rd))
                        ret = -EBUSY;
-       } else {
-               if (valid_pmu) {
-                       val = IDREG(vcpu->kvm, SYS_ID_DFR0_EL1); -                       val &= ~ID_DFR0_EL1_PerfMon_MASK; -                       val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK,
perfmon);
-                       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val; -
-                       val = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1);
-                       val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; -                       val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK, perfmon_to_pmuver(perfmon));
-                       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val;
-               } else {
-

assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags,
-                                  perfmon == ID_DFR0_EL1_PerfMon_IMPDEF);
-               }

Quoted 8 lines
Quoted 6 lines

+               goto out;
+       }
+
+       if (!valid_pmu) {
+               /*
+                * Ignore the PerfMon filed in @val. The PerfMon

Nit s/filed/field

Thanks, will fix it.

Quoted 36 lines
Quoted 28 lines

would be determined
+                * by arch flags bit KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU,
+                */
+               perfmon = FIELD_GET(ID_DFR0_EL1_PerfMon_MASK, +                                   IDREG(vcpu->kvm, SYS_ID_DFR0_EL1));
+               val &= ~ID_DFR0_EL1_PerfMon_MASK;
+               val |= FIELD_PREP(ID_DFR0_EL1_PerfMon_MASK, perfmon);
        }

+       ret = arm64_check_features(vcpu, rd, val); +       if (ret)
+               goto out;

arch/arm64/kvm/id_regs.c +6 -0
+
+       IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) = val;
+
+       val = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1);
+       val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
+       val |= FIELD_PREP(ID_AA64DFR0_EL1_PMUVer_MASK,

perfmon_to_pmuver(perfmon));
+       IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) = val; +
+       if (!valid_pmu)
+               assign_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu-

Quoted 7 lines
Quoted 1 line

kvm->arch.flags,

+                          perfmon ==
ID_DFR0_EL1_PerfMon_IMPDEF);
+
 out:
        mutex_unlock(&arch->config_lock);
        return ret;

Quoted 4 lines

Otherwise looks good!

Thanks,
Suraj

Thanks,
Jing

maintainer
#17 lore raw

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

Quoted 9 lines · Suraj

Reading init_cpu_ftr_reg() is hurting my head :p but don't we have basically 2 cases here?

1. We are unaffected by spectre|meltdown i.e. arm64_get_[spectre|meltdown]_v2_state() will return SPECTRE_UNAFFECTED and we will set the limit to 1 for the csv[1|2] bit fields in read_sanitised_id_aa64pfr0_el1()

Quoted 8 lines

or

2. We ARE affected by spectre|meltdown in which case arm64_get_[spectre|meltdown]_v2_state() will be SPECTRE_VULNERABLE|SPECTRE_MITIGATED in which case read_sanitised_ftr_reg() will return a value with csv[1|2] set to 0 essentially setting the limit for either of these bit fields to 0 in read_sanitised_id_aa64pfr0_el1().

What is "WE"?

Quoted 9 lines

Are we trying to catch the case where csv[1|2] is 0 on the host but we are unaffected as detected by e.g. cpuid and that cpu happens to incorrectly not set csv[1|2] in the ID register but we still want to allow the guest to see those bits as set?

This isn't really related to the CR as I know this is existing code which has just been moved around and sorry if I'm missing something obvious.

This code is called from *userspace*, and tries to cope with a VM being restored. So we have 3 (not 2 cases):

- both the source and the destination have the same level of CSVx support, and all is great in the world

- the source has CSVx==0, while the destination has CSVx=1. All good, as we won't be lying to the guest, and the extra mitigation executed by the guest isn't a functional problem. The guest will still see CSVx=0 after migration.

- the source has CSVx=1, while the destination has CSVx=0. This isn't an acceptable situation, and we return an error

Is that clearer?

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

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

Quoted 28 lines · Marc Zyngier

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

Quoted 12 lines · Suraj

Reading init_cpu_ftr_reg() is hurting my head :p but don't we have basically 2 cases here?

1. We are unaffected by spectre|meltdown i.e. arm64_get_[spectre|meltdown]_v2_state() will return SPECTRE_UNAFFECTED
and we will set the limit to 1 for the csv[1|2] bit fields in read_sanitised_id_aa64pfr0_el1()

or

Quoted 7 lines

2. We ARE affected by spectre|meltdown in which case arm64_get_[spectre|meltdown]_v2_state() will be SPECTRE_VULNERABLE|SPECTRE_MITIGATED in which case read_sanitised_ftr_reg() will return a value with csv[1|2] set to 0 essentially setting the limit for either of these bit fields to 0 in
read_sanitised_id_aa64pfr0_el1().

Quoted 1 line

What is "WE"?

The CPU

Quoted 24 lines
Quoted 9 lines

Are we trying to catch the case where csv[1|2] is 0 on the host but we
are unaffected as detected by e.g. cpuid and that cpu happens to incorrectly not set csv[1|2] in the ID register but we still want to
allow the guest to see those bits as set?

Quoted 3 lines

This isn't really related to the CR as I know this is existing code which has just been moved around and sorry if I'm missing something obvious.

This code is called from *userspace*, and tries to cope with a VM being restored. So we have 3 (not 2 cases):

- both the source and the destination have the same level of CSVx support, and all is great in the world

- the source has CSVx==0, while the destination has CSVx=1. All good, as we won't be lying to the guest, and the extra mitigation executed by the guest isn't a functional problem. The guest will still see CSVx=0 after migration.

Quoted 4 lines

- the source has CSVx=1, while the destination has CSVx=0. This isn't an acceptable situation, and we return an error

Is that clearer?

Yes thanks, that all makes sense.

My initial question was why we needed to enforce the limit essentially twice in set_id_aa64pfr0_el1().

Once with:
/*
* Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
* it doesn't promise more than what is actually provided (the
* guest could otherwise be covered in ectoplasmic residue). */
csv2 = cpuid_feature_extract_unsigned_field(val,

ID_AA64PFR0_EL1_CSV2_SHIFT);
        if (csv2 > 1 ||
            (csv2 && arm64_get_spectre_v2_state() !=
SPECTRE_UNAFFECTED))
                return -EINVAL;

        /* Same thing for CSV3 */
        csv3 = cpuid_feature_extract_unsigned_field(val,
ID_AA64PFR0_EL1_CSV3_SHIFT);
        if (csv3 > 1 ||
            (csv3 && arm64_get_meltdown_state() != SPECTRE_UNAFFECTED))
                return -EINVAL;

And then again with the check in arm64_check_features().

Thanks,
Suraj

Quoted 5 lines

        M.

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

maintainer
#19 lore raw

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

Quoted 30 lines · Suraj
Quoted 28 lines · Marc Zyngier

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

Quoted 19 lines · Suraj

Reading init_cpu_ftr_reg() is hurting my head :p but don't we have basically 2 cases here?

1. We are unaffected by spectre|meltdown i.e. arm64_get_[spectre|meltdown]_v2_state() will return SPECTRE_UNAFFECTED
and we will set the limit to 1 for the csv[1|2] bit fields in read_sanitised_id_aa64pfr0_el1()

or

2. We ARE affected by spectre|meltdown in which case arm64_get_[spectre|meltdown]_v2_state() will be SPECTRE_VULNERABLE|SPECTRE_MITIGATED in which case read_sanitised_ftr_reg() will return a value with csv[1|2] set to 0 essentially setting the limit for either of these bit fields to 0 in
read_sanitised_id_aa64pfr0_el1().

Quoted 3 lines
Quoted 1 line

What is "WE"?

The CPU

Hilarious.

Quoted 55 lines
Quoted 25 lines
Quoted 10 lines

Are we trying to catch the case where csv[1|2] is 0 on the host but we
are unaffected as detected by e.g. cpuid and that cpu happens to incorrectly not set csv[1|2] in the ID register but we still want to
allow the guest to see those bits as set?

Quoted 3 lines

This isn't really related to the CR as I know this is existing code which has just been moved around and sorry if I'm missing something obvious.

This code is called from *userspace*, and tries to cope with a VM being restored. So we have 3 (not 2 cases):

- both the source and the destination have the same level of CSVx support, and all is great in the world

- the source has CSVx==0, while the destination has CSVx=1. All good, as we won't be lying to the guest, and the extra mitigation executed by the guest isn't a functional problem. The guest will still see CSVx=0 after migration.

Quoted 4 lines

- the source has CSVx=1, while the destination has CSVx=0. This isn't an acceptable situation, and we return an error

Is that clearer?

Yes thanks, that all makes sense.

My initial question was why we needed to enforce the limit essentially twice in set_id_aa64pfr0_el1().

Once with:
/*
* Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
* it doesn't promise more than what is actually provided (the
* guest could otherwise be covered in ectoplasmic residue). */
csv2 = cpuid_feature_extract_unsigned_field(val,

ID_AA64PFR0_EL1_CSV2_SHIFT);
        if (csv2 > 1 ||
            (csv2 && arm64_get_spectre_v2_state() !=
SPECTRE_UNAFFECTED))
                return -EINVAL;

        /* Same thing for CSV3 */
        csv3 = cpuid_feature_extract_unsigned_field(val,
ID_AA64PFR0_EL1_CSV3_SHIFT);
        if (csv3 > 1 ||
            (csv3 && arm64_get_meltdown_state() != SPECTRE_UNAFFECTED))
                return -EINVAL;
Quoted 1 line

And then again with the check in arm64_check_features().

Ah, I see what you mean. Yes, this isn't right. I asked for the generic code to be used in the past, but the old stuff was left in. Which is obviously not great.

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

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

In reply to Jing Zhang
Quoted 5 lines · Jing Zhang

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 array.

Hi,

Quoted 8 lines

---
 arch/arm64/include/asm/cpufeature.h |   1 + arch/arm64/kernel/cpufeature.c      |   2 +- arch/arm64/kvm/id_regs.c            | 361 ++++++++++++++++++-------- --

 3 files changed, 242 insertions(+), 122 deletions(-)

[ snip ]

Quoted 31 lines
arch/arm64/kvm/id_regs.c +0 -0
index 64d40aa395be..6cd56c9e6428 100644
@@ -18,6 +18,86 @@


 #include "sys_regs.h"

[ snip ]

+static u64 read_sanitised_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, +                                         const struct sys_reg_desc *rd)

arch/arm64/kvm/id_regs.c +7 -0
+{
+       u64 val;
+       u32 id = reg_to_encoding(rd);
+
+       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 |=

Quoted 8 lines

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); +       }
+

I think the virtual GIC check also needs to be moved here from kvm_arm_read_id_reg()

        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);
        }

Since the host supports GICv4.1 ID_AA64PFR0_EL1_GIC == 3 when qemu tries to read this register then overwrite it with the same value it read previously an error occurs. ID_AA64PFR0_EL1_GIC == 3 is the "limit" value however this field will read as ID_AA64PFR0_EL1_GIC == 1 when a virtual GICv3 is in use. Thus when qemu tries to set ID_AA64PFR0_EL1_GIC == 1, arm64_check_features() fails as those bits aren't set in id_reg.val meaning that modifications aren't allowed.

Additionally this means it's possible to set ID_AA64PFR0_EL1_GIC == 3 from userspace however any reads will see this field as ID_AA64PFR0_EL1_GIC == 1.

This all means the smp kvm_unit_tests failed. With the above change they pass.

Thanks,
Suraj

Quoted 8 lines

+       val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);

arch/arm64/kvm/id_regs.c +4 -0
+
+       return val;
+}
+

 static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 val)

[ snip ]

#21 lore raw
In reply to Jing Zhang
Quoted 14 lines

-----Original Message-----
From: Jing Zhang [mailto:[email protected]]
Sent: 03 May 2023 18:16
To: KVM <[email protected]>; KVMARM <[email protected]>; ARMLinux <[email protected]>; Marc Zyngier <[email protected]>; Oliver Upton <[email protected]>
Cc: Will Deacon <[email protected]>; Paolo Bonzini <[email protected]>; James Morse <[email protected]>; Alexandru Elisei <[email protected]>; Suzuki K Poulose <[email protected]>; Fuad Tabba <[email protected]>; Reiji Watanabe <[email protected]>; Raghavendra Rao Ananta <[email protected]>; Jing Zhang <[email protected]>
Subject: [PATCH v8 0/6] Support writable CPU ID registers from userspace

Quoted 4 lines

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]

Hi Jing/Reiji,

Just to check the status on the above mentioned series "KVM: arm64: Make CPU ID registers writable by userspace". Is there any plan to respin that one soon? (Sorry, not sure there is any other series in progress for that support currently)

Please let me know.

Thanks,
Shameer

Quoted 123 lines

.
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.

This patchset uses kvm->arch.config_lock from Oliver's lock inversion fixes at
https://lore.kernel.org/linux-arm-kernel/20230327164747.2466958-1-oliver.
[email protected]/

---

* v7 -> v8
- Move idregs table sanity check to kvm_sys_reg_table_init.
- Only allow userspace writing before VM running.
- No lock is hold for guest access to idregs.
- Addressed some other comments from Reiji and Oliver.

* v6 -> v7
- Rebased to v6.3-rc7.
- Add helpers for idregs read/write.
- Guard all idregs reads/writes.
- Add code to fix features' safe value type which is different for KVM than for the host.

* v5 -> v6
- Rebased to v6.3-rc5.
- Reuse struct sys_reg_desc's reset() callback and field val for KVM. sanitisation function and writable mask instead of creating a new data structure for idregs.
- Use get_arm64_ftr_reg() instead of exposing idregs ftr_bits array.

* v4 -> v5
- Rebased to 2fad20ae05cb (kvmarm/next)
Merge branch kvm-arm64/selftest/misc-6,4 into kvmarm-master/next
- Use kvm->arch.config_lock to guard update to multiple VM scope idregs to avoid lock inversion
- Add back IDREG() macro for idregs access
- Refactor struct id_reg_desc by using existing infrastructure.
- Addressed many other comments from Marc.

* 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/20230201025048.205820-1-jingzhangos@google.
com
[2]
https://lore.kernel.org/all/20230212215830.2975485-1-jingzhangos@googl
e.com
[3]
https://lore.kernel.org/all/20230228062246.1222387-1-jingzhangos@googl
e.com
[4]
https://lore.kernel.org/all/20230317050637.766317-1-jingzhangos@google.
com
[5]
https://lore.kernel.org/all/20230402183735.3011540-1-jingzhangos@googl
e.com
[6]
https://lore.kernel.org/all/20230404035344.4043856-1-jingzhangos@googl
e.com
[7]
https://lore.kernel.org/all/20230424234704.2571444-1-jingzhangos@googl
e.com

---

Jing Zhang (6):
KVM: arm64: Move CPU ID feature registers emulation into a separate
file
KVM: arm64: Save ID registers' sanitized value per guest
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: Reuse fields of sys_reg_desc for idreg
KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

 arch/arm64/include/asm/cpufeature.h |   1 +
 arch/arm64/include/asm/kvm_host.h   |  33 +-
 arch/arm64/kernel/cpufeature.c      |   2 +-
 arch/arm64/kvm/Makefile             |   2 +-
 arch/arm64/kvm/arm.c                |  24 +-
 arch/arm64/kvm/id_regs.c            | 717
++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c           | 534 ++++-----------------
 arch/arm64/kvm/sys_regs.h           |  28 +-
 include/kvm/arm_pmu.h               |   5 +-
 9 files changed, 864 insertions(+), 482 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c

base-commit: 6a8f57ae2eb07ab39a6f0ccad60c760743051026 --
2.40.1.495.gc816e09b53d-goog

maintainer
#22 lore raw
Quoted 20 lines · Shameerali Kolothum Thodi
Quoted 15 lines

-----Original Message-----
From: Jing Zhang [mailto:[email protected]]
Sent: 03 May 2023 18:16
To: KVM <[email protected]>; KVMARM <[email protected]>; ARMLinux <[email protected]>; Marc Zyngier <[email protected]>; Oliver Upton <[email protected]>
Cc: Will Deacon <[email protected]>; Paolo Bonzini <[email protected]>; James Morse <[email protected]>; Alexandru Elisei <[email protected]>; Suzuki K Poulose <[email protected]>; Fuad Tabba <[email protected]>; Reiji Watanabe <[email protected]>; Raghavendra Rao Ananta <[email protected]>; Jing Zhang <[email protected]>
Subject: [PATCH v8 0/6] Support writable CPU ID registers from userspace

Quoted 4 lines

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]

Quoted 5 lines

Hi Jing/Reiji,

Just to check the status on the above mentioned series "KVM: arm64: Make CPU ID registers writable by userspace". Is there any plan to respin that one soon? (Sorry, not sure there is any other series in progress for that support currently)

I think this still is the latest, which I'm about to review again. I'd appreciate if you could have a look to!

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#23 lore raw
Quoted 47 lines

-----Original Message-----
From: Marc Zyngier [mailto:[email protected]]
Sent: 16 May 2023 12:01
To: Shameerali Kolothum Thodi <[email protected]>
Cc: Jing Zhang <[email protected]>; KVM <[email protected]>; KVMARM <[email protected]>; ARMLinux <[email protected]>; Oliver Upton <[email protected]>; Will Deacon <[email protected]>; Paolo Bonzini <[email protected]>; James Morse <[email protected]>; Alexandru Elisei <[email protected]>; Suzuki K Poulose <[email protected]>; Fuad Tabba <[email protected]>; Reiji Watanabe <[email protected]>; Raghavendra Rao Ananta <[email protected]>
Subject: Re: [PATCH v8 0/6] Support writable CPU ID registers from userspace

Quoted 8 lines · Shameerali Kolothum Thodi
Quoted 8 lines

-----Original Message-----
From: Jing Zhang [mailto:[email protected]]
Sent: 03 May 2023 18:16
To: KVM <[email protected]>; KVMARM <[email protected]>; ARMLinux <[email protected]>; Marc Zyngier <[email protected]>; Oliver Upton <[email protected]>
Cc: Will Deacon <[email protected]>; Paolo Bonzini

<[email protected]>;

Quoted 2 lines
Quoted 2 lines

James Morse <[email protected]>; Alexandru Elisei <[email protected]>; Suzuki K Poulose

<[email protected]>;

Quoted 4 lines
Quoted 4 lines

Fuad Tabba <[email protected]>; Reiji Watanabe <[email protected]>; Raghavendra Rao Ananta <[email protected]>; Jing Zhang <[email protected]>
Subject: [PATCH v8 0/6] Support writable CPU ID registers from

userspace

Quoted 5 lines
Quoted 5 lines

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]

Quoted 4 lines

Hi Jing/Reiji,

Just to check the status on the above mentioned series "KVM: arm64: Make

Quoted 8 lines

CPU

Quoted 1 line

ID registers writable by userspace". Is there any plan to respin that one

soon?

Quoted 1 line

(Sorry, not sure there is any other series in progress for that support

currently)

I think this still is the latest, which I'm about to review again. I'd appreciate if you could have a look to!

Thanks Marc for confirming. Will go through. We do have some requirement to add support for Qemu CPU models/migration between different hosts.

Thanks,
Shameer

Quoted 7 lines

Thanks,

	M.

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

#24 lore raw
Quoted 48 lines · Shameerali Kolothum Thodi
Quoted 48 lines

-----Original Message-----
From: Marc Zyngier [mailto:[email protected]]
Sent: 16 May 2023 12:01
To: Shameerali Kolothum Thodi <[email protected]>
Cc: Jing Zhang <[email protected]>; KVM <[email protected]>; KVMARM <[email protected]>; ARMLinux <[email protected]>; Oliver Upton <[email protected]>; Will Deacon <[email protected]>; Paolo Bonzini <[email protected]>; James Morse <[email protected]>; Alexandru Elisei <[email protected]>; Suzuki K Poulose <[email protected]>; Fuad Tabba <[email protected]>; Reiji Watanabe <[email protected]>; Raghavendra Rao Ananta <[email protected]>
Subject: Re: [PATCH v8 0/6] Support writable CPU ID registers from userspace

Quoted 8 lines · Shameerali Kolothum Thodi
Quoted 8 lines

-----Original Message-----
From: Jing Zhang [mailto:[email protected]]
Sent: 03 May 2023 18:16
To: KVM <[email protected]>; KVMARM <[email protected]>; ARMLinux <[email protected]>; Marc Zyngier <[email protected]>; Oliver Upton <[email protected]>
Cc: Will Deacon <[email protected]>; Paolo Bonzini

<[email protected]>;

Quoted 2 lines
Quoted 2 lines

James Morse <[email protected]>; Alexandru Elisei <[email protected]>; Suzuki K Poulose

<[email protected]>;

Quoted 4 lines
Quoted 4 lines

Fuad Tabba <[email protected]>; Reiji Watanabe <[email protected]>; Raghavendra Rao Ananta <[email protected]>; Jing Zhang <[email protected]>
Subject: [PATCH v8 0/6] Support writable CPU ID registers from

userspace

Quoted 5 lines
Quoted 5 lines

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]

Quoted 4 lines

Hi Jing/Reiji,

Just to check the status on the above mentioned series "KVM: arm64: Make

CPU

Quoted 1 line

ID registers writable by userspace". Is there any plan to respin that one

soon?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

(Sorry, not sure there is any other series in progress for that support

currently)

I think this still is the latest, which I'm about to review again. I'd appreciate if you could have a look to!

Thanks Marc for confirming. Will go through. We do have some requirement to add support for Qemu CPU models/migration between different hosts.

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

maintainer
#25 lore raw
Quoted 4 lines · Cornelia Huck

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

I'm not being asked, but I'll share my thoughts anyway! ;-)

I don't think CPU models are necessarily the most important thing. Specially when you look at the diversity of the ecosystem (and even the same CPU can be configured in different ways at integration time). Case in point, Neoverse N1 which can have its I/D caches made coherent or not. And the guest really wants to know which one it is (you can only lie in one direction).

But being able to control the feature set exposed to the guest from userspace is a huge benefit in terms of migration.

Now, this is only half of the problem (and we're back to the CPU model): most of these CPUs have various degrees of brokenness. Most of the workarounds have to be implemented by the guest, and are keyed on the MIDR values. So somehow, you need to be able to expose *all* the possible MIDR values that a guest can observe in its lifetime.

I have a vague prototype for that that I'd need to dust off and finish, because that's also needed for this very silly construct called big-little...

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#26 lore raw
Quoted 34 lines

-----Original Message-----
From: Marc Zyngier [mailto:[email protected]]
Sent: 16 May 2023 14:12
To: Cornelia Huck <[email protected]>
Cc: Shameerali Kolothum Thodi <[email protected]>; Jing Zhang <[email protected]>; KVM <[email protected]>; KVMARM <[email protected]>; ARMLinux <[email protected]>; Oliver Upton <[email protected]>; Will Deacon <[email protected]>; Paolo Bonzini <[email protected]>; James Morse <[email protected]>; Alexandru Elisei <[email protected]>; Suzuki K Poulose <[email protected]>; Fuad Tabba <[email protected]>; Reiji Watanabe <[email protected]>; Raghavendra Rao Ananta <[email protected]>
Subject: Re: [PATCH v8 0/6] Support writable CPU ID registers from userspace

Quoted 4 lines · Cornelia Huck

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

I'm not being asked, but I'll share my thoughts anyway! ;-)

I don't think CPU models are necessarily the most important thing. Specially when you look at the diversity of the ecosystem (and even the same CPU can be configured in different ways at integration time). Case in point, Neoverse N1 which can have its I/D caches made coherent or not. And the guest really wants to know which one it is (you can only lie in one direction).

Quoted 2 lines

But being able to control the feature set exposed to the guest from userspace is a huge benefit in terms of migration.

Yes, this is what we also need and was thinking of adding a named CPU with common min feature set exposed to Guest. There were some previous attempts to add the basic support in Qemu here,

https://lists.gnu.org/archive/html/qemu-devel/2020-11/msg00087.html

Quoted 5 lines

Now, this is only half of the problem (and we're back to the CPU model): most of these CPUs have various degrees of brokenness. Most of the workarounds have to be implemented by the guest, and are keyed on the MIDR values. So somehow, you need to be able to expose *all* the possible MIDR values that a guest can observe in its lifetime.

Ok. This will be a problem and I am not sure this has an impact on our platforms or not.

Thanks,
Shameer

Quoted 10 lines

I have a vague prototype for that that I'd need to dust off and finish, because that's also needed for this very silly construct called big-little...

Thanks,

	M.

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

#27 lore raw
Quoted 32 lines · Shameerali Kolothum Thodi
Quoted 32 lines

-----Original Message-----
From: Marc Zyngier [mailto:[email protected]]
Sent: 16 May 2023 14:12
To: Cornelia Huck <[email protected]>
Cc: Shameerali Kolothum Thodi <[email protected]>; Jing Zhang <[email protected]>; KVM <[email protected]>; KVMARM <[email protected]>; ARMLinux <[email protected]>; Oliver Upton <[email protected]>; Will Deacon <[email protected]>; Paolo Bonzini <[email protected]>; James Morse <[email protected]>; Alexandru Elisei <[email protected]>; Suzuki K Poulose <[email protected]>; Fuad Tabba <[email protected]>; Reiji Watanabe <[email protected]>; Raghavendra Rao Ananta <[email protected]>
Subject: Re: [PATCH v8 0/6] Support writable CPU ID registers from userspace

Quoted 4 lines · Cornelia Huck

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

I'm not being asked, but I'll share my thoughts anyway! ;-)

I don't think CPU models are necessarily the most important thing. Specially when you look at the diversity of the ecosystem (and even the same CPU can be configured in different ways at integration time). Case in point, Neoverse N1 which can have its I/D caches made coherent or not. And the guest really wants to know which one it is (you can only lie in one direction).

Quoted 8 lines
Quoted 2 lines

But being able to control the feature set exposed to the guest from userspace is a huge benefit in terms of migration.

Yes, this is what we also need and was thinking of adding a named CPU with common min feature set exposed to Guest. There were some previous attempts to add the basic support in Qemu here,

https://lists.gnu.org/archive/html/qemu-devel/2020-11/msg00087.html

Thanks for the link.

Quoted 9 lines
Quoted 6 lines

Now, this is only half of the problem (and we're back to the CPU model): most of these CPUs have various degrees of brokenness. Most of the workarounds have to be implemented by the guest, and are keyed on the MIDR values. So somehow, you need to be able to expose *all* the possible MIDR values that a guest can observe in its lifetime.

Ok. This will be a problem and I am not sure this has an impact on our platforms or not.

Oh, I see that the MIDR fun had already been mentioned in a reply to the first version of that patchset; this needs to be addressed for the general case, I guess...

#28 lore raw
In reply to Marc Zyngier
Quoted 16 lines · Marc Zyngier
Quoted 4 lines · Cornelia Huck

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

I'm not being asked, but I'll share my thoughts anyway! ;-)

I don't think CPU models are necessarily the most important thing. Specially when you look at the diversity of the ecosystem (and even the same CPU can be configured in different ways at integration time). Case in point, Neoverse N1 which can have its I/D caches made coherent or not. And the guest really wants to know which one it is (you can only lie in one direction).

Quoted 2 lines

But being able to control the feature set exposed to the guest from userspace is a huge benefit in terms of migration.

Certainly; the important part is that we can keep the guest ABI stable... which parts match to a "CPU model" in the way other architectures use it is an interesting question. It almost certainly will look different from e.g. s390, where we only have to deal with a single manufacturer.

I'm wondering whether we'll end up building frankenmonster CPUs.

Another interesting aspect is how KVM ends up influencing what the guest sees on the CPU level, as in the case where we migrate across matching CPUs, but with a different software level. I think we want userspace to control that to some extent, but I'm not sure if this fully matches the CPU model context.

Quoted 6 lines

Now, this is only half of the problem (and we're back to the CPU model): most of these CPUs have various degrees of brokenness. Most of the workarounds have to be implemented by the guest, and are keyed on the MIDR values. So somehow, you need to be able to expose *all* the possible MIDR values that a guest can observe in its lifetime.

Fun is to be had...

Quoted 4 lines

I have a vague prototype for that that I'd need to dust off and finish, because that's also needed for this very silly construct called big-little...

That would be cool to see. Or at least interesting ;)

maintainer
#29 lore raw
Quoted 22 lines · Cornelia Huck
Quoted 16 lines · Marc Zyngier
Quoted 4 lines · Cornelia Huck

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

I'm not being asked, but I'll share my thoughts anyway! ;-)

I don't think CPU models are necessarily the most important thing. Specially when you look at the diversity of the ecosystem (and even the same CPU can be configured in different ways at integration time). Case in point, Neoverse N1 which can have its I/D caches made coherent or not. And the guest really wants to know which one it is (you can only lie in one direction).

Quoted 2 lines

But being able to control the feature set exposed to the guest from userspace is a huge benefit in terms of migration.

Quoted 7 lines

Certainly; the important part is that we can keep the guest ABI stable... which parts match to a "CPU model" in the way other architectures use it is an interesting question. It almost certainly will look different from e.g. s390, where we only have to deal with a single manufacturer.

I'm wondering whether we'll end up building frankenmonster CPUs.

We already do. KVM hides a bunch of things we don't want the guest to see, either because we don't support the feature, or that we want to present it with a different shape (cache topology, for example), and these combination don't really exist in any physical implementation.

Which is why I don't really buy the "CPU model" concept as defined by x86 and s390. We already are in a vastly different place.

The way I see it, you get a bunch of architectural features that can be enabled/disabled depending on the underlying HW, hypervisor's capabilities and userspace input. On top of that, there is a layer of paint that tells you what is the overall implementation you could be running on (that's what MIDR+REVIDR+AIDR tell you) so that you can apply some unspeakable, uarch-specific hacks that keep the machine going (got to love these CPU errata).

Quoted 5 lines

Another interesting aspect is how KVM ends up influencing what the guest sees on the CPU level, as in the case where we migrate across matching CPUs, but with a different software level. I think we want userspace to control that to some extent, but I'm not sure if this fully matches the CPU model context.

I'm not sure I get the "different software level" part. Do you mean VMM revisions?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#30 lore raw
Quoted 32 lines · Marc Zyngier
Quoted 22 lines · Cornelia Huck
Quoted 16 lines · Marc Zyngier
Quoted 4 lines · Cornelia Huck

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

I'm not being asked, but I'll share my thoughts anyway! ;-)

I don't think CPU models are necessarily the most important thing. Specially when you look at the diversity of the ecosystem (and even the same CPU can be configured in different ways at integration time). Case in point, Neoverse N1 which can have its I/D caches made coherent or not. And the guest really wants to know which one it is (you can only lie in one direction).

Quoted 2 lines

But being able to control the feature set exposed to the guest from userspace is a huge benefit in terms of migration.

Quoted 7 lines

Certainly; the important part is that we can keep the guest ABI stable... which parts match to a "CPU model" in the way other architectures use it is an interesting question. It almost certainly will look different from e.g. s390, where we only have to deal with a single manufacturer.

I'm wondering whether we'll end up building frankenmonster CPUs.

Quoted 7 lines

We already do. KVM hides a bunch of things we don't want the guest to see, either because we don't support the feature, or that we want to present it with a different shape (cache topology, for example), and these combination don't really exist in any physical implementation.

Which is why I don't really buy the "CPU model" concept as defined by x86 and s390. We already are in a vastly different place.

Yes, I agree that the "named cpu models" approach probably won't work on Arm (especially if you add other accelerators into the mix -- cpu 'foo' with tcg is unlikely to be 100% identical to cpu 'foo' with KVM.) OTOH, "these two cpus are not that different from each other, so we support migration between them with a least common denominator feature/behaviour set" seems more reasonable.

Quoted 9 lines

The way I see it, you get a bunch of architectural features that can be enabled/disabled depending on the underlying HW, hypervisor's capabilities and userspace input. On top of that, there is a layer of paint that tells you what is the overall implementation you could be running on (that's what MIDR+REVIDR+AIDR tell you) so that you can apply some unspeakable, uarch-specific hacks that keep the machine going (got to love these CPU errata).

Quoted 8 lines
Quoted 5 lines

Another interesting aspect is how KVM ends up influencing what the guest sees on the CPU level, as in the case where we migrate across matching CPUs, but with a different software level. I think we want userspace to control that to some extent, but I'm not sure if this fully matches the CPU model context.

I'm not sure I get the "different software level" part. Do you mean VMM revisions?

Yes. Basically, two (for migration purposes) identical machines with different kernel/QEMU versions, but using the same QEMU compat machine. Migrate from old to new, get more regs: works. Migrate from new to old, get less regs: boom. Expectation would be for this to work, and handling it from machine compat code seems very awkward.

maintainer
#31 lore raw
Quoted 43 lines · Cornelia Huck
Quoted 32 lines · Marc Zyngier
Quoted 22 lines · Cornelia Huck
Quoted 16 lines · Marc Zyngier
Quoted 4 lines · Cornelia Huck

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

I'm not being asked, but I'll share my thoughts anyway! ;-)

I don't think CPU models are necessarily the most important thing. Specially when you look at the diversity of the ecosystem (and even the same CPU can be configured in different ways at integration time). Case in point, Neoverse N1 which can have its I/D caches made coherent or not. And the guest really wants to know which one it is (you can only lie in one direction).

Quoted 2 lines

But being able to control the feature set exposed to the guest from userspace is a huge benefit in terms of migration.

Quoted 7 lines

Certainly; the important part is that we can keep the guest ABI stable... which parts match to a "CPU model" in the way other architectures use it is an interesting question. It almost certainly will look different from e.g. s390, where we only have to deal with a single manufacturer.

I'm wondering whether we'll end up building frankenmonster CPUs.

Quoted 7 lines

We already do. KVM hides a bunch of things we don't want the guest to see, either because we don't support the feature, or that we want to present it with a different shape (cache topology, for example), and these combination don't really exist in any physical implementation.

Which is why I don't really buy the "CPU model" concept as defined by x86 and s390. We already are in a vastly different place.

Quoted 6 lines

Yes, I agree that the "named cpu models" approach probably won't work on Arm (especially if you add other accelerators into the mix -- cpu 'foo' with tcg is unlikely to be 100% identical to cpu 'foo' with KVM.) OTOH, "these two cpus are not that different from each other, so we support migration between them with a least common denominator feature/behaviour set" seems more reasonable.

It does, assuming the platform is "compatible". I realise I didn't mention the small issue of the counter frequency, which cannot be scaled. ARMv8.6 tried to specify a 1GHz counter frequency, but I still haven't see a single system having adopted it, and everyone is doing something different.

We *could* always trap/emulate the counters/timers, but that quickly becomes ridiculously bad. Isn't ARM virtualisation fun?

Quoted 14 lines
Quoted 14 lines

The way I see it, you get a bunch of architectural features that can be enabled/disabled depending on the underlying HW, hypervisor's capabilities and userspace input. On top of that, there is a layer of paint that tells you what is the overall implementation you could be running on (that's what MIDR+REVIDR+AIDR tell you) so that you can apply some unspeakable, uarch-specific hacks that keep the machine going (got to love these CPU errata).

Quoted 5 lines

Another interesting aspect is how KVM ends up influencing what the guest sees on the CPU level, as in the case where we migrate across matching CPUs, but with a different software level. I think we want userspace to control that to some extent, but I'm not sure if this fully matches the CPU model context.

Quoted 8 lines
Quoted 2 lines

I'm not sure I get the "different software level" part. Do you mean VMM revisions?

Yes. Basically, two (for migration purposes) identical machines with different kernel/QEMU versions, but using the same QEMU compat machine. Migrate from old to new, get more regs: works. Migrate from new to old, get less regs: boom. Expectation would be for this to work, and handling it from machine compat code seems very awkward.

Old to new, fine. I'm not sure how we make the other way work. Actually, scratch that. I'm pretty sure we can't make it work. You'd have constraint your guest at creation time so that it doesn't use any feature that isn't available to older VMM/kernel revisions.

I sense some interesting discussions next month! :D

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#32 lore raw
In reply to Marc Zyngier
Quoted 25 lines · Marc Zyngier
Quoted 4 lines · Cornelia Huck

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

I'm not being asked, but I'll share my thoughts anyway! ;-)

I don't think CPU models are necessarily the most important thing. Specially when you look at the diversity of the ecosystem (and even the same CPU can be configured in different ways at integration time). Case in point, Neoverse N1 which can have its I/D caches made coherent or not. And the guest really wants to know which one it is (you can only lie in one direction).

But being able to control the feature set exposed to the guest from userspace is a huge benefit in terms of migration.

Now, this is only half of the problem (and we're back to the CPU model): most of these CPUs have various degrees of brokenness. Most of the workarounds have to be implemented by the guest, and are keyed on the MIDR values. So somehow, you need to be able to expose *all* the possible MIDR values that a guest can observe in its lifetime.

Quoted 3 lines

I have a vague prototype for that that I'd need to dust off and finish, because that's also needed for this very silly construct called big-little...

And the third half of the problem is all of the other IP bits that get strung together into an SOC :) Errata that live beyond the CPU can become guest-visible (interconnect for example) and that becomes a bit difficult to express to the guest OS. So, beyond something like a big-little VM where the rest of the IP should be shared, I'm a bit fearful of migrating a VM cross-system.

But hey, userspace is in the drivers seat and it can do as it pleases.

Hopefully we wouldn't need a KVM-specific PV interface for MIDR enumeration. Perhaps the errata management spec could be expanded to describe a set of CPU implementations and associated errata...

Signature · 2 lines
Thanks,
Oliver
maintainer
#33 lore raw
Quoted 31 lines · Oliver Upton
Quoted 25 lines · Marc Zyngier
Quoted 4 lines · Cornelia Huck

Do you have more concrete ideas for QEMU CPU models already? Asking because I wanted to talk about this at KVM Forum, so collecting what others would like to do seems like a good idea :)

I'm not being asked, but I'll share my thoughts anyway! ;-)

I don't think CPU models are necessarily the most important thing. Specially when you look at the diversity of the ecosystem (and even the same CPU can be configured in different ways at integration time). Case in point, Neoverse N1 which can have its I/D caches made coherent or not. And the guest really wants to know which one it is (you can only lie in one direction).

But being able to control the feature set exposed to the guest from userspace is a huge benefit in terms of migration.

Now, this is only half of the problem (and we're back to the CPU model): most of these CPUs have various degrees of brokenness. Most of the workarounds have to be implemented by the guest, and are keyed on the MIDR values. So somehow, you need to be able to expose *all* the possible MIDR values that a guest can observe in its lifetime.

Quoted 3 lines

I have a vague prototype for that that I'd need to dust off and finish, because that's also needed for this very silly construct called big-little...

Quoted 6 lines

And the third half of the problem is all of the other IP bits that get strung together into an SOC :) Errata that live beyond the CPU can become guest-visible (interconnect for example) and that becomes a bit difficult to express to the guest OS. So, beyond something like a big-little VM where the rest of the IP should be shared, I'm a bit fearful of migrating a VM cross-system.

Indeed. But there isn't much we can do about that, and it should be clear to anyone who's remotely involved in this crap that migration to different systems is risky business.

Quoted 1 line

But hey, userspace is in the drivers seat and it can do as it pleases.

Exactly. We just need to give it enough of the proverbial rope...

Quoted 3 lines

Hopefully we wouldn't need a KVM-specific PV interface for MIDR enumeration. Perhaps the errata management spec could be expanded to describe a set of CPU implementations and associated errata...

Hence finally making it clear the big-little is a large scale, industry wide erratum? Sign me up! :D

More seriously, I'd expect this to be an ARM spec. But it wouldn't hurt having a prototype that serves as a draft for the spec. Better doing that than leaving it to... someone else.

Thanks,

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

On Tue, May 16, 2023 at 05:44:53PM +0100, Marc Zyngier wrote:

[...]

Quoted 3 lines

More seriously, I'd expect this to be an ARM spec. But it wouldn't hurt having a prototype that serves as a draft for the spec. Better doing that than leaving it to... someone else.

Completely agree. My suggestion was not meant to discourage prototyping, just wanted to make sure we have line of sight on making this someone else's problem to standardize the interface :)

Signature · 2 lines
Thanks,
Oliver