• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 //    http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #include "cpu_features_macros.h"
14 
15 #ifdef CPU_FEATURES_ARCH_MIPS
16 #if defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)
17 
18 #include "cpuinfo_mips.h"
19 
20 ////////////////////////////////////////////////////////////////////////////////
21 // Definitions for introspection.
22 ////////////////////////////////////////////////////////////////////////////////
23 #define INTROSPECTION_TABLE                                     \
24   LINE(MIPS_MSA, msa, "msa", MIPS_HWCAP_MSA, 0)                 \
25   LINE(MIPS_EVA, eva, "eva", 0, 0)                              \
26   LINE(MIPS_R6, r6, "r6", MIPS_HWCAP_R6, 0)                     \
27   LINE(MIPS_MIPS16, mips16, "mips16", MIPS_HWCAP_MIPS16, 0)     \
28   LINE(MIPS_MDMX, mdmx, "mdmx", MIPS_HWCAP_MDMX, 0)             \
29   LINE(MIPS_MIPS3D, mips3d, "mips3d", MIPS_HWCAP_MIPS3D, 0)     \
30   LINE(MIPS_SMART, smart, "smartmips", MIPS_HWCAP_SMARTMIPS, 0) \
31   LINE(MIPS_DSP, dsp, "dsp", MIPS_HWCAP_DSP, 0)
32 #define INTROSPECTION_PREFIX Mips
33 #define INTROSPECTION_ENUM_PREFIX MIPS
34 #include "define_introspection_and_hwcaps.inl"
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 // Implementation.
38 ////////////////////////////////////////////////////////////////////////////////
39 
40 #include "internal/filesystem.h"
41 #include "internal/hwcaps.h"
42 #include "internal/stack_line_reader.h"
43 #include "internal/string_view.h"
44 
HandleMipsLine(const LineResult result,MipsFeatures * const features)45 static bool HandleMipsLine(const LineResult result,
46                            MipsFeatures* const features) {
47   StringView key, value;
48   // See tests for an example.
49   if (CpuFeatures_StringView_GetAttributeKeyValue(result.line, &key, &value)) {
50     if (CpuFeatures_StringView_IsEquals(key, str("ASEs implemented"))) {
51       for (size_t i = 0; i < MIPS_LAST_; ++i) {
52         kSetters[i](features, CpuFeatures_StringView_HasWord(
53                                   value, kCpuInfoFlags[i], ' '));
54       }
55     }
56   }
57   return !result.eof;
58 }
59 
FillProcCpuInfoData(MipsFeatures * const features)60 static void FillProcCpuInfoData(MipsFeatures* const features) {
61   const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
62   if (fd >= 0) {
63     StackLineReader reader;
64     StackLineReader_Initialize(&reader, fd);
65     for (;;) {
66       if (!HandleMipsLine(StackLineReader_NextLine(&reader), features)) {
67         break;
68       }
69     }
70     CpuFeatures_CloseFile(fd);
71   }
72 }
73 
74 static const MipsInfo kEmptyMipsInfo;
75 
GetMipsInfo(void)76 MipsInfo GetMipsInfo(void) {
77   // capabilities are fetched from both getauxval and /proc/cpuinfo so we can
78   // have some information if the executable is sandboxed (aka no access to
79   // /proc/cpuinfo).
80   MipsInfo info = kEmptyMipsInfo;
81 
82   FillProcCpuInfoData(&info.features);
83   const HardwareCapabilities hwcaps = CpuFeatures_GetHardwareCapabilities();
84   for (size_t i = 0; i < MIPS_LAST_; ++i) {
85     if (CpuFeatures_IsHwCapsSet(kHardwareCapabilities[i], hwcaps)) {
86       kSetters[i](&info.features, true);
87     }
88   }
89   return info;
90 }
91 
92 #endif  //  defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)
93 #endif  // CPU_FEATURES_ARCH_MIPS
94