arm: cpu: Add optional CMOs by VA
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
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
@@ -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
@@ -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
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_ONLYso that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Will Deacon <[email protected]>Signed-off-by Pierre-Clément Tosi <[email protected]>
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
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
@@ -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
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 usingCMO_BY_VA_ONLY, these must be in a valid state from their allocation.
Quoted 6 lines
Signed-off-by Pierre-Clément Tosi <[email protected]>[ Paul: pick from the Android tree. Fix checkpatch warnings, and rebased
to the upstream. ]
Applied to u-boot/next, thanks!
Signature · 1 line
Tom