blob: 2389de9ab548a48bf100920f99a1c22a5b377fe0 [file] [log] [blame]
Guillaume Chatelet439d3712018-02-01 10:03:09 +01001// 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
24namespace cpu_features {
25
26class FakeCpu {
27 public:
28 Leaf CpuId(uint32_t leaf_id) const {
29 const auto itr = cpuid_leaves_.find(leaf_id);
30 EXPECT_TRUE(itr != cpuid_leaves_.end()) << "Missing leaf " << leaf_id;
31 return itr->second;
32 }
33
34 uint32_t GetXCR0Eax() const { return xcr0_eax_; }
35
36 void SetLeaves(std::map<uint32_t, Leaf> configuration) {
37 cpuid_leaves_ = std::move(configuration);
38 }
39
40 void SetOsBackupsExtendedRegisters(bool os_backups_extended_registers) {
41 xcr0_eax_ = os_backups_extended_registers ? -1 : 0;
42 }
43
44 private:
45 std::map<uint32_t, Leaf> cpuid_leaves_;
46 uint32_t xcr0_eax_;
47};
48
49auto* g_fake_cpu = new FakeCpu();
50
51extern "C" Leaf CpuId(uint32_t leaf_id) { return g_fake_cpu->CpuId(leaf_id); }
52extern "C" uint32_t GetXCR0Eax(void) { return g_fake_cpu->GetXCR0Eax(); }
53
54namespace {
55
56TEST(CpuidX86Test, SandyBridge) {
57 g_fake_cpu->SetOsBackupsExtendedRegisters(true);
58 g_fake_cpu->SetLeaves({
59 {0x00000000, Leaf{0x0000000D, 0x756E6547, 0x6C65746E, 0x49656E69}},
60 {0x00000001, Leaf{0x000206A6, 0x00100800, 0x1F9AE3BF, 0xBFEBFBFF}},
61 {0x00000007, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
62 });
63 const auto info = GetX86Info();
64 EXPECT_STREQ(info.vendor, "GenuineIntel");
65 EXPECT_EQ(info.family, 0x06);
66 EXPECT_EQ(info.model, 0x02A);
67 EXPECT_EQ(info.stepping, 0x06);
68 // Leaf 7 is zeroed out so none of the Leaf 7 flags are set.
69 const auto features = info.features;
70 EXPECT_FALSE(features.erms);
71 EXPECT_FALSE(features.avx2);
72 EXPECT_FALSE(features.avx512f);
73 EXPECT_FALSE(features.avx512cd);
74 EXPECT_FALSE(features.avx512er);
75 EXPECT_FALSE(features.avx512pf);
76 EXPECT_FALSE(features.avx512bw);
77 EXPECT_FALSE(features.avx512dq);
78 EXPECT_FALSE(features.avx512vl);
79 EXPECT_FALSE(features.avx512ifma);
80 EXPECT_FALSE(features.avx512vbmi);
81 EXPECT_FALSE(features.avx512vbmi2);
82 EXPECT_FALSE(features.avx512vnni);
83 EXPECT_FALSE(features.avx512bitalg);
84 EXPECT_FALSE(features.avx512vpopcntdq);
85 EXPECT_FALSE(features.avx512_4vnniw);
86 EXPECT_FALSE(features.avx512_4vbmi2);
87 // All old cpu features should be set.
88 EXPECT_TRUE(features.aes);
89 EXPECT_TRUE(features.ssse3);
90 EXPECT_TRUE(features.sse4_1);
91 EXPECT_TRUE(features.sse4_2);
92 EXPECT_TRUE(features.avx);
Guillaume Chateletd395dfa2019-01-22 13:19:42 +010093 EXPECT_FALSE(features.sha);
94 EXPECT_TRUE(features.popcnt);
95 EXPECT_FALSE(features.movbe);
96 EXPECT_FALSE(features.rdrnd);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010097}
98
99TEST(CpuidX86Test, SandyBridgeTestOsSupport) {
100 g_fake_cpu->SetLeaves({
101 {0x00000000, Leaf{0x0000000D, 0x756E6547, 0x6C65746E, 0x49656E69}},
102 {0x00000001, Leaf{0x000206A6, 0x00100800, 0x1F9AE3BF, 0xBFEBFBFF}},
103 {0x00000007, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
104 });
105 // avx is disabled if os does not support backing up ymm registers.
106 g_fake_cpu->SetOsBackupsExtendedRegisters(false);
107 EXPECT_FALSE(GetX86Info().features.avx);
108 // avx is disabled if os does not support backing up ymm registers.
109 g_fake_cpu->SetOsBackupsExtendedRegisters(true);
110 EXPECT_TRUE(GetX86Info().features.avx);
111}
112
113TEST(CpuidX86Test, SkyLake) {
114 g_fake_cpu->SetOsBackupsExtendedRegisters(true);
115 g_fake_cpu->SetLeaves({
116 {0x00000000, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}},
117 {0x00000001, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}},
118 {0x00000007, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}},
119 });
120 const auto info = GetX86Info();
121 EXPECT_STREQ(info.vendor, "GenuineIntel");
122 EXPECT_EQ(info.family, 0x06);
123 EXPECT_EQ(info.model, 0x04E);
124 EXPECT_EQ(info.stepping, 0x03);
125 EXPECT_EQ(GetX86Microarchitecture(&info), X86Microarchitecture::INTEL_SKL);
126}
127
128TEST(CpuidX86Test, Branding) {
129 g_fake_cpu->SetLeaves({
130 {0x00000000, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}},
131 {0x00000001, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}},
132 {0x00000007, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}},
133 {0x80000000, Leaf{0x80000008, 0x00000000, 0x00000000, 0x00000000}},
134 {0x80000001, Leaf{0x00000000, 0x00000000, 0x00000121, 0x2C100000}},
135 {0x80000002, Leaf{0x65746E49, 0x2952286C, 0x726F4320, 0x4D542865}},
136 {0x80000003, Leaf{0x37692029, 0x3035362D, 0x43205530, 0x40205550}},
137 {0x80000004, Leaf{0x352E3220, 0x7A484730, 0x00000000, 0x00000000}},
138 });
139 char brand_string[49];
140 FillX86BrandString(brand_string);
141 EXPECT_STREQ(brand_string, "Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz");
142}
143
144// http://users.atw.hu/instlatx64/AuthenticAMD0630F81_K15_Godavari_CPUID.txt
145TEST(CpuidX86Test, AMD_K15) {
146 g_fake_cpu->SetLeaves({
147 {0x00000000, Leaf{0x0000000D, 0x68747541, 0x444D4163, 0x69746E65}},
148 {0x00000001, Leaf{0x00630F81, 0x00040800, 0x3E98320B, 0x178BFBFF}},
149 {0x00000007, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
150 {0x80000000, Leaf{0x8000001E, 0x68747541, 0x444D4163, 0x69746E65}},
151 {0x80000001, Leaf{0x00630F81, 0x10000000, 0x0FEBBFFF, 0x2FD3FBFF}},
152 {0x80000002, Leaf{0x20444D41, 0x372D3841, 0x4B303736, 0x64615220}},
153 {0x80000003, Leaf{0x206E6F65, 0x202C3752, 0x43203031, 0x75706D6F}},
154 {0x80000004, Leaf{0x43206574, 0x7365726F, 0x2B433420, 0x00204736}},
155 {0x80000005, Leaf{0xFF40FF18, 0xFF40FF30, 0x10040140, 0x60030140}},
156 });
157 const auto info = GetX86Info();
158
159 EXPECT_STREQ(info.vendor, "AuthenticAMD");
160 EXPECT_EQ(info.family, 0x15);
161 EXPECT_EQ(info.model, 0x38);
162 EXPECT_EQ(info.stepping, 0x01);
163 EXPECT_EQ(GetX86Microarchitecture(&info),
164 X86Microarchitecture::AMD_BULLDOZER);
165
166 char brand_string[49];
167 FillX86BrandString(brand_string);
168 EXPECT_STREQ(brand_string, "AMD A8-7670K Radeon R7, 10 Compute Cores 4C+6G ");
169}
170
171// TODO(user): test what happens when xsave/osxsave are not present.
172// TODO(user): test what happens when xmm/ymm/zmm os support are not
173// present.
174
175} // namespace
176} // namespace cpu_features