Debian Codegraph @

crt/aws-c-common/source/arch/arm/auxv/cpuid.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <aws/common/cpuid.h>
17 #include <stdlib.h>
18
19 #if defined(__linux__) || defined(__FreeBSD__)
20 # include <sys/auxv.h>
21
22 static unsigned long s_hwcap[2];
23 static bool s_hwcap_cached;
24
25 struct cap_bits {
26 unsigned long cap;
27 unsigned long bit;
28 };
29
30 # if (defined(__aarch64__))
31 struct cap_bits s_check_cap[AWS_CPU_FEATURE_COUNT] = {
32 [AWS_CPU_FEATURE_ARM_CRC] = {0, 1 << 7 /* HWCAP_CRC32 */},
33 [AWS_CPU_FEATURE_ARM_PMULL] = {0, 1 << 4 /* HWCAP_PMULL */},
34 [AWS_CPU_FEATURE_ARM_CRYPTO] = {0, 1 << 3 /* HWCAP_AES */},
35 };
36 # else
37 struct cap_bits s_check_cap[AWS_CPU_FEATURE_COUNT] = {
38 [AWS_CPU_FEATURE_ARM_CRC] = {1, 1 << 4 /* HWCAP_CRC */},
39 };
40 # endif
41
42 # if (defined(__linux__))
43 static void s_cache_hwcap(void) {
44 s_hwcap[0] = getauxval(AT_HWCAP);
45 s_hwcap[1] = getauxval(AT_HWCAP2);
46 s_hwcap_cached = true;
47 }
48 # elif (defined(__FreeBSD__))
49 static void s_cache_hwcap(void) {
50 int ret;
51
52 ret = elf_aux_info(AT_HWCAP, &s_hwcap[0], sizeof(unsigned long));
53 if (ret)
54 s_hwcap[0] = 0;
55
56 ret = elf_aux_info(AT_HWCAP2, &s_hwcap[1], sizeof(unsigned long));
57 if (ret)
58 s_hwcap[1] = 0;
59 s_hwcap_cached = true;
60 }
61 # else
62 # error "Unknown method"
63 # endif
64
65 bool aws_cpu_has_feature(enum aws_cpu_feature_name feature_name) {
66
67 if (!s_hwcap_cached)
68 s_cache_hwcap();
69
70 switch (feature_name) {
71 case AWS_CPU_FEATURE_ARM_CRC:
72 # if (defined(__aarch64__))
73 case AWS_CPU_FEATURE_ARM_PMULL:
74 case AWS_CPU_FEATURE_ARM_CRYPTO:
75 # endif // (defined(__aarch64__))
76 return s_hwcap[s_check_cap[feature_name].cap] & s_check_cap[feature_name].bit;
77 default:
78 return false;
79 }
80 }
81
82 #else /* defined(__linux__) || defined(__FreeBSD__) */
83 bool aws_cpu_has_feature(enum aws_cpu_feature_name feature_name) {
84 return false;
85 }
86 #endif /* defined(__linux__) || defined(__FreeBSD__) */