[PATCH] D40423: [ARM][AArch64] Workaround ARMv8.2 percularity in clearing icache.

Maxim Kuvyrkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 24 03:56:51 PST 2017


maxim-kuvyrkov created this revision.
Herald added subscribers: rengolin, aemerson.

  ARMv8.2 treats icache clear instruction as a memory read,
  and CPU segfaults on trying to clear cache on !PROT_READ page.
  We workaround this in Memory::protectMappedMemory by adding
  PROT_READ to affected pages, clearing the cache, and then setting
  desired protection.


https://reviews.llvm.org/D40423

Files:
  lib/Support/Unix/Memory.inc


Index: lib/Support/Unix/Memory.inc
===================================================================
--- lib/Support/Unix/Memory.inc
+++ lib/Support/Unix/Memory.inc
@@ -126,8 +126,12 @@
   Result.Address = Addr;
   Result.Size = NumPages*PageSize;
 
-  if (PFlags & MF_EXEC)
-    Memory::InvalidateInstructionCache(Result.Address, Result.Size);
+  // Rely on protectMappedMemory to invalidate instruction cache.
+  if (PFlags & MF_EXEC) {
+    EC = Memory::protectMappedMemory (Result, PFlags);
+    if (EC != std::error_code())
+      return MemoryBlock();
+  }
 
   return Result;
 }
@@ -156,15 +160,31 @@
     return std::error_code(EINVAL, std::generic_category());
 
   int Protect = getPosixProtectionFlags(Flags);
-
   uintptr_t Start = alignAddr((uint8_t *)M.Address - PageSize + 1, PageSize);
   uintptr_t End = alignAddr((uint8_t *)M.Address + M.Size, PageSize);
+
+  bool InvalidateCache = (Flags & MF_EXEC);
+
+#if defined(__arm__) || defined(__aarch64__)
+  // Certain architectures (e.g., ARMv8.2) treat icache clear instruction as a memory read,
+  // and CPU segfaults on trying to clear cache on !PROT_READ page.  Therefore we need
+  // to temporarily add PROT_READ for the sake of flushing the instruction caches.
+  if (InvalidateCache && !(Protect && PROT_READ)) {
+    int Result = ::mprotect((void *)Start, End - Start, Protect | PROT_READ);
+    if (Result != 0)
+      return std::error_code(errno, std::generic_category());
+
+    Memory::InvalidateInstructionCache(M.Address, M.Size);
+    InvalidateCache = false;
+  }
+#endif
+
   int Result = ::mprotect((void *)Start, End - Start, Protect);
 
   if (Result != 0)
     return std::error_code(errno, std::generic_category());
 
-  if (Flags & MF_EXEC)
+  if (InvalidateCache)
     Memory::InvalidateInstructionCache(M.Address, M.Size);
 
   return std::error_code();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40423.124158.patch
Type: text/x-patch
Size: 1848 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171124/233e2942/attachment.bin>


More information about the llvm-commits mailing list