3ea064191f884ac451ce8cf1a820d8efd4c531eb
[iec.git] / src / type3_AndroidCloud / anbox-master / external / cpu_features / src / cpuinfo_arm.c
1 // Copyright 2017 Google Inc.
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 "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"
23
24 #include <ctype.h>
25
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)
39
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},          //
54 };
55
56 static const size_t kConfigsSize = sizeof(kConfigs) / sizeof(CapabilityConfig);
57
58 typedef struct {
59   bool processor_reports_armv6;
60   bool hardware_reports_goldfish;
61 } ProcCpuInfoData;
62
63 static int IndexOfNonDigit(StringView str) {
64   size_t index = 0;
65   while (str.size && isdigit(CpuFeatures_StringView_Front(str))) {
66     str = CpuFeatures_StringView_PopFront(str, 1);
67     ++index;
68   }
69   return index;
70 }
71
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.
89       // "6TEJ", "7".
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"));
99     }
100   }
101   return !result.eof;
102 }
103
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);
109 }
110
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;
117   }
118
119   // Handle kernel configuration bugs that prevent the correct reporting of CPU
120   // features.
121   switch (GetCpuId(info)) {
122     case 0x4100C080:
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
127       // slightly updated.
128       if (info->architecture >= 7 &&
129           proc_cpu_info_data->hardware_reports_goldfish) {
130         info->features.idiva = true;
131       }
132       break;
133     case 0x511004D0:
134       // https://crbug.com/341598.
135       info->features.neon = false;
136       break;
137     case 0x510006F2:
138     case 0x510006F3:
139       // The Nexus 4 (Qualcomm Krait) kernel configuration forgets to report
140       // IDIV support.
141       info->features.idiva = true;
142       info->features.idivt = true;
143       break;
144   }
145
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;
150 }
151
152 static void FillProcCpuInfoData(ArmInfo* const info,
153                                 ProcCpuInfoData* proc_cpu_info_data) {
154   const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
155   if (fd >= 0) {
156     StackLineReader reader;
157     StackLineReader_Initialize(&reader, fd);
158     for (;;) {
159       if (!HandleArmLine(StackLineReader_NextLine(&reader), info,
160                          proc_cpu_info_data)) {
161         break;
162       }
163     }
164     CpuFeatures_CloseFile(fd);
165   }
166 }
167
168 static const ArmInfo kEmptyArmInfo;
169
170 static const ProcCpuInfoData kEmptyProcCpuInfoData;
171
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
175   // /proc/cpuinfo).
176   ArmInfo info = kEmptyArmInfo;
177   ProcCpuInfoData proc_cpu_info_data = kEmptyProcCpuInfoData;
178
179   FillProcCpuInfoData(&info, &proc_cpu_info_data);
180   CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs,
181                                  CpuFeatures_GetHardwareCapabilities(),
182                                  &info.features);
183
184   FixErrors(&info, &proc_cpu_info_data);
185
186   return info;
187 }
188
189 ////////////////////////////////////////////////////////////////////////////////
190 // Introspection functions
191
192 int GetArmFeaturesEnumValue(const ArmFeatures* features,
193                             ArmFeaturesEnum value) {
194   switch (value) {
195     case ARM_VFP:
196       return features->vfp;
197     case ARM_IWMMXT:
198       return features->iwmmxt;
199     case ARM_NEON:
200       return features->neon;
201     case ARM_VFPV3:
202       return features->vfpv3;
203     case ARM_VFPV3D16:
204       return features->vfpv3d16;
205     case ARM_VFPV4:
206       return features->vfpv4;
207     case ARM_IDIVA:
208       return features->idiva;
209     case ARM_IDIVT:
210       return features->idivt;
211     case ARM_AES:
212       return features->aes;
213     case ARM_PMULL:
214       return features->pmull;
215     case ARM_SHA1:
216       return features->sha1;
217     case ARM_SHA2:
218       return features->sha2;
219     case ARM_CRC32:
220       return features->crc32;
221     case ARM_LAST_:
222       break;
223   }
224   return false;
225 }
226
227 const char* GetArmFeaturesEnumName(ArmFeaturesEnum value) {
228   switch (value) {
229     case ARM_VFP:
230       return "vfp";
231     case ARM_IWMMXT:
232       return "iwmmxt";
233     case ARM_NEON:
234       return "neon";
235     case ARM_VFPV3:
236       return "vfpv3";
237     case ARM_VFPV3D16:
238       return "vfpv3d16";
239     case ARM_VFPV4:
240       return "vfpv4";
241     case ARM_IDIVA:
242       return "idiva";
243     case ARM_IDIVT:
244       return "idivt";
245     case ARM_AES:
246       return "aes";
247     case ARM_PMULL:
248       return "pmull";
249     case ARM_SHA1:
250       return "sha1";
251     case ARM_SHA2:
252       return "sha2";
253     case ARM_CRC32:
254       return "crc32";
255     case ARM_LAST_:
256       break;
257   }
258   return "unknown feature";
259 }