1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2017-2022 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;" : "=r" (number
));
68 __asm__
volatile ("rdrand %%bx;" : "=r" (number
));
69 __asm__
volatile ("rdrand %%cx;" : "=r" (number
));
70 __asm__
volatile ("rdrand %%dx;" : "=r" (number
));
72 __asm__
volatile ("mov %%di, %%ax;" : "=r" (number
));
73 __asm__
volatile ("rdrand %%di;" : "=r" (number
));
74 __asm__
volatile ("mov %%ax, %%di;" : "=r" (number
));
76 __asm__
volatile ("mov %%si, %%ax;" : "=r" (number
));
77 __asm__
volatile ("rdrand %%si;" : "=r" (number
));
78 __asm__
volatile ("mov %%ax, %%si;" : "=r" (number
));
80 __asm__
volatile ("mov %%bp, %%ax;" : "=r" (number
));
81 __asm__
volatile ("rdrand %%bp;" : "=r" (number
));
82 __asm__
volatile ("mov %%ax, %%bp;" : "=r" (number
));
84 __asm__
volatile ("mov %%sp, %%ax;" : "=r" (number
));
85 __asm__
volatile ("rdrand %%sp;" : "=r" (number
));
86 __asm__
volatile ("mov %%ax, %%sp;" : "=r" (number
));
89 __asm__
volatile ("rdrand %%r8w;" : "=r" (number
));
90 __asm__
volatile ("rdrand %%r9w;" : "=r" (number
));
91 __asm__
volatile ("rdrand %%r10w;" : "=r" (number
));
92 __asm__
volatile ("rdrand %%r11w;" : "=r" (number
));
93 __asm__
volatile ("rdrand %%r12w;" : "=r" (number
));
94 __asm__
volatile ("rdrand %%r13w;" : "=r" (number
));
95 __asm__
volatile ("rdrand %%r14w;" : "=r" (number
));
96 __asm__
volatile ("rdrand %%r15w;" : "=r" (number
));
99 /* 32-bit random numbers. */
100 __asm__
volatile ("rdrand %%eax;" : "=r" (number
));
101 __asm__
volatile ("rdrand %%ebx;" : "=r" (number
));
102 __asm__
volatile ("rdrand %%ecx;" : "=r" (number
));
103 __asm__
volatile ("rdrand %%edx;" : "=r" (number
));
106 __asm__
volatile ("mov %%rdi, %%rax;" : "=r" (number
));
107 __asm__
volatile ("rdrand %%edi;" : "=r" (number
));
108 __asm__
volatile ("mov %%rax, %%rdi;" : "=r" (number
));
110 __asm__
volatile ("mov %%rsi, %%rax;" : "=r" (number
));
111 __asm__
volatile ("rdrand %%esi;" : "=r" (number
));
112 __asm__
volatile ("mov %%rax, %%rsi;" : "=r" (number
));
114 __asm__
volatile ("mov %%rbp, %%rax;" : "=r" (number
));
115 __asm__
volatile ("rdrand %%ebp;" : "=r" (number
));
116 __asm__
volatile ("mov %%rax, %%rbp;" : "=r" (number
));
118 __asm__
volatile ("mov %%rsp, %%rax;" : "=r" (number
));
119 __asm__
volatile ("rdrand %%esp;" : "=r" (number
));
120 __asm__
volatile ("mov %%rax, %%rsp;" : "=r" (number
));
122 __asm__
volatile ("rdrand %%r8d;" : "=r" (number
));
123 __asm__
volatile ("rdrand %%r9d;" : "=r" (number
));
124 __asm__
volatile ("rdrand %%r10d;" : "=r" (number
));
125 __asm__
volatile ("rdrand %%r11d;" : "=r" (number
));
126 __asm__
volatile ("rdrand %%r12d;" : "=r" (number
));
127 __asm__
volatile ("rdrand %%r13d;" : "=r" (number
));
128 __asm__
volatile ("rdrand %%r14d;" : "=r" (number
));
129 __asm__
volatile ("rdrand %%r15d;" : "=r" (number
));
131 /* 64-bit random numbers. */
132 __asm__
volatile ("rdrand %%rax;" : "=r" (number
));
133 __asm__
volatile ("rdrand %%rbx;" : "=r" (number
));
134 __asm__
volatile ("rdrand %%rcx;" : "=r" (number
));
135 __asm__
volatile ("rdrand %%rdx;" : "=r" (number
));
137 __asm__
volatile ("mov %%rdi, %%rax;" : "=r" (number
));
138 __asm__
volatile ("rdrand %%rdi;" : "=r" (number
));
139 __asm__
volatile ("mov %%rax, %%rdi;" : "=r" (number
));
141 __asm__
volatile ("mov %%rsi, %%rax;" : "=r" (number
));
142 __asm__
volatile ("rdrand %%rsi;" : "=r" (number
));
143 __asm__
volatile ("mov %%rax, %%rsi;" : "=r" (number
));
145 __asm__
volatile ("mov %%rbp, %%rax;" : "=r" (number
));
146 __asm__
volatile ("rdrand %%rbp;" : "=r" (number
));
147 __asm__
volatile ("mov %%rax, %%rbp;" : "=r" (number
));
149 __asm__
volatile ("mov %%rsp, %%rax;" : "=r" (number
));
150 __asm__
volatile ("rdrand %%rsp;" : "=r" (number
));
151 __asm__
volatile ("mov %%rax, %%rsp;" : "=r" (number
));
153 __asm__
volatile ("rdrand %%r8;" : "=r" (number
));
154 __asm__
volatile ("rdrand %%r9;" : "=r" (number
));
155 __asm__
volatile ("rdrand %%r10;" : "=r" (number
));
156 __asm__
volatile ("rdrand %%r11;" : "=r" (number
));
157 __asm__
volatile ("rdrand %%r12;" : "=r" (number
));
158 __asm__
volatile ("rdrand %%r13;" : "=r" (number
));
159 __asm__
volatile ("rdrand %%r14;" : "=r" (number
));
160 __asm__
volatile ("rdrand %%r15;" : "=r" (number
));
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;" : "=r" (seed
));
177 __asm__
volatile ("rdseed %%bx;" : "=r" (seed
));
178 __asm__
volatile ("rdseed %%cx;" : "=r" (seed
));
179 __asm__
volatile ("rdseed %%dx;" : "=r" (seed
));
181 __asm__
volatile ("mov %%di, %%ax;" : "=r" (seed
));
182 __asm__
volatile ("rdseed %%di;" : "=r" (seed
));
183 __asm__
volatile ("mov %%ax, %%di;" : "=r" (seed
));
185 __asm__
volatile ("mov %%si, %%ax;" : "=r" (seed
));
186 __asm__
volatile ("rdseed %%si;" : "=r" (seed
));
187 __asm__
volatile ("mov %%ax, %%si;" : "=r" (seed
));
189 __asm__
volatile ("mov %%bp, %%ax;" : "=r" (seed
));
190 __asm__
volatile ("rdseed %%bp;" : "=r" (seed
));
191 __asm__
volatile ("mov %%ax, %%bp;" : "=r" (seed
));
193 __asm__
volatile ("mov %%sp, %%ax;" : "=r" (seed
));
194 __asm__
volatile ("rdseed %%sp;" : "=r" (seed
));
195 __asm__
volatile ("mov %%ax, %%sp;" : "=r" (seed
));
198 __asm__
volatile ("rdseed %%r8w;" : "=r" (seed
));
199 __asm__
volatile ("rdseed %%r9w;" : "=r" (seed
));
200 __asm__
volatile ("rdseed %%r10w;" : "=r" (seed
));
201 __asm__
volatile ("rdseed %%r11w;" : "=r" (seed
));
202 __asm__
volatile ("rdseed %%r12w;" : "=r" (seed
));
203 __asm__
volatile ("rdseed %%r13w;" : "=r" (seed
));
204 __asm__
volatile ("rdseed %%r14w;" : "=r" (seed
));
205 __asm__
volatile ("rdseed %%r15w;" : "=r" (seed
));
208 /* 32-bit random seeds. */
209 __asm__
volatile ("rdseed %%eax;" : "=r" (seed
));
210 __asm__
volatile ("rdseed %%ebx;" : "=r" (seed
));
211 __asm__
volatile ("rdseed %%ecx;" : "=r" (seed
));
212 __asm__
volatile ("rdseed %%edx;" : "=r" (seed
));
215 __asm__
volatile ("mov %%rdi, %%rax;" : "=r" (seed
));
216 __asm__
volatile ("rdseed %%edi;" : "=r" (seed
));
217 __asm__
volatile ("mov %%rax, %%rdi;" : "=r" (seed
));
219 __asm__
volatile ("mov %%rsi, %%rax;" : "=r" (seed
));
220 __asm__
volatile ("rdseed %%esi;" : "=r" (seed
));
221 __asm__
volatile ("mov %%rax, %%rsi;" : "=r" (seed
));
223 __asm__
volatile ("mov %%rbp, %%rax;" : "=r" (seed
));
224 __asm__
volatile ("rdseed %%ebp;" : "=r" (seed
));
225 __asm__
volatile ("mov %%rax, %%rbp;" : "=r" (seed
));
227 __asm__
volatile ("mov %%rsp, %%rax;" : "=r" (seed
));
228 __asm__
volatile ("rdseed %%esp;" : "=r" (seed
));
229 __asm__
volatile ("mov %%rax, %%rsp;" : "=r" (seed
));
231 __asm__
volatile ("rdseed %%r8d;" : "=r" (seed
));
232 __asm__
volatile ("rdseed %%r9d;" : "=r" (seed
));
233 __asm__
volatile ("rdseed %%r10d;" : "=r" (seed
));
234 __asm__
volatile ("rdseed %%r11d;" : "=r" (seed
));
235 __asm__
volatile ("rdseed %%r12d;" : "=r" (seed
));
236 __asm__
volatile ("rdseed %%r13d;" : "=r" (seed
));
237 __asm__
volatile ("rdseed %%r14d;" : "=r" (seed
));
238 __asm__
volatile ("rdseed %%r15d;" : "=r" (seed
));
240 /* 64-bit random seeds. */
241 __asm__
volatile ("rdseed %%rax;" : "=r" (seed
));
242 __asm__
volatile ("rdseed %%rbx;" : "=r" (seed
));
243 __asm__
volatile ("rdseed %%rcx;" : "=r" (seed
));
244 __asm__
volatile ("rdseed %%rdx;" : "=r" (seed
));
246 __asm__
volatile ("mov %%rdi, %%rax;" : "=r" (seed
));
247 __asm__
volatile ("rdseed %%rdi;" : "=r" (seed
));
248 __asm__
volatile ("mov %%rax, %%rdi;" : "=r" (seed
));
250 __asm__
volatile ("mov %%rsi, %%rax;" : "=r" (seed
));
251 __asm__
volatile ("rdseed %%rsi;" : "=r" (seed
));
252 __asm__
volatile ("mov %%rax, %%rsi;" : "=r" (seed
));
254 __asm__
volatile ("mov %%rbp, %%rax;" : "=r" (seed
));
255 __asm__
volatile ("rdseed %%rbp;" : "=r" (seed
));
256 __asm__
volatile ("mov %%rax, %%rbp;" : "=r" (seed
));
258 __asm__
volatile ("mov %%rsp, %%rax;" : "=r" (seed
));
259 __asm__
volatile ("rdseed %%rsp;" : "=r" (seed
));
260 __asm__
volatile ("mov %%rax, %%rsp;" : "=r" (seed
));
262 __asm__
volatile ("rdseed %%r8;" : "=r" (seed
));
263 __asm__
volatile ("rdseed %%r9;" : "=r" (seed
));
264 __asm__
volatile ("rdseed %%r10;" : "=r" (seed
));
265 __asm__
volatile ("rdseed %%r11;" : "=r" (seed
));
266 __asm__
volatile ("rdseed %%r12;" : "=r" (seed
));
267 __asm__
volatile ("rdseed %%r13;" : "=r" (seed
));
268 __asm__
volatile ("rdseed %%r14;" : "=r" (seed
));
269 __asm__
volatile ("rdseed %%r15;" : "=r" (seed
));
273 /* Initialize arch-specific bits. */
278 /* Initialize supported features. */
279 check_supported_features ();
282 /* Functions testing instruction decodings. GDB will test all of these. */
283 static testcase_ftype testcases
[] =