Skip to contentSkip to conversation

KVM: arm64: Properly advertise !FEAT_LPA2 for NV

PATCH Superseded Wei-Lin Chang 4 participants 13 messages Sep 9 – Sep 11

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover KVM: arm64: Properly advertise !FEAT_LPA2 for NV

Hi,

Previously for NV FEAT_LPA2 is accidentally exposed to the guest at stage-1 if hardware supports it. Stage-2 is correctly sanitised out in ID_AA64MMFR0_EL1 but not stage-1. This is wrong on two fronts:

- There is not FEAT_LPA2 support for NV yet.
- It is non-architectural to advertise different FEAT_LPA2 support in stage-1 and stage-2.

This series first removes FEAT_LPA2 from TGRAN4, TGRAN16 for NV, to present a !FEAT_LPA2 system. Then guest TCR_EL2 sanitisation is added, to make the DS bit RES0 for NV guests. Note that this sanitisation can't cover all cases, as we don't trap VHE L1 accesses to the vCPU's TCR_EL2 via TCR_EL1.

TCR_EL2 sanitisation differs slightly from the AARCHMRS, please see the commits for details.

For testing I booted a 4K 52-bit nVHE L1 and checked:
1. It is using 48-bit VAs in stage-1.
2. Writing 1 to TCR_EL2.DS and reading it back reads 0.

Series based on v7.3-rc2.

Thanks!

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/sysreg.h   |   1 -
 arch/arm64/kvm/config.c           | 113 +++++++++++++++++++++++++++
 arch/arm64/kvm/nested.c           |  17 ++++
 arch/arm64/tools/sysreg           | 125 ++++++++++++++++++++++++++++++
 6 files changed, 256 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

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

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.

---
 arch/arm64/kvm/nested.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/arm64/kvm/nested.c +13 -0
index 3c4fc566eafc..1fd98117df23 100644
@@ -1731,6 +1731,19 @@ 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.43.0
#2 lore raw

Re: [PATCH v1 1/3] KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Equality checks for TGRAN4 and TGRAN16 fail to mask future architectural enum values, leaking…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Equality checks for TGRAN4 and TGRAN16 fail to mask future architectural enum values, leaking unsupported hardware capabilities to NV guests. --

commit 6c2ebbb5542b3e22eaee90b7cf8d8cbbc9704e7c
Author: Wei-Lin Chang <[email protected]>

KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1

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.

arch/arm64/kvm/nested.c u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val) · line 1736

Quoted 11 lines
arch/arm64/kvm/nested.c +3 -0
index 17123f0b6daba..4d1884b32fc48 100644
@@ -1731,6 +1731,19 @@ 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) {

[Severity: Medium]
Will this exact equality check fail to mask future architectural enum values?

If future hardware implements a capability enum value greater than 52_BIT for TGRAN4, this check will evaluate to false, leaking the unsupported capability to NV guests.

Could this be capped instead, perhaps using inequalities or the ID_REG_LIMIT_FIELD_ENUM macro, to maintain forward compatibility?

arch/arm64/kvm/nested.c u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val) · line 1742

Quoted 6 lines
arch/arm64/kvm/nested.c +6 -0
+			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) {

[Severity: Medium]
Similarly, will this exact equality check fail to mask future unsigned capability values greater than 52_BIT for TGRAN16?

Quoted 7 lines
arch/arm64/kvm/nested.c +4 -0
+			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
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v1 1/3] KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1

Quoted 28 lines · On Wed, Sep 09, 2026 at 10:44:58PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Equality checks for TGRAN4 and TGRAN16 fail to mask future architectural enum values, leaking unsupported hardware capabilities to NV guests. --

commit 6c2ebbb5542b3e22eaee90b7cf8d8cbbc9704e7c
Author: Wei-Lin Chang <[email protected]>

KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1

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.

arch/arm64/kvm/nested.c u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val) · line 1736

Quoted 11 lines
arch/arm64/kvm/nested.c +3 -0
index 17123f0b6daba..4d1884b32fc48 100644
@@ -1731,6 +1731,19 @@ 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) {

[Severity: Medium]
Will this exact equality check fail to mask future architectural enum values?

Quoted 6 lines

If future hardware implements a capability enum value greater than 52_BIT for TGRAN4, this check will evaluate to false, leaking the unsupported capability to NV guests.

Could this be capped instead, perhaps using inequalities or the ID_REG_LIMIT_FIELD_ENUM macro, to maintain forward compatibility?

ID_REG_LIMIT_FIELD_ENUM works only for unsigned fields, perhaps it is worth extending it so it can deal with signed enums too.

Thanks,
Wei-Lin Chang

Quoted 22 lines

arch/arm64/kvm/nested.c u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val) · line 1742

Quoted 7 lines
arch/arm64/kvm/nested.c +6 -0
+			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) {

[Severity: Medium]
Similarly, will this exact equality check fail to mask future unsigned capability values greater than 52_BIT for TGRAN16?

Quoted 7 lines
arch/arm64/kvm/nested.c +4 -0
+			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
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#4 lore raw

Re: [PATCH v1 1/3] KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1

Quoted 34 lines · Wei-Lin Chang
Quoted 32 lines · On Wed, Sep 09, 2026 at 10:44:58PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Equality checks for TGRAN4 and TGRAN16 fail to mask future architectural enum values, leaking unsupported hardware capabilities to NV guests. --

commit 6c2ebbb5542b3e22eaee90b7cf8d8cbbc9704e7c
Author: Wei-Lin Chang <[email protected]>

KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1

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.

arch/arm64/kvm/nested.c u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val) · line 1736

Quoted 11 lines
arch/arm64/kvm/nested.c +3 -0
index 17123f0b6daba..4d1884b32fc48 100644
@@ -1731,6 +1731,19 @@ 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) {

[Severity: Medium]
Will this exact equality check fail to mask future architectural enum values?

If future hardware implements a capability enum value greater than 52_BIT for TGRAN4, this check will evaluate to false, leaking the unsupported capability to NV guests.

Quoted 5 lines
Quoted 2 lines

Could this be capped instead, perhaps using inequalities or the ID_REG_LIMIT_FIELD_ENUM macro, to maintain forward compatibility?

ID_REG_LIMIT_FIELD_ENUM works only for unsigned fields, perhaps it is worth extending it so it can deal with signed enums too.

Don't bother. We're not doing "crystal ball architecture".

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

Re: [PATCH v1 1/3] KVM: arm64: nv: Don't advertise FEAT_LPA2 for guest stage-1

In reply to Wei-Lin Chang
Quoted 15 lines · Wei-Lin Chang

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.

---
 arch/arm64/kvm/nested.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/arm64/kvm/nested.c +0 -0
index 3c4fc566eafc..1fd98117df23 100644

arch/arm64/kvm/nested.c u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val) · line 1736

Quoted 6 lines
arch/arm64/kvm/nested.c +3 -0
@@ -1731,6 +1731,19 @@ 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) {

nit: keep both sides of == on a single line.

arch/arm64/kvm/nested.c u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val) · line 1749

Quoted 13 lines
arch/arm64/kvm/nested.c +10 -0
+			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:

In theory, we should also change the userspace interface to accept (but ignore) FEAT_LPA2 being set from userspace.

But since there is no LPA2 HW in the wild, I'm not too worried about that. You probably want to Cc stable on this patch though.

Thanks,

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

PATCH 2/3 arm64: sysreg: Add TCR_EL2 to sysreg infrastructure

In reply to Wei-Lin Chang

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 4bfbd827c5aa..690c13d0b068 100644
@@ -108,19 +108,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 7aa08d59d494..3499e5fab39e 100644
@@ -514,7 +514,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.43.0
#7 lore raw

Re: [PATCH v1 2/3] arm64: sysreg: Add TCR_EL2 to sysreg infrastructure

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Declarative update to TCR_EL2 regenerates the TCR_EL2_RES1 aggregate mask which is consumed by C…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Declarative update to TCR_EL2 regenerates the TCR_EL2_RES1 aggregate mask which is consumed by C initialization, requiring confirmation of robustness to future value changes. --

commit 522a184d95be9c0f88cc6fb418668546ef83e3e4
Author: Wei-Lin Chang <[email protected]>

arm64: sysreg: Add TCR_EL2 to sysreg infrastructure

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

Quoted 4 lines
arch/arm64/tools/sysreg +0 -0
index 94bf065c8ac71..8d89f34580bac 100644

[ ... ]

Quoted 1 line

+Sysreg TCR_EL2 3 4 2 0 2

[ ... ]

arch/arm64/tools/sysreg Res0 6 · line 5656

Quoted 12 lines
arch/arm64/tools/sysreg +12 -0
+Field	33	MTX
+Field	32	DS
+Res1	31
+Field	30	TCMA
+Field	29	TBID
+Field	28	HWU62
+Field	27	HWU61
+Field	26	HWU60
+Field	25	HWU59
+Field	24	HPD
+Res1	23
+Field	22	HD

[Severity: Medium]
This isn't a bug, but this declarative update regenerates the TCR_EL2_RES1 aggregate mask. Are the C initialization consumers of this mask robust to its value silently changing if a future .sysreg patch adds or removes a Res1 bit?

For example, the generated mask is OR'd into the initial EL2 register value:

arch/arm64/kvm/arm.c:cpu_prepare_hyp_mode() {
    ...
    tcr |= TCR_EL2_RES1 | FIELD_PREP(TCR_EL2_PS_MASK, ips);
    ...
}

It is also used to initialize the guest emulation reset state in:

arch/arm64/kvm/sys_regs.c:sys_reg_descs[] = {
    ...
    EL2_REG(TCR_EL2, access_rw, reset_val, TCR_EL2_RES1),
    ...
}

Because the generator's contract is to reflect global architectural truth, a future edit that reclassifies a bit as Res1 will silently change the value of TCR_EL2_RES1 without triggering a compiler or CI warning. Could you confirm that these consumers do not rely on the previous static semantic value of the mask?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

Re: [PATCH v1 2/3] arm64: sysreg: Add TCR_EL2 to sysreg infrastructure

Quoted 54 lines · On Wed, Sep 09, 2026 at 10:38:10PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Declarative update to TCR_EL2 regenerates the TCR_EL2_RES1 aggregate mask which is consumed by C initialization, requiring confirmation of robustness to future value changes. --

commit 522a184d95be9c0f88cc6fb418668546ef83e3e4
Author: Wei-Lin Chang <[email protected]>

arm64: sysreg: Add TCR_EL2 to sysreg infrastructure

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

Quoted 4 lines
arch/arm64/tools/sysreg +0 -0
index 94bf065c8ac71..8d89f34580bac 100644

[ ... ]

Quoted 1 line

+Sysreg TCR_EL2 3 4 2 0 2

[ ... ]

arch/arm64/tools/sysreg Res0 6 · line 5656

Quoted 12 lines
arch/arm64/tools/sysreg +12 -0
+Field	33	MTX
+Field	32	DS
+Res1	31
+Field	30	TCMA
+Field	29	TBID
+Field	28	HWU62
+Field	27	HWU61
+Field	26	HWU60
+Field	25	HWU59
+Field	24	HPD
+Res1	23
+Field	22	HD

[Severity: Medium]
This isn't a bug, but this declarative update regenerates the TCR_EL2_RES1 aggregate mask. Are the C initialization consumers of this mask robust to its value silently changing if a future .sysreg patch adds or removes a Res1 bit?

For example, the generated mask is OR'd into the initial EL2 register value:

arch/arm64/kvm/arm.c:cpu_prepare_hyp_mode() {
    ...
    tcr |= TCR_EL2_RES1 | FIELD_PREP(TCR_EL2_PS_MASK, ips);
    ...
}

It is also used to initialize the guest emulation reset state in:

arch/arm64/kvm/sys_regs.c:sys_reg_descs[] = {
    ...
    EL2_REG(TCR_EL2, access_rw, reset_val, TCR_EL2_RES1),
    ...
}
Quoted 5 lines

Because the generator's contract is to reflect global architectural truth, a future edit that reclassifies a bit as Res1 will silently change the value of TCR_EL2_RES1 without triggering a compiler or CI warning. Could you confirm that these consumers do not rely on the previous static semantic value of the mask?

I don't fully understand but doesn't this mean the architecture introduced a change that breaks existing software?

Thanks,
Wei-Lin Chang

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#9 lore raw

Re: [PATCH v1 2/3] arm64: sysreg: Add TCR_EL2 to sysreg infrastructure

Quoted 40 lines · Wei-Lin Chang
Quoted 38 lines · On Wed, Sep 09, 2026 at 10:38:10PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Declarative update to TCR_EL2 regenerates the TCR_EL2_RES1 aggregate mask which is consumed by C initialization, requiring confirmation of robustness to future value changes. --

commit 522a184d95be9c0f88cc6fb418668546ef83e3e4
Author: Wei-Lin Chang <[email protected]>

arm64: sysreg: Add TCR_EL2 to sysreg infrastructure

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

Quoted 4 lines
arch/arm64/tools/sysreg +0 -0
index 94bf065c8ac71..8d89f34580bac 100644

[ ... ]

Quoted 1 line

+Sysreg TCR_EL2 3 4 2 0 2

[ ... ]

arch/arm64/tools/sysreg

Quoted 12 lines
arch/arm64/tools/sysreg +12 -0
+Field	33	MTX
+Field	32	DS
+Res1	31
+Field	30	TCMA
+Field	29	TBID
+Field	28	HWU62
+Field	27	HWU61
+Field	26	HWU60
+Field	25	HWU59
+Field	24	HPD
+Res1	23
+Field	22	HD

[Severity: Medium]
This isn't a bug, but this declarative update regenerates the TCR_EL2_RES1 aggregate mask. Are the C initialization consumers of this mask robust to its value silently changing if a future .sysreg patch adds or removes a Res1 bit?

Quoted 7 lines
Quoted 7 lines

For example, the generated mask is OR'd into the initial EL2 register value:

arch/arm64/kvm/arm.c:cpu_prepare_hyp_mode() {
    ...
    tcr |= TCR_EL2_RES1 | FIELD_PREP(TCR_EL2_PS_MASK, ips);
    ...
}

Nonsense. In this case, we run nVHE, and we use the !VHE definition. The hVHE path correctly derives the setting from TCR_EL1, which has the same format.

Quoted 8 lines
Quoted 8 lines

It is also used to initialize the guest emulation reset state in:

arch/arm64/kvm/sys_regs.c:sys_reg_descs[] = {
    ...
    EL2_REG(TCR_EL2, access_rw, reset_val, TCR_EL2_RES1),
    ...
}

This one is a potential bug, but not in the current revision of the architecture. Bits 23 and 31 are RES1 when E2H==0, but reset as UNKNOWN when E2H==1, so nothing breaks. This is just ugly.

Quoted 9 lines
Quoted 6 lines

Because the generator's contract is to reflect global architectural truth, a future edit that reclassifies a bit as Res1 will silently change the value of TCR_EL2_RES1 without triggering a compiler or CI warning. Could you confirm that these consumers do not rely on the previous static semantic value of the mask?

I don't fully understand but doesn't this mean the architecture introduced a change that breaks existing software?

That's exactly what VHE (and by extension HCR_EL2.E2H being RES1) did. Which is why I can't wait for FEAT_E2H0 to be eradicated (it is already forbidden in 9.6 as FEAT_SRMASK is mandatory).

I think this is simply a case of providing a specific reset function for TCR_EL2 (untested).

	M.
arch/arm64/kvm/sys_regs.c +9 -1
index 2c90e185c6e8e..964ecbfde9bb6 100644
@@ -3275,6 +3275,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
@@ -3853,7 +3861,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
Without deviation from the norm, progress is not possible.
Reviewed-by on v1 2/3
#10 lore raw

Re: [PATCH v1 2/3] arm64: sysreg: Add TCR_EL2 to sysreg infrastructure

In reply to Wei-Lin Chang
Quoted 3 lines · Wei-Lin Chang

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

Checks out against DDI0601 2026-06.

arch/arm64/tools/sysreg Res0 6 · line 5627

Quoted 5 lines
arch/arm64/tools/sysreg +5 -0
+Enum	13:12	SH0
+	0b00	NONE
+	0b10	OUTER
+	0b11	INNER
+EndEnum

Could do NS, OS, IS but that's bikeshedding. Either way:

#11 lore raw

PATCH 3/3 KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

In reply to Wei-Lin Chang

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           | 113 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/nested.c           |   4 ++
 3 files changed, 118 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +1 -1
index 27fe0cd5b2d7..6a5e22ca3430 100644
@@ -516,7 +516,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) */
@@ -541,6 +540,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 +113 -0

151 lines not on this page · raw message

arch/arm64/kvm/nested.c +4 -0
index 1fd98117df23..5aa16d9d555b 100644
@@ -1959,6 +1959,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.43.0
maintainer
#12 lore raw

Re: [PATCH v1 3/3] KVM: arm64: Convert TCR_EL2 to config-driven sanitisation

Quoted 7 lines · Wei-Lin Chang

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:

Quoted 8 lines

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

The runtime stuff is never something we can do as part of the static initialisation. I think the way this is captured in the JSON is a bit hackish, and it should probably say:

IsFeatureImplemented(FEAT_LPA2) && (EffectiveValue(TCR2_EL2.D128) == '0')

which would naturally work.

Quoted 79 lines

- 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           | 113 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/nested.c           |   4 ++
 3 files changed, 118 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +1 -1
index 27fe0cd5b2d7..6a5e22ca3430 100644
@@ -516,7 +516,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) */
@@ -541,6 +540,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 +20 -0
index 1053676551af..3ccf5359a373 100644
@@ -203,8 +203,11 @@ struct reg_feat_map_desc {
 #define FEAT_CPA2		ID_AA64ISAR3_EL1, CPA, CPA2
 #define FEAT_ASID2		ID_AA64MMFR4_EL1, ASID2, IMP
 #define FEAT_MEC		ID_AA64MMFR3_EL1, MEC, IMP
+#define FEAT_HAF		ID_AA64MMFR1_EL1, HAFDBS, AF
 #define FEAT_HAFT		ID_AA64MMFR1_EL1, HAFDBS, HAFT
+#define FEAT_HAFDBS		ID_AA64MMFR1_EL1, HAFDBS, DBM
 #define FEAT_HDBSS		ID_AA64MMFR1_EL1, HAFDBS, HDBSS
+#define FEAT_HPDS		ID_AA64MMFR1_EL1, HPDS, IMP
 #define FEAT_HPDS2		ID_AA64MMFR1_EL1, HPDS, HPDS2
 #define FEAT_BTI		ID_AA64PFR1_EL1, BT, IMP
 #define FEAT_ExS		ID_AA64MMFR0_EL1, EXS, IMP
@@ -215,6 +218,10 @@ struct reg_feat_map_desc {
 #define FEAT_MixedEndEL0	ID_AA64MMFR0_EL1, BIGENDEL0, IMP
 #define FEAT_MTE_ASYNC		ID_AA64PFR1_EL1, MTE_frac, ASYNC
 #define FEAT_MTE_STORE_ONLY	ID_AA64PFR2_EL1, MTESTOREONLY, IMP
+#define FEAT_MTE_CANONICAL_TAGS	ID_AA64PFR1_EL1, MTEX, MTE4
+#define FEAT_MTE_NO_ADDRESS_TAGS	ID_AA64PFR1_EL1, MTEX, MTE4
+#define FEAT_E0PD		ID_AA64MMFR2_EL1, E0PD, IMP
+#define FEAT_SVE		ID_AA64PFR0_EL1, SVE, IMP
 #define FEAT_PAN		ID_AA64MMFR1_EL1, PAN, IMP
 #define FEAT_PAN3		ID_AA64MMFR1_EL1, PAN, PAN3
 #define FEAT_SSBS		ID_AA64PFR1_EL1, SSBS, IMP
@@ -227,6 +234,19 @@ struct reg_feat_map_desc {
 #define FEAT_GCIE		ID_AA64PFR2_EL1, GCIE, IMP
 #define FEAT_NV3		ID_AA64MMFR4_EL1, NV_frac, NV3
 
+static bool feat_asid16(struct kvm *kvm)
+{
+	return kvm_has_feat_enum(kvm, ID_AA64MMFR0_EL1, ASIDBITS, 16);
+}
+
+static bool feat_mte_no_addr_tags_or_canonical_tags(struct kvm *kvm)
+{
+	bool no_addr_tags = kvm_has_feat(kvm, FEAT_MTE_NO_ADDRESS_TAGS);
+	bool canonical_tags = kvm_has_feat(kvm, FEAT_MTE_CANONICAL_TAGS);
+
+	return no_addr_tags || canonical_tags;
+}
+
 static bool not_feat_aa64el3(struct kvm *kvm)
 {
 	return !kvm_has_feat(kvm, FEAT_AA64EL3);

arch/arm64/kvm/config.c static const struct reg_bits_to_feat_map sctlr2_feat_map[] = { · line 1067

Quoted 4 lines
arch/arm64/kvm/config.c +1 -0
@@ -1044,6 +1064,91 @@ static const struct reg_bits_to_feat_map sctlr2_feat_map[] = {
 static const DECLARE_FEAT_MAP(sctlr2_desc, SCTLR2_EL1,
 			      sctlr2_feat_map, FEAT_SCTLR2);
 
+static const struct reg_bits_to_feat_map tcr_el2_vhe_feat_map[] = {

Maybe be consistent with the generated field names and call the array 'vhe_tcr_el2_feat_map'. Similarly for all the other names.

Quoted 46 lines
arch/arm64/kvm/config.c +46 -0
+	NEEDS_FEAT(VHE_TCR_EL2_MTX1	|
+		   VHE_TCR_EL2_MTX0,
+		   feat_mte_no_addr_tags_or_canonical_tags),
+	NEEDS_FEAT(VHE_TCR_EL2_DS, feat_lpa2),
+	NEEDS_FEAT(VHE_TCR_EL2_TCMA1	|
+		   VHE_TCR_EL2_TCMA0,
+		   FEAT_MTE2),
+	NEEDS_FEAT(VHE_TCR_EL2_E0PD1	|
+		   VHE_TCR_EL2_E0PD0,
+		   FEAT_E0PD),
+	NEEDS_FEAT(VHE_TCR_EL2_NFD1	|
+		   VHE_TCR_EL2_NFD0,
+		   FEAT_SVE),
+	NEEDS_FEAT(VHE_TCR_EL2_TBID1	|
+		   VHE_TCR_EL2_TBID0,
+		   feat_pauth),
+	NEEDS_FEAT(VHE_TCR_EL2_HWU162	|
+		   VHE_TCR_EL2_HWU161	|
+		   VHE_TCR_EL2_HWU160	|
+		   VHE_TCR_EL2_HWU159	|
+		   VHE_TCR_EL2_HWU062	|
+		   VHE_TCR_EL2_HWU061	|
+		   VHE_TCR_EL2_HWU060	|
+		   VHE_TCR_EL2_HWU059,
+		   FEAT_HPDS2),
+	NEEDS_FEAT(VHE_TCR_EL2_HPD1	|
+		   VHE_TCR_EL2_HPD0,
+		   FEAT_HPDS),
+	NEEDS_FEAT(VHE_TCR_EL2_HD, FEAT_HAFDBS),
+	NEEDS_FEAT(VHE_TCR_EL2_HA, FEAT_HAF),
+	NEEDS_FEAT(VHE_TCR_EL2_AS, feat_asid16),
+	NEEDS_FEAT(VHE_TCR_EL2_TBI1		|
+		   VHE_TCR_EL2_TBI0		|
+		   VHE_TCR_EL2_IPS		|
+		   VHE_TCR_EL2_TG1		|
+		   VHE_TCR_EL2_SH1		|
+		   VHE_TCR_EL2_ORGN1		|
+		   VHE_TCR_EL2_IRGN1		|
+		   VHE_TCR_EL2_EPD1		|
+		   VHE_TCR_EL2_A1		|
+		   VHE_TCR_EL2_T1SZ		|
+		   VHE_TCR_EL2_TG0		|
+		   VHE_TCR_EL2_SH0		|
+		   VHE_TCR_EL2_ORGN0		|
+		   VHE_TCR_EL2_IRGN0		|
+		   VHE_TCR_EL2_EPD0		|

arch/arm64/kvm/config.c static const struct reg_bits_to_feat_map sctlr2_feat_map[] = { · line 1123

Quoted 10 lines
arch/arm64/kvm/config.c +10 -0
+		   VHE_TCR_EL2_T0SZ,
+		   FEAT_AA64EL2),
+	FORCE_RES0(VHE_TCR_EL2_RES0),
+	FORCE_RES1(VHE_TCR_EL2_RES1),
+};
+
+static const DECLARE_FEAT_MAP(tcr_el2_vhe_desc, VHE_TCR_EL2,
+			      tcr_el2_vhe_feat_map, FEAT_AA64EL2);
+
+static const struct reg_bits_to_feat_map tcr_el2_nvhe_feat_map[] = {

and drop the nvhe here.

Thanks,

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