Guillaume Chatelet | 3cc8f31 | 2020-10-12 08:55:20 +0000 | [diff] [blame] | 1 | // Copyright 2017 Google LLC |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 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 | |
Guillaume Chatelet | 22a5362 | 2020-09-23 11:52:20 +0200 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 19 | #include "internal/filesystem.h" |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame] | 20 | #include "internal/hwcaps.h" |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 21 | #include "internal/stack_line_reader.h" |
| 22 | #include "internal/string_view.h" |
| 23 | |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame] | 24 | // Generation of feature's getters/setters functions and kGetters, kSetters, |
| 25 | // kCpuInfoFlags and kHardwareCapabilities global tables. |
Guillaume Chatelet | cdab59a | 2020-10-13 13:05:04 +0200 | [diff] [blame] | 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) |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame] | 30 | #define DEFINE_TABLE_FEATURE_TYPE MipsFeatures |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame] | 31 | #include "define_tables.h" |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 32 | |
| 33 | static bool HandleMipsLine(const LineResult result, |
| 34 | MipsFeatures* const features) { |
| 35 | StringView key, value; |
| 36 | // See tests for an example. |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 37 | if (CpuFeatures_StringView_GetAttributeKeyValue(result.line, &key, &value)) { |
| 38 | if (CpuFeatures_StringView_IsEquals(key, str("ASEs implemented"))) { |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame] | 39 | for (size_t i = 0; i < MIPS_LAST_; ++i) { |
| 40 | kSetters[i](features, |
| 41 | CpuFeatures_StringView_HasWord(value, kCpuInfoFlags[i])); |
| 42 | } |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | return !result.eof; |
| 46 | } |
| 47 | |
| 48 | static void FillProcCpuInfoData(MipsFeatures* const features) { |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 49 | const int fd = CpuFeatures_OpenFile("/proc/cpuinfo"); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 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 | } |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 58 | CpuFeatures_CloseFile(fd); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | static const MipsInfo kEmptyMipsInfo; |
| 63 | |
| 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); |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame] | 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 | } |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 77 | return info; |
| 78 | } |
| 79 | |
| 80 | //////////////////////////////////////////////////////////////////////////////// |
| 81 | // Introspection functions |
| 82 | |
| 83 | int GetMipsFeaturesEnumValue(const MipsFeatures* features, |
| 84 | MipsFeaturesEnum value) { |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame] | 85 | if (value >= MIPS_LAST_) return false; |
| 86 | return kGetters[value](features); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | const char* GetMipsFeaturesEnumName(MipsFeaturesEnum value) { |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame] | 90 | if (value >= MIPS_LAST_) return "unknown feature"; |
| 91 | return kCpuInfoFlags[value]; |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 92 | } |