Skip to contentSkip to conversation

Enable writable for idregs DFR0,PFR0, MMFR{0,1,2,3}

PATCH In review Jing Zhang 4 participants 20 messages Aug 21, 2023 – Sep 6, 2023

Subsystems (5)
Documentation KVM Kselftest arm64 +1 more subsystems
lore
Share & tools
lore

Jing Zhang

author
#0 lore raw

v9 cover Enable writable for idregs DFR0,PFR0, MMFR{0,1,2,3}

This patch series enable userspace writable for below idregs:
ID_AA64DFR0_EL1, ID_DFR0_EL1, ID_AA64PFR0_EL1, ID_AA64MMFR{0, 1, 2, 3}_EL1. A vm ioctl is added to get feature ID register writable masks from userspace. A selftest is added to verify that KVM handles the writings from user space correctly.
A relevant patch from Oliver is picked from [3].

---

* v8 -> v9
- Rebase on v6.5-rc7.
- Don't allow writable on RES0 fields and those fields not exposed by KVM.
- Fixed build dependency issue for system register definition generation in selftest.

* v7 -> v8
- Rebase on v6.5-rc5.
- Add reserved parameters to UAPI to get writable masks for future expansion.
- Import system regiesters definition automatic generation from kernel to selftests.
- Refactor the selftest for verifying ID register writable from userspace to cover all writable ID register fields.

* v6 -> v7
- Rebase on v6.5-rc4.
- Add a vm ioctl to get feature ID register writable masks from userspace.
- Split the change for debug version in ID_{AA64}DFR0_EL1.
- Addressed some bugs in selftest.

* v5 -> v6
- Override the type of field AA64DFR0_EL1_DebugVer to be FTR_LOWER_SAFE by the discussion of Oliver and Suraj.

* v4 -> v5
- Rebase on v6.5-rc1 which contains infrastructure for writable idregs.
- Use guest ID registers values for the sake of emulation.
- Added a selftest to verify idreg userspace writing.

* v3 -> v4
- Rebase on v11 of writable idregs series at [2].

* v2 -> v3
- Rebase on v6 of writable idregs series.
- Enable writable for ID_AA64PFR0_EL1 and ID_AA64MMFR{0, 1, 2}_EL1.

* v1 -> v2
- Rebase on latest patch series [1] of enabling writable ID register.

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

[v1] https://lore.kernel.org/all/[email protected]
[v2] https://lore.kernel.org/all/[email protected]
[v3] https://lore.kernel.org/all/[email protected]
[v4] https://lore.kernel.org/all/[email protected]
[v5] https://lore.kernel.org/all/[email protected]
[v6] https://lore.kernel.org/all/[email protected]
[v7] https://lore.kernel.org/all/[email protected]
[v8] https://lore.kernel.org/all/[email protected]

---

Jing Zhang (9):
KVM: arm64: Allow userspace to get the writable masks for feature ID
registers
KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS
KVM: arm64: Use guest ID register values for the sake of emulation
KVM: arm64: Enable writable for ID_AA64DFR0_EL1 and ID_DFR0_EL1
KVM: arm64: Enable writable for ID_AA64PFR0_EL1
KVM: arm64: Refactor helper Macros for idreg desc
KVM: arm64: Enable writable for ID_AA64MMFR{0, 1, 2, 3}_EL1
KVM: arm64: selftests: Import automatic system register definition
generation from kernel
KVM: arm64: selftests: Test for setting ID register from usersapce

Oliver Upton (2):
KVM: arm64: Reject attempts to set invalid debug arch version
KVM: arm64: Bump up the default KVM sanitised debug version to v8p8

 Documentation/virt/kvm/api.rst                |   29 +
 arch/arm64/include/asm/kvm_host.h             |    2 +
 arch/arm64/include/uapi/asm/kvm.h             |   26 +
 arch/arm64/kvm/arm.c                          |    7 +
 arch/arm64/kvm/sys_regs.c                     |  207 +-
 include/uapi/linux/kvm.h                      |    1 +
 tools/arch/arm64/include/.gitignore           |    1 +
 tools/arch/arm64/include/asm/gpr-num.h        |   26 +
 tools/arch/arm64/include/asm/sysreg.h         |  839 ++----
 tools/arch/arm64/tools/gen-sysreg.awk         |  336 +++
 tools/arch/arm64/tools/sysreg                 | 2497 +++++++++++++++++
 tools/testing/selftests/kvm/Makefile          |   15 +-
 .../selftests/kvm/aarch64/aarch32_id_regs.c   |    4 +-
 .../selftests/kvm/aarch64/debug-exceptions.c  |   12 +-
 .../selftests/kvm/aarch64/page_fault_test.c   |    6 +-
 .../selftests/kvm/aarch64/set_id_regs.c       |  448 +++
 .../selftests/kvm/lib/aarch64/processor.c     |    6 +-
 17 files changed, 3739 insertions(+), 723 deletions(-)
 create mode 100644 tools/arch/arm64/include/.gitignore
 create mode 100644 tools/arch/arm64/include/asm/gpr-num.h
 create mode 100755 tools/arch/arm64/tools/gen-sysreg.awk
 create mode 100644 tools/arch/arm64/tools/sysreg
 create mode 100644 tools/testing/selftests/kvm/aarch64/set_id_regs.c

base-commit: 706a741595047797872e669b3101429ab8d378ef

Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
author
#1 lore raw

PATCH v9 1/11 KVM: arm64: Allow userspace to get the writable masks for feature ID registers

Add a VM ioctl to allow userspace to get writable masks for feature ID registers in below system register space:
op0 = 3, op1 = {0, 1, 3}, CRn = 0, CRm = {0 - 7}, op2 = {0 - 7} This is used to support mix-and-match userspace and kernels for writable ID registers, where userspace may want to know upfront whether it can actually tweak the contents of an idreg or not.

---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/include/uapi/asm/kvm.h | 26 ++++++++++++++
 arch/arm64/kvm/arm.c              |  7 ++++
 arch/arm64/kvm/sys_regs.c         | 57 +++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h          |  1 +
 5 files changed, 93 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
index d3dd05bbfe23..a328d362df5a 100644
@@ -1074,6 +1074,8 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 			       struct kvm_arm_copy_mte_tags *copy_tags);
 int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm,
 				    struct kvm_arm_counter_offset *offset);
+int kvm_vm_ioctl_get_reg_writable_masks(struct kvm *kvm,
+					struct reg_mask_range *range);
 
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
arch/arm64/include/uapi/asm/kvm.h +26 -0
index f7ddd73a8c0f..7a21bbb8a0f7 100644
@@ -505,6 +505,32 @@ struct kvm_smccc_filter {
 #define KVM_HYPERCALL_EXIT_SMC		(1U << 0)
 #define KVM_HYPERCALL_EXIT_16BIT	(1U << 1)
 
+/* Get feature ID registers userspace writable mask. */
+/*
+ * From DDI0487J.a, D19.2.66 ("ID_AA64MMFR2_EL1, AArch64 Memory Model
+ * Feature Register 2"):
+ *
+ * "The Feature ID space is defined as the System register space in
+ * AArch64 with op0==3, op1=={0, 1, 3}, CRn==0, CRm=={0-7},
+ * op2=={0-7}."
+ *
+ * This covers all R/O registers that indicate anything useful feature
+ * wise, including the ID registers.
+ */
+#define ARM64_FEATURE_ID_SPACE_IDX(op0, op1, crn, crm, op2)		\
+	({								\
+		__u64 __op1 = (op1) & 3;				\
+		__op1 -= (__op1 == 3);					\
+		(__op1 << 6 | ((crm) & 7) << 3 | (op2));		\
+	})
+
+#define ARM64_FEATURE_ID_SPACE_SIZE	(3 * 8 * 8)
+
+struct reg_mask_range {
+	__u64 addr;		/* Pointer to mask array */
+	__u64 reserved[7];
+};
+
 #endif
 
 #endif /* __ARM_KVM_H__ */
arch/arm64/kvm/arm.c +7 -0
index d1cb298a58a0..cfb5e9ef43c4 100644
@@ -1630,6 +1630,13 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 
 		return kvm_vm_set_attr(kvm, &attr);
 	}
+	case KVM_ARM_GET_REG_WRITABLE_MASKS: {
+		struct reg_mask_range range;
+
+		if (copy_from_user(&range, argp, sizeof(range)))
+			return -EFAULT;
+		return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range);
+	}
 	default:
 		return -EINVAL;
 	}
arch/arm64/kvm/sys_regs.c +57 -0

63 lines not on this page · raw message

include/uapi/linux/kvm.h +1 -0
index f089ab290978..424b6d00440b 100644
@@ -1555,6 +1555,7 @@ struct kvm_s390_ucas_mapping {
 #define KVM_ARM_MTE_COPY_TAGS	  _IOR(KVMIO,  0xb4, struct kvm_arm_copy_mte_tags)
 /* Available with KVM_CAP_COUNTER_OFFSET */
 #define KVM_ARM_SET_COUNTER_OFFSET _IOW(KVMIO,  0xb5, struct kvm_arm_counter_offset)
+#define KVM_ARM_GET_REG_WRITABLE_MASKS _IOR(KVMIO,  0xb6, struct reg_mask_range)
 
 /* ioctl for vm fd */
 #define KVM_CREATE_DEVICE	  _IOWR(KVMIO,  0xe0, struct kvm_create_device)
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
author
#2 lore raw

PATCH v9 2/11 KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

In reply to Jing Zhang

Add some basic documentation on how to get feature ID register writable masks from userspace.

---
 Documentation/virt/kvm/api.rst | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
Documentation/virt/kvm/api.rst +29 -0
index c0ddd3035462..92a9b20f970e 100644
@@ -6068,6 +6068,35 @@ writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG
 interface. No error will be returned, but the resulting offset will not be
 applied.
 
+4.139 KVM_ARM_GET_REG_WRITABLE_MASKS
+-------------------------------------------
+
+:Capability: none
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct reg_mask_range (in/out)
+:Returns: 0 on success, < 0 on error
+
+
+::
+
+        #define ARM64_FEATURE_ID_SPACE_SIZE	(3 * 8 * 8)
+
+        struct reg_mask_range {
+                __u64 addr;             /* Pointer to mask array */
+                __u64 reserved[7];
+        };
+
+This ioctl would copy the writable masks for feature ID registers to userspace.
+The Feature ID space is defined as the System register space in AArch64 with
+op0==3, op1=={0, 1, 3}, CRn==0, CRm=={0-7}, op2=={0-7}.
+To get the index in the mask array pointed by ``addr`` for a specified feature
+ID register, use the macro ``ARM64_FEATURE_ID_SPACE_IDX(op0, op1, crn, crm, op2)``.
+This allows the userspace to know upfront whether it can actually tweak the
+contents of a feature ID register or not.
+The ``reserved[7]`` is reserved for future use to add other register space. For
+feature ID registers, it should be 0, otherwise, KVM may return error.
+
 5. The kvm_run structure
 ========================
 
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
author
#3 lore raw

PATCH v9 3/11 KVM: arm64: Use guest ID register values for the sake of emulation

In reply to Jing Zhang

Since KVM now supports per-VM ID registers, use per-VM ID register values for the sake of emulation for DBGDIDR and LORegion.

---
 arch/arm64/kvm/sys_regs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kvm/sys_regs.c +3 -3
index 216905840c92..42c4d71f40f3 100644
@@ -379,7 +379,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 = IDREG(vcpu->kvm, SYS_ID_AA64MMFR1_EL1);
 	u32 sr = reg_to_encoding(r);
 
 	if (!(val & (0xfUL << ID_AA64MMFR1_EL1_LO_SHIFT))) {
@@ -2429,8 +2429,8 @@ static bool trap_dbgdidr(struct kvm_vcpu *vcpu,
 	if (p->is_write) {
 		return ignore_write(vcpu, p);
 	} else {
-		u64 dfr = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
-		u64 pfr = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
+		u64 dfr = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1);
+		u64 pfr = IDREG(vcpu->kvm, SYS_ID_AA64PFR0_EL1);
 		u32 el3 = !!cpuid_feature_extract_unsigned_field(pfr, ID_AA64PFR0_EL1_EL3_SHIFT);
 
 		p->regval = ((((dfr >> ID_AA64DFR0_EL1_WRPs_SHIFT) & 0xf) << 28) |
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
author
#4 lore raw

PATCH v9 4/11 KVM: arm64: Reject attempts to set invalid debug arch version

In reply to Jing Zhang

From: Oliver Upton <[email protected]>

The debug architecture is mandatory in ARMv8, so KVM should not allow userspace to configure a vCPU with less than that. Of course, this isn't handled elegantly by the generic ID register plumbing, as the respective ID register fields have a nonzero starting value.

Add an explicit check for debug versions less than v8 of the architecture.

---
 arch/arm64/kvm/sys_regs.c | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)
arch/arm64/kvm/sys_regs.c +29 -3
index 42c4d71f40f3..afade7186675 100644
@@ -1216,8 +1216,14 @@ static s64 kvm_arm64_ftr_safe_value(u32 id, const struct arm64_ftr_bits *ftrp,
 	/* Some features have different safe value type in KVM than host features */
 	switch (id) {
 	case SYS_ID_AA64DFR0_EL1:
-		if (kvm_ftr.shift == ID_AA64DFR0_EL1_PMUVer_SHIFT)
+		switch (kvm_ftr.shift) {
+		case ID_AA64DFR0_EL1_PMUVer_SHIFT:
 			kvm_ftr.type = FTR_LOWER_SAFE;
+			break;
+		case ID_AA64DFR0_EL1_DebugVer_SHIFT:
+			kvm_ftr.type = FTR_LOWER_SAFE;
+			break;
+		}
 		break;
 	case SYS_ID_DFR0_EL1:
 		if (kvm_ftr.shift == ID_DFR0_EL1_PerfMon_SHIFT)
@@ -1469,14 +1475,22 @@ static u64 read_sanitised_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 	return val;
 }
 
+#define ID_REG_LIMIT_FIELD_ENUM(val, reg, field, limit)			       \
+({									       \
+	u64 __f_val = FIELD_GET(reg##_##field##_MASK, val);		       \
+	(val) &= ~reg##_##field##_MASK;					       \
+	(val) |= FIELD_PREP(reg##_##field##_MASK,			       \
+			min(__f_val, (u64)reg##_##field##_##limit));	       \
+	(val);								       \
+})
+
 static u64 read_sanitised_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 					  const struct sys_reg_desc *rd)
 {
 	u64 val = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
 
 	/* Limit debug to ARMv8.0 */
-	val &= ~ID_AA64DFR0_EL1_DebugVer_MASK;
-	val |= SYS_FIELD_PREP_ENUM(ID_AA64DFR0_EL1, DebugVer, IMP);
+	val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64DFR0_EL1, DebugVer, IMP);
 
 	/*
 	 * Only initialize the PMU version if the vCPU was configured with one.
@@ -1496,6 +1510,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 			       const struct sys_reg_desc *rd,
 			       u64 val)
 {
+	u8 debugver = SYS_FIELD_GET(ID_AA64DFR0_EL1, DebugVer, val);
 	u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, val);
 
 	/*
@@ -1515,6 +1530,13 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 	if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF)
 		val &= ~ID_AA64DFR0_EL1_PMUVer_MASK;
 
+	/*
+	 * ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a
+	 * nonzero minimum safe value.
+	 */
+	if (debugver < ID_AA64DFR0_EL1_DebugVer_IMP)
+		return -EINVAL;
+
 	return set_id_reg(vcpu, rd, val);
 }
 
@@ -1536,6 +1558,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 			   u64 val)
 {
 	u8 perfmon = SYS_FIELD_GET(ID_DFR0_EL1, PerfMon, val);
+	u8 copdbg = SYS_FIELD_GET(ID_DFR0_EL1, CopDbg, val);
 
 	if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
 		val &= ~ID_DFR0_EL1_PerfMon_MASK;
@@ -1551,6 +1574,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	if (perfmon != 0 && perfmon < ID_DFR0_EL1_PerfMon_PMUv3)
 		return -EINVAL;
 
+	if (copdbg < ID_DFR0_EL1_CopDbg_Armv8)
+		return -EINVAL;
+
 	return set_id_reg(vcpu, rd, val);
 }
 
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
author
#5 lore raw

PATCH v9 5/11 KVM: arm64: Enable writable for ID_AA64DFR0_EL1 and ID_DFR0_EL1

In reply to Jing Zhang

All valid fields in ID_AA64DFR0_EL1 and ID_DFR0_EL1 are writable from userspace with this change.
RES0 fields and those fields hidden by KVM are not writable.

---
 arch/arm64/kvm/sys_regs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +4 -2
index afade7186675..20fc38bad4e8 100644
@@ -1931,6 +1931,8 @@ static bool access_spsr(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#define ID_AA64DFR0_EL1_RES0_MASK (GENMASK(59, 56) | GENMASK(27, 24) | GENMASK(19, 16))
+
 /*
  * Architected system registers.
  * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
@@ -2006,7 +2008,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	  .set_user = set_id_dfr0_el1,
 	  .visibility = aa32_id_visibility,
 	  .reset = read_sanitised_id_dfr0_el1,
-	  .val = ID_DFR0_EL1_PerfMon_MASK, },
+	  .val = GENMASK(31, 0), },
 	ID_HIDDEN(ID_AFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -2055,7 +2057,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	  .get_user = get_id_reg,
 	  .set_user = set_id_aa64dfr0_el1,
 	  .reset = read_sanitised_id_aa64dfr0_el1,
-	  .val = ID_AA64DFR0_EL1_PMUVer_MASK, },
+	  .val = ~(ID_AA64DFR0_EL1_PMSVer_MASK | ID_AA64DFR0_EL1_RES0_MASK), },
 	ID_SANITISED(ID_AA64DFR1_EL1),
 	ID_UNALLOCATED(5,2),
 	ID_UNALLOCATED(5,3),
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
maintainer
#6 lore raw

Re: [PATCH v9 05/11] KVM: arm64: Enable writable for ID_AA64DFR0_EL1 and ID_DFR0_EL1

Quoted 24 lines · Jing Zhang

All valid fields in ID_AA64DFR0_EL1 and ID_DFR0_EL1 are writable from userspace with this change.
RES0 fields and those fields hidden by KVM are not writable.

---
 arch/arm64/kvm/sys_regs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -0
index afade7186675..20fc38bad4e8 100644
@@ -1931,6 +1931,8 @@ static bool access_spsr(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#define ID_AA64DFR0_EL1_RES0_MASK (GENMASK(59, 56) | GENMASK(27, 24) | GENMASK(19, 16))
+
 /*
  * Architected system registers.
  * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2011

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2006,7 +2008,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	  .set_user = set_id_dfr0_el1,
 	  .visibility = aa32_id_visibility,
 	  .reset = read_sanitised_id_dfr0_el1,
-	  .val = ID_DFR0_EL1_PerfMon_MASK, },
+	  .val = GENMASK(31, 0), },

Can you *please* look at the register and realise that we *don't* support writing to the whole of the low 32 bits? What does it mean to allow selecting the M-profile debug? Or the memory-mapped trace?

You are advertising a lot of crap to userspace, and that's definitely not on.

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2060

Quoted 9 lines
arch/arm64/kvm/sys_regs.c +1 -1
 	ID_HIDDEN(ID_AFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -2055,7 +2057,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	  .get_user = get_id_reg,
 	  .set_user = set_id_aa64dfr0_el1,
 	  .reset = read_sanitised_id_aa64dfr0_el1,
-	  .val = ID_AA64DFR0_EL1_PMUVer_MASK, },
+	  .val = ~(ID_AA64DFR0_EL1_PMSVer_MASK | ID_AA64DFR0_EL1_RES0_MASK), },

And it is the same thing here. Where is the handling code to deal with variable breakpoint numbers? Oh wait, there is none. Really, the only thing we support writing to are the PMU and Debug versions. And nothing else.

What does it mean for userspace? Either the write will be denied despite being advertised a writable field (remember the first patch of the series???), or we'll blindly accept the write and further ignore the requested values. Do you really think any of this is acceptable?

This is the *9th* version of this series, and we're still battling over some extremely basic userspace issues... I don't think we can merge this series as is stands.

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

Re: [PATCH v9 05/11] KVM: arm64: Enable writable for ID_AA64DFR0_EL1 and ID_DFR0_EL1

Hi Marc,

Quoted 55 lines · Marc Zyngier
Quoted 24 lines · Jing Zhang

All valid fields in ID_AA64DFR0_EL1 and ID_DFR0_EL1 are writable from userspace with this change.
RES0 fields and those fields hidden by KVM are not writable.

---
 arch/arm64/kvm/sys_regs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -0
index afade7186675..20fc38bad4e8 100644
@@ -1931,6 +1931,8 @@ static bool access_spsr(struct kvm_vcpu *vcpu,
      return true;
 }
 
+#define ID_AA64DFR0_EL1_RES0_MASK (GENMASK(59, 56) | GENMASK(27, 24) | GENMASK(19, 16))
+
 /*
  * Architected system registers.
  * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2011

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2006,7 +2008,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
        .set_user = set_id_dfr0_el1,
        .visibility = aa32_id_visibility,
        .reset = read_sanitised_id_dfr0_el1,
-       .val = ID_DFR0_EL1_PerfMon_MASK, },
+       .val = GENMASK(31, 0), },

Can you *please* look at the register and realise that we *don't* support writing to the whole of the low 32 bits? What does it mean to allow selecting the M-profile debug? Or the memory-mapped trace?

You are advertising a lot of crap to userspace, and that's definitely not on.

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2060

Quoted 9 lines
arch/arm64/kvm/sys_regs.c +1 -1
      ID_HIDDEN(ID_AFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -2055,7 +2057,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
        .get_user = get_id_reg,
        .set_user = set_id_aa64dfr0_el1,
        .reset = read_sanitised_id_aa64dfr0_el1,
-       .val = ID_AA64DFR0_EL1_PMUVer_MASK, },
+       .val = ~(ID_AA64DFR0_EL1_PMSVer_MASK | ID_AA64DFR0_EL1_RES0_MASK), },

And it is the same thing here. Where is the handling code to deal with variable breakpoint numbers? Oh wait, there is none. Really, the only thing we support writing to are the PMU and Debug versions. And nothing else.

Quoted 8 lines

What does it mean for userspace? Either the write will be denied despite being advertised a writable field (remember the first patch of the series???), or we'll blindly accept the write and further ignore the requested values. Do you really think any of this is acceptable?

This is the *9th* version of this series, and we're still battling over some extremely basic userspace issues... I don't think we can merge this series as is stands.

I removed sanity checks across multiple fields after the discussion
here: https://lore.kernel.org/all/ZKRC80hb4hXwW8WK@thinky-boi/
I might have misunderstood the discussion. I thought we'd let VMM do more complete sanity checks.

Quoted 5 lines
        M.

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

Jing

maintainer
#8 lore raw

Re: [PATCH v9 05/11] KVM: arm64: Enable writable for ID_AA64DFR0_EL1 and ID_DFR0_EL1

Quoted 64 lines · Jing Zhang

Hi Marc,

Quoted 60 lines · Marc Zyngier
Quoted 24 lines · Jing Zhang

All valid fields in ID_AA64DFR0_EL1 and ID_DFR0_EL1 are writable from userspace with this change.
RES0 fields and those fields hidden by KVM are not writable.

---
 arch/arm64/kvm/sys_regs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -0
index afade7186675..20fc38bad4e8 100644
@@ -1931,6 +1931,8 @@ static bool access_spsr(struct kvm_vcpu *vcpu,
      return true;
 }
 
+#define ID_AA64DFR0_EL1_RES0_MASK (GENMASK(59, 56) | GENMASK(27, 24) | GENMASK(19, 16))
+
 /*
  * Architected system registers.
  * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2011

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2006,7 +2008,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
        .set_user = set_id_dfr0_el1,
        .visibility = aa32_id_visibility,
        .reset = read_sanitised_id_dfr0_el1,
-       .val = ID_DFR0_EL1_PerfMon_MASK, },
+       .val = GENMASK(31, 0), },

Can you *please* look at the register and realise that we *don't* support writing to the whole of the low 32 bits? What does it mean to allow selecting the M-profile debug? Or the memory-mapped trace?

You are advertising a lot of crap to userspace, and that's definitely not on.

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2060

Quoted 9 lines
arch/arm64/kvm/sys_regs.c +1 -1
      ID_HIDDEN(ID_AFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -2055,7 +2057,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
        .get_user = get_id_reg,
        .set_user = set_id_aa64dfr0_el1,
        .reset = read_sanitised_id_aa64dfr0_el1,
-       .val = ID_AA64DFR0_EL1_PMUVer_MASK, },
+       .val = ~(ID_AA64DFR0_EL1_PMSVer_MASK | ID_AA64DFR0_EL1_RES0_MASK), },

And it is the same thing here. Where is the handling code to deal with variable breakpoint numbers? Oh wait, there is none. Really, the only thing we support writing to are the PMU and Debug versions. And nothing else.

What does it mean for userspace? Either the write will be denied despite being advertised a writable field (remember the first patch of the series???), or we'll blindly accept the write and further ignore the requested values. Do you really think any of this is acceptable?

Quoted 8 lines
Quoted 3 lines

This is the *9th* version of this series, and we're still battling over some extremely basic userspace issues... I don't think we can merge this series as is stands.

I removed sanity checks across multiple fields after the discussion
here: https://lore.kernel.org/all/ZKRC80hb4hXwW8WK@thinky-boi/
I might have misunderstood the discussion. I thought we'd let VMM do more complete sanity checks.

The problem is that you don't even have a statement about how this is supposed to be handled. What are the rules? How can the VMM author *know*?

That's my real issue with this series: at no point do we state when an ID register can be written, what are the rules that must be followed, where is the split in responsibility between KVM and the VMM, and what is the expected behaviour when the VMM exposes something that is completely outlandish to the guest (such as the M-profile debug).

Do you see the issue? We can always fix the code. But once we've exposed that to userspace, there is no going back. And I really want everybody's buy-in on that front, including the VMM people.

Thanks,

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

Re: [PATCH v9 05/11] KVM: arm64: Enable writable for ID_AA64DFR0_EL1 and ID_DFR0_EL1

Hi Marc,

Quoted 86 lines · Marc Zyngier
Quoted 64 lines · Jing Zhang

Hi Marc,

Quoted 60 lines · Marc Zyngier
Quoted 24 lines · Jing Zhang

All valid fields in ID_AA64DFR0_EL1 and ID_DFR0_EL1 are writable from userspace with this change.
RES0 fields and those fields hidden by KVM are not writable.

---
 arch/arm64/kvm/sys_regs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -0
index afade7186675..20fc38bad4e8 100644
@@ -1931,6 +1931,8 @@ static bool access_spsr(struct kvm_vcpu *vcpu,
      return true;
 }
 
+#define ID_AA64DFR0_EL1_RES0_MASK (GENMASK(59, 56) | GENMASK(27, 24) | GENMASK(19, 16))
+
 /*
  * Architected system registers.
  * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2011

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2006,7 +2008,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
        .set_user = set_id_dfr0_el1,
        .visibility = aa32_id_visibility,
        .reset = read_sanitised_id_dfr0_el1,
-       .val = ID_DFR0_EL1_PerfMon_MASK, },
+       .val = GENMASK(31, 0), },

Can you *please* look at the register and realise that we *don't* support writing to the whole of the low 32 bits? What does it mean to allow selecting the M-profile debug? Or the memory-mapped trace?

You are advertising a lot of crap to userspace, and that's definitely not on.

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2060

Quoted 9 lines
arch/arm64/kvm/sys_regs.c +1 -1
      ID_HIDDEN(ID_AFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -2055,7 +2057,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
        .get_user = get_id_reg,
        .set_user = set_id_aa64dfr0_el1,
        .reset = read_sanitised_id_aa64dfr0_el1,
-       .val = ID_AA64DFR0_EL1_PMUVer_MASK, },
+       .val = ~(ID_AA64DFR0_EL1_PMSVer_MASK | ID_AA64DFR0_EL1_RES0_MASK), },

And it is the same thing here. Where is the handling code to deal with variable breakpoint numbers? Oh wait, there is none. Really, the only thing we support writing to are the PMU and Debug versions. And nothing else.

What does it mean for userspace? Either the write will be denied despite being advertised a writable field (remember the first patch of the series???), or we'll blindly accept the write and further ignore the requested values. Do you really think any of this is acceptable?

Quoted 8 lines
Quoted 3 lines

This is the *9th* version of this series, and we're still battling over some extremely basic userspace issues... I don't think we can merge this series as is stands.

I removed sanity checks across multiple fields after the discussion
here: https://lore.kernel.org/all/ZKRC80hb4hXwW8WK@thinky-boi/
I might have misunderstood the discussion. I thought we'd let VMM do more complete sanity checks.

The problem is that you don't even have a statement about how this is supposed to be handled. What are the rules? How can the VMM author *know*?

That's my real issue with this series: at no point do we state when an ID register can be written, what are the rules that must be followed, where is the split in responsibility between KVM and the VMM, and what is the expected behaviour when the VMM exposes something that is completely outlandish to the guest (such as the M-profile debug).

Quoted 3 lines

Do you see the issue? We can always fix the code. But once we've exposed that to userspace, there is no going back. And I really want everybody's buy-in on that front, including the VMM people.

Understood.
Looks like it would be less complicated to have KVM do all the sanity checks to determine if a feature field is configured correctly. The determination can be done by following rules:
1. Architecture constraints from ARM ARM.
2. KVM constraints. (Those features not exposed by KVM should be read-only)
3. VCPU features. (The write mask needs to be determined on-the-fly)

Thanks,
Jing

Quoted 7 lines

Thanks,

        M.

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

author
#10 lore raw

Re: [PATCH v9 05/11] KVM: arm64: Enable writable for ID_AA64DFR0_EL1 and ID_DFR0_EL1

Hi Marc,

Quoted 94 lines · Jing Zhang

Hi Marc,

Quoted 86 lines · Marc Zyngier
Quoted 64 lines · Jing Zhang

Hi Marc,

Quoted 60 lines · Marc Zyngier
Quoted 24 lines · Jing Zhang

All valid fields in ID_AA64DFR0_EL1 and ID_DFR0_EL1 are writable from userspace with this change.
RES0 fields and those fields hidden by KVM are not writable.

---
 arch/arm64/kvm/sys_regs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -0
index afade7186675..20fc38bad4e8 100644
@@ -1931,6 +1931,8 @@ static bool access_spsr(struct kvm_vcpu *vcpu,
      return true;
 }
 
+#define ID_AA64DFR0_EL1_RES0_MASK (GENMASK(59, 56) | GENMASK(27, 24) | GENMASK(19, 16))
+
 /*
  * Architected system registers.
  * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2011

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2006,7 +2008,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
        .set_user = set_id_dfr0_el1,
        .visibility = aa32_id_visibility,
        .reset = read_sanitised_id_dfr0_el1,
-       .val = ID_DFR0_EL1_PerfMon_MASK, },
+       .val = GENMASK(31, 0), },

Can you *please* look at the register and realise that we *don't* support writing to the whole of the low 32 bits? What does it mean to allow selecting the M-profile debug? Or the memory-mapped trace?

You are advertising a lot of crap to userspace, and that's definitely not on.

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2060

Quoted 9 lines
arch/arm64/kvm/sys_regs.c +1 -1
      ID_HIDDEN(ID_AFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR0_EL1),
      AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -2055,7 +2057,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
        .get_user = get_id_reg,
        .set_user = set_id_aa64dfr0_el1,
        .reset = read_sanitised_id_aa64dfr0_el1,
-       .val = ID_AA64DFR0_EL1_PMUVer_MASK, },
+       .val = ~(ID_AA64DFR0_EL1_PMSVer_MASK | ID_AA64DFR0_EL1_RES0_MASK), },

And it is the same thing here. Where is the handling code to deal with variable breakpoint numbers? Oh wait, there is none. Really, the only thing we support writing to are the PMU and Debug versions. And nothing else.

What does it mean for userspace? Either the write will be denied despite being advertised a writable field (remember the first patch of the series???), or we'll blindly accept the write and further ignore the requested values. Do you really think any of this is acceptable?

Quoted 8 lines
Quoted 3 lines

This is the *9th* version of this series, and we're still battling over some extremely basic userspace issues... I don't think we can merge this series as is stands.

I removed sanity checks across multiple fields after the discussion
here: https://lore.kernel.org/all/ZKRC80hb4hXwW8WK@thinky-boi/
I might have misunderstood the discussion. I thought we'd let VMM do more complete sanity checks.

The problem is that you don't even have a statement about how this is supposed to be handled. What are the rules? How can the VMM author *know*?

That's my real issue with this series: at no point do we state when an ID register can be written, what are the rules that must be followed, where is the split in responsibility between KVM and the VMM, and what is the expected behaviour when the VMM exposes something that is completely outlandish to the guest (such as the M-profile debug).

Quoted 3 lines

Do you see the issue? We can always fix the code. But once we've exposed that to userspace, there is no going back. And I really want everybody's buy-in on that front, including the VMM people.

Quoted 7 lines

Understood.
Looks like it would be less complicated to have KVM do all the sanity checks to determine if a feature field is configured correctly. The determination can be done by following rules:
1. Architecture constraints from ARM ARM.
2. KVM constraints. (Those features not exposed by KVM should be read-only)
3. VCPU features. (The write mask needs to be determined on-the-fly)

Does this sound good to you to have all sanity checks in KVM?

Thanks,
Jing

Quoted 11 lines

Thanks,
Jing

Quoted 7 lines

Thanks,

        M.

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

#11 lore raw

Re: [PATCH v9 05/11] KVM: arm64: Enable writable for ID_AA64DFR0_EL1 and ID_DFR0_EL1

On Tue, Sep 05, 2023 at 07:13:35PM -0700, Jing Zhang wrote:

[...]

Quoted 27 lines
Quoted 19 lines
Quoted 15 lines
Quoted 4 lines

I removed sanity checks across multiple fields after the discussion
here: https://lore.kernel.org/all/ZKRC80hb4hXwW8WK@thinky-boi/
I might have misunderstood the discussion. I thought we'd let VMM do more complete sanity checks.

The problem is that you don't even have a statement about how this is supposed to be handled. What are the rules? How can the VMM author *know*?

That's my real issue with this series: at no point do we state when an ID register can be written, what are the rules that must be followed, where is the split in responsibility between KVM and the VMM, and what is the expected behaviour when the VMM exposes something that is completely outlandish to the guest (such as the M-profile debug).

Quoted 3 lines

Do you see the issue? We can always fix the code. But once we've exposed that to userspace, there is no going back. And I really want everybody's buy-in on that front, including the VMM people.

Quoted 7 lines

Understood.
Looks like it would be less complicated to have KVM do all the sanity checks to determine if a feature field is configured correctly. The determination can be done by following rules:
1. Architecture constraints from ARM ARM.
2. KVM constraints. (Those features not exposed by KVM should be read-only)
3. VCPU features. (The write mask needs to be determined on-the-fly)

Quoted 1 line

Does this sound good to you to have all sanity checks in KVM?

I would rather we not implement exhaustive checks in KVM, because we *will* get them wrong. I don't believe Marc is asking for exhaustive sanity checks in KVM either, just that we prevent userspace from selecting features we will _never_ emulate (like the MProfDbg example). You need very clear documentation for the usage pattern and what the VMM's responsibilities are (like obeying the ARM ARM).

While we're here, I'll subject the both of you to one of the ongoing thoughts I've had with the whole configurable CPU model UAPI. Ideally we should get to the point where all emulation and trap configuration is solely determined from the ID register values of the VM.

I'm a bit worried that this mixes poorly with userspace system register accesses, though. As implemented, nothing stops userspce from interleaving ID register writes with other system registers that might be conditional on a particular CPU feature. For example, disabling the PMU via DFR0 and then writing to the PMCs. Sure, this could be hacked around by making PMCs visible to userspace only when the ID register hides the feature, but we may be painting ourselves in a corner w.r.t. the addition of new features.

One way out would be to make the ID registers immutable after the first system register write outside of the ID register range. Changes under the hood wouldn't be too terrible; AFAICT it involves hoisting error checking into kvm_vcpu_init_check_features() and deferring kvm_vcpu_reset() to the point the ID regs are immutable.

I somewhat like the clear order of operations, and it _shouldn't_ break existing VMMs since they just save/restore the KVM values verbatim. Of course, this requires some very clear documentation for VMMs that want to adopt the new UAPI.

Signature · 2 lines
Thanks,
Oliver
author
#12 lore raw

PATCH v9 6/11 KVM: arm64: Bump up the default KVM sanitised debug version to v8p8

In reply to Jing Zhang

From: Oliver Upton <[email protected]>

Since ID_AA64DFR0_EL1 and ID_DFR0_EL1 are now writable from userspace, it is safe to bump up the default KVM sanitised debug version to v8p8.

---
 arch/arm64/kvm/sys_regs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +3 -2
index 20fc38bad4e8..cee5f879df19 100644
@@ -1489,8 +1489,7 @@ static u64 read_sanitised_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 {
 	u64 val = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
 
-	/* Limit debug to ARMv8.0 */
-	val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64DFR0_EL1, DebugVer, IMP);
+	val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64DFR0_EL1, DebugVer, V8P8);
 
 	/*
 	 * Only initialize the PMU version if the vCPU was configured with one.
@@ -1550,6 +1549,8 @@ static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	if (kvm_vcpu_has_pmu(vcpu))
 		val |= SYS_FIELD_PREP(ID_DFR0_EL1, PerfMon, perfmon);
 
+	val = ID_REG_LIMIT_FIELD_ENUM(val, ID_DFR0_EL1, CopDbg, Debugv8p8);
+
 	return val;
 }
 
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
author
#13 lore raw

PATCH v9 7/11 KVM: arm64: Enable writable for ID_AA64PFR0_EL1

In reply to Jing Zhang

All valid fields in ID_AA64PFR0_EL1 are writable from userspace with this change except AMU field which isn't exposed by KVM.

---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +1 -1
index cee5f879df19..bf716f646872 100644
@@ -2043,7 +2043,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	  .get_user = get_id_reg,
 	  .set_user = set_id_reg,
 	  .reset = read_sanitised_id_aa64pfr0_el1,
-	  .val = ID_AA64PFR0_EL1_CSV2_MASK | ID_AA64PFR0_EL1_CSV3_MASK, },
+	  .val = ~ID_AA64PFR0_EL1_AMU_MASK, },
 	ID_SANITISED(ID_AA64PFR1_EL1),
 	ID_UNALLOCATED(4,2),
 	ID_UNALLOCATED(4,3),
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
maintainer
#14 lore raw

Re: [PATCH v9 07/11] KVM: arm64: Enable writable for ID_AA64PFR0_EL1

Quoted 14 lines · Jing Zhang

All valid fields in ID_AA64PFR0_EL1 are writable from userspace with this change except AMU field which isn't exposed by KVM.

---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +0 -0
index cee5f879df19..bf716f646872 100644

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 2049

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2043,7 +2043,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	  .get_user = get_id_reg,
 	  .set_user = set_id_reg,
 	  .reset = read_sanitised_id_aa64pfr0_el1,
-	  .val = ID_AA64PFR0_EL1_CSV2_MASK | ID_AA64PFR0_EL1_CSV3_MASK, },
+	  .val = ~ID_AA64PFR0_EL1_AMU_MASK, },
 	ID_SANITISED(ID_AA64PFR1_EL1),
 	ID_UNALLOCATED(4,2),
 	ID_UNALLOCATED(4,3),

Same thing. Does it mean we can disable the GIC? Or FP? Have you taken my previous comment about the AMU as the only issue?

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

PATCH v9 8/11 KVM: arm64: Refactor helper Macros for idreg desc

In reply to Jing Zhang

Add some helpers to ease the declaration for idreg desc. These Macros will be heavily used for future commits enabling writable for idregs.

---
 arch/arm64/kvm/sys_regs.c | 82 +++++++++++++++++----------------------
 1 file changed, 36 insertions(+), 46 deletions(-)
arch/arm64/kvm/sys_regs.c +36 -46
index bf716f646872..44d164d47756 100644
@@ -1844,27 +1844,37 @@ static unsigned int elx2_visibility(const struct kvm_vcpu *vcpu,
  * from userspace.
  */
 
-/* sys_reg_desc initialiser for known cpufeature ID registers */
-#define ID_SANITISED(name) {			\
-	SYS_DESC(SYS_##name),			\
-	.access	= access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = id_visibility,		\
-	.reset = kvm_read_sanitised_id_reg,	\
-	.val = 0,				\
+#define ID_DESC(name, _set_user, _visibility, _reset, mask) {	\
+	SYS_DESC(SYS_##name),					\
+	.access	= access_id_reg,				\
+	.get_user = get_id_reg,					\
+	.set_user = _set_user,					\
+	.visibility = _visibility,				\
+	.reset = _reset,					\
+	.val = mask,						\
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define AA32_ID_SANITISED(name) {		\
-	SYS_DESC(SYS_##name),			\
-	.access	= access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = aa32_id_visibility,	\
-	.reset = kvm_read_sanitised_id_reg,	\
-	.val = 0,				\
-}
+#define _ID_SANITISED(name, _set_user, _reset) \
+	ID_DESC(name, _set_user, id_visibility, _reset, 0)
+#define ID_SANITISED(name) \
+	_ID_SANITISED(name, set_id_reg, kvm_read_sanitised_id_reg)
+
+#define _ID_SANITISED_W(name, _set_user, _reset, mask) \
+	ID_DESC(name, _set_user, id_visibility, _reset, mask)
+#define ID_SANITISED_W(name, mask) \
+	_ID_SANITISED_W(name, set_id_reg, kvm_read_sanitised_id_reg, mask)
+
+/* sys_reg_desc initialiser for known cpufeature ID registers */
+#define _AA32_ID_SANITISED(name, _set_user, _reset) \
+	ID_DESC(name, _set_user, aa32_id_visibility, _reset, 0)
+#define AA32_ID_SANITISED(name) \
+	_AA32_ID_SANITISED(name, set_id_reg, kvm_read_sanitised_id_reg)
+
+#define _AA32_ID_SANITISED_W(name, _set_user, _reset, mask) \
+	ID_DESC(name, _set_user, aa32_id_visibility, _reset, mask)
+#define AA32_ID_SANITISED_W(name, mask) \
+	_AA32_ID_SANITISED_W(name, set_id_reg, kvm_read_sanitised_id_reg, mask)
 
 /*
  * sys_reg_desc initialiser for architecturally unallocated cpufeature ID
@@ -1886,15 +1896,8 @@ static unsigned int elx2_visibility(const struct kvm_vcpu *vcpu,
  * For now, these are exposed just like unallocated ID regs: they appear
  * RAZ for the guest.
  */
-#define ID_HIDDEN(name) {			\
-	SYS_DESC(SYS_##name),			\
-	.access = access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = raz_visibility,		\
-	.reset = kvm_read_sanitised_id_reg,	\
-	.val = 0,				\
-}
+#define ID_HIDDEN(name) \
+	ID_DESC(name, set_id_reg, raz_visibility, kvm_read_sanitised_id_reg, 0)
 
 static bool access_sp_el1(struct kvm_vcpu *vcpu,
 			  struct sys_reg_params *p,
@@ -2003,13 +2006,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	/* CRm=1 */
 	AA32_ID_SANITISED(ID_PFR0_EL1),
 	AA32_ID_SANITISED(ID_PFR1_EL1),
-	{ SYS_DESC(SYS_ID_DFR0_EL1),
-	  .access = access_id_reg,
-	  .get_user = get_id_reg,
-	  .set_user = set_id_dfr0_el1,
-	  .visibility = aa32_id_visibility,
-	  .reset = read_sanitised_id_dfr0_el1,
-	  .val = GENMASK(31, 0), },
+	_AA32_ID_SANITISED_W(ID_DFR0_EL1, set_id_dfr0_el1,
+			     read_sanitised_id_dfr0_el1, GENMASK(31, 0)),
 	ID_HIDDEN(ID_AFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -2038,12 +2036,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 
 	/* AArch64 ID registers */
 	/* CRm=4 */
-	{ SYS_DESC(SYS_ID_AA64PFR0_EL1),
-	  .access = access_id_reg,
-	  .get_user = get_id_reg,
-	  .set_user = set_id_reg,
-	  .reset = read_sanitised_id_aa64pfr0_el1,
-	  .val = ~ID_AA64PFR0_EL1_AMU_MASK, },
+	_ID_SANITISED_W(ID_AA64PFR0_EL1, set_id_reg,
+			read_sanitised_id_aa64pfr0_el1, ~ID_AA64PFR0_EL1_AMU_MASK),
 	ID_SANITISED(ID_AA64PFR1_EL1),
 	ID_UNALLOCATED(4,2),
 	ID_UNALLOCATED(4,3),
@@ -2053,12 +2047,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	ID_UNALLOCATED(4,7),
 
 	/* CRm=5 */
-	{ SYS_DESC(SYS_ID_AA64DFR0_EL1),
-	  .access = access_id_reg,
-	  .get_user = get_id_reg,
-	  .set_user = set_id_aa64dfr0_el1,
-	  .reset = read_sanitised_id_aa64dfr0_el1,
-	  .val = ~(ID_AA64DFR0_EL1_PMSVer_MASK | ID_AA64DFR0_EL1_RES0_MASK), },
+	_ID_SANITISED_W(ID_AA64DFR0_EL1, set_id_aa64dfr0_el1, read_sanitised_id_aa64dfr0_el1,
+			~(ID_AA64DFR0_EL1_PMSVer_MASK | ID_AA64DFR0_EL1_RES0_MASK)),
 	ID_SANITISED(ID_AA64DFR1_EL1),
 	ID_UNALLOCATED(5,2),
 	ID_UNALLOCATED(5,3),
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
author
#16 lore raw

PATCH v9 9/11 KVM: arm64: Enable writable for ID_AA64MMFR{0, 1, 2, 3}_EL1

In reply to Jing Zhang

Enable writable from userspace for ID_AA64MMFR{0, 1, 2, 3}_EL1. RES0 fields and those fields not exposed by KVM are not writable.

---
 arch/arm64/kvm/sys_regs.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
arch/arm64/kvm/sys_regs.c +18 -7
index 44d164d47756..96a1dccf1af5 100644
@@ -1346,9 +1346,6 @@ static u64 __kvm_read_sanitised_id_reg(const struct kvm_vcpu *vcpu,
 			val &= ~ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_WFxT);
 		val &= ~ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_MOPS);
 		break;
-	case SYS_ID_AA64MMFR2_EL1:
-		val &= ~ID_AA64MMFR2_EL1_CCIDX_MASK;
-		break;
 	case SYS_ID_MMFR4_EL1:
 		val &= ~ARM64_FEATURE_MASK(ID_MMFR4_EL1_CCIDX);
 		break;
@@ -1581,6 +1578,15 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	return set_id_reg(vcpu, rd, val);
 }
 
+static u64 read_sanitised_id_aa64mmfr2_el1(struct kvm_vcpu *vcpu,
+					   const struct sys_reg_desc *rd)
+{
+	u64 val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
+
+	val &= ~ID_AA64MMFR2_EL1_CCIDX_MASK;
+	return val;
+}
+
 /*
  * cpufeature ID register user accessors
  *
@@ -1936,6 +1942,10 @@ static bool access_spsr(struct kvm_vcpu *vcpu,
 }
 
 #define ID_AA64DFR0_EL1_RES0_MASK (GENMASK(59, 56) | GENMASK(27, 24) | GENMASK(19, 16))
+#define ID_AA64MMFR0_EL1_RES0_MASK GENMASK(55, 48)
+#define ID_AA64MMFR1_EL1_RES0_MASK GENMASK(63, 60)
+#define ID_AA64MMFR2_EL1_RES0_MASK GENMASK(47, 44)
+#define ID_AA64MMFR3_EL1_RES0_MASK (GENMASK(59, 32) | GENMASK(27, 8))
 
 /*
  * Architected system registers.
@@ -2068,10 +2078,11 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	ID_UNALLOCATED(6,7),
 
 	/* CRm=7 */
-	ID_SANITISED(ID_AA64MMFR0_EL1),
-	ID_SANITISED(ID_AA64MMFR1_EL1),
-	ID_SANITISED(ID_AA64MMFR2_EL1),
-	ID_SANITISED(ID_AA64MMFR3_EL1),
+	ID_SANITISED_W(ID_AA64MMFR0_EL1, ~ID_AA64MMFR0_EL1_RES0_MASK),
+	ID_SANITISED_W(ID_AA64MMFR1_EL1, ~ID_AA64MMFR1_EL1_RES0_MASK),
+	_ID_SANITISED_W(ID_AA64MMFR2_EL1, set_id_reg, read_sanitised_id_aa64mmfr2_el1,
+			~(ID_AA64MMFR2_EL1_CCIDX_MASK | ID_AA64MMFR2_EL1_RES0_MASK)),
+	ID_SANITISED_W(ID_AA64MMFR3_EL1, ~ID_AA64MMFR3_EL1_RES0_MASK),
 	ID_UNALLOCATED(7,4),
 	ID_UNALLOCATED(7,5),
 	ID_UNALLOCATED(7,6),
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
author
#17 lore raw

PATCH v9 10/11 KVM: arm64: selftests: Import automatic system register definition generation from kernel

In reply to Jing Zhang

Import automatic system register definition generation from kernel and update system register usage accordingly.

---
 tools/arch/arm64/include/.gitignore           |    1 +
 tools/arch/arm64/include/asm/gpr-num.h        |   26 +
 tools/arch/arm64/include/asm/sysreg.h         |  839 ++----
 tools/arch/arm64/tools/gen-sysreg.awk         |  336 +++
 tools/arch/arm64/tools/sysreg                 | 2497 +++++++++++++++++
 tools/testing/selftests/kvm/Makefile          |   14 +-
 .../selftests/kvm/aarch64/aarch32_id_regs.c   |    4 +-
 .../selftests/kvm/aarch64/debug-exceptions.c  |   12 +-
 .../selftests/kvm/aarch64/page_fault_test.c   |    6 +-
 .../selftests/kvm/lib/aarch64/processor.c     |    6 +-
 10 files changed, 3078 insertions(+), 663 deletions(-)
 create mode 100644 tools/arch/arm64/include/.gitignore
 create mode 100644 tools/arch/arm64/include/asm/gpr-num.h
 create mode 100755 tools/arch/arm64/tools/gen-sysreg.awk
 create mode 100644 tools/arch/arm64/tools/sysreg
tools/arch/arm64/include/.gitignore new file +1 -0
new file mode 100644
index 000000000000..9ab870da897d
@@ -0,0 +1 @@
+generated/
tools/arch/arm64/include/asm/gpr-num.h new file +26 -0
new file mode 100644
index 000000000000..05da4a7c5788
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __ASM_GPR_NUM_H
+#define __ASM_GPR_NUM_H
+
+#ifdef __ASSEMBLY__
+
+	.irp	num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
+	.equ	.L__gpr_num_x\num, \num
+	.equ	.L__gpr_num_w\num, \num
+	.endr
+	.equ	.L__gpr_num_xzr, 31
+	.equ	.L__gpr_num_wzr, 31
+
+#else /* __ASSEMBLY__ */
+
+#define __DEFINE_ASM_GPR_NUMS					\
+"	.irp	num,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30\n" \
+"	.equ	.L__gpr_num_x\\num, \\num\n"			\
+"	.equ	.L__gpr_num_w\\num, \\num\n"			\
+"	.endr\n"						\
+"	.equ	.L__gpr_num_xzr, 31\n"				\
+"	.equ	.L__gpr_num_wzr, 31\n"
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ASM_GPR_NUM_H */
tools/arch/arm64/include/asm/sysreg.h +192 -647

1,107 lines not on this page · raw message

tools/arch/arm64/tools/gen-sysreg.awk new file +336 -0

336 lines not on this page · raw message

tools/arch/arm64/tools/sysreg new file +2497 -0

1,530 lines not on this page · raw message

tools/testing/selftests/kvm/Makefile +12 -2
index c692cc86e7da..bae87c50bddc 100644

Lines of this file are not shown. View the raw message.

tools/testing/selftests/kvm/aarch64/aarch32_id_regs.c +2 -2
index 4951ac53d1f8..9695b9d229cb 100644

Lines of this file are not shown. View the raw message.

tools/testing/selftests/kvm/aarch64/debug-exceptions.c +6 -6
index 637be796086f..f77893e48711 100644

Lines of this file are not shown. View the raw message.

tools/testing/selftests/kvm/aarch64/page_fault_test.c +3 -3
index df10f1ffa20d..cfb7ab3d7f5d 100644

Lines of this file are not shown. View the raw message.

tools/testing/selftests/kvm/lib/aarch64/processor.c +3 -3
index 3a0259e25335..6fe12e985ba5 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog

Part of the diff is not shown here. View the raw message.

author
#18 lore raw

PATCH v9 11/11 KVM: arm64: selftests: Test for setting ID register from usersapce

In reply to Jing Zhang

Add tests to verify setting ID registers from userapce is handled correctly by KVM. Also add a test case to use ioctl KVM_ARM_GET_REG_WRITABLE_MASKS to get writable masks.

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/aarch64/set_id_regs.c       | 448 ++++++++++++++++++
 2 files changed, 449 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/aarch64/set_id_regs.c
tools/testing/selftests/kvm/Makefile +1 -0
index bae87c50bddc..6862e906c9f3 100644
@@ -144,6 +144,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
 TEST_GEN_PROGS_aarch64 += aarch64/hypercalls
 TEST_GEN_PROGS_aarch64 += aarch64/page_fault_test
 TEST_GEN_PROGS_aarch64 += aarch64/psci_test
+TEST_GEN_PROGS_aarch64 += aarch64/set_id_regs
 TEST_GEN_PROGS_aarch64 += aarch64/smccc_filter
 TEST_GEN_PROGS_aarch64 += aarch64/vcpu_width_config
 TEST_GEN_PROGS_aarch64 += aarch64/vgic_init
tools/testing/selftests/kvm/aarch64/set_id_regs.c new file +448 -0

448 lines not on this page · raw message

Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
#19 lore raw
In reply to Jing Zhang
Quoted 7 lines · Jing Zhang

This patch series enable userspace writable for below idregs:
ID_AA64DFR0_EL1, ID_DFR0_EL1, ID_AA64PFR0_EL1, ID_AA64MMFR{0, 1, 2, 3}_EL1. A vm ioctl is added to get feature ID register writable masks from userspace. A selftest is added to verify that KVM handles the writings from user space correctly.
A relevant patch from Oliver is picked from [3].

Quoted 7 lines

---

* v8 -> v9
- Rebase on v6.5-rc7.
- Don't allow writable on RES0 fields and those fields not exposed by KVM.
- Fixed build dependency issue for system register definition generation in selftest.

What about the changes to the userspace interface as proposed by Marc in <[email protected]>?