diff options
author | Marat Dukhan <marat@fb.com> | 2017-11-29 15:11:56 -0800 |
---|---|---|
committer | Marat Dukhan <marat@fb.com> | 2017-11-29 15:14:52 -0800 |
commit | 63a7a6b48f72e7eef62522240a39d3bd0eb7fb02 (patch) | |
tree | cae717ea9e1eed455693edab6d45711b8ed82cdc /src/arm | |
parent | 3708e8f43ef7020168c52bea80213d206d6a562f (diff) | |
download | cpuinfo-63a7a6b48f72e7eef62522240a39d3bd0eb7fb02.tar.gz |
More robust ISA detection on ARM Linux
Diffstat (limited to 'src/arm')
-rw-r--r-- | src/arm/linux/api.h | 8 | ||||
-rw-r--r-- | src/arm/linux/hwcap.c | 149 | ||||
-rw-r--r-- | src/arm/linux/init.c | 65 |
3 files changed, 200 insertions, 22 deletions
diff --git a/src/arm/linux/api.h b/src/arm/linux/api.h index 6764032..7bbdc01 100644 --- a/src/arm/linux/api.h +++ b/src/arm/linux/api.h @@ -246,6 +246,13 @@ bool cpuinfo_arm_linux_parse_proc_cpuinfo( struct cpuinfo_arm_linux_processor processors[restrict static max_processors_count]); #if CPUINFO_ARCH_ARM + bool cpuinfo_arm_linux_hwcap_from_getauxval( + uint32_t hwcap[restrict static 1], + uint32_t hwcap2[restrict static 1]); + bool cpuinfo_arm_linux_hwcap_from_procfs( + uint32_t hwcap[restrict static 1], + uint32_t hwcap2[restrict static 1]); + void cpuinfo_arm_linux_decode_isa_from_proc_cpuinfo( uint32_t features, uint32_t features2, @@ -254,6 +261,7 @@ bool cpuinfo_arm_linux_parse_proc_cpuinfo( uint32_t architecture_flags, struct cpuinfo_arm_isa isa[restrict static 1]); #elif CPUINFO_ARCH_ARM64 + uint32_t cpuinfo_arm_linux_hwcap_from_getauxval(void); void cpuinfo_arm64_linux_decode_isa_from_proc_cpuinfo( uint32_t features, struct cpuinfo_arm_isa isa[restrict static 1]); diff --git a/src/arm/linux/hwcap.c b/src/arm/linux/hwcap.c new file mode 100644 index 0000000..d60b518 --- /dev/null +++ b/src/arm/linux/hwcap.c @@ -0,0 +1,149 @@ +#include <string.h> + +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/auxv.h> +#include <fcntl.h> +#include <errno.h> +#include <dlfcn.h> +#include <elf.h> + +#if CPUINFO_MOCK + #include <cpuinfo-mock.h> +#endif +#include <cpuinfo.h> +#include <arm/linux/api.h> +#include <log.h> + + +#if CPUINFO_MOCK + static uint32_t mock_hwcap = 0; + void cpuinfo_set_hwcap(uint32_t hwcap) { + mock_hwcap = hwcap; + } + + #if CPUINFO_ARCH_ARM + static uint32_t mock_hwcap2 = 0; + void cpuinfo_set_hwcap2(uint32_t hwcap2) { + mock_hwcap2 = hwcap2; + } + #endif +#endif + + +#if CPUINFO_ARCH_ARM + typedef unsigned long (*getauxval_function_t)(unsigned long); + + bool cpuinfo_arm_linux_hwcap_from_getauxval( + uint32_t hwcap[restrict static 1], + uint32_t hwcap2[restrict static 1]) + { + #if CPUINFO_MOCK + *hwcap = mock_hwcap; + *hwcap2 = mock_hwcap2; + return true; + #elif defined(__ANDROID__) + /* Android: dynamically check if getauxval is supported */ + void* libc = NULL; + getauxval_function_t getauxval = NULL; + + dlerror(); + libc = dlopen("libc.so", RTLD_LAZY); + if (libc == NULL) { + cpuinfo_log_warning("failed to load libc.so: %s", dlerror()); + goto cleanup; + } + + getauxval = (getauxval_function_t) dlsym(libc, "getauxval"); + if (getauxval == NULL) { + cpuinfo_log_info("failed to locate getauxval in libc.so: %s", dlerror()); + goto cleanup; + } + + *hwcap = getauxval(AT_HWCAP); + *hwcap2 = getauxval(AT_HWCAP2); + + cleanup: + if (libc != NULL) { + dlclose(libc); + libc = NULL; + } + return getauxval != NULL; + #else + /* GNU/Linux: getauxval is always supported */ + *hwcap = getauxval(AT_HWCAP); + *hwcap2 = getauxval(AT_HWCAP2); + return true; + #endif + } + + #ifdef __ANDROID__ + bool cpuinfo_arm_linux_hwcap_from_procfs( + uint32_t hwcap[restrict static 1], + uint32_t hwcap2[restrict static 1]) + { + #if CPUINFO_MOCK + *hwcap = mock_hwcap; + *hwcap2 = mock_hwcap2; + return true; + #else + uint32_t hwcaps[2] = { 0, 0 }; + bool result = false; + int file = -1; + + file = open("/proc/self/auxv", O_RDONLY); + if (file == -1) { + cpuinfo_log_warning("failed to open /proc/self/auxv: %s", strerror(errno)); + goto cleanup; + } + + ssize_t bytes_read; + do { + Elf32_auxv_t elf_auxv; + bytes_read = read(file, &elf_auxv, sizeof(Elf32_auxv_t)); + if (bytes_read < 0) { + cpuinfo_log_warning("failed to read /proc/self/auxv: %s", strerror(errno)); + goto cleanup; + } else if (bytes_read > 0) { + if (bytes_read == sizeof(elf_auxv)) { + switch (elf_auxv.a_type) { + case AT_HWCAP: + hwcaps[0] = (uint32_t) elf_auxv.a_un.a_val; + break; + case AT_HWCAP2: + hwcaps[1] = (uint32_t) elf_auxv.a_un.a_val; + break; + } + } else { + cpuinfo_log_warning( + "failed to read %zu bytes from /proc/self/auxv: %zu bytes available", + sizeof(elf_auxv), (size_t) bytes_read); + goto cleanup; + } + } + } while (bytes_read == sizeof(Elf32_auxv_t)); + + /* Success, commit results */ + *hwcap = hwcaps[0]; + *hwcap2 = hwcaps[1]; + result = true; + + cleanup: + if (file != -1) { + close(file); + file = -1; + } + return result; + #endif + } + #endif /* __ANDROID__ */ +#elif CPUINFO_ARCH_ARM64 + uint32_t cpuinfo_arm_linux_hwcap_from_getauxval(void) { + #if CPUINFO_MOCK + return mock_hwcap; + #else + return (uint32_t) getauxval(AT_HWCAP); + #endif + } +#endif diff --git a/src/arm/linux/init.c b/src/arm/linux/init.c index d7ad54a..cb3d748 100644 --- a/src/arm/linux/init.c +++ b/src/arm/linux/init.c @@ -202,35 +202,56 @@ void cpuinfo_arm_linux_init(void) { } } - /* Detect ISA. If different processors report different ISA features, take the intersection. */ - uint32_t isa_features = 0, processors_with_features = 0; #if CPUINFO_ARCH_ARM - uint32_t isa_features2 = 0; - #endif - for (uint32_t i = 0; i < arm_linux_processors_count; i++) { - if (bitmask_all(arm_linux_processors[i].flags, CPUINFO_LINUX_MASK_USABLE | CPUINFO_ARM_LINUX_VALID_FEATURES)) { - if (processors_with_features == 0) { - isa_features = arm_linux_processors[i].features; - #if CPUINFO_ARCH_ARM - isa_features2 = arm_linux_processors[i].features2; - #endif - } else { - isa_features &= arm_linux_processors[i].features; - #if CPUINFO_ARCH_ARM - isa_features2 &= arm_linux_processors[i].features2; - #endif + uint32_t isa_features = 0, isa_features2 = 0; + #ifdef __ANDROID__ + /* + * On Android before API 20, libc.so does not provide getauxval function. + * Thus, we try to dynamically find it, or use two fallback mechanisms: + * 1. dlopen libc.so, and try to find getauxval + * 2. Parse /proc/self/auxv procfs file + * 3. Use features reported in /proc/cpuinfo + */ + if (!cpuinfo_arm_linux_hwcap_from_getauxval(&isa_features, &isa_features2)) { + /* getauxval can't be used, fall back to parsing /proc/self/auxv */ + if (!cpuinfo_arm_linux_hwcap_from_procfs(&isa_features, &isa_features2)) { + /* + * Reading /proc/self/auxv failed, probably due to file permissions. + * Use information from /proc/cpuinfo to detect ISA. + * + * If different processors report different ISA features, take the intersection. + */ + uint32_t processors_with_features = 0; + for (uint32_t i = 0; i < arm_linux_processors_count; i++) { + if (bitmask_all(arm_linux_processors[i].flags, CPUINFO_LINUX_MASK_USABLE | CPUINFO_ARM_LINUX_VALID_FEATURES)) { + if (processors_with_features == 0) { + isa_features = arm_linux_processors[i].features; + #if CPUINFO_ARCH_ARM + isa_features2 = arm_linux_processors[i].features2; + #endif + } else { + isa_features &= arm_linux_processors[i].features; + #if CPUINFO_ARCH_ARM + isa_features2 &= arm_linux_processors[i].features2; + #endif + } + processors_with_features += 1; + } + } + } } - processors_with_features += 1; - } - } - #if CPUINFO_ARCH_ARM + #else + /* On GNU/Linux getauxval is always available */ + cpuinfo_arm_linux_hwcap_from_getauxval(&isa_features, &isa_features2); + #endif cpuinfo_arm_linux_decode_isa_from_proc_cpuinfo( isa_features, isa_features2, last_midr, last_architecture_version, last_architecture_flags, &cpuinfo_isa); #elif CPUINFO_ARCH_ARM64 - cpuinfo_arm64_linux_decode_isa_from_proc_cpuinfo( - isa_features, &cpuinfo_isa); + /* getauxval is always available on ARM64 Android */ + const uint32_t isa_features = cpuinfo_arm_linux_hwcap_from_getauxval(); + cpuinfo_arm64_linux_decode_isa_from_proc_cpuinfo(isa_features, &cpuinfo_isa); #endif /* Detect min/max frequency and package ID */ |