]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/aarch64-tdep.c
ld/aarch64elf: add support for DT_AARCH64_MEMTAG_STACK dynamic tag
[thirdparty/binutils-gdb.git] / gdb / aarch64-tdep.c
CommitLineData
07b287a0
MS
1/* Common target dependent code for GDB on AArch64 systems.
2
d01e8234 3 Copyright (C) 2009-2025 Free Software Foundation, Inc.
07b287a0
MS
4 Contributed by ARM Ltd.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
07b287a0 21
ec452525 22#include "extract-store-integer.h"
07b287a0 23#include "frame.h"
83b6e1f1 24#include "language.h"
5b9707eb 25#include "cli/cli-cmds.h"
07b287a0 26#include "gdbcore.h"
4de283e4 27#include "dis-asm.h"
d55e5aa6
TT
28#include "regcache.h"
29#include "reggroups.h"
4de283e4
TT
30#include "value.h"
31#include "arch-utils.h"
32#include "osabi.h"
33#include "frame-unwind.h"
34#include "frame-base.h"
d55e5aa6 35#include "trad-frame.h"
4de283e4
TT
36#include "objfiles.h"
37#include "dwarf2.h"
82ca8957 38#include "dwarf2/frame.h"
4de283e4
TT
39#include "gdbtypes.h"
40#include "prologue-value.h"
41#include "target-descriptions.h"
07b287a0 42#include "user-regs.h"
4de283e4 43#include "ax-gdb.h"
268a13a5 44#include "gdbsupport/selftest.h"
4de283e4
TT
45
46#include "aarch64-tdep.h"
47#include "aarch64-ravenscar-thread.h"
b782c65b 48#include "arch/aarch64-mte.h"
4de283e4 49
4de283e4
TT
50#include "record.h"
51#include "record-full.h"
52#include "arch/aarch64-insn.h"
0d12e84c 53#include "gdbarch.h"
4de283e4
TT
54
55#include "opcode/aarch64.h"
56#include <algorithm>
0ee6b1c5 57#include <unordered_map>
f77ee802 58
ef139898
LM
59/* For inferior_ptid and current_inferior (). */
60#include "inferior.h"
ca65640f
LM
61/* For std::sqrt and std::pow. */
62#include <cmath>
ef139898 63
ea92689a
AH
64/* A Homogeneous Floating-Point or Short-Vector Aggregate may have at most
65 four members. */
66#define HA_MAX_NUM_FLDS 4
67
95228a0d 68/* All possible aarch64 target descriptors. */
0ee6b1c5 69static std::unordered_map <aarch64_features, target_desc *> tdesc_aarch64_map;
95228a0d 70
ea2f6fad
TV
71/* The standard register names, and all the valid aliases for them.
72 We're not adding fp here, that name is already taken, see
73 _initialize_frame_reg. */
07b287a0
MS
74static const struct
75{
76 const char *const name;
77 int regnum;
78} aarch64_register_aliases[] =
79{
ea2f6fad 80 /* Link register alias for x30. */
07b287a0 81 {"lr", AARCH64_LR_REGNUM},
ea2f6fad
TV
82 /* SP is the canonical name for x31 according to aarch64_r_register_names,
83 so we're adding an x31 alias for sp. */
84 {"x31", AARCH64_SP_REGNUM},
07b287a0
MS
85 /* specials */
86 {"ip0", AARCH64_X0_REGNUM + 16},
87 {"ip1", AARCH64_X0_REGNUM + 17}
88};
89
90/* The required core 'R' registers. */
91static const char *const aarch64_r_register_names[] =
92{
93 /* These registers must appear in consecutive RAW register number
94 order and they must begin with AARCH64_X0_REGNUM! */
95 "x0", "x1", "x2", "x3",
96 "x4", "x5", "x6", "x7",
97 "x8", "x9", "x10", "x11",
98 "x12", "x13", "x14", "x15",
99 "x16", "x17", "x18", "x19",
100 "x20", "x21", "x22", "x23",
101 "x24", "x25", "x26", "x27",
102 "x28", "x29", "x30", "sp",
103 "pc", "cpsr"
104};
105
106/* The FP/SIMD 'V' registers. */
107static const char *const aarch64_v_register_names[] =
108{
109 /* These registers must appear in consecutive RAW register number
110 order and they must begin with AARCH64_V0_REGNUM! */
111 "v0", "v1", "v2", "v3",
112 "v4", "v5", "v6", "v7",
113 "v8", "v9", "v10", "v11",
114 "v12", "v13", "v14", "v15",
115 "v16", "v17", "v18", "v19",
116 "v20", "v21", "v22", "v23",
117 "v24", "v25", "v26", "v27",
118 "v28", "v29", "v30", "v31",
119 "fpsr",
120 "fpcr"
121};
122
739e8682
AH
123/* The SVE 'Z' and 'P' registers. */
124static const char *const aarch64_sve_register_names[] =
125{
126 /* These registers must appear in consecutive RAW register number
127 order and they must begin with AARCH64_SVE_Z0_REGNUM! */
128 "z0", "z1", "z2", "z3",
129 "z4", "z5", "z6", "z7",
130 "z8", "z9", "z10", "z11",
131 "z12", "z13", "z14", "z15",
132 "z16", "z17", "z18", "z19",
133 "z20", "z21", "z22", "z23",
134 "z24", "z25", "z26", "z27",
135 "z28", "z29", "z30", "z31",
136 "fpsr", "fpcr",
137 "p0", "p1", "p2", "p3",
138 "p4", "p5", "p6", "p7",
139 "p8", "p9", "p10", "p11",
140 "p12", "p13", "p14", "p15",
141 "ffr", "vg"
142};
143
76bed0fd
AH
144static const char *const aarch64_pauth_register_names[] =
145{
6d002087 146 /* Authentication mask for data pointer, low half/user pointers. */
76bed0fd 147 "pauth_dmask",
6d002087
LM
148 /* Authentication mask for code pointer, low half/user pointers. */
149 "pauth_cmask",
150 /* Authentication mask for data pointer, high half / kernel pointers. */
151 "pauth_dmask_high",
152 /* Authentication mask for code pointer, high half / kernel pointers. */
153 "pauth_cmask_high"
76bed0fd
AH
154};
155
5e984dbf
LM
156static const char *const aarch64_mte_register_names[] =
157{
158 /* Tag Control Register. */
159 "tag_ctl"
160};
161
29e09a42
TV
162static int aarch64_stack_frame_destroyed_p (struct gdbarch *, CORE_ADDR);
163
07b287a0
MS
164/* AArch64 prologue cache structure. */
165struct aarch64_prologue_cache
166{
db634143
PL
167 /* The program counter at the start of the function. It is used to
168 identify this frame as a prologue frame. */
169 CORE_ADDR func;
170
171 /* The program counter at the time this frame was created; i.e. where
172 this function was called from. It is used to identify this frame as a
173 stub frame. */
174 CORE_ADDR prev_pc;
175
07b287a0
MS
176 /* The stack pointer at the time this frame was created; i.e. the
177 caller's stack pointer when this function was called. It is used
178 to identify this frame. */
179 CORE_ADDR prev_sp;
180
7dfa3edc
PL
181 /* Is the target available to read from? */
182 int available_p;
183
07b287a0
MS
184 /* The frame base for this frame is just prev_sp - frame size.
185 FRAMESIZE is the distance from the frame pointer to the
186 initial stack pointer. */
187 int framesize;
188
189 /* The register used to hold the frame pointer for this frame. */
190 int framereg;
191
192 /* Saved register offsets. */
098caef4 193 trad_frame_saved_reg *saved_regs;
07b287a0
MS
194};
195
ca65640f
LM
196/* Holds information used to read/write from/to ZA
197 pseudo-registers.
198
199 With this information, the read/write code can be simplified so it
200 deals only with the required information to map a ZA pseudo-register
201 to the exact bytes into the ZA contents buffer. Otherwise we'd need
202 to use a lot of conditionals. */
203
204struct za_offsets
205{
206 /* Offset, into ZA, of the starting byte of the pseudo-register. */
207 size_t starting_offset;
208 /* The size of the contiguous chunks of the pseudo-register. */
209 size_t chunk_size;
210 /* The number of pseudo-register chunks contained in ZA. */
211 size_t chunks;
212 /* The offset between each contiguous chunk. */
213 size_t stride_size;
214};
215
216/* Holds data that is helpful to determine the individual fields that make
217 up the names of the ZA pseudo-registers. It is also very helpful to
218 determine offsets, stride and sizes for reading ZA tiles and tile
219 slices. */
220
221struct za_pseudo_encoding
222{
223 /* The slice index (0 ~ svl). Only used for tile slices. */
224 uint8_t slice_index;
225 /* The tile number (0 ~ 15). */
226 uint8_t tile_index;
227 /* Direction (horizontal/vertical). Only used for tile slices. */
228 bool horizontal;
229 /* Qualifier index (0 ~ 4). These map to B, H, S, D and Q. */
230 uint8_t qualifier_index;
231};
232
07b287a0
MS
233static void
234show_aarch64_debug (struct ui_file *file, int from_tty,
dda83cd7 235 struct cmd_list_element *c, const char *value)
07b287a0 236{
6cb06a8c 237 gdb_printf (file, _("AArch64 debugging is %s.\n"), value);
07b287a0
MS
238}
239
ffdbe864
YQ
240namespace {
241
4d9a9006
YQ
242/* Abstract instruction reader. */
243
244class abstract_instruction_reader
245{
246public:
247 /* Read in one instruction. */
248 virtual ULONGEST read (CORE_ADDR memaddr, int len,
249 enum bfd_endian byte_order) = 0;
250};
251
252/* Instruction reader from real target. */
253
254class instruction_reader : public abstract_instruction_reader
255{
256 public:
257 ULONGEST read (CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
632e107b 258 override
4d9a9006 259 {
fc2f703e 260 return read_code_unsigned_integer (memaddr, len, byte_order);
4d9a9006
YQ
261 }
262};
263
ffdbe864
YQ
264} // namespace
265
3d31bc39
AH
266/* If address signing is enabled, mask off the signature bits from the link
267 register, which is passed by value in ADDR, using the register values in
268 THIS_FRAME. */
11e1b75f
AH
269
270static CORE_ADDR
345bd07c 271aarch64_frame_unmask_lr (aarch64_gdbarch_tdep *tdep,
8480a37e 272 const frame_info_ptr &this_frame, CORE_ADDR addr)
11e1b75f
AH
273{
274 if (tdep->has_pauth ()
275 && frame_unwind_register_unsigned (this_frame,
c9cd8ca4 276 tdep->ra_sign_state_regnum))
11e1b75f 277 {
6d002087
LM
278 /* VA range select (bit 55) tells us whether to use the low half masks
279 or the high half masks. */
280 int cmask_num;
281 if (tdep->pauth_reg_count > 2 && addr & VA_RANGE_SELECT_BIT_MASK)
282 cmask_num = AARCH64_PAUTH_CMASK_HIGH_REGNUM (tdep->pauth_reg_base);
283 else
284 cmask_num = AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base);
285
286 /* By default, we assume TBI and discard the top 8 bits plus the VA range
287 select bit (55). */
288 CORE_ADDR mask = AARCH64_TOP_BITS_MASK;
289 mask |= frame_unwind_register_unsigned (this_frame, cmask_num);
290 addr = aarch64_remove_top_bits (addr, mask);
3d31bc39
AH
291
292 /* Record in the frame that the link register required unmasking. */
293 set_frame_previous_pc_masked (this_frame);
11e1b75f
AH
294 }
295
296 return addr;
297}
298
aa7ca1bb
AH
299/* Implement the "get_pc_address_flags" gdbarch method. */
300
301static std::string
8480a37e 302aarch64_get_pc_address_flags (const frame_info_ptr &frame, CORE_ADDR pc)
aa7ca1bb
AH
303{
304 if (pc != 0 && get_frame_pc_masked (frame))
305 return "PAC";
306
307 return "";
308}
309
07b287a0
MS
310/* Analyze a prologue, looking for a recognizable stack frame
311 and frame pointer. Scan until we encounter a store that could
312 clobber the stack frame unexpectedly, or an unknown instruction. */
313
314static CORE_ADDR
315aarch64_analyze_prologue (struct gdbarch *gdbarch,
316 CORE_ADDR start, CORE_ADDR limit,
4d9a9006
YQ
317 struct aarch64_prologue_cache *cache,
318 abstract_instruction_reader& reader)
07b287a0
MS
319{
320 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
321 int i;
f8e3fe0d
LM
322
323 /* Whether the stack has been set. This should be true when we notice a SP
324 to FP move or if we are using the SP as the base register for storing
33b5899f 325 data, in case the FP is omitted. */
f8e3fe0d
LM
326 bool seen_stack_set = false;
327
187f5d00
YQ
328 /* Track X registers and D registers in prologue. */
329 pv_t regs[AARCH64_X_REGISTER_COUNT + AARCH64_D_REGISTER_COUNT];
07b287a0 330
187f5d00 331 for (i = 0; i < AARCH64_X_REGISTER_COUNT + AARCH64_D_REGISTER_COUNT; i++)
07b287a0 332 regs[i] = pv_register (i, 0);
f7b7ed97 333 pv_area stack (AARCH64_SP_REGNUM, gdbarch_addr_bit (gdbarch));
07b287a0
MS
334
335 for (; start < limit; start += 4)
336 {
337 uint32_t insn;
d9ebcbce 338 aarch64_inst inst;
07b287a0 339
4d9a9006 340 insn = reader.read (start, 4, byte_order_for_code);
07b287a0 341
561a72d4 342 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
d9ebcbce
YQ
343 break;
344
345 if (inst.opcode->iclass == addsub_imm
346 && (inst.opcode->op == OP_ADD
347 || strcmp ("sub", inst.opcode->name) == 0))
07b287a0 348 {
d9ebcbce
YQ
349 unsigned rd = inst.operands[0].reg.regno;
350 unsigned rn = inst.operands[1].reg.regno;
351
352 gdb_assert (aarch64_num_of_operands (inst.opcode) == 3);
353 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd_SP);
354 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rn_SP);
355 gdb_assert (inst.operands[2].type == AARCH64_OPND_AIMM);
356
357 if (inst.opcode->op == OP_ADD)
358 {
359 regs[rd] = pv_add_constant (regs[rn],
360 inst.operands[2].imm.value);
361 }
362 else
363 {
364 regs[rd] = pv_add_constant (regs[rn],
365 -inst.operands[2].imm.value);
366 }
f8e3fe0d
LM
367
368 /* Did we move SP to FP? */
369 if (rn == AARCH64_SP_REGNUM && rd == AARCH64_FP_REGNUM)
370 seen_stack_set = true;
d9ebcbce 371 }
60adf22c
TV
372 else if (inst.opcode->iclass == addsub_ext
373 && strcmp ("sub", inst.opcode->name) == 0)
374 {
375 unsigned rd = inst.operands[0].reg.regno;
376 unsigned rn = inst.operands[1].reg.regno;
377 unsigned rm = inst.operands[2].reg.regno;
378
379 gdb_assert (aarch64_num_of_operands (inst.opcode) == 3);
380 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd_SP);
381 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rn_SP);
382 gdb_assert (inst.operands[2].type == AARCH64_OPND_Rm_EXT);
383
384 regs[rd] = pv_subtract (regs[rn], regs[rm]);
385 }
d9ebcbce 386 else if (inst.opcode->iclass == branch_imm)
07b287a0
MS
387 {
388 /* Stop analysis on branch. */
389 break;
390 }
d9ebcbce 391 else if (inst.opcode->iclass == condbranch)
07b287a0
MS
392 {
393 /* Stop analysis on branch. */
394 break;
395 }
d9ebcbce 396 else if (inst.opcode->iclass == branch_reg)
07b287a0
MS
397 {
398 /* Stop analysis on branch. */
399 break;
400 }
d9ebcbce 401 else if (inst.opcode->iclass == compbranch)
07b287a0
MS
402 {
403 /* Stop analysis on branch. */
404 break;
405 }
d9ebcbce
YQ
406 else if (inst.opcode->op == OP_MOVZ)
407 {
60adf22c
TV
408 unsigned rd = inst.operands[0].reg.regno;
409
410 gdb_assert (aarch64_num_of_operands (inst.opcode) == 2);
d9ebcbce 411 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
60adf22c
TV
412 gdb_assert (inst.operands[1].type == AARCH64_OPND_HALF);
413 gdb_assert (inst.operands[1].shifter.kind == AARCH64_MOD_LSL);
f8e3fe0d
LM
414
415 /* If this shows up before we set the stack, keep going. Otherwise
416 stop the analysis. */
417 if (seen_stack_set)
418 break;
419
60adf22c
TV
420 regs[rd] = pv_constant (inst.operands[1].imm.value
421 << inst.operands[1].shifter.amount);
d9ebcbce
YQ
422 }
423 else if (inst.opcode->iclass == log_shift
424 && strcmp (inst.opcode->name, "orr") == 0)
07b287a0 425 {
d9ebcbce
YQ
426 unsigned rd = inst.operands[0].reg.regno;
427 unsigned rn = inst.operands[1].reg.regno;
428 unsigned rm = inst.operands[2].reg.regno;
429
430 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
431 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rn);
432 gdb_assert (inst.operands[2].type == AARCH64_OPND_Rm_SFT);
433
434 if (inst.operands[2].shifter.amount == 0
435 && rn == AARCH64_SP_REGNUM)
07b287a0
MS
436 regs[rd] = regs[rm];
437 else
438 {
c6185dce
SM
439 aarch64_debug_printf ("prologue analysis gave up "
440 "addr=%s opcode=0x%x (orr x register)",
441 core_addr_to_string_nz (start), insn);
442
07b287a0
MS
443 break;
444 }
445 }
d9ebcbce 446 else if (inst.opcode->op == OP_STUR)
07b287a0 447 {
d9ebcbce
YQ
448 unsigned rt = inst.operands[0].reg.regno;
449 unsigned rn = inst.operands[1].addr.base_regno;
75faf5c4 450 int size = aarch64_get_qualifier_esize (inst.operands[0].qualifier);
d9ebcbce
YQ
451
452 gdb_assert (aarch64_num_of_operands (inst.opcode) == 2);
453 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt);
454 gdb_assert (inst.operands[1].type == AARCH64_OPND_ADDR_SIMM9);
455 gdb_assert (!inst.operands[1].addr.offset.is_reg);
456
75faf5c4
AH
457 stack.store
458 (pv_add_constant (regs[rn], inst.operands[1].addr.offset.imm),
459 size, regs[rt]);
f8e3fe0d
LM
460
461 /* Are we storing with SP as a base? */
462 if (rn == AARCH64_SP_REGNUM)
463 seen_stack_set = true;
07b287a0 464 }
d9ebcbce 465 else if ((inst.opcode->iclass == ldstpair_off
03bcd739
YQ
466 || (inst.opcode->iclass == ldstpair_indexed
467 && inst.operands[2].addr.preind))
d9ebcbce 468 && strcmp ("stp", inst.opcode->name) == 0)
07b287a0 469 {
03bcd739 470 /* STP with addressing mode Pre-indexed and Base register. */
187f5d00
YQ
471 unsigned rt1;
472 unsigned rt2;
d9ebcbce
YQ
473 unsigned rn = inst.operands[2].addr.base_regno;
474 int32_t imm = inst.operands[2].addr.offset.imm;
75faf5c4 475 int size = aarch64_get_qualifier_esize (inst.operands[0].qualifier);
d9ebcbce 476
187f5d00
YQ
477 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt
478 || inst.operands[0].type == AARCH64_OPND_Ft);
479 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rt2
480 || inst.operands[1].type == AARCH64_OPND_Ft2);
d9ebcbce
YQ
481 gdb_assert (inst.operands[2].type == AARCH64_OPND_ADDR_SIMM7);
482 gdb_assert (!inst.operands[2].addr.offset.is_reg);
483
07b287a0
MS
484 /* If recording this store would invalidate the store area
485 (perhaps because rn is not known) then we should abandon
486 further prologue analysis. */
f7b7ed97 487 if (stack.store_would_trash (pv_add_constant (regs[rn], imm)))
07b287a0
MS
488 break;
489
f7b7ed97 490 if (stack.store_would_trash (pv_add_constant (regs[rn], imm + 8)))
07b287a0
MS
491 break;
492
187f5d00
YQ
493 rt1 = inst.operands[0].reg.regno;
494 rt2 = inst.operands[1].reg.regno;
495 if (inst.operands[0].type == AARCH64_OPND_Ft)
496 {
187f5d00
YQ
497 rt1 += AARCH64_X_REGISTER_COUNT;
498 rt2 += AARCH64_X_REGISTER_COUNT;
499 }
500
75faf5c4
AH
501 stack.store (pv_add_constant (regs[rn], imm), size, regs[rt1]);
502 stack.store (pv_add_constant (regs[rn], imm + size), size, regs[rt2]);
14ac654f 503
d9ebcbce 504 if (inst.operands[2].addr.writeback)
93d96012 505 regs[rn] = pv_add_constant (regs[rn], imm);
07b287a0 506
f8e3fe0d
LM
507 /* Ignore the instruction that allocates stack space and sets
508 the SP. */
509 if (rn == AARCH64_SP_REGNUM && !inst.operands[2].addr.writeback)
510 seen_stack_set = true;
07b287a0 511 }
432ec081
YQ
512 else if ((inst.opcode->iclass == ldst_imm9 /* Signed immediate. */
513 || (inst.opcode->iclass == ldst_pos /* Unsigned immediate. */
514 && (inst.opcode->op == OP_STR_POS
515 || inst.opcode->op == OP_STRF_POS)))
516 && inst.operands[1].addr.base_regno == AARCH64_SP_REGNUM
517 && strcmp ("str", inst.opcode->name) == 0)
518 {
519 /* STR (immediate) */
520 unsigned int rt = inst.operands[0].reg.regno;
521 int32_t imm = inst.operands[1].addr.offset.imm;
522 unsigned int rn = inst.operands[1].addr.base_regno;
75faf5c4 523 int size = aarch64_get_qualifier_esize (inst.operands[0].qualifier);
432ec081
YQ
524 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt
525 || inst.operands[0].type == AARCH64_OPND_Ft);
526
527 if (inst.operands[0].type == AARCH64_OPND_Ft)
75faf5c4 528 rt += AARCH64_X_REGISTER_COUNT;
432ec081 529
75faf5c4 530 stack.store (pv_add_constant (regs[rn], imm), size, regs[rt]);
432ec081
YQ
531 if (inst.operands[1].addr.writeback)
532 regs[rn] = pv_add_constant (regs[rn], imm);
f8e3fe0d
LM
533
534 /* Are we storing with SP as a base? */
535 if (rn == AARCH64_SP_REGNUM)
536 seen_stack_set = true;
432ec081 537 }
d9ebcbce 538 else if (inst.opcode->iclass == testbranch)
07b287a0
MS
539 {
540 /* Stop analysis on branch. */
541 break;
542 }
17e116a7
AH
543 else if (inst.opcode->iclass == ic_system)
544 {
345bd07c 545 aarch64_gdbarch_tdep *tdep
08106042 546 = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
17e116a7
AH
547 int ra_state_val = 0;
548
549 if (insn == 0xd503233f /* paciasp. */
550 || insn == 0xd503237f /* pacibsp. */)
551 {
552 /* Return addresses are mangled. */
553 ra_state_val = 1;
554 }
555 else if (insn == 0xd50323bf /* autiasp. */
556 || insn == 0xd50323ff /* autibsp. */)
557 {
558 /* Return addresses are not mangled. */
559 ra_state_val = 0;
560 }
37989733
LM
561 else if (IS_BTI (insn))
562 /* We don't need to do anything special for a BTI instruction. */
563 continue;
17e116a7
AH
564 else
565 {
c6185dce
SM
566 aarch64_debug_printf ("prologue analysis gave up addr=%s"
567 " opcode=0x%x (iclass)",
568 core_addr_to_string_nz (start), insn);
17e116a7
AH
569 break;
570 }
571
572 if (tdep->has_pauth () && cache != nullptr)
a9a87d35 573 {
c9cd8ca4 574 int regnum = tdep->ra_sign_state_regnum;
a9a87d35
LM
575 cache->saved_regs[regnum].set_value (ra_state_val);
576 }
17e116a7 577 }
07b287a0
MS
578 else
579 {
c6185dce
SM
580 aarch64_debug_printf ("prologue analysis gave up addr=%s"
581 " opcode=0x%x",
582 core_addr_to_string_nz (start), insn);
583
07b287a0
MS
584 break;
585 }
586 }
587
588 if (cache == NULL)
f7b7ed97 589 return start;
07b287a0
MS
590
591 if (pv_is_register (regs[AARCH64_FP_REGNUM], AARCH64_SP_REGNUM))
592 {
593 /* Frame pointer is fp. Frame size is constant. */
594 cache->framereg = AARCH64_FP_REGNUM;
595 cache->framesize = -regs[AARCH64_FP_REGNUM].k;
596 }
597 else if (pv_is_register (regs[AARCH64_SP_REGNUM], AARCH64_SP_REGNUM))
598 {
599 /* Try the stack pointer. */
600 cache->framesize = -regs[AARCH64_SP_REGNUM].k;
601 cache->framereg = AARCH64_SP_REGNUM;
602 }
603 else
604 {
605 /* We're just out of luck. We don't know where the frame is. */
606 cache->framereg = -1;
607 cache->framesize = 0;
608 }
609
610 for (i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
611 {
612 CORE_ADDR offset;
613
f7b7ed97 614 if (stack.find_reg (gdbarch, i, &offset))
098caef4 615 cache->saved_regs[i].set_addr (offset);
07b287a0
MS
616 }
617
187f5d00
YQ
618 for (i = 0; i < AARCH64_D_REGISTER_COUNT; i++)
619 {
620 int regnum = gdbarch_num_regs (gdbarch);
621 CORE_ADDR offset;
622
f7b7ed97
TT
623 if (stack.find_reg (gdbarch, i + AARCH64_X_REGISTER_COUNT,
624 &offset))
098caef4 625 cache->saved_regs[i + regnum + AARCH64_D0_REGNUM].set_addr (offset);
187f5d00
YQ
626 }
627
07b287a0
MS
628 return start;
629}
630
4d9a9006
YQ
631static CORE_ADDR
632aarch64_analyze_prologue (struct gdbarch *gdbarch,
633 CORE_ADDR start, CORE_ADDR limit,
634 struct aarch64_prologue_cache *cache)
635{
636 instruction_reader reader;
637
638 return aarch64_analyze_prologue (gdbarch, start, limit, cache,
639 reader);
640}
641
642#if GDB_SELF_TEST
643
644namespace selftests {
645
646/* Instruction reader from manually cooked instruction sequences. */
647
648class instruction_reader_test : public abstract_instruction_reader
649{
650public:
651 template<size_t SIZE>
652 explicit instruction_reader_test (const uint32_t (&insns)[SIZE])
653 : m_insns (insns), m_insns_size (SIZE)
654 {}
655
656 ULONGEST read (CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
632e107b 657 override
4d9a9006
YQ
658 {
659 SELF_CHECK (len == 4);
660 SELF_CHECK (memaddr % 4 == 0);
661 SELF_CHECK (memaddr / 4 < m_insns_size);
662
663 return m_insns[memaddr / 4];
664 }
665
666private:
667 const uint32_t *m_insns;
668 size_t m_insns_size;
669};
670
671static void
672aarch64_analyze_prologue_test (void)
673{
674 struct gdbarch_info info;
675
4d9a9006
YQ
676 info.bfd_arch_info = bfd_scan_arch ("aarch64");
677
678 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
679 SELF_CHECK (gdbarch != NULL);
680
17e116a7
AH
681 struct aarch64_prologue_cache cache;
682 cache.saved_regs = trad_frame_alloc_saved_regs (gdbarch);
683
08106042 684 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
17e116a7 685
4d9a9006
YQ
686 /* Test the simple prologue in which frame pointer is used. */
687 {
4d9a9006
YQ
688 static const uint32_t insns[] = {
689 0xa9af7bfd, /* stp x29, x30, [sp,#-272]! */
690 0x910003fd, /* mov x29, sp */
691 0x97ffffe6, /* bl 0x400580 */
692 };
693 instruction_reader_test reader (insns);
694
695 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache, reader);
696 SELF_CHECK (end == 4 * 2);
697
698 SELF_CHECK (cache.framereg == AARCH64_FP_REGNUM);
699 SELF_CHECK (cache.framesize == 272);
700
701 for (int i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
702 {
703 if (i == AARCH64_FP_REGNUM)
098caef4 704 SELF_CHECK (cache.saved_regs[i].addr () == -272);
4d9a9006 705 else if (i == AARCH64_LR_REGNUM)
098caef4 706 SELF_CHECK (cache.saved_regs[i].addr () == -264);
4d9a9006 707 else
a9a87d35
LM
708 SELF_CHECK (cache.saved_regs[i].is_realreg ()
709 && cache.saved_regs[i].realreg () == i);
4d9a9006
YQ
710 }
711
712 for (int i = 0; i < AARCH64_D_REGISTER_COUNT; i++)
713 {
a9a87d35
LM
714 int num_regs = gdbarch_num_regs (gdbarch);
715 int regnum = i + num_regs + AARCH64_D0_REGNUM;
4d9a9006 716
a9a87d35
LM
717 SELF_CHECK (cache.saved_regs[regnum].is_realreg ()
718 && cache.saved_regs[regnum].realreg () == regnum);
4d9a9006
YQ
719 }
720 }
432ec081
YQ
721
722 /* Test a prologue in which STR is used and frame pointer is not
723 used. */
724 {
432ec081
YQ
725 static const uint32_t insns[] = {
726 0xf81d0ff3, /* str x19, [sp, #-48]! */
727 0xb9002fe0, /* str w0, [sp, #44] */
728 0xf90013e1, /* str x1, [sp, #32]*/
729 0xfd000fe0, /* str d0, [sp, #24] */
730 0xaa0203f3, /* mov x19, x2 */
731 0xf94013e0, /* ldr x0, [sp, #32] */
732 };
733 instruction_reader_test reader (insns);
734
68811f8f 735 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
432ec081
YQ
736 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache, reader);
737
738 SELF_CHECK (end == 4 * 5);
739
740 SELF_CHECK (cache.framereg == AARCH64_SP_REGNUM);
741 SELF_CHECK (cache.framesize == 48);
742
743 for (int i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
744 {
745 if (i == 1)
098caef4 746 SELF_CHECK (cache.saved_regs[i].addr () == -16);
432ec081 747 else if (i == 19)
098caef4 748 SELF_CHECK (cache.saved_regs[i].addr () == -48);
432ec081 749 else
a9a87d35
LM
750 SELF_CHECK (cache.saved_regs[i].is_realreg ()
751 && cache.saved_regs[i].realreg () == i);
432ec081
YQ
752 }
753
754 for (int i = 0; i < AARCH64_D_REGISTER_COUNT; i++)
755 {
a9a87d35
LM
756 int num_regs = gdbarch_num_regs (gdbarch);
757 int regnum = i + num_regs + AARCH64_D0_REGNUM;
758
432ec081
YQ
759
760 if (i == 0)
a9a87d35 761 SELF_CHECK (cache.saved_regs[regnum].addr () == -24);
432ec081 762 else
a9a87d35
LM
763 SELF_CHECK (cache.saved_regs[regnum].is_realreg ()
764 && cache.saved_regs[regnum].realreg () == regnum);
432ec081
YQ
765 }
766 }
17e116a7 767
f8e3fe0d
LM
768 /* Test handling of movz before setting the frame pointer. */
769 {
770 static const uint32_t insns[] = {
771 0xa9bf7bfd, /* stp x29, x30, [sp, #-16]! */
772 0x52800020, /* mov w0, #0x1 */
773 0x910003fd, /* mov x29, sp */
774 0x528000a2, /* mov w2, #0x5 */
775 0x97fffff8, /* bl 6e4 */
776 };
777
778 instruction_reader_test reader (insns);
779
780 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
781 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache, reader);
782
783 /* We should stop at the 4th instruction. */
784 SELF_CHECK (end == (4 - 1) * 4);
785 SELF_CHECK (cache.framereg == AARCH64_FP_REGNUM);
786 SELF_CHECK (cache.framesize == 16);
787 }
788
789 /* Test handling of movz/stp when using the stack pointer as frame
790 pointer. */
791 {
792 static const uint32_t insns[] = {
793 0xa9bc7bfd, /* stp x29, x30, [sp, #-64]! */
794 0x52800020, /* mov w0, #0x1 */
795 0x290207e0, /* stp w0, w1, [sp, #16] */
796 0xa9018fe2, /* stp x2, x3, [sp, #24] */
797 0x528000a2, /* mov w2, #0x5 */
798 0x97fffff8, /* bl 6e4 */
799 };
800
801 instruction_reader_test reader (insns);
802
803 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
804 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache, reader);
805
806 /* We should stop at the 5th instruction. */
807 SELF_CHECK (end == (5 - 1) * 4);
808 SELF_CHECK (cache.framereg == AARCH64_SP_REGNUM);
809 SELF_CHECK (cache.framesize == 64);
810 }
811
812 /* Test handling of movz/str when using the stack pointer as frame
813 pointer */
814 {
815 static const uint32_t insns[] = {
816 0xa9bc7bfd, /* stp x29, x30, [sp, #-64]! */
817 0x52800020, /* mov w0, #0x1 */
818 0xb9002be4, /* str w4, [sp, #40] */
819 0xf9001be5, /* str x5, [sp, #48] */
820 0x528000a2, /* mov w2, #0x5 */
821 0x97fffff8, /* bl 6e4 */
822 };
823
824 instruction_reader_test reader (insns);
825
826 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
827 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache, reader);
828
829 /* We should stop at the 5th instruction. */
830 SELF_CHECK (end == (5 - 1) * 4);
831 SELF_CHECK (cache.framereg == AARCH64_SP_REGNUM);
832 SELF_CHECK (cache.framesize == 64);
833 }
834
835 /* Test handling of movz/stur when using the stack pointer as frame
836 pointer. */
837 {
838 static const uint32_t insns[] = {
839 0xa9bc7bfd, /* stp x29, x30, [sp, #-64]! */
840 0x52800020, /* mov w0, #0x1 */
841 0xb80343e6, /* stur w6, [sp, #52] */
842 0xf80383e7, /* stur x7, [sp, #56] */
843 0x528000a2, /* mov w2, #0x5 */
844 0x97fffff8, /* bl 6e4 */
845 };
846
847 instruction_reader_test reader (insns);
848
849 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
850 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache, reader);
851
852 /* We should stop at the 5th instruction. */
853 SELF_CHECK (end == (5 - 1) * 4);
854 SELF_CHECK (cache.framereg == AARCH64_SP_REGNUM);
855 SELF_CHECK (cache.framesize == 64);
856 }
857
858 /* Test handling of movz when there is no frame pointer set or no stack
859 pointer used. */
860 {
861 static const uint32_t insns[] = {
862 0xa9bf7bfd, /* stp x29, x30, [sp, #-16]! */
863 0x52800020, /* mov w0, #0x1 */
864 0x528000a2, /* mov w2, #0x5 */
865 0x97fffff8, /* bl 6e4 */
866 };
867
868 instruction_reader_test reader (insns);
869
870 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
871 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache, reader);
872
873 /* We should stop at the 4th instruction. */
874 SELF_CHECK (end == (4 - 1) * 4);
875 SELF_CHECK (cache.framereg == AARCH64_SP_REGNUM);
876 SELF_CHECK (cache.framesize == 16);
877 }
878
17e116a7
AH
879 /* Test a prologue in which there is a return address signing instruction. */
880 if (tdep->has_pauth ())
881 {
882 static const uint32_t insns[] = {
883 0xd503233f, /* paciasp */
884 0xa9bd7bfd, /* stp x29, x30, [sp, #-48]! */
885 0x910003fd, /* mov x29, sp */
886 0xf801c3f3, /* str x19, [sp, #28] */
887 0xb9401fa0, /* ldr x19, [x29, #28] */
888 };
889 instruction_reader_test reader (insns);
890
68811f8f 891 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
17e116a7
AH
892 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache,
893 reader);
894
895 SELF_CHECK (end == 4 * 4);
896 SELF_CHECK (cache.framereg == AARCH64_FP_REGNUM);
897 SELF_CHECK (cache.framesize == 48);
898
899 for (int i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
900 {
901 if (i == 19)
098caef4 902 SELF_CHECK (cache.saved_regs[i].addr () == -20);
17e116a7 903 else if (i == AARCH64_FP_REGNUM)
098caef4 904 SELF_CHECK (cache.saved_regs[i].addr () == -48);
17e116a7 905 else if (i == AARCH64_LR_REGNUM)
098caef4 906 SELF_CHECK (cache.saved_regs[i].addr () == -40);
17e116a7 907 else
a9a87d35
LM
908 SELF_CHECK (cache.saved_regs[i].is_realreg ()
909 && cache.saved_regs[i].realreg () == i);
17e116a7
AH
910 }
911
912 if (tdep->has_pauth ())
913 {
c9cd8ca4 914 int regnum = tdep->ra_sign_state_regnum;
a9a87d35 915 SELF_CHECK (cache.saved_regs[regnum].is_value ());
17e116a7
AH
916 }
917 }
37989733
LM
918
919 /* Test a prologue with a BTI instruction. */
920 {
921 static const uint32_t insns[] = {
922 0xd503245f, /* bti */
923 0xa9bd7bfd, /* stp x29, x30, [sp, #-48]! */
924 0x910003fd, /* mov x29, sp */
925 0xf801c3f3, /* str x19, [sp, #28] */
926 0xb9401fa0, /* ldr x19, [x29, #28] */
927 };
928 instruction_reader_test reader (insns);
929
930 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
931 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache,
932 reader);
933
934 SELF_CHECK (end == 4 * 4);
935 SELF_CHECK (cache.framereg == AARCH64_FP_REGNUM);
936 SELF_CHECK (cache.framesize == 48);
937
938 for (int i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
939 {
940 if (i == 19)
941 SELF_CHECK (cache.saved_regs[i].addr () == -20);
942 else if (i == AARCH64_FP_REGNUM)
943 SELF_CHECK (cache.saved_regs[i].addr () == -48);
944 else if (i == AARCH64_LR_REGNUM)
945 SELF_CHECK (cache.saved_regs[i].addr () == -40);
946 else
947 SELF_CHECK (cache.saved_regs[i].is_realreg ()
948 && cache.saved_regs[i].realreg () == i);
949 }
950 }
4d9a9006 951}
b6fb76ec 952} /* namespace selftests */
4d9a9006
YQ
953#endif /* GDB_SELF_TEST */
954
07b287a0
MS
955/* Implement the "skip_prologue" gdbarch method. */
956
957static CORE_ADDR
958aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
959{
22f2cf64 960 CORE_ADDR func_addr, func_end_addr, limit_pc;
07b287a0
MS
961
962 /* See if we can determine the end of the prologue via the symbol
963 table. If so, then return either PC, or the PC after the
964 prologue, whichever is greater. */
22f2cf64
TV
965 bool func_addr_found
966 = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
967
968 if (func_addr_found)
07b287a0
MS
969 {
970 CORE_ADDR post_prologue_pc
971 = skip_prologue_using_sal (gdbarch, func_addr);
972
973 if (post_prologue_pc != 0)
325fac50 974 return std::max (pc, post_prologue_pc);
07b287a0
MS
975 }
976
977 /* Can't determine prologue from the symbol table, need to examine
978 instructions. */
979
980 /* Find an upper limit on the function prologue using the debug
981 information. If the debug information could not be used to
982 provide that bound, then use an arbitrary large number as the
983 upper bound. */
984 limit_pc = skip_prologue_using_sal (gdbarch, pc);
985 if (limit_pc == 0)
986 limit_pc = pc + 128; /* Magic. */
987
22f2cf64 988 limit_pc
05d63baf 989 = func_end_addr == 0 ? limit_pc : std::min (limit_pc, func_end_addr - 4);
22f2cf64 990
07b287a0
MS
991 /* Try disassembling prologue. */
992 return aarch64_analyze_prologue (gdbarch, pc, limit_pc, NULL);
993}
994
995/* Scan the function prologue for THIS_FRAME and populate the prologue
996 cache CACHE. */
997
998static void
8480a37e 999aarch64_scan_prologue (const frame_info_ptr &this_frame,
07b287a0
MS
1000 struct aarch64_prologue_cache *cache)
1001{
1002 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1003 CORE_ADDR prologue_start;
1004 CORE_ADDR prologue_end;
1005 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1006 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1007
db634143
PL
1008 cache->prev_pc = prev_pc;
1009
07b287a0
MS
1010 /* Assume we do not find a frame. */
1011 cache->framereg = -1;
1012 cache->framesize = 0;
1013
1014 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1015 &prologue_end))
1016 {
1017 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1018
1019 if (sal.line == 0)
1020 {
1021 /* No line info so use the current PC. */
1022 prologue_end = prev_pc;
1023 }
1024 else if (sal.end < prologue_end)
1025 {
1026 /* The next line begins after the function end. */
1027 prologue_end = sal.end;
1028 }
1029
325fac50 1030 prologue_end = std::min (prologue_end, prev_pc);
07b287a0
MS
1031 aarch64_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1032 }
1033 else
1034 {
1035 CORE_ADDR frame_loc;
07b287a0
MS
1036
1037 frame_loc = get_frame_register_unsigned (this_frame, AARCH64_FP_REGNUM);
1038 if (frame_loc == 0)
1039 return;
1040
1041 cache->framereg = AARCH64_FP_REGNUM;
1042 cache->framesize = 16;
098caef4
LM
1043 cache->saved_regs[29].set_addr (0);
1044 cache->saved_regs[30].set_addr (8);
07b287a0
MS
1045 }
1046}
1047
7dfa3edc
PL
1048/* Fill in *CACHE with information about the prologue of *THIS_FRAME. This
1049 function may throw an exception if the inferior's registers or memory is
1050 not available. */
07b287a0 1051
7dfa3edc 1052static void
8480a37e 1053aarch64_make_prologue_cache_1 (const frame_info_ptr &this_frame,
7dfa3edc 1054 struct aarch64_prologue_cache *cache)
07b287a0 1055{
07b287a0
MS
1056 CORE_ADDR unwound_fp;
1057 int reg;
1058
07b287a0
MS
1059 aarch64_scan_prologue (this_frame, cache);
1060
1061 if (cache->framereg == -1)
7dfa3edc 1062 return;
07b287a0
MS
1063
1064 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
1065 if (unwound_fp == 0)
7dfa3edc 1066 return;
07b287a0 1067
29e09a42
TV
1068 cache->prev_sp = unwound_fp;
1069 if (!aarch64_stack_frame_destroyed_p (get_frame_arch (this_frame),
1070 cache->prev_pc))
1071 cache->prev_sp += cache->framesize;
07b287a0
MS
1072
1073 /* Calculate actual addresses of saved registers using offsets
1074 determined by aarch64_analyze_prologue. */
1075 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
a9a87d35 1076 if (cache->saved_regs[reg].is_addr ())
098caef4
LM
1077 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
1078 + cache->prev_sp);
07b287a0 1079
db634143
PL
1080 cache->func = get_frame_func (this_frame);
1081
7dfa3edc
PL
1082 cache->available_p = 1;
1083}
1084
1085/* Allocate and fill in *THIS_CACHE with information about the prologue of
1086 *THIS_FRAME. Do not do this is if *THIS_CACHE was already allocated.
1087 Return a pointer to the current aarch64_prologue_cache in
1088 *THIS_CACHE. */
1089
1090static struct aarch64_prologue_cache *
8480a37e 1091aarch64_make_prologue_cache (const frame_info_ptr &this_frame, void **this_cache)
7dfa3edc
PL
1092{
1093 struct aarch64_prologue_cache *cache;
1094
1095 if (*this_cache != NULL)
9a3c8263 1096 return (struct aarch64_prologue_cache *) *this_cache;
7dfa3edc
PL
1097
1098 cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
1099 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1100 *this_cache = cache;
1101
a70b8144 1102 try
7dfa3edc
PL
1103 {
1104 aarch64_make_prologue_cache_1 (this_frame, cache);
1105 }
230d2906 1106 catch (const gdb_exception_error &ex)
7dfa3edc
PL
1107 {
1108 if (ex.error != NOT_AVAILABLE_ERROR)
eedc3f4f 1109 throw;
7dfa3edc 1110 }
7dfa3edc 1111
07b287a0
MS
1112 return cache;
1113}
1114
7dfa3edc
PL
1115/* Implement the "stop_reason" frame_unwind method. */
1116
1117static enum unwind_stop_reason
8480a37e 1118aarch64_prologue_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
7dfa3edc
PL
1119 void **this_cache)
1120{
1121 struct aarch64_prologue_cache *cache
1122 = aarch64_make_prologue_cache (this_frame, this_cache);
1123
1124 if (!cache->available_p)
1125 return UNWIND_UNAVAILABLE;
1126
1127 /* Halt the backtrace at "_start". */
345bd07c 1128 gdbarch *arch = get_frame_arch (this_frame);
08106042 1129 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (arch);
345bd07c 1130 if (cache->prev_pc <= tdep->lowest_pc)
7dfa3edc
PL
1131 return UNWIND_OUTERMOST;
1132
1133 /* We've hit a wall, stop. */
1134 if (cache->prev_sp == 0)
1135 return UNWIND_OUTERMOST;
1136
1137 return UNWIND_NO_REASON;
1138}
1139
07b287a0
MS
1140/* Our frame ID for a normal frame is the current function's starting
1141 PC and the caller's SP when we were called. */
1142
1143static void
8480a37e 1144aarch64_prologue_this_id (const frame_info_ptr &this_frame,
07b287a0
MS
1145 void **this_cache, struct frame_id *this_id)
1146{
7c8edfae
PL
1147 struct aarch64_prologue_cache *cache
1148 = aarch64_make_prologue_cache (this_frame, this_cache);
07b287a0 1149
7dfa3edc
PL
1150 if (!cache->available_p)
1151 *this_id = frame_id_build_unavailable_stack (cache->func);
1152 else
1153 *this_id = frame_id_build (cache->prev_sp, cache->func);
07b287a0
MS
1154}
1155
1156/* Implement the "prev_register" frame_unwind method. */
1157
1158static struct value *
8480a37e 1159aarch64_prologue_prev_register (const frame_info_ptr &this_frame,
07b287a0
MS
1160 void **this_cache, int prev_regnum)
1161{
7c8edfae
PL
1162 struct aarch64_prologue_cache *cache
1163 = aarch64_make_prologue_cache (this_frame, this_cache);
07b287a0
MS
1164
1165 /* If we are asked to unwind the PC, then we need to return the LR
1166 instead. The prologue may save PC, but it will point into this
1167 frame's prologue, not the next frame's resume location. */
1168 if (prev_regnum == AARCH64_PC_REGNUM)
1169 {
1170 CORE_ADDR lr;
17e116a7 1171 struct gdbarch *gdbarch = get_frame_arch (this_frame);
345bd07c 1172 aarch64_gdbarch_tdep *tdep
08106042 1173 = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0
MS
1174
1175 lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
17e116a7
AH
1176
1177 if (tdep->has_pauth ()
c9cd8ca4 1178 && cache->saved_regs[tdep->ra_sign_state_regnum].is_value ())
3d31bc39 1179 lr = aarch64_frame_unmask_lr (tdep, this_frame, lr);
17e116a7 1180
07b287a0
MS
1181 return frame_unwind_got_constant (this_frame, prev_regnum, lr);
1182 }
1183
1184 /* SP is generally not saved to the stack, but this frame is
1185 identified by the next frame's stack pointer at the time of the
1186 call. The value was already reconstructed into PREV_SP. */
1187 /*
dda83cd7
SM
1188 +----------+ ^
1189 | saved lr | |
07b287a0
MS
1190 +->| saved fp |--+
1191 | | |
1192 | | | <- Previous SP
1193 | +----------+
1194 | | saved lr |
1195 +--| saved fp |<- FP
dda83cd7
SM
1196 | |
1197 | |<- SP
1198 +----------+ */
07b287a0
MS
1199 if (prev_regnum == AARCH64_SP_REGNUM)
1200 return frame_unwind_got_constant (this_frame, prev_regnum,
1201 cache->prev_sp);
1202
1203 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1204 prev_regnum);
1205}
1206
1207/* AArch64 prologue unwinder. */
1239e7cf 1208static const frame_unwind_legacy aarch64_prologue_unwind (
a154d838 1209 "aarch64 prologue",
07b287a0 1210 NORMAL_FRAME,
ce36ef63 1211 FRAME_UNWIND_ARCH,
7dfa3edc 1212 aarch64_prologue_frame_unwind_stop_reason,
07b287a0
MS
1213 aarch64_prologue_this_id,
1214 aarch64_prologue_prev_register,
1215 NULL,
1216 default_frame_sniffer
1239e7cf 1217);
07b287a0 1218
8b61f75d
PL
1219/* Allocate and fill in *THIS_CACHE with information about the prologue of
1220 *THIS_FRAME. Do not do this is if *THIS_CACHE was already allocated.
1221 Return a pointer to the current aarch64_prologue_cache in
1222 *THIS_CACHE. */
07b287a0
MS
1223
1224static struct aarch64_prologue_cache *
8480a37e 1225aarch64_make_stub_cache (const frame_info_ptr &this_frame, void **this_cache)
07b287a0 1226{
07b287a0 1227 struct aarch64_prologue_cache *cache;
8b61f75d
PL
1228
1229 if (*this_cache != NULL)
9a3c8263 1230 return (struct aarch64_prologue_cache *) *this_cache;
07b287a0
MS
1231
1232 cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
1233 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
8b61f75d 1234 *this_cache = cache;
07b287a0 1235
a70b8144 1236 try
02a2a705
PL
1237 {
1238 cache->prev_sp = get_frame_register_unsigned (this_frame,
1239 AARCH64_SP_REGNUM);
1240 cache->prev_pc = get_frame_pc (this_frame);
1241 cache->available_p = 1;
1242 }
230d2906 1243 catch (const gdb_exception_error &ex)
02a2a705
PL
1244 {
1245 if (ex.error != NOT_AVAILABLE_ERROR)
eedc3f4f 1246 throw;
02a2a705 1247 }
07b287a0
MS
1248
1249 return cache;
1250}
1251
02a2a705
PL
1252/* Implement the "stop_reason" frame_unwind method. */
1253
1254static enum unwind_stop_reason
8480a37e 1255aarch64_stub_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
02a2a705
PL
1256 void **this_cache)
1257{
1258 struct aarch64_prologue_cache *cache
1259 = aarch64_make_stub_cache (this_frame, this_cache);
1260
1261 if (!cache->available_p)
1262 return UNWIND_UNAVAILABLE;
1263
1264 return UNWIND_NO_REASON;
1265}
1266
07b287a0
MS
1267/* Our frame ID for a stub frame is the current SP and LR. */
1268
1269static void
8480a37e 1270aarch64_stub_this_id (const frame_info_ptr &this_frame,
07b287a0
MS
1271 void **this_cache, struct frame_id *this_id)
1272{
8b61f75d
PL
1273 struct aarch64_prologue_cache *cache
1274 = aarch64_make_stub_cache (this_frame, this_cache);
07b287a0 1275
02a2a705
PL
1276 if (cache->available_p)
1277 *this_id = frame_id_build (cache->prev_sp, cache->prev_pc);
1278 else
1279 *this_id = frame_id_build_unavailable_stack (cache->prev_pc);
07b287a0
MS
1280}
1281
1282/* Implement the "sniffer" frame_unwind method. */
1283
1284static int
1285aarch64_stub_unwind_sniffer (const struct frame_unwind *self,
8480a37e 1286 const frame_info_ptr &this_frame,
07b287a0
MS
1287 void **this_prologue_cache)
1288{
1289 CORE_ADDR addr_in_block;
1290 gdb_byte dummy[4];
1291
1292 addr_in_block = get_frame_address_in_block (this_frame);
3e5d3a5a 1293 if (in_plt_section (addr_in_block)
07b287a0
MS
1294 /* We also use the stub winder if the target memory is unreadable
1295 to avoid having the prologue unwinder trying to read it. */
1296 || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
1297 return 1;
1298
1299 return 0;
1300}
1301
1302/* AArch64 stub unwinder. */
1239e7cf 1303static const frame_unwind_legacy aarch64_stub_unwind (
a154d838 1304 "aarch64 stub",
07b287a0 1305 NORMAL_FRAME,
ce36ef63 1306 FRAME_UNWIND_ARCH,
02a2a705 1307 aarch64_stub_frame_unwind_stop_reason,
07b287a0
MS
1308 aarch64_stub_this_id,
1309 aarch64_prologue_prev_register,
1310 NULL,
1311 aarch64_stub_unwind_sniffer
1239e7cf 1312);
07b287a0
MS
1313
1314/* Return the frame base address of *THIS_FRAME. */
1315
1316static CORE_ADDR
8480a37e 1317aarch64_normal_frame_base (const frame_info_ptr &this_frame, void **this_cache)
07b287a0 1318{
7c8edfae
PL
1319 struct aarch64_prologue_cache *cache
1320 = aarch64_make_prologue_cache (this_frame, this_cache);
07b287a0
MS
1321
1322 return cache->prev_sp - cache->framesize;
1323}
1324
1325/* AArch64 default frame base information. */
6bd434d6 1326static frame_base aarch64_normal_base =
07b287a0
MS
1327{
1328 &aarch64_prologue_unwind,
1329 aarch64_normal_frame_base,
1330 aarch64_normal_frame_base,
1331 aarch64_normal_frame_base
1332};
1333
07b287a0
MS
1334/* Return the value of the REGNUM register in the previous frame of
1335 *THIS_FRAME. */
1336
1337static struct value *
8480a37e 1338aarch64_dwarf2_prev_register (const frame_info_ptr &this_frame,
07b287a0
MS
1339 void **this_cache, int regnum)
1340{
345bd07c 1341 gdbarch *arch = get_frame_arch (this_frame);
08106042 1342 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (arch);
07b287a0
MS
1343 CORE_ADDR lr;
1344
1345 switch (regnum)
1346 {
1347 case AARCH64_PC_REGNUM:
1348 lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
3d31bc39 1349 lr = aarch64_frame_unmask_lr (tdep, this_frame, lr);
07b287a0
MS
1350 return frame_unwind_got_constant (this_frame, regnum, lr);
1351
1352 default:
f34652de 1353 internal_error (_("Unexpected register %d"), regnum);
07b287a0
MS
1354 }
1355}
1356
11e1b75f
AH
1357static const unsigned char op_lit0 = DW_OP_lit0;
1358static const unsigned char op_lit1 = DW_OP_lit1;
1359
07b287a0
MS
1360/* Implement the "init_reg" dwarf2_frame_ops method. */
1361
1362static void
1363aarch64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1364 struct dwarf2_frame_state_reg *reg,
8480a37e 1365 const frame_info_ptr &this_frame)
07b287a0 1366{
08106042 1367 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
11e1b75f 1368
07b287a0
MS
1369 switch (regnum)
1370 {
1371 case AARCH64_PC_REGNUM:
1372 reg->how = DWARF2_FRAME_REG_FN;
1373 reg->loc.fn = aarch64_dwarf2_prev_register;
11e1b75f
AH
1374 return;
1375
07b287a0
MS
1376 case AARCH64_SP_REGNUM:
1377 reg->how = DWARF2_FRAME_REG_CFA;
11e1b75f
AH
1378 return;
1379 }
1380
1381 /* Init pauth registers. */
1382 if (tdep->has_pauth ())
1383 {
c9cd8ca4 1384 if (regnum == tdep->ra_sign_state_regnum)
11e1b75f
AH
1385 {
1386 /* Initialize RA_STATE to zero. */
1387 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
1388 reg->loc.exp.start = &op_lit0;
1389 reg->loc.exp.len = 1;
1390 return;
1391 }
6d002087
LM
1392 else if (regnum >= tdep->pauth_reg_base
1393 && regnum < tdep->pauth_reg_base + tdep->pauth_reg_count)
11e1b75f
AH
1394 {
1395 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1396 return;
1397 }
07b287a0
MS
1398 }
1399}
1400
11e1b75f
AH
1401/* Implement the execute_dwarf_cfa_vendor_op method. */
1402
1403static bool
1404aarch64_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
1405 struct dwarf2_frame_state *fs)
1406{
08106042 1407 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
11e1b75f
AH
1408 struct dwarf2_frame_state_reg *ra_state;
1409
8fca4da0 1410 if (op == DW_CFA_AARCH64_negate_ra_state)
11e1b75f 1411 {
8fca4da0
AH
1412 /* On systems without pauth, treat as a nop. */
1413 if (!tdep->has_pauth ())
1414 return true;
1415
11e1b75f 1416 /* Allocate RA_STATE column if it's not allocated yet. */
c9cd8ca4 1417 fs->regs.alloc_regs (AARCH64_DWARF_RA_SIGN_STATE + 1);
11e1b75f
AH
1418
1419 /* Toggle the status of RA_STATE between 0 and 1. */
c9cd8ca4 1420 ra_state = &(fs->regs.reg[AARCH64_DWARF_RA_SIGN_STATE]);
11e1b75f
AH
1421 ra_state->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
1422
1423 if (ra_state->loc.exp.start == nullptr
1424 || ra_state->loc.exp.start == &op_lit0)
1425 ra_state->loc.exp.start = &op_lit1;
1426 else
1427 ra_state->loc.exp.start = &op_lit0;
1428
1429 ra_state->loc.exp.len = 1;
1430
1431 return true;
1432 }
1433
1434 return false;
1435}
1436
5133a315
LM
1437/* Used for matching BRK instructions for AArch64. */
1438static constexpr uint32_t BRK_INSN_MASK = 0xffe0001f;
1439static constexpr uint32_t BRK_INSN_BASE = 0xd4200000;
1440
1441/* Implementation of gdbarch_program_breakpoint_here_p for aarch64. */
1442
1443static bool
1444aarch64_program_breakpoint_here_p (gdbarch *gdbarch, CORE_ADDR address)
1445{
1446 const uint32_t insn_len = 4;
1447 gdb_byte target_mem[4];
1448
1449 /* Enable the automatic memory restoration from breakpoints while
1450 we read the memory. Otherwise we may find temporary breakpoints, ones
1451 inserted by GDB, and flag them as permanent breakpoints. */
1452 scoped_restore restore_memory
1453 = make_scoped_restore_show_memory_breakpoints (0);
1454
1455 if (target_read_memory (address, target_mem, insn_len) == 0)
1456 {
1457 uint32_t insn =
1458 (uint32_t) extract_unsigned_integer (target_mem, insn_len,
1459 gdbarch_byte_order_for_code (gdbarch));
1460
1461 /* Check if INSN is a BRK instruction pattern. There are multiple choices
1462 of such instructions with different immediate values. Different OS'
1463 may use a different variation, but they have the same outcome. */
1464 return ((insn & BRK_INSN_MASK) == BRK_INSN_BASE);
1465 }
1466
1467 return false;
1468}
1469
07b287a0
MS
1470/* When arguments must be pushed onto the stack, they go on in reverse
1471 order. The code below implements a FILO (stack) to do this. */
1472
89055eaa 1473struct stack_item_t
07b287a0 1474{
c3c87445
YQ
1475 /* Value to pass on stack. It can be NULL if this item is for stack
1476 padding. */
7c543f7b 1477 const gdb_byte *data;
07b287a0
MS
1478
1479 /* Size in bytes of value to pass on stack. */
1480 int len;
89055eaa 1481};
07b287a0 1482
b907456c
AB
1483/* Implement the gdbarch type alignment method, overrides the generic
1484 alignment algorithm for anything that is aarch64 specific. */
07b287a0 1485
b907456c
AB
1486static ULONGEST
1487aarch64_type_align (gdbarch *gdbarch, struct type *t)
07b287a0 1488{
07b287a0 1489 t = check_typedef (t);
bd63c870 1490 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
07b287a0 1491 {
b907456c
AB
1492 /* Use the natural alignment for vector types (the same for
1493 scalar type), but the maximum alignment is 128-bit. */
df86565b 1494 if (t->length () > 16)
b907456c 1495 return 16;
238f2452 1496 else
df86565b 1497 return t->length ();
07b287a0 1498 }
b907456c
AB
1499
1500 /* Allow the common code to calculate the alignment. */
1501 return 0;
07b287a0
MS
1502}
1503
ea92689a
AH
1504/* Worker function for aapcs_is_vfp_call_or_return_candidate.
1505
1506 Return the number of register required, or -1 on failure.
1507
1508 When encountering a base element, if FUNDAMENTAL_TYPE is not set then set it
1509 to the element, else fail if the type of this element does not match the
1510 existing value. */
1511
1512static int
1513aapcs_is_vfp_call_or_return_candidate_1 (struct type *type,
1514 struct type **fundamental_type)
1515{
1516 if (type == nullptr)
1517 return -1;
1518
78134374 1519 switch (type->code ())
ea92689a
AH
1520 {
1521 case TYPE_CODE_FLT:
81657e58 1522 case TYPE_CODE_DECFLOAT:
df86565b 1523 if (type->length () > 16)
ea92689a
AH
1524 return -1;
1525
1526 if (*fundamental_type == nullptr)
1527 *fundamental_type = type;
df86565b 1528 else if (type->length () != (*fundamental_type)->length ()
78134374 1529 || type->code () != (*fundamental_type)->code ())
ea92689a
AH
1530 return -1;
1531
1532 return 1;
1533
1534 case TYPE_CODE_COMPLEX:
1535 {
27710edb 1536 struct type *target_type = check_typedef (type->target_type ());
df86565b 1537 if (target_type->length () > 16)
ea92689a
AH
1538 return -1;
1539
1540 if (*fundamental_type == nullptr)
1541 *fundamental_type = target_type;
df86565b 1542 else if (target_type->length () != (*fundamental_type)->length ()
78134374 1543 || target_type->code () != (*fundamental_type)->code ())
ea92689a
AH
1544 return -1;
1545
1546 return 2;
1547 }
1548
1549 case TYPE_CODE_ARRAY:
1550 {
bd63c870 1551 if (type->is_vector ())
ea92689a 1552 {
df86565b 1553 if (type->length () != 8 && type->length () != 16)
ea92689a
AH
1554 return -1;
1555
1556 if (*fundamental_type == nullptr)
1557 *fundamental_type = type;
df86565b 1558 else if (type->length () != (*fundamental_type)->length ()
78134374 1559 || type->code () != (*fundamental_type)->code ())
ea92689a
AH
1560 return -1;
1561
1562 return 1;
1563 }
1564 else
1565 {
27710edb 1566 struct type *target_type = type->target_type ();
ea92689a
AH
1567 int count = aapcs_is_vfp_call_or_return_candidate_1
1568 (target_type, fundamental_type);
1569
1570 if (count == -1)
1571 return count;
1572
df86565b 1573 count *= (type->length () / target_type->length ());
ea92689a
AH
1574 return count;
1575 }
1576 }
1577
1578 case TYPE_CODE_STRUCT:
1579 case TYPE_CODE_UNION:
1580 {
1581 int count = 0;
1582
1f704f76 1583 for (int i = 0; i < type->num_fields (); i++)
ea92689a 1584 {
353229bf 1585 /* Ignore any static fields. */
c819a338 1586 if (type->field (i).is_static ())
353229bf
AH
1587 continue;
1588
940da03e 1589 struct type *member = check_typedef (type->field (i).type ());
ea92689a
AH
1590
1591 int sub_count = aapcs_is_vfp_call_or_return_candidate_1
1592 (member, fundamental_type);
1593 if (sub_count == -1)
1594 return -1;
1595 count += sub_count;
1596 }
73021deb
AH
1597
1598 /* Ensure there is no padding between the fields (allowing for empty
1599 zero length structs) */
1600 int ftype_length = (*fundamental_type == nullptr)
df86565b
SM
1601 ? 0 : (*fundamental_type)->length ();
1602 if (count * ftype_length != type->length ())
73021deb
AH
1603 return -1;
1604
ea92689a
AH
1605 return count;
1606 }
1607
1608 default:
1609 break;
1610 }
1611
1612 return -1;
1613}
1614
1615/* Return true if an argument, whose type is described by TYPE, can be passed or
1616 returned in simd/fp registers, providing enough parameter passing registers
1617 are available. This is as described in the AAPCS64.
1618
1619 Upon successful return, *COUNT returns the number of needed registers,
1620 *FUNDAMENTAL_TYPE contains the type of those registers.
1621
1622 Candidate as per the AAPCS64 5.4.2.C is either a:
1623 - float.
1624 - short-vector.
1625 - HFA (Homogeneous Floating-point Aggregate, 4.3.5.1). A Composite type where
1626 all the members are floats and has at most 4 members.
1627 - HVA (Homogeneous Short-vector Aggregate, 4.3.5.2). A Composite type where
1628 all the members are short vectors and has at most 4 members.
1629 - Complex (7.1.1)
1630
1631 Note that HFAs and HVAs can include nested structures and arrays. */
1632
0e745c60 1633static bool
ea92689a
AH
1634aapcs_is_vfp_call_or_return_candidate (struct type *type, int *count,
1635 struct type **fundamental_type)
1636{
1637 if (type == nullptr)
1638 return false;
1639
1640 *fundamental_type = nullptr;
1641
1642 int ag_count = aapcs_is_vfp_call_or_return_candidate_1 (type,
1643 fundamental_type);
1644
1645 if (ag_count > 0 && ag_count <= HA_MAX_NUM_FLDS)
1646 {
1647 *count = ag_count;
1648 return true;
1649 }
1650 else
1651 return false;
1652}
1653
07b287a0
MS
1654/* AArch64 function call information structure. */
1655struct aarch64_call_info
1656{
1657 /* the current argument number. */
89055eaa 1658 unsigned argnum = 0;
07b287a0
MS
1659
1660 /* The next general purpose register number, equivalent to NGRN as
1661 described in the AArch64 Procedure Call Standard. */
89055eaa 1662 unsigned ngrn = 0;
07b287a0
MS
1663
1664 /* The next SIMD and floating point register number, equivalent to
1665 NSRN as described in the AArch64 Procedure Call Standard. */
89055eaa 1666 unsigned nsrn = 0;
07b287a0
MS
1667
1668 /* The next stacked argument address, equivalent to NSAA as
1669 described in the AArch64 Procedure Call Standard. */
89055eaa 1670 unsigned nsaa = 0;
07b287a0
MS
1671
1672 /* Stack item vector. */
89055eaa 1673 std::vector<stack_item_t> si;
07b287a0
MS
1674};
1675
1676/* Pass a value in a sequence of consecutive X registers. The caller
30baf67b 1677 is responsible for ensuring sufficient registers are available. */
07b287a0
MS
1678
1679static void
1680pass_in_x (struct gdbarch *gdbarch, struct regcache *regcache,
1681 struct aarch64_call_info *info, struct type *type,
8e80f9d1 1682 struct value *arg)
07b287a0
MS
1683{
1684 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
df86565b 1685 int len = type->length ();
78134374 1686 enum type_code typecode = type->code ();
07b287a0 1687 int regnum = AARCH64_X0_REGNUM + info->ngrn;
efaf1ae0 1688 const bfd_byte *buf = arg->contents ().data ();
07b287a0
MS
1689
1690 info->argnum++;
1691
1692 while (len > 0)
1693 {
1694 int partial_len = len < X_REGISTER_SIZE ? len : X_REGISTER_SIZE;
1695 CORE_ADDR regval = extract_unsigned_integer (buf, partial_len,
1696 byte_order);
1697
1698
1699 /* Adjust sub-word struct/union args when big-endian. */
1700 if (byte_order == BFD_ENDIAN_BIG
1701 && partial_len < X_REGISTER_SIZE
1702 && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
1703 regval <<= ((X_REGISTER_SIZE - partial_len) * TARGET_CHAR_BIT);
1704
c6185dce
SM
1705 aarch64_debug_printf ("arg %d in %s = 0x%s", info->argnum,
1706 gdbarch_register_name (gdbarch, regnum),
1707 phex (regval, X_REGISTER_SIZE));
1708
07b287a0
MS
1709 regcache_cooked_write_unsigned (regcache, regnum, regval);
1710 len -= partial_len;
1711 buf += partial_len;
1712 regnum++;
1713 }
1714}
1715
1716/* Attempt to marshall a value in a V register. Return 1 if
1717 successful, or 0 if insufficient registers are available. This
1718 function, unlike the equivalent pass_in_x() function does not
1719 handle arguments spread across multiple registers. */
1720
1721static int
1722pass_in_v (struct gdbarch *gdbarch,
1723 struct regcache *regcache,
1724 struct aarch64_call_info *info,
0735fddd 1725 int len, const bfd_byte *buf)
07b287a0
MS
1726{
1727 if (info->nsrn < 8)
1728 {
07b287a0 1729 int regnum = AARCH64_V0_REGNUM + info->nsrn;
3ff2c72e 1730 /* Enough space for a full vector register. */
d724d71a
SM
1731 gdb::byte_vector reg (register_size (gdbarch, regnum), 0);
1732 gdb_assert (len <= reg.size ());
07b287a0
MS
1733
1734 info->argnum++;
1735 info->nsrn++;
1736
0735fddd
YQ
1737 /* PCS C.1, the argument is allocated to the least significant
1738 bits of V register. */
d724d71a 1739 memcpy (reg.data (), buf, len);
b66f5587 1740 regcache->cooked_write (regnum, reg);
0735fddd 1741
c6185dce
SM
1742 aarch64_debug_printf ("arg %d in %s", info->argnum,
1743 gdbarch_register_name (gdbarch, regnum));
1744
07b287a0
MS
1745 return 1;
1746 }
1747 info->nsrn = 8;
1748 return 0;
1749}
1750
1751/* Marshall an argument onto the stack. */
1752
1753static void
1754pass_on_stack (struct aarch64_call_info *info, struct type *type,
8e80f9d1 1755 struct value *arg)
07b287a0 1756{
efaf1ae0 1757 const bfd_byte *buf = arg->contents ().data ();
df86565b 1758 int len = type->length ();
07b287a0
MS
1759 int align;
1760 stack_item_t item;
1761
1762 info->argnum++;
1763
b907456c 1764 align = type_align (type);
07b287a0
MS
1765
1766 /* PCS C.17 Stack should be aligned to the larger of 8 bytes or the
1767 Natural alignment of the argument's type. */
1768 align = align_up (align, 8);
1769
1770 /* The AArch64 PCS requires at most doubleword alignment. */
1771 if (align > 16)
1772 align = 16;
1773
c6185dce
SM
1774 aarch64_debug_printf ("arg %d len=%d @ sp + %d\n", info->argnum, len,
1775 info->nsaa);
07b287a0
MS
1776
1777 item.len = len;
1778 item.data = buf;
89055eaa 1779 info->si.push_back (item);
07b287a0
MS
1780
1781 info->nsaa += len;
1782 if (info->nsaa & (align - 1))
1783 {
1784 /* Push stack alignment padding. */
1785 int pad = align - (info->nsaa & (align - 1));
1786
1787 item.len = pad;
c3c87445 1788 item.data = NULL;
07b287a0 1789
89055eaa 1790 info->si.push_back (item);
07b287a0
MS
1791 info->nsaa += pad;
1792 }
1793}
1794
1795/* Marshall an argument into a sequence of one or more consecutive X
1796 registers or, if insufficient X registers are available then onto
1797 the stack. */
1798
1799static void
1800pass_in_x_or_stack (struct gdbarch *gdbarch, struct regcache *regcache,
1801 struct aarch64_call_info *info, struct type *type,
8e80f9d1 1802 struct value *arg)
07b287a0 1803{
df86565b 1804 int len = type->length ();
07b287a0
MS
1805 int nregs = (len + X_REGISTER_SIZE - 1) / X_REGISTER_SIZE;
1806
1807 /* PCS C.13 - Pass in registers if we have enough spare */
1808 if (info->ngrn + nregs <= 8)
1809 {
8e80f9d1 1810 pass_in_x (gdbarch, regcache, info, type, arg);
07b287a0
MS
1811 info->ngrn += nregs;
1812 }
1813 else
1814 {
1815 info->ngrn = 8;
8e80f9d1 1816 pass_on_stack (info, type, arg);
07b287a0
MS
1817 }
1818}
1819
0e745c60
AH
1820/* Pass a value, which is of type arg_type, in a V register. Assumes value is a
1821 aapcs_is_vfp_call_or_return_candidate and there are enough spare V
1822 registers. A return value of false is an error state as the value will have
1823 been partially passed to the stack. */
1824static bool
1825pass_in_v_vfp_candidate (struct gdbarch *gdbarch, struct regcache *regcache,
1826 struct aarch64_call_info *info, struct type *arg_type,
1827 struct value *arg)
07b287a0 1828{
78134374 1829 switch (arg_type->code ())
0e745c60
AH
1830 {
1831 case TYPE_CODE_FLT:
81657e58 1832 case TYPE_CODE_DECFLOAT:
df86565b 1833 return pass_in_v (gdbarch, regcache, info, arg_type->length (),
efaf1ae0 1834 arg->contents ().data ());
0e745c60
AH
1835 break;
1836
1837 case TYPE_CODE_COMPLEX:
1838 {
efaf1ae0 1839 const bfd_byte *buf = arg->contents ().data ();
27710edb 1840 struct type *target_type = check_typedef (arg_type->target_type ());
0e745c60 1841
df86565b 1842 if (!pass_in_v (gdbarch, regcache, info, target_type->length (),
0e745c60
AH
1843 buf))
1844 return false;
1845
df86565b
SM
1846 return pass_in_v (gdbarch, regcache, info, target_type->length (),
1847 buf + target_type->length ());
0e745c60
AH
1848 }
1849
1850 case TYPE_CODE_ARRAY:
bd63c870 1851 if (arg_type->is_vector ())
df86565b 1852 return pass_in_v (gdbarch, regcache, info, arg_type->length (),
efaf1ae0 1853 arg->contents ().data ());
d182e398 1854 [[fallthrough]];
0e745c60
AH
1855
1856 case TYPE_CODE_STRUCT:
1857 case TYPE_CODE_UNION:
1f704f76 1858 for (int i = 0; i < arg_type->num_fields (); i++)
0e745c60 1859 {
353229bf 1860 /* Don't include static fields. */
c819a338 1861 if (arg_type->field (i).is_static ())
353229bf
AH
1862 continue;
1863
6c49729e 1864 struct value *field = arg->primitive_field (0, i, arg_type);
d0c97917 1865 struct type *field_type = check_typedef (field->type ());
0e745c60
AH
1866
1867 if (!pass_in_v_vfp_candidate (gdbarch, regcache, info, field_type,
1868 field))
1869 return false;
1870 }
1871 return true;
1872
1873 default:
1874 return false;
1875 }
07b287a0
MS
1876}
1877
1878/* Implement the "push_dummy_call" gdbarch method. */
1879
1880static CORE_ADDR
1881aarch64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1882 struct regcache *regcache, CORE_ADDR bp_addr,
1883 int nargs,
cf84fa6b
AH
1884 struct value **args, CORE_ADDR sp,
1885 function_call_return_method return_method,
07b287a0
MS
1886 CORE_ADDR struct_addr)
1887{
07b287a0 1888 int argnum;
07b287a0 1889 struct aarch64_call_info info;
07b287a0 1890
07b287a0
MS
1891 /* We need to know what the type of the called function is in order
1892 to determine the number of named/anonymous arguments for the
1893 actual argument placement, and the return type in order to handle
1894 return value correctly.
1895
1896 The generic code above us views the decision of return in memory
1897 or return in registers as a two stage processes. The language
1898 handler is consulted first and may decide to return in memory (eg
1899 class with copy constructor returned by value), this will cause
1900 the generic code to allocate space AND insert an initial leading
1901 argument.
1902
1903 If the language code does not decide to pass in memory then the
1904 target code is consulted.
1905
1906 If the language code decides to pass in memory we want to move
1907 the pointer inserted as the initial argument from the argument
1908 list and into X8, the conventional AArch64 struct return pointer
38a72da0 1909 register. */
07b287a0
MS
1910
1911 /* Set the return address. For the AArch64, the return breakpoint
1912 is always at BP_ADDR. */
1913 regcache_cooked_write_unsigned (regcache, AARCH64_LR_REGNUM, bp_addr);
1914
38a72da0
AH
1915 /* If we were given an initial argument for the return slot, lose it. */
1916 if (return_method == return_method_hidden_param)
07b287a0
MS
1917 {
1918 args++;
1919 nargs--;
1920 }
1921
1922 /* The struct_return pointer occupies X8. */
38a72da0 1923 if (return_method != return_method_normal)
07b287a0 1924 {
c6185dce
SM
1925 aarch64_debug_printf ("struct return in %s = 0x%s",
1926 gdbarch_register_name
1927 (gdbarch, AARCH64_STRUCT_RETURN_REGNUM),
1928 paddress (gdbarch, struct_addr));
1929
07b287a0
MS
1930 regcache_cooked_write_unsigned (regcache, AARCH64_STRUCT_RETURN_REGNUM,
1931 struct_addr);
1932 }
1933
1934 for (argnum = 0; argnum < nargs; argnum++)
1935 {
1936 struct value *arg = args[argnum];
0e745c60
AH
1937 struct type *arg_type, *fundamental_type;
1938 int len, elements;
07b287a0 1939
d0c97917 1940 arg_type = check_typedef (arg->type ());
df86565b 1941 len = arg_type->length ();
07b287a0 1942
0e745c60
AH
1943 /* If arg can be passed in v registers as per the AAPCS64, then do so if
1944 if there are enough spare registers. */
1945 if (aapcs_is_vfp_call_or_return_candidate (arg_type, &elements,
1946 &fundamental_type))
1947 {
1948 if (info.nsrn + elements <= 8)
1949 {
1950 /* We know that we have sufficient registers available therefore
1951 this will never need to fallback to the stack. */
1952 if (!pass_in_v_vfp_candidate (gdbarch, regcache, &info, arg_type,
1953 arg))
1954 gdb_assert_not_reached ("Failed to push args");
1955 }
1956 else
1957 {
1958 info.nsrn = 8;
1959 pass_on_stack (&info, arg_type, arg);
1960 }
1961 continue;
1962 }
1963
78134374 1964 switch (arg_type->code ())
07b287a0
MS
1965 {
1966 case TYPE_CODE_INT:
1967 case TYPE_CODE_BOOL:
1968 case TYPE_CODE_CHAR:
1969 case TYPE_CODE_RANGE:
1970 case TYPE_CODE_ENUM:
28397ae7 1971 if (len < 4 && !is_fixed_point_type (arg_type))
07b287a0
MS
1972 {
1973 /* Promote to 32 bit integer. */
c6d940a9 1974 if (arg_type->is_unsigned ())
07b287a0
MS
1975 arg_type = builtin_type (gdbarch)->builtin_uint32;
1976 else
1977 arg_type = builtin_type (gdbarch)->builtin_int32;
1978 arg = value_cast (arg_type, arg);
1979 }
8e80f9d1 1980 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
07b287a0
MS
1981 break;
1982
07b287a0
MS
1983 case TYPE_CODE_STRUCT:
1984 case TYPE_CODE_ARRAY:
1985 case TYPE_CODE_UNION:
0e745c60 1986 if (len > 16)
07b287a0
MS
1987 {
1988 /* PCS B.7 Aggregates larger than 16 bytes are passed by
1989 invisible reference. */
1990
1991 /* Allocate aligned storage. */
1992 sp = align_down (sp - len, 16);
1993
1994 /* Write the real data into the stack. */
efaf1ae0 1995 write_memory (sp, arg->contents ().data (), len);
07b287a0
MS
1996
1997 /* Construct the indirection. */
1998 arg_type = lookup_pointer_type (arg_type);
1999 arg = value_from_pointer (arg_type, sp);
8e80f9d1 2000 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
07b287a0
MS
2001 }
2002 else
2003 /* PCS C.15 / C.18 multiple values pass. */
8e80f9d1 2004 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
07b287a0
MS
2005 break;
2006
2007 default:
8e80f9d1 2008 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
07b287a0
MS
2009 break;
2010 }
2011 }
2012
2013 /* Make sure stack retains 16 byte alignment. */
2014 if (info.nsaa & 15)
2015 sp -= 16 - (info.nsaa & 15);
2016
89055eaa 2017 while (!info.si.empty ())
07b287a0 2018 {
89055eaa 2019 const stack_item_t &si = info.si.back ();
07b287a0 2020
89055eaa
TT
2021 sp -= si.len;
2022 if (si.data != NULL)
2023 write_memory (sp, si.data, si.len);
2024 info.si.pop_back ();
07b287a0
MS
2025 }
2026
07b287a0
MS
2027 /* Finally, update the SP register. */
2028 regcache_cooked_write_unsigned (regcache, AARCH64_SP_REGNUM, sp);
2029
2030 return sp;
2031}
2032
2033/* Implement the "frame_align" gdbarch method. */
2034
2035static CORE_ADDR
2036aarch64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2037{
2038 /* Align the stack to sixteen bytes. */
2039 return sp & ~(CORE_ADDR) 15;
2040}
2041
2042/* Return the type for an AdvSISD Q register. */
2043
2044static struct type *
2045aarch64_vnq_type (struct gdbarch *gdbarch)
2046{
08106042 2047 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0
MS
2048
2049 if (tdep->vnq_type == NULL)
2050 {
2051 struct type *t;
2052 struct type *elem;
2053
2054 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnq",
2055 TYPE_CODE_UNION);
2056
2057 elem = builtin_type (gdbarch)->builtin_uint128;
2058 append_composite_type_field (t, "u", elem);
2059
2060 elem = builtin_type (gdbarch)->builtin_int128;
2061 append_composite_type_field (t, "s", elem);
2062
2063 tdep->vnq_type = t;
2064 }
2065
2066 return tdep->vnq_type;
2067}
2068
2069/* Return the type for an AdvSISD D register. */
2070
2071static struct type *
2072aarch64_vnd_type (struct gdbarch *gdbarch)
2073{
08106042 2074 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0
MS
2075
2076 if (tdep->vnd_type == NULL)
2077 {
2078 struct type *t;
2079 struct type *elem;
2080
2081 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnd",
2082 TYPE_CODE_UNION);
2083
2084 elem = builtin_type (gdbarch)->builtin_double;
2085 append_composite_type_field (t, "f", elem);
2086
2087 elem = builtin_type (gdbarch)->builtin_uint64;
2088 append_composite_type_field (t, "u", elem);
2089
2090 elem = builtin_type (gdbarch)->builtin_int64;
2091 append_composite_type_field (t, "s", elem);
2092
2093 tdep->vnd_type = t;
2094 }
2095
2096 return tdep->vnd_type;
2097}
2098
2099/* Return the type for an AdvSISD S register. */
2100
2101static struct type *
2102aarch64_vns_type (struct gdbarch *gdbarch)
2103{
08106042 2104 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0
MS
2105
2106 if (tdep->vns_type == NULL)
2107 {
2108 struct type *t;
2109 struct type *elem;
2110
2111 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vns",
2112 TYPE_CODE_UNION);
2113
2114 elem = builtin_type (gdbarch)->builtin_float;
2115 append_composite_type_field (t, "f", elem);
2116
2117 elem = builtin_type (gdbarch)->builtin_uint32;
2118 append_composite_type_field (t, "u", elem);
2119
2120 elem = builtin_type (gdbarch)->builtin_int32;
2121 append_composite_type_field (t, "s", elem);
2122
2123 tdep->vns_type = t;
2124 }
2125
2126 return tdep->vns_type;
2127}
2128
2129/* Return the type for an AdvSISD H register. */
2130
2131static struct type *
2132aarch64_vnh_type (struct gdbarch *gdbarch)
2133{
08106042 2134 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0
MS
2135
2136 if (tdep->vnh_type == NULL)
2137 {
2138 struct type *t;
2139 struct type *elem;
2140
2141 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnh",
2142 TYPE_CODE_UNION);
2143
5291fe3c
SP
2144 elem = builtin_type (gdbarch)->builtin_bfloat16;
2145 append_composite_type_field (t, "bf", elem);
2146
a6d0f249
AH
2147 elem = builtin_type (gdbarch)->builtin_half;
2148 append_composite_type_field (t, "f", elem);
2149
07b287a0
MS
2150 elem = builtin_type (gdbarch)->builtin_uint16;
2151 append_composite_type_field (t, "u", elem);
2152
2153 elem = builtin_type (gdbarch)->builtin_int16;
2154 append_composite_type_field (t, "s", elem);
2155
2156 tdep->vnh_type = t;
2157 }
2158
2159 return tdep->vnh_type;
2160}
2161
2162/* Return the type for an AdvSISD B register. */
2163
2164static struct type *
2165aarch64_vnb_type (struct gdbarch *gdbarch)
2166{
08106042 2167 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0
MS
2168
2169 if (tdep->vnb_type == NULL)
2170 {
2171 struct type *t;
2172 struct type *elem;
2173
2174 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnb",
2175 TYPE_CODE_UNION);
2176
2177 elem = builtin_type (gdbarch)->builtin_uint8;
2178 append_composite_type_field (t, "u", elem);
2179
2180 elem = builtin_type (gdbarch)->builtin_int8;
2181 append_composite_type_field (t, "s", elem);
2182
2183 tdep->vnb_type = t;
2184 }
2185
2186 return tdep->vnb_type;
2187}
2188
ca65640f
LM
2189/* Return TRUE if REGNUM is a ZA tile slice pseudo-register number. Return
2190 FALSE otherwise. */
2191
2192static bool
2193is_sme_tile_slice_pseudo_register (struct gdbarch *gdbarch, int regnum)
2194{
2195 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2196
2197 gdb_assert (tdep->has_sme ());
2198 gdb_assert (tdep->sme_svq > 0);
2199 gdb_assert (tdep->sme_pseudo_base <= regnum);
2200 gdb_assert (regnum < tdep->sme_pseudo_base + tdep->sme_pseudo_count);
2201
2202 if (tdep->sme_tile_slice_pseudo_base <= regnum
2203 && regnum < tdep->sme_tile_slice_pseudo_base
2204 + tdep->sme_tile_slice_pseudo_count)
2205 return true;
2206
2207 return false;
2208}
2209
2210/* Given REGNUM, a ZA pseudo-register number, return, in ENCODING, the
2211 decoded fields that make up its name. */
2212
2213static void
2214aarch64_za_decode_pseudos (struct gdbarch *gdbarch, int regnum,
2215 struct za_pseudo_encoding &encoding)
2216{
2217 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2218
2219 gdb_assert (tdep->has_sme ());
2220 gdb_assert (tdep->sme_svq > 0);
2221 gdb_assert (tdep->sme_pseudo_base <= regnum);
2222 gdb_assert (regnum < tdep->sme_pseudo_base + tdep->sme_pseudo_count);
2223
2224 if (is_sme_tile_slice_pseudo_register (gdbarch, regnum))
2225 {
2226 /* Calculate the tile slice pseudo-register offset relative to the other
2227 tile slice pseudo-registers. */
2228 int offset = regnum - tdep->sme_tile_slice_pseudo_base;
2229
2230 /* Fetch the qualifier. We can have 160 to 2560 possible tile slice
2231 pseudo-registers. Each qualifier (we have 5 of them: B, H, S, D
2232 and Q) covers 32 * svq pseudo-registers, so we divide the offset by
2233 that constant. */
2234 size_t qualifier = offset / (tdep->sme_svq * 32);
2235 encoding.qualifier_index = qualifier;
2236
2237 /* Prepare to fetch the direction (d), tile number (t) and slice
2238 number (s). */
2239 int dts = offset % (tdep->sme_svq * 32);
2240
2241 /* The direction is represented by the even/odd numbers. Even-numbered
2242 pseudo-registers are horizontal tile slices and odd-numbered
2243 pseudo-registers are vertical tile slices. */
2244 encoding.horizontal = !(dts & 1);
2245
2246 /* Fetch the tile number. The tile number is closely related to the
2247 qualifier. B has 1 tile, H has 2 tiles, S has 4 tiles, D has 8 tiles
2248 and Q has 16 tiles. */
2249 encoding.tile_index = (dts >> 1) & ((1 << qualifier) - 1);
2250
2251 /* Fetch the slice number. The slice number is closely related to the
2252 qualifier and the svl. */
2253 encoding.slice_index = dts >> (qualifier + 1);
2254 }
2255 else
2256 {
2257 /* Calculate the tile pseudo-register offset relative to the other
2258 tile pseudo-registers. */
2259 int offset = regnum - tdep->sme_tile_pseudo_base;
2260
2261 encoding.qualifier_index = std::floor (std::log2 (offset + 1));
2262 /* Calculate the tile number. */
2263 encoding.tile_index = (offset + 1) - (1 << encoding.qualifier_index);
2264 /* Direction and slice index don't get used for tiles. Set them to
2265 0/false values. */
2266 encoding.slice_index = 0;
2267 encoding.horizontal = false;
2268 }
2269}
2270
2271/* Return the type for a ZA tile slice pseudo-register based on ENCODING. */
2272
2273static struct type *
2274aarch64_za_tile_slice_type (struct gdbarch *gdbarch,
2275 const struct za_pseudo_encoding &encoding)
2276{
2277 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2278
2279 gdb_assert (tdep->has_sme ());
2280 gdb_assert (tdep->sme_svq > 0);
2281
2282 if (tdep->sme_tile_slice_type_q == nullptr)
2283 {
2284 /* Q tile slice type. */
2285 tdep->sme_tile_slice_type_q
2286 = init_vector_type (builtin_type (gdbarch)->builtin_uint128,
2287 tdep->sme_svq);
2288 /* D tile slice type. */
2289 tdep->sme_tile_slice_type_d
2290 = init_vector_type (builtin_type (gdbarch)->builtin_uint64,
2291 tdep->sme_svq * 2);
2292 /* S tile slice type. */
2293 tdep->sme_tile_slice_type_s
2294 = init_vector_type (builtin_type (gdbarch)->builtin_uint32,
2295 tdep->sme_svq * 4);
2296 /* H tile slice type. */
2297 tdep->sme_tile_slice_type_h
2298 = init_vector_type (builtin_type (gdbarch)->builtin_uint16,
2299 tdep->sme_svq * 8);
2300 /* B tile slice type. */
2301 tdep->sme_tile_slice_type_b
2302 = init_vector_type (builtin_type (gdbarch)->builtin_uint8,
2303 tdep->sme_svq * 16);
2304 }
2305
2306 switch (encoding.qualifier_index)
2307 {
2308 case 4:
2309 return tdep->sme_tile_slice_type_q;
2310 case 3:
2311 return tdep->sme_tile_slice_type_d;
2312 case 2:
2313 return tdep->sme_tile_slice_type_s;
2314 case 1:
2315 return tdep->sme_tile_slice_type_h;
2316 case 0:
2317 return tdep->sme_tile_slice_type_b;
2318 default:
2319 error (_("Invalid qualifier index %s for tile slice pseudo register."),
2320 pulongest (encoding.qualifier_index));
2321 }
2322
2323 gdb_assert_not_reached ("Unknown qualifier for ZA tile slice register");
2324}
2325
2326/* Return the type for a ZA tile pseudo-register based on ENCODING. */
2327
2328static struct type *
2329aarch64_za_tile_type (struct gdbarch *gdbarch,
2330 const struct za_pseudo_encoding &encoding)
2331{
2332 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2333
2334 gdb_assert (tdep->has_sme ());
2335 gdb_assert (tdep->sme_svq > 0);
2336
2337 if (tdep->sme_tile_type_q == nullptr)
2338 {
2339 struct type *inner_vectors_type;
2340
2341 /* Q tile type. */
2342 inner_vectors_type
2343 = init_vector_type (builtin_type (gdbarch)->builtin_uint128,
2344 tdep->sme_svq);
2345 tdep->sme_tile_type_q
2346 = init_vector_type (inner_vectors_type, tdep->sme_svq);
2347
2348 /* D tile type. */
2349 inner_vectors_type
2350 = init_vector_type (builtin_type (gdbarch)->builtin_uint64,
2351 tdep->sme_svq * 2);
2352 tdep->sme_tile_type_d
2353 = init_vector_type (inner_vectors_type, tdep->sme_svq * 2);
2354
2355 /* S tile type. */
2356 inner_vectors_type
2357 = init_vector_type (builtin_type (gdbarch)->builtin_uint32,
2358 tdep->sme_svq * 4);
2359 tdep->sme_tile_type_s
2360 = init_vector_type (inner_vectors_type, tdep->sme_svq * 4);
2361
2362 /* H tile type. */
2363 inner_vectors_type
2364 = init_vector_type (builtin_type (gdbarch)->builtin_uint16,
2365 tdep->sme_svq * 8);
2366 tdep->sme_tile_type_h
2367 = init_vector_type (inner_vectors_type, tdep->sme_svq * 8);
2368
2369 /* B tile type. */
2370 inner_vectors_type
2371 = init_vector_type (builtin_type (gdbarch)->builtin_uint8,
2372 tdep->sme_svq * 16);
2373 tdep->sme_tile_type_b
2374 = init_vector_type (inner_vectors_type, tdep->sme_svq * 16);
2375 }
2376
2377 switch (encoding.qualifier_index)
2378 {
2379 case 4:
2380 return tdep->sme_tile_type_q;
2381 case 3:
2382 return tdep->sme_tile_type_d;
2383 case 2:
2384 return tdep->sme_tile_type_s;
2385 case 1:
2386 return tdep->sme_tile_type_h;
2387 case 0:
2388 return tdep->sme_tile_type_b;
2389 default:
2390 error (_("Invalid qualifier index %s for ZA tile pseudo register."),
2391 pulongest (encoding.qualifier_index));
2392 }
2393
2394 gdb_assert_not_reached ("unknown qualifier for tile pseudo-register");
2395}
2396
63bad7b6
AH
2397/* Return the type for an AdvSISD V register. */
2398
2399static struct type *
2400aarch64_vnv_type (struct gdbarch *gdbarch)
2401{
08106042 2402 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
63bad7b6
AH
2403
2404 if (tdep->vnv_type == NULL)
2405 {
09624f1f 2406 /* The other AArch64 pseudo registers (Q,D,H,S,B) refer to a single value
bffa1015
AH
2407 slice from the non-pseudo vector registers. However NEON V registers
2408 are always vector registers, and need constructing as such. */
2409 const struct builtin_type *bt = builtin_type (gdbarch);
2410
63bad7b6
AH
2411 struct type *t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnv",
2412 TYPE_CODE_UNION);
2413
bffa1015
AH
2414 struct type *sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vnd",
2415 TYPE_CODE_UNION);
2416 append_composite_type_field (sub, "f",
2417 init_vector_type (bt->builtin_double, 2));
2418 append_composite_type_field (sub, "u",
2419 init_vector_type (bt->builtin_uint64, 2));
2420 append_composite_type_field (sub, "s",
2421 init_vector_type (bt->builtin_int64, 2));
2422 append_composite_type_field (t, "d", sub);
2423
2424 sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vns",
2425 TYPE_CODE_UNION);
2426 append_composite_type_field (sub, "f",
2427 init_vector_type (bt->builtin_float, 4));
2428 append_composite_type_field (sub, "u",
2429 init_vector_type (bt->builtin_uint32, 4));
2430 append_composite_type_field (sub, "s",
2431 init_vector_type (bt->builtin_int32, 4));
2432 append_composite_type_field (t, "s", sub);
2433
2434 sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vnh",
2435 TYPE_CODE_UNION);
5291fe3c
SP
2436 append_composite_type_field (sub, "bf",
2437 init_vector_type (bt->builtin_bfloat16, 8));
a6d0f249
AH
2438 append_composite_type_field (sub, "f",
2439 init_vector_type (bt->builtin_half, 8));
bffa1015
AH
2440 append_composite_type_field (sub, "u",
2441 init_vector_type (bt->builtin_uint16, 8));
2442 append_composite_type_field (sub, "s",
2443 init_vector_type (bt->builtin_int16, 8));
2444 append_composite_type_field (t, "h", sub);
2445
2446 sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vnb",
2447 TYPE_CODE_UNION);
2448 append_composite_type_field (sub, "u",
2449 init_vector_type (bt->builtin_uint8, 16));
2450 append_composite_type_field (sub, "s",
2451 init_vector_type (bt->builtin_int8, 16));
2452 append_composite_type_field (t, "b", sub);
2453
2454 sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vnq",
2455 TYPE_CODE_UNION);
2456 append_composite_type_field (sub, "u",
2457 init_vector_type (bt->builtin_uint128, 1));
2458 append_composite_type_field (sub, "s",
2459 init_vector_type (bt->builtin_int128, 1));
2460 append_composite_type_field (t, "q", sub);
63bad7b6
AH
2461
2462 tdep->vnv_type = t;
2463 }
2464
2465 return tdep->vnv_type;
2466}
2467
07b287a0
MS
2468/* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
2469
2470static int
2471aarch64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
2472{
08106042 2473 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
34dcc7cf 2474
07b287a0
MS
2475 if (reg >= AARCH64_DWARF_X0 && reg <= AARCH64_DWARF_X0 + 30)
2476 return AARCH64_X0_REGNUM + reg - AARCH64_DWARF_X0;
2477
2478 if (reg == AARCH64_DWARF_SP)
2479 return AARCH64_SP_REGNUM;
2480
1fe84861
YY
2481 if (reg == AARCH64_DWARF_PC)
2482 return AARCH64_PC_REGNUM;
2483
07b287a0
MS
2484 if (reg >= AARCH64_DWARF_V0 && reg <= AARCH64_DWARF_V0 + 31)
2485 return AARCH64_V0_REGNUM + reg - AARCH64_DWARF_V0;
2486
65d4cada
AH
2487 if (reg == AARCH64_DWARF_SVE_VG)
2488 return AARCH64_SVE_VG_REGNUM;
2489
2490 if (reg == AARCH64_DWARF_SVE_FFR)
2491 return AARCH64_SVE_FFR_REGNUM;
2492
2493 if (reg >= AARCH64_DWARF_SVE_P0 && reg <= AARCH64_DWARF_SVE_P0 + 15)
2494 return AARCH64_SVE_P0_REGNUM + reg - AARCH64_DWARF_SVE_P0;
2495
2496 if (reg >= AARCH64_DWARF_SVE_Z0 && reg <= AARCH64_DWARF_SVE_Z0 + 15)
2497 return AARCH64_SVE_Z0_REGNUM + reg - AARCH64_DWARF_SVE_Z0;
2498
34dcc7cf
AH
2499 if (tdep->has_pauth ())
2500 {
c9cd8ca4
LM
2501 if (reg == AARCH64_DWARF_RA_SIGN_STATE)
2502 return tdep->ra_sign_state_regnum;
34dcc7cf
AH
2503 }
2504
07b287a0
MS
2505 return -1;
2506}
07b287a0
MS
2507
2508/* Implement the "print_insn" gdbarch method. */
2509
2510static int
2511aarch64_gdb_print_insn (bfd_vma memaddr, disassemble_info *info)
2512{
2513 info->symbols = NULL;
6394c606 2514 return default_print_insn (memaddr, info);
07b287a0
MS
2515}
2516
2517/* AArch64 BRK software debug mode instruction.
2518 Note that AArch64 code is always little-endian.
2519 1101.0100.0010.0000.0000.0000.0000.0000 = 0xd4200000. */
04180708 2520constexpr gdb_byte aarch64_default_breakpoint[] = {0x00, 0x00, 0x20, 0xd4};
07b287a0 2521
04180708 2522typedef BP_MANIPULATION (aarch64_default_breakpoint) aarch64_breakpoint;
07b287a0
MS
2523
2524/* Extract from an array REGS containing the (raw) register state a
2525 function return value of type TYPE, and copy that, in virtual
2526 format, into VALBUF. */
2527
2528static void
2529aarch64_extract_return_value (struct type *type, struct regcache *regs,
2530 gdb_byte *valbuf)
2531{
ac7936df 2532 struct gdbarch *gdbarch = regs->arch ();
07b287a0 2533 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4f4aedeb
AH
2534 int elements;
2535 struct type *fundamental_type;
07b287a0 2536
4f4aedeb
AH
2537 if (aapcs_is_vfp_call_or_return_candidate (type, &elements,
2538 &fundamental_type))
07b287a0 2539 {
df86565b 2540 int len = fundamental_type->length ();
4f4aedeb
AH
2541
2542 for (int i = 0; i < elements; i++)
2543 {
2544 int regno = AARCH64_V0_REGNUM + i;
3ff2c72e 2545 /* Enough space for a full vector register. */
d724d71a
SM
2546 gdb::byte_vector buf (register_size (gdbarch, regno));
2547 gdb_assert (len <= buf.size ());
4f4aedeb 2548
c6185dce
SM
2549 aarch64_debug_printf
2550 ("read HFA or HVA return value element %d from %s",
2551 i + 1, gdbarch_register_name (gdbarch, regno));
2552
4f4aedeb 2553 regs->cooked_read (regno, buf);
07b287a0 2554
d724d71a 2555 memcpy (valbuf, buf.data (), len);
4f4aedeb
AH
2556 valbuf += len;
2557 }
07b287a0 2558 }
78134374
SM
2559 else if (type->code () == TYPE_CODE_INT
2560 || type->code () == TYPE_CODE_CHAR
2561 || type->code () == TYPE_CODE_BOOL
2562 || type->code () == TYPE_CODE_PTR
aa006118 2563 || TYPE_IS_REFERENCE (type)
78134374 2564 || type->code () == TYPE_CODE_ENUM)
07b287a0 2565 {
6471e7d2 2566 /* If the type is a plain integer, then the access is
07b287a0
MS
2567 straight-forward. Otherwise we have to play around a bit
2568 more. */
df86565b 2569 int len = type->length ();
07b287a0
MS
2570 int regno = AARCH64_X0_REGNUM;
2571 ULONGEST tmp;
2572
2573 while (len > 0)
2574 {
2575 /* By using store_unsigned_integer we avoid having to do
2576 anything special for small big-endian values. */
2577 regcache_cooked_read_unsigned (regs, regno++, &tmp);
2578 store_unsigned_integer (valbuf,
2579 (len > X_REGISTER_SIZE
2580 ? X_REGISTER_SIZE : len), byte_order, tmp);
2581 len -= X_REGISTER_SIZE;
2582 valbuf += X_REGISTER_SIZE;
2583 }
2584 }
07b287a0
MS
2585 else
2586 {
ac51afb5 2587 /* For a structure or union the behavior is as if the value had
dda83cd7
SM
2588 been stored to word-aligned memory and then loaded into
2589 registers with 64-bit load instruction(s). */
df86565b 2590 int len = type->length ();
07b287a0
MS
2591 int regno = AARCH64_X0_REGNUM;
2592 bfd_byte buf[X_REGISTER_SIZE];
2593
2594 while (len > 0)
2595 {
dca08e1f 2596 regs->cooked_read (regno++, buf);
07b287a0
MS
2597 memcpy (valbuf, buf, len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
2598 len -= X_REGISTER_SIZE;
2599 valbuf += X_REGISTER_SIZE;
2600 }
2601 }
2602}
2603
2604
2605/* Will a function return an aggregate type in memory or in a
2606 register? Return 0 if an aggregate type can be returned in a
2607 register, 1 if it must be returned in memory. */
2608
2609static int
2610aarch64_return_in_memory (struct gdbarch *gdbarch, struct type *type)
2611{
f168693b 2612 type = check_typedef (type);
4f4aedeb
AH
2613 int elements;
2614 struct type *fundamental_type;
07b287a0 2615
911627e7
TT
2616 if (TYPE_HAS_DYNAMIC_LENGTH (type))
2617 return 1;
2618
4f4aedeb
AH
2619 if (aapcs_is_vfp_call_or_return_candidate (type, &elements,
2620 &fundamental_type))
07b287a0 2621 {
cd635f74
YQ
2622 /* v0-v7 are used to return values and one register is allocated
2623 for one member. However, HFA or HVA has at most four members. */
07b287a0
MS
2624 return 0;
2625 }
2626
df86565b 2627 if (type->length () > 16
bab22d06 2628 || !language_pass_by_reference (type).trivially_copyable)
07b287a0
MS
2629 {
2630 /* PCS B.6 Aggregates larger than 16 bytes are passed by
dda83cd7 2631 invisible reference. */
07b287a0
MS
2632
2633 return 1;
2634 }
2635
2636 return 0;
2637}
2638
2639/* Write into appropriate registers a function return value of type
2640 TYPE, given in virtual format. */
2641
2642static void
2643aarch64_store_return_value (struct type *type, struct regcache *regs,
2644 const gdb_byte *valbuf)
2645{
ac7936df 2646 struct gdbarch *gdbarch = regs->arch ();
07b287a0 2647 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4f4aedeb
AH
2648 int elements;
2649 struct type *fundamental_type;
07b287a0 2650
4f4aedeb
AH
2651 if (aapcs_is_vfp_call_or_return_candidate (type, &elements,
2652 &fundamental_type))
07b287a0 2653 {
df86565b 2654 int len = fundamental_type->length ();
4f4aedeb
AH
2655
2656 for (int i = 0; i < elements; i++)
2657 {
2658 int regno = AARCH64_V0_REGNUM + i;
3ff2c72e 2659 /* Enough space for a full vector register. */
d724d71a
SM
2660 gdb::byte_vector tmpbuf (register_size (gdbarch, regno));
2661 gdb_assert (len <= tmpbuf.size ());
4f4aedeb 2662
c6185dce
SM
2663 aarch64_debug_printf
2664 ("write HFA or HVA return value element %d to %s",
2665 i + 1, gdbarch_register_name (gdbarch, regno));
07b287a0 2666
89c4ee83
LM
2667 /* Depending on whether the target supports SVE or not, the V
2668 registers may report a size > 16 bytes. In that case, read the
2669 original contents of the register before overriding it with a new
2670 value that has a potential size <= 16 bytes. */
2671 regs->cooked_read (regno, tmpbuf);
d724d71a 2672 memcpy (tmpbuf.data (), valbuf,
4f4aedeb
AH
2673 len > V_REGISTER_SIZE ? V_REGISTER_SIZE : len);
2674 regs->cooked_write (regno, tmpbuf);
2675 valbuf += len;
2676 }
07b287a0 2677 }
78134374
SM
2678 else if (type->code () == TYPE_CODE_INT
2679 || type->code () == TYPE_CODE_CHAR
2680 || type->code () == TYPE_CODE_BOOL
2681 || type->code () == TYPE_CODE_PTR
aa006118 2682 || TYPE_IS_REFERENCE (type)
78134374 2683 || type->code () == TYPE_CODE_ENUM)
07b287a0 2684 {
df86565b 2685 if (type->length () <= X_REGISTER_SIZE)
07b287a0
MS
2686 {
2687 /* Values of one word or less are zero/sign-extended and
2688 returned in r0. */
2689 bfd_byte tmpbuf[X_REGISTER_SIZE];
2690 LONGEST val = unpack_long (type, valbuf);
2691
2692 store_signed_integer (tmpbuf, X_REGISTER_SIZE, byte_order, val);
b66f5587 2693 regs->cooked_write (AARCH64_X0_REGNUM, tmpbuf);
07b287a0
MS
2694 }
2695 else
2696 {
2697 /* Integral values greater than one word are stored in
2698 consecutive registers starting with r0. This will always
973c5759 2699 be a multiple of the register size. */
df86565b 2700 int len = type->length ();
07b287a0
MS
2701 int regno = AARCH64_X0_REGNUM;
2702
2703 while (len > 0)
2704 {
b66f5587 2705 regs->cooked_write (regno++, valbuf);
07b287a0
MS
2706 len -= X_REGISTER_SIZE;
2707 valbuf += X_REGISTER_SIZE;
2708 }
2709 }
2710 }
07b287a0
MS
2711 else
2712 {
ac51afb5 2713 /* For a structure or union the behavior is as if the value had
07b287a0
MS
2714 been stored to word-aligned memory and then loaded into
2715 registers with 64-bit load instruction(s). */
df86565b 2716 int len = type->length ();
07b287a0
MS
2717 int regno = AARCH64_X0_REGNUM;
2718 bfd_byte tmpbuf[X_REGISTER_SIZE];
2719
2720 while (len > 0)
2721 {
2722 memcpy (tmpbuf, valbuf,
2723 len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
b66f5587 2724 regs->cooked_write (regno++, tmpbuf);
07b287a0
MS
2725 len -= X_REGISTER_SIZE;
2726 valbuf += X_REGISTER_SIZE;
2727 }
2728 }
2729}
2730
2731/* Implement the "return_value" gdbarch method. */
2732
2733static enum return_value_convention
2734aarch64_return_value (struct gdbarch *gdbarch, struct value *func_value,
2735 struct type *valtype, struct regcache *regcache,
5cb0f2d5 2736 struct value **read_value, const gdb_byte *writebuf)
07b287a0 2737{
78134374
SM
2738 if (valtype->code () == TYPE_CODE_STRUCT
2739 || valtype->code () == TYPE_CODE_UNION
2740 || valtype->code () == TYPE_CODE_ARRAY)
07b287a0
MS
2741 {
2742 if (aarch64_return_in_memory (gdbarch, valtype))
2743 {
bab22d06
LM
2744 /* From the AAPCS64's Result Return section:
2745
2746 "Otherwise, the caller shall reserve a block of memory of
2747 sufficient size and alignment to hold the result. The address
2748 of the memory block shall be passed as an additional argument to
2749 the function in x8. */
2750
c6185dce 2751 aarch64_debug_printf ("return value in memory");
bab22d06 2752
911627e7 2753 if (read_value != nullptr)
bab22d06
LM
2754 {
2755 CORE_ADDR addr;
2756
2757 regcache->cooked_read (AARCH64_STRUCT_RETURN_REGNUM, &addr);
911627e7 2758 *read_value = value_at_non_lval (valtype, addr);
bab22d06
LM
2759 }
2760
2761 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
07b287a0
MS
2762 }
2763 }
2764
2765 if (writebuf)
2766 aarch64_store_return_value (valtype, regcache, writebuf);
2767
911627e7
TT
2768 if (read_value)
2769 {
317c3ed9 2770 *read_value = value::allocate (valtype);
911627e7 2771 aarch64_extract_return_value (valtype, regcache,
bbe912ba 2772 (*read_value)->contents_raw ().data ());
911627e7 2773 }
07b287a0 2774
c6185dce 2775 aarch64_debug_printf ("return value in registers");
07b287a0
MS
2776
2777 return RETURN_VALUE_REGISTER_CONVENTION;
2778}
2779
2780/* Implement the "get_longjmp_target" gdbarch method. */
2781
2782static int
8480a37e 2783aarch64_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
07b287a0
MS
2784{
2785 CORE_ADDR jb_addr;
2786 gdb_byte buf[X_REGISTER_SIZE];
2787 struct gdbarch *gdbarch = get_frame_arch (frame);
08106042 2788 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0
MS
2789 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2790
2791 jb_addr = get_frame_register_unsigned (frame, AARCH64_X0_REGNUM);
2792
2793 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
2794 X_REGISTER_SIZE))
2795 return 0;
2796
2797 *pc = extract_unsigned_integer (buf, X_REGISTER_SIZE, byte_order);
2798 return 1;
2799}
ea873d8e
PL
2800
2801/* Implement the "gen_return_address" gdbarch method. */
2802
2803static void
2804aarch64_gen_return_address (struct gdbarch *gdbarch,
2805 struct agent_expr *ax, struct axs_value *value,
2806 CORE_ADDR scope)
2807{
2808 value->type = register_type (gdbarch, AARCH64_LR_REGNUM);
2809 value->kind = axs_lvalue_register;
2810 value->u.reg = AARCH64_LR_REGNUM;
2811}
07b287a0
MS
2812\f
2813
e63ae49b
LM
2814/* Return TRUE if REGNUM is a W pseudo-register number. Return FALSE
2815 otherwise. */
2816
2817static bool
2818is_w_pseudo_register (struct gdbarch *gdbarch, int regnum)
2819{
2820 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2821
2822 if (tdep->w_pseudo_base <= regnum
2823 && regnum < tdep->w_pseudo_base + tdep->w_pseudo_count)
2824 return true;
2825
2826 return false;
2827}
2828
ca65640f
LM
2829/* Return TRUE if REGNUM is a SME pseudo-register number. Return FALSE
2830 otherwise. */
2831
2832static bool
2833is_sme_pseudo_register (struct gdbarch *gdbarch, int regnum)
2834{
2835 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2836
2837 if (tdep->has_sme () && tdep->sme_pseudo_base <= regnum
2838 && regnum < tdep->sme_pseudo_base + tdep->sme_pseudo_count)
2839 return true;
2840
2841 return false;
2842}
2843
2844/* Convert ENCODING into a ZA tile slice name. */
2845
2846static const std::string
2847aarch64_za_tile_slice_name (const struct za_pseudo_encoding &encoding)
2848{
2849 gdb_assert (encoding.qualifier_index >= 0);
2850 gdb_assert (encoding.qualifier_index <= 4);
2851 gdb_assert (encoding.tile_index >= 0);
2852 gdb_assert (encoding.tile_index <= 15);
2853 gdb_assert (encoding.slice_index >= 0);
2854 gdb_assert (encoding.slice_index <= 255);
2855
2856 const char orientation = encoding.horizontal ? 'h' : 'v';
2857
2858 const char qualifiers[6] = "bhsdq";
2859 const char qualifier = qualifiers [encoding.qualifier_index];
2860 return string_printf ("za%d%c%c%d", encoding.tile_index, orientation,
2861 qualifier, encoding.slice_index);
2862}
2863
2864/* Convert ENCODING into a ZA tile name. */
2865
2866static const std::string
2867aarch64_za_tile_name (const struct za_pseudo_encoding &encoding)
2868{
2869 /* Tiles don't use the slice number and the direction fields. */
2870 gdb_assert (encoding.qualifier_index >= 0);
2871 gdb_assert (encoding.qualifier_index <= 4);
2872 gdb_assert (encoding.tile_index >= 0);
2873 gdb_assert (encoding.tile_index <= 15);
2874
2875 const char qualifiers[6] = "bhsdq";
2876 const char qualifier = qualifiers [encoding.qualifier_index];
2877 return (string_printf ("za%d%c", encoding.tile_index, qualifier));
2878}
2879
2880/* Given a SME pseudo-register REGNUM, return its type. */
2881
2882static struct type *
2883aarch64_sme_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
2884{
2885 struct za_pseudo_encoding encoding;
2886
2887 /* Decode the SME pseudo-register number. */
2888 aarch64_za_decode_pseudos (gdbarch, regnum, encoding);
2889
2890 if (is_sme_tile_slice_pseudo_register (gdbarch, regnum))
2891 return aarch64_za_tile_slice_type (gdbarch, encoding);
2892 else
2893 return aarch64_za_tile_type (gdbarch, encoding);
2894}
2895
07b287a0
MS
2896/* Return the pseudo register name corresponding to register regnum. */
2897
2898static const char *
2899aarch64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
2900{
08106042 2901 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
63bad7b6 2902
e63ae49b
LM
2903 /* W pseudo-registers. Bottom halves of the X registers. */
2904 static const char *const w_name[] =
2905 {
2906 "w0", "w1", "w2", "w3",
2907 "w4", "w5", "w6", "w7",
2908 "w8", "w9", "w10", "w11",
2909 "w12", "w13", "w14", "w15",
2910 "w16", "w17", "w18", "w19",
2911 "w20", "w21", "w22", "w23",
2912 "w24", "w25", "w26", "w27",
2913 "w28", "w29", "w30",
2914 };
2915
07b287a0
MS
2916 static const char *const q_name[] =
2917 {
2918 "q0", "q1", "q2", "q3",
2919 "q4", "q5", "q6", "q7",
2920 "q8", "q9", "q10", "q11",
2921 "q12", "q13", "q14", "q15",
2922 "q16", "q17", "q18", "q19",
2923 "q20", "q21", "q22", "q23",
2924 "q24", "q25", "q26", "q27",
2925 "q28", "q29", "q30", "q31",
2926 };
2927
2928 static const char *const d_name[] =
2929 {
2930 "d0", "d1", "d2", "d3",
2931 "d4", "d5", "d6", "d7",
2932 "d8", "d9", "d10", "d11",
2933 "d12", "d13", "d14", "d15",
2934 "d16", "d17", "d18", "d19",
2935 "d20", "d21", "d22", "d23",
2936 "d24", "d25", "d26", "d27",
2937 "d28", "d29", "d30", "d31",
2938 };
2939
2940 static const char *const s_name[] =
2941 {
2942 "s0", "s1", "s2", "s3",
2943 "s4", "s5", "s6", "s7",
2944 "s8", "s9", "s10", "s11",
2945 "s12", "s13", "s14", "s15",
2946 "s16", "s17", "s18", "s19",
2947 "s20", "s21", "s22", "s23",
2948 "s24", "s25", "s26", "s27",
2949 "s28", "s29", "s30", "s31",
2950 };
2951
2952 static const char *const h_name[] =
2953 {
2954 "h0", "h1", "h2", "h3",
2955 "h4", "h5", "h6", "h7",
2956 "h8", "h9", "h10", "h11",
2957 "h12", "h13", "h14", "h15",
2958 "h16", "h17", "h18", "h19",
2959 "h20", "h21", "h22", "h23",
2960 "h24", "h25", "h26", "h27",
2961 "h28", "h29", "h30", "h31",
2962 };
2963
2964 static const char *const b_name[] =
2965 {
2966 "b0", "b1", "b2", "b3",
2967 "b4", "b5", "b6", "b7",
2968 "b8", "b9", "b10", "b11",
2969 "b12", "b13", "b14", "b15",
2970 "b16", "b17", "b18", "b19",
2971 "b20", "b21", "b22", "b23",
2972 "b24", "b25", "b26", "b27",
2973 "b28", "b29", "b30", "b31",
2974 };
2975
34dcc7cf 2976 int p_regnum = regnum - gdbarch_num_regs (gdbarch);
07b287a0 2977
34dcc7cf
AH
2978 if (p_regnum >= AARCH64_Q0_REGNUM && p_regnum < AARCH64_Q0_REGNUM + 32)
2979 return q_name[p_regnum - AARCH64_Q0_REGNUM];
07b287a0 2980
34dcc7cf
AH
2981 if (p_regnum >= AARCH64_D0_REGNUM && p_regnum < AARCH64_D0_REGNUM + 32)
2982 return d_name[p_regnum - AARCH64_D0_REGNUM];
07b287a0 2983
34dcc7cf
AH
2984 if (p_regnum >= AARCH64_S0_REGNUM && p_regnum < AARCH64_S0_REGNUM + 32)
2985 return s_name[p_regnum - AARCH64_S0_REGNUM];
07b287a0 2986
34dcc7cf
AH
2987 if (p_regnum >= AARCH64_H0_REGNUM && p_regnum < AARCH64_H0_REGNUM + 32)
2988 return h_name[p_regnum - AARCH64_H0_REGNUM];
07b287a0 2989
34dcc7cf
AH
2990 if (p_regnum >= AARCH64_B0_REGNUM && p_regnum < AARCH64_B0_REGNUM + 32)
2991 return b_name[p_regnum - AARCH64_B0_REGNUM];
07b287a0 2992
e63ae49b
LM
2993 /* W pseudo-registers? */
2994 if (is_w_pseudo_register (gdbarch, regnum))
2995 return w_name[regnum - tdep->w_pseudo_base];
2996
63bad7b6
AH
2997 if (tdep->has_sve ())
2998 {
2999 static const char *const sve_v_name[] =
3000 {
3001 "v0", "v1", "v2", "v3",
3002 "v4", "v5", "v6", "v7",
3003 "v8", "v9", "v10", "v11",
3004 "v12", "v13", "v14", "v15",
3005 "v16", "v17", "v18", "v19",
3006 "v20", "v21", "v22", "v23",
3007 "v24", "v25", "v26", "v27",
3008 "v28", "v29", "v30", "v31",
3009 };
3010
34dcc7cf
AH
3011 if (p_regnum >= AARCH64_SVE_V0_REGNUM
3012 && p_regnum < AARCH64_SVE_V0_REGNUM + AARCH64_V_REGS_NUM)
3013 return sve_v_name[p_regnum - AARCH64_SVE_V0_REGNUM];
63bad7b6
AH
3014 }
3015
ca65640f
LM
3016 if (is_sme_pseudo_register (gdbarch, regnum))
3017 return tdep->sme_pseudo_names[regnum - tdep->sme_pseudo_base].c_str ();
3018
34dcc7cf
AH
3019 /* RA_STATE is used for unwinding only. Do not assign it a name - this
3020 prevents it from being read by methods such as
3021 mi_cmd_trace_frame_collected. */
c9cd8ca4 3022 if (tdep->has_pauth () && regnum == tdep->ra_sign_state_regnum)
34dcc7cf
AH
3023 return "";
3024
f34652de 3025 internal_error (_("aarch64_pseudo_register_name: bad register number %d"),
34dcc7cf 3026 p_regnum);
07b287a0
MS
3027}
3028
3029/* Implement the "pseudo_register_type" tdesc_arch_data method. */
3030
3031static struct type *
3032aarch64_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
3033{
08106042 3034 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
63bad7b6 3035
34dcc7cf 3036 int p_regnum = regnum - gdbarch_num_regs (gdbarch);
07b287a0 3037
34dcc7cf 3038 if (p_regnum >= AARCH64_Q0_REGNUM && p_regnum < AARCH64_Q0_REGNUM + 32)
07b287a0
MS
3039 return aarch64_vnq_type (gdbarch);
3040
34dcc7cf 3041 if (p_regnum >= AARCH64_D0_REGNUM && p_regnum < AARCH64_D0_REGNUM + 32)
07b287a0
MS
3042 return aarch64_vnd_type (gdbarch);
3043
34dcc7cf 3044 if (p_regnum >= AARCH64_S0_REGNUM && p_regnum < AARCH64_S0_REGNUM + 32)
07b287a0
MS
3045 return aarch64_vns_type (gdbarch);
3046
34dcc7cf 3047 if (p_regnum >= AARCH64_H0_REGNUM && p_regnum < AARCH64_H0_REGNUM + 32)
07b287a0
MS
3048 return aarch64_vnh_type (gdbarch);
3049
34dcc7cf 3050 if (p_regnum >= AARCH64_B0_REGNUM && p_regnum < AARCH64_B0_REGNUM + 32)
07b287a0
MS
3051 return aarch64_vnb_type (gdbarch);
3052
34dcc7cf
AH
3053 if (tdep->has_sve () && p_regnum >= AARCH64_SVE_V0_REGNUM
3054 && p_regnum < AARCH64_SVE_V0_REGNUM + AARCH64_V_REGS_NUM)
63bad7b6
AH
3055 return aarch64_vnv_type (gdbarch);
3056
e63ae49b
LM
3057 /* W pseudo-registers are 32-bit. */
3058 if (is_w_pseudo_register (gdbarch, regnum))
3059 return builtin_type (gdbarch)->builtin_uint32;
3060
ca65640f
LM
3061 if (is_sme_pseudo_register (gdbarch, regnum))
3062 return aarch64_sme_pseudo_register_type (gdbarch, regnum);
3063
c9cd8ca4 3064 if (tdep->has_pauth () && regnum == tdep->ra_sign_state_regnum)
34dcc7cf
AH
3065 return builtin_type (gdbarch)->builtin_uint64;
3066
f34652de 3067 internal_error (_("aarch64_pseudo_register_type: bad register number %d"),
34dcc7cf 3068 p_regnum);
07b287a0
MS
3069}
3070
3071/* Implement the "pseudo_register_reggroup_p" tdesc_arch_data method. */
3072
3073static int
3074aarch64_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
dbf5d61b 3075 const struct reggroup *group)
07b287a0 3076{
08106042 3077 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
63bad7b6 3078
34dcc7cf 3079 int p_regnum = regnum - gdbarch_num_regs (gdbarch);
07b287a0 3080
34dcc7cf 3081 if (p_regnum >= AARCH64_Q0_REGNUM && p_regnum < AARCH64_Q0_REGNUM + 32)
07b287a0 3082 return group == all_reggroup || group == vector_reggroup;
34dcc7cf 3083 else if (p_regnum >= AARCH64_D0_REGNUM && p_regnum < AARCH64_D0_REGNUM + 32)
07b287a0
MS
3084 return (group == all_reggroup || group == vector_reggroup
3085 || group == float_reggroup);
34dcc7cf 3086 else if (p_regnum >= AARCH64_S0_REGNUM && p_regnum < AARCH64_S0_REGNUM + 32)
07b287a0
MS
3087 return (group == all_reggroup || group == vector_reggroup
3088 || group == float_reggroup);
34dcc7cf 3089 else if (p_regnum >= AARCH64_H0_REGNUM && p_regnum < AARCH64_H0_REGNUM + 32)
07b287a0 3090 return group == all_reggroup || group == vector_reggroup;
34dcc7cf 3091 else if (p_regnum >= AARCH64_B0_REGNUM && p_regnum < AARCH64_B0_REGNUM + 32)
07b287a0 3092 return group == all_reggroup || group == vector_reggroup;
34dcc7cf
AH
3093 else if (tdep->has_sve () && p_regnum >= AARCH64_SVE_V0_REGNUM
3094 && p_regnum < AARCH64_SVE_V0_REGNUM + AARCH64_V_REGS_NUM)
63bad7b6 3095 return group == all_reggroup || group == vector_reggroup;
ca65640f
LM
3096 else if (is_sme_pseudo_register (gdbarch, regnum))
3097 return group == all_reggroup || group == vector_reggroup;
34dcc7cf 3098 /* RA_STATE is used for unwinding only. Do not assign it to any groups. */
c9cd8ca4 3099 if (tdep->has_pauth () && regnum == tdep->ra_sign_state_regnum)
34dcc7cf 3100 return 0;
07b287a0
MS
3101
3102 return group == all_reggroup;
3103}
3104
3c5cd5c3
AH
3105/* Helper for aarch64_pseudo_read_value. */
3106
b3245cef 3107static value *
8480a37e 3108aarch64_pseudo_read_value_1 (const frame_info_ptr &next_frame,
b3245cef 3109 const int pseudo_reg_num, int raw_regnum_offset)
3c5cd5c3 3110{
b3245cef 3111 unsigned v_regnum = AARCH64_V0_REGNUM + raw_regnum_offset;
63bad7b6 3112
b3245cef
SM
3113 return pseudo_from_raw_part (next_frame, pseudo_reg_num, v_regnum, 0);
3114}
3c5cd5c3 3115
ca65640f 3116/* Helper function for reading/writing ZA pseudo-registers. Given REGNUM,
246179ea
SM
3117 a ZA pseudo-register number, return the information on positioning of the
3118 bytes that must be read from/written to. */
ca65640f 3119
246179ea
SM
3120static za_offsets
3121aarch64_za_offsets_from_regnum (struct gdbarch *gdbarch, int regnum)
ca65640f
LM
3122{
3123 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
3124
3125 gdb_assert (tdep->has_sme ());
3126 gdb_assert (tdep->sme_svq > 0);
3127 gdb_assert (tdep->sme_pseudo_base <= regnum);
3128 gdb_assert (regnum < tdep->sme_pseudo_base + tdep->sme_pseudo_count);
3129
3130 struct za_pseudo_encoding encoding;
3131
3132 /* Decode the ZA pseudo-register number. */
3133 aarch64_za_decode_pseudos (gdbarch, regnum, encoding);
3134
3135 /* Fetch the streaming vector length. */
3136 size_t svl = sve_vl_from_vq (tdep->sme_svq);
246179ea 3137 za_offsets offsets;
ca65640f
LM
3138
3139 if (is_sme_tile_slice_pseudo_register (gdbarch, regnum))
3140 {
3141 if (encoding.horizontal)
3142 {
3143 /* Horizontal tile slices are contiguous ranges of svl bytes. */
3144
3145 /* The starting offset depends on the tile index (to locate the tile
3146 in the ZA buffer), the slice index (to locate the slice within the
3147 tile) and the qualifier. */
3148 offsets.starting_offset
3149 = encoding.tile_index * svl + encoding.slice_index
3150 * (svl >> encoding.qualifier_index);
3151 /* Horizontal tile slice data is contiguous and thus doesn't have
3152 a stride. */
3153 offsets.stride_size = 0;
3154 /* Horizontal tile slice data is contiguous and thus only has 1
3155 chunk. */
3156 offsets.chunks = 1;
3157 /* The chunk size is always svl bytes. */
3158 offsets.chunk_size = svl;
3159 }
3160 else
3161 {
3162 /* Vertical tile slices are non-contiguous ranges of
3163 (1 << qualifier_index) bytes. */
3164
3165 /* The starting offset depends on the tile number (to locate the
3166 tile in the ZA buffer), the slice index (to locate the element
3167 within the tile slice) and the qualifier. */
3168 offsets.starting_offset
3169 = encoding.tile_index * svl + encoding.slice_index
3170 * (1 << encoding.qualifier_index);
3171 /* The offset between vertical tile slices depends on the qualifier
3172 and svl. */
3173 offsets.stride_size = svl << encoding.qualifier_index;
3174 /* The number of chunks depends on svl and the qualifier size. */
3175 offsets.chunks = svl >> encoding.qualifier_index;
3176 /* The chunk size depends on the qualifier. */
3177 offsets.chunk_size = 1 << encoding.qualifier_index;
3178 }
3179 }
3180 else
3181 {
3182 /* ZA tile pseudo-register. */
3183
3184 /* Starting offset depends on the tile index and qualifier. */
3185 offsets.starting_offset = encoding.tile_index * svl;
3186 /* The offset between tile slices depends on the qualifier and svl. */
3187 offsets.stride_size = svl << encoding.qualifier_index;
3188 /* The number of chunks depends on the qualifier and svl. */
3189 offsets.chunks = svl >> encoding.qualifier_index;
3190 /* The chunk size is always svl bytes. */
3191 offsets.chunk_size = svl;
3192 }
246179ea
SM
3193
3194 return offsets;
ca65640f
LM
3195}
3196
3197/* Given REGNUM, a SME pseudo-register number, return its value in RESULT. */
3198
b3245cef 3199static value *
8480a37e 3200aarch64_sme_pseudo_register_read (gdbarch *gdbarch, const frame_info_ptr &next_frame,
b3245cef 3201 const int pseudo_reg_num)
ca65640f
LM
3202{
3203 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
3204
3205 gdb_assert (tdep->has_sme ());
3206 gdb_assert (tdep->sme_svq > 0);
b3245cef
SM
3207 gdb_assert (tdep->sme_pseudo_base <= pseudo_reg_num);
3208 gdb_assert (pseudo_reg_num < tdep->sme_pseudo_base + tdep->sme_pseudo_count);
ca65640f
LM
3209
3210 /* Fetch the offsets that we need in order to read from the correct blocks
3211 of ZA. */
246179ea
SM
3212 za_offsets offsets
3213 = aarch64_za_offsets_from_regnum (gdbarch, pseudo_reg_num);
ca65640f
LM
3214
3215 /* Fetch the contents of ZA. */
b3245cef
SM
3216 value *za_value = value_of_register (tdep->sme_za_regnum, next_frame);
3217 value *result = value::allocate_register (next_frame, pseudo_reg_num);
ca65640f
LM
3218
3219 /* Copy the requested data. */
3220 for (int chunks = 0; chunks < offsets.chunks; chunks++)
3221 {
b3245cef
SM
3222 int src_offset = offsets.starting_offset + chunks * offsets.stride_size;
3223 int dst_offset = chunks * offsets.chunk_size;
3224 za_value->contents_copy (result, dst_offset, src_offset,
3225 offsets.chunk_size);
ca65640f 3226 }
b3245cef 3227
ca65640f
LM
3228 return result;
3229}
3230
07b287a0
MS
3231/* Implement the "pseudo_register_read_value" gdbarch method. */
3232
b3245cef 3233static value *
8480a37e 3234aarch64_pseudo_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
b3245cef 3235 const int pseudo_reg_num)
07b287a0 3236{
08106042 3237 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0 3238
b3245cef 3239 if (is_w_pseudo_register (gdbarch, pseudo_reg_num))
e63ae49b
LM
3240 {
3241 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3242 /* Default offset for little endian. */
3243 int offset = 0;
3244
3245 if (byte_order == BFD_ENDIAN_BIG)
3246 offset = 4;
3247
3248 /* Find the correct X register to extract the data from. */
b3245cef
SM
3249 int x_regnum
3250 = AARCH64_X0_REGNUM + (pseudo_reg_num - tdep->w_pseudo_base);
e63ae49b
LM
3251
3252 /* Read the bottom 4 bytes of X. */
b3245cef
SM
3253 return pseudo_from_raw_part (next_frame, pseudo_reg_num, x_regnum,
3254 offset);
e63ae49b 3255 }
b3245cef
SM
3256 else if (is_sme_pseudo_register (gdbarch, pseudo_reg_num))
3257 return aarch64_sme_pseudo_register_read (gdbarch, next_frame,
3258 pseudo_reg_num);
3259
3260 /* Offset in the "pseudo-register space". */
3261 int pseudo_offset = pseudo_reg_num - gdbarch_num_regs (gdbarch);
3262
3263 if (pseudo_offset >= AARCH64_Q0_REGNUM
3264 && pseudo_offset < AARCH64_Q0_REGNUM + 32)
3265 return aarch64_pseudo_read_value_1 (next_frame, pseudo_reg_num,
3266 pseudo_offset - AARCH64_Q0_REGNUM);
3267
3268 if (pseudo_offset >= AARCH64_D0_REGNUM
3269 && pseudo_offset < AARCH64_D0_REGNUM + 32)
3270 return aarch64_pseudo_read_value_1 (next_frame, pseudo_reg_num,
3271 pseudo_offset - AARCH64_D0_REGNUM);
3272
3273 if (pseudo_offset >= AARCH64_S0_REGNUM
3274 && pseudo_offset < AARCH64_S0_REGNUM + 32)
3275 return aarch64_pseudo_read_value_1 (next_frame, pseudo_reg_num,
3276 pseudo_offset - AARCH64_S0_REGNUM);
3277
3278 if (pseudo_offset >= AARCH64_H0_REGNUM
3279 && pseudo_offset < AARCH64_H0_REGNUM + 32)
3280 return aarch64_pseudo_read_value_1 (next_frame, pseudo_reg_num,
3281 pseudo_offset - AARCH64_H0_REGNUM);
3282
3283 if (pseudo_offset >= AARCH64_B0_REGNUM
3284 && pseudo_offset < AARCH64_B0_REGNUM + 32)
3285 return aarch64_pseudo_read_value_1 (next_frame, pseudo_reg_num,
3286 pseudo_offset - AARCH64_B0_REGNUM);
3287
3288 if (tdep->has_sve () && pseudo_offset >= AARCH64_SVE_V0_REGNUM
3289 && pseudo_offset < AARCH64_SVE_V0_REGNUM + 32)
3290 return aarch64_pseudo_read_value_1 (next_frame, pseudo_reg_num,
3291 pseudo_offset - AARCH64_SVE_V0_REGNUM);
63bad7b6 3292
5d43cdc1
KG
3293 if (tdep->has_pauth () && pseudo_reg_num == tdep->ra_sign_state_regnum)
3294 return value::zero (builtin_type (gdbarch)->builtin_uint64, lval_register);
3295
07b287a0
MS
3296 gdb_assert_not_reached ("regnum out of bound");
3297}
3298
3c5cd5c3 3299/* Helper for aarch64_pseudo_write. */
07b287a0
MS
3300
3301static void
8480a37e 3302aarch64_pseudo_write_1 (gdbarch *gdbarch, const frame_info_ptr &next_frame,
bdbf4262
SM
3303 int regnum_offset,
3304 gdb::array_view<const gdb_byte> buf)
07b287a0 3305{
bdbf4262 3306 unsigned raw_regnum = AARCH64_V0_REGNUM + regnum_offset;
07b287a0 3307
d724d71a 3308 /* Enough space for a full vector register.
63bad7b6 3309
d724d71a 3310 Ensure the register buffer is zero, we want gdb writes of the
07b287a0
MS
3311 various 'scalar' pseudo registers to behavior like architectural
3312 writes, register width bytes are written the remainder are set to
3313 zero. */
d724d71a
SM
3314 gdb::byte_vector raw_buf (register_size (gdbarch, raw_regnum), 0);
3315 static_assert (AARCH64_V0_REGNUM == AARCH64_SVE_Z0_REGNUM);
07b287a0 3316
d724d71a 3317 gdb::array_view<gdb_byte> raw_view (raw_buf);
bdbf4262
SM
3318 copy (buf, raw_view.slice (0, buf.size ()));
3319 put_frame_register (next_frame, raw_regnum, raw_view);
3c5cd5c3
AH
3320}
3321
ca65640f
LM
3322/* Given REGNUM, a SME pseudo-register number, store the bytes from DATA to the
3323 pseudo-register. */
3324
3325static void
8480a37e 3326aarch64_sme_pseudo_register_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
bdbf4262
SM
3327 const int regnum,
3328 gdb::array_view<const gdb_byte> data)
ca65640f
LM
3329{
3330 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
3331
3332 gdb_assert (tdep->has_sme ());
3333 gdb_assert (tdep->sme_svq > 0);
3334 gdb_assert (tdep->sme_pseudo_base <= regnum);
3335 gdb_assert (regnum < tdep->sme_pseudo_base + tdep->sme_pseudo_count);
3336
3337 /* Fetch the offsets that we need in order to write to the correct blocks
3338 of ZA. */
246179ea 3339 za_offsets offsets = aarch64_za_offsets_from_regnum (gdbarch, regnum);
ca65640f
LM
3340
3341 /* Fetch the contents of ZA. */
bdbf4262 3342 value *za_value = value_of_register (tdep->sme_za_regnum, next_frame);
ca65640f 3343
bdbf4262
SM
3344 {
3345 /* Create a view only on the portion of za we want to write. */
3346 gdb::array_view<gdb_byte> za_view
3347 = za_value->contents_writeable ().slice (offsets.starting_offset);
ca65640f 3348
bdbf4262
SM
3349 /* Copy the requested data. */
3350 for (int chunks = 0; chunks < offsets.chunks; chunks++)
3351 {
3352 gdb::array_view<const gdb_byte> src
3353 = data.slice (chunks * offsets.chunk_size, offsets.chunk_size);
3354 gdb::array_view<gdb_byte> dst
3355 = za_view.slice (chunks * offsets.stride_size, offsets.chunk_size);
3356 copy (src, dst);
3357 }
3358 }
ca65640f
LM
3359
3360 /* Write back to ZA. */
bdbf4262
SM
3361 put_frame_register (next_frame, tdep->sme_za_regnum,
3362 za_value->contents_raw ());
ca65640f
LM
3363}
3364
3c5cd5c3
AH
3365/* Implement the "pseudo_register_write" gdbarch method. */
3366
3367static void
8480a37e 3368aarch64_pseudo_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
bdbf4262
SM
3369 const int pseudo_reg_num,
3370 gdb::array_view<const gdb_byte> buf)
3c5cd5c3 3371{
08106042 3372 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
e63ae49b 3373
bdbf4262 3374 if (is_w_pseudo_register (gdbarch, pseudo_reg_num))
e63ae49b
LM
3375 {
3376 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3377 /* Default offset for little endian. */
3378 int offset = 0;
3379
3380 if (byte_order == BFD_ENDIAN_BIG)
3381 offset = 4;
3382
3383 /* Find the correct X register to extract the data from. */
bdbf4262 3384 int x_regnum = AARCH64_X0_REGNUM + (pseudo_reg_num - tdep->w_pseudo_base);
e63ae49b
LM
3385
3386 /* First zero-out the contents of X. */
bdbf4262
SM
3387 gdb_byte bytes[8] {};
3388 gdb::array_view<gdb_byte> bytes_view (bytes);
3389 copy (buf, bytes_view.slice (offset, 4));
3390
e63ae49b 3391 /* Write to the bottom 4 bytes of X. */
bdbf4262 3392 put_frame_register (next_frame, x_regnum, bytes_view);
e63ae49b
LM
3393 return;
3394 }
bdbf4262 3395 else if (is_sme_pseudo_register (gdbarch, pseudo_reg_num))
ca65640f 3396 {
bdbf4262
SM
3397 aarch64_sme_pseudo_register_write (gdbarch, next_frame, pseudo_reg_num,
3398 buf);
ca65640f
LM
3399 return;
3400 }
e63ae49b 3401
bdbf4262
SM
3402 /* Offset in the "pseudo-register space". */
3403 int pseudo_offset = pseudo_reg_num - gdbarch_num_regs (gdbarch);
07b287a0 3404
bdbf4262
SM
3405 if (pseudo_offset >= AARCH64_Q0_REGNUM
3406 && pseudo_offset < AARCH64_Q0_REGNUM + 32)
3407 return aarch64_pseudo_write_1 (gdbarch, next_frame,
3408 pseudo_offset - AARCH64_Q0_REGNUM, buf);
07b287a0 3409
bdbf4262
SM
3410 if (pseudo_offset >= AARCH64_D0_REGNUM
3411 && pseudo_offset < AARCH64_D0_REGNUM + 32)
3412 return aarch64_pseudo_write_1 (gdbarch, next_frame,
3413 pseudo_offset - AARCH64_D0_REGNUM, buf);
07b287a0 3414
bdbf4262
SM
3415 if (pseudo_offset >= AARCH64_S0_REGNUM
3416 && pseudo_offset < AARCH64_S0_REGNUM + 32)
3417 return aarch64_pseudo_write_1 (gdbarch, next_frame,
3418 pseudo_offset - AARCH64_S0_REGNUM, buf);
07b287a0 3419
bdbf4262
SM
3420 if (pseudo_offset >= AARCH64_H0_REGNUM
3421 && pseudo_offset < AARCH64_H0_REGNUM + 32)
3422 return aarch64_pseudo_write_1 (gdbarch, next_frame,
3423 pseudo_offset - AARCH64_H0_REGNUM, buf);
07b287a0 3424
bdbf4262
SM
3425 if (pseudo_offset >= AARCH64_B0_REGNUM
3426 && pseudo_offset < AARCH64_B0_REGNUM + 32)
3427 return aarch64_pseudo_write_1 (gdbarch, next_frame,
3428 pseudo_offset - AARCH64_B0_REGNUM, buf);
63bad7b6 3429
bdbf4262
SM
3430 if (tdep->has_sve () && pseudo_offset >= AARCH64_SVE_V0_REGNUM
3431 && pseudo_offset < AARCH64_SVE_V0_REGNUM + 32)
3432 return aarch64_pseudo_write_1 (gdbarch, next_frame,
3433 pseudo_offset - AARCH64_SVE_V0_REGNUM, buf);
07b287a0
MS
3434
3435 gdb_assert_not_reached ("regnum out of bound");
3436}
3437
07b287a0
MS
3438/* Callback function for user_reg_add. */
3439
3440static struct value *
8480a37e 3441value_of_aarch64_user_reg (const frame_info_ptr &frame, const void *baton)
07b287a0 3442{
9a3c8263 3443 const int *reg_p = (const int *) baton;
07b287a0 3444
a7952927 3445 return value_of_register (*reg_p, get_next_frame_sentinel_okay (frame));
07b287a0 3446}
07b287a0 3447
9404b58f
KM
3448/* Implement the "software_single_step" gdbarch method, needed to
3449 single step through atomic sequences on AArch64. */
3450
a0ff9e1a 3451static std::vector<CORE_ADDR>
f5ea389a 3452aarch64_software_single_step (struct regcache *regcache)
9404b58f 3453{
ac7936df 3454 struct gdbarch *gdbarch = regcache->arch ();
9404b58f
KM
3455 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3456 const int insn_size = 4;
3457 const int atomic_sequence_length = 16; /* Instruction sequence length. */
0187a92f 3458 CORE_ADDR pc = regcache_read_pc (regcache);
70ab8ccd 3459 CORE_ADDR breaks[2] = { CORE_ADDR_MAX, CORE_ADDR_MAX };
9404b58f
KM
3460 CORE_ADDR loc = pc;
3461 CORE_ADDR closing_insn = 0;
94355de7
LM
3462
3463 ULONGEST insn_from_memory;
3464 if (!safe_read_memory_unsigned_integer (loc, insn_size,
3465 byte_order_for_code,
3466 &insn_from_memory))
3467 {
3468 /* Assume we don't have a atomic sequence, as we couldn't read the
3469 instruction in this location. */
3470 return {};
3471 }
3472
3473 uint32_t insn = insn_from_memory;
9404b58f
KM
3474 int index;
3475 int insn_count;
3476 int bc_insn_count = 0; /* Conditional branch instruction count. */
3477 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
f77ee802
YQ
3478 aarch64_inst inst;
3479
561a72d4 3480 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
a0ff9e1a 3481 return {};
9404b58f
KM
3482
3483 /* Look for a Load Exclusive instruction which begins the sequence. */
f77ee802 3484 if (inst.opcode->iclass != ldstexcl || bit (insn, 22) == 0)
a0ff9e1a 3485 return {};
9404b58f
KM
3486
3487 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
3488 {
9404b58f 3489 loc += insn_size;
9404b58f 3490
94355de7
LM
3491 if (!safe_read_memory_unsigned_integer (loc, insn_size,
3492 byte_order_for_code,
3493 &insn_from_memory))
3494 {
3495 /* Assume we don't have a atomic sequence, as we couldn't read the
3496 instruction in this location. */
3497 return {};
3498 }
3499
3500 insn = insn_from_memory;
561a72d4 3501 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
a0ff9e1a 3502 return {};
9404b58f 3503 /* Check if the instruction is a conditional branch. */
f77ee802 3504 if (inst.opcode->iclass == condbranch)
9404b58f 3505 {
f77ee802
YQ
3506 gdb_assert (inst.operands[0].type == AARCH64_OPND_ADDR_PCREL19);
3507
9404b58f 3508 if (bc_insn_count >= 1)
a0ff9e1a 3509 return {};
9404b58f
KM
3510
3511 /* It is, so we'll try to set a breakpoint at the destination. */
f77ee802 3512 breaks[1] = loc + inst.operands[0].imm.value;
9404b58f
KM
3513
3514 bc_insn_count++;
3515 last_breakpoint++;
3516 }
3517
3518 /* Look for the Store Exclusive which closes the atomic sequence. */
f77ee802 3519 if (inst.opcode->iclass == ldstexcl && bit (insn, 22) == 0)
9404b58f
KM
3520 {
3521 closing_insn = loc;
3522 break;
3523 }
3524 }
3525
3526 /* We didn't find a closing Store Exclusive instruction, fall back. */
3527 if (!closing_insn)
a0ff9e1a 3528 return {};
9404b58f
KM
3529
3530 /* Insert breakpoint after the end of the atomic sequence. */
3531 breaks[0] = loc + insn_size;
3532
3533 /* Check for duplicated breakpoints, and also check that the second
3534 breakpoint is not within the atomic sequence. */
3535 if (last_breakpoint
3536 && (breaks[1] == breaks[0]
3537 || (breaks[1] >= pc && breaks[1] <= closing_insn)))
3538 last_breakpoint = 0;
3539
a0ff9e1a
SM
3540 std::vector<CORE_ADDR> next_pcs;
3541
9404b58f
KM
3542 /* Insert the breakpoint at the end of the sequence, and one at the
3543 destination of the conditional branch, if it exists. */
3544 for (index = 0; index <= last_breakpoint; index++)
a0ff9e1a 3545 next_pcs.push_back (breaks[index]);
9404b58f 3546
93f9a11f 3547 return next_pcs;
9404b58f
KM
3548}
3549
1152d984
SM
3550struct aarch64_displaced_step_copy_insn_closure
3551 : public displaced_step_copy_insn_closure
b6542f81
YQ
3552{
3553 /* It is true when condition instruction, such as B.CON, TBZ, etc,
3554 is being displaced stepping. */
f0c702d4 3555 bool cond = false;
b6542f81 3556
0c271889
LM
3557 /* PC adjustment offset after displaced stepping. If 0, then we don't
3558 write the PC back, assuming the PC is already the right address. */
cfba9872 3559 int32_t pc_adjust = 0;
b6542f81
YQ
3560};
3561
3562/* Data when visiting instructions for displaced stepping. */
3563
3564struct aarch64_displaced_step_data
3565{
3566 struct aarch64_insn_data base;
3567
3568 /* The address where the instruction will be executed at. */
3569 CORE_ADDR new_addr;
3570 /* Buffer of instructions to be copied to NEW_ADDR to execute. */
e935475c 3571 uint32_t insn_buf[AARCH64_DISPLACED_MODIFIED_INSNS];
b6542f81
YQ
3572 /* Number of instructions in INSN_BUF. */
3573 unsigned insn_count;
3574 /* Registers when doing displaced stepping. */
3575 struct regcache *regs;
3576
1152d984 3577 aarch64_displaced_step_copy_insn_closure *dsc;
b6542f81
YQ
3578};
3579
3580/* Implementation of aarch64_insn_visitor method "b". */
3581
3582static void
3583aarch64_displaced_step_b (const int is_bl, const int32_t offset,
3584 struct aarch64_insn_data *data)
3585{
3586 struct aarch64_displaced_step_data *dsd
3587 = (struct aarch64_displaced_step_data *) data;
2ac09a5b 3588 int64_t new_offset = data->insn_addr - dsd->new_addr + offset;
b6542f81
YQ
3589
3590 if (can_encode_int32 (new_offset, 28))
3591 {
3592 /* Emit B rather than BL, because executing BL on a new address
3593 will get the wrong address into LR. In order to avoid this,
3594 we emit B, and update LR if the instruction is BL. */
3595 emit_b (dsd->insn_buf, 0, new_offset);
3596 dsd->insn_count++;
3597 }
3598 else
3599 {
3600 /* Write NOP. */
3601 emit_nop (dsd->insn_buf);
3602 dsd->insn_count++;
3603 dsd->dsc->pc_adjust = offset;
3604 }
3605
3606 if (is_bl)
3607 {
3608 /* Update LR. */
3609 regcache_cooked_write_unsigned (dsd->regs, AARCH64_LR_REGNUM,
3610 data->insn_addr + 4);
3611 }
3612}
3613
3614/* Implementation of aarch64_insn_visitor method "b_cond". */
3615
3616static void
3617aarch64_displaced_step_b_cond (const unsigned cond, const int32_t offset,
3618 struct aarch64_insn_data *data)
3619{
3620 struct aarch64_displaced_step_data *dsd
3621 = (struct aarch64_displaced_step_data *) data;
b6542f81
YQ
3622
3623 /* GDB has to fix up PC after displaced step this instruction
3624 differently according to the condition is true or false. Instead
3625 of checking COND against conditional flags, we can use
3626 the following instructions, and GDB can tell how to fix up PC
3627 according to the PC value.
3628
3629 B.COND TAKEN ; If cond is true, then jump to TAKEN.
3630 INSN1 ;
3631 TAKEN:
3632 INSN2
3633 */
3634
3635 emit_bcond (dsd->insn_buf, cond, 8);
f0c702d4 3636 dsd->dsc->cond = true;
b6542f81
YQ
3637 dsd->dsc->pc_adjust = offset;
3638 dsd->insn_count = 1;
3639}
3640
3641/* Dynamically allocate a new register. If we know the register
3642 statically, we should make it a global as above instead of using this
3643 helper function. */
3644
3645static struct aarch64_register
3646aarch64_register (unsigned num, int is64)
3647{
3648 return (struct aarch64_register) { num, is64 };
3649}
3650
3651/* Implementation of aarch64_insn_visitor method "cb". */
3652
3653static void
3654aarch64_displaced_step_cb (const int32_t offset, const int is_cbnz,
3655 const unsigned rn, int is64,
3656 struct aarch64_insn_data *data)
3657{
3658 struct aarch64_displaced_step_data *dsd
3659 = (struct aarch64_displaced_step_data *) data;
b6542f81
YQ
3660
3661 /* The offset is out of range for a compare and branch
3662 instruction. We can use the following instructions instead:
3663
3664 CBZ xn, TAKEN ; xn == 0, then jump to TAKEN.
3665 INSN1 ;
3666 TAKEN:
3667 INSN2
3668 */
3669 emit_cb (dsd->insn_buf, is_cbnz, aarch64_register (rn, is64), 8);
3670 dsd->insn_count = 1;
f0c702d4 3671 dsd->dsc->cond = true;
b6542f81
YQ
3672 dsd->dsc->pc_adjust = offset;
3673}
3674
3675/* Implementation of aarch64_insn_visitor method "tb". */
3676
3677static void
3678aarch64_displaced_step_tb (const int32_t offset, int is_tbnz,
3679 const unsigned rt, unsigned bit,
3680 struct aarch64_insn_data *data)
3681{
3682 struct aarch64_displaced_step_data *dsd
3683 = (struct aarch64_displaced_step_data *) data;
b6542f81
YQ
3684
3685 /* The offset is out of range for a test bit and branch
3686 instruction We can use the following instructions instead:
3687
3688 TBZ xn, #bit, TAKEN ; xn[bit] == 0, then jump to TAKEN.
3689 INSN1 ;
3690 TAKEN:
3691 INSN2
3692
3693 */
3694 emit_tb (dsd->insn_buf, is_tbnz, bit, aarch64_register (rt, 1), 8);
3695 dsd->insn_count = 1;
f0c702d4 3696 dsd->dsc->cond = true;
b6542f81
YQ
3697 dsd->dsc->pc_adjust = offset;
3698}
3699
3700/* Implementation of aarch64_insn_visitor method "adr". */
3701
3702static void
3703aarch64_displaced_step_adr (const int32_t offset, const unsigned rd,
3704 const int is_adrp, struct aarch64_insn_data *data)
3705{
3706 struct aarch64_displaced_step_data *dsd
3707 = (struct aarch64_displaced_step_data *) data;
3708 /* We know exactly the address the ADR{P,} instruction will compute.
3709 We can just write it to the destination register. */
3710 CORE_ADDR address = data->insn_addr + offset;
3711
3712 if (is_adrp)
3713 {
3714 /* Clear the lower 12 bits of the offset to get the 4K page. */
3715 regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rd,
3716 address & ~0xfff);
3717 }
3718 else
3719 regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rd,
3720 address);
3721
3722 dsd->dsc->pc_adjust = 4;
3723 emit_nop (dsd->insn_buf);
3724 dsd->insn_count = 1;
3725}
3726
3727/* Implementation of aarch64_insn_visitor method "ldr_literal". */
3728
3729static void
3730aarch64_displaced_step_ldr_literal (const int32_t offset, const int is_sw,
3731 const unsigned rt, const int is64,
3732 struct aarch64_insn_data *data)
3733{
3734 struct aarch64_displaced_step_data *dsd
3735 = (struct aarch64_displaced_step_data *) data;
3736 CORE_ADDR address = data->insn_addr + offset;
3737 struct aarch64_memory_operand zero = { MEMORY_OPERAND_OFFSET, 0 };
3738
3739 regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rt,
3740 address);
3741
3742 if (is_sw)
3743 dsd->insn_count = emit_ldrsw (dsd->insn_buf, aarch64_register (rt, 1),
3744 aarch64_register (rt, 1), zero);
3745 else
3746 dsd->insn_count = emit_ldr (dsd->insn_buf, aarch64_register (rt, is64),
3747 aarch64_register (rt, 1), zero);
3748
3749 dsd->dsc->pc_adjust = 4;
3750}
3751
3752/* Implementation of aarch64_insn_visitor method "others". */
3753
3754static void
3755aarch64_displaced_step_others (const uint32_t insn,
3756 struct aarch64_insn_data *data)
3757{
3758 struct aarch64_displaced_step_data *dsd
3759 = (struct aarch64_displaced_step_data *) data;
3760
807f647c
MM
3761 uint32_t masked_insn = (insn & CLEAR_Rn_MASK);
3762 if (masked_insn == BLR)
b6542f81 3763 {
807f647c
MM
3764 /* Emit a BR to the same register and then update LR to the original
3765 address (similar to aarch64_displaced_step_b). */
3766 aarch64_emit_insn (dsd->insn_buf, insn & 0xffdfffff);
3767 regcache_cooked_write_unsigned (dsd->regs, AARCH64_LR_REGNUM,
3768 data->insn_addr + 4);
b6542f81 3769 }
807f647c
MM
3770 else
3771 aarch64_emit_insn (dsd->insn_buf, insn);
3772 dsd->insn_count = 1;
3773
3774 if (masked_insn == RET || masked_insn == BR || masked_insn == BLR)
3775 dsd->dsc->pc_adjust = 0;
b6542f81
YQ
3776 else
3777 dsd->dsc->pc_adjust = 4;
3778}
3779
3780static const struct aarch64_insn_visitor visitor =
3781{
3782 aarch64_displaced_step_b,
3783 aarch64_displaced_step_b_cond,
3784 aarch64_displaced_step_cb,
3785 aarch64_displaced_step_tb,
3786 aarch64_displaced_step_adr,
3787 aarch64_displaced_step_ldr_literal,
3788 aarch64_displaced_step_others,
3789};
3790
3791/* Implement the "displaced_step_copy_insn" gdbarch method. */
3792
1152d984 3793displaced_step_copy_insn_closure_up
b6542f81
YQ
3794aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
3795 CORE_ADDR from, CORE_ADDR to,
3796 struct regcache *regs)
3797{
b6542f81 3798 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
b6542f81 3799 struct aarch64_displaced_step_data dsd;
c86a40c6 3800 aarch64_inst inst;
94355de7
LM
3801 ULONGEST insn_from_memory;
3802
3803 if (!safe_read_memory_unsigned_integer (from, 4, byte_order_for_code,
3804 &insn_from_memory))
3805 return nullptr;
3806
3807 uint32_t insn = insn_from_memory;
c86a40c6 3808
561a72d4 3809 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
c86a40c6 3810 return NULL;
b6542f81 3811
b995344c
TJB
3812 /* Look for a Load Exclusive instruction which begins the sequence,
3813 or for a MOPS instruction. */
3814 if ((inst.opcode->iclass == ldstexcl && bit (insn, 22))
3815 || AARCH64_CPU_HAS_FEATURE (*inst.opcode->avariant, MOPS))
b6542f81 3816 {
b995344c 3817 /* We can't displaced step atomic sequences nor MOPS instructions. */
b6542f81
YQ
3818 return NULL;
3819 }
3820
1152d984
SM
3821 std::unique_ptr<aarch64_displaced_step_copy_insn_closure> dsc
3822 (new aarch64_displaced_step_copy_insn_closure);
b6542f81
YQ
3823 dsd.base.insn_addr = from;
3824 dsd.new_addr = to;
3825 dsd.regs = regs;
cfba9872 3826 dsd.dsc = dsc.get ();
034f1a81 3827 dsd.insn_count = 0;
b6542f81
YQ
3828 aarch64_relocate_instruction (insn, &visitor,
3829 (struct aarch64_insn_data *) &dsd);
e935475c 3830 gdb_assert (dsd.insn_count <= AARCH64_DISPLACED_MODIFIED_INSNS);
b6542f81
YQ
3831
3832 if (dsd.insn_count != 0)
3833 {
3834 int i;
3835
3836 /* Instruction can be relocated to scratch pad. Copy
3837 relocated instruction(s) there. */
3838 for (i = 0; i < dsd.insn_count; i++)
3839 {
136821d9
SM
3840 displaced_debug_printf ("writing insn %.8x at %s",
3841 dsd.insn_buf[i],
3842 paddress (gdbarch, to + i * 4));
3843
b6542f81
YQ
3844 write_memory_unsigned_integer (to + i * 4, 4, byte_order_for_code,
3845 (ULONGEST) dsd.insn_buf[i]);
3846 }
3847 }
3848 else
3849 {
b6542f81
YQ
3850 dsc = NULL;
3851 }
3852
6d0cf446 3853 /* This is a work around for a problem with g++ 4.8. */
1152d984 3854 return displaced_step_copy_insn_closure_up (dsc.release ());
b6542f81
YQ
3855}
3856
3857/* Implement the "displaced_step_fixup" gdbarch method. */
3858
3859void
3860aarch64_displaced_step_fixup (struct gdbarch *gdbarch,
1152d984 3861 struct displaced_step_copy_insn_closure *dsc_,
b6542f81 3862 CORE_ADDR from, CORE_ADDR to,
cf141dd8 3863 struct regcache *regs, bool completed_p)
b6542f81 3864{
cf141dd8 3865 CORE_ADDR pc = regcache_read_pc (regs);
cfba9872 3866
cf141dd8
AB
3867 /* If the displaced instruction didn't complete successfully then all we
3868 need to do is restore the program counter. */
3869 if (!completed_p)
3870 {
3871 pc = from + (pc - to);
3872 regcache_write_pc (regs, pc);
3873 return;
3874 }
0c271889 3875
cf141dd8
AB
3876 aarch64_displaced_step_copy_insn_closure *dsc
3877 = (aarch64_displaced_step_copy_insn_closure *) dsc_;
0c271889 3878
136821d9
SM
3879 displaced_debug_printf ("PC after stepping: %s (was %s).",
3880 paddress (gdbarch, pc), paddress (gdbarch, to));
1ab139e5 3881
b6542f81
YQ
3882 if (dsc->cond)
3883 {
136821d9
SM
3884 displaced_debug_printf ("[Conditional] pc_adjust before: %d",
3885 dsc->pc_adjust);
1ab139e5 3886
b6542f81
YQ
3887 if (pc - to == 8)
3888 {
3889 /* Condition is true. */
3890 }
3891 else if (pc - to == 4)
3892 {
3893 /* Condition is false. */
3894 dsc->pc_adjust = 4;
3895 }
3896 else
3897 gdb_assert_not_reached ("Unexpected PC value after displaced stepping");
1ab139e5 3898
136821d9
SM
3899 displaced_debug_printf ("[Conditional] pc_adjust after: %d",
3900 dsc->pc_adjust);
b6542f81
YQ
3901 }
3902
136821d9
SM
3903 displaced_debug_printf ("%s PC by %d",
3904 dsc->pc_adjust ? "adjusting" : "not adjusting",
3905 dsc->pc_adjust);
1ab139e5 3906
b6542f81
YQ
3907 if (dsc->pc_adjust != 0)
3908 {
0c271889
LM
3909 /* Make sure the previous instruction was executed (that is, the PC
3910 has changed). If the PC didn't change, then discard the adjustment
3911 offset. Otherwise we may skip an instruction before its execution
3912 took place. */
3913 if ((pc - to) == 0)
1ab139e5 3914 {
136821d9 3915 displaced_debug_printf ("PC did not move. Discarding PC adjustment.");
1ab139e5
LM
3916 dsc->pc_adjust = 0;
3917 }
0c271889 3918
136821d9
SM
3919 displaced_debug_printf ("fixup: set PC to %s:%d",
3920 paddress (gdbarch, from), dsc->pc_adjust);
3921
b6542f81
YQ
3922 regcache_cooked_write_unsigned (regs, AARCH64_PC_REGNUM,
3923 from + dsc->pc_adjust);
3924 }
3925}
3926
3927/* Implement the "displaced_step_hw_singlestep" gdbarch method. */
3928
07fbbd01 3929bool
40a53766 3930aarch64_displaced_step_hw_singlestep (struct gdbarch *gdbarch)
b6542f81 3931{
07fbbd01 3932 return true;
b6542f81
YQ
3933}
3934
95228a0d
AH
3935/* Get the correct target description for the given VQ value.
3936 If VQ is zero then it is assumed SVE is not supported.
c1bd443b
LM
3937 (It is not possible to set VQ to zero on an SVE system).
3938
414d5848
JB
3939 MTE_P indicates the presence of the Memory Tagging Extension feature.
3940
3941 TLS_P indicates the presence of the Thread Local Storage feature. */
da434ccb
AH
3942
3943const target_desc *
0ee6b1c5 3944aarch64_read_description (const aarch64_features &features)
da434ccb 3945{
0ee6b1c5
JB
3946 if (features.vq > AARCH64_MAX_SVE_VQ)
3947 error (_("VQ is %" PRIu64 ", maximum supported value is %d"), features.vq,
95228a0d
AH
3948 AARCH64_MAX_SVE_VQ);
3949
0ee6b1c5 3950 struct target_desc *tdesc = tdesc_aarch64_map[features];
da434ccb 3951
95228a0d
AH
3952 if (tdesc == NULL)
3953 {
0ee6b1c5
JB
3954 tdesc = aarch64_create_target_description (features);
3955 tdesc_aarch64_map[features] = tdesc;
95228a0d 3956 }
da434ccb 3957
95228a0d 3958 return tdesc;
da434ccb
AH
3959}
3960
ba2d2bb2
AH
3961/* Return the VQ used when creating the target description TDESC. */
3962
1332a140 3963static uint64_t
ba2d2bb2
AH
3964aarch64_get_tdesc_vq (const struct target_desc *tdesc)
3965{
3966 const struct tdesc_feature *feature_sve;
3967
3968 if (!tdesc_has_registers (tdesc))
3969 return 0;
3970
3971 feature_sve = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sve");
3972
3973 if (feature_sve == nullptr)
3974 return 0;
3975
12863263
AH
3976 uint64_t vl = tdesc_register_bitsize (feature_sve,
3977 aarch64_sve_register_names[0]) / 8;
ba2d2bb2
AH
3978 return sve_vq_from_vl (vl);
3979}
3980
ca65640f
LM
3981
3982/* Return the svq (streaming vector quotient) used when creating the target
3983 description TDESC. */
3984
3985static uint64_t
3986aarch64_get_tdesc_svq (const struct target_desc *tdesc)
3987{
3988 const struct tdesc_feature *feature_sme;
3989
3990 if (!tdesc_has_registers (tdesc))
3991 return 0;
3992
3993 feature_sme = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sme");
3994
3995 if (feature_sme == nullptr)
3996 return 0;
3997
3998 size_t svl_squared = tdesc_register_bitsize (feature_sme, "za");
3999
4000 /* We have the total size of the ZA matrix, in bits. Figure out the svl
4001 value. */
4002 size_t svl = std::sqrt (svl_squared / 8);
4003
4004 /* Now extract svq. */
4005 return sve_vq_from_vl (svl);
4006}
4007
4f3681cc
TJB
4008/* Get the AArch64 features present in the given target description. */
4009
4010aarch64_features
4011aarch64_features_from_target_desc (const struct target_desc *tdesc)
4012{
4013 aarch64_features features;
4014
4015 if (tdesc == nullptr)
4016 return features;
4017
4018 features.vq = aarch64_get_tdesc_vq (tdesc);
acdf6071
LM
4019
4020 /* We need to look for a couple pauth feature name variations. */
4f3681cc
TJB
4021 features.pauth
4022 = (tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.pauth") != nullptr);
acdf6071
LM
4023
4024 if (!features.pauth)
4025 features.pauth = (tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.pauth_v2")
4026 != nullptr);
4027
4f3681cc
TJB
4028 features.mte
4029 = (tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.mte") != nullptr);
ba60b963
LM
4030
4031 const struct tdesc_feature *tls_feature
4032 = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.tls");
4033
4034 if (tls_feature != nullptr)
4035 {
4036 /* We have TLS registers. Find out how many. */
4037 if (tdesc_unnumbered_register (tls_feature, "tpidr2"))
4038 features.tls = 2;
4039 else
4040 features.tls = 1;
4041 }
4f3681cc 4042
ca65640f
LM
4043 features.svq = aarch64_get_tdesc_svq (tdesc);
4044
42019af6
LM
4045 /* Check for the SME2 feature. */
4046 features.sme2 = (tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sme2")
4047 != nullptr);
4048
4f3681cc
TJB
4049 return features;
4050}
4051
76bed0fd
AH
4052/* Implement the "cannot_store_register" gdbarch method. */
4053
4054static int
4055aarch64_cannot_store_register (struct gdbarch *gdbarch, int regnum)
4056{
08106042 4057 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
76bed0fd
AH
4058
4059 if (!tdep->has_pauth ())
4060 return 0;
4061
4062 /* Pointer authentication registers are read-only. */
6d002087
LM
4063 return (regnum >= tdep->pauth_reg_base
4064 && regnum < tdep->pauth_reg_base + tdep->pauth_reg_count);
76bed0fd
AH
4065}
4066
da729c5c
TT
4067/* Implement the stack_frame_destroyed_p gdbarch method. */
4068
4069static int
4070aarch64_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4071{
4072 CORE_ADDR func_start, func_end;
4073 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4074 return 0;
4075
4076 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
94355de7
LM
4077
4078 ULONGEST insn_from_memory;
4079 if (!safe_read_memory_unsigned_integer (pc, 4, byte_order_for_code,
4080 &insn_from_memory))
4081 return 0;
4082
4083 uint32_t insn = insn_from_memory;
da729c5c
TT
4084
4085 aarch64_inst inst;
4086 if (aarch64_decode_insn (insn, &inst, 1, nullptr) != 0)
4087 return 0;
4088
4089 return streq (inst.opcode->name, "ret");
4090}
4091
b782c65b
GR
4092/* Helper to get the allocation tag from a 64-bit ADDRESS.
4093
4094 Return the allocation tag if successful and nullopt otherwise. */
4095
4096std::optional<CORE_ADDR>
4097aarch64_mte_get_atag (CORE_ADDR address)
4098{
4099 gdb::byte_vector tags;
4100
4101 /* Attempt to fetch the allocation tag. */
4102 if (!target_fetch_memtags (address, 1, tags,
4103 static_cast<int> (memtag_type::allocation)))
4104 return {};
4105
4106 /* Only one tag should've been returned. Make sure we got exactly that. */
4107 if (tags.size () != 1)
4108 error (_("Target returned an unexpected number of tags."));
4109
4110 /* Although our tags are 4 bits in size, they are stored in a
4111 byte. */
4112 return tags[0];
4113}
4114
4115/* Implement the memtag_matches_p gdbarch method. */
4116
4117static bool
4118aarch64_memtag_matches_p (struct gdbarch *gdbarch,
4119 struct value *address)
4120{
4121 gdb_assert (address != nullptr);
4122
4123 CORE_ADDR addr = value_as_address (address);
4124
4125 /* Fetch the allocation tag for ADDRESS. */
4126 std::optional<CORE_ADDR> atag
86bb38ce 4127 = aarch64_mte_get_atag (aarch64_remove_non_address_bits (gdbarch, addr));
b782c65b
GR
4128
4129 if (!atag.has_value ())
4130 return true;
4131
4132 /* Fetch the logical tag for ADDRESS. */
4133 gdb_byte ltag = aarch64_mte_get_ltag (addr);
4134
4135 /* Are the tags the same? */
4136 return ltag == *atag;
4137}
4138
4139/* Implement the set_memtags gdbarch method. */
4140
4141static bool
4142aarch64_set_memtags (struct gdbarch *gdbarch, struct value *address,
4143 size_t length, const gdb::byte_vector &tags,
4144 memtag_type tag_type)
4145{
4146 gdb_assert (!tags.empty ());
4147 gdb_assert (address != nullptr);
4148
4149 CORE_ADDR addr = value_as_address (address);
4150
4151 /* Set the logical tag or the allocation tag. */
4152 if (tag_type == memtag_type::logical)
4153 {
4154 /* When setting logical tags, we don't care about the length, since
4155 we are only setting a single logical tag. */
4156 addr = aarch64_mte_set_ltag (addr, tags[0]);
4157
4158 /* Update the value's content with the tag. */
4159 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4160 gdb_byte *srcbuf = address->contents_raw ().data ();
4161 store_unsigned_integer (srcbuf, sizeof (addr), byte_order, addr);
4162 }
4163 else
4164 {
4165 /* Remove the top byte. */
86bb38ce 4166 addr = aarch64_remove_non_address_bits (gdbarch, addr);
b782c65b
GR
4167
4168 /* With G being the number of tag granules and N the number of tags
4169 passed in, we can have the following cases:
4170
4171 1 - G == N: Store all the N tags to memory.
4172
4173 2 - G < N : Warn about having more tags than granules, but write G
4174 tags.
4175
4176 3 - G > N : This is a "fill tags" operation. We should use the tags
4177 as a pattern to fill the granules repeatedly until we have
4178 written G tags to memory.
4179 */
4180
4181 size_t g = aarch64_mte_get_tag_granules (addr, length,
4182 AARCH64_MTE_GRANULE_SIZE);
4183 size_t n = tags.size ();
4184
4185 if (g < n)
4186 warning (_("Got more tags than memory granules. Tags will be "
4187 "truncated."));
4188 else if (g > n)
4189 warning (_("Using tag pattern to fill memory range."));
4190
4191 if (!target_store_memtags (addr, length, tags,
4192 static_cast<int> (memtag_type::allocation)))
4193 return false;
4194 }
4195 return true;
4196}
4197
4198/* Implement the get_memtag gdbarch method. */
4199
4200static struct value *
4201aarch64_get_memtag (struct gdbarch *gdbarch, struct value *address,
4202 memtag_type tag_type)
4203{
4204 gdb_assert (address != nullptr);
4205
4206 CORE_ADDR addr = value_as_address (address);
4207 CORE_ADDR tag = 0;
4208
4209 /* Get the logical tag or the allocation tag. */
4210 if (tag_type == memtag_type::logical)
4211 tag = aarch64_mte_get_ltag (addr);
4212 else
4213 {
4214 /* Remove the top byte. */
86bb38ce 4215 addr = aarch64_remove_non_address_bits (gdbarch, addr);
b782c65b
GR
4216 std::optional<CORE_ADDR> atag = aarch64_mte_get_atag (addr);
4217
4218 if (!atag.has_value ())
4219 return nullptr;
4220
4221 tag = *atag;
4222 }
4223
4224 /* Convert the tag to a value. */
4225 return value_from_ulongest (builtin_type (gdbarch)->builtin_unsigned_int,
4226 tag);
4227}
4228
4229/* Implement the memtag_to_string gdbarch method. */
4230
4231static std::string
4232aarch64_memtag_to_string (struct gdbarch *gdbarch, struct value *tag_value)
4233{
4234 if (tag_value == nullptr)
4235 return "";
4236
4237 CORE_ADDR tag = value_as_address (tag_value);
4238
9c1f84c9 4239 return string_printf ("0x%s", phex_nz (tag));
b782c65b
GR
4240}
4241
86bb38ce 4242/* See aarch64-tdep.h. */
6d002087 4243
86bb38ce 4244CORE_ADDR
6d002087
LM
4245aarch64_remove_non_address_bits (struct gdbarch *gdbarch, CORE_ADDR pointer)
4246{
6d002087 4247 /* By default, we assume TBI and discard the top 8 bits plus the VA range
ef139898
LM
4248 select bit (55). Below we try to fetch information about pointer
4249 authentication masks in order to make non-address removal more
4250 precise. */
6d002087
LM
4251 CORE_ADDR mask = AARCH64_TOP_BITS_MASK;
4252
ef139898
LM
4253 /* Check if we have an inferior first. If not, just use the default
4254 mask.
6d002087 4255
ef139898
LM
4256 We use the inferior_ptid here because the pointer authentication masks
4257 should be the same across threads of a process. Since we may not have
4258 access to the current thread (gdb may have switched to no inferiors
4259 momentarily), we use the inferior ptid. */
4260 if (inferior_ptid != null_ptid)
4261 {
4262 /* If we do have an inferior, attempt to fetch its thread's thread_info
4263 struct. */
9213a6d7 4264 thread_info *thread = current_inferior ()->find_thread (inferior_ptid);
6d002087 4265
ef139898
LM
4266 /* If the thread is running, we will not be able to fetch the mask
4267 registers. */
4268 if (thread != nullptr && thread->state != THREAD_RUNNING)
6d002087 4269 {
ef139898
LM
4270 /* Otherwise, fetch the register cache and the masks. */
4271 struct regcache *regs
4272 = get_thread_regcache (current_inferior ()->process_target (),
4273 inferior_ptid);
4274
4275 /* Use the gdbarch from the register cache to check for pointer
4276 authentication support, as it matches the features found in
4277 that particular thread. */
4278 aarch64_gdbarch_tdep *tdep
4279 = gdbarch_tdep<aarch64_gdbarch_tdep> (regs->arch ());
6d002087 4280
ef139898
LM
4281 /* Is there pointer authentication support? */
4282 if (tdep->has_pauth ())
4283 {
4284 CORE_ADDR cmask, dmask;
4285 int dmask_regnum
4286 = AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base);
4287 int cmask_regnum
4288 = AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base);
4289
4290 /* If we have a kernel address and we have kernel-mode address
4291 mask registers, use those instead. */
4292 if (tdep->pauth_reg_count > 2
4293 && pointer & VA_RANGE_SELECT_BIT_MASK)
4294 {
4295 dmask_regnum
4296 = AARCH64_PAUTH_DMASK_HIGH_REGNUM (tdep->pauth_reg_base);
4297 cmask_regnum
4298 = AARCH64_PAUTH_CMASK_HIGH_REGNUM (tdep->pauth_reg_base);
4299 }
4300
4301 /* We have both a code mask and a data mask. For now they are
4302 the same, but this may change in the future. */
4303 if (regs->cooked_read (dmask_regnum, &dmask) != REG_VALID)
4304 dmask = mask;
4305
4306 if (regs->cooked_read (cmask_regnum, &cmask) != REG_VALID)
4307 cmask = mask;
4308
4309 mask |= aarch64_mask_from_pac_registers (cmask, dmask);
4310 }
4311 }
6d002087
LM
4312 }
4313
4314 return aarch64_remove_top_bits (pointer, mask);
4315}
4316
ca65640f
LM
4317/* Given NAMES, a vector of strings, initialize it with all the SME
4318 pseudo-register names for the current streaming vector length. */
4319
4320static void
4321aarch64_initialize_sme_pseudo_names (struct gdbarch *gdbarch,
4322 std::vector<std::string> &names)
4323{
4324 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
4325
4326 gdb_assert (tdep->has_sme ());
4327 gdb_assert (tdep->sme_tile_slice_pseudo_base > 0);
4328 gdb_assert (tdep->sme_tile_pseudo_base > 0);
4329
4330 for (int i = 0; i < tdep->sme_tile_slice_pseudo_count; i++)
4331 {
4332 int regnum = tdep->sme_tile_slice_pseudo_base + i;
4333 struct za_pseudo_encoding encoding;
4334 aarch64_za_decode_pseudos (gdbarch, regnum, encoding);
4335 names.push_back (aarch64_za_tile_slice_name (encoding));
4336 }
4337 for (int i = 0; i < AARCH64_ZA_TILES_NUM; i++)
4338 {
4339 int regnum = tdep->sme_tile_pseudo_base + i;
4340 struct za_pseudo_encoding encoding;
4341 aarch64_za_decode_pseudos (gdbarch, regnum, encoding);
4342 names.push_back (aarch64_za_tile_name (encoding));
4343 }
4344}
4345
07b287a0 4346/* Initialize the current architecture based on INFO. If possible,
973c5759 4347 reuse an architecture from ARCHES, which is a list of
07b287a0
MS
4348 architectures already created during this debugging session.
4349
4350 Called e.g. at program startup, when reading a core file, and when
4351 reading a binary file. */
4352
4353static struct gdbarch *
4354aarch64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
4355{
ccb8d7e8 4356 const struct tdesc_feature *feature_core, *feature_fpu, *feature_sve;
76bed0fd 4357 const struct tdesc_feature *feature_pauth;
ccb8d7e8
AH
4358 bool valid_p = true;
4359 int i, num_regs = 0, num_pseudo_regs = 0;
c9cd8ca4 4360 int first_pauth_regnum = -1, ra_sign_state_offset = -1;
ba60b963 4361 int first_mte_regnum = -1, first_tls_regnum = -1;
4f3681cc 4362 uint64_t vq = aarch64_get_tdesc_vq (info.target_desc);
ca65640f 4363 uint64_t svq = aarch64_get_tdesc_svq (info.target_desc);
4da037ef
AH
4364
4365 if (vq > AARCH64_MAX_SVE_VQ)
f34652de 4366 internal_error (_("VQ out of bounds: %s (max %d)"),
596179f7 4367 pulongest (vq), AARCH64_MAX_SVE_VQ);
4da037ef 4368
ca65640f
LM
4369 if (svq > AARCH64_MAX_SVE_VQ)
4370 internal_error (_("Streaming vector quotient (svq) out of bounds: %s"
4371 " (max %d)"),
4372 pulongest (svq), AARCH64_MAX_SVE_VQ);
4373
ccb8d7e8
AH
4374 /* If there is already a candidate, use it. */
4375 for (gdbarch_list *best_arch = gdbarch_list_lookup_by_info (arches, &info);
4376 best_arch != nullptr;
4377 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
4378 {
345bd07c 4379 aarch64_gdbarch_tdep *tdep
08106042 4380 = gdbarch_tdep<aarch64_gdbarch_tdep> (best_arch->gdbarch);
ca65640f 4381 if (tdep && tdep->vq == vq && tdep->sme_svq == svq)
ccb8d7e8
AH
4382 return best_arch->gdbarch;
4383 }
07b287a0 4384
4da037ef
AH
4385 /* Ensure we always have a target descriptor, and that it is for the given VQ
4386 value. */
ccb8d7e8 4387 const struct target_desc *tdesc = info.target_desc;
ca65640f
LM
4388 if (!tdesc_has_registers (tdesc) || vq != aarch64_get_tdesc_vq (tdesc)
4389 || svq != aarch64_get_tdesc_svq (tdesc))
4390 {
4391 aarch64_features features;
4392 features.vq = vq;
4393 features.svq = svq;
4394 tdesc = aarch64_read_description (features);
4395 }
07b287a0
MS
4396 gdb_assert (tdesc);
4397
ccb8d7e8 4398 feature_core = tdesc_find_feature (tdesc,"org.gnu.gdb.aarch64.core");
ba2d2bb2
AH
4399 feature_fpu = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.fpu");
4400 feature_sve = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sve");
5e984dbf
LM
4401 const struct tdesc_feature *feature_mte
4402 = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.mte");
414d5848
JB
4403 const struct tdesc_feature *feature_tls
4404 = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.tls");
07b287a0 4405
ccb8d7e8
AH
4406 if (feature_core == nullptr)
4407 return nullptr;
07b287a0 4408
c1e1314d 4409 tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
07b287a0 4410
ba2d2bb2 4411 /* Validate the description provides the mandatory core R registers
07b287a0
MS
4412 and allocate their numbers. */
4413 for (i = 0; i < ARRAY_SIZE (aarch64_r_register_names); i++)
c1e1314d 4414 valid_p &= tdesc_numbered_register (feature_core, tdesc_data.get (),
ba2d2bb2
AH
4415 AARCH64_X0_REGNUM + i,
4416 aarch64_r_register_names[i]);
07b287a0
MS
4417
4418 num_regs = AARCH64_X0_REGNUM + i;
4419
ba2d2bb2 4420 /* Add the V registers. */
ccb8d7e8 4421 if (feature_fpu != nullptr)
07b287a0 4422 {
ccb8d7e8 4423 if (feature_sve != nullptr)
ba2d2bb2
AH
4424 error (_("Program contains both fpu and SVE features."));
4425
4426 /* Validate the description provides the mandatory V registers
4427 and allocate their numbers. */
07b287a0 4428 for (i = 0; i < ARRAY_SIZE (aarch64_v_register_names); i++)
c1e1314d 4429 valid_p &= tdesc_numbered_register (feature_fpu, tdesc_data.get (),
ba2d2bb2
AH
4430 AARCH64_V0_REGNUM + i,
4431 aarch64_v_register_names[i]);
07b287a0
MS
4432
4433 num_regs = AARCH64_V0_REGNUM + i;
ba2d2bb2 4434 }
07b287a0 4435
ba2d2bb2 4436 /* Add the SVE registers. */
ccb8d7e8 4437 if (feature_sve != nullptr)
ba2d2bb2
AH
4438 {
4439 /* Validate the description provides the mandatory SVE registers
4440 and allocate their numbers. */
4441 for (i = 0; i < ARRAY_SIZE (aarch64_sve_register_names); i++)
c1e1314d 4442 valid_p &= tdesc_numbered_register (feature_sve, tdesc_data.get (),
ba2d2bb2
AH
4443 AARCH64_SVE_Z0_REGNUM + i,
4444 aarch64_sve_register_names[i]);
4445
4446 num_regs = AARCH64_SVE_Z0_REGNUM + i;
4447 num_pseudo_regs += 32; /* add the Vn register pseudos. */
4448 }
4449
ccb8d7e8 4450 if (feature_fpu != nullptr || feature_sve != nullptr)
ba2d2bb2 4451 {
07b287a0
MS
4452 num_pseudo_regs += 32; /* add the Qn scalar register pseudos */
4453 num_pseudo_regs += 32; /* add the Dn scalar register pseudos */
4454 num_pseudo_regs += 32; /* add the Sn scalar register pseudos */
4455 num_pseudo_regs += 32; /* add the Hn scalar register pseudos */
4456 num_pseudo_regs += 32; /* add the Bn scalar register pseudos */
4457 }
4458
ca65640f 4459 int first_sme_regnum = -1;
42019af6 4460 int first_sme2_regnum = -1;
ca65640f
LM
4461 int first_sme_pseudo_regnum = -1;
4462 const struct tdesc_feature *feature_sme
4463 = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sme");
4464 if (feature_sme != nullptr)
4465 {
4466 /* Record the first SME register. */
4467 first_sme_regnum = num_regs;
4468
4469 valid_p &= tdesc_numbered_register (feature_sme, tdesc_data.get (),
4470 num_regs++, "svg");
4471
4472 valid_p &= tdesc_numbered_register (feature_sme, tdesc_data.get (),
4473 num_regs++, "svcr");
4474
4475 valid_p &= tdesc_numbered_register (feature_sme, tdesc_data.get (),
4476 num_regs++, "za");
4477
4478 /* Record the first SME pseudo register. */
4479 first_sme_pseudo_regnum = num_pseudo_regs;
4480
4481 /* Add the ZA tile slice pseudo registers. The number of tile slice
4482 pseudo-registers depend on the svl, and is always a multiple of 5. */
4483 num_pseudo_regs += (svq << 5) * 5;
4484
4485 /* Add the ZA tile pseudo registers. */
4486 num_pseudo_regs += AARCH64_ZA_TILES_NUM;
42019af6
LM
4487
4488 /* Now check for the SME2 feature. SME2 is only available if SME is
4489 available. */
4490 const struct tdesc_feature *feature_sme2
4491 = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sme2");
4492 if (feature_sme2 != nullptr)
4493 {
4494 /* Record the first SME2 register. */
4495 first_sme2_regnum = num_regs;
4496
4497 valid_p &= tdesc_numbered_register (feature_sme2, tdesc_data.get (),
4498 num_regs++, "zt0");
4499 }
ca65640f
LM
4500 }
4501
414d5848 4502 /* Add the TLS register. */
ba60b963 4503 int tls_register_count = 0;
414d5848
JB
4504 if (feature_tls != nullptr)
4505 {
ba60b963 4506 first_tls_regnum = num_regs;
414d5848 4507
ba60b963
LM
4508 /* Look for the TLS registers. tpidr is required, but tpidr2 is
4509 optional. */
4510 valid_p
4511 = tdesc_numbered_register (feature_tls, tdesc_data.get (),
4512 first_tls_regnum, "tpidr");
4513
4514 if (valid_p)
4515 {
4516 tls_register_count++;
4517
4518 bool has_tpidr2
4519 = tdesc_numbered_register (feature_tls, tdesc_data.get (),
4520 first_tls_regnum + tls_register_count,
4521 "tpidr2");
4522
4523 /* Figure out how many TLS registers we have. */
4524 if (has_tpidr2)
4525 tls_register_count++;
4526
4527 num_regs += tls_register_count;
4528 }
4529 else
4530 {
4531 warning (_("Provided TLS register feature doesn't contain "
4532 "required tpidr register."));
4533 return nullptr;
4534 }
414d5848
JB
4535 }
4536
acdf6071
LM
4537 /* We have two versions of the pauth target description due to a past bug
4538 where GDB would crash when seeing the first version of the pauth target
4539 description. */
4540 feature_pauth = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.pauth");
4541 if (feature_pauth == nullptr)
4542 feature_pauth = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.pauth_v2");
4543
76bed0fd 4544 /* Add the pauth registers. */
6d002087 4545 int pauth_masks = 0;
76bed0fd
AH
4546 if (feature_pauth != NULL)
4547 {
4548 first_pauth_regnum = num_regs;
c9cd8ca4 4549 ra_sign_state_offset = num_pseudo_regs;
6d002087
LM
4550
4551 /* Size of the expected register set with all 4 masks. */
4552 int set_size = ARRAY_SIZE (aarch64_pauth_register_names);
4553
4554 /* QEMU exposes a couple additional masks for the high half of the
4555 address. We should either have 2 registers or 4 registers. */
4556 if (tdesc_unnumbered_register (feature_pauth,
4557 "pauth_dmask_high") == 0)
4558 {
4559 /* We did not find pauth_dmask_high, assume we only have
4560 2 masks. We are not dealing with QEMU/Emulators then. */
4561 set_size -= 2;
4562 }
4563
76bed0fd
AH
4564 /* Validate the descriptor provides the mandatory PAUTH registers and
4565 allocate their numbers. */
6d002087 4566 for (i = 0; i < set_size; i++)
c1e1314d 4567 valid_p &= tdesc_numbered_register (feature_pauth, tdesc_data.get (),
76bed0fd
AH
4568 first_pauth_regnum + i,
4569 aarch64_pauth_register_names[i]);
4570
4571 num_regs += i;
34dcc7cf 4572 num_pseudo_regs += 1; /* Count RA_STATE pseudo register. */
6d002087 4573 pauth_masks = set_size;
76bed0fd
AH
4574 }
4575
5e984dbf
LM
4576 /* Add the MTE registers. */
4577 if (feature_mte != NULL)
4578 {
4579 first_mte_regnum = num_regs;
4580 /* Validate the descriptor provides the mandatory MTE registers and
4581 allocate their numbers. */
4582 for (i = 0; i < ARRAY_SIZE (aarch64_mte_register_names); i++)
4583 valid_p &= tdesc_numbered_register (feature_mte, tdesc_data.get (),
4584 first_mte_regnum + i,
4585 aarch64_mte_register_names[i]);
4586
4587 num_regs += i;
4588 }
e63ae49b
LM
4589 /* W pseudo-registers */
4590 int first_w_regnum = num_pseudo_regs;
4591 num_pseudo_regs += 31;
5e984dbf 4592
07b287a0 4593 if (!valid_p)
c1e1314d 4594 return nullptr;
07b287a0
MS
4595
4596 /* AArch64 code is always little-endian. */
4597 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
4598
2b16913c
SM
4599 gdbarch *gdbarch
4600 = gdbarch_alloc (&info, gdbarch_tdep_up (new aarch64_gdbarch_tdep));
4601 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0
MS
4602
4603 /* This should be low enough for everything. */
4604 tdep->lowest_pc = 0x20;
4605 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
4606 tdep->jb_elt_size = 8;
4da037ef 4607 tdep->vq = vq;
76bed0fd 4608 tdep->pauth_reg_base = first_pauth_regnum;
6d002087 4609 tdep->pauth_reg_count = pauth_masks;
1ba3a322 4610 tdep->ra_sign_state_regnum = -1;
5e984dbf 4611 tdep->mte_reg_base = first_mte_regnum;
ba60b963
LM
4612 tdep->tls_regnum_base = first_tls_regnum;
4613 tdep->tls_register_count = tls_register_count;
34dcc7cf 4614
ca65640f
LM
4615 /* Set the SME register set details. The pseudo-registers will be adjusted
4616 later. */
4617 tdep->sme_reg_base = first_sme_regnum;
4618 tdep->sme_svg_regnum = first_sme_regnum;
4619 tdep->sme_svcr_regnum = first_sme_regnum + 1;
4620 tdep->sme_za_regnum = first_sme_regnum + 2;
4621 tdep->sme_svq = svq;
4622
42019af6
LM
4623 /* Set the SME2 register set details. */
4624 tdep->sme2_zt0_regnum = first_sme2_regnum;
4625
07b287a0
MS
4626 set_gdbarch_push_dummy_call (gdbarch, aarch64_push_dummy_call);
4627 set_gdbarch_frame_align (gdbarch, aarch64_frame_align);
4628
07b287a0
MS
4629 /* Advance PC across function entry code. */
4630 set_gdbarch_skip_prologue (gdbarch, aarch64_skip_prologue);
4631
4632 /* The stack grows downward. */
4633 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
4634
4635 /* Breakpoint manipulation. */
04180708
YQ
4636 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
4637 aarch64_breakpoint::kind_from_pc);
4638 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
4639 aarch64_breakpoint::bp_from_kind);
07b287a0 4640 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
9404b58f 4641 set_gdbarch_software_single_step (gdbarch, aarch64_software_single_step);
07b287a0
MS
4642
4643 /* Information about registers, etc. */
4644 set_gdbarch_sp_regnum (gdbarch, AARCH64_SP_REGNUM);
4645 set_gdbarch_pc_regnum (gdbarch, AARCH64_PC_REGNUM);
4646 set_gdbarch_num_regs (gdbarch, num_regs);
4647
4648 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudo_regs);
4649 set_gdbarch_pseudo_register_read_value (gdbarch, aarch64_pseudo_read_value);
bdbf4262 4650 set_gdbarch_pseudo_register_write (gdbarch, aarch64_pseudo_write);
07b287a0
MS
4651 set_tdesc_pseudo_register_name (gdbarch, aarch64_pseudo_register_name);
4652 set_tdesc_pseudo_register_type (gdbarch, aarch64_pseudo_register_type);
4653 set_tdesc_pseudo_register_reggroup_p (gdbarch,
4654 aarch64_pseudo_register_reggroup_p);
76bed0fd 4655 set_gdbarch_cannot_store_register (gdbarch, aarch64_cannot_store_register);
07b287a0 4656
b782c65b
GR
4657 /* Set the allocation tag granule size to 16 bytes. */
4658 set_gdbarch_memtag_granule_size (gdbarch, AARCH64_MTE_GRANULE_SIZE);
4659
4660 /* Register a hook for checking if there is a memory tag match. */
4661 set_gdbarch_memtag_matches_p (gdbarch, aarch64_memtag_matches_p);
4662
4663 /* Register a hook for setting the logical/allocation tags for
4664 a range of addresses. */
4665 set_gdbarch_set_memtags (gdbarch, aarch64_set_memtags);
4666
4667 /* Register a hook for extracting the logical/allocation tag from an
4668 address. */
4669 set_gdbarch_get_memtag (gdbarch, aarch64_get_memtag);
4670
4671 /* Register a hook for converting a memory tag to a string. */
4672 set_gdbarch_memtag_to_string (gdbarch, aarch64_memtag_to_string);
4673
07b287a0
MS
4674 /* ABI */
4675 set_gdbarch_short_bit (gdbarch, 16);
4676 set_gdbarch_int_bit (gdbarch, 32);
4677 set_gdbarch_float_bit (gdbarch, 32);
4678 set_gdbarch_double_bit (gdbarch, 64);
4679 set_gdbarch_long_double_bit (gdbarch, 128);
4680 set_gdbarch_long_bit (gdbarch, 64);
4681 set_gdbarch_long_long_bit (gdbarch, 64);
4682 set_gdbarch_ptr_bit (gdbarch, 64);
4683 set_gdbarch_char_signed (gdbarch, 0);
53375380 4684 set_gdbarch_wchar_signed (gdbarch, 0);
07b287a0
MS
4685 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
4686 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
552f1157 4687 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
b907456c 4688 set_gdbarch_type_align (gdbarch, aarch64_type_align);
07b287a0 4689
da729c5c
TT
4690 /* Detect whether PC is at a point where the stack has been destroyed. */
4691 set_gdbarch_stack_frame_destroyed_p (gdbarch, aarch64_stack_frame_destroyed_p);
4692
07b287a0
MS
4693 /* Internal <-> external register number maps. */
4694 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, aarch64_dwarf_reg_to_regnum);
4695
4696 /* Returning results. */
5cb0f2d5 4697 set_gdbarch_return_value_as_value (gdbarch, aarch64_return_value);
07b287a0
MS
4698
4699 /* Disassembly. */
4700 set_gdbarch_print_insn (gdbarch, aarch64_gdb_print_insn);
4701
4702 /* Virtual tables. */
4703 set_gdbarch_vbit_in_delta (gdbarch, 1);
4704
4705 /* Hook in the ABI-specific overrides, if they have been registered. */
4706 info.target_desc = tdesc;
c1e1314d 4707 info.tdesc_data = tdesc_data.get ();
07b287a0
MS
4708 gdbarch_init_osabi (info, gdbarch);
4709
4710 dwarf2_frame_set_init_reg (gdbarch, aarch64_dwarf2_frame_init_reg);
11e1b75f
AH
4711 /* Register DWARF CFA vendor handler. */
4712 set_gdbarch_execute_dwarf_cfa_vendor_op (gdbarch,
4713 aarch64_execute_dwarf_cfa_vendor_op);
07b287a0 4714
5133a315
LM
4715 /* Permanent/Program breakpoint handling. */
4716 set_gdbarch_program_breakpoint_here_p (gdbarch,
4717 aarch64_program_breakpoint_here_p);
4718
07b287a0
MS
4719 /* Add some default predicates. */
4720 frame_unwind_append_unwinder (gdbarch, &aarch64_stub_unwind);
4721 dwarf2_append_unwinders (gdbarch);
4722 frame_unwind_append_unwinder (gdbarch, &aarch64_prologue_unwind);
4723
4724 frame_base_set_default (gdbarch, &aarch64_normal_base);
4725
4726 /* Now we have tuned the configuration, set a few final things,
4727 based on what the OS ABI has told us. */
4728
4729 if (tdep->jb_pc >= 0)
4730 set_gdbarch_get_longjmp_target (gdbarch, aarch64_get_longjmp_target);
4731
ea873d8e
PL
4732 set_gdbarch_gen_return_address (gdbarch, aarch64_gen_return_address);
4733
aa7ca1bb
AH
4734 set_gdbarch_get_pc_address_flags (gdbarch, aarch64_get_pc_address_flags);
4735
c1e1314d 4736 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
07b287a0 4737
1ba3a322
LM
4738 /* Fetch the updated number of registers after we're done adding all
4739 entries from features we don't explicitly care about. This is the case
4740 for bare metal debugging stubs that include a lot of system registers. */
4741 num_regs = gdbarch_num_regs (gdbarch);
4742
4743 /* With the number of real registers updated, setup the pseudo-registers and
4744 record their numbers. */
4745
e63ae49b
LM
4746 /* Setup W pseudo-register numbers. */
4747 tdep->w_pseudo_base = first_w_regnum + num_regs;
4748 tdep->w_pseudo_count = 31;
4749
1ba3a322
LM
4750 /* Pointer authentication pseudo-registers. */
4751 if (tdep->has_pauth ())
4752 tdep->ra_sign_state_regnum = ra_sign_state_offset + num_regs;
4753
6d002087 4754 /* Architecture hook to remove bits of a pointer that are not part of the
86bb38ce
CS
4755 address, like memory tags (MTE) and pointer authentication signatures.
4756 Configure address adjustment for watchpoints, breakpoints and memory
4757 transfer. */
4758 set_gdbarch_remove_non_address_bits_watchpoint
4759 (gdbarch, aarch64_remove_non_address_bits);
4760 set_gdbarch_remove_non_address_bits_breakpoint
4761 (gdbarch, aarch64_remove_non_address_bits);
4762 set_gdbarch_remove_non_address_bits_memory
4763 (gdbarch, aarch64_remove_non_address_bits);
6d002087 4764
ca65640f
LM
4765 /* SME pseudo-registers. */
4766 if (tdep->has_sme ())
4767 {
4768 tdep->sme_pseudo_base = num_regs + first_sme_pseudo_regnum;
4769 tdep->sme_tile_slice_pseudo_base = tdep->sme_pseudo_base;
4770 tdep->sme_tile_slice_pseudo_count = (svq * 32) * 5;
4771 tdep->sme_tile_pseudo_base
4772 = tdep->sme_pseudo_base + tdep->sme_tile_slice_pseudo_count;
4773 tdep->sme_pseudo_count
4774 = tdep->sme_tile_slice_pseudo_count + AARCH64_ZA_TILES_NUM;
4775
4776 /* The SME ZA pseudo-registers are a set of 160 to 2560 pseudo-registers
4777 depending on the value of svl.
4778
4779 The tile pseudo-registers are organized around their qualifiers
4780 (b, h, s, d and q). Their numbers are distributed as follows:
4781
4782 b 0
4783 h 1~2
4784 s 3~6
4785 d 7~14
4786 q 15~30
4787
4788 The naming of the tile pseudo-registers follows the pattern za<t><q>,
4789 where:
4790
4791 <t> is the tile number, with the following possible values based on
4792 the qualifiers:
4793
4794 Qualifier - Allocated indexes
4795
4796 b - 0
4797 h - 0~1
4798 s - 0~3
4799 d - 0~7
4800 q - 0~15
4801
4802 <q> is the qualifier: b, h, s, d and q.
4803
4804 The tile slice pseudo-registers are organized around their
4805 qualifiers as well (b, h, s, d and q), but also around their
4806 direction (h - horizontal and v - vertical).
4807
4808 Even-numbered tile slice pseudo-registers are horizontally-oriented
4809 and odd-numbered tile slice pseudo-registers are vertically-oriented.
4810
4811 Their numbers are distributed as follows:
4812
4813 Qualifier - Allocated indexes
4814
4815 b tile slices - 0~511
4816 h tile slices - 512~1023
4817 s tile slices - 1024~1535
4818 d tile slices - 1536~2047
4819 q tile slices - 2048~2559
4820
4821 The naming of the tile slice pseudo-registers follows the pattern
4822 za<t><d><q><s>, where:
4823
4824 <t> is the tile number as described for the tile pseudo-registers.
4825 <d> is the direction of the tile slice (h or v)
4826 <q> is the qualifier of the tile slice (b, h, s, d or q)
4827 <s> is the slice number, defined as follows:
4828
4829 Qualifier - Allocated indexes
4830
4831 b - 0~15
4832 h - 0~7
4833 s - 0~3
4834 d - 0~1
4835 q - 0
4836
4837 We have helper functions to translate to/from register index from/to
4838 the set of fields that make the pseudo-register names. */
4839
4840 /* Build the array of pseudo-register names available for this
4841 particular gdbarch configuration. */
4842 aarch64_initialize_sme_pseudo_names (gdbarch, tdep->sme_pseudo_names);
4843 }
4844
07b287a0
MS
4845 /* Add standard register aliases. */
4846 for (i = 0; i < ARRAY_SIZE (aarch64_register_aliases); i++)
4847 user_reg_add (gdbarch, aarch64_register_aliases[i].name,
4848 value_of_aarch64_user_reg,
4849 &aarch64_register_aliases[i].regnum);
4850
e8bf1ce4
JB
4851 register_aarch64_ravenscar_ops (gdbarch);
4852
07b287a0
MS
4853 return gdbarch;
4854}
4855
4856static void
4857aarch64_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
4858{
08106042 4859 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
07b287a0
MS
4860
4861 if (tdep == NULL)
4862 return;
4863
09a5d200 4864 gdb_printf (file, _("aarch64_dump_tdep: Lowest pc = 0x%s\n"),
6cb06a8c 4865 paddress (gdbarch, tdep->lowest_pc));
ca65640f
LM
4866
4867 /* SME fields. */
4868 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_type_q = %s\n"),
4869 host_address_to_string (tdep->sme_tile_type_q));
4870 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_type_d = %s\n"),
4871 host_address_to_string (tdep->sme_tile_type_d));
4872 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_type_s = %s\n"),
4873 host_address_to_string (tdep->sme_tile_type_s));
4874 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_type_h = %s\n"),
4875 host_address_to_string (tdep->sme_tile_type_h));
4876 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_type_n = %s\n"),
4877 host_address_to_string (tdep->sme_tile_type_b));
4878 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_slice_type_q = %s\n"),
4879 host_address_to_string (tdep->sme_tile_slice_type_q));
4880 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_slice_type_d = %s\n"),
4881 host_address_to_string (tdep->sme_tile_slice_type_d));
4882 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_slice_type_s = %s\n"),
4883 host_address_to_string (tdep->sme_tile_slice_type_s));
4884 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_slice_type_h = %s\n"),
4885 host_address_to_string (tdep->sme_tile_slice_type_h));
4886 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_slice_type_b = %s\n"),
4887 host_address_to_string (tdep->sme_tile_slice_type_b));
4888 gdb_printf (file, _("aarch64_dump_tdep: sme_reg_base = %s\n"),
4889 pulongest (tdep->sme_reg_base));
4890 gdb_printf (file, _("aarch64_dump_tdep: sme_svg_regnum = %s\n"),
4891 pulongest (tdep->sme_svg_regnum));
4892 gdb_printf (file, _("aarch64_dump_tdep: sme_svcr_regnum = %s\n"),
4893 pulongest (tdep->sme_svcr_regnum));
4894 gdb_printf (file, _("aarch64_dump_tdep: sme_za_regnum = %s\n"),
4895 pulongest (tdep->sme_za_regnum));
4896 gdb_printf (file, _("aarch64_dump_tdep: sme_pseudo_base = %s\n"),
4897 pulongest (tdep->sme_pseudo_base));
4898 gdb_printf (file, _("aarch64_dump_tdep: sme_pseudo_count = %s\n"),
4899 pulongest (tdep->sme_pseudo_count));
4900 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_slice_pseudo_base = %s\n"),
4901 pulongest (tdep->sme_tile_slice_pseudo_base));
4902 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_slice_pseudo_count = %s\n"),
4903 pulongest (tdep->sme_tile_slice_pseudo_count));
4904 gdb_printf (file, _("aarch64_dump_tdep: sme_tile_pseudo_base = %s\n"),
4905 pulongest (tdep->sme_tile_pseudo_base));
4906 gdb_printf (file, _("aarch64_dump_tdep: sme_svq = %s\n"),
4907 pulongest (tdep->sme_svq));
07b287a0
MS
4908}
4909
0d4c07af 4910#if GDB_SELF_TEST
1e2b521d
YQ
4911namespace selftests
4912{
4913static void aarch64_process_record_test (void);
4914}
0d4c07af 4915#endif
1e2b521d 4916
5fe70629 4917INIT_GDB_FILE (aarch64_tdep)
07b287a0
MS
4918{
4919 gdbarch_register (bfd_arch_aarch64, aarch64_gdbarch_init,
4920 aarch64_dump_tdep);
4921
07b287a0
MS
4922 /* Debug this file's internals. */
4923 add_setshow_boolean_cmd ("aarch64", class_maintenance, &aarch64_debug, _("\
4924Set AArch64 debugging."), _("\
4925Show AArch64 debugging."), _("\
4926When on, AArch64 specific debugging is enabled."),
4927 NULL,
4928 show_aarch64_debug,
4929 &setdebuglist, &showdebuglist);
4d9a9006
YQ
4930
4931#if GDB_SELF_TEST
1526853e
SM
4932 selftests::register_test ("aarch64-analyze-prologue",
4933 selftests::aarch64_analyze_prologue_test);
4934 selftests::register_test ("aarch64-process-record",
4935 selftests::aarch64_process_record_test);
4d9a9006 4936#endif
07b287a0 4937}
99afc88b
OJ
4938
4939/* AArch64 process record-replay related structures, defines etc. */
4940
99afc88b 4941#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
dda83cd7
SM
4942 do \
4943 { \
4944 unsigned int reg_len = LENGTH; \
4945 if (reg_len) \
4946 { \
4947 REGS = XNEWVEC (uint32_t, reg_len); \
4948 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
4949 } \
4950 } \
4951 while (0)
99afc88b
OJ
4952
4953#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
dda83cd7
SM
4954 do \
4955 { \
4956 unsigned int mem_len = LENGTH; \
4957 if (mem_len) \
01add95b
SM
4958 { \
4959 MEMS = XNEWVEC (struct aarch64_mem_r, mem_len); \
91ddba83 4960 memcpy(MEMS, &RECORD_BUF[0], \
01add95b
SM
4961 sizeof(struct aarch64_mem_r) * LENGTH); \
4962 } \
dda83cd7
SM
4963 } \
4964 while (0)
99afc88b
OJ
4965
4966/* AArch64 record/replay structures and enumerations. */
4967
4968struct aarch64_mem_r
4969{
4970 uint64_t len; /* Record length. */
4971 uint64_t addr; /* Memory address. */
4972};
4973
4974enum aarch64_record_result
4975{
4976 AARCH64_RECORD_SUCCESS,
99afc88b
OJ
4977 AARCH64_RECORD_UNSUPPORTED,
4978 AARCH64_RECORD_UNKNOWN
4979};
4980
4748a9be 4981struct aarch64_insn_decode_record
99afc88b
OJ
4982{
4983 struct gdbarch *gdbarch;
4984 struct regcache *regcache;
4985 CORE_ADDR this_addr; /* Address of insn to be recorded. */
4986 uint32_t aarch64_insn; /* Insn to be recorded. */
4987 uint32_t mem_rec_count; /* Count of memory records. */
4988 uint32_t reg_rec_count; /* Count of register records. */
4989 uint32_t *aarch64_regs; /* Registers to be recorded. */
4990 struct aarch64_mem_r *aarch64_mems; /* Memory locations to be recorded. */
4748a9be 4991};
99afc88b
OJ
4992
4993/* Record handler for data processing - register instructions. */
4994
4995static unsigned int
4748a9be 4996aarch64_record_data_proc_reg (aarch64_insn_decode_record *aarch64_insn_r)
99afc88b
OJ
4997{
4998 uint8_t reg_rd, insn_bits24_27, insn_bits21_23;
4999 uint32_t record_buf[4];
5000
5001 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
5002 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
5003 insn_bits21_23 = bits (aarch64_insn_r->aarch64_insn, 21, 23);
5004
5005 if (!bit (aarch64_insn_r->aarch64_insn, 28))
5006 {
5007 uint8_t setflags;
5008
5009 /* Logical (shifted register). */
5010 if (insn_bits24_27 == 0x0a)
5011 setflags = (bits (aarch64_insn_r->aarch64_insn, 29, 30) == 0x03);
5012 /* Add/subtract. */
5013 else if (insn_bits24_27 == 0x0b)
5014 setflags = bit (aarch64_insn_r->aarch64_insn, 29);
5015 else
5016 return AARCH64_RECORD_UNKNOWN;
5017
5018 record_buf[0] = reg_rd;
5019 aarch64_insn_r->reg_rec_count = 1;
5020 if (setflags)
5021 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
5022 }
5023 else
5024 {
5025 if (insn_bits24_27 == 0x0b)
5026 {
5027 /* Data-processing (3 source). */
5028 record_buf[0] = reg_rd;
5029 aarch64_insn_r->reg_rec_count = 1;
5030 }
5031 else if (insn_bits24_27 == 0x0a)
5032 {
5033 if (insn_bits21_23 == 0x00)
5034 {
5035 /* Add/subtract (with carry). */
5036 record_buf[0] = reg_rd;
5037 aarch64_insn_r->reg_rec_count = 1;
5038 if (bit (aarch64_insn_r->aarch64_insn, 29))
5039 {
5040 record_buf[1] = AARCH64_CPSR_REGNUM;
5041 aarch64_insn_r->reg_rec_count = 2;
5042 }
5043 }
5044 else if (insn_bits21_23 == 0x02)
5045 {
5046 /* Conditional compare (register) and conditional compare
5047 (immediate) instructions. */
5048 record_buf[0] = AARCH64_CPSR_REGNUM;
5049 aarch64_insn_r->reg_rec_count = 1;
5050 }
5051 else if (insn_bits21_23 == 0x04 || insn_bits21_23 == 0x06)
5052 {
85102364 5053 /* Conditional select. */
99afc88b
OJ
5054 /* Data-processing (2 source). */
5055 /* Data-processing (1 source). */
5056 record_buf[0] = reg_rd;
5057 aarch64_insn_r->reg_rec_count = 1;
5058 }
5059 else
5060 return AARCH64_RECORD_UNKNOWN;
5061 }
5062 }
5063
5064 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
5065 record_buf);
5066 return AARCH64_RECORD_SUCCESS;
5067}
5068
5069/* Record handler for data processing - immediate instructions. */
5070
5071static unsigned int
4748a9be 5072aarch64_record_data_proc_imm (aarch64_insn_decode_record *aarch64_insn_r)
99afc88b 5073{
78cc6c2d 5074 uint8_t reg_rd, insn_bit23, insn_bits24_27, setflags;
99afc88b
OJ
5075 uint32_t record_buf[4];
5076
5077 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
99afc88b
OJ
5078 insn_bit23 = bit (aarch64_insn_r->aarch64_insn, 23);
5079 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
5080
5081 if (insn_bits24_27 == 0x00 /* PC rel addressing. */
5082 || insn_bits24_27 == 0x03 /* Bitfield and Extract. */
5083 || (insn_bits24_27 == 0x02 && insn_bit23)) /* Move wide (immediate). */
5084 {
5085 record_buf[0] = reg_rd;
5086 aarch64_insn_r->reg_rec_count = 1;
5087 }
5088 else if (insn_bits24_27 == 0x01)
5089 {
5090 /* Add/Subtract (immediate). */
5091 setflags = bit (aarch64_insn_r->aarch64_insn, 29);
5092 record_buf[0] = reg_rd;
5093 aarch64_insn_r->reg_rec_count = 1;
5094 if (setflags)
5095 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
5096 }
5097 else if (insn_bits24_27 == 0x02 && !insn_bit23)
5098 {
5099 /* Logical (immediate). */
5100 setflags = bits (aarch64_insn_r->aarch64_insn, 29, 30) == 0x03;
5101 record_buf[0] = reg_rd;
5102 aarch64_insn_r->reg_rec_count = 1;
5103 if (setflags)
5104 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
5105 }
5106 else
5107 return AARCH64_RECORD_UNKNOWN;
5108
5109 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
5110 record_buf);
5111 return AARCH64_RECORD_SUCCESS;
5112}
5113
5114/* Record handler for branch, exception generation and system instructions. */
5115
5116static unsigned int
4748a9be 5117aarch64_record_branch_except_sys (aarch64_insn_decode_record *aarch64_insn_r)
99afc88b 5118{
345bd07c
SM
5119
5120 aarch64_gdbarch_tdep *tdep
08106042 5121 = gdbarch_tdep<aarch64_gdbarch_tdep> (aarch64_insn_r->gdbarch);
99afc88b
OJ
5122 uint8_t insn_bits24_27, insn_bits28_31, insn_bits22_23;
5123 uint32_t record_buf[4];
5124
5125 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
5126 insn_bits28_31 = bits (aarch64_insn_r->aarch64_insn, 28, 31);
5127 insn_bits22_23 = bits (aarch64_insn_r->aarch64_insn, 22, 23);
5128
5129 if (insn_bits28_31 == 0x0d)
5130 {
5131 /* Exception generation instructions. */
5132 if (insn_bits24_27 == 0x04)
5133 {
5d98d3cd
YQ
5134 if (!bits (aarch64_insn_r->aarch64_insn, 2, 4)
5135 && !bits (aarch64_insn_r->aarch64_insn, 21, 23)
5136 && bits (aarch64_insn_r->aarch64_insn, 0, 1) == 0x01)
99afc88b
OJ
5137 {
5138 ULONGEST svc_number;
5139
5140 regcache_raw_read_unsigned (aarch64_insn_r->regcache, 8,
5141 &svc_number);
5142 return tdep->aarch64_syscall_record (aarch64_insn_r->regcache,
5143 svc_number);
5144 }
5145 else
5146 return AARCH64_RECORD_UNSUPPORTED;
5147 }
5148 /* System instructions. */
5149 else if (insn_bits24_27 == 0x05 && insn_bits22_23 == 0x00)
5150 {
5151 uint32_t reg_rt, reg_crn;
5152
5153 reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
5154 reg_crn = bits (aarch64_insn_r->aarch64_insn, 12, 15);
5155
5156 /* Record rt in case of sysl and mrs instructions. */
5157 if (bit (aarch64_insn_r->aarch64_insn, 21))
5158 {
5159 record_buf[0] = reg_rt;
5160 aarch64_insn_r->reg_rec_count = 1;
5161 }
5162 /* Record cpsr for hint and msr(immediate) instructions. */
5163 else if (reg_crn == 0x02 || reg_crn == 0x04)
5164 {
5165 record_buf[0] = AARCH64_CPSR_REGNUM;
5166 aarch64_insn_r->reg_rec_count = 1;
5167 }
5168 }
5169 /* Unconditional branch (register). */
5170 else if((insn_bits24_27 & 0x0e) == 0x06)
5171 {
5172 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
5173 if (bits (aarch64_insn_r->aarch64_insn, 21, 22) == 0x01)
5174 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_LR_REGNUM;
5175 }
5176 else
5177 return AARCH64_RECORD_UNKNOWN;
5178 }
5179 /* Unconditional branch (immediate). */
5180 else if ((insn_bits28_31 & 0x07) == 0x01 && (insn_bits24_27 & 0x0c) == 0x04)
5181 {
5182 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
5183 if (bit (aarch64_insn_r->aarch64_insn, 31))
5184 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_LR_REGNUM;
5185 }
5186 else
5187 /* Compare & branch (immediate), Test & branch (immediate) and
5188 Conditional branch (immediate). */
5189 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
5190
5191 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
5192 record_buf);
5193 return AARCH64_RECORD_SUCCESS;
5194}
5195
5196/* Record handler for advanced SIMD load and store instructions. */
5197
5198static unsigned int
4748a9be 5199aarch64_record_asimd_load_store (aarch64_insn_decode_record *aarch64_insn_r)
99afc88b
OJ
5200{
5201 CORE_ADDR address;
5202 uint64_t addr_offset = 0;
5203 uint32_t record_buf[24];
51729ea0 5204 std::vector<uint64_t> record_buf_mem;
99afc88b 5205 uint32_t reg_rn, reg_rt;
51729ea0 5206 uint32_t reg_index = 0;
99afc88b
OJ
5207 uint8_t opcode_bits, size_bits;
5208
5209 reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
5210 reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
5211 size_bits = bits (aarch64_insn_r->aarch64_insn, 10, 11);
5212 opcode_bits = bits (aarch64_insn_r->aarch64_insn, 12, 15);
5213 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn, &address);
5214
5215 if (record_debug)
b277c936 5216 debug_printf ("Process record: Advanced SIMD load/store\n");
99afc88b
OJ
5217
5218 /* Load/store single structure. */
5219 if (bit (aarch64_insn_r->aarch64_insn, 24))
5220 {
5221 uint8_t sindex, scale, selem, esize, replicate = 0;
5222 scale = opcode_bits >> 2;
5223 selem = ((opcode_bits & 0x02) |
dda83cd7 5224 bit (aarch64_insn_r->aarch64_insn, 21)) + 1;
99afc88b 5225 switch (scale)
dda83cd7
SM
5226 {
5227 case 1:
5228 if (size_bits & 0x01)
5229 return AARCH64_RECORD_UNKNOWN;
5230 break;
5231 case 2:
5232 if ((size_bits >> 1) & 0x01)
5233 return AARCH64_RECORD_UNKNOWN;
5234 if (size_bits & 0x01)
5235 {
5236 if (!((opcode_bits >> 1) & 0x01))
5237 scale = 3;
5238 else
5239 return AARCH64_RECORD_UNKNOWN;
5240 }
5241 break;
5242 case 3:
5243 if (bit (aarch64_insn_r->aarch64_insn, 22) && !(opcode_bits & 0x01))
5244 {
5245 scale = size_bits;
5246 replicate = 1;
5247 break;
5248 }
5249 else
5250 return AARCH64_RECORD_UNKNOWN;
5251 default:
5252 break;
5253 }
99afc88b
OJ
5254 esize = 8 << scale;
5255 if (replicate)
dda83cd7
SM
5256 for (sindex = 0; sindex < selem; sindex++)
5257 {
5258 record_buf[reg_index++] = reg_rt + AARCH64_V0_REGNUM;
5259 reg_rt = (reg_rt + 1) % 32;
5260 }
99afc88b 5261 else
dda83cd7
SM
5262 {
5263 for (sindex = 0; sindex < selem; sindex++)
a2e3e93f
SM
5264 {
5265 if (bit (aarch64_insn_r->aarch64_insn, 22))
5266 record_buf[reg_index++] = reg_rt + AARCH64_V0_REGNUM;
5267 else
5268 {
51729ea0
TV
5269 record_buf_mem.push_back (esize / 8);
5270 record_buf_mem.push_back (address + addr_offset);
a2e3e93f
SM
5271 }
5272 addr_offset = addr_offset + (esize / 8);
5273 reg_rt = (reg_rt + 1) % 32;
5274 }
dda83cd7 5275 }
99afc88b
OJ
5276 }
5277 /* Load/store multiple structure. */
5278 else
5279 {
5280 uint8_t selem, esize, rpt, elements;
5281 uint8_t eindex, rindex;
5282
5283 esize = 8 << size_bits;
5284 if (bit (aarch64_insn_r->aarch64_insn, 30))
dda83cd7 5285 elements = 128 / esize;
99afc88b 5286 else
dda83cd7 5287 elements = 64 / esize;
99afc88b
OJ
5288
5289 switch (opcode_bits)
dda83cd7
SM
5290 {
5291 /*LD/ST4 (4 Registers). */
5292 case 0:
5293 rpt = 1;
5294 selem = 4;
5295 break;
5296 /*LD/ST1 (4 Registers). */
5297 case 2:
5298 rpt = 4;
5299 selem = 1;
5300 break;
5301 /*LD/ST3 (3 Registers). */
5302 case 4:
5303 rpt = 1;
5304 selem = 3;
5305 break;
5306 /*LD/ST1 (3 Registers). */
5307 case 6:
5308 rpt = 3;
5309 selem = 1;
5310 break;
5311 /*LD/ST1 (1 Register). */
5312 case 7:
5313 rpt = 1;
5314 selem = 1;
5315 break;
5316 /*LD/ST2 (2 Registers). */
5317 case 8:
5318 rpt = 1;
5319 selem = 2;
5320 break;
5321 /*LD/ST1 (2 Registers). */
5322 case 10:
5323 rpt = 2;
5324 selem = 1;
5325 break;
5326 default:
5327 return AARCH64_RECORD_UNSUPPORTED;
5328 break;
5329 }
99afc88b 5330 for (rindex = 0; rindex < rpt; rindex++)
dda83cd7
SM
5331 for (eindex = 0; eindex < elements; eindex++)
5332 {
5333 uint8_t reg_tt, sindex;
5334 reg_tt = (reg_rt + rindex) % 32;
5335 for (sindex = 0; sindex < selem; sindex++)
5336 {
5337 if (bit (aarch64_insn_r->aarch64_insn, 22))
5338 record_buf[reg_index++] = reg_tt + AARCH64_V0_REGNUM;
5339 else
5340 {
51729ea0
TV
5341 record_buf_mem.push_back (esize / 8);
5342 record_buf_mem.push_back (address + addr_offset);
dda83cd7
SM
5343 }
5344 addr_offset = addr_offset + (esize / 8);
5345 reg_tt = (reg_tt + 1) % 32;
5346 }
5347 }
99afc88b
OJ
5348 }
5349
5350 if (bit (aarch64_insn_r->aarch64_insn, 23))
5351 record_buf[reg_index++] = reg_rn;
5352
5353 aarch64_insn_r->reg_rec_count = reg_index;
51729ea0 5354 aarch64_insn_r->mem_rec_count = record_buf_mem.size () / 2;
99afc88b 5355 MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
51729ea0 5356 record_buf_mem.data ());
99afc88b 5357 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
dda83cd7 5358 record_buf);
99afc88b
OJ
5359 return AARCH64_RECORD_SUCCESS;
5360}
5361
ebd06ca6
TJB
5362/* Record handler for Memory Copy and Memory Set instructions. */
5363
5364static unsigned int
5365aarch64_record_memcopy_memset (aarch64_insn_decode_record *aarch64_insn_r)
5366{
5367 if (record_debug)
5368 debug_printf ("Process record: memory copy and memory set\n");
5369
5370 uint8_t op1 = bits (aarch64_insn_r->aarch64_insn, 22, 23);
5371 uint8_t op2 = bits (aarch64_insn_r->aarch64_insn, 12, 15);
5372 uint32_t reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
5373 uint32_t reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
5374 uint32_t record_buf[3];
5375 uint64_t record_buf_mem[4];
5376
5377 if (op1 == 3 && op2 > 11)
5378 /* Unallocated instructions. */
5379 return AARCH64_RECORD_UNKNOWN;
5380
5381 /* Set instructions have two registers and one memory region to be
5382 recorded. */
5383 record_buf[0] = reg_rd;
5384 record_buf[1] = reg_rn;
5385 aarch64_insn_r->reg_rec_count = 2;
5386
5387 ULONGEST dest_addr;
5388 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rd, &dest_addr);
5389
5390 LONGEST length;
5391 regcache_raw_read_signed (aarch64_insn_r->regcache, reg_rn, &length);
5392
5393 /* In one of the algorithm options a processor can implement, the length
5394 in Rn has an inverted sign. */
5395 if (length < 0)
5396 length *= -1;
5397
5398 record_buf_mem[0] = length;
5399 record_buf_mem[1] = dest_addr;
5400 aarch64_insn_r->mem_rec_count = 1;
5401
5402 if (op1 != 3)
5403 {
5404 /* Copy instructions have an additional register and an additional
5405 memory region to be recorded. */
5406 uint32_t reg_rs = bits (aarch64_insn_r->aarch64_insn, 16, 20);
5407
5408 record_buf[2] = reg_rs;
5409 aarch64_insn_r->reg_rec_count++;
5410
5411 ULONGEST source_addr;
5412 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rs,
5413 &source_addr);
5414
5415 record_buf_mem[2] = length;
5416 record_buf_mem[3] = source_addr;
5417 aarch64_insn_r->mem_rec_count++;
5418 }
5419
5420 MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
5421 record_buf_mem);
5422 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
5423 record_buf);
5424 return AARCH64_RECORD_SUCCESS;
5425}
5426
99afc88b
OJ
5427/* Record handler for load and store instructions. */
5428
5429static unsigned int
4748a9be 5430aarch64_record_load_store (aarch64_insn_decode_record *aarch64_insn_r)
99afc88b
OJ
5431{
5432 uint8_t insn_bits24_27, insn_bits28_29, insn_bits10_11;
5433 uint8_t insn_bit23, insn_bit21;
5434 uint8_t opc, size_bits, ld_flag, vector_flag;
5435 uint32_t reg_rn, reg_rt, reg_rt2;
5436 uint64_t datasize, offset;
5437 uint32_t record_buf[8];
5438 uint64_t record_buf_mem[8];
5439 CORE_ADDR address;
5440
5441 insn_bits10_11 = bits (aarch64_insn_r->aarch64_insn, 10, 11);
5442 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
5443 insn_bits28_29 = bits (aarch64_insn_r->aarch64_insn, 28, 29);
5444 insn_bit21 = bit (aarch64_insn_r->aarch64_insn, 21);
5445 insn_bit23 = bit (aarch64_insn_r->aarch64_insn, 23);
5446 ld_flag = bit (aarch64_insn_r->aarch64_insn, 22);
5447 vector_flag = bit (aarch64_insn_r->aarch64_insn, 26);
5448 reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
5449 reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
5450 reg_rt2 = bits (aarch64_insn_r->aarch64_insn, 10, 14);
5451 size_bits = bits (aarch64_insn_r->aarch64_insn, 30, 31);
5452
5453 /* Load/store exclusive. */
5454 if (insn_bits24_27 == 0x08 && insn_bits28_29 == 0x00)
5455 {
5456 if (record_debug)
b277c936 5457 debug_printf ("Process record: load/store exclusive\n");
99afc88b
OJ
5458
5459 if (ld_flag)
5460 {
5461 record_buf[0] = reg_rt;
5462 aarch64_insn_r->reg_rec_count = 1;
5463 if (insn_bit21)
5464 {
5465 record_buf[1] = reg_rt2;
5466 aarch64_insn_r->reg_rec_count = 2;
5467 }
5468 }
5469 else
5470 {
5471 if (insn_bit21)
5472 datasize = (8 << size_bits) * 2;
5473 else
5474 datasize = (8 << size_bits);
5475 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
5476 &address);
5477 record_buf_mem[0] = datasize / 8;
5478 record_buf_mem[1] = address;
5479 aarch64_insn_r->mem_rec_count = 1;
5480 if (!insn_bit23)
5481 {
5482 /* Save register rs. */
5483 record_buf[0] = bits (aarch64_insn_r->aarch64_insn, 16, 20);
5484 aarch64_insn_r->reg_rec_count = 1;
5485 }
5486 }
5487 }
5488 /* Load register (literal) instructions decoding. */
5489 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x01)
5490 {
5491 if (record_debug)
b277c936 5492 debug_printf ("Process record: load register (literal)\n");
99afc88b 5493 if (vector_flag)
dda83cd7 5494 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
99afc88b 5495 else
dda83cd7 5496 record_buf[0] = reg_rt;
99afc88b
OJ
5497 aarch64_insn_r->reg_rec_count = 1;
5498 }
5499 /* All types of load/store pair instructions decoding. */
5500 else if ((insn_bits24_27 & 0x0a) == 0x08 && insn_bits28_29 == 0x02)
5501 {
5502 if (record_debug)
b277c936 5503 debug_printf ("Process record: load/store pair\n");
99afc88b
OJ
5504
5505 if (ld_flag)
dda83cd7
SM
5506 {
5507 if (vector_flag)
5508 {
5509 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
5510 record_buf[1] = reg_rt2 + AARCH64_V0_REGNUM;
5511 }
5512 else
5513 {
5514 record_buf[0] = reg_rt;
5515 record_buf[1] = reg_rt2;
5516 }
5517 aarch64_insn_r->reg_rec_count = 2;
5518 }
99afc88b 5519 else
dda83cd7
SM
5520 {
5521 uint16_t imm7_off;
5522 imm7_off = bits (aarch64_insn_r->aarch64_insn, 15, 21);
5523 if (!vector_flag)
5524 size_bits = size_bits >> 1;
5525 datasize = 8 << (2 + size_bits);
5526 offset = (imm7_off & 0x40) ? (~imm7_off & 0x007f) + 1 : imm7_off;
5527 offset = offset << (2 + size_bits);
5528 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
5529 &address);
5530 if (!((insn_bits24_27 & 0x0b) == 0x08 && insn_bit23))
5531 {
5532 if (imm7_off & 0x40)
5533 address = address - offset;
5534 else
5535 address = address + offset;
5536 }
5537
5538 record_buf_mem[0] = datasize / 8;
5539 record_buf_mem[1] = address;
5540 record_buf_mem[2] = datasize / 8;
5541 record_buf_mem[3] = address + (datasize / 8);
5542 aarch64_insn_r->mem_rec_count = 2;
5543 }
99afc88b 5544 if (bit (aarch64_insn_r->aarch64_insn, 23))
dda83cd7 5545 record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
99afc88b
OJ
5546 }
5547 /* Load/store register (unsigned immediate) instructions. */
5548 else if ((insn_bits24_27 & 0x0b) == 0x09 && insn_bits28_29 == 0x03)
5549 {
5550 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
5551 if (!(opc >> 1))
33877125
YQ
5552 {
5553 if (opc & 0x01)
5554 ld_flag = 0x01;
5555 else
5556 ld_flag = 0x0;
5557 }
99afc88b 5558 else
33877125 5559 {
1e2b521d
YQ
5560 if (size_bits == 0x3 && vector_flag == 0x0 && opc == 0x2)
5561 {
5562 /* PRFM (immediate) */
5563 return AARCH64_RECORD_SUCCESS;
5564 }
5565 else if (size_bits == 0x2 && vector_flag == 0x0 && opc == 0x2)
5566 {
5567 /* LDRSW (immediate) */
5568 ld_flag = 0x1;
5569 }
33877125 5570 else
1e2b521d
YQ
5571 {
5572 if (opc & 0x01)
5573 ld_flag = 0x01;
5574 else
5575 ld_flag = 0x0;
5576 }
33877125 5577 }
99afc88b
OJ
5578
5579 if (record_debug)
5580 {
b277c936
PL
5581 debug_printf ("Process record: load/store (unsigned immediate):"
5582 " size %x V %d opc %x\n", size_bits, vector_flag,
5583 opc);
99afc88b
OJ
5584 }
5585
5586 if (!ld_flag)
dda83cd7
SM
5587 {
5588 offset = bits (aarch64_insn_r->aarch64_insn, 10, 21);
5589 datasize = 8 << size_bits;
5590 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
5591 &address);
5592 offset = offset << size_bits;
5593 address = address + offset;
5594
5595 record_buf_mem[0] = datasize >> 3;
5596 record_buf_mem[1] = address;
5597 aarch64_insn_r->mem_rec_count = 1;
5598 }
99afc88b 5599 else
dda83cd7
SM
5600 {
5601 if (vector_flag)
5602 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
5603 else
5604 record_buf[0] = reg_rt;
5605 aarch64_insn_r->reg_rec_count = 1;
5606 }
99afc88b
OJ
5607 }
5608 /* Load/store register (register offset) instructions. */
5d98d3cd
YQ
5609 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
5610 && insn_bits10_11 == 0x02 && insn_bit21)
99afc88b
OJ
5611 {
5612 if (record_debug)
b277c936 5613 debug_printf ("Process record: load/store (register offset)\n");
99afc88b
OJ
5614 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
5615 if (!(opc >> 1))
dda83cd7
SM
5616 if (opc & 0x01)
5617 ld_flag = 0x01;
5618 else
5619 ld_flag = 0x0;
99afc88b 5620 else
dda83cd7
SM
5621 if (size_bits != 0x03)
5622 ld_flag = 0x01;
5623 else
5624 return AARCH64_RECORD_UNKNOWN;
99afc88b
OJ
5625
5626 if (!ld_flag)
dda83cd7
SM
5627 {
5628 ULONGEST reg_rm_val;
5629
5630 regcache_raw_read_unsigned (aarch64_insn_r->regcache,
5631 bits (aarch64_insn_r->aarch64_insn, 16, 20), &reg_rm_val);
5632 if (bit (aarch64_insn_r->aarch64_insn, 12))
5633 offset = reg_rm_val << size_bits;
5634 else
5635 offset = reg_rm_val;
5636 datasize = 8 << size_bits;
5637 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
5638 &address);
5639 address = address + offset;
5640 record_buf_mem[0] = datasize >> 3;
5641 record_buf_mem[1] = address;
5642 aarch64_insn_r->mem_rec_count = 1;
5643 }
99afc88b 5644 else
dda83cd7
SM
5645 {
5646 if (vector_flag)
5647 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
5648 else
5649 record_buf[0] = reg_rt;
5650 aarch64_insn_r->reg_rec_count = 1;
5651 }
99afc88b
OJ
5652 }
5653 /* Load/store register (immediate and unprivileged) instructions. */
5d98d3cd
YQ
5654 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
5655 && !insn_bit21)
99afc88b
OJ
5656 {
5657 if (record_debug)
5658 {
b277c936
PL
5659 debug_printf ("Process record: load/store "
5660 "(immediate and unprivileged)\n");
99afc88b
OJ
5661 }
5662 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
5663 if (!(opc >> 1))
dda83cd7
SM
5664 if (opc & 0x01)
5665 ld_flag = 0x01;
5666 else
5667 ld_flag = 0x0;
99afc88b 5668 else
dda83cd7
SM
5669 if (size_bits != 0x03)
5670 ld_flag = 0x01;
5671 else
5672 return AARCH64_RECORD_UNKNOWN;
99afc88b
OJ
5673
5674 if (!ld_flag)
dda83cd7
SM
5675 {
5676 uint16_t imm9_off;
5677 imm9_off = bits (aarch64_insn_r->aarch64_insn, 12, 20);
5678 offset = (imm9_off & 0x0100) ? (((~imm9_off) & 0x01ff) + 1) : imm9_off;
5679 datasize = 8 << size_bits;
5680 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
5681 &address);
5682 if (insn_bits10_11 != 0x01)
5683 {
5684 if (imm9_off & 0x0100)
5685 address = address - offset;
5686 else
5687 address = address + offset;
5688 }
5689 record_buf_mem[0] = datasize >> 3;
5690 record_buf_mem[1] = address;
5691 aarch64_insn_r->mem_rec_count = 1;
5692 }
99afc88b 5693 else
dda83cd7
SM
5694 {
5695 if (vector_flag)
5696 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
5697 else
5698 record_buf[0] = reg_rt;
5699 aarch64_insn_r->reg_rec_count = 1;
5700 }
99afc88b 5701 if (insn_bits10_11 == 0x01 || insn_bits10_11 == 0x03)
dda83cd7 5702 record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
99afc88b 5703 }
ebd06ca6
TJB
5704 /* Memory Copy and Memory Set instructions. */
5705 else if ((insn_bits24_27 & 1) == 1 && insn_bits28_29 == 1
5706 && insn_bits10_11 == 1 && !insn_bit21)
5707 return aarch64_record_memcopy_memset (aarch64_insn_r);
99afc88b
OJ
5708 /* Advanced SIMD load/store instructions. */
5709 else
5710 return aarch64_record_asimd_load_store (aarch64_insn_r);
5711
5712 MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
dda83cd7 5713 record_buf_mem);
99afc88b 5714 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
dda83cd7 5715 record_buf);
99afc88b
OJ
5716 return AARCH64_RECORD_SUCCESS;
5717}
5718
5719/* Record handler for data processing SIMD and floating point instructions. */
5720
5721static unsigned int
4748a9be 5722aarch64_record_data_proc_simd_fp (aarch64_insn_decode_record *aarch64_insn_r)
99afc88b
OJ
5723{
5724 uint8_t insn_bit21, opcode, rmode, reg_rd;
5725 uint8_t insn_bits24_27, insn_bits28_31, insn_bits10_11, insn_bits12_15;
5726 uint8_t insn_bits11_14;
5727 uint32_t record_buf[2];
5728
5729 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
5730 insn_bits28_31 = bits (aarch64_insn_r->aarch64_insn, 28, 31);
5731 insn_bits10_11 = bits (aarch64_insn_r->aarch64_insn, 10, 11);
5732 insn_bits12_15 = bits (aarch64_insn_r->aarch64_insn, 12, 15);
5733 insn_bits11_14 = bits (aarch64_insn_r->aarch64_insn, 11, 14);
5734 opcode = bits (aarch64_insn_r->aarch64_insn, 16, 18);
5735 rmode = bits (aarch64_insn_r->aarch64_insn, 19, 20);
5736 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
5737 insn_bit21 = bit (aarch64_insn_r->aarch64_insn, 21);
5738
5739 if (record_debug)
b277c936 5740 debug_printf ("Process record: data processing SIMD/FP: ");
99afc88b
OJ
5741
5742 if ((insn_bits28_31 & 0x05) == 0x01 && insn_bits24_27 == 0x0e)
5743 {
5744 /* Floating point - fixed point conversion instructions. */
5745 if (!insn_bit21)
5746 {
5747 if (record_debug)
b277c936 5748 debug_printf ("FP - fixed point conversion");
99afc88b
OJ
5749
5750 if ((opcode >> 1) == 0x0 && rmode == 0x03)
5751 record_buf[0] = reg_rd;
5752 else
5753 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
5754 }
5755 /* Floating point - conditional compare instructions. */
5756 else if (insn_bits10_11 == 0x01)
5757 {
5758 if (record_debug)
b277c936 5759 debug_printf ("FP - conditional compare");
99afc88b
OJ
5760
5761 record_buf[0] = AARCH64_CPSR_REGNUM;
5762 }
5763 /* Floating point - data processing (2-source) and
dda83cd7 5764 conditional select instructions. */
99afc88b
OJ
5765 else if (insn_bits10_11 == 0x02 || insn_bits10_11 == 0x03)
5766 {
5767 if (record_debug)
b277c936 5768 debug_printf ("FP - DP (2-source)");
99afc88b
OJ
5769
5770 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
5771 }
5772 else if (insn_bits10_11 == 0x00)
5773 {
5774 /* Floating point - immediate instructions. */
5775 if ((insn_bits12_15 & 0x01) == 0x01
5776 || (insn_bits12_15 & 0x07) == 0x04)
5777 {
5778 if (record_debug)
b277c936 5779 debug_printf ("FP - immediate");
99afc88b
OJ
5780 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
5781 }
5782 /* Floating point - compare instructions. */
5783 else if ((insn_bits12_15 & 0x03) == 0x02)
5784 {
5785 if (record_debug)
b277c936 5786 debug_printf ("FP - immediate");
99afc88b
OJ
5787 record_buf[0] = AARCH64_CPSR_REGNUM;
5788 }
5789 /* Floating point - integer conversions instructions. */
f62fce35 5790 else if (insn_bits12_15 == 0x00)
99afc88b
OJ
5791 {
5792 /* Convert float to integer instruction. */
5793 if (!(opcode >> 1) || ((opcode >> 1) == 0x02 && !rmode))
5794 {
5795 if (record_debug)
b277c936 5796 debug_printf ("float to int conversion");
99afc88b
OJ
5797
5798 record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
5799 }
5800 /* Convert integer to float instruction. */
5801 else if ((opcode >> 1) == 0x01 && !rmode)
5802 {
5803 if (record_debug)
b277c936 5804 debug_printf ("int to float conversion");
99afc88b
OJ
5805
5806 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
5807 }
5808 /* Move float to integer instruction. */
5809 else if ((opcode >> 1) == 0x03)
5810 {
5811 if (record_debug)
b277c936 5812 debug_printf ("move float to int");
99afc88b
OJ
5813
5814 if (!(opcode & 0x01))
5815 record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
5816 else
5817 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
5818 }
f62fce35
YQ
5819 else
5820 return AARCH64_RECORD_UNKNOWN;
dda83cd7 5821 }
f62fce35
YQ
5822 else
5823 return AARCH64_RECORD_UNKNOWN;
dda83cd7 5824 }
f62fce35
YQ
5825 else
5826 return AARCH64_RECORD_UNKNOWN;
99afc88b
OJ
5827 }
5828 else if ((insn_bits28_31 & 0x09) == 0x00 && insn_bits24_27 == 0x0e)
5829 {
5830 if (record_debug)
b277c936 5831 debug_printf ("SIMD copy");
99afc88b
OJ
5832
5833 /* Advanced SIMD copy instructions. */
5834 if (!bits (aarch64_insn_r->aarch64_insn, 21, 23)
5835 && !bit (aarch64_insn_r->aarch64_insn, 15)
5836 && bit (aarch64_insn_r->aarch64_insn, 10))
5837 {
5838 if (insn_bits11_14 == 0x05 || insn_bits11_14 == 0x07)
5839 record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
5840 else
5841 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
5842 }
5843 else
5844 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
5845 }
5846 /* All remaining floating point or advanced SIMD instructions. */
5847 else
5848 {
5849 if (record_debug)
b277c936 5850 debug_printf ("all remain");
99afc88b
OJ
5851
5852 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
5853 }
5854
5855 if (record_debug)
b277c936 5856 debug_printf ("\n");
99afc88b 5857
bfbe4b84 5858 /* Record the V/X register. */
99afc88b 5859 aarch64_insn_r->reg_rec_count++;
bfbe4b84
LM
5860
5861 /* Some of these instructions may set bits in the FPSR, so record it
5862 too. */
5863 record_buf[1] = AARCH64_FPSR_REGNUM;
5864 aarch64_insn_r->reg_rec_count++;
5865
5866 gdb_assert (aarch64_insn_r->reg_rec_count == 2);
99afc88b
OJ
5867 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
5868 record_buf);
5869 return AARCH64_RECORD_SUCCESS;
5870}
5871
5872/* Decodes insns type and invokes its record handler. */
5873
5874static unsigned int
4748a9be 5875aarch64_record_decode_insn_handler (aarch64_insn_decode_record *aarch64_insn_r)
99afc88b
OJ
5876{
5877 uint32_t ins_bit25, ins_bit26, ins_bit27, ins_bit28;
5878
5879 ins_bit25 = bit (aarch64_insn_r->aarch64_insn, 25);
5880 ins_bit26 = bit (aarch64_insn_r->aarch64_insn, 26);
5881 ins_bit27 = bit (aarch64_insn_r->aarch64_insn, 27);
5882 ins_bit28 = bit (aarch64_insn_r->aarch64_insn, 28);
5883
5884 /* Data processing - immediate instructions. */
5885 if (!ins_bit26 && !ins_bit27 && ins_bit28)
5886 return aarch64_record_data_proc_imm (aarch64_insn_r);
5887
5888 /* Branch, exception generation and system instructions. */
5889 if (ins_bit26 && !ins_bit27 && ins_bit28)
5890 return aarch64_record_branch_except_sys (aarch64_insn_r);
5891
5892 /* Load and store instructions. */
5893 if (!ins_bit25 && ins_bit27)
5894 return aarch64_record_load_store (aarch64_insn_r);
5895
5896 /* Data processing - register instructions. */
5897 if (ins_bit25 && !ins_bit26 && ins_bit27)
5898 return aarch64_record_data_proc_reg (aarch64_insn_r);
5899
5900 /* Data processing - SIMD and floating point instructions. */
5901 if (ins_bit25 && ins_bit26 && ins_bit27)
5902 return aarch64_record_data_proc_simd_fp (aarch64_insn_r);
5903
5904 return AARCH64_RECORD_UNSUPPORTED;
5905}
5906
5907/* Cleans up local record registers and memory allocations. */
5908
5909static void
4748a9be 5910deallocate_reg_mem (aarch64_insn_decode_record *record)
99afc88b
OJ
5911{
5912 xfree (record->aarch64_regs);
5913 xfree (record->aarch64_mems);
5914}
5915
1e2b521d
YQ
5916#if GDB_SELF_TEST
5917namespace selftests {
5918
5919static void
5920aarch64_process_record_test (void)
5921{
5922 struct gdbarch_info info;
5923 uint32_t ret;
5924
1e2b521d
YQ
5925 info.bfd_arch_info = bfd_scan_arch ("aarch64");
5926
5927 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
5928 SELF_CHECK (gdbarch != NULL);
5929
4748a9be 5930 aarch64_insn_decode_record aarch64_record;
1e2b521d 5931
4748a9be 5932 memset (&aarch64_record, 0, sizeof (aarch64_insn_decode_record));
1e2b521d
YQ
5933 aarch64_record.regcache = NULL;
5934 aarch64_record.this_addr = 0;
5935 aarch64_record.gdbarch = gdbarch;
5936
5937 /* 20 00 80 f9 prfm pldl1keep, [x1] */
5938 aarch64_record.aarch64_insn = 0xf9800020;
5939 ret = aarch64_record_decode_insn_handler (&aarch64_record);
5940 SELF_CHECK (ret == AARCH64_RECORD_SUCCESS);
5941 SELF_CHECK (aarch64_record.reg_rec_count == 0);
5942 SELF_CHECK (aarch64_record.mem_rec_count == 0);
5943
5944 deallocate_reg_mem (&aarch64_record);
5945}
5946
b6fb76ec 5947} /* namespace selftests */
1e2b521d
YQ
5948#endif /* GDB_SELF_TEST */
5949
99afc88b
OJ
5950/* Parse the current instruction and record the values of the registers and
5951 memory that will be changed in current instruction to record_arch_list
5952 return -1 if something is wrong. */
5953
5954int
5955aarch64_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
5956 CORE_ADDR insn_addr)
5957{
5958 uint32_t rec_no = 0;
6e4be957 5959 const uint8_t insn_size = 4;
99afc88b 5960 uint32_t ret = 0;
99afc88b 5961 gdb_byte buf[insn_size];
4748a9be 5962 aarch64_insn_decode_record aarch64_record;
99afc88b
OJ
5963
5964 memset (&buf[0], 0, insn_size);
4748a9be 5965 memset (&aarch64_record, 0, sizeof (aarch64_insn_decode_record));
99afc88b
OJ
5966 target_read_memory (insn_addr, &buf[0], insn_size);
5967 aarch64_record.aarch64_insn
5968 = (uint32_t) extract_unsigned_integer (&buf[0],
5969 insn_size,
5970 gdbarch_byte_order (gdbarch));
5971 aarch64_record.regcache = regcache;
5972 aarch64_record.this_addr = insn_addr;
5973 aarch64_record.gdbarch = gdbarch;
5974
5975 ret = aarch64_record_decode_insn_handler (&aarch64_record);
5976 if (ret == AARCH64_RECORD_UNSUPPORTED)
5977 {
6cb06a8c
TT
5978 gdb_printf (gdb_stderr,
5979 _("Process record does not support instruction "
5980 "0x%0x at address %s.\n"),
5981 aarch64_record.aarch64_insn,
5982 paddress (gdbarch, insn_addr));
99afc88b
OJ
5983 ret = -1;
5984 }
5985
5986 if (0 == ret)
5987 {
5988 /* Record registers. */
5989 record_full_arch_list_add_reg (aarch64_record.regcache,
5990 AARCH64_PC_REGNUM);
5991 /* Always record register CPSR. */
5992 record_full_arch_list_add_reg (aarch64_record.regcache,
5993 AARCH64_CPSR_REGNUM);
5994 if (aarch64_record.aarch64_regs)
5995 for (rec_no = 0; rec_no < aarch64_record.reg_rec_count; rec_no++)
5996 if (record_full_arch_list_add_reg (aarch64_record.regcache,
5997 aarch64_record.aarch64_regs[rec_no]))
5998 ret = -1;
5999
6000 /* Record memories. */
6001 if (aarch64_record.aarch64_mems)
6002 for (rec_no = 0; rec_no < aarch64_record.mem_rec_count; rec_no++)
6003 if (record_full_arch_list_add_mem
6004 ((CORE_ADDR)aarch64_record.aarch64_mems[rec_no].addr,
6005 aarch64_record.aarch64_mems[rec_no].len))
6006 ret = -1;
6007
6008 if (record_full_arch_list_add_end ())
6009 ret = -1;
6010 }
6011
6012 deallocate_reg_mem (&aarch64_record);
6013 return ret;
6014}