Botan 3.14.1
Crypto and TLS for C&
cpuid_ppc.cpp
Go to the documentation of this file.
1/*
2* Runtime CPU detection for POWER/PowerPC
3* (C) 2009,2010,2013,2017,2021,2024 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/cpuid.h>
9
10#include <botan/internal/os_utils.h>
11#include <botan/internal/target_info.h>
12
13namespace Botan {
14
15uint32_t CPUID::CPUID_Data::detect_cpu_features([[maybe_unused]] uint32_t allowed) {
16 uint32_t feat = 0;
17
18 if(auto auxval = OS::get_auxval_hwcap()) {
19 const auto [hwcap, hwcap2] = *auxval;
20
21 enum class PPC_hwcap_bit : uint64_t /* NOLINT(performance-enum-size) */ {
22 VSX_bit = (1 << 7),
23 };
24
25 enum class PPC_hwcap2_bit : uint64_t /* NOLINT(performance-enum-size) */ {
26 ARCH_2_07_bit = (1U << 31),
27 CRYPTO_bit = (1 << 25),
28 DARN_bit = (1 << 21),
29 };
30
31 // POWER7 has VSX but lacks the ISA 2.07 vector instructions we rely on
32 if((hwcap2 & static_cast<uint64_t>(PPC_hwcap2_bit::ARCH_2_07_bit)) != 0) {
33 feat |= if_set(hwcap, PPC_hwcap_bit::VSX_bit, CPUFeature::Bit::VSX, allowed);
34 }
35
36 if(is_set(feat, CPUFeature::Bit::VSX)) {
37 feat |= if_set(hwcap2, PPC_hwcap2_bit::CRYPTO_bit, CPUFeature::Bit::POWER_CRYPTO, allowed);
38 }
39
40 // DARN is a scalar instruction (ISA 3.0) and does not depend on VSX
41 feat |= if_set(hwcap2, PPC_hwcap2_bit::DARN_bit, CPUFeature::Bit::DARN, allowed);
42
43 return feat;
44 }
45
46#if defined(BOTAN_USE_GCC_INLINE_ASM)
47
48 // NOLINTBEGIN(*-no-assembler)
49
50 /*
51 * No auxval available, fall back on probing instructions. A probe is only
52 * executed if the associated feature is allowed, so that a cleared feature
53 * is never executed even during detection.
54 */
55
56 if(is_set(allowed, CPUFeature::Bit::VSX)) {
57 // vaddudm is new in ISA 2.07 (POWER8)
58 auto vsx_probe = []() noexcept -> int {
59 asm volatile("vaddudm 0, 0, 0" ::: "v0");
60 return 1;
61 };
62
63 if(OS::run_cpu_instruction_probe(vsx_probe) == 1) {
64 feat |= CPUFeature::Bit::VSX;
65 }
66 }
67
68 if(is_set(feat, CPUFeature::Bit::VSX) && is_set(allowed, CPUFeature::Bit::POWER_CRYPTO)) {
69 auto vcipher_probe = []() noexcept -> int {
70 asm volatile("vcipher 0, 0, 0" ::: "v0");
71 return 1;
72 };
73
74 if(OS::run_cpu_instruction_probe(vcipher_probe) == 1) {
75 feat |= CPUFeature::Bit::POWER_CRYPTO;
76 }
77 }
78
79 // DARN is a scalar instruction (ISA 3.0) and does not depend on VSX
80 if(is_set(allowed, CPUFeature::Bit::DARN)) {
81 auto darn_probe = []() noexcept -> int {
82 /*
83 * DARN returns all ones to indicate a (transient) error. PowerISA 3.0
84 * recommends retrying ten times before giving up, as is done in
85 * Processor_RNG. Here if DARN never produces a value, it is treated as
86 * if the instruction was unavailable.
87 */
88 for(size_t i = 0; i != 10; ++i) {
89 uint64_t output = 0;
90 asm volatile("darn %0, 1" : "=r"(output));
91 if((~output) != 0) {
92 return 1;
93 }
94 }
95 return 0;
96 };
97
98 if(OS::run_cpu_instruction_probe(darn_probe) == 1) {
99 feat |= CPUFeature::Bit::DARN;
100 }
101 }
102
103 // NOLINTEND(*-no-assembler)
104
105#endif
106
107 return feat;
108}
109
110} // namespace Botan
static uint32_t if_set(uint64_t cpuid, T flag, CPUID::Feature bit, uint32_t allowed)
Definition cpuid.h:116
std::optional< std::pair< unsigned long, unsigned long > > get_auxval_hwcap()
Definition os_utils.cpp:137
int BOTAN_TEST_API run_cpu_instruction_probe(const std::function< int()> &probe_fn)
Definition os_utils.cpp:814