Add translations for various sub-directories
[binutils-gdb.git] / gdb / testsuite / gdb.reverse / insn-reverse-x86.c
blob4213e4075c97d045bd3dfcfcb8dd8007d661189d
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/>. */
18 #include <cpuid.h>
19 #include <stdint.h>
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. */
30 static void
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;
36 unsigned int vendor;
37 unsigned int max_level;
39 max_level = __get_cpuid_max (0, &vendor);
41 if (max_level < 1)
42 return;
44 __cpuid (1, eax, ebx, ecx, edx);
46 supports_rdrand = ((ecx & rdrand_mask) == rdrand_mask);
48 if (max_level >= 7)
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. */
57 void
58 rdrand (void)
60 /* Get a random number from the rdrand assembly instruction. */
61 register uint64_t number;
63 if (!supports_rdrand)
64 return;
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\
73 rdrand %%di;\n\
74 mov %%ax, %%di;" : : : "%ax");
76 __asm__ volatile ("mov %%si, %%ax;\n\
77 rdrand %%si;\n\
78 mov %%ax, %%si;" : : : "%ax");
80 __asm__ volatile ("mov %%bp, %%ax;\n\
81 rdrand %%bp;\n\
82 mov %%ax, %%bp;" : : : "%ax");
84 __asm__ volatile ("mov %%sp, %%ax;\n\
85 rdrand %%sp;\n\
86 mov %%ax, %%sp;" : : : "%ax");
88 #ifdef __x86_64__
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");
97 #endif
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");
105 #ifdef __x86_64__
106 __asm__ volatile ("mov %%rdi, %%rax;\n\
107 rdrand %%edi;\n\
108 mov %%rax, %%rdi;" : : : "%rax");
110 __asm__ volatile ("mov %%rsi, %%rax;\n\
111 rdrand %%esi;\n\
112 mov %%rax, %%rsi;" : : : "%rax");
114 __asm__ volatile ("mov %%rbp, %%rax;\n\
115 rdrand %%ebp;\n\
116 mov %%rax, %%rbp;" : : : "%rax");
118 __asm__ volatile ("mov %%rsp, %%rax;\n\
119 rdrand %%esp;\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\
138 rdrand %%rdi;\n\
139 mov %%rax, %%rdi;" : : : "%rax");
141 __asm__ volatile ("mov %%rsi, %%rax;\n\
142 rdrand %%rsi;\n\
143 mov %%rax, %%rsi;" : : : "%rax");
145 __asm__ volatile ("mov %%rbp, %%rax;\n\
146 rdrand %%rbp;\n\
147 mov %%rax, %%rbp;" : : : "%rax");
149 __asm__ volatile ("mov %%rsp, %%rax;\n\
150 rdrand %%rsp;\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");
161 #endif
164 /* Test rdseed support for various output registers. */
166 void
167 rdseed (void)
169 /* Get a random seed from the rdseed assembly instruction. */
170 register long seed;
172 if (!supports_rdseed)
173 return;
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\
182 rdseed %%di;\n\
183 mov %%ax, %%di;" : : : "%ax");
185 __asm__ volatile ("mov %%si, %%ax;\n\
186 rdseed %%si;\n\
187 mov %%ax, %%si;" : : : "%ax");
189 __asm__ volatile ("mov %%bp, %%ax;\n\
190 rdseed %%bp;\n\
191 mov %%ax, %%bp;" : : : "%ax");
193 __asm__ volatile ("mov %%sp, %%ax;\n\
194 rdseed %%sp;\n\
195 mov %%ax, %%sp;" : : : "%ax");
197 #ifdef __x86_64__
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");
206 #endif
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");
214 #ifdef __x86_64__
215 __asm__ volatile ("mov %%rdi, %%rax;\n\
216 rdseed %%edi;\n\
217 mov %%rax, %%rdi;" : : : "%rax");
219 __asm__ volatile ("mov %%rsi, %%rax;\n\
220 rdseed %%esi;\n\
221 mov %%rax, %%rsi;" : : : "%rax");
223 __asm__ volatile ("mov %%rbp, %%rax;\n\
224 rdseed %%ebp;\n\
225 mov %%rax, %%rbp;" : : : "%rax");
227 __asm__ volatile ("mov %%rsp, %%rax;\n\
228 rdseed %%esp;\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\
247 rdseed %%rdi;\n\
248 mov %%rax, %%rdi;" : : : "%rax");
250 __asm__ volatile ("mov %%rsi, %%rax;\n\
251 rdseed %%rsi;\n\
252 mov %%rax, %%rsi;" : : : "%rax");
254 __asm__ volatile ("mov %%rbp, %%rax;\n\
255 rdseed %%rbp;\n\
256 mov %%rax, %%rbp;" : : : "%rax");
258 __asm__ volatile ("mov %%rsp, %%rax;\n\
259 rdseed %%rsp;\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");
270 #endif
273 /* Test rdtscp support. */
275 void
276 rdtscp (void)
278 #ifdef __x86_64__
279 __asm__ volatile ("rdtscp");
280 #endif
283 /* Initialize arch-specific bits. */
285 static void
286 initialize (void)
288 /* Initialize supported features. */
289 check_supported_features ();
292 /* Functions testing instruction decodings. GDB will test all of these. */
293 static testcase_ftype testcases[] =
295 rdrand,
296 rdseed,
297 rdtscp