Skip to contentSkip to conversation

Get writable masks for feature ID from userspace

PATCH In review Jing Zhang 3 participants 9 messages Sep 19, 2023 – Feb 14, 2024

Subsystems (4)
lore
Share & tools
lore

Jing Zhang

author
#0 lore raw

cover Get writable masks for feature ID from userspace

This patch series were part of [1], but actually are independent from that. In this patch series, a VM ioctl is added to allow userspace to get writable masks for feature ID registers.
Another two fixes for ID registers are also included in this series. This is based on v6.6-rc2.

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

---

Jing Zhang (3):
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

Oliver Upton (1):
KVM: arm64: Reject attempts to set invalid debug arch version

 Documentation/virt/kvm/api.rst    |  42 ++++++++++++
 arch/arm64/include/asm/kvm_host.h |   2 +
 arch/arm64/include/uapi/asm/kvm.h |  32 +++++++++
 arch/arm64/kvm/arm.c              |  10 +++
 arch/arm64/kvm/sys_regs.c         | 104 ++++++++++++++++++++++++++++--
 include/uapi/linux/kvm.h          |   2 +
 6 files changed, 186 insertions(+), 6 deletions(-)

base-commit: ce9ecca0238b140b88f43859b211c9fdfd8e5b70

Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
author
#1 lore raw

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

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

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.

---
 arch/arm64/include/asm/kvm_host.h |  2 +
 arch/arm64/include/uapi/asm/kvm.h | 32 +++++++++++++++
 arch/arm64/kvm/arm.c              | 10 +++++
 arch/arm64/kvm/sys_regs.c         | 66 +++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h          |  2 +
 5 files changed, 112 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
index af06ccb7ee34..5e3f778f81db 100644
@@ -1078,6 +1078,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 +32 -0
index f7ddd73a8c0f..01242274db87 100644
@@ -505,6 +505,38 @@ 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 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)		\
+	({								\
+		__u64 __op1 = (op1) & 3;				\
+		__op1 -= (__op1 == 3);					\
+		(__op1 << 6 | ((crm) & 7) << 3 | (op2));		\
+	})
+
+#define ARM64_FEATURE_ID_SPACE_SIZE	(3 * 8 * 8)
+#define ARM64_FEATURE_ID_RANGE_IDREGS	BIT(0)
+
+struct reg_mask_range {
+	__u64 addr;		/* Pointer to mask array */
+	__u32 range;		/* Requested range */
+	__u32 reserved[13];
+};
+
 #endif
 
 #endif /* __ARM_KVM_H__ */
arch/arm64/kvm/arm.c +10 -0
index 4866b3f7b4ea..1c4017a253fd 100644
@@ -317,6 +317,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 = ARM64_FEATURE_ID_RANGE_IDREGS;
+		break;
 	default:
 		r = 0;
 	}
@@ -1629,6 +1632,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 +66 -0

78 lines not on this page · raw message

include/uapi/linux/kvm.h +2 -0
index 13065dd96132..2102df261f1b 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
 
@@ -1562,6 +1563,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.459.ge4e396fd5e-goog
author
#2 lore raw

PATCH 2/4 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 | 42 ++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
Documentation/virt/kvm/api.rst +42 -0
index 21a7578142a1..2defb5e198ce 100644
@@ -6070,6 +6070,48 @@ 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: KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES
+: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)
+        #define ARM64_FEATURE_ID_RANGE_IDREGS	BIT(0)
+
+        struct reg_mask_range {
+                __u64 addr;             /* Pointer to mask array */
+                __u32 range;            /* Requested range */
+                __u32 reserved[13];
+        };
+
+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}.
+
+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 ``ARM64_FEATURE_ID_RANGE_IDREGS``
+is valid 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
 ========================
 
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#3 lore raw

Re: [PATCH v1 2/4] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Hi,

Quoted 30 lines · Jing Zhang

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

---
 Documentation/virt/kvm/api.rst | 42 ++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
Documentation/virt/kvm/api.rst +14 -0
index 21a7578142a1..2defb5e198ce 100644
@@ -6070,6 +6070,48 @@ 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: KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES
+: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)
+        #define ARM64_FEATURE_ID_RANGE_IDREGS	BIT(0)

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

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+
+        struct reg_mask_range {
+                __u64 addr;             /* Pointer to mask array */
+                __u32 range;            /* Requested range */
+                __u32 reserved[13];
+        };
+
+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}.

when attempting a migration between Ampere Altra and ThunderXv2 the first hurdle is to handle a failure when writing ICC_CTLR_EL1 (3.0.12.12.4) on dest. This reg is outside of the scope of the above single range (BIT(0)).

This may be questionable if we want to migrate between those types of machines but the goal is to exercise different scenarios to have a gloval view of the problems.

This reg exposes some RO capabilities such as ExtRange, A3V, SEIS, IDBits, ...
So to get the migration going further I would need to tweek this on the source - for instance I guess SEIS could be reset despite the host HW cap - without making too much trouble.

What would you recommend, adding a new range? But I guess we need to design ranges carefully otherwise we may be quickly limited by the number of flag bits.

Any suggestion?

Thanks

Eric

Quoted 21 lines
Documentation/virt/kvm/api.rst +18 -0
+
+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 ``ARM64_FEATURE_ID_RANGE_IDREGS``
+is valid 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
 ========================
 
maintainer
#4 lore raw

Re: [PATCH v1 2/4] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Hey Eric,

Quoted 40 lines · Eric Auger

Hi,

Quoted 36 lines · Jing Zhang

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

---
 Documentation/virt/kvm/api.rst | 42 ++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
Documentation/virt/kvm/api.rst +20 -0
index 21a7578142a1..2defb5e198ce 100644
@@ -6070,6 +6070,48 @@ 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: KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES
+: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)
+        #define ARM64_FEATURE_ID_RANGE_IDREGS	BIT(0)
+
+        struct reg_mask_range {
+                __u64 addr;             /* Pointer to mask array */
+                __u32 range;            /* Requested range */
+                __u32 reserved[13];
+        };
Quoted 8 lines

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

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

when attempting a migration between Ampere Altra and ThunderXv2 the first hurdle is to handle a failure when writing ICC_CTLR_EL1 (3.0.12.12.4) on dest. This reg is outside of the scope of the above single range (BIT(0)).

Indeed. But more importantly, this isn't really an ID register. Plenty of variable bits in there.

Quoted 3 lines

This may be questionable if we want to migrate between those types of machines but the goal is to exercise different scenarios to have a gloval view of the problems.

I think this is a valuable experiment, and we should definitely explore this sort of things (as I cannot see the diversity of ARM system slowing down any time soon).

Quoted 6 lines

This reg exposes some RO capabilities such as ExtRange, A3V, SEIS, IDBits, ...
So to get the migration going further I would need to tweek this on the source - for instance I guess SEIS could be reset despite the host HW cap - without making too much trouble.

I'm not sure SEIS is such an easy one: if you promised the guest that it would never get an SError doing the most stupid things (SEIS=0), it really shouldn't get one after migration. If you advertised it on the source HW (Altra), a migration to TX2 would be fine.

The other bits are possible to change depending on the requirements of the VM (aff3, IDBits), and ExtRange should always be set to 0 (because our GIC implementation doesn't support EPPI/ESPI).

The really ugly part here is that if you want to affect these bits, you will need to trap and emulate the access. Not a big deal, but in the absence of FGT, you will need to handle the full Common trap group, which is going to slow things down (you will have to trap ICV_PMR_EL1, for example).

Quoted 3 lines

What would you recommend, adding a new range? But I guess we need to design ranges carefully otherwise we may be quickly limited by the number of flag bits.

I can see a need to adding a range that would cover non-ID registers that have RO fields. But we also need to consider the case of EL2 registers that take part in this.

For example, ICV_CTLR_EL1 and ICH_VTR_EL2 and deeply linked, and share some fields. Without NV, no need to expose HCR_VTR_EL2. With NV, this register actually drives ICV_CTLR_EL1.

So careful planning is required here, and the impact of this measured.

Thanks,

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

Re: [PATCH v1 2/4] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Hi Marc,

Quoted 71 lines · Marc Zyngier

Hey Eric,

Quoted 40 lines · Eric Auger

Hi,

Quoted 36 lines · Jing Zhang

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

---
 Documentation/virt/kvm/api.rst | 42 ++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
Documentation/virt/kvm/api.rst +20 -0
index 21a7578142a1..2defb5e198ce 100644
@@ -6070,6 +6070,48 @@ 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: KVM_CAP_ARM_SUPPORTED_FEATURE_ID_RANGES
+: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)
+        #define ARM64_FEATURE_ID_RANGE_IDREGS	BIT(0)
+
+        struct reg_mask_range {
+                __u64 addr;             /* Pointer to mask array */
+                __u32 range;            /* Requested range */
+                __u32 reserved[13];
+        };
Quoted 8 lines

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

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

when attempting a migration between Ampere Altra and ThunderXv2 the first hurdle is to handle a failure when writing ICC_CTLR_EL1 (3.0.12.12.4) on dest. This reg is outside of the scope of the above single range (BIT(0)).

Indeed. But more importantly, this isn't really an ID register. Plenty of variable bits in there.

Quoted 3 lines

This may be questionable if we want to migrate between those types of machines but the goal is to exercise different scenarios to have a gloval view of the problems.

I think this is a valuable experiment, and we should definitely explore this sort of things (as I cannot see the diversity of ARM system slowing down any time soon).

Quoted 6 lines

This reg exposes some RO capabilities such as ExtRange, A3V, SEIS,hyp/nvhe/hyp-main.c IDBits, ...
So to get the migration going further I would need to tweek this on the source - for instance I guess SEIS could be reset despite the host HW cap - without making too much trouble.

Quoted 4 lines

I'm not sure SEIS is such an easy one: if you promised the guest that it would never get an SError doing the most stupid things (SEIS=0), it really shouldn't get one after migration. If you advertised it on the source HW (Altra), a migration to TX2 would be fine.

I see. Indeed for sure we must make sure KVM cannot inject SEIs in the guest if SEIS is not advertised on guest side.

In this case SEIS is 0 on src Altra. On dst TX2 ich_vtr_el2 advertises it and host seis =1 causing set_gic_ctlr to fail (vgic-sys-reg-v3.c).

Quoted 10 lines

The other bits are possible to change depending on the requirements of the VM (aff3, IDBits), and ExtRange should always be set to 0 (because our GIC implementation doesn't support EPPI/ESPI).

The really ugly part here is that if you want to affect these bits, you will need to trap and emulate the access. Not a big deal, but in the absence of FGT, you will need to handle the full Common trap group, which is going to slow things down (you will have to trap ICV_PMR_EL1, for example).

Would it be sensible to simplify things and only support the new range if FGT is supported?

Quoted 8 lines
Quoted 4 lines

What would you recommend, adding a new range? But I guess we need to design ranges carefully otherwise we may be quickly limited by the number of flag bits.

I can see a need to adding a range that would cover non-ID registers that have RO fields. But we also need to consider the case of EL2 registers that take part in this.

Yes I need to better understand the consistency with ICH_VTR_EL2

Quoted 4 lines

For example, ICV_CTLR_EL1 and ICH_VTR_EL2 and deeply linked, and share some fields. Without NV, no need to expose HCR_VTR_EL2. With NV, this register actually drives ICV_CTLR_EL1.

understood.

Quoted 2 lines

So careful planning is required here, and the impact of this measured.

Understood. More time needed to study the code ;-)

Thanks!

Eric

Quoted 5 lines

Thanks,

	M.
maintainer
#6 lore raw

Re: [PATCH v1 2/4] KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS

Hi Eric,

Quoted 7 lines · Eric Auger
Quoted 5 lines

I'm not sure SEIS is such an easy one: if you promised the guest that it would never get an SError doing the most stupid things (SEIS=0), it really shouldn't get one after migration. If you advertised it on the source HW (Altra), a migration to TX2 would be fine.

I see. Indeed for sure we must make sure KVM cannot inject SEIs in the guest if SEIS is not advertised on guest side.

The problem isn't KVM injecting an SError, but the HW doing it (that's what SEIS indicates). On some HW, it only takes an old enough EFI build to trigger those.

Quoted 8 lines

In this case SEIS is 0 on src Altra. On dst TX2 ich_vtr_el2 advertises it and host seis =1 causing set_gic_ctlr to fail (vgic-sys-reg-v3.c).

Quoted 5 lines

The other bits are possible to change depending on the requirements of the VM (aff3, IDBits), and ExtRange should always be set to 0 (because our GIC implementation doesn't support EPPI/ESPI).

Quoted 7 lines
Quoted 5 lines

The really ugly part here is that if you want to affect these bits, you will need to trap and emulate the access. Not a big deal, but in the absence of FGT, you will need to handle the full Common trap group, which is going to slow things down (you will have to trap ICV_PMR_EL1, for example).

Would it be sensible to simplify things and only support the new range if FGT is supported?

I'm not sure that helps. The only FGT that covers the GIC is for ICC_IGRPENn_EL1, and we don't care much about that guy. So ICH_VMCR_EL2.TC it is, and that sucks a bit. But if that's what you want to show, you don't have a choice.

Thanks,

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

PATCH 3/4 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 cdb9976d8091..4dcc9272fbb8 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))) {
@@ -2445,8 +2445,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.459.ge4e396fd5e-goog
author
#8 lore raw

PATCH 4/4 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 4dcc9272fbb8..fdebd9d042c3 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)
@@ -1476,14 +1482,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.
@@ -1503,6 +1517,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);
 
 	/*
@@ -1522,6 +1537,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);
 }
 
@@ -1543,6 +1565,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;
@@ -1558,6 +1581,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.459.ge4e396fd5e-goog