1 /* Common target dependent code for GDB on AArch64 systems.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by ARM Ltd.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "reggroups.h"
30 #include "arch-utils.h"
32 #include "frame-unwind.h"
33 #include "frame-base.h"
34 #include "trad-frame.h"
37 #include "dwarf2/frame.h"
39 #include "prologue-value.h"
40 #include "target-descriptions.h"
41 #include "user-regs.h"
43 #include "gdbsupport/selftest.h"
45 #include "aarch64-tdep.h"
46 #include "aarch64-ravenscar-thread.h"
49 #include "record-full.h"
50 #include "arch/aarch64-insn.h"
53 #include "opcode/aarch64.h"
56 /* A Homogeneous Floating-Point or Short-Vector Aggregate may have at most
58 #define HA_MAX_NUM_FLDS 4
60 /* All possible aarch64 target descriptors. */
61 static target_desc
*tdesc_aarch64_list
[AARCH64_MAX_SVE_VQ
+ 1][2/*pauth*/][2 /* mte */];
63 /* The standard register names, and all the valid aliases for them. */
66 const char *const name
;
68 } aarch64_register_aliases
[] =
70 /* 64-bit register names. */
71 {"fp", AARCH64_FP_REGNUM
},
72 {"lr", AARCH64_LR_REGNUM
},
73 {"sp", AARCH64_SP_REGNUM
},
75 /* 32-bit register names. */
76 {"w0", AARCH64_X0_REGNUM
+ 0},
77 {"w1", AARCH64_X0_REGNUM
+ 1},
78 {"w2", AARCH64_X0_REGNUM
+ 2},
79 {"w3", AARCH64_X0_REGNUM
+ 3},
80 {"w4", AARCH64_X0_REGNUM
+ 4},
81 {"w5", AARCH64_X0_REGNUM
+ 5},
82 {"w6", AARCH64_X0_REGNUM
+ 6},
83 {"w7", AARCH64_X0_REGNUM
+ 7},
84 {"w8", AARCH64_X0_REGNUM
+ 8},
85 {"w9", AARCH64_X0_REGNUM
+ 9},
86 {"w10", AARCH64_X0_REGNUM
+ 10},
87 {"w11", AARCH64_X0_REGNUM
+ 11},
88 {"w12", AARCH64_X0_REGNUM
+ 12},
89 {"w13", AARCH64_X0_REGNUM
+ 13},
90 {"w14", AARCH64_X0_REGNUM
+ 14},
91 {"w15", AARCH64_X0_REGNUM
+ 15},
92 {"w16", AARCH64_X0_REGNUM
+ 16},
93 {"w17", AARCH64_X0_REGNUM
+ 17},
94 {"w18", AARCH64_X0_REGNUM
+ 18},
95 {"w19", AARCH64_X0_REGNUM
+ 19},
96 {"w20", AARCH64_X0_REGNUM
+ 20},
97 {"w21", AARCH64_X0_REGNUM
+ 21},
98 {"w22", AARCH64_X0_REGNUM
+ 22},
99 {"w23", AARCH64_X0_REGNUM
+ 23},
100 {"w24", AARCH64_X0_REGNUM
+ 24},
101 {"w25", AARCH64_X0_REGNUM
+ 25},
102 {"w26", AARCH64_X0_REGNUM
+ 26},
103 {"w27", AARCH64_X0_REGNUM
+ 27},
104 {"w28", AARCH64_X0_REGNUM
+ 28},
105 {"w29", AARCH64_X0_REGNUM
+ 29},
106 {"w30", AARCH64_X0_REGNUM
+ 30},
109 {"ip0", AARCH64_X0_REGNUM
+ 16},
110 {"ip1", AARCH64_X0_REGNUM
+ 17}
113 /* The required core 'R' registers. */
114 static const char *const aarch64_r_register_names
[] =
116 /* These registers must appear in consecutive RAW register number
117 order and they must begin with AARCH64_X0_REGNUM! */
118 "x0", "x1", "x2", "x3",
119 "x4", "x5", "x6", "x7",
120 "x8", "x9", "x10", "x11",
121 "x12", "x13", "x14", "x15",
122 "x16", "x17", "x18", "x19",
123 "x20", "x21", "x22", "x23",
124 "x24", "x25", "x26", "x27",
125 "x28", "x29", "x30", "sp",
129 /* The FP/SIMD 'V' registers. */
130 static const char *const aarch64_v_register_names
[] =
132 /* These registers must appear in consecutive RAW register number
133 order and they must begin with AARCH64_V0_REGNUM! */
134 "v0", "v1", "v2", "v3",
135 "v4", "v5", "v6", "v7",
136 "v8", "v9", "v10", "v11",
137 "v12", "v13", "v14", "v15",
138 "v16", "v17", "v18", "v19",
139 "v20", "v21", "v22", "v23",
140 "v24", "v25", "v26", "v27",
141 "v28", "v29", "v30", "v31",
146 /* The SVE 'Z' and 'P' registers. */
147 static const char *const aarch64_sve_register_names
[] =
149 /* These registers must appear in consecutive RAW register number
150 order and they must begin with AARCH64_SVE_Z0_REGNUM! */
151 "z0", "z1", "z2", "z3",
152 "z4", "z5", "z6", "z7",
153 "z8", "z9", "z10", "z11",
154 "z12", "z13", "z14", "z15",
155 "z16", "z17", "z18", "z19",
156 "z20", "z21", "z22", "z23",
157 "z24", "z25", "z26", "z27",
158 "z28", "z29", "z30", "z31",
160 "p0", "p1", "p2", "p3",
161 "p4", "p5", "p6", "p7",
162 "p8", "p9", "p10", "p11",
163 "p12", "p13", "p14", "p15",
167 static const char *const aarch64_pauth_register_names
[] =
169 /* Authentication mask for data pointer. */
171 /* Authentication mask for code pointer. */
175 static const char *const aarch64_mte_register_names
[] =
177 /* Tag Control Register. */
181 /* AArch64 prologue cache structure. */
182 struct aarch64_prologue_cache
184 /* The program counter at the start of the function. It is used to
185 identify this frame as a prologue frame. */
188 /* The program counter at the time this frame was created; i.e. where
189 this function was called from. It is used to identify this frame as a
193 /* The stack pointer at the time this frame was created; i.e. the
194 caller's stack pointer when this function was called. It is used
195 to identify this frame. */
198 /* Is the target available to read from? */
201 /* The frame base for this frame is just prev_sp - frame size.
202 FRAMESIZE is the distance from the frame pointer to the
203 initial stack pointer. */
206 /* The register used to hold the frame pointer for this frame. */
209 /* Saved register offsets. */
210 trad_frame_saved_reg
*saved_regs
;
214 show_aarch64_debug (struct ui_file
*file
, int from_tty
,
215 struct cmd_list_element
*c
, const char *value
)
217 gdb_printf (file
, _("AArch64 debugging is %s.\n"), value
);
222 /* Abstract instruction reader. */
224 class abstract_instruction_reader
227 /* Read in one instruction. */
228 virtual ULONGEST
read (CORE_ADDR memaddr
, int len
,
229 enum bfd_endian byte_order
) = 0;
232 /* Instruction reader from real target. */
234 class instruction_reader
: public abstract_instruction_reader
237 ULONGEST
read (CORE_ADDR memaddr
, int len
, enum bfd_endian byte_order
)
240 return read_code_unsigned_integer (memaddr
, len
, byte_order
);
246 /* If address signing is enabled, mask off the signature bits from the link
247 register, which is passed by value in ADDR, using the register values in
251 aarch64_frame_unmask_lr (aarch64_gdbarch_tdep
*tdep
,
252 struct frame_info
*this_frame
, CORE_ADDR addr
)
254 if (tdep
->has_pauth ()
255 && frame_unwind_register_unsigned (this_frame
,
256 tdep
->pauth_ra_state_regnum
))
258 int cmask_num
= AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
);
259 CORE_ADDR cmask
= frame_unwind_register_unsigned (this_frame
, cmask_num
);
260 addr
= addr
& ~cmask
;
262 /* Record in the frame that the link register required unmasking. */
263 set_frame_previous_pc_masked (this_frame
);
269 /* Implement the "get_pc_address_flags" gdbarch method. */
272 aarch64_get_pc_address_flags (frame_info
*frame
, CORE_ADDR pc
)
274 if (pc
!= 0 && get_frame_pc_masked (frame
))
280 /* Analyze a prologue, looking for a recognizable stack frame
281 and frame pointer. Scan until we encounter a store that could
282 clobber the stack frame unexpectedly, or an unknown instruction. */
285 aarch64_analyze_prologue (struct gdbarch
*gdbarch
,
286 CORE_ADDR start
, CORE_ADDR limit
,
287 struct aarch64_prologue_cache
*cache
,
288 abstract_instruction_reader
& reader
)
290 enum bfd_endian byte_order_for_code
= gdbarch_byte_order_for_code (gdbarch
);
293 /* Whether the stack has been set. This should be true when we notice a SP
294 to FP move or if we are using the SP as the base register for storing
295 data, in case the FP is ommitted. */
296 bool seen_stack_set
= false;
298 /* Track X registers and D registers in prologue. */
299 pv_t regs
[AARCH64_X_REGISTER_COUNT
+ AARCH64_D_REGISTER_COUNT
];
301 for (i
= 0; i
< AARCH64_X_REGISTER_COUNT
+ AARCH64_D_REGISTER_COUNT
; i
++)
302 regs
[i
] = pv_register (i
, 0);
303 pv_area
stack (AARCH64_SP_REGNUM
, gdbarch_addr_bit (gdbarch
));
305 for (; start
< limit
; start
+= 4)
310 insn
= reader
.read (start
, 4, byte_order_for_code
);
312 if (aarch64_decode_insn (insn
, &inst
, 1, NULL
) != 0)
315 if (inst
.opcode
->iclass
== addsub_imm
316 && (inst
.opcode
->op
== OP_ADD
317 || strcmp ("sub", inst
.opcode
->name
) == 0))
319 unsigned rd
= inst
.operands
[0].reg
.regno
;
320 unsigned rn
= inst
.operands
[1].reg
.regno
;
322 gdb_assert (aarch64_num_of_operands (inst
.opcode
) == 3);
323 gdb_assert (inst
.operands
[0].type
== AARCH64_OPND_Rd_SP
);
324 gdb_assert (inst
.operands
[1].type
== AARCH64_OPND_Rn_SP
);
325 gdb_assert (inst
.operands
[2].type
== AARCH64_OPND_AIMM
);
327 if (inst
.opcode
->op
== OP_ADD
)
329 regs
[rd
] = pv_add_constant (regs
[rn
],
330 inst
.operands
[2].imm
.value
);
334 regs
[rd
] = pv_add_constant (regs
[rn
],
335 -inst
.operands
[2].imm
.value
);
338 /* Did we move SP to FP? */
339 if (rn
== AARCH64_SP_REGNUM
&& rd
== AARCH64_FP_REGNUM
)
340 seen_stack_set
= true;
342 else if (inst
.opcode
->iclass
== pcreladdr
343 && inst
.operands
[1].type
== AARCH64_OPND_ADDR_ADRP
)
345 gdb_assert (aarch64_num_of_operands (inst
.opcode
) == 2);
346 gdb_assert (inst
.operands
[0].type
== AARCH64_OPND_Rd
);
348 regs
[inst
.operands
[0].reg
.regno
] = pv_unknown ();
350 else if (inst
.opcode
->iclass
== branch_imm
)
352 /* Stop analysis on branch. */
355 else if (inst
.opcode
->iclass
== condbranch
)
357 /* Stop analysis on branch. */
360 else if (inst
.opcode
->iclass
== branch_reg
)
362 /* Stop analysis on branch. */
365 else if (inst
.opcode
->iclass
== compbranch
)
367 /* Stop analysis on branch. */
370 else if (inst
.opcode
->op
== OP_MOVZ
)
372 gdb_assert (inst
.operands
[0].type
== AARCH64_OPND_Rd
);
374 /* If this shows up before we set the stack, keep going. Otherwise
375 stop the analysis. */
379 regs
[inst
.operands
[0].reg
.regno
] = pv_unknown ();
381 else if (inst
.opcode
->iclass
== log_shift
382 && strcmp (inst
.opcode
->name
, "orr") == 0)
384 unsigned rd
= inst
.operands
[0].reg
.regno
;
385 unsigned rn
= inst
.operands
[1].reg
.regno
;
386 unsigned rm
= inst
.operands
[2].reg
.regno
;
388 gdb_assert (inst
.operands
[0].type
== AARCH64_OPND_Rd
);
389 gdb_assert (inst
.operands
[1].type
== AARCH64_OPND_Rn
);
390 gdb_assert (inst
.operands
[2].type
== AARCH64_OPND_Rm_SFT
);
392 if (inst
.operands
[2].shifter
.amount
== 0
393 && rn
== AARCH64_SP_REGNUM
)
397 aarch64_debug_printf ("prologue analysis gave up "
398 "addr=%s opcode=0x%x (orr x register)",
399 core_addr_to_string_nz (start
), insn
);
404 else if (inst
.opcode
->op
== OP_STUR
)
406 unsigned rt
= inst
.operands
[0].reg
.regno
;
407 unsigned rn
= inst
.operands
[1].addr
.base_regno
;
408 int size
= aarch64_get_qualifier_esize (inst
.operands
[0].qualifier
);
410 gdb_assert (aarch64_num_of_operands (inst
.opcode
) == 2);
411 gdb_assert (inst
.operands
[0].type
== AARCH64_OPND_Rt
);
412 gdb_assert (inst
.operands
[1].type
== AARCH64_OPND_ADDR_SIMM9
);
413 gdb_assert (!inst
.operands
[1].addr
.offset
.is_reg
);
416 (pv_add_constant (regs
[rn
], inst
.operands
[1].addr
.offset
.imm
),
419 /* Are we storing with SP as a base? */
420 if (rn
== AARCH64_SP_REGNUM
)
421 seen_stack_set
= true;
423 else if ((inst
.opcode
->iclass
== ldstpair_off
424 || (inst
.opcode
->iclass
== ldstpair_indexed
425 && inst
.operands
[2].addr
.preind
))
426 && strcmp ("stp", inst
.opcode
->name
) == 0)
428 /* STP with addressing mode Pre-indexed and Base register. */
431 unsigned rn
= inst
.operands
[2].addr
.base_regno
;
432 int32_t imm
= inst
.operands
[2].addr
.offset
.imm
;
433 int size
= aarch64_get_qualifier_esize (inst
.operands
[0].qualifier
);
435 gdb_assert (inst
.operands
[0].type
== AARCH64_OPND_Rt
436 || inst
.operands
[0].type
== AARCH64_OPND_Ft
);
437 gdb_assert (inst
.operands
[1].type
== AARCH64_OPND_Rt2
438 || inst
.operands
[1].type
== AARCH64_OPND_Ft2
);
439 gdb_assert (inst
.operands
[2].type
== AARCH64_OPND_ADDR_SIMM7
);
440 gdb_assert (!inst
.operands
[2].addr
.offset
.is_reg
);
442 /* If recording this store would invalidate the store area
443 (perhaps because rn is not known) then we should abandon
444 further prologue analysis. */
445 if (stack
.store_would_trash (pv_add_constant (regs
[rn
], imm
)))
448 if (stack
.store_would_trash (pv_add_constant (regs
[rn
], imm
+ 8)))
451 rt1
= inst
.operands
[0].reg
.regno
;
452 rt2
= inst
.operands
[1].reg
.regno
;
453 if (inst
.operands
[0].type
== AARCH64_OPND_Ft
)
455 rt1
+= AARCH64_X_REGISTER_COUNT
;
456 rt2
+= AARCH64_X_REGISTER_COUNT
;
459 stack
.store (pv_add_constant (regs
[rn
], imm
), size
, regs
[rt1
]);
460 stack
.store (pv_add_constant (regs
[rn
], imm
+ size
), size
, regs
[rt2
]);
462 if (inst
.operands
[2].addr
.writeback
)
463 regs
[rn
] = pv_add_constant (regs
[rn
], imm
);
465 /* Ignore the instruction that allocates stack space and sets
467 if (rn
== AARCH64_SP_REGNUM
&& !inst
.operands
[2].addr
.writeback
)
468 seen_stack_set
= true;
470 else if ((inst
.opcode
->iclass
== ldst_imm9
/* Signed immediate. */
471 || (inst
.opcode
->iclass
== ldst_pos
/* Unsigned immediate. */
472 && (inst
.opcode
->op
== OP_STR_POS
473 || inst
.opcode
->op
== OP_STRF_POS
)))
474 && inst
.operands
[1].addr
.base_regno
== AARCH64_SP_REGNUM
475 && strcmp ("str", inst
.opcode
->name
) == 0)
477 /* STR (immediate) */
478 unsigned int rt
= inst
.operands
[0].reg
.regno
;
479 int32_t imm
= inst
.operands
[1].addr
.offset
.imm
;
480 unsigned int rn
= inst
.operands
[1].addr
.base_regno
;
481 int size
= aarch64_get_qualifier_esize (inst
.operands
[0].qualifier
);
482 gdb_assert (inst
.operands
[0].type
== AARCH64_OPND_Rt
483 || inst
.operands
[0].type
== AARCH64_OPND_Ft
);
485 if (inst
.operands
[0].type
== AARCH64_OPND_Ft
)
486 rt
+= AARCH64_X_REGISTER_COUNT
;
488 stack
.store (pv_add_constant (regs
[rn
], imm
), size
, regs
[rt
]);
489 if (inst
.operands
[1].addr
.writeback
)
490 regs
[rn
] = pv_add_constant (regs
[rn
], imm
);
492 /* Are we storing with SP as a base? */
493 if (rn
== AARCH64_SP_REGNUM
)
494 seen_stack_set
= true;
496 else if (inst
.opcode
->iclass
== testbranch
)
498 /* Stop analysis on branch. */
501 else if (inst
.opcode
->iclass
== ic_system
)
503 aarch64_gdbarch_tdep
*tdep
504 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
505 int ra_state_val
= 0;
507 if (insn
== 0xd503233f /* paciasp. */
508 || insn
== 0xd503237f /* pacibsp. */)
510 /* Return addresses are mangled. */
513 else if (insn
== 0xd50323bf /* autiasp. */
514 || insn
== 0xd50323ff /* autibsp. */)
516 /* Return addresses are not mangled. */
519 else if (IS_BTI (insn
))
520 /* We don't need to do anything special for a BTI instruction. */
524 aarch64_debug_printf ("prologue analysis gave up addr=%s"
525 " opcode=0x%x (iclass)",
526 core_addr_to_string_nz (start
), insn
);
530 if (tdep
->has_pauth () && cache
!= nullptr)
532 int regnum
= tdep
->pauth_ra_state_regnum
;
533 cache
->saved_regs
[regnum
].set_value (ra_state_val
);
538 aarch64_debug_printf ("prologue analysis gave up addr=%s"
540 core_addr_to_string_nz (start
), insn
);
549 if (pv_is_register (regs
[AARCH64_FP_REGNUM
], AARCH64_SP_REGNUM
))
551 /* Frame pointer is fp. Frame size is constant. */
552 cache
->framereg
= AARCH64_FP_REGNUM
;
553 cache
->framesize
= -regs
[AARCH64_FP_REGNUM
].k
;
555 else if (pv_is_register (regs
[AARCH64_SP_REGNUM
], AARCH64_SP_REGNUM
))
557 /* Try the stack pointer. */
558 cache
->framesize
= -regs
[AARCH64_SP_REGNUM
].k
;
559 cache
->framereg
= AARCH64_SP_REGNUM
;
563 /* We're just out of luck. We don't know where the frame is. */
564 cache
->framereg
= -1;
565 cache
->framesize
= 0;
568 for (i
= 0; i
< AARCH64_X_REGISTER_COUNT
; i
++)
572 if (stack
.find_reg (gdbarch
, i
, &offset
))
573 cache
->saved_regs
[i
].set_addr (offset
);
576 for (i
= 0; i
< AARCH64_D_REGISTER_COUNT
; i
++)
578 int regnum
= gdbarch_num_regs (gdbarch
);
581 if (stack
.find_reg (gdbarch
, i
+ AARCH64_X_REGISTER_COUNT
,
583 cache
->saved_regs
[i
+ regnum
+ AARCH64_D0_REGNUM
].set_addr (offset
);
590 aarch64_analyze_prologue (struct gdbarch
*gdbarch
,
591 CORE_ADDR start
, CORE_ADDR limit
,
592 struct aarch64_prologue_cache
*cache
)
594 instruction_reader reader
;
596 return aarch64_analyze_prologue (gdbarch
, start
, limit
, cache
,
602 namespace selftests
{
604 /* Instruction reader from manually cooked instruction sequences. */
606 class instruction_reader_test
: public abstract_instruction_reader
609 template<size_t SIZE
>
610 explicit instruction_reader_test (const uint32_t (&insns
)[SIZE
])
611 : m_insns (insns
), m_insns_size (SIZE
)
614 ULONGEST
read (CORE_ADDR memaddr
, int len
, enum bfd_endian byte_order
)
617 SELF_CHECK (len
== 4);
618 SELF_CHECK (memaddr
% 4 == 0);
619 SELF_CHECK (memaddr
/ 4 < m_insns_size
);
621 return m_insns
[memaddr
/ 4];
625 const uint32_t *m_insns
;
630 aarch64_analyze_prologue_test (void)
632 struct gdbarch_info info
;
634 info
.bfd_arch_info
= bfd_scan_arch ("aarch64");
636 struct gdbarch
*gdbarch
= gdbarch_find_by_info (info
);
637 SELF_CHECK (gdbarch
!= NULL
);
639 struct aarch64_prologue_cache cache
;
640 cache
.saved_regs
= trad_frame_alloc_saved_regs (gdbarch
);
642 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
644 /* Test the simple prologue in which frame pointer is used. */
646 static const uint32_t insns
[] = {
647 0xa9af7bfd, /* stp x29, x30, [sp,#-272]! */
648 0x910003fd, /* mov x29, sp */
649 0x97ffffe6, /* bl 0x400580 */
651 instruction_reader_test
reader (insns
);
653 CORE_ADDR end
= aarch64_analyze_prologue (gdbarch
, 0, 128, &cache
, reader
);
654 SELF_CHECK (end
== 4 * 2);
656 SELF_CHECK (cache
.framereg
== AARCH64_FP_REGNUM
);
657 SELF_CHECK (cache
.framesize
== 272);
659 for (int i
= 0; i
< AARCH64_X_REGISTER_COUNT
; i
++)
661 if (i
== AARCH64_FP_REGNUM
)
662 SELF_CHECK (cache
.saved_regs
[i
].addr () == -272);
663 else if (i
== AARCH64_LR_REGNUM
)
664 SELF_CHECK (cache
.saved_regs
[i
].addr () == -264);
666 SELF_CHECK (cache
.saved_regs
[i
].is_realreg ()
667 && cache
.saved_regs
[i
].realreg () == i
);
670 for (int i
= 0; i
< AARCH64_D_REGISTER_COUNT
; i
++)
672 int num_regs
= gdbarch_num_regs (gdbarch
);
673 int regnum
= i
+ num_regs
+ AARCH64_D0_REGNUM
;
675 SELF_CHECK (cache
.saved_regs
[regnum
].is_realreg ()
676 && cache
.saved_regs
[regnum
].realreg () == regnum
);
680 /* Test a prologue in which STR is used and frame pointer is not
683 static const uint32_t insns
[] = {
684 0xf81d0ff3, /* str x19, [sp, #-48]! */
685 0xb9002fe0, /* str w0, [sp, #44] */
686 0xf90013e1, /* str x1, [sp, #32]*/
687 0xfd000fe0, /* str d0, [sp, #24] */
688 0xaa0203f3, /* mov x19, x2 */
689 0xf94013e0, /* ldr x0, [sp, #32] */
691 instruction_reader_test
reader (insns
);
693 trad_frame_reset_saved_regs (gdbarch
, cache
.saved_regs
);
694 CORE_ADDR end
= aarch64_analyze_prologue (gdbarch
, 0, 128, &cache
, reader
);
696 SELF_CHECK (end
== 4 * 5);
698 SELF_CHECK (cache
.framereg
== AARCH64_SP_REGNUM
);
699 SELF_CHECK (cache
.framesize
== 48);
701 for (int i
= 0; i
< AARCH64_X_REGISTER_COUNT
; i
++)
704 SELF_CHECK (cache
.saved_regs
[i
].addr () == -16);
706 SELF_CHECK (cache
.saved_regs
[i
].addr () == -48);
708 SELF_CHECK (cache
.saved_regs
[i
].is_realreg ()
709 && cache
.saved_regs
[i
].realreg () == i
);
712 for (int i
= 0; i
< AARCH64_D_REGISTER_COUNT
; i
++)
714 int num_regs
= gdbarch_num_regs (gdbarch
);
715 int regnum
= i
+ num_regs
+ AARCH64_D0_REGNUM
;
719 SELF_CHECK (cache
.saved_regs
[regnum
].addr () == -24);
721 SELF_CHECK (cache
.saved_regs
[regnum
].is_realreg ()
722 && cache
.saved_regs
[regnum
].realreg () == regnum
);
726 /* Test handling of movz before setting the frame pointer. */
728 static const uint32_t insns
[] = {
729 0xa9bf7bfd, /* stp x29, x30, [sp, #-16]! */
730 0x52800020, /* mov w0, #0x1 */
731 0x910003fd, /* mov x29, sp */
732 0x528000a2, /* mov w2, #0x5 */
733 0x97fffff8, /* bl 6e4 */
736 instruction_reader_test
reader (insns
);
738 trad_frame_reset_saved_regs (gdbarch
, cache
.saved_regs
);
739 CORE_ADDR end
= aarch64_analyze_prologue (gdbarch
, 0, 128, &cache
, reader
);
741 /* We should stop at the 4th instruction. */
742 SELF_CHECK (end
== (4 - 1) * 4);
743 SELF_CHECK (cache
.framereg
== AARCH64_FP_REGNUM
);
744 SELF_CHECK (cache
.framesize
== 16);
747 /* Test handling of movz/stp when using the stack pointer as frame
750 static const uint32_t insns
[] = {
751 0xa9bc7bfd, /* stp x29, x30, [sp, #-64]! */
752 0x52800020, /* mov w0, #0x1 */
753 0x290207e0, /* stp w0, w1, [sp, #16] */
754 0xa9018fe2, /* stp x2, x3, [sp, #24] */
755 0x528000a2, /* mov w2, #0x5 */
756 0x97fffff8, /* bl 6e4 */
759 instruction_reader_test
reader (insns
);
761 trad_frame_reset_saved_regs (gdbarch
, cache
.saved_regs
);
762 CORE_ADDR end
= aarch64_analyze_prologue (gdbarch
, 0, 128, &cache
, reader
);
764 /* We should stop at the 5th instruction. */
765 SELF_CHECK (end
== (5 - 1) * 4);
766 SELF_CHECK (cache
.framereg
== AARCH64_SP_REGNUM
);
767 SELF_CHECK (cache
.framesize
== 64);
770 /* Test handling of movz/str when using the stack pointer as frame
773 static const uint32_t insns
[] = {
774 0xa9bc7bfd, /* stp x29, x30, [sp, #-64]! */
775 0x52800020, /* mov w0, #0x1 */
776 0xb9002be4, /* str w4, [sp, #40] */
777 0xf9001be5, /* str x5, [sp, #48] */
778 0x528000a2, /* mov w2, #0x5 */
779 0x97fffff8, /* bl 6e4 */
782 instruction_reader_test
reader (insns
);
784 trad_frame_reset_saved_regs (gdbarch
, cache
.saved_regs
);
785 CORE_ADDR end
= aarch64_analyze_prologue (gdbarch
, 0, 128, &cache
, reader
);
787 /* We should stop at the 5th instruction. */
788 SELF_CHECK (end
== (5 - 1) * 4);
789 SELF_CHECK (cache
.framereg
== AARCH64_SP_REGNUM
);
790 SELF_CHECK (cache
.framesize
== 64);
793 /* Test handling of movz/stur when using the stack pointer as frame
796 static const uint32_t insns
[] = {
797 0xa9bc7bfd, /* stp x29, x30, [sp, #-64]! */
798 0x52800020, /* mov w0, #0x1 */
799 0xb80343e6, /* stur w6, [sp, #52] */
800 0xf80383e7, /* stur x7, [sp, #56] */
801 0x528000a2, /* mov w2, #0x5 */
802 0x97fffff8, /* bl 6e4 */
805 instruction_reader_test
reader (insns
);
807 trad_frame_reset_saved_regs (gdbarch
, cache
.saved_regs
);
808 CORE_ADDR end
= aarch64_analyze_prologue (gdbarch
, 0, 128, &cache
, reader
);
810 /* We should stop at the 5th instruction. */
811 SELF_CHECK (end
== (5 - 1) * 4);
812 SELF_CHECK (cache
.framereg
== AARCH64_SP_REGNUM
);
813 SELF_CHECK (cache
.framesize
== 64);
816 /* Test handling of movz when there is no frame pointer set or no stack
819 static const uint32_t insns
[] = {
820 0xa9bf7bfd, /* stp x29, x30, [sp, #-16]! */
821 0x52800020, /* mov w0, #0x1 */
822 0x528000a2, /* mov w2, #0x5 */
823 0x97fffff8, /* bl 6e4 */
826 instruction_reader_test
reader (insns
);
828 trad_frame_reset_saved_regs (gdbarch
, cache
.saved_regs
);
829 CORE_ADDR end
= aarch64_analyze_prologue (gdbarch
, 0, 128, &cache
, reader
);
831 /* We should stop at the 4th instruction. */
832 SELF_CHECK (end
== (4 - 1) * 4);
833 SELF_CHECK (cache
.framereg
== AARCH64_SP_REGNUM
);
834 SELF_CHECK (cache
.framesize
== 16);
837 /* Test a prologue in which there is a return address signing instruction. */
838 if (tdep
->has_pauth ())
840 static const uint32_t insns
[] = {
841 0xd503233f, /* paciasp */
842 0xa9bd7bfd, /* stp x29, x30, [sp, #-48]! */
843 0x910003fd, /* mov x29, sp */
844 0xf801c3f3, /* str x19, [sp, #28] */
845 0xb9401fa0, /* ldr x19, [x29, #28] */
847 instruction_reader_test
reader (insns
);
849 trad_frame_reset_saved_regs (gdbarch
, cache
.saved_regs
);
850 CORE_ADDR end
= aarch64_analyze_prologue (gdbarch
, 0, 128, &cache
,
853 SELF_CHECK (end
== 4 * 4);
854 SELF_CHECK (cache
.framereg
== AARCH64_FP_REGNUM
);
855 SELF_CHECK (cache
.framesize
== 48);
857 for (int i
= 0; i
< AARCH64_X_REGISTER_COUNT
; i
++)
860 SELF_CHECK (cache
.saved_regs
[i
].addr () == -20);
861 else if (i
== AARCH64_FP_REGNUM
)
862 SELF_CHECK (cache
.saved_regs
[i
].addr () == -48);
863 else if (i
== AARCH64_LR_REGNUM
)
864 SELF_CHECK (cache
.saved_regs
[i
].addr () == -40);
866 SELF_CHECK (cache
.saved_regs
[i
].is_realreg ()
867 && cache
.saved_regs
[i
].realreg () == i
);
870 if (tdep
->has_pauth ())
872 int regnum
= tdep
->pauth_ra_state_regnum
;
873 SELF_CHECK (cache
.saved_regs
[regnum
].is_value ());
877 /* Test a prologue with a BTI instruction. */
879 static const uint32_t insns
[] = {
880 0xd503245f, /* bti */
881 0xa9bd7bfd, /* stp x29, x30, [sp, #-48]! */
882 0x910003fd, /* mov x29, sp */
883 0xf801c3f3, /* str x19, [sp, #28] */
884 0xb9401fa0, /* ldr x19, [x29, #28] */
886 instruction_reader_test
reader (insns
);
888 trad_frame_reset_saved_regs (gdbarch
, cache
.saved_regs
);
889 CORE_ADDR end
= aarch64_analyze_prologue (gdbarch
, 0, 128, &cache
,
892 SELF_CHECK (end
== 4 * 4);
893 SELF_CHECK (cache
.framereg
== AARCH64_FP_REGNUM
);
894 SELF_CHECK (cache
.framesize
== 48);
896 for (int i
= 0; i
< AARCH64_X_REGISTER_COUNT
; i
++)
899 SELF_CHECK (cache
.saved_regs
[i
].addr () == -20);
900 else if (i
== AARCH64_FP_REGNUM
)
901 SELF_CHECK (cache
.saved_regs
[i
].addr () == -48);
902 else if (i
== AARCH64_LR_REGNUM
)
903 SELF_CHECK (cache
.saved_regs
[i
].addr () == -40);
905 SELF_CHECK (cache
.saved_regs
[i
].is_realreg ()
906 && cache
.saved_regs
[i
].realreg () == i
);
910 } // namespace selftests
911 #endif /* GDB_SELF_TEST */
913 /* Implement the "skip_prologue" gdbarch method. */
916 aarch64_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
918 CORE_ADDR func_addr
, limit_pc
;
920 /* See if we can determine the end of the prologue via the symbol
921 table. If so, then return either PC, or the PC after the
922 prologue, whichever is greater. */
923 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
))
925 CORE_ADDR post_prologue_pc
926 = skip_prologue_using_sal (gdbarch
, func_addr
);
928 if (post_prologue_pc
!= 0)
929 return std::max (pc
, post_prologue_pc
);
932 /* Can't determine prologue from the symbol table, need to examine
935 /* Find an upper limit on the function prologue using the debug
936 information. If the debug information could not be used to
937 provide that bound, then use an arbitrary large number as the
939 limit_pc
= skip_prologue_using_sal (gdbarch
, pc
);
941 limit_pc
= pc
+ 128; /* Magic. */
943 /* Try disassembling prologue. */
944 return aarch64_analyze_prologue (gdbarch
, pc
, limit_pc
, NULL
);
947 /* Scan the function prologue for THIS_FRAME and populate the prologue
951 aarch64_scan_prologue (struct frame_info
*this_frame
,
952 struct aarch64_prologue_cache
*cache
)
954 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
955 CORE_ADDR prologue_start
;
956 CORE_ADDR prologue_end
;
957 CORE_ADDR prev_pc
= get_frame_pc (this_frame
);
958 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
960 cache
->prev_pc
= prev_pc
;
962 /* Assume we do not find a frame. */
963 cache
->framereg
= -1;
964 cache
->framesize
= 0;
966 if (find_pc_partial_function (block_addr
, NULL
, &prologue_start
,
969 struct symtab_and_line sal
= find_pc_line (prologue_start
, 0);
973 /* No line info so use the current PC. */
974 prologue_end
= prev_pc
;
976 else if (sal
.end
< prologue_end
)
978 /* The next line begins after the function end. */
979 prologue_end
= sal
.end
;
982 prologue_end
= std::min (prologue_end
, prev_pc
);
983 aarch64_analyze_prologue (gdbarch
, prologue_start
, prologue_end
, cache
);
989 frame_loc
= get_frame_register_unsigned (this_frame
, AARCH64_FP_REGNUM
);
993 cache
->framereg
= AARCH64_FP_REGNUM
;
994 cache
->framesize
= 16;
995 cache
->saved_regs
[29].set_addr (0);
996 cache
->saved_regs
[30].set_addr (8);
1000 /* Fill in *CACHE with information about the prologue of *THIS_FRAME. This
1001 function may throw an exception if the inferior's registers or memory is
1005 aarch64_make_prologue_cache_1 (struct frame_info
*this_frame
,
1006 struct aarch64_prologue_cache
*cache
)
1008 CORE_ADDR unwound_fp
;
1011 aarch64_scan_prologue (this_frame
, cache
);
1013 if (cache
->framereg
== -1)
1016 unwound_fp
= get_frame_register_unsigned (this_frame
, cache
->framereg
);
1017 if (unwound_fp
== 0)
1020 cache
->prev_sp
= unwound_fp
+ cache
->framesize
;
1022 /* Calculate actual addresses of saved registers using offsets
1023 determined by aarch64_analyze_prologue. */
1024 for (reg
= 0; reg
< gdbarch_num_regs (get_frame_arch (this_frame
)); reg
++)
1025 if (cache
->saved_regs
[reg
].is_addr ())
1026 cache
->saved_regs
[reg
].set_addr (cache
->saved_regs
[reg
].addr ()
1029 cache
->func
= get_frame_func (this_frame
);
1031 cache
->available_p
= 1;
1034 /* Allocate and fill in *THIS_CACHE with information about the prologue of
1035 *THIS_FRAME. Do not do this is if *THIS_CACHE was already allocated.
1036 Return a pointer to the current aarch64_prologue_cache in
1039 static struct aarch64_prologue_cache
*
1040 aarch64_make_prologue_cache (struct frame_info
*this_frame
, void **this_cache
)
1042 struct aarch64_prologue_cache
*cache
;
1044 if (*this_cache
!= NULL
)
1045 return (struct aarch64_prologue_cache
*) *this_cache
;
1047 cache
= FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache
);
1048 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
1049 *this_cache
= cache
;
1053 aarch64_make_prologue_cache_1 (this_frame
, cache
);
1055 catch (const gdb_exception_error
&ex
)
1057 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
1064 /* Implement the "stop_reason" frame_unwind method. */
1066 static enum unwind_stop_reason
1067 aarch64_prologue_frame_unwind_stop_reason (struct frame_info
*this_frame
,
1070 struct aarch64_prologue_cache
*cache
1071 = aarch64_make_prologue_cache (this_frame
, this_cache
);
1073 if (!cache
->available_p
)
1074 return UNWIND_UNAVAILABLE
;
1076 /* Halt the backtrace at "_start". */
1077 gdbarch
*arch
= get_frame_arch (this_frame
);
1078 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (arch
);
1079 if (cache
->prev_pc
<= tdep
->lowest_pc
)
1080 return UNWIND_OUTERMOST
;
1082 /* We've hit a wall, stop. */
1083 if (cache
->prev_sp
== 0)
1084 return UNWIND_OUTERMOST
;
1086 return UNWIND_NO_REASON
;
1089 /* Our frame ID for a normal frame is the current function's starting
1090 PC and the caller's SP when we were called. */
1093 aarch64_prologue_this_id (struct frame_info
*this_frame
,
1094 void **this_cache
, struct frame_id
*this_id
)
1096 struct aarch64_prologue_cache
*cache
1097 = aarch64_make_prologue_cache (this_frame
, this_cache
);
1099 if (!cache
->available_p
)
1100 *this_id
= frame_id_build_unavailable_stack (cache
->func
);
1102 *this_id
= frame_id_build (cache
->prev_sp
, cache
->func
);
1105 /* Implement the "prev_register" frame_unwind method. */
1107 static struct value
*
1108 aarch64_prologue_prev_register (struct frame_info
*this_frame
,
1109 void **this_cache
, int prev_regnum
)
1111 struct aarch64_prologue_cache
*cache
1112 = aarch64_make_prologue_cache (this_frame
, this_cache
);
1114 /* If we are asked to unwind the PC, then we need to return the LR
1115 instead. The prologue may save PC, but it will point into this
1116 frame's prologue, not the next frame's resume location. */
1117 if (prev_regnum
== AARCH64_PC_REGNUM
)
1120 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1121 aarch64_gdbarch_tdep
*tdep
1122 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
1124 lr
= frame_unwind_register_unsigned (this_frame
, AARCH64_LR_REGNUM
);
1126 if (tdep
->has_pauth ()
1127 && cache
->saved_regs
[tdep
->pauth_ra_state_regnum
].is_value ())
1128 lr
= aarch64_frame_unmask_lr (tdep
, this_frame
, lr
);
1130 return frame_unwind_got_constant (this_frame
, prev_regnum
, lr
);
1133 /* SP is generally not saved to the stack, but this frame is
1134 identified by the next frame's stack pointer at the time of the
1135 call. The value was already reconstructed into PREV_SP. */
1141 | | | <- Previous SP
1144 +--| saved fp |<- FP
1148 if (prev_regnum
== AARCH64_SP_REGNUM
)
1149 return frame_unwind_got_constant (this_frame
, prev_regnum
,
1152 return trad_frame_get_prev_register (this_frame
, cache
->saved_regs
,
1156 /* AArch64 prologue unwinder. */
1157 static frame_unwind aarch64_prologue_unwind
=
1161 aarch64_prologue_frame_unwind_stop_reason
,
1162 aarch64_prologue_this_id
,
1163 aarch64_prologue_prev_register
,
1165 default_frame_sniffer
1168 /* Allocate and fill in *THIS_CACHE with information about the prologue of
1169 *THIS_FRAME. Do not do this is if *THIS_CACHE was already allocated.
1170 Return a pointer to the current aarch64_prologue_cache in
1173 static struct aarch64_prologue_cache
*
1174 aarch64_make_stub_cache (struct frame_info
*this_frame
, void **this_cache
)
1176 struct aarch64_prologue_cache
*cache
;
1178 if (*this_cache
!= NULL
)
1179 return (struct aarch64_prologue_cache
*) *this_cache
;
1181 cache
= FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache
);
1182 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
1183 *this_cache
= cache
;
1187 cache
->prev_sp
= get_frame_register_unsigned (this_frame
,
1189 cache
->prev_pc
= get_frame_pc (this_frame
);
1190 cache
->available_p
= 1;
1192 catch (const gdb_exception_error
&ex
)
1194 if (ex
.error
!= NOT_AVAILABLE_ERROR
)
1201 /* Implement the "stop_reason" frame_unwind method. */
1203 static enum unwind_stop_reason
1204 aarch64_stub_frame_unwind_stop_reason (struct frame_info
*this_frame
,
1207 struct aarch64_prologue_cache
*cache
1208 = aarch64_make_stub_cache (this_frame
, this_cache
);
1210 if (!cache
->available_p
)
1211 return UNWIND_UNAVAILABLE
;
1213 return UNWIND_NO_REASON
;
1216 /* Our frame ID for a stub frame is the current SP and LR. */
1219 aarch64_stub_this_id (struct frame_info
*this_frame
,
1220 void **this_cache
, struct frame_id
*this_id
)
1222 struct aarch64_prologue_cache
*cache
1223 = aarch64_make_stub_cache (this_frame
, this_cache
);
1225 if (cache
->available_p
)
1226 *this_id
= frame_id_build (cache
->prev_sp
, cache
->prev_pc
);
1228 *this_id
= frame_id_build_unavailable_stack (cache
->prev_pc
);
1231 /* Implement the "sniffer" frame_unwind method. */
1234 aarch64_stub_unwind_sniffer (const struct frame_unwind
*self
,
1235 struct frame_info
*this_frame
,
1236 void **this_prologue_cache
)
1238 CORE_ADDR addr_in_block
;
1241 addr_in_block
= get_frame_address_in_block (this_frame
);
1242 if (in_plt_section (addr_in_block
)
1243 /* We also use the stub winder if the target memory is unreadable
1244 to avoid having the prologue unwinder trying to read it. */
1245 || target_read_memory (get_frame_pc (this_frame
), dummy
, 4) != 0)
1251 /* AArch64 stub unwinder. */
1252 static frame_unwind aarch64_stub_unwind
=
1256 aarch64_stub_frame_unwind_stop_reason
,
1257 aarch64_stub_this_id
,
1258 aarch64_prologue_prev_register
,
1260 aarch64_stub_unwind_sniffer
1263 /* Return the frame base address of *THIS_FRAME. */
1266 aarch64_normal_frame_base (struct frame_info
*this_frame
, void **this_cache
)
1268 struct aarch64_prologue_cache
*cache
1269 = aarch64_make_prologue_cache (this_frame
, this_cache
);
1271 return cache
->prev_sp
- cache
->framesize
;
1274 /* AArch64 default frame base information. */
1275 static frame_base aarch64_normal_base
=
1277 &aarch64_prologue_unwind
,
1278 aarch64_normal_frame_base
,
1279 aarch64_normal_frame_base
,
1280 aarch64_normal_frame_base
1283 /* Return the value of the REGNUM register in the previous frame of
1286 static struct value
*
1287 aarch64_dwarf2_prev_register (struct frame_info
*this_frame
,
1288 void **this_cache
, int regnum
)
1290 gdbarch
*arch
= get_frame_arch (this_frame
);
1291 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (arch
);
1296 case AARCH64_PC_REGNUM
:
1297 lr
= frame_unwind_register_unsigned (this_frame
, AARCH64_LR_REGNUM
);
1298 lr
= aarch64_frame_unmask_lr (tdep
, this_frame
, lr
);
1299 return frame_unwind_got_constant (this_frame
, regnum
, lr
);
1302 internal_error (__FILE__
, __LINE__
,
1303 _("Unexpected register %d"), regnum
);
1307 static const unsigned char op_lit0
= DW_OP_lit0
;
1308 static const unsigned char op_lit1
= DW_OP_lit1
;
1310 /* Implement the "init_reg" dwarf2_frame_ops method. */
1313 aarch64_dwarf2_frame_init_reg (struct gdbarch
*gdbarch
, int regnum
,
1314 struct dwarf2_frame_state_reg
*reg
,
1315 struct frame_info
*this_frame
)
1317 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
1321 case AARCH64_PC_REGNUM
:
1322 reg
->how
= DWARF2_FRAME_REG_FN
;
1323 reg
->loc
.fn
= aarch64_dwarf2_prev_register
;
1326 case AARCH64_SP_REGNUM
:
1327 reg
->how
= DWARF2_FRAME_REG_CFA
;
1331 /* Init pauth registers. */
1332 if (tdep
->has_pauth ())
1334 if (regnum
== tdep
->pauth_ra_state_regnum
)
1336 /* Initialize RA_STATE to zero. */
1337 reg
->how
= DWARF2_FRAME_REG_SAVED_VAL_EXP
;
1338 reg
->loc
.exp
.start
= &op_lit0
;
1339 reg
->loc
.exp
.len
= 1;
1342 else if (regnum
== AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
)
1343 || regnum
== AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
))
1345 reg
->how
= DWARF2_FRAME_REG_SAME_VALUE
;
1351 /* Implement the execute_dwarf_cfa_vendor_op method. */
1354 aarch64_execute_dwarf_cfa_vendor_op (struct gdbarch
*gdbarch
, gdb_byte op
,
1355 struct dwarf2_frame_state
*fs
)
1357 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
1358 struct dwarf2_frame_state_reg
*ra_state
;
1360 if (op
== DW_CFA_AARCH64_negate_ra_state
)
1362 /* On systems without pauth, treat as a nop. */
1363 if (!tdep
->has_pauth ())
1366 /* Allocate RA_STATE column if it's not allocated yet. */
1367 fs
->regs
.alloc_regs (AARCH64_DWARF_PAUTH_RA_STATE
+ 1);
1369 /* Toggle the status of RA_STATE between 0 and 1. */
1370 ra_state
= &(fs
->regs
.reg
[AARCH64_DWARF_PAUTH_RA_STATE
]);
1371 ra_state
->how
= DWARF2_FRAME_REG_SAVED_VAL_EXP
;
1373 if (ra_state
->loc
.exp
.start
== nullptr
1374 || ra_state
->loc
.exp
.start
== &op_lit0
)
1375 ra_state
->loc
.exp
.start
= &op_lit1
;
1377 ra_state
->loc
.exp
.start
= &op_lit0
;
1379 ra_state
->loc
.exp
.len
= 1;
1387 /* Used for matching BRK instructions for AArch64. */
1388 static constexpr uint32_t BRK_INSN_MASK
= 0xffe0001f;
1389 static constexpr uint32_t BRK_INSN_BASE
= 0xd4200000;
1391 /* Implementation of gdbarch_program_breakpoint_here_p for aarch64. */
1394 aarch64_program_breakpoint_here_p (gdbarch
*gdbarch
, CORE_ADDR address
)
1396 const uint32_t insn_len
= 4;
1397 gdb_byte target_mem
[4];
1399 /* Enable the automatic memory restoration from breakpoints while
1400 we read the memory. Otherwise we may find temporary breakpoints, ones
1401 inserted by GDB, and flag them as permanent breakpoints. */
1402 scoped_restore restore_memory
1403 = make_scoped_restore_show_memory_breakpoints (0);
1405 if (target_read_memory (address
, target_mem
, insn_len
) == 0)
1408 (uint32_t) extract_unsigned_integer (target_mem
, insn_len
,
1409 gdbarch_byte_order_for_code (gdbarch
));
1411 /* Check if INSN is a BRK instruction pattern. There are multiple choices
1412 of such instructions with different immediate values. Different OS'
1413 may use a different variation, but they have the same outcome. */
1414 return ((insn
& BRK_INSN_MASK
) == BRK_INSN_BASE
);
1420 /* When arguments must be pushed onto the stack, they go on in reverse
1421 order. The code below implements a FILO (stack) to do this. */
1425 /* Value to pass on stack. It can be NULL if this item is for stack
1427 const gdb_byte
*data
;
1429 /* Size in bytes of value to pass on stack. */
1433 /* Implement the gdbarch type alignment method, overrides the generic
1434 alignment algorithm for anything that is aarch64 specific. */
1437 aarch64_type_align (gdbarch
*gdbarch
, struct type
*t
)
1439 t
= check_typedef (t
);
1440 if (t
->code () == TYPE_CODE_ARRAY
&& t
->is_vector ())
1442 /* Use the natural alignment for vector types (the same for
1443 scalar type), but the maximum alignment is 128-bit. */
1444 if (TYPE_LENGTH (t
) > 16)
1447 return TYPE_LENGTH (t
);
1450 /* Allow the common code to calculate the alignment. */
1454 /* Worker function for aapcs_is_vfp_call_or_return_candidate.
1456 Return the number of register required, or -1 on failure.
1458 When encountering a base element, if FUNDAMENTAL_TYPE is not set then set it
1459 to the element, else fail if the type of this element does not match the
1463 aapcs_is_vfp_call_or_return_candidate_1 (struct type
*type
,
1464 struct type
**fundamental_type
)
1466 if (type
== nullptr)
1469 switch (type
->code ())
1472 if (TYPE_LENGTH (type
) > 16)
1475 if (*fundamental_type
== nullptr)
1476 *fundamental_type
= type
;
1477 else if (TYPE_LENGTH (type
) != TYPE_LENGTH (*fundamental_type
)
1478 || type
->code () != (*fundamental_type
)->code ())
1483 case TYPE_CODE_COMPLEX
:
1485 struct type
*target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
1486 if (TYPE_LENGTH (target_type
) > 16)
1489 if (*fundamental_type
== nullptr)
1490 *fundamental_type
= target_type
;
1491 else if (TYPE_LENGTH (target_type
) != TYPE_LENGTH (*fundamental_type
)
1492 || target_type
->code () != (*fundamental_type
)->code ())
1498 case TYPE_CODE_ARRAY
:
1500 if (type
->is_vector ())
1502 if (TYPE_LENGTH (type
) != 8 && TYPE_LENGTH (type
) != 16)
1505 if (*fundamental_type
== nullptr)
1506 *fundamental_type
= type
;
1507 else if (TYPE_LENGTH (type
) != TYPE_LENGTH (*fundamental_type
)
1508 || type
->code () != (*fundamental_type
)->code ())
1515 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
1516 int count
= aapcs_is_vfp_call_or_return_candidate_1
1517 (target_type
, fundamental_type
);
1522 count
*= (TYPE_LENGTH (type
) / TYPE_LENGTH (target_type
));
1527 case TYPE_CODE_STRUCT
:
1528 case TYPE_CODE_UNION
:
1532 for (int i
= 0; i
< type
->num_fields (); i
++)
1534 /* Ignore any static fields. */
1535 if (field_is_static (&type
->field (i
)))
1538 struct type
*member
= check_typedef (type
->field (i
).type ());
1540 int sub_count
= aapcs_is_vfp_call_or_return_candidate_1
1541 (member
, fundamental_type
);
1542 if (sub_count
== -1)
1547 /* Ensure there is no padding between the fields (allowing for empty
1548 zero length structs) */
1549 int ftype_length
= (*fundamental_type
== nullptr)
1550 ? 0 : TYPE_LENGTH (*fundamental_type
);
1551 if (count
* ftype_length
!= TYPE_LENGTH (type
))
1564 /* Return true if an argument, whose type is described by TYPE, can be passed or
1565 returned in simd/fp registers, providing enough parameter passing registers
1566 are available. This is as described in the AAPCS64.
1568 Upon successful return, *COUNT returns the number of needed registers,
1569 *FUNDAMENTAL_TYPE contains the type of those registers.
1571 Candidate as per the AAPCS64 5.4.2.C is either a:
1574 - HFA (Homogeneous Floating-point Aggregate, 4.3.5.1). A Composite type where
1575 all the members are floats and has at most 4 members.
1576 - HVA (Homogeneous Short-vector Aggregate, 4.3.5.2). A Composite type where
1577 all the members are short vectors and has at most 4 members.
1580 Note that HFAs and HVAs can include nested structures and arrays. */
1583 aapcs_is_vfp_call_or_return_candidate (struct type
*type
, int *count
,
1584 struct type
**fundamental_type
)
1586 if (type
== nullptr)
1589 *fundamental_type
= nullptr;
1591 int ag_count
= aapcs_is_vfp_call_or_return_candidate_1 (type
,
1594 if (ag_count
> 0 && ag_count
<= HA_MAX_NUM_FLDS
)
1603 /* AArch64 function call information structure. */
1604 struct aarch64_call_info
1606 /* the current argument number. */
1607 unsigned argnum
= 0;
1609 /* The next general purpose register number, equivalent to NGRN as
1610 described in the AArch64 Procedure Call Standard. */
1613 /* The next SIMD and floating point register number, equivalent to
1614 NSRN as described in the AArch64 Procedure Call Standard. */
1617 /* The next stacked argument address, equivalent to NSAA as
1618 described in the AArch64 Procedure Call Standard. */
1621 /* Stack item vector. */
1622 std::vector
<stack_item_t
> si
;
1625 /* Pass a value in a sequence of consecutive X registers. The caller
1626 is responsible for ensuring sufficient registers are available. */
1629 pass_in_x (struct gdbarch
*gdbarch
, struct regcache
*regcache
,
1630 struct aarch64_call_info
*info
, struct type
*type
,
1633 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1634 int len
= TYPE_LENGTH (type
);
1635 enum type_code typecode
= type
->code ();
1636 int regnum
= AARCH64_X0_REGNUM
+ info
->ngrn
;
1637 const bfd_byte
*buf
= value_contents (arg
).data ();
1643 int partial_len
= len
< X_REGISTER_SIZE
? len
: X_REGISTER_SIZE
;
1644 CORE_ADDR regval
= extract_unsigned_integer (buf
, partial_len
,
1648 /* Adjust sub-word struct/union args when big-endian. */
1649 if (byte_order
== BFD_ENDIAN_BIG
1650 && partial_len
< X_REGISTER_SIZE
1651 && (typecode
== TYPE_CODE_STRUCT
|| typecode
== TYPE_CODE_UNION
))
1652 regval
<<= ((X_REGISTER_SIZE
- partial_len
) * TARGET_CHAR_BIT
);
1654 aarch64_debug_printf ("arg %d in %s = 0x%s", info
->argnum
,
1655 gdbarch_register_name (gdbarch
, regnum
),
1656 phex (regval
, X_REGISTER_SIZE
));
1658 regcache_cooked_write_unsigned (regcache
, regnum
, regval
);
1665 /* Attempt to marshall a value in a V register. Return 1 if
1666 successful, or 0 if insufficient registers are available. This
1667 function, unlike the equivalent pass_in_x() function does not
1668 handle arguments spread across multiple registers. */
1671 pass_in_v (struct gdbarch
*gdbarch
,
1672 struct regcache
*regcache
,
1673 struct aarch64_call_info
*info
,
1674 int len
, const bfd_byte
*buf
)
1678 int regnum
= AARCH64_V0_REGNUM
+ info
->nsrn
;
1679 /* Enough space for a full vector register. */
1680 gdb_byte reg
[register_size (gdbarch
, regnum
)];
1681 gdb_assert (len
<= sizeof (reg
));
1686 memset (reg
, 0, sizeof (reg
));
1687 /* PCS C.1, the argument is allocated to the least significant
1688 bits of V register. */
1689 memcpy (reg
, buf
, len
);
1690 regcache
->cooked_write (regnum
, reg
);
1692 aarch64_debug_printf ("arg %d in %s", info
->argnum
,
1693 gdbarch_register_name (gdbarch
, regnum
));
1701 /* Marshall an argument onto the stack. */
1704 pass_on_stack (struct aarch64_call_info
*info
, struct type
*type
,
1707 const bfd_byte
*buf
= value_contents (arg
).data ();
1708 int len
= TYPE_LENGTH (type
);
1714 align
= type_align (type
);
1716 /* PCS C.17 Stack should be aligned to the larger of 8 bytes or the
1717 Natural alignment of the argument's type. */
1718 align
= align_up (align
, 8);
1720 /* The AArch64 PCS requires at most doubleword alignment. */
1724 aarch64_debug_printf ("arg %d len=%d @ sp + %d\n", info
->argnum
, len
,
1729 info
->si
.push_back (item
);
1732 if (info
->nsaa
& (align
- 1))
1734 /* Push stack alignment padding. */
1735 int pad
= align
- (info
->nsaa
& (align
- 1));
1740 info
->si
.push_back (item
);
1745 /* Marshall an argument into a sequence of one or more consecutive X
1746 registers or, if insufficient X registers are available then onto
1750 pass_in_x_or_stack (struct gdbarch
*gdbarch
, struct regcache
*regcache
,
1751 struct aarch64_call_info
*info
, struct type
*type
,
1754 int len
= TYPE_LENGTH (type
);
1755 int nregs
= (len
+ X_REGISTER_SIZE
- 1) / X_REGISTER_SIZE
;
1757 /* PCS C.13 - Pass in registers if we have enough spare */
1758 if (info
->ngrn
+ nregs
<= 8)
1760 pass_in_x (gdbarch
, regcache
, info
, type
, arg
);
1761 info
->ngrn
+= nregs
;
1766 pass_on_stack (info
, type
, arg
);
1770 /* Pass a value, which is of type arg_type, in a V register. Assumes value is a
1771 aapcs_is_vfp_call_or_return_candidate and there are enough spare V
1772 registers. A return value of false is an error state as the value will have
1773 been partially passed to the stack. */
1775 pass_in_v_vfp_candidate (struct gdbarch
*gdbarch
, struct regcache
*regcache
,
1776 struct aarch64_call_info
*info
, struct type
*arg_type
,
1779 switch (arg_type
->code ())
1782 return pass_in_v (gdbarch
, regcache
, info
, TYPE_LENGTH (arg_type
),
1783 value_contents (arg
).data ());
1786 case TYPE_CODE_COMPLEX
:
1788 const bfd_byte
*buf
= value_contents (arg
).data ();
1789 struct type
*target_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
1791 if (!pass_in_v (gdbarch
, regcache
, info
, TYPE_LENGTH (target_type
),
1795 return pass_in_v (gdbarch
, regcache
, info
, TYPE_LENGTH (target_type
),
1796 buf
+ TYPE_LENGTH (target_type
));
1799 case TYPE_CODE_ARRAY
:
1800 if (arg_type
->is_vector ())
1801 return pass_in_v (gdbarch
, regcache
, info
, TYPE_LENGTH (arg_type
),
1802 value_contents (arg
).data ());
1805 case TYPE_CODE_STRUCT
:
1806 case TYPE_CODE_UNION
:
1807 for (int i
= 0; i
< arg_type
->num_fields (); i
++)
1809 /* Don't include static fields. */
1810 if (field_is_static (&arg_type
->field (i
)))
1813 struct value
*field
= value_primitive_field (arg
, 0, i
, arg_type
);
1814 struct type
*field_type
= check_typedef (value_type (field
));
1816 if (!pass_in_v_vfp_candidate (gdbarch
, regcache
, info
, field_type
,
1827 /* Implement the "push_dummy_call" gdbarch method. */
1830 aarch64_push_dummy_call (struct gdbarch
*gdbarch
, struct value
*function
,
1831 struct regcache
*regcache
, CORE_ADDR bp_addr
,
1833 struct value
**args
, CORE_ADDR sp
,
1834 function_call_return_method return_method
,
1835 CORE_ADDR struct_addr
)
1838 struct aarch64_call_info info
;
1840 /* We need to know what the type of the called function is in order
1841 to determine the number of named/anonymous arguments for the
1842 actual argument placement, and the return type in order to handle
1843 return value correctly.
1845 The generic code above us views the decision of return in memory
1846 or return in registers as a two stage processes. The language
1847 handler is consulted first and may decide to return in memory (eg
1848 class with copy constructor returned by value), this will cause
1849 the generic code to allocate space AND insert an initial leading
1852 If the language code does not decide to pass in memory then the
1853 target code is consulted.
1855 If the language code decides to pass in memory we want to move
1856 the pointer inserted as the initial argument from the argument
1857 list and into X8, the conventional AArch64 struct return pointer
1860 /* Set the return address. For the AArch64, the return breakpoint
1861 is always at BP_ADDR. */
1862 regcache_cooked_write_unsigned (regcache
, AARCH64_LR_REGNUM
, bp_addr
);
1864 /* If we were given an initial argument for the return slot, lose it. */
1865 if (return_method
== return_method_hidden_param
)
1871 /* The struct_return pointer occupies X8. */
1872 if (return_method
!= return_method_normal
)
1874 aarch64_debug_printf ("struct return in %s = 0x%s",
1875 gdbarch_register_name
1876 (gdbarch
, AARCH64_STRUCT_RETURN_REGNUM
),
1877 paddress (gdbarch
, struct_addr
));
1879 regcache_cooked_write_unsigned (regcache
, AARCH64_STRUCT_RETURN_REGNUM
,
1883 for (argnum
= 0; argnum
< nargs
; argnum
++)
1885 struct value
*arg
= args
[argnum
];
1886 struct type
*arg_type
, *fundamental_type
;
1889 arg_type
= check_typedef (value_type (arg
));
1890 len
= TYPE_LENGTH (arg_type
);
1892 /* If arg can be passed in v registers as per the AAPCS64, then do so if
1893 if there are enough spare registers. */
1894 if (aapcs_is_vfp_call_or_return_candidate (arg_type
, &elements
,
1897 if (info
.nsrn
+ elements
<= 8)
1899 /* We know that we have sufficient registers available therefore
1900 this will never need to fallback to the stack. */
1901 if (!pass_in_v_vfp_candidate (gdbarch
, regcache
, &info
, arg_type
,
1903 gdb_assert_not_reached ("Failed to push args");
1908 pass_on_stack (&info
, arg_type
, arg
);
1913 switch (arg_type
->code ())
1916 case TYPE_CODE_BOOL
:
1917 case TYPE_CODE_CHAR
:
1918 case TYPE_CODE_RANGE
:
1919 case TYPE_CODE_ENUM
:
1920 if (len
< 4 && !is_fixed_point_type (arg_type
))
1922 /* Promote to 32 bit integer. */
1923 if (arg_type
->is_unsigned ())
1924 arg_type
= builtin_type (gdbarch
)->builtin_uint32
;
1926 arg_type
= builtin_type (gdbarch
)->builtin_int32
;
1927 arg
= value_cast (arg_type
, arg
);
1929 pass_in_x_or_stack (gdbarch
, regcache
, &info
, arg_type
, arg
);
1932 case TYPE_CODE_STRUCT
:
1933 case TYPE_CODE_ARRAY
:
1934 case TYPE_CODE_UNION
:
1937 /* PCS B.7 Aggregates larger than 16 bytes are passed by
1938 invisible reference. */
1940 /* Allocate aligned storage. */
1941 sp
= align_down (sp
- len
, 16);
1943 /* Write the real data into the stack. */
1944 write_memory (sp
, value_contents (arg
).data (), len
);
1946 /* Construct the indirection. */
1947 arg_type
= lookup_pointer_type (arg_type
);
1948 arg
= value_from_pointer (arg_type
, sp
);
1949 pass_in_x_or_stack (gdbarch
, regcache
, &info
, arg_type
, arg
);
1952 /* PCS C.15 / C.18 multiple values pass. */
1953 pass_in_x_or_stack (gdbarch
, regcache
, &info
, arg_type
, arg
);
1957 pass_in_x_or_stack (gdbarch
, regcache
, &info
, arg_type
, arg
);
1962 /* Make sure stack retains 16 byte alignment. */
1964 sp
-= 16 - (info
.nsaa
& 15);
1966 while (!info
.si
.empty ())
1968 const stack_item_t
&si
= info
.si
.back ();
1971 if (si
.data
!= NULL
)
1972 write_memory (sp
, si
.data
, si
.len
);
1973 info
.si
.pop_back ();
1976 /* Finally, update the SP register. */
1977 regcache_cooked_write_unsigned (regcache
, AARCH64_SP_REGNUM
, sp
);
1982 /* Implement the "frame_align" gdbarch method. */
1985 aarch64_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR sp
)
1987 /* Align the stack to sixteen bytes. */
1988 return sp
& ~(CORE_ADDR
) 15;
1991 /* Return the type for an AdvSISD Q register. */
1993 static struct type
*
1994 aarch64_vnq_type (struct gdbarch
*gdbarch
)
1996 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
1998 if (tdep
->vnq_type
== NULL
)
2003 t
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vnq",
2006 elem
= builtin_type (gdbarch
)->builtin_uint128
;
2007 append_composite_type_field (t
, "u", elem
);
2009 elem
= builtin_type (gdbarch
)->builtin_int128
;
2010 append_composite_type_field (t
, "s", elem
);
2015 return tdep
->vnq_type
;
2018 /* Return the type for an AdvSISD D register. */
2020 static struct type
*
2021 aarch64_vnd_type (struct gdbarch
*gdbarch
)
2023 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2025 if (tdep
->vnd_type
== NULL
)
2030 t
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vnd",
2033 elem
= builtin_type (gdbarch
)->builtin_double
;
2034 append_composite_type_field (t
, "f", elem
);
2036 elem
= builtin_type (gdbarch
)->builtin_uint64
;
2037 append_composite_type_field (t
, "u", elem
);
2039 elem
= builtin_type (gdbarch
)->builtin_int64
;
2040 append_composite_type_field (t
, "s", elem
);
2045 return tdep
->vnd_type
;
2048 /* Return the type for an AdvSISD S register. */
2050 static struct type
*
2051 aarch64_vns_type (struct gdbarch
*gdbarch
)
2053 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2055 if (tdep
->vns_type
== NULL
)
2060 t
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vns",
2063 elem
= builtin_type (gdbarch
)->builtin_float
;
2064 append_composite_type_field (t
, "f", elem
);
2066 elem
= builtin_type (gdbarch
)->builtin_uint32
;
2067 append_composite_type_field (t
, "u", elem
);
2069 elem
= builtin_type (gdbarch
)->builtin_int32
;
2070 append_composite_type_field (t
, "s", elem
);
2075 return tdep
->vns_type
;
2078 /* Return the type for an AdvSISD H register. */
2080 static struct type
*
2081 aarch64_vnh_type (struct gdbarch
*gdbarch
)
2083 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2085 if (tdep
->vnh_type
== NULL
)
2090 t
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vnh",
2093 elem
= builtin_type (gdbarch
)->builtin_bfloat16
;
2094 append_composite_type_field (t
, "bf", elem
);
2096 elem
= builtin_type (gdbarch
)->builtin_half
;
2097 append_composite_type_field (t
, "f", elem
);
2099 elem
= builtin_type (gdbarch
)->builtin_uint16
;
2100 append_composite_type_field (t
, "u", elem
);
2102 elem
= builtin_type (gdbarch
)->builtin_int16
;
2103 append_composite_type_field (t
, "s", elem
);
2108 return tdep
->vnh_type
;
2111 /* Return the type for an AdvSISD B register. */
2113 static struct type
*
2114 aarch64_vnb_type (struct gdbarch
*gdbarch
)
2116 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2118 if (tdep
->vnb_type
== NULL
)
2123 t
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vnb",
2126 elem
= builtin_type (gdbarch
)->builtin_uint8
;
2127 append_composite_type_field (t
, "u", elem
);
2129 elem
= builtin_type (gdbarch
)->builtin_int8
;
2130 append_composite_type_field (t
, "s", elem
);
2135 return tdep
->vnb_type
;
2138 /* Return the type for an AdvSISD V register. */
2140 static struct type
*
2141 aarch64_vnv_type (struct gdbarch
*gdbarch
)
2143 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2145 if (tdep
->vnv_type
== NULL
)
2147 /* The other AArch64 pseudo registers (Q,D,H,S,B) refer to a single value
2148 slice from the non-pseudo vector registers. However NEON V registers
2149 are always vector registers, and need constructing as such. */
2150 const struct builtin_type
*bt
= builtin_type (gdbarch
);
2152 struct type
*t
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vnv",
2155 struct type
*sub
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vnd",
2157 append_composite_type_field (sub
, "f",
2158 init_vector_type (bt
->builtin_double
, 2));
2159 append_composite_type_field (sub
, "u",
2160 init_vector_type (bt
->builtin_uint64
, 2));
2161 append_composite_type_field (sub
, "s",
2162 init_vector_type (bt
->builtin_int64
, 2));
2163 append_composite_type_field (t
, "d", sub
);
2165 sub
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vns",
2167 append_composite_type_field (sub
, "f",
2168 init_vector_type (bt
->builtin_float
, 4));
2169 append_composite_type_field (sub
, "u",
2170 init_vector_type (bt
->builtin_uint32
, 4));
2171 append_composite_type_field (sub
, "s",
2172 init_vector_type (bt
->builtin_int32
, 4));
2173 append_composite_type_field (t
, "s", sub
);
2175 sub
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vnh",
2177 append_composite_type_field (sub
, "bf",
2178 init_vector_type (bt
->builtin_bfloat16
, 8));
2179 append_composite_type_field (sub
, "f",
2180 init_vector_type (bt
->builtin_half
, 8));
2181 append_composite_type_field (sub
, "u",
2182 init_vector_type (bt
->builtin_uint16
, 8));
2183 append_composite_type_field (sub
, "s",
2184 init_vector_type (bt
->builtin_int16
, 8));
2185 append_composite_type_field (t
, "h", sub
);
2187 sub
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vnb",
2189 append_composite_type_field (sub
, "u",
2190 init_vector_type (bt
->builtin_uint8
, 16));
2191 append_composite_type_field (sub
, "s",
2192 init_vector_type (bt
->builtin_int8
, 16));
2193 append_composite_type_field (t
, "b", sub
);
2195 sub
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vnq",
2197 append_composite_type_field (sub
, "u",
2198 init_vector_type (bt
->builtin_uint128
, 1));
2199 append_composite_type_field (sub
, "s",
2200 init_vector_type (bt
->builtin_int128
, 1));
2201 append_composite_type_field (t
, "q", sub
);
2206 return tdep
->vnv_type
;
2209 /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
2212 aarch64_dwarf_reg_to_regnum (struct gdbarch
*gdbarch
, int reg
)
2214 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2216 if (reg
>= AARCH64_DWARF_X0
&& reg
<= AARCH64_DWARF_X0
+ 30)
2217 return AARCH64_X0_REGNUM
+ reg
- AARCH64_DWARF_X0
;
2219 if (reg
== AARCH64_DWARF_SP
)
2220 return AARCH64_SP_REGNUM
;
2222 if (reg
>= AARCH64_DWARF_V0
&& reg
<= AARCH64_DWARF_V0
+ 31)
2223 return AARCH64_V0_REGNUM
+ reg
- AARCH64_DWARF_V0
;
2225 if (reg
== AARCH64_DWARF_SVE_VG
)
2226 return AARCH64_SVE_VG_REGNUM
;
2228 if (reg
== AARCH64_DWARF_SVE_FFR
)
2229 return AARCH64_SVE_FFR_REGNUM
;
2231 if (reg
>= AARCH64_DWARF_SVE_P0
&& reg
<= AARCH64_DWARF_SVE_P0
+ 15)
2232 return AARCH64_SVE_P0_REGNUM
+ reg
- AARCH64_DWARF_SVE_P0
;
2234 if (reg
>= AARCH64_DWARF_SVE_Z0
&& reg
<= AARCH64_DWARF_SVE_Z0
+ 15)
2235 return AARCH64_SVE_Z0_REGNUM
+ reg
- AARCH64_DWARF_SVE_Z0
;
2237 if (tdep
->has_pauth ())
2239 if (reg
>= AARCH64_DWARF_PAUTH_DMASK
&& reg
<= AARCH64_DWARF_PAUTH_CMASK
)
2240 return tdep
->pauth_reg_base
+ reg
- AARCH64_DWARF_PAUTH_DMASK
;
2242 if (reg
== AARCH64_DWARF_PAUTH_RA_STATE
)
2243 return tdep
->pauth_ra_state_regnum
;
2249 /* Implement the "print_insn" gdbarch method. */
2252 aarch64_gdb_print_insn (bfd_vma memaddr
, disassemble_info
*info
)
2254 info
->symbols
= NULL
;
2255 return default_print_insn (memaddr
, info
);
2258 /* AArch64 BRK software debug mode instruction.
2259 Note that AArch64 code is always little-endian.
2260 1101.0100.0010.0000.0000.0000.0000.0000 = 0xd4200000. */
2261 constexpr gdb_byte aarch64_default_breakpoint
[] = {0x00, 0x00, 0x20, 0xd4};
2263 typedef BP_MANIPULATION (aarch64_default_breakpoint
) aarch64_breakpoint
;
2265 /* Extract from an array REGS containing the (raw) register state a
2266 function return value of type TYPE, and copy that, in virtual
2267 format, into VALBUF. */
2270 aarch64_extract_return_value (struct type
*type
, struct regcache
*regs
,
2273 struct gdbarch
*gdbarch
= regs
->arch ();
2274 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2276 struct type
*fundamental_type
;
2278 if (aapcs_is_vfp_call_or_return_candidate (type
, &elements
,
2281 int len
= TYPE_LENGTH (fundamental_type
);
2283 for (int i
= 0; i
< elements
; i
++)
2285 int regno
= AARCH64_V0_REGNUM
+ i
;
2286 /* Enough space for a full vector register. */
2287 gdb_byte buf
[register_size (gdbarch
, regno
)];
2288 gdb_assert (len
<= sizeof (buf
));
2290 aarch64_debug_printf
2291 ("read HFA or HVA return value element %d from %s",
2292 i
+ 1, gdbarch_register_name (gdbarch
, regno
));
2294 regs
->cooked_read (regno
, buf
);
2296 memcpy (valbuf
, buf
, len
);
2300 else if (type
->code () == TYPE_CODE_INT
2301 || type
->code () == TYPE_CODE_CHAR
2302 || type
->code () == TYPE_CODE_BOOL
2303 || type
->code () == TYPE_CODE_PTR
2304 || TYPE_IS_REFERENCE (type
)
2305 || type
->code () == TYPE_CODE_ENUM
)
2307 /* If the type is a plain integer, then the access is
2308 straight-forward. Otherwise we have to play around a bit
2310 int len
= TYPE_LENGTH (type
);
2311 int regno
= AARCH64_X0_REGNUM
;
2316 /* By using store_unsigned_integer we avoid having to do
2317 anything special for small big-endian values. */
2318 regcache_cooked_read_unsigned (regs
, regno
++, &tmp
);
2319 store_unsigned_integer (valbuf
,
2320 (len
> X_REGISTER_SIZE
2321 ? X_REGISTER_SIZE
: len
), byte_order
, tmp
);
2322 len
-= X_REGISTER_SIZE
;
2323 valbuf
+= X_REGISTER_SIZE
;
2328 /* For a structure or union the behaviour is as if the value had
2329 been stored to word-aligned memory and then loaded into
2330 registers with 64-bit load instruction(s). */
2331 int len
= TYPE_LENGTH (type
);
2332 int regno
= AARCH64_X0_REGNUM
;
2333 bfd_byte buf
[X_REGISTER_SIZE
];
2337 regs
->cooked_read (regno
++, buf
);
2338 memcpy (valbuf
, buf
, len
> X_REGISTER_SIZE
? X_REGISTER_SIZE
: len
);
2339 len
-= X_REGISTER_SIZE
;
2340 valbuf
+= X_REGISTER_SIZE
;
2346 /* Will a function return an aggregate type in memory or in a
2347 register? Return 0 if an aggregate type can be returned in a
2348 register, 1 if it must be returned in memory. */
2351 aarch64_return_in_memory (struct gdbarch
*gdbarch
, struct type
*type
)
2353 type
= check_typedef (type
);
2355 struct type
*fundamental_type
;
2357 if (aapcs_is_vfp_call_or_return_candidate (type
, &elements
,
2360 /* v0-v7 are used to return values and one register is allocated
2361 for one member. However, HFA or HVA has at most four members. */
2365 if (TYPE_LENGTH (type
) > 16
2366 || !language_pass_by_reference (type
).trivially_copyable
)
2368 /* PCS B.6 Aggregates larger than 16 bytes are passed by
2369 invisible reference. */
2377 /* Write into appropriate registers a function return value of type
2378 TYPE, given in virtual format. */
2381 aarch64_store_return_value (struct type
*type
, struct regcache
*regs
,
2382 const gdb_byte
*valbuf
)
2384 struct gdbarch
*gdbarch
= regs
->arch ();
2385 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2387 struct type
*fundamental_type
;
2389 if (aapcs_is_vfp_call_or_return_candidate (type
, &elements
,
2392 int len
= TYPE_LENGTH (fundamental_type
);
2394 for (int i
= 0; i
< elements
; i
++)
2396 int regno
= AARCH64_V0_REGNUM
+ i
;
2397 /* Enough space for a full vector register. */
2398 gdb_byte tmpbuf
[register_size (gdbarch
, regno
)];
2399 gdb_assert (len
<= sizeof (tmpbuf
));
2401 aarch64_debug_printf
2402 ("write HFA or HVA return value element %d to %s",
2403 i
+ 1, gdbarch_register_name (gdbarch
, regno
));
2405 memcpy (tmpbuf
, valbuf
,
2406 len
> V_REGISTER_SIZE
? V_REGISTER_SIZE
: len
);
2407 regs
->cooked_write (regno
, tmpbuf
);
2411 else if (type
->code () == TYPE_CODE_INT
2412 || type
->code () == TYPE_CODE_CHAR
2413 || type
->code () == TYPE_CODE_BOOL
2414 || type
->code () == TYPE_CODE_PTR
2415 || TYPE_IS_REFERENCE (type
)
2416 || type
->code () == TYPE_CODE_ENUM
)
2418 if (TYPE_LENGTH (type
) <= X_REGISTER_SIZE
)
2420 /* Values of one word or less are zero/sign-extended and
2422 bfd_byte tmpbuf
[X_REGISTER_SIZE
];
2423 LONGEST val
= unpack_long (type
, valbuf
);
2425 store_signed_integer (tmpbuf
, X_REGISTER_SIZE
, byte_order
, val
);
2426 regs
->cooked_write (AARCH64_X0_REGNUM
, tmpbuf
);
2430 /* Integral values greater than one word are stored in
2431 consecutive registers starting with r0. This will always
2432 be a multiple of the regiser size. */
2433 int len
= TYPE_LENGTH (type
);
2434 int regno
= AARCH64_X0_REGNUM
;
2438 regs
->cooked_write (regno
++, valbuf
);
2439 len
-= X_REGISTER_SIZE
;
2440 valbuf
+= X_REGISTER_SIZE
;
2446 /* For a structure or union the behaviour is as if the value had
2447 been stored to word-aligned memory and then loaded into
2448 registers with 64-bit load instruction(s). */
2449 int len
= TYPE_LENGTH (type
);
2450 int regno
= AARCH64_X0_REGNUM
;
2451 bfd_byte tmpbuf
[X_REGISTER_SIZE
];
2455 memcpy (tmpbuf
, valbuf
,
2456 len
> X_REGISTER_SIZE
? X_REGISTER_SIZE
: len
);
2457 regs
->cooked_write (regno
++, tmpbuf
);
2458 len
-= X_REGISTER_SIZE
;
2459 valbuf
+= X_REGISTER_SIZE
;
2464 /* Implement the "return_value" gdbarch method. */
2466 static enum return_value_convention
2467 aarch64_return_value (struct gdbarch
*gdbarch
, struct value
*func_value
,
2468 struct type
*valtype
, struct regcache
*regcache
,
2469 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
2472 if (valtype
->code () == TYPE_CODE_STRUCT
2473 || valtype
->code () == TYPE_CODE_UNION
2474 || valtype
->code () == TYPE_CODE_ARRAY
)
2476 if (aarch64_return_in_memory (gdbarch
, valtype
))
2478 /* From the AAPCS64's Result Return section:
2480 "Otherwise, the caller shall reserve a block of memory of
2481 sufficient size and alignment to hold the result. The address
2482 of the memory block shall be passed as an additional argument to
2483 the function in x8. */
2485 aarch64_debug_printf ("return value in memory");
2491 regcache
->cooked_read (AARCH64_STRUCT_RETURN_REGNUM
, &addr
);
2492 read_memory (addr
, readbuf
, TYPE_LENGTH (valtype
));
2495 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
2500 aarch64_store_return_value (valtype
, regcache
, writebuf
);
2503 aarch64_extract_return_value (valtype
, regcache
, readbuf
);
2505 aarch64_debug_printf ("return value in registers");
2507 return RETURN_VALUE_REGISTER_CONVENTION
;
2510 /* Implement the "get_longjmp_target" gdbarch method. */
2513 aarch64_get_longjmp_target (struct frame_info
*frame
, CORE_ADDR
*pc
)
2516 gdb_byte buf
[X_REGISTER_SIZE
];
2517 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
2518 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2519 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2521 jb_addr
= get_frame_register_unsigned (frame
, AARCH64_X0_REGNUM
);
2523 if (target_read_memory (jb_addr
+ tdep
->jb_pc
* tdep
->jb_elt_size
, buf
,
2527 *pc
= extract_unsigned_integer (buf
, X_REGISTER_SIZE
, byte_order
);
2531 /* Implement the "gen_return_address" gdbarch method. */
2534 aarch64_gen_return_address (struct gdbarch
*gdbarch
,
2535 struct agent_expr
*ax
, struct axs_value
*value
,
2538 value
->type
= register_type (gdbarch
, AARCH64_LR_REGNUM
);
2539 value
->kind
= axs_lvalue_register
;
2540 value
->u
.reg
= AARCH64_LR_REGNUM
;
2544 /* Return the pseudo register name corresponding to register regnum. */
2547 aarch64_pseudo_register_name (struct gdbarch
*gdbarch
, int regnum
)
2549 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2551 static const char *const q_name
[] =
2553 "q0", "q1", "q2", "q3",
2554 "q4", "q5", "q6", "q7",
2555 "q8", "q9", "q10", "q11",
2556 "q12", "q13", "q14", "q15",
2557 "q16", "q17", "q18", "q19",
2558 "q20", "q21", "q22", "q23",
2559 "q24", "q25", "q26", "q27",
2560 "q28", "q29", "q30", "q31",
2563 static const char *const d_name
[] =
2565 "d0", "d1", "d2", "d3",
2566 "d4", "d5", "d6", "d7",
2567 "d8", "d9", "d10", "d11",
2568 "d12", "d13", "d14", "d15",
2569 "d16", "d17", "d18", "d19",
2570 "d20", "d21", "d22", "d23",
2571 "d24", "d25", "d26", "d27",
2572 "d28", "d29", "d30", "d31",
2575 static const char *const s_name
[] =
2577 "s0", "s1", "s2", "s3",
2578 "s4", "s5", "s6", "s7",
2579 "s8", "s9", "s10", "s11",
2580 "s12", "s13", "s14", "s15",
2581 "s16", "s17", "s18", "s19",
2582 "s20", "s21", "s22", "s23",
2583 "s24", "s25", "s26", "s27",
2584 "s28", "s29", "s30", "s31",
2587 static const char *const h_name
[] =
2589 "h0", "h1", "h2", "h3",
2590 "h4", "h5", "h6", "h7",
2591 "h8", "h9", "h10", "h11",
2592 "h12", "h13", "h14", "h15",
2593 "h16", "h17", "h18", "h19",
2594 "h20", "h21", "h22", "h23",
2595 "h24", "h25", "h26", "h27",
2596 "h28", "h29", "h30", "h31",
2599 static const char *const b_name
[] =
2601 "b0", "b1", "b2", "b3",
2602 "b4", "b5", "b6", "b7",
2603 "b8", "b9", "b10", "b11",
2604 "b12", "b13", "b14", "b15",
2605 "b16", "b17", "b18", "b19",
2606 "b20", "b21", "b22", "b23",
2607 "b24", "b25", "b26", "b27",
2608 "b28", "b29", "b30", "b31",
2611 int p_regnum
= regnum
- gdbarch_num_regs (gdbarch
);
2613 if (p_regnum
>= AARCH64_Q0_REGNUM
&& p_regnum
< AARCH64_Q0_REGNUM
+ 32)
2614 return q_name
[p_regnum
- AARCH64_Q0_REGNUM
];
2616 if (p_regnum
>= AARCH64_D0_REGNUM
&& p_regnum
< AARCH64_D0_REGNUM
+ 32)
2617 return d_name
[p_regnum
- AARCH64_D0_REGNUM
];
2619 if (p_regnum
>= AARCH64_S0_REGNUM
&& p_regnum
< AARCH64_S0_REGNUM
+ 32)
2620 return s_name
[p_regnum
- AARCH64_S0_REGNUM
];
2622 if (p_regnum
>= AARCH64_H0_REGNUM
&& p_regnum
< AARCH64_H0_REGNUM
+ 32)
2623 return h_name
[p_regnum
- AARCH64_H0_REGNUM
];
2625 if (p_regnum
>= AARCH64_B0_REGNUM
&& p_regnum
< AARCH64_B0_REGNUM
+ 32)
2626 return b_name
[p_regnum
- AARCH64_B0_REGNUM
];
2628 if (tdep
->has_sve ())
2630 static const char *const sve_v_name
[] =
2632 "v0", "v1", "v2", "v3",
2633 "v4", "v5", "v6", "v7",
2634 "v8", "v9", "v10", "v11",
2635 "v12", "v13", "v14", "v15",
2636 "v16", "v17", "v18", "v19",
2637 "v20", "v21", "v22", "v23",
2638 "v24", "v25", "v26", "v27",
2639 "v28", "v29", "v30", "v31",
2642 if (p_regnum
>= AARCH64_SVE_V0_REGNUM
2643 && p_regnum
< AARCH64_SVE_V0_REGNUM
+ AARCH64_V_REGS_NUM
)
2644 return sve_v_name
[p_regnum
- AARCH64_SVE_V0_REGNUM
];
2647 /* RA_STATE is used for unwinding only. Do not assign it a name - this
2648 prevents it from being read by methods such as
2649 mi_cmd_trace_frame_collected. */
2650 if (tdep
->has_pauth () && regnum
== tdep
->pauth_ra_state_regnum
)
2653 internal_error (__FILE__
, __LINE__
,
2654 _("aarch64_pseudo_register_name: bad register number %d"),
2658 /* Implement the "pseudo_register_type" tdesc_arch_data method. */
2660 static struct type
*
2661 aarch64_pseudo_register_type (struct gdbarch
*gdbarch
, int regnum
)
2663 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2665 int p_regnum
= regnum
- gdbarch_num_regs (gdbarch
);
2667 if (p_regnum
>= AARCH64_Q0_REGNUM
&& p_regnum
< AARCH64_Q0_REGNUM
+ 32)
2668 return aarch64_vnq_type (gdbarch
);
2670 if (p_regnum
>= AARCH64_D0_REGNUM
&& p_regnum
< AARCH64_D0_REGNUM
+ 32)
2671 return aarch64_vnd_type (gdbarch
);
2673 if (p_regnum
>= AARCH64_S0_REGNUM
&& p_regnum
< AARCH64_S0_REGNUM
+ 32)
2674 return aarch64_vns_type (gdbarch
);
2676 if (p_regnum
>= AARCH64_H0_REGNUM
&& p_regnum
< AARCH64_H0_REGNUM
+ 32)
2677 return aarch64_vnh_type (gdbarch
);
2679 if (p_regnum
>= AARCH64_B0_REGNUM
&& p_regnum
< AARCH64_B0_REGNUM
+ 32)
2680 return aarch64_vnb_type (gdbarch
);
2682 if (tdep
->has_sve () && p_regnum
>= AARCH64_SVE_V0_REGNUM
2683 && p_regnum
< AARCH64_SVE_V0_REGNUM
+ AARCH64_V_REGS_NUM
)
2684 return aarch64_vnv_type (gdbarch
);
2686 if (tdep
->has_pauth () && regnum
== tdep
->pauth_ra_state_regnum
)
2687 return builtin_type (gdbarch
)->builtin_uint64
;
2689 internal_error (__FILE__
, __LINE__
,
2690 _("aarch64_pseudo_register_type: bad register number %d"),
2694 /* Implement the "pseudo_register_reggroup_p" tdesc_arch_data method. */
2697 aarch64_pseudo_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
2698 const struct reggroup
*group
)
2700 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2702 int p_regnum
= regnum
- gdbarch_num_regs (gdbarch
);
2704 if (p_regnum
>= AARCH64_Q0_REGNUM
&& p_regnum
< AARCH64_Q0_REGNUM
+ 32)
2705 return group
== all_reggroup
|| group
== vector_reggroup
;
2706 else if (p_regnum
>= AARCH64_D0_REGNUM
&& p_regnum
< AARCH64_D0_REGNUM
+ 32)
2707 return (group
== all_reggroup
|| group
== vector_reggroup
2708 || group
== float_reggroup
);
2709 else if (p_regnum
>= AARCH64_S0_REGNUM
&& p_regnum
< AARCH64_S0_REGNUM
+ 32)
2710 return (group
== all_reggroup
|| group
== vector_reggroup
2711 || group
== float_reggroup
);
2712 else if (p_regnum
>= AARCH64_H0_REGNUM
&& p_regnum
< AARCH64_H0_REGNUM
+ 32)
2713 return group
== all_reggroup
|| group
== vector_reggroup
;
2714 else if (p_regnum
>= AARCH64_B0_REGNUM
&& p_regnum
< AARCH64_B0_REGNUM
+ 32)
2715 return group
== all_reggroup
|| group
== vector_reggroup
;
2716 else if (tdep
->has_sve () && p_regnum
>= AARCH64_SVE_V0_REGNUM
2717 && p_regnum
< AARCH64_SVE_V0_REGNUM
+ AARCH64_V_REGS_NUM
)
2718 return group
== all_reggroup
|| group
== vector_reggroup
;
2719 /* RA_STATE is used for unwinding only. Do not assign it to any groups. */
2720 if (tdep
->has_pauth () && regnum
== tdep
->pauth_ra_state_regnum
)
2723 return group
== all_reggroup
;
2726 /* Helper for aarch64_pseudo_read_value. */
2728 static struct value
*
2729 aarch64_pseudo_read_value_1 (struct gdbarch
*gdbarch
,
2730 readable_regcache
*regcache
, int regnum_offset
,
2731 int regsize
, struct value
*result_value
)
2733 unsigned v_regnum
= AARCH64_V0_REGNUM
+ regnum_offset
;
2735 /* Enough space for a full vector register. */
2736 gdb_byte reg_buf
[register_size (gdbarch
, AARCH64_V0_REGNUM
)];
2737 gdb_static_assert (AARCH64_V0_REGNUM
== AARCH64_SVE_Z0_REGNUM
);
2739 if (regcache
->raw_read (v_regnum
, reg_buf
) != REG_VALID
)
2740 mark_value_bytes_unavailable (result_value
, 0,
2741 TYPE_LENGTH (value_type (result_value
)));
2743 memcpy (value_contents_raw (result_value
).data (), reg_buf
, regsize
);
2745 return result_value
;
2748 /* Implement the "pseudo_register_read_value" gdbarch method. */
2750 static struct value
*
2751 aarch64_pseudo_read_value (struct gdbarch
*gdbarch
, readable_regcache
*regcache
,
2754 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2755 struct value
*result_value
= allocate_value (register_type (gdbarch
, regnum
));
2757 VALUE_LVAL (result_value
) = lval_register
;
2758 VALUE_REGNUM (result_value
) = regnum
;
2760 regnum
-= gdbarch_num_regs (gdbarch
);
2762 if (regnum
>= AARCH64_Q0_REGNUM
&& regnum
< AARCH64_Q0_REGNUM
+ 32)
2763 return aarch64_pseudo_read_value_1 (gdbarch
, regcache
,
2764 regnum
- AARCH64_Q0_REGNUM
,
2765 Q_REGISTER_SIZE
, result_value
);
2767 if (regnum
>= AARCH64_D0_REGNUM
&& regnum
< AARCH64_D0_REGNUM
+ 32)
2768 return aarch64_pseudo_read_value_1 (gdbarch
, regcache
,
2769 regnum
- AARCH64_D0_REGNUM
,
2770 D_REGISTER_SIZE
, result_value
);
2772 if (regnum
>= AARCH64_S0_REGNUM
&& regnum
< AARCH64_S0_REGNUM
+ 32)
2773 return aarch64_pseudo_read_value_1 (gdbarch
, regcache
,
2774 regnum
- AARCH64_S0_REGNUM
,
2775 S_REGISTER_SIZE
, result_value
);
2777 if (regnum
>= AARCH64_H0_REGNUM
&& regnum
< AARCH64_H0_REGNUM
+ 32)
2778 return aarch64_pseudo_read_value_1 (gdbarch
, regcache
,
2779 regnum
- AARCH64_H0_REGNUM
,
2780 H_REGISTER_SIZE
, result_value
);
2782 if (regnum
>= AARCH64_B0_REGNUM
&& regnum
< AARCH64_B0_REGNUM
+ 32)
2783 return aarch64_pseudo_read_value_1 (gdbarch
, regcache
,
2784 regnum
- AARCH64_B0_REGNUM
,
2785 B_REGISTER_SIZE
, result_value
);
2787 if (tdep
->has_sve () && regnum
>= AARCH64_SVE_V0_REGNUM
2788 && regnum
< AARCH64_SVE_V0_REGNUM
+ 32)
2789 return aarch64_pseudo_read_value_1 (gdbarch
, regcache
,
2790 regnum
- AARCH64_SVE_V0_REGNUM
,
2791 V_REGISTER_SIZE
, result_value
);
2793 gdb_assert_not_reached ("regnum out of bound");
2796 /* Helper for aarch64_pseudo_write. */
2799 aarch64_pseudo_write_1 (struct gdbarch
*gdbarch
, struct regcache
*regcache
,
2800 int regnum_offset
, int regsize
, const gdb_byte
*buf
)
2802 unsigned v_regnum
= AARCH64_V0_REGNUM
+ regnum_offset
;
2804 /* Enough space for a full vector register. */
2805 gdb_byte reg_buf
[register_size (gdbarch
, AARCH64_V0_REGNUM
)];
2806 gdb_static_assert (AARCH64_V0_REGNUM
== AARCH64_SVE_Z0_REGNUM
);
2808 /* Ensure the register buffer is zero, we want gdb writes of the
2809 various 'scalar' pseudo registers to behavior like architectural
2810 writes, register width bytes are written the remainder are set to
2812 memset (reg_buf
, 0, register_size (gdbarch
, AARCH64_V0_REGNUM
));
2814 memcpy (reg_buf
, buf
, regsize
);
2815 regcache
->raw_write (v_regnum
, reg_buf
);
2818 /* Implement the "pseudo_register_write" gdbarch method. */
2821 aarch64_pseudo_write (struct gdbarch
*gdbarch
, struct regcache
*regcache
,
2822 int regnum
, const gdb_byte
*buf
)
2824 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
2825 regnum
-= gdbarch_num_regs (gdbarch
);
2827 if (regnum
>= AARCH64_Q0_REGNUM
&& regnum
< AARCH64_Q0_REGNUM
+ 32)
2828 return aarch64_pseudo_write_1 (gdbarch
, regcache
,
2829 regnum
- AARCH64_Q0_REGNUM
, Q_REGISTER_SIZE
,
2832 if (regnum
>= AARCH64_D0_REGNUM
&& regnum
< AARCH64_D0_REGNUM
+ 32)
2833 return aarch64_pseudo_write_1 (gdbarch
, regcache
,
2834 regnum
- AARCH64_D0_REGNUM
, D_REGISTER_SIZE
,
2837 if (regnum
>= AARCH64_S0_REGNUM
&& regnum
< AARCH64_S0_REGNUM
+ 32)
2838 return aarch64_pseudo_write_1 (gdbarch
, regcache
,
2839 regnum
- AARCH64_S0_REGNUM
, S_REGISTER_SIZE
,
2842 if (regnum
>= AARCH64_H0_REGNUM
&& regnum
< AARCH64_H0_REGNUM
+ 32)
2843 return aarch64_pseudo_write_1 (gdbarch
, regcache
,
2844 regnum
- AARCH64_H0_REGNUM
, H_REGISTER_SIZE
,
2847 if (regnum
>= AARCH64_B0_REGNUM
&& regnum
< AARCH64_B0_REGNUM
+ 32)
2848 return aarch64_pseudo_write_1 (gdbarch
, regcache
,
2849 regnum
- AARCH64_B0_REGNUM
, B_REGISTER_SIZE
,
2852 if (tdep
->has_sve () && regnum
>= AARCH64_SVE_V0_REGNUM
2853 && regnum
< AARCH64_SVE_V0_REGNUM
+ 32)
2854 return aarch64_pseudo_write_1 (gdbarch
, regcache
,
2855 regnum
- AARCH64_SVE_V0_REGNUM
,
2856 V_REGISTER_SIZE
, buf
);
2858 gdb_assert_not_reached ("regnum out of bound");
2861 /* Callback function for user_reg_add. */
2863 static struct value
*
2864 value_of_aarch64_user_reg (struct frame_info
*frame
, const void *baton
)
2866 const int *reg_p
= (const int *) baton
;
2868 return value_of_register (*reg_p
, frame
);
2872 /* Implement the "software_single_step" gdbarch method, needed to
2873 single step through atomic sequences on AArch64. */
2875 static std::vector
<CORE_ADDR
>
2876 aarch64_software_single_step (struct regcache
*regcache
)
2878 struct gdbarch
*gdbarch
= regcache
->arch ();
2879 enum bfd_endian byte_order_for_code
= gdbarch_byte_order_for_code (gdbarch
);
2880 const int insn_size
= 4;
2881 const int atomic_sequence_length
= 16; /* Instruction sequence length. */
2882 CORE_ADDR pc
= regcache_read_pc (regcache
);
2883 CORE_ADDR breaks
[2] = { CORE_ADDR_MAX
, CORE_ADDR_MAX
};
2885 CORE_ADDR closing_insn
= 0;
2886 uint32_t insn
= read_memory_unsigned_integer (loc
, insn_size
,
2887 byte_order_for_code
);
2890 int bc_insn_count
= 0; /* Conditional branch instruction count. */
2891 int last_breakpoint
= 0; /* Defaults to 0 (no breakpoints placed). */
2894 if (aarch64_decode_insn (insn
, &inst
, 1, NULL
) != 0)
2897 /* Look for a Load Exclusive instruction which begins the sequence. */
2898 if (inst
.opcode
->iclass
!= ldstexcl
|| bit (insn
, 22) == 0)
2901 for (insn_count
= 0; insn_count
< atomic_sequence_length
; ++insn_count
)
2904 insn
= read_memory_unsigned_integer (loc
, insn_size
,
2905 byte_order_for_code
);
2907 if (aarch64_decode_insn (insn
, &inst
, 1, NULL
) != 0)
2909 /* Check if the instruction is a conditional branch. */
2910 if (inst
.opcode
->iclass
== condbranch
)
2912 gdb_assert (inst
.operands
[0].type
== AARCH64_OPND_ADDR_PCREL19
);
2914 if (bc_insn_count
>= 1)
2917 /* It is, so we'll try to set a breakpoint at the destination. */
2918 breaks
[1] = loc
+ inst
.operands
[0].imm
.value
;
2924 /* Look for the Store Exclusive which closes the atomic sequence. */
2925 if (inst
.opcode
->iclass
== ldstexcl
&& bit (insn
, 22) == 0)
2932 /* We didn't find a closing Store Exclusive instruction, fall back. */
2936 /* Insert breakpoint after the end of the atomic sequence. */
2937 breaks
[0] = loc
+ insn_size
;
2939 /* Check for duplicated breakpoints, and also check that the second
2940 breakpoint is not within the atomic sequence. */
2942 && (breaks
[1] == breaks
[0]
2943 || (breaks
[1] >= pc
&& breaks
[1] <= closing_insn
)))
2944 last_breakpoint
= 0;
2946 std::vector
<CORE_ADDR
> next_pcs
;
2948 /* Insert the breakpoint at the end of the sequence, and one at the
2949 destination of the conditional branch, if it exists. */
2950 for (index
= 0; index
<= last_breakpoint
; index
++)
2951 next_pcs
.push_back (breaks
[index
]);
2956 struct aarch64_displaced_step_copy_insn_closure
2957 : public displaced_step_copy_insn_closure
2959 /* It is true when condition instruction, such as B.CON, TBZ, etc,
2960 is being displaced stepping. */
2963 /* PC adjustment offset after displaced stepping. If 0, then we don't
2964 write the PC back, assuming the PC is already the right address. */
2965 int32_t pc_adjust
= 0;
2968 /* Data when visiting instructions for displaced stepping. */
2970 struct aarch64_displaced_step_data
2972 struct aarch64_insn_data base
;
2974 /* The address where the instruction will be executed at. */
2976 /* Buffer of instructions to be copied to NEW_ADDR to execute. */
2977 uint32_t insn_buf
[AARCH64_DISPLACED_MODIFIED_INSNS
];
2978 /* Number of instructions in INSN_BUF. */
2979 unsigned insn_count
;
2980 /* Registers when doing displaced stepping. */
2981 struct regcache
*regs
;
2983 aarch64_displaced_step_copy_insn_closure
*dsc
;
2986 /* Implementation of aarch64_insn_visitor method "b". */
2989 aarch64_displaced_step_b (const int is_bl
, const int32_t offset
,
2990 struct aarch64_insn_data
*data
)
2992 struct aarch64_displaced_step_data
*dsd
2993 = (struct aarch64_displaced_step_data
*) data
;
2994 int64_t new_offset
= data
->insn_addr
- dsd
->new_addr
+ offset
;
2996 if (can_encode_int32 (new_offset
, 28))
2998 /* Emit B rather than BL, because executing BL on a new address
2999 will get the wrong address into LR. In order to avoid this,
3000 we emit B, and update LR if the instruction is BL. */
3001 emit_b (dsd
->insn_buf
, 0, new_offset
);
3007 emit_nop (dsd
->insn_buf
);
3009 dsd
->dsc
->pc_adjust
= offset
;
3015 regcache_cooked_write_unsigned (dsd
->regs
, AARCH64_LR_REGNUM
,
3016 data
->insn_addr
+ 4);
3020 /* Implementation of aarch64_insn_visitor method "b_cond". */
3023 aarch64_displaced_step_b_cond (const unsigned cond
, const int32_t offset
,
3024 struct aarch64_insn_data
*data
)
3026 struct aarch64_displaced_step_data
*dsd
3027 = (struct aarch64_displaced_step_data
*) data
;
3029 /* GDB has to fix up PC after displaced step this instruction
3030 differently according to the condition is true or false. Instead
3031 of checking COND against conditional flags, we can use
3032 the following instructions, and GDB can tell how to fix up PC
3033 according to the PC value.
3035 B.COND TAKEN ; If cond is true, then jump to TAKEN.
3041 emit_bcond (dsd
->insn_buf
, cond
, 8);
3042 dsd
->dsc
->cond
= true;
3043 dsd
->dsc
->pc_adjust
= offset
;
3044 dsd
->insn_count
= 1;
3047 /* Dynamically allocate a new register. If we know the register
3048 statically, we should make it a global as above instead of using this
3051 static struct aarch64_register
3052 aarch64_register (unsigned num
, int is64
)
3054 return (struct aarch64_register
) { num
, is64
};
3057 /* Implementation of aarch64_insn_visitor method "cb". */
3060 aarch64_displaced_step_cb (const int32_t offset
, const int is_cbnz
,
3061 const unsigned rn
, int is64
,
3062 struct aarch64_insn_data
*data
)
3064 struct aarch64_displaced_step_data
*dsd
3065 = (struct aarch64_displaced_step_data
*) data
;
3067 /* The offset is out of range for a compare and branch
3068 instruction. We can use the following instructions instead:
3070 CBZ xn, TAKEN ; xn == 0, then jump to TAKEN.
3075 emit_cb (dsd
->insn_buf
, is_cbnz
, aarch64_register (rn
, is64
), 8);
3076 dsd
->insn_count
= 1;
3077 dsd
->dsc
->cond
= true;
3078 dsd
->dsc
->pc_adjust
= offset
;
3081 /* Implementation of aarch64_insn_visitor method "tb". */
3084 aarch64_displaced_step_tb (const int32_t offset
, int is_tbnz
,
3085 const unsigned rt
, unsigned bit
,
3086 struct aarch64_insn_data
*data
)
3088 struct aarch64_displaced_step_data
*dsd
3089 = (struct aarch64_displaced_step_data
*) data
;
3091 /* The offset is out of range for a test bit and branch
3092 instruction We can use the following instructions instead:
3094 TBZ xn, #bit, TAKEN ; xn[bit] == 0, then jump to TAKEN.
3100 emit_tb (dsd
->insn_buf
, is_tbnz
, bit
, aarch64_register (rt
, 1), 8);
3101 dsd
->insn_count
= 1;
3102 dsd
->dsc
->cond
= true;
3103 dsd
->dsc
->pc_adjust
= offset
;
3106 /* Implementation of aarch64_insn_visitor method "adr". */
3109 aarch64_displaced_step_adr (const int32_t offset
, const unsigned rd
,
3110 const int is_adrp
, struct aarch64_insn_data
*data
)
3112 struct aarch64_displaced_step_data
*dsd
3113 = (struct aarch64_displaced_step_data
*) data
;
3114 /* We know exactly the address the ADR{P,} instruction will compute.
3115 We can just write it to the destination register. */
3116 CORE_ADDR address
= data
->insn_addr
+ offset
;
3120 /* Clear the lower 12 bits of the offset to get the 4K page. */
3121 regcache_cooked_write_unsigned (dsd
->regs
, AARCH64_X0_REGNUM
+ rd
,
3125 regcache_cooked_write_unsigned (dsd
->regs
, AARCH64_X0_REGNUM
+ rd
,
3128 dsd
->dsc
->pc_adjust
= 4;
3129 emit_nop (dsd
->insn_buf
);
3130 dsd
->insn_count
= 1;
3133 /* Implementation of aarch64_insn_visitor method "ldr_literal". */
3136 aarch64_displaced_step_ldr_literal (const int32_t offset
, const int is_sw
,
3137 const unsigned rt
, const int is64
,
3138 struct aarch64_insn_data
*data
)
3140 struct aarch64_displaced_step_data
*dsd
3141 = (struct aarch64_displaced_step_data
*) data
;
3142 CORE_ADDR address
= data
->insn_addr
+ offset
;
3143 struct aarch64_memory_operand zero
= { MEMORY_OPERAND_OFFSET
, 0 };
3145 regcache_cooked_write_unsigned (dsd
->regs
, AARCH64_X0_REGNUM
+ rt
,
3149 dsd
->insn_count
= emit_ldrsw (dsd
->insn_buf
, aarch64_register (rt
, 1),
3150 aarch64_register (rt
, 1), zero
);
3152 dsd
->insn_count
= emit_ldr (dsd
->insn_buf
, aarch64_register (rt
, is64
),
3153 aarch64_register (rt
, 1), zero
);
3155 dsd
->dsc
->pc_adjust
= 4;
3158 /* Implementation of aarch64_insn_visitor method "others". */
3161 aarch64_displaced_step_others (const uint32_t insn
,
3162 struct aarch64_insn_data
*data
)
3164 struct aarch64_displaced_step_data
*dsd
3165 = (struct aarch64_displaced_step_data
*) data
;
3167 uint32_t masked_insn
= (insn
& CLEAR_Rn_MASK
);
3168 if (masked_insn
== BLR
)
3170 /* Emit a BR to the same register and then update LR to the original
3171 address (similar to aarch64_displaced_step_b). */
3172 aarch64_emit_insn (dsd
->insn_buf
, insn
& 0xffdfffff);
3173 regcache_cooked_write_unsigned (dsd
->regs
, AARCH64_LR_REGNUM
,
3174 data
->insn_addr
+ 4);
3177 aarch64_emit_insn (dsd
->insn_buf
, insn
);
3178 dsd
->insn_count
= 1;
3180 if (masked_insn
== RET
|| masked_insn
== BR
|| masked_insn
== BLR
)
3181 dsd
->dsc
->pc_adjust
= 0;
3183 dsd
->dsc
->pc_adjust
= 4;
3186 static const struct aarch64_insn_visitor visitor
=
3188 aarch64_displaced_step_b
,
3189 aarch64_displaced_step_b_cond
,
3190 aarch64_displaced_step_cb
,
3191 aarch64_displaced_step_tb
,
3192 aarch64_displaced_step_adr
,
3193 aarch64_displaced_step_ldr_literal
,
3194 aarch64_displaced_step_others
,
3197 /* Implement the "displaced_step_copy_insn" gdbarch method. */
3199 displaced_step_copy_insn_closure_up
3200 aarch64_displaced_step_copy_insn (struct gdbarch
*gdbarch
,
3201 CORE_ADDR from
, CORE_ADDR to
,
3202 struct regcache
*regs
)
3204 enum bfd_endian byte_order_for_code
= gdbarch_byte_order_for_code (gdbarch
);
3205 uint32_t insn
= read_memory_unsigned_integer (from
, 4, byte_order_for_code
);
3206 struct aarch64_displaced_step_data dsd
;
3209 if (aarch64_decode_insn (insn
, &inst
, 1, NULL
) != 0)
3212 /* Look for a Load Exclusive instruction which begins the sequence. */
3213 if (inst
.opcode
->iclass
== ldstexcl
&& bit (insn
, 22))
3215 /* We can't displaced step atomic sequences. */
3219 std::unique_ptr
<aarch64_displaced_step_copy_insn_closure
> dsc
3220 (new aarch64_displaced_step_copy_insn_closure
);
3221 dsd
.base
.insn_addr
= from
;
3224 dsd
.dsc
= dsc
.get ();
3226 aarch64_relocate_instruction (insn
, &visitor
,
3227 (struct aarch64_insn_data
*) &dsd
);
3228 gdb_assert (dsd
.insn_count
<= AARCH64_DISPLACED_MODIFIED_INSNS
);
3230 if (dsd
.insn_count
!= 0)
3234 /* Instruction can be relocated to scratch pad. Copy
3235 relocated instruction(s) there. */
3236 for (i
= 0; i
< dsd
.insn_count
; i
++)
3238 displaced_debug_printf ("writing insn %.8x at %s",
3240 paddress (gdbarch
, to
+ i
* 4));
3242 write_memory_unsigned_integer (to
+ i
* 4, 4, byte_order_for_code
,
3243 (ULONGEST
) dsd
.insn_buf
[i
]);
3251 /* This is a work around for a problem with g++ 4.8. */
3252 return displaced_step_copy_insn_closure_up (dsc
.release ());
3255 /* Implement the "displaced_step_fixup" gdbarch method. */
3258 aarch64_displaced_step_fixup (struct gdbarch
*gdbarch
,
3259 struct displaced_step_copy_insn_closure
*dsc_
,
3260 CORE_ADDR from
, CORE_ADDR to
,
3261 struct regcache
*regs
)
3263 aarch64_displaced_step_copy_insn_closure
*dsc
3264 = (aarch64_displaced_step_copy_insn_closure
*) dsc_
;
3268 regcache_cooked_read_unsigned (regs
, AARCH64_PC_REGNUM
, &pc
);
3270 displaced_debug_printf ("PC after stepping: %s (was %s).",
3271 paddress (gdbarch
, pc
), paddress (gdbarch
, to
));
3275 displaced_debug_printf ("[Conditional] pc_adjust before: %d",
3280 /* Condition is true. */
3282 else if (pc
- to
== 4)
3284 /* Condition is false. */
3288 gdb_assert_not_reached ("Unexpected PC value after displaced stepping");
3290 displaced_debug_printf ("[Conditional] pc_adjust after: %d",
3294 displaced_debug_printf ("%s PC by %d",
3295 dsc
->pc_adjust
? "adjusting" : "not adjusting",
3298 if (dsc
->pc_adjust
!= 0)
3300 /* Make sure the previous instruction was executed (that is, the PC
3301 has changed). If the PC didn't change, then discard the adjustment
3302 offset. Otherwise we may skip an instruction before its execution
3306 displaced_debug_printf ("PC did not move. Discarding PC adjustment.");
3310 displaced_debug_printf ("fixup: set PC to %s:%d",
3311 paddress (gdbarch
, from
), dsc
->pc_adjust
);
3313 regcache_cooked_write_unsigned (regs
, AARCH64_PC_REGNUM
,
3314 from
+ dsc
->pc_adjust
);
3318 /* Implement the "displaced_step_hw_singlestep" gdbarch method. */
3321 aarch64_displaced_step_hw_singlestep (struct gdbarch
*gdbarch
)
3326 /* Get the correct target description for the given VQ value.
3327 If VQ is zero then it is assumed SVE is not supported.
3328 (It is not possible to set VQ to zero on an SVE system).
3330 MTE_P indicates the presence of the Memory Tagging Extension feature. */
3333 aarch64_read_description (uint64_t vq
, bool pauth_p
, bool mte_p
)
3335 if (vq
> AARCH64_MAX_SVE_VQ
)
3336 error (_("VQ is %" PRIu64
", maximum supported value is %d"), vq
,
3337 AARCH64_MAX_SVE_VQ
);
3339 struct target_desc
*tdesc
= tdesc_aarch64_list
[vq
][pauth_p
][mte_p
];
3343 tdesc
= aarch64_create_target_description (vq
, pauth_p
, mte_p
);
3344 tdesc_aarch64_list
[vq
][pauth_p
][mte_p
] = tdesc
;
3350 /* Return the VQ used when creating the target description TDESC. */
3353 aarch64_get_tdesc_vq (const struct target_desc
*tdesc
)
3355 const struct tdesc_feature
*feature_sve
;
3357 if (!tdesc_has_registers (tdesc
))
3360 feature_sve
= tdesc_find_feature (tdesc
, "org.gnu.gdb.aarch64.sve");
3362 if (feature_sve
== nullptr)
3365 uint64_t vl
= tdesc_register_bitsize (feature_sve
,
3366 aarch64_sve_register_names
[0]) / 8;
3367 return sve_vq_from_vl (vl
);
3370 /* Implement the "cannot_store_register" gdbarch method. */
3373 aarch64_cannot_store_register (struct gdbarch
*gdbarch
, int regnum
)
3375 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
3377 if (!tdep
->has_pauth ())
3380 /* Pointer authentication registers are read-only. */
3381 return (regnum
== AARCH64_PAUTH_DMASK_REGNUM (tdep
->pauth_reg_base
)
3382 || regnum
== AARCH64_PAUTH_CMASK_REGNUM (tdep
->pauth_reg_base
));
3385 /* Implement the stack_frame_destroyed_p gdbarch method. */
3388 aarch64_stack_frame_destroyed_p (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
3390 CORE_ADDR func_start
, func_end
;
3391 if (!find_pc_partial_function (pc
, NULL
, &func_start
, &func_end
))
3394 enum bfd_endian byte_order_for_code
= gdbarch_byte_order_for_code (gdbarch
);
3395 uint32_t insn
= read_memory_unsigned_integer (pc
, 4, byte_order_for_code
);
3398 if (aarch64_decode_insn (insn
, &inst
, 1, nullptr) != 0)
3401 return streq (inst
.opcode
->name
, "ret");
3404 /* Initialize the current architecture based on INFO. If possible,
3405 re-use an architecture from ARCHES, which is a list of
3406 architectures already created during this debugging session.
3408 Called e.g. at program startup, when reading a core file, and when
3409 reading a binary file. */
3411 static struct gdbarch
*
3412 aarch64_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
3414 const struct tdesc_feature
*feature_core
, *feature_fpu
, *feature_sve
;
3415 const struct tdesc_feature
*feature_pauth
;
3416 bool valid_p
= true;
3417 int i
, num_regs
= 0, num_pseudo_regs
= 0;
3418 int first_pauth_regnum
= -1, pauth_ra_state_offset
= -1;
3419 int first_mte_regnum
= -1;
3421 /* Use the vector length passed via the target info. Here -1 is used for no
3422 SVE, and 0 is unset. If unset then use the vector length from the existing
3425 if (info
.id
== (int *) -1)
3427 else if (info
.id
!= 0)
3428 vq
= (uint64_t) info
.id
;
3430 vq
= aarch64_get_tdesc_vq (info
.target_desc
);
3432 if (vq
> AARCH64_MAX_SVE_VQ
)
3433 internal_error (__FILE__
, __LINE__
, _("VQ out of bounds: %s (max %d)"),
3434 pulongest (vq
), AARCH64_MAX_SVE_VQ
);
3436 /* If there is already a candidate, use it. */
3437 for (gdbarch_list
*best_arch
= gdbarch_list_lookup_by_info (arches
, &info
);
3438 best_arch
!= nullptr;
3439 best_arch
= gdbarch_list_lookup_by_info (best_arch
->next
, &info
))
3441 aarch64_gdbarch_tdep
*tdep
3442 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (best_arch
->gdbarch
);
3443 if (tdep
&& tdep
->vq
== vq
)
3444 return best_arch
->gdbarch
;
3447 /* Ensure we always have a target descriptor, and that it is for the given VQ
3449 const struct target_desc
*tdesc
= info
.target_desc
;
3450 if (!tdesc_has_registers (tdesc
) || vq
!= aarch64_get_tdesc_vq (tdesc
))
3451 tdesc
= aarch64_read_description (vq
, false, false);
3454 feature_core
= tdesc_find_feature (tdesc
,"org.gnu.gdb.aarch64.core");
3455 feature_fpu
= tdesc_find_feature (tdesc
, "org.gnu.gdb.aarch64.fpu");
3456 feature_sve
= tdesc_find_feature (tdesc
, "org.gnu.gdb.aarch64.sve");
3457 feature_pauth
= tdesc_find_feature (tdesc
, "org.gnu.gdb.aarch64.pauth");
3458 const struct tdesc_feature
*feature_mte
3459 = tdesc_find_feature (tdesc
, "org.gnu.gdb.aarch64.mte");
3461 if (feature_core
== nullptr)
3464 tdesc_arch_data_up tdesc_data
= tdesc_data_alloc ();
3466 /* Validate the description provides the mandatory core R registers
3467 and allocate their numbers. */
3468 for (i
= 0; i
< ARRAY_SIZE (aarch64_r_register_names
); i
++)
3469 valid_p
&= tdesc_numbered_register (feature_core
, tdesc_data
.get (),
3470 AARCH64_X0_REGNUM
+ i
,
3471 aarch64_r_register_names
[i
]);
3473 num_regs
= AARCH64_X0_REGNUM
+ i
;
3475 /* Add the V registers. */
3476 if (feature_fpu
!= nullptr)
3478 if (feature_sve
!= nullptr)
3479 error (_("Program contains both fpu and SVE features."));
3481 /* Validate the description provides the mandatory V registers
3482 and allocate their numbers. */
3483 for (i
= 0; i
< ARRAY_SIZE (aarch64_v_register_names
); i
++)
3484 valid_p
&= tdesc_numbered_register (feature_fpu
, tdesc_data
.get (),
3485 AARCH64_V0_REGNUM
+ i
,
3486 aarch64_v_register_names
[i
]);
3488 num_regs
= AARCH64_V0_REGNUM
+ i
;
3491 /* Add the SVE registers. */
3492 if (feature_sve
!= nullptr)
3494 /* Validate the description provides the mandatory SVE registers
3495 and allocate their numbers. */
3496 for (i
= 0; i
< ARRAY_SIZE (aarch64_sve_register_names
); i
++)
3497 valid_p
&= tdesc_numbered_register (feature_sve
, tdesc_data
.get (),
3498 AARCH64_SVE_Z0_REGNUM
+ i
,
3499 aarch64_sve_register_names
[i
]);
3501 num_regs
= AARCH64_SVE_Z0_REGNUM
+ i
;
3502 num_pseudo_regs
+= 32; /* add the Vn register pseudos. */
3505 if (feature_fpu
!= nullptr || feature_sve
!= nullptr)
3507 num_pseudo_regs
+= 32; /* add the Qn scalar register pseudos */
3508 num_pseudo_regs
+= 32; /* add the Dn scalar register pseudos */
3509 num_pseudo_regs
+= 32; /* add the Sn scalar register pseudos */
3510 num_pseudo_regs
+= 32; /* add the Hn scalar register pseudos */
3511 num_pseudo_regs
+= 32; /* add the Bn scalar register pseudos */
3514 /* Add the pauth registers. */
3515 if (feature_pauth
!= NULL
)
3517 first_pauth_regnum
= num_regs
;
3518 pauth_ra_state_offset
= num_pseudo_regs
;
3519 /* Validate the descriptor provides the mandatory PAUTH registers and
3520 allocate their numbers. */
3521 for (i
= 0; i
< ARRAY_SIZE (aarch64_pauth_register_names
); i
++)
3522 valid_p
&= tdesc_numbered_register (feature_pauth
, tdesc_data
.get (),
3523 first_pauth_regnum
+ i
,
3524 aarch64_pauth_register_names
[i
]);
3527 num_pseudo_regs
+= 1; /* Count RA_STATE pseudo register. */
3530 /* Add the MTE registers. */
3531 if (feature_mte
!= NULL
)
3533 first_mte_regnum
= num_regs
;
3534 /* Validate the descriptor provides the mandatory MTE registers and
3535 allocate their numbers. */
3536 for (i
= 0; i
< ARRAY_SIZE (aarch64_mte_register_names
); i
++)
3537 valid_p
&= tdesc_numbered_register (feature_mte
, tdesc_data
.get (),
3538 first_mte_regnum
+ i
,
3539 aarch64_mte_register_names
[i
]);
3547 /* AArch64 code is always little-endian. */
3548 info
.byte_order_for_code
= BFD_ENDIAN_LITTLE
;
3550 aarch64_gdbarch_tdep
*tdep
= new aarch64_gdbarch_tdep
;
3551 struct gdbarch
*gdbarch
= gdbarch_alloc (&info
, tdep
);
3553 /* This should be low enough for everything. */
3554 tdep
->lowest_pc
= 0x20;
3555 tdep
->jb_pc
= -1; /* Longjump support not enabled by default. */
3556 tdep
->jb_elt_size
= 8;
3558 tdep
->pauth_reg_base
= first_pauth_regnum
;
3559 tdep
->pauth_ra_state_regnum
= (feature_pauth
== NULL
) ? -1
3560 : pauth_ra_state_offset
+ num_regs
;
3561 tdep
->mte_reg_base
= first_mte_regnum
;
3563 set_gdbarch_push_dummy_call (gdbarch
, aarch64_push_dummy_call
);
3564 set_gdbarch_frame_align (gdbarch
, aarch64_frame_align
);
3566 /* Advance PC across function entry code. */
3567 set_gdbarch_skip_prologue (gdbarch
, aarch64_skip_prologue
);
3569 /* The stack grows downward. */
3570 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
3572 /* Breakpoint manipulation. */
3573 set_gdbarch_breakpoint_kind_from_pc (gdbarch
,
3574 aarch64_breakpoint::kind_from_pc
);
3575 set_gdbarch_sw_breakpoint_from_kind (gdbarch
,
3576 aarch64_breakpoint::bp_from_kind
);
3577 set_gdbarch_have_nonsteppable_watchpoint (gdbarch
, 1);
3578 set_gdbarch_software_single_step (gdbarch
, aarch64_software_single_step
);
3580 /* Information about registers, etc. */
3581 set_gdbarch_sp_regnum (gdbarch
, AARCH64_SP_REGNUM
);
3582 set_gdbarch_pc_regnum (gdbarch
, AARCH64_PC_REGNUM
);
3583 set_gdbarch_num_regs (gdbarch
, num_regs
);
3585 set_gdbarch_num_pseudo_regs (gdbarch
, num_pseudo_regs
);
3586 set_gdbarch_pseudo_register_read_value (gdbarch
, aarch64_pseudo_read_value
);
3587 set_gdbarch_pseudo_register_write (gdbarch
, aarch64_pseudo_write
);
3588 set_tdesc_pseudo_register_name (gdbarch
, aarch64_pseudo_register_name
);
3589 set_tdesc_pseudo_register_type (gdbarch
, aarch64_pseudo_register_type
);
3590 set_tdesc_pseudo_register_reggroup_p (gdbarch
,
3591 aarch64_pseudo_register_reggroup_p
);
3592 set_gdbarch_cannot_store_register (gdbarch
, aarch64_cannot_store_register
);
3595 set_gdbarch_short_bit (gdbarch
, 16);
3596 set_gdbarch_int_bit (gdbarch
, 32);
3597 set_gdbarch_float_bit (gdbarch
, 32);
3598 set_gdbarch_double_bit (gdbarch
, 64);
3599 set_gdbarch_long_double_bit (gdbarch
, 128);
3600 set_gdbarch_long_bit (gdbarch
, 64);
3601 set_gdbarch_long_long_bit (gdbarch
, 64);
3602 set_gdbarch_ptr_bit (gdbarch
, 64);
3603 set_gdbarch_char_signed (gdbarch
, 0);
3604 set_gdbarch_wchar_signed (gdbarch
, 0);
3605 set_gdbarch_float_format (gdbarch
, floatformats_ieee_single
);
3606 set_gdbarch_double_format (gdbarch
, floatformats_ieee_double
);
3607 set_gdbarch_long_double_format (gdbarch
, floatformats_ieee_quad
);
3608 set_gdbarch_type_align (gdbarch
, aarch64_type_align
);
3610 /* Detect whether PC is at a point where the stack has been destroyed. */
3611 set_gdbarch_stack_frame_destroyed_p (gdbarch
, aarch64_stack_frame_destroyed_p
);
3613 /* Internal <-> external register number maps. */
3614 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, aarch64_dwarf_reg_to_regnum
);
3616 /* Returning results. */
3617 set_gdbarch_return_value (gdbarch
, aarch64_return_value
);
3620 set_gdbarch_print_insn (gdbarch
, aarch64_gdb_print_insn
);
3622 /* Virtual tables. */
3623 set_gdbarch_vbit_in_delta (gdbarch
, 1);
3625 /* Hook in the ABI-specific overrides, if they have been registered. */
3626 info
.target_desc
= tdesc
;
3627 info
.tdesc_data
= tdesc_data
.get ();
3628 gdbarch_init_osabi (info
, gdbarch
);
3630 dwarf2_frame_set_init_reg (gdbarch
, aarch64_dwarf2_frame_init_reg
);
3631 /* Register DWARF CFA vendor handler. */
3632 set_gdbarch_execute_dwarf_cfa_vendor_op (gdbarch
,
3633 aarch64_execute_dwarf_cfa_vendor_op
);
3635 /* Permanent/Program breakpoint handling. */
3636 set_gdbarch_program_breakpoint_here_p (gdbarch
,
3637 aarch64_program_breakpoint_here_p
);
3639 /* Add some default predicates. */
3640 frame_unwind_append_unwinder (gdbarch
, &aarch64_stub_unwind
);
3641 dwarf2_append_unwinders (gdbarch
);
3642 frame_unwind_append_unwinder (gdbarch
, &aarch64_prologue_unwind
);
3644 frame_base_set_default (gdbarch
, &aarch64_normal_base
);
3646 /* Now we have tuned the configuration, set a few final things,
3647 based on what the OS ABI has told us. */
3649 if (tdep
->jb_pc
>= 0)
3650 set_gdbarch_get_longjmp_target (gdbarch
, aarch64_get_longjmp_target
);
3652 set_gdbarch_gen_return_address (gdbarch
, aarch64_gen_return_address
);
3654 set_gdbarch_get_pc_address_flags (gdbarch
, aarch64_get_pc_address_flags
);
3656 tdesc_use_registers (gdbarch
, tdesc
, std::move (tdesc_data
));
3658 /* Add standard register aliases. */
3659 for (i
= 0; i
< ARRAY_SIZE (aarch64_register_aliases
); i
++)
3660 user_reg_add (gdbarch
, aarch64_register_aliases
[i
].name
,
3661 value_of_aarch64_user_reg
,
3662 &aarch64_register_aliases
[i
].regnum
);
3664 register_aarch64_ravenscar_ops (gdbarch
);
3670 aarch64_dump_tdep (struct gdbarch
*gdbarch
, struct ui_file
*file
)
3672 aarch64_gdbarch_tdep
*tdep
= (aarch64_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
3677 gdb_printf (file
, _("aarch64_dump_tdep: Lowest pc = 0x%s"),
3678 paddress (gdbarch
, tdep
->lowest_pc
));
3684 static void aarch64_process_record_test (void);
3688 void _initialize_aarch64_tdep ();
3690 _initialize_aarch64_tdep ()
3692 gdbarch_register (bfd_arch_aarch64
, aarch64_gdbarch_init
,
3695 /* Debug this file's internals. */
3696 add_setshow_boolean_cmd ("aarch64", class_maintenance
, &aarch64_debug
, _("\
3697 Set AArch64 debugging."), _("\
3698 Show AArch64 debugging."), _("\
3699 When on, AArch64 specific debugging is enabled."),
3702 &setdebuglist
, &showdebuglist
);
3705 selftests::register_test ("aarch64-analyze-prologue",
3706 selftests::aarch64_analyze_prologue_test
);
3707 selftests::register_test ("aarch64-process-record",
3708 selftests::aarch64_process_record_test
);
3712 /* AArch64 process record-replay related structures, defines etc. */
3714 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
3717 unsigned int reg_len = LENGTH; \
3720 REGS = XNEWVEC (uint32_t, reg_len); \
3721 memcpy(®S[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
3726 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
3729 unsigned int mem_len = LENGTH; \
3732 MEMS = XNEWVEC (struct aarch64_mem_r, mem_len); \
3733 memcpy(MEMS, &RECORD_BUF[0], \
3734 sizeof(struct aarch64_mem_r) * LENGTH); \
3739 /* AArch64 record/replay structures and enumerations. */
3741 struct aarch64_mem_r
3743 uint64_t len
; /* Record length. */
3744 uint64_t addr
; /* Memory address. */
3747 enum aarch64_record_result
3749 AARCH64_RECORD_SUCCESS
,
3750 AARCH64_RECORD_UNSUPPORTED
,
3751 AARCH64_RECORD_UNKNOWN
3754 typedef struct insn_decode_record_t
3756 struct gdbarch
*gdbarch
;
3757 struct regcache
*regcache
;
3758 CORE_ADDR this_addr
; /* Address of insn to be recorded. */
3759 uint32_t aarch64_insn
; /* Insn to be recorded. */
3760 uint32_t mem_rec_count
; /* Count of memory records. */
3761 uint32_t reg_rec_count
; /* Count of register records. */
3762 uint32_t *aarch64_regs
; /* Registers to be recorded. */
3763 struct aarch64_mem_r
*aarch64_mems
; /* Memory locations to be recorded. */
3764 } insn_decode_record
;
3766 /* Record handler for data processing - register instructions. */
3769 aarch64_record_data_proc_reg (insn_decode_record
*aarch64_insn_r
)
3771 uint8_t reg_rd
, insn_bits24_27
, insn_bits21_23
;
3772 uint32_t record_buf
[4];
3774 reg_rd
= bits (aarch64_insn_r
->aarch64_insn
, 0, 4);
3775 insn_bits24_27
= bits (aarch64_insn_r
->aarch64_insn
, 24, 27);
3776 insn_bits21_23
= bits (aarch64_insn_r
->aarch64_insn
, 21, 23);
3778 if (!bit (aarch64_insn_r
->aarch64_insn
, 28))
3782 /* Logical (shifted register). */
3783 if (insn_bits24_27
== 0x0a)
3784 setflags
= (bits (aarch64_insn_r
->aarch64_insn
, 29, 30) == 0x03);
3786 else if (insn_bits24_27
== 0x0b)
3787 setflags
= bit (aarch64_insn_r
->aarch64_insn
, 29);
3789 return AARCH64_RECORD_UNKNOWN
;
3791 record_buf
[0] = reg_rd
;
3792 aarch64_insn_r
->reg_rec_count
= 1;
3794 record_buf
[aarch64_insn_r
->reg_rec_count
++] = AARCH64_CPSR_REGNUM
;
3798 if (insn_bits24_27
== 0x0b)
3800 /* Data-processing (3 source). */
3801 record_buf
[0] = reg_rd
;
3802 aarch64_insn_r
->reg_rec_count
= 1;
3804 else if (insn_bits24_27
== 0x0a)
3806 if (insn_bits21_23
== 0x00)
3808 /* Add/subtract (with carry). */
3809 record_buf
[0] = reg_rd
;
3810 aarch64_insn_r
->reg_rec_count
= 1;
3811 if (bit (aarch64_insn_r
->aarch64_insn
, 29))
3813 record_buf
[1] = AARCH64_CPSR_REGNUM
;
3814 aarch64_insn_r
->reg_rec_count
= 2;
3817 else if (insn_bits21_23
== 0x02)
3819 /* Conditional compare (register) and conditional compare
3820 (immediate) instructions. */
3821 record_buf
[0] = AARCH64_CPSR_REGNUM
;
3822 aarch64_insn_r
->reg_rec_count
= 1;
3824 else if (insn_bits21_23
== 0x04 || insn_bits21_23
== 0x06)
3826 /* Conditional select. */
3827 /* Data-processing (2 source). */
3828 /* Data-processing (1 source). */
3829 record_buf
[0] = reg_rd
;
3830 aarch64_insn_r
->reg_rec_count
= 1;
3833 return AARCH64_RECORD_UNKNOWN
;
3837 REG_ALLOC (aarch64_insn_r
->aarch64_regs
, aarch64_insn_r
->reg_rec_count
,
3839 return AARCH64_RECORD_SUCCESS
;
3842 /* Record handler for data processing - immediate instructions. */
3845 aarch64_record_data_proc_imm (insn_decode_record
*aarch64_insn_r
)
3847 uint8_t reg_rd
, insn_bit23
, insn_bits24_27
, setflags
;
3848 uint32_t record_buf
[4];
3850 reg_rd
= bits (aarch64_insn_r
->aarch64_insn
, 0, 4);
3851 insn_bit23
= bit (aarch64_insn_r
->aarch64_insn
, 23);
3852 insn_bits24_27
= bits (aarch64_insn_r
->aarch64_insn
, 24, 27);
3854 if (insn_bits24_27
== 0x00 /* PC rel addressing. */
3855 || insn_bits24_27
== 0x03 /* Bitfield and Extract. */
3856 || (insn_bits24_27
== 0x02 && insn_bit23
)) /* Move wide (immediate). */
3858 record_buf
[0] = reg_rd
;
3859 aarch64_insn_r
->reg_rec_count
= 1;
3861 else if (insn_bits24_27
== 0x01)
3863 /* Add/Subtract (immediate). */
3864 setflags
= bit (aarch64_insn_r
->aarch64_insn
, 29);
3865 record_buf
[0] = reg_rd
;
3866 aarch64_insn_r
->reg_rec_count
= 1;
3868 record_buf
[aarch64_insn_r
->reg_rec_count
++] = AARCH64_CPSR_REGNUM
;
3870 else if (insn_bits24_27
== 0x02 && !insn_bit23
)
3872 /* Logical (immediate). */
3873 setflags
= bits (aarch64_insn_r
->aarch64_insn
, 29, 30) == 0x03;
3874 record_buf
[0] = reg_rd
;
3875 aarch64_insn_r
->reg_rec_count
= 1;
3877 record_buf
[aarch64_insn_r
->reg_rec_count
++] = AARCH64_CPSR_REGNUM
;
3880 return AARCH64_RECORD_UNKNOWN
;
3882 REG_ALLOC (aarch64_insn_r
->aarch64_regs
, aarch64_insn_r
->reg_rec_count
,
3884 return AARCH64_RECORD_SUCCESS
;
3887 /* Record handler for branch, exception generation and system instructions. */
3890 aarch64_record_branch_except_sys (insn_decode_record
*aarch64_insn_r
)
3893 aarch64_gdbarch_tdep
*tdep
3894 = (aarch64_gdbarch_tdep
*) gdbarch_tdep (aarch64_insn_r
->gdbarch
);
3895 uint8_t insn_bits24_27
, insn_bits28_31
, insn_bits22_23
;
3896 uint32_t record_buf
[4];
3898 insn_bits24_27
= bits (aarch64_insn_r
->aarch64_insn
, 24, 27);
3899 insn_bits28_31
= bits (aarch64_insn_r
->aarch64_insn
, 28, 31);
3900 insn_bits22_23
= bits (aarch64_insn_r
->aarch64_insn
, 22, 23);
3902 if (insn_bits28_31
== 0x0d)
3904 /* Exception generation instructions. */
3905 if (insn_bits24_27
== 0x04)
3907 if (!bits (aarch64_insn_r
->aarch64_insn
, 2, 4)
3908 && !bits (aarch64_insn_r
->aarch64_insn
, 21, 23)
3909 && bits (aarch64_insn_r
->aarch64_insn
, 0, 1) == 0x01)
3911 ULONGEST svc_number
;
3913 regcache_raw_read_unsigned (aarch64_insn_r
->regcache
, 8,
3915 return tdep
->aarch64_syscall_record (aarch64_insn_r
->regcache
,
3919 return AARCH64_RECORD_UNSUPPORTED
;
3921 /* System instructions. */
3922 else if (insn_bits24_27
== 0x05 && insn_bits22_23
== 0x00)
3924 uint32_t reg_rt
, reg_crn
;
3926 reg_rt
= bits (aarch64_insn_r
->aarch64_insn
, 0, 4);
3927 reg_crn
= bits (aarch64_insn_r
->aarch64_insn
, 12, 15);
3929 /* Record rt in case of sysl and mrs instructions. */
3930 if (bit (aarch64_insn_r
->aarch64_insn
, 21))
3932 record_buf
[0] = reg_rt
;
3933 aarch64_insn_r
->reg_rec_count
= 1;
3935 /* Record cpsr for hint and msr(immediate) instructions. */
3936 else if (reg_crn
== 0x02 || reg_crn
== 0x04)
3938 record_buf
[0] = AARCH64_CPSR_REGNUM
;
3939 aarch64_insn_r
->reg_rec_count
= 1;
3942 /* Unconditional branch (register). */
3943 else if((insn_bits24_27
& 0x0e) == 0x06)
3945 record_buf
[aarch64_insn_r
->reg_rec_count
++] = AARCH64_PC_REGNUM
;
3946 if (bits (aarch64_insn_r
->aarch64_insn
, 21, 22) == 0x01)
3947 record_buf
[aarch64_insn_r
->reg_rec_count
++] = AARCH64_LR_REGNUM
;
3950 return AARCH64_RECORD_UNKNOWN
;
3952 /* Unconditional branch (immediate). */
3953 else if ((insn_bits28_31
& 0x07) == 0x01 && (insn_bits24_27
& 0x0c) == 0x04)
3955 record_buf
[aarch64_insn_r
->reg_rec_count
++] = AARCH64_PC_REGNUM
;
3956 if (bit (aarch64_insn_r
->aarch64_insn
, 31))
3957 record_buf
[aarch64_insn_r
->reg_rec_count
++] = AARCH64_LR_REGNUM
;
3960 /* Compare & branch (immediate), Test & branch (immediate) and
3961 Conditional branch (immediate). */
3962 record_buf
[aarch64_insn_r
->reg_rec_count
++] = AARCH64_PC_REGNUM
;
3964 REG_ALLOC (aarch64_insn_r
->aarch64_regs
, aarch64_insn_r
->reg_rec_count
,
3966 return AARCH64_RECORD_SUCCESS
;
3969 /* Record handler for advanced SIMD load and store instructions. */
3972 aarch64_record_asimd_load_store (insn_decode_record
*aarch64_insn_r
)
3975 uint64_t addr_offset
= 0;
3976 uint32_t record_buf
[24];
3977 uint64_t record_buf_mem
[24];
3978 uint32_t reg_rn
, reg_rt
;
3979 uint32_t reg_index
= 0, mem_index
= 0;
3980 uint8_t opcode_bits
, size_bits
;
3982 reg_rt
= bits (aarch64_insn_r
->aarch64_insn
, 0, 4);
3983 reg_rn
= bits (aarch64_insn_r
->aarch64_insn
, 5, 9);
3984 size_bits
= bits (aarch64_insn_r
->aarch64_insn
, 10, 11);
3985 opcode_bits
= bits (aarch64_insn_r
->aarch64_insn
, 12, 15);
3986 regcache_raw_read_unsigned (aarch64_insn_r
->regcache
, reg_rn
, &address
);
3989 debug_printf ("Process record: Advanced SIMD load/store\n");
3991 /* Load/store single structure. */
3992 if (bit (aarch64_insn_r
->aarch64_insn
, 24))
3994 uint8_t sindex
, scale
, selem
, esize
, replicate
= 0;
3995 scale
= opcode_bits
>> 2;
3996 selem
= ((opcode_bits
& 0x02) |
3997 bit (aarch64_insn_r
->aarch64_insn
, 21)) + 1;
4001 if (size_bits
& 0x01)
4002 return AARCH64_RECORD_UNKNOWN
;
4005 if ((size_bits
>> 1) & 0x01)
4006 return AARCH64_RECORD_UNKNOWN
;
4007 if (size_bits
& 0x01)
4009 if (!((opcode_bits
>> 1) & 0x01))
4012 return AARCH64_RECORD_UNKNOWN
;
4016 if (bit (aarch64_insn_r
->aarch64_insn
, 22) && !(opcode_bits
& 0x01))
4023 return AARCH64_RECORD_UNKNOWN
;
4029 for (sindex
= 0; sindex
< selem
; sindex
++)
4031 record_buf
[reg_index
++] = reg_rt
+ AARCH64_V0_REGNUM
;
4032 reg_rt
= (reg_rt
+ 1) % 32;
4036 for (sindex
= 0; sindex
< selem
; sindex
++)
4038 if (bit (aarch64_insn_r
->aarch64_insn
, 22))
4039 record_buf
[reg_index
++] = reg_rt
+ AARCH64_V0_REGNUM
;
4042 record_buf_mem
[mem_index
++] = esize
/ 8;
4043 record_buf_mem
[mem_index
++] = address
+ addr_offset
;
4045 addr_offset
= addr_offset
+ (esize
/ 8);
4046 reg_rt
= (reg_rt
+ 1) % 32;
4050 /* Load/store multiple structure. */
4053 uint8_t selem
, esize
, rpt
, elements
;
4054 uint8_t eindex
, rindex
;
4056 esize
= 8 << size_bits
;
4057 if (bit (aarch64_insn_r
->aarch64_insn
, 30))
4058 elements
= 128 / esize
;
4060 elements
= 64 / esize
;
4062 switch (opcode_bits
)
4064 /*LD/ST4 (4 Registers). */
4069 /*LD/ST1 (4 Registers). */
4074 /*LD/ST3 (3 Registers). */
4079 /*LD/ST1 (3 Registers). */
4084 /*LD/ST1 (1 Register). */
4089 /*LD/ST2 (2 Registers). */
4094 /*LD/ST1 (2 Registers). */
4100 return AARCH64_RECORD_UNSUPPORTED
;
4103 for (rindex
= 0; rindex
< rpt
; rindex
++)
4104 for (eindex
= 0; eindex
< elements
; eindex
++)
4106 uint8_t reg_tt
, sindex
;
4107 reg_tt
= (reg_rt
+ rindex
) % 32;
4108 for (sindex
= 0; sindex
< selem
; sindex
++)
4110 if (bit (aarch64_insn_r
->aarch64_insn
, 22))
4111 record_buf
[reg_index
++] = reg_tt
+ AARCH64_V0_REGNUM
;
4114 record_buf_mem
[mem_index
++] = esize
/ 8;
4115 record_buf_mem
[mem_index
++] = address
+ addr_offset
;
4117 addr_offset
= addr_offset
+ (esize
/ 8);
4118 reg_tt
= (reg_tt
+ 1) % 32;
4123 if (bit (aarch64_insn_r
->aarch64_insn
, 23))
4124 record_buf
[reg_index
++] = reg_rn
;
4126 aarch64_insn_r
->reg_rec_count
= reg_index
;
4127 aarch64_insn_r
->mem_rec_count
= mem_index
/ 2;
4128 MEM_ALLOC (aarch64_insn_r
->aarch64_mems
, aarch64_insn_r
->mem_rec_count
,
4130 REG_ALLOC (aarch64_insn_r
->aarch64_regs
, aarch64_insn_r
->reg_rec_count
,
4132 return AARCH64_RECORD_SUCCESS
;
4135 /* Record handler for load and store instructions. */
4138 aarch64_record_load_store (insn_decode_record
*aarch64_insn_r
)
4140 uint8_t insn_bits24_27
, insn_bits28_29
, insn_bits10_11
;
4141 uint8_t insn_bit23
, insn_bit21
;
4142 uint8_t opc
, size_bits
, ld_flag
, vector_flag
;
4143 uint32_t reg_rn
, reg_rt
, reg_rt2
;
4144 uint64_t datasize
, offset
;
4145 uint32_t record_buf
[8];
4146 uint64_t record_buf_mem
[8];
4149 insn_bits10_11
= bits (aarch64_insn_r
->aarch64_insn
, 10, 11);
4150 insn_bits24_27
= bits (aarch64_insn_r
->aarch64_insn
, 24, 27);
4151 insn_bits28_29
= bits (aarch64_insn_r
->aarch64_insn
, 28, 29);
4152 insn_bit21
= bit (aarch64_insn_r
->aarch64_insn
, 21);
4153 insn_bit23
= bit (aarch64_insn_r
->aarch64_insn
, 23);
4154 ld_flag
= bit (aarch64_insn_r
->aarch64_insn
, 22);
4155 vector_flag
= bit (aarch64_insn_r
->aarch64_insn
, 26);
4156 reg_rt
= bits (aarch64_insn_r
->aarch64_insn
, 0, 4);
4157 reg_rn
= bits (aarch64_insn_r
->aarch64_insn
, 5, 9);
4158 reg_rt2
= bits (aarch64_insn_r
->aarch64_insn
, 10, 14);
4159 size_bits
= bits (aarch64_insn_r
->aarch64_insn
, 30, 31);
4161 /* Load/store exclusive. */
4162 if (insn_bits24_27
== 0x08 && insn_bits28_29
== 0x00)
4165 debug_printf ("Process record: load/store exclusive\n");
4169 record_buf
[0] = reg_rt
;
4170 aarch64_insn_r
->reg_rec_count
= 1;
4173 record_buf
[1] = reg_rt2
;
4174 aarch64_insn_r
->reg_rec_count
= 2;
4180 datasize
= (8 << size_bits
) * 2;
4182 datasize
= (8 << size_bits
);
4183 regcache_raw_read_unsigned (aarch64_insn_r
->regcache
, reg_rn
,
4185 record_buf_mem
[0] = datasize
/ 8;
4186 record_buf_mem
[1] = address
;
4187 aarch64_insn_r
->mem_rec_count
= 1;
4190 /* Save register rs. */
4191 record_buf
[0] = bits (aarch64_insn_r
->aarch64_insn
, 16, 20);
4192 aarch64_insn_r
->reg_rec_count
= 1;
4196 /* Load register (literal) instructions decoding. */
4197 else if ((insn_bits24_27
& 0x0b) == 0x08 && insn_bits28_29
== 0x01)
4200 debug_printf ("Process record: load register (literal)\n");
4202 record_buf
[0] = reg_rt
+ AARCH64_V0_REGNUM
;
4204 record_buf
[0] = reg_rt
;
4205 aarch64_insn_r
->reg_rec_count
= 1;
4207 /* All types of load/store pair instructions decoding. */
4208 else if ((insn_bits24_27
& 0x0a) == 0x08 && insn_bits28_29
== 0x02)
4211 debug_printf ("Process record: load/store pair\n");
4217 record_buf
[0] = reg_rt
+ AARCH64_V0_REGNUM
;
4218 record_buf
[1] = reg_rt2
+ AARCH64_V0_REGNUM
;
4222 record_buf
[0] = reg_rt
;
4223 record_buf
[1] = reg_rt2
;
4225 aarch64_insn_r
->reg_rec_count
= 2;
4230 imm7_off
= bits (aarch64_insn_r
->aarch64_insn
, 15, 21);
4232 size_bits
= size_bits
>> 1;
4233 datasize
= 8 << (2 + size_bits
);
4234 offset
= (imm7_off
& 0x40) ? (~imm7_off
& 0x007f) + 1 : imm7_off
;
4235 offset
= offset
<< (2 + size_bits
);
4236 regcache_raw_read_unsigned (aarch64_insn_r
->regcache
, reg_rn
,
4238 if (!((insn_bits24_27
& 0x0b) == 0x08 && insn_bit23
))
4240 if (imm7_off
& 0x40)
4241 address
= address
- offset
;
4243 address
= address
+ offset
;
4246 record_buf_mem
[0] = datasize
/ 8;
4247 record_buf_mem
[1] = address
;
4248 record_buf_mem
[2] = datasize
/ 8;
4249 record_buf_mem
[3] = address
+ (datasize
/ 8);
4250 aarch64_insn_r
->mem_rec_count
= 2;
4252 if (bit (aarch64_insn_r
->aarch64_insn
, 23))
4253 record_buf
[aarch64_insn_r
->reg_rec_count
++] = reg_rn
;
4255 /* Load/store register (unsigned immediate) instructions. */
4256 else if ((insn_bits24_27
& 0x0b) == 0x09 && insn_bits28_29
== 0x03)
4258 opc
= bits (aarch64_insn_r
->aarch64_insn
, 22, 23);
4268 if (size_bits
== 0x3 && vector_flag
== 0x0 && opc
== 0x2)
4270 /* PRFM (immediate) */
4271 return AARCH64_RECORD_SUCCESS
;
4273 else if (size_bits
== 0x2 && vector_flag
== 0x0 && opc
== 0x2)
4275 /* LDRSW (immediate) */
4289 debug_printf ("Process record: load/store (unsigned immediate):"
4290 " size %x V %d opc %x\n", size_bits
, vector_flag
,
4296 offset
= bits (aarch64_insn_r
->aarch64_insn
, 10, 21);
4297 datasize
= 8 << size_bits
;
4298 regcache_raw_read_unsigned (aarch64_insn_r
->regcache
, reg_rn
,
4300 offset
= offset
<< size_bits
;
4301 address
= address
+ offset
;
4303 record_buf_mem
[0] = datasize
>> 3;
4304 record_buf_mem
[1] = address
;
4305 aarch64_insn_r
->mem_rec_count
= 1;
4310 record_buf
[0] = reg_rt
+ AARCH64_V0_REGNUM
;
4312 record_buf
[0] = reg_rt
;
4313 aarch64_insn_r
->reg_rec_count
= 1;
4316 /* Load/store register (register offset) instructions. */
4317 else if ((insn_bits24_27
& 0x0b) == 0x08 && insn_bits28_29
== 0x03
4318 && insn_bits10_11
== 0x02 && insn_bit21
)
4321 debug_printf ("Process record: load/store (register offset)\n");
4322 opc
= bits (aarch64_insn_r
->aarch64_insn
, 22, 23);
4329 if (size_bits
!= 0x03)
4332 return AARCH64_RECORD_UNKNOWN
;
4336 ULONGEST reg_rm_val
;
4338 regcache_raw_read_unsigned (aarch64_insn_r
->regcache
,
4339 bits (aarch64_insn_r
->aarch64_insn
, 16, 20), ®_rm_val
);
4340 if (bit (aarch64_insn_r
->aarch64_insn
, 12))
4341 offset
= reg_rm_val
<< size_bits
;
4343 offset
= reg_rm_val
;
4344 datasize
= 8 << size_bits
;
4345 regcache_raw_read_unsigned (aarch64_insn_r
->regcache
, reg_rn
,
4347 address
= address
+ offset
;
4348 record_buf_mem
[0] = datasize
>> 3;
4349 record_buf_mem
[1] = address
;
4350 aarch64_insn_r
->mem_rec_count
= 1;
4355 record_buf
[0] = reg_rt
+ AARCH64_V0_REGNUM
;
4357 record_buf
[0] = reg_rt
;
4358 aarch64_insn_r
->reg_rec_count
= 1;
4361 /* Load/store register (immediate and unprivileged) instructions. */
4362 else if ((insn_bits24_27
& 0x0b) == 0x08 && insn_bits28_29
== 0x03
4367 debug_printf ("Process record: load/store "
4368 "(immediate and unprivileged)\n");
4370 opc
= bits (aarch64_insn_r
->aarch64_insn
, 22, 23);
4377 if (size_bits
!= 0x03)
4380 return AARCH64_RECORD_UNKNOWN
;
4385 imm9_off
= bits (aarch64_insn_r
->aarch64_insn
, 12, 20);
4386 offset
= (imm9_off
& 0x0100) ? (((~imm9_off
) & 0x01ff) + 1) : imm9_off
;
4387 datasize
= 8 << size_bits
;
4388 regcache_raw_read_unsigned (aarch64_insn_r
->regcache
, reg_rn
,
4390 if (insn_bits10_11
!= 0x01)
4392 if (imm9_off
& 0x0100)
4393 address
= address
- offset
;
4395 address
= address
+ offset
;
4397 record_buf_mem
[0] = datasize
>> 3;
4398 record_buf_mem
[1] = address
;
4399 aarch64_insn_r
->mem_rec_count
= 1;
4404 record_buf
[0] = reg_rt
+ AARCH64_V0_REGNUM
;
4406 record_buf
[0] = reg_rt
;
4407 aarch64_insn_r
->reg_rec_count
= 1;
4409 if (insn_bits10_11
== 0x01 || insn_bits10_11
== 0x03)
4410 record_buf
[aarch64_insn_r
->reg_rec_count
++] = reg_rn
;
4412 /* Advanced SIMD load/store instructions. */
4414 return aarch64_record_asimd_load_store (aarch64_insn_r
);
4416 MEM_ALLOC (aarch64_insn_r
->aarch64_mems
, aarch64_insn_r
->mem_rec_count
,
4418 REG_ALLOC (aarch64_insn_r
->aarch64_regs
, aarch64_insn_r
->reg_rec_count
,
4420 return AARCH64_RECORD_SUCCESS
;
4423 /* Record handler for data processing SIMD and floating point instructions. */
4426 aarch64_record_data_proc_simd_fp (insn_decode_record
*aarch64_insn_r
)
4428 uint8_t insn_bit21
, opcode
, rmode
, reg_rd
;
4429 uint8_t insn_bits24_27
, insn_bits28_31
, insn_bits10_11
, insn_bits12_15
;
4430 uint8_t insn_bits11_14
;
4431 uint32_t record_buf
[2];
4433 insn_bits24_27
= bits (aarch64_insn_r
->aarch64_insn
, 24, 27);
4434 insn_bits28_31
= bits (aarch64_insn_r
->aarch64_insn
, 28, 31);
4435 insn_bits10_11
= bits (aarch64_insn_r
->aarch64_insn
, 10, 11);
4436 insn_bits12_15
= bits (aarch64_insn_r
->aarch64_insn
, 12, 15);
4437 insn_bits11_14
= bits (aarch64_insn_r
->aarch64_insn
, 11, 14);
4438 opcode
= bits (aarch64_insn_r
->aarch64_insn
, 16, 18);
4439 rmode
= bits (aarch64_insn_r
->aarch64_insn
, 19, 20);
4440 reg_rd
= bits (aarch64_insn_r
->aarch64_insn
, 0, 4);
4441 insn_bit21
= bit (aarch64_insn_r
->aarch64_insn
, 21);
4444 debug_printf ("Process record: data processing SIMD/FP: ");
4446 if ((insn_bits28_31
& 0x05) == 0x01 && insn_bits24_27
== 0x0e)
4448 /* Floating point - fixed point conversion instructions. */
4452 debug_printf ("FP - fixed point conversion");
4454 if ((opcode
>> 1) == 0x0 && rmode
== 0x03)
4455 record_buf
[0] = reg_rd
;
4457 record_buf
[0] = reg_rd
+ AARCH64_V0_REGNUM
;
4459 /* Floating point - conditional compare instructions. */
4460 else if (insn_bits10_11
== 0x01)
4463 debug_printf ("FP - conditional compare");
4465 record_buf
[0] = AARCH64_CPSR_REGNUM
;
4467 /* Floating point - data processing (2-source) and
4468 conditional select instructions. */
4469 else if (insn_bits10_11
== 0x02 || insn_bits10_11
== 0x03)
4472 debug_printf ("FP - DP (2-source)");
4474 record_buf
[0] = reg_rd
+ AARCH64_V0_REGNUM
;
4476 else if (insn_bits10_11
== 0x00)
4478 /* Floating point - immediate instructions. */
4479 if ((insn_bits12_15
& 0x01) == 0x01
4480 || (insn_bits12_15
& 0x07) == 0x04)
4483 debug_printf ("FP - immediate");
4484 record_buf
[0] = reg_rd
+ AARCH64_V0_REGNUM
;
4486 /* Floating point - compare instructions. */
4487 else if ((insn_bits12_15
& 0x03) == 0x02)
4490 debug_printf ("FP - immediate");
4491 record_buf
[0] = AARCH64_CPSR_REGNUM
;
4493 /* Floating point - integer conversions instructions. */
4494 else if (insn_bits12_15
== 0x00)
4496 /* Convert float to integer instruction. */
4497 if (!(opcode
>> 1) || ((opcode
>> 1) == 0x02 && !rmode
))
4500 debug_printf ("float to int conversion");
4502 record_buf
[0] = reg_rd
+ AARCH64_X0_REGNUM
;
4504 /* Convert integer to float instruction. */
4505 else if ((opcode
>> 1) == 0x01 && !rmode
)
4508 debug_printf ("int to float conversion");
4510 record_buf
[0] = reg_rd
+ AARCH64_V0_REGNUM
;
4512 /* Move float to integer instruction. */
4513 else if ((opcode
>> 1) == 0x03)
4516 debug_printf ("move float to int");
4518 if (!(opcode
& 0x01))
4519 record_buf
[0] = reg_rd
+ AARCH64_X0_REGNUM
;
4521 record_buf
[0] = reg_rd
+ AARCH64_V0_REGNUM
;
4524 return AARCH64_RECORD_UNKNOWN
;
4527 return AARCH64_RECORD_UNKNOWN
;
4530 return AARCH64_RECORD_UNKNOWN
;
4532 else if ((insn_bits28_31
& 0x09) == 0x00 && insn_bits24_27
== 0x0e)
4535 debug_printf ("SIMD copy");
4537 /* Advanced SIMD copy instructions. */
4538 if (!bits (aarch64_insn_r
->aarch64_insn
, 21, 23)
4539 && !bit (aarch64_insn_r
->aarch64_insn
, 15)
4540 && bit (aarch64_insn_r
->aarch64_insn
, 10))
4542 if (insn_bits11_14
== 0x05 || insn_bits11_14
== 0x07)
4543 record_buf
[0] = reg_rd
+ AARCH64_X0_REGNUM
;
4545 record_buf
[0] = reg_rd
+ AARCH64_V0_REGNUM
;
4548 record_buf
[0] = reg_rd
+ AARCH64_V0_REGNUM
;
4550 /* All remaining floating point or advanced SIMD instructions. */
4554 debug_printf ("all remain");
4556 record_buf
[0] = reg_rd
+ AARCH64_V0_REGNUM
;
4560 debug_printf ("\n");
4562 /* Record the V/X register. */
4563 aarch64_insn_r
->reg_rec_count
++;
4565 /* Some of these instructions may set bits in the FPSR, so record it
4567 record_buf
[1] = AARCH64_FPSR_REGNUM
;
4568 aarch64_insn_r
->reg_rec_count
++;
4570 gdb_assert (aarch64_insn_r
->reg_rec_count
== 2);
4571 REG_ALLOC (aarch64_insn_r
->aarch64_regs
, aarch64_insn_r
->reg_rec_count
,
4573 return AARCH64_RECORD_SUCCESS
;
4576 /* Decodes insns type and invokes its record handler. */
4579 aarch64_record_decode_insn_handler (insn_decode_record
*aarch64_insn_r
)
4581 uint32_t ins_bit25
, ins_bit26
, ins_bit27
, ins_bit28
;
4583 ins_bit25
= bit (aarch64_insn_r
->aarch64_insn
, 25);
4584 ins_bit26
= bit (aarch64_insn_r
->aarch64_insn
, 26);
4585 ins_bit27
= bit (aarch64_insn_r
->aarch64_insn
, 27);
4586 ins_bit28
= bit (aarch64_insn_r
->aarch64_insn
, 28);
4588 /* Data processing - immediate instructions. */
4589 if (!ins_bit26
&& !ins_bit27
&& ins_bit28
)
4590 return aarch64_record_data_proc_imm (aarch64_insn_r
);
4592 /* Branch, exception generation and system instructions. */
4593 if (ins_bit26
&& !ins_bit27
&& ins_bit28
)
4594 return aarch64_record_branch_except_sys (aarch64_insn_r
);
4596 /* Load and store instructions. */
4597 if (!ins_bit25
&& ins_bit27
)
4598 return aarch64_record_load_store (aarch64_insn_r
);
4600 /* Data processing - register instructions. */
4601 if (ins_bit25
&& !ins_bit26
&& ins_bit27
)
4602 return aarch64_record_data_proc_reg (aarch64_insn_r
);
4604 /* Data processing - SIMD and floating point instructions. */
4605 if (ins_bit25
&& ins_bit26
&& ins_bit27
)
4606 return aarch64_record_data_proc_simd_fp (aarch64_insn_r
);
4608 return AARCH64_RECORD_UNSUPPORTED
;
4611 /* Cleans up local record registers and memory allocations. */
4614 deallocate_reg_mem (insn_decode_record
*record
)
4616 xfree (record
->aarch64_regs
);
4617 xfree (record
->aarch64_mems
);
4621 namespace selftests
{
4624 aarch64_process_record_test (void)
4626 struct gdbarch_info info
;
4629 info
.bfd_arch_info
= bfd_scan_arch ("aarch64");
4631 struct gdbarch
*gdbarch
= gdbarch_find_by_info (info
);
4632 SELF_CHECK (gdbarch
!= NULL
);
4634 insn_decode_record aarch64_record
;
4636 memset (&aarch64_record
, 0, sizeof (insn_decode_record
));
4637 aarch64_record
.regcache
= NULL
;
4638 aarch64_record
.this_addr
= 0;
4639 aarch64_record
.gdbarch
= gdbarch
;
4641 /* 20 00 80 f9 prfm pldl1keep, [x1] */
4642 aarch64_record
.aarch64_insn
= 0xf9800020;
4643 ret
= aarch64_record_decode_insn_handler (&aarch64_record
);
4644 SELF_CHECK (ret
== AARCH64_RECORD_SUCCESS
);
4645 SELF_CHECK (aarch64_record
.reg_rec_count
== 0);
4646 SELF_CHECK (aarch64_record
.mem_rec_count
== 0);
4648 deallocate_reg_mem (&aarch64_record
);
4651 } // namespace selftests
4652 #endif /* GDB_SELF_TEST */
4654 /* Parse the current instruction and record the values of the registers and
4655 memory that will be changed in current instruction to record_arch_list
4656 return -1 if something is wrong. */
4659 aarch64_process_record (struct gdbarch
*gdbarch
, struct regcache
*regcache
,
4660 CORE_ADDR insn_addr
)
4662 uint32_t rec_no
= 0;
4663 uint8_t insn_size
= 4;
4665 gdb_byte buf
[insn_size
];
4666 insn_decode_record aarch64_record
;
4668 memset (&buf
[0], 0, insn_size
);
4669 memset (&aarch64_record
, 0, sizeof (insn_decode_record
));
4670 target_read_memory (insn_addr
, &buf
[0], insn_size
);
4671 aarch64_record
.aarch64_insn
4672 = (uint32_t) extract_unsigned_integer (&buf
[0],
4674 gdbarch_byte_order (gdbarch
));
4675 aarch64_record
.regcache
= regcache
;
4676 aarch64_record
.this_addr
= insn_addr
;
4677 aarch64_record
.gdbarch
= gdbarch
;
4679 ret
= aarch64_record_decode_insn_handler (&aarch64_record
);
4680 if (ret
== AARCH64_RECORD_UNSUPPORTED
)
4682 gdb_printf (gdb_stderr
,
4683 _("Process record does not support instruction "
4684 "0x%0x at address %s.\n"),
4685 aarch64_record
.aarch64_insn
,
4686 paddress (gdbarch
, insn_addr
));
4692 /* Record registers. */
4693 record_full_arch_list_add_reg (aarch64_record
.regcache
,
4695 /* Always record register CPSR. */
4696 record_full_arch_list_add_reg (aarch64_record
.regcache
,
4697 AARCH64_CPSR_REGNUM
);
4698 if (aarch64_record
.aarch64_regs
)
4699 for (rec_no
= 0; rec_no
< aarch64_record
.reg_rec_count
; rec_no
++)
4700 if (record_full_arch_list_add_reg (aarch64_record
.regcache
,
4701 aarch64_record
.aarch64_regs
[rec_no
]))
4704 /* Record memories. */
4705 if (aarch64_record
.aarch64_mems
)
4706 for (rec_no
= 0; rec_no
< aarch64_record
.mem_rec_count
; rec_no
++)
4707 if (record_full_arch_list_add_mem
4708 ((CORE_ADDR
)aarch64_record
.aarch64_mems
[rec_no
].addr
,
4709 aarch64_record
.aarch64_mems
[rec_no
].len
))
4712 if (record_full_arch_list_add_end ())
4716 deallocate_reg_mem (&aarch64_record
);