util: add cacheinfo

Add helpers to gather cache info from the host at init-time.

For now, only export the host's I/D cache line sizes, which we
will use to improve cache locality to avoid false sharing.

Backports commit b255b2c8a5484742606e8760870ba3e14d0c9605 from qemu
This commit is contained in:
Emilio G. Cota 2018-03-03 16:48:17 -05:00 committed by Lioncash
parent da4d407317
commit 8e58c67968
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
4 changed files with 196 additions and 0 deletions

View file

@ -270,6 +270,10 @@ struct uc_struct {
uint32_t target_page_align; uint32_t target_page_align;
uint64_t next_pc; // save next PC for some special cases uint64_t next_pc; // save next PC for some special cases
bool hook_insert; // insert new hook at begin of the hook list (append by default) bool hook_insert; // insert new hook at begin of the hook list (append by default)
// util/cacheinfo.c
int qemu_icache_linesize;
int qemu_dcache_linesize;
}; };
// Metadata stub for the variable-size cpu context used with uc_context_*() // Metadata stub for the variable-size cpu context used with uc_context_*()
@ -281,5 +285,9 @@ struct uc_context {
// check if this address is mapped in (via uc_mem_map()) // check if this address is mapped in (via uc_mem_map())
MemoryRegion *memory_mapping(struct uc_struct* uc, uint64_t address); MemoryRegion *memory_mapping(struct uc_struct* uc, uint64_t address);
// Defined in util/cacheinfo.c. Made externally linked to
// allow calling it directly.
void init_cache_info(struct uc_struct *uc);
#endif #endif
/* vim: set ts=4 noet: */ /* vim: set ts=4 noet: */

View file

@ -5,6 +5,7 @@ util-obj-$(CONFIG_POSIX) += mmap-alloc.o
util-obj-y += module.o util-obj-y += module.o
util-obj-y += bitmap.o bitops.o util-obj-y += bitmap.o bitops.o
util-obj-y += error.o util-obj-y += error.o
util-obj-y += cacheinfo.o
util-obj-y += crc32c.o util-obj-y += crc32c.o
util-obj-y += host-utils.o util-obj-y += host-utils.o
util-obj-y += getauxval.o util-obj-y += getauxval.o

184
qemu/util/cacheinfo.c Normal file
View file

@ -0,0 +1,184 @@
/*
* cacheinfo.c - helpers to query the host about its caches
*
* Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
* License: GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "uc_priv.h"
#include "qemu/osdep.h"
/*
* Operating system specific detection mechanisms.
*/
#if defined(_AIX)
# include <sys/systemcfg.h>
static void sys_cache_info(int *isize, int *dsize)
{
*isize = _system_configuration.icache_line;
*dsize = _system_configuration.dcache_line;
}
#elif defined(_WIN32)
static void sys_cache_info(int *isize, int *dsize)
{
SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buf;
DWORD size = 0;
BOOL success;
size_t i, n;
/* Check for the required buffer size first. Note that if the zero
size we use for the probe results in success, then there is no
data available; fail in that case. */
success = GetLogicalProcessorInformation(0, &size);
if (success || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
return;
}
n = size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
size = n * sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
buf = g_new0(SYSTEM_LOGICAL_PROCESSOR_INFORMATION, n);
if (!GetLogicalProcessorInformation(buf, &size)) {
goto fail;
}
for (i = 0; i < n; i++) {
if (buf[i].Relationship == RelationCache
&& buf[i].Cache.Level == 1) {
switch (buf[i].Cache.Type) {
case CacheUnified:
*isize = *dsize = buf[i].Cache.LineSize;
break;
case CacheInstruction:
*isize = buf[i].Cache.LineSize;
break;
case CacheData:
*dsize = buf[i].Cache.LineSize;
break;
default:
break;
}
}
}
fail:
g_free(buf);
}
#elif defined(__APPLE__) \
|| defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
# include <sys/sysctl.h>
# if defined(__APPLE__)
# define SYSCTL_CACHELINE_NAME "hw.cachelinesize"
# else
# define SYSCTL_CACHELINE_NAME "machdep.cacheline_size"
# endif
static void sys_cache_info(int *isize, int *dsize)
{
/* There's only a single sysctl for both I/D cache line sizes. */
long size;
size_t len = sizeof(size);
if (!sysctlbyname(SYSCTL_CACHELINE_NAME, &size, &len, NULL, 0)) {
*isize = *dsize = size;
}
}
#else
/* POSIX */
static void sys_cache_info(int *isize, int *dsize)
{
# ifdef _SC_LEVEL1_ICACHE_LINESIZE
*isize = sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
# endif
# ifdef _SC_LEVEL1_DCACHE_LINESIZE
*dsize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
# endif
}
#endif /* sys_cache_info */
/*
* Architecture (+ OS) specific detection mechanisms.
*/
#if defined(__aarch64__)
static void arch_cache_info(int *isize, int *dsize)
{
if (*isize == 0 || *dsize == 0) {
unsigned ctr;
/* The real cache geometry is in CCSIDR_EL1/CLIDR_EL1/CSSELR_EL1,
but (at least under Linux) these are marked protected by the
kernel. However, CTR_EL0 contains the minimum linesize in the
entire hierarchy, and is used by userspace cache flushing. */
asm volatile("mrs\t%0, ctr_el0" : "=r"(ctr));
if (*isize == 0) {
*isize = 4 << (ctr & 0xf);
}
if (*dsize == 0) {
*dsize = 4 << ((ctr >> 16) & 0xf);
}
}
}
#elif defined(_ARCH_PPC) && defined(__linux__)
static void arch_cache_info(int *isize, int *dsize)
{
if (*isize == 0) {
*isize = qemu_getauxval(AT_ICACHEBSIZE);
}
if (*dsize == 0) {
*dsize = qemu_getauxval(AT_DCACHEBSIZE);
}
}
#else
static void arch_cache_info(int *isize, int *dsize) { }
#endif /* arch_cache_info */
/*
* ... and if all else fails ...
*/
static void fallback_cache_info(int *isize, int *dsize)
{
/* If we can only find one of the two, assume they're the same. */
if (*isize) {
if (*dsize) {
/* Success! */
} else {
*dsize = *isize;
}
} else if (*dsize) {
*isize = *dsize;
} else {
#if defined(_ARCH_PPC)
/* For PPC, we're going to use the icache size computed for
flush_icache_range. Which means that we must use the
architecture minimum. */
*isize = *dsize = 16;
#else
/* Otherwise, 64 bytes is not uncommon. */
*isize = *dsize = 64;
#endif
}
}
// Unicorn: made externally linked so it can be called directly
void init_cache_info(struct uc_struct *uc)
{
int isize = 0, dsize = 0;
sys_cache_info(&isize, &dsize);
arch_cache_info(&isize, &dsize);
fallback_cache_info(&isize, &dsize);
uc->qemu_icache_linesize = isize;
uc->qemu_dcache_linesize = dsize;
}

View file

@ -120,6 +120,9 @@ int machine_initialize(struct uc_struct *uc)
cpu_register_types(uc); cpu_register_types(uc);
qdev_register_types(uc); qdev_register_types(uc);
// Initialize cache information
init_cache_info(uc);
// Initialize arch specific. // Initialize arch specific.
uc->init_arch(uc); uc->init_arch(uc);