1 // Copyright 2017 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "cpuinfo_aarch64.h"
16 
17 #include <assert.h>
18 #include <ctype.h>
19 
20 #include "internal/filesystem.h"
21 #include "internal/hwcaps.h"
22 #include "internal/stack_line_reader.h"
23 #include "internal/string_view.h"
24 
25 // Generation of feature's getters/setters functions and kGetters, kSetters,
26 // kCpuInfoFlags and kHardwareCapabilities global tables.
27 #define DEFINE_TABLE_FEATURES                                                 \
28   FEATURE(AARCH64_FP, fp, "fp", AARCH64_HWCAP_FP, 0)                          \
29   FEATURE(AARCH64_ASIMD, asimd, "asimd", AARCH64_HWCAP_ASIMD, 0)              \
30   FEATURE(AARCH64_EVTSTRM, evtstrm, "evtstrm", AARCH64_HWCAP_EVTSTRM, 0)      \
31   FEATURE(AARCH64_AES, aes, "aes", AARCH64_HWCAP_AES, 0)                      \
32   FEATURE(AARCH64_PMULL, pmull, "pmull", AARCH64_HWCAP_PMULL, 0)              \
33   FEATURE(AARCH64_SHA1, sha1, "sha1", AARCH64_HWCAP_SHA1, 0)                  \
34   FEATURE(AARCH64_SHA2, sha2, "sha2", AARCH64_HWCAP_SHA2, 0)                  \
35   FEATURE(AARCH64_CRC32, crc32, "crc32", AARCH64_HWCAP_CRC32, 0)              \
36   FEATURE(AARCH64_ATOMICS, atomics, "atomics", AARCH64_HWCAP_ATOMICS, 0)      \
37   FEATURE(AARCH64_FPHP, fphp, "fphp", AARCH64_HWCAP_FPHP, 0)                  \
38   FEATURE(AARCH64_ASIMDHP, asimdhp, "asimdhp", AARCH64_HWCAP_ASIMDHP, 0)      \
39   FEATURE(AARCH64_CPUID, cpuid, "cpuid", AARCH64_HWCAP_CPUID, 0)              \
40   FEATURE(AARCH64_ASIMDRDM, asimdrdm, "asimdrdm", AARCH64_HWCAP_ASIMDRDM, 0)  \
41   FEATURE(AARCH64_JSCVT, jscvt, "jscvt", AARCH64_HWCAP_JSCVT, 0)              \
42   FEATURE(AARCH64_FCMA, fcma, "fcma", AARCH64_HWCAP_FCMA, 0)                  \
43   FEATURE(AARCH64_LRCPC, lrcpc, "lrcpc", AARCH64_HWCAP_LRCPC, 0)              \
44   FEATURE(AARCH64_DCPOP, dcpop, "dcpop", AARCH64_HWCAP_DCPOP, 0)              \
45   FEATURE(AARCH64_SHA3, sha3, "sha3", AARCH64_HWCAP_SHA3, 0)                  \
46   FEATURE(AARCH64_SM3, sm3, "sm3", AARCH64_HWCAP_SM3, 0)                      \
47   FEATURE(AARCH64_SM4, sm4, "sm4", AARCH64_HWCAP_SM4, 0)                      \
48   FEATURE(AARCH64_ASIMDDP, asimddp, "asimddp", AARCH64_HWCAP_ASIMDDP, 0)      \
49   FEATURE(AARCH64_SHA512, sha512, "sha512", AARCH64_HWCAP_SHA512, 0)          \
50   FEATURE(AARCH64_SVE, sve, "sve", AARCH64_HWCAP_SVE, 0)                      \
51   FEATURE(AARCH64_ASIMDFHM, asimdfhm, "asimdfhm", AARCH64_HWCAP_ASIMDFHM, 0)  \
52   FEATURE(AARCH64_DIT, dit, "dit", AARCH64_HWCAP_DIT, 0)                      \
53   FEATURE(AARCH64_USCAT, uscat, "uscat", AARCH64_HWCAP_USCAT, 0)              \
54   FEATURE(AARCH64_ILRCPC, ilrcpc, "ilrcpc", AARCH64_HWCAP_ILRCPC, 0)          \
55   FEATURE(AARCH64_FLAGM, flagm, "flagm", AARCH64_HWCAP_FLAGM, 0)              \
56   FEATURE(AARCH64_SSBS, ssbs, "ssbs", AARCH64_HWCAP_SSBS, 0)                  \
57   FEATURE(AARCH64_SB, sb, "sb", AARCH64_HWCAP_SB, 0)                          \
58   FEATURE(AARCH64_PACA, paca, "paca", AARCH64_HWCAP_PACA, 0)                  \
59   FEATURE(AARCH64_PACG, pacg, "pacg", AARCH64_HWCAP_PACG, 0)                  \
60   FEATURE(AARCH64_DCPODP, dcpodp, "dcpodp", 0, AARCH64_HWCAP2_DCPODP)         \
61   FEATURE(AARCH64_SVE2, sve2, "sve2", 0, AARCH64_HWCAP2_SVE2)                 \
62   FEATURE(AARCH64_SVEAES, sveaes, "sveaes", 0, AARCH64_HWCAP2_SVEAES)         \
63   FEATURE(AARCH64_SVEPMULL, svepmull, "svepmull", 0, AARCH64_HWCAP2_SVEPMULL) \
64   FEATURE(AARCH64_SVEBITPERM, svebitperm, "svebitperm", 0,                    \
65           AARCH64_HWCAP2_SVEBITPERM)                                          \
66   FEATURE(AARCH64_SVESHA3, svesha3, "svesha3", 0, AARCH64_HWCAP2_SVESHA3)     \
67   FEATURE(AARCH64_SVESM4, svesm4, "svesm4", 0, AARCH64_HWCAP2_SVESM4)         \
68   FEATURE(AARCH64_FLAGM2, flagm2, "flagm2", 0, AARCH64_HWCAP2_FLAGM2)         \
69   FEATURE(AARCH64_FRINT, frint, "frint", 0, AARCH64_HWCAP2_FRINT)             \
70   FEATURE(AARCH64_SVEI8MM, svei8mm, "svei8mm", 0, AARCH64_HWCAP2_SVEI8MM)     \
71   FEATURE(AARCH64_SVEF32MM, svef32mm, "svef32mm", 0, AARCH64_HWCAP2_SVEF32MM) \
72   FEATURE(AARCH64_SVEF64MM, svef64mm, "svef64mm", 0, AARCH64_HWCAP2_SVEF64MM) \
73   FEATURE(AARCH64_SVEBF16, svebf16, "svebf16", 0, AARCH64_HWCAP2_SVEBF16)     \
74   FEATURE(AARCH64_I8MM, i8mm, "i8mm", 0, AARCH64_HWCAP2_I8MM)                 \
75   FEATURE(AARCH64_BF16, bf16, "bf16", 0, AARCH64_HWCAP2_BF16)                 \
76   FEATURE(AARCH64_DGH, dgh, "dgh", 0, AARCH64_HWCAP2_DGH)                     \
77   FEATURE(AARCH64_RNG, rng, "rng", 0, AARCH64_HWCAP2_RNG)                     \
78   FEATURE(AARCH64_BTI, bti, "bti", 0, AARCH64_HWCAP2_BTI)
79 #define DEFINE_TABLE_FEATURE_TYPE Aarch64Features
80 #include "define_tables.h"
81 
HandleAarch64Line(const LineResult result,Aarch64Info * const info)82 static bool HandleAarch64Line(const LineResult result,
83                               Aarch64Info* const info) {
84   StringView line = result.line;
85   StringView key, value;
86   if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
87     if (CpuFeatures_StringView_IsEquals(key, str("Features"))) {
88       for (size_t i = 0; i < AARCH64_LAST_; ++i) {
89         kSetters[i](&info->features,
90                     CpuFeatures_StringView_HasWord(value, kCpuInfoFlags[i]));
91       }
92     } else if (CpuFeatures_StringView_IsEquals(key, str("CPU implementer"))) {
93       info->implementer = CpuFeatures_StringView_ParsePositiveNumber(value);
94     } else if (CpuFeatures_StringView_IsEquals(key, str("CPU variant"))) {
95       info->variant = CpuFeatures_StringView_ParsePositiveNumber(value);
96     } else if (CpuFeatures_StringView_IsEquals(key, str("CPU part"))) {
97       info->part = CpuFeatures_StringView_ParsePositiveNumber(value);
98     } else if (CpuFeatures_StringView_IsEquals(key, str("CPU revision"))) {
99       info->revision = CpuFeatures_StringView_ParsePositiveNumber(value);
100     }
101   }
102   return !result.eof;
103 }
104 
FillProcCpuInfoData(Aarch64Info * const info)105 static void FillProcCpuInfoData(Aarch64Info* const info) {
106   const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
107   if (fd >= 0) {
108     StackLineReader reader;
109     StackLineReader_Initialize(&reader, fd);
110     for (;;) {
111       if (!HandleAarch64Line(StackLineReader_NextLine(&reader), info)) {
112         break;
113       }
114     }
115     CpuFeatures_CloseFile(fd);
116   }
117 }
118 
119 static const Aarch64Info kEmptyAarch64Info;
120 
GetAarch64Info(void)121 Aarch64Info GetAarch64Info(void) {
122   // capabilities are fetched from both getauxval and /proc/cpuinfo so we can
123   // have some information if the executable is sandboxed (aka no access to
124   // /proc/cpuinfo).
125   Aarch64Info info = kEmptyAarch64Info;
126 
127   FillProcCpuInfoData(&info);
128   const HardwareCapabilities hwcaps = CpuFeatures_GetHardwareCapabilities();
129   for (size_t i = 0; i < AARCH64_LAST_; ++i) {
130     if (CpuFeatures_IsHwCapsSet(kHardwareCapabilities[i], hwcaps)) {
131       kSetters[i](&info.features, true);
132     }
133   }
134 
135   return info;
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 // Introspection functions
140 
GetAarch64FeaturesEnumValue(const Aarch64Features * features,Aarch64FeaturesEnum value)141 int GetAarch64FeaturesEnumValue(const Aarch64Features* features,
142                                 Aarch64FeaturesEnum value) {
143   if (value >= AARCH64_LAST_) return false;
144   return kGetters[value](features);
145 }
146 
GetAarch64FeaturesEnumName(Aarch64FeaturesEnum value)147 const char* GetAarch64FeaturesEnumName(Aarch64FeaturesEnum value) {
148   if (value >= AARCH64_LAST_) return "unknown feature";
149   return kCpuInfoFlags[value];
150 }
151