blob: 88cc992ecb455ea2698137309921191dc8e0f567 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]14cd2e62011-02-24 09:20:162// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/cpu.h"
Jan Wilken Dörrieb5a41c32020-12-09 18:55:476#include "base/containers/contains.h"
Richard Townsend8cb7ba0b2020-11-26 23:23:227#include "base/logging.h"
Robert Sesekfd71c382021-01-14 18:40:528#include "base/strings/string_util.h"
[email protected]d1811bc2012-03-31 07:08:539#include "build/build_config.h"
[email protected]14cd2e62011-02-24 09:20:1610#include "testing/gtest/include/gtest/gtest.h"
11
12// Tests whether we can run extended instructions represented by the CPU
13// information. This test actually executes some extended instructions (such as
14// MMX, SSE, etc.) supported by the CPU and sees we can run them without
15// "undefined instruction" exceptions. That is, this test succeeds when this
16// test finishes without a crash.
17TEST(CPU, RunExtendedInstructions) {
[email protected]14cd2e62011-02-24 09:20:1618 // Retrieve the CPU information.
19 base::CPU cpu;
Richard Townsend8cb7ba0b2020-11-26 23:23:2220#if defined(ARCH_CPU_X86_FAMILY)
[email protected]14cd2e62011-02-24 09:20:1621
[email protected]14cd2e62011-02-24 09:20:1622 ASSERT_TRUE(cpu.has_mmx());
fbarchard0ce41ae2015-10-02 03:23:1923 ASSERT_TRUE(cpu.has_sse());
24 ASSERT_TRUE(cpu.has_sse2());
Victor Costan5bb28642020-11-14 06:42:4925 ASSERT_TRUE(cpu.has_sse3());
[email protected]14cd2e62011-02-24 09:20:1626
fbarchard20028e62015-10-06 17:26:2627// GCC and clang instruction test.
fbarchard0ce41ae2015-10-02 03:23:1928#if defined(COMPILER_GCC)
[email protected]14cd2e62011-02-24 09:20:1629 // Execute an MMX instruction.
30 __asm__ __volatile__("emms\n" : : : "mm0");
31
fbarchard0ce41ae2015-10-02 03:23:1932 // Execute an SSE instruction.
33 __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0");
[email protected]14cd2e62011-02-24 09:20:1634
fbarchard0ce41ae2015-10-02 03:23:1935 // Execute an SSE 2 instruction.
36 __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0");
[email protected]14cd2e62011-02-24 09:20:1637
Victor Costan5bb28642020-11-14 06:42:4938 // Execute an SSE 3 instruction.
39 __asm__ __volatile__("addsubpd %%xmm0, %%xmm0\n" : : : "xmm0");
[email protected]14cd2e62011-02-24 09:20:1640
41 if (cpu.has_ssse3()) {
42 // Execute a Supplimental SSE 3 instruction.
43 __asm__ __volatile__("psignb %%xmm0, %%xmm0\n" : : : "xmm0");
44 }
45
46 if (cpu.has_sse41()) {
47 // Execute an SSE 4.1 instruction.
48 __asm__ __volatile__("pmuldq %%xmm0, %%xmm0\n" : : : "xmm0");
49 }
50
51 if (cpu.has_sse42()) {
52 // Execute an SSE 4.2 instruction.
53 __asm__ __volatile__("crc32 %%eax, %%eax\n" : : : "eax");
54 }
fbarchard0ce41ae2015-10-02 03:23:1955
ilevyb7d2f4082016-10-30 20:46:5756 if (cpu.has_popcnt()) {
57 // Execute a POPCNT instruction.
58 __asm__ __volatile__("popcnt %%eax, %%eax\n" : : : "eax");
59 }
60
fbarchard0ce41ae2015-10-02 03:23:1961 if (cpu.has_avx()) {
62 // Execute an AVX instruction.
63 __asm__ __volatile__("vzeroupper\n" : : : "xmm0");
64 }
65
Ng Zhi An03baf5e2021-10-04 22:56:5266 if (cpu.has_fma3()) {
67 // Execute a FMA3 instruction.
68 __asm__ __volatile__("vfmadd132ps %%xmm0, %%xmm0, %%xmm0\n" : : : "xmm0");
69 }
70
fbarchard0ce41ae2015-10-02 03:23:1971 if (cpu.has_avx2()) {
72 // Execute an AVX 2 instruction.
73 __asm__ __volatile__("vpunpcklbw %%ymm0, %%ymm0, %%ymm0\n" : : : "xmm0");
74 }
fbarchard20028e62015-10-06 17:26:2675// Visual C 32 bit and ClangCL 32/64 bit test.
76#elif defined(COMPILER_MSVC) && (defined(ARCH_CPU_32_BITS) || \
77 (defined(ARCH_CPU_64_BITS) && defined(__clang__)))
fbarchard0ce41ae2015-10-02 03:23:1978
79 // Execute an MMX instruction.
80 __asm emms;
81
82 // Execute an SSE instruction.
83 __asm xorps xmm0, xmm0;
84
85 // Execute an SSE 2 instruction.
86 __asm psrldq xmm0, 0;
87
Victor Costan5bb28642020-11-14 06:42:4988 // Execute an SSE 3 instruction.
89 __asm addsubpd xmm0, xmm0;
fbarchard0ce41ae2015-10-02 03:23:1990
91 if (cpu.has_ssse3()) {
92 // Execute a Supplimental SSE 3 instruction.
93 __asm psignb xmm0, xmm0;
94 }
95
96 if (cpu.has_sse41()) {
97 // Execute an SSE 4.1 instruction.
98 __asm pmuldq xmm0, xmm0;
99 }
100
101 if (cpu.has_sse42()) {
102 // Execute an SSE 4.2 instruction.
103 __asm crc32 eax, eax;
104 }
105
ilevyb7d2f4082016-10-30 20:46:57106 if (cpu.has_popcnt()) {
107 // Execute a POPCNT instruction.
108 __asm popcnt eax, eax;
109 }
110
fbarchard0ce41ae2015-10-02 03:23:19111 if (cpu.has_avx()) {
112 // Execute an AVX instruction.
113 __asm vzeroupper;
114 }
115
Ng Zhi An03baf5e2021-10-04 22:56:52116 if (cpu.has_fma3()) {
117 // Execute an AVX instruction.
118 __asm vfmadd132ps xmm0, xmm0, xmm0;
119 }
120
fbarchard0ce41ae2015-10-02 03:23:19121 if (cpu.has_avx2()) {
122 // Execute an AVX 2 instruction.
123 __asm vpunpcklbw ymm0, ymm0, ymm0
124 }
fbarchard0ce41ae2015-10-02 03:23:19125#endif // defined(COMPILER_GCC)
126#endif // defined(ARCH_CPU_X86_FAMILY)
Richard Townsend8cb7ba0b2020-11-26 23:23:22127
128#if defined(ARCH_CPU_ARM64)
129 // Check that the CPU is correctly reporting support for the Armv8.5-A memory
130 // tagging extension. The new MTE instructions aren't encoded in NOP space
131 // like BTI/Pointer Authentication and will crash older cores with a SIGILL if
132 // used incorrectly. This test demonstrates how it should be done and that
133 // this approach works.
134 if (cpu.has_mte()) {
135#if !defined(__ARM_FEATURE_MEMORY_TAGGING)
136 // In this section, we're running on an MTE-compatible core, but we're
137 // building this file without MTE support. Fail this test to indicate that
138 // there's a problem with the base/ build configuration.
139 GTEST_FAIL()
140 << "MTE support detected (but base/ built without MTE support)";
141#else
142 char ptr[32];
143 uint64_t val;
144 // Execute a trivial MTE instruction. Normally, MTE should be used via the
145 // intrinsics documented at
146 // https://developer.arm.com/documentation/101028/0012/10--Memory-tagging-intrinsics,
147 // this test uses the irg (Insert Random Tag) instruction directly to make
148 // sure that it's not optimized out by the compiler.
149 __asm__ __volatile__("irg %0, %1" : "=r"(val) : "r"(ptr));
150#endif // __ARM_FEATURE_MEMORY_TAGGING
Richard Townsend8cb7ba0b2020-11-26 23:23:22151 }
152#endif // ARCH_CPU_ARM64
[email protected]14cd2e62011-02-24 09:20:16153}
Lei Zhang49c4b2a2017-11-03 21:34:23154
155// For https://crbug.com/249713
156TEST(CPU, BrandAndVendorContainsNoNUL) {
157 base::CPU cpu;
Jan Wilken Dörrief61e74c2019-06-07 08:20:02158 EXPECT_FALSE(base::Contains(cpu.cpu_brand(), '\0'));
159 EXPECT_FALSE(base::Contains(cpu.vendor_name(), '\0'));
Lei Zhang49c4b2a2017-11-03 21:34:23160}
Gabriel Marin8fdd7772019-08-17 00:28:09161
162#if defined(ARCH_CPU_X86_FAMILY)
163// Tests that we compute the correct CPU family and model based on the vendor
164// and CPUID signature.
165TEST(CPU, X86FamilyAndModel) {
Lei Zhang1b1116c52021-05-14 22:54:38166 base::internal::X86ModelInfo info;
Gabriel Marin8fdd7772019-08-17 00:28:09167
168 // Check with an Intel Skylake signature.
Lei Zhang1b1116c52021-05-14 22:54:38169 info = base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x000406e3);
170 EXPECT_EQ(info.family, 6);
171 EXPECT_EQ(info.model, 78);
172 EXPECT_EQ(info.ext_family, 0);
173 EXPECT_EQ(info.ext_model, 4);
Gabriel Marin8fdd7772019-08-17 00:28:09174
175 // Check with an Intel Airmont signature.
Lei Zhang1b1116c52021-05-14 22:54:38176 info = base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x000406c2);
177 EXPECT_EQ(info.family, 6);
178 EXPECT_EQ(info.model, 76);
179 EXPECT_EQ(info.ext_family, 0);
180 EXPECT_EQ(info.ext_model, 4);
Gabriel Marin8fdd7772019-08-17 00:28:09181
182 // Check with an Intel Prescott signature.
Lei Zhang1b1116c52021-05-14 22:54:38183 info = base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x00000f31);
184 EXPECT_EQ(info.family, 15);
185 EXPECT_EQ(info.model, 3);
186 EXPECT_EQ(info.ext_family, 0);
187 EXPECT_EQ(info.ext_model, 0);
Gabriel Marin8fdd7772019-08-17 00:28:09188
189 // Check with an AMD Excavator signature.
Lei Zhang1b1116c52021-05-14 22:54:38190 info = base::internal::ComputeX86FamilyAndModel("AuthenticAMD", 0x00670f00);
191 EXPECT_EQ(info.family, 21);
192 EXPECT_EQ(info.model, 112);
193 EXPECT_EQ(info.ext_family, 6);
194 EXPECT_EQ(info.ext_model, 7);
Gabriel Marin8fdd7772019-08-17 00:28:09195}
196#endif // defined(ARCH_CPU_X86_FAMILY)
Robert Sesekfd71c382021-01-14 18:40:52197
198#if defined(ARCH_CPU_ARM_FAMILY) && \
Xiaohan Wang38e4ebb2022-01-19 06:57:43199 (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS))
Robert Sesekfd71c382021-01-14 18:40:52200TEST(CPU, ARMImplementerAndPartNumber) {
201 base::CPU cpu;
202
203 const std::string& cpu_brand = cpu.cpu_brand();
204
Robert Sesek988155862021-01-15 18:32:07205 // Some devices, including on the CQ, do not report a cpu_brand
206 // https://crbug.com/1166533 and https://crbug.com/1167123.
Robert Sesekfd71c382021-01-14 18:40:52207 EXPECT_EQ(cpu_brand, base::TrimWhitespaceASCII(cpu_brand, base::TRIM_ALL));
208 EXPECT_GT(cpu.implementer(), 0u);
209 EXPECT_GT(cpu.part_number(), 0u);
210}
Xiaohan Wang38e4ebb2022-01-19 06:57:43211#endif // defined(ARCH_CPU_ARM_FAMILY) && (BUILDFLAG(IS_LINUX) ||
212 // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS))