1 // Copyright 2017 Google LLC
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 "cpuinfo_aarch64.h"
16
17 #include "filesystem_for_testing.h"
18 #include "gtest/gtest.h"
19 #include "hwcaps_for_testing.h"
20
21 namespace cpu_features {
22 namespace {
23
DisableHardwareCapabilities()24 void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }
25
TEST(CpuinfoAarch64Test,FromHardwareCap)26 TEST(CpuinfoAarch64Test, FromHardwareCap) {
27 ResetHwcaps();
28 SetHardwareCapabilities(AARCH64_HWCAP_FP | AARCH64_HWCAP_AES, 0);
29 GetEmptyFilesystem(); // disabling /proc/cpuinfo
30 const auto info = GetAarch64Info();
31 EXPECT_TRUE(info.features.fp);
32 EXPECT_FALSE(info.features.asimd);
33 EXPECT_FALSE(info.features.evtstrm);
34 EXPECT_TRUE(info.features.aes);
35 EXPECT_FALSE(info.features.pmull);
36 EXPECT_FALSE(info.features.sha1);
37 EXPECT_FALSE(info.features.sha2);
38 EXPECT_FALSE(info.features.crc32);
39 EXPECT_FALSE(info.features.atomics);
40 EXPECT_FALSE(info.features.fphp);
41 EXPECT_FALSE(info.features.asimdhp);
42 EXPECT_FALSE(info.features.cpuid);
43 EXPECT_FALSE(info.features.asimdrdm);
44 EXPECT_FALSE(info.features.jscvt);
45 EXPECT_FALSE(info.features.fcma);
46 EXPECT_FALSE(info.features.lrcpc);
47 EXPECT_FALSE(info.features.dcpop);
48 EXPECT_FALSE(info.features.sha3);
49 EXPECT_FALSE(info.features.sm3);
50 EXPECT_FALSE(info.features.sm4);
51 EXPECT_FALSE(info.features.asimddp);
52 EXPECT_FALSE(info.features.sha512);
53 EXPECT_FALSE(info.features.sve);
54 EXPECT_FALSE(info.features.asimdfhm);
55 EXPECT_FALSE(info.features.dit);
56 EXPECT_FALSE(info.features.uscat);
57 EXPECT_FALSE(info.features.ilrcpc);
58 EXPECT_FALSE(info.features.flagm);
59 EXPECT_FALSE(info.features.ssbs);
60 EXPECT_FALSE(info.features.sb);
61 EXPECT_FALSE(info.features.paca);
62 EXPECT_FALSE(info.features.pacg);
63 }
64
TEST(CpuinfoAarch64Test,FromHardwareCap2)65 TEST(CpuinfoAarch64Test, FromHardwareCap2) {
66 ResetHwcaps();
67 SetHardwareCapabilities(AARCH64_HWCAP_FP,
68 AARCH64_HWCAP2_SVE2 | AARCH64_HWCAP2_BTI);
69 GetEmptyFilesystem(); // disabling /proc/cpuinfo
70 const auto info = GetAarch64Info();
71 EXPECT_TRUE(info.features.fp);
72
73 EXPECT_TRUE(info.features.sve2);
74 EXPECT_TRUE(info.features.bti);
75
76 EXPECT_FALSE(info.features.dcpodp);
77 EXPECT_FALSE(info.features.sveaes);
78 EXPECT_FALSE(info.features.svepmull);
79 EXPECT_FALSE(info.features.svebitperm);
80 EXPECT_FALSE(info.features.svesha3);
81 EXPECT_FALSE(info.features.svesm4);
82 EXPECT_FALSE(info.features.flagm2);
83 EXPECT_FALSE(info.features.frint);
84 EXPECT_FALSE(info.features.svei8mm);
85 EXPECT_FALSE(info.features.svef32mm);
86 EXPECT_FALSE(info.features.svef64mm);
87 EXPECT_FALSE(info.features.svebf16);
88 EXPECT_FALSE(info.features.i8mm);
89 EXPECT_FALSE(info.features.bf16);
90 EXPECT_FALSE(info.features.dgh);
91 EXPECT_FALSE(info.features.rng);
92 }
93
TEST(CpuinfoAarch64Test,ARMCortexA53)94 TEST(CpuinfoAarch64Test, ARMCortexA53) {
95 ResetHwcaps();
96 auto& fs = GetEmptyFilesystem();
97 fs.CreateFile("/proc/cpuinfo",
98 R"(Processor : AArch64 Processor rev 3 (aarch64)
99 processor : 0
100 processor : 1
101 processor : 2
102 processor : 3
103 processor : 4
104 processor : 5
105 processor : 6
106 processor : 7
107 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
108 CPU implementer : 0x41
109 CPU architecture: AArch64
110 CPU variant : 0x0
111 CPU part : 0xd03
112 CPU revision : 3)");
113 const auto info = GetAarch64Info();
114 EXPECT_EQ(info.implementer, 0x41);
115 EXPECT_EQ(info.variant, 0x0);
116 EXPECT_EQ(info.part, 0xd03);
117 EXPECT_EQ(info.revision, 3);
118
119 EXPECT_TRUE(info.features.fp);
120 EXPECT_TRUE(info.features.asimd);
121 EXPECT_TRUE(info.features.evtstrm);
122 EXPECT_TRUE(info.features.aes);
123 EXPECT_TRUE(info.features.pmull);
124 EXPECT_TRUE(info.features.sha1);
125 EXPECT_TRUE(info.features.sha2);
126 EXPECT_TRUE(info.features.crc32);
127
128 EXPECT_FALSE(info.features.atomics);
129 EXPECT_FALSE(info.features.fphp);
130 EXPECT_FALSE(info.features.asimdhp);
131 EXPECT_FALSE(info.features.cpuid);
132 EXPECT_FALSE(info.features.asimdrdm);
133 EXPECT_FALSE(info.features.jscvt);
134 EXPECT_FALSE(info.features.fcma);
135 EXPECT_FALSE(info.features.lrcpc);
136 EXPECT_FALSE(info.features.dcpop);
137 EXPECT_FALSE(info.features.sha3);
138 EXPECT_FALSE(info.features.sm3);
139 EXPECT_FALSE(info.features.sm4);
140 EXPECT_FALSE(info.features.asimddp);
141 EXPECT_FALSE(info.features.sha512);
142 EXPECT_FALSE(info.features.sve);
143 EXPECT_FALSE(info.features.asimdfhm);
144 EXPECT_FALSE(info.features.dit);
145 EXPECT_FALSE(info.features.uscat);
146 EXPECT_FALSE(info.features.ilrcpc);
147 EXPECT_FALSE(info.features.flagm);
148 EXPECT_FALSE(info.features.ssbs);
149 EXPECT_FALSE(info.features.sb);
150 EXPECT_FALSE(info.features.paca);
151 EXPECT_FALSE(info.features.pacg);
152 EXPECT_FALSE(info.features.dcpodp);
153 EXPECT_FALSE(info.features.sve2);
154 EXPECT_FALSE(info.features.sveaes);
155 EXPECT_FALSE(info.features.svepmull);
156 EXPECT_FALSE(info.features.svebitperm);
157 EXPECT_FALSE(info.features.svesha3);
158 EXPECT_FALSE(info.features.svesm4);
159 EXPECT_FALSE(info.features.flagm2);
160 EXPECT_FALSE(info.features.frint);
161 EXPECT_FALSE(info.features.svei8mm);
162 EXPECT_FALSE(info.features.svef32mm);
163 EXPECT_FALSE(info.features.svef64mm);
164 EXPECT_FALSE(info.features.svebf16);
165 EXPECT_FALSE(info.features.i8mm);
166 EXPECT_FALSE(info.features.bf16);
167 EXPECT_FALSE(info.features.dgh);
168 EXPECT_FALSE(info.features.rng);
169 EXPECT_FALSE(info.features.bti);
170 EXPECT_FALSE(info.features.mte);
171 }
172
173 } // namespace
174 } // namespace cpu_features
175