Skip to contentSkip to conversation

KVM: arm64: TCR_EL2 fixes, FEAT_LPA2=NI for NV

PATCH Applied Oliver Upton 5 participants 15 messages Oct 29 – Nov 6

Subsystems (3)
lore
Share & tools
lore

Oliver Upton

author maintainer
#0 lore raw

v3 cover KVM: arm64: TCR_EL2 fixes, FEAT_LPA2=NI for NV

I went ahead and grabbed Wei-Lin's series [1] that added TCR_EL2 sanitisation since it makes life quite a bit easier for computing an effective TCR_EL1.

Tested on Neoverse-V2 running a guest with FEAT_E2H0.

v2 -> v3:
- Collect R-b's from Joey
- Add patch "KVM: arm64: nv: Correctly compute the effective TCR_EL1 for nVHE TCR_EL2", itself picking up fixes for forwarding missing fields into the effective TCR_EL1 value (Fuad)

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

Marc Zyngier (1):
KVM: arm64: Reset TCR_EL2 according to guest's E2H mode

Oliver Upton (1):
KVM: arm64: nv: Correctly compute the effective TCR_EL1 for nVHE

    TCR_EL2

Wei-Lin Chang (3):
KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1
arm64: sysreg: Add TCR_EL2 to sysreg infrastructure
KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

 arch/arm64/include/asm/kvm_arm.h    |  13 ---
 arch/arm64/include/asm/kvm_host.h   |   2 +-
 arch/arm64/include/asm/kvm_nested.h |  38 +++++++--
 arch/arm64/include/asm/sysreg.h     |   1 -
 arch/arm64/kvm/config.c             | 112 +++++++++++++++++++++++++
 arch/arm64/kvm/nested.c             |  15 ++++
 arch/arm64/kvm/sys_regs.c           |  10 ++-
 arch/arm64/tools/sysreg             | 125 ++++++++++++++++++++++++++++
 8 files changed, 291 insertions(+), 25 deletions(-)

base-commit: 9e9283e4c941e806d2735f09b66763487bc48b50

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v3 1/5 KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1

From: Wei-Lin Chang <[email protected]>

There is no FEAT_LPA2 support for NV guests as of now, but we missed limiting it for the non-stage-2 granule size fields (TGRAN4, TGRAN16) in ID_AA64MMFR0_EL1. Add the sanitisation.

Fixes 9f75b6d447d7 ("KVM: arm64: nv: Filter out unsupported features from ID regs")
---
 arch/arm64/kvm/nested.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/arm64/kvm/nested.c +11 -0
index 4d83281a7a28..cd0dabcf3f81 100644
@@ -1875,6 +1875,17 @@ u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val)
 		/* Hide CNTPOFF if present */
 		val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64MMFR0_EL1, ECV, IMP);
 
+		/* NV does not support FEAT_LPA2. */
+		if (SYS_FIELD_GET(ID_AA64MMFR0_EL1, TGRAN4, val) == ID_AA64MMFR0_EL1_TGRAN4_52_BIT) {
+			val &= ~ID_AA64MMFR0_EL1_TGRAN4;
+			val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR0_EL1, TGRAN4, IMP);
+		}
+
+		if (SYS_FIELD_GET(ID_AA64MMFR0_EL1, TGRAN16, val) == ID_AA64MMFR0_EL1_TGRAN16_52_BIT) {
+			val &= ~ID_AA64MMFR0_EL1_TGRAN16;
+			val |= SYS_FIELD_PREP_ENUM(ID_AA64MMFR0_EL1, TGRAN16, IMP);
+		}
+
 		/* Disallow unsupported S2 page sizes */
 		switch (PAGE_SIZE) {
 		case SZ_64K:
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v3 2/5 arm64: sysreg: Add TCR_EL2 to sysreg infrastructure

In reply to Oliver Upton

From: Wei-Lin Chang <[email protected]>

Convert TCR_EL2 to the sysreg infrastructure with data extracted from Registers.json of the BSD licenced AARCHMRS (AARCHMRS_OPENSOURCE_A_profile_FAT-2026-06).

There are two formats of TCR_EL2 based on whether the effective value of HCR_EL2.E2H is 0 or 1. For the E2H == 1 case, prefix the macros with "VHE_". For the E2H == 0 case, the macros are left unprefixed.

---
 arch/arm64/include/asm/kvm_arm.h |  13 ----
 arch/arm64/include/asm/sysreg.h  |   1 -
 arch/arm64/tools/sysreg          | 125 +++++++++++++++++++++++++++++++
 3 files changed, 125 insertions(+), 14 deletions(-)
arch/arm64/include/asm/kvm_arm.h +0 -13
index ba0062849be9..3e1c9e31028e 100644
@@ -45,19 +45,6 @@
 #define MPAMHCR_HOST_FLAGS	0
 
 /* TCR_EL2 Registers bits */
-#define TCR_EL2_DS		(1UL << 32)
-#define TCR_EL2_RES1		((1U << 31) | (1 << 23))
-#define TCR_EL2_HPD		(1 << 24)
-#define TCR_EL2_HA		(1 << 21)
-#define TCR_EL2_TBI		(1 << 20)
-#define TCR_EL2_PS_SHIFT	16
-#define TCR_EL2_PS_MASK		(7 << TCR_EL2_PS_SHIFT)
-#define TCR_EL2_PS_40B		(2 << TCR_EL2_PS_SHIFT)
-#define TCR_EL2_TG0_MASK	TCR_TG0_MASK
-#define TCR_EL2_SH0_MASK	TCR_SH0_MASK
-#define TCR_EL2_ORGN0_MASK	TCR_ORGN0_MASK
-#define TCR_EL2_IRGN0_MASK	TCR_IRGN0_MASK
-#define TCR_EL2_T0SZ_MASK	0x3f
 #define TCR_EL2_MASK	(TCR_EL2_TG0_MASK | TCR_EL2_SH0_MASK | \
 			 TCR_EL2_ORGN0_MASK | TCR_EL2_IRGN0_MASK)
 
arch/arm64/include/asm/sysreg.h +0 -1
index aa65603455a2..fee89cd667d9 100644
@@ -519,7 +519,6 @@
 
 #define SYS_TTBR0_EL2			sys_reg(3, 4, 2, 0, 0)
 #define SYS_TTBR1_EL2			sys_reg(3, 4, 2, 0, 1)
-#define SYS_TCR_EL2			sys_reg(3, 4, 2, 0, 2)
 #define SYS_VTTBR_EL2			sys_reg(3, 4, 2, 1, 0)
 
 #define SYS_HAFGRTR_EL2			sys_reg(3, 4, 3, 1, 6)
arch/arm64/tools/sysreg +125 -0

131 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH v3 3/5 KVM: arm64: Reset TCR_EL2 according to guest's E2H mode

In reply to Oliver Upton

From: Marc Zyngier <[email protected]>

Previously vCPU TCR_EL2's reset value didn't consider TCR_EL2's different formats based on the value of HCR_EL2.E2H. It always resets to the E2H == 0 version.

This didn't cause any problem because the bits set in the E2H == 0 version (TCR_EL2_RES1) all reset to UNKNOWN for E2H == 1 per the architecture.

Still, it's better to fix it. Choose the correct reset value by checking KVM_ARM_VCPU_HAS_EL2_E2H0.

---
 arch/arm64/kvm/sys_regs.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +9 -1
index 0687b78cd7c2..bc880d777442 100644
@@ -3273,6 +3273,14 @@ static u64 reset_mdcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	return vcpu->kvm->arch.nr_pmu_counters;
 }
 
+static u64 reset_tcr_el2(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
+{
+	__vcpu_assign_sys_reg(vcpu, r->reg,
+			      vcpu_has_feature(vcpu, KVM_ARM_VCPU_HAS_EL2_E2H0) ?
+			      TCR_EL2_RES1 : VHE_TCR_EL2_RES1);
+	return __vcpu_sys_reg(vcpu, r->reg);
+}
+
 /*
  * Architected system registers.
  * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
@@ -3851,7 +3859,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 
 	EL2_REG(TTBR0_EL2, access_rw, reset_val, 0),
 	EL2_REG(TTBR1_EL2, access_rw, reset_val, 0),
-	EL2_REG(TCR_EL2, access_rw, reset_val, TCR_EL2_RES1),
+	EL2_REG(TCR_EL2, access_rw, reset_tcr_el2, 0),
 	EL2_REG_FILTERED(TCR2_EL2, access_rw, reset_val, TCR2_EL2_RES1,
 			 tcr2_el2_visibility),
 	EL2_REG_VNCR(VTTBR_EL2, reset_val, 0),
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH v3 4/5 KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

In reply to Oliver Upton

From: Wei-Lin Chang <[email protected]>

Feature dependencies of the fields in TCR_EL2 are extracted from Registers.json of the BSD licenced AARCHMRS (AARCHMRS_OPENSOURCE_A_profile_FAT-2026-06).

Places where we slightly deviate from the above:

- Stick to the ARM ARM M.c requirement for TCR_EL2.TCMA*, which only depends on FEAT_MTE2. This avoids updating ID_AA64PFR2_EL1 with new VMTE* definitions that we don't know the meaning of, as they aren't present in the ARM ARM yet.

- Ignore TCR_EL2.DS's requirement of needing the effective value of TCR2_EL2.D128 be 0. This requires runtime detection of TCR2_EL2.D128 and we don't have D128 support yet.

- TCR_EL2.AS is dependent on FEAT_ASID16, this information is missing from the json.

---
 arch/arm64/include/asm/kvm_host.h |   2 +-
 arch/arm64/kvm/config.c           | 112 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/nested.c           |   4 ++
 3 files changed, 117 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +1 -1
index 95de3dd23fa1..eda8bfcc5eaa 100644
@@ -598,7 +598,6 @@ enum vcpu_sysreg {
 	HACR_EL2,	/* Hypervisor Auxiliary Control Register */
 	TTBR0_EL2,	/* Translation Table Base Register 0 (EL2) */
 	TTBR1_EL2,	/* Translation Table Base Register 1 (EL2) */
-	TCR_EL2,	/* Translation Control Register (EL2) */
 	PIRE0_EL2,	/* Permission Indirection Register 0 (EL2) */
 	PIR_EL2,	/* Permission Indirection Register 1 (EL2) */
 	POR_EL2,	/* Permission Overlay Register 2 (EL2) */
@@ -623,6 +622,7 @@ enum vcpu_sysreg {
 	/* Anything from this can be RES0/RES1 sanitised */
 	MARKER(__SANITISED_REG_START__),
 	SCTLR_EL2,	/* System Control Register (EL2) */
+	TCR_EL2,	/* Translation Control Register (EL2) */
 	TCR2_EL2,	/* Extended Translation Control Register (EL2) */
 	SCTLR2_EL2,	/* System Control Register 2 (EL2) */
 	MDCR_EL2,	/* Monitor Debug Configuration Register (EL2) */
arch/arm64/kvm/config.c +112 -0

149 lines not on this page · raw message

arch/arm64/kvm/nested.c +4 -0
index cd0dabcf3f81..78fa433774c6 100644
@@ -2101,6 +2101,10 @@ int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu)
 	resx = get_reg_fixed_bits(kvm, HFGITR2_EL2);
 	set_sysreg_masks(kvm, HFGITR2_EL2, resx);
 
+	/* TCR_EL2 */
+	resx = get_reg_fixed_bits(kvm, TCR_EL2);
+	set_sysreg_masks(kvm, TCR_EL2, resx);
+
 	/* TCR2_EL2 */
 	resx = get_reg_fixed_bits(kvm, TCR2_EL2);
 	set_sysreg_masks(kvm, TCR2_EL2, resx);
Signature · 1 line
2.47.3
#5 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

Hi Oliver,

I have checked TCR_EL2 information in the latest 2026-09 release, the only difference is it correctly states TCR_EL2.AS is dependent on FEAT_ASID16 now, like what Joey said.

Quoted 5 lines · Oliver Upton

From: Wei-Lin Chang <[email protected]>

Feature dependencies of the fields in TCR_EL2 are extracted from Registers.json of the BSD licenced AARCHMRS (AARCHMRS_OPENSOURCE_A_profile_FAT-2026-06).

So if you like you can change this to 2026-09,

Quoted 4 lines

Places where we slightly deviate from the above:

- Stick to the ARM ARM M.c requirement for TCR_EL2.TCMA*, which only

and change this to M.d,

Quoted 10 lines

depends on FEAT_MTE2. This avoids updating ID_AA64PFR2_EL1 with new VMTE* definitions that we don't know the meaning of, as they aren't present in the ARM ARM yet.

- Ignore TCR_EL2.DS's requirement of needing the effective value of TCR2_EL2.D128 be 0. This requires runtime detection of TCR2_EL2.D128 and we don't have D128 support yet.

- TCR_EL2.AS is dependent on FEAT_ASID16, this information is missing from the json.

and remove this item.

Not that these stay up to date for long though, but just thought I'd point them out.

Thanks,
Wei-Lin Chang

Quoted 5 lines

---

[...]

#6 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

In reply to Oliver Upton
Quoted 13 lines · Oliver Upton

From: Wei-Lin Chang <[email protected]>

Feature dependencies of the fields in TCR_EL2 are extracted from Registers.json of the BSD licenced AARCHMRS (AARCHMRS_OPENSOURCE_A_profile_FAT-2026-06).

Places where we slightly deviate from the above:

- Stick to the ARM ARM M.c requirement for TCR_EL2.TCMA*, which only depends on FEAT_MTE2. This avoids updating ID_AA64PFR2_EL1 with new VMTE* definitions that we don't know the meaning of, as they aren't present in the ARM ARM yet.

Quoted 6 lines

- Ignore TCR_EL2.DS's requirement of needing the effective value of TCR2_EL2.D128 be 0. This requires runtime detection of TCR2_EL2.D128 and we don't have D128 support yet.

- TCR_EL2.AS is dependent on FEAT_ASID16, this information is missing from the json.

I'm seeing a regression in -next for the pre_fault_memory_test selftest when run on qemu with EL2 and NV enabled which bisects to this commit in -next:

# selftests: kvm: pre_fault_memory_test

<47>[ 1358.708301] systemd-journald[75]: Sent WATCHDOG=1 notification.

# Random seed: 0xe322a57
# Testing guest mode: PA-bits:52, VA-bits:48, 4K pages # Testing memory backing src type: anonymous
#
# ==== Test Assertion Failure ====
# pre_fault_memory_test.c:260: false
# pid=1888 tid=1888 errno=2 - No such file or directory
# sh: 1: addr2line: not found
# Unknown ucall 0x0.
not ok 48 selftests: kvm: pre_fault_memory_test # exit=254

The NV pre fault test appears to run fine in the same run, and the test passes when run on the same qemu version in vanilla VHE mode.

Full log:

https://lava.sirena.org.uk/scheduler/job/196379#L2476

bisect log:

# bad: [39ee95e368c911fdd4d896d9e30dc39e03655533] Merge branch 'for-linux-next-fixes' of https://gitlab.freedesktop.org/drm/misc/kernel.git
# good: [05441d697f7f9a927264f4058cbbc6a03dfcac9d] Merge tag 'cocci-7.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jlawall/linux
# good: [5b4696e0bfc358619adaeab25ad35b4979af8331] ocfs2: cache the mount context for dentry lock callbacks
# good: [81755e75f86c490bc9296f6cec0cd221edecaa27] KVM: arm64: Reset TCR_EL2 according to guest's E2H mode
# good: [b5d08dcf25e7a22a7f52d9806c6c6db0c3354a0b] KVM: arm64: selftests: Test MOVI and MOVALL on a pending LPI
# good: [2942baff3a951e0805d0ce9608f318e21c7e3744] arm64: sysreg: Add TCR_EL2 to sysreg infrastructure
# good: [126429e2f503ade63502978beb0c519066442250] ocfs2: stop recovery before shutting down local allocators
git bisect start '39ee95e368c911fdd4d896d9e30dc39e03655533' '05441d697f7f9a927264f4058cbbc6a03dfcac9d' '5b4696e0bfc358619adaeab25ad35b4979af8331' '81755e75f86c490bc9296f6cec0cd221edecaa27' 'b5d08dcf25e7a22a7f52d9806c6c6db0c3354a0b' '2942baff3a951e0805d0ce9608f318e21c7e3744' '126429e2f503ade63502978beb0c519066442250'

# test job: [5b4696e0bfc358619adaeab25ad35b4979af8331] https://lava.sirena.org.uk/scheduler/job/190826
# test job: [81755e75f86c490bc9296f6cec0cd221edecaa27] https://lava.sirena.org.uk/scheduler/job/196795
# test job: [b5d08dcf25e7a22a7f52d9806c6c6db0c3354a0b] https://lava.sirena.org.uk/scheduler/job/194830
# test job: [2942baff3a951e0805d0ce9608f318e21c7e3744] https://lava.sirena.org.uk/scheduler/job/196614
# test job: [126429e2f503ade63502978beb0c519066442250] https://lava.sirena.org.uk/scheduler/job/190308
# test job: [39ee95e368c911fdd4d896d9e30dc39e03655533] https://lava.sirena.org.uk/scheduler/job/198582
# bad: [39ee95e368c911fdd4d896d9e30dc39e03655533] Merge branch 'for-linux-next-fixes' of https://gitlab.freedesktop.org/drm/misc/kernel.git
git bisect bad 39ee95e368c911fdd4d896d9e30dc39e03655533
# test job: [cbf47fe57a568f178ac55176d98a13caba1d8598] https://lava.sirena.org.uk/scheduler/job/195738
# bad: [cbf47fe57a568f178ac55176d98a13caba1d8598] KVM: arm64: Clear the whole hugetlb folio when sanitising MTE tags
git bisect bad cbf47fe57a568f178ac55176d98a13caba1d8598
# test job: [f86c6685ac5cf60f80478c006a521aef122a9517] https://lava.sirena.org.uk/scheduler/job/194120
# bad: [f86c6685ac5cf60f80478c006a521aef122a9517] Merge branch kvm-arm64/mmu-fixes-7.4-1 into kvmarm-master/fixes
git bisect bad f86c6685ac5cf60f80478c006a521aef122a9517
# test job: [388faffbd371fd367d39535ad2aa07fbbd745c08] https://lava.sirena.org.uk/scheduler/job/196379
# bad: [388faffbd371fd367d39535ad2aa07fbbd745c08] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation
git bisect bad 388faffbd371fd367d39535ad2aa07fbbd745c08
# first bad commit: [388faffbd371fd367d39535ad2aa07fbbd745c08] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation
# test job: [62b9e1f75a3c39cfc38b3950bbad017ac5f95bcc] https://lava.sirena.org.uk/scheduler/job/199296
# bad: [62b9e1f75a3c39cfc38b3950bbad017ac5f95bcc] KVM: arm64: Clear the whole hugetlb folio when storing MTE tags
git bisect bad 62b9e1f75a3c39cfc38b3950bbad017ac5f95bcc
# test job: [0e24f9db2ebedc271add0c5dbf2821b8f8a0ab06] https://lava.sirena.org.uk/scheduler/job/199765
# bad: [0e24f9db2ebedc271add0c5dbf2821b8f8a0ab06] KVM: arm64: Add an ISB after mapping a hyp fixmap slot
git bisect bad 0e24f9db2ebedc271add0c5dbf2821b8f8a0ab06
# test job: [361564a8d4305100ee4140ff7b345d1bf2c1a1b2] https://lava.sirena.org.uk/scheduler/job/200104
# bad: [361564a8d4305100ee4140ff7b345d1bf2c1a1b2] KVM: arm64: nv: Correctly compute the effective TCR_EL1 for nVHE TCR_EL2
git bisect bad 361564a8d4305100ee4140ff7b345d1bf2c1a1b2
# test job: [388faffbd371fd367d39535ad2aa07fbbd745c08] https://lava.sirena.org.uk/scheduler/job/196379
# bad: [388faffbd371fd367d39535ad2aa07fbbd745c08] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation
git bisect bad 388faffbd371fd367d39535ad2aa07fbbd745c08
# first bad commit: [388faffbd371fd367d39535ad2aa07fbbd745c08] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation
#7 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

Quoted 38 lines · Mark Brown
Quoted 13 lines · Oliver Upton

From: Wei-Lin Chang <[email protected]>

Feature dependencies of the fields in TCR_EL2 are extracted from Registers.json of the BSD licenced AARCHMRS (AARCHMRS_OPENSOURCE_A_profile_FAT-2026-06).

Places where we slightly deviate from the above:

- Stick to the ARM ARM M.c requirement for TCR_EL2.TCMA*, which only depends on FEAT_MTE2. This avoids updating ID_AA64PFR2_EL1 with new VMTE* definitions that we don't know the meaning of, as they aren't present in the ARM ARM yet.

Quoted 6 lines

- Ignore TCR_EL2.DS's requirement of needing the effective value of TCR2_EL2.D128 be 0. This requires runtime detection of TCR2_EL2.D128 and we don't have D128 support yet.

- TCR_EL2.AS is dependent on FEAT_ASID16, this information is missing from the json.

I'm seeing a regression in -next for the pre_fault_memory_test selftest when run on qemu with EL2 and NV enabled which bisects to this commit in -next:

# selftests: kvm: pre_fault_memory_test

<47>[ 1358.708301] systemd-journald[75]: Sent WATCHDOG=1 notification.

# Random seed: 0xe322a57
# Testing guest mode: PA-bits:52, VA-bits:48, 4K pages # Testing memory backing src type: anonymous
#
# ==== Test Assertion Failure ====
# pre_fault_memory_test.c:260: false
# pid=1888 tid=1888 errno=2 - No such file or directory
# sh: 1: addr2line: not found
# Unknown ucall 0x0.
not ok 48 selftests: kvm: pre_fault_memory_test # exit=254

Quoted 7 lines

The NV pre fault test appears to run fine in the same run, and the test passes when run on the same qemu version in vanilla VHE mode.

Full log:

https://lava.sirena.org.uk/scheduler/job/196379#L2476

The selftests are running vCPUs in vEL2 with 52-bit page tables it seems like. NV doesn't support 52-bit and this commit enforces that by forcing vCPU TCR_EL2.DS (hardware TCR_EL1.DS) to 0, so bad things happen.

aarch64_get_supported_page_sizes() asks KVM for the preferred target, which does not include KVM_ARM_VCPU_HAS_EL2. Without KVM_ARM_VCPU_HAS_EL2, 52bit ipa is permitted if the host supports it.

Then with 52-bit PA deemed supported, the selftest builds page tables with the 52-bit format for that case.

Then when creating the actual vCPU to run, kvm_get_default_vcpu_target() includes KVM_ARM_VCPU_HAS_EL2.

One potential solution is make vm_supports_el2() return false when vm->pa_bits > 48.

Thanks,
Wei-Lin Chang

[...]

author maintainer
#8 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

Hi,

Quoted 19 lines · Wei-Lin Chang
Quoted 18 lines · Mark Brown
Quoted 17 lines · Oliver Upton

From: Wei-Lin Chang <[email protected]>

Feature dependencies of the fields in TCR_EL2 are extracted from Registers.json of the BSD licenced AARCHMRS (AARCHMRS_OPENSOURCE_A_profile_FAT-2026-06).

Places where we slightly deviate from the above:

- Stick to the ARM ARM M.c requirement for TCR_EL2.TCMA*, which only depends on FEAT_MTE2. This avoids updating ID_AA64PFR2_EL1 with new VMTE* definitions that we don't know the meaning of, as they aren't present in the ARM ARM yet.

- Ignore TCR_EL2.DS's requirement of needing the effective value of TCR2_EL2.D128 be 0. This requires runtime detection of TCR2_EL2.D128 and we don't have D128 support yet.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

- TCR_EL2.AS is dependent on FEAT_ASID16, this information is missing from the json.

I'm seeing a regression in -next for the pre_fault_memory_test selftest when run on qemu with EL2 and NV enabled which bisects to this commit in -next:

Thanks for reporting this Mark. Next time can you also include the QEMU command line that reproduces the issue?

Quoted 29 lines
Quoted 13 lines

# selftests: kvm: pre_fault_memory_test

<47>[ 1358.708301] systemd-journald[75]: Sent WATCHDOG=1 notification.

# Random seed: 0xe322a57
# Testing guest mode: PA-bits:52, VA-bits:48, 4K pages # Testing memory backing src type: anonymous
#
# ==== Test Assertion Failure ====
# pre_fault_memory_test.c:260: false
# pid=1888 tid=1888 errno=2 - No such file or directory
# sh: 1: addr2line: not found
# Unknown ucall 0x0.
not ok 48 selftests: kvm: pre_fault_memory_test # exit=254

Quoted 7 lines

The NV pre fault test appears to run fine in the same run, and the test passes when run on the same qemu version in vanilla VHE mode.

Full log:

https://lava.sirena.org.uk/scheduler/job/196379#L2476

The selftests are running vCPUs in vEL2 with 52-bit page tables it seems like. NV doesn't support 52-bit and this commit enforces that by forcing vCPU TCR_EL2.DS (hardware TCR_EL1.DS) to 0, so bad things happen.

aarch64_get_supported_page_sizes() asks KVM for the preferred target, which does not include KVM_ARM_VCPU_HAS_EL2. Without KVM_ARM_VCPU_HAS_EL2, 52bit ipa is permitted if the host supports it.

Quoted 8 lines

Then with 52-bit PA deemed supported, the selftest builds page tables with the 52-bit format for that case.

Then when creating the actual vCPU to run, kvm_get_default_vcpu_target() includes KVM_ARM_VCPU_HAS_EL2.

One potential solution is make vm_supports_el2() return false when vm->pa_bits > 48.

Bit of a hack but gets the job done, do you have time to send a patch? I'm sure we'll get around to doing LPA2 long before anything useful actually shows up anyway.

Thanks,
Oliver

#9 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

Quoted 2 lines · Oliver Upton

Thanks for reporting this Mark. Next time can you also include the QEMU command line that reproduces the issue?

It, and links to the rootfs and test binaries used in the tests and a print of the qemu version, are in the logs that I link to when I send things.

#10 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

In reply to Oliver Upton
Quoted 67 lines · Oliver Upton

Hi,

Quoted 19 lines · Wei-Lin Chang
Quoted 18 lines · Mark Brown
Quoted 17 lines · Oliver Upton

From: Wei-Lin Chang <[email protected]>

Feature dependencies of the fields in TCR_EL2 are extracted from Registers.json of the BSD licenced AARCHMRS (AARCHMRS_OPENSOURCE_A_profile_FAT-2026-06).

Places where we slightly deviate from the above:

- Stick to the ARM ARM M.c requirement for TCR_EL2.TCMA*, which only depends on FEAT_MTE2. This avoids updating ID_AA64PFR2_EL1 with new VMTE* definitions that we don't know the meaning of, as they aren't present in the ARM ARM yet.

- Ignore TCR_EL2.DS's requirement of needing the effective value of TCR2_EL2.D128 be 0. This requires runtime detection of TCR2_EL2.D128 and we don't have D128 support yet.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

- TCR_EL2.AS is dependent on FEAT_ASID16, this information is missing from the json.

I'm seeing a regression in -next for the pre_fault_memory_test selftest when run on qemu with EL2 and NV enabled which bisects to this commit in -next:

Thanks for reporting this Mark. Next time can you also include the QEMU command line that reproduces the issue?

Quoted 35 lines
Quoted 13 lines

# selftests: kvm: pre_fault_memory_test

<47>[ 1358.708301] systemd-journald[75]: Sent WATCHDOG=1 notification.

# Random seed: 0xe322a57
# Testing guest mode: PA-bits:52, VA-bits:48, 4K pages # Testing memory backing src type: anonymous
#
# ==== Test Assertion Failure ====
# pre_fault_memory_test.c:260: false
# pid=1888 tid=1888 errno=2 - No such file or directory
# sh: 1: addr2line: not found
# Unknown ucall 0x0.
not ok 48 selftests: kvm: pre_fault_memory_test # exit=254

Quoted 7 lines

The NV pre fault test appears to run fine in the same run, and the test passes when run on the same qemu version in vanilla VHE mode.

Full log:

https://lava.sirena.org.uk/scheduler/job/196379#L2476

The selftests are running vCPUs in vEL2 with 52-bit page tables it seems like. NV doesn't support 52-bit and this commit enforces that by forcing vCPU TCR_EL2.DS (hardware TCR_EL1.DS) to 0, so bad things happen.

aarch64_get_supported_page_sizes() asks KVM for the preferred target, which does not include KVM_ARM_VCPU_HAS_EL2. Without KVM_ARM_VCPU_HAS_EL2, 52bit ipa is permitted if the host supports it.

Then with 52-bit PA deemed supported, the selftest builds page tables with the 52-bit format for that case.

Then when creating the actual vCPU to run, kvm_get_default_vcpu_target() includes KVM_ARM_VCPU_HAS_EL2.

Quoted 6 lines
Quoted 2 lines

One potential solution is make vm_supports_el2() return false when vm->pa_bits > 48.

Bit of a hack but gets the job done, do you have time to send a patch? I'm sure we'll get around to doing LPA2 long before anything useful actually shows up anyway.

Ack, I can do it.

Thanks,
Wei-Lin Chang

Quoted 3 lines

Thanks,
Oliver

author maintainer
#11 lore raw

PATCH v3 5/5 KVM: arm64: nv: Correctly compute the effective TCR_EL1 for nVHE TCR_EL2

In reply to Oliver Upton

It would appear that nVHE hypervisor support has a few warts which shouldn't come as a surprise. Several fields in TCR_EL2 for features supported in the VM (TCR_EL2.HA for example) are not correctly propagated to the effective value of TCR_EL1.

Forward fields in bulk from nVHE TCR_EL2 to the effective TCR_EL1, relying on config-based sanitisation to enforce the RESx behavior of unsupported features. Deal with the slightly annoying case where the E2H==0 bitfield names are not annotated by TTBR like they are in E2H==1.

Cc [email protected] # v6.18+
Fixes d5bbb76f4474 ("KVM: arm64: nv: Expose hardware access flag management to NV guests")
Fixes 3606e0b2e462 ("KVM: arm64: nv: Add non-VHE-EL2->EL1 translation helpers")
---
 arch/arm64/include/asm/kvm_nested.h | 38 ++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_nested.h +29 -9
index 0f899addafa8..27ec73da02c6 100644
@@ -20,16 +20,36 @@ static inline u64 tcr_el2_ps_to_tcr_el1_ips(u64 tcr_el2)
 	return (u64)FIELD_GET(TCR_EL2_PS_MASK, tcr_el2) << TCR_IPS_SHIFT;
 }
 
-static inline u64 translate_tcr_el2_to_tcr_el1(u64 tcr)
+#define __xlate_tcr_nvhe_to_vhe(nfld, vfld, nvhe, vhe)				\
+	FIELD_MODIFY(VHE_TCR_EL2_##vfld, vhe, FIELD_GET(TCR_EL2_##nfld, *(nvhe)))
+
+#define __xlate_tcr_nvhe_to_vhe_field(fld, nvhe, vhe)				\
+	__xlate_tcr_nvhe_to_vhe(fld, fld, nvhe, vhe)
+
+static inline u64 translate_tcr_el2_to_tcr_el1(u64 nvhe)
 {
-	return TCR_EPD1_MASK |				/* disable TTBR1_EL1 */
-	       ((tcr & TCR_EL2_DS) ? TCR_DS : 0) |
-	       ((tcr & TCR_EL2_TBI) ? TCR_TBI0 : 0) |
-	       tcr_el2_ps_to_tcr_el1_ips(tcr) |
-	       (tcr & TCR_EL2_TG0_MASK) |
-	       (tcr & TCR_EL2_ORGN0_MASK) |
-	       (tcr & TCR_EL2_IRGN0_MASK) |
-	       (tcr & TCR_EL2_T0SZ_MASK);
+	u64 vhe = VHE_TCR_EL2_EPD1 |			/* disable TTBR1_EL1 */
+		  tcr_el2_ps_to_tcr_el1_ips(nvhe);
+
+	__xlate_tcr_nvhe_to_vhe(MTX,	MTX0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(TCMA,	TCMA0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(TBID,	TBID0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HWU62,	HWU062,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HWU61,	HWU061,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HWU60,	HWU060,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HWU59,	HWU059,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HPD,	HPD0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(TBI,	TBI0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	DS,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	HA,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	HD,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	TG0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	SH0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	ORGN0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	IRGN0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	T0SZ,	&nvhe,	&vhe);
+
+	return vhe;
 }
 
 static inline u64 translate_cptr_el2_to_cpacr_el1(u64 cptr_el2)
Signature · 1 line
2.47.3
Reviewed-by on v3 5/5
#12 lore raw

Re: [PATCH v3 5/5] KVM: arm64: nv: Correctly compute the effective TCR_EL1 for nVHE TCR_EL2

Quoted 10 lines · Oliver Upton

It would appear that nVHE hypervisor support has a few warts which shouldn't come as a surprise. Several fields in TCR_EL2 for features supported in the VM (TCR_EL2.HA for example) are not correctly propagated to the effective value of TCR_EL1.

Forward fields in bulk from nVHE TCR_EL2 to the effective TCR_EL1, relying on config-based sanitisation to enforce the RESx behavior of unsupported features. Deal with the slightly annoying case where the E2H==0 bitfield names are not annotated by TTBR like they are in E2H==1.

Quoted 4 lines
Cc [email protected] # v6.18+
Fixes d5bbb76f4474 ("KVM: arm64: nv: Expose hardware access flag management to NV guests")
Fixes 3606e0b2e462 ("KVM: arm64: nv: Add non-VHE-EL2->EL1 translation helpers")
Quoted 57 lines
---
 arch/arm64/include/asm/kvm_nested.h | 38 ++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_nested.h +29 -9
index 0f899addafa8..27ec73da02c6 100644
@@ -20,16 +20,36 @@ static inline u64 tcr_el2_ps_to_tcr_el1_ips(u64 tcr_el2)
 	return (u64)FIELD_GET(TCR_EL2_PS_MASK, tcr_el2) << TCR_IPS_SHIFT;
 }
 
-static inline u64 translate_tcr_el2_to_tcr_el1(u64 tcr)
+#define __xlate_tcr_nvhe_to_vhe(nfld, vfld, nvhe, vhe)				\
+	FIELD_MODIFY(VHE_TCR_EL2_##vfld, vhe, FIELD_GET(TCR_EL2_##nfld, *(nvhe)))
+
+#define __xlate_tcr_nvhe_to_vhe_field(fld, nvhe, vhe)				\
+	__xlate_tcr_nvhe_to_vhe(fld, fld, nvhe, vhe)
+
+static inline u64 translate_tcr_el2_to_tcr_el1(u64 nvhe)
 {
-	return TCR_EPD1_MASK |				/* disable TTBR1_EL1 */
-	       ((tcr & TCR_EL2_DS) ? TCR_DS : 0) |
-	       ((tcr & TCR_EL2_TBI) ? TCR_TBI0 : 0) |
-	       tcr_el2_ps_to_tcr_el1_ips(tcr) |
-	       (tcr & TCR_EL2_TG0_MASK) |
-	       (tcr & TCR_EL2_ORGN0_MASK) |
-	       (tcr & TCR_EL2_IRGN0_MASK) |
-	       (tcr & TCR_EL2_T0SZ_MASK);
+	u64 vhe = VHE_TCR_EL2_EPD1 |			/* disable TTBR1_EL1 */
+		  tcr_el2_ps_to_tcr_el1_ips(nvhe);
+
+	__xlate_tcr_nvhe_to_vhe(MTX,	MTX0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(TCMA,	TCMA0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(TBID,	TBID0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HWU62,	HWU062,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HWU61,	HWU061,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HWU60,	HWU060,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HWU59,	HWU059,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(HPD,	HPD0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe(TBI,	TBI0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	DS,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	HA,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	HD,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	TG0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	SH0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	ORGN0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	IRGN0,	&nvhe,	&vhe);
+	__xlate_tcr_nvhe_to_vhe_field(	T0SZ,	&nvhe,	&vhe);
+
+	return vhe;
 }
 
 static inline u64 translate_cptr_el2_to_cpacr_el1(u64 cptr_el2)
Signature · 1 line
2.47.3
reviewer Reviewed-by Tested-by on the v3 series
#13 lore raw
In reply to Oliver Upton

Hi Oliver,

On Thu, 29 Oct 2026 22:37:27 +0000, Oliver Upton <[email protected]> wrote:
[...]

Quoted 1 line

Tested on Neoverse-V2 running a guest with FEAT_E2H0.

Tested on QEMU, booting a guest hypervisor running with FEAT_E2H0, plus the nested at, hello_el2 and get-reg-list selftests. No regressions.

On QEMU's max CPU (52-bit IPA limit), the E2H0 guest hypervisor's KVM init doesn't complete within 20 minutes, with or without this series:
on every TLBI ALLE1IS, KVM unmaps the nested stage-2s over 52 bits, while the guest only sees 48. I've posted a fix for that [1], and the testing above was with it applied.

For the series:

Cheers,
/fuad

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

maintainer Applied
#14 lore raw
In reply to Oliver Upton
Quoted 6 lines · Oliver Upton

I went ahead and grabbed Wei-Lin's series [1] that added TCR_EL2 sanitisation since it makes life quite a bit easier for computing an effective TCR_EL1.

Tested on Neoverse-V2 running a guest with FEAT_E2H0.

Quoted 7 lines

v2 -> v3:
- Collect R-b's from Joey
- Add patch "KVM: arm64: nv: Correctly compute the effective TCR_EL1 for nVHE TCR_EL2", itself picking up fixes for forwarding missing fields into the effective TCR_EL1 value (Fuad)

[...]

Applied to fixes, thanks!

[1/5] KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1

      commit: 192a20f6e105e679c48e98c66af6336d37095f09
[2/5] arm64: sysreg: Add TCR_EL2 to sysreg infrastructure
      commit: 2942baff3a951e0805d0ce9608f318e21c7e3744
[3/5] KVM: arm64: Reset TCR_EL2 according to guest's E2H mode
      commit: 81755e75f86c490bc9296f6cec0cd221edecaa27
[4/5] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation
      commit: 388faffbd371fd367d39535ad2aa07fbbd745c08
[5/5] KVM: arm64: nv: Correctly compute the effective TCR_EL1 for nVHE TCR_EL2
      commit: 361564a8d4305100ee4140ff7b345d1bf2c1a1b2

Cheers,

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