1 // Copyright 2017 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "cpuinfo_mips.h"
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"
22 DECLARE_SETTER(MipsFeatures, msa)
23 DECLARE_SETTER(MipsFeatures, eva)
25 static const CapabilityConfig kConfigs[] = {
26 {{MIPS_HWCAP_MSA, 0}, "msa", &set_msa}, //
27 {{MIPS_HWCAP_EVA, 0}, "eva", &set_eva}, //
29 static const size_t kConfigsSize = sizeof(kConfigs) / sizeof(CapabilityConfig);
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);
43 static void FillProcCpuInfoData(MipsFeatures* const features) {
44 const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
46 StackLineReader reader;
47 StackLineReader_Initialize(&reader, fd);
49 if (!HandleMipsLine(StackLineReader_NextLine(&reader), features)) {
53 CpuFeatures_CloseFile(fd);
57 static const MipsInfo kEmptyMipsInfo;
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
63 MipsInfo info = kEmptyMipsInfo;
65 FillProcCpuInfoData(&info.features);
66 CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs,
67 CpuFeatures_GetHardwareCapabilities(),
72 ////////////////////////////////////////////////////////////////////////////////
73 // Introspection functions
75 int GetMipsFeaturesEnumValue(const MipsFeatures* features,
76 MipsFeaturesEnum value) {
88 const char* GetMipsFeaturesEnumName(MipsFeaturesEnum value) {
97 return "unknown feature";