blob: 0b6b02fbe4d1b7a5642a32ecfdf5bc06886dbb3d [file] [log] [blame]
David Benjamin8cd61f72018-08-31 15:37:56 -05001/* Copyright (c) 2018, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
David Benjamin295b3132021-12-21 18:54:15 -050015#include "cpu_arm_linux.h"
David Benjamin8cd61f72018-08-31 15:37:56 -050016
17#include <string.h>
18
19#include <gtest/gtest.h>
20
21
David Benjamin8cd61f72018-08-31 15:37:56 -050022TEST(ARMLinuxTest, CPUInfo) {
Adam Langley3faf3db2018-09-06 07:10:15 -070023 struct CPUInfoTest {
David Benjamin8cd61f72018-08-31 15:37:56 -050024 const char *cpuinfo;
David Benjamin8cd61f72018-08-31 15:37:56 -050025 unsigned long hwcap2;
David Benjamin8cd61f72018-08-31 15:37:56 -050026 } kTests[] = {
David Benjamin8cd61f72018-08-31 15:37:56 -050027 // Nexus 4 from https://crbug.com/341598#c43
28 {
29 "Processor : ARMv7 Processor rev 2 (v7l)\n"
30 "processor : 0\n"
31 "BogoMIPS : 13.53\n"
32 "\n"
33 "processor : 1\n"
34 "BogoMIPS : 13.53\n"
35 "\n"
36 "processor : 2\n"
37 "BogoMIPS : 13.53\n"
38 "\n"
39 "processor : 3\n"
40 "BogoMIPS : 13.53\n"
41 "\n"
42 "Features : swp half thumb fastmult vfp edsp neon vfpv3 tls "
43 "vfpv4 \n"
44 "CPU implementer : 0x51\n"
45 "CPU architecture: 7\n"
46 "CPU variant : 0x0\n"
47 "CPU part : 0x06f\n"
48 "CPU revision : 2\n"
49 "\n"
50 "Hardware : QCT APQ8064 MAKO\n"
51 "Revision : 000b\n"
52 "Serial : 0000000000000000\n",
David Benjamin8cd61f72018-08-31 15:37:56 -050053 0,
David Benjamin8cd61f72018-08-31 15:37:56 -050054 },
55 // Pixel 2 (truncated slightly)
56 {
57 "Processor : AArch64 Processor rev 1 (aarch64)\n"
58 "processor : 0\n"
59 "BogoMIPS : 38.00\n"
60 "Features : fp asimd evtstrm aes pmull sha1 sha2 crc32\n"
61 "CPU implementer : 0x51\n"
62 "CPU architecture: 8\n"
63 "CPU variant : 0xa\n"
64 "CPU part : 0x801\n"
65 "CPU revision : 4\n"
66 "\n"
67 "processor : 1\n"
68 "BogoMIPS : 38.00\n"
69 "Features : fp asimd evtstrm aes pmull sha1 sha2 crc32\n"
70 "CPU implementer : 0x51\n"
71 "CPU architecture: 8\n"
72 "CPU variant : 0xa\n"
73 "CPU part : 0x801\n"
74 "CPU revision : 4\n"
75 "\n"
76 "processor : 2\n"
77 "BogoMIPS : 38.00\n"
78 "Features : fp asimd evtstrm aes pmull sha1 sha2 crc32\n"
79 "CPU implementer : 0x51\n"
80 "CPU architecture: 8\n"
81 "CPU variant : 0xa\n"
82 "CPU part : 0x801\n"
83 "CPU revision : 4\n"
84 "\n"
85 "processor : 3\n"
86 "BogoMIPS : 38.00\n"
87 "Features : fp asimd evtstrm aes pmull sha1 sha2 crc32\n"
88 "CPU implementer : 0x51\n"
89 "CPU architecture: 8\n"
90 "CPU variant : 0xa\n"
91 "CPU part : 0x801\n"
92 "CPU revision : 4\n"
93 // (Extra processors omitted.)
94 "\n"
95 "Hardware : Qualcomm Technologies, Inc MSM8998\n",
David Benjamin8cd61f72018-08-31 15:37:56 -050096 HWCAP2_AES | HWCAP2_PMULL | HWCAP2_SHA1 | HWCAP2_SHA2,
David Benjamin8cd61f72018-08-31 15:37:56 -050097 },
David Benjamin8cd61f72018-08-31 15:37:56 -050098 // Garbage should be tolerated.
99 {
100 "Blah blah blah this is definitely an ARM CPU",
101 0,
David Benjamin8cd61f72018-08-31 15:37:56 -0500102 },
103 // A hypothetical ARMv8 CPU without crc32 (and thus no trailing space
104 // after the last crypto entry).
105 {
106 "Features : aes pmull sha1 sha2\n"
107 "CPU architecture: 8\n",
David Benjamin8cd61f72018-08-31 15:37:56 -0500108 HWCAP2_AES | HWCAP2_PMULL | HWCAP2_SHA1 | HWCAP2_SHA2,
David Benjamin8cd61f72018-08-31 15:37:56 -0500109 },
110 // Various combinations of ARMv8 flags.
111 {
112 "Features : aes sha1 sha2\n"
113 "CPU architecture: 8\n",
David Benjamin8cd61f72018-08-31 15:37:56 -0500114 HWCAP2_AES | HWCAP2_SHA1 | HWCAP2_SHA2,
David Benjamin8cd61f72018-08-31 15:37:56 -0500115 },
116 {
117 "Features : pmull sha2\n"
118 "CPU architecture: 8\n",
David Benjamin8cd61f72018-08-31 15:37:56 -0500119 HWCAP2_PMULL | HWCAP2_SHA2,
David Benjamin8cd61f72018-08-31 15:37:56 -0500120 },
121 {
122 "Features : aes aes aes not_aes aes aes \n"
123 "CPU architecture: 8\n",
David Benjamin8cd61f72018-08-31 15:37:56 -0500124 HWCAP2_AES,
David Benjamin8cd61f72018-08-31 15:37:56 -0500125 },
126 {
127 "Features : \n"
128 "CPU architecture: 8\n",
David Benjamin8cd61f72018-08-31 15:37:56 -0500129 0,
David Benjamin8cd61f72018-08-31 15:37:56 -0500130 },
131 {
132 "Features : nothing\n"
133 "CPU architecture: 8\n",
David Benjamin8cd61f72018-08-31 15:37:56 -0500134 0,
David Benjamin8cd61f72018-08-31 15:37:56 -0500135 },
David Benjamin9cf9d3e2020-05-08 18:28:07 -0400136 // If opening /proc/cpuinfo fails, we process the empty string.
137 {
138 "",
139 0,
David Benjamin9cf9d3e2020-05-08 18:28:07 -0400140 },
David Benjamin8cd61f72018-08-31 15:37:56 -0500141 };
142
143 for (const auto &t : kTests) {
144 SCOPED_TRACE(t.cpuinfo);
145 STRING_PIECE sp = {t.cpuinfo, strlen(t.cpuinfo)};
David Benjamin8cd61f72018-08-31 15:37:56 -0500146 EXPECT_EQ(t.hwcap2, crypto_get_arm_hwcap2_from_cpuinfo(&sp));
David Benjamin8cd61f72018-08-31 15:37:56 -0500147 }
148}