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 "cpuinfo_mips.h"
16 
17 #include <assert.h>
18 
19 #include "internal/filesystem.h"
20 #include "internal/hwcaps.h"
21 #include "internal/stack_line_reader.h"
22 #include "internal/string_view.h"
23 
24 // Generation of feature's getters/setters functions and kGetters, kSetters,
25 // kCpuInfoFlags and kHardwareCapabilities global tables.
26 #define DEFINE_TABLE_FEATURES                      \
27   FEATURE(MIPS_MSA, msa, "msa", MIPS_HWCAP_MSA, 0) \
28   FEATURE(MIPS_EVA, eva, "eva", 0, 0)              \
29   FEATURE(MIPS_R6, r6, "r6", MIPS_HWCAP_R6, 0)
30 #define DEFINE_TABLE_FEATURE_TYPE MipsFeatures
31 #include "define_tables.h"
32 
HandleMipsLine(const LineResult result,MipsFeatures * const features)33 static bool HandleMipsLine(const LineResult result,
34                            MipsFeatures* const features) {
35   StringView key, value;
36   // See tests for an example.
37   if (CpuFeatures_StringView_GetAttributeKeyValue(result.line, &key, &value)) {
38     if (CpuFeatures_StringView_IsEquals(key, str("ASEs implemented"))) {
39       for (size_t i = 0; i < MIPS_LAST_; ++i) {
40         kSetters[i](features,
41                     CpuFeatures_StringView_HasWord(value, kCpuInfoFlags[i]));
42       }
43     }
44   }
45   return !result.eof;
46 }
47 
FillProcCpuInfoData(MipsFeatures * const features)48 static void FillProcCpuInfoData(MipsFeatures* const features) {
49   const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
50   if (fd >= 0) {
51     StackLineReader reader;
52     StackLineReader_Initialize(&reader, fd);
53     for (;;) {
54       if (!HandleMipsLine(StackLineReader_NextLine(&reader), features)) {
55         break;
56       }
57     }
58     CpuFeatures_CloseFile(fd);
59   }
60 }
61 
62 static const MipsInfo kEmptyMipsInfo;
63 
GetMipsInfo(void)64 MipsInfo GetMipsInfo(void) {
65   // capabilities are fetched from both getauxval and /proc/cpuinfo so we can
66   // have some information if the executable is sandboxed (aka no access to
67   // /proc/cpuinfo).
68   MipsInfo info = kEmptyMipsInfo;
69 
70   FillProcCpuInfoData(&info.features);
71   const HardwareCapabilities hwcaps = CpuFeatures_GetHardwareCapabilities();
72   for (size_t i = 0; i < MIPS_LAST_; ++i) {
73     if (CpuFeatures_IsHwCapsSet(kHardwareCapabilities[i], hwcaps)) {
74       kSetters[i](&info.features, true);
75     }
76   }
77   return info;
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 // Introspection functions
82 
GetMipsFeaturesEnumValue(const MipsFeatures * features,MipsFeaturesEnum value)83 int GetMipsFeaturesEnumValue(const MipsFeatures* features,
84                              MipsFeaturesEnum value) {
85   if (value >= MIPS_LAST_) return false;
86   return kGetters[value](features);
87 }
88 
GetMipsFeaturesEnumName(MipsFeaturesEnum value)89 const char* GetMipsFeaturesEnumName(MipsFeaturesEnum value) {
90   if (value >= MIPS_LAST_) return "unknown feature";
91   return kCpuInfoFlags[value];
92 }
93