Skip to contentSkip to conversation

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

PATCH Superseded Jing Zhang 4 participants 27 messages Aug 7, 2023 – Aug 21, 2023

Subsystems (5)
Documentation KVM Kselftest arm64 +1 more subsystems

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Jing Zhang

author
#0 lore raw

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

---

* 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]

---

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                     |  197 +-
 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          |   16 +-
 .../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       |  453 +++
 .../selftests/kvm/lib/aarch64/processor.c     |    6 +-
 17 files changed, 3734 insertions(+), 724 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: 52a93d39b17dc7eb98b6aa3edb93943248e03b2f

Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
author
#1 lore raw

PATCH v8 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 72dc53a75d1c..e08894692829 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.41.0.585.gd2178a4bd4-goog
author
#2 lore raw

PATCH v8 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.41.0.585.gd2178a4bd4-goog
#3 lore raw

Re: [PATCH v8 02/11] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Quoted 34 lines · 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 +18 -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];
+        };

Documentation/virt/kvm/api.rst writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG · line 6098

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+
+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.

In case of future extensions, this means that userspace needs to figure out what the kernel supports via different content in reg_mask_range (i.e. try with a value in one of the currently reserved fields and fall back to using addr only if that doesn't work.) Can we do better?

Maybe we could introduce a capability that returns the supported ranges as flags, i.e. now we would return 1 for id regs masks, and for the future case where we have some values in the next reserved field we could return 1 & 2 etc. Would make life easier for userspace that needs to work with different kernels, but might be overkill if reserved[] is more like an insurance without any concrete plans for extensions.

author
#4 lore raw

Re: [PATCH v8 02/11] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Quoted 53 lines · Cornelia Huck
Quoted 34 lines · 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 +18 -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];
+        };

Documentation/virt/kvm/api.rst writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG · line 6098

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+
+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.

In case of future extensions, this means that userspace needs to figure out what the kernel supports via different content in reg_mask_range (i.e. try with a value in one of the currently reserved fields and fall back to using addr only if that doesn't work.) Can we do better?

Quoted 7 lines

Maybe we could introduce a capability that returns the supported ranges as flags, i.e. now we would return 1 for id regs masks, and for the future case where we have some values in the next reserved field we could return 1 & 2 etc. Would make life easier for userspace that needs to work with different kernels, but might be overkill if reserved[] is more like an insurance without any concrete plans for extensions.

Maybe it'd be better to leave this to whenever we do need to add other range support?

Jing

#5 lore raw

Re: [PATCH v8 02/11] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Quoted 62 lines · Jing Zhang
Quoted 53 lines · Cornelia Huck
Quoted 34 lines · 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 +18 -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];
+        };

Documentation/virt/kvm/api.rst writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG · line 6098

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+
+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.

In case of future extensions, this means that userspace needs to figure out what the kernel supports via different content in reg_mask_range (i.e. try with a value in one of the currently reserved fields and fall back to using addr only if that doesn't work.) Can we do better?

Quoted 7 lines

Maybe we could introduce a capability that returns the supported ranges as flags, i.e. now we would return 1 for id regs masks, and for the future case where we have some values in the next reserved field we could return 1 & 2 etc. Would make life easier for userspace that needs to work with different kernels, but might be overkill if reserved[] is more like an insurance without any concrete plans for extensions.

Quoted 2 lines

Maybe it'd be better to leave this to whenever we do need to add other range support?

My point is: How does userspace figure out if the kernel that is running supports ranges other than id regs? If this is just an insurance against changes that might arrive or not, we can live with the awkward "just try it out" approach; if we think it's likely that we'll need to extend it, we need to add the mechanism for userspace to find out about it now, or it would need to probe for presence of the mechanism...

maintainer
#6 lore raw

Re: [PATCH v8 02/11] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Quoted 6 lines · Cornelia Huck
Quoted 2 lines · Jing Zhang

Maybe it'd be better to leave this to whenever we do need to add other range support?

Quoted 6 lines

My point is: How does userspace figure out if the kernel that is running supports ranges other than id regs? If this is just an insurance against changes that might arrive or not, we can live with the awkward "just try it out" approach; if we think it's likely that we'll need to extend it, we need to add the mechanism for userspace to find out about it now, or it would need to probe for presence of the mechanism...

Agreed. Nothing like the present to address this sort of things. it really doesn't cost much, and I'd rather have it right now.

Here's a vague attempt at an advertising mechanism. If people are OK with it, I can stash that on top of Jing's series.

Thanks,

	M.

From bcfd87e85954e24ac4a905a3486c9040cdfc6d0b Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Thu, 17 Aug 2023 14:48:16 +0100
Subject: [PATCH] KVM: arm64: Add KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES

While the Feature ID range is well defined and pretty large, it isn't inconceivable that the architecture will eventually grow some other ranges that will need to similarly be described to userspace.

Add a new capability (KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES) that returns a bitmap of the valid ranges, which can subsequently be retrieved, one at a time by setting the index of the set bit as the range identifier.

Obviously, we only support a value of 0 for now.

---
 Documentation/virt/kvm/api.rst    | 37 ++++++++++++++++++++-----------
 arch/arm64/include/uapi/asm/kvm.h | 13 +++++++----
 arch/arm64/kvm/arm.c              |  3 +++
 arch/arm64/kvm/sys_regs.c         |  5 +++--
 include/uapi/linux/kvm.h          |  1 +
 5 files changed, 40 insertions(+), 19 deletions(-)
Documentation/virt/kvm/api.rst +24 -13
index 92a9b20f970e..0e6ce02cac3b 100644
@@ -6071,7 +6071,7 @@ applied.
 4.139 KVM_ARM_GET_REG_WRITABLE_MASKS
 -------------------------------------------
 
-:Capability: none
+:Capability: KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES
 :Architectures: arm64
 :Type: vm ioctl
 :Parameters: struct reg_mask_range (in/out)
@@ -6082,20 +6082,31 @@ applied.
 
         #define ARM64_FEATURE_ID_SPACE_SIZE	(3 * 8 * 8)
 
-        struct reg_mask_range {
-                __u64 addr;             /* Pointer to mask array */
-                __u64 reserved[7];
-        };
+	struct reg_mask_range {
+		__u64 addr;		/* Pointer to mask array */
+		__u32 range;		/* Requested range */
+		__u32 reserved[13];
+	};
 
-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
+This ioctl copies the writable masks for Feature ID registers to userspace.
+The Feature ID space is defined as the AArch64 System Register space 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.
+
+The mask array pointed to by ``addr`` is indexed by the macro
+``ARM64_FEATURE_ID_SPACE_IDX(op0, op1, crn, crm, op2)``, allowing userspace
+to know what bits can be changed for the system register described by ``op0,
+op1, crn, crm, op2``.
+
+The ``range`` field describes the requested range of registers. The valid
+ranges can be retrieved by checking the return value of
+KVM_CAP_CHECK_EXTENSION_VM for the KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES
+capability, which will return a bitmask of the supported ranges. Each bit
+set in the return value represents a possible value for the ``range``
+field.  At the time of writing, only bit 0 is returned set by the
+capability, meaning that only the value 0 is value for ``range``.
+
+The ``reserved[13]`` array is reserved for future use and should be 0, or
+KVM may return an error.
 
 5. The kvm_run structure
 ========================
arch/arm64/include/uapi/asm/kvm.h +9 -4
index 7a21bbb8a0f7..5148b4c22549 100644
@@ -505,8 +505,9 @@ 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. */
 /*
+ * Get feature ID registers userspace writable mask.
+ *
  * From DDI0487J.a, D19.2.66 ("ID_AA64MMFR2_EL1, AArch64 Memory Model
  * Feature Register 2"):
  *
@@ -514,8 +515,11 @@ struct kvm_smccc_filter {
  * 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.
+ * This covers all currently known R/O registers that indicate
+ * anything useful feature wise, including the ID registers.
+ *
+ * If we ever need to introduce a new range, it will be described as
+ * such in the range field.
  */
 #define ARM64_FEATURE_ID_SPACE_IDX(op0, op1, crn, crm, op2)		\
 	({								\
@@ -528,7 +532,8 @@ struct kvm_smccc_filter {
 
 struct reg_mask_range {
 	__u64 addr;		/* Pointer to mask array */
-	__u64 reserved[7];
+	__u32 range;		/* Requested range */
+	__u32 reserved[13];
 };
 
 #endif
arch/arm64/kvm/arm.c +3 -0
index e08894692829..6ea4d8b0e744 100644
@@ -316,6 +316,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
 		r = kvm_supported_block_sizes();
 		break;
+	case KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES:
+		r = BIT(0);
+		break;
 	default:
 		r = 0;
 	}
arch/arm64/kvm/sys_regs.c +3 -2
index 59c590fff4f2..6eadd0fa2c53 100644
@@ -3600,8 +3600,9 @@ int kvm_vm_ioctl_get_reg_writable_masks(struct kvm *kvm, struct reg_mask_range *
 	const void *zero_page = page_to_virt(ZERO_PAGE(0));
 	u64 __user *masks = (u64 __user *)range->addr;
 
-	/* Only feature id range is supported, reserved[7] must be zero. */
-	if (memcmp(range->reserved, zero_page, sizeof(range->reserved)))
+	/* Only feature id range is supported, reserved[13] must be zero. */
+	if (range->range ||
+	    memcmp(range->reserved, zero_page, sizeof(range->reserved)))
 		return -EINVAL;
 
 	/* Wipe the whole thing first */
include/uapi/linux/kvm.h +1 -0
index 424b6d00440b..f5100055a1a6 100644
@@ -1192,6 +1192,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_COUNTER_OFFSET 227
 #define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228
 #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229
+#define KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES 230
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
Without deviation from the norm, progress is not possible.
#7 lore raw

Re: [PATCH v8 02/11] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Quoted 15 lines · Marc Zyngier
Quoted 6 lines · Cornelia Huck
Quoted 2 lines · Jing Zhang

Maybe it'd be better to leave this to whenever we do need to add other range support?

Quoted 6 lines

My point is: How does userspace figure out if the kernel that is running supports ranges other than id regs? If this is just an insurance against changes that might arrive or not, we can live with the awkward "just try it out" approach; if we think it's likely that we'll need to extend it, we need to add the mechanism for userspace to find out about it now, or it would need to probe for presence of the mechanism...

Quoted 5 lines

Agreed. Nothing like the present to address this sort of things. it really doesn't cost much, and I'd rather have it right now.

Here's a vague attempt at an advertising mechanism. If people are OK with it, I can stash that on top of Jing's series.

I think that looks reasonable.

author
#8 lore raw

Re: [PATCH v8 02/11] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

In reply to Marc Zyngier
Quoted 192 lines · Marc Zyngier
Quoted 6 lines · Cornelia Huck
Quoted 2 lines · Jing Zhang

Maybe it'd be better to leave this to whenever we do need to add other range support?

Quoted 6 lines

My point is: How does userspace figure out if the kernel that is running supports ranges other than id regs? If this is just an insurance against changes that might arrive or not, we can live with the awkward "just try it out" approach; if we think it's likely that we'll need to extend it, we need to add the mechanism for userspace to find out about it now, or it would need to probe for presence of the mechanism...

Agreed. Nothing like the present to address this sort of things. it really doesn't cost much, and I'd rather have it right now.

Here's a vague attempt at an advertising mechanism. If people are OK with it, I can stash that on top of Jing's series.

Thanks,

        M.

From bcfd87e85954e24ac4a905a3486c9040cdfc6d0b Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Thu, 17 Aug 2023 14:48:16 +0100
Subject: [PATCH] KVM: arm64: Add KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES

While the Feature ID range is well defined and pretty large, it isn't inconceivable that the architecture will eventually grow some other ranges that will need to similarly be described to userspace.

Add a new capability (KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES) that returns a bitmap of the valid ranges, which can subsequently be retrieved, one at a time by setting the index of the set bit as the range identifier.

Obviously, we only support a value of 0 for now.

---
 Documentation/virt/kvm/api.rst    | 37 ++++++++++++++++++++-----------
 arch/arm64/include/uapi/asm/kvm.h | 13 +++++++----
 arch/arm64/kvm/arm.c              |  3 +++
 arch/arm64/kvm/sys_regs.c         |  5 +++--
 include/uapi/linux/kvm.h          |  1 +
 5 files changed, 40 insertions(+), 19 deletions(-)
Documentation/virt/kvm/api.rst +24 -13
index 92a9b20f970e..0e6ce02cac3b 100644
@@ -6071,7 +6071,7 @@ applied.
 4.139 KVM_ARM_GET_REG_WRITABLE_MASKS
 -------------------------------------------
 
-:Capability: none
+:Capability: KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES
 :Architectures: arm64
 :Type: vm ioctl
 :Parameters: struct reg_mask_range (in/out)
@@ -6082,20 +6082,31 @@ applied.
 
         #define ARM64_FEATURE_ID_SPACE_SIZE    (3 * 8 * 8)
 
-        struct reg_mask_range {
-                __u64 addr;             /* Pointer to mask array */
-                __u64 reserved[7];
-        };
+       struct reg_mask_range {
+               __u64 addr;             /* Pointer to mask array */
+               __u32 range;            /* Requested range */
+               __u32 reserved[13];
+       };
 
-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
+This ioctl copies the writable masks for Feature ID registers to userspace.
+The Feature ID space is defined as the AArch64 System Register space 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.
+
+The mask array pointed to by ``addr`` is indexed by the macro
+``ARM64_FEATURE_ID_SPACE_IDX(op0, op1, crn, crm, op2)``, allowing userspace
+to know what bits can be changed for the system register described by ``op0,
+op1, crn, crm, op2``.
+
+The ``range`` field describes the requested range of registers. The valid
+ranges can be retrieved by checking the return value of
+KVM_CAP_CHECK_EXTENSION_VM for the KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES
+capability, which will return a bitmask of the supported ranges. Each bit
+set in the return value represents a possible value for the ``range``
+field.  At the time of writing, only bit 0 is returned set by the
+capability, meaning that only the value 0 is value for ``range``.
+
+The ``reserved[13]`` array is reserved for future use and should be 0, or
+KVM may return an error.
 
 5. The kvm_run structure
 ========================
arch/arm64/include/uapi/asm/kvm.h +9 -4
index 7a21bbb8a0f7..5148b4c22549 100644
@@ -505,8 +505,9 @@ 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. */
 /*
+ * Get feature ID registers userspace writable mask.
+ *
  * From DDI0487J.a, D19.2.66 ("ID_AA64MMFR2_EL1, AArch64 Memory Model
  * Feature Register 2"):
  *
@@ -514,8 +515,11 @@ struct kvm_smccc_filter {
  * 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.
+ * This covers all currently known R/O registers that indicate
+ * anything useful feature wise, including the ID registers.
+ *
+ * If we ever need to introduce a new range, it will be described as
+ * such in the range field.
  */
 #define ARM64_FEATURE_ID_SPACE_IDX(op0, op1, crn, crm, op2)            \
        ({                                                              \
@@ -528,7 +532,8 @@ struct kvm_smccc_filter {
 
 struct reg_mask_range {
        __u64 addr;             /* Pointer to mask array */
-       __u64 reserved[7];
+       __u32 range;            /* Requested range */
+       __u32 reserved[13];
 };
 
 #endif
arch/arm64/kvm/arm.c +3 -0
index e08894692829..6ea4d8b0e744 100644
@@ -316,6 +316,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
        case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
                r = kvm_supported_block_sizes();
                break;
+       case KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES:
+               r = BIT(0);
+               break;
        default:
                r = 0;
        }
arch/arm64/kvm/sys_regs.c +3 -2
index 59c590fff4f2..6eadd0fa2c53 100644
@@ -3600,8 +3600,9 @@ int kvm_vm_ioctl_get_reg_writable_masks(struct kvm *kvm, struct reg_mask_range *
        const void *zero_page = page_to_virt(ZERO_PAGE(0));
        u64 __user *masks = (u64 __user *)range->addr;
 
-       /* Only feature id range is supported, reserved[7] must be zero. */
-       if (memcmp(range->reserved, zero_page, sizeof(range->reserved)))
+       /* Only feature id range is supported, reserved[13] must be zero. */
+       if (range->range ||
+           memcmp(range->reserved, zero_page, sizeof(range->reserved)))
                return -EINVAL;
 
        /* Wipe the whole thing first */
include/uapi/linux/kvm.h +1 -1
index 424b6d00440b..f5100055a1a6 100644
@@ -1192,6 +1192,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_COUNTER_OFFSET 227
 #define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228
 #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229
+#define KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES 230
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
 
--
Quoted 1 line

Without deviation from the norm, progress is not possible.

Looks good to me.

Thanks,
Jing

maintainer
#9 lore raw

Re: [PATCH v8 02/11] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Quoted 22 lines · Jing Zhang
Quoted 21 lines · Marc Zyngier
Quoted 6 lines · Cornelia Huck
Quoted 2 lines · Jing Zhang

Maybe it'd be better to leave this to whenever we do need to add other range support?

Quoted 6 lines

My point is: How does userspace figure out if the kernel that is running supports ranges other than id regs? If this is just an insurance against changes that might arrive or not, we can live with the awkward "just try it out" approach; if we think it's likely that we'll need to extend it, we need to add the mechanism for userspace to find out about it now, or it would need to probe for presence of the mechanism...

Agreed. Nothing like the present to address this sort of things. it really doesn't cost much, and I'd rather have it right now.

Here's a vague attempt at an advertising mechanism. If people are OK with it, I can stash that on top of Jing's series.

[...]

Quoted 1 line

Looks good to me.

Well, that's of course conditional on the other comments I made against this series. And we're seriously running out of time...

        M.
Signature · 1 line
Jazz is not dead. It just smells funny...
author
#10 lore raw

PATCH v8 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.41.0.585.gd2178a4bd4-goog
author
#11 lore raw

PATCH v8 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.41.0.585.gd2178a4bd4-goog
author
#12 lore raw

PATCH v8 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 usrespace with this change.

---
 arch/arm64/kvm/sys_regs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -2
index afade7186675..5f6c2be12e44 100644
@@ -2006,7 +2006,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(63, 0), },
 	ID_HIDDEN(ID_AFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -2055,7 +2055,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 = GENMASK(63, 0), },
 	ID_SANITISED(ID_AA64DFR1_EL1),
 	ID_UNALLOCATED(5,2),
 	ID_UNALLOCATED(5,3),
Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
maintainer
#13 lore raw

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

Quoted 3 lines · Jing Zhang

All valid fields in ID_AA64DFR0_EL1 and ID_DFR0_EL1 are writable from usrespace with this change.

nit: userspace

Quoted 11 lines
---
 arch/arm64/kvm/sys_regs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +0 -0
index afade7186675..5f6c2be12e44 100644

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

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2006,7 +2006,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(63, 0), },

For obvious reasons, this cannot be a 64 bit mask...

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

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 +2055,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 = GENMASK(63, 0), },

What is the actual justification to go from "only the PMU version is writable" to "everything is writable"?

Also, what about the RES0 fields?

Thanks,

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

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

Hi Marc,

Quoted 9 lines · Marc Zyngier
Quoted 3 lines · Jing Zhang

All valid fields in ID_AA64DFR0_EL1 and ID_DFR0_EL1 are writable from usrespace with this change.

nit: userspace

Fixed.

Quoted 32 lines
Quoted 11 lines
---
 arch/arm64/kvm/sys_regs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +0 -0
index afade7186675..5f6c2be12e44 100644

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

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2006,7 +2006,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(63, 0), },

For obvious reasons, this cannot be a 64 bit mask...

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

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 +2055,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 = GENMASK(63, 0), },

What is the actual justification to go from "only the PMU version is writable" to "everything is writable"?

Quoted 1 line

Also, what about the RES0 fields?

You're right. We should not mark RES0 fields and those fields KVM hide from userspace as writable.
So, I think all the other fields can be writable?

Quoted 8 lines

Thanks,

        M.

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

Thanks,
Jing

author
#15 lore raw

PATCH v8 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 5f6c2be12e44..879004fd37e5 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.41.0.585.gd2178a4bd4-goog
author
#16 lore raw

PATCH v8 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 usrespace with this change.

---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +1 -1
index 879004fd37e5..392613bec560 100644
@@ -2041,7 +2041,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 = GENMASK(63, 0), },
 	ID_SANITISED(ID_AA64PFR1_EL1),
 	ID_UNALLOCATED(4,2),
 	ID_UNALLOCATED(4,3),
Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
maintainer
#17 lore raw

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

Quoted 3 lines · Jing Zhang

All valid fields in ID_AA64PFR0_EL1 are writable from usrespace with this change.

userspace

Quoted 11 lines
---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +0 -0
index 879004fd37e5..392613bec560 100644

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

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2041,7 +2041,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 = GENMASK(63, 0), },
 	ID_SANITISED(ID_AA64PFR1_EL1),
 	ID_UNALLOCATED(4,2),
 	ID_UNALLOCATED(4,3),

Same remark as the previous patch. What makes it legal to make *everything* writable? For example, we don't expose the AMU. And yet you are telling userspace "sure, go ahead".

Userspace will then try and restore *something*, and will eventually crap itself because the kernel won't allow it.

Why do we bother describing the writable fields if userspace can't write to them?

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

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

Hi Marc,

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

All valid fields in ID_AA64PFR0_EL1 are writable from usrespace with this change.

userspace

Quoted 11 lines
---
 arch/arm64/kvm/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +0 -0
index 879004fd37e5..392613bec560 100644

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

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2041,7 +2041,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 = GENMASK(63, 0), },
      ID_SANITISED(ID_AA64PFR1_EL1),
      ID_UNALLOCATED(4,2),
      ID_UNALLOCATED(4,3),

Same remark as the previous patch. What makes it legal to make *everything* writable? For example, we don't expose the AMU. And yet you are telling userspace "sure, go ahead".

Quoted 5 lines

Userspace will then try and restore *something*, and will eventually crap itself because the kernel won't allow it.

Why do we bother describing the writable fields if userspace can't write to them?

I'll send out another version which wouldn't enable writable masks for RES0 fields and KVM hidden fields.
Does it sound good to you?

Quoted 5 lines
        M.

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

Thanks,
Jing

author
#19 lore raw

PATCH v8 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 | 79 ++++++++++++++++-----------------------
 1 file changed, 33 insertions(+), 46 deletions(-)
arch/arm64/kvm/sys_regs.c +33 -46
index 392613bec560..85b5312bdee6 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) \
+	ID_DESC(name, _set_user, id_visibility, _reset, GENMASK(63, 0))
+#define ID_SANITISED_W(name) \
+	_ID_SANITISED_W(name, set_id_reg, kvm_read_sanitised_id_reg)
+
+/* 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) \
+	ID_DESC(name, _set_user, aa32_id_visibility, _reset, GENMASK(63, 0))
+#define AA32_ID_SANITISED_W(name) \
+	_AA32_ID_SANITISED_W(name, set_id_reg, kvm_read_sanitised_id_reg)
 
 /*
  * 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,
@@ -2001,13 +2004,7 @@ 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(63, 0), },
+	_AA32_ID_SANITISED_W(ID_DFR0_EL1, set_id_dfr0_el1, read_sanitised_id_dfr0_el1),
 	ID_HIDDEN(ID_AFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -2036,12 +2033,7 @@ 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 = GENMASK(63, 0), },
+	_ID_SANITISED_W(ID_AA64PFR0_EL1, set_id_reg, read_sanitised_id_aa64pfr0_el1),
 	ID_SANITISED(ID_AA64PFR1_EL1),
 	ID_UNALLOCATED(4,2),
 	ID_UNALLOCATED(4,3),
@@ -2051,12 +2043,7 @@ 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 = GENMASK(63, 0), },
+	_ID_SANITISED_W(ID_AA64DFR0_EL1, set_id_aa64dfr0_el1, read_sanitised_id_aa64dfr0_el1),
 	ID_SANITISED(ID_AA64DFR1_EL1),
 	ID_UNALLOCATED(5,2),
 	ID_UNALLOCATED(5,3),
Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
author
#20 lore raw

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

---
 arch/arm64/kvm/sys_regs.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
arch/arm64/kvm/sys_regs.c +13 -7
index 85b5312bdee6..59c590fff4f2 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
  *
@@ -2063,10 +2069,10 @@ 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_SANITISED_W(ID_AA64MMFR1_EL1),
+	_ID_SANITISED_W(ID_AA64MMFR2_EL1, set_id_reg, read_sanitised_id_aa64mmfr2_el1),
+	ID_SANITISED_W(ID_AA64MMFR3_EL1),
 	ID_UNALLOCATED(7,4),
 	ID_UNALLOCATED(7,5),
 	ID_UNALLOCATED(7,6),
Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
author
#21 lore raw

PATCH v8 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          |   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/lib/aarch64/processor.c     |    6 +-
 10 files changed, 3078 insertions(+), 664 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 -3
index c692cc86e7da..a8cf0cb04db7 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.41.0.585.gd2178a4bd4-goog

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

#22 lore raw

Re: [PATCH v8 10/11] KVM: arm64: selftests: Import automatic system register definition generation from kernel

Hi Jing,

Quoted 32 lines · Jing Zhang

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

---

tools/testing/selftests/kvm/Makefile +4 -2
index c692cc86e7da..a8cf0cb04db7 100644
@@ -200,14 +200,15 @@ ifeq ($(ARCH),x86_64)
  LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include
  else
  LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
+ARCH_GENERATED_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include/generated
  endif
  CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
-	-Wno-gnu-variable-sized-type-not-at-end -MD\
+	-Wno-gnu-variable-sized-type-not-at-end -MD \
  	-fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \
  	-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
  	-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
  	-I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \
-	$(KHDR_INCLUDES)
+	-I$(ARCH_GENERATED_INCLUDE) $(KHDR_INCLUDES)
  ifeq ($(ARCH),s390)
  	CFLAGS += -march=z10
  endif
@@ -255,8 +256,16 @@ $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S
  $(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c
  	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@
 
+ifeq ($(ARCH),arm64)

tools/testing/selftests/kvm/Makefile $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S · line 268

Quoted 10 lines
tools/testing/selftests/kvm/Makefile +8 -1
+GEN_SYSREGS := $(ARCH_GENERATED_INCLUDE)/asm/sysreg-defs.h
+ARCH_TOOLS := $(top_srcdir)/tools/arch/$(ARCH)/tools/
+
+$(GEN_SYSREGS): $(ARCH_TOOLS)/gen-sysreg.awk $(ARCH_TOOLS)/sysreg
+	mkdir -p $(dir $@); awk -f $(ARCH_TOOLS)/gen-sysreg.awk $(ARCH_TOOLS)/sysreg > $@
+endif
+
  x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))
-$(TEST_GEN_PROGS): $(LIBKVM_OBJS)
+$(TEST_GEN_PROGS): $(LIBKVM_OBJS) $(GEN_SYSREGS)

I don't this this really works. Since the $(GEN_SYSREG) is the prerequisites of $(TEST_GEN_PROGS). Only when $(TEST_GEN_PROGS) being compiled, the $(GEN_SYSREG) can be generated.

But the fact is, the $(TEST_GEN_PROGS) is relies on $(TEST_GEN_OBJ), which means $(TEST_GEN_OBJ) will be compiled before $(TEST_GEN_PROGS), but $(TEST_GEN_OBJ) depends on $(GEN_SYSREG) again, at the time, the $(GEN_SYSREG) hasn't been generated, so it will has error:

No such file or directory.

#include "asm/sysreg-defs.h"

I think the correct way to generate $(GEN_SYSREGS) is add a prerequisite for $(TEST_GEN_OBJ), like:

$(TEST_GEN_OBJ): $(GEN_SYSREGS)

Thanks,
Shaoqin

Quoted 3 lines
tools/testing/selftests/kvm/Makefile +0 -0
  $(TEST_GEN_PROGS_EXTENDED): $(LIBKVM_OBJS)
 
  cscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib ..
Signature · 1 line
Shaoqin
author
#23 lore raw

Re: [PATCH v8 10/11] KVM: arm64: selftests: Import automatic system register definition generation from kernel

Hi Shaoqin,

Quoted 56 lines · Shaoqin Huang

Hi Jing,

Quoted 32 lines · Jing Zhang

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

---

tools/testing/selftests/kvm/Makefile +4 -2
index c692cc86e7da..a8cf0cb04db7 100644
@@ -200,14 +200,15 @@ ifeq ($(ARCH),x86_64)
  LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include
  else
  LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
+ARCH_GENERATED_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include/generated
  endif
  CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
-     -Wno-gnu-variable-sized-type-not-at-end -MD\
+     -Wno-gnu-variable-sized-type-not-at-end -MD \
      -fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \
      -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
      -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
      -I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \
-     $(KHDR_INCLUDES)
+     -I$(ARCH_GENERATED_INCLUDE) $(KHDR_INCLUDES)
  ifeq ($(ARCH),s390)
      CFLAGS += -march=z10
  endif
@@ -255,8 +256,16 @@ $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S
  $(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c
      $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@
 
+ifeq ($(ARCH),arm64)

tools/testing/selftests/kvm/Makefile $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S · line 268

Quoted 10 lines
tools/testing/selftests/kvm/Makefile +8 -1
+GEN_SYSREGS := $(ARCH_GENERATED_INCLUDE)/asm/sysreg-defs.h
+ARCH_TOOLS := $(top_srcdir)/tools/arch/$(ARCH)/tools/
+
+$(GEN_SYSREGS): $(ARCH_TOOLS)/gen-sysreg.awk $(ARCH_TOOLS)/sysreg
+     mkdir -p $(dir $@); awk -f $(ARCH_TOOLS)/gen-sysreg.awk $(ARCH_TOOLS)/sysreg > $@
+endif
+
  x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))
-$(TEST_GEN_PROGS): $(LIBKVM_OBJS)
+$(TEST_GEN_PROGS): $(LIBKVM_OBJS) $(GEN_SYSREGS)

I don't this this really works. Since the $(GEN_SYSREG) is the prerequisites of $(TEST_GEN_PROGS). Only when $(TEST_GEN_PROGS) being compiled, the $(GEN_SYSREG) can be generated.

But the fact is, the $(TEST_GEN_PROGS) is relies on $(TEST_GEN_OBJ), which means $(TEST_GEN_OBJ) will be compiled before $(TEST_GEN_PROGS), but $(TEST_GEN_OBJ) depends on $(GEN_SYSREG) again, at the time, the $(GEN_SYSREG) hasn't been generated, so it will has error:

Quoted 8 lines

No such file or directory.

#include "asm/sysreg-defs.h"

I think the correct way to generate $(GEN_SYSREGS) is add a prerequisite for $(TEST_GEN_OBJ), like:

$(TEST_GEN_OBJ): $(GEN_SYSREGS)

You're right. Fixed.

Quoted 10 lines

Thanks,
Shaoqin

tools/testing/selftests/kvm/Makefile $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S · line 271

Quoted 3 lines
tools/testing/selftests/kvm/Makefile +0 -0
  $(TEST_GEN_PROGS_EXTENDED): $(LIBKVM_OBJS)
 
  cscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib ..

--
Shaoqin

Thanks,
Jing

author
#24 lore raw

PATCH v8 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       | 453 ++++++++++++++++++
 2 files changed, 454 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/aarch64/set_id_regs.c
tools/testing/selftests/kvm/Makefile +1 -0
index a8cf0cb04db7..65da8959fade 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 +453 -0

453 lines not on this page · raw message

Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
#25 lore raw

Re: [PATCH v8 11/11] KVM: arm64: selftests: Test for setting ID register from usersapce

Hi Jing,

Quoted 16 lines · 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       | 453 ++++++++++++++++++
  2 files changed, 454 insertions(+)

create mode 100644 tools/testing/selftests/kvm/aarch64/set_id_regs.c

tools/testing/selftests/kvm/aarch64/set_id_regs.c new file +5 -0
+
+static void test_guest_reg_read(struct kvm_vcpu *vcpu)
+{
+	struct ucall uc;
+	bool done = false;

tools/testing/selftests/kvm/aarch64/set_id_regs.c line 408

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/set_id_regs.c new file +10 -0
+
+	while (!done) {
+		vcpu_run(vcpu);
+
+		switch (get_ucall(vcpu, &uc)) {
+		case UCALL_ABORT:
+			REPORT_GUEST_ASSERT(uc);
+			break;
+		case UCALL_SYNC:
+			uint64_t val;

aarch64/set_id_regs.c:408:4: error: a label can only be part of a statement and a declaration is not a statement.

I can encounter a compiler error at this line. Why not just put the uint64_t at the beginning of the function.

Thanks,
Shaoqin

Quoted 45 lines
tools/testing/selftests/kvm/aarch64/set_id_regs.c new file +45 -0
+
+			/* Make sure the written values are seen by guest */
+			vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(uc.args[2]), &val);
+			ASSERT_EQ(val, uc.args[3]);
+			break;
+		case UCALL_DONE:
+			done = true;
+			break;
+		default:
+			TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
+		}
+	}
+}
+
+int main(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	bool aarch64_only;
+	uint64_t val, el0;
+	int ftr_cnt;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+	/* Check for AARCH64 only system */
+	vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64PFR0_EL1), &val);
+	el0 = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_EL0), val);
+	aarch64_only = (el0 == ID_AA64PFR0_EL1_ELx_64BIT_ONLY);
+
+	ksft_print_header();
+
+	ftr_cnt = ARRAY_SIZE(ftr_id_aa64dfr0_el1) + ARRAY_SIZE(ftr_id_dfr0_el1)
+		  + ARRAY_SIZE(ftr_id_aa64pfr0_el1) + ARRAY_SIZE(ftr_id_aa64mmfr0_el1)
+		  + ARRAY_SIZE(ftr_id_aa64mmfr1_el1) + ARRAY_SIZE(ftr_id_aa64mmfr2_el1)
+		  + ARRAY_SIZE(ftr_id_aa64mmfr3_el1) - ARRAY_SIZE(test_regs);
+
+	ksft_set_plan(ftr_cnt);
+
+	test_user_set_reg(vcpu, aarch64_only);
+	test_guest_reg_read(vcpu);
+
+	kvm_vm_free(vm);
+
+	ksft_finished();
+}
Signature · 1 line
Shaoqin
author
#26 lore raw

Re: [PATCH v8 11/11] KVM: arm64: selftests: Test for setting ID register from usersapce

Hi Shaoqin,

Quoted 33 lines · Shaoqin Huang

Hi Jing,

Quoted 16 lines · 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       | 453 ++++++++++++++++++
  2 files changed, 454 insertions(+)

create mode 100644 tools/testing/selftests/kvm/aarch64/set_id_regs.c

tools/testing/selftests/kvm/aarch64/set_id_regs.c new file +5 -0
+
+static void test_guest_reg_read(struct kvm_vcpu *vcpu)
+{
+     struct ucall uc;
+     bool done = false;

tools/testing/selftests/kvm/aarch64/set_id_regs.c line 408

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/set_id_regs.c new file +10 -0
+
+     while (!done) {
+             vcpu_run(vcpu);
+
+             switch (get_ucall(vcpu, &uc)) {
+             case UCALL_ABORT:
+                     REPORT_GUEST_ASSERT(uc);
+                     break;
+             case UCALL_SYNC:
+                     uint64_t val;

aarch64/set_id_regs.c:408:4: error: a label can only be part of a statement and a declaration is not a statement.

Quoted 2 lines

I can encounter a compiler error at this line. Why not just put the uint64_t at the beginning of the function.

Sure. Will move it to the beginning of the function.

Quoted 44 lines

Thanks,
Shaoqin

Quoted 40 lines
tools/testing/selftests/kvm/aarch64/set_id_regs.c new file +40 -0
+
+                     /* Make sure the written values are seen by guest */
+                     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(uc.args[2]), &val);
+                     ASSERT_EQ(val, uc.args[3]);
+                     break;
+             case UCALL_DONE:
+                     done = true;
+                     break;
+             default:
+                     TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
+             }
+     }
+}
+
+int main(void)
+{
+     struct kvm_vcpu *vcpu;
+     struct kvm_vm *vm;
+     bool aarch64_only;
+     uint64_t val, el0;
+     int ftr_cnt;
+
+     vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+     /* Check for AARCH64 only system */
+     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64PFR0_EL1), &val);
+     el0 = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_EL0), val);
+     aarch64_only = (el0 == ID_AA64PFR0_EL1_ELx_64BIT_ONLY);
+
+     ksft_print_header();
+
+     ftr_cnt = ARRAY_SIZE(ftr_id_aa64dfr0_el1) + ARRAY_SIZE(ftr_id_dfr0_el1)
+               + ARRAY_SIZE(ftr_id_aa64pfr0_el1) + ARRAY_SIZE(ftr_id_aa64mmfr0_el1)
+               + ARRAY_SIZE(ftr_id_aa64mmfr1_el1) + ARRAY_SIZE(ftr_id_aa64mmfr2_el1)
+               + ARRAY_SIZE(ftr_id_aa64mmfr3_el1) - ARRAY_SIZE(test_regs);
+
+     ksft_set_plan(ftr_cnt);
+
+     test_user_set_reg(vcpu, aarch64_only);
+     test_guest_reg_read(vcpu);
Quoted 9 lines

tools/testing/selftests/kvm/aarch64/set_id_regs.c line 453

Quoted 5 lines
tools/testing/selftests/kvm/aarch64/set_id_regs.c new file +5 -0
+
+     kvm_vm_free(vm);
+
+     ksft_finished();
+}

--
Shaoqin

Thanks,
Jing