Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. |
| 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 <cassert> |
| 16 | #include <cstdio> |
| 17 | #include <map> |
| 18 | |
| 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | #include "cpuinfo_x86.h" |
| 22 | #include "internal/cpuid_x86.h" |
| 23 | |
| 24 | namespace cpu_features { |
| 25 | |
| 26 | class FakeCpu { |
| 27 | public: |
Artem Alekseev | bfb4cf9 | 2019-06-21 15:13:29 +0300 | [diff] [blame] | 28 | Leaf CpuIdEx(uint32_t leaf_id, int ecx) const { |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 29 | const auto itr = cpuid_leaves_.find(std::make_pair(leaf_id, ecx)); |
| 30 | if (itr != cpuid_leaves_.end()) { |
| 31 | return itr->second; |
| 32 | } |
| 33 | return {0, 0, 0, 0}; |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | uint32_t GetXCR0Eax() const { return xcr0_eax_; } |
| 37 | |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 38 | void SetLeaves(std::map<std::pair<uint32_t, int>, Leaf> configuration) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 39 | cpuid_leaves_ = std::move(configuration); |
| 40 | } |
| 41 | |
| 42 | void SetOsBackupsExtendedRegisters(bool os_backups_extended_registers) { |
| 43 | xcr0_eax_ = os_backups_extended_registers ? -1 : 0; |
| 44 | } |
| 45 | |
| 46 | private: |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 47 | std::map<std::pair<uint32_t, int>, Leaf> cpuid_leaves_; |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 48 | uint32_t xcr0_eax_; |
| 49 | }; |
| 50 | |
| 51 | auto* g_fake_cpu = new FakeCpu(); |
| 52 | |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 53 | extern "C" Leaf CpuIdEx(uint32_t leaf_id, int ecx) { |
| 54 | return g_fake_cpu->CpuIdEx(leaf_id, ecx); |
| 55 | } |
Artem Alekseev | bfb4cf9 | 2019-06-21 15:13:29 +0300 | [diff] [blame] | 56 | |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 57 | extern "C" uint32_t GetXCR0Eax(void) { return g_fake_cpu->GetXCR0Eax(); } |
| 58 | |
| 59 | namespace { |
| 60 | |
| 61 | TEST(CpuidX86Test, SandyBridge) { |
| 62 | g_fake_cpu->SetOsBackupsExtendedRegisters(true); |
| 63 | g_fake_cpu->SetLeaves({ |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 64 | {{0x00000000, 0}, Leaf{0x0000000D, 0x756E6547, 0x6C65746E, 0x49656E69}}, |
| 65 | {{0x00000001, 0}, Leaf{0x000206A6, 0x00100800, 0x1F9AE3BF, 0xBFEBFBFF}}, |
| 66 | {{0x00000007, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}}, |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 67 | }); |
| 68 | const auto info = GetX86Info(); |
| 69 | EXPECT_STREQ(info.vendor, "GenuineIntel"); |
| 70 | EXPECT_EQ(info.family, 0x06); |
| 71 | EXPECT_EQ(info.model, 0x02A); |
| 72 | EXPECT_EQ(info.stepping, 0x06); |
| 73 | // Leaf 7 is zeroed out so none of the Leaf 7 flags are set. |
| 74 | const auto features = info.features; |
| 75 | EXPECT_FALSE(features.erms); |
| 76 | EXPECT_FALSE(features.avx2); |
| 77 | EXPECT_FALSE(features.avx512f); |
| 78 | EXPECT_FALSE(features.avx512cd); |
| 79 | EXPECT_FALSE(features.avx512er); |
| 80 | EXPECT_FALSE(features.avx512pf); |
| 81 | EXPECT_FALSE(features.avx512bw); |
| 82 | EXPECT_FALSE(features.avx512dq); |
| 83 | EXPECT_FALSE(features.avx512vl); |
| 84 | EXPECT_FALSE(features.avx512ifma); |
| 85 | EXPECT_FALSE(features.avx512vbmi); |
| 86 | EXPECT_FALSE(features.avx512vbmi2); |
| 87 | EXPECT_FALSE(features.avx512vnni); |
| 88 | EXPECT_FALSE(features.avx512bitalg); |
| 89 | EXPECT_FALSE(features.avx512vpopcntdq); |
| 90 | EXPECT_FALSE(features.avx512_4vnniw); |
| 91 | EXPECT_FALSE(features.avx512_4vbmi2); |
| 92 | // All old cpu features should be set. |
| 93 | EXPECT_TRUE(features.aes); |
| 94 | EXPECT_TRUE(features.ssse3); |
| 95 | EXPECT_TRUE(features.sse4_1); |
| 96 | EXPECT_TRUE(features.sse4_2); |
| 97 | EXPECT_TRUE(features.avx); |
Guillaume Chatelet | d395dfa | 2019-01-22 13:19:42 +0100 | [diff] [blame] | 98 | EXPECT_FALSE(features.sha); |
| 99 | EXPECT_TRUE(features.popcnt); |
| 100 | EXPECT_FALSE(features.movbe); |
| 101 | EXPECT_FALSE(features.rdrnd); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 104 | const int KiB = 1024; |
| 105 | const int MiB = 1024 * KiB; |
| 106 | |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 107 | TEST(CpuidX86Test, SandyBridgeTestOsSupport) { |
| 108 | g_fake_cpu->SetLeaves({ |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 109 | {{0x00000000, 0}, Leaf{0x0000000D, 0x756E6547, 0x6C65746E, 0x49656E69}}, |
| 110 | {{0x00000001, 0}, Leaf{0x000206A6, 0x00100800, 0x1F9AE3BF, 0xBFEBFBFF}}, |
| 111 | {{0x00000007, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}}, |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 112 | }); |
| 113 | // avx is disabled if os does not support backing up ymm registers. |
| 114 | g_fake_cpu->SetOsBackupsExtendedRegisters(false); |
| 115 | EXPECT_FALSE(GetX86Info().features.avx); |
| 116 | // avx is disabled if os does not support backing up ymm registers. |
| 117 | g_fake_cpu->SetOsBackupsExtendedRegisters(true); |
| 118 | EXPECT_TRUE(GetX86Info().features.avx); |
| 119 | } |
| 120 | |
| 121 | TEST(CpuidX86Test, SkyLake) { |
| 122 | g_fake_cpu->SetOsBackupsExtendedRegisters(true); |
| 123 | g_fake_cpu->SetLeaves({ |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 124 | {{0x00000000, 0}, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}}, |
| 125 | {{0x00000001, 0}, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}}, |
| 126 | {{0x00000007, 0}, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}}, |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 127 | }); |
| 128 | const auto info = GetX86Info(); |
| 129 | EXPECT_STREQ(info.vendor, "GenuineIntel"); |
| 130 | EXPECT_EQ(info.family, 0x06); |
| 131 | EXPECT_EQ(info.model, 0x04E); |
| 132 | EXPECT_EQ(info.stepping, 0x03); |
| 133 | EXPECT_EQ(GetX86Microarchitecture(&info), X86Microarchitecture::INTEL_SKL); |
| 134 | } |
| 135 | |
| 136 | TEST(CpuidX86Test, Branding) { |
| 137 | g_fake_cpu->SetLeaves({ |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 138 | {{0x00000000, 0}, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}}, |
| 139 | {{0x00000001, 0}, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}}, |
| 140 | {{0x00000007, 0}, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}}, |
| 141 | {{0x80000000, 0}, Leaf{0x80000008, 0x00000000, 0x00000000, 0x00000000}}, |
| 142 | {{0x80000001, 0}, Leaf{0x00000000, 0x00000000, 0x00000121, 0x2C100000}}, |
| 143 | {{0x80000002, 0}, Leaf{0x65746E49, 0x2952286C, 0x726F4320, 0x4D542865}}, |
| 144 | {{0x80000003, 0}, Leaf{0x37692029, 0x3035362D, 0x43205530, 0x40205550}}, |
| 145 | {{0x80000004, 0}, Leaf{0x352E3220, 0x7A484730, 0x00000000, 0x00000000}}, |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 146 | }); |
| 147 | char brand_string[49]; |
| 148 | FillX86BrandString(brand_string); |
| 149 | EXPECT_STREQ(brand_string, "Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz"); |
| 150 | } |
| 151 | |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 152 | TEST(CpuidX86Test, KabyLakeCache) { |
| 153 | g_fake_cpu->SetLeaves({ |
| 154 | {{0x00000000, 0}, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}}, |
| 155 | {{0x00000001, 0}, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}}, |
| 156 | {{0x00000004, 0}, Leaf{0x1C004121, 0x01C0003F, 0x0000003F, 0x00000000}}, |
| 157 | {{0x00000004, 1}, Leaf{0x1C004122, 0x01C0003F, 0x0000003F, 0x00000000}}, |
| 158 | {{0x00000004, 2}, Leaf{0x1C004143, 0x00C0003F, 0x000003FF, 0x00000000}}, |
| 159 | {{0x00000004, 3}, Leaf{0x1C03C163, 0x02C0003F, 0x00001FFF, 0x00000002}}, |
| 160 | {{0x00000007, 0}, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}}, |
| 161 | {{0x80000000, 0}, Leaf{0x80000008, 0x00000000, 0x00000000, 0x00000000}}, |
| 162 | {{0x80000001, 0}, Leaf{0x00000000, 0x00000000, 0x00000121, 0x2C100000}}, |
| 163 | {{0x80000002, 0}, Leaf{0x65746E49, 0x2952286C, 0x726F4320, 0x4D542865}}, |
| 164 | {{0x80000003, 0}, Leaf{0x37692029, 0x3035362D, 0x43205530, 0x40205550}}, |
| 165 | }); |
| 166 | const auto info = GetX86CacheInfo(); |
| 167 | EXPECT_EQ(info.size, 4); |
| 168 | EXPECT_EQ(info.levels[0].level, 1); |
| 169 | EXPECT_EQ(info.levels[0].cache_type, 1); |
| 170 | EXPECT_EQ(info.levels[0].cache_size, 32 * KiB); |
| 171 | EXPECT_EQ(info.levels[0].ways, 8); |
| 172 | EXPECT_EQ(info.levels[0].line_size, 64); |
| 173 | EXPECT_EQ(info.levels[0].tlb_entries, 64); |
| 174 | EXPECT_EQ(info.levels[0].partitioning, 1); |
| 175 | |
| 176 | EXPECT_EQ(info.levels[1].level, 1); |
| 177 | EXPECT_EQ(info.levels[1].cache_type, 2); |
| 178 | EXPECT_EQ(info.levels[1].cache_size, 32 * KiB); |
| 179 | EXPECT_EQ(info.levels[1].ways, 8); |
| 180 | EXPECT_EQ(info.levels[1].line_size, 64); |
| 181 | EXPECT_EQ(info.levels[1].tlb_entries, 64); |
| 182 | EXPECT_EQ(info.levels[1].partitioning, 1); |
| 183 | |
| 184 | EXPECT_EQ(info.levels[2].level, 2); |
| 185 | EXPECT_EQ(info.levels[2].cache_type, 3); |
| 186 | EXPECT_EQ(info.levels[2].cache_size, 256 * KiB); |
| 187 | EXPECT_EQ(info.levels[2].ways, 4); |
| 188 | EXPECT_EQ(info.levels[2].line_size, 64); |
| 189 | EXPECT_EQ(info.levels[2].tlb_entries, 1024); |
| 190 | EXPECT_EQ(info.levels[2].partitioning, 1); |
| 191 | |
| 192 | EXPECT_EQ(info.levels[3].level, 3); |
| 193 | EXPECT_EQ(info.levels[3].cache_type, 3); |
| 194 | EXPECT_EQ(info.levels[3].cache_size, 6 * MiB); |
| 195 | EXPECT_EQ(info.levels[3].ways, 12); |
| 196 | EXPECT_EQ(info.levels[3].line_size, 64); |
| 197 | EXPECT_EQ(info.levels[3].tlb_entries, 8192); |
| 198 | EXPECT_EQ(info.levels[3].partitioning, 1); |
| 199 | } |
| 200 | |
| 201 | TEST(CpuidX86Test, HSWCache) { |
| 202 | g_fake_cpu->SetLeaves({ |
| 203 | {{0x00000000, 0}, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}}, |
| 204 | {{0x00000001, 0}, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}}, |
| 205 | {{0x00000004, 0}, Leaf{0x1C004121, 0x01C0003F, 0x0000003F, 0x00000000}}, |
| 206 | {{0x00000004, 1}, Leaf{0x1C004122, 0x01C0003F, 0x0000003F, 0x00000000}}, |
| 207 | {{0x00000004, 2}, Leaf{0x1C004143, 0x01C0003F, 0x000001FF, 0x00000000}}, |
| 208 | {{0x00000004, 3}, Leaf{0x1C03C163, 0x02C0003F, 0x00001FFF, 0x00000006}}, |
| 209 | {{0x00000007, 0}, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}}, |
| 210 | {{0x80000000, 0}, Leaf{0x80000008, 0x00000000, 0x00000000, 0x00000000}}, |
| 211 | {{0x80000001, 0}, Leaf{0x00000000, 0x00000000, 0x00000121, 0x2C100000}}, |
| 212 | {{0x80000002, 0}, Leaf{0x65746E49, 0x2952286C, 0x726F4320, 0x4D542865}}, |
| 213 | {{0x80000003, 0}, Leaf{0x37692029, 0x3035362D, 0x43205530, 0x40205550}}, |
| 214 | }); |
| 215 | const auto info = GetX86CacheInfo(); |
| 216 | EXPECT_EQ(info.size, 4); |
| 217 | EXPECT_EQ(info.levels[0].level, 1); |
| 218 | EXPECT_EQ(info.levels[0].cache_type, 1); |
| 219 | EXPECT_EQ(info.levels[0].cache_size, 32 * KiB); |
| 220 | EXPECT_EQ(info.levels[0].ways, 8); |
| 221 | EXPECT_EQ(info.levels[0].line_size, 64); |
| 222 | EXPECT_EQ(info.levels[0].tlb_entries, 64); |
| 223 | EXPECT_EQ(info.levels[0].partitioning, 1); |
| 224 | |
| 225 | EXPECT_EQ(info.levels[1].level, 1); |
| 226 | EXPECT_EQ(info.levels[1].cache_type, 2); |
| 227 | EXPECT_EQ(info.levels[1].cache_size, 32 * KiB); |
| 228 | EXPECT_EQ(info.levels[1].ways, 8); |
| 229 | EXPECT_EQ(info.levels[1].line_size, 64); |
| 230 | EXPECT_EQ(info.levels[1].tlb_entries, 64); |
| 231 | EXPECT_EQ(info.levels[1].partitioning, 1); |
| 232 | |
| 233 | EXPECT_EQ(info.levels[2].level, 2); |
| 234 | EXPECT_EQ(info.levels[2].cache_type, 3); |
| 235 | EXPECT_EQ(info.levels[2].cache_size, 256 * KiB); |
| 236 | EXPECT_EQ(info.levels[2].ways, 8); |
| 237 | EXPECT_EQ(info.levels[2].line_size, 64); |
| 238 | EXPECT_EQ(info.levels[2].tlb_entries, 512); |
| 239 | EXPECT_EQ(info.levels[2].partitioning, 1); |
| 240 | |
| 241 | EXPECT_EQ(info.levels[3].level, 3); |
| 242 | EXPECT_EQ(info.levels[3].cache_type, 3); |
| 243 | EXPECT_EQ(info.levels[3].cache_size, 6 * MiB); |
| 244 | EXPECT_EQ(info.levels[3].ways, 12); |
| 245 | EXPECT_EQ(info.levels[3].line_size, 64); |
| 246 | EXPECT_EQ(info.levels[3].tlb_entries, 8192); |
| 247 | EXPECT_EQ(info.levels[3].partitioning, 1); |
| 248 | } |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 249 | // http://users.atw.hu/instlatx64/AuthenticAMD0630F81_K15_Godavari_CPUID.txt |
| 250 | TEST(CpuidX86Test, AMD_K15) { |
| 251 | g_fake_cpu->SetLeaves({ |
Artem Alekseev | 653d581 | 2019-07-02 17:52:25 +0300 | [diff] [blame] | 252 | {{0x00000000, 0}, Leaf{0x0000000D, 0x68747541, 0x444D4163, 0x69746E65}}, |
| 253 | {{0x00000001, 0}, Leaf{0x00630F81, 0x00040800, 0x3E98320B, 0x178BFBFF}}, |
| 254 | {{0x00000007, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}}, |
| 255 | {{0x80000000, 0}, Leaf{0x8000001E, 0x68747541, 0x444D4163, 0x69746E65}}, |
| 256 | {{0x80000001, 0}, Leaf{0x00630F81, 0x10000000, 0x0FEBBFFF, 0x2FD3FBFF}}, |
| 257 | {{0x80000002, 0}, Leaf{0x20444D41, 0x372D3841, 0x4B303736, 0x64615220}}, |
| 258 | {{0x80000003, 0}, Leaf{0x206E6F65, 0x202C3752, 0x43203031, 0x75706D6F}}, |
| 259 | {{0x80000004, 0}, Leaf{0x43206574, 0x7365726F, 0x2B433420, 0x00204736}}, |
| 260 | {{0x80000005, 0}, Leaf{0xFF40FF18, 0xFF40FF30, 0x10040140, 0x60030140}}, |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 261 | }); |
| 262 | const auto info = GetX86Info(); |
| 263 | |
| 264 | EXPECT_STREQ(info.vendor, "AuthenticAMD"); |
| 265 | EXPECT_EQ(info.family, 0x15); |
| 266 | EXPECT_EQ(info.model, 0x38); |
| 267 | EXPECT_EQ(info.stepping, 0x01); |
| 268 | EXPECT_EQ(GetX86Microarchitecture(&info), |
| 269 | X86Microarchitecture::AMD_BULLDOZER); |
| 270 | |
| 271 | char brand_string[49]; |
| 272 | FillX86BrandString(brand_string); |
| 273 | EXPECT_STREQ(brand_string, "AMD A8-7670K Radeon R7, 10 Compute Cores 4C+6G "); |
| 274 | } |
| 275 | |
| 276 | // TODO(user): test what happens when xsave/osxsave are not present. |
| 277 | // TODO(user): test what happens when xmm/ymm/zmm os support are not |
| 278 | // present. |
| 279 | |
| 280 | } // namespace |
| 281 | } // namespace cpu_features |