blob: 0a5271890a4a1ccde97d7c576d0d58066158ebfb [file] [log] [blame]
Guillaume Chatelet3cc8f312020-10-12 08:55:20 +00001// Copyright 2017 Google LLC
Guillaume Chatelet439d3712018-02-01 10:03:09 +01002//
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
Guillaume Chatelet22a53622020-09-23 11:52:20 +020017#include <assert.h>
18#include <ctype.h>
19
Guillaume Chatelet439d3712018-02-01 10:03:09 +010020#include "internal/filesystem.h"
21#include "internal/hwcaps.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010022#include "internal/stack_line_reader.h"
23#include "internal/string_view.h"
24
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020025// Generation of feature's getters/setters functions and kGetters, kSetters,
26// kCpuInfoFlags and kHardwareCapabilities global tables.
Guillaume Chateletcdab59a2020-10-13 13:05:04 +020027#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)
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020079#define DEFINE_TABLE_FEATURE_TYPE Aarch64Features
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020080#include "define_tables.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010081
82static bool HandleAarch64Line(const LineResult result,
83 Aarch64Info* const info) {
84 StringView line = result.line;
85 StringView key, value;
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020086 if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
87 if (CpuFeatures_StringView_IsEquals(key, str("Features"))) {
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020088 for (size_t i = 0; i < AARCH64_LAST_; ++i) {
89 kSetters[i](&info->features,
90 CpuFeatures_StringView_HasWord(value, kCpuInfoFlags[i]));
91 }
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020092 } 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);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100100 }
101 }
102 return !result.eof;
103}
104
105static void FillProcCpuInfoData(Aarch64Info* const info) {
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +0200106 const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100107 if (fd >= 0) {
108 StackLineReader reader;
109 StackLineReader_Initialize(&reader, fd);
110 for (;;) {
111 if (!HandleAarch64Line(StackLineReader_NextLine(&reader), info)) {
112 break;
113 }
114 }
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +0200115 CpuFeatures_CloseFile(fd);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100116 }
117}
118
119static const Aarch64Info kEmptyAarch64Info;
120
121Aarch64Info 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);
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +0200128 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 }
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100134
135 return info;
136}
137
138////////////////////////////////////////////////////////////////////////////////
139// Introspection functions
140
141int GetAarch64FeaturesEnumValue(const Aarch64Features* features,
142 Aarch64FeaturesEnum value) {
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +0200143 if (value >= AARCH64_LAST_) return false;
144 return kGetters[value](features);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100145}
146
147const char* GetAarch64FeaturesEnumName(Aarch64FeaturesEnum value) {
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +0200148 if (value >= AARCH64_LAST_) return "unknown feature";
149 return kCpuInfoFlags[value];
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100150}