Skip to content

Commit

Permalink
Different, simpler approach
Browse files Browse the repository at this point in the history
  • Loading branch information
gchatelet committed Jun 29, 2021
1 parent 001faef commit c7497ec
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 51 deletions.
7 changes: 6 additions & 1 deletion include/cpuinfo_ppc.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ typedef struct {
// This function is guaranteed to be malloc, memset and memcpy free.
PPCInfo GetPPCInfo(void);

typedef struct {
char platform[64]; // 0 terminated string
char base_platform[64]; // 0 terminated string
} PPCPlatformTypeStrings;

typedef struct {
char platform[64]; // 0 terminated string
char model[64]; // 0 terminated string
char machine[64]; // 0 terminated string
char cpu[64]; // 0 terminated string
PlatformType type;
PPCPlatformTypeStrings type;
} PPCPlatformStrings;

PPCPlatformStrings GetPPCPlatformStrings(void);
Expand Down
15 changes: 9 additions & 6 deletions include/internal/hwcaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,19 @@ typedef struct {
unsigned long hwcaps2;
} HardwareCapabilities;

// Retrieves values from auxiliary vector for types AT_HWCAP and AT_HWCAP2.
// First tries to call getauxval(), if not available falls back to reading
// "/proc/self/auxv".
HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void);

// Checks whether value for AT_HWCAP (or AT_HWCAP2) match hwcaps_mask.
bool CpuFeatures_IsHwCapsSet(const HardwareCapabilities hwcaps_mask,
const HardwareCapabilities hwcaps);

typedef struct {
char platform[64]; // 0 terminated string
char base_platform[64]; // 0 terminated string
} PlatformType;

PlatformType CpuFeatures_GetPlatformType(void);
// Get pointer for the AT_PLATFORM type.
const char* CpuFeatures_GetPlatformPointer(void);
// Get pointer for the AT_BASE_PLATFORM type.
const char* CpuFeatures_GetBasePlatformPointer(void);

CPU_FEATURES_END_CPP_NAMESPACE

Expand Down
12 changes: 11 additions & 1 deletion src/cpuinfo_ppc.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,19 @@ static const PPCPlatformStrings kEmptyPPCPlatformStrings;

PPCPlatformStrings GetPPCPlatformStrings(void) {
PPCPlatformStrings strings = kEmptyPPCPlatformStrings;
const char* platform = CpuFeatures_GetPlatformPointer();
const char* base_platform = CpuFeatures_GetBasePlatformPointer();

FillProcCpuInfoData(&strings);
strings.type = CpuFeatures_GetPlatformType();

if (platform != NULL)
CpuFeatures_StringView_CopyString(str(platform), strings.type.platform,
sizeof(strings.type.platform));
if (base_platform != NULL)
CpuFeatures_StringView_CopyString(str(base_platform),
strings.type.base_platform,
sizeof(strings.type.base_platform));

return strings;
}

Expand Down
21 changes: 7 additions & 14 deletions src/hwcaps.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ bool CpuFeatures_IsHwCapsSet(const HardwareCapabilities hwcaps_mask,
#ifdef CPU_FEATURES_TEST
// In test mode, hwcaps_for_testing will define the following functions.
HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void);
PlatformType CpuFeatures_GetPlatformType(void);
char* CpuFeatures_GetPlatformPointer(void);
char* CpuFeatures_GetBasePlatformPointer(void);
#else

// Debug facilities
Expand Down Expand Up @@ -163,20 +164,12 @@ HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) {
return capabilities;
}

PlatformType kEmptyPlatformType;

PlatformType CpuFeatures_GetPlatformType(void) {
PlatformType type = kEmptyPlatformType;
char *platform = (char *)GetHardwareCapabilitiesFor(AT_PLATFORM);
char *base_platform = (char *)GetHardwareCapabilitiesFor(AT_BASE_PLATFORM);
const char *CpuFeatures_GetPlatformPointer(void) {
return (char *)GetHardwareCapabilitiesFor(AT_PLATFORM);
}

if (platform != NULL)
CpuFeatures_StringView_CopyString(str(platform), type.platform,
sizeof(type.platform));
if (base_platform != NULL)
CpuFeatures_StringView_CopyString(str(base_platform), type.base_platform,
sizeof(type.base_platform));
return type;
const char *CpuFeatures_GetBasePlatformPointer(void) {
return (char *)GetHardwareCapabilitiesFor(AT_BASE_PLATFORM);
}

#endif // CPU_FEATURES_TEST
4 changes: 3 additions & 1 deletion test/cpuinfo_aarch64_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace {
void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }

TEST(CpuinfoAarch64Test, FromHardwareCap) {
ResetHwcaps();
SetHardwareCapabilities(AARCH64_HWCAP_FP | AARCH64_HWCAP_AES, 0);
GetEmptyFilesystem(); // disabling /proc/cpuinfo
const auto info = GetAarch64Info();
Expand Down Expand Up @@ -62,6 +63,7 @@ TEST(CpuinfoAarch64Test, FromHardwareCap) {
}

TEST(CpuinfoAarch64Test, FromHardwareCap2) {
ResetHwcaps();
SetHardwareCapabilities(AARCH64_HWCAP_FP,
AARCH64_HWCAP2_SVE2 | AARCH64_HWCAP2_BTI);
GetEmptyFilesystem(); // disabling /proc/cpuinfo
Expand Down Expand Up @@ -90,7 +92,7 @@ TEST(CpuinfoAarch64Test, FromHardwareCap2) {
}

TEST(CpuinfoAarch64Test, ARMCortexA53) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(Processor : AArch64 Processor rev 3 (aarch64)
Expand Down
17 changes: 9 additions & 8 deletions test/cpuinfo_arm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
namespace cpu_features {
namespace {

void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }

TEST(CpuinfoArmTest, FromHardwareCap) {
ResetHwcaps();
SetHardwareCapabilities(ARM_HWCAP_NEON, ARM_HWCAP2_AES | ARM_HWCAP2_CRC32);
GetEmptyFilesystem(); // disabling /proc/cpuinfo
const auto info = GetArmInfo();
Expand Down Expand Up @@ -52,7 +52,7 @@ TEST(CpuinfoArmTest, FromHardwareCap) {
}

TEST(CpuinfoArmTest, ODroidFromCpuInfo) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo", R"(processor : 0
model name : ARMv7 Processor rev 3 (v71)
Expand Down Expand Up @@ -101,7 +101,7 @@ CPU revision : 3)");

// Linux test-case
TEST(CpuinfoArmTest, RaspberryPiZeroFromCpuInfo) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo", R"(processor : 0
model name : ARMv6-compatible processor rev 7 (v6l)
Expand Down Expand Up @@ -153,7 +153,7 @@ Serial : 000000006cd946f3)");
}

TEST(CpuinfoArmTest, MarvellArmadaFromCpuInfo) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo", R"(processor : 0
model name : ARMv7 Processor rev 1 (v7l)
Expand Down Expand Up @@ -217,7 +217,7 @@ Serial : 0000000000000000)");
// Android test-case
// http://code.google.com/p/android/issues/detail?id=10812
TEST(CpuinfoArmTest, InvalidArmv7) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(Processor : ARMv6-compatible processor rev 6 (v6l)
Expand Down Expand Up @@ -267,6 +267,7 @@ Serial : 33323613546d00ec )");
// Android test-case
// https://crbug.com/341598.
TEST(CpuinfoArmTest, InvalidNeon) {
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(Processor: ARMv7 Processory rev 0 (v71)
Expand Down Expand Up @@ -294,7 +295,7 @@ Serial: 00001e030000354e)");
// The Nexus 4 (Qualcomm Krait) kernel configuration forgets to report IDIV
// support.
TEST(CpuinfoArmTest, Nexus4_0x510006f2) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(CPU implementer : 0x51
Expand All @@ -312,7 +313,7 @@ CPU revision : 2)");
// The Nexus 4 (Qualcomm Krait) kernel configuration forgets to report IDIV
// support.
TEST(CpuinfoArmTest, Nexus4_0x510006f3) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(CPU implementer : 0x51
Expand All @@ -331,7 +332,7 @@ CPU revision : 3)");
// 32-bit ARM IDIV instruction. Technically, this is a feature of the virtual
// CPU implemented by the emulator.
TEST(CpuinfoArmTest, EmulatorSpecificIdiv) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(Processor : ARMv7 Processor rev 0 (v7l)
Expand Down
10 changes: 5 additions & 5 deletions test/cpuinfo_mips_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ namespace cpu_features {

namespace {

void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }

TEST(CpuinfoMipsTest, FromHardwareCapBoth) {
ResetHwcaps();
SetHardwareCapabilities(MIPS_HWCAP_MSA | MIPS_HWCAP_R6, 0);
GetEmptyFilesystem(); // disabling /proc/cpuinfo
const auto info = GetMipsInfo();
Expand All @@ -36,6 +35,7 @@ TEST(CpuinfoMipsTest, FromHardwareCapBoth) {
}

TEST(CpuinfoMipsTest, FromHardwareCapOnlyOne) {
ResetHwcaps();
SetHardwareCapabilities(MIPS_HWCAP_MSA, 0);
GetEmptyFilesystem(); // disabling /proc/cpuinfo
const auto info = GetMipsInfo();
Expand All @@ -44,7 +44,7 @@ TEST(CpuinfoMipsTest, FromHardwareCapOnlyOne) {
}

TEST(CpuinfoMipsTest, Ci40) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo", R"(system type : IMG Pistachio SoC (B0)
machine : IMG Marduk – Ci40 with cc2520
Expand Down Expand Up @@ -72,7 +72,7 @@ VPE : 0
}

TEST(CpuinfoMipsTest, AR7161) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(system type : Atheros AR7161 rev 2
Expand All @@ -98,7 +98,7 @@ VCEI exceptions : not available
}

TEST(CpuinfoMipsTest, Goldfish) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo", R"(system type : MIPS-Goldfish
Hardware : goldfish
Expand Down
11 changes: 6 additions & 5 deletions test/cpuinfo_ppc_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
namespace cpu_features {
namespace {

void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }

TEST(CpustringsPPCTest, FromHardwareCap) {
ResetHwcaps();
SetHardwareCapabilities(PPC_FEATURE_HAS_FPU | PPC_FEATURE_HAS_VSX,
PPC_FEATURE2_ARCH_3_00);
GetEmptyFilesystem(); // disabling /proc/cpuinfo
Expand All @@ -40,7 +40,7 @@ TEST(CpustringsPPCTest, FromHardwareCap) {
}

TEST(CpustringsPPCTest, Blade) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(processor : 14
Expand All @@ -57,7 +57,8 @@ timebase : 512000000
platform : pSeries
model : IBM,8406-70Y
machine : CHRP IBM,8406-70Y)");
SetPlatformTypes("power7", "power8");
SetPlatformPointer("power7");
SetBasePlatformPointer("power8");
const auto strings = GetPPCPlatformStrings();
ASSERT_STREQ(strings.platform, "pSeries");
ASSERT_STREQ(strings.model, "IBM,8406-70Y");
Expand All @@ -68,7 +69,7 @@ machine : CHRP IBM,8406-70Y)");
}

TEST(CpustringsPPCTest, Firestone) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(processor : 126
Expand All @@ -94,7 +95,7 @@ firmware : OPAL v3)");
}

TEST(CpustringsPPCTest, w8) {
DisableHardwareCapabilities();
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/proc/cpuinfo",
R"(processor : 143
Expand Down
23 changes: 14 additions & 9 deletions test/hwcaps_for_testing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,30 @@ namespace cpu_features {

namespace {
static auto* const g_hardware_capabilities = new HardwareCapabilities();
static auto* const g_platform_types = new PlatformType();
static const char* g_platform_pointer = nullptr;
static const char* g_base_platform_pointer = nullptr;
} // namespace

void SetHardwareCapabilities(uint32_t hwcaps, uint32_t hwcaps2) {
g_hardware_capabilities->hwcaps = hwcaps;
g_hardware_capabilities->hwcaps2 = hwcaps2;
}
void SetPlatformPointer(const char* string) { g_platform_pointer = string; }
void SetBasePlatformPointer(const char* string) {
g_base_platform_pointer = string;
}

HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) {
return *g_hardware_capabilities;
}

void SetPlatformTypes(const char* platform, const char* base_platform) {
CpuFeatures_StringView_CopyString(str(platform), g_platform_types->platform,
sizeof(g_platform_types->platform));
CpuFeatures_StringView_CopyString(str(base_platform),
g_platform_types->base_platform,
sizeof(g_platform_types->base_platform));
const char* CpuFeatures_GetPlatformPointer(void) { return g_platform_pointer; }
const char* CpuFeatures_GetBasePlatformPointer(void) {
return g_base_platform_pointer;
}

PlatformType CpuFeatures_GetPlatformType(void) { return *g_platform_types; }
void ResetHwcaps(){
SetHardwareCapabilities(0,0);
SetPlatformPointer(nullptr);
SetBasePlatformPointer(nullptr);
}
} // namespace cpu_features
6 changes: 5 additions & 1 deletion test/hwcaps_for_testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
namespace cpu_features {

void SetHardwareCapabilities(uint32_t hwcaps, uint32_t hwcaps2);
void SetPlatformTypes(const char *platform, const char *base_platform);
void SetPlatformPointer(const char* string);
void SetBasePlatformPointer(const char* string);

// To be called before each test.
void ResetHwcaps();

} // namespace cpu_features

Expand Down

0 comments on commit c7497ec

Please sign in to comment.