Skip to content

Commit

Permalink
Add detection hwcap cpuid to hwcaps.h
Browse files Browse the repository at this point in the history
  • Loading branch information
toor1245 committed Sep 15, 2023
1 parent d773ad7 commit afe7840
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 55 deletions.
82 changes: 43 additions & 39 deletions include/internal/hwcaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,32 +198,32 @@ CPU_FEATURES_START_CPP_NAMESPACE
#endif

// https://elixir.bootlin.com/linux/v6.0-rc6/source/arch/s390/include/asm/elf.h
#define HWCAP_S390_ESAN3 1
#define HWCAP_S390_ZARCH 2
#define HWCAP_S390_STFLE 4
#define HWCAP_S390_MSA 8
#define HWCAP_S390_LDISP 16
#define HWCAP_S390_EIMM 32
#define HWCAP_S390_DFP 64
#define HWCAP_S390_HPAGE 128
#define HWCAP_S390_ETF3EH 256
#define HWCAP_S390_HIGH_GPRS 512
#define HWCAP_S390_TE 1024
#define HWCAP_S390_VX 2048
#define HWCAP_S390_VXRS HWCAP_S390_VX
#define HWCAP_S390_VXD 4096
#define HWCAP_S390_VXRS_BCD HWCAP_S390_VXD
#define HWCAP_S390_VXE 8192
#define HWCAP_S390_VXRS_EXT HWCAP_S390_VXE
#define HWCAP_S390_GS 16384
#define HWCAP_S390_VXRS_EXT2 32768
#define HWCAP_S390_VXRS_PDE 65536
#define HWCAP_S390_SORT 131072
#define HWCAP_S390_DFLT 262144
#define HWCAP_S390_VXRS_PDE2 524288
#define HWCAP_S390_NNPA 1048576
#define HWCAP_S390_PCI_MIO 2097152
#define HWCAP_S390_SIE 4194304
#define HWCAP_S390_ESAN3 1
#define HWCAP_S390_ZARCH 2
#define HWCAP_S390_STFLE 4
#define HWCAP_S390_MSA 8
#define HWCAP_S390_LDISP 16
#define HWCAP_S390_EIMM 32
#define HWCAP_S390_DFP 64
#define HWCAP_S390_HPAGE 128
#define HWCAP_S390_ETF3EH 256
#define HWCAP_S390_HIGH_GPRS 512
#define HWCAP_S390_TE 1024
#define HWCAP_S390_VX 2048
#define HWCAP_S390_VXRS HWCAP_S390_VX
#define HWCAP_S390_VXD 4096
#define HWCAP_S390_VXRS_BCD HWCAP_S390_VXD
#define HWCAP_S390_VXE 8192
#define HWCAP_S390_VXRS_EXT HWCAP_S390_VXE
#define HWCAP_S390_GS 16384
#define HWCAP_S390_VXRS_EXT2 32768
#define HWCAP_S390_VXRS_PDE 65536
#define HWCAP_S390_SORT 131072
#define HWCAP_S390_DFLT 262144
#define HWCAP_S390_VXRS_PDE2 524288
#define HWCAP_S390_NNPA 1048576
#define HWCAP_S390_PCI_MIO 2097152
#define HWCAP_S390_SIE 4194304

// https://elixir.bootlin.com/linux/latest/source/arch/riscv/include/uapi/asm/hwcap.h
#define RISCV_HWCAP_32 0x32
Expand All @@ -238,20 +238,20 @@ CPU_FEATURES_START_CPP_NAMESPACE
#define RISCV_HWCAP_V (1UL << ('V' - 'A'))

// https://github.com/torvalds/linux/blob/master/arch/loongarch/include/uapi/asm/hwcap.h
#define HWCAP_LOONGARCH_CPUCFG (1 << 0)
#define HWCAP_LOONGARCH_LAM (1 << 1)
#define HWCAP_LOONGARCH_UAL (1 << 2)
#define HWCAP_LOONGARCH_FPU (1 << 3)
#define HWCAP_LOONGARCH_LSX (1 << 4)
#define HWCAP_LOONGARCH_LASX (1 << 5)
#define HWCAP_LOONGARCH_CRC32 (1 << 6)
#define HWCAP_LOONGARCH_COMPLEX (1 << 7)
#define HWCAP_LOONGARCH_CRYPTO (1 << 8)
#define HWCAP_LOONGARCH_LVZ (1 << 9)
#define HWCAP_LOONGARCH_LBT_X86 (1 << 10)
#define HWCAP_LOONGARCH_LBT_ARM (1 << 11)
#define HWCAP_LOONGARCH_CPUCFG (1 << 0)
#define HWCAP_LOONGARCH_LAM (1 << 1)
#define HWCAP_LOONGARCH_UAL (1 << 2)
#define HWCAP_LOONGARCH_FPU (1 << 3)
#define HWCAP_LOONGARCH_LSX (1 << 4)
#define HWCAP_LOONGARCH_LASX (1 << 5)
#define HWCAP_LOONGARCH_CRC32 (1 << 6)
#define HWCAP_LOONGARCH_COMPLEX (1 << 7)
#define HWCAP_LOONGARCH_CRYPTO (1 << 8)
#define HWCAP_LOONGARCH_LVZ (1 << 9)
#define HWCAP_LOONGARCH_LBT_X86 (1 << 10)
#define HWCAP_LOONGARCH_LBT_ARM (1 << 11)
#define HWCAP_LOONGARCH_LBT_MIPS (1 << 12)
#define HWCAP_LOONGARCH_PTW (1 << 13)
#define HWCAP_LOONGARCH_PTW (1 << 13)

typedef struct {
unsigned long hwcaps;
Expand All @@ -272,6 +272,10 @@ const char* CpuFeatures_GetPlatformPointer(void);
// Get pointer for the AT_BASE_PLATFORM type.
const char* CpuFeatures_GetBasePlatformPointer(void);

// Checks whether privilege system registers are supported in user mode for
// AArch64.
bool CpuFeatures_IsHwCapCpuidSupported();

CPU_FEATURES_END_CPP_NAMESPACE

#endif // CPU_FEATURES_INCLUDE_INTERNAL_HWCAPS_H_
7 changes: 5 additions & 2 deletions src/hwcaps_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "cpu_features_macros.h"

#ifdef CPU_FEATURES_ARCH_AARCH64
#ifdef CPU_FEATURES_OS_FREEBSD

#include "internal/hwcaps.h"
Expand All @@ -27,6 +26,7 @@ const char* CpuFeatures_GetBasePlatformPointer(void);
#else

#ifdef HAVE_STRONG_ELF_AUX_INFO
#include <stdbool.h>
#include <stddef.h>
#include <sys/auxv.h>

Expand All @@ -46,10 +46,13 @@ HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) {
const char *CpuFeatures_GetPlatformPointer(void) { return NULL; }

const char *CpuFeatures_GetBasePlatformPointer(void) { return NULL; }

bool CpuFeatures_IsHwCapCpuidSupported() {
return GetElfHwcapFromElfAuxInfo(AARCH64_HWCAP_CPUID);
}
#else
#error "FreeBSD needs support for elf_aux_info"
#endif // HAVE_STRONG_ELF_AUX_INFO

#endif // CPU_FEATURES_TEST
#endif // CPU_FEATURES_OS_FREEBSD
#endif // CPU_FEATURES_ARCH_AARCH64
4 changes: 4 additions & 0 deletions src/hwcaps_linux_or_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,9 @@ const char *CpuFeatures_GetBasePlatformPointer(void) {
return (const char *)GetHardwareCapabilitiesFor(AT_BASE_PLATFORM);
}

bool CpuFeatures_IsHwCapCpuidSupported() {
return GetElfHwcapFromGetauxval(AARCH64_HWCAP_CPUID);
}

#endif // CPU_FEATURES_TEST
#endif // defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)
22 changes: 8 additions & 14 deletions src/impl_aarch64_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <printf.h>

#include "cpu_features_macros.h"

#ifdef CPU_FEATURES_ARCH_AARCH64
#ifdef CPU_FEATURES_OS_FREEBSD

#include "cpuinfo_aarch64.h"
#include "internal/cpuid_aarch64.h"
#include "impl_aarch64__base_implementation.inl"
#include "sys/auxv.h"
#include "internal/cpuid_aarch64.h"
#include "internal/hwcaps.h"

static const Aarch64Info kEmptyAarch64Info;

Expand All @@ -34,17 +32,13 @@ Aarch64Info GetAarch64Info(void) {
kSetters[i](&info.features, true);
}
}

unsigned long hwcap_cpuid;
elf_aux_info(HWCAP_CPUID, &hwcap_cpuid, sizeof(hwcap_cpuid));

if (hwcap_cpuid) {
info.features.cpuid = true;
info.features.cpuid = CpuFeatures_IsHwCapCpuidSupported();
if (info.features.cpuid) {
const uint64_t midr_el1 = GetMidrEl1();
info.implementer = (int) ExtractBitRange(midr_el1, 31, 24);
info.variant = (int) ExtractBitRange(midr_el1, 23, 20);
info.part = (int) ExtractBitRange(midr_el1, 15, 4);
info.revision = (int) ExtractBitRange(midr_el1, 3, 0);
info.implementer = (int)ExtractBitRange(midr_el1, 31, 24);
info.variant = (int)ExtractBitRange(midr_el1, 23, 20);
info.part = (int)ExtractBitRange(midr_el1, 15, 4);
info.revision = (int)ExtractBitRange(midr_el1, 3, 0);
}
return info;
}
Expand Down

0 comments on commit afe7840

Please sign in to comment.