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