xen/arm: flush icache as well when XEN_DOMCTL_cacheflush is issued
PATCH v2 1/1 xen/arm: flush icache as well when XEN_DOMCTL_cacheflush is issued
When the toolstack modifies memory of a running ARM VM it may happen that the underlying memory of a current vCPU PC is changed. Without flushing the icache the vCPU may continue executing stale instructions.
In this patch we introduce VA-based icache flushing macros. Also expose the xc_domain_cacheflush through xenctrl.h.
---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Note: patch has been verified to solve stale icache issues on the
HiKey platform.
v2: Return 0 on x86 and clarify comment in xenctrl.h
--- tools/libxc/include/xenctrl.h | 8 ++++++++ tools/libxc/xc_domain.c | 6 +++--- tools/libxc/xc_private.h | 3 --- xen/arch/arm/mm.c | 1 + xen/include/asm-arm/arm32/page.h | 3 +++ xen/include/asm-arm/arm64/page.h | 3 +++ xen/include/asm-arm/page.h | 31 +++++++++++++++++++++++++++++++ 7 files changed, 49 insertions(+), 6 deletions(-)
tools/libxc/include/xenctrl.h +8 -0
@@ -2720,6 +2720,14 @@ int xc_livepatch_revert(xc_interface *xch, char *name, uint32_t timeout);int xc_livepatch_unload (xc_interface *xch,char *name,uint32_t timeout);int xc_livepatch_replace (xc_interface *xch,char *name,uint32_t timeout); +/* +* Ensure cache coherency after memory modifications. A call to this function +* is only required on ARM as the x86 architecture provides cache coherency +* guarantees. Calling this function on x86 is allowed but has no effect. +*/ +int xc_domain_cacheflush (xc_interface *xch,uint32_t domid, +xen_pfn_t start_pfn,xen_pfn_t nr_pfns); +/* Compat shims */ #include "xenctrl_compat.h"
tools/libxc/xc_domain.c +3 -3
@@ -74,10 +74,10 @@ int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,/* * The x86 architecture provides cache coherency guarantees which prevent * the need for this hypercall. Avoid the overhead of making a hypercall -* just for Xen to return -ENOSYS. +* just for Xen to return -ENOSYS. It is safe to ignore this call on x86 +* so we just return 0. */ - errno =ENOSYS ; -return -1 ; +return 0 ;#else DECLARE_DOMCTL ; domctl.cmd = XEN_DOMCTL_cacheflush;
tools/libxc/xc_private.h +0 -3
@@ -366,9 +366,6 @@ void bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, int nbits);/* Optionally flush file to disk and discard page cache */ void discard_file_cache (xc_interface *xch,int fd,int flush); -int xc_domain_cacheflush (xc_interface *xch,uint32_t domid, -xen_pfn_t start_pfn,xen_pfn_t nr_pfns); -#define MAX_MMU_UPDATES 1024struct xc_mmu {mmu_update_t updates[MAX_MMU_UPDATES ];
xen/arch/arm/mm.c +1 -0
@@ -389,6 +389,7 @@ void flush_page_to_ram(unsigned long mfn)void *v =map_domain_page (_mfn (mfn));clean_and_invalidate_dcache_va_range (v,PAGE_SIZE ); +invalidate_icache_va_range (v,PAGE_SIZE );unmap_domain_page (v); }
xen/include/asm-arm/arm32/page.h +3 -0
@@ -19,6 +19,9 @@ static inline void write_pte(lpae_t *p, lpae_t pte) : : "r" (pte.bits), "r" (p) : "memory"); } +/* Inline ASM to invalidate icache on register R (may be an inline asm operand) */ +#define __invalidate_icache_one (R ) STORE_CP32(R, ICIMVAU) +/* Inline ASM to invalidate dcache on register R (may be an inline asm operand) */ #define __invalidate_dcache_one (R ) STORE_CP32(R, DCIMVAC)
xen/include/asm-arm/arm64/page.h +3 -0
@@ -16,6 +16,9 @@ static inline void write_pte(lpae_t *p, lpae_t pte) : : "r" (pte.bits), "r" (p) : "memory"); } +/* Inline ASM to invalidate icache on register R (may be an inline asm operand) */ +#define __invalidate_icache_one (R ) "ic ivau, %" #R ";" +/* Inline ASM to invalidate dcache on register R (may be an inline asm operand) */ #define __invalidate_dcache_one (R ) "dc ivac, %" #R ";"
xen/include/asm-arm/page.h +31 -0
@@ -371,6 +371,37 @@ static inline int clean_and_invalidate_dcache_va_range : : "r" (_p), "m" (*_p)); \ }while (0 ) +static inline int invalidate_icache_va_range(const void *p, unsigned long size) +{ +size_t off; +const void *end = p + size; + +dsb (sy);/* So the CPU issues all writes to the range */ + + off = (unsigned long )p % cacheline_bytes; +if ( off ) + { + p -= off; + asm volatile (__invalidate_icache_one(0) : : "r" (p)); + p += cacheline_bytes; + size -= cacheline_bytes - off; + } + off = (unsigned long )end % cacheline_bytes; +if ( off ) + { + end -= off; + size -= off; + asm volatile (__invalidate_icache_one(0) : : "r" (end)); + } + +for ( ; p < end; p += cacheline_bytes ) + asm volatile (__invalidate_icache_one(0) : : "r" (p)); + +dsb (sy);/* So we know the flushes happen before continuing */ + +return 0 ; +} +/* * Flush a range of VA's hypervisor mappings from the data TLB of the * local processor. This is not sufficient when changing code mappings
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 14 lines · Tamas K Lengyel
When the toolstack modifies memory of a running ARM VM it may happen that the underlying memory of a current vCPU PC is changed. Without flushing the icache the vCPU may continue executing stale instructions.
In this patch we introduce VA-based icache flushing macros. Also expose the
xc_domain_cacheflushthroughxenctrl.h.Signed-off-by Tamas K Lengyel <[email protected]>---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Quoted 8 lines
Note: patch has been verified to solve stale icache issues on the
HiKey platform.v2: Return 0 on x86 and clarify comment in
xenctrl.h--- tools/libxc/include/xenctrl.h | 8 ++++++++ tools/libxc/xc_domain.c | 6 +++--- tools/libxc/xc_private.h | 3 ---
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Hello Tamas,
Please give a bit more time to people for reviewing before sending a new version. Patches adding cache instruction are never easy to review.
Quoted 7 lines · Tamas K Lengyel
When the toolstack modifies memory of a running ARM VM it may happen that the underlying memory of a current vCPU PC is changed. Without flushing the icache the vCPU may continue executing stale instructions. In this patch we introduce VA-based icache flushing macros. Also expose the
xc_domain_cacheflushthroughxenctrl.h.Signed-off-by Tamas K Lengyel <[email protected]>
Quoted 8 lines
---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>Note: patch has been verified to solve stale icache issues on the
HiKey platform.
In the future, please include a reference to the ARM ARM to corroborate your testing. This would speed-up the review.
Quoted 69 lines
v2: Return 0 on x86 and clarify comment in
xenctrl.h--- tools/libxc/include/xenctrl.h | 8 ++++++++ tools/libxc/xc_domain.c | 6 +++--- tools/libxc/xc_private.h | 3 --- xen/arch/arm/mm.c | 1 + xen/include/asm-arm/arm32/page.h | 3 +++ xen/include/asm-arm/arm64/page.h | 3 +++ xen/include/asm-arm/page.h | 31 +++++++++++++++++++++++++++++++ 7 files changed, 49 insertions(+), 6 deletions(-)tools/libxc/include/xenctrl.h +8
-0@@ -2720,6 +2720,14 @@ int xc_livepatch_revert(xc_interface *xch, char *name, uint32_t timeout);int xc_livepatch_unload (xc_interface *xch,char *name,uint32_t timeout);int xc_livepatch_replace (xc_interface *xch,char *name,uint32_t timeout); +/* +* Ensure cache coherency after memory modifications. A call to this function +* is only required on ARM as the x86 architecture provides cache coherency +* guarantees. Calling this function on x86 is allowed but has no effect. +*/ +int xc_domain_cacheflush (xc_interface *xch,uint32_t domid, +xen_pfn_t start_pfn,xen_pfn_t nr_pfns); +/* Compat shims */ #include "xenctrl_compat.h" tools/libxc/xc_domain.c +3
-3@@ -74,10 +74,10 @@ int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,/* * The x86 architecture provides cache coherency guarantees which prevent * the need for this hypercall. Avoid the overhead of making a hypercall -* just for Xen to return -ENOSYS. +* just for Xen to return -ENOSYS. It is safe to ignore this call on x86 +* so we just return 0. */ - errno =ENOSYS ; -return -1 ; +return 0 ;#else DECLARE_DOMCTL ; domctl.cmd = XEN_DOMCTL_cacheflush;tools/libxc/xc_private.h +0
-3@@ -366,9 +366,6 @@ void bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, int nbits);/* Optionally flush file to disk and discard page cache */ void discard_file_cache (xc_interface *xch,int fd,int flush); -int xc_domain_cacheflush (xc_interface *xch,uint32_t domid, -xen_pfn_t start_pfn,xen_pfn_t nr_pfns); -#define MAX_MMU_UPDATES 1024struct xc_mmu {mmu_update_t updates[MAX_MMU_UPDATES];xen/arch/arm/mm.c +0
-0
xen/arch/arm/mm.c void flush_page_to_ram(unsigned long mfn) · line 392
Quoted 4 lines
xen/arch/arm/mm.c +1
-0@@ -389,6 +389,7 @@ void flush_page_to_ram(unsigned long mfn)void *v =map_domain_page (_mfn (mfn));clean_and_invalidate_dcache_va_range (v,PAGE_SIZE ); +invalidate_icache_va_range (v,PAGE_SIZE );
I was about to say that the instruction cache flush would not be necessary for the current use case. But in fact, even if the domain is not yet running or we are allocating a page, we need to flush the I-Cache.
However, I am afraid that invalidating the I-Cache by VA range will not work as you expect on all platforms. This will highly depend on the behavior of the I-Cache (See D4.9.2 in ARM DDI 0487A.k_iss10775).
For some of the instruction cache (such as VIPT), you would need to flush the entire I-Cache to guarantee that all the aliases of a given physical address will be removed from the cache.
There are 2 options to fix the issue:
1) Always flush the entire cache
2) Either flush by VA or the entire cache depending of the I-cache
implementation
I don't mind if you implement option 1 for now.
xen/include/asm-arm/arm32/page.h static inline void write_pte(lpae_t *p, lpae_t pte) · line 23
Quoted 13 lines
xen/arch/arm/mm.c +0
-0unmap_domain_page (v); }xen/include/asm-arm/arm32/page.h +2
-0@@ -19,6 +19,9 @@ static inline void write_pte(lpae_t *p, lpae_t pte) : : "r" (pte.bits), "r" (p) : "memory"); } +/* Inline ASM to invalidate icache on register R (may be an inline asm operand) */ +#define __invalidate_icache_one (R ) STORE_CP32(R, ICIMVAU)
For ARM32, you would also need to invalidate the branch predictor.
Quoted 23 lines
xen/include/asm-arm/arm32/page.h +1
-0+/* Inline ASM to invalidate dcache on register R (may be an inline asm operand) */ #define __invalidate_dcache_one (R ) STORE_CP32(R, DCIMVAC)xen/include/asm-arm/arm64/page.h +3
-0@@ -16,6 +16,9 @@ static inline void write_pte(lpae_t *p, lpae_t pte) : : "r" (pte.bits), "r" (p) : "memory"); } +/* Inline ASM to invalidate icache on register R (may be an inline asm operand) */ +#define __invalidate_icache_one (R ) "ic ivau, %" #R ";" +/* Inline ASM to invalidate dcache on register R (may be an inline asm operand) */ #define __invalidate_dcache_one (R ) "dc ivac, %" #R ";"xen/include/asm-arm/page.h +0
-0
xen/include/asm-arm/page.h static inline int clean_and_invalidate_dcache_va_range · line 379
Quoted 9 lines
xen/include/asm-arm/page.h +6
-0@@ -371,6 +371,37 @@ static inline int clean_and_invalidate_dcache_va_range : : "r" (_p), "m" (*_p)); \ }while (0 ) +static inline int invalidate_icache_va_range(const void *p, unsigned long size) +{ +size_t off; +const void *end = p + size; + +dsb (sy);/* So the CPU issues all writes to the range */
The invalidation will be happen on the innershareable domain, so dsb(ish) is enough here.
xen/include/asm-arm/page.h static inline int clean_and_invalidate_dcache_va_range · line 381
Quoted 2 lines
xen/include/asm-arm/page.h +2
-0+ + off = (unsigned long )p % cacheline_bytes;
cacheline_bytes contains the cacheline size of the data cache, not instruction cache.
Quoted 9 lines
xen/include/asm-arm/page.h +9
-0+if ( off ) + { + p -= off; + asm volatile (__invalidate_icache_one(0) : : "r" (p)); + p += cacheline_bytes; + size -= cacheline_bytes - off; + } + off = (unsigned long )end % cacheline_bytes; +if ( off )
xen/include/asm-arm/page.h static inline int clean_and_invalidate_dcache_va_range · line 400
Quoted 10 lines
xen/include/asm-arm/page.h +10
-0+ { + end -= off; + size -= off; + asm volatile (__invalidate_icache_one(0) : : "r" (end)); + } + +for ( ; p < end; p += cacheline_bytes ) + asm volatile (__invalidate_icache_one(0) : : "r" (p)); + +dsb (sy);/* So we know the flushes happen before continuing */
dsb(ish)
xen/include/asm-arm/page.h static inline int clean_and_invalidate_dcache_va_range · line 407
Quoted 8 lines
xen/include/asm-arm/page.h +4
-0+ +return 0 ; +} +/* * Flush a range of VA's hypervisor mappings from the data TLB of the * local processor. This is not sufficient when changing code mappings
Regards,
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 20 lines · Julien Grall
Hello Tamas,
Please give a bit more time to people for reviewing before sending a new version. Patches adding cache instruction are never easy to review.
Quoted 14 lines · Tamas K Lengyel
When the toolstack modifies memory of a running ARM VM it may happen that the underlying memory of a current vCPU PC is changed. Without flushing the icache the vCPU may continue executing stale instructions. In this patch we introduce VA-based icache flushing macros. Also expose the
xc_domain_cacheflushthroughxenctrl.h.Signed-off-by Tamas K Lengyel <[email protected]>---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Quoted 6 lines
Quoted 2 lines
Note: patch has been verified to solve stale icache issues on the
HiKey platform.In the future, please include a reference to the ARM ARM to corroborate your testing. This would speed-up the review.
Ack.
Quoted 99 lines
Quoted 81 lines
v2: Return 0 on x86 and clarify comment in
xenctrl.h--- tools/libxc/include/xenctrl.h | 8 ++++++++ tools/libxc/xc_domain.c | 6 +++--- tools/libxc/xc_private.h | 3 --- xen/arch/arm/mm.c | 1 + xen/include/asm-arm/arm32/page.h | 3 +++ xen/include/asm-arm/arm64/page.h | 3 +++ xen/include/asm-arm/page.h | 31 +++++++++++++++++++++++++++++++ 7 files changed, 49 insertions(+), 6 deletions(-)tools/libxc/include/xenctrl.h +2
-0@@ -2720,6 +2720,14 @@ int xc_livepatch_revert(xc_interface *xch, char *name, uint32_t timeout);int xc_livepatch_unload (xc_interface *xch,char *name,uint32_t timeout);int xc_livepatch_replace (xc_interface *xch,char *name,uint32_t timeout); +/* +* Ensure cache coherency after memory modifications. A call to this function
+ * is only required on ARM as the x86 architecture provides cache coherency
+ * guarantees. Calling this function on x86 is allowed but has no effect.
+ */tools/libxc/include/xenctrl.h +3
-0+int xc_domain_cacheflush (xc_interface *xch,uint32_t domid, +xen_pfn_t start_pfn,xen_pfn_t nr_pfns); +/* Compat shims */ #include "xenctrl_compat.h" tools/libxc/xc_domain.c +0
-0@@ -74,10 +74,10 @@ int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,/* * The x86 architecture provides cache coherency guarantees which prevent
* the need for this hypercall. Avoid the overhead of making a
hypercalltools/libxc/xc_domain.c +1
-1-* just for Xen to return -ENOSYS. +* just for Xen to return -ENOSYS. It is safe to ignore this call on x86
tools/libxc/xc_domain.c +2
-2+* so we just return 0. */ - errno =ENOSYS ; -return -1 ; +return 0 ;#else DECLARE_DOMCTL ; domctl.cmd = XEN_DOMCTL_cacheflush;tools/libxc/xc_private.h +0
-3@@ -366,9 +366,6 @@ void bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, int nbits);/* Optionally flush file to disk and discard page cache */ void discard_file_cache (xc_interface *xch,int fd,int flush); -int xc_domain_cacheflush (xc_interface *xch,uint32_t domid, -xen_pfn_t start_pfn,xen_pfn_t nr_pfns); -#define MAX_MMU_UPDATES 1024struct xc_mmu {mmu_update_t updates[MAX_MMU_UPDATES];xen/arch/arm/mm.c +0
-0xen/arch/arm/mm.c void flush_page_to_ram(unsigned long mfn) · line 392
Quoted 4 lines
xen/arch/arm/mm.c +1
-0@@ -389,6 +389,7 @@ void flush_page_to_ram(unsigned long mfn)void *v =map_domain_page (_mfn (mfn));clean_and_invalidate_dcache_va_range (v,PAGE_SIZE ); +invalidate_icache_va_range (v,PAGE_SIZE );I was about to say that the instruction cache flush would not be necessary for the current use case. But in fact, even if the domain is not yet running or we are allocating a page, we need to flush the I-Cache.
However, I am afraid that invalidating the I-Cache by VA range will not work as you expect on all platforms. This will highly depend on the behavior of the I-Cache (See D4.9.2 in ARM DDI 0487A.k_iss10775).
For some of the instruction cache (such as VIPT), you would need to flush the entire I-Cache to guarantee that all the aliases of a given physical address will be removed from the cache.
Quoted 6 lines
There are 2 options to fix the issue:
1) Always flush the entire cache
2) Either flush by VA or the entire cache depending of the I-cache
implementationI don't mind if you implement option 1 for now.
That is certainly fine by me. Thanks for looking into this!
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel