Skip to contentSkip to conversation

KVM: arm64: Revamp Fine Grained Trap handling

PATCH Superseded Marc Zyngier 3 participants 30 messages Feb 10, 2025 – Mar 11, 2025

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

cover KVM: arm64: Revamp Fine Grained Trap handling

Mark recently mentioned that the way we handled FGTs, and particularly their RES0 behaviour was not entirely future-proof.

The main issue is that RES0 bits are extracted from the sysreg file, which is independently updated, and that the KVM-specific enablement is not necessarily done at the same time. This means that a bit that KVM considered RES0 at some point all of a sudden acquires a meaning, and that this can trigger unexpected behaviours.

One way of fixing that would be to mandate that everything gets updated synchronously. While this is something I'd really want to see, it is unlikely to happen within my lifetime.

So the next best option is to reintroduce KVM's own view of RES0 masks, so that it can continue to ignore a given feature in a safe manner. But instead of going back to what we have a while back in the form of hand-crafted masks that are likely to be wrong, this series makes use of the rather exhaustive description of traps that we know about, and then some more:

- compile the encoding_to_fgt[] array into individual positive, negative, and res0 masks

- use these masks in place of anything that is hardcoded

- fix a few bugs along the way (thanks Mark!)

- perform some validation and let users know that KVM may be missing some FGT bit definitions

But it would be foolish to stop here. We also use FGTs to implement the so called "Fine Grained UNDEF" (FGU) bits, and make sure that system registers that are not exposed to a guest do UNDEF. This means evaluating the guest configuration and setting up these FGU bits when said configuration isn't supported.

This series therefore goes one step further and, for the HFG*TR_EL2, HDFG*TR_EL2, and HAFGRTR_EL2 registers, define which bit is controlled by which feature. This is done mechanically, with very minimal human intervention, by extracting the relevant data from the ARM-released JSON files.

With that data, we greatly simplify the dependency between FGUs and features, as no code needs to be written. We also use the same data to set the RES0 bits for any register that requires sanitisation (the VNCR-backed registers, for example).

Overall, and while this is a lot of new LoCs, it is IMO a reduction in complexity and maintenance burden. I also have additional patches addressing HCRX_EL2 and HCR_EL2, but this is already a large series and I'd like some feedback on it.

This series is atop of 6.14-rc2, and contains bits and pieces of FEAT_LS64 support thanks to the newly added HFGITR_EL2.PSBCSYNC bit sharing an EC with LS64...

Marc Zyngier (17):
arm64: Add ID_AA64ISAR1_EL1.LS64 encoding for FEAT_LS64WB
arm64: Add syndrome information for trapped LD64B/ST64B{,V,V0}
KVM: arm64: Handle trapping of FEAT_LS64* instructions
KVM: arm64: Restrict ACCDATA_EL1 undef to FEAT_ST64_ACCDATA being
disabled
KVM: arm64: Don't treat HCRX_EL2 as a FGT register
KVM: arm64: Plug FEAT_GCS handling
KVM: arm64: Compute FGT masks from KVM's own FGT tables
KVM: arm64: Add description of FGT bits leading to EC!=0x18
KVM: arm64: Use computed masks as sanitisers for FGT registers
KVM: arm64: Propagate FGT masks to the nVHE hypervisor
KVM: arm64: Use computed FGT masks to setup FGT registers
KVM: arm64: Remove most hand-crafted masks for FGT registers
KVM: arm64: Use KVM-specific HCRX_EL2 RES0 mask
KVM: arm64: Handle PSB CSYNC traps
KVM: arm64: Switch to table-driven FGU configuration
KVM: arm64: Validate FGT register descriptions against RES0 masks
KVM: arm64: Use FGT feature maps to drive RES0 bits

Mark Rutland (1):
KVM: arm64: Unconditionally configure fine-grain traps

 arch/arm64/include/asm/esr.h            |   9 +-
 arch/arm64/include/asm/kvm_arm.h        |  64 +--
 arch/arm64/include/asm/kvm_host.h       |  25 +
 arch/arm64/kvm/Makefile                 |   2 +-
 arch/arm64/kvm/arm.c                    |   8 +
 arch/arm64/kvm/config.c                 | 614 ++++++++++++++++++++++++
 arch/arm64/kvm/emulate-nested.c         | 140 +++++-
 arch/arm64/kvm/handle_exit.c            |  81 ++++
 arch/arm64/kvm/hyp/include/hyp/switch.h | 110 ++---
 arch/arm64/kvm/hyp/nvhe/switch.c        |   7 +
 arch/arm64/kvm/nested.c                 | 137 +-----
 arch/arm64/kvm/sys_regs.c               |  65 +--
 arch/arm64/tools/sysreg                 |   3 +-
 13 files changed, 976 insertions(+), 289 deletions(-)
 create mode 100644 arch/arm64/kvm/config.c
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH 1/18 arm64: Add ID_AA64ISAR1_EL1.LS64 encoding for FEAT_LS64WB

The 2024 extensions are adding yet another variant of LS64 (aptly named FEAT_LS64WB) supporting LS64 accesses to write-back memory, as well as 32 byte single-copy atomic accesses using pairs of FP registers.

Add the relevant encoding to ID_AA64ISAR1_EL1.LS64.

---
 arch/arm64/tools/sysreg | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/tools/sysreg +1 -0
index 762ee084b37c5..8c4229b34840f 100644
@@ -1466,6 +1466,7 @@ UnsignedEnum	63:60	LS64
 	0b0001	LS64
 	0b0010	LS64_V
 	0b0011	LS64_ACCDATA
+	0b0100	LS64WB
 EndEnum
 UnsignedEnum	59:56	XS
 	0b0000	NI
Signature · 1 line
2.39.2
author maintainer
#2 lore raw

PATCH 2/18 arm64: Add syndrome information for trapped LD64B/ST64B{,V,V0}

In reply to Marc Zyngier

Provide the architected EC and ISS values for all the FEAT_LS64* instructions.

---
 arch/arm64/include/asm/esr.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/arm64/include/asm/esr.h +7 -1
index d1b1a33f9a8b0..d5c2fac21a16c 100644
@@ -20,7 +20,8 @@
 #define ESR_ELx_EC_FP_ASIMD	UL(0x07)
 #define ESR_ELx_EC_CP10_ID	UL(0x08)	/* EL2 only */
 #define ESR_ELx_EC_PAC		UL(0x09)	/* EL2 and above */
-/* Unallocated EC: 0x0A - 0x0B */
+#define ESR_ELx_EC_LS64B	UL(0x0A)
+/* Unallocated EC: 0x0B */
 #define ESR_ELx_EC_CP14_64	UL(0x0C)
 #define ESR_ELx_EC_BTI		UL(0x0D)
 #define ESR_ELx_EC_ILL		UL(0x0E)
@@ -174,6 +175,11 @@
 #define ESR_ELx_WFx_ISS_WFE	(UL(1) << 0)
 #define ESR_ELx_xVC_IMM_MASK	((UL(1) << 16) - 1)
 
+/* ISS definitions for LD64B/ST64B instructions */
+#define ESR_ELx_ISS_ST64BV	(0)
+#define ESR_ELx_ISS_ST64BV0	(1)
+#define ESR_ELx_ISS_LDST64B	(2)
+
 #define DISR_EL1_IDS		(UL(1) << 24)
 /*
  * DISR_EL1 and ESR_ELx share the bottom 13 bits, but the RES0 bits may mean
Signature · 1 line
2.39.2
reviewer
#3 lore raw

Re: [PATCH 02/18] arm64: Add syndrome information for trapped LD64B/ST64B{,V,V0}

Quoted 13 lines · Marc Zyngier

Provide the architected EC and ISS values for all the FEAT_LS64* instructions.

---
 arch/arm64/include/asm/esr.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/arm64/include/asm/esr.h +0 -0
index d1b1a33f9a8b0..d5c2fac21a16c 100644

arch/arm64/include/asm/esr.h line 23

Quoted 5 lines
arch/arm64/include/asm/esr.h +1 -1
@@ -20,7 +20,8 @@
 #define ESR_ELx_EC_FP_ASIMD	UL(0x07)
 #define ESR_ELx_EC_CP10_ID	UL(0x08)	/* EL2 only */
 #define ESR_ELx_EC_PAC		UL(0x09)	/* EL2 and above */
-/* Unallocated EC: 0x0A - 0x0B */
+#define ESR_ELx_EC_LS64B	UL(0x0A)

This EC code has been generalised recently. In the latest ARM ARM (ARM DDI 0487 L.a), which can be found at:

https://developer.arm.com/documentation/ddi0487/la/?lang=en

... the table on page D24-7333 refers to it as:

| Trapped execution of any instruction not covered by other EC values.

... and the corresponding ISS description is named:

| ISS encoding for an exception from any other instruction

... so maybe it makes sense to call it 'ESR_ELx_EC_OTHER_INSN', 'ESR_ELx_EC_INSN_MISC', or something of that rough shape?

With that, the PSB CSYNC oddity in patch 15 makes a bit more sense, though the L.a release of the ARM ARM is still missing the description of that.

Mark.

Quoted 19 lines
arch/arm64/include/asm/esr.h +6 -0
+/* Unallocated EC: 0x0B */
 #define ESR_ELx_EC_CP14_64	UL(0x0C)
 #define ESR_ELx_EC_BTI		UL(0x0D)
 #define ESR_ELx_EC_ILL		UL(0x0E)
@@ -174,6 +175,11 @@
 #define ESR_ELx_WFx_ISS_WFE	(UL(1) << 0)
 #define ESR_ELx_xVC_IMM_MASK	((UL(1) << 16) - 1)
 
+/* ISS definitions for LD64B/ST64B instructions */
+#define ESR_ELx_ISS_ST64BV	(0)
+#define ESR_ELx_ISS_ST64BV0	(1)
+#define ESR_ELx_ISS_LDST64B	(2)
+
 #define DISR_EL1_IDS		(UL(1) << 24)
 /*
  * DISR_EL1 and ESR_ELx share the bottom 13 bits, but the RES0 bits may mean
Signature · 1 line
2.39.2
author maintainer
#4 lore raw

PATCH 3/18 KVM: arm64: Handle trapping of FEAT_LS64* instructions

In reply to Marc Zyngier

We generally don't expect FEAT_LS64* instructions to trap, unless they are trapped by a guest hypervisor.

Otherwise, this is just the guest playing tricks on us by using an instruction that isn't advertised, which we handle with a well deserved UNDEF.

---
 arch/arm64/kvm/handle_exit.c | 64 ++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
arch/arm64/kvm/handle_exit.c +64 -0
index 512d152233ff2..4f8354bf7dc5f 100644
@@ -294,6 +294,69 @@ static int handle_svc(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_ls64b(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 esr = kvm_vcpu_get_esr(vcpu);
+	u64 iss = ESR_ELx_ISS(esr);
+	bool allowed;
+
+	switch (iss) {
+	case ESR_ELx_ISS_ST64BV:
+		allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V);
+		break;
+	case ESR_ELx_ISS_ST64BV0:
+		allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_ACCDATA);
+		break;
+	case ESR_ELx_ISS_LDST64B:
+		allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64);
+		break;
+	default:
+		/* Clearly, we're missing something. */
+		goto unknown_trap;
+	}
+
+	if (!allowed)
+		goto undef;
+
+	if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
+		u64 hcrx = __vcpu_sys_reg(vcpu, HCRX_EL2);
+		bool fwd;
+
+		switch (iss) {
+		case ESR_ELx_ISS_ST64BV:
+			fwd = !(hcrx & HCRX_EL2_EnASR);
+			break;
+		case ESR_ELx_ISS_ST64BV0:
+			fwd = !(hcrx & HCRX_EL2_EnAS0);
+			break;
+		case ESR_ELx_ISS_LDST64B:
+			fwd = !(hcrx & HCRX_EL2_EnALS);
+			break;
+		default:
+			/* We don't expect to be here */
+			fwd = false;
+		}
+
+		if (fwd) {
+			kvm_inject_nested_sync(vcpu, esr);
+			return 1;
+		}
+	}
+
+unknown_trap:
+	/*
+	 * If we land here, something must be very wrong, because we
+	 * have no idea why we trapped at all. Warn and undef as a
+	 * fallback.
+	 */
+	WARN_ON(1);
+
+undef:
+	kvm_inject_undefined(vcpu);
+	return 1;
+}
+
 static exit_handle_fn arm_exit_handlers[] = {
 	[0 ... ESR_ELx_EC_MAX]	= kvm_handle_unknown_ec,
 	[ESR_ELx_EC_WFx]	= kvm_handle_wfx,
@@ -303,6 +366,7 @@ static exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_CP14_LS]	= kvm_handle_cp14_load_store,
 	[ESR_ELx_EC_CP10_ID]	= kvm_handle_cp10_id,
 	[ESR_ELx_EC_CP14_64]	= kvm_handle_cp14_64,
+	[ESR_ELx_EC_LS64B]	= handle_ls64b,
 	[ESR_ELx_EC_HVC32]	= handle_hvc,
 	[ESR_ELx_EC_SMC32]	= handle_smc,
 	[ESR_ELx_EC_HVC64]	= handle_hvc,
Signature · 1 line
2.39.2
reviewer
#5 lore raw

Re: [PATCH 03/18] KVM: arm64: Handle trapping of FEAT_LS64* instructions

Quoted 17 lines · Marc Zyngier

We generally don't expect FEAT_LS64* instructions to trap, unless they are trapped by a guest hypervisor.

Otherwise, this is just the guest playing tricks on us by using an instruction that isn't advertised, which we handle with a well deserved UNDEF.

---
 arch/arm64/kvm/handle_exit.c | 64 ++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
arch/arm64/kvm/handle_exit.c +0 -0
index 512d152233ff2..4f8354bf7dc5f 100644

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 297

Quoted 4 lines
arch/arm64/kvm/handle_exit.c +1 -0
@@ -294,6 +294,69 @@ static int handle_svc(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_ls64b(struct kvm_vcpu *vcpu)

Structurally this looks good. As noted on patch 2, I think that naming-wise this should be more general, e.g. handle_other_insn().

Mark.

Quoted 76 lines
arch/arm64/kvm/handle_exit.c +63 -0
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 esr = kvm_vcpu_get_esr(vcpu);
+	u64 iss = ESR_ELx_ISS(esr);
+	bool allowed;
+
+	switch (iss) {
+	case ESR_ELx_ISS_ST64BV:
+		allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V);
+		break;
+	case ESR_ELx_ISS_ST64BV0:
+		allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_ACCDATA);
+		break;
+	case ESR_ELx_ISS_LDST64B:
+		allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64);
+		break;
+	default:
+		/* Clearly, we're missing something. */
+		goto unknown_trap;
+	}
+
+	if (!allowed)
+		goto undef;
+
+	if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
+		u64 hcrx = __vcpu_sys_reg(vcpu, HCRX_EL2);
+		bool fwd;
+
+		switch (iss) {
+		case ESR_ELx_ISS_ST64BV:
+			fwd = !(hcrx & HCRX_EL2_EnASR);
+			break;
+		case ESR_ELx_ISS_ST64BV0:
+			fwd = !(hcrx & HCRX_EL2_EnAS0);
+			break;
+		case ESR_ELx_ISS_LDST64B:
+			fwd = !(hcrx & HCRX_EL2_EnALS);
+			break;
+		default:
+			/* We don't expect to be here */
+			fwd = false;
+		}
+
+		if (fwd) {
+			kvm_inject_nested_sync(vcpu, esr);
+			return 1;
+		}
+	}
+
+unknown_trap:
+	/*
+	 * If we land here, something must be very wrong, because we
+	 * have no idea why we trapped at all. Warn and undef as a
+	 * fallback.
+	 */
+	WARN_ON(1);
+
+undef:
+	kvm_inject_undefined(vcpu);
+	return 1;
+}
+
 static exit_handle_fn arm_exit_handlers[] = {
 	[0 ... ESR_ELx_EC_MAX]	= kvm_handle_unknown_ec,
 	[ESR_ELx_EC_WFx]	= kvm_handle_wfx,
@@ -303,6 +366,7 @@ static exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_CP14_LS]	= kvm_handle_cp14_load_store,
 	[ESR_ELx_EC_CP10_ID]	= kvm_handle_cp10_id,
 	[ESR_ELx_EC_CP14_64]	= kvm_handle_cp14_64,
+	[ESR_ELx_EC_LS64B]	= handle_ls64b,
 	[ESR_ELx_EC_HVC32]	= handle_hvc,
 	[ESR_ELx_EC_SMC32]	= handle_smc,
 	[ESR_ELx_EC_HVC64]	= handle_hvc,
Signature · 1 line
2.39.2
#6 lore raw

Re: [PATCH 03/18] KVM: arm64: Handle trapping of FEAT_LS64* instructions

In reply to Marc Zyngier

Hi Marc,

Quoted 68 lines · Marc Zyngier

We generally don't expect FEAT_LS64* instructions to trap, unless they are trapped by a guest hypervisor.

Otherwise, this is just the guest playing tricks on us by using an instruction that isn't advertised, which we handle with a well deserved UNDEF.

---
 arch/arm64/kvm/handle_exit.c | 64 ++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
arch/arm64/kvm/handle_exit.c +47 -0
index 512d152233ff2..4f8354bf7dc5f 100644
@@ -294,6 +294,69 @@ static int handle_svc(struct kvm_vcpu *vcpu)
        return 1;
 }
 
+static int handle_ls64b(struct kvm_vcpu *vcpu)
+{
+       struct kvm *kvm = vcpu->kvm;
+       u64 esr = kvm_vcpu_get_esr(vcpu);
+       u64 iss = ESR_ELx_ISS(esr);
+       bool allowed;
+
+       switch (iss) {
+       case ESR_ELx_ISS_ST64BV:
+               allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V);
+               break;
+       case ESR_ELx_ISS_ST64BV0:
+               allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_ACCDATA);
+               break;
+       case ESR_ELx_ISS_LDST64B:
+               allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64);
+               break;
+       default:
+               /* Clearly, we're missing something. */
+               goto unknown_trap;
+       }
+
+       if (!allowed)
+               goto undef;
+
+       if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
+               u64 hcrx = __vcpu_sys_reg(vcpu, HCRX_EL2);
+               bool fwd;
+
+               switch (iss) {
+               case ESR_ELx_ISS_ST64BV:
+                       fwd = !(hcrx & HCRX_EL2_EnASR);
+                       break;
+               case ESR_ELx_ISS_ST64BV0:
+                       fwd = !(hcrx & HCRX_EL2_EnAS0);
+                       break;
+               case ESR_ELx_ISS_LDST64B:
+                       fwd = !(hcrx & HCRX_EL2_EnALS);
+                       break;
+               default:
+                       /* We don't expect to be here */
+                       fwd = false;
+               }
+
+               if (fwd) {
+                       kvm_inject_nested_sync(vcpu, esr);
+                       return 1;

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 353

Quoted 10 lines
arch/arm64/kvm/handle_exit.c +10 -0
+               }
+       }
+
+unknown_trap:
+       /*
+        * If we land here, something must be very wrong, because we
+        * have no idea why we trapped at all. Warn and undef as a
+        * fallback.
+        */
+       WARN_ON(1);

nit: should this be WARN_ONCE() instead?

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 358

Quoted 5 lines
arch/arm64/kvm/handle_exit.c +5 -0
+
+undef:
+       kvm_inject_undefined(vcpu);
+       return 1;
+}

I'm wondering if this can be simplified by having one switch() statement that toggles both allowed and fwd (or maybe even only fwd), and then inject depending on that, e.g.,

+static int handle_ls64b(struct kvm_vcpu *vcpu)
+{

+    struct kvm *kvm = vcpu->kvm;
+    bool is_nv = vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu);
+    u64 hcrx = __vcpu_sys_reg(vcpu, HCRX_EL2);
+    u64 esr = kvm_vcpu_get_esr(vcpu);
+    u64 iss = ESR_ELx_ISS(esr);
+    bool fwd = false;

+
+ switch (iss) {
+ case ESR_ELx_ISS_ST64BV:
+ fwd = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V) &&
+ !(hcrx & HCRX_EL2_EnASR)
+ break;
...
+ default:
+ WARN_ONCE(1);
+ }
+
+ if (is_nv && fwd) {
+ kvm_inject_nested_sync(vcpu, esr);
+ else
+ kvm_inject_undefined(vcpu);
+
+ return 1;
+}

I think this has the same effect as the code above.

Cheers,
/fuad

Quoted 15 lines
arch/arm64/kvm/handle_exit.c +2 -1
+
 static exit_handle_fn arm_exit_handlers[] = {
        [0 ... ESR_ELx_EC_MAX]  = kvm_handle_unknown_ec,
        [ESR_ELx_EC_WFx]        = kvm_handle_wfx,
@@ -303,6 +366,7 @@ static exit_handle_fn arm_exit_handlers[] = {
        [ESR_ELx_EC_CP14_LS]    = kvm_handle_cp14_load_store,
        [ESR_ELx_EC_CP10_ID]    = kvm_handle_cp10_id,
        [ESR_ELx_EC_CP14_64]    = kvm_handle_cp14_64,
+       [ESR_ELx_EC_LS64B]      = handle_ls64b,
        [ESR_ELx_EC_HVC32]      = handle_hvc,
        [ESR_ELx_EC_SMC32]      = handle_smc,
        [ESR_ELx_EC_HVC64]      = handle_hvc,
--

2.39.2

author maintainer
#7 lore raw

Re: [PATCH 03/18] KVM: arm64: Handle trapping of FEAT_LS64* instructions

Hi Fuad,

Quoted 122 lines · Fuad Tabba

Hi Marc,

Quoted 68 lines · Marc Zyngier

We generally don't expect FEAT_LS64* instructions to trap, unless they are trapped by a guest hypervisor.

Otherwise, this is just the guest playing tricks on us by using an instruction that isn't advertised, which we handle with a well deserved UNDEF.

---
 arch/arm64/kvm/handle_exit.c | 64 ++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
arch/arm64/kvm/handle_exit.c +47 -0
index 512d152233ff2..4f8354bf7dc5f 100644
@@ -294,6 +294,69 @@ static int handle_svc(struct kvm_vcpu *vcpu)
        return 1;
 }
 
+static int handle_ls64b(struct kvm_vcpu *vcpu)
+{
+       struct kvm *kvm = vcpu->kvm;
+       u64 esr = kvm_vcpu_get_esr(vcpu);
+       u64 iss = ESR_ELx_ISS(esr);
+       bool allowed;
+
+       switch (iss) {
+       case ESR_ELx_ISS_ST64BV:
+               allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V);
+               break;
+       case ESR_ELx_ISS_ST64BV0:
+               allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_ACCDATA);
+               break;
+       case ESR_ELx_ISS_LDST64B:
+               allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64);
+               break;
+       default:
+               /* Clearly, we're missing something. */
+               goto unknown_trap;
+       }
+
+       if (!allowed)
+               goto undef;
+
+       if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
+               u64 hcrx = __vcpu_sys_reg(vcpu, HCRX_EL2);
+               bool fwd;
+
+               switch (iss) {
+               case ESR_ELx_ISS_ST64BV:
+                       fwd = !(hcrx & HCRX_EL2_EnASR);
+                       break;
+               case ESR_ELx_ISS_ST64BV0:
+                       fwd = !(hcrx & HCRX_EL2_EnAS0);
+                       break;
+               case ESR_ELx_ISS_LDST64B:
+                       fwd = !(hcrx & HCRX_EL2_EnALS);
+                       break;
+               default:
+                       /* We don't expect to be here */
+                       fwd = false;
+               }
+
+               if (fwd) {
+                       kvm_inject_nested_sync(vcpu, esr);
+                       return 1;

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 353

Quoted 10 lines
arch/arm64/kvm/handle_exit.c +10 -0
+               }
+       }
+
+unknown_trap:
+       /*
+        * If we land here, something must be very wrong, because we
+        * have no idea why we trapped at all. Warn and undef as a
+        * fallback.
+        */
+       WARN_ON(1);

nit: should this be WARN_ONCE() instead?

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 358

Quoted 5 lines
arch/arm64/kvm/handle_exit.c +5 -0
+
+undef:
+       kvm_inject_undefined(vcpu);
+       return 1;
+}

I'm wondering if this can be simplified by having one switch() statement that toggles both allowed and fwd (or maybe even only fwd), and then inject depending on that, e.g.,

arch/arm64/kvm/handle_exit.c +25 -0
+static int handle_ls64b(struct kvm_vcpu *vcpu)
+{
+    struct kvm *kvm = vcpu->kvm;
+    bool is_nv = vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu);
+    u64 hcrx = __vcpu_sys_reg(vcpu, HCRX_EL2);
+    u64 esr = kvm_vcpu_get_esr(vcpu);
+    u64 iss = ESR_ELx_ISS(esr);
+    bool fwd = false;
+
+    switch (iss) {
+    case ESR_ELx_ISS_ST64BV:
+         fwd = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V) &&
+                   !(hcrx & HCRX_EL2_EnASR)
+         break;
...
+    default:
+        WARN_ONCE(1);
+ }
+
+    if (is_nv && fwd) {
+        kvm_inject_nested_sync(vcpu, esr);
+    else
+        kvm_inject_undefined(vcpu);
+
+    return 1;
+}
Quoted 1 line

I think this has the same effect as the code above.

Yeah, I think something like that could work. I initially was trying to handle all 4 states (allowed, fwd), but obviously some states do not exist (you don't forward something that isn't allowed).

My only concern here is that we don't have clear rules on the init of EL2 guest registers such as HCRX_EL2 when !NV, but that's something that can be easily done (or even worked around locally).

Maybe we should introduce the dreaded notion of "effective value" for EL2 registers when NV is not enabled...

Thanks,

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

Re: [PATCH 03/18] KVM: arm64: Handle trapping of FEAT_LS64* instructions

In reply to Fuad Tabba
Quoted 98 lines · Fuad Tabba

Hi Marc,

Quoted 68 lines · Marc Zyngier

We generally don't expect FEAT_LS64* instructions to trap, unless they are trapped by a guest hypervisor.

Otherwise, this is just the guest playing tricks on us by using an instruction that isn't advertised, which we handle with a well deserved UNDEF.

---
 arch/arm64/kvm/handle_exit.c | 64 ++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
arch/arm64/kvm/handle_exit.c +47 -0
index 512d152233ff2..4f8354bf7dc5f 100644
@@ -294,6 +294,69 @@ static int handle_svc(struct kvm_vcpu *vcpu)
        return 1;
 }
 
+static int handle_ls64b(struct kvm_vcpu *vcpu)
+{
+       struct kvm *kvm = vcpu->kvm;
+       u64 esr = kvm_vcpu_get_esr(vcpu);
+       u64 iss = ESR_ELx_ISS(esr);
+       bool allowed;
+
+       switch (iss) {
+       case ESR_ELx_ISS_ST64BV:
+               allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V);
+               break;
+       case ESR_ELx_ISS_ST64BV0:
+               allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_ACCDATA);
+               break;
+       case ESR_ELx_ISS_LDST64B:
+               allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64);
+               break;
+       default:
+               /* Clearly, we're missing something. */
+               goto unknown_trap;
+       }
+
+       if (!allowed)
+               goto undef;
+
+       if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
+               u64 hcrx = __vcpu_sys_reg(vcpu, HCRX_EL2);
+               bool fwd;
+
+               switch (iss) {
+               case ESR_ELx_ISS_ST64BV:
+                       fwd = !(hcrx & HCRX_EL2_EnASR);
+                       break;
+               case ESR_ELx_ISS_ST64BV0:
+                       fwd = !(hcrx & HCRX_EL2_EnAS0);
+                       break;
+               case ESR_ELx_ISS_LDST64B:
+                       fwd = !(hcrx & HCRX_EL2_EnALS);
+                       break;
+               default:
+                       /* We don't expect to be here */
+                       fwd = false;
+               }
+
+               if (fwd) {
+                       kvm_inject_nested_sync(vcpu, esr);
+                       return 1;

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 353

Quoted 10 lines
arch/arm64/kvm/handle_exit.c +10 -0
+               }
+       }
+
+unknown_trap:
+       /*
+        * If we land here, something must be very wrong, because we
+        * have no idea why we trapped at all. Warn and undef as a
+        * fallback.
+        */
+       WARN_ON(1);

nit: should this be WARN_ONCE() instead?

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 358

Quoted 5 lines
arch/arm64/kvm/handle_exit.c +5 -0
+
+undef:
+       kvm_inject_undefined(vcpu);
+       return 1;
+}

I'm wondering if this can be simplified by having one switch() statement that toggles both allowed and fwd (or maybe even only fwd), and then inject depending on that, e.g.,

arch/arm64/kvm/handle_exit.c +3 -0
+static int handle_ls64b(struct kvm_vcpu *vcpu)
+{
+    struct kvm *kvm = vcpu->kvm;

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu)

Quoted 10 lines
arch/arm64/kvm/handle_exit.c +10 -0
+    bool is_nv = vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu);
+    u64 hcrx = __vcpu_sys_reg(vcpu, HCRX_EL2);
+    u64 esr = kvm_vcpu_get_esr(vcpu);
+    u64 iss = ESR_ELx_ISS(esr);
+    bool fwd = false;
+
+    switch (iss) {
+    case ESR_ELx_ISS_ST64BV:
+         fwd = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V) &&
+                   !(hcrx & HCRX_EL2_EnASR)

Ah, I know what I dislike about this approach: If your L1 guest runs at EL2, HCRX_EL2 doesn't apply (it is only for an L2 guest). Yet we evaluate it.

I think this still works because you shouldn't have HCRX_EL2.EnASR clear on the host if LS64V is advertised to the guest, but it feels a bit fragile.

I'll have another think at refactoring that code.

Thanks,

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

PATCH 4/18 KVM: arm64: Restrict ACCDATA_EL1 undef to FEAT_ST64_ACCDATA being disabled

In reply to Marc Zyngier

We currently unconditionally make ACCDATA_EL1 accesses UNDEF.

As we are about to support it, restrict the UNDEF behaviour to cases where FEAT_ST64_ACCDATA is not exposed to the guest.

---
 arch/arm64/kvm/sys_regs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +3 -1
index 82430c1e1dd02..18721c773475d 100644
@@ -4997,10 +4997,12 @@ void kvm_calculate_traps(struct kvm_vcpu *vcpu)
 	kvm->arch.fgu[HFGxTR_GROUP] = (HFGxTR_EL2_nAMAIR2_EL1		|
 				       HFGxTR_EL2_nMAIR2_EL1		|
 				       HFGxTR_EL2_nS2POR_EL1		|
-				       HFGxTR_EL2_nACCDATA_EL1		|
 				       HFGxTR_EL2_nSMPRI_EL1_MASK	|
 				       HFGxTR_EL2_nTPIDR2_EL0_MASK);
 
+	if (!kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_ACCDATA))
+		kvm->arch.fgu[HFGxTR_GROUP] |= HFGxTR_EL2_nACCDATA_EL1;
+
 	if (!kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, OS))
 		kvm->arch.fgu[HFGITR_GROUP] |= (HFGITR_EL2_TLBIRVAALE1OS|
 						HFGITR_EL2_TLBIRVALE1OS	|
Signature · 1 line
2.39.2
author maintainer
#10 lore raw

PATCH 5/18 KVM: arm64: Don't treat HCRX_EL2 as a FGT register

In reply to Marc Zyngier

Treating HCRX_EL2 as yet another FGT register seems excessive, and gets in a way of further improvements. It is actually simpler to just be explicit about the masking, so just to that.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
arch/arm64/kvm/hyp/include/hyp/switch.h +3 -6
index f838a45665f26..25a7ff5012ed6 100644
@@ -261,12 +261,9 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
 	if (cpus_have_final_cap(ARM64_HAS_HCX)) {
 		u64 hcrx = vcpu->arch.hcrx_el2;
 		if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
-			u64 clr = 0, set = 0;
-
-			compute_clr_set(vcpu, HCRX_EL2, clr, set);
-
-			hcrx |= set;
-			hcrx &= ~clr;
+			u64 val = __vcpu_sys_reg(vcpu, HCRX_EL2);
+			hcrx |= val & __HCRX_EL2_MASK;
+			hcrx &= ~(~val & __HCRX_EL2_nMASK);
 		}
 
 		write_sysreg_s(hcrx, SYS_HCRX_EL2);
Signature · 1 line
2.39.2
author maintainer
#11 lore raw

PATCH 6/18 KVM: arm64: Plug FEAT_GCS handling

In reply to Marc Zyngier

We don't seem to be handling the GCS-specific exception class. Handle it by delivering an UNDEF to the guest, and populate the relevant trap bits.

---
 arch/arm64/kvm/handle_exit.c | 11 +++++++++++
 arch/arm64/kvm/sys_regs.c    |  8 ++++++++
 2 files changed, 19 insertions(+)
arch/arm64/kvm/handle_exit.c +11 -0
index 4f8354bf7dc5f..624a78a99e38a 100644
@@ -294,6 +294,16 @@ static int handle_svc(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int kvm_handle_gcs(struct kvm_vcpu *vcpu)
+{
+	/* We don't expect GCS, so treat it with contempt */
+	if (kvm_has_feat(vcpu->kvm, ID_AA64PFR1_EL1, GCS, IMP))
+		WARN_ON_ONCE(1);
+
+	kvm_inject_undefined(vcpu);
+	return 1;
+}
+
 static int handle_ls64b(struct kvm_vcpu *vcpu)
 {
 	struct kvm *kvm = vcpu->kvm;
@@ -384,6 +394,7 @@ static exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_BRK64]	= kvm_handle_guest_debug,
 	[ESR_ELx_EC_FP_ASIMD]	= kvm_handle_fpasimd,
 	[ESR_ELx_EC_PAC]	= kvm_handle_ptrauth,
+	[ESR_ELx_EC_GCS]	= kvm_handle_gcs,
 };
 
 static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
arch/arm64/kvm/sys_regs.c +8 -0
index 18721c773475d..2ecd0d51a2dae 100644
@@ -5056,6 +5056,14 @@ void kvm_calculate_traps(struct kvm_vcpu *vcpu)
 						HFGITR_EL2_nBRBIALL);
 	}
 
+	if (!kvm_has_feat(kvm, ID_AA64PFR1_EL1, GCS, IMP)) {
+		kvm->arch.fgu[HFGxTR_GROUP] |= (HFGxTR_EL2_nGCS_EL0 |
+						HFGxTR_EL2_nGCS_EL1);
+		kvm->arch.fgu[HFGITR_GROUP] |= (HFGITR_EL2_nGCSPUSHM_EL1 |
+						HFGITR_EL2_nGCSSTR_EL1 |
+						HFGITR_EL2_nGCSEPP);
+	}
+
 	set_bit(KVM_ARCH_FLAG_FGU_INITIALIZED, &kvm->arch.flags);
 out:
 	mutex_unlock(&kvm->arch.config_lock);
Signature · 1 line
2.39.2
reviewer
#12 lore raw

Re: [PATCH 06/18] KVM: arm64: Plug FEAT_GCS handling

Quoted 15 lines · Marc Zyngier

We don't seem to be handling the GCS-specific exception class. Handle it by delivering an UNDEF to the guest, and populate the relevant trap bits.

---
 arch/arm64/kvm/handle_exit.c | 11 +++++++++++
 arch/arm64/kvm/sys_regs.c    |  8 ++++++++
 2 files changed, 19 insertions(+)
arch/arm64/kvm/handle_exit.c +0 -0
index 4f8354bf7dc5f..624a78a99e38a 100644

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 301

Quoted 8 lines
arch/arm64/kvm/handle_exit.c +5 -0
@@ -294,6 +294,16 @@ static int handle_svc(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int kvm_handle_gcs(struct kvm_vcpu *vcpu)
+{
+	/* We don't expect GCS, so treat it with contempt */
+	if (kvm_has_feat(vcpu->kvm, ID_AA64PFR1_EL1, GCS, IMP))
+		WARN_ON_ONCE(1);

Just to check / better my understanging, do we enforce that this can't be exposed to the guest somewhere?

I see __kvm_read_sanitised_id_reg() masks it out, and the sys_reg_descs table has it filtered, but I'm not immediately sure whether that prevents host userspace maliciously setting this?

Otherwise this looks good to me.

Mark.

Quoted 38 lines
arch/arm64/kvm/handle_exit.c +6 -0
+
+	kvm_inject_undefined(vcpu);
+	return 1;
+}
+
 static int handle_ls64b(struct kvm_vcpu *vcpu)
 {
 	struct kvm *kvm = vcpu->kvm;
@@ -384,6 +394,7 @@ static exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_BRK64]	= kvm_handle_guest_debug,
 	[ESR_ELx_EC_FP_ASIMD]	= kvm_handle_fpasimd,
 	[ESR_ELx_EC_PAC]	= kvm_handle_ptrauth,
+	[ESR_ELx_EC_GCS]	= kvm_handle_gcs,
 };
 
 static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
arch/arm64/kvm/sys_regs.c +8 -0
index 18721c773475d..2ecd0d51a2dae 100644
@@ -5056,6 +5056,14 @@ void kvm_calculate_traps(struct kvm_vcpu *vcpu)
 						HFGITR_EL2_nBRBIALL);
 	}
 
+	if (!kvm_has_feat(kvm, ID_AA64PFR1_EL1, GCS, IMP)) {
+		kvm->arch.fgu[HFGxTR_GROUP] |= (HFGxTR_EL2_nGCS_EL0 |
+						HFGxTR_EL2_nGCS_EL1);
+		kvm->arch.fgu[HFGITR_GROUP] |= (HFGITR_EL2_nGCSPUSHM_EL1 |
+						HFGITR_EL2_nGCSSTR_EL1 |
+						HFGITR_EL2_nGCSEPP);
+	}
+
 	set_bit(KVM_ARCH_FLAG_FGU_INITIALIZED, &kvm->arch.flags);
 out:
 	mutex_unlock(&kvm->arch.config_lock);
Signature · 1 line
2.39.2
author maintainer
#13 lore raw

Re: [PATCH 06/18] KVM: arm64: Plug FEAT_GCS handling

Quoted 29 lines · Mark Rutland
Quoted 15 lines · Marc Zyngier

We don't seem to be handling the GCS-specific exception class. Handle it by delivering an UNDEF to the guest, and populate the relevant trap bits.

---
 arch/arm64/kvm/handle_exit.c | 11 +++++++++++
 arch/arm64/kvm/sys_regs.c    |  8 ++++++++
 2 files changed, 19 insertions(+)
arch/arm64/kvm/handle_exit.c +0 -0
index 4f8354bf7dc5f..624a78a99e38a 100644

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 301

Quoted 8 lines
arch/arm64/kvm/handle_exit.c +5 -0
@@ -294,6 +294,16 @@ static int handle_svc(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int kvm_handle_gcs(struct kvm_vcpu *vcpu)
+{
+	/* We don't expect GCS, so treat it with contempt */
+	if (kvm_has_feat(vcpu->kvm, ID_AA64PFR1_EL1, GCS, IMP))
+		WARN_ON_ONCE(1);

Just to check / better my understanging, do we enforce that this can't be exposed to the guest somewhere?

Quoted 3 lines

I see __kvm_read_sanitised_id_reg() masks it out, and the sys_reg_descs table has it filtered, but I'm not immediately sure whether that prevents host userspace maliciously setting this?

On writing to the idreg, you end-up in set_id_aa64pfr1_el1(), which calls into set_id_reg(). There, arm64_check_features() compares each and every feature in that register with the mask and limits that have been established.

Since GCS is not part of the writable mask, and that it has been disabled, the only valid value for ID_AA64PFR1_EL1.GCS is 0. A non-zero value provided by userspace will be caught by the last check in arm64_check_features(), and an error be returned.

HTH,

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

Re: [PATCH 06/18] KVM: arm64: Plug FEAT_GCS handling

Quoted 22 lines · Marc Zyngier
Quoted 11 lines · Mark Rutland

arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 301

Quoted 5 lines · Marc Zyngier
arch/arm64/kvm/handle_exit.c +5 -0
+static int kvm_handle_gcs(struct kvm_vcpu *vcpu)
+{
+	/* We don't expect GCS, so treat it with contempt */
+	if (kvm_has_feat(vcpu->kvm, ID_AA64PFR1_EL1, GCS, IMP))
+		WARN_ON_ONCE(1);

Just to check / better my understanging, do we enforce that this can't be exposed to the guest somewhere?

Quoted 3 lines

I see __kvm_read_sanitised_id_reg() masks it out, and the sys_reg_descs table has it filtered, but I'm not immediately sure whether that prevents host userspace maliciously setting this?

On writing to the idreg, you end-up in set_id_aa64pfr1_el1(), which calls into set_id_reg(). There, arm64_check_features() compares each and every feature in that register with the mask and limits that have been established.

Quoted 4 lines

Since GCS is not part of the writable mask, and that it has been disabled, the only valid value for ID_AA64PFR1_EL1.GCS is 0. A non-zero value provided by userspace will be caught by the last check in arm64_check_features(), and an error be returned.

Perfect, thanks!

Mark.

author maintainer
#15 lore raw

PATCH 7/18 KVM: arm64: Compute FGT masks from KVM's own FGT tables

In reply to Marc Zyngier

In the process of decoupling KVM's view of the FGT bits from the wider architectural state, use KVM's own FGT tables to build a synthitic view of what is actually known.

This allows for some checking along the way.

---
 arch/arm64/include/asm/kvm_arm.h  |   4 ++
 arch/arm64/include/asm/kvm_host.h |  14 ++++
 arch/arm64/kvm/emulate-nested.c   | 102 ++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+)
arch/arm64/include/asm/kvm_arm.h +4 -0
index 8d94a6c0ed5c4..e424085f2aaca 100644
@@ -359,6 +359,10 @@
 #define __HAFGRTR_EL2_MASK	(GENMASK(49, 17) | GENMASK(4, 0))
 #define __HAFGRTR_EL2_nMASK	~(__HAFGRTR_EL2_RES0 | __HAFGRTR_EL2_MASK)
 
+/* Because the sysreg file mixes R and W... */
+#define HFGRTR_EL2_RES0		HFGxTR_EL2_RES0
+#define HFGWTR_EL2_RES0		(HFGRTR_EL2_RES0 | __HFGRTR_ONLY_MASK)
+
 /* Similar definitions for HCRX_EL2 */
 #define __HCRX_EL2_RES0         HCRX_EL2_RES0
 #define __HCRX_EL2_MASK		(BIT(6))
arch/arm64/include/asm/kvm_host.h +14 -0
index 7cfa024de4e34..4e67d4064f409 100644
@@ -569,6 +569,20 @@ struct kvm_sysreg_masks {
 	} mask[NR_SYS_REGS - __SANITISED_REG_START__];
 };
 
+struct fgt_masks {
+	const char	*str;
+	u64		mask;
+	u64		nmask;
+	u64		res0;
+};
+
+extern struct fgt_masks hfgrtr_masks;
+extern struct fgt_masks hfgwtr_masks;
+extern struct fgt_masks hfgitr_masks;
+extern struct fgt_masks hdfgrtr_masks;
+extern struct fgt_masks hdfgwtr_masks;
+extern struct fgt_masks hafgrtr_masks;
+
 struct kvm_cpu_context {
 	struct user_pt_regs regs;	/* sp = sp_el0 */
 
arch/arm64/kvm/emulate-nested.c +102 -0

116 lines not on this page · raw message

Signature · 1 line
2.39.2
#16 lore raw

Re: [PATCH 07/18] KVM: arm64: Compute FGT masks from KVM's own FGT tables

Hi Marc,

Quoted 4 lines · Marc Zyngier

In the process of decoupling KVM's view of the FGT bits from the wider architectural state, use KVM's own FGT tables to build a synthitic view of what is actually known.

synthitic -> synthetic

Quoted 14 lines

This allows for some checking along the way.

---
 arch/arm64/include/asm/kvm_arm.h  |   4 ++
 arch/arm64/include/asm/kvm_host.h |  14 ++++
 arch/arm64/kvm/emulate-nested.c   | 102 ++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+)
arch/arm64/include/asm/kvm_arm.h +0 -0
index 8d94a6c0ed5c4..e424085f2aaca 100644

arch/arm64/include/asm/kvm_arm.h line 364

Quoted 6 lines
arch/arm64/include/asm/kvm_arm.h +3 -0
@@ -359,6 +359,10 @@
 #define __HAFGRTR_EL2_MASK     (GENMASK(49, 17) | GENMASK(4, 0))
 #define __HAFGRTR_EL2_nMASK    ~(__HAFGRTR_EL2_RES0 | __HAFGRTR_EL2_MASK)
 
+/* Because the sysreg file mixes R and W... */
+#define HFGRTR_EL2_RES0                HFGxTR_EL2_RES0 (0)
+#define HFGWTR_EL2_RES0                (HFGRTR_EL2_RES0 | __HFGRTR_ONLY_MASK)

__HFGRTR_ONLY_MASK is a hand-crafted bitmask. The only bit remaining in HFGxTR_EL2 that is RES0 is bit 51. If that were to be used as an HFGRTR-only bit without __HFGRTR_ONLY_MASK getting updated, then aggregate_fgt() below would set its bit in hfgwtr_masks. Could this be a problem if this happens and the polarity of this bit ends up being negative, thereby setting the corresponding nmask bit?

Cheers,
/fuad

Quoted 154 lines
arch/arm64/include/asm/kvm_arm.h +1 -0
+
 /* Similar definitions for HCRX_EL2 */
 #define __HCRX_EL2_RES0         HCRX_EL2_RES0
 #define __HCRX_EL2_MASK                (BIT(6))
arch/arm64/include/asm/kvm_host.h +14 -0
index 7cfa024de4e34..4e67d4064f409 100644
@@ -569,6 +569,20 @@ struct kvm_sysreg_masks {
        } mask[NR_SYS_REGS - __SANITISED_REG_START__];
 };
 
+struct fgt_masks {
+       const char      *str;
+       u64             mask;
+       u64             nmask;
+       u64             res0;
+};
+
+extern struct fgt_masks hfgrtr_masks;
+extern struct fgt_masks hfgwtr_masks;
+extern struct fgt_masks hfgitr_masks;
+extern struct fgt_masks hdfgrtr_masks;
+extern struct fgt_masks hdfgwtr_masks;
+extern struct fgt_masks hafgrtr_masks;
+
 struct kvm_cpu_context {
        struct user_pt_regs regs;       /* sp = sp_el0 */
 
arch/arm64/kvm/emulate-nested.c +102 -1
index 607d37bab70b4..bbfe89c37a86e 100644
@@ -2033,6 +2033,101 @@ static u32 encoding_next(u32 encoding)
        return sys_reg(op0 + 1, 0, 0, 0, 0);
 }
 
+#define FGT_MASKS(__n, __m)                                            \
+       struct fgt_masks __n = { .str = #__m, .res0 = __m, }
+
+FGT_MASKS(hfgrtr_masks, HFGRTR_EL2_RES0);
+FGT_MASKS(hfgwtr_masks, HFGWTR_EL2_RES0);
+FGT_MASKS(hfgitr_masks, HFGITR_EL2_RES0);
+FGT_MASKS(hdfgrtr_masks, HDFGRTR_EL2_RES0);
+FGT_MASKS(hdfgwtr_masks, HDFGWTR_EL2_RES0);
+FGT_MASKS(hafgrtr_masks, HAFGRTR_EL2_RES0);
+
+static __init bool aggregate_fgt(union trap_config tc)
+{
+       struct fgt_masks *rmasks, *wmasks;
+
+       switch (tc.fgt) {
+       case HFGxTR_GROUP:
+               rmasks = &hfgrtr_masks;
+               wmasks = &hfgwtr_masks;
+               break;
+       case HDFGRTR_GROUP:
+               rmasks = &hdfgrtr_masks;
+               wmasks = &hdfgwtr_masks;
+               break;
+       case HAFGRTR_GROUP:
+               rmasks = &hafgrtr_masks;
+               wmasks = NULL;
+               break;
+       case HFGITR_GROUP:
+               rmasks = &hfgitr_masks;
+               wmasks = NULL;
+               break;
+       }
+
+       /*
+        * A bit can be reserved in either the R or W register, but
+        * not both.
+        */
+       if ((BIT(tc.bit) & rmasks->res0) &&
+           (!wmasks || (BIT(tc.bit) & wmasks->res0)))
+               return false;
+
+       if (tc.pol)
+               rmasks->mask |= BIT(tc.bit) & ~rmasks->res0;
+       else
+               rmasks->nmask |= BIT(tc.bit) & ~rmasks->res0;
+
+       if (wmasks) {
+               if (tc.pol)
+                       wmasks->mask |= BIT(tc.bit) & ~wmasks->res0;
+               else
+                       wmasks->nmask |= BIT(tc.bit) & ~wmasks->res0;
+       }
+
+       return true;
+}
+
+static __init int check_fgt_masks(struct fgt_masks *masks)
+{
+       unsigned long duplicate = masks->mask & masks->nmask;
+       u64 res0 = masks->res0;
+       int ret = 0;
+
+       if (duplicate) {
+               int i;
+
+               for_each_set_bit(i, &duplicate, 64) {
+                       kvm_err("%s[%d] bit has both polarities\n",
+                               masks->str, i);
+               }
+
+               ret = -EINVAL;
+       }
+
+       masks->res0 = ~(masks->mask | masks->nmask);
+       if (masks->res0 != res0)
+               kvm_info("Implicit %s = %016llx, expecting %016llx\n",
+                        masks->str, masks->res0, res0);
+
+       return ret;
+}
+
+static __init int check_all_fgt_masks(int ret)
+{
+       int err = 0;
+
+       err |= check_fgt_masks(&hfgrtr_masks);
+       err |= check_fgt_masks(&hfgwtr_masks);
+       err |= check_fgt_masks(&hfgitr_masks);
+       err |= check_fgt_masks(&hdfgrtr_masks);
+       err |= check_fgt_masks(&hdfgwtr_masks);
+       err |= check_fgt_masks(&hafgrtr_masks);
+
+       return ret ?: err;
+}
+
 int __init populate_nv_trap_config(void)
 {
        int ret = 0;
@@ -2097,8 +2192,15 @@ int __init populate_nv_trap_config(void)
                        ret = xa_err(prev);
                        print_nv_trap_error(fgt, "Failed FGT insertion", ret);
                }
+
+               if (!aggregate_fgt(tc)) {
+                       ret = -EINVAL;
+                       print_nv_trap_error(fgt, "FGT bit is reserved", ret);
+               }
        }
 
+       ret = check_all_fgt_masks(ret);
+
        kvm_info("nv: %ld fine grained trap handlers\n",
                 ARRAY_SIZE(encoding_to_fgt));
 
--

2.39.2

author maintainer
#17 lore raw

Re: [PATCH 07/18] KVM: arm64: Compute FGT masks from KVM's own FGT tables

Quoted 29 lines · Fuad Tabba

Hi Marc,

Quoted 4 lines · Marc Zyngier

In the process of decoupling KVM's view of the FGT bits from the wider architectural state, use KVM's own FGT tables to build a synthitic view of what is actually known.

synthitic -> synthetic

Quoted 17 lines

This allows for some checking along the way.

---
 arch/arm64/include/asm/kvm_arm.h  |   4 ++
 arch/arm64/include/asm/kvm_host.h |  14 ++++
 arch/arm64/kvm/emulate-nested.c   | 102 ++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+)
arch/arm64/include/asm/kvm_arm.h +0 -0
index 8d94a6c0ed5c4..e424085f2aaca 100644
@@ -359,6 +359,10 @@
 #define __HAFGRTR_EL2_MASK     (GENMASK(49, 17) | GENMASK(4, 0))
 #define __HAFGRTR_EL2_nMASK    ~(__HAFGRTR_EL2_RES0 | __HAFGRTR_EL2_MASK)
 
Quoted 10 lines

arch/arm64/include/asm/kvm_arm.h line 364

Quoted 3 lines
arch/arm64/include/asm/kvm_arm.h +3 -0
+/* Because the sysreg file mixes R and W... */
+#define HFGRTR_EL2_RES0                HFGxTR_EL2_RES0 (0)
+#define HFGWTR_EL2_RES0                (HFGRTR_EL2_RES0 | __HFGRTR_ONLY_MASK)

__HFGRTR_ONLY_MASK is a hand-crafted bitmask. The only bit remaining in HFGxTR_EL2 that is RES0 is bit 51. If that were to be used as an HFGRTR-only bit without __HFGRTR_ONLY_MASK getting updated, then aggregate_fgt() below would set its bit in hfgwtr_masks. Could this be a problem if this happens and the polarity of this bit ends up being negative, thereby setting the corresponding nmask bit?

This could become a problem indeed. But the only fix for that is to kill the HFGxTR stupidity and describe all the bits as needed so that we stop assuming things.

I'm half tempted to do that next.

Thanks,

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

Re: [PATCH 07/18] KVM: arm64: Compute FGT masks from KVM's own FGT tables

In reply to Fuad Tabba
Quoted 10 lines · Fuad Tabba

Hi Marc,

Quoted 4 lines · Marc Zyngier

In the process of decoupling KVM's view of the FGT bits from the wider architectural state, use KVM's own FGT tables to build a synthitic view of what is actually known.

synthitic -> synthetic

Ah, I missed that one earlier. Will fix.

Quoted 19 lines
Quoted 19 lines

This allows for some checking along the way.

---
 arch/arm64/include/asm/kvm_arm.h  |   4 ++
 arch/arm64/include/asm/kvm_host.h |  14 ++++
 arch/arm64/kvm/emulate-nested.c   | 102 ++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+)
arch/arm64/include/asm/kvm_arm.h +0 -0
index 8d94a6c0ed5c4..e424085f2aaca 100644
@@ -359,6 +359,10 @@
 #define __HAFGRTR_EL2_MASK     (GENMASK(49, 17) | GENMASK(4, 0))
 #define __HAFGRTR_EL2_nMASK    ~(__HAFGRTR_EL2_RES0 | __HAFGRTR_EL2_MASK)
 
Quoted 10 lines

arch/arm64/include/asm/kvm_arm.h line 364

Quoted 3 lines
arch/arm64/include/asm/kvm_arm.h +3 -0
+/* Because the sysreg file mixes R and W... */
+#define HFGRTR_EL2_RES0                HFGxTR_EL2_RES0 (0)
+#define HFGWTR_EL2_RES0                (HFGRTR_EL2_RES0 | __HFGRTR_ONLY_MASK)

__HFGRTR_ONLY_MASK is a hand-crafted bitmask. The only bit remaining in HFGxTR_EL2 that is RES0 is bit 51. If that were to be used as an HFGRTR-only bit without __HFGRTR_ONLY_MASK getting updated, then aggregate_fgt() below would set its bit in hfgwtr_masks. Could this be a problem if this happens and the polarity of this bit ends up being negative, thereby setting the corresponding nmask bit?

So I ended up doing exactly what I threatened to do, which is to completely get rid of the HFGxTR nonsense, and bring HFG{R,W}TR to their full glory.

The diffstat is a bit annoying:

 arch/arm64/include/asm/el2_setup.h      |  14 +--
 arch/arm64/include/asm/kvm_arm.h        |   4 +-
 arch/arm64/include/asm/kvm_host.h       |   3 +-
 arch/arm64/kvm/emulate-nested.c         | 154 ++++++++++++-------------
 arch/arm64/kvm/hyp/include/hyp/switch.h |   4 +-
 arch/arm64/kvm/hyp/vgic-v3-sr.c         |   8 +-
 arch/arm64/kvm/nested.c                 |  42 +++----
 arch/arm64/kvm/sys_regs.c               |  20 ++--
 arch/arm64/tools/sysreg                 | 194 ++++++++++++++++++++------------
 9 files changed, 250 insertions(+), 193 deletions(-)

but at least it puts all registers in the same bucket, and we don't assume anything anymore.

I'll repost the series on Monday, once I'm on holiday.

Thanks,

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

PATCH 8/18 KVM: arm64: Add description of FGT bits leading to EC!=0x18

In reply to Marc Zyngier

The current FTP tables are only concerned with the bits generating ESR_ELx.EC==0x18. However, we want an exhaustive view of what KVM really knows about.

So let's add another small table that provides that extra information.

---
 arch/arm64/kvm/emulate-nested.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)
arch/arm64/kvm/emulate-nested.c +26 -6
index bbfe89c37a86e..4f468759268c0 100644
@@ -1279,16 +1279,21 @@ enum fg_filter_id {
 	__NR_FG_FILTER_IDS__
 };
 
-#define SR_FGF(sr, g, b, p, f)					\
-	{							\
-		.encoding	= sr,				\
-		.end		= sr,				\
-		.tc		= {				\
+#define __FGT(g, b, p, f)					\
+		{						\
 			.fgt = g ## _GROUP,			\
 			.bit = g ## _EL2_ ## b ## _SHIFT,	\
 			.pol = p,				\
 			.fgf = f,				\
-		},						\
+		}
+
+#define FGT(g, b, p)		__FGT(g, b, p, __NO_FGF__)
+
+#define SR_FGF(sr, g, b, p, f)					\
+	{							\
+		.encoding	= sr,				\
+		.end		= sr,				\
+		.tc		= __FGT(g, b, p, f),		\
 		.line = __LINE__,				\
 	}
 
@@ -1989,6 +1994,14 @@ static const struct encoding_to_trap_config encoding_to_fgt[] __initconst = {
 	SR_FGT(SYS_AMEVCNTR0_EL0(0),	HAFGRTR, AMEVCNTR00_EL0, 1),
 };
 
+/* Additional FGTs that do not fire with ESR_EL2.EC==0x18 */
+static const union trap_config non_0x18_fgt[] __initconst = {
+	FGT(HFGITR, nGCSSTR_EL1, 0),
+	FGT(HFGITR, SVC_EL1, 1),
+	FGT(HFGITR, SVC_EL0, 1),
+	FGT(HFGITR, ERET, 1),
+};
+
 static union trap_config get_trap_config(u32 sysreg)
 {
 	return (union trap_config) {
@@ -2199,6 +2212,13 @@ int __init populate_nv_trap_config(void)
 		}
 	}
 
+	for (int i = 0; i < ARRAY_SIZE(non_0x18_fgt); i++) {
+		if (!aggregate_fgt(non_0x18_fgt[i])) {
+			ret = -EINVAL;
+			kvm_err("non_0x18_fgt[%d] is reserved\n", i);
+		}
+	}
+
 	ret = check_all_fgt_masks(ret);
 
 	kvm_info("nv: %ld fine grained trap handlers\n",
Signature · 1 line
2.39.2
author maintainer
#20 lore raw

PATCH 9/18 KVM: arm64: Use computed masks as sanitisers for FGT registers

In reply to Marc Zyngier

Now that we have computed RES0 bits, use them to sanitise the guest view of FGT registers.

---
 arch/arm64/kvm/nested.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
arch/arm64/kvm/nested.c +6 -6
index 0c9387d2f5070..63fe1595f318d 100644
@@ -1118,8 +1118,8 @@ int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu)
 		res0 |= HFGxTR_EL2_nS2POR_EL1;
 	if (!kvm_has_feat(kvm, ID_AA64MMFR3_EL1, AIE, IMP))
 		res0 |= (HFGxTR_EL2_nMAIR2_EL1 | HFGxTR_EL2_nAMAIR2_EL1);
-	set_sysreg_masks(kvm, HFGRTR_EL2, res0 | __HFGRTR_EL2_RES0, res1);
-	set_sysreg_masks(kvm, HFGWTR_EL2, res0 | __HFGWTR_EL2_RES0, res1);
+	set_sysreg_masks(kvm, HFGRTR_EL2, res0 | hfgrtr_masks.res0, res1);
+	set_sysreg_masks(kvm, HFGWTR_EL2, res0 | hfgwtr_masks.res0, res1);
 
 	/* HDFG[RW]TR_EL2 */
 	res0 = res1 = 0;
@@ -1157,7 +1157,7 @@ int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu)
 			 HDFGRTR_EL2_nBRBDATA);
 	if (!kvm_has_feat(kvm, ID_AA64DFR0_EL1, PMSVer, V1P2))
 		res0 |= HDFGRTR_EL2_nPMSNEVFR_EL1;
-	set_sysreg_masks(kvm, HDFGRTR_EL2, res0 | HDFGRTR_EL2_RES0, res1);
+	set_sysreg_masks(kvm, HDFGRTR_EL2, res0 | hdfgrtr_masks.res0, res1);
 
 	/* Reuse the bits from the read-side and add the write-specific stuff */
 	if (!kvm_has_feat(kvm, ID_AA64DFR0_EL1, PMUVer, IMP))
@@ -1166,10 +1166,10 @@ int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu)
 		res0 |= HDFGWTR_EL2_TRCOSLAR;
 	if (!kvm_has_feat(kvm, ID_AA64DFR0_EL1, TraceFilt, IMP))
 		res0 |= HDFGWTR_EL2_TRFCR_EL1;
-	set_sysreg_masks(kvm, HFGWTR_EL2, res0 | HDFGWTR_EL2_RES0, res1);
+	set_sysreg_masks(kvm, HFGWTR_EL2, res0 | hdfgwtr_masks.res0, res1);
 
 	/* HFGITR_EL2 */
-	res0 = HFGITR_EL2_RES0;
+	res0 = hfgitr_masks.res0;
 	res1 = HFGITR_EL2_RES1;
 	if (!kvm_has_feat(kvm, ID_AA64ISAR1_EL1, DPB, DPB2))
 		res0 |= HFGITR_EL2_DCCVADP;
@@ -1203,7 +1203,7 @@ int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu)
 	set_sysreg_masks(kvm, HFGITR_EL2, res0, res1);
 
 	/* HAFGRTR_EL2 - not a lot to see here */
-	res0 = HAFGRTR_EL2_RES0;
+	res0 = hafgrtr_masks.res0;
 	res1 = HAFGRTR_EL2_RES1;
 	if (!kvm_has_feat(kvm, ID_AA64PFR0_EL1, AMU, V1P1))
 		res0 |= ~(res0 | res1);
Signature · 1 line
2.39.2
author maintainer
#21 lore raw

PATCH 10/18 KVM: arm64: Unconditionally configure fine-grain traps

In reply to Marc Zyngier

From: Mark Rutland <[email protected]>

... otherwise we can inherit the host configuration if this differs from the KVM configuration.

[maz: simplified a couple of things]
---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 39 ++++++++++---------------
 1 file changed, 15 insertions(+), 24 deletions(-)
arch/arm64/kvm/hyp/include/hyp/switch.h +15 -24
index 25a7ff5012ed6..29f4110d3758e 100644
@@ -107,7 +107,8 @@ static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
 
 #define update_fgt_traps_cs(hctxt, vcpu, kvm, reg, clr, set)		\
 	do {								\
-		u64 c = 0, s = 0;					\
+		u64 c = clr, s = set;					\
+		u64 val;						\
 									\
 		ctxt_sys_reg(hctxt, reg) = read_sysreg_s(SYS_ ## reg);	\
 		if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu))		\
@@ -115,14 +116,10 @@ static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
 									\
 		compute_undef_clr_set(vcpu, kvm, reg, c, s);		\
 									\
-		s |= set;						\
-		c |= clr;						\
-		if (c || s) {						\
-			u64 val = __ ## reg ## _nMASK;			\
-			val |= s;					\
-			val &= ~c;					\
-			write_sysreg_s(val, SYS_ ## reg);		\
-		}							\
+		val = __ ## reg ## _nMASK;				\
+		val |= s;						\
+		val &= ~c;						\
+		write_sysreg_s(val, SYS_ ## reg);			\
 	} while(0)
 
 #define update_fgt_traps(hctxt, vcpu, kvm, reg)		\
@@ -175,33 +172,27 @@ static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
 		update_fgt_traps(hctxt, vcpu, kvm, HAFGRTR_EL2);
 }
 
-#define __deactivate_fgt(htcxt, vcpu, kvm, reg)				\
+#define __deactivate_fgt(htcxt, vcpu, reg)				\
 	do {								\
-		if ((vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) ||	\
-		    kvm->arch.fgu[reg_to_fgt_group_id(reg)])		\
-			write_sysreg_s(ctxt_sys_reg(hctxt, reg),	\
-				       SYS_ ## reg);			\
+		write_sysreg_s(ctxt_sys_reg(hctxt, reg),		\
+			       SYS_ ## reg);				\
 	} while(0)
 
 static inline void __deactivate_traps_hfgxtr(struct kvm_vcpu *vcpu)
 {
 	struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
-	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
 
 	if (!cpus_have_final_cap(ARM64_HAS_FGT))
 		return;
 
-	__deactivate_fgt(hctxt, vcpu, kvm, HFGRTR_EL2);
-	if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38))
-		write_sysreg_s(ctxt_sys_reg(hctxt, HFGWTR_EL2), SYS_HFGWTR_EL2);
-	else
-		__deactivate_fgt(hctxt, vcpu, kvm, HFGWTR_EL2);
-	__deactivate_fgt(hctxt, vcpu, kvm, HFGITR_EL2);
-	__deactivate_fgt(hctxt, vcpu, kvm, HDFGRTR_EL2);
-	__deactivate_fgt(hctxt, vcpu, kvm, HDFGWTR_EL2);
+	__deactivate_fgt(hctxt, vcpu, HFGRTR_EL2);
+	__deactivate_fgt(hctxt, vcpu, HFGWTR_EL2);
+	__deactivate_fgt(hctxt, vcpu, HFGITR_EL2);
+	__deactivate_fgt(hctxt, vcpu, HDFGRTR_EL2);
+	__deactivate_fgt(hctxt, vcpu, HDFGWTR_EL2);
 
 	if (cpu_has_amu())
-		__deactivate_fgt(hctxt, vcpu, kvm, HAFGRTR_EL2);
+		__deactivate_fgt(hctxt, vcpu, HAFGRTR_EL2);
 }
 
 static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)
Signature · 1 line
2.39.2
author maintainer
#22 lore raw

PATCH 11/18 KVM: arm64: Propagate FGT masks to the nVHE hypervisor

In reply to Marc Zyngier

The nVHE hypervisor needs to have access to its own view of the FGT masks, which unfortunately results in a bit of data duplication.

---
 arch/arm64/include/asm/kvm_host.h | 7 +++++++
 arch/arm64/kvm/arm.c              | 8 ++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c  | 7 +++++++
 3 files changed, 22 insertions(+)
arch/arm64/include/asm/kvm_host.h +7 -0
index 4e67d4064f409..7220382aeb9dc 100644
@@ -583,6 +583,13 @@ extern struct fgt_masks hdfgrtr_masks;
 extern struct fgt_masks hdfgwtr_masks;
 extern struct fgt_masks hafgrtr_masks;
 
+extern struct fgt_masks kvm_nvhe_sym(hfgrtr_masks);
+extern struct fgt_masks kvm_nvhe_sym(hfgwtr_masks);
+extern struct fgt_masks kvm_nvhe_sym(hfgitr_masks);
+extern struct fgt_masks kvm_nvhe_sym(hdfgrtr_masks);
+extern struct fgt_masks kvm_nvhe_sym(hdfgwtr_masks);
+extern struct fgt_masks kvm_nvhe_sym(hafgrtr_masks);
+
 struct kvm_cpu_context {
 	struct user_pt_regs regs;	/* sp = sp_el0 */
 
arch/arm64/kvm/arm.c +8 -0
index 071a7d75be689..0747ab90fc283 100644
@@ -2414,6 +2414,14 @@ static void kvm_hyp_init_symbols(void)
 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
 
+	/* Propagate the FGT state to the the nVHE side */
+	kvm_nvhe_sym(hfgrtr_masks)  = hfgrtr_masks;
+	kvm_nvhe_sym(hfgwtr_masks)  = hfgwtr_masks;
+	kvm_nvhe_sym(hfgitr_masks)  = hfgitr_masks;
+	kvm_nvhe_sym(hdfgrtr_masks) = hdfgrtr_masks;
+	kvm_nvhe_sym(hdfgwtr_masks) = hdfgwtr_masks;
+	kvm_nvhe_sym(hafgrtr_masks) = hafgrtr_masks;
+
 	/*
 	 * Flush entire BSS since part of its data containing init symbols is read
 	 * while the MMU is off.
arch/arm64/kvm/hyp/nvhe/switch.c +7 -0
index 6c846d033d24a..2add6e4c33f1e 100644
@@ -33,6 +33,13 @@ DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data);
 DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
 DEFINE_PER_CPU(unsigned long, kvm_hyp_vector);
 
+struct fgt_masks hfgrtr_masks;
+struct fgt_masks hfgwtr_masks;
+struct fgt_masks hfgitr_masks;
+struct fgt_masks hdfgrtr_masks;
+struct fgt_masks hdfgwtr_masks;
+struct fgt_masks hafgrtr_masks;
+
 extern void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc);
 
 static void __activate_cptr_traps(struct kvm_vcpu *vcpu)
Signature · 1 line
2.39.2
author maintainer
#23 lore raw

PATCH 12/18 KVM: arm64: Use computed FGT masks to setup FGT registers

In reply to Marc Zyngier

Flip the hyervisor FGT configuration over to the computed FGT masks.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 45 +++++++++++++++++++++----
 1 file changed, 38 insertions(+), 7 deletions(-)
arch/arm64/kvm/hyp/include/hyp/switch.h +38 -7
index 29f4110d3758e..f9cf5985561ab 100644
@@ -65,12 +65,41 @@ static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
 	}
 }
 
+#define reg_to_fgt_masks(reg)						\
+	({								\
+		struct fgt_masks *m;					\
+		switch(reg) {						\
+		case HFGRTR_EL2:					\
+			m = &hfgrtr_masks;				\
+			break;						\
+		case HFGWTR_EL2:					\
+			m = &hfgwtr_masks;				\
+			break;						\
+		case HFGITR_EL2:					\
+			m = &hfgitr_masks;				\
+			break;						\
+		case HDFGRTR_EL2:					\
+			m = &hdfgrtr_masks;				\
+			break;						\
+		case HDFGWTR_EL2:					\
+			m = &hdfgwtr_masks;				\
+			break;						\
+		case HAFGRTR_EL2:					\
+			m = &hafgrtr_masks;				\
+			break;						\
+		default:						\
+			BUILD_BUG_ON(1);				\
+		}							\
+									\
+		m;							\
+	})
+
 #define compute_clr_set(vcpu, reg, clr, set)				\
 	do {								\
-		u64 hfg;						\
-		hfg = __vcpu_sys_reg(vcpu, reg) & ~__ ## reg ## _RES0;	\
-		set |= hfg & __ ## reg ## _MASK; 			\
-		clr |= ~hfg & __ ## reg ## _nMASK; 			\
+		u64 hfg = __vcpu_sys_reg(vcpu, reg);			\
+		struct fgt_masks *m = reg_to_fgt_masks(reg);		\
+		set |= hfg & m->mask;					\
+		clr |= ~hfg & m->nmask;					\
 	} while(0)
 
 #define reg_to_fgt_group_id(reg)					\
@@ -101,12 +130,14 @@ static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
 #define compute_undef_clr_set(vcpu, kvm, reg, clr, set)			\
 	do {								\
 		u64 hfg = kvm->arch.fgu[reg_to_fgt_group_id(reg)];	\
-		set |= hfg & __ ## reg ## _MASK;			\
-		clr |= hfg & __ ## reg ## _nMASK; 			\
+		struct fgt_masks *m = reg_to_fgt_masks(reg);		\
+		set |= hfg & m->mask;					\
+		clr |= hfg & m->nmask;					\
 	} while(0)
 
 #define update_fgt_traps_cs(hctxt, vcpu, kvm, reg, clr, set)		\
 	do {								\
+		struct fgt_masks *m = reg_to_fgt_masks(reg);		\
 		u64 c = clr, s = set;					\
 		u64 val;						\
 									\
@@ -116,7 +147,7 @@ static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
 									\
 		compute_undef_clr_set(vcpu, kvm, reg, c, s);		\
 									\
-		val = __ ## reg ## _nMASK;				\
+		val = m->nmask;						\
 		val |= s;						\
 		val &= ~c;						\
 		write_sysreg_s(val, SYS_ ## reg);			\
Signature · 1 line
2.39.2
author maintainer
#24 lore raw

PATCH 13/18 KVM: arm64: Remove most hand-crafted masks for FGT registers

In reply to Marc Zyngier

These masks are now useless, and can be removed. One notable exception is __HFGRTR_ONLY_MASK, as the sysreg file conflates the HFGRTR_EL2 and HFGWTR_EL2 bits in a most unhelpful way.

To be fixed one day...

---
 arch/arm64/include/asm/kvm_arm.h        | 42 +------------------------
 arch/arm64/kvm/hyp/include/hyp/switch.h | 19 -----------
 2 files changed, 1 insertion(+), 60 deletions(-)
arch/arm64/include/asm/kvm_arm.h +1 -41
index e424085f2aaca..af87afae6ca3d 100644
@@ -314,56 +314,16 @@
 
 /*
  * FGT register definitions
- *
- * RES0 and polarity masks as of DDI0487J.a, to be updated as needed.
- * We're not using the generated masks as they are usually ahead of
- * the published ARM ARM, which we use as a reference.
- *
- * Once we get to a point where the two describe the same thing, we'll
- * merge the definitions. One day.
- */
-#define __HFGRTR_EL2_RES0	HFGxTR_EL2_RES0
-#define __HFGRTR_EL2_MASK	GENMASK(49, 0)
-#define __HFGRTR_EL2_nMASK	~(__HFGRTR_EL2_RES0 | __HFGRTR_EL2_MASK)
-
-/*
- * The HFGWTR bits are a subset of HFGRTR bits. To ensure we don't miss any
- * future additions, define __HFGWTR* macros relative to __HFGRTR* ones.
  */
 #define __HFGRTR_ONLY_MASK	(BIT(46) | BIT(42) | BIT(40) | BIT(28) | \
 				 GENMASK(26, 25) | BIT(21) | BIT(18) | \
 				 GENMASK(15, 14) | GENMASK(10, 9) | BIT(2))
-#define __HFGWTR_EL2_RES0	(__HFGRTR_EL2_RES0 | __HFGRTR_ONLY_MASK)
-#define __HFGWTR_EL2_MASK	(__HFGRTR_EL2_MASK & ~__HFGRTR_ONLY_MASK)
-#define __HFGWTR_EL2_nMASK	~(__HFGWTR_EL2_RES0 | __HFGWTR_EL2_MASK)
-
-#define __HFGITR_EL2_RES0	HFGITR_EL2_RES0
-#define __HFGITR_EL2_MASK	(BIT(62) | BIT(60) | GENMASK(54, 0))
-#define __HFGITR_EL2_nMASK	~(__HFGITR_EL2_RES0 | __HFGITR_EL2_MASK)
-
-#define __HDFGRTR_EL2_RES0	HDFGRTR_EL2_RES0
-#define __HDFGRTR_EL2_MASK	(BIT(63) | GENMASK(58, 50) | GENMASK(48, 43) | \
-				 GENMASK(41, 40) | GENMASK(37, 22) | \
-				 GENMASK(19, 9) | GENMASK(7, 0))
-#define __HDFGRTR_EL2_nMASK	~(__HDFGRTR_EL2_RES0 | __HDFGRTR_EL2_MASK)
-
-#define __HDFGWTR_EL2_RES0	HDFGWTR_EL2_RES0
-#define __HDFGWTR_EL2_MASK	(GENMASK(57, 52) | GENMASK(50, 48) | \
-				 GENMASK(46, 44) | GENMASK(42, 41) | \
-				 GENMASK(37, 35) | GENMASK(33, 31) | \
-				 GENMASK(29, 23) | GENMASK(21, 10) | \
-				 GENMASK(8, 7) | GENMASK(5, 0))
-#define __HDFGWTR_EL2_nMASK	~(__HDFGWTR_EL2_RES0 | __HDFGWTR_EL2_MASK)
-
-#define __HAFGRTR_EL2_RES0	HAFGRTR_EL2_RES0
-#define __HAFGRTR_EL2_MASK	(GENMASK(49, 17) | GENMASK(4, 0))
-#define __HAFGRTR_EL2_nMASK	~(__HAFGRTR_EL2_RES0 | __HAFGRTR_EL2_MASK)
 
 /* Because the sysreg file mixes R and W... */
 #define HFGRTR_EL2_RES0		HFGxTR_EL2_RES0
 #define HFGWTR_EL2_RES0		(HFGRTR_EL2_RES0 | __HFGRTR_ONLY_MASK)
 
-/* Similar definitions for HCRX_EL2 */
+/* Polarity masks for HCRX_EL2 */
 #define __HCRX_EL2_RES0         HCRX_EL2_RES0
 #define __HCRX_EL2_MASK		(BIT(6))
 #define __HCRX_EL2_nMASK	~(__HCRX_EL2_RES0 | __HCRX_EL2_MASK)
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -19
index f9cf5985561ab..a36bcf6ec0d32 100644
@@ -156,17 +156,6 @@ static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
 #define update_fgt_traps(hctxt, vcpu, kvm, reg)		\
 	update_fgt_traps_cs(hctxt, vcpu, kvm, reg, 0, 0)
 
-/*
- * Validate the fine grain trap masks.
- * Check that the masks do not overlap and that all bits are accounted for.
- */
-#define CHECK_FGT_MASKS(reg)							\
-	do {									\
-		BUILD_BUG_ON((__ ## reg ## _MASK) & (__ ## reg ## _nMASK));	\
-		BUILD_BUG_ON(~((__ ## reg ## _RES0) ^ (__ ## reg ## _MASK) ^	\
-			       (__ ## reg ## _nMASK)));				\
-	} while(0)
-
 static inline bool cpu_has_amu(void)
 {
        u64 pfr0 = read_sysreg_s(SYS_ID_AA64PFR0_EL1);
@@ -180,14 +169,6 @@ static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
 	struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
 	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
 
-	CHECK_FGT_MASKS(HFGRTR_EL2);
-	CHECK_FGT_MASKS(HFGWTR_EL2);
-	CHECK_FGT_MASKS(HFGITR_EL2);
-	CHECK_FGT_MASKS(HDFGRTR_EL2);
-	CHECK_FGT_MASKS(HDFGWTR_EL2);
-	CHECK_FGT_MASKS(HAFGRTR_EL2);
-	CHECK_FGT_MASKS(HCRX_EL2);
-
 	if (!cpus_have_final_cap(ARM64_HAS_FGT))
 		return;
 
Signature · 1 line
2.39.2
author maintainer
#25 lore raw

PATCH 14/18 KVM: arm64: Use KVM-specific HCRX_EL2 RES0 mask

In reply to Marc Zyngier

We do not have a computed table for HCRX_EL2, so statically define the bits we know about. A warning will fire if the architecture grows bits that are not handled yet.

---
 arch/arm64/include/asm/kvm_arm.h | 18 ++++++++++++++----
 arch/arm64/kvm/emulate-nested.c  |  5 +++++
 arch/arm64/kvm/nested.c          |  4 ++--
 3 files changed, 21 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_arm.h +14 -4
index af87afae6ca3d..67b381e4c1e14 100644
@@ -323,10 +323,20 @@
 #define HFGRTR_EL2_RES0		HFGxTR_EL2_RES0
 #define HFGWTR_EL2_RES0		(HFGRTR_EL2_RES0 | __HFGRTR_ONLY_MASK)
 
-/* Polarity masks for HCRX_EL2 */
-#define __HCRX_EL2_RES0         HCRX_EL2_RES0
-#define __HCRX_EL2_MASK		(BIT(6))
-#define __HCRX_EL2_nMASK	~(__HCRX_EL2_RES0 | __HCRX_EL2_MASK)
+/*
+ * Polarity masks for HCRX_EL2, limited to the bits that we know about
+ * at this point in time. It doesn't mean that we actually *handle*
+ * them, but that at least those that are not advertised to a guest
+ * will be RES0 for that guest.
+ */
+#define __HCRX_EL2_MASK		(BIT_ULL(6))
+#define __HCRX_EL2_nMASK	(GENMASK_ULL(24, 14) | \
+				 GENMASK_ULL(11, 7)  | \
+				 GENMASK_ULL(5, 0))
+#define __HCRX_EL2_RES0		~(__HCRX_EL2_nMASK | __HCRX_EL2_MASK)
+#define __HCRX_EL2_RES1		~(__HCRX_EL2_nMASK | \
+				  __HCRX_EL2_MASK  | \
+				  __HCRX_EL2_RES0)
 
 /* Hyp Prefetch Fault Address Register (HPFAR/HDFAR) */
 #define HPFAR_MASK	(~UL(0xf))
arch/arm64/kvm/emulate-nested.c +5 -0
index 4f468759268c0..f6c7331c21ca4 100644
@@ -2149,6 +2149,7 @@ int __init populate_nv_trap_config(void)
 	BUILD_BUG_ON(__NR_CGT_GROUP_IDS__ > BIT(TC_CGT_BITS));
 	BUILD_BUG_ON(__NR_FGT_GROUP_IDS__ > BIT(TC_FGT_BITS));
 	BUILD_BUG_ON(__NR_FG_FILTER_IDS__ > BIT(TC_FGF_BITS));
+	BUILD_BUG_ON(__HCRX_EL2_MASK & __HCRX_EL2_nMASK);
 
 	for (int i = 0; i < ARRAY_SIZE(encoding_to_cgt); i++) {
 		const struct encoding_to_trap_config *cgt = &encoding_to_cgt[i];
@@ -2174,6 +2175,10 @@ int __init populate_nv_trap_config(void)
 		}
 	}
 
+	if (__HCRX_EL2_RES0 != HCRX_EL2_RES0)
+		kvm_info("Sanitised HCR_EL2_RES0 = %016llx, expecting %016llx\n",
+			 __HCRX_EL2_RES0, HCRX_EL2_RES0);
+
 	kvm_info("nv: %ld coarse grained trap handlers\n",
 		 ARRAY_SIZE(encoding_to_cgt));
 
arch/arm64/kvm/nested.c +2 -2
index 63fe1595f318d..48b8a700de457 100644
@@ -1039,8 +1039,8 @@ int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu)
 	set_sysreg_masks(kvm, HCR_EL2, res0, res1);
 
 	/* HCRX_EL2 */
-	res0 = HCRX_EL2_RES0;
-	res1 = HCRX_EL2_RES1;
+	res0 = __HCRX_EL2_RES0;
+	res1 = __HCRX_EL2_RES1;
 	if (!kvm_has_feat(kvm, ID_AA64ISAR3_EL1, PACM, TRIVIAL_IMP))
 		res0 |= HCRX_EL2_PACMEn;
 	if (!kvm_has_feat(kvm, ID_AA64PFR2_EL1, FPMR, IMP))
Signature · 1 line
2.39.2
author maintainer
#26 lore raw

PATCH 15/18 KVM: arm64: Handle PSB CSYNC traps

In reply to Marc Zyngier

Bizarrely, the architecture introduces a trap for PSB CSYNC that has the same EC as LS64. Let's deal with this oddity and add specific handling for it.

It's not that we expect this to be useful any time soon anyway.

---
 arch/arm64/include/asm/esr.h    | 3 ++-
 arch/arm64/kvm/emulate-nested.c | 1 +
 arch/arm64/kvm/handle_exit.c    | 6 ++++++
 arch/arm64/tools/sysreg         | 2 +-
 4 files changed, 10 insertions(+), 2 deletions(-)
arch/arm64/include/asm/esr.h +2 -1
index d5c2fac21a16c..3c283cf6a9c43 100644
@@ -175,10 +175,11 @@
 #define ESR_ELx_WFx_ISS_WFE	(UL(1) << 0)
 #define ESR_ELx_xVC_IMM_MASK	((UL(1) << 16) - 1)
 
-/* ISS definitions for LD64B/ST64B instructions */
+/* ISS definitions for LD64B/ST64B/PSBCSYNC instructions */
 #define ESR_ELx_ISS_ST64BV	(0)
 #define ESR_ELx_ISS_ST64BV0	(1)
 #define ESR_ELx_ISS_LDST64B	(2)
+#define ESR_ELx_ISS_PSBCSYNC	(3)
 
 #define DISR_EL1_IDS		(UL(1) << 24)
 /*
arch/arm64/kvm/emulate-nested.c +1 -0
index f6c7331c21ca4..ebfb2805f716b 100644
@@ -1996,6 +1996,7 @@ static const struct encoding_to_trap_config encoding_to_fgt[] __initconst = {
 
 /* Additional FGTs that do not fire with ESR_EL2.EC==0x18 */
 static const union trap_config non_0x18_fgt[] __initconst = {
+	FGT(HFGITR, PSBCSYNC, 1),
 	FGT(HFGITR, nGCSSTR_EL1, 0),
 	FGT(HFGITR, SVC_EL1, 1),
 	FGT(HFGITR, SVC_EL0, 1),
arch/arm64/kvm/handle_exit.c +6 -0
index 624a78a99e38a..d0e35e9a1c48f 100644
@@ -321,6 +321,9 @@ static int handle_ls64b(struct kvm_vcpu *vcpu)
 	case ESR_ELx_ISS_LDST64B:
 		allowed = kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64);
 		break;
+	case ESR_ELx_ISS_PSBCSYNC:
+		allowed = kvm_has_feat(kvm, ID_AA64DFR0_EL1, PMSVer, V1P5);
+		break;
 	default:
 		/* Clearly, we're missing something. */
 		goto unknown_trap;
@@ -343,6 +346,9 @@ static int handle_ls64b(struct kvm_vcpu *vcpu)
 		case ESR_ELx_ISS_LDST64B:
 			fwd = !(hcrx & HCRX_EL2_EnALS);
 			break;
+		case ESR_ELx_ISS_PSBCSYNC:
+			fwd = (__vcpu_sys_reg(vcpu, HFGITR_EL2) & HFGITR_EL2_PSBCSYNC);
+			break;
 		default:
 			/* We don't expect to be here */
 			fwd = false;
arch/arm64/tools/sysreg +1 -1
index 8c4229b34840f..b4fe211934410 100644
@@ -2560,7 +2560,7 @@ Fields	HFGxTR_EL2
 EndSysreg
 
 Sysreg HFGITR_EL2	3	4	1	1	6
-Res0	63
+Field   63	PSBCSYNC
 Field	62	ATS1E1A
 Res0	61
 Field	60	COSPRCTX
Signature · 1 line
2.39.2
author maintainer
#27 lore raw

PATCH 16/18 KVM: arm64: Switch to table-driven FGU configuration

In reply to Marc Zyngier

Defining the FGU behaviour is extremely tedious. It relies on matching each set of bits from FGT registers with am architectural feature, and adding them to the FGU list if the corresponding feature isn't advertised to the guest.

It is however relatively easy to dump most of that information from the architecture JSON description, and use that to control the FGU bits.

Let's introduce a new set of tables descripbing the mapping between FGT bits and features. Most of the time, this is only a lookup in an idreg field, with a few more complex exceptions.

While this is obviously many more lines in a new file, this is mostly generated, and is pretty easy to maintain.

---
 arch/arm64/include/asm/kvm_host.h |   2 +
 arch/arm64/kvm/Makefile           |   2 +-
 arch/arm64/kvm/config.c           | 559 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c         |  73 +---
 4 files changed, 566 insertions(+), 70 deletions(-)
 create mode 100644 arch/arm64/kvm/config.c
arch/arm64/include/asm/kvm_host.h +2 -0
index 7220382aeb9dc..f9975b5f8907a 100644
@@ -1576,4 +1576,6 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
 #define kvm_has_s1poe(k)				\
 	(kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP))
 
+void compute_fgu(struct kvm *kvm, enum fgt_group_id fgt);
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Makefile +1 -1
index 3cf7adb2b5038..f05713e125077 100644
@@ -14,7 +14,7 @@ CFLAGS_sys_regs.o += -Wno-override-init
 CFLAGS_handle_exit.o += -Wno-override-init
 
 kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
-	 inject_fault.o va_layout.o handle_exit.o \
+	 inject_fault.o va_layout.o handle_exit.o config.o \
 	 guest.o debug.o reset.o sys_regs.o stacktrace.o \
 	 vgic-sys-reg-v3.o fpsimd.o pkvm.o \
 	 arch_timer.o trng.o vmid.o emulate-nested.o nested.o at.o \
arch/arm64/kvm/config.c new file +559 -0

559 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +4 -69

79 lines not on this page · raw message

Signature · 1 line
2.39.2
author maintainer
#28 lore raw

PATCH 17/18 KVM: arm64: Validate FGT register descriptions against RES0 masks

In reply to Marc Zyngier

In order to point out to the unsuspecting KVM hacker that they are missing something somewhere, validate that the known FGT bits do not intersect with the corresponding RES0 mask, as computed at boot time.

THis check is also performed at boot time, ensuring that there is no runtime overhead.

---
 arch/arm64/include/asm/kvm_host.h |  1 +
 arch/arm64/kvm/config.c           | 29 +++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c         |  2 ++
 3 files changed, 32 insertions(+)
arch/arm64/include/asm/kvm_host.h +1 -0
index f9975b5f8907a..b537adc5c5557 100644
@@ -1577,5 +1577,6 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
 	(kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP))
 
 void compute_fgu(struct kvm *kvm, enum fgt_group_id fgt);
+void check_feature_map(void);
 
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/config.c +29 -0
index 0a68555068f11..c9dff71006bf4 100644
@@ -482,6 +482,35 @@ static struct reg_bits_to_feat_map hafgrtr_feat_map[] = {
 		   FEAT_AMUv1),
 };
 
+static void __init check_feat_map(struct reg_bits_to_feat_map *map,
+				  int map_size, u64 res0, const char *str)
+{
+	u64 mask = 0;
+
+	for (int i = 0; i < map_size; i++)
+		mask |= map[i].bits;
+
+	if (mask != ~res0)
+		kvm_err("Undefined %s behaviour, bits %016llx\n",
+			str, mask ^ ~res0);
+}
+
+void __init check_feature_map(void)
+{
+	check_feat_map(hfgrtr_feat_map, ARRAY_SIZE(hfgrtr_feat_map),
+		       hfgrtr_masks.res0, hfgrtr_masks.str);
+	check_feat_map(hfgwtr_feat_map, ARRAY_SIZE(hfgwtr_feat_map),
+		       hfgwtr_masks.res0, hfgwtr_masks.str);
+	check_feat_map(hfgitr_feat_map, ARRAY_SIZE(hfgitr_feat_map),
+		       hfgitr_masks.res0, hfgitr_masks.str);
+	check_feat_map(hdfgrtr_feat_map, ARRAY_SIZE(hdfgrtr_feat_map),
+		       hdfgrtr_masks.res0, hdfgrtr_masks.str);
+	check_feat_map(hdfgwtr_feat_map, ARRAY_SIZE(hdfgwtr_feat_map),
+		       hdfgwtr_masks.res0, hdfgwtr_masks.str);
+	check_feat_map(hafgrtr_feat_map, ARRAY_SIZE(hafgrtr_feat_map),
+		       hafgrtr_masks.res0, hafgrtr_masks.str);
+}
+
 static bool idreg_feat_match(struct kvm *kvm, struct reg_bits_to_feat_map *map)
 {
 	u64 regval = kvm->arch.id_regs[map->regidx];
arch/arm64/kvm/sys_regs.c +2 -0
index d3990ceaa59c2..89fc07c57e438 100644
@@ -5058,6 +5058,8 @@ int __init kvm_sys_reg_table_init(void)
 
 	ret = populate_nv_trap_config();
 
+	check_feature_map();
+
 	for (i = 0; !ret && i < ARRAY_SIZE(sys_reg_descs); i++)
 		ret = populate_sysreg_config(sys_reg_descs + i, i);
 
Signature · 1 line
2.39.2
author maintainer
#29 lore raw

PATCH 18/18 KVM: arm64: Use FGT feature maps to drive RES0 bits

In reply to Marc Zyngier

Another benefit of mapping bits to features is that it becomes trivial to define which bits should be handled as RES0.

Let's apply this principle to the guest's view of the FGT registers.

---
 arch/arm64/include/asm/kvm_host.h |   1 +
 arch/arm64/kvm/config.c           |  26 +++++++
 arch/arm64/kvm/nested.c           | 125 +++---------------------------
 3 files changed, 37 insertions(+), 115 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index b537adc5c5557..663b16750fdb6 100644
@@ -1577,6 +1577,7 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
 	(kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP))
 
 void compute_fgu(struct kvm *kvm, enum fgt_group_id fgt);
+u64 get_reg_disabled_bits(struct kvm *kvm, enum vcpu_sysreg reg);
 void check_feature_map(void);
 
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/config.c +26 -0
index c9dff71006bf4..5034947eee349 100644
@@ -586,3 +586,29 @@ void compute_fgu(struct kvm *kvm, enum fgt_group_id fgt)
 
 	kvm->arch.fgu[fgt] = val;
 }
+
+u64 get_reg_disabled_bits(struct kvm *kvm, enum vcpu_sysreg reg)
+{
+	switch (reg) {
+	case HFGRTR_EL2:
+		return  __compute_unsupported_bits(kvm, hfgrtr_feat_map,
+						   ARRAY_SIZE(hfgrtr_feat_map), 0);
+	case HFGWTR_EL2:
+		return  __compute_unsupported_bits(kvm, hfgwtr_feat_map,
+						   ARRAY_SIZE(hfgwtr_feat_map), 0);
+	case HFGITR_EL2:
+		return  __compute_unsupported_bits(kvm, hfgitr_feat_map,
+						   ARRAY_SIZE(hfgitr_feat_map), 0);
+	case HDFGRTR_EL2:
+		return  __compute_unsupported_bits(kvm, hdfgrtr_feat_map,
+						   ARRAY_SIZE(hdfgrtr_feat_map), 0);
+	case HDFGWTR_EL2:
+		return  __compute_unsupported_bits(kvm, hdfgwtr_feat_map,
+						   ARRAY_SIZE(hdfgwtr_feat_map), 0);
+	case HAFGRTR_EL2:
+		return  __compute_unsupported_bits(kvm, hafgrtr_feat_map,
+						   ARRAY_SIZE(hafgrtr_feat_map), 0);
+	default:
+		return 0;
+	}
+}
arch/arm64/kvm/nested.c +10 -115

143 lines not on this page · raw message

Signature · 1 line
2.39.2