clear_cache.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //===-- clear_cache.c - Implement __clear_cache ---------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "int_lib.h"
  9. #if defined(__linux__)
  10. #include <assert.h>
  11. #endif
  12. #include <stddef.h>
  13. #if __APPLE__
  14. #include <libkern/OSCacheControl.h>
  15. #endif
  16. #if defined(_WIN32)
  17. // Forward declare Win32 APIs since the GCC mode driver does not handle the
  18. // newer SDKs as well as needed.
  19. uint32_t FlushInstructionCache(uintptr_t hProcess, void *lpBaseAddress,
  20. uintptr_t dwSize);
  21. uintptr_t GetCurrentProcess(void);
  22. #endif
  23. #if defined(__FreeBSD__) && defined(__arm__)
  24. // clang-format off
  25. #include <sys/types.h>
  26. #include <machine/sysarch.h>
  27. // clang-format on
  28. #endif
  29. #if defined(__NetBSD__) && defined(__arm__)
  30. #include <machine/sysarch.h>
  31. #endif
  32. #if defined(__OpenBSD__) && (defined(__arm__) || defined(__mips__) || defined(__riscv))
  33. // clang-format off
  34. #include <sys/types.h>
  35. #include <machine/sysarch.h>
  36. // clang-format on
  37. #endif
  38. #if defined(__linux__) && defined(__mips__)
  39. #include <sys/cachectl.h>
  40. #include <sys/syscall.h>
  41. #include <unistd.h>
  42. #endif
  43. #if defined(__linux__) && defined(__riscv)
  44. // to get platform-specific syscall definitions
  45. #include <linux/unistd.h>
  46. #endif
  47. // The compiler generates calls to __clear_cache() when creating
  48. // trampoline functions on the stack for use with nested functions.
  49. // It is expected to invalidate the instruction cache for the
  50. // specified range.
  51. void __clear_cache(void *start, void *end) {
  52. #if __i386__ || __x86_64__ || defined(_M_IX86) || defined(_M_X64)
  53. // Intel processors have a unified instruction and data cache
  54. // so there is nothing to do
  55. #elif defined(_WIN32) && (defined(__arm__) || defined(__aarch64__))
  56. FlushInstructionCache(GetCurrentProcess(), start, end - start);
  57. #elif defined(__arm__) && !defined(__APPLE__)
  58. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
  59. struct arm_sync_icache_args arg;
  60. arg.addr = (uintptr_t)start;
  61. arg.len = (uintptr_t)end - (uintptr_t)start;
  62. sysarch(ARM_SYNC_ICACHE, &arg);
  63. #elif defined(__linux__)
  64. // We used to include asm/unistd.h for the __ARM_NR_cacheflush define, but
  65. // it also brought many other unused defines, as well as a dependency on
  66. // kernel headers to be installed.
  67. //
  68. // This value is stable at least since Linux 3.13 and should remain so for
  69. // compatibility reasons, warranting it's re-definition here.
  70. #define __ARM_NR_cacheflush 0x0f0002
  71. register int start_reg __asm("r0") = (int)(intptr_t)start;
  72. const register int end_reg __asm("r1") = (int)(intptr_t)end;
  73. const register int flags __asm("r2") = 0;
  74. const register int syscall_nr __asm("r7") = __ARM_NR_cacheflush;
  75. __asm __volatile("svc 0x0"
  76. : "=r"(start_reg)
  77. : "r"(syscall_nr), "r"(start_reg), "r"(end_reg), "r"(flags));
  78. assert(start_reg == 0 && "Cache flush syscall failed.");
  79. #else
  80. compilerrt_abort();
  81. #endif
  82. #elif defined(__linux__) && defined(__loongarch__)
  83. __asm__ volatile("ibar 0");
  84. #elif defined(__mips__)
  85. const uintptr_t start_int = (uintptr_t)start;
  86. const uintptr_t end_int = (uintptr_t)end;
  87. uintptr_t synci_step;
  88. __asm__ volatile("rdhwr %0, $1" : "=r"(synci_step));
  89. if (synci_step != 0) {
  90. #if __mips_isa_rev >= 6
  91. for (uintptr_t p = start_int; p < end_int; p += synci_step)
  92. __asm__ volatile("synci 0(%0)" : : "r"(p));
  93. // The last "move $at, $0" is the target of jr.hb instead of delay slot.
  94. __asm__ volatile(".set noat\n"
  95. "sync\n"
  96. "addiupc $at, 12\n"
  97. "jr.hb $at\n"
  98. "move $at, $0\n"
  99. ".set at");
  100. #else
  101. // Pre-R6 may not be globalized. And some implementations may give strange
  102. // synci_step. So, let's use libc call for it.
  103. cacheflush(start, end_int - start_int, BCACHE);
  104. #endif
  105. }
  106. #elif defined(__aarch64__) && !defined(__APPLE__)
  107. uint64_t xstart = (uint64_t)(uintptr_t)start;
  108. uint64_t xend = (uint64_t)(uintptr_t)end;
  109. // Get Cache Type Info.
  110. static uint64_t ctr_el0 = 0;
  111. if (ctr_el0 == 0)
  112. __asm __volatile("mrs %0, ctr_el0" : "=r"(ctr_el0));
  113. // The DC and IC instructions must use 64-bit registers so we don't use
  114. // uintptr_t in case this runs in an IPL32 environment.
  115. uint64_t addr;
  116. // If CTR_EL0.IDC is set, data cache cleaning to the point of unification
  117. // is not required for instruction to data coherence.
  118. if (((ctr_el0 >> 28) & 0x1) == 0x0) {
  119. const size_t dcache_line_size = 4 << ((ctr_el0 >> 16) & 15);
  120. for (addr = xstart & ~(dcache_line_size - 1); addr < xend;
  121. addr += dcache_line_size)
  122. __asm __volatile("dc cvau, %0" ::"r"(addr));
  123. }
  124. __asm __volatile("dsb ish");
  125. // If CTR_EL0.DIC is set, instruction cache invalidation to the point of
  126. // unification is not required for instruction to data coherence.
  127. if (((ctr_el0 >> 29) & 0x1) == 0x0) {
  128. const size_t icache_line_size = 4 << ((ctr_el0 >> 0) & 15);
  129. for (addr = xstart & ~(icache_line_size - 1); addr < xend;
  130. addr += icache_line_size)
  131. __asm __volatile("ic ivau, %0" ::"r"(addr));
  132. __asm __volatile("dsb ish");
  133. }
  134. __asm __volatile("isb sy");
  135. #elif defined(__powerpc__)
  136. // Newer CPUs have a bigger line size made of multiple blocks, so the
  137. // following value is a minimal common denominator for what used to be
  138. // a single block cache line and is therefore inneficient.
  139. const size_t line_size = 32;
  140. const size_t len = (uintptr_t)end - (uintptr_t)start;
  141. const uintptr_t mask = ~(line_size - 1);
  142. const uintptr_t start_line = ((uintptr_t)start) & mask;
  143. const uintptr_t end_line = ((uintptr_t)start + len + line_size - 1) & mask;
  144. for (uintptr_t line = start_line; line < end_line; line += line_size)
  145. __asm__ volatile("dcbf 0, %0" : : "r"(line));
  146. __asm__ volatile("sync");
  147. for (uintptr_t line = start_line; line < end_line; line += line_size)
  148. __asm__ volatile("icbi 0, %0" : : "r"(line));
  149. __asm__ volatile("isync");
  150. #elif defined(__sparc__)
  151. const size_t dword_size = 8;
  152. const size_t len = (uintptr_t)end - (uintptr_t)start;
  153. const uintptr_t mask = ~(dword_size - 1);
  154. const uintptr_t start_dword = ((uintptr_t)start) & mask;
  155. const uintptr_t end_dword = ((uintptr_t)start + len + dword_size - 1) & mask;
  156. for (uintptr_t dword = start_dword; dword < end_dword; dword += dword_size)
  157. __asm__ volatile("flush %0" : : "r"(dword));
  158. #elif defined(__riscv) && defined(__linux__)
  159. // See: arch/riscv/include/asm/cacheflush.h, arch/riscv/kernel/sys_riscv.c
  160. register void *start_reg __asm("a0") = start;
  161. const register void *end_reg __asm("a1") = end;
  162. // "0" means that we clear cache for all threads (SYS_RISCV_FLUSH_ICACHE_ALL)
  163. const register long flags __asm("a2") = 0;
  164. const register long syscall_nr __asm("a7") = __NR_riscv_flush_icache;
  165. __asm __volatile("ecall"
  166. : "=r"(start_reg)
  167. : "r"(start_reg), "r"(end_reg), "r"(flags), "r"(syscall_nr));
  168. assert(start_reg == 0 && "Cache flush syscall failed.");
  169. #elif defined(__riscv) && defined(__OpenBSD__)
  170. struct riscv_sync_icache_args arg;
  171. arg.addr = (uintptr_t)start;
  172. arg.len = (uintptr_t)end - (uintptr_t)start;
  173. sysarch(RISCV_SYNC_ICACHE, &arg);
  174. #elif defined(__ve__)
  175. __asm__ volatile("fencec 2");
  176. #else
  177. #if __APPLE__
  178. // On Darwin, sys_icache_invalidate() provides this functionality
  179. sys_icache_invalidate(start, end - start);
  180. #else
  181. compilerrt_abort();
  182. #endif
  183. #endif
  184. }