Blame SOURCES/0006-shim-Flush-the-memory-region-from-i-cache-before-exe.patch

9de34f
From 5c537b3d0cf8c393dad2e61d49aade68f3af1401 Mon Sep 17 00:00:00 2001
9de34f
From: dann frazier <dann.frazier@canonical.com>
9de34f
Date: Tue, 6 Sep 2022 09:28:22 -0600
9de34f
Subject: [PATCH 06/13] shim: Flush the memory region from i-cache before
9de34f
 execution
9de34f
9de34f
We've seen crashes in early GRUB code on an ARM Cortex-A72-based
9de34f
platform that point at seemingly harmless instructions. Flushing
9de34f
the i-cache of those instructions prior to executing has been
9de34f
shown to avoid the problem, which has parallels with this story:
9de34f
  https://www.mail-archive.com/osv-dev@googlegroups.com/msg06203.html
9de34f
9de34f
Add a cache flushing utility function and provide an implementation
9de34f
using a GCC intrinsic. This will need to be extended to support other
9de34f
compilers. Note that this intrinsic is a no-op for x86 platforms.
9de34f
9de34f
This fixes issue #498.
9de34f
9de34f
Signed-off-by: dann frazier <dann.frazier@canonical.com>
9de34f
---
9de34f
 pe.c               | 3 +++
9de34f
 include/compiler.h | 6 ++++++
9de34f
 2 files changed, 9 insertions(+)
9de34f
9de34f
diff --git a/pe.c b/pe.c
9de34f
index ba3e2bbc444..f94530a20c0 100644
9de34f
--- a/pe.c
9de34f
+++ b/pe.c
9de34f
@@ -1196,6 +1196,9 @@ handle_image (void *data, unsigned int datasize,
9de34f
 
9de34f
 	CopyMem(buffer, data, context.SizeOfHeaders);
9de34f
 
9de34f
+	/* Flush the instruction cache for the region holding the image */
9de34f
+	cache_invalidate(buffer, buffer + context.ImageSize);
9de34f
+
9de34f
 	*entry_point = ImageAddress(buffer, context.ImageSize, context.EntryPoint);
9de34f
 	if (!*entry_point) {
9de34f
 		perror(L"Entry point is invalid\n");
9de34f
diff --git a/include/compiler.h b/include/compiler.h
9de34f
index b4bf10319ee..b0d595f32c2 100644
9de34f
--- a/include/compiler.h
9de34f
+++ b/include/compiler.h
9de34f
@@ -192,5 +192,11 @@
9de34f
  */
9de34f
 #define unreachable() __builtin_unreachable()
9de34f
 
9de34f
+#if defined(__GNUC__)
9de34f
+#define cache_invalidate(begin, end)  __builtin___clear_cache(begin, end)
9de34f
+#else /* __GNUC__ */
9de34f
+#error shim has no cache_invalidate() implementation for this compiler
9de34f
+#endif /* __GNUC__ */
9de34f
+
9de34f
 #endif /* !COMPILER_H_ */
9de34f
 // vim:fenc=utf-8:tw=75:et
9de34f
-- 
9de34f
2.37.1
9de34f