Botan 3.14.1
Crypto and TLS for C&
cpuid_riscv64.cpp
Go to the documentation of this file.
1/*
2* (C) 2025 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/cpuid.h>
8
9#include <botan/assert.h>
10#include <botan/internal/os_utils.h>
11#include <botan/internal/target_info.h>
12#include <concepts>
13
14#if defined(BOTAN_TARGET_OS_IS_LINUX) && __has_include(<sys/hwprobe.h>)
15 #include <asm/hwprobe.h>
16 #include <sys/hwprobe.h>
17
18 #define BOTAN_TARGET_HAS_RISCV_HWPROBE
19#endif
20
21namespace Botan {
22
23namespace {
24
25template <std::convertible_to<uint64_t>... Bs>
26 requires(sizeof...(Bs) > 0)
27constexpr uint64_t bitflag(Bs... bs) {
28 return ((uint64_t(1) << bs) | ...);
29}
30
31} // namespace
32
33uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) {
34 uint32_t feat = 0;
35
36#if defined(BOTAN_TARGET_HAS_RISCV_HWPROBE)
37 /*
38 * For scalar operations we require additionally
39 * Zba (bit 3), Zbb (bit 4), Zkt (bit 16)
40 *
41 * For vector operations we require
42 * V (bit 2), Vbb (bit 17), VZkt (bit 26),
43 */
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),
49
50 Vector = bitflag(2, 17, 26),
51 Vector_Aes = bitflag(2, 17, 26, 21),
52 // Zvknha (bit 22) provides SHA-256, Zvknhb (bit 23) provides SHA-256 and SHA-512
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),
58 };
59
60 struct riscv_hwprobe p;
61 p.key = RISCV_HWPROBE_KEY_IMA_EXT_0;
62
63 if(__riscv_hwprobe(&p, 1, 0, nullptr, 0) == 0) {
64 const uint64_t riscv_features = p.value;
65
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);
70
71 feat |= if_set(riscv_features, RISCV_HWPROBE_bit::Vector, CPUFeature::Bit::VECTOR, allowed);
72
73 if(is_set(feat, CPUFeature::Bit::VECTOR)) {
74 feat |= if_set(riscv_features, RISCV_HWPROBE_bit::Vector_Aes, CPUFeature::Bit::VECTOR_AES, allowed);
75 feat |=
76 if_set(riscv_features, RISCV_HWPROBE_bit::Vector_Sha256_Zvknha, CPUFeature::Bit::VECTOR_SHA256, allowed);
77 feat |=
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);
81 }
82 }
83#else
84 BOTAN_UNUSED(allowed);
85#endif
86
87 return feat;
88}
89
90} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
static uint32_t if_set(uint64_t cpuid, T flag, CPUID::Feature bit, uint32_t allowed)
Definition cpuid.h:116