blob: 636d0f9a8043a0bb4b4aac55c223f4fa74c6a8c6 [file] [log] [blame]
Guillaume Chatelet3cc8f312020-10-12 08:55:20 +00001// Copyright 2017 Google LLC
Guillaume Chatelet439d3712018-02-01 10:03:09 +01002//
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
Guillaume Chatelet22a53622020-09-23 11:52:20 +020015#include "cpuinfo_x86.h"
16
Guillaume Chatelet439d3712018-02-01 10:03:09 +010017#include <cassert>
18#include <cstdio>
19#include <map>
Guillaume Chatelet47953732020-10-09 17:20:25 +020020#include <set>
21#if defined(CPU_FEATURES_OS_WINDOWS)
22#include <windows.h> // IsProcessorFeaturePresent
23#endif // CPU_FEATURES_OS_WINDOWS
Guillaume Chatelet439d3712018-02-01 10:03:09 +010024
Guillaume Chatelet47953732020-10-09 17:20:25 +020025#include "filesystem_for_testing.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010026#include "gtest/gtest.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010027#include "internal/cpuid_x86.h"
28
29namespace cpu_features {
30
31class FakeCpu {
32 public:
Guillaume Chatelet47953732020-10-09 17:20:25 +020033 Leaf GetCpuidLeaf(uint32_t leaf_id, int ecx) const {
Artem Alekseev653d5812019-07-02 17:52:25 +030034 const auto itr = cpuid_leaves_.find(std::make_pair(leaf_id, ecx));
35 if (itr != cpuid_leaves_.end()) {
36 return itr->second;
37 }
38 return {0, 0, 0, 0};
Guillaume Chatelet439d3712018-02-01 10:03:09 +010039 }
40
41 uint32_t GetXCR0Eax() const { return xcr0_eax_; }
42
Artem Alekseev653d5812019-07-02 17:52:25 +030043 void SetLeaves(std::map<std::pair<uint32_t, int>, Leaf> configuration) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +010044 cpuid_leaves_ = std::move(configuration);
45 }
46
47 void SetOsBackupsExtendedRegisters(bool os_backups_extended_registers) {
48 xcr0_eax_ = os_backups_extended_registers ? -1 : 0;
49 }
50
Guillaume Chatelet47953732020-10-09 17:20:25 +020051#if defined(CPU_FEATURES_OS_DARWIN)
52 bool GetDarwinSysCtlByName(std::string name) const {
53 return darwin_sysctlbyname_.count(name);
54 }
55
56 void SetDarwinSysCtlByName(std::string name) {
57 darwin_sysctlbyname_.insert(name);
58 }
59#endif // CPU_FEATURES_OS_DARWIN
60
61#if defined(CPU_FEATURES_OS_WINDOWS)
62 bool GetWindowsIsProcessorFeaturePresent(DWORD ProcessorFeature) {
63 return windows_isprocessorfeaturepresent_.count(ProcessorFeature);
64 }
65
66 void SetWindowsIsProcessorFeaturePresent(DWORD ProcessorFeature) {
67 windows_isprocessorfeaturepresent_.insert(ProcessorFeature);
68 }
69#endif // CPU_FEATURES_OS_WINDOWS
70
Guillaume Chatelet439d3712018-02-01 10:03:09 +010071 private:
Artem Alekseev653d5812019-07-02 17:52:25 +030072 std::map<std::pair<uint32_t, int>, Leaf> cpuid_leaves_;
Guillaume Chatelet47953732020-10-09 17:20:25 +020073#if defined(CPU_FEATURES_OS_DARWIN)
74 std::set<std::string> darwin_sysctlbyname_;
75#endif // CPU_FEATURES_OS_DARWIN
76#if defined(CPU_FEATURES_OS_WINDOWS)
77 std::set<DWORD> windows_isprocessorfeaturepresent_;
78#endif // CPU_FEATURES_OS_WINDOWS
Guillaume Chatelet439d3712018-02-01 10:03:09 +010079 uint32_t xcr0_eax_;
80};
81
Guillaume Chatelet47953732020-10-09 17:20:25 +020082FakeCpu* g_fake_cpu = nullptr;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010083
Guillaume Chatelet47953732020-10-09 17:20:25 +020084extern "C" Leaf GetCpuidLeaf(uint32_t leaf_id, int ecx) {
85 return g_fake_cpu->GetCpuidLeaf(leaf_id, ecx);
Artem Alekseev653d5812019-07-02 17:52:25 +030086}
Artem Alekseevbfb4cf92019-06-21 15:13:29 +030087
Guillaume Chatelet439d3712018-02-01 10:03:09 +010088extern "C" uint32_t GetXCR0Eax(void) { return g_fake_cpu->GetXCR0Eax(); }
89
Guillaume Chatelet47953732020-10-09 17:20:25 +020090#if defined(CPU_FEATURES_OS_DARWIN)
91extern "C" bool GetDarwinSysCtlByName(const char* name) {
92 return g_fake_cpu->GetDarwinSysCtlByName(name);
93}
94#endif // CPU_FEATURES_OS_DARWIN
95
96#if defined(CPU_FEATURES_OS_WINDOWS)
97extern "C" bool GetWindowsIsProcessorFeaturePresent(DWORD ProcessorFeature) {
98 return g_fake_cpu->GetWindowsIsProcessorFeaturePresent(ProcessorFeature);
99}
100#endif // CPU_FEATURES_OS_WINDOWS
101
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100102namespace {
103
Guillaume Chatelet47953732020-10-09 17:20:25 +0200104class CpuidX86Test : public ::testing::Test {
105 protected:
106 void SetUp() override { g_fake_cpu = new FakeCpu(); }
107 void TearDown() override { delete g_fake_cpu; }
108};
109
110TEST_F(CpuidX86Test, SandyBridge) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100111 g_fake_cpu->SetOsBackupsExtendedRegisters(true);
112 g_fake_cpu->SetLeaves({
Artem Alekseev653d5812019-07-02 17:52:25 +0300113 {{0x00000000, 0}, Leaf{0x0000000D, 0x756E6547, 0x6C65746E, 0x49656E69}},
114 {{0x00000001, 0}, Leaf{0x000206A6, 0x00100800, 0x1F9AE3BF, 0xBFEBFBFF}},
115 {{0x00000007, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100116 });
117 const auto info = GetX86Info();
118 EXPECT_STREQ(info.vendor, "GenuineIntel");
119 EXPECT_EQ(info.family, 0x06);
120 EXPECT_EQ(info.model, 0x02A);
121 EXPECT_EQ(info.stepping, 0x06);
122 // Leaf 7 is zeroed out so none of the Leaf 7 flags are set.
123 const auto features = info.features;
124 EXPECT_FALSE(features.erms);
125 EXPECT_FALSE(features.avx2);
126 EXPECT_FALSE(features.avx512f);
127 EXPECT_FALSE(features.avx512cd);
128 EXPECT_FALSE(features.avx512er);
129 EXPECT_FALSE(features.avx512pf);
130 EXPECT_FALSE(features.avx512bw);
131 EXPECT_FALSE(features.avx512dq);
132 EXPECT_FALSE(features.avx512vl);
133 EXPECT_FALSE(features.avx512ifma);
134 EXPECT_FALSE(features.avx512vbmi);
135 EXPECT_FALSE(features.avx512vbmi2);
136 EXPECT_FALSE(features.avx512vnni);
137 EXPECT_FALSE(features.avx512bitalg);
138 EXPECT_FALSE(features.avx512vpopcntdq);
139 EXPECT_FALSE(features.avx512_4vnniw);
Jeff Hammond33bd72c2020-09-21 00:56:26 -0700140 EXPECT_FALSE(features.avx512_4fmaps);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100141 // All old cpu features should be set.
142 EXPECT_TRUE(features.aes);
143 EXPECT_TRUE(features.ssse3);
144 EXPECT_TRUE(features.sse4_1);
145 EXPECT_TRUE(features.sse4_2);
146 EXPECT_TRUE(features.avx);
Guillaume Chateletd395dfa2019-01-22 13:19:42 +0100147 EXPECT_FALSE(features.sha);
148 EXPECT_TRUE(features.popcnt);
149 EXPECT_FALSE(features.movbe);
150 EXPECT_FALSE(features.rdrnd);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100151}
152
Artem Alekseev653d5812019-07-02 17:52:25 +0300153const int KiB = 1024;
154const int MiB = 1024 * KiB;
155
Guillaume Chatelet47953732020-10-09 17:20:25 +0200156TEST_F(CpuidX86Test, SandyBridgeTestOsSupport) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100157 g_fake_cpu->SetLeaves({
Artem Alekseev653d5812019-07-02 17:52:25 +0300158 {{0x00000000, 0}, Leaf{0x0000000D, 0x756E6547, 0x6C65746E, 0x49656E69}},
159 {{0x00000001, 0}, Leaf{0x000206A6, 0x00100800, 0x1F9AE3BF, 0xBFEBFBFF}},
160 {{0x00000007, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100161 });
162 // avx is disabled if os does not support backing up ymm registers.
163 g_fake_cpu->SetOsBackupsExtendedRegisters(false);
164 EXPECT_FALSE(GetX86Info().features.avx);
165 // avx is disabled if os does not support backing up ymm registers.
166 g_fake_cpu->SetOsBackupsExtendedRegisters(true);
167 EXPECT_TRUE(GetX86Info().features.avx);
168}
169
Guillaume Chatelet47953732020-10-09 17:20:25 +0200170TEST_F(CpuidX86Test, SkyLake) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100171 g_fake_cpu->SetOsBackupsExtendedRegisters(true);
172 g_fake_cpu->SetLeaves({
Artem Alekseev653d5812019-07-02 17:52:25 +0300173 {{0x00000000, 0}, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}},
174 {{0x00000001, 0}, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}},
175 {{0x00000007, 0}, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}},
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100176 });
177 const auto info = GetX86Info();
178 EXPECT_STREQ(info.vendor, "GenuineIntel");
179 EXPECT_EQ(info.family, 0x06);
180 EXPECT_EQ(info.model, 0x04E);
181 EXPECT_EQ(info.stepping, 0x03);
182 EXPECT_EQ(GetX86Microarchitecture(&info), X86Microarchitecture::INTEL_SKL);
183}
184
Guillaume Chatelet47953732020-10-09 17:20:25 +0200185TEST_F(CpuidX86Test, Branding) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100186 g_fake_cpu->SetLeaves({
Artem Alekseev653d5812019-07-02 17:52:25 +0300187 {{0x00000000, 0}, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}},
188 {{0x00000001, 0}, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}},
189 {{0x00000007, 0}, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}},
190 {{0x80000000, 0}, Leaf{0x80000008, 0x00000000, 0x00000000, 0x00000000}},
191 {{0x80000001, 0}, Leaf{0x00000000, 0x00000000, 0x00000121, 0x2C100000}},
192 {{0x80000002, 0}, Leaf{0x65746E49, 0x2952286C, 0x726F4320, 0x4D542865}},
193 {{0x80000003, 0}, Leaf{0x37692029, 0x3035362D, 0x43205530, 0x40205550}},
194 {{0x80000004, 0}, Leaf{0x352E3220, 0x7A484730, 0x00000000, 0x00000000}},
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100195 });
196 char brand_string[49];
197 FillX86BrandString(brand_string);
198 EXPECT_STREQ(brand_string, "Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz");
199}
200
Guillaume Chatelet47953732020-10-09 17:20:25 +0200201TEST_F(CpuidX86Test, KabyLakeCache) {
Artem Alekseev653d5812019-07-02 17:52:25 +0300202 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, 0x00C0003F, 0x000003FF, 0x00000000}},
208 {{0x00000004, 3}, Leaf{0x1C03C163, 0x02C0003F, 0x00001FFF, 0x00000002}},
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, 4);
237 EXPECT_EQ(info.levels[2].line_size, 64);
238 EXPECT_EQ(info.levels[2].tlb_entries, 1024);
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}
249
Guillaume Chatelet47953732020-10-09 17:20:25 +0200250TEST_F(CpuidX86Test, HSWCache) {
Artem Alekseev653d5812019-07-02 17:52:25 +0300251 g_fake_cpu->SetLeaves({
252 {{0x00000000, 0}, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}},
253 {{0x00000001, 0}, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}},
254 {{0x00000004, 0}, Leaf{0x1C004121, 0x01C0003F, 0x0000003F, 0x00000000}},
255 {{0x00000004, 1}, Leaf{0x1C004122, 0x01C0003F, 0x0000003F, 0x00000000}},
256 {{0x00000004, 2}, Leaf{0x1C004143, 0x01C0003F, 0x000001FF, 0x00000000}},
257 {{0x00000004, 3}, Leaf{0x1C03C163, 0x02C0003F, 0x00001FFF, 0x00000006}},
258 {{0x00000007, 0}, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}},
259 {{0x80000000, 0}, Leaf{0x80000008, 0x00000000, 0x00000000, 0x00000000}},
260 {{0x80000001, 0}, Leaf{0x00000000, 0x00000000, 0x00000121, 0x2C100000}},
261 {{0x80000002, 0}, Leaf{0x65746E49, 0x2952286C, 0x726F4320, 0x4D542865}},
262 {{0x80000003, 0}, Leaf{0x37692029, 0x3035362D, 0x43205530, 0x40205550}},
263 });
264 const auto info = GetX86CacheInfo();
265 EXPECT_EQ(info.size, 4);
266 EXPECT_EQ(info.levels[0].level, 1);
267 EXPECT_EQ(info.levels[0].cache_type, 1);
268 EXPECT_EQ(info.levels[0].cache_size, 32 * KiB);
269 EXPECT_EQ(info.levels[0].ways, 8);
270 EXPECT_EQ(info.levels[0].line_size, 64);
271 EXPECT_EQ(info.levels[0].tlb_entries, 64);
272 EXPECT_EQ(info.levels[0].partitioning, 1);
273
274 EXPECT_EQ(info.levels[1].level, 1);
275 EXPECT_EQ(info.levels[1].cache_type, 2);
276 EXPECT_EQ(info.levels[1].cache_size, 32 * KiB);
277 EXPECT_EQ(info.levels[1].ways, 8);
278 EXPECT_EQ(info.levels[1].line_size, 64);
279 EXPECT_EQ(info.levels[1].tlb_entries, 64);
280 EXPECT_EQ(info.levels[1].partitioning, 1);
281
282 EXPECT_EQ(info.levels[2].level, 2);
283 EXPECT_EQ(info.levels[2].cache_type, 3);
284 EXPECT_EQ(info.levels[2].cache_size, 256 * KiB);
285 EXPECT_EQ(info.levels[2].ways, 8);
286 EXPECT_EQ(info.levels[2].line_size, 64);
287 EXPECT_EQ(info.levels[2].tlb_entries, 512);
288 EXPECT_EQ(info.levels[2].partitioning, 1);
289
290 EXPECT_EQ(info.levels[3].level, 3);
291 EXPECT_EQ(info.levels[3].cache_type, 3);
292 EXPECT_EQ(info.levels[3].cache_size, 6 * MiB);
293 EXPECT_EQ(info.levels[3].ways, 12);
294 EXPECT_EQ(info.levels[3].line_size, 64);
295 EXPECT_EQ(info.levels[3].tlb_entries, 8192);
296 EXPECT_EQ(info.levels[3].partitioning, 1);
297}
Guillaume Chatelet47953732020-10-09 17:20:25 +0200298
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100299// http://users.atw.hu/instlatx64/AuthenticAMD0630F81_K15_Godavari_CPUID.txt
Guillaume Chatelet47953732020-10-09 17:20:25 +0200300TEST_F(CpuidX86Test, AMD_K15) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100301 g_fake_cpu->SetLeaves({
Artem Alekseev653d5812019-07-02 17:52:25 +0300302 {{0x00000000, 0}, Leaf{0x0000000D, 0x68747541, 0x444D4163, 0x69746E65}},
303 {{0x00000001, 0}, Leaf{0x00630F81, 0x00040800, 0x3E98320B, 0x178BFBFF}},
304 {{0x00000007, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
305 {{0x80000000, 0}, Leaf{0x8000001E, 0x68747541, 0x444D4163, 0x69746E65}},
306 {{0x80000001, 0}, Leaf{0x00630F81, 0x10000000, 0x0FEBBFFF, 0x2FD3FBFF}},
307 {{0x80000002, 0}, Leaf{0x20444D41, 0x372D3841, 0x4B303736, 0x64615220}},
308 {{0x80000003, 0}, Leaf{0x206E6F65, 0x202C3752, 0x43203031, 0x75706D6F}},
309 {{0x80000004, 0}, Leaf{0x43206574, 0x7365726F, 0x2B433420, 0x00204736}},
310 {{0x80000005, 0}, Leaf{0xFF40FF18, 0xFF40FF30, 0x10040140, 0x60030140}},
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100311 });
312 const auto info = GetX86Info();
313
314 EXPECT_STREQ(info.vendor, "AuthenticAMD");
315 EXPECT_EQ(info.family, 0x15);
316 EXPECT_EQ(info.model, 0x38);
317 EXPECT_EQ(info.stepping, 0x01);
318 EXPECT_EQ(GetX86Microarchitecture(&info),
319 X86Microarchitecture::AMD_BULLDOZER);
320
321 char brand_string[49];
322 FillX86BrandString(brand_string);
323 EXPECT_STREQ(brand_string, "AMD A8-7670K Radeon R7, 10 Compute Cores 4C+6G ");
324}
325
Guillaume Chatelet47953732020-10-09 17:20:25 +0200326// https://github.com/InstLatx64/InstLatx64/blob/master/GenuineIntel/GenuineIntel00106A1_Nehalem_CPUID.txt
327TEST_F(CpuidX86Test, Nehalem) {
328 // Pre AVX cpus don't have xsave
329 g_fake_cpu->SetOsBackupsExtendedRegisters(false);
330#if defined(CPU_FEATURES_OS_WINDOWS)
331 g_fake_cpu->SetWindowsIsProcessorFeaturePresent(
332 PF_XMMI_INSTRUCTIONS_AVAILABLE);
333 g_fake_cpu->SetWindowsIsProcessorFeaturePresent(
334 PF_XMMI64_INSTRUCTIONS_AVAILABLE);
335 g_fake_cpu->SetWindowsIsProcessorFeaturePresent(
336 PF_SSE3_INSTRUCTIONS_AVAILABLE);
337#endif // CPU_FEATURES_OS_WINDOWS
338#if defined(CPU_FEATURES_OS_DARWIN)
339 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse");
340 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse2");
341 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse3");
342 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.supplementalsse3");
343 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse4_1");
344 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse4_2");
345#endif // CPU_FEATURES_OS_DARWIN
346#if defined(CPU_FEATURES_OS_LINUX_OR_ANDROID)
347 auto& fs = GetEmptyFilesystem();
348 fs.CreateFile("/proc/cpuinfo", R"(processor :
349flags : fpu mmx sse sse2 sse3 ssse3 sse4_1 sse4_2
350)");
351#endif // CPU_FEATURES_OS_LINUX_OR_ANDROID
352 g_fake_cpu->SetLeaves({
353 {{0x00000000, 0}, Leaf{0x0000000B, 0x756E6547, 0x6C65746E, 0x49656E69}},
354 {{0x00000001, 0}, Leaf{0x000106A2, 0x00100800, 0x00BCE3BD, 0xBFEBFBFF}},
355 {{0x00000002, 0}, Leaf{0x55035A01, 0x00F0B0E3, 0x00000000, 0x09CA212C}},
356 {{0x00000003, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
357 {{0x00000004, 0}, Leaf{0x1C004121, 0x01C0003F, 0x0000003F, 0x00000000}},
358 {{0x00000004, 0}, Leaf{0x1C004122, 0x00C0003F, 0x0000007F, 0x00000000}},
359 {{0x00000004, 0}, Leaf{0x1C004143, 0x01C0003F, 0x000001FF, 0x00000000}},
360 {{0x00000004, 0}, Leaf{0x1C03C163, 0x03C0003F, 0x00000FFF, 0x00000002}},
361 {{0x00000005, 0}, Leaf{0x00000040, 0x00000040, 0x00000003, 0x00021120}},
362 {{0x00000006, 0}, Leaf{0x00000001, 0x00000002, 0x00000001, 0x00000000}},
363 {{0x00000007, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
364 {{0x00000008, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
365 {{0x00000009, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
366 {{0x0000000A, 0}, Leaf{0x07300403, 0x00000000, 0x00000000, 0x00000603}},
367 {{0x0000000B, 0}, Leaf{0x00000001, 0x00000001, 0x00000100, 0x00000000}},
368 {{0x0000000B, 0}, Leaf{0x00000004, 0x00000002, 0x00000201, 0x00000000}},
369 {{0x80000000, 0}, Leaf{0x80000008, 0x00000000, 0x00000000, 0x00000000}},
370 {{0x80000001, 0}, Leaf{0x00000000, 0x00000000, 0x00000001, 0x28100000}},
371 {{0x80000002, 0}, Leaf{0x756E6547, 0x20656E69, 0x65746E49, 0x2952286C}},
372 {{0x80000003, 0}, Leaf{0x55504320, 0x20202020, 0x20202020, 0x40202020}},
373 {{0x80000004, 0}, Leaf{0x30303020, 0x20402030, 0x37382E31, 0x007A4847}},
374 {{0x80000005, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
375 {{0x80000006, 0}, Leaf{0x00000000, 0x00000000, 0x01006040, 0x00000000}},
376 {{0x80000007, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000100}},
377 {{0x80000008, 0}, Leaf{0x00003028, 0x00000000, 0x00000000, 0x00000000}},
378 });
379 const auto info = GetX86Info();
380
381 EXPECT_STREQ(info.vendor, "GenuineIntel");
382 EXPECT_EQ(info.family, 0x06);
383 EXPECT_EQ(info.model, 0x1A);
384 EXPECT_EQ(info.stepping, 0x02);
385 EXPECT_EQ(GetX86Microarchitecture(&info), X86Microarchitecture::INTEL_NHM);
386
387 char brand_string[49];
388 FillX86BrandString(brand_string);
389 EXPECT_STREQ(brand_string, "Genuine Intel(R) CPU @ 0000 @ 1.87GHz");
390
391 EXPECT_TRUE(info.features.sse);
392 EXPECT_TRUE(info.features.sse2);
393 EXPECT_TRUE(info.features.sse3);
394#ifndef CPU_FEATURES_OS_WINDOWS
395 // Currently disabled on Windows as IsProcessorFeaturePresent do not support
396 // feature detection > sse3.
397 EXPECT_TRUE(info.features.ssse3);
398 EXPECT_TRUE(info.features.sse4_1);
399 EXPECT_TRUE(info.features.sse4_2);
400#endif // CPU_FEATURES_OS_WINDOWS
401}
402
403// https://github.com/InstLatx64/InstLatx64/blob/master/GenuineIntel/GenuineIntel0030673_Silvermont3_CPUID.txt
404TEST_F(CpuidX86Test, Atom) {
405 // Pre AVX cpus don't have xsave
406 g_fake_cpu->SetOsBackupsExtendedRegisters(false);
407#if defined(CPU_FEATURES_OS_WINDOWS)
408 g_fake_cpu->SetWindowsIsProcessorFeaturePresent(
409 PF_XMMI_INSTRUCTIONS_AVAILABLE);
410 g_fake_cpu->SetWindowsIsProcessorFeaturePresent(
411 PF_XMMI64_INSTRUCTIONS_AVAILABLE);
412 g_fake_cpu->SetWindowsIsProcessorFeaturePresent(
413 PF_SSE3_INSTRUCTIONS_AVAILABLE);
414#endif // CPU_FEATURES_OS_WINDOWS
415#if defined(CPU_FEATURES_OS_DARWIN)
416 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse");
417 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse2");
418 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse3");
419 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.supplementalsse3");
420 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse4_1");
421 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse4_2");
422#endif // CPU_FEATURES_OS_DARWIN
423#if defined(CPU_FEATURES_OS_LINUX_OR_ANDROID)
424 auto& fs = GetEmptyFilesystem();
425 fs.CreateFile("/proc/cpuinfo", R"(
426flags : fpu mmx sse sse2 sse3 ssse3 sse4_1 sse4_2
427)");
428#endif // CPU_FEATURES_OS_LINUX_OR_ANDROID
429 g_fake_cpu->SetLeaves({
430 {{0x00000000, 0}, Leaf{0x0000000B, 0x756E6547, 0x6C65746E, 0x49656E69}},
431 {{0x00000001, 0}, Leaf{0x00030673, 0x00100800, 0x41D8E3BF, 0xBFEBFBFF}},
432 {{0x00000002, 0}, Leaf{0x61B3A001, 0x0000FFC2, 0x00000000, 0x00000000}},
433 {{0x00000003, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
434 {{0x00000004, 0}, Leaf{0x1C000121, 0x0140003F, 0x0000003F, 0x00000001}},
435 {{0x00000004, 1}, Leaf{0x1C000122, 0x01C0003F, 0x0000003F, 0x00000001}},
436 {{0x00000004, 2}, Leaf{0x1C00C143, 0x03C0003F, 0x000003FF, 0x00000001}},
437 {{0x00000005, 0}, Leaf{0x00000040, 0x00000040, 0x00000003, 0x33000020}},
438 {{0x00000006, 0}, Leaf{0x00000005, 0x00000002, 0x00000009, 0x00000000}},
439 {{0x00000007, 0}, Leaf{0x00000000, 0x00002282, 0x00000000, 0x00000000}},
440 {{0x00000008, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
441 {{0x00000009, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
442 {{0x0000000A, 0}, Leaf{0x07280203, 0x00000000, 0x00000000, 0x00004503}},
443 {{0x0000000B, 0}, Leaf{0x00000001, 0x00000001, 0x00000100, 0x00000000}},
444 {{0x0000000B, 1}, Leaf{0x00000004, 0x00000004, 0x00000201, 0x00000000}},
445 {{0x80000000, 0}, Leaf{0x80000008, 0x00000000, 0x00000000, 0x00000000}},
446 {{0x80000001, 0}, Leaf{0x00000000, 0x00000000, 0x00000101, 0x28100000}},
447 {{0x80000002, 0}, Leaf{0x20202020, 0x6E492020, 0x286C6574, 0x43202952}},
448 {{0x80000003, 0}, Leaf{0x72656C65, 0x52286E6F, 0x50432029, 0x4A202055}},
449 {{0x80000004, 0}, Leaf{0x30303931, 0x20402020, 0x39392E31, 0x007A4847}},
450 {{0x80000005, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
451 {{0x80000006, 0}, Leaf{0x00000000, 0x00000000, 0x04008040, 0x00000000}},
452 {{0x80000007, 0}, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000100}},
453 {{0x80000008, 0}, Leaf{0x00003024, 0x00000000, 0x00000000, 0x00000000}},
454 });
455 const auto info = GetX86Info();
456
457 EXPECT_STREQ(info.vendor, "GenuineIntel");
458 EXPECT_EQ(info.family, 0x06);
459 EXPECT_EQ(info.model, 0x37);
460 EXPECT_EQ(info.stepping, 0x03);
461 EXPECT_EQ(GetX86Microarchitecture(&info),
462 X86Microarchitecture::INTEL_ATOM_SMT);
463
464 char brand_string[49];
465 FillX86BrandString(brand_string);
466 EXPECT_STREQ(brand_string, " Intel(R) Celeron(R) CPU J1900 @ 1.99GHz");
467
468 EXPECT_TRUE(info.features.sse);
469 EXPECT_TRUE(info.features.sse2);
470 EXPECT_TRUE(info.features.sse3);
471#ifndef CPU_FEATURES_OS_WINDOWS
472 // Currently disabled on Windows as IsProcessorFeaturePresent do not support
473 // feature detection > sse3.
474 EXPECT_TRUE(info.features.ssse3);
475 EXPECT_TRUE(info.features.sse4_1);
476 EXPECT_TRUE(info.features.sse4_2);
477#endif // CPU_FEATURES_OS_WINDOWS
478}
479
480// https://github.com/InstLatx64/InstLatx64/blob/master/GenuineIntel/GenuineIntel0000673_P3_KatmaiDP_CPUID.txt
481TEST_F(CpuidX86Test, P3) {
482 // Pre AVX cpus don't have xsave
483 g_fake_cpu->SetOsBackupsExtendedRegisters(false);
484#if defined(CPU_FEATURES_OS_WINDOWS)
485 g_fake_cpu->SetWindowsIsProcessorFeaturePresent(
486 PF_XMMI_INSTRUCTIONS_AVAILABLE);
487#endif // CPU_FEATURES_OS_WINDOWS
488#if defined(CPU_FEATURES_OS_DARWIN)
489 g_fake_cpu->SetDarwinSysCtlByName("hw.optional.sse");
490#endif // CPU_FEATURES_OS_DARWIN
491#if defined(CPU_FEATURES_OS_LINUX_OR_ANDROID)
492 auto& fs = GetEmptyFilesystem();
493 fs.CreateFile("/proc/cpuinfo", R"(
494flags : fpu mmx sse
495)");
496#endif // CPU_FEATURES_OS_LINUX_OR_ANDROID
497 g_fake_cpu->SetLeaves({
498 {{0x00000000, 0}, Leaf{0x00000003, 0x756E6547, 0x6C65746E, 0x49656E69}},
499 {{0x00000001, 0}, Leaf{0x00000673, 0x00000000, 0x00000000, 0x0387FBFF}},
500 {{0x00000002, 0}, Leaf{0x03020101, 0x00000000, 0x00000000, 0x0C040843}},
501 {{0x00000003, 0}, Leaf{0x00000000, 0x00000000, 0x4CECC782, 0x00006778}},
502 });
503 const auto info = GetX86Info();
504
505 EXPECT_STREQ(info.vendor, "GenuineIntel");
506 EXPECT_EQ(info.family, 0x06);
507 EXPECT_EQ(info.model, 0x07);
508 EXPECT_EQ(info.stepping, 0x03);
509 EXPECT_EQ(GetX86Microarchitecture(&info), X86Microarchitecture::X86_UNKNOWN);
510
511 char brand_string[49];
512 FillX86BrandString(brand_string);
513 EXPECT_STREQ(brand_string, "");
514
515 EXPECT_TRUE(info.features.mmx);
516 EXPECT_TRUE(info.features.sse);
517 EXPECT_FALSE(info.features.sse2);
518 EXPECT_FALSE(info.features.sse3);
519#ifndef CPU_FEATURES_OS_WINDOWS
520 // Currently disabled on Windows as IsProcessorFeaturePresent do not support
521 // feature detection > sse3.
522 EXPECT_FALSE(info.features.ssse3);
523 EXPECT_FALSE(info.features.sse4_1);
524 EXPECT_FALSE(info.features.sse4_2);
525#endif // CPU_FEATURES_OS_WINDOWS
526}
527
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100528// TODO(user): test what happens when xsave/osxsave are not present.
529// TODO(user): test what happens when xmm/ymm/zmm os support are not
530// present.
531
532} // namespace
533} // namespace cpu_features