blob: add38ce1e983e94eb38eeaa87c4d9e3b6fb16bab [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.
28#define DEFINE_TABLE_FEATURE_TYPE ArmFeatures
29#define DEFINE_TABLE_DB_FILENAME "cpuinfo_arm_db.inl"
30#include "define_tables.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010031
32typedef struct {
33 bool processor_reports_armv6;
34 bool hardware_reports_goldfish;
35} ProcCpuInfoData;
36
37static int IndexOfNonDigit(StringView str) {
38 size_t index = 0;
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020039 while (str.size && isdigit(CpuFeatures_StringView_Front(str))) {
40 str = CpuFeatures_StringView_PopFront(str, 1);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010041 ++index;
42 }
43 return index;
44}
45
46static bool HandleArmLine(const LineResult result, ArmInfo* const info,
47 ProcCpuInfoData* const proc_info) {
48 StringView line = result.line;
49 StringView key, value;
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020050 if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
51 if (CpuFeatures_StringView_IsEquals(key, str("Features"))) {
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020052 for (size_t i = 0; i < ARM_LAST_; ++i) {
53 kSetters[i](&info->features,
54 CpuFeatures_StringView_HasWord(value, kCpuInfoFlags[i]));
55 }
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020056 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU implementer"))) {
57 info->implementer = CpuFeatures_StringView_ParsePositiveNumber(value);
58 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU variant"))) {
59 info->variant = CpuFeatures_StringView_ParsePositiveNumber(value);
60 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU part"))) {
61 info->part = CpuFeatures_StringView_ParsePositiveNumber(value);
62 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU revision"))) {
63 info->revision = CpuFeatures_StringView_ParsePositiveNumber(value);
64 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU architecture"))) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +010065 // CPU architecture is a number that may be followed by letters. e.g.
66 // "6TEJ", "7".
Arvid Gerstmannd9689912018-05-04 09:32:17 +020067 const StringView digits =
68 CpuFeatures_StringView_KeepFront(value, IndexOfNonDigit(value));
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020069 info->architecture = CpuFeatures_StringView_ParsePositiveNumber(digits);
Guillaume Chatelet22a53622020-09-23 11:52:20 +020070 } else if (CpuFeatures_StringView_IsEquals(key, str("Processor")) ||
71 CpuFeatures_StringView_IsEquals(key, str("model name"))) {
Dr.-Ing. Patrick Siegl18342782019-06-25 17:58:31 +020072 // Android reports this in a non-Linux standard "Processor" but sometimes
73 // also in "model name", Linux reports it only in "model name"
74 // see RaspberryPiZero (Linux) vs InvalidArmv7 (Android) test-cases
Arvid Gerstmannd9689912018-05-04 09:32:17 +020075 proc_info->processor_reports_armv6 =
76 CpuFeatures_StringView_IndexOf(value, str("(v6l)")) >= 0;
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020077 } else if (CpuFeatures_StringView_IsEquals(key, str("Hardware"))) {
Arvid Gerstmannd9689912018-05-04 09:32:17 +020078 proc_info->hardware_reports_goldfish =
79 CpuFeatures_StringView_IsEquals(value, str("Goldfish"));
Guillaume Chatelet439d3712018-02-01 10:03:09 +010080 }
81 }
82 return !result.eof;
83}
84
Guillaume Chatelet918553a2019-01-17 15:28:04 +010085uint32_t GetArmCpuId(const ArmInfo* const info) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +010086 return (ExtractBitRange(info->implementer, 7, 0) << 24) |
87 (ExtractBitRange(info->variant, 3, 0) << 20) |
88 (ExtractBitRange(info->part, 11, 0) << 4) |
89 (ExtractBitRange(info->revision, 3, 0) << 0);
90}
91
92static void FixErrors(ArmInfo* const info,
93 ProcCpuInfoData* const proc_cpu_info_data) {
94 // Fixing Samsung kernel reporting invalid cpu architecture.
95 // http://code.google.com/p/android/issues/detail?id=10812
96 if (proc_cpu_info_data->processor_reports_armv6 && info->architecture >= 7) {
97 info->architecture = 6;
98 }
99
100 // Handle kernel configuration bugs that prevent the correct reporting of CPU
101 // features.
Guillaume Chatelet918553a2019-01-17 15:28:04 +0100102 switch (GetArmCpuId(info)) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100103 case 0x4100C080:
104 // Special case: The emulator-specific Android 4.2 kernel fails to report
105 // support for the 32-bit ARM IDIV instruction. Technically, this is a
106 // feature of the virtual CPU implemented by the emulator. Note that it
107 // could also support Thumb IDIV in the future, and this will have to be
108 // slightly updated.
109 if (info->architecture >= 7 &&
110 proc_cpu_info_data->hardware_reports_goldfish) {
111 info->features.idiva = true;
112 }
113 break;
114 case 0x511004D0:
115 // https://crbug.com/341598.
116 info->features.neon = false;
117 break;
118 case 0x510006F2:
119 case 0x510006F3:
120 // The Nexus 4 (Qualcomm Krait) kernel configuration forgets to report
121 // IDIV support.
122 info->features.idiva = true;
123 info->features.idivt = true;
124 break;
125 }
126
127 // Propagate cpu features.
128 if (info->features.vfpv4) info->features.vfpv3 = true;
129 if (info->features.neon) info->features.vfpv3 = true;
130 if (info->features.vfpv3) info->features.vfp = true;
131}
132
133static void FillProcCpuInfoData(ArmInfo* const info,
134 ProcCpuInfoData* proc_cpu_info_data) {
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +0200135 const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100136 if (fd >= 0) {
137 StackLineReader reader;
138 StackLineReader_Initialize(&reader, fd);
139 for (;;) {
140 if (!HandleArmLine(StackLineReader_NextLine(&reader), info,
141 proc_cpu_info_data)) {
142 break;
143 }
144 }
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +0200145 CpuFeatures_CloseFile(fd);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100146 }
147}
148
149static const ArmInfo kEmptyArmInfo;
150
151static const ProcCpuInfoData kEmptyProcCpuInfoData;
152
153ArmInfo GetArmInfo(void) {
154 // capabilities are fetched from both getauxval and /proc/cpuinfo so we can
155 // have some information if the executable is sandboxed (aka no access to
156 // /proc/cpuinfo).
157 ArmInfo info = kEmptyArmInfo;
158 ProcCpuInfoData proc_cpu_info_data = kEmptyProcCpuInfoData;
159
160 FillProcCpuInfoData(&info, &proc_cpu_info_data);
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +0200161 const HardwareCapabilities hwcaps = CpuFeatures_GetHardwareCapabilities();
162 for (size_t i = 0; i < ARM_LAST_; ++i) {
163 if (CpuFeatures_IsHwCapsSet(kHardwareCapabilities[i], hwcaps)) {
164 kSetters[i](&info.features, true);
165 }
166 }
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100167
168 FixErrors(&info, &proc_cpu_info_data);
169
170 return info;
171}
172
173////////////////////////////////////////////////////////////////////////////////
174// Introspection functions
175
176int GetArmFeaturesEnumValue(const ArmFeatures* features,
177 ArmFeaturesEnum value) {
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +0200178 if (value >= ARM_LAST_) return false;
179 return kGetters[value](features);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100180}
181
182const char* GetArmFeaturesEnumName(ArmFeaturesEnum value) {
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +0200183 if (value >= ARM_LAST_) return "unknown feature";
184 return kCpuInfoFlags[value];
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100185}