i386-avx-reverse.exp revision 1.1.1.1 1 # Copyright 2009-2023 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # This file is part of the gdb testsuite.
17
18 #
19 # This test tests i386 AVX instructions for reverse execution. This
20 # is supposed to test all supported instructions eventually.
21 #
22
23 require supports_reverse
24 require have_avx
25
26 # TODO: this is the case because I used xmm15 all over the test.
27 # Some parts of the test require xmm15 to validate some code paths, but
28 # that could be done only on 64bit targets and the rest could use xmm7
29 # instead.
30 if {![istarget "x86_64-*-*"]} {
31 verbose "avx-reverse requires 64 bit targets"
32 return
33 }
34
35 standard_testfile
36
37 # some targets have leading underscores on assembly symbols.
38 set additional_flags [gdb_target_symbol_prefix_flags]
39 lappend_include_file alloc_lib $srcdir/lib/precise-aligned-alloc.c
40
41 if {[prepare_for_testing "failed to prepare" $testfile $srcfile \
42 [list debug $additional_flags $alloc_lib]]} {
43 return -1
44 }
45
46 # Shorthand to test reversing through one instruction and
47 # testing if a register has the expected value.
48 # Prefix, if included, should end with a colon and space.
49
50 proc test_one_register {insn register value {prefix ""}} {
51 gdb_test "reverse-step" "$insn.*" \
52 "${prefix}reverse-step from $insn to test register $register"
53
54 if {[regexp {^ymm} $register]} {
55 set value "\\\{$value\\\}"
56 }
57
58 gdb_test "info register $register" \
59 "$register.*int128 = $value.*" \
60 "${prefix}verify $register before $insn"
61 }
62
63 # Shorthand to test reversing through one instruction and
64 # testing if a general purpose register has the expected value.
65 # Prefix, if included, should end with a colon and space.
66
67 proc test_one_general_register {insn register value {prefix ""}} {
68 gdb_test "reverse-step" "$insn.*" \
69 "${prefix}reverse-step from $insn to test register $register"
70
71 gdb_test "info register $register" \
72 "$register\\s+$value.*" \
73 "${prefix}verify $register before $insn"
74 }
75
76 # Shorthand to test reversing through one instruction and
77 # testing if a variable has the expected value.
78 # Prefix, if used, should end with a colon and space.
79
80 proc test_one_memory {insn mem value {dynamic false} {prefix ""}} {
81 gdb_test "reverse-step" "$insn.*" \
82 "${prefix}reverse-step from $insn to test memory $mem"
83
84 # For the dynamic buffer, we have to cast and dereference the pointer
85 set cast ""
86 if {$dynamic == true} {
87 set cast {(char [32]) *}
88 }
89
90 gdb_test "p/x $cast$mem" \
91 ".*$value.*" \
92 "${prefix}verify $mem before $insn"
93
94 }
95
96 # Record the execution for the whole function, and stop at its end
97 # to check if we can correctly reconstruct the state.
98 # In the source code, the function must be FUNCTION_test, and
99 # at the end, it must have a comment in the form:
100 # /* end FUNCTION_test */
101 # Returns true if the full function could be recorded, false otherwise.
102 proc record_full_function {function} {
103 set end [gdb_get_line_number "end ${function}_test "]
104 set start [gdb_get_line_number "start ${function}_test"]
105 gdb_breakpoint $start temporary
106 gdb_breakpoint $end temporary
107 gdb_continue_to_breakpoint "start ${function}_test"
108
109 if [supports_process_record] {
110 # Activate process record/replay.
111 gdb_test_no_output "record" "${function}: turn on process record"
112 }
113
114 # We return early in gdb_test_multiple because the rest of the
115 # function is aborting recording and cleaning up to put us back in
116 # a known location.
117 gdb_test_multiple "continue" "continue to end of ${function}_test" {
118 -re " end ${function}_test .*\r\n$::gdb_prompt $" {
119 pass $gdb_test_name
120 return true
121 }
122 -re " Illegal instruction.*\r\n$::gdb_prompt $" {
123 fail $gdb_test_name
124 }
125 -re "Process record does not support VEX instruction.*" {
126 fail $gdb_test_name
127 }
128 }
129
130 gdb_test "record stop" "Process record is stopped.*" \
131 "delete failed record history"
132 # If we didn't exit early, the temporary breakpoint at the end of
133 # the function hasn't been hit yet, so we can just continue.
134 gdb_continue_to_breakpoint "end ${function}_test" ".*end ${function}_test.*"
135
136 return false
137 }
138
139 runto_main
140
141 global hex
142 global decimal
143
144 # Record all the execution for vmov tests first.
145
146 if {[record_full_function "vmov"] == true} {
147 # Now execute backwards, checking all instructions.
148 test_one_register "vmovdqa" "ymm0" \
149 "0x2f2e2d2c2b2a29282726252423222120, 0x2f2e2d2c2b2a29282726252423222120" \
150 "from register: "
151 test_one_register "vmovdqu" "ymm15" \
152 "0x2f2e2d2c2b2a29282726252423222120, 0x2f2e2d2c2b2a29282726252423222120" \
153 "from register: "
154 test_one_register "vmovdqu" "ymm0" \
155 "0x2f2e2d2c2b2a29282726252423222120, 0x2f2e2d2c2b2a29282726252423222120" \
156 "from register: "
157
158 test_one_memory "vmovdqu" "dyn_buf1" \
159 "0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29" \
160 true "dynamic buffer: "
161 test_one_register "vmovdqu" "ymm0" \
162 "0x1f1e1d1c1b1a19181716151413121110, 0x1f1e1d1c1b1a19181716151413121110" \
163 "dynamic buffer: "
164 test_one_memory "vmovdqa" "dyn_buf1" "0x0 .repeats 32 times" true
165 test_one_register "vmovdqa" "ymm15" "0x0, 0x0"
166
167 # Don't check the full buffer because that'd be too long
168 test_one_memory "vmovdqu" "global_buf1" \
169 "0x0 .repeats 32 times" \
170 false "global buffer: "
171 test_one_register "vmovdqu" "ymm0" \
172 "0x3f3e3d3c3b3a39383736353433323130, 0x3f3e3d3c3b3a39383736353433323130" \
173 "global buffer: "
174
175 test_one_memory "vmovdqu" "buf1" "0x0 .repeats 32 times"
176 test_one_register "vmovdqu" "ymm0" "0x2726252423222120, 0x0" "local buffer: "
177
178 test_one_register "vmovq" "xmm15" "0x3736353433323130" "reg_reset: "
179 test_one_register "vmovq" "xmm15" "0x0"
180 test_one_register "vmovd" "xmm15" "0x33323130" "reg_reset: "
181 test_one_register "vmovd" "xmm15" "0x0"
182
183 with_test_prefix buffer_reset {
184 test_one_memory "vmovq" "dyn_buf1" \
185 "0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x0" true
186 test_one_memory "vmovq" "global_buf1" \
187 "0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x0"
188 test_one_memory "vmovq" "buf1" \
189 "0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x0"
190 }
191
192 with_test_prefix dynamic_buffers {
193 test_one_memory "vmovq" "dyn_buf1" "0x20, 0x21, 0x22, 0x23, 0x0" true
194 test_one_register "vmovq" "xmm0" "0x23222120"
195 test_one_memory "vmovd" "dyn_buf1" "0x0 .repeats 32 times" true
196 test_one_register "vmovd" "xmm0" "0x1716151413121110"
197 }
198
199 with_test_prefix global_buffers {
200 test_one_memory "vmovq" "global_buf1" "0x10, 0x11, 0x12, 0x13, 0x0"
201 test_one_register "vmovq" "xmm0" "0x13121110"
202 test_one_memory "vmovd" "global_buf1" "0x0 .repeats 32 times"
203 test_one_register "vmovd" "xmm0" "0x3736353433323130"
204 }
205
206 with_test_prefix local_buffers {
207 test_one_memory "vmovq" "buf1" "0x30, 0x31, 0x32, 0x33, 0x0"
208 test_one_register "vmovq" "xmm0" "0x33323130"
209 test_one_memory "vmovd" "buf1" "0x0 .repeats 32 times"
210 test_one_register "vmovd" "xmm0" "0xbeef"
211 }
212
213 # regular registers don't have uint128 members, so do it manually.
214 with_test_prefix registers {
215 test_one_register "vmovq" "xmm15" "0xbeef" "reset xmm15: "
216
217 test_one_register "vmovq" "xmm15" "0x0" "xmm0 to xmm15: "
218
219 gdb_test "reverse-step" "vmovd %xmm0, %rcx.*" \
220 "reverse step to check rcx recording"
221 gdb_test "print/x \$rcx" "= 0x0" "rcx was recorded"
222
223 test_one_register "vmovd" "xmm0" "0x0"
224 }
225
226 # Stop recording to get a clean history.
227 gdb_test "record stop" "Process record is stopped.*" \
228 "delete history for vmov_test"
229 } else {
230 untested "could not record vmov_test"
231 }
232
233 # Move to the end of vmov_test to set up next.
234 gdb_test "finish" "Run till exit from.*vmov_test.*" "leaving vmov_test"
235
236 # Starting vpunpck tests.
237 gdb_test_no_output \
238 "set \$ymm0.v4_int64 = {0x1716151413121110, 0x1f1e1d1c1b1a1918, 0x2726252423222120, 0x2f2e2d2c2b2a2928}"
239 gdb_test_no_output \
240 "set \$ymm1.v4_int64 = {0x3736353433323130, 0x3f3e3d3c3b3a3938, 0x4746454443424140, 0x4f4e4d4c4b4a4948}"
241 gdb_test_no_output "set \$ymm2.v2_int128 = {0x0, 0x0}"
242 gdb_test_no_output "set \$ymm15.v2_int128 = {0xdead, 0xbeef}"
243 if {[record_full_function "vpunpck"] == true} {
244 test_one_register "vpunpckhqdq" "ymm15" \
245 "0x1f1e1d1c3f3e3d3c1b1a19183b3a3938, 0x2f2e2d2c4f4e4d4c2b2a29284b4a4948" \
246 "ymm: "
247 test_one_register "vpunpckhdq" "ymm15" \
248 "0x17163736151435341312333211103130, 0x27264746252445442322434221204140" \
249 "ymm: "
250 test_one_register "vpunpcklwd" "ymm15" \
251 "0x17371636153514341333123211311030, 0x27472646254524442343224221412040" \
252 "ymm: "
253 test_one_register "vpunpcklbw" "ymm15" \
254 "0x1f1e3f3e1d1c3d3c1b1a3b3a19183938, 0x0" "ymm: "
255
256 test_one_register "vpunpckhqdq" "ymm2" \
257 "0x1f1e1d1c3f3e3d3c1b1a19183b3a3938, 0x0"
258 test_one_register "vpunpckhdq" "ymm2" \
259 "0x17161514131211103736353433323130, 0x0"
260 test_one_register "vpunpckhwd" "ymm15" \
261 "0x1f3f1e3e1d3d1c3c1b3b1a3a19391838, 0x0"
262 test_one_register "vpunpckhbw" "ymm15" \
263 "0x17163736151435341312333211103130, 0x0"
264
265 test_one_register "vpunpcklqdq" "ymm2" \
266 "0x17161514373635341312111033323130, 0x0"
267 test_one_register "vpunpckldq" "ymm2" "0x0, 0x0"
268 test_one_register "vpunpcklwd" "ymm15" \
269 "0x17371636153514341333123211311030, 0x0"
270 test_one_register "vpunpcklbw" "ymm15" "0xdead, 0xbeef"
271 } else {
272 untested "couldn't test vpunpck tests"
273 }
274
275 # Move to the end of vmov_test to set up next.
276 # Stop recording in case of recording errors.
277 gdb_test "record stop" "Process record is stopped.*" \
278 "delete history for vpunpck_test"
279 gdb_test "finish" "Run till exit from.*vpunpck_test.*" "leaving vpunpck_test"
280
281 # Start vpbroadcast tests
282 gdb_test_no_output "set \$ymm0.v2_int128 = {0x0, 0x0}" "set xmm0 for vpbroadcast"
283 gdb_test_no_output "set \$xmm1.v2_int64 = {0x1716151413121110, 0x1f1e1d1c1b1a1918}" \
284 "set xmm1 for vpbroadcast"
285 gdb_test_no_output "set \$ymm15.v2_int128 = {0x0, 0x0}" "set xmm15 for vpbroadcast"
286 if {[record_full_function "vpbroadcast"] == true} {
287 test_one_register "vpbroadcastq" "ymm15" "0x13121110131211101312111013121110, 0x0"
288 test_one_register "vpbroadcastq" "ymm0" "0x13121110131211101312111013121110, 0x0"
289
290 test_one_register "vpbroadcastd" "ymm15" \
291 "0x11101110111011101110111011101110, 0x11101110111011101110111011101110"
292 test_one_register "vpbroadcastd" "ymm0" \
293 "0x11101110111011101110111011101110, 0x11101110111011101110111011101110"
294
295 test_one_register "vpbroadcastw" "ymm15" "0x10101010101010101010101010101010, 0x0"
296 test_one_register "vpbroadcastw" "ymm0" "0x10101010101010101010101010101010, 0x0"
297
298 test_one_register "vpbroadcastb" "ymm15" "0x0, 0x0"
299 test_one_register "vpbroadcastb" "ymm0" "0x0, 0x0"
300
301 gdb_test "record stop" "Process record is stopped.*" \
302 "delete history for vpbroadcast_test"
303 } else {
304 untested "couldn't run vpbroadcast tests"
305 }
306
307 gdb_test "finish" "Run till exit from.*vpbroadcast_test.*" \
308 "leaving vpbroadcast"
309
310 # Preparation and testing of vzeroupper
311 gdb_test_no_output "set \$ymm0.v2_int128 = {0x0, 0x12345}" "set ymm0 for vzeroupper"
312 gdb_test_no_output "set \$ymm1.v2_int128 = {0x1f1e1d1c1b1a1918, 0x0}" \
313 "set ymm1 for vzeroupper"
314 gdb_test_no_output "set \$ymm2.v2_int128 = {0x0, 0xbeef}" "set ymm2 for vzeroupper"
315 gdb_test_no_output "set \$ymm15.v2_int128 = {0x0, 0xcafeface}" "set ymm15 for vpbroadcast"
316 if {[record_full_function "vzeroupper"] == true} {
317 # Since vzeroupper needs to save 8 or 16 registers, let's check what was
318 # actually recorded, instead of just undoing an instruction. Only
319 # really check the values of egisters 0, 1, 2 and 15 because those are
320 # the only ones we're setting.
321 gdb_test "maint print record-instruction" \
322 [multi_line "Register ymm0h changed: 74565" \
323 "Register ymm1h changed: 0" \
324 "Register ymm2h changed: 48879" \
325 "Register ymm3h changed: ${decimal}" \
326 "Register ymm4h changed: ${decimal}" \
327 "Register ymm5h changed: ${decimal}" \
328 "Register ymm6h changed: ${decimal}" \
329 "Register ymm7h changed: ${decimal}" \
330 "Register ymm8h changed: ${decimal}" \
331 "Register ymm9h changed: ${decimal}" \
332 "Register ymm10h changed: ${decimal}" \
333 "Register ymm11h changed: ${decimal}" \
334 "Register ymm12h changed: ${decimal}" \
335 "Register ymm13h changed: ${decimal}" \
336 "Register ymm14h changed: ${decimal}" \
337 "Register ymm15h changed: 3405707982" \
338 "Register rip changed: \[^\r\n\]+" ] \
339 "verify vzeroupper recording"
340
341 gdb_test "record stop" "Process record is stopped.*" \
342 "delete history for vzeroupper_test"
343 } else {
344 untested "couldn't run vzeroupper tests"
345 }
346
347 gdb_test "finish" "Run till exit from.*vzeroupper_test.*" \
348 "leaving vzeroupper"
349
350 # Preparation and testing vpxor instructions.
351 gdb_test_no_output "set \$ymm0.v2_int128 = {0x0, 0x12345}" "set ymm0 for vpor_xor"
352 gdb_test_no_output "set \$ymm1.v2_int128 = {0x1f1e1d1c1b1a1918, 0x0}" \
353 "set ymm1 for vpor_xor"
354 gdb_test_no_output "set \$ymm2.v2_int128 = {0x0, 0xbeef}" "set ymm2 for vpor_xor"
355 gdb_test_no_output "set \$ymm15.v2_int128 = {0x0, 0xcafeface}" "set ymm15 for vpor_xor"
356
357 if {[record_full_function "vpor_xor"] == true} {
358 test_one_register "vpor" "ymm15" "0x0, 0xcafe4421"
359 test_one_register "vpor" "ymm2" "0x0, 0x0"
360 test_one_register "vpor" "ymm1" "0x0, 0xcafe4421"
361 test_one_register "vpor" "ymm0" "0x1f1e1d1c1b1a1918, 0x0" "first: "
362 test_one_register "vpor" "ymm0" "0x1f1e1d1c1b1a1918, 0x0" "second: "
363
364 test_one_register "vpxor" "ymm15" "0x0, 0xcafeface"
365 test_one_register "vpxor" "ymm2" "0x0, 0xbeef"
366 test_one_register "vpxor" "ymm1" "0x1f1e1d1c1b1a1918, 0x0"
367 test_one_register "vpxor" "ymm0" "0x0, 0x0" "first: "
368 test_one_register "vpxor" "ymm0" "0x0, 0x12345" "second: "
369
370 gdb_test "record stop" "Process record is stopped.*" \
371 "delete history for vpor_xor_test"
372 } else {
373 untested "couldn't run vpor_xor tests"
374 }
375 gdb_test "finish" "Run till exit from.*vpor_xor_test.*" \
376 "leaving vpor_xor"
377
378 # Preparation and testing vpcmpeq instructions.
379 gdb_test_no_output "set \$ymm0.v2_int128 = {0x12345, 0x12345}" \
380 "set ymm0 for vpcmpeq"
381 gdb_test_no_output \
382 "set \$ymm1.v8_int32 = {0xcafe, 0xbeef, 0xff, 0x1234, 0x0, 0xff00, 0xff0000ff, 0xface0f0f}" \
383 "set ymm1 for vpcmpeq"
384 gdb_test_no_output \
385 "set \$ymm2.v8_int32 = {0xcafe0, 0xbeef, 0xff00, 0x12345678, 0x90abcdef, 0xffff00, 0xff, 0xf}" \
386 "set ymm2 for vpcmpeq"
387 gdb_test_no_output "set \$ymm15.v2_int128 = {0xcafeface, 0xcafeface}" \
388 "set ymm15 for vpcmpeq"
389
390 if {[record_full_function "vpcmpeq"] == true} {
391 test_one_register "vpcmpeqd" "ymm15" \
392 "0xffff0000ffffffff00000000, 0xffff0000ffff00000000" "ymm: "
393 test_one_register "vpcmpeqw" "ymm15" \
394 "0xffff0000ffffffffff000000, 0xff00ffffffff00ffff00000000" "ymm: "
395 test_one_register "vpcmpeqb" "ymm15" \
396 "0xffffffff00000000, 0x0" "ymm: "
397 test_one_register "vpcmpeqd" "ymm15" \
398 "0xffff0000ffffffff00000000, 0x0" "xmm: "
399 test_one_register "vpcmpeqw" "ymm15" \
400 "0xffff0000ffffffffff000000, 0x0" "xmm: "
401 test_one_register "vpcmpeqb" "ymm15" "0xcafeface, 0xcafeface" "xmm: "
402
403 test_one_register "vpcmpeqd" "ymm0" \
404 "0xffff0000ffffffff00000000, 0xffff0000ffff00000000" "ymm: "
405 test_one_register "vpcmpeqw" "ymm0" \
406 "0xffff0000ffffffffff000000, 0xff00ffffffff00ffff00000000" "ymm: "
407 test_one_register "vpcmpeqb" "ymm0" \
408 "0xffffffff00000000, 0x0" "ymm: "
409 test_one_register "vpcmpeqd" "ymm0" \
410 "0xffff0000ffffffff00000000, 0x0" "xmm: "
411 test_one_register "vpcmpeqw" "ymm0" \
412 "0xffff0000ffffffffff000000, 0x0" "xmm: "
413 test_one_register "vpcmpeqb" "ymm0" "0x12345, 0x12345" "xmm: "
414
415 gdb_test "record stop" "Process record is stopped.*" \
416 "delete history for vpcmpeq_test"
417 } else {
418 untested "couldn't run vpcmpeq tests"
419 }
420 gdb_test "finish" "Run till exit from.*vpcmpeq_test.*" \
421 "leaving vpcmpeq"
422
423 # Preparation and testing vpcmpeq instructions.
424 gdb_test_no_output "set \$rbx = 2" "set rbx for vpmovmskb"
425 gdb_test_no_output "set \$r8 = 3" "set r8 for vpmovmskb"
426 gdb_test_no_output "set \$r9 = 4" "set ymm15 for vpmovmskb"
427
428 if {[record_full_function "vpmovmskb"] == true} {
429 test_one_general_register "vpmovmskb" "r9" "0x4"
430 test_one_general_register "vpmovmskb" "r8" "0x3"
431 test_one_general_register "vpmovmskb" "rbx" "0x2"
432 # Because of the infrastructure of the test, we can't set rax.
433 # However, it seems to always be set to 0, so this should be fine.
434 test_one_general_register "vpmovmskb" "rax" "0x0"
435
436 gdb_test "record stop" "Process record is stopped.*" \
437 "delete history for vpmovmskb_test"
438 } else {
439 untested "couldn't run vpmovmskb tests"
440 }
441 gdb_test "finish" "Run till exit from.*vpmovmskb_test.*" \
442 "leaving vpmovmskb"
443