1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2017-2024 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* 0 if the CPU supports rdrand and non-zero otherwise. */
22 static unsigned int supports_rdrand
;
24 /* 0 if the CPU supports rdseed and non-zero otherwise. */
25 static unsigned int supports_rdseed
;
27 /* Check supported features and set globals accordingly. The globals
28 can be used to prevent unsupported tests from running. */
31 check_supported_features (void)
33 unsigned int rdrand_mask
= (1 << 30);
34 unsigned int rdseed_mask
= (1 << 18);
35 unsigned int eax
, ebx
, ecx
, edx
;
37 unsigned int max_level
;
39 max_level
= __get_cpuid_max (0, &vendor
);
44 __cpuid (1, eax
, ebx
, ecx
, edx
);
46 supports_rdrand
= ((ecx
& rdrand_mask
) == rdrand_mask
);
50 __cpuid_count (7, 0, eax
, ebx
, ecx
, edx
);
51 supports_rdseed
= ((ebx
& rdseed_mask
) == rdseed_mask
);
55 /* Test rdrand support for various output registers. */
60 /* Get a random number from the rdrand assembly instruction. */
61 register uint64_t number
;
66 /* 16-bit random numbers. */
67 __asm__
volatile ("rdrand %%ax;": : : "%ax");
68 __asm__
volatile ("rdrand %%bx;": : : "%bx");
69 __asm__
volatile ("rdrand %%cx;": : : "%cx");
70 __asm__
volatile ("rdrand %%dx;": : : "%dx");
72 __asm__
volatile ("mov %%di, %%ax;\n\
74 mov %%ax, %%di;" : : : "%ax");
76 __asm__
volatile ("mov %%si, %%ax;\n\
78 mov %%ax, %%si;" : : : "%ax");
80 __asm__
volatile ("mov %%bp, %%ax;\n\
82 mov %%ax, %%bp;" : : : "%ax");
84 __asm__
volatile ("mov %%sp, %%ax;\n\
86 mov %%ax, %%sp;" : : : "%ax");
89 __asm__
volatile ("rdrand %%r8w;": : : "%r8");
90 __asm__
volatile ("rdrand %%r9w;": : : "%r9");
91 __asm__
volatile ("rdrand %%r10w;": : : "%r10");
92 __asm__
volatile ("rdrand %%r11w;": : : "%r11");
93 __asm__
volatile ("rdrand %%r12w;": : : "%r12");
94 __asm__
volatile ("rdrand %%r13w;": : : "%r13");
95 __asm__
volatile ("rdrand %%r14w;": : : "%r14");
96 __asm__
volatile ("rdrand %%r15w;": : : "%r15");
99 /* 32-bit random numbers. */
100 __asm__
volatile ("rdrand %%eax;": : : "%eax");
101 __asm__
volatile ("rdrand %%ebx;": : : "%ebx");
102 __asm__
volatile ("rdrand %%ecx;": : : "%ecx");
103 __asm__
volatile ("rdrand %%edx;": : : "%edx");
106 __asm__
volatile ("mov %%rdi, %%rax;\n\
108 mov %%rax, %%rdi;" : : : "%rax");
110 __asm__
volatile ("mov %%rsi, %%rax;\n\
112 mov %%rax, %%rsi;" : : : "%rax");
114 __asm__
volatile ("mov %%rbp, %%rax;\n\
116 mov %%rax, %%rbp;" : : : "%rax");
118 __asm__
volatile ("mov %%rsp, %%rax;\n\
120 mov %%rax, %%rsp;" : : : "%rax");
122 __asm__
volatile ("rdrand %%r8d;": : : "%r8");
123 __asm__
volatile ("rdrand %%r9d;": : : "%r9");
124 __asm__
volatile ("rdrand %%r10d;": : : "%r10");
125 __asm__
volatile ("rdrand %%r11d;": : : "%r11");
126 __asm__
volatile ("rdrand %%r12d;": : : "%r12");
127 __asm__
volatile ("rdrand %%r13d;": : : "%r13");
128 __asm__
volatile ("rdrand %%r14d;": : : "%r14");
129 __asm__
volatile ("rdrand %%r15d;": : : "%r15");
131 /* 64-bit random numbers. */
132 __asm__
volatile ("rdrand %%rax;": : : "%rax");
133 __asm__
volatile ("rdrand %%rbx;": : : "%rbx");
134 __asm__
volatile ("rdrand %%rcx;": : : "%rcx");
135 __asm__
volatile ("rdrand %%rdx;": : : "%rdx");
137 __asm__
volatile ("mov %%rdi, %%rax;\n\
139 mov %%rax, %%rdi;" : : : "%rax");
141 __asm__
volatile ("mov %%rsi, %%rax;\n\
143 mov %%rax, %%rsi;" : : : "%rax");
145 __asm__
volatile ("mov %%rbp, %%rax;\n\
147 mov %%rax, %%rbp;" : : : "%rax");
149 __asm__
volatile ("mov %%rsp, %%rax;\n\
151 mov %%rax, %%rsp;" : : : "%rax");
153 __asm__
volatile ("rdrand %%r8;": : : "%r8");
154 __asm__
volatile ("rdrand %%r9;": : : "%r9");
155 __asm__
volatile ("rdrand %%r10;": : : "%r10");
156 __asm__
volatile ("rdrand %%r11;": : : "%r11");
157 __asm__
volatile ("rdrand %%r12;": : : "%r12");
158 __asm__
volatile ("rdrand %%r13;": : : "%r13");
159 __asm__
volatile ("rdrand %%r14;": : : "%r14");
160 __asm__
volatile ("rdrand %%r15;": : : "%r15");
164 /* Test rdseed support for various output registers. */
169 /* Get a random seed from the rdseed assembly instruction. */
172 if (!supports_rdseed
)
175 /* 16-bit random seeds. */
176 __asm__
volatile ("rdseed %%ax;": : : "%ax");
177 __asm__
volatile ("rdseed %%bx;": : : "%bx");
178 __asm__
volatile ("rdseed %%cx;": : : "%cx");
179 __asm__
volatile ("rdseed %%dx;": : : "%dx");
181 __asm__
volatile ("mov %%di, %%ax;\n\
183 mov %%ax, %%di;" : : : "%ax");
185 __asm__
volatile ("mov %%si, %%ax;\n\
187 mov %%ax, %%si;" : : : "%ax");
189 __asm__
volatile ("mov %%bp, %%ax;\n\
191 mov %%ax, %%bp;" : : : "%ax");
193 __asm__
volatile ("mov %%sp, %%ax;\n\
195 mov %%ax, %%sp;" : : : "%ax");
198 __asm__
volatile ("rdseed %%r8w;": : : "%r8");
199 __asm__
volatile ("rdseed %%r9w;": : : "%r9");
200 __asm__
volatile ("rdseed %%r10w;": : : "%r10");
201 __asm__
volatile ("rdseed %%r11w;": : : "%r11");
202 __asm__
volatile ("rdseed %%r12w;": : : "%r12");
203 __asm__
volatile ("rdseed %%r13w;": : : "%r13");
204 __asm__
volatile ("rdseed %%r14w;": : : "%r14");
205 __asm__
volatile ("rdseed %%r15w;": : : "%r15");
208 /* 32-bit random seeds. */
209 __asm__
volatile ("rdseed %%eax;": : : "%eax");
210 __asm__
volatile ("rdseed %%ebx;": : : "%ebx");
211 __asm__
volatile ("rdseed %%ecx;": : : "%ecx");
212 __asm__
volatile ("rdseed %%edx;": : : "%edx");
215 __asm__
volatile ("mov %%rdi, %%rax;\n\
217 mov %%rax, %%rdi;" : : : "%rax");
219 __asm__
volatile ("mov %%rsi, %%rax;\n\
221 mov %%rax, %%rsi;" : : : "%rax");
223 __asm__
volatile ("mov %%rbp, %%rax;\n\
225 mov %%rax, %%rbp;" : : : "%rax");
227 __asm__
volatile ("mov %%rsp, %%rax;\n\
229 mov %%rax, %%rsp;" : : : "%rax");
231 __asm__
volatile ("rdseed %%r8d;": : : "%r8");
232 __asm__
volatile ("rdseed %%r9d;": : : "%r9");
233 __asm__
volatile ("rdseed %%r10d;": : : "%r10");
234 __asm__
volatile ("rdseed %%r11d;": : : "%r11");
235 __asm__
volatile ("rdseed %%r12d;": : : "%r12");
236 __asm__
volatile ("rdseed %%r13d;": : : "%r13");
237 __asm__
volatile ("rdseed %%r14d;": : : "%r14");
238 __asm__
volatile ("rdseed %%r15d;": : : "%r15");
240 /* 64-bit random seeds. */
241 __asm__
volatile ("rdseed %%rax;": : : "%rax");
242 __asm__
volatile ("rdseed %%rbx;": : : "%rbx");
243 __asm__
volatile ("rdseed %%rcx;": : : "%rcx");
244 __asm__
volatile ("rdseed %%rdx;": : : "%rdx");
246 __asm__
volatile ("mov %%rdi, %%rax;\n\
248 mov %%rax, %%rdi;" : : : "%rax");
250 __asm__
volatile ("mov %%rsi, %%rax;\n\
252 mov %%rax, %%rsi;" : : : "%rax");
254 __asm__
volatile ("mov %%rbp, %%rax;\n\
256 mov %%rax, %%rbp;" : : : "%rax");
258 __asm__
volatile ("mov %%rsp, %%rax;\n\
260 mov %%rax, %%rsp;" : : : "%rax");
262 __asm__
volatile ("rdseed %%r8;": : : "%r8");
263 __asm__
volatile ("rdseed %%r9;": : : "%r9");
264 __asm__
volatile ("rdseed %%r10;": : : "%r10");
265 __asm__
volatile ("rdseed %%r11;": : : "%r11");
266 __asm__
volatile ("rdseed %%r12;": : : "%r12");
267 __asm__
volatile ("rdseed %%r13;": : : "%r13");
268 __asm__
volatile ("rdseed %%r14;": : : "%r14");
269 __asm__
volatile ("rdseed %%r15;": : : "%r15");
273 /* Test rdtscp support. */
279 __asm__
volatile ("rdtscp");
283 /* Initialize arch-specific bits. */
288 /* Initialize supported features. */
289 check_supported_features ();
292 /* Functions testing instruction decodings. GDB will test all of these. */
293 static testcase_ftype testcases
[] =