Skip to contentSkip to conversation

arm64: KVM: Backport VHE-only boot fixes

PATCH Superseded Colton Lewis 3 participants 14 messages Jul 8 – Jul 9

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover arm64: KVM: Backport VHE-only boot fixes

Architectural updates retroactively made FEAT_E2H0 optional, meaning hardware can implement FEAT_VHE without FEAT_E2H0. On such CPUs, HCR_EL2.E2H can reset to an unknown state and must be initialized early so later code can reliably detect whether E2H mode is active.

Without these fixes, booting 6.6.y as a guest under KVM nested virtualization will hang at boot.

This series targets 6.6.y specifically because these patches are all present in 6.12.y and applying these patches to 6.1.y and presumably older kernels has more conflicts and results in other issues booting as a guest under nested virtualization. More work is needed to enable that.

Changes in v2:
- Updated commit messages to reference correct upstream SHA1s.
- Preserved inline EL2 state initialization in Patch 4 to avoid unrelated code churn.
- Added upstream commit 3855a7b91d42 ("KVM: arm64: Initialize SCTLR_EL1 in __kvm_hyp_init_cpu()") as Patch 5.
- Verified boot and KVM initialization across all KVM execution modes (nVHE, hVHE/nested, VHE, and protected).

v1:
https://lore.kernel.org/kvmarm/[email protected]/

Ahmed Genidi (1):
KVM: arm64: Initialize SCTLR_EL1 in __kvm_hyp_init_cpu()

Marc Zyngier (4):
arm64: sysreg: Add layout for ID_AA64MMFR4_EL1
arm64: Treat HCR_EL2.E2H as RES1 when ID_AA64MMFR4_EL1.E2H0 is
negative
arm64: Fix early handling of FEAT_E2H0 not being implemented
arm64: Revamp HCR_EL2.E2H RES1 detection

Mark Rutland (1):
KVM: arm64: Initialize HCR_EL2.E2H early

 arch/arm64/include/asm/el2_setup.h   | 56 ++++++++++++++++++++++++++--
 arch/arm64/kernel/head.S             | 20 ++++------
 arch/arm64/kvm/hyp/nvhe/hyp-init.S   |  3 +-
 arch/arm64/kvm/hyp/nvhe/psci-relay.c |  3 ++
 arch/arm64/tools/sysreg              | 37 ++++++++++++++++++
 5 files changed, 101 insertions(+), 18 deletions(-)

base-commit: da47cbc254661aa66d61ef061485a7080305c4be --
2.55.0.795.g602f6c329a-goog

#1 lore raw

PATCH v2 1/6 arm64: sysreg: Add layout for ID_AA64MMFR4_EL1

From: Marc Zyngier <[email protected]>

[ Upstream commit cfc680bb04c54e61faa51a34d8383a0aa25b583f ]

ARMv9.5 has infroduced ID_AA64MMFR4_EL1 with a bunch of new features. Add the corresponding layout.

This is extracted from the public ARM SysReg_xml_A_profile-2023-09 delivery, timestamped d55f5af8e09052abe92a02adf820deea2eaed717.

---
 arch/arm64/tools/sysreg | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
arch/arm64/tools/sysreg +37 -0
index 76ce150e7347e..f7180d391f829 100644
@@ -1669,6 +1669,43 @@ UnsignedEnum	3:0	TCRX
 EndEnum
 EndSysreg
 
+Sysreg	ID_AA64MMFR4_EL1	3	0	0	7	4
+Res0	63:40
+UnsignedEnum	39:36	E3DSE
+	0b0000	NI
+	0b0001	IMP
+EndEnum
+Res0	35:28
+SignedEnum	27:24	E2H0
+	0b0000	IMP
+	0b1110	NI_NV1
+	0b1111	NI
+EndEnum
+UnsignedEnum	23:20	NV_frac
+	0b0000	NV_NV2
+	0b0001	NV2_ONLY
+EndEnum
+UnsignedEnum	19:16	FGWTE3
+	0b0000	NI
+	0b0001	IMP
+EndEnum
+UnsignedEnum	15:12	HACDBS
+	0b0000	NI
+	0b0001	IMP
+EndEnum
+UnsignedEnum	11:8	ASID2
+	0b0000	NI
+	0b0001	IMP
+EndEnum
+SignedEnum	7:4	EIESB
+	0b0000	NI
+	0b0001	ToEL3
+	0b0010	ToELx
+	0b1111	ANY
+EndEnum
+Res0	3:0
+EndSysreg
+
 Sysreg	SCTLR_EL1	3	0	1	0	0
 Field	63	TIDCP
 Field	62	SPINTMASK
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#2 lore raw

Re: [PATCH 6.6 v2 1/6] arm64: sysreg: Add layout for ID_AA64MMFR4_EL1

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `EIESB` field in `ID_AA64MMFR4_EL1` is incorrectly defined as a `SignedEnum` instead of an…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The EIESB field in ID_AA64MMFR4_EL1 is incorrectly defined as a SignedEnum instead of an UnsignedEnum, which will break cross-CPU feature resolution and capability detection. --

--- Patch [1]: [PATCH 6.6 v2 1/6] arm64: sysreg: Add layout for ID_AA64MMFR4_EL1 --- commit 553e432fbf7b950e883b1046b692639bcd77b3c1
Author: Marc Zyngier <[email protected]>

arm64: sysreg: Add layout for ID_AA64MMFR4_EL1

ARMv9.5 has introduced ID_AA64MMFR4_EL1 with a bunch of new features. Add the corresponding layout.

Quoted 5 lines
arch/arm64/tools/sysreg +0 -0
index 76ce150e7347e..f7180d391f829 100644
@@ -1669,6 +1669,43 @@ UnsignedEnum	3:0	TCRX

[ ... ]

arch/arm64/tools/sysreg UnsignedEnum 3:0 TCRX · line 1705

Quoted 10 lines
arch/arm64/tools/sysreg +10 -0
+UnsignedEnum	11:8	ASID2
+	0b0000	NI
+	0b0001	IMP
+EndEnum
+SignedEnum	7:4	EIESB
+	0b0000	NI
+	0b0001	ToEL3
+	0b0010	ToELx
+	0b1111	ANY
+EndEnum

[Severity: Medium]
Will defining EIESB as a SignedEnum cause issues with capability detection?

If this generates ID_AA64MMFR4_EL1_EIESB_SIGNED as true, it seems like the cpufeature framework will extract and evaluate the 4-bit field 0b1111 (ANY) as -1.

Since -1 < 2 (ToELx), could this cause the kernel to fail to detect the ANY error injection capability or resolve to the wrong capability level on heterogeneous systems?

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

Re: [PATCH 6.6 v2 1/6] arm64: sysreg: Add layout for ID_AA64MMFR4_EL1

In reply to Colton Lewis
Quoted 11 lines · Colton Lewis

From: Marc Zyngier <[email protected]>

[ Upstream commit cfc680bb04c54e61faa51a34d8383a0aa25b583f ]

ARMv9.5 has infroduced ID_AA64MMFR4_EL1 with a bunch of new features. Add the corresponding layout.

This is extracted from the public ARM SysReg_xml_A_profile-2023-09 delivery, timestamped d55f5af8e09052abe92a02adf820deea2eaed717.

Quoted 6 lines

Please read Documentation/process/backporting.rst, and in particular the section indicating the requirement for your own SoB.

None of these patches can be merged if this is missing.

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#4 lore raw

Re: [PATCH 6.6 v2 1/6] arm64: sysreg: Add layout for ID_AA64MMFR4_EL1

Hi Marc,

Quoted 20 lines · Marc Zyngier
Quoted 10 lines · Colton Lewis

From: Marc Zyngier <[email protected]>

[ Upstream commit cfc680bb04c54e61faa51a34d8383a0aa25b583f ]

ARMv9.5 has infroduced ID_AA64MMFR4_EL1 with a bunch of new features. Add the corresponding layout.

This is extracted from the public ARM SysReg_xml_A_profile-2023-09 delivery, timestamped d55f5af8e09052abe92a02adf820deea2eaed717.

Quoted 6 lines
Quoted 4 lines

Please read Documentation/process/backporting.rst, and in particular the section indicating the requirement for your own SoB.

None of these patches can be merged if this is missing.

That taught me a few tricks. Thanks. And I'll make sure to include my SoB.

Quoted 4 lines
	M.

--
Jazz isn't dead. It just smells funny.

#5 lore raw

PATCH v2 2/6 arm64: Treat HCR_EL2.E2H as RES1 when ID_AA64MMFR4_EL1.E2H0 is negative

In reply to Colton Lewis

From: Marc Zyngier <[email protected]>

[ Upstream commit 3944382fa6f22b54fd399632b1af92c28123979b ]

For CPUs that have ID_AA64MMFR4_EL1.E2H0 as negative, it is important to avoid the boot path that sets HCR_EL2.E2H=0. Fortunately, we already have this path to cope with fruity CPUs.

Tweak init_el2 to look at ID_AA64MMFR4_EL1.E2H0 first.

---
 arch/arm64/kernel/head.S | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
arch/arm64/kernel/head.S +15 -8
index 6517bf2644a08..e32c8dd0b17a7 100644
@@ -589,25 +589,32 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
 	mov_q	x1, INIT_SCTLR_EL1_MMU_OFF
 
 	/*
-	 * Fruity CPUs seem to have HCR_EL2.E2H set to RES1,
-	 * making it impossible to start in nVHE mode. Is that
-	 * compliant with the architecture? Absolutely not!
+	 * Compliant CPUs advertise their VHE-onlyness with
+	 * ID_AA64MMFR4_EL1.E2H0 < 0. HCR_EL2.E2H can be
+	 * RES1 in that case.
+	 *
+	 * Fruity CPUs seem to have HCR_EL2.E2H set to RES1, but
+	 * don't advertise it (they predate this relaxation).
 	 */
+	mrs_s	x0, SYS_ID_AA64MMFR4_EL1
+	ubfx	x0, x0, #ID_AA64MMFR4_EL1_E2H0_SHIFT, #ID_AA64MMFR4_EL1_E2H0_WIDTH
+	tbnz	x0, #(ID_AA64MMFR4_EL1_E2H0_SHIFT + ID_AA64MMFR4_EL1_E2H0_WIDTH - 1), 1f
+
 	mrs	x0, hcr_el2
 	and	x0, x0, #HCR_E2H
-	cbz	x0, 1f
-
+	cbz	x0, 2f
+1:
 	/* Set a sane SCTLR_EL1, the VHE way */
 	pre_disable_mmu_workaround
 	msr_s	SYS_SCTLR_EL12, x1
 	mov	x2, #BOOT_CPU_FLAG_E2H
-	b	2f
+	b	3f
 
-1:
+2:
 	pre_disable_mmu_workaround
 	msr	sctlr_el1, x1
 	mov	x2, xzr
-2:
+3:
 	__init_el2_nvhe_prepare_eret
 
 	mov	w0, #BOOT_CPU_MODE_EL2
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#6 lore raw

Re: [PATCH 6.6 v2 2/6] arm64: Treat HCR_EL2.E2H as RES1 when ID_AA64MMFR4_EL1.E2H0 is negative

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] The feature check for ID_AA64MMFR4_EL1.E2H0 < 0 will always evaluate to false because tbnz tests a bit that has been unconditionally cleared by ubfx.

Pre-existing issues:
- [High] Writes to control-plane system registers (SYS_SCTLR_EL12 and sctlr_el1) are not immediately followed by an isb() instruction. --

--- Patch [2]: [PATCH 6.6 v2 2/6] arm64: Treat HCR_EL2.E2H as RES1 when ID_AA64MMFR4_EL1.E2H0 is negative --- commit c23e23c763b1ca2a9cc5f6843036bd8b47bf9194
Author: Marc Zyngier <[email protected]>

arm64: Treat HCR_EL2.E2H as RES1 when ID_AA64MMFR4_EL1.E2H0 is negative

For CPUs that have ID_AA64MMFR4_EL1.E2H0 as negative, it is important to avoid the boot path that sets HCR_EL2.E2H=0. This patch tweaks init_el2 to look at ID_AA64MMFR4_EL1.E2H0 first.

Quoted 11 lines
arch/arm64/kernel/head.S +0 -3
index 6517bf2644a08..e32c8dd0b17a7 100644
@@ -589,25 +589,32 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
 	mov_q	x1, INIT_SCTLR_EL1_MMU_OFF
 
 	/*
-	 * Fruity CPUs seem to have HCR_EL2.E2H set to RES1,
-	 * making it impossible to start in nVHE mode. Is that
-	 * compliant with the architecture? Absolutely not!

arch/arm64/kernel/head.S SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) · line 601

Quoted 10 lines
arch/arm64/kernel/head.S +9 -0
+	 * Compliant CPUs advertise their VHE-onlyness with
+	 * ID_AA64MMFR4_EL1.E2H0 < 0. HCR_EL2.E2H can be
+	 * RES1 in that case.
+	 *
+	 * Fruity CPUs seem to have HCR_EL2.E2H set to RES1, but
+	 * don't advertise it (they predate this relaxation).
 	 */
+	mrs_s	x0, SYS_ID_AA64MMFR4_EL1
+	ubfx	x0, x0, #ID_AA64MMFR4_EL1_E2H0_SHIFT, #ID_AA64MMFR4_EL1_E2H0_WIDTH
+	tbnz	x0, #(ID_AA64MMFR4_EL1_E2H0_SHIFT + ID_AA64MMFR4_EL1_E2H0_WIDTH - 1), 1f

[Severity: High]
Does this bit test evaluate correctly? The ubfx instruction extracts 4 bits starting at the shift and places them into bits 3:0 of x0, zero-extending the rest of the register.

However, the tbnz instruction here tests a bit based on the original shift amount plus the width minus 1. Because the upper bits of x0 are unconditionally cleared by ubfx, will this branch ever be taken?

arch/arm64/kernel/head.S SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) · line 610

Quoted 11 lines
arch/arm64/kernel/head.S +3 -2
+
 	mrs	x0, hcr_el2
 	and	x0, x0, #HCR_E2H
-	cbz	x0, 1f
-
+	cbz	x0, 2f
+1:
 	/* Set a sane SCTLR_EL1, the VHE way */
 	pre_disable_mmu_workaround
 	msr_s	SYS_SCTLR_EL12, x1
 	mov	x2, #BOOT_CPU_FLAG_E2H

[Severity: High]
This is a pre-existing issue, but does this system register write need an immediate isb()?

According to ARM64 context synchronization rules, writes to control-plane system registers like SYS_SCTLR_EL12 must be followed by an isb() as the very next instruction. Here, a mov instruction intervenes, which might observe architecturally undefined pipeline state.

arch/arm64/kernel/head.S SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) · line 616

Quoted 8 lines
arch/arm64/kernel/head.S +2 -2
-	b	2f
+	b	3f
 
-1:
+2:
 	pre_disable_mmu_workaround
 	msr	sctlr_el1, x1
 	mov	x2, xzr

[Severity: High]
This is a pre-existing issue, but is the nVHE path missing an isb() here as well?

Similar to the VHE path above, msr sctlr_el1, x1 is immediately followed by a mov instruction rather than a synchronization barrier.

Quoted 3 lines
arch/arm64/kernel/head.S +1 -1
-2:
+3:
 	__init_el2_nvhe_prepare_eret
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

PATCH v2 3/6 arm64: Fix early handling of FEAT_E2H0 not being implemented

In reply to Colton Lewis

From: Marc Zyngier <[email protected]>

[ Upstream commit b3320142f3db9b3f2a23460abd3e22292e1530a5 ]

Commit 3944382fa6f2 introduced checks for the FEAT_E2H0 not being implemented. However, the check is absolutely wrong and makes a point it testing a bit that is guaranteed to be zero.

On top of that, the detection happens way too late, after the init_el2_state has done its job.

This went undetected because the HW this was tested on has E2H being RAO/WI, and not RES1. However, the bug shows up when run as a nested guest, where HCR_EL2.E2H is not necessarily set to 1. As a result, booting the kernel in hVHE mode fails with timer accesses being cought in a trap loop (which was fun to debug).

Fix the check for ID_AA64MMFR4_EL1.E2H0, and set the HCR_EL2.E2H bit early so that it can be checked by the rest of the init sequence.

With this, hVHE works again in a NV environment that doesn't have FEAT_E2H0.

Fixes 3944382fa6f2 ("arm64: Treat HCR_EL2.E2H as RES1 when ID_AA64MMFR4_EL1.E2H0 is negative")
---
 arch/arm64/kernel/head.S | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)
arch/arm64/kernel/head.S +16 -13
index e32c8dd0b17a7..e0e710b36da37 100644
@@ -576,6 +576,21 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
 	isb
 0:
 	mov_q	x0, HCR_HOST_NVHE_FLAGS
+
+	/*
+	 * Compliant CPUs advertise their VHE-onlyness with
+	 * ID_AA64MMFR4_EL1.E2H0 < 0. HCR_EL2.E2H can be
+	 * RES1 in that case. Publish the E2H bit early so that
+	 * it can be picked up by the init_el2_state macro.
+	 *
+	 * Fruity CPUs seem to have HCR_EL2.E2H set to RAO/WI, but
+	 * don't advertise it (they predate this relaxation).
+	 */
+	mrs_s	x1, SYS_ID_AA64MMFR4_EL1
+	tbz	x1, #(ID_AA64MMFR4_EL1_E2H0_SHIFT + ID_AA64MMFR4_EL1_E2H0_WIDTH - 1), 1f
+
+	orr	x0, x0, #HCR_E2H
+1:
 	msr	hcr_el2, x0
 	isb
 
@@ -588,22 +603,10 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
 
 	mov_q	x1, INIT_SCTLR_EL1_MMU_OFF
 
-	/*
-	 * Compliant CPUs advertise their VHE-onlyness with
-	 * ID_AA64MMFR4_EL1.E2H0 < 0. HCR_EL2.E2H can be
-	 * RES1 in that case.
-	 *
-	 * Fruity CPUs seem to have HCR_EL2.E2H set to RES1, but
-	 * don't advertise it (they predate this relaxation).
-	 */
-	mrs_s	x0, SYS_ID_AA64MMFR4_EL1
-	ubfx	x0, x0, #ID_AA64MMFR4_EL1_E2H0_SHIFT, #ID_AA64MMFR4_EL1_E2H0_WIDTH
-	tbnz	x0, #(ID_AA64MMFR4_EL1_E2H0_SHIFT + ID_AA64MMFR4_EL1_E2H0_WIDTH - 1), 1f
-
 	mrs	x0, hcr_el2
 	and	x0, x0, #HCR_E2H
 	cbz	x0, 2f
-1:
+
 	/* Set a sane SCTLR_EL1, the VHE way */
 	pre_disable_mmu_workaround
 	msr_s	SYS_SCTLR_EL12, x1
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#8 lore raw

Re: [PATCH 6.6 v2 3/6] arm64: Fix early handling of FEAT_E2H0 not being implemented

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Writes to control-plane system registers are not immediately followed by an isb(). --

--- Patch [3]: [PATCH 6.6 v2 3/6] arm64: Fix early handling of FEAT_E2H0 not being implemented --- commit 2b993fe86ae9a0ae8ce53be35446a4e7f0a66876
Author: Marc Zyngier <[email protected]>

arm64: Fix early handling of FEAT_E2H0 not being implemented

Commit 3944382fa6f2 introduced checks for FEAT_E2H0 that were incorrect and executed too late. This commit fixes the check for ID_AA64MMFR4_EL1.E2H0 and sets the HCR_EL2.E2H bit early to allow proper initialization for hVHE mode as a nested guest.

Quoted 4 lines
arch/arm64/kernel/head.S +0 -0
index e32c8dd0b17a7..e0e710b36da37 100644

[ ... ]

Quoted 4 lines
arch/arm64/kernel/head.S +0 -0
@@ -588,22 +603,10 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
 
 	mov_q	x1, INIT_SCTLR_EL1_MMU_OFF
 

[ ... ]

arch/arm64/kernel/head.S SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) · line 612

Quoted 8 lines
arch/arm64/kernel/head.S +1 -1
 	mrs	x0, hcr_el2
 	and	x0, x0, #HCR_E2H
 	cbz	x0, 2f
-1:
+
 	/* Set a sane SCTLR_EL1, the VHE way */
 	pre_disable_mmu_workaround
 	msr_s	SYS_SCTLR_EL12, x1

[Severity: High]
This isn't a bug introduced by this patch, but shouldn't writes to control-plane system registers be immediately followed by an isb() instruction?

Looking at init_el2() in arch/arm64/kernel/head.S, as well as __init_el2_hcrx() and __init_el2_timers() in arch/arm64/include/asm/el2_setup.h, there are several locations where registers like SYS_SCTLR_EL12, SYS_HCRX_EL2, and cnthctl_el2 are written without an immediate context synchronization event.

For example, right after the code shown in the diff above, SYS_SCTLR_EL12 is updated:

	msr_s	SYS_SCTLR_EL12, x1
	mov	x2, #BOOT_CPU_FLAG_E2H
	b	3f

Could executing the subsequent branch or ALU instructions under an architecturally undefined pipeline state cause unpredictable behavior or unexpected traps during early boot?

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

PATCH v2 4/6 KVM: arm64: Initialize HCR_EL2.E2H early

In reply to Colton Lewis

From: Mark Rutland <[email protected]>

[ Upstream commit 7a68b55ff39b0a1638acb1694c185d49f6077a0d ]

On CPUs without FEAT_E2H0, HCR_EL2.E2H is RES1, but may reset to an UNKNOWN value out of reset and consequently may not read as 1 unless it has been explicitly initialized.

We handled this for the head.S boot code in commits:

3944382fa6f22b54 ("arm64: Treat HCR_EL2.E2H as RES1 when ID_AA64MMFR4_EL1.E2H0 is negative") b3320142f3db9b3f ("arm64: Fix early handling of FEAT_E2H0 not being implemented")

Unfortunately, we forgot to apply a similar fix to the KVM PSCI entry points used when relaying CPU_ON, CPU_SUSPEND, and SYSTEM SUSPEND. When KVM is entered via these entry points, the value of HCR_EL2.E2H may be consumed before it has been initialized (e.g. by the 'init_el2_state' macro).

Initialize HCR_EL2.E2H early in these paths such that it can be consumed reliably. The existing code in head.S is factored out into a new 'init_el2_hcr' macro, and this is used in the __kvm_hyp_init_cpu() function common to all the relevant PSCI entry points.

For clarity, I've tweaked the assembly used to check whether ID_AA64MMFR4_EL1.E2H0 is negative. The bitfield is extracted as a signed value, and this is checked with a signed-greater-or-equal (GE) comparison.

As the hyp code will reconfigure HCR_EL2 later in ___kvm_hyp_init(), all bits other than E2H are initialized to zero in __kvm_hyp_init_cpu().

Fixes 3944382fa6f22b54 ("arm64: Treat HCR_EL2.E2H as RES1 when ID_AA64MMFR4_EL1.E2H0 is negative")
Fixes b3320142f3db9b3f ("arm64: Fix early handling of FEAT_E2H0 not being implemented")
Cc Ahmed Genidi <[email protected]>
Cc Ben Horgan <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Leo Yan <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Oliver Upton <[email protected]>
Cc Will Deacon <[email protected]>
[maz: fixed LT->GE thinko]
---
 arch/arm64/include/asm/el2_setup.h | 26 ++++++++++++++++++++++++++
 arch/arm64/kernel/head.S           | 19 +------------------
 arch/arm64/kvm/hyp/nvhe/hyp-init.S |  2 ++
 3 files changed, 29 insertions(+), 18 deletions(-)
arch/arm64/include/asm/el2_setup.h +26 -0
index b7afaa026842b..3498dc5d02c18 100644
@@ -16,6 +16,32 @@
 #include <asm/sysreg.h>
 #include <linux/irqchip/arm-gic-v3.h>
 
+.macro init_el2_hcr	val
+	mov_q	x0, \val
+
+	/*
+	 * Compliant CPUs advertise their VHE-onlyness with
+	 * ID_AA64MMFR4_EL1.E2H0 < 0. On such CPUs HCR_EL2.E2H is RES1, but it
+	 * can reset into an UNKNOWN state and might not read as 1 until it has
+	 * been initialized explicitly.
+	 *
+	 * Fruity CPUs seem to have HCR_EL2.E2H set to RAO/WI, but
+	 * don't advertise it (they predate this relaxation).
+	 *
+	 * Initalize HCR_EL2.E2H so that later code can rely upon HCR_EL2.E2H
+	 * indicating whether the CPU is running in E2H mode.
+	 */
+	mrs_s	x1, SYS_ID_AA64MMFR4_EL1
+	sbfx	x1, x1, #ID_AA64MMFR4_EL1_E2H0_SHIFT, #ID_AA64MMFR4_EL1_E2H0_WIDTH
+	cmp	x1, #0
+	b.ge	.LnVHE_\@
+
+	orr	x0, x0, #HCR_E2H
+.LnVHE_\@:
+	msr	hcr_el2, x0
+	isb
+.endm
+
 .macro __init_el2_sctlr
 	mov_q	x0, INIT_SCTLR_EL2_MMU_OFF
 	msr	sctlr_el2, x0
arch/arm64/kernel/head.S +1 -18
index e0e710b36da37..ff7769821166a 100644
@@ -575,25 +575,8 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
 	msr	sctlr_el2, x0
 	isb
 0:
-	mov_q	x0, HCR_HOST_NVHE_FLAGS
-
-	/*
-	 * Compliant CPUs advertise their VHE-onlyness with
-	 * ID_AA64MMFR4_EL1.E2H0 < 0. HCR_EL2.E2H can be
-	 * RES1 in that case. Publish the E2H bit early so that
-	 * it can be picked up by the init_el2_state macro.
-	 *
-	 * Fruity CPUs seem to have HCR_EL2.E2H set to RAO/WI, but
-	 * don't advertise it (they predate this relaxation).
-	 */
-	mrs_s	x1, SYS_ID_AA64MMFR4_EL1
-	tbz	x1, #(ID_AA64MMFR4_EL1_E2H0_SHIFT + ID_AA64MMFR4_EL1_E2H0_WIDTH - 1), 1f
-
-	orr	x0, x0, #HCR_E2H
-1:
-	msr	hcr_el2, x0
-	isb
 
+	init_el2_hcr	HCR_HOST_NVHE_FLAGS
 	init_el2_state
 
 	/* Hypervisor stub */
arch/arm64/kvm/hyp/nvhe/hyp-init.S +2 -0
index 1cc06e6797bda..3efa9cfaa9d48 100644
@@ -202,6 +202,8 @@ SYM_CODE_START_LOCAL(__kvm_hyp_init_cpu)
 
 2:	msr	SPsel, #1			// We want to use SP_EL{1,2}
 
+	init_el2_hcr	0
+
 	/* Initialize EL2 CPU state to sane values. */
 	init_el2_state				// Clobbers x0..x2
 	finalise_el2_state
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#10 lore raw

Re: [PATCH 6.6 v2 4/6] KVM: arm64: Initialize HCR_EL2.E2H early

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Writes to control-plane system registers are not immediately followed by an isb().
- [High] Missing dsb after tlbi alle2 in __pkvm_init_switch_pgd.
--

--- Patch [4]: [PATCH 6.6 v2 4/6] KVM: arm64: Initialize HCR_EL2.E2H early ---
(Note: The standard inline diff formatting has been replaced with a free-form summary in this report due to code recitation restrictions.)

commit 016cd26b0c3a9694155a974c024598368716d8e4
Author: Mark Rutland <[email protected]>
KVM: arm64: Initialize HCR_EL2.E2H early

This patch addresses an issue where HCR_EL2.E2H might read as an UNKNOWN value out of reset on CPUs without FEAT_E2H0 if it is not explicitly initialized. It introduces a new 'init_el2_hcr' macro to initialize this early in the KVM PSCI entry points so it can be reliably consumed.

[Severity: High]
This is a pre-existing issue, but in arch/arm64/kvm/hyp/nvhe/hyp-init.S and arch/arm64/include/asm/el2_setup.h, several writes to control-plane system registers are not immediately followed by an isb(). For example, in ___kvm_hyp_init(), writes to mair_el2 and hcr_el2 are followed by ldr and mov instructions rather than an isb(). Similarly, in __init_el2_fgt(), SYS_HFGRTR_EL2 and SYS_HFGWTR_EL2 are written consecutively without an intervening isb(), and in finalise_el2_state(), writing cpacr_el1 is followed directly by a branch.

Since the subsystem guidelines mandate that every write to a control-plane system register must be followed by an isb() as the very next instruction, could this leave the CPU pipeline in an architecturally undefined state for the intervening instructions and cause unpredictable behavior or spurious traps during boot?

[Severity: High]
This is also a pre-existing issue, but in arch/arm64/kvm/hyp/nvhe/hyp-init.S, __pkvm_init_switch_pgd() issues a "tlbi alle2" instruction and subsequently installs new page tables and re-enables the MMU without executing a dsb in between.

Because the architecture requires a dsb to guarantee the completion of a TLB maintenance instruction, might the TLB invalidation still be pending when the MMU is turned back on? Could this window cause the PE to fetch instructions or data using stale TLB entries, or raise a TLB conflict abort?

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

PATCH v2 5/6 KVM: arm64: Initialize SCTLR_EL1 in __kvm_hyp_init_cpu()

In reply to Colton Lewis

From: Ahmed Genidi <[email protected]>

[ Upstream commit 3855a7b91d42ebf3513b7ccffc44807274978b3d ]

When KVM is in protected mode, host calls to PSCI are proxied via EL2, and cold entries from CPU_ON, CPU_SUSPEND, and SYSTEM_SUSPEND bounce through __kvm_hyp_init_cpu() at EL2 before entering the host kernel's entry point at EL1. While __kvm_hyp_init_cpu() initializes SPSR_EL2 for the exception return to EL1, it does not initialize SCTLR_EL1.

Due to this, it's possible to enter EL1 with SCTLR_EL1 in an UNKNOWN state. In practice this has been seen to result in kernel crashes after CPU_ON as a result of SCTLR_EL1.M being 1 in violation of the initial core configuration specified by PSCI.

Fix this by initializing SCTLR_EL1 for cold entry to the host kernel. As it's necessary to write to SCTLR_EL12 in VHE mode, this initialization is moved into __kvm_host_psci_cpu_entry() where we can use write_sysreg_el1().

The remnants of the '__init_el2_nvhe_prepare_eret' macro are folded into its only caller, as this is clearer than having the macro.

Fixes cdf367192766ad11 ("KVM: arm64: Intercept host's CPU_ON SMCs")
[ Mark: clarify commit message, handle E2H, move to C, remove macro ]
Cc Ahmed Genidi <[email protected]>
Cc Ben Horgan <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Leo Yan <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Oliver Upton <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm64/include/asm/el2_setup.h   | 4 ----
 arch/arm64/kernel/head.S             | 3 ++-
 arch/arm64/kvm/hyp/nvhe/hyp-init.S   | 1 -
 arch/arm64/kvm/hyp/nvhe/psci-relay.c | 3 +++
 4 files changed, 5 insertions(+), 6 deletions(-)
arch/arm64/include/asm/el2_setup.h +0 -4
index 3498dc5d02c18..76b0d50d286d5 100644
@@ -229,10 +229,6 @@
 .Lskip_fgt_\@:
 .endm
 
-.macro __init_el2_nvhe_prepare_eret
-	mov	x0, #INIT_PSTATE_EL1
-	msr	spsr_el2, x0
-.endm
 
 /**
  * Initialize EL2 registers to sane values. This should be called early on all
arch/arm64/kernel/head.S +2 -1
index ff7769821166a..9996029853d23 100644
@@ -601,7 +601,8 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
 	msr	sctlr_el1, x1
 	mov	x2, xzr
 3:
-	__init_el2_nvhe_prepare_eret
+	mov	x0, #INIT_PSTATE_EL1
+	msr	spsr_el2, x0
 
 	mov	w0, #BOOT_CPU_MODE_EL2
 	orr	x0, x0, x2
arch/arm64/kvm/hyp/nvhe/hyp-init.S +0 -1
index 3efa9cfaa9d48..9b2ada54be538 100644
@@ -207,7 +207,6 @@ SYM_CODE_START_LOCAL(__kvm_hyp_init_cpu)
 	/* Initialize EL2 CPU state to sane values. */
 	init_el2_state				// Clobbers x0..x2
 	finalise_el2_state
-	__init_el2_nvhe_prepare_eret
 
 	/* Enable MMU, set vectors and stack. */
 	mov	x0, x28
arch/arm64/kvm/hyp/nvhe/psci-relay.c +3 -0
index d57bcb6ab94d2..5688a16e2ea75 100644
@@ -218,6 +218,9 @@ asmlinkage void __noreturn __kvm_host_psci_cpu_entry(bool is_cpu_on)
 	if (is_cpu_on)
 		release_boot_args(boot_args);
 
+	write_sysreg_el1(INIT_SCTLR_EL1_MMU_OFF, SYS_SCTLR);
+	write_sysreg(INIT_PSTATE_EL1, SPSR_EL2);
+
 	__host_enter(host_ctxt);
 }
 
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#12 lore raw

PATCH v2 6/6 arm64: Revamp HCR_EL2.E2H RES1 detection

In reply to Colton Lewis

From: Marc Zyngier <[email protected]>

[ Upstream commit ca88ecdce5f51874a7c151809bd2c936ee0d3805 ]

We currently have two ways to identify CPUs that only implement FEAT_VHE and not FEAT_E2H0:

- either they advertise it via ID_AA64MMFR4_EL1.E2H0,
- or the HCR_EL2.E2H bit is RAO/WI

However, there is a third category of "cpus" that fall between these two cases: on CPUs that do not implement FEAT_FGT, it is IMPDEF whether an access to ID_AA64MMFR4_EL1 can trap to EL2 when the register value is zero.

A consequence of this is that on systems such as Neoverse V2, a NV guest cannot reliably detect that it is in a VHE-only configuration (E2H is writable, and ID_AA64MMFR0_EL1 is 0), despite the hypervisor's best effort to repaint the id register.

Replace the RAO/WI test by a sequence that makes use of the VHE register remnapping between EL1 and EL2 to detect this situation, and work out whether we get the VHE behaviour even after having set HCR_EL2.E2H to 0.

This solves the NV problem, and provides a more reliable acid test for CPUs that do not completely follow the letter of the architecture while providing a RES1 behaviour for HCR_EL2.E2H.

[ Backport: Resolved conflict in arch/arm64/include/asm/el2_setup.h
by replacing msr_hcr_el2 macro usages with raw msr hcr_el2 (since the macro is missing in 6.6.y). ]

---
 arch/arm64/include/asm/el2_setup.h | 38 +++++++++++++++++++++++++-----
 1 file changed, 32 insertions(+), 6 deletions(-)
arch/arm64/include/asm/el2_setup.h +32 -6
index 76b0d50d286d5..4c7467b382b60 100644
@@ -24,22 +24,48 @@
 	 * ID_AA64MMFR4_EL1.E2H0 < 0. On such CPUs HCR_EL2.E2H is RES1, but it
 	 * can reset into an UNKNOWN state and might not read as 1 until it has
 	 * been initialized explicitly.
-	 *
-	 * Fruity CPUs seem to have HCR_EL2.E2H set to RAO/WI, but
-	 * don't advertise it (they predate this relaxation).
-	 *
 	 * Initalize HCR_EL2.E2H so that later code can rely upon HCR_EL2.E2H
 	 * indicating whether the CPU is running in E2H mode.
 	 */
 	mrs_s	x1, SYS_ID_AA64MMFR4_EL1
 	sbfx	x1, x1, #ID_AA64MMFR4_EL1_E2H0_SHIFT, #ID_AA64MMFR4_EL1_E2H0_WIDTH
 	cmp	x1, #0
-	b.ge	.LnVHE_\@
+	b.lt	.LnE2H0_\@
 
+	/*
+	 * Unfortunately, HCR_EL2.E2H can be RES1 even if not advertised
+	 * as such via ID_AA64MMFR4_EL1.E2H0:
+	 *
+	 * - Fruity CPUs predate the !FEAT_E2H0 relaxation, and seem to
+	 *   have HCR_EL2.E2H implemented as RAO/WI.
+	 *
+	 * - On CPUs that lack FEAT_FGT, a hypervisor can't trap guest
+	 *   reads of ID_AA64MMFR4_EL1 to advertise !FEAT_E2H0. NV
+	 *   guests on these hosts can write to HCR_EL2.E2H without
+	 *   trapping to the hypervisor, but these writes have no
+	 *   functional effect.
+	 *
+	 * Handle both cases by checking for an essential VHE property
+	 * (system register remapping) to decide whether we're
+	 * effectively VHE-only or not.
+	 */
+	msr	hcr_el2, x0		// Setup HCR_EL2 as nVHE
+	isb
+	mov	x1, #1		// Write something to FAR_EL1
+	msr	far_el1, x1
+	isb
+	mov	x1, #2		// Try to overwrite it via FAR_EL2
+	msr	far_el2, x1
+	isb
+	mrs	x1, far_el1	// If we see the latest write in FAR_EL1,
+	cmp	x1, #2		// we can safely assume we are VHE only.
+	b.ne	.LnVHE_\@	// Otherwise, we know that nVHE works.
+
+.LnE2H0_\@:
 	orr	x0, x0, #HCR_E2H
-.LnVHE_\@:
 	msr	hcr_el2, x0
 	isb
+.LnVHE_\@:
 .endm
 
 .macro __init_el2_sctlr
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#13 lore raw

Re: [PATCH 6.6 v2 6/6] arm64: Revamp HCR_EL2.E2H RES1 detection

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing immediate Context Synchronization Event (ISB) after writes to control-plane system registers. --

--- Patch [6]: [PATCH 6.6 v2 6/6] arm64: Revamp HCR_EL2.E2H RES1 detection --- commit 75069b0b3126a153b742f982dd5dff757dc34d3e
Author: Marc Zyngier <[email protected]>

arm64: Revamp HCR_EL2.E2H RES1 detection

This patch replaces the RAO/WI test for VHE-only CPUs with a sequence utilizing VHE register remapping. This allows Neoverse V2 NV guests to reliably detect they are in a VHE-only configuration despite the absence of FEAT_FGT.

Quoted 4 lines
arch/arm64/include/asm/el2_setup.h +0 -0
index 76b0d50d286d5..4c7467b382b60 100644

[ ... ]

Quoted 16 lines
arch/arm64/include/asm/el2_setup.h +1 -5
@@ -24,22 +24,48 @@
 	 * ID_AA64MMFR4_EL1.E2H0 < 0. On such CPUs HCR_EL2.E2H is RES1, but it
 	 * can reset into an UNKNOWN state and might not read as 1 until it has
 	 * been initialized explicitly.
-	 *
-	 * Fruity CPUs seem to have HCR_EL2.E2H set to RAO/WI, but
-	 * don't advertise it (they predate this relaxation).
-	 *
 	 * Initalize HCR_EL2.E2H so that later code can rely upon HCR_EL2.E2H
 	 * indicating whether the CPU is running in E2H mode.
 	 */
 	mrs_s	x1, SYS_ID_AA64MMFR4_EL1
 	sbfx	x1, x1, #ID_AA64MMFR4_EL1_E2H0_SHIFT, #ID_AA64MMFR4_EL1_E2H0_WIDTH
 	cmp	x1, #0
-	b.ge	.LnVHE_\@
+	b.lt	.LnE2H0_\@

[ ... ]

arch/arm64/include/asm/el2_setup.h line 69

Quoted 7 lines

+.LnE2H0_\@:

arch/arm64/include/asm/el2_setup.h +1 -1
 	orr	x0, x0, #HCR_E2H
-.LnVHE_\@:
 	msr	hcr_el2, x0
 	isb
+.LnVHE_\@:
 .endm

[Severity: High]
This isn't a bug introduced by this patch, but while reviewing this file I noticed there might be missing Context Synchronization Events (ISB) after writes to control-plane system registers.

In the __init_el2_timers macro:

	msr	cnthctl_el2, x0
	msr	cntvoff_el2, xzr		// Clear virtual offset

Does the write to cnthctl_el2 require an isb instruction immediately after it, before executing the write to cntvoff_el2?

Similarly, in the __init_el2_fgt macro, there are multiple writes executed without synchronization:

	msr_s	SYS_HFGRTR_EL2, x0
	msr_s	SYS_HFGWTR_EL2, x0
	msr_s	SYS_HFGITR_EL2, xzr

	mrs	x1, id_aa64pfr0_el1		// AMU traps UNDEF without AMU

Could executing subsequent instructions without an isb cause the CPU to operate under a stale configuration here?

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