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 "cpu_features_macros.h"
16
17 #ifdef CPU_FEATURES_ARCH_ARM
18 #if defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)
19
20 #include "cpuinfo_arm.h"
21
22 ////////////////////////////////////////////////////////////////////////////////
23 // Definitions for introspection.
24 ////////////////////////////////////////////////////////////////////////////////
25 #define INTROSPECTION_TABLE \
26 LINE(ARM_SWP, swp, "swp", ARM_HWCAP_SWP, 0) \
27 LINE(ARM_HALF, half, "half", ARM_HWCAP_HALF, 0) \
28 LINE(ARM_THUMB, thumb, "thumb", ARM_HWCAP_THUMB, 0) \
29 LINE(ARM_26BIT, _26bit, "26bit", ARM_HWCAP_26BIT, 0) \
30 LINE(ARM_FASTMULT, fastmult, "fastmult", ARM_HWCAP_FAST_MULT, 0) \
31 LINE(ARM_FPA, fpa, "fpa", ARM_HWCAP_FPA, 0) \
32 LINE(ARM_VFP, vfp, "vfp", ARM_HWCAP_VFP, 0) \
33 LINE(ARM_EDSP, edsp, "edsp", ARM_HWCAP_EDSP, 0) \
34 LINE(ARM_JAVA, java, "java", ARM_HWCAP_JAVA, 0) \
35 LINE(ARM_IWMMXT, iwmmxt, "iwmmxt", ARM_HWCAP_IWMMXT, 0) \
36 LINE(ARM_CRUNCH, crunch, "crunch", ARM_HWCAP_CRUNCH, 0) \
37 LINE(ARM_THUMBEE, thumbee, "thumbee", ARM_HWCAP_THUMBEE, 0) \
38 LINE(ARM_NEON, neon, "neon", ARM_HWCAP_NEON, 0) \
39 LINE(ARM_VFPV3, vfpv3, "vfpv3", ARM_HWCAP_VFPV3, 0) \
40 LINE(ARM_VFPV3D16, vfpv3d16, "vfpv3d16", ARM_HWCAP_VFPV3D16, 0) \
41 LINE(ARM_TLS, tls, "tls", ARM_HWCAP_TLS, 0) \
42 LINE(ARM_VFPV4, vfpv4, "vfpv4", ARM_HWCAP_VFPV4, 0) \
43 LINE(ARM_IDIVA, idiva, "idiva", ARM_HWCAP_IDIVA, 0) \
44 LINE(ARM_IDIVT, idivt, "idivt", ARM_HWCAP_IDIVT, 0) \
45 LINE(ARM_VFPD32, vfpd32, "vfpd32", ARM_HWCAP_VFPD32, 0) \
46 LINE(ARM_LPAE, lpae, "lpae", ARM_HWCAP_LPAE, 0) \
47 LINE(ARM_EVTSTRM, evtstrm, "evtstrm", ARM_HWCAP_EVTSTRM, 0) \
48 LINE(ARM_AES, aes, "aes", 0, ARM_HWCAP2_AES) \
49 LINE(ARM_PMULL, pmull, "pmull", 0, ARM_HWCAP2_PMULL) \
50 LINE(ARM_SHA1, sha1, "sha1", 0, ARM_HWCAP2_SHA1) \
51 LINE(ARM_SHA2, sha2, "sha2", 0, ARM_HWCAP2_SHA2) \
52 LINE(ARM_CRC32, crc32, "crc32", 0, ARM_HWCAP2_CRC32)
53 #define INTROSPECTION_PREFIX Arm
54 #define INTROSPECTION_ENUM_PREFIX ARM
55 #include "define_introspection_and_hwcaps.inl"
56
57 ////////////////////////////////////////////////////////////////////////////////
58 // Implementation.
59 ////////////////////////////////////////////////////////////////////////////////
60
61 #include <ctype.h>
62 #include <stdbool.h>
63
64 #include "internal/bit_utils.h"
65 #include "internal/filesystem.h"
66 #include "internal/stack_line_reader.h"
67 #include "internal/string_view.h"
68
69 typedef struct {
70 bool processor_reports_armv6;
71 bool hardware_reports_goldfish;
72 } ProcCpuInfoData;
73
IndexOfNonDigit(StringView str)74 static int IndexOfNonDigit(StringView str) {
75 size_t index = 0;
76 while (str.size && isdigit(CpuFeatures_StringView_Front(str))) {
77 str = CpuFeatures_StringView_PopFront(str, 1);
78 ++index;
79 }
80 return index;
81 }
82
HandleArmLine(const LineResult result,ArmInfo * const info,ProcCpuInfoData * const proc_info)83 static bool HandleArmLine(const LineResult result, ArmInfo* const info,
84 ProcCpuInfoData* const proc_info) {
85 StringView line = result.line;
86 StringView key, value;
87 if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
88 if (CpuFeatures_StringView_IsEquals(key, str("Features"))) {
89 for (size_t i = 0; i < ARM_LAST_; ++i) {
90 kSetters[i](&info->features, CpuFeatures_StringView_HasWord(
91 value, kCpuInfoFlags[i], ' '));
92 }
93 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU implementer"))) {
94 info->implementer = CpuFeatures_StringView_ParsePositiveNumber(value);
95 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU variant"))) {
96 info->variant = CpuFeatures_StringView_ParsePositiveNumber(value);
97 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU part"))) {
98 info->part = CpuFeatures_StringView_ParsePositiveNumber(value);
99 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU revision"))) {
100 info->revision = CpuFeatures_StringView_ParsePositiveNumber(value);
101 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU architecture"))) {
102 // CPU architecture is a number that may be followed by letters. e.g.
103 // "6TEJ", "7".
104 const StringView digits =
105 CpuFeatures_StringView_KeepFront(value, IndexOfNonDigit(value));
106 info->architecture = CpuFeatures_StringView_ParsePositiveNumber(digits);
107 } else if (CpuFeatures_StringView_IsEquals(key, str("Processor")) ||
108 CpuFeatures_StringView_IsEquals(key, str("model name"))) {
109 // Android reports this in a non-Linux standard "Processor" but sometimes
110 // also in "model name", Linux reports it only in "model name"
111 // see RaspberryPiZero (Linux) vs InvalidArmv7 (Android) test-cases
112 proc_info->processor_reports_armv6 =
113 CpuFeatures_StringView_IndexOf(value, str("(v6l)")) >= 0;
114 } else if (CpuFeatures_StringView_IsEquals(key, str("Hardware"))) {
115 proc_info->hardware_reports_goldfish =
116 CpuFeatures_StringView_IsEquals(value, str("Goldfish"));
117 }
118 }
119 return !result.eof;
120 }
121
GetArmCpuId(const ArmInfo * const info)122 uint32_t GetArmCpuId(const ArmInfo* const info) {
123 return (ExtractBitRange(info->implementer, 7, 0) << 24) |
124 (ExtractBitRange(info->variant, 3, 0) << 20) |
125 (ExtractBitRange(info->part, 11, 0) << 4) |
126 (ExtractBitRange(info->revision, 3, 0) << 0);
127 }
128
FixErrors(ArmInfo * const info,ProcCpuInfoData * const proc_cpu_info_data)129 static void FixErrors(ArmInfo* const info,
130 ProcCpuInfoData* const proc_cpu_info_data) {
131 // Fixing Samsung kernel reporting invalid cpu architecture.
132 // http://code.google.com/p/android/issues/detail?id=10812
133 if (proc_cpu_info_data->processor_reports_armv6 && info->architecture >= 7) {
134 info->architecture = 6;
135 }
136
137 // Handle kernel configuration bugs that prevent the correct reporting of CPU
138 // features.
139 switch (GetArmCpuId(info)) {
140 case 0x4100C080:
141 // Special case: The emulator-specific Android 4.2 kernel fails to report
142 // support for the 32-bit ARM IDIV instruction. Technically, this is a
143 // feature of the virtual CPU implemented by the emulator. Note that it
144 // could also support Thumb IDIV in the future, and this will have to be
145 // slightly updated.
146 if (info->architecture >= 7 &&
147 proc_cpu_info_data->hardware_reports_goldfish) {
148 info->features.idiva = true;
149 }
150 break;
151 case 0x511004D0:
152 // https://crbug.com/341598.
153 info->features.neon = false;
154 break;
155 }
156
157 // Some Qualcomm Krait kernels forget to report IDIV support.
158 // https://github.com/torvalds/linux/commit/120ecfafabec382c4feb79ff159ef42a39b6d33b
159 if (info->implementer == 0x51 && info->architecture == 7 &&
160 (info->part == 0x4d || info->part == 0x6f)) {
161 info->features.idiva = true;
162 info->features.idivt = true;
163 }
164
165 // Propagate cpu features.
166 if (info->features.vfpv4) info->features.vfpv3 = true;
167 if (info->features.neon) info->features.vfpv3 = true;
168 if (info->features.vfpv3) info->features.vfp = true;
169 }
170
FillProcCpuInfoData(ArmInfo * const info,ProcCpuInfoData * proc_cpu_info_data)171 static void FillProcCpuInfoData(ArmInfo* const info,
172 ProcCpuInfoData* proc_cpu_info_data) {
173 const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
174 if (fd >= 0) {
175 StackLineReader reader;
176 StackLineReader_Initialize(&reader, fd);
177 for (;;) {
178 if (!HandleArmLine(StackLineReader_NextLine(&reader), info,
179 proc_cpu_info_data)) {
180 break;
181 }
182 }
183 CpuFeatures_CloseFile(fd);
184 }
185 }
186
187 static const ArmInfo kEmptyArmInfo;
188
189 static const ProcCpuInfoData kEmptyProcCpuInfoData;
190
GetArmInfo(void)191 ArmInfo GetArmInfo(void) {
192 // capabilities are fetched from both getauxval and /proc/cpuinfo so we can
193 // have some information if the executable is sandboxed (aka no access to
194 // /proc/cpuinfo).
195 ArmInfo info = kEmptyArmInfo;
196 ProcCpuInfoData proc_cpu_info_data = kEmptyProcCpuInfoData;
197
198 FillProcCpuInfoData(&info, &proc_cpu_info_data);
199 const HardwareCapabilities hwcaps = CpuFeatures_GetHardwareCapabilities();
200 for (size_t i = 0; i < ARM_LAST_; ++i) {
201 if (CpuFeatures_IsHwCapsSet(kHardwareCapabilities[i], hwcaps)) {
202 kSetters[i](&info.features, true);
203 }
204 }
205
206 FixErrors(&info, &proc_cpu_info_data);
207
208 return info;
209 }
210
211 #endif // defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)
212 #endif // CPU_FEATURES_ARCH_ARM
213