Botan 3.4.0
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 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
12#if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
13
14namespace Botan {
15
16uint32_t CPUID::CPUID_Data::detect_cpu_features() {
17 uint32_t detected_features = 0;
18
19 #if(defined(BOTAN_TARGET_OS_HAS_GETAUXVAL) || defined(BOTAN_TARGET_HAS_ELF_AUX_INFO)) && \
20 defined(BOTAN_TARGET_ARCH_IS_PPC64)
21
22 enum PPC_hwcap_bit {
23 ALTIVEC_bit = (1 << 28),
24 CRYPTO_bit = (1 << 25),
25 DARN_bit = (1 << 21),
26
27 ARCH_hwcap_altivec = 16, // AT_HWCAP
28 ARCH_hwcap_crypto = 26, // AT_HWCAP2
29 };
30
31 const unsigned long hwcap_altivec = OS::get_auxval(PPC_hwcap_bit::ARCH_hwcap_altivec);
32 if(hwcap_altivec & PPC_hwcap_bit::ALTIVEC_bit) {
33 detected_features |= CPUID::CPUID_ALTIVEC_BIT;
34
35 const unsigned long hwcap_crypto = OS::get_auxval(PPC_hwcap_bit::ARCH_hwcap_crypto);
36 if(hwcap_crypto & PPC_hwcap_bit::CRYPTO_bit)
37 detected_features |= CPUID::CPUID_POWER_CRYPTO_BIT;
38 if(hwcap_crypto & PPC_hwcap_bit::DARN_bit)
39 detected_features |= CPUID::CPUID_DARN_BIT;
40 }
41
42 #else
43
44 auto vmx_probe = []() noexcept -> int {
45 asm("vor 0, 0, 0");
46 return 1;
47 };
48
49 if(OS::run_cpu_instruction_probe(vmx_probe) == 1) {
50 detected_features |= CPUID::CPUID_ALTIVEC_BIT;
51
52 #if defined(BOTAN_TARGET_ARCH_IS_PPC64)
53 auto vcipher_probe = []() noexcept -> int {
54 asm("vcipher 0, 0, 0");
55 return 1;
56 };
57
58 if(OS::run_cpu_instruction_probe(vcipher_probe) == 1)
59 detected_features |= CPUID::CPUID_POWER_CRYPTO_BIT;
60
61 auto darn_probe = []() noexcept -> int {
62 uint64_t output = 0;
63 asm volatile("darn %0, 1" : "=r"(output));
64 return (~output) != 0;
65 };
66
67 if(OS::run_cpu_instruction_probe(darn_probe) == 1)
68 detected_features |= CPUID::CPUID_DARN_BIT;
69 #endif
70 }
71
72 #endif
73
74 return detected_features;
75}
76
77} // namespace Botan
78
79#endif
int BOTAN_TEST_API run_cpu_instruction_probe(const std::function< int()> &probe_fn)
Definition os_utils.cpp:683
unsigned long get_auxval(unsigned long id)
Definition os_utils.cpp:128