2 * cacheinfo.c - helpers to query the host about its caches
4 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
5 * License: GNU GPL, version 2 or later.
6 * See the COPYING file in the top-level directory.
9 #include "qemu/osdep.h"
11 int qemu_icache_linesize
= 0;
12 int qemu_dcache_linesize
= 0;
15 * Operating system specific detection mechanisms.
19 # include <sys/systemcfg.h>
21 static void sys_cache_info(int *isize
, int *dsize
)
23 *isize
= _system_configuration
.icache_line
;
24 *dsize
= _system_configuration
.dcache_line
;
29 static void sys_cache_info(int *isize
, int *dsize
)
31 SYSTEM_LOGICAL_PROCESSOR_INFORMATION
*buf
;
36 /* Check for the required buffer size first. Note that if the zero
37 size we use for the probe results in success, then there is no
38 data available; fail in that case. */
39 success
= GetLogicalProcessorInformation(0, &size
);
40 if (success
|| GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
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
)) {
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
) {
56 *isize
= *dsize
= buf
[i
].Cache
.LineSize
;
58 case CacheInstruction
:
59 *isize
= buf
[i
].Cache
.LineSize
;
62 *dsize
= buf
[i
].Cache
.LineSize
;
73 #elif defined(__APPLE__) \
74 || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
75 # include <sys/sysctl.h>
76 # if defined(__APPLE__)
77 # define SYSCTL_CACHELINE_NAME "hw.cachelinesize"
79 # define SYSCTL_CACHELINE_NAME "machdep.cacheline_size"
82 static void sys_cache_info(int *isize
, int *dsize
)
84 /* There's only a single sysctl for both I/D cache line sizes. */
86 size_t len
= sizeof(size
);
87 if (!sysctlbyname(SYSCTL_CACHELINE_NAME
, &size
, &len
, NULL
, 0)) {
88 *isize
= *dsize
= size
;
95 static void sys_cache_info(int *isize
, int *dsize
)
97 # ifdef _SC_LEVEL1_ICACHE_LINESIZE
98 *isize
= sysconf(_SC_LEVEL1_ICACHE_LINESIZE
);
100 # ifdef _SC_LEVEL1_DCACHE_LINESIZE
101 *dsize
= sysconf(_SC_LEVEL1_DCACHE_LINESIZE
);
104 #endif /* sys_cache_info */
107 * Architecture (+ OS) specific detection mechanisms.
110 #if defined(__aarch64__)
112 static void arch_cache_info(int *isize
, int *dsize
)
114 if (*isize
== 0 || *dsize
== 0) {
117 /* The real cache geometry is in CCSIDR_EL1/CLIDR_EL1/CSSELR_EL1,
118 but (at least under Linux) these are marked protected by the
119 kernel. However, CTR_EL0 contains the minimum linesize in the
120 entire hierarchy, and is used by userspace cache flushing. */
121 asm volatile("mrs\t%0, ctr_el0" : "=r"(ctr
));
123 *isize
= 4 << (ctr
& 0xf);
126 *dsize
= 4 << ((ctr
>> 16) & 0xf);
131 #elif defined(_ARCH_PPC) && defined(__linux__)
134 static void arch_cache_info(int *isize
, int *dsize
)
137 *isize
= qemu_getauxval(AT_ICACHEBSIZE
);
140 *dsize
= qemu_getauxval(AT_DCACHEBSIZE
);
145 static void arch_cache_info(int *isize
, int *dsize
) { }
146 #endif /* arch_cache_info */
149 * ... and if all else fails ...
152 static void fallback_cache_info(int *isize
, int *dsize
)
154 /* If we can only find one of the two, assume they're the same. */
164 #if defined(_ARCH_PPC)
165 /* For PPC, we're going to use the icache size computed for
166 flush_icache_range. Which means that we must use the
167 architecture minimum. */
168 *isize
= *dsize
= 16;
170 /* Otherwise, 64 bytes is not uncommon. */
171 *isize
= *dsize
= 64;
176 static void __attribute__((constructor
)) init_cache_info(void)
178 int isize
= 0, dsize
= 0;
180 sys_cache_info(&isize
, &dsize
);
181 arch_cache_info(&isize
, &dsize
);
182 fallback_cache_info(&isize
, &dsize
);
184 qemu_icache_linesize
= isize
;
185 qemu_dcache_linesize
= dsize
;