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"
10 #include "qemu/host-utils.h"
11 #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
;
19 * Operating system specific detection mechanisms.
24 static void sys_cache_info(int *isize
, int *dsize
)
26 SYSTEM_LOGICAL_PROCESSOR_INFORMATION
*buf
;
31 /* Check for the required buffer size first. Note that if the zero
32 size we use for the probe results in success, then there is no
33 data available; fail in that case. */
34 success
= GetLogicalProcessorInformation(0, &size
);
35 if (success
|| GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
39 n
= size
/ sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION
);
40 size
= n
* sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION
);
41 buf
= g_new0(SYSTEM_LOGICAL_PROCESSOR_INFORMATION
, n
);
42 if (!GetLogicalProcessorInformation(buf
, &size
)) {
46 for (i
= 0; i
< n
; i
++) {
47 if (buf
[i
].Relationship
== RelationCache
48 && buf
[i
].Cache
.Level
== 1) {
49 switch (buf
[i
].Cache
.Type
) {
51 *isize
= *dsize
= buf
[i
].Cache
.LineSize
;
53 case CacheInstruction
:
54 *isize
= buf
[i
].Cache
.LineSize
;
57 *dsize
= buf
[i
].Cache
.LineSize
;
68 #elif defined(__APPLE__)
69 # include <sys/sysctl.h>
70 static void sys_cache_info(int *isize
, int *dsize
)
72 /* There's only a single sysctl for both I/D cache line sizes. */
74 size_t len
= sizeof(size
);
75 if (!sysctlbyname("hw.cachelinesize", &size
, &len
, NULL
, 0)) {
76 *isize
= *dsize
= size
;
79 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
80 # include <sys/sysctl.h>
81 static void sys_cache_info(int *isize
, int *dsize
)
83 /* There's only a single sysctl for both I/D cache line sizes. */
85 size_t len
= sizeof(size
);
86 if (!sysctlbyname("machdep.cacheline_size", &size
, &len
, NULL
, 0)) {
87 *isize
= *dsize
= size
;
93 static void sys_cache_info(int *isize
, int *dsize
)
95 # ifdef _SC_LEVEL1_ICACHE_LINESIZE
96 *isize
= sysconf(_SC_LEVEL1_ICACHE_LINESIZE
);
98 # ifdef _SC_LEVEL1_DCACHE_LINESIZE
99 *dsize
= sysconf(_SC_LEVEL1_DCACHE_LINESIZE
);
102 #endif /* sys_cache_info */
105 * Architecture (+ OS) specific detection mechanisms.
108 #if defined(__aarch64__)
110 static void arch_cache_info(int *isize
, int *dsize
)
112 if (*isize
== 0 || *dsize
== 0) {
115 /* The real cache geometry is in CCSIDR_EL1/CLIDR_EL1/CSSELR_EL1,
116 but (at least under Linux) these are marked protected by the
117 kernel. However, CTR_EL0 contains the minimum linesize in the
118 entire hierarchy, and is used by userspace cache flushing. */
119 asm volatile("mrs\t%0, ctr_el0" : "=r"(ctr
));
121 *isize
= 4 << (ctr
& 0xf);
124 *dsize
= 4 << ((ctr
>> 16) & 0xf);
129 #elif defined(_ARCH_PPC) && defined(__linux__)
132 static void arch_cache_info(int *isize
, int *dsize
)
135 *isize
= qemu_getauxval(AT_ICACHEBSIZE
);
138 *dsize
= qemu_getauxval(AT_DCACHEBSIZE
);
143 static void arch_cache_info(int *isize
, int *dsize
) { }
144 #endif /* arch_cache_info */
147 * ... and if all else fails ...
150 static void fallback_cache_info(int *isize
, int *dsize
)
152 /* If we can only find one of the two, assume they're the same. */
162 #if defined(_ARCH_PPC)
163 /* For PPC, we're going to use the icache size computed for
164 flush_icache_range. Which means that we must use the
165 architecture minimum. */
166 *isize
= *dsize
= 16;
168 /* Otherwise, 64 bytes is not uncommon. */
169 *isize
= *dsize
= 64;
174 static void __attribute__((constructor
)) init_cache_info(void)
176 int isize
= 0, dsize
= 0;
178 sys_cache_info(&isize
, &dsize
);
179 arch_cache_info(&isize
, &dsize
);
180 fallback_cache_info(&isize
, &dsize
);
182 assert((isize
& (isize
- 1)) == 0);
183 assert((dsize
& (dsize
- 1)) == 0);
185 qemu_icache_linesize
= isize
;
186 qemu_icache_linesize_log
= ctz32(isize
);
187 qemu_dcache_linesize
= dsize
;
188 qemu_dcache_linesize_log
= ctz32(dsize
);