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_arm.h" |
| 16 | |
Guillaume Chatelet | 22a5362 | 2020-09-23 11:52:20 +0200 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <ctype.h> |
| 19 | |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 20 | #include "internal/bit_utils.h" |
| 21 | #include "internal/filesystem.h" |
| 22 | #include "internal/hwcaps.h" |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 23 | #include "internal/stack_line_reader.h" |
| 24 | #include "internal/string_view.h" |
| 25 | |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame^] | 26 | // Generation of feature's getters/setters functions and kGetters, kSetters, |
| 27 | // kCpuInfoFlags and kHardwareCapabilities global tables. |
| 28 | #define DEFINE_TABLE_FEATURE_TYPE ArmFeatures |
| 29 | #define DEFINE_TABLE_DB_FILENAME "cpuinfo_arm_db.inl" |
| 30 | #include "define_tables.h" |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 31 | |
| 32 | typedef struct { |
| 33 | bool processor_reports_armv6; |
| 34 | bool hardware_reports_goldfish; |
| 35 | } ProcCpuInfoData; |
| 36 | |
| 37 | static int IndexOfNonDigit(StringView str) { |
| 38 | size_t index = 0; |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 39 | while (str.size && isdigit(CpuFeatures_StringView_Front(str))) { |
| 40 | str = CpuFeatures_StringView_PopFront(str, 1); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 41 | ++index; |
| 42 | } |
| 43 | return index; |
| 44 | } |
| 45 | |
| 46 | static bool HandleArmLine(const LineResult result, ArmInfo* const info, |
| 47 | ProcCpuInfoData* const proc_info) { |
| 48 | StringView line = result.line; |
| 49 | StringView key, value; |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 50 | if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) { |
| 51 | if (CpuFeatures_StringView_IsEquals(key, str("Features"))) { |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame^] | 52 | for (size_t i = 0; i < ARM_LAST_; ++i) { |
| 53 | kSetters[i](&info->features, |
| 54 | CpuFeatures_StringView_HasWord(value, kCpuInfoFlags[i])); |
| 55 | } |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 56 | } else if (CpuFeatures_StringView_IsEquals(key, str("CPU implementer"))) { |
| 57 | info->implementer = CpuFeatures_StringView_ParsePositiveNumber(value); |
| 58 | } else if (CpuFeatures_StringView_IsEquals(key, str("CPU variant"))) { |
| 59 | info->variant = CpuFeatures_StringView_ParsePositiveNumber(value); |
| 60 | } else if (CpuFeatures_StringView_IsEquals(key, str("CPU part"))) { |
| 61 | info->part = CpuFeatures_StringView_ParsePositiveNumber(value); |
| 62 | } else if (CpuFeatures_StringView_IsEquals(key, str("CPU revision"))) { |
| 63 | info->revision = CpuFeatures_StringView_ParsePositiveNumber(value); |
| 64 | } else if (CpuFeatures_StringView_IsEquals(key, str("CPU architecture"))) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 65 | // CPU architecture is a number that may be followed by letters. e.g. |
| 66 | // "6TEJ", "7". |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 67 | const StringView digits = |
| 68 | CpuFeatures_StringView_KeepFront(value, IndexOfNonDigit(value)); |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 69 | info->architecture = CpuFeatures_StringView_ParsePositiveNumber(digits); |
Guillaume Chatelet | 22a5362 | 2020-09-23 11:52:20 +0200 | [diff] [blame] | 70 | } else if (CpuFeatures_StringView_IsEquals(key, str("Processor")) || |
| 71 | CpuFeatures_StringView_IsEquals(key, str("model name"))) { |
Dr.-Ing. Patrick Siegl | 1834278 | 2019-06-25 17:58:31 +0200 | [diff] [blame] | 72 | // Android reports this in a non-Linux standard "Processor" but sometimes |
| 73 | // also in "model name", Linux reports it only in "model name" |
| 74 | // see RaspberryPiZero (Linux) vs InvalidArmv7 (Android) test-cases |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 75 | proc_info->processor_reports_armv6 = |
| 76 | CpuFeatures_StringView_IndexOf(value, str("(v6l)")) >= 0; |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 77 | } else if (CpuFeatures_StringView_IsEquals(key, str("Hardware"))) { |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 78 | proc_info->hardware_reports_goldfish = |
| 79 | CpuFeatures_StringView_IsEquals(value, str("Goldfish")); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | return !result.eof; |
| 83 | } |
| 84 | |
Guillaume Chatelet | 918553a | 2019-01-17 15:28:04 +0100 | [diff] [blame] | 85 | uint32_t GetArmCpuId(const ArmInfo* const info) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 86 | return (ExtractBitRange(info->implementer, 7, 0) << 24) | |
| 87 | (ExtractBitRange(info->variant, 3, 0) << 20) | |
| 88 | (ExtractBitRange(info->part, 11, 0) << 4) | |
| 89 | (ExtractBitRange(info->revision, 3, 0) << 0); |
| 90 | } |
| 91 | |
| 92 | static void FixErrors(ArmInfo* const info, |
| 93 | ProcCpuInfoData* const proc_cpu_info_data) { |
| 94 | // Fixing Samsung kernel reporting invalid cpu architecture. |
| 95 | // http://code.google.com/p/android/issues/detail?id=10812 |
| 96 | if (proc_cpu_info_data->processor_reports_armv6 && info->architecture >= 7) { |
| 97 | info->architecture = 6; |
| 98 | } |
| 99 | |
| 100 | // Handle kernel configuration bugs that prevent the correct reporting of CPU |
| 101 | // features. |
Guillaume Chatelet | 918553a | 2019-01-17 15:28:04 +0100 | [diff] [blame] | 102 | switch (GetArmCpuId(info)) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 103 | case 0x4100C080: |
| 104 | // Special case: The emulator-specific Android 4.2 kernel fails to report |
| 105 | // support for the 32-bit ARM IDIV instruction. Technically, this is a |
| 106 | // feature of the virtual CPU implemented by the emulator. Note that it |
| 107 | // could also support Thumb IDIV in the future, and this will have to be |
| 108 | // slightly updated. |
| 109 | if (info->architecture >= 7 && |
| 110 | proc_cpu_info_data->hardware_reports_goldfish) { |
| 111 | info->features.idiva = true; |
| 112 | } |
| 113 | break; |
| 114 | case 0x511004D0: |
| 115 | // https://crbug.com/341598. |
| 116 | info->features.neon = false; |
| 117 | break; |
| 118 | case 0x510006F2: |
| 119 | case 0x510006F3: |
| 120 | // The Nexus 4 (Qualcomm Krait) kernel configuration forgets to report |
| 121 | // IDIV support. |
| 122 | info->features.idiva = true; |
| 123 | info->features.idivt = true; |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | // Propagate cpu features. |
| 128 | if (info->features.vfpv4) info->features.vfpv3 = true; |
| 129 | if (info->features.neon) info->features.vfpv3 = true; |
| 130 | if (info->features.vfpv3) info->features.vfp = true; |
| 131 | } |
| 132 | |
| 133 | static void FillProcCpuInfoData(ArmInfo* const info, |
| 134 | ProcCpuInfoData* proc_cpu_info_data) { |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 135 | const int fd = CpuFeatures_OpenFile("/proc/cpuinfo"); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 136 | if (fd >= 0) { |
| 137 | StackLineReader reader; |
| 138 | StackLineReader_Initialize(&reader, fd); |
| 139 | for (;;) { |
| 140 | if (!HandleArmLine(StackLineReader_NextLine(&reader), info, |
| 141 | proc_cpu_info_data)) { |
| 142 | break; |
| 143 | } |
| 144 | } |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 145 | CpuFeatures_CloseFile(fd); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | static const ArmInfo kEmptyArmInfo; |
| 150 | |
| 151 | static const ProcCpuInfoData kEmptyProcCpuInfoData; |
| 152 | |
| 153 | ArmInfo GetArmInfo(void) { |
| 154 | // capabilities are fetched from both getauxval and /proc/cpuinfo so we can |
| 155 | // have some information if the executable is sandboxed (aka no access to |
| 156 | // /proc/cpuinfo). |
| 157 | ArmInfo info = kEmptyArmInfo; |
| 158 | ProcCpuInfoData proc_cpu_info_data = kEmptyProcCpuInfoData; |
| 159 | |
| 160 | FillProcCpuInfoData(&info, &proc_cpu_info_data); |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame^] | 161 | const HardwareCapabilities hwcaps = CpuFeatures_GetHardwareCapabilities(); |
| 162 | for (size_t i = 0; i < ARM_LAST_; ++i) { |
| 163 | if (CpuFeatures_IsHwCapsSet(kHardwareCapabilities[i], hwcaps)) { |
| 164 | kSetters[i](&info.features, true); |
| 165 | } |
| 166 | } |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 167 | |
| 168 | FixErrors(&info, &proc_cpu_info_data); |
| 169 | |
| 170 | return info; |
| 171 | } |
| 172 | |
| 173 | //////////////////////////////////////////////////////////////////////////////// |
| 174 | // Introspection functions |
| 175 | |
| 176 | int GetArmFeaturesEnumValue(const ArmFeatures* features, |
| 177 | ArmFeaturesEnum value) { |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame^] | 178 | if (value >= ARM_LAST_) return false; |
| 179 | return kGetters[value](features); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | const char* GetArmFeaturesEnumName(ArmFeaturesEnum value) { |
Guillaume Chatelet | 9a8f04b | 2020-10-12 11:50:35 +0200 | [diff] [blame^] | 183 | if (value >= ARM_LAST_) return "unknown feature"; |
| 184 | return kCpuInfoFlags[value]; |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 185 | } |