Skip to content

Commit ebd06ca

Browse files
committed
gdb/aarch64: Add record support for MOPS instructions.
There are two kinds of MOPS instructions: set instructions and copy instructions. Within each group there are variants with minor differences in how they read or write to memory — e.g., non-temporal read and/or write, unprivileged read and/or write and permutations of those — but they work in the same way in terms of the registers and regions of memory that they modify. The new gdb.reverse/aarch64-mops.exp testcase verifies that MOPS instructions are recorded and correctly reversed. Not all variants of the copy and set instructions are tested, since there are many and the record and replay target processes them in the same way. PR tdep/31666 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31666 Approved-By: Luis Machado <luis.machado@arm.com> Tested-By: Luis Machado <luis.machado@arm.com>
1 parent b995344 commit ebd06ca

File tree

3 files changed

+333
-0
lines changed

3 files changed

+333
-0
lines changed

gdb/aarch64-tdep.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5188,6 +5188,71 @@ aarch64_record_asimd_load_store (aarch64_insn_decode_record *aarch64_insn_r)
51885188
return AARCH64_RECORD_SUCCESS;
51895189
}
51905190

5191+
/* Record handler for Memory Copy and Memory Set instructions. */
5192+
5193+
static unsigned int
5194+
aarch64_record_memcopy_memset (aarch64_insn_decode_record *aarch64_insn_r)
5195+
{
5196+
if (record_debug)
5197+
debug_printf ("Process record: memory copy and memory set\n");
5198+
5199+
uint8_t op1 = bits (aarch64_insn_r->aarch64_insn, 22, 23);
5200+
uint8_t op2 = bits (aarch64_insn_r->aarch64_insn, 12, 15);
5201+
uint32_t reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
5202+
uint32_t reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
5203+
uint32_t record_buf[3];
5204+
uint64_t record_buf_mem[4];
5205+
5206+
if (op1 == 3 && op2 > 11)
5207+
/* Unallocated instructions. */
5208+
return AARCH64_RECORD_UNKNOWN;
5209+
5210+
/* Set instructions have two registers and one memory region to be
5211+
recorded. */
5212+
record_buf[0] = reg_rd;
5213+
record_buf[1] = reg_rn;
5214+
aarch64_insn_r->reg_rec_count = 2;
5215+
5216+
ULONGEST dest_addr;
5217+
regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rd, &dest_addr);
5218+
5219+
LONGEST length;
5220+
regcache_raw_read_signed (aarch64_insn_r->regcache, reg_rn, &length);
5221+
5222+
/* In one of the algorithm options a processor can implement, the length
5223+
in Rn has an inverted sign. */
5224+
if (length < 0)
5225+
length *= -1;
5226+
5227+
record_buf_mem[0] = length;
5228+
record_buf_mem[1] = dest_addr;
5229+
aarch64_insn_r->mem_rec_count = 1;
5230+
5231+
if (op1 != 3)
5232+
{
5233+
/* Copy instructions have an additional register and an additional
5234+
memory region to be recorded. */
5235+
uint32_t reg_rs = bits (aarch64_insn_r->aarch64_insn, 16, 20);
5236+
5237+
record_buf[2] = reg_rs;
5238+
aarch64_insn_r->reg_rec_count++;
5239+
5240+
ULONGEST source_addr;
5241+
regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rs,
5242+
&source_addr);
5243+
5244+
record_buf_mem[2] = length;
5245+
record_buf_mem[3] = source_addr;
5246+
aarch64_insn_r->mem_rec_count++;
5247+
}
5248+
5249+
MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
5250+
record_buf_mem);
5251+
REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
5252+
record_buf);
5253+
return AARCH64_RECORD_SUCCESS;
5254+
}
5255+
51915256
/* Record handler for load and store instructions. */
51925257

51935258
static unsigned int
@@ -5465,6 +5530,10 @@ aarch64_record_load_store (aarch64_insn_decode_record *aarch64_insn_r)
54655530
if (insn_bits10_11 == 0x01 || insn_bits10_11 == 0x03)
54665531
record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
54675532
}
5533+
/* Memory Copy and Memory Set instructions. */
5534+
else if ((insn_bits24_27 & 1) == 1 && insn_bits28_29 == 1
5535+
&& insn_bits10_11 == 1 && !insn_bit21)
5536+
return aarch64_record_memcopy_memset (aarch64_insn_r);
54685537
/* Advanced SIMD load/store instructions. */
54695538
else
54705539
return aarch64_record_asimd_load_store (aarch64_insn_r);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* This test program is part of GDB, the GNU debugger.
2+
3+
Copyright 2024 Free Software Foundation, Inc.
4+
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.
9+
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.
14+
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/>. */
17+
18+
#include <string.h>
19+
20+
#define INITIAL_STRING "Initial fill value."
21+
#define NEW_STRING "Just a test string."
22+
#define BUF_SIZE sizeof(NEW_STRING)
23+
24+
int
25+
main (void)
26+
{
27+
char dest[BUF_SIZE] = INITIAL_STRING;
28+
char source[BUF_SIZE] = NEW_STRING;
29+
register char *p asm ("x19");
30+
register char *q asm ("x20");
31+
register long size asm ("x21");
32+
register long zero asm ("x22");
33+
34+
p = dest;
35+
size = BUF_SIZE;
36+
zero = 0;
37+
/* Before setp. */
38+
/* memset implemented in MOPS instructions. */
39+
__asm__ volatile ("setp [%0]!, %1!, %2\n\t"
40+
"setm [%0]!, %1!, %2\n\t"
41+
"sete [%0]!, %1!, %2\n\t"
42+
: "+&r"(p), "+&r"(size)
43+
: "r"(zero)
44+
: "memory");
45+
46+
/* After sete. */
47+
p = dest;
48+
q = source;
49+
size = BUF_SIZE;
50+
memcpy (dest, INITIAL_STRING, sizeof (dest));
51+
/* Before cpyp. */
52+
/* memmove implemented in MOPS instructions. */
53+
__asm__ volatile ("cpyp [%0]!, [%1]!, %2!\n\t"
54+
"cpym [%0]!, [%1]!, %2!\n\t"
55+
"cpye [%0]!, [%1]!, %2!\n\t"
56+
: "+&r" (p), "+&r" (q), "+&r" (size)
57+
:
58+
: "memory");
59+
60+
/* After cpye. */
61+
p = dest;
62+
q = source;
63+
size = BUF_SIZE;
64+
memcpy (dest, INITIAL_STRING, sizeof (dest));
65+
/* Before cpyfp. */
66+
/* memcpy implemented in MOPS instructions. */
67+
__asm__ volatile ("cpyfp [%0]!, [%1]!, %2!\n\t"
68+
"cpyfm [%0]!, [%1]!, %2!\n\t"
69+
"cpyfe [%0]!, [%1]!, %2!\n\t"
70+
: "+&r" (p), "+&r" (q), "+&r" (size)
71+
:
72+
: "memory");
73+
74+
/* After cpyfe. */
75+
p = dest;
76+
77+
return 0;
78+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# Copyright 2024 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+
# Test instruction record for AArch64 FEAT_MOPS instructions.
17+
# Based on gdb.reverse/ppc_record_test_isa_3_1.exp
18+
#
19+
# The basic flow of the record tests are:
20+
# 1) Stop before executing the instructions of interest. Record
21+
# the initial value of the registers that the instruction will
22+
# change, i.e. the destination register.
23+
# 2) Execute the instructions. Record the new value of the
24+
# registers that changed.
25+
# 3) Reverse the direction of the execution and execute back to
26+
# just before the instructions of interest. Record the final
27+
# value of the registers of interest.
28+
# 4) Check that the initial and new values of the registers are
29+
# different, i.e. the instruction changed the registers as expected.
30+
# 5) Check that the initial and final values of the registers are
31+
# the same, i.e. GDB record restored the registers to their
32+
# original values.
33+
34+
require allow_aarch64_mops_tests
35+
36+
standard_testfile
37+
38+
if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
39+
[list debug additional_flags=-march=armv9.3-a]] } {
40+
return -1
41+
}
42+
43+
if ![runto_main] {
44+
return -1
45+
}
46+
47+
gdb_test_no_output "record full"
48+
49+
foreach_with_prefix insn_prefix {"set" "cpy" "cpyf"} {
50+
global decimal hex
51+
52+
set before_seq [gdb_get_line_number "Before ${insn_prefix}p"]
53+
set after_seq [gdb_get_line_number "After ${insn_prefix}e"]
54+
55+
gdb_test "break $before_seq" \
56+
"Breakpoint $decimal at $hex: file .*/aarch64-mops.c, line $decimal\\." \
57+
"break before instruction sequence"
58+
gdb_continue_to_breakpoint "about to execute instruction sequence" \
59+
[multi_line ".*/aarch64-mops.c:$decimal" \
60+
"$decimal\[ \t\]+__asm__ volatile \\(\"${insn_prefix}p \[^\r\n\]+\""]
61+
62+
# Depending on the compiler, the line number information may put GDB a few
63+
# instructions before the beginning of the asm statement.
64+
arrive_at_instruction "${insn_prefix}p"
65+
# Add a breakpoint that we're sure is at the prologue instruction.
66+
gdb_test "break *\$pc" \
67+
"Breakpoint $decimal at $hex: file .*/aarch64-mops.c, line $decimal\\." \
68+
"break at prologue instruction"
69+
70+
# Record the initial memory and register values.
71+
set dest_initial [get_valueof "/x" "dest" "unable to read initial" \
72+
"get dest initial value"]
73+
set x19_initial [capture_command_output "info register x19" ""]
74+
set x21_initial [capture_command_output "info register x21" ""]
75+
76+
# The set instructions use the ZERO variable, but not Q nor SOURCE,
77+
# and the other instructions are the opposite.
78+
if {[string compare $insn_prefix "set"] == 0} {
79+
set x22_initial [capture_command_output "info register x22" ""]
80+
} else {
81+
set x20_initial [capture_command_output "info register x20" ""]
82+
set source_initial [get_valueof "/x" "source" "unable to read initial" \
83+
"get source initial value"]
84+
}
85+
86+
gdb_test "break $after_seq" \
87+
"Breakpoint $decimal at $hex: file .*/aarch64-mops.c, line $decimal\\." \
88+
"break after instruction sequence"
89+
gdb_continue_to_breakpoint "executed instruction sequence" \
90+
[multi_line ".*/aarch64-mops.c:$decimal" "$decimal\[ \t\]+p = dest;"]
91+
92+
# Record the new memory and register values.
93+
set dest_new [get_valueof "/x" "dest" "unable to read new" \
94+
"get dest new value"]
95+
set x19_new [capture_command_output "info register x19" ""]
96+
set x21_new [capture_command_output "info register x21" ""]
97+
98+
if {[string compare $insn_prefix "set"] == 0} {
99+
set x22_new [capture_command_output "info register x22" ""]
100+
} else {
101+
set x20_new [capture_command_output "info register x20" ""]
102+
set source_new [get_valueof "/x" "source" "unable to read new" \
103+
"get source new value"]
104+
}
105+
106+
# Execute in reverse to before the instruction sequence.
107+
gdb_test_no_output "set exec-direction reverse"
108+
109+
gdb_continue_to_breakpoint "reversed execution of instruction sequence" \
110+
[multi_line ".*/aarch64-mops.c:$decimal" \
111+
"$decimal\[ \t\]+__asm__ volatile \\(\"${insn_prefix}p \[^\r\n\]+\""]
112+
113+
# Record the final memory and register values.
114+
set dest_final [get_valueof "/x" "dest" "unable to read final" \
115+
"get dest final value"]
116+
set x19_final [capture_command_output "info register x19" ""]
117+
set x21_final [capture_command_output "info register x21" ""]
118+
119+
if {[string compare $insn_prefix "set"] == 0} {
120+
set x22_final [capture_command_output "info register x22" ""]
121+
} else {
122+
set x20_final [capture_command_output "info register x20" ""]
123+
set source_final [get_valueof "/x" "source" "unable to read final" \
124+
"get source final value"]
125+
}
126+
127+
# Check initial and new values of dest are different.
128+
gdb_assert [string compare $dest_initial $dest_new] \
129+
"check dest initial value versus dest new value"
130+
131+
# Check initial and new values of x19 are different.
132+
gdb_assert [string compare $x19_initial $x19_new] \
133+
"check x19 initial value versus x19 new value"
134+
135+
# Check initial and new values of x21 are different.
136+
gdb_assert [string compare $x21_initial $x21_new] \
137+
"check x21 initial value versus x21 new value"
138+
139+
if {[string compare $insn_prefix "set"] == 0} {
140+
# Check initial and new values of x22 are the same.
141+
# The register with the value to set shouldn't change.
142+
gdb_assert ![string compare $x22_initial $x22_new] \
143+
"check x22 initial value versus x22 new value"
144+
} else {
145+
# Check initial and new values of x20 are different.
146+
gdb_assert [string compare $x20_initial $x20_new] \
147+
"check x20 initial value versus x20 new value"
148+
# Check initial and new values of source are the same.
149+
gdb_assert ![string compare $source_initial $source_new] \
150+
"check source initial value versus source new value"
151+
}
152+
153+
# Check initial and final values of dest are the same.
154+
gdb_assert ![string compare $dest_initial $dest_final] \
155+
"check dest initial value versus dest final value"
156+
157+
# Check initial and final values of x19 are the same.
158+
gdb_assert ![string compare $x19_initial $x19_final] \
159+
"check x19 initial value versus x19 final value"
160+
161+
# Check initial and final values of x21 are the same.
162+
gdb_assert ![string compare $x21_initial $x21_final] \
163+
"check x21 initial value versus x21 final value"
164+
165+
if {[string compare $insn_prefix "set"] == 0} {
166+
# Check initial and final values of x22 are the same.
167+
gdb_assert ![string compare $x22_initial $x22_final] \
168+
"check x22 initial value versus x22 final value"
169+
} else {
170+
# Check initial and final values of x20 are the same.
171+
gdb_assert ![string compare $x20_initial $x20_final] \
172+
"check x20 initial value versus x20 final value"
173+
174+
# Check initial and final values of source are the same.
175+
gdb_assert ![string compare $source_initial $source_final] \
176+
"check source initial value versus source final value"
177+
}
178+
179+
# Restore forward execution and go to end of recording.
180+
gdb_test_no_output "set exec-direction forward"
181+
gdb_test "record goto end" \
182+
[multi_line \
183+
"Go forward to insn number $decimal" \
184+
"#0 main \\(\\) at .*/aarch64-mops.c:$decimal" \
185+
"$decimal\[ \t\]+p = dest;"]
186+
}

0 commit comments

Comments
 (0)