1 // Copyright 2017 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "cpuinfo_arm.h"
17 #include "internal/bit_utils.h"
18 #include "internal/filesystem.h"
19 #include "internal/hwcaps.h"
20 #include "internal/linux_features_aggregator.h"
21 #include "internal/stack_line_reader.h"
22 #include "internal/string_view.h"
26 DECLARE_SETTER(ArmFeatures, vfp)
27 DECLARE_SETTER(ArmFeatures, iwmmxt)
28 DECLARE_SETTER(ArmFeatures, neon)
29 DECLARE_SETTER(ArmFeatures, vfpv3)
30 DECLARE_SETTER(ArmFeatures, vfpv3d16)
31 DECLARE_SETTER(ArmFeatures, vfpv4)
32 DECLARE_SETTER(ArmFeatures, idiva)
33 DECLARE_SETTER(ArmFeatures, idivt)
34 DECLARE_SETTER(ArmFeatures, aes)
35 DECLARE_SETTER(ArmFeatures, pmull)
36 DECLARE_SETTER(ArmFeatures, sha1)
37 DECLARE_SETTER(ArmFeatures, sha2)
38 DECLARE_SETTER(ArmFeatures, crc32)
40 static const CapabilityConfig kConfigs[] = {
41 {{ARM_HWCAP_VFP, 0}, "vfp", &set_vfp}, //
42 {{ARM_HWCAP_IWMMXT, 0}, "iwmmxt", &set_iwmmxt}, //
43 {{ARM_HWCAP_NEON, 0}, "neon", &set_neon}, //
44 {{ARM_HWCAP_VFPV3, 0}, "vfpv3", &set_vfpv3}, //
45 {{ARM_HWCAP_VFPV3D16, 0}, "vfpv3d16", &set_vfpv3d16}, //
46 {{ARM_HWCAP_VFPV4, 0}, "vfpv4", &set_vfpv4}, //
47 {{ARM_HWCAP_IDIVA, 0}, "idiva", &set_idiva}, //
48 {{ARM_HWCAP_IDIVT, 0}, "idivt", &set_idivt}, //
49 {{0, ARM_HWCAP2_AES}, "aes", &set_aes}, //
50 {{0, ARM_HWCAP2_PMULL}, "pmull", &set_pmull}, //
51 {{0, ARM_HWCAP2_SHA1}, "sha1", &set_sha1}, //
52 {{0, ARM_HWCAP2_SHA2}, "sha2", &set_sha2}, //
53 {{0, ARM_HWCAP2_CRC32}, "crc32", &set_crc32}, //
56 static const size_t kConfigsSize = sizeof(kConfigs) / sizeof(CapabilityConfig);
59 bool processor_reports_armv6;
60 bool hardware_reports_goldfish;
63 static int IndexOfNonDigit(StringView str) {
65 while (str.size && isdigit(CpuFeatures_StringView_Front(str))) {
66 str = CpuFeatures_StringView_PopFront(str, 1);
72 static bool HandleArmLine(const LineResult result, ArmInfo* const info,
73 ProcCpuInfoData* const proc_info) {
74 StringView line = result.line;
75 StringView key, value;
76 if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
77 if (CpuFeatures_StringView_IsEquals(key, str("Features"))) {
78 CpuFeatures_SetFromFlags(kConfigsSize, kConfigs, value, &info->features);
79 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU implementer"))) {
80 info->implementer = CpuFeatures_StringView_ParsePositiveNumber(value);
81 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU variant"))) {
82 info->variant = CpuFeatures_StringView_ParsePositiveNumber(value);
83 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU part"))) {
84 info->part = CpuFeatures_StringView_ParsePositiveNumber(value);
85 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU revision"))) {
86 info->revision = CpuFeatures_StringView_ParsePositiveNumber(value);
87 } else if (CpuFeatures_StringView_IsEquals(key, str("CPU architecture"))) {
88 // CPU architecture is a number that may be followed by letters. e.g.
90 const StringView digits =
91 CpuFeatures_StringView_KeepFront(value, IndexOfNonDigit(value));
92 info->architecture = CpuFeatures_StringView_ParsePositiveNumber(digits);
93 } else if (CpuFeatures_StringView_IsEquals(key, str("Processor"))) {
94 proc_info->processor_reports_armv6 =
95 CpuFeatures_StringView_IndexOf(value, str("(v6l)")) >= 0;
96 } else if (CpuFeatures_StringView_IsEquals(key, str("Hardware"))) {
97 proc_info->hardware_reports_goldfish =
98 CpuFeatures_StringView_IsEquals(value, str("Goldfish"));
104 static uint32_t GetCpuId(const ArmInfo* const info) {
105 return (ExtractBitRange(info->implementer, 7, 0) << 24) |
106 (ExtractBitRange(info->variant, 3, 0) << 20) |
107 (ExtractBitRange(info->part, 11, 0) << 4) |
108 (ExtractBitRange(info->revision, 3, 0) << 0);
111 static void FixErrors(ArmInfo* const info,
112 ProcCpuInfoData* const proc_cpu_info_data) {
113 // Fixing Samsung kernel reporting invalid cpu architecture.
114 // http://code.google.com/p/android/issues/detail?id=10812
115 if (proc_cpu_info_data->processor_reports_armv6 && info->architecture >= 7) {
116 info->architecture = 6;
119 // Handle kernel configuration bugs that prevent the correct reporting of CPU
121 switch (GetCpuId(info)) {
123 // Special case: The emulator-specific Android 4.2 kernel fails to report
124 // support for the 32-bit ARM IDIV instruction. Technically, this is a
125 // feature of the virtual CPU implemented by the emulator. Note that it
126 // could also support Thumb IDIV in the future, and this will have to be
128 if (info->architecture >= 7 &&
129 proc_cpu_info_data->hardware_reports_goldfish) {
130 info->features.idiva = true;
134 // https://crbug.com/341598.
135 info->features.neon = false;
139 // The Nexus 4 (Qualcomm Krait) kernel configuration forgets to report
141 info->features.idiva = true;
142 info->features.idivt = true;
146 // Propagate cpu features.
147 if (info->features.vfpv4) info->features.vfpv3 = true;
148 if (info->features.neon) info->features.vfpv3 = true;
149 if (info->features.vfpv3) info->features.vfp = true;
152 static void FillProcCpuInfoData(ArmInfo* const info,
153 ProcCpuInfoData* proc_cpu_info_data) {
154 const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
156 StackLineReader reader;
157 StackLineReader_Initialize(&reader, fd);
159 if (!HandleArmLine(StackLineReader_NextLine(&reader), info,
160 proc_cpu_info_data)) {
164 CpuFeatures_CloseFile(fd);
168 static const ArmInfo kEmptyArmInfo;
170 static const ProcCpuInfoData kEmptyProcCpuInfoData;
172 ArmInfo GetArmInfo(void) {
173 // capabilities are fetched from both getauxval and /proc/cpuinfo so we can
174 // have some information if the executable is sandboxed (aka no access to
176 ArmInfo info = kEmptyArmInfo;
177 ProcCpuInfoData proc_cpu_info_data = kEmptyProcCpuInfoData;
179 FillProcCpuInfoData(&info, &proc_cpu_info_data);
180 CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs,
181 CpuFeatures_GetHardwareCapabilities(),
184 FixErrors(&info, &proc_cpu_info_data);
189 ////////////////////////////////////////////////////////////////////////////////
190 // Introspection functions
192 int GetArmFeaturesEnumValue(const ArmFeatures* features,
193 ArmFeaturesEnum value) {
196 return features->vfp;
198 return features->iwmmxt;
200 return features->neon;
202 return features->vfpv3;
204 return features->vfpv3d16;
206 return features->vfpv4;
208 return features->idiva;
210 return features->idivt;
212 return features->aes;
214 return features->pmull;
216 return features->sha1;
218 return features->sha2;
220 return features->crc32;
227 const char* GetArmFeaturesEnumName(ArmFeaturesEnum value) {
258 return "unknown feature";