Skip to contentSkip to conversation

arm: cpu: Add optional CMOs by VA

PATCH In review Ying-Chun Liu (PaulLiu) 2 participants 5 messages Feb 8, 2023 – Mar 7, 2023 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

v2 cover arm: cpu: Add optional CMOs by VA

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

Marc Zyngier (1):
arm: cpu: Add optional CMOs by VA

Pierre-Clément Tosi (1):
arm64: Initialize TLB memory if CMO_BY_VA_ONLY

v2: Fix the Signed-off-by list.

 arch/arm/cpu/armv8/Kconfig    |  4 ++
 arch/arm/cpu/armv8/cache.S    | 50 +++++++++++++-----
 arch/arm/cpu/armv8/cache_v8.c | 97 ++++++++++++++++++++++++++++++++++-
 arch/arm/cpu/armv8/cpu.c      | 30 +++++++----
 arch/arm/lib/cache.c          |  9 ++++
 5 files changed, 164 insertions(+), 26 deletions(-)
Signature · 1 line
2.39.1
#1 lore raw

PATCH v2 1/2 arm: cpu: Add optional CMOs by VA

From: Marc Zyngier <[email protected]>

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

[ Paul: pick from the Android tree. Fixup Pierre's commit. And fix some
checkpatch warnings. Rebased to upstream. ]

---
v2: Fix the Signed-off-by list.

---
 arch/arm/cpu/armv8/Kconfig    |  4 ++
 arch/arm/cpu/armv8/cache.S    | 50 +++++++++++++-----
 arch/arm/cpu/armv8/cache_v8.c | 97 ++++++++++++++++++++++++++++++++++-
 arch/arm/cpu/armv8/cpu.c      | 30 +++++++----
 4 files changed, 155 insertions(+), 26 deletions(-)
arch/arm/cpu/armv8/Kconfig +4 -0
index 1305238c9d..7d5cf1594d 100644
@@ -1,5 +1,9 @@
 if ARM64
 
+config CMO_BY_VA_ONLY
+	bool "Force cache maintenance to be exclusively by VA"
+	depends on !SYS_DISABLE_DCACHE_OPS
+
 config ARMV8_SPL_EXCEPTION_VECTORS
 	bool "Install crash dump exception vectors"
 	depends on SPL
arch/arm/cpu/armv8/cache.S +36 -14

68 lines not on this page · raw message

arch/arm/cpu/armv8/cache_v8.c +96 -1

131 lines not on this page · raw message

arch/arm/cpu/armv8/cpu.c +19 -11
index db5d460eb4..3c7f36ad8d 100644
@@ -48,18 +48,26 @@ int cleanup_before_linux(void)
 
 	disable_interrupts();
 
-	/*
-	 * Turn off I-cache and invalidate it
-	 */
-	icache_disable();
-	invalidate_icache_all();
+	if (IS_ENABLED(CONFIG_CMO_BY_VA_ONLY)) {
+		/*
+		 * Disable D-cache.
+		 */
+		dcache_disable();
+	} else {
+		/*
+		 * Turn off I-cache and invalidate it
+		 */
+		icache_disable();
+		invalidate_icache_all();
 
-	/*
-	 * turn off D-cache
-	 * dcache_disable() in turn flushes the d-cache and disables MMU
-	 */
-	dcache_disable();
-	invalidate_dcache_all();
+		/*
+		 * turn off D-cache
+		 * dcache_disable() in turn flushes the d-cache and disables
+		 * MMU
+		 */
+		dcache_disable();
+		invalidate_dcache_all();
+	}
 
 	return 0;
 }
Signature · 1 line
2.39.1
#2 lore raw
Quoted 17 lines · Ying-Chun Liu (PaulLiu)

From: Marc Zyngier <[email protected]>

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

Quoted 6 lines

[ Paul: pick from the Android tree. Fixup Pierre's commit. And fix some
checkpatch warnings. Rebased to upstream. ]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#3 lore raw

PATCH v2 2/2 arm64: Initialize TLB memory if CMO_BY_VA_ONLY

In reply to Ying-Chun Liu (PaulLiu)

From: Pierre-Clément Tosi <[email protected]>

Memory used to hold the page tables is allocated from the top of RAM with no prior initialization and could therefore hold invalid data. As invalidate_dcache_all() will be called before the MMU has been initialized and as that function relies indirectly on the page tables when using CMO_BY_VA_ONLY, these must be in a valid state from their allocation.

[ Paul: pick from the Android tree. Fix checkpatch warnings, and rebased
to the upstream. ]

---
v2: Fix the Signed-off-by list.

---
 arch/arm/lib/cache.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/arm/lib/cache.c +9 -0
index 1a589c7e2a..7a16015867 100644
@@ -159,6 +159,15 @@ __weak int arm_reserve_mmu(void)
 	 */
 	gd->arch.tlb_allocated = gd->arch.tlb_addr;
 #endif
+
+	if (IS_ENABLED(CONFIG_CMO_BY_VA_ONLY)) {
+		/*
+		 * As invalidate_dcache_all() will be called before
+		 * mmu_setup(), we should make sure that the PTs are
+		 * already in a valid state.
+		 */
+		memset((void *)gd->arch.tlb_addr, 0, gd->arch.tlb_size);
+	}
 #endif
 
 	return 0;
Signature · 1 line
2.39.1
#4 lore raw

Re: [PATCH v2 2/2] arm64: Initialize TLB memory if CMO_BY_VA_ONLY

Quoted 9 lines · Ying-Chun Liu (PaulLiu)

From: Pierre-Clément Tosi <[email protected]>

Memory used to hold the page tables is allocated from the top of RAM with no prior initialization and could therefore hold invalid data. As invalidate_dcache_all() will be called before the MMU has been initialized and as that function relies indirectly on the page tables when using CMO_BY_VA_ONLY, these must be in a valid state from their allocation.

Quoted 6 lines

[ Paul: pick from the Android tree. Fix checkpatch warnings, and rebased
to the upstream. ]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom