0d111ff934301b24f25bc5045e72b84f5e258e7d
[iec.git] / src / type3_AndroidCloud / anbox-master / external / cpu_features / src / cpuinfo_aarch64.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_aarch64.h"
16
17 #include "internal/filesystem.h"
18 #include "internal/hwcaps.h"
19 #include "internal/linux_features_aggregator.h"
20 #include "internal/stack_line_reader.h"
21 #include "internal/string_view.h"
22
23 #include <ctype.h>
24
25 DECLARE_SETTER(Aarch64Features, fp)
26 DECLARE_SETTER(Aarch64Features, asimd)
27 DECLARE_SETTER(Aarch64Features, aes)
28 DECLARE_SETTER(Aarch64Features, pmull)
29 DECLARE_SETTER(Aarch64Features, sha1)
30 DECLARE_SETTER(Aarch64Features, sha2)
31 DECLARE_SETTER(Aarch64Features, crc32)
32
33 static const CapabilityConfig kConfigs[] = {
34     {{AARCH64_HWCAP_FP, 0}, "fp", &set_fp},           //
35     {{AARCH64_HWCAP_ASIMD, 0}, "asimd", &set_asimd},  //
36     {{AARCH64_HWCAP_AES, 0}, "aes", &set_aes},        //
37     {{AARCH64_HWCAP_PMULL, 0}, "pmull", &set_pmull},  //
38     {{AARCH64_HWCAP_SHA1, 0}, "sha1", &set_sha1},     //
39     {{AARCH64_HWCAP_SHA2, 0}, "sha2", &set_sha2},     //
40     {{AARCH64_HWCAP_CRC32, 0}, "crc32", &set_crc32},  //
41 };
42
43 static const size_t kConfigsSize = sizeof(kConfigs) / sizeof(CapabilityConfig);
44
45 static bool HandleAarch64Line(const LineResult result,
46                               Aarch64Info* const info) {
47   StringView line = result.line;
48   StringView key, value;
49   if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
50     if (CpuFeatures_StringView_IsEquals(key, str("Features"))) {
51       CpuFeatures_SetFromFlags(kConfigsSize, kConfigs, value, &info->features);
52     } else if (CpuFeatures_StringView_IsEquals(key, str("CPU implementer"))) {
53       info->implementer = CpuFeatures_StringView_ParsePositiveNumber(value);
54     } else if (CpuFeatures_StringView_IsEquals(key, str("CPU variant"))) {
55       info->variant = CpuFeatures_StringView_ParsePositiveNumber(value);
56     } else if (CpuFeatures_StringView_IsEquals(key, str("CPU part"))) {
57       info->part = CpuFeatures_StringView_ParsePositiveNumber(value);
58     } else if (CpuFeatures_StringView_IsEquals(key, str("CPU revision"))) {
59       info->revision = CpuFeatures_StringView_ParsePositiveNumber(value);
60     }
61   }
62   return !result.eof;
63 }
64
65 static void FillProcCpuInfoData(Aarch64Info* const info) {
66   const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
67   if (fd >= 0) {
68     StackLineReader reader;
69     StackLineReader_Initialize(&reader, fd);
70     for (;;) {
71       if (!HandleAarch64Line(StackLineReader_NextLine(&reader), info)) {
72         break;
73       }
74     }
75     CpuFeatures_CloseFile(fd);
76   }
77 }
78
79 static const Aarch64Info kEmptyAarch64Info;
80
81 Aarch64Info GetAarch64Info(void) {
82   // capabilities are fetched from both getauxval and /proc/cpuinfo so we can
83   // have some information if the executable is sandboxed (aka no access to
84   // /proc/cpuinfo).
85   Aarch64Info info = kEmptyAarch64Info;
86
87   FillProcCpuInfoData(&info);
88   CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs,
89                                  CpuFeatures_GetHardwareCapabilities(),
90                                  &info.features);
91
92   return info;
93 }
94
95 ////////////////////////////////////////////////////////////////////////////////
96 // Introspection functions
97
98 int GetAarch64FeaturesEnumValue(const Aarch64Features* features,
99                                 Aarch64FeaturesEnum value) {
100   switch (value) {
101     case AARCH64_FP:
102       return features->fp;
103     case AARCH64_ASIMD:
104       return features->asimd;
105     case AARCH64_AES:
106       return features->aes;
107     case AARCH64_PMULL:
108       return features->pmull;
109     case AARCH64_SHA1:
110       return features->sha1;
111     case AARCH64_SHA2:
112       return features->sha2;
113     case AARCH64_CRC32:
114       return features->crc32;
115     case AARCH64_LAST_:
116       break;
117   }
118   return false;
119 }
120
121 const char* GetAarch64FeaturesEnumName(Aarch64FeaturesEnum value) {
122   switch (value) {
123     case AARCH64_FP:
124       return "fp";
125     case AARCH64_ASIMD:
126       return "asimd";
127     case AARCH64_AES:
128       return "aes";
129     case AARCH64_PMULL:
130       return "pmull";
131     case AARCH64_SHA1:
132       return "sha1";
133     case AARCH64_SHA2:
134       return "sha2";
135     case AARCH64_CRC32:
136       return "crc32";
137     case AARCH64_LAST_:
138       break;
139   }
140   return "unknown feature";
141 }