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_arm.h"
16
17 #include <assert.h>
18 #include <ctype.h>
19
20 #include "internal/bit_utils.h"
21 #include "internal/filesystem.h"
22 #include "internal/hwcaps.h"
23 #include "internal/stack_line_reader.h"
24 #include "internal/string_view.h"
25
26 // Generation of feature's getters/setters functions and kGetters, kSetters,
27 // kCpuInfoFlags and kHardwareCapabilities global tables.
28 #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)
56 #define DEFINE_TABLE_FEATURE_TYPE ArmFeatures
57 #include "define_tables.h"
58
59 typedef struct {
60 bool processor_reports_armv6;
61 bool hardware_reports_goldfish;
62 } ProcCpuInfoData;
63
IndexOfNonDigit(StringView str)64 static int IndexOfNonDigit(StringView str) {
65 size_t index = 0;
66 while (str.size && isdigit(CpuFeatures_StringView_Front(str))) {
67 str = CpuFeatures_StringView_PopFront(str, 1);
68 ++index;
69 }
70 return index;
71 }
72
HandleArmLine(const LineResult result,ArmInfo * const info,ProcCpuInfoData * const proc_info)73 static bool HandleArmLine(const LineResult result, ArmInfo* const info,
74 ProcCpuInfoData* const proc_info) {
75 StringView line = result.line;
76 StringView key, value;
77 if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
78 if (CpuFeatures_StringView_IsEquals(key, str("Features"))) {
79 for (size_t i = 0; i < ARM_LAST_; ++i) {
80 kSetters[i](&info->features,
81 CpuFeatures_StringView_HasWord(value, kCpuInfoFlags[i]));
82 }
83 } 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"))) {
92 // CPU architecture is a number that may be followed by letters. e.g.
93 // "6TEJ", "7".
94 const StringView digits =
95 CpuFeatures_StringView_KeepFront(value, IndexOfNonDigit(value));
96 info->architecture = CpuFeatures_StringView_ParsePositiveNumber(digits);
97 } else if (CpuFeatures_StringView_IsEquals(key, str("Processor")) ||
98 CpuFeatures_StringView_IsEquals(key, str("model name"))) {
99 // 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
102 proc_info->processor_reports_armv6 =
103 CpuFeatures_StringView_IndexOf(value, str("(v6l)")) >= 0;
104 } else if (CpuFeatures_StringView_IsEquals(key, str("Hardware"))) {
105 proc_info->hardware_reports_goldfish =
106 CpuFeatures_StringView_IsEquals(value, str("Goldfish"));
107 }
108 }
109 return !result.eof;
110 }
111
GetArmCpuId(const ArmInfo * const info)112 uint32_t GetArmCpuId(const ArmInfo* const info) {
113 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
FixErrors(ArmInfo * const info,ProcCpuInfoData * const proc_cpu_info_data)119 static 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.
129 switch (GetArmCpuId(info)) {
130 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
FillProcCpuInfoData(ArmInfo * const info,ProcCpuInfoData * proc_cpu_info_data)160 static void FillProcCpuInfoData(ArmInfo* const info,
161 ProcCpuInfoData* proc_cpu_info_data) {
162 const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
163 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 }
172 CpuFeatures_CloseFile(fd);
173 }
174 }
175
176 static const ArmInfo kEmptyArmInfo;
177
178 static const ProcCpuInfoData kEmptyProcCpuInfoData;
179
GetArmInfo(void)180 ArmInfo 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);
188 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 }
194
195 FixErrors(&info, &proc_cpu_info_data);
196
197 return info;
198 }
199
200 ////////////////////////////////////////////////////////////////////////////////
201 // Introspection functions
202
GetArmFeaturesEnumValue(const ArmFeatures * features,ArmFeaturesEnum value)203 int GetArmFeaturesEnumValue(const ArmFeatures* features,
204 ArmFeaturesEnum value) {
205 if (value >= ARM_LAST_) return false;
206 return kGetters[value](features);
207 }
208
GetArmFeaturesEnumName(ArmFeaturesEnum value)209 const char* GetArmFeaturesEnumName(ArmFeaturesEnum value) {
210 if (value >= ARM_LAST_) return "unknown feature";
211 return kCpuInfoFlags[value];
212 }
213