f7fc08177f5715c7b54fa737d9fb2e261a912b46
[iec.git] / src / type3_AndroidCloud / anbox-master / external / cpu_features / test / cpuinfo_x86_test.cc
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:
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
49 auto* g_fake_cpu = new FakeCpu();
50
51 extern "C" Leaf CpuId(uint32_t leaf_id) { return g_fake_cpu->CpuId(leaf_id); }
52 extern "C" uint32_t GetXCR0Eax(void) { return g_fake_cpu->GetXCR0Eax(); }
53
54 namespace {
55
56 TEST(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);
93 }
94
95 TEST(CpuidX86Test, SandyBridgeTestOsSupport) {
96   g_fake_cpu->SetLeaves({
97       {0x00000000, Leaf{0x0000000D, 0x756E6547, 0x6C65746E, 0x49656E69}},
98       {0x00000001, Leaf{0x000206A6, 0x00100800, 0x1F9AE3BF, 0xBFEBFBFF}},
99       {0x00000007, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
100   });
101   // avx is disabled if os does not support backing up ymm registers.
102   g_fake_cpu->SetOsBackupsExtendedRegisters(false);
103   EXPECT_FALSE(GetX86Info().features.avx);
104   // avx is disabled if os does not support backing up ymm registers.
105   g_fake_cpu->SetOsBackupsExtendedRegisters(true);
106   EXPECT_TRUE(GetX86Info().features.avx);
107 }
108
109 TEST(CpuidX86Test, SkyLake) {
110   g_fake_cpu->SetOsBackupsExtendedRegisters(true);
111   g_fake_cpu->SetLeaves({
112       {0x00000000, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}},
113       {0x00000001, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}},
114       {0x00000007, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}},
115   });
116   const auto info = GetX86Info();
117   EXPECT_STREQ(info.vendor, "GenuineIntel");
118   EXPECT_EQ(info.family, 0x06);
119   EXPECT_EQ(info.model, 0x04E);
120   EXPECT_EQ(info.stepping, 0x03);
121   EXPECT_EQ(GetX86Microarchitecture(&info), X86Microarchitecture::INTEL_SKL);
122 }
123
124 TEST(CpuidX86Test, Branding) {
125   g_fake_cpu->SetLeaves({
126       {0x00000000, Leaf{0x00000016, 0x756E6547, 0x6C65746E, 0x49656E69}},
127       {0x00000001, Leaf{0x000406E3, 0x00100800, 0x7FFAFBBF, 0xBFEBFBFF}},
128       {0x00000007, Leaf{0x00000000, 0x029C67AF, 0x00000000, 0x00000000}},
129       {0x80000000, Leaf{0x80000008, 0x00000000, 0x00000000, 0x00000000}},
130       {0x80000001, Leaf{0x00000000, 0x00000000, 0x00000121, 0x2C100000}},
131       {0x80000002, Leaf{0x65746E49, 0x2952286C, 0x726F4320, 0x4D542865}},
132       {0x80000003, Leaf{0x37692029, 0x3035362D, 0x43205530, 0x40205550}},
133       {0x80000004, Leaf{0x352E3220, 0x7A484730, 0x00000000, 0x00000000}},
134   });
135   char brand_string[49];
136   FillX86BrandString(brand_string);
137   EXPECT_STREQ(brand_string, "Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz");
138 }
139
140 // http://users.atw.hu/instlatx64/AuthenticAMD0630F81_K15_Godavari_CPUID.txt
141 TEST(CpuidX86Test, AMD_K15) {
142   g_fake_cpu->SetLeaves({
143       {0x00000000, Leaf{0x0000000D, 0x68747541, 0x444D4163, 0x69746E65}},
144       {0x00000001, Leaf{0x00630F81, 0x00040800, 0x3E98320B, 0x178BFBFF}},
145       {0x00000007, Leaf{0x00000000, 0x00000000, 0x00000000, 0x00000000}},
146       {0x80000000, Leaf{0x8000001E, 0x68747541, 0x444D4163, 0x69746E65}},
147       {0x80000001, Leaf{0x00630F81, 0x10000000, 0x0FEBBFFF, 0x2FD3FBFF}},
148       {0x80000002, Leaf{0x20444D41, 0x372D3841, 0x4B303736, 0x64615220}},
149       {0x80000003, Leaf{0x206E6F65, 0x202C3752, 0x43203031, 0x75706D6F}},
150       {0x80000004, Leaf{0x43206574, 0x7365726F, 0x2B433420, 0x00204736}},
151       {0x80000005, Leaf{0xFF40FF18, 0xFF40FF30, 0x10040140, 0x60030140}},
152   });
153   const auto info = GetX86Info();
154
155   EXPECT_STREQ(info.vendor, "AuthenticAMD");
156   EXPECT_EQ(info.family, 0x15);
157   EXPECT_EQ(info.model, 0x38);
158   EXPECT_EQ(info.stepping, 0x01);
159   EXPECT_EQ(GetX86Microarchitecture(&info),
160             X86Microarchitecture::AMD_BULLDOZER);
161
162   char brand_string[49];
163   FillX86BrandString(brand_string);
164   EXPECT_STREQ(brand_string, "AMD A8-7670K Radeon R7, 10 Compute Cores 4C+6G ");
165 }
166
167 // TODO(user): test what happens when xsave/osxsave are not present.
168 // TODO(user): test what happens when xmm/ymm/zmm os support are not
169 // present.
170
171 }  // namespace
172 }  // namespace cpu_features