2023-05-18 05:50:45 +02:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2023-08-23 08:53:15 +02:00
|
|
|
* Host specific cpu identification for AArch64.
|
2023-05-18 05:50:45 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "host/cpuinfo.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_LINUX
|
|
|
|
# ifdef CONFIG_GETAUXVAL
|
|
|
|
# include <sys/auxv.h>
|
|
|
|
# else
|
|
|
|
# include <asm/hwcap.h>
|
|
|
|
# include "elf.h"
|
|
|
|
# endif
|
2023-08-16 02:33:56 +02:00
|
|
|
# ifndef HWCAP2_BTI
|
|
|
|
# define HWCAP2_BTI 0 /* added in glibc 2.32 */
|
|
|
|
# endif
|
2023-05-18 05:50:45 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_DARWIN
|
|
|
|
# include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
unsigned cpuinfo;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DARWIN
|
|
|
|
static bool sysctl_for_bool(const char *name)
|
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
size_t len = sizeof(val);
|
|
|
|
|
|
|
|
if (sysctlbyname(name, &val, &len, NULL, 0) == 0) {
|
|
|
|
return val != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We might in the future ask for properties not present in older kernels,
|
|
|
|
* but we're only asking about static properties, all of which should be
|
2023-08-23 08:53:15 +02:00
|
|
|
* 'int'. So we shouldn't see ENOMEM (val too small), or any of the other
|
2023-05-18 05:50:45 +02:00
|
|
|
* more exotic errors.
|
|
|
|
*/
|
|
|
|
assert(errno == ENOENT);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Called both as constructor and (possibly) via other constructors. */
|
|
|
|
unsigned __attribute__((constructor)) cpuinfo_init(void)
|
|
|
|
{
|
|
|
|
unsigned info = cpuinfo;
|
|
|
|
|
|
|
|
if (info) {
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = CPUINFO_ALWAYS;
|
|
|
|
|
|
|
|
#ifdef CONFIG_LINUX
|
|
|
|
unsigned long hwcap = qemu_getauxval(AT_HWCAP);
|
|
|
|
info |= (hwcap & HWCAP_ATOMICS ? CPUINFO_LSE : 0);
|
|
|
|
info |= (hwcap & HWCAP_USCAT ? CPUINFO_LSE2 : 0);
|
2023-07-12 20:21:19 +02:00
|
|
|
info |= (hwcap & HWCAP_AES ? CPUINFO_AES : 0);
|
|
|
|
info |= (hwcap & HWCAP_PMULL ? CPUINFO_PMULL : 0);
|
2023-08-16 02:33:56 +02:00
|
|
|
|
|
|
|
unsigned long hwcap2 = qemu_getauxval(AT_HWCAP2);
|
|
|
|
info |= (hwcap2 & HWCAP2_BTI ? CPUINFO_BTI : 0);
|
2023-05-18 05:50:45 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_DARWIN
|
|
|
|
info |= sysctl_for_bool("hw.optional.arm.FEAT_LSE") * CPUINFO_LSE;
|
|
|
|
info |= sysctl_for_bool("hw.optional.arm.FEAT_LSE2") * CPUINFO_LSE2;
|
2023-06-02 09:43:40 +02:00
|
|
|
info |= sysctl_for_bool("hw.optional.arm.FEAT_AES") * CPUINFO_AES;
|
2023-07-12 20:21:19 +02:00
|
|
|
info |= sysctl_for_bool("hw.optional.arm.FEAT_PMULL") * CPUINFO_PMULL;
|
2023-08-16 02:33:56 +02:00
|
|
|
info |= sysctl_for_bool("hw.optional.arm.FEAT_BTI") * CPUINFO_BTI;
|
2023-05-18 05:50:45 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
cpuinfo = info;
|
|
|
|
return info;
|
|
|
|
}
|