7#include <botan/internal/cpuid.h>
9#include <botan/assert.h>
10#include <botan/internal/os_utils.h>
11#include <botan/internal/target_info.h>
14#if defined(BOTAN_TARGET_OS_IS_LINUX) && __has_include(<sys/hwprobe.h>)
15 #include <asm/hwprobe.h>
16 #include <sys/hwprobe.h>
18 #define BOTAN_TARGET_HAS_RISCV_HWPROBE
25template <std::convertible_to<u
int64_t>... Bs>
26 requires(
sizeof...(Bs) > 0)
27constexpr uint64_t bitflag(Bs... bs) {
28 return ((uint64_t(1) << bs) | ...);
33uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) {
36#if defined(BOTAN_TARGET_HAS_RISCV_HWPROBE)
44 enum class RISCV_HWPROBE_bit : uint64_t {
45 Scalar_Aes = bitflag(3, 4, 16, 11, 12),
46 Scalar_Sha256 = bitflag(3, 4, 16, 13),
47 Scalar_SM4 = bitflag(3, 4, 16, 14),
48 Scalar_SM3 = bitflag(3, 4, 16, 15),
50 Vector = bitflag(2, 17, 26),
51 Vector_Aes = bitflag(2, 17, 26, 21),
53 Vector_Sha256_Zvknha = bitflag(2, 17, 26, 22),
54 Vector_Sha256_Zvknhb = bitflag(2, 17, 26, 23),
55 Vector_SM4 = bitflag(2, 17, 26, 24),
56 Vector_SM3 = bitflag(2, 17, 26, 25),
57 Vector_GCM = bitflag(2, 17, 26, 20),
60 struct riscv_hwprobe p;
61 p.key = RISCV_HWPROBE_KEY_IMA_EXT_0;
63 if(__riscv_hwprobe(&p, 1, 0,
nullptr, 0) == 0) {
64 const uint64_t riscv_features = p.value;
66 feat |=
if_set(riscv_features, RISCV_HWPROBE_bit::Scalar_Aes, CPUFeature::Bit::SCALAR_AES, allowed);
67 feat |=
if_set(riscv_features, RISCV_HWPROBE_bit::Scalar_Sha256, CPUFeature::Bit::SCALAR_SHA256, allowed);
68 feat |=
if_set(riscv_features, RISCV_HWPROBE_bit::Scalar_SM3, CPUFeature::Bit::SCALAR_SM3, allowed);
69 feat |=
if_set(riscv_features, RISCV_HWPROBE_bit::Scalar_SM4, CPUFeature::Bit::SCALAR_SM4, allowed);
71 feat |=
if_set(riscv_features, RISCV_HWPROBE_bit::Vector, CPUFeature::Bit::VECTOR, allowed);
73 if(is_set(feat, CPUFeature::Bit::VECTOR)) {
74 feat |=
if_set(riscv_features, RISCV_HWPROBE_bit::Vector_Aes, CPUFeature::Bit::VECTOR_AES, allowed);
76 if_set(riscv_features, RISCV_HWPROBE_bit::Vector_Sha256_Zvknha, CPUFeature::Bit::VECTOR_SHA256, allowed);
78 if_set(riscv_features, RISCV_HWPROBE_bit::Vector_Sha256_Zvknhb, CPUFeature::Bit::VECTOR_SHA256, allowed);
79 feat |=
if_set(riscv_features, RISCV_HWPROBE_bit::Vector_SM3, CPUFeature::Bit::VECTOR_SM3, allowed);
80 feat |=
if_set(riscv_features, RISCV_HWPROBE_bit::Vector_SM4, CPUFeature::Bit::VECTOR_SM4, allowed);
static uint32_t if_set(uint64_t cpuid, T flag, CPUID::Feature bit, uint32_t allowed)