clear_cache.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #elif defined(__linux__) || defined(__OpenBSD__)
  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. _flush_cache(start, end_int - start_int, BCACHE);
  104. #else
  105. (void)start_int;
  106. (void)end_int;
  107. compilerrt_abort();
  108. #endif
  109. }
  110. #elif defined(__aarch64__) && !defined(__APPLE__)
  111. uint64_t xstart = (uint64_t)(uintptr_t)start;
  112. uint64_t xend = (uint64_t)(uintptr_t)end;
  113. // Get Cache Type Info.
  114. static uint64_t ctr_el0 = 0;
  115. if (ctr_el0 == 0)
  116. __asm __volatile("mrs %0, ctr_el0" : "=r"(ctr_el0));
  117. // The DC and IC instructions must use 64-bit registers so we don't use
  118. // uintptr_t in case this runs in an IPL32 environment.
  119. uint64_t addr;
  120. // If CTR_EL0.IDC is set, data cache cleaning to the point of unification
  121. // is not required for instruction to data coherence.
  122. if (((ctr_el0 >> 28) & 0x1) == 0x0) {
  123. const size_t dcache_line_size = 4 << ((ctr_el0 >> 16) & 15);
  124. for (addr = xstart & ~(dcache_line_size - 1); addr < xend;
  125. addr += dcache_line_size)
  126. __asm __volatile("dc cvau, %0" ::"r"(addr));
  127. }
  128. __asm __volatile("dsb ish");
  129. // If CTR_EL0.DIC is set, instruction cache invalidation to the point of
  130. // unification is not required for instruction to data coherence.
  131. if (((ctr_el0 >> 29) & 0x1) == 0x0) {
  132. const size_t icache_line_size = 4 << ((ctr_el0 >> 0) & 15);
  133. for (addr = xstart & ~(icache_line_size - 1); addr < xend;
  134. addr += icache_line_size)
  135. __asm __volatile("ic ivau, %0" ::"r"(addr));
  136. __asm __volatile("dsb ish");
  137. }
  138. __asm __volatile("isb sy");
  139. #elif defined(__powerpc__)
  140. // Newer CPUs have a bigger line size made of multiple blocks, so the
  141. // following value is a minimal common denominator for what used to be
  142. // a single block cache line and is therefore inneficient.
  143. const size_t line_size = 32;
  144. const size_t len = (uintptr_t)end - (uintptr_t)start;
  145. const uintptr_t mask = ~(line_size - 1);
  146. const uintptr_t start_line = ((uintptr_t)start) & mask;
  147. const uintptr_t end_line = ((uintptr_t)start + len + line_size - 1) & mask;
  148. for (uintptr_t line = start_line; line < end_line; line += line_size)
  149. __asm__ volatile("dcbf 0, %0" : : "r"(line));
  150. __asm__ volatile("sync");
  151. for (uintptr_t line = start_line; line < end_line; line += line_size)
  152. __asm__ volatile("icbi 0, %0" : : "r"(line));
  153. __asm__ volatile("isync");
  154. #elif defined(__sparc__)
  155. const size_t dword_size = 8;
  156. const size_t len = (uintptr_t)end - (uintptr_t)start;
  157. const uintptr_t mask = ~(dword_size - 1);
  158. const uintptr_t start_dword = ((uintptr_t)start) & mask;
  159. const uintptr_t end_dword = ((uintptr_t)start + len + dword_size - 1) & mask;
  160. for (uintptr_t dword = start_dword; dword < end_dword; dword += dword_size)
  161. __asm__ volatile("flush %0" : : "r"(dword));
  162. #elif defined(__riscv) && defined(__linux__)
  163. // See: arch/riscv/include/asm/cacheflush.h, arch/riscv/kernel/sys_riscv.c
  164. register void *start_reg __asm("a0") = start;
  165. const register void *end_reg __asm("a1") = end;
  166. // "0" means that we clear cache for all threads (SYS_RISCV_FLUSH_ICACHE_ALL)
  167. const register long flags __asm("a2") = 0;
  168. const register long syscall_nr __asm("a7") = __NR_riscv_flush_icache;
  169. __asm __volatile("ecall"
  170. : "=r"(start_reg)
  171. : "r"(start_reg), "r"(end_reg), "r"(flags), "r"(syscall_nr));
  172. assert(start_reg == 0 && "Cache flush syscall failed.");
  173. #elif defined(__riscv) && defined(__OpenBSD__)
  174. struct riscv_sync_icache_args arg;
  175. arg.addr = (uintptr_t)start;
  176. arg.len = (uintptr_t)end - (uintptr_t)start;
  177. sysarch(RISCV_SYNC_ICACHE, &arg);
  178. #elif defined(__ve__)
  179. __asm__ volatile("fencec 2");
  180. #else
  181. #if __APPLE__
  182. // On Darwin, sys_icache_invalidate() provides this functionality
  183. sys_icache_invalidate(start, end - start);
  184. #else
  185. compilerrt_abort();
  186. #endif
  187. #endif
  188. }