cacheflush.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Info about, and flushing the host cpu caches.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  5. * See the COPYING file in the top-level directory.
  6. */
  7. #include "qemu/osdep.h"
  8. #include "qemu/cacheflush.h"
  9. #include "qemu/cacheinfo.h"
  10. #include "qemu/bitops.h"
  11. #include "qemu/host-utils.h"
  12. #include "qemu/atomic.h"
  13. int qemu_icache_linesize = 0;
  14. int qemu_icache_linesize_log;
  15. int qemu_dcache_linesize = 0;
  16. int qemu_dcache_linesize_log;
  17. /*
  18. * Operating system specific cache detection mechanisms.
  19. */
  20. #if defined(_WIN32)
  21. static void sys_cache_info(int *isize, int *dsize)
  22. {
  23. SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buf;
  24. DWORD size = 0;
  25. BOOL success;
  26. size_t i, n;
  27. /*
  28. * Check for the required buffer size first. Note that if the zero
  29. * size we use for the probe results in success, then there is no
  30. * data available; fail in that case.
  31. */
  32. success = GetLogicalProcessorInformation(0, &size);
  33. if (success || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  34. return;
  35. }
  36. n = size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
  37. size = n * sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
  38. buf = g_new0(SYSTEM_LOGICAL_PROCESSOR_INFORMATION, n);
  39. if (!GetLogicalProcessorInformation(buf, &size)) {
  40. goto fail;
  41. }
  42. for (i = 0; i < n; i++) {
  43. if (buf[i].Relationship == RelationCache
  44. && buf[i].Cache.Level == 1) {
  45. switch (buf[i].Cache.Type) {
  46. case CacheUnified:
  47. *isize = *dsize = buf[i].Cache.LineSize;
  48. break;
  49. case CacheInstruction:
  50. *isize = buf[i].Cache.LineSize;
  51. break;
  52. case CacheData:
  53. *dsize = buf[i].Cache.LineSize;
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. }
  60. fail:
  61. g_free(buf);
  62. }
  63. #elif defined(CONFIG_DARWIN)
  64. # include <sys/sysctl.h>
  65. static void sys_cache_info(int *isize, int *dsize)
  66. {
  67. /* There's only a single sysctl for both I/D cache line sizes. */
  68. long size;
  69. size_t len = sizeof(size);
  70. if (!sysctlbyname("hw.cachelinesize", &size, &len, NULL, 0)) {
  71. *isize = *dsize = size;
  72. }
  73. }
  74. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  75. # include <sys/sysctl.h>
  76. static void sys_cache_info(int *isize, int *dsize)
  77. {
  78. /* There's only a single sysctl for both I/D cache line sizes. */
  79. int size;
  80. size_t len = sizeof(size);
  81. if (!sysctlbyname("machdep.cacheline_size", &size, &len, NULL, 0)) {
  82. *isize = *dsize = size;
  83. }
  84. }
  85. #else
  86. /* POSIX */
  87. static void sys_cache_info(int *isize, int *dsize)
  88. {
  89. # ifdef _SC_LEVEL1_ICACHE_LINESIZE
  90. int tmp_isize = (int) sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
  91. if (tmp_isize > 0) {
  92. *isize = tmp_isize;
  93. }
  94. # endif
  95. # ifdef _SC_LEVEL1_DCACHE_LINESIZE
  96. int tmp_dsize = (int) sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
  97. if (tmp_dsize > 0) {
  98. *dsize = tmp_dsize;
  99. }
  100. # endif
  101. }
  102. #endif /* sys_cache_info */
  103. /*
  104. * Architecture (+ OS) specific cache detection mechanisms.
  105. */
  106. #if defined(__powerpc__)
  107. static bool have_coherent_icache;
  108. #endif
  109. #if defined(__aarch64__) && !defined(CONFIG_DARWIN) && !defined(CONFIG_WIN32)
  110. /*
  111. * Apple does not expose CTR_EL0, so we must use system interfaces.
  112. * Windows neither, but we use a generic implementation of flush_idcache_range
  113. * in this case.
  114. */
  115. static uint64_t save_ctr_el0;
  116. static void arch_cache_info(int *isize, int *dsize)
  117. {
  118. uint64_t ctr;
  119. /*
  120. * The real cache geometry is in CCSIDR_EL1/CLIDR_EL1/CSSELR_EL1,
  121. * but (at least under Linux) these are marked protected by the
  122. * kernel. However, CTR_EL0 contains the minimum linesize in the
  123. * entire hierarchy, and is used by userspace cache flushing.
  124. *
  125. * We will also use this value in flush_idcache_range.
  126. */
  127. asm volatile("mrs\t%0, ctr_el0" : "=r"(ctr));
  128. save_ctr_el0 = ctr;
  129. if (*isize == 0 || *dsize == 0) {
  130. if (*isize == 0) {
  131. *isize = 4 << (ctr & 0xf);
  132. }
  133. if (*dsize == 0) {
  134. *dsize = 4 << ((ctr >> 16) & 0xf);
  135. }
  136. }
  137. }
  138. #elif defined(_ARCH_PPC) && defined(__linux__)
  139. # include "elf.h"
  140. static void arch_cache_info(int *isize, int *dsize)
  141. {
  142. if (*isize == 0) {
  143. *isize = qemu_getauxval(AT_ICACHEBSIZE);
  144. }
  145. if (*dsize == 0) {
  146. *dsize = qemu_getauxval(AT_DCACHEBSIZE);
  147. }
  148. have_coherent_icache = qemu_getauxval(AT_HWCAP) & PPC_FEATURE_ICACHE_SNOOP;
  149. }
  150. #else
  151. static void arch_cache_info(int *isize, int *dsize) { }
  152. #endif /* arch_cache_info */
  153. /*
  154. * ... and if all else fails ...
  155. */
  156. static void fallback_cache_info(int *isize, int *dsize)
  157. {
  158. /* If we can only find one of the two, assume they're the same. */
  159. if (*isize) {
  160. if (*dsize) {
  161. /* Success! */
  162. } else {
  163. *dsize = *isize;
  164. }
  165. } else if (*dsize) {
  166. *isize = *dsize;
  167. } else {
  168. #if defined(_ARCH_PPC)
  169. /*
  170. * For PPC, we're going to use the cache sizes computed for
  171. * flush_idcache_range. Which means that we must use the
  172. * architecture minimum.
  173. */
  174. *isize = *dsize = 16;
  175. #else
  176. /* Otherwise, 64 bytes is not uncommon. */
  177. *isize = *dsize = 64;
  178. #endif
  179. }
  180. }
  181. static void __attribute__((constructor)) init_cache_info(void)
  182. {
  183. int isize = 0, dsize = 0;
  184. sys_cache_info(&isize, &dsize);
  185. arch_cache_info(&isize, &dsize);
  186. fallback_cache_info(&isize, &dsize);
  187. assert((isize & (isize - 1)) == 0);
  188. assert((dsize & (dsize - 1)) == 0);
  189. qemu_icache_linesize = isize;
  190. qemu_icache_linesize_log = ctz32(isize);
  191. qemu_dcache_linesize = dsize;
  192. qemu_dcache_linesize_log = ctz32(dsize);
  193. qatomic64_init();
  194. }
  195. /*
  196. * Architecture (+ OS) specific cache flushing mechanisms.
  197. */
  198. #if defined(__i386__) || defined(__x86_64__) || defined(__s390__)
  199. /* Caches are coherent and do not require flushing; symbol inline. */
  200. #elif defined(__aarch64__) && !defined(CONFIG_WIN32)
  201. /*
  202. * For Windows, we use generic implementation of flush_idcache_range, that
  203. * performs a call to FlushInstructionCache, through __builtin___clear_cache.
  204. */
  205. #ifdef CONFIG_DARWIN
  206. /* Apple does not expose CTR_EL0, so we must use system interfaces. */
  207. extern void sys_icache_invalidate(void *start, size_t len);
  208. extern void sys_dcache_flush(void *start, size_t len);
  209. void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
  210. {
  211. sys_dcache_flush((void *)rw, len);
  212. sys_icache_invalidate((void *)rx, len);
  213. }
  214. #else
  215. /*
  216. * This is a copy of gcc's __aarch64_sync_cache_range, modified
  217. * to fit this three-operand interface.
  218. */
  219. void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
  220. {
  221. const unsigned CTR_IDC = 1u << 28;
  222. const unsigned CTR_DIC = 1u << 29;
  223. const uint64_t ctr_el0 = save_ctr_el0;
  224. const uintptr_t icache_lsize = qemu_icache_linesize;
  225. const uintptr_t dcache_lsize = qemu_dcache_linesize;
  226. uintptr_t p;
  227. /*
  228. * If CTR_EL0.IDC is enabled, Data cache clean to the Point of Unification
  229. * is not required for instruction to data coherence.
  230. */
  231. if (!(ctr_el0 & CTR_IDC)) {
  232. /*
  233. * Loop over the address range, clearing one cache line at once.
  234. * Data cache must be flushed to unification first to make sure
  235. * the instruction cache fetches the updated data.
  236. */
  237. for (p = rw & -dcache_lsize; p < rw + len; p += dcache_lsize) {
  238. asm volatile("dc\tcvau, %0" : : "r" (p) : "memory");
  239. }
  240. asm volatile("dsb\tish" : : : "memory");
  241. }
  242. /*
  243. * If CTR_EL0.DIC is enabled, Instruction cache cleaning to the Point
  244. * of Unification is not required for instruction to data coherence.
  245. */
  246. if (!(ctr_el0 & CTR_DIC)) {
  247. for (p = rx & -icache_lsize; p < rx + len; p += icache_lsize) {
  248. asm volatile("ic\tivau, %0" : : "r"(p) : "memory");
  249. }
  250. asm volatile ("dsb\tish" : : : "memory");
  251. }
  252. asm volatile("isb" : : : "memory");
  253. }
  254. #endif /* CONFIG_DARWIN */
  255. #elif defined(__mips__)
  256. #ifdef __OpenBSD__
  257. #include <machine/sysarch.h>
  258. #else
  259. #include <sys/cachectl.h>
  260. #endif
  261. void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
  262. {
  263. if (rx != rw) {
  264. cacheflush((void *)rw, len, DCACHE);
  265. }
  266. cacheflush((void *)rx, len, ICACHE);
  267. }
  268. #elif defined(__powerpc__)
  269. void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
  270. {
  271. uintptr_t p, b, e;
  272. size_t dsize, isize;
  273. /*
  274. * Some processors have coherent caches and support a simplified
  275. * flushing procedure. See
  276. * POWER9 UM, 4.6.2.2 Instruction Cache Block Invalidate (icbi)
  277. * https://ibm.ent.box.com/s/tmklq90ze7aj8f4n32er1mu3sy9u8k3k
  278. */
  279. if (have_coherent_icache) {
  280. asm volatile ("sync\n\t"
  281. "icbi 0,%0\n\t"
  282. "isync"
  283. : : "r"(rx) : "memory");
  284. return;
  285. }
  286. dsize = qemu_dcache_linesize;
  287. isize = qemu_icache_linesize;
  288. b = rw & ~(dsize - 1);
  289. e = (rw + len + dsize - 1) & ~(dsize - 1);
  290. for (p = b; p < e; p += dsize) {
  291. asm volatile ("dcbst 0,%0" : : "r"(p) : "memory");
  292. }
  293. asm volatile ("sync" : : : "memory");
  294. b = rx & ~(isize - 1);
  295. e = (rx + len + isize - 1) & ~(isize - 1);
  296. for (p = b; p < e; p += isize) {
  297. asm volatile ("icbi 0,%0" : : "r"(p) : "memory");
  298. }
  299. asm volatile ("sync" : : : "memory");
  300. asm volatile ("isync" : : : "memory");
  301. }
  302. #elif defined(__sparc__)
  303. void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
  304. {
  305. /* No additional data flush to the RW virtual address required. */
  306. uintptr_t p, end = (rx + len + 7) & -8;
  307. for (p = rx & -8; p < end; p += 8) {
  308. __asm__ __volatile__("flush\t%0" : : "r" (p));
  309. }
  310. }
  311. #else
  312. void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
  313. {
  314. if (rw != rx) {
  315. __builtin___clear_cache((char *)rw, (char *)rw + len);
  316. }
  317. __builtin___clear_cache((char *)rx, (char *)rx + len);
  318. }
  319. #endif