blob: 0f216bf93adf35888c505ed1bf244a3928c220b0 [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_arm.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/bit_utils.h"
21#include "internal/filesystem.h"
22#include "internal/hwcaps.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010023#include "internal/stack_line_reader.h"
24#include "internal/string_view.h"
25
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020026// Generation of feature's getters/setters functions and kGetters, kSetters,
27// kCpuInfoFlags and kHardwareCapabilities global tables.
Guillaume Chateletcdab59a2020-10-13 13:05:04 +020028#define DEFINE_TABLE_FEATURES \
29 FEATURE(ARM_SWP, swp, "swp", ARM_HWCAP_SWP, 0) \
30 FEATURE(ARM_HALF, half, "half", ARM_HWCAP_HALF, 0) \
31 FEATURE(ARM_THUMB, thumb, "thumb", ARM_HWCAP_THUMB, 0) \
32 FEATURE(ARM_26BIT, _26bit, "26bit", ARM_HWCAP_26BIT, 0) \
33 FEATURE(ARM_FASTMULT, fastmult, "fastmult", ARM_HWCAP_FAST_MULT, 0) \
34 FEATURE(ARM_FPA, fpa, "fpa", ARM_HWCAP_FPA, 0) \
35 FEATURE(ARM_VFP, vfp, "vfp", ARM_HWCAP_VFP, 0) \
36 FEATURE(ARM_EDSP, edsp, "edsp", ARM_HWCAP_EDSP, 0) \
37 FEATURE(ARM_JAVA, java, "java", ARM_HWCAP_JAVA, 0) \
38 FEATURE(ARM_IWMMXT, iwmmxt, "iwmmxt", ARM_HWCAP_IWMMXT, 0) \
39 FEATURE(ARM_CRUNCH, crunch, "crunch", ARM_HWCAP_CRUNCH, 0) \
40 FEATURE(ARM_THUMBEE, thumbee, "thumbee", ARM_HWCAP_THUMBEE, 0) \
41 FEATURE(ARM_NEON, neon, "neon", ARM_HWCAP_NEON, 0) \
42 FEATURE(ARM_VFPV3, vfpv3, "vfpv3", ARM_HWCAP_VFPV3, 0) \
43 FEATURE(ARM_VFPV3D16, vfpv3d16, "vfpv3d16", ARM_HWCAP_VFPV3D16, 0) \
44 FEATURE(ARM_TLS, tls, "tls", ARM_HWCAP_TLS, 0) \
45 FEATURE(ARM_VFPV4, vfpv4, "vfpv4", ARM_HWCAP_VFPV4, 0) \
46 FEATURE(ARM_IDIVA, idiva, "idiva", ARM_HWCAP_IDIVA, 0) \
47 FEATURE(ARM_IDIVT, idivt, "idivt", ARM_HWCAP_IDIVT, 0) \
48 FEATURE(ARM_VFPD32, vfpd32, "vfpd32", ARM_HWCAP_VFPD32, 0) \
49 FEATURE(ARM_LPAE, lpae, "lpae", ARM_HWCAP_LPAE, 0) \
50 FEATURE(ARM_EVTSTRM, evtstrm, "evtstrm", ARM_HWCAP_EVTSTRM, 0) \
51 FEATURE(ARM_AES, aes, "aes", 0, ARM_HWCAP2_AES) \
52 FEATURE(ARM_PMULL, pmull, "pmull", 0, ARM_HWCAP2_PMULL) \
53 FEATURE(ARM_SHA1, sha1, "sha1", 0, ARM_HWCAP2_SHA1) \
54 FEATURE(ARM_SHA2, sha2, "sha2", 0, ARM_HWCAP2_SHA2) \
55 FEATURE(ARM_CRC32, crc32, "crc32", 0, ARM_HWCAP2_CRC32)
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020056#define DEFINE_TABLE_FEATURE_TYPE ArmFeatures
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020057#include "define_tables.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010058
59typedef struct {
60 bool processor_reports_armv6;
61 bool hardware_reports_goldfish;
62} ProcCpuInfoData;
63
64static int IndexOfNonDigit(StringView str) {
65 size_t index = 0;
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020066 while (str.size && isdigit(CpuFeatures_StringView_Front(str))) {
67 str = CpuFeatures_StringView_PopFront(str, 1);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010068 ++index;
69 }
70 return index;
71}
72
73static bool HandleArmLine(const LineResult result, ArmInfo* const info,
74 ProcCpuInfoData* const proc_info) {
75 StringView line = result.line;
76 StringView key, value;
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020077 if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
78 if (CpuFeatures_StringView_IsEquals(key, str("Features"))) {
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020079 for (size_t i = 0; i < ARM_LAST_; ++i) {
80 kSetters[i](&info->features,
81 CpuFeatures_StringView_HasWord(value, kCpuInfoFlags[i]));
82 }
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020083 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU implementer"))) {
84 info->implementer = CpuFeatures_StringView_ParsePositiveNumber(value);
85 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU variant"))) {
86 info->variant = CpuFeatures_StringView_ParsePositiveNumber(value);
87 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU part"))) {
88 info->part = CpuFeatures_StringView_ParsePositiveNumber(value);
89 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU revision"))) {
90 info->revision = CpuFeatures_StringView_ParsePositiveNumber(value);
91 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU architecture"))) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +010092 // CPU architecture is a number that may be followed by letters. e.g.
93 // "6TEJ", "7".
Arvid Gerstmannd9689912018-05-04 09:32:17 +020094 const StringView digits =
95 CpuFeatures_StringView_KeepFront(value, IndexOfNonDigit(value));
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020096 info->architecture = CpuFeatures_StringView_ParsePositiveNumber(digits);
Guillaume Chatelet22a53622020-09-23 11:52:20 +020097 } else if (CpuFeatures_StringView_IsEquals(key, str("Processor")) ||
98 CpuFeatures_StringView_IsEquals(key, str("model name"))) {
Dr.-Ing. Patrick Siegl18342782019-06-25 17:58:31 +020099 // Android reports this in a non-Linux standard "Processor" but sometimes
100 // also in "model name", Linux reports it only in "model name"
101 // see RaspberryPiZero (Linux) vs InvalidArmv7 (Android) test-cases
Arvid Gerstmannd9689912018-05-04 09:32:17 +0200102 proc_info->processor_reports_armv6 =
103 CpuFeatures_StringView_IndexOf(value, str("(v6l)")) >= 0;
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +0200104 } else if (CpuFeatures_StringView_IsEquals(key, str("Hardware"))) {
Arvid Gerstmannd9689912018-05-04 09:32:17 +0200105 proc_info->hardware_reports_goldfish =
106 CpuFeatures_StringView_IsEquals(value, str("Goldfish"));
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100107 }
108 }
109 return !result.eof;
110}
111
Guillaume Chatelet918553a2019-01-17 15:28:04 +0100112uint32_t GetArmCpuId(const ArmInfo* const info) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100113 return (ExtractBitRange(info->implementer, 7, 0) << 24) |
114 (ExtractBitRange(info->variant, 3, 0) << 20) |
115 (ExtractBitRange(info->part, 11, 0) << 4) |
116 (ExtractBitRange(info->revision, 3, 0) << 0);
117}
118
119static void FixErrors(ArmInfo* const info,
120 ProcCpuInfoData* const proc_cpu_info_data) {
121 // Fixing Samsung kernel reporting invalid cpu architecture.
122 // http://code.google.com/p/android/issues/detail?id=10812
123 if (proc_cpu_info_data->processor_reports_armv6 && info->architecture >= 7) {
124 info->architecture = 6;
125 }
126
127 // Handle kernel configuration bugs that prevent the correct reporting of CPU
128 // features.
Guillaume Chatelet918553a2019-01-17 15:28:04 +0100129 switch (GetArmCpuId(info)) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100130 case 0x4100C080:
131 // Special case: The emulator-specific Android 4.2 kernel fails to report
132 // support for the 32-bit ARM IDIV instruction. Technically, this is a
133 // feature of the virtual CPU implemented by the emulator. Note that it
134 // could also support Thumb IDIV in the future, and this will have to be
135 // slightly updated.
136 if (info->architecture >= 7 &&
137 proc_cpu_info_data->hardware_reports_goldfish) {
138 info->features.idiva = true;
139 }
140 break;
141 case 0x511004D0:
142 // https://crbug.com/341598.
143 info->features.neon = false;
144 break;
145 case 0x510006F2:
146 case 0x510006F3:
147 // The Nexus 4 (Qualcomm Krait) kernel configuration forgets to report
148 // IDIV support.
149 info->features.idiva = true;
150 info->features.idivt = true;
151 break;
152 }
153
154 // Propagate cpu features.
155 if (info->features.vfpv4) info->features.vfpv3 = true;
156 if (info->features.neon) info->features.vfpv3 = true;
157 if (info->features.vfpv3) info->features.vfp = true;
158}
159
160static void FillProcCpuInfoData(ArmInfo* const info,
161 ProcCpuInfoData* proc_cpu_info_data) {
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +0200162 const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100163 if (fd >= 0) {
164 StackLineReader reader;
165 StackLineReader_Initialize(&reader, fd);
166 for (;;) {
167 if (!HandleArmLine(StackLineReader_NextLine(&reader), info,
168 proc_cpu_info_data)) {
169 break;
170 }
171 }
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +0200172 CpuFeatures_CloseFile(fd);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100173 }
174}
175
176static const ArmInfo kEmptyArmInfo;
177
178static const ProcCpuInfoData kEmptyProcCpuInfoData;
179
180ArmInfo GetArmInfo(void) {
181 // capabilities are fetched from both getauxval and /proc/cpuinfo so we can
182 // have some information if the executable is sandboxed (aka no access to
183 // /proc/cpuinfo).
184 ArmInfo info = kEmptyArmInfo;
185 ProcCpuInfoData proc_cpu_info_data = kEmptyProcCpuInfoData;
186
187 FillProcCpuInfoData(&info, &proc_cpu_info_data);
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +0200188 const HardwareCapabilities hwcaps = CpuFeatures_GetHardwareCapabilities();
189 for (size_t i = 0; i < ARM_LAST_; ++i) {
190 if (CpuFeatures_IsHwCapsSet(kHardwareCapabilities[i], hwcaps)) {
191 kSetters[i](&info.features, true);
192 }
193 }
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100194
195 FixErrors(&info, &proc_cpu_info_data);
196
197 return info;
198}
199
200////////////////////////////////////////////////////////////////////////////////
201// Introspection functions
202
203int GetArmFeaturesEnumValue(const ArmFeatures* features,
204 ArmFeaturesEnum value) {
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +0200205 if (value >= ARM_LAST_) return false;
206 return kGetters[value](features);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100207}
208
209const char* GetArmFeaturesEnumName(ArmFeaturesEnum value) {
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +0200210 if (value >= ARM_LAST_) return "unknown feature";
211 return kCpuInfoFlags[value];
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100212}