TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / external / cpu_features / src / cpuinfo_mips.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_mips.h"
16
17 #include "internal/filesystem.h"
18 #include "internal/linux_features_aggregator.h"
19 #include "internal/stack_line_reader.h"
20 #include "internal/string_view.h"
21
22 DECLARE_SETTER(MipsFeatures, msa)
23 DECLARE_SETTER(MipsFeatures, eva)
24
25 static const CapabilityConfig kConfigs[] = {
26     {{MIPS_HWCAP_MSA, 0}, "msa", &set_msa},  //
27     {{MIPS_HWCAP_EVA, 0}, "eva", &set_eva},  //
28 };
29 static const size_t kConfigsSize = sizeof(kConfigs) / sizeof(CapabilityConfig);
30
31 static bool HandleMipsLine(const LineResult result,
32                            MipsFeatures* const features) {
33   StringView key, value;
34   // See tests for an example.
35   if (CpuFeatures_StringView_GetAttributeKeyValue(result.line, &key, &value)) {
36     if (CpuFeatures_StringView_IsEquals(key, str("ASEs implemented"))) {
37       CpuFeatures_SetFromFlags(kConfigsSize, kConfigs, value, features);
38     }
39   }
40   return !result.eof;
41 }
42
43 static void FillProcCpuInfoData(MipsFeatures* const features) {
44   const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
45   if (fd >= 0) {
46     StackLineReader reader;
47     StackLineReader_Initialize(&reader, fd);
48     for (;;) {
49       if (!HandleMipsLine(StackLineReader_NextLine(&reader), features)) {
50         break;
51       }
52     }
53     CpuFeatures_CloseFile(fd);
54   }
55 }
56
57 static const MipsInfo kEmptyMipsInfo;
58
59 MipsInfo GetMipsInfo(void) {
60   // capabilities are fetched from both getauxval and /proc/cpuinfo so we can
61   // have some information if the executable is sandboxed (aka no access to
62   // /proc/cpuinfo).
63   MipsInfo info = kEmptyMipsInfo;
64
65   FillProcCpuInfoData(&info.features);
66   CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs,
67                                  CpuFeatures_GetHardwareCapabilities(),
68                                  &info.features);
69   return info;
70 }
71
72 ////////////////////////////////////////////////////////////////////////////////
73 // Introspection functions
74
75 int GetMipsFeaturesEnumValue(const MipsFeatures* features,
76                              MipsFeaturesEnum value) {
77   switch (value) {
78     case MIPS_MSA:
79       return features->msa;
80     case MIPS_EVA:
81       return features->eva;
82     case MIPS_LAST_:
83       break;
84   }
85   return false;
86 }
87
88 const char* GetMipsFeaturesEnumName(MipsFeaturesEnum value) {
89   switch (value) {
90     case MIPS_MSA:
91       return "msa";
92     case MIPS_EVA:
93       return "eva";
94     case MIPS_LAST_:
95       break;
96   }
97   return "unknown feature";
98 }