]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/aarch64-tdep.c
f2e3ce2a36c0d01454c9b72c583cf3141eadb3dd
[thirdparty/binutils-gdb.git] / gdb / aarch64-tdep.c
1 /* Common target dependent code for GDB on AArch64 systems.
2
3 Copyright (C) 2009-2025 Free Software Foundation, Inc.
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
21
22 #include "extract-store-integer.h"
23 #include "frame.h"
24 #include "language.h"
25 #include "cli/cli-cmds.h"
26 #include "gdbcore.h"
27 #include "dis-asm.h"
28 #include "regcache.h"
29 #include "reggroups.h"
30 #include "value.h"
31 #include "arch-utils.h"
32 #include "osabi.h"
33 #include "frame-unwind.h"
34 #include "frame-base.h"
35 #include "trad-frame.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "dwarf2/frame.h"
39 #include "gdbtypes.h"
40 #include "prologue-value.h"
41 #include "target-descriptions.h"
42 #include "user-regs.h"
43 #include "ax-gdb.h"
44 #include "gdbsupport/selftest.h"
45
46 #include "aarch64-tdep.h"
47 #include "aarch64-ravenscar-thread.h"
48 #include "arch/aarch64-mte.h"
49
50 #include "record.h"
51 #include "record-full.h"
52 #include "arch/aarch64-insn.h"
53 #include "gdbarch.h"
54
55 #include "opcode/aarch64.h"
56 #include <algorithm>
57 #include <unordered_map>
58
59 /* For inferior_ptid and current_inferior (). */
60 #include "inferior.h"
61 /* For std::sqrt and std::pow. */
62 #include <cmath>
63
64 /* A Homogeneous Floating-Point or Short-Vector Aggregate may have at most
65 four members. */
66 #define HA_MAX_NUM_FLDS 4
67
68 /* All possible aarch64 target descriptors. */
69 static std::unordered_map <aarch64_features, target_desc *> tdesc_aarch64_map;
70
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. */
74 static const struct
75 {
76 const char *const name;
77 int regnum;
78 } aarch64_register_aliases[] =
79 {
80 /* Link register alias for x30. */
81 {"lr", AARCH64_LR_REGNUM},
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},
85 /* specials */
86 {"ip0", AARCH64_X0_REGNUM + 16},
87 {"ip1", AARCH64_X0_REGNUM + 17}
88 };
89
90 /* The required core 'R' registers. */
91 static 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. */
107 static 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
123 /* The SVE 'Z' and 'P' registers. */
124 static 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
144 static const char *const aarch64_pauth_register_names[] =
145 {
146 /* Authentication mask for data pointer, low half/user pointers. */
147 "pauth_dmask",
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"
154 };
155
156 static const char *const aarch64_mte_register_names[] =
157 {
158 /* Tag Control Register. */
159 "tag_ctl"
160 };
161
162 static int aarch64_stack_frame_destroyed_p (struct gdbarch *, CORE_ADDR);
163
164 /* AArch64 prologue cache structure. */
165 struct aarch64_prologue_cache
166 {
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
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
181 /* Is the target available to read from? */
182 int available_p;
183
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. */
193 trad_frame_saved_reg *saved_regs;
194 };
195
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
204 struct 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
221 struct 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
233 static void
234 show_aarch64_debug (struct ui_file *file, int from_tty,
235 struct cmd_list_element *c, const char *value)
236 {
237 gdb_printf (file, _("AArch64 debugging is %s.\n"), value);
238 }
239
240 namespace {
241
242 /* Abstract instruction reader. */
243
244 class abstract_instruction_reader
245 {
246 public:
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
254 class instruction_reader : public abstract_instruction_reader
255 {
256 public:
257 ULONGEST read (CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
258 override
259 {
260 return read_code_unsigned_integer (memaddr, len, byte_order);
261 }
262 };
263
264 } // namespace
265
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. */
269
270 static CORE_ADDR
271 aarch64_frame_unmask_lr (aarch64_gdbarch_tdep *tdep,
272 const frame_info_ptr &this_frame, CORE_ADDR addr)
273 {
274 if (tdep->has_pauth ()
275 && frame_unwind_register_unsigned (this_frame,
276 tdep->ra_sign_state_regnum))
277 {
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);
291
292 /* Record in the frame that the link register required unmasking. */
293 set_frame_previous_pc_masked (this_frame);
294 }
295
296 return addr;
297 }
298
299 /* Implement the "get_pc_address_flags" gdbarch method. */
300
301 static std::string
302 aarch64_get_pc_address_flags (const frame_info_ptr &frame, CORE_ADDR pc)
303 {
304 if (pc != 0 && get_frame_pc_masked (frame))
305 return "PAC";
306
307 return "";
308 }
309
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
314 static CORE_ADDR
315 aarch64_analyze_prologue (struct gdbarch *gdbarch,
316 CORE_ADDR start, CORE_ADDR limit,
317 struct aarch64_prologue_cache *cache,
318 abstract_instruction_reader& reader)
319 {
320 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
321 int i;
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
325 data, in case the FP is omitted. */
326 bool seen_stack_set = false;
327
328 /* Track X registers and D registers in prologue. */
329 pv_t regs[AARCH64_X_REGISTER_COUNT + AARCH64_D_REGISTER_COUNT];
330
331 for (i = 0; i < AARCH64_X_REGISTER_COUNT + AARCH64_D_REGISTER_COUNT; i++)
332 regs[i] = pv_register (i, 0);
333 pv_area stack (AARCH64_SP_REGNUM, gdbarch_addr_bit (gdbarch));
334
335 for (; start < limit; start += 4)
336 {
337 uint32_t insn;
338 aarch64_inst inst;
339
340 insn = reader.read (start, 4, byte_order_for_code);
341
342 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
343 break;
344
345 if (inst.opcode->iclass == addsub_imm
346 && (inst.opcode->op == OP_ADD
347 || strcmp ("sub", inst.opcode->name) == 0))
348 {
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 }
367
368 /* Did we move SP to FP? */
369 if (rn == AARCH64_SP_REGNUM && rd == AARCH64_FP_REGNUM)
370 seen_stack_set = true;
371 }
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 }
386 else if (inst.opcode->iclass == branch_imm)
387 {
388 /* Stop analysis on branch. */
389 break;
390 }
391 else if (inst.opcode->iclass == condbranch)
392 {
393 /* Stop analysis on branch. */
394 break;
395 }
396 else if (inst.opcode->iclass == branch_reg)
397 {
398 /* Stop analysis on branch. */
399 break;
400 }
401 else if (inst.opcode->iclass == compbranch)
402 {
403 /* Stop analysis on branch. */
404 break;
405 }
406 else if (inst.opcode->op == OP_MOVZ)
407 {
408 unsigned rd = inst.operands[0].reg.regno;
409
410 gdb_assert (aarch64_num_of_operands (inst.opcode) == 2);
411 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
412 gdb_assert (inst.operands[1].type == AARCH64_OPND_HALF);
413 gdb_assert (inst.operands[1].shifter.kind == AARCH64_MOD_LSL);
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
420 regs[rd] = pv_constant (inst.operands[1].imm.value
421 << inst.operands[1].shifter.amount);
422 }
423 else if (inst.opcode->iclass == log_shift
424 && strcmp (inst.opcode->name, "orr") == 0)
425 {
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)
436 regs[rd] = regs[rm];
437 else
438 {
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
443 break;
444 }
445 }
446 else if (inst.opcode->op == OP_STUR)
447 {
448 unsigned rt = inst.operands[0].reg.regno;
449 unsigned rn = inst.operands[1].addr.base_regno;
450 int size = aarch64_get_qualifier_esize (inst.operands[0].qualifier);
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
457 stack.store
458 (pv_add_constant (regs[rn], inst.operands[1].addr.offset.imm),
459 size, regs[rt]);
460
461 /* Are we storing with SP as a base? */
462 if (rn == AARCH64_SP_REGNUM)
463 seen_stack_set = true;
464 }
465 else if ((inst.opcode->iclass == ldstpair_off
466 || (inst.opcode->iclass == ldstpair_indexed
467 && inst.operands[2].addr.preind))
468 && strcmp ("stp", inst.opcode->name) == 0)
469 {
470 /* STP with addressing mode Pre-indexed and Base register. */
471 unsigned rt1;
472 unsigned rt2;
473 unsigned rn = inst.operands[2].addr.base_regno;
474 int32_t imm = inst.operands[2].addr.offset.imm;
475 int size = aarch64_get_qualifier_esize (inst.operands[0].qualifier);
476
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);
481 gdb_assert (inst.operands[2].type == AARCH64_OPND_ADDR_SIMM7);
482 gdb_assert (!inst.operands[2].addr.offset.is_reg);
483
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. */
487 if (stack.store_would_trash (pv_add_constant (regs[rn], imm)))
488 break;
489
490 if (stack.store_would_trash (pv_add_constant (regs[rn], imm + 8)))
491 break;
492
493 rt1 = inst.operands[0].reg.regno;
494 rt2 = inst.operands[1].reg.regno;
495 if (inst.operands[0].type == AARCH64_OPND_Ft)
496 {
497 rt1 += AARCH64_X_REGISTER_COUNT;
498 rt2 += AARCH64_X_REGISTER_COUNT;
499 }
500
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]);
503
504 if (inst.operands[2].addr.writeback)
505 regs[rn] = pv_add_constant (regs[rn], imm);
506
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;
511 }
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;
523 int size = aarch64_get_qualifier_esize (inst.operands[0].qualifier);
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)
528 rt += AARCH64_X_REGISTER_COUNT;
529
530 stack.store (pv_add_constant (regs[rn], imm), size, regs[rt]);
531 if (inst.operands[1].addr.writeback)
532 regs[rn] = pv_add_constant (regs[rn], imm);
533
534 /* Are we storing with SP as a base? */
535 if (rn == AARCH64_SP_REGNUM)
536 seen_stack_set = true;
537 }
538 else if (inst.opcode->iclass == testbranch)
539 {
540 /* Stop analysis on branch. */
541 break;
542 }
543 else if (inst.opcode->iclass == ic_system)
544 {
545 aarch64_gdbarch_tdep *tdep
546 = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
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 }
561 else if (IS_BTI (insn))
562 /* We don't need to do anything special for a BTI instruction. */
563 continue;
564 else
565 {
566 aarch64_debug_printf ("prologue analysis gave up addr=%s"
567 " opcode=0x%x (iclass)",
568 core_addr_to_string_nz (start), insn);
569 break;
570 }
571
572 if (tdep->has_pauth () && cache != nullptr)
573 {
574 int regnum = tdep->ra_sign_state_regnum;
575 cache->saved_regs[regnum].set_value (ra_state_val);
576 }
577 }
578 else
579 {
580 aarch64_debug_printf ("prologue analysis gave up addr=%s"
581 " opcode=0x%x",
582 core_addr_to_string_nz (start), insn);
583
584 break;
585 }
586 }
587
588 if (cache == NULL)
589 return start;
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
614 if (stack.find_reg (gdbarch, i, &offset))
615 cache->saved_regs[i].set_addr (offset);
616 }
617
618 for (i = 0; i < AARCH64_D_REGISTER_COUNT; i++)
619 {
620 int regnum = gdbarch_num_regs (gdbarch);
621 CORE_ADDR offset;
622
623 if (stack.find_reg (gdbarch, i + AARCH64_X_REGISTER_COUNT,
624 &offset))
625 cache->saved_regs[i + regnum + AARCH64_D0_REGNUM].set_addr (offset);
626 }
627
628 return start;
629 }
630
631 static CORE_ADDR
632 aarch64_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
644 namespace selftests {
645
646 /* Instruction reader from manually cooked instruction sequences. */
647
648 class instruction_reader_test : public abstract_instruction_reader
649 {
650 public:
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)
657 override
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
666 private:
667 const uint32_t *m_insns;
668 size_t m_insns_size;
669 };
670
671 static void
672 aarch64_analyze_prologue_test (void)
673 {
674 struct gdbarch_info info;
675
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
681 struct aarch64_prologue_cache cache;
682 cache.saved_regs = trad_frame_alloc_saved_regs (gdbarch);
683
684 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
685
686 /* Test the simple prologue in which frame pointer is used. */
687 {
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)
704 SELF_CHECK (cache.saved_regs[i].addr () == -272);
705 else if (i == AARCH64_LR_REGNUM)
706 SELF_CHECK (cache.saved_regs[i].addr () == -264);
707 else
708 SELF_CHECK (cache.saved_regs[i].is_realreg ()
709 && cache.saved_regs[i].realreg () == i);
710 }
711
712 for (int i = 0; i < AARCH64_D_REGISTER_COUNT; i++)
713 {
714 int num_regs = gdbarch_num_regs (gdbarch);
715 int regnum = i + num_regs + AARCH64_D0_REGNUM;
716
717 SELF_CHECK (cache.saved_regs[regnum].is_realreg ()
718 && cache.saved_regs[regnum].realreg () == regnum);
719 }
720 }
721
722 /* Test a prologue in which STR is used and frame pointer is not
723 used. */
724 {
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
735 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
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)
746 SELF_CHECK (cache.saved_regs[i].addr () == -16);
747 else if (i == 19)
748 SELF_CHECK (cache.saved_regs[i].addr () == -48);
749 else
750 SELF_CHECK (cache.saved_regs[i].is_realreg ()
751 && cache.saved_regs[i].realreg () == i);
752 }
753
754 for (int i = 0; i < AARCH64_D_REGISTER_COUNT; i++)
755 {
756 int num_regs = gdbarch_num_regs (gdbarch);
757 int regnum = i + num_regs + AARCH64_D0_REGNUM;
758
759
760 if (i == 0)
761 SELF_CHECK (cache.saved_regs[regnum].addr () == -24);
762 else
763 SELF_CHECK (cache.saved_regs[regnum].is_realreg ()
764 && cache.saved_regs[regnum].realreg () == regnum);
765 }
766 }
767
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
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
891 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
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)
902 SELF_CHECK (cache.saved_regs[i].addr () == -20);
903 else if (i == AARCH64_FP_REGNUM)
904 SELF_CHECK (cache.saved_regs[i].addr () == -48);
905 else if (i == AARCH64_LR_REGNUM)
906 SELF_CHECK (cache.saved_regs[i].addr () == -40);
907 else
908 SELF_CHECK (cache.saved_regs[i].is_realreg ()
909 && cache.saved_regs[i].realreg () == i);
910 }
911
912 if (tdep->has_pauth ())
913 {
914 int regnum = tdep->ra_sign_state_regnum;
915 SELF_CHECK (cache.saved_regs[regnum].is_value ());
916 }
917 }
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 }
951 }
952 } /* namespace selftests */
953 #endif /* GDB_SELF_TEST */
954
955 /* Implement the "skip_prologue" gdbarch method. */
956
957 static CORE_ADDR
958 aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
959 {
960 CORE_ADDR func_addr, func_end_addr, limit_pc;
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. */
965 bool func_addr_found
966 = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
967
968 if (func_addr_found)
969 {
970 CORE_ADDR post_prologue_pc
971 = skip_prologue_using_sal (gdbarch, func_addr);
972
973 if (post_prologue_pc != 0)
974 return std::max (pc, post_prologue_pc);
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
988 limit_pc
989 = func_end_addr == 0 ? limit_pc : std::min (limit_pc, func_end_addr - 4);
990
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
998 static void
999 aarch64_scan_prologue (const frame_info_ptr &this_frame,
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
1008 cache->prev_pc = prev_pc;
1009
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
1030 prologue_end = std::min (prologue_end, prev_pc);
1031 aarch64_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1032 }
1033 else
1034 {
1035 CORE_ADDR frame_loc;
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;
1043 cache->saved_regs[29].set_addr (0);
1044 cache->saved_regs[30].set_addr (8);
1045 }
1046 }
1047
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. */
1051
1052 static void
1053 aarch64_make_prologue_cache_1 (const frame_info_ptr &this_frame,
1054 struct aarch64_prologue_cache *cache)
1055 {
1056 CORE_ADDR unwound_fp;
1057 int reg;
1058
1059 aarch64_scan_prologue (this_frame, cache);
1060
1061 if (cache->framereg == -1)
1062 return;
1063
1064 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
1065 if (unwound_fp == 0)
1066 return;
1067
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;
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++)
1076 if (cache->saved_regs[reg].is_addr ())
1077 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
1078 + cache->prev_sp);
1079
1080 cache->func = get_frame_func (this_frame);
1081
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
1090 static struct aarch64_prologue_cache *
1091 aarch64_make_prologue_cache (const frame_info_ptr &this_frame, void **this_cache)
1092 {
1093 struct aarch64_prologue_cache *cache;
1094
1095 if (*this_cache != NULL)
1096 return (struct aarch64_prologue_cache *) *this_cache;
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
1102 try
1103 {
1104 aarch64_make_prologue_cache_1 (this_frame, cache);
1105 }
1106 catch (const gdb_exception_error &ex)
1107 {
1108 if (ex.error != NOT_AVAILABLE_ERROR)
1109 throw;
1110 }
1111
1112 return cache;
1113 }
1114
1115 /* Implement the "stop_reason" frame_unwind method. */
1116
1117 static enum unwind_stop_reason
1118 aarch64_prologue_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
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". */
1128 gdbarch *arch = get_frame_arch (this_frame);
1129 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (arch);
1130 if (cache->prev_pc <= tdep->lowest_pc)
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
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
1143 static void
1144 aarch64_prologue_this_id (const frame_info_ptr &this_frame,
1145 void **this_cache, struct frame_id *this_id)
1146 {
1147 struct aarch64_prologue_cache *cache
1148 = aarch64_make_prologue_cache (this_frame, this_cache);
1149
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);
1154 }
1155
1156 /* Implement the "prev_register" frame_unwind method. */
1157
1158 static struct value *
1159 aarch64_prologue_prev_register (const frame_info_ptr &this_frame,
1160 void **this_cache, int prev_regnum)
1161 {
1162 struct aarch64_prologue_cache *cache
1163 = aarch64_make_prologue_cache (this_frame, this_cache);
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;
1171 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1172 aarch64_gdbarch_tdep *tdep
1173 = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1174
1175 lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
1176
1177 if (tdep->has_pauth ()
1178 && cache->saved_regs[tdep->ra_sign_state_regnum].is_value ())
1179 lr = aarch64_frame_unmask_lr (tdep, this_frame, lr);
1180
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 /*
1188 +----------+ ^
1189 | saved lr | |
1190 +->| saved fp |--+
1191 | | |
1192 | | | <- Previous SP
1193 | +----------+
1194 | | saved lr |
1195 +--| saved fp |<- FP
1196 | |
1197 | |<- SP
1198 +----------+ */
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. */
1208 static const frame_unwind_legacy aarch64_prologue_unwind (
1209 "aarch64 prologue",
1210 NORMAL_FRAME,
1211 FRAME_UNWIND_ARCH,
1212 aarch64_prologue_frame_unwind_stop_reason,
1213 aarch64_prologue_this_id,
1214 aarch64_prologue_prev_register,
1215 NULL,
1216 default_frame_sniffer
1217 );
1218
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. */
1223
1224 static struct aarch64_prologue_cache *
1225 aarch64_make_stub_cache (const frame_info_ptr &this_frame, void **this_cache)
1226 {
1227 struct aarch64_prologue_cache *cache;
1228
1229 if (*this_cache != NULL)
1230 return (struct aarch64_prologue_cache *) *this_cache;
1231
1232 cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
1233 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1234 *this_cache = cache;
1235
1236 try
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 }
1243 catch (const gdb_exception_error &ex)
1244 {
1245 if (ex.error != NOT_AVAILABLE_ERROR)
1246 throw;
1247 }
1248
1249 return cache;
1250 }
1251
1252 /* Implement the "stop_reason" frame_unwind method. */
1253
1254 static enum unwind_stop_reason
1255 aarch64_stub_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
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
1267 /* Our frame ID for a stub frame is the current SP and LR. */
1268
1269 static void
1270 aarch64_stub_this_id (const frame_info_ptr &this_frame,
1271 void **this_cache, struct frame_id *this_id)
1272 {
1273 struct aarch64_prologue_cache *cache
1274 = aarch64_make_stub_cache (this_frame, this_cache);
1275
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);
1280 }
1281
1282 /* Implement the "sniffer" frame_unwind method. */
1283
1284 static int
1285 aarch64_stub_unwind_sniffer (const struct frame_unwind *self,
1286 const frame_info_ptr &this_frame,
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);
1293 if (in_plt_section (addr_in_block)
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. */
1303 static const frame_unwind_legacy aarch64_stub_unwind (
1304 "aarch64 stub",
1305 NORMAL_FRAME,
1306 FRAME_UNWIND_ARCH,
1307 aarch64_stub_frame_unwind_stop_reason,
1308 aarch64_stub_this_id,
1309 aarch64_prologue_prev_register,
1310 NULL,
1311 aarch64_stub_unwind_sniffer
1312 );
1313
1314 /* Return the frame base address of *THIS_FRAME. */
1315
1316 static CORE_ADDR
1317 aarch64_normal_frame_base (const frame_info_ptr &this_frame, void **this_cache)
1318 {
1319 struct aarch64_prologue_cache *cache
1320 = aarch64_make_prologue_cache (this_frame, this_cache);
1321
1322 return cache->prev_sp - cache->framesize;
1323 }
1324
1325 /* AArch64 default frame base information. */
1326 static frame_base aarch64_normal_base =
1327 {
1328 &aarch64_prologue_unwind,
1329 aarch64_normal_frame_base,
1330 aarch64_normal_frame_base,
1331 aarch64_normal_frame_base
1332 };
1333
1334 /* Return the value of the REGNUM register in the previous frame of
1335 *THIS_FRAME. */
1336
1337 static struct value *
1338 aarch64_dwarf2_prev_register (const frame_info_ptr &this_frame,
1339 void **this_cache, int regnum)
1340 {
1341 gdbarch *arch = get_frame_arch (this_frame);
1342 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (arch);
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);
1349 lr = aarch64_frame_unmask_lr (tdep, this_frame, lr);
1350 return frame_unwind_got_constant (this_frame, regnum, lr);
1351
1352 default:
1353 internal_error (_("Unexpected register %d"), regnum);
1354 }
1355 }
1356
1357 static const unsigned char op_lit0 = DW_OP_lit0;
1358 static const unsigned char op_lit1 = DW_OP_lit1;
1359
1360 /* Implement the "init_reg" dwarf2_frame_ops method. */
1361
1362 static void
1363 aarch64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1364 struct dwarf2_frame_state_reg *reg,
1365 const frame_info_ptr &this_frame)
1366 {
1367 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1368
1369 switch (regnum)
1370 {
1371 case AARCH64_PC_REGNUM:
1372 reg->how = DWARF2_FRAME_REG_FN;
1373 reg->loc.fn = aarch64_dwarf2_prev_register;
1374 return;
1375
1376 case AARCH64_SP_REGNUM:
1377 reg->how = DWARF2_FRAME_REG_CFA;
1378 return;
1379 }
1380
1381 /* Init pauth registers. */
1382 if (tdep->has_pauth ())
1383 {
1384 if (regnum == tdep->ra_sign_state_regnum)
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 }
1392 else if (regnum >= tdep->pauth_reg_base
1393 && regnum < tdep->pauth_reg_base + tdep->pauth_reg_count)
1394 {
1395 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1396 return;
1397 }
1398 }
1399 }
1400
1401 /* Implement the execute_dwarf_cfa_vendor_op method. */
1402
1403 static bool
1404 aarch64_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
1405 struct dwarf2_frame_state *fs)
1406 {
1407 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
1408 struct dwarf2_frame_state_reg *ra_state;
1409
1410 if (op == DW_CFA_AARCH64_negate_ra_state)
1411 {
1412 /* On systems without pauth, treat as a nop. */
1413 if (!tdep->has_pauth ())
1414 return true;
1415
1416 /* Allocate RA_STATE column if it's not allocated yet. */
1417 fs->regs.alloc_regs (AARCH64_DWARF_RA_SIGN_STATE + 1);
1418
1419 /* Toggle the status of RA_STATE between 0 and 1. */
1420 ra_state = &(fs->regs.reg[AARCH64_DWARF_RA_SIGN_STATE]);
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
1437 /* Used for matching BRK instructions for AArch64. */
1438 static constexpr uint32_t BRK_INSN_MASK = 0xffe0001f;
1439 static constexpr uint32_t BRK_INSN_BASE = 0xd4200000;
1440
1441 /* Implementation of gdbarch_program_breakpoint_here_p for aarch64. */
1442
1443 static bool
1444 aarch64_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
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
1473 struct stack_item_t
1474 {
1475 /* Value to pass on stack. It can be NULL if this item is for stack
1476 padding. */
1477 const gdb_byte *data;
1478
1479 /* Size in bytes of value to pass on stack. */
1480 int len;
1481 };
1482
1483 /* Implement the gdbarch type alignment method, overrides the generic
1484 alignment algorithm for anything that is aarch64 specific. */
1485
1486 static ULONGEST
1487 aarch64_type_align (gdbarch *gdbarch, struct type *t)
1488 {
1489 t = check_typedef (t);
1490 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
1491 {
1492 /* Use the natural alignment for vector types (the same for
1493 scalar type), but the maximum alignment is 128-bit. */
1494 if (t->length () > 16)
1495 return 16;
1496 else
1497 return t->length ();
1498 }
1499
1500 /* Allow the common code to calculate the alignment. */
1501 return 0;
1502 }
1503
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
1512 static int
1513 aapcs_is_vfp_call_or_return_candidate_1 (struct type *type,
1514 struct type **fundamental_type)
1515 {
1516 if (type == nullptr)
1517 return -1;
1518
1519 switch (type->code ())
1520 {
1521 case TYPE_CODE_FLT:
1522 case TYPE_CODE_DECFLOAT:
1523 if (type->length () > 16)
1524 return -1;
1525
1526 if (*fundamental_type == nullptr)
1527 *fundamental_type = type;
1528 else if (type->length () != (*fundamental_type)->length ()
1529 || type->code () != (*fundamental_type)->code ())
1530 return -1;
1531
1532 return 1;
1533
1534 case TYPE_CODE_COMPLEX:
1535 {
1536 struct type *target_type = check_typedef (type->target_type ());
1537 if (target_type->length () > 16)
1538 return -1;
1539
1540 if (*fundamental_type == nullptr)
1541 *fundamental_type = target_type;
1542 else if (target_type->length () != (*fundamental_type)->length ()
1543 || target_type->code () != (*fundamental_type)->code ())
1544 return -1;
1545
1546 return 2;
1547 }
1548
1549 case TYPE_CODE_ARRAY:
1550 {
1551 if (type->is_vector ())
1552 {
1553 if (type->length () != 8 && type->length () != 16)
1554 return -1;
1555
1556 if (*fundamental_type == nullptr)
1557 *fundamental_type = type;
1558 else if (type->length () != (*fundamental_type)->length ()
1559 || type->code () != (*fundamental_type)->code ())
1560 return -1;
1561
1562 return 1;
1563 }
1564 else
1565 {
1566 struct type *target_type = type->target_type ();
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
1573 count *= (type->length () / target_type->length ());
1574 return count;
1575 }
1576 }
1577
1578 case TYPE_CODE_STRUCT:
1579 case TYPE_CODE_UNION:
1580 {
1581 int count = 0;
1582
1583 for (int i = 0; i < type->num_fields (); i++)
1584 {
1585 /* Ignore any static fields. */
1586 if (type->field (i).is_static ())
1587 continue;
1588
1589 struct type *member = check_typedef (type->field (i).type ());
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 }
1597
1598 /* Ensure there is no padding between the fields (allowing for empty
1599 zero length structs) */
1600 int ftype_length = (*fundamental_type == nullptr)
1601 ? 0 : (*fundamental_type)->length ();
1602 if (count * ftype_length != type->length ())
1603 return -1;
1604
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
1633 static bool
1634 aapcs_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
1654 /* AArch64 function call information structure. */
1655 struct aarch64_call_info
1656 {
1657 /* the current argument number. */
1658 unsigned argnum = 0;
1659
1660 /* The next general purpose register number, equivalent to NGRN as
1661 described in the AArch64 Procedure Call Standard. */
1662 unsigned ngrn = 0;
1663
1664 /* The next SIMD and floating point register number, equivalent to
1665 NSRN as described in the AArch64 Procedure Call Standard. */
1666 unsigned nsrn = 0;
1667
1668 /* The next stacked argument address, equivalent to NSAA as
1669 described in the AArch64 Procedure Call Standard. */
1670 unsigned nsaa = 0;
1671
1672 /* Stack item vector. */
1673 std::vector<stack_item_t> si;
1674 };
1675
1676 /* Pass a value in a sequence of consecutive X registers. The caller
1677 is responsible for ensuring sufficient registers are available. */
1678
1679 static void
1680 pass_in_x (struct gdbarch *gdbarch, struct regcache *regcache,
1681 struct aarch64_call_info *info, struct type *type,
1682 struct value *arg)
1683 {
1684 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1685 int len = type->length ();
1686 enum type_code typecode = type->code ();
1687 int regnum = AARCH64_X0_REGNUM + info->ngrn;
1688 const bfd_byte *buf = arg->contents ().data ();
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
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
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
1721 static int
1722 pass_in_v (struct gdbarch *gdbarch,
1723 struct regcache *regcache,
1724 struct aarch64_call_info *info,
1725 int len, const bfd_byte *buf)
1726 {
1727 if (info->nsrn < 8)
1728 {
1729 int regnum = AARCH64_V0_REGNUM + info->nsrn;
1730 /* Enough space for a full vector register. */
1731 gdb::byte_vector reg (register_size (gdbarch, regnum), 0);
1732 gdb_assert (len <= reg.size ());
1733
1734 info->argnum++;
1735 info->nsrn++;
1736
1737 /* PCS C.1, the argument is allocated to the least significant
1738 bits of V register. */
1739 memcpy (reg.data (), buf, len);
1740 regcache->cooked_write (regnum, reg);
1741
1742 aarch64_debug_printf ("arg %d in %s", info->argnum,
1743 gdbarch_register_name (gdbarch, regnum));
1744
1745 return 1;
1746 }
1747 info->nsrn = 8;
1748 return 0;
1749 }
1750
1751 /* Marshall an argument onto the stack. */
1752
1753 static void
1754 pass_on_stack (struct aarch64_call_info *info, struct type *type,
1755 struct value *arg)
1756 {
1757 const bfd_byte *buf = arg->contents ().data ();
1758 int len = type->length ();
1759 int align;
1760 stack_item_t item;
1761
1762 info->argnum++;
1763
1764 align = type_align (type);
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
1774 aarch64_debug_printf ("arg %d len=%d @ sp + %d\n", info->argnum, len,
1775 info->nsaa);
1776
1777 item.len = len;
1778 item.data = buf;
1779 info->si.push_back (item);
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;
1788 item.data = NULL;
1789
1790 info->si.push_back (item);
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
1799 static void
1800 pass_in_x_or_stack (struct gdbarch *gdbarch, struct regcache *regcache,
1801 struct aarch64_call_info *info, struct type *type,
1802 struct value *arg)
1803 {
1804 int len = type->length ();
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 {
1810 pass_in_x (gdbarch, regcache, info, type, arg);
1811 info->ngrn += nregs;
1812 }
1813 else
1814 {
1815 info->ngrn = 8;
1816 pass_on_stack (info, type, arg);
1817 }
1818 }
1819
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. */
1824 static bool
1825 pass_in_v_vfp_candidate (struct gdbarch *gdbarch, struct regcache *regcache,
1826 struct aarch64_call_info *info, struct type *arg_type,
1827 struct value *arg)
1828 {
1829 switch (arg_type->code ())
1830 {
1831 case TYPE_CODE_FLT:
1832 case TYPE_CODE_DECFLOAT:
1833 return pass_in_v (gdbarch, regcache, info, arg_type->length (),
1834 arg->contents ().data ());
1835 break;
1836
1837 case TYPE_CODE_COMPLEX:
1838 {
1839 const bfd_byte *buf = arg->contents ().data ();
1840 struct type *target_type = check_typedef (arg_type->target_type ());
1841
1842 if (!pass_in_v (gdbarch, regcache, info, target_type->length (),
1843 buf))
1844 return false;
1845
1846 return pass_in_v (gdbarch, regcache, info, target_type->length (),
1847 buf + target_type->length ());
1848 }
1849
1850 case TYPE_CODE_ARRAY:
1851 if (arg_type->is_vector ())
1852 return pass_in_v (gdbarch, regcache, info, arg_type->length (),
1853 arg->contents ().data ());
1854 [[fallthrough]];
1855
1856 case TYPE_CODE_STRUCT:
1857 case TYPE_CODE_UNION:
1858 for (int i = 0; i < arg_type->num_fields (); i++)
1859 {
1860 /* Don't include static fields. */
1861 if (arg_type->field (i).is_static ())
1862 continue;
1863
1864 struct value *field = arg->primitive_field (0, i, arg_type);
1865 struct type *field_type = check_typedef (field->type ());
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 }
1876 }
1877
1878 /* Implement the "push_dummy_call" gdbarch method. */
1879
1880 static CORE_ADDR
1881 aarch64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1882 struct regcache *regcache, CORE_ADDR bp_addr,
1883 int nargs,
1884 struct value **args, CORE_ADDR sp,
1885 function_call_return_method return_method,
1886 CORE_ADDR struct_addr)
1887 {
1888 int argnum;
1889 struct aarch64_call_info info;
1890
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
1909 register. */
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
1915 /* If we were given an initial argument for the return slot, lose it. */
1916 if (return_method == return_method_hidden_param)
1917 {
1918 args++;
1919 nargs--;
1920 }
1921
1922 /* The struct_return pointer occupies X8. */
1923 if (return_method != return_method_normal)
1924 {
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
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];
1937 struct type *arg_type, *fundamental_type;
1938 int len, elements;
1939
1940 arg_type = check_typedef (arg->type ());
1941 len = arg_type->length ();
1942
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
1964 switch (arg_type->code ())
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:
1971 if (len < 4 && !is_fixed_point_type (arg_type))
1972 {
1973 /* Promote to 32 bit integer. */
1974 if (arg_type->is_unsigned ())
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 }
1980 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
1981 break;
1982
1983 case TYPE_CODE_STRUCT:
1984 case TYPE_CODE_ARRAY:
1985 case TYPE_CODE_UNION:
1986 if (len > 16)
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. */
1995 write_memory (sp, arg->contents ().data (), len);
1996
1997 /* Construct the indirection. */
1998 arg_type = lookup_pointer_type (arg_type);
1999 arg = value_from_pointer (arg_type, sp);
2000 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
2001 }
2002 else
2003 /* PCS C.15 / C.18 multiple values pass. */
2004 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
2005 break;
2006
2007 default:
2008 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
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
2017 while (!info.si.empty ())
2018 {
2019 const stack_item_t &si = info.si.back ();
2020
2021 sp -= si.len;
2022 if (si.data != NULL)
2023 write_memory (sp, si.data, si.len);
2024 info.si.pop_back ();
2025 }
2026
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
2035 static CORE_ADDR
2036 aarch64_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
2044 static struct type *
2045 aarch64_vnq_type (struct gdbarch *gdbarch)
2046 {
2047 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
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
2071 static struct type *
2072 aarch64_vnd_type (struct gdbarch *gdbarch)
2073 {
2074 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
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
2101 static struct type *
2102 aarch64_vns_type (struct gdbarch *gdbarch)
2103 {
2104 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
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
2131 static struct type *
2132 aarch64_vnh_type (struct gdbarch *gdbarch)
2133 {
2134 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
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
2144 elem = builtin_type (gdbarch)->builtin_bfloat16;
2145 append_composite_type_field (t, "bf", elem);
2146
2147 elem = builtin_type (gdbarch)->builtin_half;
2148 append_composite_type_field (t, "f", elem);
2149
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
2164 static struct type *
2165 aarch64_vnb_type (struct gdbarch *gdbarch)
2166 {
2167 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
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
2189 /* Return TRUE if REGNUM is a ZA tile slice pseudo-register number. Return
2190 FALSE otherwise. */
2191
2192 static bool
2193 is_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
2213 static void
2214 aarch64_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
2273 static struct type *
2274 aarch64_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
2328 static struct type *
2329 aarch64_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
2397 /* Return the type for an AdvSISD V register. */
2398
2399 static struct type *
2400 aarch64_vnv_type (struct gdbarch *gdbarch)
2401 {
2402 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2403
2404 if (tdep->vnv_type == NULL)
2405 {
2406 /* The other AArch64 pseudo registers (Q,D,H,S,B) refer to a single value
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
2411 struct type *t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnv",
2412 TYPE_CODE_UNION);
2413
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);
2436 append_composite_type_field (sub, "bf",
2437 init_vector_type (bt->builtin_bfloat16, 8));
2438 append_composite_type_field (sub, "f",
2439 init_vector_type (bt->builtin_half, 8));
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);
2461
2462 tdep->vnv_type = t;
2463 }
2464
2465 return tdep->vnv_type;
2466 }
2467
2468 /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
2469
2470 static int
2471 aarch64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
2472 {
2473 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2474
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
2481 if (reg == AARCH64_DWARF_PC)
2482 return AARCH64_PC_REGNUM;
2483
2484 if (reg >= AARCH64_DWARF_V0 && reg <= AARCH64_DWARF_V0 + 31)
2485 return AARCH64_V0_REGNUM + reg - AARCH64_DWARF_V0;
2486
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
2499 if (tdep->has_pauth ())
2500 {
2501 if (reg == AARCH64_DWARF_RA_SIGN_STATE)
2502 return tdep->ra_sign_state_regnum;
2503 }
2504
2505 return -1;
2506 }
2507
2508 /* Implement the "print_insn" gdbarch method. */
2509
2510 static int
2511 aarch64_gdb_print_insn (bfd_vma memaddr, disassemble_info *info)
2512 {
2513 info->symbols = NULL;
2514 return default_print_insn (memaddr, info);
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. */
2520 constexpr gdb_byte aarch64_default_breakpoint[] = {0x00, 0x00, 0x20, 0xd4};
2521
2522 typedef BP_MANIPULATION (aarch64_default_breakpoint) aarch64_breakpoint;
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
2528 static void
2529 aarch64_extract_return_value (struct type *type, struct regcache *regs,
2530 gdb_byte *valbuf)
2531 {
2532 struct gdbarch *gdbarch = regs->arch ();
2533 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2534 int elements;
2535 struct type *fundamental_type;
2536
2537 if (aapcs_is_vfp_call_or_return_candidate (type, &elements,
2538 &fundamental_type))
2539 {
2540 int len = fundamental_type->length ();
2541
2542 for (int i = 0; i < elements; i++)
2543 {
2544 int regno = AARCH64_V0_REGNUM + i;
2545 /* Enough space for a full vector register. */
2546 gdb::byte_vector buf (register_size (gdbarch, regno));
2547 gdb_assert (len <= buf.size ());
2548
2549 aarch64_debug_printf
2550 ("read HFA or HVA return value element %d from %s",
2551 i + 1, gdbarch_register_name (gdbarch, regno));
2552
2553 regs->cooked_read (regno, buf);
2554
2555 memcpy (valbuf, buf.data (), len);
2556 valbuf += len;
2557 }
2558 }
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
2563 || TYPE_IS_REFERENCE (type)
2564 || type->code () == TYPE_CODE_ENUM)
2565 {
2566 /* If the type is a plain integer, then the access is
2567 straight-forward. Otherwise we have to play around a bit
2568 more. */
2569 int len = type->length ();
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 }
2585 else
2586 {
2587 /* For a structure or union the behavior is as if the value had
2588 been stored to word-aligned memory and then loaded into
2589 registers with 64-bit load instruction(s). */
2590 int len = type->length ();
2591 int regno = AARCH64_X0_REGNUM;
2592 bfd_byte buf[X_REGISTER_SIZE];
2593
2594 while (len > 0)
2595 {
2596 regs->cooked_read (regno++, buf);
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
2609 static int
2610 aarch64_return_in_memory (struct gdbarch *gdbarch, struct type *type)
2611 {
2612 type = check_typedef (type);
2613 int elements;
2614 struct type *fundamental_type;
2615
2616 if (TYPE_HAS_DYNAMIC_LENGTH (type))
2617 return 1;
2618
2619 if (aapcs_is_vfp_call_or_return_candidate (type, &elements,
2620 &fundamental_type))
2621 {
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. */
2624 return 0;
2625 }
2626
2627 if (type->length () > 16
2628 || !language_pass_by_reference (type).trivially_copyable)
2629 {
2630 /* PCS B.6 Aggregates larger than 16 bytes are passed by
2631 invisible reference. */
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
2642 static void
2643 aarch64_store_return_value (struct type *type, struct regcache *regs,
2644 const gdb_byte *valbuf)
2645 {
2646 struct gdbarch *gdbarch = regs->arch ();
2647 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2648 int elements;
2649 struct type *fundamental_type;
2650
2651 if (aapcs_is_vfp_call_or_return_candidate (type, &elements,
2652 &fundamental_type))
2653 {
2654 int len = fundamental_type->length ();
2655
2656 for (int i = 0; i < elements; i++)
2657 {
2658 int regno = AARCH64_V0_REGNUM + i;
2659 /* Enough space for a full vector register. */
2660 gdb::byte_vector tmpbuf (register_size (gdbarch, regno));
2661 gdb_assert (len <= tmpbuf.size ());
2662
2663 aarch64_debug_printf
2664 ("write HFA or HVA return value element %d to %s",
2665 i + 1, gdbarch_register_name (gdbarch, regno));
2666
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);
2672 memcpy (tmpbuf.data (), valbuf,
2673 len > V_REGISTER_SIZE ? V_REGISTER_SIZE : len);
2674 regs->cooked_write (regno, tmpbuf);
2675 valbuf += len;
2676 }
2677 }
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
2682 || TYPE_IS_REFERENCE (type)
2683 || type->code () == TYPE_CODE_ENUM)
2684 {
2685 if (type->length () <= X_REGISTER_SIZE)
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);
2693 regs->cooked_write (AARCH64_X0_REGNUM, tmpbuf);
2694 }
2695 else
2696 {
2697 /* Integral values greater than one word are stored in
2698 consecutive registers starting with r0. This will always
2699 be a multiple of the register size. */
2700 int len = type->length ();
2701 int regno = AARCH64_X0_REGNUM;
2702
2703 while (len > 0)
2704 {
2705 regs->cooked_write (regno++, valbuf);
2706 len -= X_REGISTER_SIZE;
2707 valbuf += X_REGISTER_SIZE;
2708 }
2709 }
2710 }
2711 else
2712 {
2713 /* For a structure or union the behavior is as if the value had
2714 been stored to word-aligned memory and then loaded into
2715 registers with 64-bit load instruction(s). */
2716 int len = type->length ();
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);
2724 regs->cooked_write (regno++, tmpbuf);
2725 len -= X_REGISTER_SIZE;
2726 valbuf += X_REGISTER_SIZE;
2727 }
2728 }
2729 }
2730
2731 /* Implement the "return_value" gdbarch method. */
2732
2733 static enum return_value_convention
2734 aarch64_return_value (struct gdbarch *gdbarch, struct value *func_value,
2735 struct type *valtype, struct regcache *regcache,
2736 struct value **read_value, const gdb_byte *writebuf)
2737 {
2738 if (valtype->code () == TYPE_CODE_STRUCT
2739 || valtype->code () == TYPE_CODE_UNION
2740 || valtype->code () == TYPE_CODE_ARRAY)
2741 {
2742 if (aarch64_return_in_memory (gdbarch, valtype))
2743 {
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
2751 aarch64_debug_printf ("return value in memory");
2752
2753 if (read_value != nullptr)
2754 {
2755 CORE_ADDR addr;
2756
2757 regcache->cooked_read (AARCH64_STRUCT_RETURN_REGNUM, &addr);
2758 *read_value = value_at_non_lval (valtype, addr);
2759 }
2760
2761 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
2762 }
2763 }
2764
2765 if (writebuf)
2766 aarch64_store_return_value (valtype, regcache, writebuf);
2767
2768 if (read_value)
2769 {
2770 *read_value = value::allocate (valtype);
2771 aarch64_extract_return_value (valtype, regcache,
2772 (*read_value)->contents_raw ().data ());
2773 }
2774
2775 aarch64_debug_printf ("return value in registers");
2776
2777 return RETURN_VALUE_REGISTER_CONVENTION;
2778 }
2779
2780 /* Implement the "get_longjmp_target" gdbarch method. */
2781
2782 static int
2783 aarch64_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
2784 {
2785 CORE_ADDR jb_addr;
2786 gdb_byte buf[X_REGISTER_SIZE];
2787 struct gdbarch *gdbarch = get_frame_arch (frame);
2788 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
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 }
2800
2801 /* Implement the "gen_return_address" gdbarch method. */
2802
2803 static void
2804 aarch64_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 }
2812 \f
2813
2814 /* Return TRUE if REGNUM is a W pseudo-register number. Return FALSE
2815 otherwise. */
2816
2817 static bool
2818 is_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
2829 /* Return TRUE if REGNUM is a SME pseudo-register number. Return FALSE
2830 otherwise. */
2831
2832 static bool
2833 is_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
2846 static const std::string
2847 aarch64_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
2866 static const std::string
2867 aarch64_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
2882 static struct type *
2883 aarch64_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
2896 /* Return the pseudo register name corresponding to register regnum. */
2897
2898 static const char *
2899 aarch64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
2900 {
2901 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
2902
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
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
2976 int p_regnum = regnum - gdbarch_num_regs (gdbarch);
2977
2978 if (p_regnum >= AARCH64_Q0_REGNUM && p_regnum < AARCH64_Q0_REGNUM + 32)
2979 return q_name[p_regnum - AARCH64_Q0_REGNUM];
2980
2981 if (p_regnum >= AARCH64_D0_REGNUM && p_regnum < AARCH64_D0_REGNUM + 32)
2982 return d_name[p_regnum - AARCH64_D0_REGNUM];
2983
2984 if (p_regnum >= AARCH64_S0_REGNUM && p_regnum < AARCH64_S0_REGNUM + 32)
2985 return s_name[p_regnum - AARCH64_S0_REGNUM];
2986
2987 if (p_regnum >= AARCH64_H0_REGNUM && p_regnum < AARCH64_H0_REGNUM + 32)
2988 return h_name[p_regnum - AARCH64_H0_REGNUM];
2989
2990 if (p_regnum >= AARCH64_B0_REGNUM && p_regnum < AARCH64_B0_REGNUM + 32)
2991 return b_name[p_regnum - AARCH64_B0_REGNUM];
2992
2993 /* W pseudo-registers? */
2994 if (is_w_pseudo_register (gdbarch, regnum))
2995 return w_name[regnum - tdep->w_pseudo_base];
2996
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
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];
3014 }
3015
3016 if (is_sme_pseudo_register (gdbarch, regnum))
3017 return tdep->sme_pseudo_names[regnum - tdep->sme_pseudo_base].c_str ();
3018
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. */
3022 if (tdep->has_pauth () && regnum == tdep->ra_sign_state_regnum)
3023 return "";
3024
3025 internal_error (_("aarch64_pseudo_register_name: bad register number %d"),
3026 p_regnum);
3027 }
3028
3029 /* Implement the "pseudo_register_type" tdesc_arch_data method. */
3030
3031 static struct type *
3032 aarch64_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
3033 {
3034 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
3035
3036 int p_regnum = regnum - gdbarch_num_regs (gdbarch);
3037
3038 if (p_regnum >= AARCH64_Q0_REGNUM && p_regnum < AARCH64_Q0_REGNUM + 32)
3039 return aarch64_vnq_type (gdbarch);
3040
3041 if (p_regnum >= AARCH64_D0_REGNUM && p_regnum < AARCH64_D0_REGNUM + 32)
3042 return aarch64_vnd_type (gdbarch);
3043
3044 if (p_regnum >= AARCH64_S0_REGNUM && p_regnum < AARCH64_S0_REGNUM + 32)
3045 return aarch64_vns_type (gdbarch);
3046
3047 if (p_regnum >= AARCH64_H0_REGNUM && p_regnum < AARCH64_H0_REGNUM + 32)
3048 return aarch64_vnh_type (gdbarch);
3049
3050 if (p_regnum >= AARCH64_B0_REGNUM && p_regnum < AARCH64_B0_REGNUM + 32)
3051 return aarch64_vnb_type (gdbarch);
3052
3053 if (tdep->has_sve () && p_regnum >= AARCH64_SVE_V0_REGNUM
3054 && p_regnum < AARCH64_SVE_V0_REGNUM + AARCH64_V_REGS_NUM)
3055 return aarch64_vnv_type (gdbarch);
3056
3057 /* W pseudo-registers are 32-bit. */
3058 if (is_w_pseudo_register (gdbarch, regnum))
3059 return builtin_type (gdbarch)->builtin_uint32;
3060
3061 if (is_sme_pseudo_register (gdbarch, regnum))
3062 return aarch64_sme_pseudo_register_type (gdbarch, regnum);
3063
3064 if (tdep->has_pauth () && regnum == tdep->ra_sign_state_regnum)
3065 return builtin_type (gdbarch)->builtin_uint64;
3066
3067 internal_error (_("aarch64_pseudo_register_type: bad register number %d"),
3068 p_regnum);
3069 }
3070
3071 /* Implement the "pseudo_register_reggroup_p" tdesc_arch_data method. */
3072
3073 static int
3074 aarch64_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
3075 const struct reggroup *group)
3076 {
3077 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
3078
3079 int p_regnum = regnum - gdbarch_num_regs (gdbarch);
3080
3081 if (p_regnum >= AARCH64_Q0_REGNUM && p_regnum < AARCH64_Q0_REGNUM + 32)
3082 return group == all_reggroup || group == vector_reggroup;
3083 else if (p_regnum >= AARCH64_D0_REGNUM && p_regnum < AARCH64_D0_REGNUM + 32)
3084 return (group == all_reggroup || group == vector_reggroup
3085 || group == float_reggroup);
3086 else if (p_regnum >= AARCH64_S0_REGNUM && p_regnum < AARCH64_S0_REGNUM + 32)
3087 return (group == all_reggroup || group == vector_reggroup
3088 || group == float_reggroup);
3089 else if (p_regnum >= AARCH64_H0_REGNUM && p_regnum < AARCH64_H0_REGNUM + 32)
3090 return group == all_reggroup || group == vector_reggroup;
3091 else if (p_regnum >= AARCH64_B0_REGNUM && p_regnum < AARCH64_B0_REGNUM + 32)
3092 return group == all_reggroup || group == vector_reggroup;
3093 else if (tdep->has_sve () && p_regnum >= AARCH64_SVE_V0_REGNUM
3094 && p_regnum < AARCH64_SVE_V0_REGNUM + AARCH64_V_REGS_NUM)
3095 return group == all_reggroup || group == vector_reggroup;
3096 else if (is_sme_pseudo_register (gdbarch, regnum))
3097 return group == all_reggroup || group == vector_reggroup;
3098 /* RA_STATE is used for unwinding only. Do not assign it to any groups. */
3099 if (tdep->has_pauth () && regnum == tdep->ra_sign_state_regnum)
3100 return 0;
3101
3102 return group == all_reggroup;
3103 }
3104
3105 /* Helper for aarch64_pseudo_read_value. */
3106
3107 static value *
3108 aarch64_pseudo_read_value_1 (const frame_info_ptr &next_frame,
3109 const int pseudo_reg_num, int raw_regnum_offset)
3110 {
3111 unsigned v_regnum = AARCH64_V0_REGNUM + raw_regnum_offset;
3112
3113 return pseudo_from_raw_part (next_frame, pseudo_reg_num, v_regnum, 0);
3114 }
3115
3116 /* Helper function for reading/writing ZA pseudo-registers. Given REGNUM,
3117 a ZA pseudo-register number, return the information on positioning of the
3118 bytes that must be read from/written to. */
3119
3120 static za_offsets
3121 aarch64_za_offsets_from_regnum (struct gdbarch *gdbarch, int regnum)
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);
3137 za_offsets offsets;
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 }
3193
3194 return offsets;
3195 }
3196
3197 /* Given REGNUM, a SME pseudo-register number, return its value in RESULT. */
3198
3199 static value *
3200 aarch64_sme_pseudo_register_read (gdbarch *gdbarch, const frame_info_ptr &next_frame,
3201 const int pseudo_reg_num)
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);
3207 gdb_assert (tdep->sme_pseudo_base <= pseudo_reg_num);
3208 gdb_assert (pseudo_reg_num < tdep->sme_pseudo_base + tdep->sme_pseudo_count);
3209
3210 /* Fetch the offsets that we need in order to read from the correct blocks
3211 of ZA. */
3212 za_offsets offsets
3213 = aarch64_za_offsets_from_regnum (gdbarch, pseudo_reg_num);
3214
3215 /* Fetch the contents of ZA. */
3216 value *za_value = value_of_register (tdep->sme_za_regnum, next_frame);
3217 value *result = value::allocate_register (next_frame, pseudo_reg_num);
3218
3219 /* Copy the requested data. */
3220 for (int chunks = 0; chunks < offsets.chunks; chunks++)
3221 {
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);
3226 }
3227
3228 return result;
3229 }
3230
3231 /* Implement the "pseudo_register_read_value" gdbarch method. */
3232
3233 static value *
3234 aarch64_pseudo_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
3235 const int pseudo_reg_num)
3236 {
3237 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
3238
3239 if (is_w_pseudo_register (gdbarch, pseudo_reg_num))
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. */
3249 int x_regnum
3250 = AARCH64_X0_REGNUM + (pseudo_reg_num - tdep->w_pseudo_base);
3251
3252 /* Read the bottom 4 bytes of X. */
3253 return pseudo_from_raw_part (next_frame, pseudo_reg_num, x_regnum,
3254 offset);
3255 }
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);
3292
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
3296 gdb_assert_not_reached ("regnum out of bound");
3297 }
3298
3299 /* Helper for aarch64_pseudo_write. */
3300
3301 static void
3302 aarch64_pseudo_write_1 (gdbarch *gdbarch, const frame_info_ptr &next_frame,
3303 int regnum_offset,
3304 gdb::array_view<const gdb_byte> buf)
3305 {
3306 unsigned raw_regnum = AARCH64_V0_REGNUM + regnum_offset;
3307
3308 /* Enough space for a full vector register.
3309
3310 Ensure the register buffer is zero, we want gdb writes of the
3311 various 'scalar' pseudo registers to behavior like architectural
3312 writes, register width bytes are written the remainder are set to
3313 zero. */
3314 gdb::byte_vector raw_buf (register_size (gdbarch, raw_regnum), 0);
3315 static_assert (AARCH64_V0_REGNUM == AARCH64_SVE_Z0_REGNUM);
3316
3317 gdb::array_view<gdb_byte> raw_view (raw_buf);
3318 copy (buf, raw_view.slice (0, buf.size ()));
3319 put_frame_register (next_frame, raw_regnum, raw_view);
3320 }
3321
3322 /* Given REGNUM, a SME pseudo-register number, store the bytes from DATA to the
3323 pseudo-register. */
3324
3325 static void
3326 aarch64_sme_pseudo_register_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
3327 const int regnum,
3328 gdb::array_view<const gdb_byte> data)
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. */
3339 za_offsets offsets = aarch64_za_offsets_from_regnum (gdbarch, regnum);
3340
3341 /* Fetch the contents of ZA. */
3342 value *za_value = value_of_register (tdep->sme_za_regnum, next_frame);
3343
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);
3348
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 }
3359
3360 /* Write back to ZA. */
3361 put_frame_register (next_frame, tdep->sme_za_regnum,
3362 za_value->contents_raw ());
3363 }
3364
3365 /* Implement the "pseudo_register_write" gdbarch method. */
3366
3367 static void
3368 aarch64_pseudo_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
3369 const int pseudo_reg_num,
3370 gdb::array_view<const gdb_byte> buf)
3371 {
3372 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
3373
3374 if (is_w_pseudo_register (gdbarch, pseudo_reg_num))
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. */
3384 int x_regnum = AARCH64_X0_REGNUM + (pseudo_reg_num - tdep->w_pseudo_base);
3385
3386 /* First zero-out the contents of X. */
3387 gdb_byte bytes[8] {};
3388 gdb::array_view<gdb_byte> bytes_view (bytes);
3389 copy (buf, bytes_view.slice (offset, 4));
3390
3391 /* Write to the bottom 4 bytes of X. */
3392 put_frame_register (next_frame, x_regnum, bytes_view);
3393 return;
3394 }
3395 else if (is_sme_pseudo_register (gdbarch, pseudo_reg_num))
3396 {
3397 aarch64_sme_pseudo_register_write (gdbarch, next_frame, pseudo_reg_num,
3398 buf);
3399 return;
3400 }
3401
3402 /* Offset in the "pseudo-register space". */
3403 int pseudo_offset = pseudo_reg_num - gdbarch_num_regs (gdbarch);
3404
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);
3409
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);
3414
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);
3419
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);
3424
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);
3429
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);
3434
3435 gdb_assert_not_reached ("regnum out of bound");
3436 }
3437
3438 /* Callback function for user_reg_add. */
3439
3440 static struct value *
3441 value_of_aarch64_user_reg (const frame_info_ptr &frame, const void *baton)
3442 {
3443 const int *reg_p = (const int *) baton;
3444
3445 return value_of_register (*reg_p, get_next_frame_sentinel_okay (frame));
3446 }
3447
3448 /* Implement the "software_single_step" gdbarch method, needed to
3449 single step through atomic sequences on AArch64. */
3450
3451 static std::vector<CORE_ADDR>
3452 aarch64_software_single_step (struct regcache *regcache)
3453 {
3454 struct gdbarch *gdbarch = regcache->arch ();
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. */
3458 CORE_ADDR pc = regcache_read_pc (regcache);
3459 CORE_ADDR breaks[2] = { CORE_ADDR_MAX, CORE_ADDR_MAX };
3460 CORE_ADDR loc = pc;
3461 CORE_ADDR closing_insn = 0;
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;
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). */
3478 aarch64_inst inst;
3479
3480 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
3481 return {};
3482
3483 /* Look for a Load Exclusive instruction which begins the sequence. */
3484 if (inst.opcode->iclass != ldstexcl || bit (insn, 22) == 0)
3485 return {};
3486
3487 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
3488 {
3489 loc += insn_size;
3490
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;
3501 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
3502 return {};
3503 /* Check if the instruction is a conditional branch. */
3504 if (inst.opcode->iclass == condbranch)
3505 {
3506 gdb_assert (inst.operands[0].type == AARCH64_OPND_ADDR_PCREL19);
3507
3508 if (bc_insn_count >= 1)
3509 return {};
3510
3511 /* It is, so we'll try to set a breakpoint at the destination. */
3512 breaks[1] = loc + inst.operands[0].imm.value;
3513
3514 bc_insn_count++;
3515 last_breakpoint++;
3516 }
3517
3518 /* Look for the Store Exclusive which closes the atomic sequence. */
3519 if (inst.opcode->iclass == ldstexcl && bit (insn, 22) == 0)
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)
3528 return {};
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
3540 std::vector<CORE_ADDR> next_pcs;
3541
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++)
3545 next_pcs.push_back (breaks[index]);
3546
3547 return next_pcs;
3548 }
3549
3550 struct aarch64_displaced_step_copy_insn_closure
3551 : public displaced_step_copy_insn_closure
3552 {
3553 /* It is true when condition instruction, such as B.CON, TBZ, etc,
3554 is being displaced stepping. */
3555 bool cond = false;
3556
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. */
3559 int32_t pc_adjust = 0;
3560 };
3561
3562 /* Data when visiting instructions for displaced stepping. */
3563
3564 struct 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. */
3571 uint32_t insn_buf[AARCH64_DISPLACED_MODIFIED_INSNS];
3572 /* Number of instructions in INSN_BUF. */
3573 unsigned insn_count;
3574 /* Registers when doing displaced stepping. */
3575 struct regcache *regs;
3576
3577 aarch64_displaced_step_copy_insn_closure *dsc;
3578 };
3579
3580 /* Implementation of aarch64_insn_visitor method "b". */
3581
3582 static void
3583 aarch64_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;
3588 int64_t new_offset = data->insn_addr - dsd->new_addr + offset;
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
3616 static void
3617 aarch64_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;
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);
3636 dsd->dsc->cond = true;
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
3645 static struct aarch64_register
3646 aarch64_register (unsigned num, int is64)
3647 {
3648 return (struct aarch64_register) { num, is64 };
3649 }
3650
3651 /* Implementation of aarch64_insn_visitor method "cb". */
3652
3653 static void
3654 aarch64_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;
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;
3671 dsd->dsc->cond = true;
3672 dsd->dsc->pc_adjust = offset;
3673 }
3674
3675 /* Implementation of aarch64_insn_visitor method "tb". */
3676
3677 static void
3678 aarch64_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;
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;
3696 dsd->dsc->cond = true;
3697 dsd->dsc->pc_adjust = offset;
3698 }
3699
3700 /* Implementation of aarch64_insn_visitor method "adr". */
3701
3702 static void
3703 aarch64_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
3729 static void
3730 aarch64_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
3754 static void
3755 aarch64_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
3761 uint32_t masked_insn = (insn & CLEAR_Rn_MASK);
3762 if (masked_insn == BLR)
3763 {
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);
3769 }
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;
3776 else
3777 dsd->dsc->pc_adjust = 4;
3778 }
3779
3780 static 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
3793 displaced_step_copy_insn_closure_up
3794 aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
3795 CORE_ADDR from, CORE_ADDR to,
3796 struct regcache *regs)
3797 {
3798 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3799 struct aarch64_displaced_step_data dsd;
3800 aarch64_inst inst;
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;
3808
3809 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
3810 return NULL;
3811
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))
3816 {
3817 /* We can't displaced step atomic sequences nor MOPS instructions. */
3818 return NULL;
3819 }
3820
3821 std::unique_ptr<aarch64_displaced_step_copy_insn_closure> dsc
3822 (new aarch64_displaced_step_copy_insn_closure);
3823 dsd.base.insn_addr = from;
3824 dsd.new_addr = to;
3825 dsd.regs = regs;
3826 dsd.dsc = dsc.get ();
3827 dsd.insn_count = 0;
3828 aarch64_relocate_instruction (insn, &visitor,
3829 (struct aarch64_insn_data *) &dsd);
3830 gdb_assert (dsd.insn_count <= AARCH64_DISPLACED_MODIFIED_INSNS);
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 {
3840 displaced_debug_printf ("writing insn %.8x at %s",
3841 dsd.insn_buf[i],
3842 paddress (gdbarch, to + i * 4));
3843
3844 write_memory_unsigned_integer (to + i * 4, 4, byte_order_for_code,
3845 (ULONGEST) dsd.insn_buf[i]);
3846 }
3847 }
3848 else
3849 {
3850 dsc = NULL;
3851 }
3852
3853 /* This is a work around for a problem with g++ 4.8. */
3854 return displaced_step_copy_insn_closure_up (dsc.release ());
3855 }
3856
3857 /* Implement the "displaced_step_fixup" gdbarch method. */
3858
3859 void
3860 aarch64_displaced_step_fixup (struct gdbarch *gdbarch,
3861 struct displaced_step_copy_insn_closure *dsc_,
3862 CORE_ADDR from, CORE_ADDR to,
3863 struct regcache *regs, bool completed_p)
3864 {
3865 CORE_ADDR pc = regcache_read_pc (regs);
3866
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 }
3875
3876 aarch64_displaced_step_copy_insn_closure *dsc
3877 = (aarch64_displaced_step_copy_insn_closure *) dsc_;
3878
3879 displaced_debug_printf ("PC after stepping: %s (was %s).",
3880 paddress (gdbarch, pc), paddress (gdbarch, to));
3881
3882 if (dsc->cond)
3883 {
3884 displaced_debug_printf ("[Conditional] pc_adjust before: %d",
3885 dsc->pc_adjust);
3886
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");
3898
3899 displaced_debug_printf ("[Conditional] pc_adjust after: %d",
3900 dsc->pc_adjust);
3901 }
3902
3903 displaced_debug_printf ("%s PC by %d",
3904 dsc->pc_adjust ? "adjusting" : "not adjusting",
3905 dsc->pc_adjust);
3906
3907 if (dsc->pc_adjust != 0)
3908 {
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)
3914 {
3915 displaced_debug_printf ("PC did not move. Discarding PC adjustment.");
3916 dsc->pc_adjust = 0;
3917 }
3918
3919 displaced_debug_printf ("fixup: set PC to %s:%d",
3920 paddress (gdbarch, from), dsc->pc_adjust);
3921
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
3929 bool
3930 aarch64_displaced_step_hw_singlestep (struct gdbarch *gdbarch)
3931 {
3932 return true;
3933 }
3934
3935 /* Get the correct target description for the given VQ value.
3936 If VQ is zero then it is assumed SVE is not supported.
3937 (It is not possible to set VQ to zero on an SVE system).
3938
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. */
3942
3943 const target_desc *
3944 aarch64_read_description (const aarch64_features &features)
3945 {
3946 if (features.vq > AARCH64_MAX_SVE_VQ)
3947 error (_("VQ is %" PRIu64 ", maximum supported value is %d"), features.vq,
3948 AARCH64_MAX_SVE_VQ);
3949
3950 struct target_desc *tdesc = tdesc_aarch64_map[features];
3951
3952 if (tdesc == NULL)
3953 {
3954 tdesc = aarch64_create_target_description (features);
3955 tdesc_aarch64_map[features] = tdesc;
3956 }
3957
3958 return tdesc;
3959 }
3960
3961 /* Return the VQ used when creating the target description TDESC. */
3962
3963 static uint64_t
3964 aarch64_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
3976 uint64_t vl = tdesc_register_bitsize (feature_sve,
3977 aarch64_sve_register_names[0]) / 8;
3978 return sve_vq_from_vl (vl);
3979 }
3980
3981
3982 /* Return the svq (streaming vector quotient) used when creating the target
3983 description TDESC. */
3984
3985 static uint64_t
3986 aarch64_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
4008 /* Get the AArch64 features present in the given target description. */
4009
4010 aarch64_features
4011 aarch64_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);
4019
4020 /* We need to look for a couple pauth feature name variations. */
4021 features.pauth
4022 = (tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.pauth") != nullptr);
4023
4024 if (!features.pauth)
4025 features.pauth = (tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.pauth_v2")
4026 != nullptr);
4027
4028 features.mte
4029 = (tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.mte") != nullptr);
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 }
4042
4043 features.svq = aarch64_get_tdesc_svq (tdesc);
4044
4045 /* Check for the SME2 feature. */
4046 features.sme2 = (tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sme2")
4047 != nullptr);
4048
4049 return features;
4050 }
4051
4052 /* Implement the "cannot_store_register" gdbarch method. */
4053
4054 static int
4055 aarch64_cannot_store_register (struct gdbarch *gdbarch, int regnum)
4056 {
4057 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
4058
4059 if (!tdep->has_pauth ())
4060 return 0;
4061
4062 /* Pointer authentication registers are read-only. */
4063 return (regnum >= tdep->pauth_reg_base
4064 && regnum < tdep->pauth_reg_base + tdep->pauth_reg_count);
4065 }
4066
4067 /* Implement the stack_frame_destroyed_p gdbarch method. */
4068
4069 static int
4070 aarch64_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);
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;
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
4092 /* Helper to get the allocation tag from a 64-bit ADDRESS.
4093
4094 Return the allocation tag if successful and nullopt otherwise. */
4095
4096 std::optional<CORE_ADDR>
4097 aarch64_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
4117 static bool
4118 aarch64_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
4127 = aarch64_mte_get_atag (aarch64_remove_non_address_bits (gdbarch, addr));
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
4141 static bool
4142 aarch64_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. */
4166 addr = aarch64_remove_non_address_bits (gdbarch, addr);
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
4200 static struct value *
4201 aarch64_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. */
4215 addr = aarch64_remove_non_address_bits (gdbarch, addr);
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
4231 static std::string
4232 aarch64_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
4239 return string_printf ("0x%s", phex_nz (tag));
4240 }
4241
4242 /* See aarch64-tdep.h. */
4243
4244 CORE_ADDR
4245 aarch64_remove_non_address_bits (struct gdbarch *gdbarch, CORE_ADDR pointer)
4246 {
4247 /* By default, we assume TBI and discard the top 8 bits plus the VA range
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. */
4251 CORE_ADDR mask = AARCH64_TOP_BITS_MASK;
4252
4253 /* Check if we have an inferior first. If not, just use the default
4254 mask.
4255
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. */
4264 thread_info *thread = current_inferior ()->find_thread (inferior_ptid);
4265
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)
4269 {
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 ());
4280
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 }
4312 }
4313
4314 return aarch64_remove_top_bits (pointer, mask);
4315 }
4316
4317 /* Given NAMES, a vector of strings, initialize it with all the SME
4318 pseudo-register names for the current streaming vector length. */
4319
4320 static void
4321 aarch64_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
4346 /* Initialize the current architecture based on INFO. If possible,
4347 reuse an architecture from ARCHES, which is a list of
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
4353 static struct gdbarch *
4354 aarch64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
4355 {
4356 const struct tdesc_feature *feature_core, *feature_fpu, *feature_sve;
4357 const struct tdesc_feature *feature_pauth;
4358 bool valid_p = true;
4359 int i, num_regs = 0, num_pseudo_regs = 0;
4360 int first_pauth_regnum = -1, ra_sign_state_offset = -1;
4361 int first_mte_regnum = -1, first_tls_regnum = -1;
4362 uint64_t vq = aarch64_get_tdesc_vq (info.target_desc);
4363 uint64_t svq = aarch64_get_tdesc_svq (info.target_desc);
4364
4365 if (vq > AARCH64_MAX_SVE_VQ)
4366 internal_error (_("VQ out of bounds: %s (max %d)"),
4367 pulongest (vq), AARCH64_MAX_SVE_VQ);
4368
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
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 {
4379 aarch64_gdbarch_tdep *tdep
4380 = gdbarch_tdep<aarch64_gdbarch_tdep> (best_arch->gdbarch);
4381 if (tdep && tdep->vq == vq && tdep->sme_svq == svq)
4382 return best_arch->gdbarch;
4383 }
4384
4385 /* Ensure we always have a target descriptor, and that it is for the given VQ
4386 value. */
4387 const struct target_desc *tdesc = info.target_desc;
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 }
4396 gdb_assert (tdesc);
4397
4398 feature_core = tdesc_find_feature (tdesc,"org.gnu.gdb.aarch64.core");
4399 feature_fpu = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.fpu");
4400 feature_sve = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sve");
4401 const struct tdesc_feature *feature_mte
4402 = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.mte");
4403 const struct tdesc_feature *feature_tls
4404 = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.tls");
4405
4406 if (feature_core == nullptr)
4407 return nullptr;
4408
4409 tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
4410
4411 /* Validate the description provides the mandatory core R registers
4412 and allocate their numbers. */
4413 for (i = 0; i < ARRAY_SIZE (aarch64_r_register_names); i++)
4414 valid_p &= tdesc_numbered_register (feature_core, tdesc_data.get (),
4415 AARCH64_X0_REGNUM + i,
4416 aarch64_r_register_names[i]);
4417
4418 num_regs = AARCH64_X0_REGNUM + i;
4419
4420 /* Add the V registers. */
4421 if (feature_fpu != nullptr)
4422 {
4423 if (feature_sve != nullptr)
4424 error (_("Program contains both fpu and SVE features."));
4425
4426 /* Validate the description provides the mandatory V registers
4427 and allocate their numbers. */
4428 for (i = 0; i < ARRAY_SIZE (aarch64_v_register_names); i++)
4429 valid_p &= tdesc_numbered_register (feature_fpu, tdesc_data.get (),
4430 AARCH64_V0_REGNUM + i,
4431 aarch64_v_register_names[i]);
4432
4433 num_regs = AARCH64_V0_REGNUM + i;
4434 }
4435
4436 /* Add the SVE registers. */
4437 if (feature_sve != nullptr)
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++)
4442 valid_p &= tdesc_numbered_register (feature_sve, tdesc_data.get (),
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
4450 if (feature_fpu != nullptr || feature_sve != nullptr)
4451 {
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
4459 int first_sme_regnum = -1;
4460 int first_sme2_regnum = -1;
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;
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 }
4500 }
4501
4502 /* Add the TLS register. */
4503 int tls_register_count = 0;
4504 if (feature_tls != nullptr)
4505 {
4506 first_tls_regnum = num_regs;
4507
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 }
4535 }
4536
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
4544 /* Add the pauth registers. */
4545 int pauth_masks = 0;
4546 if (feature_pauth != NULL)
4547 {
4548 first_pauth_regnum = num_regs;
4549 ra_sign_state_offset = num_pseudo_regs;
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
4564 /* Validate the descriptor provides the mandatory PAUTH registers and
4565 allocate their numbers. */
4566 for (i = 0; i < set_size; i++)
4567 valid_p &= tdesc_numbered_register (feature_pauth, tdesc_data.get (),
4568 first_pauth_regnum + i,
4569 aarch64_pauth_register_names[i]);
4570
4571 num_regs += i;
4572 num_pseudo_regs += 1; /* Count RA_STATE pseudo register. */
4573 pauth_masks = set_size;
4574 }
4575
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 }
4589 /* W pseudo-registers */
4590 int first_w_regnum = num_pseudo_regs;
4591 num_pseudo_regs += 31;
4592
4593 if (!valid_p)
4594 return nullptr;
4595
4596 /* AArch64 code is always little-endian. */
4597 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
4598
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);
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;
4607 tdep->vq = vq;
4608 tdep->pauth_reg_base = first_pauth_regnum;
4609 tdep->pauth_reg_count = pauth_masks;
4610 tdep->ra_sign_state_regnum = -1;
4611 tdep->mte_reg_base = first_mte_regnum;
4612 tdep->tls_regnum_base = first_tls_regnum;
4613 tdep->tls_register_count = tls_register_count;
4614
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
4623 /* Set the SME2 register set details. */
4624 tdep->sme2_zt0_regnum = first_sme2_regnum;
4625
4626 set_gdbarch_push_dummy_call (gdbarch, aarch64_push_dummy_call);
4627 set_gdbarch_frame_align (gdbarch, aarch64_frame_align);
4628
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. */
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);
4640 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
4641 set_gdbarch_software_single_step (gdbarch, aarch64_software_single_step);
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);
4650 set_gdbarch_pseudo_register_write (gdbarch, aarch64_pseudo_write);
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);
4655 set_gdbarch_cannot_store_register (gdbarch, aarch64_cannot_store_register);
4656
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
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);
4684 set_gdbarch_wchar_signed (gdbarch, 0);
4685 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
4686 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
4687 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
4688 set_gdbarch_type_align (gdbarch, aarch64_type_align);
4689
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
4693 /* Internal <-> external register number maps. */
4694 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, aarch64_dwarf_reg_to_regnum);
4695
4696 /* Returning results. */
4697 set_gdbarch_return_value_as_value (gdbarch, aarch64_return_value);
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;
4707 info.tdesc_data = tdesc_data.get ();
4708 gdbarch_init_osabi (info, gdbarch);
4709
4710 dwarf2_frame_set_init_reg (gdbarch, aarch64_dwarf2_frame_init_reg);
4711 /* Register DWARF CFA vendor handler. */
4712 set_gdbarch_execute_dwarf_cfa_vendor_op (gdbarch,
4713 aarch64_execute_dwarf_cfa_vendor_op);
4714
4715 /* Permanent/Program breakpoint handling. */
4716 set_gdbarch_program_breakpoint_here_p (gdbarch,
4717 aarch64_program_breakpoint_here_p);
4718
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
4732 set_gdbarch_gen_return_address (gdbarch, aarch64_gen_return_address);
4733
4734 set_gdbarch_get_pc_address_flags (gdbarch, aarch64_get_pc_address_flags);
4735
4736 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
4737
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
4746 /* Setup W pseudo-register numbers. */
4747 tdep->w_pseudo_base = first_w_regnum + num_regs;
4748 tdep->w_pseudo_count = 31;
4749
4750 /* Pointer authentication pseudo-registers. */
4751 if (tdep->has_pauth ())
4752 tdep->ra_sign_state_regnum = ra_sign_state_offset + num_regs;
4753
4754 /* Architecture hook to remove bits of a pointer that are not part of the
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);
4764
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
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
4851 register_aarch64_ravenscar_ops (gdbarch);
4852
4853 return gdbarch;
4854 }
4855
4856 static void
4857 aarch64_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
4858 {
4859 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
4860
4861 if (tdep == NULL)
4862 return;
4863
4864 gdb_printf (file, _("aarch64_dump_tdep: Lowest pc = 0x%s\n"),
4865 paddress (gdbarch, tdep->lowest_pc));
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));
4908 }
4909
4910 #if GDB_SELF_TEST
4911 namespace selftests
4912 {
4913 static void aarch64_process_record_test (void);
4914 }
4915 #endif
4916
4917 INIT_GDB_FILE (aarch64_tdep)
4918 {
4919 gdbarch_register (bfd_arch_aarch64, aarch64_gdbarch_init,
4920 aarch64_dump_tdep);
4921
4922 /* Debug this file's internals. */
4923 add_setshow_boolean_cmd ("aarch64", class_maintenance, &aarch64_debug, _("\
4924 Set AArch64 debugging."), _("\
4925 Show AArch64 debugging."), _("\
4926 When on, AArch64 specific debugging is enabled."),
4927 NULL,
4928 show_aarch64_debug,
4929 &setdebuglist, &showdebuglist);
4930
4931 #if GDB_SELF_TEST
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);
4936 #endif
4937 }
4938
4939 /* AArch64 process record-replay related structures, defines etc. */
4940
4941 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
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)
4952
4953 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
4954 do \
4955 { \
4956 unsigned int mem_len = LENGTH; \
4957 if (mem_len) \
4958 { \
4959 MEMS = XNEWVEC (struct aarch64_mem_r, mem_len); \
4960 memcpy(MEMS, &RECORD_BUF[0], \
4961 sizeof(struct aarch64_mem_r) * LENGTH); \
4962 } \
4963 } \
4964 while (0)
4965
4966 /* AArch64 record/replay structures and enumerations. */
4967
4968 struct aarch64_mem_r
4969 {
4970 uint64_t len; /* Record length. */
4971 uint64_t addr; /* Memory address. */
4972 };
4973
4974 enum aarch64_record_result
4975 {
4976 AARCH64_RECORD_SUCCESS,
4977 AARCH64_RECORD_UNSUPPORTED,
4978 AARCH64_RECORD_UNKNOWN
4979 };
4980
4981 struct aarch64_insn_decode_record
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. */
4991 };
4992
4993 /* Record handler for data processing - register instructions. */
4994
4995 static unsigned int
4996 aarch64_record_data_proc_reg (aarch64_insn_decode_record *aarch64_insn_r)
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 {
5053 /* Conditional select. */
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
5071 static unsigned int
5072 aarch64_record_data_proc_imm (aarch64_insn_decode_record *aarch64_insn_r)
5073 {
5074 uint8_t reg_rd, insn_bit23, insn_bits24_27, setflags;
5075 uint32_t record_buf[4];
5076
5077 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
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
5116 static unsigned int
5117 aarch64_record_branch_except_sys (aarch64_insn_decode_record *aarch64_insn_r)
5118 {
5119
5120 aarch64_gdbarch_tdep *tdep
5121 = gdbarch_tdep<aarch64_gdbarch_tdep> (aarch64_insn_r->gdbarch);
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 {
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)
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
5198 static unsigned int
5199 aarch64_record_asimd_load_store (aarch64_insn_decode_record *aarch64_insn_r)
5200 {
5201 CORE_ADDR address;
5202 uint64_t addr_offset = 0;
5203 uint32_t record_buf[24];
5204 std::vector<uint64_t> record_buf_mem;
5205 uint32_t reg_rn, reg_rt;
5206 uint32_t reg_index = 0;
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)
5216 debug_printf ("Process record: Advanced SIMD load/store\n");
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) |
5224 bit (aarch64_insn_r->aarch64_insn, 21)) + 1;
5225 switch (scale)
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 }
5254 esize = 8 << scale;
5255 if (replicate)
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 }
5261 else
5262 {
5263 for (sindex = 0; sindex < selem; sindex++)
5264 {
5265 if (bit (aarch64_insn_r->aarch64_insn, 22))
5266 record_buf[reg_index++] = reg_rt + AARCH64_V0_REGNUM;
5267 else
5268 {
5269 record_buf_mem.push_back (esize / 8);
5270 record_buf_mem.push_back (address + addr_offset);
5271 }
5272 addr_offset = addr_offset + (esize / 8);
5273 reg_rt = (reg_rt + 1) % 32;
5274 }
5275 }
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))
5285 elements = 128 / esize;
5286 else
5287 elements = 64 / esize;
5288
5289 switch (opcode_bits)
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 }
5330 for (rindex = 0; rindex < rpt; rindex++)
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 {
5341 record_buf_mem.push_back (esize / 8);
5342 record_buf_mem.push_back (address + addr_offset);
5343 }
5344 addr_offset = addr_offset + (esize / 8);
5345 reg_tt = (reg_tt + 1) % 32;
5346 }
5347 }
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;
5354 aarch64_insn_r->mem_rec_count = record_buf_mem.size () / 2;
5355 MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
5356 record_buf_mem.data ());
5357 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
5358 record_buf);
5359 return AARCH64_RECORD_SUCCESS;
5360 }
5361
5362 /* Record handler for Memory Copy and Memory Set instructions. */
5363
5364 static unsigned int
5365 aarch64_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
5427 /* Record handler for load and store instructions. */
5428
5429 static unsigned int
5430 aarch64_record_load_store (aarch64_insn_decode_record *aarch64_insn_r)
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)
5457 debug_printf ("Process record: load/store exclusive\n");
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)
5492 debug_printf ("Process record: load register (literal)\n");
5493 if (vector_flag)
5494 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
5495 else
5496 record_buf[0] = reg_rt;
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)
5503 debug_printf ("Process record: load/store pair\n");
5504
5505 if (ld_flag)
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 }
5519 else
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 }
5544 if (bit (aarch64_insn_r->aarch64_insn, 23))
5545 record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
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))
5552 {
5553 if (opc & 0x01)
5554 ld_flag = 0x01;
5555 else
5556 ld_flag = 0x0;
5557 }
5558 else
5559 {
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 }
5570 else
5571 {
5572 if (opc & 0x01)
5573 ld_flag = 0x01;
5574 else
5575 ld_flag = 0x0;
5576 }
5577 }
5578
5579 if (record_debug)
5580 {
5581 debug_printf ("Process record: load/store (unsigned immediate):"
5582 " size %x V %d opc %x\n", size_bits, vector_flag,
5583 opc);
5584 }
5585
5586 if (!ld_flag)
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 }
5599 else
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 }
5607 }
5608 /* Load/store register (register offset) instructions. */
5609 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
5610 && insn_bits10_11 == 0x02 && insn_bit21)
5611 {
5612 if (record_debug)
5613 debug_printf ("Process record: load/store (register offset)\n");
5614 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
5615 if (!(opc >> 1))
5616 if (opc & 0x01)
5617 ld_flag = 0x01;
5618 else
5619 ld_flag = 0x0;
5620 else
5621 if (size_bits != 0x03)
5622 ld_flag = 0x01;
5623 else
5624 return AARCH64_RECORD_UNKNOWN;
5625
5626 if (!ld_flag)
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 }
5644 else
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 }
5652 }
5653 /* Load/store register (immediate and unprivileged) instructions. */
5654 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
5655 && !insn_bit21)
5656 {
5657 if (record_debug)
5658 {
5659 debug_printf ("Process record: load/store "
5660 "(immediate and unprivileged)\n");
5661 }
5662 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
5663 if (!(opc >> 1))
5664 if (opc & 0x01)
5665 ld_flag = 0x01;
5666 else
5667 ld_flag = 0x0;
5668 else
5669 if (size_bits != 0x03)
5670 ld_flag = 0x01;
5671 else
5672 return AARCH64_RECORD_UNKNOWN;
5673
5674 if (!ld_flag)
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 }
5693 else
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 }
5701 if (insn_bits10_11 == 0x01 || insn_bits10_11 == 0x03)
5702 record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
5703 }
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);
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,
5713 record_buf_mem);
5714 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
5715 record_buf);
5716 return AARCH64_RECORD_SUCCESS;
5717 }
5718
5719 /* Record handler for data processing SIMD and floating point instructions. */
5720
5721 static unsigned int
5722 aarch64_record_data_proc_simd_fp (aarch64_insn_decode_record *aarch64_insn_r)
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)
5740 debug_printf ("Process record: data processing SIMD/FP: ");
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)
5748 debug_printf ("FP - fixed point conversion");
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)
5759 debug_printf ("FP - conditional compare");
5760
5761 record_buf[0] = AARCH64_CPSR_REGNUM;
5762 }
5763 /* Floating point - data processing (2-source) and
5764 conditional select instructions. */
5765 else if (insn_bits10_11 == 0x02 || insn_bits10_11 == 0x03)
5766 {
5767 if (record_debug)
5768 debug_printf ("FP - DP (2-source)");
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)
5779 debug_printf ("FP - immediate");
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)
5786 debug_printf ("FP - immediate");
5787 record_buf[0] = AARCH64_CPSR_REGNUM;
5788 }
5789 /* Floating point - integer conversions instructions. */
5790 else if (insn_bits12_15 == 0x00)
5791 {
5792 /* Convert float to integer instruction. */
5793 if (!(opcode >> 1) || ((opcode >> 1) == 0x02 && !rmode))
5794 {
5795 if (record_debug)
5796 debug_printf ("float to int conversion");
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)
5804 debug_printf ("int to float conversion");
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)
5812 debug_printf ("move float to int");
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 }
5819 else
5820 return AARCH64_RECORD_UNKNOWN;
5821 }
5822 else
5823 return AARCH64_RECORD_UNKNOWN;
5824 }
5825 else
5826 return AARCH64_RECORD_UNKNOWN;
5827 }
5828 else if ((insn_bits28_31 & 0x09) == 0x00 && insn_bits24_27 == 0x0e)
5829 {
5830 if (record_debug)
5831 debug_printf ("SIMD copy");
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)
5850 debug_printf ("all remain");
5851
5852 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
5853 }
5854
5855 if (record_debug)
5856 debug_printf ("\n");
5857
5858 /* Record the V/X register. */
5859 aarch64_insn_r->reg_rec_count++;
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);
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
5874 static unsigned int
5875 aarch64_record_decode_insn_handler (aarch64_insn_decode_record *aarch64_insn_r)
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
5909 static void
5910 deallocate_reg_mem (aarch64_insn_decode_record *record)
5911 {
5912 xfree (record->aarch64_regs);
5913 xfree (record->aarch64_mems);
5914 }
5915
5916 #if GDB_SELF_TEST
5917 namespace selftests {
5918
5919 static void
5920 aarch64_process_record_test (void)
5921 {
5922 struct gdbarch_info info;
5923 uint32_t ret;
5924
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
5930 aarch64_insn_decode_record aarch64_record;
5931
5932 memset (&aarch64_record, 0, sizeof (aarch64_insn_decode_record));
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
5947 } /* namespace selftests */
5948 #endif /* GDB_SELF_TEST */
5949
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
5954 int
5955 aarch64_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
5956 CORE_ADDR insn_addr)
5957 {
5958 uint32_t rec_no = 0;
5959 const uint8_t insn_size = 4;
5960 uint32_t ret = 0;
5961 gdb_byte buf[insn_size];
5962 aarch64_insn_decode_record aarch64_record;
5963
5964 memset (&buf[0], 0, insn_size);
5965 memset (&aarch64_record, 0, sizeof (aarch64_insn_decode_record));
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 {
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));
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 }