gdb: rename displaced_step_closure to displaced_step_copy_insn_closure
[deliverable/binutils-gdb.git] / gdb / aarch64-tdep.c
CommitLineData
07b287a0
MS
1/* Common target dependent code for GDB on AArch64 systems.
2
b811d2c2 3 Copyright (C) 2009-2020 Free Software Foundation, Inc.
07b287a0
MS
4 Contributed by ARM Ltd.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include "defs.h"
22
23#include "frame.h"
07b287a0
MS
24#include "gdbcmd.h"
25#include "gdbcore.h"
4de283e4 26#include "dis-asm.h"
d55e5aa6
TT
27#include "regcache.h"
28#include "reggroups.h"
4de283e4
TT
29#include "value.h"
30#include "arch-utils.h"
31#include "osabi.h"
32#include "frame-unwind.h"
33#include "frame-base.h"
d55e5aa6 34#include "trad-frame.h"
4de283e4
TT
35#include "objfiles.h"
36#include "dwarf2.h"
82ca8957 37#include "dwarf2/frame.h"
4de283e4
TT
38#include "gdbtypes.h"
39#include "prologue-value.h"
40#include "target-descriptions.h"
07b287a0 41#include "user-regs.h"
4de283e4 42#include "ax-gdb.h"
268a13a5 43#include "gdbsupport/selftest.h"
4de283e4
TT
44
45#include "aarch64-tdep.h"
46#include "aarch64-ravenscar-thread.h"
47
4de283e4
TT
48#include "record.h"
49#include "record-full.h"
50#include "arch/aarch64-insn.h"
0d12e84c 51#include "gdbarch.h"
4de283e4
TT
52
53#include "opcode/aarch64.h"
54#include <algorithm>
f77ee802
YQ
55
56#define submask(x) ((1L << ((x) + 1)) - 1)
57#define bit(obj,st) (((obj) >> (st)) & 1)
58#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
59
ea92689a
AH
60/* A Homogeneous Floating-Point or Short-Vector Aggregate may have at most
61 four members. */
62#define HA_MAX_NUM_FLDS 4
63
95228a0d 64/* All possible aarch64 target descriptors. */
6dc0ebde 65struct target_desc *tdesc_aarch64_list[AARCH64_MAX_SVE_VQ + 1][2/*pauth*/];
95228a0d 66
07b287a0
MS
67/* The standard register names, and all the valid aliases for them. */
68static const struct
69{
70 const char *const name;
71 int regnum;
72} aarch64_register_aliases[] =
73{
74 /* 64-bit register names. */
75 {"fp", AARCH64_FP_REGNUM},
76 {"lr", AARCH64_LR_REGNUM},
77 {"sp", AARCH64_SP_REGNUM},
78
79 /* 32-bit register names. */
80 {"w0", AARCH64_X0_REGNUM + 0},
81 {"w1", AARCH64_X0_REGNUM + 1},
82 {"w2", AARCH64_X0_REGNUM + 2},
83 {"w3", AARCH64_X0_REGNUM + 3},
84 {"w4", AARCH64_X0_REGNUM + 4},
85 {"w5", AARCH64_X0_REGNUM + 5},
86 {"w6", AARCH64_X0_REGNUM + 6},
87 {"w7", AARCH64_X0_REGNUM + 7},
88 {"w8", AARCH64_X0_REGNUM + 8},
89 {"w9", AARCH64_X0_REGNUM + 9},
90 {"w10", AARCH64_X0_REGNUM + 10},
91 {"w11", AARCH64_X0_REGNUM + 11},
92 {"w12", AARCH64_X0_REGNUM + 12},
93 {"w13", AARCH64_X0_REGNUM + 13},
94 {"w14", AARCH64_X0_REGNUM + 14},
95 {"w15", AARCH64_X0_REGNUM + 15},
96 {"w16", AARCH64_X0_REGNUM + 16},
97 {"w17", AARCH64_X0_REGNUM + 17},
98 {"w18", AARCH64_X0_REGNUM + 18},
99 {"w19", AARCH64_X0_REGNUM + 19},
100 {"w20", AARCH64_X0_REGNUM + 20},
101 {"w21", AARCH64_X0_REGNUM + 21},
102 {"w22", AARCH64_X0_REGNUM + 22},
103 {"w23", AARCH64_X0_REGNUM + 23},
104 {"w24", AARCH64_X0_REGNUM + 24},
105 {"w25", AARCH64_X0_REGNUM + 25},
106 {"w26", AARCH64_X0_REGNUM + 26},
107 {"w27", AARCH64_X0_REGNUM + 27},
108 {"w28", AARCH64_X0_REGNUM + 28},
109 {"w29", AARCH64_X0_REGNUM + 29},
110 {"w30", AARCH64_X0_REGNUM + 30},
111
112 /* specials */
113 {"ip0", AARCH64_X0_REGNUM + 16},
114 {"ip1", AARCH64_X0_REGNUM + 17}
115};
116
117/* The required core 'R' registers. */
118static const char *const aarch64_r_register_names[] =
119{
120 /* These registers must appear in consecutive RAW register number
121 order and they must begin with AARCH64_X0_REGNUM! */
122 "x0", "x1", "x2", "x3",
123 "x4", "x5", "x6", "x7",
124 "x8", "x9", "x10", "x11",
125 "x12", "x13", "x14", "x15",
126 "x16", "x17", "x18", "x19",
127 "x20", "x21", "x22", "x23",
128 "x24", "x25", "x26", "x27",
129 "x28", "x29", "x30", "sp",
130 "pc", "cpsr"
131};
132
133/* The FP/SIMD 'V' registers. */
134static const char *const aarch64_v_register_names[] =
135{
136 /* These registers must appear in consecutive RAW register number
137 order and they must begin with AARCH64_V0_REGNUM! */
138 "v0", "v1", "v2", "v3",
139 "v4", "v5", "v6", "v7",
140 "v8", "v9", "v10", "v11",
141 "v12", "v13", "v14", "v15",
142 "v16", "v17", "v18", "v19",
143 "v20", "v21", "v22", "v23",
144 "v24", "v25", "v26", "v27",
145 "v28", "v29", "v30", "v31",
146 "fpsr",
147 "fpcr"
148};
149
739e8682
AH
150/* The SVE 'Z' and 'P' registers. */
151static const char *const aarch64_sve_register_names[] =
152{
153 /* These registers must appear in consecutive RAW register number
154 order and they must begin with AARCH64_SVE_Z0_REGNUM! */
155 "z0", "z1", "z2", "z3",
156 "z4", "z5", "z6", "z7",
157 "z8", "z9", "z10", "z11",
158 "z12", "z13", "z14", "z15",
159 "z16", "z17", "z18", "z19",
160 "z20", "z21", "z22", "z23",
161 "z24", "z25", "z26", "z27",
162 "z28", "z29", "z30", "z31",
163 "fpsr", "fpcr",
164 "p0", "p1", "p2", "p3",
165 "p4", "p5", "p6", "p7",
166 "p8", "p9", "p10", "p11",
167 "p12", "p13", "p14", "p15",
168 "ffr", "vg"
169};
170
76bed0fd
AH
171static const char *const aarch64_pauth_register_names[] =
172{
173 /* Authentication mask for data pointer. */
174 "pauth_dmask",
175 /* Authentication mask for code pointer. */
176 "pauth_cmask"
177};
178
07b287a0
MS
179/* AArch64 prologue cache structure. */
180struct aarch64_prologue_cache
181{
db634143
PL
182 /* The program counter at the start of the function. It is used to
183 identify this frame as a prologue frame. */
184 CORE_ADDR func;
185
186 /* The program counter at the time this frame was created; i.e. where
187 this function was called from. It is used to identify this frame as a
188 stub frame. */
189 CORE_ADDR prev_pc;
190
07b287a0
MS
191 /* The stack pointer at the time this frame was created; i.e. the
192 caller's stack pointer when this function was called. It is used
193 to identify this frame. */
194 CORE_ADDR prev_sp;
195
7dfa3edc
PL
196 /* Is the target available to read from? */
197 int available_p;
198
07b287a0
MS
199 /* The frame base for this frame is just prev_sp - frame size.
200 FRAMESIZE is the distance from the frame pointer to the
201 initial stack pointer. */
202 int framesize;
203
204 /* The register used to hold the frame pointer for this frame. */
205 int framereg;
206
207 /* Saved register offsets. */
208 struct trad_frame_saved_reg *saved_regs;
209};
210
07b287a0
MS
211static void
212show_aarch64_debug (struct ui_file *file, int from_tty,
213 struct cmd_list_element *c, const char *value)
214{
215 fprintf_filtered (file, _("AArch64 debugging is %s.\n"), value);
216}
217
ffdbe864
YQ
218namespace {
219
4d9a9006
YQ
220/* Abstract instruction reader. */
221
222class abstract_instruction_reader
223{
224public:
225 /* Read in one instruction. */
226 virtual ULONGEST read (CORE_ADDR memaddr, int len,
227 enum bfd_endian byte_order) = 0;
228};
229
230/* Instruction reader from real target. */
231
232class instruction_reader : public abstract_instruction_reader
233{
234 public:
235 ULONGEST read (CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
632e107b 236 override
4d9a9006 237 {
fc2f703e 238 return read_code_unsigned_integer (memaddr, len, byte_order);
4d9a9006
YQ
239 }
240};
241
ffdbe864
YQ
242} // namespace
243
3d31bc39
AH
244/* If address signing is enabled, mask off the signature bits from the link
245 register, which is passed by value in ADDR, using the register values in
246 THIS_FRAME. */
11e1b75f
AH
247
248static CORE_ADDR
3d31bc39
AH
249aarch64_frame_unmask_lr (struct gdbarch_tdep *tdep,
250 struct frame_info *this_frame, CORE_ADDR addr)
11e1b75f
AH
251{
252 if (tdep->has_pauth ()
253 && frame_unwind_register_unsigned (this_frame,
254 tdep->pauth_ra_state_regnum))
255 {
256 int cmask_num = AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base);
257 CORE_ADDR cmask = frame_unwind_register_unsigned (this_frame, cmask_num);
258 addr = addr & ~cmask;
3d31bc39
AH
259
260 /* Record in the frame that the link register required unmasking. */
261 set_frame_previous_pc_masked (this_frame);
11e1b75f
AH
262 }
263
264 return addr;
265}
266
aa7ca1bb
AH
267/* Implement the "get_pc_address_flags" gdbarch method. */
268
269static std::string
270aarch64_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
271{
272 if (pc != 0 && get_frame_pc_masked (frame))
273 return "PAC";
274
275 return "";
276}
277
07b287a0
MS
278/* Analyze a prologue, looking for a recognizable stack frame
279 and frame pointer. Scan until we encounter a store that could
280 clobber the stack frame unexpectedly, or an unknown instruction. */
281
282static CORE_ADDR
283aarch64_analyze_prologue (struct gdbarch *gdbarch,
284 CORE_ADDR start, CORE_ADDR limit,
4d9a9006
YQ
285 struct aarch64_prologue_cache *cache,
286 abstract_instruction_reader& reader)
07b287a0
MS
287{
288 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
289 int i;
187f5d00
YQ
290 /* Track X registers and D registers in prologue. */
291 pv_t regs[AARCH64_X_REGISTER_COUNT + AARCH64_D_REGISTER_COUNT];
07b287a0 292
187f5d00 293 for (i = 0; i < AARCH64_X_REGISTER_COUNT + AARCH64_D_REGISTER_COUNT; i++)
07b287a0 294 regs[i] = pv_register (i, 0);
f7b7ed97 295 pv_area stack (AARCH64_SP_REGNUM, gdbarch_addr_bit (gdbarch));
07b287a0
MS
296
297 for (; start < limit; start += 4)
298 {
299 uint32_t insn;
d9ebcbce 300 aarch64_inst inst;
07b287a0 301
4d9a9006 302 insn = reader.read (start, 4, byte_order_for_code);
07b287a0 303
561a72d4 304 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
d9ebcbce
YQ
305 break;
306
307 if (inst.opcode->iclass == addsub_imm
308 && (inst.opcode->op == OP_ADD
309 || strcmp ("sub", inst.opcode->name) == 0))
07b287a0 310 {
d9ebcbce
YQ
311 unsigned rd = inst.operands[0].reg.regno;
312 unsigned rn = inst.operands[1].reg.regno;
313
314 gdb_assert (aarch64_num_of_operands (inst.opcode) == 3);
315 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd_SP);
316 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rn_SP);
317 gdb_assert (inst.operands[2].type == AARCH64_OPND_AIMM);
318
319 if (inst.opcode->op == OP_ADD)
320 {
321 regs[rd] = pv_add_constant (regs[rn],
322 inst.operands[2].imm.value);
323 }
324 else
325 {
326 regs[rd] = pv_add_constant (regs[rn],
327 -inst.operands[2].imm.value);
328 }
329 }
330 else if (inst.opcode->iclass == pcreladdr
331 && inst.operands[1].type == AARCH64_OPND_ADDR_ADRP)
332 {
333 gdb_assert (aarch64_num_of_operands (inst.opcode) == 2);
334 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
335
336 regs[inst.operands[0].reg.regno] = pv_unknown ();
07b287a0 337 }
d9ebcbce 338 else if (inst.opcode->iclass == branch_imm)
07b287a0
MS
339 {
340 /* Stop analysis on branch. */
341 break;
342 }
d9ebcbce 343 else if (inst.opcode->iclass == condbranch)
07b287a0
MS
344 {
345 /* Stop analysis on branch. */
346 break;
347 }
d9ebcbce 348 else if (inst.opcode->iclass == branch_reg)
07b287a0
MS
349 {
350 /* Stop analysis on branch. */
351 break;
352 }
d9ebcbce 353 else if (inst.opcode->iclass == compbranch)
07b287a0
MS
354 {
355 /* Stop analysis on branch. */
356 break;
357 }
d9ebcbce
YQ
358 else if (inst.opcode->op == OP_MOVZ)
359 {
360 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
361 regs[inst.operands[0].reg.regno] = pv_unknown ();
362 }
363 else if (inst.opcode->iclass == log_shift
364 && strcmp (inst.opcode->name, "orr") == 0)
07b287a0 365 {
d9ebcbce
YQ
366 unsigned rd = inst.operands[0].reg.regno;
367 unsigned rn = inst.operands[1].reg.regno;
368 unsigned rm = inst.operands[2].reg.regno;
369
370 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
371 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rn);
372 gdb_assert (inst.operands[2].type == AARCH64_OPND_Rm_SFT);
373
374 if (inst.operands[2].shifter.amount == 0
375 && rn == AARCH64_SP_REGNUM)
07b287a0
MS
376 regs[rd] = regs[rm];
377 else
378 {
379 if (aarch64_debug)
b277c936
PL
380 {
381 debug_printf ("aarch64: prologue analysis gave up "
0a0da556 382 "addr=%s opcode=0x%x (orr x register)\n",
b277c936
PL
383 core_addr_to_string_nz (start), insn);
384 }
07b287a0
MS
385 break;
386 }
387 }
d9ebcbce 388 else if (inst.opcode->op == OP_STUR)
07b287a0 389 {
d9ebcbce
YQ
390 unsigned rt = inst.operands[0].reg.regno;
391 unsigned rn = inst.operands[1].addr.base_regno;
75faf5c4 392 int size = aarch64_get_qualifier_esize (inst.operands[0].qualifier);
d9ebcbce
YQ
393
394 gdb_assert (aarch64_num_of_operands (inst.opcode) == 2);
395 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt);
396 gdb_assert (inst.operands[1].type == AARCH64_OPND_ADDR_SIMM9);
397 gdb_assert (!inst.operands[1].addr.offset.is_reg);
398
75faf5c4
AH
399 stack.store
400 (pv_add_constant (regs[rn], inst.operands[1].addr.offset.imm),
401 size, regs[rt]);
07b287a0 402 }
d9ebcbce 403 else if ((inst.opcode->iclass == ldstpair_off
03bcd739
YQ
404 || (inst.opcode->iclass == ldstpair_indexed
405 && inst.operands[2].addr.preind))
d9ebcbce 406 && strcmp ("stp", inst.opcode->name) == 0)
07b287a0 407 {
03bcd739 408 /* STP with addressing mode Pre-indexed and Base register. */
187f5d00
YQ
409 unsigned rt1;
410 unsigned rt2;
d9ebcbce
YQ
411 unsigned rn = inst.operands[2].addr.base_regno;
412 int32_t imm = inst.operands[2].addr.offset.imm;
75faf5c4 413 int size = aarch64_get_qualifier_esize (inst.operands[0].qualifier);
d9ebcbce 414
187f5d00
YQ
415 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt
416 || inst.operands[0].type == AARCH64_OPND_Ft);
417 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rt2
418 || inst.operands[1].type == AARCH64_OPND_Ft2);
d9ebcbce
YQ
419 gdb_assert (inst.operands[2].type == AARCH64_OPND_ADDR_SIMM7);
420 gdb_assert (!inst.operands[2].addr.offset.is_reg);
421
07b287a0
MS
422 /* If recording this store would invalidate the store area
423 (perhaps because rn is not known) then we should abandon
424 further prologue analysis. */
f7b7ed97 425 if (stack.store_would_trash (pv_add_constant (regs[rn], imm)))
07b287a0
MS
426 break;
427
f7b7ed97 428 if (stack.store_would_trash (pv_add_constant (regs[rn], imm + 8)))
07b287a0
MS
429 break;
430
187f5d00
YQ
431 rt1 = inst.operands[0].reg.regno;
432 rt2 = inst.operands[1].reg.regno;
433 if (inst.operands[0].type == AARCH64_OPND_Ft)
434 {
187f5d00
YQ
435 rt1 += AARCH64_X_REGISTER_COUNT;
436 rt2 += AARCH64_X_REGISTER_COUNT;
437 }
438
75faf5c4
AH
439 stack.store (pv_add_constant (regs[rn], imm), size, regs[rt1]);
440 stack.store (pv_add_constant (regs[rn], imm + size), size, regs[rt2]);
14ac654f 441
d9ebcbce 442 if (inst.operands[2].addr.writeback)
93d96012 443 regs[rn] = pv_add_constant (regs[rn], imm);
07b287a0 444
07b287a0 445 }
432ec081
YQ
446 else if ((inst.opcode->iclass == ldst_imm9 /* Signed immediate. */
447 || (inst.opcode->iclass == ldst_pos /* Unsigned immediate. */
448 && (inst.opcode->op == OP_STR_POS
449 || inst.opcode->op == OP_STRF_POS)))
450 && inst.operands[1].addr.base_regno == AARCH64_SP_REGNUM
451 && strcmp ("str", inst.opcode->name) == 0)
452 {
453 /* STR (immediate) */
454 unsigned int rt = inst.operands[0].reg.regno;
455 int32_t imm = inst.operands[1].addr.offset.imm;
456 unsigned int rn = inst.operands[1].addr.base_regno;
75faf5c4 457 int size = aarch64_get_qualifier_esize (inst.operands[0].qualifier);
432ec081
YQ
458 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt
459 || inst.operands[0].type == AARCH64_OPND_Ft);
460
461 if (inst.operands[0].type == AARCH64_OPND_Ft)
75faf5c4 462 rt += AARCH64_X_REGISTER_COUNT;
432ec081 463
75faf5c4 464 stack.store (pv_add_constant (regs[rn], imm), size, regs[rt]);
432ec081
YQ
465 if (inst.operands[1].addr.writeback)
466 regs[rn] = pv_add_constant (regs[rn], imm);
467 }
d9ebcbce 468 else if (inst.opcode->iclass == testbranch)
07b287a0
MS
469 {
470 /* Stop analysis on branch. */
471 break;
472 }
17e116a7
AH
473 else if (inst.opcode->iclass == ic_system)
474 {
475 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
476 int ra_state_val = 0;
477
478 if (insn == 0xd503233f /* paciasp. */
479 || insn == 0xd503237f /* pacibsp. */)
480 {
481 /* Return addresses are mangled. */
482 ra_state_val = 1;
483 }
484 else if (insn == 0xd50323bf /* autiasp. */
485 || insn == 0xd50323ff /* autibsp. */)
486 {
487 /* Return addresses are not mangled. */
488 ra_state_val = 0;
489 }
490 else
491 {
492 if (aarch64_debug)
493 debug_printf ("aarch64: prologue analysis gave up addr=%s"
494 " opcode=0x%x (iclass)\n",
495 core_addr_to_string_nz (start), insn);
496 break;
497 }
498
499 if (tdep->has_pauth () && cache != nullptr)
500 trad_frame_set_value (cache->saved_regs,
501 tdep->pauth_ra_state_regnum,
502 ra_state_val);
503 }
07b287a0
MS
504 else
505 {
506 if (aarch64_debug)
b277c936 507 {
0a0da556 508 debug_printf ("aarch64: prologue analysis gave up addr=%s"
b277c936
PL
509 " opcode=0x%x\n",
510 core_addr_to_string_nz (start), insn);
511 }
07b287a0
MS
512 break;
513 }
514 }
515
516 if (cache == NULL)
f7b7ed97 517 return start;
07b287a0
MS
518
519 if (pv_is_register (regs[AARCH64_FP_REGNUM], AARCH64_SP_REGNUM))
520 {
521 /* Frame pointer is fp. Frame size is constant. */
522 cache->framereg = AARCH64_FP_REGNUM;
523 cache->framesize = -regs[AARCH64_FP_REGNUM].k;
524 }
525 else if (pv_is_register (regs[AARCH64_SP_REGNUM], AARCH64_SP_REGNUM))
526 {
527 /* Try the stack pointer. */
528 cache->framesize = -regs[AARCH64_SP_REGNUM].k;
529 cache->framereg = AARCH64_SP_REGNUM;
530 }
531 else
532 {
533 /* We're just out of luck. We don't know where the frame is. */
534 cache->framereg = -1;
535 cache->framesize = 0;
536 }
537
538 for (i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
539 {
540 CORE_ADDR offset;
541
f7b7ed97 542 if (stack.find_reg (gdbarch, i, &offset))
07b287a0
MS
543 cache->saved_regs[i].addr = offset;
544 }
545
187f5d00
YQ
546 for (i = 0; i < AARCH64_D_REGISTER_COUNT; i++)
547 {
548 int regnum = gdbarch_num_regs (gdbarch);
549 CORE_ADDR offset;
550
f7b7ed97
TT
551 if (stack.find_reg (gdbarch, i + AARCH64_X_REGISTER_COUNT,
552 &offset))
187f5d00
YQ
553 cache->saved_regs[i + regnum + AARCH64_D0_REGNUM].addr = offset;
554 }
555
07b287a0
MS
556 return start;
557}
558
4d9a9006
YQ
559static CORE_ADDR
560aarch64_analyze_prologue (struct gdbarch *gdbarch,
561 CORE_ADDR start, CORE_ADDR limit,
562 struct aarch64_prologue_cache *cache)
563{
564 instruction_reader reader;
565
566 return aarch64_analyze_prologue (gdbarch, start, limit, cache,
567 reader);
568}
569
570#if GDB_SELF_TEST
571
572namespace selftests {
573
574/* Instruction reader from manually cooked instruction sequences. */
575
576class instruction_reader_test : public abstract_instruction_reader
577{
578public:
579 template<size_t SIZE>
580 explicit instruction_reader_test (const uint32_t (&insns)[SIZE])
581 : m_insns (insns), m_insns_size (SIZE)
582 {}
583
584 ULONGEST read (CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
632e107b 585 override
4d9a9006
YQ
586 {
587 SELF_CHECK (len == 4);
588 SELF_CHECK (memaddr % 4 == 0);
589 SELF_CHECK (memaddr / 4 < m_insns_size);
590
591 return m_insns[memaddr / 4];
592 }
593
594private:
595 const uint32_t *m_insns;
596 size_t m_insns_size;
597};
598
599static void
600aarch64_analyze_prologue_test (void)
601{
602 struct gdbarch_info info;
603
604 gdbarch_info_init (&info);
605 info.bfd_arch_info = bfd_scan_arch ("aarch64");
606
607 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
608 SELF_CHECK (gdbarch != NULL);
609
17e116a7
AH
610 struct aarch64_prologue_cache cache;
611 cache.saved_regs = trad_frame_alloc_saved_regs (gdbarch);
612
613 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
614
4d9a9006
YQ
615 /* Test the simple prologue in which frame pointer is used. */
616 {
4d9a9006
YQ
617 static const uint32_t insns[] = {
618 0xa9af7bfd, /* stp x29, x30, [sp,#-272]! */
619 0x910003fd, /* mov x29, sp */
620 0x97ffffe6, /* bl 0x400580 */
621 };
622 instruction_reader_test reader (insns);
623
624 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache, reader);
625 SELF_CHECK (end == 4 * 2);
626
627 SELF_CHECK (cache.framereg == AARCH64_FP_REGNUM);
628 SELF_CHECK (cache.framesize == 272);
629
630 for (int i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
631 {
632 if (i == AARCH64_FP_REGNUM)
633 SELF_CHECK (cache.saved_regs[i].addr == -272);
634 else if (i == AARCH64_LR_REGNUM)
635 SELF_CHECK (cache.saved_regs[i].addr == -264);
636 else
637 SELF_CHECK (cache.saved_regs[i].addr == -1);
638 }
639
640 for (int i = 0; i < AARCH64_D_REGISTER_COUNT; i++)
641 {
642 int regnum = gdbarch_num_regs (gdbarch);
643
644 SELF_CHECK (cache.saved_regs[i + regnum + AARCH64_D0_REGNUM].addr
645 == -1);
646 }
647 }
432ec081
YQ
648
649 /* Test a prologue in which STR is used and frame pointer is not
650 used. */
651 {
432ec081
YQ
652 static const uint32_t insns[] = {
653 0xf81d0ff3, /* str x19, [sp, #-48]! */
654 0xb9002fe0, /* str w0, [sp, #44] */
655 0xf90013e1, /* str x1, [sp, #32]*/
656 0xfd000fe0, /* str d0, [sp, #24] */
657 0xaa0203f3, /* mov x19, x2 */
658 0xf94013e0, /* ldr x0, [sp, #32] */
659 };
660 instruction_reader_test reader (insns);
661
68811f8f 662 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
432ec081
YQ
663 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache, reader);
664
665 SELF_CHECK (end == 4 * 5);
666
667 SELF_CHECK (cache.framereg == AARCH64_SP_REGNUM);
668 SELF_CHECK (cache.framesize == 48);
669
670 for (int i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
671 {
672 if (i == 1)
673 SELF_CHECK (cache.saved_regs[i].addr == -16);
674 else if (i == 19)
675 SELF_CHECK (cache.saved_regs[i].addr == -48);
676 else
677 SELF_CHECK (cache.saved_regs[i].addr == -1);
678 }
679
680 for (int i = 0; i < AARCH64_D_REGISTER_COUNT; i++)
681 {
682 int regnum = gdbarch_num_regs (gdbarch);
683
684 if (i == 0)
685 SELF_CHECK (cache.saved_regs[i + regnum + AARCH64_D0_REGNUM].addr
686 == -24);
687 else
688 SELF_CHECK (cache.saved_regs[i + regnum + AARCH64_D0_REGNUM].addr
689 == -1);
690 }
691 }
17e116a7
AH
692
693 /* Test a prologue in which there is a return address signing instruction. */
694 if (tdep->has_pauth ())
695 {
696 static const uint32_t insns[] = {
697 0xd503233f, /* paciasp */
698 0xa9bd7bfd, /* stp x29, x30, [sp, #-48]! */
699 0x910003fd, /* mov x29, sp */
700 0xf801c3f3, /* str x19, [sp, #28] */
701 0xb9401fa0, /* ldr x19, [x29, #28] */
702 };
703 instruction_reader_test reader (insns);
704
68811f8f 705 trad_frame_reset_saved_regs (gdbarch, cache.saved_regs);
17e116a7
AH
706 CORE_ADDR end = aarch64_analyze_prologue (gdbarch, 0, 128, &cache,
707 reader);
708
709 SELF_CHECK (end == 4 * 4);
710 SELF_CHECK (cache.framereg == AARCH64_FP_REGNUM);
711 SELF_CHECK (cache.framesize == 48);
712
713 for (int i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
714 {
715 if (i == 19)
716 SELF_CHECK (cache.saved_regs[i].addr == -20);
717 else if (i == AARCH64_FP_REGNUM)
718 SELF_CHECK (cache.saved_regs[i].addr == -48);
719 else if (i == AARCH64_LR_REGNUM)
720 SELF_CHECK (cache.saved_regs[i].addr == -40);
721 else
722 SELF_CHECK (cache.saved_regs[i].addr == -1);
723 }
724
725 if (tdep->has_pauth ())
726 {
727 SELF_CHECK (trad_frame_value_p (cache.saved_regs,
728 tdep->pauth_ra_state_regnum));
729 SELF_CHECK (cache.saved_regs[tdep->pauth_ra_state_regnum].addr == 1);
730 }
731 }
4d9a9006
YQ
732}
733} // namespace selftests
734#endif /* GDB_SELF_TEST */
735
07b287a0
MS
736/* Implement the "skip_prologue" gdbarch method. */
737
738static CORE_ADDR
739aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
740{
07b287a0 741 CORE_ADDR func_addr, limit_pc;
07b287a0
MS
742
743 /* See if we can determine the end of the prologue via the symbol
744 table. If so, then return either PC, or the PC after the
745 prologue, whichever is greater. */
746 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
747 {
748 CORE_ADDR post_prologue_pc
749 = skip_prologue_using_sal (gdbarch, func_addr);
750
751 if (post_prologue_pc != 0)
325fac50 752 return std::max (pc, post_prologue_pc);
07b287a0
MS
753 }
754
755 /* Can't determine prologue from the symbol table, need to examine
756 instructions. */
757
758 /* Find an upper limit on the function prologue using the debug
759 information. If the debug information could not be used to
760 provide that bound, then use an arbitrary large number as the
761 upper bound. */
762 limit_pc = skip_prologue_using_sal (gdbarch, pc);
763 if (limit_pc == 0)
764 limit_pc = pc + 128; /* Magic. */
765
766 /* Try disassembling prologue. */
767 return aarch64_analyze_prologue (gdbarch, pc, limit_pc, NULL);
768}
769
770/* Scan the function prologue for THIS_FRAME and populate the prologue
771 cache CACHE. */
772
773static void
774aarch64_scan_prologue (struct frame_info *this_frame,
775 struct aarch64_prologue_cache *cache)
776{
777 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
778 CORE_ADDR prologue_start;
779 CORE_ADDR prologue_end;
780 CORE_ADDR prev_pc = get_frame_pc (this_frame);
781 struct gdbarch *gdbarch = get_frame_arch (this_frame);
782
db634143
PL
783 cache->prev_pc = prev_pc;
784
07b287a0
MS
785 /* Assume we do not find a frame. */
786 cache->framereg = -1;
787 cache->framesize = 0;
788
789 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
790 &prologue_end))
791 {
792 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
793
794 if (sal.line == 0)
795 {
796 /* No line info so use the current PC. */
797 prologue_end = prev_pc;
798 }
799 else if (sal.end < prologue_end)
800 {
801 /* The next line begins after the function end. */
802 prologue_end = sal.end;
803 }
804
325fac50 805 prologue_end = std::min (prologue_end, prev_pc);
07b287a0
MS
806 aarch64_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
807 }
808 else
809 {
810 CORE_ADDR frame_loc;
07b287a0
MS
811
812 frame_loc = get_frame_register_unsigned (this_frame, AARCH64_FP_REGNUM);
813 if (frame_loc == 0)
814 return;
815
816 cache->framereg = AARCH64_FP_REGNUM;
817 cache->framesize = 16;
818 cache->saved_regs[29].addr = 0;
819 cache->saved_regs[30].addr = 8;
820 }
821}
822
7dfa3edc
PL
823/* Fill in *CACHE with information about the prologue of *THIS_FRAME. This
824 function may throw an exception if the inferior's registers or memory is
825 not available. */
07b287a0 826
7dfa3edc
PL
827static void
828aarch64_make_prologue_cache_1 (struct frame_info *this_frame,
829 struct aarch64_prologue_cache *cache)
07b287a0 830{
07b287a0
MS
831 CORE_ADDR unwound_fp;
832 int reg;
833
07b287a0
MS
834 aarch64_scan_prologue (this_frame, cache);
835
836 if (cache->framereg == -1)
7dfa3edc 837 return;
07b287a0
MS
838
839 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
840 if (unwound_fp == 0)
7dfa3edc 841 return;
07b287a0
MS
842
843 cache->prev_sp = unwound_fp + cache->framesize;
844
845 /* Calculate actual addresses of saved registers using offsets
846 determined by aarch64_analyze_prologue. */
847 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
848 if (trad_frame_addr_p (cache->saved_regs, reg))
849 cache->saved_regs[reg].addr += cache->prev_sp;
850
db634143
PL
851 cache->func = get_frame_func (this_frame);
852
7dfa3edc
PL
853 cache->available_p = 1;
854}
855
856/* Allocate and fill in *THIS_CACHE with information about the prologue of
857 *THIS_FRAME. Do not do this is if *THIS_CACHE was already allocated.
858 Return a pointer to the current aarch64_prologue_cache in
859 *THIS_CACHE. */
860
861static struct aarch64_prologue_cache *
862aarch64_make_prologue_cache (struct frame_info *this_frame, void **this_cache)
863{
864 struct aarch64_prologue_cache *cache;
865
866 if (*this_cache != NULL)
9a3c8263 867 return (struct aarch64_prologue_cache *) *this_cache;
7dfa3edc
PL
868
869 cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
870 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
871 *this_cache = cache;
872
a70b8144 873 try
7dfa3edc
PL
874 {
875 aarch64_make_prologue_cache_1 (this_frame, cache);
876 }
230d2906 877 catch (const gdb_exception_error &ex)
7dfa3edc
PL
878 {
879 if (ex.error != NOT_AVAILABLE_ERROR)
eedc3f4f 880 throw;
7dfa3edc 881 }
7dfa3edc 882
07b287a0
MS
883 return cache;
884}
885
7dfa3edc
PL
886/* Implement the "stop_reason" frame_unwind method. */
887
888static enum unwind_stop_reason
889aarch64_prologue_frame_unwind_stop_reason (struct frame_info *this_frame,
890 void **this_cache)
891{
892 struct aarch64_prologue_cache *cache
893 = aarch64_make_prologue_cache (this_frame, this_cache);
894
895 if (!cache->available_p)
896 return UNWIND_UNAVAILABLE;
897
898 /* Halt the backtrace at "_start". */
899 if (cache->prev_pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
900 return UNWIND_OUTERMOST;
901
902 /* We've hit a wall, stop. */
903 if (cache->prev_sp == 0)
904 return UNWIND_OUTERMOST;
905
906 return UNWIND_NO_REASON;
907}
908
07b287a0
MS
909/* Our frame ID for a normal frame is the current function's starting
910 PC and the caller's SP when we were called. */
911
912static void
913aarch64_prologue_this_id (struct frame_info *this_frame,
914 void **this_cache, struct frame_id *this_id)
915{
7c8edfae
PL
916 struct aarch64_prologue_cache *cache
917 = aarch64_make_prologue_cache (this_frame, this_cache);
07b287a0 918
7dfa3edc
PL
919 if (!cache->available_p)
920 *this_id = frame_id_build_unavailable_stack (cache->func);
921 else
922 *this_id = frame_id_build (cache->prev_sp, cache->func);
07b287a0
MS
923}
924
925/* Implement the "prev_register" frame_unwind method. */
926
927static struct value *
928aarch64_prologue_prev_register (struct frame_info *this_frame,
929 void **this_cache, int prev_regnum)
930{
7c8edfae
PL
931 struct aarch64_prologue_cache *cache
932 = aarch64_make_prologue_cache (this_frame, this_cache);
07b287a0
MS
933
934 /* If we are asked to unwind the PC, then we need to return the LR
935 instead. The prologue may save PC, but it will point into this
936 frame's prologue, not the next frame's resume location. */
937 if (prev_regnum == AARCH64_PC_REGNUM)
938 {
939 CORE_ADDR lr;
17e116a7
AH
940 struct gdbarch *gdbarch = get_frame_arch (this_frame);
941 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
07b287a0
MS
942
943 lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
17e116a7
AH
944
945 if (tdep->has_pauth ()
946 && trad_frame_value_p (cache->saved_regs,
947 tdep->pauth_ra_state_regnum))
3d31bc39 948 lr = aarch64_frame_unmask_lr (tdep, this_frame, lr);
17e116a7 949
07b287a0
MS
950 return frame_unwind_got_constant (this_frame, prev_regnum, lr);
951 }
952
953 /* SP is generally not saved to the stack, but this frame is
954 identified by the next frame's stack pointer at the time of the
955 call. The value was already reconstructed into PREV_SP. */
956 /*
957 +----------+ ^
958 | saved lr | |
959 +->| saved fp |--+
960 | | |
961 | | | <- Previous SP
962 | +----------+
963 | | saved lr |
964 +--| saved fp |<- FP
965 | |
966 | |<- SP
967 +----------+ */
968 if (prev_regnum == AARCH64_SP_REGNUM)
969 return frame_unwind_got_constant (this_frame, prev_regnum,
970 cache->prev_sp);
971
972 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
973 prev_regnum);
974}
975
976/* AArch64 prologue unwinder. */
977struct frame_unwind aarch64_prologue_unwind =
978{
979 NORMAL_FRAME,
7dfa3edc 980 aarch64_prologue_frame_unwind_stop_reason,
07b287a0
MS
981 aarch64_prologue_this_id,
982 aarch64_prologue_prev_register,
983 NULL,
984 default_frame_sniffer
985};
986
8b61f75d
PL
987/* Allocate and fill in *THIS_CACHE with information about the prologue of
988 *THIS_FRAME. Do not do this is if *THIS_CACHE was already allocated.
989 Return a pointer to the current aarch64_prologue_cache in
990 *THIS_CACHE. */
07b287a0
MS
991
992static struct aarch64_prologue_cache *
8b61f75d 993aarch64_make_stub_cache (struct frame_info *this_frame, void **this_cache)
07b287a0 994{
07b287a0 995 struct aarch64_prologue_cache *cache;
8b61f75d
PL
996
997 if (*this_cache != NULL)
9a3c8263 998 return (struct aarch64_prologue_cache *) *this_cache;
07b287a0
MS
999
1000 cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
1001 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
8b61f75d 1002 *this_cache = cache;
07b287a0 1003
a70b8144 1004 try
02a2a705
PL
1005 {
1006 cache->prev_sp = get_frame_register_unsigned (this_frame,
1007 AARCH64_SP_REGNUM);
1008 cache->prev_pc = get_frame_pc (this_frame);
1009 cache->available_p = 1;
1010 }
230d2906 1011 catch (const gdb_exception_error &ex)
02a2a705
PL
1012 {
1013 if (ex.error != NOT_AVAILABLE_ERROR)
eedc3f4f 1014 throw;
02a2a705 1015 }
07b287a0
MS
1016
1017 return cache;
1018}
1019
02a2a705
PL
1020/* Implement the "stop_reason" frame_unwind method. */
1021
1022static enum unwind_stop_reason
1023aarch64_stub_frame_unwind_stop_reason (struct frame_info *this_frame,
1024 void **this_cache)
1025{
1026 struct aarch64_prologue_cache *cache
1027 = aarch64_make_stub_cache (this_frame, this_cache);
1028
1029 if (!cache->available_p)
1030 return UNWIND_UNAVAILABLE;
1031
1032 return UNWIND_NO_REASON;
1033}
1034
07b287a0
MS
1035/* Our frame ID for a stub frame is the current SP and LR. */
1036
1037static void
1038aarch64_stub_this_id (struct frame_info *this_frame,
1039 void **this_cache, struct frame_id *this_id)
1040{
8b61f75d
PL
1041 struct aarch64_prologue_cache *cache
1042 = aarch64_make_stub_cache (this_frame, this_cache);
07b287a0 1043
02a2a705
PL
1044 if (cache->available_p)
1045 *this_id = frame_id_build (cache->prev_sp, cache->prev_pc);
1046 else
1047 *this_id = frame_id_build_unavailable_stack (cache->prev_pc);
07b287a0
MS
1048}
1049
1050/* Implement the "sniffer" frame_unwind method. */
1051
1052static int
1053aarch64_stub_unwind_sniffer (const struct frame_unwind *self,
1054 struct frame_info *this_frame,
1055 void **this_prologue_cache)
1056{
1057 CORE_ADDR addr_in_block;
1058 gdb_byte dummy[4];
1059
1060 addr_in_block = get_frame_address_in_block (this_frame);
3e5d3a5a 1061 if (in_plt_section (addr_in_block)
07b287a0
MS
1062 /* We also use the stub winder if the target memory is unreadable
1063 to avoid having the prologue unwinder trying to read it. */
1064 || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
1065 return 1;
1066
1067 return 0;
1068}
1069
1070/* AArch64 stub unwinder. */
1071struct frame_unwind aarch64_stub_unwind =
1072{
1073 NORMAL_FRAME,
02a2a705 1074 aarch64_stub_frame_unwind_stop_reason,
07b287a0
MS
1075 aarch64_stub_this_id,
1076 aarch64_prologue_prev_register,
1077 NULL,
1078 aarch64_stub_unwind_sniffer
1079};
1080
1081/* Return the frame base address of *THIS_FRAME. */
1082
1083static CORE_ADDR
1084aarch64_normal_frame_base (struct frame_info *this_frame, void **this_cache)
1085{
7c8edfae
PL
1086 struct aarch64_prologue_cache *cache
1087 = aarch64_make_prologue_cache (this_frame, this_cache);
07b287a0
MS
1088
1089 return cache->prev_sp - cache->framesize;
1090}
1091
1092/* AArch64 default frame base information. */
1093struct frame_base aarch64_normal_base =
1094{
1095 &aarch64_prologue_unwind,
1096 aarch64_normal_frame_base,
1097 aarch64_normal_frame_base,
1098 aarch64_normal_frame_base
1099};
1100
07b287a0
MS
1101/* Return the value of the REGNUM register in the previous frame of
1102 *THIS_FRAME. */
1103
1104static struct value *
1105aarch64_dwarf2_prev_register (struct frame_info *this_frame,
1106 void **this_cache, int regnum)
1107{
11e1b75f 1108 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
07b287a0
MS
1109 CORE_ADDR lr;
1110
1111 switch (regnum)
1112 {
1113 case AARCH64_PC_REGNUM:
1114 lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
3d31bc39 1115 lr = aarch64_frame_unmask_lr (tdep, this_frame, lr);
07b287a0
MS
1116 return frame_unwind_got_constant (this_frame, regnum, lr);
1117
1118 default:
1119 internal_error (__FILE__, __LINE__,
1120 _("Unexpected register %d"), regnum);
1121 }
1122}
1123
11e1b75f
AH
1124static const unsigned char op_lit0 = DW_OP_lit0;
1125static const unsigned char op_lit1 = DW_OP_lit1;
1126
07b287a0
MS
1127/* Implement the "init_reg" dwarf2_frame_ops method. */
1128
1129static void
1130aarch64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1131 struct dwarf2_frame_state_reg *reg,
1132 struct frame_info *this_frame)
1133{
11e1b75f
AH
1134 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1135
07b287a0
MS
1136 switch (regnum)
1137 {
1138 case AARCH64_PC_REGNUM:
1139 reg->how = DWARF2_FRAME_REG_FN;
1140 reg->loc.fn = aarch64_dwarf2_prev_register;
11e1b75f
AH
1141 return;
1142
07b287a0
MS
1143 case AARCH64_SP_REGNUM:
1144 reg->how = DWARF2_FRAME_REG_CFA;
11e1b75f
AH
1145 return;
1146 }
1147
1148 /* Init pauth registers. */
1149 if (tdep->has_pauth ())
1150 {
1151 if (regnum == tdep->pauth_ra_state_regnum)
1152 {
1153 /* Initialize RA_STATE to zero. */
1154 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
1155 reg->loc.exp.start = &op_lit0;
1156 reg->loc.exp.len = 1;
1157 return;
1158 }
1159 else if (regnum == AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base)
1160 || regnum == AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base))
1161 {
1162 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1163 return;
1164 }
07b287a0
MS
1165 }
1166}
1167
11e1b75f
AH
1168/* Implement the execute_dwarf_cfa_vendor_op method. */
1169
1170static bool
1171aarch64_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
1172 struct dwarf2_frame_state *fs)
1173{
1174 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1175 struct dwarf2_frame_state_reg *ra_state;
1176
8fca4da0 1177 if (op == DW_CFA_AARCH64_negate_ra_state)
11e1b75f 1178 {
8fca4da0
AH
1179 /* On systems without pauth, treat as a nop. */
1180 if (!tdep->has_pauth ())
1181 return true;
1182
11e1b75f
AH
1183 /* Allocate RA_STATE column if it's not allocated yet. */
1184 fs->regs.alloc_regs (AARCH64_DWARF_PAUTH_RA_STATE + 1);
1185
1186 /* Toggle the status of RA_STATE between 0 and 1. */
1187 ra_state = &(fs->regs.reg[AARCH64_DWARF_PAUTH_RA_STATE]);
1188 ra_state->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
1189
1190 if (ra_state->loc.exp.start == nullptr
1191 || ra_state->loc.exp.start == &op_lit0)
1192 ra_state->loc.exp.start = &op_lit1;
1193 else
1194 ra_state->loc.exp.start = &op_lit0;
1195
1196 ra_state->loc.exp.len = 1;
1197
1198 return true;
1199 }
1200
1201 return false;
1202}
1203
5133a315
LM
1204/* Used for matching BRK instructions for AArch64. */
1205static constexpr uint32_t BRK_INSN_MASK = 0xffe0001f;
1206static constexpr uint32_t BRK_INSN_BASE = 0xd4200000;
1207
1208/* Implementation of gdbarch_program_breakpoint_here_p for aarch64. */
1209
1210static bool
1211aarch64_program_breakpoint_here_p (gdbarch *gdbarch, CORE_ADDR address)
1212{
1213 const uint32_t insn_len = 4;
1214 gdb_byte target_mem[4];
1215
1216 /* Enable the automatic memory restoration from breakpoints while
1217 we read the memory. Otherwise we may find temporary breakpoints, ones
1218 inserted by GDB, and flag them as permanent breakpoints. */
1219 scoped_restore restore_memory
1220 = make_scoped_restore_show_memory_breakpoints (0);
1221
1222 if (target_read_memory (address, target_mem, insn_len) == 0)
1223 {
1224 uint32_t insn =
1225 (uint32_t) extract_unsigned_integer (target_mem, insn_len,
1226 gdbarch_byte_order_for_code (gdbarch));
1227
1228 /* Check if INSN is a BRK instruction pattern. There are multiple choices
1229 of such instructions with different immediate values. Different OS'
1230 may use a different variation, but they have the same outcome. */
1231 return ((insn & BRK_INSN_MASK) == BRK_INSN_BASE);
1232 }
1233
1234 return false;
1235}
1236
07b287a0
MS
1237/* When arguments must be pushed onto the stack, they go on in reverse
1238 order. The code below implements a FILO (stack) to do this. */
1239
89055eaa 1240struct stack_item_t
07b287a0 1241{
c3c87445
YQ
1242 /* Value to pass on stack. It can be NULL if this item is for stack
1243 padding. */
7c543f7b 1244 const gdb_byte *data;
07b287a0
MS
1245
1246 /* Size in bytes of value to pass on stack. */
1247 int len;
89055eaa 1248};
07b287a0 1249
b907456c
AB
1250/* Implement the gdbarch type alignment method, overrides the generic
1251 alignment algorithm for anything that is aarch64 specific. */
07b287a0 1252
b907456c
AB
1253static ULONGEST
1254aarch64_type_align (gdbarch *gdbarch, struct type *t)
07b287a0 1255{
07b287a0 1256 t = check_typedef (t);
78134374 1257 if (t->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (t))
07b287a0 1258 {
b907456c
AB
1259 /* Use the natural alignment for vector types (the same for
1260 scalar type), but the maximum alignment is 128-bit. */
1261 if (TYPE_LENGTH (t) > 16)
1262 return 16;
238f2452 1263 else
b907456c 1264 return TYPE_LENGTH (t);
07b287a0 1265 }
b907456c
AB
1266
1267 /* Allow the common code to calculate the alignment. */
1268 return 0;
07b287a0
MS
1269}
1270
ea92689a
AH
1271/* Worker function for aapcs_is_vfp_call_or_return_candidate.
1272
1273 Return the number of register required, or -1 on failure.
1274
1275 When encountering a base element, if FUNDAMENTAL_TYPE is not set then set it
1276 to the element, else fail if the type of this element does not match the
1277 existing value. */
1278
1279static int
1280aapcs_is_vfp_call_or_return_candidate_1 (struct type *type,
1281 struct type **fundamental_type)
1282{
1283 if (type == nullptr)
1284 return -1;
1285
78134374 1286 switch (type->code ())
ea92689a
AH
1287 {
1288 case TYPE_CODE_FLT:
1289 if (TYPE_LENGTH (type) > 16)
1290 return -1;
1291
1292 if (*fundamental_type == nullptr)
1293 *fundamental_type = type;
1294 else if (TYPE_LENGTH (type) != TYPE_LENGTH (*fundamental_type)
78134374 1295 || type->code () != (*fundamental_type)->code ())
ea92689a
AH
1296 return -1;
1297
1298 return 1;
1299
1300 case TYPE_CODE_COMPLEX:
1301 {
1302 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1303 if (TYPE_LENGTH (target_type) > 16)
1304 return -1;
1305
1306 if (*fundamental_type == nullptr)
1307 *fundamental_type = target_type;
1308 else if (TYPE_LENGTH (target_type) != TYPE_LENGTH (*fundamental_type)
78134374 1309 || target_type->code () != (*fundamental_type)->code ())
ea92689a
AH
1310 return -1;
1311
1312 return 2;
1313 }
1314
1315 case TYPE_CODE_ARRAY:
1316 {
1317 if (TYPE_VECTOR (type))
1318 {
1319 if (TYPE_LENGTH (type) != 8 && TYPE_LENGTH (type) != 16)
1320 return -1;
1321
1322 if (*fundamental_type == nullptr)
1323 *fundamental_type = type;
1324 else if (TYPE_LENGTH (type) != TYPE_LENGTH (*fundamental_type)
78134374 1325 || type->code () != (*fundamental_type)->code ())
ea92689a
AH
1326 return -1;
1327
1328 return 1;
1329 }
1330 else
1331 {
1332 struct type *target_type = TYPE_TARGET_TYPE (type);
1333 int count = aapcs_is_vfp_call_or_return_candidate_1
1334 (target_type, fundamental_type);
1335
1336 if (count == -1)
1337 return count;
1338
d4718d5c 1339 count *= (TYPE_LENGTH (type) / TYPE_LENGTH (target_type));
ea92689a
AH
1340 return count;
1341 }
1342 }
1343
1344 case TYPE_CODE_STRUCT:
1345 case TYPE_CODE_UNION:
1346 {
1347 int count = 0;
1348
1f704f76 1349 for (int i = 0; i < type->num_fields (); i++)
ea92689a 1350 {
353229bf 1351 /* Ignore any static fields. */
ceacbf6e 1352 if (field_is_static (&type->field (i)))
353229bf
AH
1353 continue;
1354
ea92689a
AH
1355 struct type *member = check_typedef (TYPE_FIELD_TYPE (type, i));
1356
1357 int sub_count = aapcs_is_vfp_call_or_return_candidate_1
1358 (member, fundamental_type);
1359 if (sub_count == -1)
1360 return -1;
1361 count += sub_count;
1362 }
73021deb
AH
1363
1364 /* Ensure there is no padding between the fields (allowing for empty
1365 zero length structs) */
1366 int ftype_length = (*fundamental_type == nullptr)
1367 ? 0 : TYPE_LENGTH (*fundamental_type);
1368 if (count * ftype_length != TYPE_LENGTH (type))
1369 return -1;
1370
ea92689a
AH
1371 return count;
1372 }
1373
1374 default:
1375 break;
1376 }
1377
1378 return -1;
1379}
1380
1381/* Return true if an argument, whose type is described by TYPE, can be passed or
1382 returned in simd/fp registers, providing enough parameter passing registers
1383 are available. This is as described in the AAPCS64.
1384
1385 Upon successful return, *COUNT returns the number of needed registers,
1386 *FUNDAMENTAL_TYPE contains the type of those registers.
1387
1388 Candidate as per the AAPCS64 5.4.2.C is either a:
1389 - float.
1390 - short-vector.
1391 - HFA (Homogeneous Floating-point Aggregate, 4.3.5.1). A Composite type where
1392 all the members are floats and has at most 4 members.
1393 - HVA (Homogeneous Short-vector Aggregate, 4.3.5.2). A Composite type where
1394 all the members are short vectors and has at most 4 members.
1395 - Complex (7.1.1)
1396
1397 Note that HFAs and HVAs can include nested structures and arrays. */
1398
0e745c60 1399static bool
ea92689a
AH
1400aapcs_is_vfp_call_or_return_candidate (struct type *type, int *count,
1401 struct type **fundamental_type)
1402{
1403 if (type == nullptr)
1404 return false;
1405
1406 *fundamental_type = nullptr;
1407
1408 int ag_count = aapcs_is_vfp_call_or_return_candidate_1 (type,
1409 fundamental_type);
1410
1411 if (ag_count > 0 && ag_count <= HA_MAX_NUM_FLDS)
1412 {
1413 *count = ag_count;
1414 return true;
1415 }
1416 else
1417 return false;
1418}
1419
07b287a0
MS
1420/* AArch64 function call information structure. */
1421struct aarch64_call_info
1422{
1423 /* the current argument number. */
89055eaa 1424 unsigned argnum = 0;
07b287a0
MS
1425
1426 /* The next general purpose register number, equivalent to NGRN as
1427 described in the AArch64 Procedure Call Standard. */
89055eaa 1428 unsigned ngrn = 0;
07b287a0
MS
1429
1430 /* The next SIMD and floating point register number, equivalent to
1431 NSRN as described in the AArch64 Procedure Call Standard. */
89055eaa 1432 unsigned nsrn = 0;
07b287a0
MS
1433
1434 /* The next stacked argument address, equivalent to NSAA as
1435 described in the AArch64 Procedure Call Standard. */
89055eaa 1436 unsigned nsaa = 0;
07b287a0
MS
1437
1438 /* Stack item vector. */
89055eaa 1439 std::vector<stack_item_t> si;
07b287a0
MS
1440};
1441
1442/* Pass a value in a sequence of consecutive X registers. The caller
30baf67b 1443 is responsible for ensuring sufficient registers are available. */
07b287a0
MS
1444
1445static void
1446pass_in_x (struct gdbarch *gdbarch, struct regcache *regcache,
1447 struct aarch64_call_info *info, struct type *type,
8e80f9d1 1448 struct value *arg)
07b287a0
MS
1449{
1450 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1451 int len = TYPE_LENGTH (type);
78134374 1452 enum type_code typecode = type->code ();
07b287a0 1453 int regnum = AARCH64_X0_REGNUM + info->ngrn;
8e80f9d1 1454 const bfd_byte *buf = value_contents (arg);
07b287a0
MS
1455
1456 info->argnum++;
1457
1458 while (len > 0)
1459 {
1460 int partial_len = len < X_REGISTER_SIZE ? len : X_REGISTER_SIZE;
1461 CORE_ADDR regval = extract_unsigned_integer (buf, partial_len,
1462 byte_order);
1463
1464
1465 /* Adjust sub-word struct/union args when big-endian. */
1466 if (byte_order == BFD_ENDIAN_BIG
1467 && partial_len < X_REGISTER_SIZE
1468 && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
1469 regval <<= ((X_REGISTER_SIZE - partial_len) * TARGET_CHAR_BIT);
1470
1471 if (aarch64_debug)
b277c936
PL
1472 {
1473 debug_printf ("arg %d in %s = 0x%s\n", info->argnum,
1474 gdbarch_register_name (gdbarch, regnum),
1475 phex (regval, X_REGISTER_SIZE));
1476 }
07b287a0
MS
1477 regcache_cooked_write_unsigned (regcache, regnum, regval);
1478 len -= partial_len;
1479 buf += partial_len;
1480 regnum++;
1481 }
1482}
1483
1484/* Attempt to marshall a value in a V register. Return 1 if
1485 successful, or 0 if insufficient registers are available. This
1486 function, unlike the equivalent pass_in_x() function does not
1487 handle arguments spread across multiple registers. */
1488
1489static int
1490pass_in_v (struct gdbarch *gdbarch,
1491 struct regcache *regcache,
1492 struct aarch64_call_info *info,
0735fddd 1493 int len, const bfd_byte *buf)
07b287a0
MS
1494{
1495 if (info->nsrn < 8)
1496 {
07b287a0 1497 int regnum = AARCH64_V0_REGNUM + info->nsrn;
3ff2c72e
AH
1498 /* Enough space for a full vector register. */
1499 gdb_byte reg[register_size (gdbarch, regnum)];
1500 gdb_assert (len <= sizeof (reg));
07b287a0
MS
1501
1502 info->argnum++;
1503 info->nsrn++;
1504
0735fddd
YQ
1505 memset (reg, 0, sizeof (reg));
1506 /* PCS C.1, the argument is allocated to the least significant
1507 bits of V register. */
1508 memcpy (reg, buf, len);
b66f5587 1509 regcache->cooked_write (regnum, reg);
0735fddd 1510
07b287a0 1511 if (aarch64_debug)
b277c936
PL
1512 {
1513 debug_printf ("arg %d in %s\n", info->argnum,
1514 gdbarch_register_name (gdbarch, regnum));
1515 }
07b287a0
MS
1516 return 1;
1517 }
1518 info->nsrn = 8;
1519 return 0;
1520}
1521
1522/* Marshall an argument onto the stack. */
1523
1524static void
1525pass_on_stack (struct aarch64_call_info *info, struct type *type,
8e80f9d1 1526 struct value *arg)
07b287a0 1527{
8e80f9d1 1528 const bfd_byte *buf = value_contents (arg);
07b287a0
MS
1529 int len = TYPE_LENGTH (type);
1530 int align;
1531 stack_item_t item;
1532
1533 info->argnum++;
1534
b907456c 1535 align = type_align (type);
07b287a0
MS
1536
1537 /* PCS C.17 Stack should be aligned to the larger of 8 bytes or the
1538 Natural alignment of the argument's type. */
1539 align = align_up (align, 8);
1540
1541 /* The AArch64 PCS requires at most doubleword alignment. */
1542 if (align > 16)
1543 align = 16;
1544
1545 if (aarch64_debug)
b277c936
PL
1546 {
1547 debug_printf ("arg %d len=%d @ sp + %d\n", info->argnum, len,
1548 info->nsaa);
1549 }
07b287a0
MS
1550
1551 item.len = len;
1552 item.data = buf;
89055eaa 1553 info->si.push_back (item);
07b287a0
MS
1554
1555 info->nsaa += len;
1556 if (info->nsaa & (align - 1))
1557 {
1558 /* Push stack alignment padding. */
1559 int pad = align - (info->nsaa & (align - 1));
1560
1561 item.len = pad;
c3c87445 1562 item.data = NULL;
07b287a0 1563
89055eaa 1564 info->si.push_back (item);
07b287a0
MS
1565 info->nsaa += pad;
1566 }
1567}
1568
1569/* Marshall an argument into a sequence of one or more consecutive X
1570 registers or, if insufficient X registers are available then onto
1571 the stack. */
1572
1573static void
1574pass_in_x_or_stack (struct gdbarch *gdbarch, struct regcache *regcache,
1575 struct aarch64_call_info *info, struct type *type,
8e80f9d1 1576 struct value *arg)
07b287a0
MS
1577{
1578 int len = TYPE_LENGTH (type);
1579 int nregs = (len + X_REGISTER_SIZE - 1) / X_REGISTER_SIZE;
1580
1581 /* PCS C.13 - Pass in registers if we have enough spare */
1582 if (info->ngrn + nregs <= 8)
1583 {
8e80f9d1 1584 pass_in_x (gdbarch, regcache, info, type, arg);
07b287a0
MS
1585 info->ngrn += nregs;
1586 }
1587 else
1588 {
1589 info->ngrn = 8;
8e80f9d1 1590 pass_on_stack (info, type, arg);
07b287a0
MS
1591 }
1592}
1593
0e745c60
AH
1594/* Pass a value, which is of type arg_type, in a V register. Assumes value is a
1595 aapcs_is_vfp_call_or_return_candidate and there are enough spare V
1596 registers. A return value of false is an error state as the value will have
1597 been partially passed to the stack. */
1598static bool
1599pass_in_v_vfp_candidate (struct gdbarch *gdbarch, struct regcache *regcache,
1600 struct aarch64_call_info *info, struct type *arg_type,
1601 struct value *arg)
07b287a0 1602{
78134374 1603 switch (arg_type->code ())
0e745c60
AH
1604 {
1605 case TYPE_CODE_FLT:
1606 return pass_in_v (gdbarch, regcache, info, TYPE_LENGTH (arg_type),
1607 value_contents (arg));
1608 break;
1609
1610 case TYPE_CODE_COMPLEX:
1611 {
1612 const bfd_byte *buf = value_contents (arg);
1613 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
1614
1615 if (!pass_in_v (gdbarch, regcache, info, TYPE_LENGTH (target_type),
1616 buf))
1617 return false;
1618
1619 return pass_in_v (gdbarch, regcache, info, TYPE_LENGTH (target_type),
1620 buf + TYPE_LENGTH (target_type));
1621 }
1622
1623 case TYPE_CODE_ARRAY:
1624 if (TYPE_VECTOR (arg_type))
1625 return pass_in_v (gdbarch, regcache, info, TYPE_LENGTH (arg_type),
1626 value_contents (arg));
1627 /* fall through. */
1628
1629 case TYPE_CODE_STRUCT:
1630 case TYPE_CODE_UNION:
1f704f76 1631 for (int i = 0; i < arg_type->num_fields (); i++)
0e745c60 1632 {
353229bf 1633 /* Don't include static fields. */
ceacbf6e 1634 if (field_is_static (&arg_type->field (i)))
353229bf
AH
1635 continue;
1636
0e745c60
AH
1637 struct value *field = value_primitive_field (arg, 0, i, arg_type);
1638 struct type *field_type = check_typedef (value_type (field));
1639
1640 if (!pass_in_v_vfp_candidate (gdbarch, regcache, info, field_type,
1641 field))
1642 return false;
1643 }
1644 return true;
1645
1646 default:
1647 return false;
1648 }
07b287a0
MS
1649}
1650
1651/* Implement the "push_dummy_call" gdbarch method. */
1652
1653static CORE_ADDR
1654aarch64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1655 struct regcache *regcache, CORE_ADDR bp_addr,
1656 int nargs,
cf84fa6b
AH
1657 struct value **args, CORE_ADDR sp,
1658 function_call_return_method return_method,
07b287a0
MS
1659 CORE_ADDR struct_addr)
1660{
07b287a0 1661 int argnum;
07b287a0 1662 struct aarch64_call_info info;
07b287a0 1663
07b287a0
MS
1664 /* We need to know what the type of the called function is in order
1665 to determine the number of named/anonymous arguments for the
1666 actual argument placement, and the return type in order to handle
1667 return value correctly.
1668
1669 The generic code above us views the decision of return in memory
1670 or return in registers as a two stage processes. The language
1671 handler is consulted first and may decide to return in memory (eg
1672 class with copy constructor returned by value), this will cause
1673 the generic code to allocate space AND insert an initial leading
1674 argument.
1675
1676 If the language code does not decide to pass in memory then the
1677 target code is consulted.
1678
1679 If the language code decides to pass in memory we want to move
1680 the pointer inserted as the initial argument from the argument
1681 list and into X8, the conventional AArch64 struct return pointer
38a72da0 1682 register. */
07b287a0
MS
1683
1684 /* Set the return address. For the AArch64, the return breakpoint
1685 is always at BP_ADDR. */
1686 regcache_cooked_write_unsigned (regcache, AARCH64_LR_REGNUM, bp_addr);
1687
38a72da0
AH
1688 /* If we were given an initial argument for the return slot, lose it. */
1689 if (return_method == return_method_hidden_param)
07b287a0
MS
1690 {
1691 args++;
1692 nargs--;
1693 }
1694
1695 /* The struct_return pointer occupies X8. */
38a72da0 1696 if (return_method != return_method_normal)
07b287a0
MS
1697 {
1698 if (aarch64_debug)
b277c936
PL
1699 {
1700 debug_printf ("struct return in %s = 0x%s\n",
1701 gdbarch_register_name (gdbarch,
1702 AARCH64_STRUCT_RETURN_REGNUM),
1703 paddress (gdbarch, struct_addr));
1704 }
07b287a0
MS
1705 regcache_cooked_write_unsigned (regcache, AARCH64_STRUCT_RETURN_REGNUM,
1706 struct_addr);
1707 }
1708
1709 for (argnum = 0; argnum < nargs; argnum++)
1710 {
1711 struct value *arg = args[argnum];
0e745c60
AH
1712 struct type *arg_type, *fundamental_type;
1713 int len, elements;
07b287a0
MS
1714
1715 arg_type = check_typedef (value_type (arg));
1716 len = TYPE_LENGTH (arg_type);
1717
0e745c60
AH
1718 /* If arg can be passed in v registers as per the AAPCS64, then do so if
1719 if there are enough spare registers. */
1720 if (aapcs_is_vfp_call_or_return_candidate (arg_type, &elements,
1721 &fundamental_type))
1722 {
1723 if (info.nsrn + elements <= 8)
1724 {
1725 /* We know that we have sufficient registers available therefore
1726 this will never need to fallback to the stack. */
1727 if (!pass_in_v_vfp_candidate (gdbarch, regcache, &info, arg_type,
1728 arg))
1729 gdb_assert_not_reached ("Failed to push args");
1730 }
1731 else
1732 {
1733 info.nsrn = 8;
1734 pass_on_stack (&info, arg_type, arg);
1735 }
1736 continue;
1737 }
1738
78134374 1739 switch (arg_type->code ())
07b287a0
MS
1740 {
1741 case TYPE_CODE_INT:
1742 case TYPE_CODE_BOOL:
1743 case TYPE_CODE_CHAR:
1744 case TYPE_CODE_RANGE:
1745 case TYPE_CODE_ENUM:
1746 if (len < 4)
1747 {
1748 /* Promote to 32 bit integer. */
1749 if (TYPE_UNSIGNED (arg_type))
1750 arg_type = builtin_type (gdbarch)->builtin_uint32;
1751 else
1752 arg_type = builtin_type (gdbarch)->builtin_int32;
1753 arg = value_cast (arg_type, arg);
1754 }
8e80f9d1 1755 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
07b287a0
MS
1756 break;
1757
07b287a0
MS
1758 case TYPE_CODE_STRUCT:
1759 case TYPE_CODE_ARRAY:
1760 case TYPE_CODE_UNION:
0e745c60 1761 if (len > 16)
07b287a0
MS
1762 {
1763 /* PCS B.7 Aggregates larger than 16 bytes are passed by
1764 invisible reference. */
1765
1766 /* Allocate aligned storage. */
1767 sp = align_down (sp - len, 16);
1768
1769 /* Write the real data into the stack. */
1770 write_memory (sp, value_contents (arg), len);
1771
1772 /* Construct the indirection. */
1773 arg_type = lookup_pointer_type (arg_type);
1774 arg = value_from_pointer (arg_type, sp);
8e80f9d1 1775 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
07b287a0
MS
1776 }
1777 else
1778 /* PCS C.15 / C.18 multiple values pass. */
8e80f9d1 1779 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
07b287a0
MS
1780 break;
1781
1782 default:
8e80f9d1 1783 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
07b287a0
MS
1784 break;
1785 }
1786 }
1787
1788 /* Make sure stack retains 16 byte alignment. */
1789 if (info.nsaa & 15)
1790 sp -= 16 - (info.nsaa & 15);
1791
89055eaa 1792 while (!info.si.empty ())
07b287a0 1793 {
89055eaa 1794 const stack_item_t &si = info.si.back ();
07b287a0 1795
89055eaa
TT
1796 sp -= si.len;
1797 if (si.data != NULL)
1798 write_memory (sp, si.data, si.len);
1799 info.si.pop_back ();
07b287a0
MS
1800 }
1801
07b287a0
MS
1802 /* Finally, update the SP register. */
1803 regcache_cooked_write_unsigned (regcache, AARCH64_SP_REGNUM, sp);
1804
1805 return sp;
1806}
1807
1808/* Implement the "frame_align" gdbarch method. */
1809
1810static CORE_ADDR
1811aarch64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
1812{
1813 /* Align the stack to sixteen bytes. */
1814 return sp & ~(CORE_ADDR) 15;
1815}
1816
1817/* Return the type for an AdvSISD Q register. */
1818
1819static struct type *
1820aarch64_vnq_type (struct gdbarch *gdbarch)
1821{
1822 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1823
1824 if (tdep->vnq_type == NULL)
1825 {
1826 struct type *t;
1827 struct type *elem;
1828
1829 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnq",
1830 TYPE_CODE_UNION);
1831
1832 elem = builtin_type (gdbarch)->builtin_uint128;
1833 append_composite_type_field (t, "u", elem);
1834
1835 elem = builtin_type (gdbarch)->builtin_int128;
1836 append_composite_type_field (t, "s", elem);
1837
1838 tdep->vnq_type = t;
1839 }
1840
1841 return tdep->vnq_type;
1842}
1843
1844/* Return the type for an AdvSISD D register. */
1845
1846static struct type *
1847aarch64_vnd_type (struct gdbarch *gdbarch)
1848{
1849 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1850
1851 if (tdep->vnd_type == NULL)
1852 {
1853 struct type *t;
1854 struct type *elem;
1855
1856 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnd",
1857 TYPE_CODE_UNION);
1858
1859 elem = builtin_type (gdbarch)->builtin_double;
1860 append_composite_type_field (t, "f", elem);
1861
1862 elem = builtin_type (gdbarch)->builtin_uint64;
1863 append_composite_type_field (t, "u", elem);
1864
1865 elem = builtin_type (gdbarch)->builtin_int64;
1866 append_composite_type_field (t, "s", elem);
1867
1868 tdep->vnd_type = t;
1869 }
1870
1871 return tdep->vnd_type;
1872}
1873
1874/* Return the type for an AdvSISD S register. */
1875
1876static struct type *
1877aarch64_vns_type (struct gdbarch *gdbarch)
1878{
1879 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1880
1881 if (tdep->vns_type == NULL)
1882 {
1883 struct type *t;
1884 struct type *elem;
1885
1886 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vns",
1887 TYPE_CODE_UNION);
1888
1889 elem = builtin_type (gdbarch)->builtin_float;
1890 append_composite_type_field (t, "f", elem);
1891
1892 elem = builtin_type (gdbarch)->builtin_uint32;
1893 append_composite_type_field (t, "u", elem);
1894
1895 elem = builtin_type (gdbarch)->builtin_int32;
1896 append_composite_type_field (t, "s", elem);
1897
1898 tdep->vns_type = t;
1899 }
1900
1901 return tdep->vns_type;
1902}
1903
1904/* Return the type for an AdvSISD H register. */
1905
1906static struct type *
1907aarch64_vnh_type (struct gdbarch *gdbarch)
1908{
1909 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1910
1911 if (tdep->vnh_type == NULL)
1912 {
1913 struct type *t;
1914 struct type *elem;
1915
1916 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnh",
1917 TYPE_CODE_UNION);
1918
a6d0f249
AH
1919 elem = builtin_type (gdbarch)->builtin_half;
1920 append_composite_type_field (t, "f", elem);
1921
07b287a0
MS
1922 elem = builtin_type (gdbarch)->builtin_uint16;
1923 append_composite_type_field (t, "u", elem);
1924
1925 elem = builtin_type (gdbarch)->builtin_int16;
1926 append_composite_type_field (t, "s", elem);
1927
1928 tdep->vnh_type = t;
1929 }
1930
1931 return tdep->vnh_type;
1932}
1933
1934/* Return the type for an AdvSISD B register. */
1935
1936static struct type *
1937aarch64_vnb_type (struct gdbarch *gdbarch)
1938{
1939 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1940
1941 if (tdep->vnb_type == NULL)
1942 {
1943 struct type *t;
1944 struct type *elem;
1945
1946 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnb",
1947 TYPE_CODE_UNION);
1948
1949 elem = builtin_type (gdbarch)->builtin_uint8;
1950 append_composite_type_field (t, "u", elem);
1951
1952 elem = builtin_type (gdbarch)->builtin_int8;
1953 append_composite_type_field (t, "s", elem);
1954
1955 tdep->vnb_type = t;
1956 }
1957
1958 return tdep->vnb_type;
1959}
1960
63bad7b6
AH
1961/* Return the type for an AdvSISD V register. */
1962
1963static struct type *
1964aarch64_vnv_type (struct gdbarch *gdbarch)
1965{
1966 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1967
1968 if (tdep->vnv_type == NULL)
1969 {
09624f1f 1970 /* The other AArch64 pseudo registers (Q,D,H,S,B) refer to a single value
bffa1015
AH
1971 slice from the non-pseudo vector registers. However NEON V registers
1972 are always vector registers, and need constructing as such. */
1973 const struct builtin_type *bt = builtin_type (gdbarch);
1974
63bad7b6
AH
1975 struct type *t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnv",
1976 TYPE_CODE_UNION);
1977
bffa1015
AH
1978 struct type *sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vnd",
1979 TYPE_CODE_UNION);
1980 append_composite_type_field (sub, "f",
1981 init_vector_type (bt->builtin_double, 2));
1982 append_composite_type_field (sub, "u",
1983 init_vector_type (bt->builtin_uint64, 2));
1984 append_composite_type_field (sub, "s",
1985 init_vector_type (bt->builtin_int64, 2));
1986 append_composite_type_field (t, "d", sub);
1987
1988 sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vns",
1989 TYPE_CODE_UNION);
1990 append_composite_type_field (sub, "f",
1991 init_vector_type (bt->builtin_float, 4));
1992 append_composite_type_field (sub, "u",
1993 init_vector_type (bt->builtin_uint32, 4));
1994 append_composite_type_field (sub, "s",
1995 init_vector_type (bt->builtin_int32, 4));
1996 append_composite_type_field (t, "s", sub);
1997
1998 sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vnh",
1999 TYPE_CODE_UNION);
a6d0f249
AH
2000 append_composite_type_field (sub, "f",
2001 init_vector_type (bt->builtin_half, 8));
bffa1015
AH
2002 append_composite_type_field (sub, "u",
2003 init_vector_type (bt->builtin_uint16, 8));
2004 append_composite_type_field (sub, "s",
2005 init_vector_type (bt->builtin_int16, 8));
2006 append_composite_type_field (t, "h", sub);
2007
2008 sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vnb",
2009 TYPE_CODE_UNION);
2010 append_composite_type_field (sub, "u",
2011 init_vector_type (bt->builtin_uint8, 16));
2012 append_composite_type_field (sub, "s",
2013 init_vector_type (bt->builtin_int8, 16));
2014 append_composite_type_field (t, "b", sub);
2015
2016 sub = arch_composite_type (gdbarch, "__gdb_builtin_type_vnq",
2017 TYPE_CODE_UNION);
2018 append_composite_type_field (sub, "u",
2019 init_vector_type (bt->builtin_uint128, 1));
2020 append_composite_type_field (sub, "s",
2021 init_vector_type (bt->builtin_int128, 1));
2022 append_composite_type_field (t, "q", sub);
63bad7b6
AH
2023
2024 tdep->vnv_type = t;
2025 }
2026
2027 return tdep->vnv_type;
2028}
2029
07b287a0
MS
2030/* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
2031
2032static int
2033aarch64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
2034{
34dcc7cf
AH
2035 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2036
07b287a0
MS
2037 if (reg >= AARCH64_DWARF_X0 && reg <= AARCH64_DWARF_X0 + 30)
2038 return AARCH64_X0_REGNUM + reg - AARCH64_DWARF_X0;
2039
2040 if (reg == AARCH64_DWARF_SP)
2041 return AARCH64_SP_REGNUM;
2042
2043 if (reg >= AARCH64_DWARF_V0 && reg <= AARCH64_DWARF_V0 + 31)
2044 return AARCH64_V0_REGNUM + reg - AARCH64_DWARF_V0;
2045
65d4cada
AH
2046 if (reg == AARCH64_DWARF_SVE_VG)
2047 return AARCH64_SVE_VG_REGNUM;
2048
2049 if (reg == AARCH64_DWARF_SVE_FFR)
2050 return AARCH64_SVE_FFR_REGNUM;
2051
2052 if (reg >= AARCH64_DWARF_SVE_P0 && reg <= AARCH64_DWARF_SVE_P0 + 15)
2053 return AARCH64_SVE_P0_REGNUM + reg - AARCH64_DWARF_SVE_P0;
2054
2055 if (reg >= AARCH64_DWARF_SVE_Z0 && reg <= AARCH64_DWARF_SVE_Z0 + 15)
2056 return AARCH64_SVE_Z0_REGNUM + reg - AARCH64_DWARF_SVE_Z0;
2057
34dcc7cf
AH
2058 if (tdep->has_pauth ())
2059 {
2060 if (reg >= AARCH64_DWARF_PAUTH_DMASK && reg <= AARCH64_DWARF_PAUTH_CMASK)
2061 return tdep->pauth_reg_base + reg - AARCH64_DWARF_PAUTH_DMASK;
2062
2063 if (reg == AARCH64_DWARF_PAUTH_RA_STATE)
2064 return tdep->pauth_ra_state_regnum;
2065 }
2066
07b287a0
MS
2067 return -1;
2068}
07b287a0
MS
2069
2070/* Implement the "print_insn" gdbarch method. */
2071
2072static int
2073aarch64_gdb_print_insn (bfd_vma memaddr, disassemble_info *info)
2074{
2075 info->symbols = NULL;
6394c606 2076 return default_print_insn (memaddr, info);
07b287a0
MS
2077}
2078
2079/* AArch64 BRK software debug mode instruction.
2080 Note that AArch64 code is always little-endian.
2081 1101.0100.0010.0000.0000.0000.0000.0000 = 0xd4200000. */
04180708 2082constexpr gdb_byte aarch64_default_breakpoint[] = {0x00, 0x00, 0x20, 0xd4};
07b287a0 2083
04180708 2084typedef BP_MANIPULATION (aarch64_default_breakpoint) aarch64_breakpoint;
07b287a0
MS
2085
2086/* Extract from an array REGS containing the (raw) register state a
2087 function return value of type TYPE, and copy that, in virtual
2088 format, into VALBUF. */
2089
2090static void
2091aarch64_extract_return_value (struct type *type, struct regcache *regs,
2092 gdb_byte *valbuf)
2093{
ac7936df 2094 struct gdbarch *gdbarch = regs->arch ();
07b287a0 2095 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4f4aedeb
AH
2096 int elements;
2097 struct type *fundamental_type;
07b287a0 2098
4f4aedeb
AH
2099 if (aapcs_is_vfp_call_or_return_candidate (type, &elements,
2100 &fundamental_type))
07b287a0 2101 {
4f4aedeb
AH
2102 int len = TYPE_LENGTH (fundamental_type);
2103
2104 for (int i = 0; i < elements; i++)
2105 {
2106 int regno = AARCH64_V0_REGNUM + i;
3ff2c72e
AH
2107 /* Enough space for a full vector register. */
2108 gdb_byte buf[register_size (gdbarch, regno)];
2109 gdb_assert (len <= sizeof (buf));
4f4aedeb
AH
2110
2111 if (aarch64_debug)
2112 {
2113 debug_printf ("read HFA or HVA return value element %d from %s\n",
2114 i + 1,
2115 gdbarch_register_name (gdbarch, regno));
2116 }
2117 regs->cooked_read (regno, buf);
07b287a0 2118
4f4aedeb
AH
2119 memcpy (valbuf, buf, len);
2120 valbuf += len;
2121 }
07b287a0 2122 }
78134374
SM
2123 else if (type->code () == TYPE_CODE_INT
2124 || type->code () == TYPE_CODE_CHAR
2125 || type->code () == TYPE_CODE_BOOL
2126 || type->code () == TYPE_CODE_PTR
aa006118 2127 || TYPE_IS_REFERENCE (type)
78134374 2128 || type->code () == TYPE_CODE_ENUM)
07b287a0 2129 {
6471e7d2 2130 /* If the type is a plain integer, then the access is
07b287a0
MS
2131 straight-forward. Otherwise we have to play around a bit
2132 more. */
2133 int len = TYPE_LENGTH (type);
2134 int regno = AARCH64_X0_REGNUM;
2135 ULONGEST tmp;
2136
2137 while (len > 0)
2138 {
2139 /* By using store_unsigned_integer we avoid having to do
2140 anything special for small big-endian values. */
2141 regcache_cooked_read_unsigned (regs, regno++, &tmp);
2142 store_unsigned_integer (valbuf,
2143 (len > X_REGISTER_SIZE
2144 ? X_REGISTER_SIZE : len), byte_order, tmp);
2145 len -= X_REGISTER_SIZE;
2146 valbuf += X_REGISTER_SIZE;
2147 }
2148 }
07b287a0
MS
2149 else
2150 {
2151 /* For a structure or union the behaviour is as if the value had
2152 been stored to word-aligned memory and then loaded into
2153 registers with 64-bit load instruction(s). */
2154 int len = TYPE_LENGTH (type);
2155 int regno = AARCH64_X0_REGNUM;
2156 bfd_byte buf[X_REGISTER_SIZE];
2157
2158 while (len > 0)
2159 {
dca08e1f 2160 regs->cooked_read (regno++, buf);
07b287a0
MS
2161 memcpy (valbuf, buf, len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
2162 len -= X_REGISTER_SIZE;
2163 valbuf += X_REGISTER_SIZE;
2164 }
2165 }
2166}
2167
2168
2169/* Will a function return an aggregate type in memory or in a
2170 register? Return 0 if an aggregate type can be returned in a
2171 register, 1 if it must be returned in memory. */
2172
2173static int
2174aarch64_return_in_memory (struct gdbarch *gdbarch, struct type *type)
2175{
f168693b 2176 type = check_typedef (type);
4f4aedeb
AH
2177 int elements;
2178 struct type *fundamental_type;
07b287a0 2179
4f4aedeb
AH
2180 if (aapcs_is_vfp_call_or_return_candidate (type, &elements,
2181 &fundamental_type))
07b287a0 2182 {
cd635f74
YQ
2183 /* v0-v7 are used to return values and one register is allocated
2184 for one member. However, HFA or HVA has at most four members. */
07b287a0
MS
2185 return 0;
2186 }
2187
2188 if (TYPE_LENGTH (type) > 16)
2189 {
2190 /* PCS B.6 Aggregates larger than 16 bytes are passed by
2191 invisible reference. */
2192
2193 return 1;
2194 }
2195
2196 return 0;
2197}
2198
2199/* Write into appropriate registers a function return value of type
2200 TYPE, given in virtual format. */
2201
2202static void
2203aarch64_store_return_value (struct type *type, struct regcache *regs,
2204 const gdb_byte *valbuf)
2205{
ac7936df 2206 struct gdbarch *gdbarch = regs->arch ();
07b287a0 2207 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4f4aedeb
AH
2208 int elements;
2209 struct type *fundamental_type;
07b287a0 2210
4f4aedeb
AH
2211 if (aapcs_is_vfp_call_or_return_candidate (type, &elements,
2212 &fundamental_type))
07b287a0 2213 {
4f4aedeb
AH
2214 int len = TYPE_LENGTH (fundamental_type);
2215
2216 for (int i = 0; i < elements; i++)
2217 {
2218 int regno = AARCH64_V0_REGNUM + i;
3ff2c72e
AH
2219 /* Enough space for a full vector register. */
2220 gdb_byte tmpbuf[register_size (gdbarch, regno)];
2221 gdb_assert (len <= sizeof (tmpbuf));
4f4aedeb
AH
2222
2223 if (aarch64_debug)
2224 {
2225 debug_printf ("write HFA or HVA return value element %d to %s\n",
2226 i + 1,
2227 gdbarch_register_name (gdbarch, regno));
2228 }
07b287a0 2229
4f4aedeb
AH
2230 memcpy (tmpbuf, valbuf,
2231 len > V_REGISTER_SIZE ? V_REGISTER_SIZE : len);
2232 regs->cooked_write (regno, tmpbuf);
2233 valbuf += len;
2234 }
07b287a0 2235 }
78134374
SM
2236 else if (type->code () == TYPE_CODE_INT
2237 || type->code () == TYPE_CODE_CHAR
2238 || type->code () == TYPE_CODE_BOOL
2239 || type->code () == TYPE_CODE_PTR
aa006118 2240 || TYPE_IS_REFERENCE (type)
78134374 2241 || type->code () == TYPE_CODE_ENUM)
07b287a0
MS
2242 {
2243 if (TYPE_LENGTH (type) <= X_REGISTER_SIZE)
2244 {
2245 /* Values of one word or less are zero/sign-extended and
2246 returned in r0. */
2247 bfd_byte tmpbuf[X_REGISTER_SIZE];
2248 LONGEST val = unpack_long (type, valbuf);
2249
2250 store_signed_integer (tmpbuf, X_REGISTER_SIZE, byte_order, val);
b66f5587 2251 regs->cooked_write (AARCH64_X0_REGNUM, tmpbuf);
07b287a0
MS
2252 }
2253 else
2254 {
2255 /* Integral values greater than one word are stored in
2256 consecutive registers starting with r0. This will always
2257 be a multiple of the regiser size. */
2258 int len = TYPE_LENGTH (type);
2259 int regno = AARCH64_X0_REGNUM;
2260
2261 while (len > 0)
2262 {
b66f5587 2263 regs->cooked_write (regno++, valbuf);
07b287a0
MS
2264 len -= X_REGISTER_SIZE;
2265 valbuf += X_REGISTER_SIZE;
2266 }
2267 }
2268 }
07b287a0
MS
2269 else
2270 {
2271 /* For a structure or union the behaviour is as if the value had
2272 been stored to word-aligned memory and then loaded into
2273 registers with 64-bit load instruction(s). */
2274 int len = TYPE_LENGTH (type);
2275 int regno = AARCH64_X0_REGNUM;
2276 bfd_byte tmpbuf[X_REGISTER_SIZE];
2277
2278 while (len > 0)
2279 {
2280 memcpy (tmpbuf, valbuf,
2281 len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
b66f5587 2282 regs->cooked_write (regno++, tmpbuf);
07b287a0
MS
2283 len -= X_REGISTER_SIZE;
2284 valbuf += X_REGISTER_SIZE;
2285 }
2286 }
2287}
2288
2289/* Implement the "return_value" gdbarch method. */
2290
2291static enum return_value_convention
2292aarch64_return_value (struct gdbarch *gdbarch, struct value *func_value,
2293 struct type *valtype, struct regcache *regcache,
2294 gdb_byte *readbuf, const gdb_byte *writebuf)
2295{
07b287a0 2296
78134374
SM
2297 if (valtype->code () == TYPE_CODE_STRUCT
2298 || valtype->code () == TYPE_CODE_UNION
2299 || valtype->code () == TYPE_CODE_ARRAY)
07b287a0
MS
2300 {
2301 if (aarch64_return_in_memory (gdbarch, valtype))
2302 {
2303 if (aarch64_debug)
b277c936 2304 debug_printf ("return value in memory\n");
07b287a0
MS
2305 return RETURN_VALUE_STRUCT_CONVENTION;
2306 }
2307 }
2308
2309 if (writebuf)
2310 aarch64_store_return_value (valtype, regcache, writebuf);
2311
2312 if (readbuf)
2313 aarch64_extract_return_value (valtype, regcache, readbuf);
2314
2315 if (aarch64_debug)
b277c936 2316 debug_printf ("return value in registers\n");
07b287a0
MS
2317
2318 return RETURN_VALUE_REGISTER_CONVENTION;
2319}
2320
2321/* Implement the "get_longjmp_target" gdbarch method. */
2322
2323static int
2324aarch64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
2325{
2326 CORE_ADDR jb_addr;
2327 gdb_byte buf[X_REGISTER_SIZE];
2328 struct gdbarch *gdbarch = get_frame_arch (frame);
2329 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2330 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2331
2332 jb_addr = get_frame_register_unsigned (frame, AARCH64_X0_REGNUM);
2333
2334 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
2335 X_REGISTER_SIZE))
2336 return 0;
2337
2338 *pc = extract_unsigned_integer (buf, X_REGISTER_SIZE, byte_order);
2339 return 1;
2340}
ea873d8e
PL
2341
2342/* Implement the "gen_return_address" gdbarch method. */
2343
2344static void
2345aarch64_gen_return_address (struct gdbarch *gdbarch,
2346 struct agent_expr *ax, struct axs_value *value,
2347 CORE_ADDR scope)
2348{
2349 value->type = register_type (gdbarch, AARCH64_LR_REGNUM);
2350 value->kind = axs_lvalue_register;
2351 value->u.reg = AARCH64_LR_REGNUM;
2352}
07b287a0
MS
2353\f
2354
2355/* Return the pseudo register name corresponding to register regnum. */
2356
2357static const char *
2358aarch64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
2359{
63bad7b6
AH
2360 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2361
07b287a0
MS
2362 static const char *const q_name[] =
2363 {
2364 "q0", "q1", "q2", "q3",
2365 "q4", "q5", "q6", "q7",
2366 "q8", "q9", "q10", "q11",
2367 "q12", "q13", "q14", "q15",
2368 "q16", "q17", "q18", "q19",
2369 "q20", "q21", "q22", "q23",
2370 "q24", "q25", "q26", "q27",
2371 "q28", "q29", "q30", "q31",
2372 };
2373
2374 static const char *const d_name[] =
2375 {
2376 "d0", "d1", "d2", "d3",
2377 "d4", "d5", "d6", "d7",
2378 "d8", "d9", "d10", "d11",
2379 "d12", "d13", "d14", "d15",
2380 "d16", "d17", "d18", "d19",
2381 "d20", "d21", "d22", "d23",
2382 "d24", "d25", "d26", "d27",
2383 "d28", "d29", "d30", "d31",
2384 };
2385
2386 static const char *const s_name[] =
2387 {
2388 "s0", "s1", "s2", "s3",
2389 "s4", "s5", "s6", "s7",
2390 "s8", "s9", "s10", "s11",
2391 "s12", "s13", "s14", "s15",
2392 "s16", "s17", "s18", "s19",
2393 "s20", "s21", "s22", "s23",
2394 "s24", "s25", "s26", "s27",
2395 "s28", "s29", "s30", "s31",
2396 };
2397
2398 static const char *const h_name[] =
2399 {
2400 "h0", "h1", "h2", "h3",
2401 "h4", "h5", "h6", "h7",
2402 "h8", "h9", "h10", "h11",
2403 "h12", "h13", "h14", "h15",
2404 "h16", "h17", "h18", "h19",
2405 "h20", "h21", "h22", "h23",
2406 "h24", "h25", "h26", "h27",
2407 "h28", "h29", "h30", "h31",
2408 };
2409
2410 static const char *const b_name[] =
2411 {
2412 "b0", "b1", "b2", "b3",
2413 "b4", "b5", "b6", "b7",
2414 "b8", "b9", "b10", "b11",
2415 "b12", "b13", "b14", "b15",
2416 "b16", "b17", "b18", "b19",
2417 "b20", "b21", "b22", "b23",
2418 "b24", "b25", "b26", "b27",
2419 "b28", "b29", "b30", "b31",
2420 };
2421
34dcc7cf 2422 int p_regnum = regnum - gdbarch_num_regs (gdbarch);
07b287a0 2423
34dcc7cf
AH
2424 if (p_regnum >= AARCH64_Q0_REGNUM && p_regnum < AARCH64_Q0_REGNUM + 32)
2425 return q_name[p_regnum - AARCH64_Q0_REGNUM];
07b287a0 2426
34dcc7cf
AH
2427 if (p_regnum >= AARCH64_D0_REGNUM && p_regnum < AARCH64_D0_REGNUM + 32)
2428 return d_name[p_regnum - AARCH64_D0_REGNUM];
07b287a0 2429
34dcc7cf
AH
2430 if (p_regnum >= AARCH64_S0_REGNUM && p_regnum < AARCH64_S0_REGNUM + 32)
2431 return s_name[p_regnum - AARCH64_S0_REGNUM];
07b287a0 2432
34dcc7cf
AH
2433 if (p_regnum >= AARCH64_H0_REGNUM && p_regnum < AARCH64_H0_REGNUM + 32)
2434 return h_name[p_regnum - AARCH64_H0_REGNUM];
07b287a0 2435
34dcc7cf
AH
2436 if (p_regnum >= AARCH64_B0_REGNUM && p_regnum < AARCH64_B0_REGNUM + 32)
2437 return b_name[p_regnum - AARCH64_B0_REGNUM];
07b287a0 2438
63bad7b6
AH
2439 if (tdep->has_sve ())
2440 {
2441 static const char *const sve_v_name[] =
2442 {
2443 "v0", "v1", "v2", "v3",
2444 "v4", "v5", "v6", "v7",
2445 "v8", "v9", "v10", "v11",
2446 "v12", "v13", "v14", "v15",
2447 "v16", "v17", "v18", "v19",
2448 "v20", "v21", "v22", "v23",
2449 "v24", "v25", "v26", "v27",
2450 "v28", "v29", "v30", "v31",
2451 };
2452
34dcc7cf
AH
2453 if (p_regnum >= AARCH64_SVE_V0_REGNUM
2454 && p_regnum < AARCH64_SVE_V0_REGNUM + AARCH64_V_REGS_NUM)
2455 return sve_v_name[p_regnum - AARCH64_SVE_V0_REGNUM];
63bad7b6
AH
2456 }
2457
34dcc7cf
AH
2458 /* RA_STATE is used for unwinding only. Do not assign it a name - this
2459 prevents it from being read by methods such as
2460 mi_cmd_trace_frame_collected. */
2461 if (tdep->has_pauth () && regnum == tdep->pauth_ra_state_regnum)
2462 return "";
2463
07b287a0
MS
2464 internal_error (__FILE__, __LINE__,
2465 _("aarch64_pseudo_register_name: bad register number %d"),
34dcc7cf 2466 p_regnum);
07b287a0
MS
2467}
2468
2469/* Implement the "pseudo_register_type" tdesc_arch_data method. */
2470
2471static struct type *
2472aarch64_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
2473{
63bad7b6
AH
2474 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2475
34dcc7cf 2476 int p_regnum = regnum - gdbarch_num_regs (gdbarch);
07b287a0 2477
34dcc7cf 2478 if (p_regnum >= AARCH64_Q0_REGNUM && p_regnum < AARCH64_Q0_REGNUM + 32)
07b287a0
MS
2479 return aarch64_vnq_type (gdbarch);
2480
34dcc7cf 2481 if (p_regnum >= AARCH64_D0_REGNUM && p_regnum < AARCH64_D0_REGNUM + 32)
07b287a0
MS
2482 return aarch64_vnd_type (gdbarch);
2483
34dcc7cf 2484 if (p_regnum >= AARCH64_S0_REGNUM && p_regnum < AARCH64_S0_REGNUM + 32)
07b287a0
MS
2485 return aarch64_vns_type (gdbarch);
2486
34dcc7cf 2487 if (p_regnum >= AARCH64_H0_REGNUM && p_regnum < AARCH64_H0_REGNUM + 32)
07b287a0
MS
2488 return aarch64_vnh_type (gdbarch);
2489
34dcc7cf 2490 if (p_regnum >= AARCH64_B0_REGNUM && p_regnum < AARCH64_B0_REGNUM + 32)
07b287a0
MS
2491 return aarch64_vnb_type (gdbarch);
2492
34dcc7cf
AH
2493 if (tdep->has_sve () && p_regnum >= AARCH64_SVE_V0_REGNUM
2494 && p_regnum < AARCH64_SVE_V0_REGNUM + AARCH64_V_REGS_NUM)
63bad7b6
AH
2495 return aarch64_vnv_type (gdbarch);
2496
34dcc7cf
AH
2497 if (tdep->has_pauth () && regnum == tdep->pauth_ra_state_regnum)
2498 return builtin_type (gdbarch)->builtin_uint64;
2499
07b287a0
MS
2500 internal_error (__FILE__, __LINE__,
2501 _("aarch64_pseudo_register_type: bad register number %d"),
34dcc7cf 2502 p_regnum);
07b287a0
MS
2503}
2504
2505/* Implement the "pseudo_register_reggroup_p" tdesc_arch_data method. */
2506
2507static int
2508aarch64_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
2509 struct reggroup *group)
2510{
63bad7b6
AH
2511 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2512
34dcc7cf 2513 int p_regnum = regnum - gdbarch_num_regs (gdbarch);
07b287a0 2514
34dcc7cf 2515 if (p_regnum >= AARCH64_Q0_REGNUM && p_regnum < AARCH64_Q0_REGNUM + 32)
07b287a0 2516 return group == all_reggroup || group == vector_reggroup;
34dcc7cf 2517 else if (p_regnum >= AARCH64_D0_REGNUM && p_regnum < AARCH64_D0_REGNUM + 32)
07b287a0
MS
2518 return (group == all_reggroup || group == vector_reggroup
2519 || group == float_reggroup);
34dcc7cf 2520 else if (p_regnum >= AARCH64_S0_REGNUM && p_regnum < AARCH64_S0_REGNUM + 32)
07b287a0
MS
2521 return (group == all_reggroup || group == vector_reggroup
2522 || group == float_reggroup);
34dcc7cf 2523 else if (p_regnum >= AARCH64_H0_REGNUM && p_regnum < AARCH64_H0_REGNUM + 32)
07b287a0 2524 return group == all_reggroup || group == vector_reggroup;
34dcc7cf 2525 else if (p_regnum >= AARCH64_B0_REGNUM && p_regnum < AARCH64_B0_REGNUM + 32)
07b287a0 2526 return group == all_reggroup || group == vector_reggroup;
34dcc7cf
AH
2527 else if (tdep->has_sve () && p_regnum >= AARCH64_SVE_V0_REGNUM
2528 && p_regnum < AARCH64_SVE_V0_REGNUM + AARCH64_V_REGS_NUM)
63bad7b6 2529 return group == all_reggroup || group == vector_reggroup;
34dcc7cf
AH
2530 /* RA_STATE is used for unwinding only. Do not assign it to any groups. */
2531 if (tdep->has_pauth () && regnum == tdep->pauth_ra_state_regnum)
2532 return 0;
07b287a0
MS
2533
2534 return group == all_reggroup;
2535}
2536
3c5cd5c3
AH
2537/* Helper for aarch64_pseudo_read_value. */
2538
2539static struct value *
63bad7b6
AH
2540aarch64_pseudo_read_value_1 (struct gdbarch *gdbarch,
2541 readable_regcache *regcache, int regnum_offset,
3c5cd5c3
AH
2542 int regsize, struct value *result_value)
2543{
3c5cd5c3
AH
2544 unsigned v_regnum = AARCH64_V0_REGNUM + regnum_offset;
2545
63bad7b6
AH
2546 /* Enough space for a full vector register. */
2547 gdb_byte reg_buf[register_size (gdbarch, AARCH64_V0_REGNUM)];
2548 gdb_static_assert (AARCH64_V0_REGNUM == AARCH64_SVE_Z0_REGNUM);
2549
3c5cd5c3
AH
2550 if (regcache->raw_read (v_regnum, reg_buf) != REG_VALID)
2551 mark_value_bytes_unavailable (result_value, 0,
2552 TYPE_LENGTH (value_type (result_value)));
2553 else
2554 memcpy (value_contents_raw (result_value), reg_buf, regsize);
63bad7b6 2555
3c5cd5c3
AH
2556 return result_value;
2557 }
2558
07b287a0
MS
2559/* Implement the "pseudo_register_read_value" gdbarch method. */
2560
2561static struct value *
3c5cd5c3 2562aarch64_pseudo_read_value (struct gdbarch *gdbarch, readable_regcache *regcache,
07b287a0
MS
2563 int regnum)
2564{
63bad7b6 2565 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3c5cd5c3 2566 struct value *result_value = allocate_value (register_type (gdbarch, regnum));
07b287a0 2567
07b287a0
MS
2568 VALUE_LVAL (result_value) = lval_register;
2569 VALUE_REGNUM (result_value) = regnum;
07b287a0
MS
2570
2571 regnum -= gdbarch_num_regs (gdbarch);
2572
2573 if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
63bad7b6
AH
2574 return aarch64_pseudo_read_value_1 (gdbarch, regcache,
2575 regnum - AARCH64_Q0_REGNUM,
3c5cd5c3 2576 Q_REGISTER_SIZE, result_value);
07b287a0
MS
2577
2578 if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
63bad7b6
AH
2579 return aarch64_pseudo_read_value_1 (gdbarch, regcache,
2580 regnum - AARCH64_D0_REGNUM,
3c5cd5c3 2581 D_REGISTER_SIZE, result_value);
07b287a0
MS
2582
2583 if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
63bad7b6
AH
2584 return aarch64_pseudo_read_value_1 (gdbarch, regcache,
2585 regnum - AARCH64_S0_REGNUM,
3c5cd5c3 2586 S_REGISTER_SIZE, result_value);
07b287a0
MS
2587
2588 if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
63bad7b6
AH
2589 return aarch64_pseudo_read_value_1 (gdbarch, regcache,
2590 regnum - AARCH64_H0_REGNUM,
3c5cd5c3 2591 H_REGISTER_SIZE, result_value);
07b287a0
MS
2592
2593 if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
63bad7b6
AH
2594 return aarch64_pseudo_read_value_1 (gdbarch, regcache,
2595 regnum - AARCH64_B0_REGNUM,
3c5cd5c3 2596 B_REGISTER_SIZE, result_value);
07b287a0 2597
63bad7b6
AH
2598 if (tdep->has_sve () && regnum >= AARCH64_SVE_V0_REGNUM
2599 && regnum < AARCH64_SVE_V0_REGNUM + 32)
2600 return aarch64_pseudo_read_value_1 (gdbarch, regcache,
2601 regnum - AARCH64_SVE_V0_REGNUM,
2602 V_REGISTER_SIZE, result_value);
2603
07b287a0
MS
2604 gdb_assert_not_reached ("regnum out of bound");
2605}
2606
3c5cd5c3 2607/* Helper for aarch64_pseudo_write. */
07b287a0
MS
2608
2609static void
63bad7b6
AH
2610aarch64_pseudo_write_1 (struct gdbarch *gdbarch, struct regcache *regcache,
2611 int regnum_offset, int regsize, const gdb_byte *buf)
07b287a0 2612{
3c5cd5c3 2613 unsigned v_regnum = AARCH64_V0_REGNUM + regnum_offset;
07b287a0 2614
63bad7b6
AH
2615 /* Enough space for a full vector register. */
2616 gdb_byte reg_buf[register_size (gdbarch, AARCH64_V0_REGNUM)];
2617 gdb_static_assert (AARCH64_V0_REGNUM == AARCH64_SVE_Z0_REGNUM);
2618
07b287a0
MS
2619 /* Ensure the register buffer is zero, we want gdb writes of the
2620 various 'scalar' pseudo registers to behavior like architectural
2621 writes, register width bytes are written the remainder are set to
2622 zero. */
63bad7b6 2623 memset (reg_buf, 0, register_size (gdbarch, AARCH64_V0_REGNUM));
07b287a0 2624
3c5cd5c3
AH
2625 memcpy (reg_buf, buf, regsize);
2626 regcache->raw_write (v_regnum, reg_buf);
2627}
2628
2629/* Implement the "pseudo_register_write" gdbarch method. */
2630
2631static void
2632aarch64_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
2633 int regnum, const gdb_byte *buf)
2634{
63bad7b6 2635 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
07b287a0
MS
2636 regnum -= gdbarch_num_regs (gdbarch);
2637
2638 if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
63bad7b6
AH
2639 return aarch64_pseudo_write_1 (gdbarch, regcache,
2640 regnum - AARCH64_Q0_REGNUM, Q_REGISTER_SIZE,
2641 buf);
07b287a0
MS
2642
2643 if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
63bad7b6
AH
2644 return aarch64_pseudo_write_1 (gdbarch, regcache,
2645 regnum - AARCH64_D0_REGNUM, D_REGISTER_SIZE,
2646 buf);
07b287a0
MS
2647
2648 if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
63bad7b6
AH
2649 return aarch64_pseudo_write_1 (gdbarch, regcache,
2650 regnum - AARCH64_S0_REGNUM, S_REGISTER_SIZE,
2651 buf);
07b287a0
MS
2652
2653 if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
63bad7b6
AH
2654 return aarch64_pseudo_write_1 (gdbarch, regcache,
2655 regnum - AARCH64_H0_REGNUM, H_REGISTER_SIZE,
2656 buf);
07b287a0
MS
2657
2658 if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
63bad7b6
AH
2659 return aarch64_pseudo_write_1 (gdbarch, regcache,
2660 regnum - AARCH64_B0_REGNUM, B_REGISTER_SIZE,
2661 buf);
2662
2663 if (tdep->has_sve () && regnum >= AARCH64_SVE_V0_REGNUM
2664 && regnum < AARCH64_SVE_V0_REGNUM + 32)
2665 return aarch64_pseudo_write_1 (gdbarch, regcache,
2666 regnum - AARCH64_SVE_V0_REGNUM,
2667 V_REGISTER_SIZE, buf);
07b287a0
MS
2668
2669 gdb_assert_not_reached ("regnum out of bound");
2670}
2671
07b287a0
MS
2672/* Callback function for user_reg_add. */
2673
2674static struct value *
2675value_of_aarch64_user_reg (struct frame_info *frame, const void *baton)
2676{
9a3c8263 2677 const int *reg_p = (const int *) baton;
07b287a0
MS
2678
2679 return value_of_register (*reg_p, frame);
2680}
2681\f
2682
9404b58f
KM
2683/* Implement the "software_single_step" gdbarch method, needed to
2684 single step through atomic sequences on AArch64. */
2685
a0ff9e1a 2686static std::vector<CORE_ADDR>
f5ea389a 2687aarch64_software_single_step (struct regcache *regcache)
9404b58f 2688{
ac7936df 2689 struct gdbarch *gdbarch = regcache->arch ();
9404b58f
KM
2690 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2691 const int insn_size = 4;
2692 const int atomic_sequence_length = 16; /* Instruction sequence length. */
0187a92f 2693 CORE_ADDR pc = regcache_read_pc (regcache);
70ab8ccd 2694 CORE_ADDR breaks[2] = { CORE_ADDR_MAX, CORE_ADDR_MAX };
9404b58f
KM
2695 CORE_ADDR loc = pc;
2696 CORE_ADDR closing_insn = 0;
2697 uint32_t insn = read_memory_unsigned_integer (loc, insn_size,
2698 byte_order_for_code);
2699 int index;
2700 int insn_count;
2701 int bc_insn_count = 0; /* Conditional branch instruction count. */
2702 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
f77ee802
YQ
2703 aarch64_inst inst;
2704
561a72d4 2705 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
a0ff9e1a 2706 return {};
9404b58f
KM
2707
2708 /* Look for a Load Exclusive instruction which begins the sequence. */
f77ee802 2709 if (inst.opcode->iclass != ldstexcl || bit (insn, 22) == 0)
a0ff9e1a 2710 return {};
9404b58f
KM
2711
2712 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
2713 {
9404b58f
KM
2714 loc += insn_size;
2715 insn = read_memory_unsigned_integer (loc, insn_size,
2716 byte_order_for_code);
2717
561a72d4 2718 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
a0ff9e1a 2719 return {};
9404b58f 2720 /* Check if the instruction is a conditional branch. */
f77ee802 2721 if (inst.opcode->iclass == condbranch)
9404b58f 2722 {
f77ee802
YQ
2723 gdb_assert (inst.operands[0].type == AARCH64_OPND_ADDR_PCREL19);
2724
9404b58f 2725 if (bc_insn_count >= 1)
a0ff9e1a 2726 return {};
9404b58f
KM
2727
2728 /* It is, so we'll try to set a breakpoint at the destination. */
f77ee802 2729 breaks[1] = loc + inst.operands[0].imm.value;
9404b58f
KM
2730
2731 bc_insn_count++;
2732 last_breakpoint++;
2733 }
2734
2735 /* Look for the Store Exclusive which closes the atomic sequence. */
f77ee802 2736 if (inst.opcode->iclass == ldstexcl && bit (insn, 22) == 0)
9404b58f
KM
2737 {
2738 closing_insn = loc;
2739 break;
2740 }
2741 }
2742
2743 /* We didn't find a closing Store Exclusive instruction, fall back. */
2744 if (!closing_insn)
a0ff9e1a 2745 return {};
9404b58f
KM
2746
2747 /* Insert breakpoint after the end of the atomic sequence. */
2748 breaks[0] = loc + insn_size;
2749
2750 /* Check for duplicated breakpoints, and also check that the second
2751 breakpoint is not within the atomic sequence. */
2752 if (last_breakpoint
2753 && (breaks[1] == breaks[0]
2754 || (breaks[1] >= pc && breaks[1] <= closing_insn)))
2755 last_breakpoint = 0;
2756
a0ff9e1a
SM
2757 std::vector<CORE_ADDR> next_pcs;
2758
9404b58f
KM
2759 /* Insert the breakpoint at the end of the sequence, and one at the
2760 destination of the conditional branch, if it exists. */
2761 for (index = 0; index <= last_breakpoint; index++)
a0ff9e1a 2762 next_pcs.push_back (breaks[index]);
9404b58f 2763
93f9a11f 2764 return next_pcs;
9404b58f
KM
2765}
2766
588de28a 2767struct aarch64_displaced_step_copy_insn_closure : public displaced_step_copy_insn_closure
b6542f81
YQ
2768{
2769 /* It is true when condition instruction, such as B.CON, TBZ, etc,
2770 is being displaced stepping. */
f0c702d4 2771 bool cond = false;
b6542f81 2772
0c271889
LM
2773 /* PC adjustment offset after displaced stepping. If 0, then we don't
2774 write the PC back, assuming the PC is already the right address. */
cfba9872 2775 int32_t pc_adjust = 0;
b6542f81
YQ
2776};
2777
2778/* Data when visiting instructions for displaced stepping. */
2779
2780struct aarch64_displaced_step_data
2781{
2782 struct aarch64_insn_data base;
2783
2784 /* The address where the instruction will be executed at. */
2785 CORE_ADDR new_addr;
2786 /* Buffer of instructions to be copied to NEW_ADDR to execute. */
e935475c 2787 uint32_t insn_buf[AARCH64_DISPLACED_MODIFIED_INSNS];
b6542f81
YQ
2788 /* Number of instructions in INSN_BUF. */
2789 unsigned insn_count;
2790 /* Registers when doing displaced stepping. */
2791 struct regcache *regs;
2792
588de28a 2793 aarch64_displaced_step_copy_insn_closure *dsc;
b6542f81
YQ
2794};
2795
2796/* Implementation of aarch64_insn_visitor method "b". */
2797
2798static void
2799aarch64_displaced_step_b (const int is_bl, const int32_t offset,
2800 struct aarch64_insn_data *data)
2801{
2802 struct aarch64_displaced_step_data *dsd
2803 = (struct aarch64_displaced_step_data *) data;
2ac09a5b 2804 int64_t new_offset = data->insn_addr - dsd->new_addr + offset;
b6542f81
YQ
2805
2806 if (can_encode_int32 (new_offset, 28))
2807 {
2808 /* Emit B rather than BL, because executing BL on a new address
2809 will get the wrong address into LR. In order to avoid this,
2810 we emit B, and update LR if the instruction is BL. */
2811 emit_b (dsd->insn_buf, 0, new_offset);
2812 dsd->insn_count++;
2813 }
2814 else
2815 {
2816 /* Write NOP. */
2817 emit_nop (dsd->insn_buf);
2818 dsd->insn_count++;
2819 dsd->dsc->pc_adjust = offset;
2820 }
2821
2822 if (is_bl)
2823 {
2824 /* Update LR. */
2825 regcache_cooked_write_unsigned (dsd->regs, AARCH64_LR_REGNUM,
2826 data->insn_addr + 4);
2827 }
2828}
2829
2830/* Implementation of aarch64_insn_visitor method "b_cond". */
2831
2832static void
2833aarch64_displaced_step_b_cond (const unsigned cond, const int32_t offset,
2834 struct aarch64_insn_data *data)
2835{
2836 struct aarch64_displaced_step_data *dsd
2837 = (struct aarch64_displaced_step_data *) data;
b6542f81
YQ
2838
2839 /* GDB has to fix up PC after displaced step this instruction
2840 differently according to the condition is true or false. Instead
2841 of checking COND against conditional flags, we can use
2842 the following instructions, and GDB can tell how to fix up PC
2843 according to the PC value.
2844
2845 B.COND TAKEN ; If cond is true, then jump to TAKEN.
2846 INSN1 ;
2847 TAKEN:
2848 INSN2
2849 */
2850
2851 emit_bcond (dsd->insn_buf, cond, 8);
f0c702d4 2852 dsd->dsc->cond = true;
b6542f81
YQ
2853 dsd->dsc->pc_adjust = offset;
2854 dsd->insn_count = 1;
2855}
2856
2857/* Dynamically allocate a new register. If we know the register
2858 statically, we should make it a global as above instead of using this
2859 helper function. */
2860
2861static struct aarch64_register
2862aarch64_register (unsigned num, int is64)
2863{
2864 return (struct aarch64_register) { num, is64 };
2865}
2866
2867/* Implementation of aarch64_insn_visitor method "cb". */
2868
2869static void
2870aarch64_displaced_step_cb (const int32_t offset, const int is_cbnz,
2871 const unsigned rn, int is64,
2872 struct aarch64_insn_data *data)
2873{
2874 struct aarch64_displaced_step_data *dsd
2875 = (struct aarch64_displaced_step_data *) data;
b6542f81
YQ
2876
2877 /* The offset is out of range for a compare and branch
2878 instruction. We can use the following instructions instead:
2879
2880 CBZ xn, TAKEN ; xn == 0, then jump to TAKEN.
2881 INSN1 ;
2882 TAKEN:
2883 INSN2
2884 */
2885 emit_cb (dsd->insn_buf, is_cbnz, aarch64_register (rn, is64), 8);
2886 dsd->insn_count = 1;
f0c702d4 2887 dsd->dsc->cond = true;
b6542f81
YQ
2888 dsd->dsc->pc_adjust = offset;
2889}
2890
2891/* Implementation of aarch64_insn_visitor method "tb". */
2892
2893static void
2894aarch64_displaced_step_tb (const int32_t offset, int is_tbnz,
2895 const unsigned rt, unsigned bit,
2896 struct aarch64_insn_data *data)
2897{
2898 struct aarch64_displaced_step_data *dsd
2899 = (struct aarch64_displaced_step_data *) data;
b6542f81
YQ
2900
2901 /* The offset is out of range for a test bit and branch
2902 instruction We can use the following instructions instead:
2903
2904 TBZ xn, #bit, TAKEN ; xn[bit] == 0, then jump to TAKEN.
2905 INSN1 ;
2906 TAKEN:
2907 INSN2
2908
2909 */
2910 emit_tb (dsd->insn_buf, is_tbnz, bit, aarch64_register (rt, 1), 8);
2911 dsd->insn_count = 1;
f0c702d4 2912 dsd->dsc->cond = true;
b6542f81
YQ
2913 dsd->dsc->pc_adjust = offset;
2914}
2915
2916/* Implementation of aarch64_insn_visitor method "adr". */
2917
2918static void
2919aarch64_displaced_step_adr (const int32_t offset, const unsigned rd,
2920 const int is_adrp, struct aarch64_insn_data *data)
2921{
2922 struct aarch64_displaced_step_data *dsd
2923 = (struct aarch64_displaced_step_data *) data;
2924 /* We know exactly the address the ADR{P,} instruction will compute.
2925 We can just write it to the destination register. */
2926 CORE_ADDR address = data->insn_addr + offset;
2927
2928 if (is_adrp)
2929 {
2930 /* Clear the lower 12 bits of the offset to get the 4K page. */
2931 regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rd,
2932 address & ~0xfff);
2933 }
2934 else
2935 regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rd,
2936 address);
2937
2938 dsd->dsc->pc_adjust = 4;
2939 emit_nop (dsd->insn_buf);
2940 dsd->insn_count = 1;
2941}
2942
2943/* Implementation of aarch64_insn_visitor method "ldr_literal". */
2944
2945static void
2946aarch64_displaced_step_ldr_literal (const int32_t offset, const int is_sw,
2947 const unsigned rt, const int is64,
2948 struct aarch64_insn_data *data)
2949{
2950 struct aarch64_displaced_step_data *dsd
2951 = (struct aarch64_displaced_step_data *) data;
2952 CORE_ADDR address = data->insn_addr + offset;
2953 struct aarch64_memory_operand zero = { MEMORY_OPERAND_OFFSET, 0 };
2954
2955 regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rt,
2956 address);
2957
2958 if (is_sw)
2959 dsd->insn_count = emit_ldrsw (dsd->insn_buf, aarch64_register (rt, 1),
2960 aarch64_register (rt, 1), zero);
2961 else
2962 dsd->insn_count = emit_ldr (dsd->insn_buf, aarch64_register (rt, is64),
2963 aarch64_register (rt, 1), zero);
2964
2965 dsd->dsc->pc_adjust = 4;
2966}
2967
2968/* Implementation of aarch64_insn_visitor method "others". */
2969
2970static void
2971aarch64_displaced_step_others (const uint32_t insn,
2972 struct aarch64_insn_data *data)
2973{
2974 struct aarch64_displaced_step_data *dsd
2975 = (struct aarch64_displaced_step_data *) data;
2976
e1c587c3 2977 aarch64_emit_insn (dsd->insn_buf, insn);
b6542f81
YQ
2978 dsd->insn_count = 1;
2979
2980 if ((insn & 0xfffffc1f) == 0xd65f0000)
2981 {
2982 /* RET */
2983 dsd->dsc->pc_adjust = 0;
2984 }
2985 else
2986 dsd->dsc->pc_adjust = 4;
2987}
2988
2989static const struct aarch64_insn_visitor visitor =
2990{
2991 aarch64_displaced_step_b,
2992 aarch64_displaced_step_b_cond,
2993 aarch64_displaced_step_cb,
2994 aarch64_displaced_step_tb,
2995 aarch64_displaced_step_adr,
2996 aarch64_displaced_step_ldr_literal,
2997 aarch64_displaced_step_others,
2998};
2999
3000/* Implement the "displaced_step_copy_insn" gdbarch method. */
3001
588de28a 3002displaced_step_copy_insn_closure_up
b6542f81
YQ
3003aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
3004 CORE_ADDR from, CORE_ADDR to,
3005 struct regcache *regs)
3006{
b6542f81
YQ
3007 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3008 uint32_t insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
3009 struct aarch64_displaced_step_data dsd;
c86a40c6
YQ
3010 aarch64_inst inst;
3011
561a72d4 3012 if (aarch64_decode_insn (insn, &inst, 1, NULL) != 0)
c86a40c6 3013 return NULL;
b6542f81
YQ
3014
3015 /* Look for a Load Exclusive instruction which begins the sequence. */
c86a40c6 3016 if (inst.opcode->iclass == ldstexcl && bit (insn, 22))
b6542f81
YQ
3017 {
3018 /* We can't displaced step atomic sequences. */
3019 return NULL;
3020 }
3021
588de28a
SM
3022 std::unique_ptr<aarch64_displaced_step_copy_insn_closure> dsc
3023 (new aarch64_displaced_step_copy_insn_closure);
b6542f81
YQ
3024 dsd.base.insn_addr = from;
3025 dsd.new_addr = to;
3026 dsd.regs = regs;
cfba9872 3027 dsd.dsc = dsc.get ();
034f1a81 3028 dsd.insn_count = 0;
b6542f81
YQ
3029 aarch64_relocate_instruction (insn, &visitor,
3030 (struct aarch64_insn_data *) &dsd);
e935475c 3031 gdb_assert (dsd.insn_count <= AARCH64_DISPLACED_MODIFIED_INSNS);
b6542f81
YQ
3032
3033 if (dsd.insn_count != 0)
3034 {
3035 int i;
3036
3037 /* Instruction can be relocated to scratch pad. Copy
3038 relocated instruction(s) there. */
3039 for (i = 0; i < dsd.insn_count; i++)
3040 {
3041 if (debug_displaced)
3042 {
3043 debug_printf ("displaced: writing insn ");
3044 debug_printf ("%.8x", dsd.insn_buf[i]);
3045 debug_printf (" at %s\n", paddress (gdbarch, to + i * 4));
3046 }
3047 write_memory_unsigned_integer (to + i * 4, 4, byte_order_for_code,
3048 (ULONGEST) dsd.insn_buf[i]);
3049 }
3050 }
3051 else
3052 {
b6542f81
YQ
3053 dsc = NULL;
3054 }
3055
6d0cf446 3056 /* This is a work around for a problem with g++ 4.8. */
588de28a 3057 return displaced_step_copy_insn_closure_up (dsc.release ());
b6542f81
YQ
3058}
3059
3060/* Implement the "displaced_step_fixup" gdbarch method. */
3061
3062void
3063aarch64_displaced_step_fixup (struct gdbarch *gdbarch,
588de28a 3064 struct displaced_step_copy_insn_closure *dsc_,
b6542f81
YQ
3065 CORE_ADDR from, CORE_ADDR to,
3066 struct regcache *regs)
3067{
588de28a 3068 aarch64_displaced_step_copy_insn_closure *dsc = (aarch64_displaced_step_copy_insn_closure *) dsc_;
cfba9872 3069
0c271889
LM
3070 ULONGEST pc;
3071
3072 regcache_cooked_read_unsigned (regs, AARCH64_PC_REGNUM, &pc);
3073
1ab139e5
LM
3074 if (debug_displaced)
3075 debug_printf ("Displaced: PC after stepping: %s (was %s).\n",
3076 paddress (gdbarch, pc), paddress (gdbarch, to));
3077
b6542f81
YQ
3078 if (dsc->cond)
3079 {
1ab139e5
LM
3080 if (debug_displaced)
3081 debug_printf ("Displaced: [Conditional] pc_adjust before: %d\n",
3082 dsc->pc_adjust);
3083
b6542f81
YQ
3084 if (pc - to == 8)
3085 {
3086 /* Condition is true. */
3087 }
3088 else if (pc - to == 4)
3089 {
3090 /* Condition is false. */
3091 dsc->pc_adjust = 4;
3092 }
3093 else
3094 gdb_assert_not_reached ("Unexpected PC value after displaced stepping");
1ab139e5
LM
3095
3096 if (debug_displaced)
3097 debug_printf ("Displaced: [Conditional] pc_adjust after: %d\n",
3098 dsc->pc_adjust);
b6542f81
YQ
3099 }
3100
1ab139e5
LM
3101 if (debug_displaced)
3102 debug_printf ("Displaced: %s PC by %d\n",
3103 dsc->pc_adjust? "adjusting" : "not adjusting",
3104 dsc->pc_adjust);
3105
3106
b6542f81
YQ
3107 if (dsc->pc_adjust != 0)
3108 {
0c271889
LM
3109 /* Make sure the previous instruction was executed (that is, the PC
3110 has changed). If the PC didn't change, then discard the adjustment
3111 offset. Otherwise we may skip an instruction before its execution
3112 took place. */
3113 if ((pc - to) == 0)
1ab139e5
LM
3114 {
3115 if (debug_displaced)
3116 debug_printf ("Displaced: PC did not move. Discarding PC "
3117 "adjustment.\n");
3118 dsc->pc_adjust = 0;
3119 }
0c271889 3120
b6542f81
YQ
3121 if (debug_displaced)
3122 {
1ab139e5 3123 debug_printf ("Displaced: fixup: set PC to %s:%d\n",
b6542f81
YQ
3124 paddress (gdbarch, from), dsc->pc_adjust);
3125 }
3126 regcache_cooked_write_unsigned (regs, AARCH64_PC_REGNUM,
3127 from + dsc->pc_adjust);
3128 }
3129}
3130
3131/* Implement the "displaced_step_hw_singlestep" gdbarch method. */
3132
3133int
3134aarch64_displaced_step_hw_singlestep (struct gdbarch *gdbarch,
588de28a 3135 struct displaced_step_copy_insn_closure *closure)
b6542f81
YQ
3136{
3137 return 1;
3138}
3139
95228a0d
AH
3140/* Get the correct target description for the given VQ value.
3141 If VQ is zero then it is assumed SVE is not supported.
3142 (It is not possible to set VQ to zero on an SVE system). */
da434ccb
AH
3143
3144const target_desc *
6dc0ebde 3145aarch64_read_description (uint64_t vq, bool pauth_p)
da434ccb 3146{
95228a0d 3147 if (vq > AARCH64_MAX_SVE_VQ)
39bfb937 3148 error (_("VQ is %" PRIu64 ", maximum supported value is %d"), vq,
95228a0d
AH
3149 AARCH64_MAX_SVE_VQ);
3150
6dc0ebde 3151 struct target_desc *tdesc = tdesc_aarch64_list[vq][pauth_p];
da434ccb 3152
95228a0d
AH
3153 if (tdesc == NULL)
3154 {
6dc0ebde
AH
3155 tdesc = aarch64_create_target_description (vq, pauth_p);
3156 tdesc_aarch64_list[vq][pauth_p] = tdesc;
95228a0d 3157 }
da434ccb 3158
95228a0d 3159 return tdesc;
da434ccb
AH
3160}
3161
ba2d2bb2
AH
3162/* Return the VQ used when creating the target description TDESC. */
3163
1332a140 3164static uint64_t
ba2d2bb2
AH
3165aarch64_get_tdesc_vq (const struct target_desc *tdesc)
3166{
3167 const struct tdesc_feature *feature_sve;
3168
3169 if (!tdesc_has_registers (tdesc))
3170 return 0;
3171
3172 feature_sve = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sve");
3173
3174 if (feature_sve == nullptr)
3175 return 0;
3176
12863263
AH
3177 uint64_t vl = tdesc_register_bitsize (feature_sve,
3178 aarch64_sve_register_names[0]) / 8;
ba2d2bb2
AH
3179 return sve_vq_from_vl (vl);
3180}
3181
0ef8a082
AH
3182/* Add all the expected register sets into GDBARCH. */
3183
3184static void
3185aarch64_add_reggroups (struct gdbarch *gdbarch)
3186{
3187 reggroup_add (gdbarch, general_reggroup);
3188 reggroup_add (gdbarch, float_reggroup);
3189 reggroup_add (gdbarch, system_reggroup);
3190 reggroup_add (gdbarch, vector_reggroup);
3191 reggroup_add (gdbarch, all_reggroup);
3192 reggroup_add (gdbarch, save_reggroup);
3193 reggroup_add (gdbarch, restore_reggroup);
3194}
ba2d2bb2 3195
76bed0fd
AH
3196/* Implement the "cannot_store_register" gdbarch method. */
3197
3198static int
3199aarch64_cannot_store_register (struct gdbarch *gdbarch, int regnum)
3200{
3201 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3202
3203 if (!tdep->has_pauth ())
3204 return 0;
3205
3206 /* Pointer authentication registers are read-only. */
3207 return (regnum == AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base)
3208 || regnum == AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base));
3209}
3210
07b287a0
MS
3211/* Initialize the current architecture based on INFO. If possible,
3212 re-use an architecture from ARCHES, which is a list of
3213 architectures already created during this debugging session.
3214
3215 Called e.g. at program startup, when reading a core file, and when
3216 reading a binary file. */
3217
3218static struct gdbarch *
3219aarch64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3220{
ccb8d7e8 3221 const struct tdesc_feature *feature_core, *feature_fpu, *feature_sve;
76bed0fd 3222 const struct tdesc_feature *feature_pauth;
ccb8d7e8
AH
3223 bool valid_p = true;
3224 int i, num_regs = 0, num_pseudo_regs = 0;
3225 int first_pauth_regnum = -1, pauth_ra_state_offset = -1;
3226
4da037ef
AH
3227 /* Use the vector length passed via the target info. Here -1 is used for no
3228 SVE, and 0 is unset. If unset then use the vector length from the existing
3229 tdesc. */
3230 uint64_t vq = 0;
3231 if (info.id == (int *) -1)
3232 vq = 0;
3233 else if (info.id != 0)
3234 vq = (uint64_t) info.id;
3235 else
3236 vq = aarch64_get_tdesc_vq (info.target_desc);
3237
3238 if (vq > AARCH64_MAX_SVE_VQ)
596179f7
SDJ
3239 internal_error (__FILE__, __LINE__, _("VQ out of bounds: %s (max %d)"),
3240 pulongest (vq), AARCH64_MAX_SVE_VQ);
4da037ef 3241
ccb8d7e8
AH
3242 /* If there is already a candidate, use it. */
3243 for (gdbarch_list *best_arch = gdbarch_list_lookup_by_info (arches, &info);
3244 best_arch != nullptr;
3245 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
3246 {
3247 struct gdbarch_tdep *tdep = gdbarch_tdep (best_arch->gdbarch);
4da037ef 3248 if (tdep && tdep->vq == vq)
ccb8d7e8
AH
3249 return best_arch->gdbarch;
3250 }
07b287a0 3251
4da037ef
AH
3252 /* Ensure we always have a target descriptor, and that it is for the given VQ
3253 value. */
ccb8d7e8 3254 const struct target_desc *tdesc = info.target_desc;
4da037ef
AH
3255 if (!tdesc_has_registers (tdesc) || vq != aarch64_get_tdesc_vq (tdesc))
3256 tdesc = aarch64_read_description (vq, false);
07b287a0
MS
3257 gdb_assert (tdesc);
3258
ccb8d7e8 3259 feature_core = tdesc_find_feature (tdesc,"org.gnu.gdb.aarch64.core");
ba2d2bb2
AH
3260 feature_fpu = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.fpu");
3261 feature_sve = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.sve");
76bed0fd 3262 feature_pauth = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.pauth");
07b287a0 3263
ccb8d7e8
AH
3264 if (feature_core == nullptr)
3265 return nullptr;
07b287a0 3266
ccb8d7e8 3267 struct tdesc_arch_data *tdesc_data = tdesc_data_alloc ();
07b287a0 3268
ba2d2bb2 3269 /* Validate the description provides the mandatory core R registers
07b287a0
MS
3270 and allocate their numbers. */
3271 for (i = 0; i < ARRAY_SIZE (aarch64_r_register_names); i++)
ba2d2bb2
AH
3272 valid_p &= tdesc_numbered_register (feature_core, tdesc_data,
3273 AARCH64_X0_REGNUM + i,
3274 aarch64_r_register_names[i]);
07b287a0
MS
3275
3276 num_regs = AARCH64_X0_REGNUM + i;
3277
ba2d2bb2 3278 /* Add the V registers. */
ccb8d7e8 3279 if (feature_fpu != nullptr)
07b287a0 3280 {
ccb8d7e8 3281 if (feature_sve != nullptr)
ba2d2bb2
AH
3282 error (_("Program contains both fpu and SVE features."));
3283
3284 /* Validate the description provides the mandatory V registers
3285 and allocate their numbers. */
07b287a0 3286 for (i = 0; i < ARRAY_SIZE (aarch64_v_register_names); i++)
ba2d2bb2
AH
3287 valid_p &= tdesc_numbered_register (feature_fpu, tdesc_data,
3288 AARCH64_V0_REGNUM + i,
3289 aarch64_v_register_names[i]);
07b287a0
MS
3290
3291 num_regs = AARCH64_V0_REGNUM + i;
ba2d2bb2 3292 }
07b287a0 3293
ba2d2bb2 3294 /* Add the SVE registers. */
ccb8d7e8 3295 if (feature_sve != nullptr)
ba2d2bb2
AH
3296 {
3297 /* Validate the description provides the mandatory SVE registers
3298 and allocate their numbers. */
3299 for (i = 0; i < ARRAY_SIZE (aarch64_sve_register_names); i++)
3300 valid_p &= tdesc_numbered_register (feature_sve, tdesc_data,
3301 AARCH64_SVE_Z0_REGNUM + i,
3302 aarch64_sve_register_names[i]);
3303
3304 num_regs = AARCH64_SVE_Z0_REGNUM + i;
3305 num_pseudo_regs += 32; /* add the Vn register pseudos. */
3306 }
3307
ccb8d7e8 3308 if (feature_fpu != nullptr || feature_sve != nullptr)
ba2d2bb2 3309 {
07b287a0
MS
3310 num_pseudo_regs += 32; /* add the Qn scalar register pseudos */
3311 num_pseudo_regs += 32; /* add the Dn scalar register pseudos */
3312 num_pseudo_regs += 32; /* add the Sn scalar register pseudos */
3313 num_pseudo_regs += 32; /* add the Hn scalar register pseudos */
3314 num_pseudo_regs += 32; /* add the Bn scalar register pseudos */
3315 }
3316
76bed0fd
AH
3317 /* Add the pauth registers. */
3318 if (feature_pauth != NULL)
3319 {
3320 first_pauth_regnum = num_regs;
34dcc7cf 3321 pauth_ra_state_offset = num_pseudo_regs;
76bed0fd
AH
3322 /* Validate the descriptor provides the mandatory PAUTH registers and
3323 allocate their numbers. */
3324 for (i = 0; i < ARRAY_SIZE (aarch64_pauth_register_names); i++)
3325 valid_p &= tdesc_numbered_register (feature_pauth, tdesc_data,
3326 first_pauth_regnum + i,
3327 aarch64_pauth_register_names[i]);
3328
3329 num_regs += i;
34dcc7cf 3330 num_pseudo_regs += 1; /* Count RA_STATE pseudo register. */
76bed0fd
AH
3331 }
3332
07b287a0
MS
3333 if (!valid_p)
3334 {
3335 tdesc_data_cleanup (tdesc_data);
ccb8d7e8 3336 return nullptr;
07b287a0
MS
3337 }
3338
3339 /* AArch64 code is always little-endian. */
3340 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
3341
ccb8d7e8
AH
3342 struct gdbarch_tdep *tdep = XCNEW (struct gdbarch_tdep);
3343 struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep);
07b287a0
MS
3344
3345 /* This should be low enough for everything. */
3346 tdep->lowest_pc = 0x20;
3347 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
3348 tdep->jb_elt_size = 8;
4da037ef 3349 tdep->vq = vq;
76bed0fd 3350 tdep->pauth_reg_base = first_pauth_regnum;
34dcc7cf
AH
3351 tdep->pauth_ra_state_regnum = (feature_pauth == NULL) ? -1
3352 : pauth_ra_state_offset + num_regs;
3353
07b287a0
MS
3354 set_gdbarch_push_dummy_call (gdbarch, aarch64_push_dummy_call);
3355 set_gdbarch_frame_align (gdbarch, aarch64_frame_align);
3356
07b287a0
MS
3357 /* Advance PC across function entry code. */
3358 set_gdbarch_skip_prologue (gdbarch, aarch64_skip_prologue);
3359
3360 /* The stack grows downward. */
3361 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3362
3363 /* Breakpoint manipulation. */
04180708
YQ
3364 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
3365 aarch64_breakpoint::kind_from_pc);
3366 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
3367 aarch64_breakpoint::bp_from_kind);
07b287a0 3368 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
9404b58f 3369 set_gdbarch_software_single_step (gdbarch, aarch64_software_single_step);
07b287a0
MS
3370
3371 /* Information about registers, etc. */
3372 set_gdbarch_sp_regnum (gdbarch, AARCH64_SP_REGNUM);
3373 set_gdbarch_pc_regnum (gdbarch, AARCH64_PC_REGNUM);
3374 set_gdbarch_num_regs (gdbarch, num_regs);
3375
3376 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudo_regs);
3377 set_gdbarch_pseudo_register_read_value (gdbarch, aarch64_pseudo_read_value);
3378 set_gdbarch_pseudo_register_write (gdbarch, aarch64_pseudo_write);
3379 set_tdesc_pseudo_register_name (gdbarch, aarch64_pseudo_register_name);
3380 set_tdesc_pseudo_register_type (gdbarch, aarch64_pseudo_register_type);
3381 set_tdesc_pseudo_register_reggroup_p (gdbarch,
3382 aarch64_pseudo_register_reggroup_p);
76bed0fd 3383 set_gdbarch_cannot_store_register (gdbarch, aarch64_cannot_store_register);
07b287a0
MS
3384
3385 /* ABI */
3386 set_gdbarch_short_bit (gdbarch, 16);
3387 set_gdbarch_int_bit (gdbarch, 32);
3388 set_gdbarch_float_bit (gdbarch, 32);
3389 set_gdbarch_double_bit (gdbarch, 64);
3390 set_gdbarch_long_double_bit (gdbarch, 128);
3391 set_gdbarch_long_bit (gdbarch, 64);
3392 set_gdbarch_long_long_bit (gdbarch, 64);
3393 set_gdbarch_ptr_bit (gdbarch, 64);
3394 set_gdbarch_char_signed (gdbarch, 0);
53375380 3395 set_gdbarch_wchar_signed (gdbarch, 0);
07b287a0
MS
3396 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
3397 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
3398 set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
b907456c 3399 set_gdbarch_type_align (gdbarch, aarch64_type_align);
07b287a0
MS
3400
3401 /* Internal <-> external register number maps. */
3402 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, aarch64_dwarf_reg_to_regnum);
3403
3404 /* Returning results. */
3405 set_gdbarch_return_value (gdbarch, aarch64_return_value);
3406
3407 /* Disassembly. */
3408 set_gdbarch_print_insn (gdbarch, aarch64_gdb_print_insn);
3409
3410 /* Virtual tables. */
3411 set_gdbarch_vbit_in_delta (gdbarch, 1);
3412
0ef8a082
AH
3413 /* Register architecture. */
3414 aarch64_add_reggroups (gdbarch);
3415
07b287a0
MS
3416 /* Hook in the ABI-specific overrides, if they have been registered. */
3417 info.target_desc = tdesc;
0dba2a6c 3418 info.tdesc_data = tdesc_data;
07b287a0
MS
3419 gdbarch_init_osabi (info, gdbarch);
3420
3421 dwarf2_frame_set_init_reg (gdbarch, aarch64_dwarf2_frame_init_reg);
11e1b75f
AH
3422 /* Register DWARF CFA vendor handler. */
3423 set_gdbarch_execute_dwarf_cfa_vendor_op (gdbarch,
3424 aarch64_execute_dwarf_cfa_vendor_op);
07b287a0 3425
5133a315
LM
3426 /* Permanent/Program breakpoint handling. */
3427 set_gdbarch_program_breakpoint_here_p (gdbarch,
3428 aarch64_program_breakpoint_here_p);
3429
07b287a0
MS
3430 /* Add some default predicates. */
3431 frame_unwind_append_unwinder (gdbarch, &aarch64_stub_unwind);
3432 dwarf2_append_unwinders (gdbarch);
3433 frame_unwind_append_unwinder (gdbarch, &aarch64_prologue_unwind);
3434
3435 frame_base_set_default (gdbarch, &aarch64_normal_base);
3436
3437 /* Now we have tuned the configuration, set a few final things,
3438 based on what the OS ABI has told us. */
3439
3440 if (tdep->jb_pc >= 0)
3441 set_gdbarch_get_longjmp_target (gdbarch, aarch64_get_longjmp_target);
3442
ea873d8e
PL
3443 set_gdbarch_gen_return_address (gdbarch, aarch64_gen_return_address);
3444
aa7ca1bb
AH
3445 set_gdbarch_get_pc_address_flags (gdbarch, aarch64_get_pc_address_flags);
3446
07b287a0
MS
3447 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
3448
3449 /* Add standard register aliases. */
3450 for (i = 0; i < ARRAY_SIZE (aarch64_register_aliases); i++)
3451 user_reg_add (gdbarch, aarch64_register_aliases[i].name,
3452 value_of_aarch64_user_reg,
3453 &aarch64_register_aliases[i].regnum);
3454
e8bf1ce4
JB
3455 register_aarch64_ravenscar_ops (gdbarch);
3456
07b287a0
MS
3457 return gdbarch;
3458}
3459
3460static void
3461aarch64_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3462{
3463 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3464
3465 if (tdep == NULL)
3466 return;
3467
3468 fprintf_unfiltered (file, _("aarch64_dump_tdep: Lowest pc = 0x%s"),
3469 paddress (gdbarch, tdep->lowest_pc));
3470}
3471
0d4c07af 3472#if GDB_SELF_TEST
1e2b521d
YQ
3473namespace selftests
3474{
3475static void aarch64_process_record_test (void);
3476}
0d4c07af 3477#endif
1e2b521d 3478
6c265988 3479void _initialize_aarch64_tdep ();
07b287a0 3480void
6c265988 3481_initialize_aarch64_tdep ()
07b287a0
MS
3482{
3483 gdbarch_register (bfd_arch_aarch64, aarch64_gdbarch_init,
3484 aarch64_dump_tdep);
3485
07b287a0
MS
3486 /* Debug this file's internals. */
3487 add_setshow_boolean_cmd ("aarch64", class_maintenance, &aarch64_debug, _("\
3488Set AArch64 debugging."), _("\
3489Show AArch64 debugging."), _("\
3490When on, AArch64 specific debugging is enabled."),
3491 NULL,
3492 show_aarch64_debug,
3493 &setdebuglist, &showdebuglist);
4d9a9006
YQ
3494
3495#if GDB_SELF_TEST
1526853e
SM
3496 selftests::register_test ("aarch64-analyze-prologue",
3497 selftests::aarch64_analyze_prologue_test);
3498 selftests::register_test ("aarch64-process-record",
3499 selftests::aarch64_process_record_test);
4d9a9006 3500#endif
07b287a0 3501}
99afc88b
OJ
3502
3503/* AArch64 process record-replay related structures, defines etc. */
3504
99afc88b
OJ
3505#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
3506 do \
3507 { \
3508 unsigned int reg_len = LENGTH; \
3509 if (reg_len) \
3510 { \
3511 REGS = XNEWVEC (uint32_t, reg_len); \
3512 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
3513 } \
3514 } \
3515 while (0)
3516
3517#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
3518 do \
3519 { \
3520 unsigned int mem_len = LENGTH; \
3521 if (mem_len) \
3522 { \
3523 MEMS = XNEWVEC (struct aarch64_mem_r, mem_len); \
3524 memcpy(&MEMS->len, &RECORD_BUF[0], \
3525 sizeof(struct aarch64_mem_r) * LENGTH); \
3526 } \
3527 } \
3528 while (0)
3529
3530/* AArch64 record/replay structures and enumerations. */
3531
3532struct aarch64_mem_r
3533{
3534 uint64_t len; /* Record length. */
3535 uint64_t addr; /* Memory address. */
3536};
3537
3538enum aarch64_record_result
3539{
3540 AARCH64_RECORD_SUCCESS,
99afc88b
OJ
3541 AARCH64_RECORD_UNSUPPORTED,
3542 AARCH64_RECORD_UNKNOWN
3543};
3544
3545typedef struct insn_decode_record_t
3546{
3547 struct gdbarch *gdbarch;
3548 struct regcache *regcache;
3549 CORE_ADDR this_addr; /* Address of insn to be recorded. */
3550 uint32_t aarch64_insn; /* Insn to be recorded. */
3551 uint32_t mem_rec_count; /* Count of memory records. */
3552 uint32_t reg_rec_count; /* Count of register records. */
3553 uint32_t *aarch64_regs; /* Registers to be recorded. */
3554 struct aarch64_mem_r *aarch64_mems; /* Memory locations to be recorded. */
3555} insn_decode_record;
3556
3557/* Record handler for data processing - register instructions. */
3558
3559static unsigned int
3560aarch64_record_data_proc_reg (insn_decode_record *aarch64_insn_r)
3561{
3562 uint8_t reg_rd, insn_bits24_27, insn_bits21_23;
3563 uint32_t record_buf[4];
3564
3565 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3566 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3567 insn_bits21_23 = bits (aarch64_insn_r->aarch64_insn, 21, 23);
3568
3569 if (!bit (aarch64_insn_r->aarch64_insn, 28))
3570 {
3571 uint8_t setflags;
3572
3573 /* Logical (shifted register). */
3574 if (insn_bits24_27 == 0x0a)
3575 setflags = (bits (aarch64_insn_r->aarch64_insn, 29, 30) == 0x03);
3576 /* Add/subtract. */
3577 else if (insn_bits24_27 == 0x0b)
3578 setflags = bit (aarch64_insn_r->aarch64_insn, 29);
3579 else
3580 return AARCH64_RECORD_UNKNOWN;
3581
3582 record_buf[0] = reg_rd;
3583 aarch64_insn_r->reg_rec_count = 1;
3584 if (setflags)
3585 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
3586 }
3587 else
3588 {
3589 if (insn_bits24_27 == 0x0b)
3590 {
3591 /* Data-processing (3 source). */
3592 record_buf[0] = reg_rd;
3593 aarch64_insn_r->reg_rec_count = 1;
3594 }
3595 else if (insn_bits24_27 == 0x0a)
3596 {
3597 if (insn_bits21_23 == 0x00)
3598 {
3599 /* Add/subtract (with carry). */
3600 record_buf[0] = reg_rd;
3601 aarch64_insn_r->reg_rec_count = 1;
3602 if (bit (aarch64_insn_r->aarch64_insn, 29))
3603 {
3604 record_buf[1] = AARCH64_CPSR_REGNUM;
3605 aarch64_insn_r->reg_rec_count = 2;
3606 }
3607 }
3608 else if (insn_bits21_23 == 0x02)
3609 {
3610 /* Conditional compare (register) and conditional compare
3611 (immediate) instructions. */
3612 record_buf[0] = AARCH64_CPSR_REGNUM;
3613 aarch64_insn_r->reg_rec_count = 1;
3614 }
3615 else if (insn_bits21_23 == 0x04 || insn_bits21_23 == 0x06)
3616 {
85102364 3617 /* Conditional select. */
99afc88b
OJ
3618 /* Data-processing (2 source). */
3619 /* Data-processing (1 source). */
3620 record_buf[0] = reg_rd;
3621 aarch64_insn_r->reg_rec_count = 1;
3622 }
3623 else
3624 return AARCH64_RECORD_UNKNOWN;
3625 }
3626 }
3627
3628 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3629 record_buf);
3630 return AARCH64_RECORD_SUCCESS;
3631}
3632
3633/* Record handler for data processing - immediate instructions. */
3634
3635static unsigned int
3636aarch64_record_data_proc_imm (insn_decode_record *aarch64_insn_r)
3637{
78cc6c2d 3638 uint8_t reg_rd, insn_bit23, insn_bits24_27, setflags;
99afc88b
OJ
3639 uint32_t record_buf[4];
3640
3641 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
99afc88b
OJ
3642 insn_bit23 = bit (aarch64_insn_r->aarch64_insn, 23);
3643 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3644
3645 if (insn_bits24_27 == 0x00 /* PC rel addressing. */
3646 || insn_bits24_27 == 0x03 /* Bitfield and Extract. */
3647 || (insn_bits24_27 == 0x02 && insn_bit23)) /* Move wide (immediate). */
3648 {
3649 record_buf[0] = reg_rd;
3650 aarch64_insn_r->reg_rec_count = 1;
3651 }
3652 else if (insn_bits24_27 == 0x01)
3653 {
3654 /* Add/Subtract (immediate). */
3655 setflags = bit (aarch64_insn_r->aarch64_insn, 29);
3656 record_buf[0] = reg_rd;
3657 aarch64_insn_r->reg_rec_count = 1;
3658 if (setflags)
3659 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
3660 }
3661 else if (insn_bits24_27 == 0x02 && !insn_bit23)
3662 {
3663 /* Logical (immediate). */
3664 setflags = bits (aarch64_insn_r->aarch64_insn, 29, 30) == 0x03;
3665 record_buf[0] = reg_rd;
3666 aarch64_insn_r->reg_rec_count = 1;
3667 if (setflags)
3668 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
3669 }
3670 else
3671 return AARCH64_RECORD_UNKNOWN;
3672
3673 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3674 record_buf);
3675 return AARCH64_RECORD_SUCCESS;
3676}
3677
3678/* Record handler for branch, exception generation and system instructions. */
3679
3680static unsigned int
3681aarch64_record_branch_except_sys (insn_decode_record *aarch64_insn_r)
3682{
3683 struct gdbarch_tdep *tdep = gdbarch_tdep (aarch64_insn_r->gdbarch);
3684 uint8_t insn_bits24_27, insn_bits28_31, insn_bits22_23;
3685 uint32_t record_buf[4];
3686
3687 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3688 insn_bits28_31 = bits (aarch64_insn_r->aarch64_insn, 28, 31);
3689 insn_bits22_23 = bits (aarch64_insn_r->aarch64_insn, 22, 23);
3690
3691 if (insn_bits28_31 == 0x0d)
3692 {
3693 /* Exception generation instructions. */
3694 if (insn_bits24_27 == 0x04)
3695 {
5d98d3cd
YQ
3696 if (!bits (aarch64_insn_r->aarch64_insn, 2, 4)
3697 && !bits (aarch64_insn_r->aarch64_insn, 21, 23)
3698 && bits (aarch64_insn_r->aarch64_insn, 0, 1) == 0x01)
99afc88b
OJ
3699 {
3700 ULONGEST svc_number;
3701
3702 regcache_raw_read_unsigned (aarch64_insn_r->regcache, 8,
3703 &svc_number);
3704 return tdep->aarch64_syscall_record (aarch64_insn_r->regcache,
3705 svc_number);
3706 }
3707 else
3708 return AARCH64_RECORD_UNSUPPORTED;
3709 }
3710 /* System instructions. */
3711 else if (insn_bits24_27 == 0x05 && insn_bits22_23 == 0x00)
3712 {
3713 uint32_t reg_rt, reg_crn;
3714
3715 reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3716 reg_crn = bits (aarch64_insn_r->aarch64_insn, 12, 15);
3717
3718 /* Record rt in case of sysl and mrs instructions. */
3719 if (bit (aarch64_insn_r->aarch64_insn, 21))
3720 {
3721 record_buf[0] = reg_rt;
3722 aarch64_insn_r->reg_rec_count = 1;
3723 }
3724 /* Record cpsr for hint and msr(immediate) instructions. */
3725 else if (reg_crn == 0x02 || reg_crn == 0x04)
3726 {
3727 record_buf[0] = AARCH64_CPSR_REGNUM;
3728 aarch64_insn_r->reg_rec_count = 1;
3729 }
3730 }
3731 /* Unconditional branch (register). */
3732 else if((insn_bits24_27 & 0x0e) == 0x06)
3733 {
3734 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
3735 if (bits (aarch64_insn_r->aarch64_insn, 21, 22) == 0x01)
3736 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_LR_REGNUM;
3737 }
3738 else
3739 return AARCH64_RECORD_UNKNOWN;
3740 }
3741 /* Unconditional branch (immediate). */
3742 else if ((insn_bits28_31 & 0x07) == 0x01 && (insn_bits24_27 & 0x0c) == 0x04)
3743 {
3744 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
3745 if (bit (aarch64_insn_r->aarch64_insn, 31))
3746 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_LR_REGNUM;
3747 }
3748 else
3749 /* Compare & branch (immediate), Test & branch (immediate) and
3750 Conditional branch (immediate). */
3751 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
3752
3753 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3754 record_buf);
3755 return AARCH64_RECORD_SUCCESS;
3756}
3757
3758/* Record handler for advanced SIMD load and store instructions. */
3759
3760static unsigned int
3761aarch64_record_asimd_load_store (insn_decode_record *aarch64_insn_r)
3762{
3763 CORE_ADDR address;
3764 uint64_t addr_offset = 0;
3765 uint32_t record_buf[24];
3766 uint64_t record_buf_mem[24];
3767 uint32_t reg_rn, reg_rt;
3768 uint32_t reg_index = 0, mem_index = 0;
3769 uint8_t opcode_bits, size_bits;
3770
3771 reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3772 reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
3773 size_bits = bits (aarch64_insn_r->aarch64_insn, 10, 11);
3774 opcode_bits = bits (aarch64_insn_r->aarch64_insn, 12, 15);
3775 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn, &address);
3776
3777 if (record_debug)
b277c936 3778 debug_printf ("Process record: Advanced SIMD load/store\n");
99afc88b
OJ
3779
3780 /* Load/store single structure. */
3781 if (bit (aarch64_insn_r->aarch64_insn, 24))
3782 {
3783 uint8_t sindex, scale, selem, esize, replicate = 0;
3784 scale = opcode_bits >> 2;
3785 selem = ((opcode_bits & 0x02) |
3786 bit (aarch64_insn_r->aarch64_insn, 21)) + 1;
3787 switch (scale)
3788 {
3789 case 1:
3790 if (size_bits & 0x01)
3791 return AARCH64_RECORD_UNKNOWN;
3792 break;
3793 case 2:
3794 if ((size_bits >> 1) & 0x01)
3795 return AARCH64_RECORD_UNKNOWN;
3796 if (size_bits & 0x01)
3797 {
3798 if (!((opcode_bits >> 1) & 0x01))
3799 scale = 3;
3800 else
3801 return AARCH64_RECORD_UNKNOWN;
3802 }
3803 break;
3804 case 3:
3805 if (bit (aarch64_insn_r->aarch64_insn, 22) && !(opcode_bits & 0x01))
3806 {
3807 scale = size_bits;
3808 replicate = 1;
3809 break;
3810 }
3811 else
3812 return AARCH64_RECORD_UNKNOWN;
3813 default:
3814 break;
3815 }
3816 esize = 8 << scale;
3817 if (replicate)
3818 for (sindex = 0; sindex < selem; sindex++)
3819 {
3820 record_buf[reg_index++] = reg_rt + AARCH64_V0_REGNUM;
3821 reg_rt = (reg_rt + 1) % 32;
3822 }
3823 else
3824 {
3825 for (sindex = 0; sindex < selem; sindex++)
a2e3e93f
SM
3826 {
3827 if (bit (aarch64_insn_r->aarch64_insn, 22))
3828 record_buf[reg_index++] = reg_rt + AARCH64_V0_REGNUM;
3829 else
3830 {
3831 record_buf_mem[mem_index++] = esize / 8;
3832 record_buf_mem[mem_index++] = address + addr_offset;
3833 }
3834 addr_offset = addr_offset + (esize / 8);
3835 reg_rt = (reg_rt + 1) % 32;
3836 }
99afc88b
OJ
3837 }
3838 }
3839 /* Load/store multiple structure. */
3840 else
3841 {
3842 uint8_t selem, esize, rpt, elements;
3843 uint8_t eindex, rindex;
3844
3845 esize = 8 << size_bits;
3846 if (bit (aarch64_insn_r->aarch64_insn, 30))
3847 elements = 128 / esize;
3848 else
3849 elements = 64 / esize;
3850
3851 switch (opcode_bits)
3852 {
3853 /*LD/ST4 (4 Registers). */
3854 case 0:
3855 rpt = 1;
3856 selem = 4;
3857 break;
3858 /*LD/ST1 (4 Registers). */
3859 case 2:
3860 rpt = 4;
3861 selem = 1;
3862 break;
3863 /*LD/ST3 (3 Registers). */
3864 case 4:
3865 rpt = 1;
3866 selem = 3;
3867 break;
3868 /*LD/ST1 (3 Registers). */
3869 case 6:
3870 rpt = 3;
3871 selem = 1;
3872 break;
3873 /*LD/ST1 (1 Register). */
3874 case 7:
3875 rpt = 1;
3876 selem = 1;
3877 break;
3878 /*LD/ST2 (2 Registers). */
3879 case 8:
3880 rpt = 1;
3881 selem = 2;
3882 break;
3883 /*LD/ST1 (2 Registers). */
3884 case 10:
3885 rpt = 2;
3886 selem = 1;
3887 break;
3888 default:
3889 return AARCH64_RECORD_UNSUPPORTED;
3890 break;
3891 }
3892 for (rindex = 0; rindex < rpt; rindex++)
3893 for (eindex = 0; eindex < elements; eindex++)
3894 {
3895 uint8_t reg_tt, sindex;
3896 reg_tt = (reg_rt + rindex) % 32;
3897 for (sindex = 0; sindex < selem; sindex++)
3898 {
3899 if (bit (aarch64_insn_r->aarch64_insn, 22))
3900 record_buf[reg_index++] = reg_tt + AARCH64_V0_REGNUM;
3901 else
3902 {
3903 record_buf_mem[mem_index++] = esize / 8;
3904 record_buf_mem[mem_index++] = address + addr_offset;
3905 }
3906 addr_offset = addr_offset + (esize / 8);
3907 reg_tt = (reg_tt + 1) % 32;
3908 }
3909 }
3910 }
3911
3912 if (bit (aarch64_insn_r->aarch64_insn, 23))
3913 record_buf[reg_index++] = reg_rn;
3914
3915 aarch64_insn_r->reg_rec_count = reg_index;
3916 aarch64_insn_r->mem_rec_count = mem_index / 2;
3917 MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
3918 record_buf_mem);
3919 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3920 record_buf);
3921 return AARCH64_RECORD_SUCCESS;
3922}
3923
3924/* Record handler for load and store instructions. */
3925
3926static unsigned int
3927aarch64_record_load_store (insn_decode_record *aarch64_insn_r)
3928{
3929 uint8_t insn_bits24_27, insn_bits28_29, insn_bits10_11;
3930 uint8_t insn_bit23, insn_bit21;
3931 uint8_t opc, size_bits, ld_flag, vector_flag;
3932 uint32_t reg_rn, reg_rt, reg_rt2;
3933 uint64_t datasize, offset;
3934 uint32_t record_buf[8];
3935 uint64_t record_buf_mem[8];
3936 CORE_ADDR address;
3937
3938 insn_bits10_11 = bits (aarch64_insn_r->aarch64_insn, 10, 11);
3939 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3940 insn_bits28_29 = bits (aarch64_insn_r->aarch64_insn, 28, 29);
3941 insn_bit21 = bit (aarch64_insn_r->aarch64_insn, 21);
3942 insn_bit23 = bit (aarch64_insn_r->aarch64_insn, 23);
3943 ld_flag = bit (aarch64_insn_r->aarch64_insn, 22);
3944 vector_flag = bit (aarch64_insn_r->aarch64_insn, 26);
3945 reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3946 reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
3947 reg_rt2 = bits (aarch64_insn_r->aarch64_insn, 10, 14);
3948 size_bits = bits (aarch64_insn_r->aarch64_insn, 30, 31);
3949
3950 /* Load/store exclusive. */
3951 if (insn_bits24_27 == 0x08 && insn_bits28_29 == 0x00)
3952 {
3953 if (record_debug)
b277c936 3954 debug_printf ("Process record: load/store exclusive\n");
99afc88b
OJ
3955
3956 if (ld_flag)
3957 {
3958 record_buf[0] = reg_rt;
3959 aarch64_insn_r->reg_rec_count = 1;
3960 if (insn_bit21)
3961 {
3962 record_buf[1] = reg_rt2;
3963 aarch64_insn_r->reg_rec_count = 2;
3964 }
3965 }
3966 else
3967 {
3968 if (insn_bit21)
3969 datasize = (8 << size_bits) * 2;
3970 else
3971 datasize = (8 << size_bits);
3972 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3973 &address);
3974 record_buf_mem[0] = datasize / 8;
3975 record_buf_mem[1] = address;
3976 aarch64_insn_r->mem_rec_count = 1;
3977 if (!insn_bit23)
3978 {
3979 /* Save register rs. */
3980 record_buf[0] = bits (aarch64_insn_r->aarch64_insn, 16, 20);
3981 aarch64_insn_r->reg_rec_count = 1;
3982 }
3983 }
3984 }
3985 /* Load register (literal) instructions decoding. */
3986 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x01)
3987 {
3988 if (record_debug)
b277c936 3989 debug_printf ("Process record: load register (literal)\n");
99afc88b
OJ
3990 if (vector_flag)
3991 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3992 else
3993 record_buf[0] = reg_rt;
3994 aarch64_insn_r->reg_rec_count = 1;
3995 }
3996 /* All types of load/store pair instructions decoding. */
3997 else if ((insn_bits24_27 & 0x0a) == 0x08 && insn_bits28_29 == 0x02)
3998 {
3999 if (record_debug)
b277c936 4000 debug_printf ("Process record: load/store pair\n");
99afc88b
OJ
4001
4002 if (ld_flag)
4003 {
4004 if (vector_flag)
4005 {
4006 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
4007 record_buf[1] = reg_rt2 + AARCH64_V0_REGNUM;
4008 }
4009 else
4010 {
4011 record_buf[0] = reg_rt;
4012 record_buf[1] = reg_rt2;
4013 }
4014 aarch64_insn_r->reg_rec_count = 2;
4015 }
4016 else
4017 {
4018 uint16_t imm7_off;
4019 imm7_off = bits (aarch64_insn_r->aarch64_insn, 15, 21);
4020 if (!vector_flag)
4021 size_bits = size_bits >> 1;
4022 datasize = 8 << (2 + size_bits);
4023 offset = (imm7_off & 0x40) ? (~imm7_off & 0x007f) + 1 : imm7_off;
4024 offset = offset << (2 + size_bits);
4025 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
4026 &address);
4027 if (!((insn_bits24_27 & 0x0b) == 0x08 && insn_bit23))
4028 {
4029 if (imm7_off & 0x40)
4030 address = address - offset;
4031 else
4032 address = address + offset;
4033 }
4034
4035 record_buf_mem[0] = datasize / 8;
4036 record_buf_mem[1] = address;
4037 record_buf_mem[2] = datasize / 8;
4038 record_buf_mem[3] = address + (datasize / 8);
4039 aarch64_insn_r->mem_rec_count = 2;
4040 }
4041 if (bit (aarch64_insn_r->aarch64_insn, 23))
4042 record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
4043 }
4044 /* Load/store register (unsigned immediate) instructions. */
4045 else if ((insn_bits24_27 & 0x0b) == 0x09 && insn_bits28_29 == 0x03)
4046 {
4047 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
4048 if (!(opc >> 1))
33877125
YQ
4049 {
4050 if (opc & 0x01)
4051 ld_flag = 0x01;
4052 else
4053 ld_flag = 0x0;
4054 }
99afc88b 4055 else
33877125 4056 {
1e2b521d
YQ
4057 if (size_bits == 0x3 && vector_flag == 0x0 && opc == 0x2)
4058 {
4059 /* PRFM (immediate) */
4060 return AARCH64_RECORD_SUCCESS;
4061 }
4062 else if (size_bits == 0x2 && vector_flag == 0x0 && opc == 0x2)
4063 {
4064 /* LDRSW (immediate) */
4065 ld_flag = 0x1;
4066 }
33877125 4067 else
1e2b521d
YQ
4068 {
4069 if (opc & 0x01)
4070 ld_flag = 0x01;
4071 else
4072 ld_flag = 0x0;
4073 }
33877125 4074 }
99afc88b
OJ
4075
4076 if (record_debug)
4077 {
b277c936
PL
4078 debug_printf ("Process record: load/store (unsigned immediate):"
4079 " size %x V %d opc %x\n", size_bits, vector_flag,
4080 opc);
99afc88b
OJ
4081 }
4082
4083 if (!ld_flag)
4084 {
4085 offset = bits (aarch64_insn_r->aarch64_insn, 10, 21);
4086 datasize = 8 << size_bits;
4087 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
4088 &address);
4089 offset = offset << size_bits;
4090 address = address + offset;
4091
4092 record_buf_mem[0] = datasize >> 3;
4093 record_buf_mem[1] = address;
4094 aarch64_insn_r->mem_rec_count = 1;
4095 }
4096 else
4097 {
4098 if (vector_flag)
4099 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
4100 else
4101 record_buf[0] = reg_rt;
4102 aarch64_insn_r->reg_rec_count = 1;
4103 }
4104 }
4105 /* Load/store register (register offset) instructions. */
5d98d3cd
YQ
4106 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
4107 && insn_bits10_11 == 0x02 && insn_bit21)
99afc88b
OJ
4108 {
4109 if (record_debug)
b277c936 4110 debug_printf ("Process record: load/store (register offset)\n");
99afc88b
OJ
4111 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
4112 if (!(opc >> 1))
4113 if (opc & 0x01)
4114 ld_flag = 0x01;
4115 else
4116 ld_flag = 0x0;
4117 else
4118 if (size_bits != 0x03)
4119 ld_flag = 0x01;
4120 else
4121 return AARCH64_RECORD_UNKNOWN;
4122
4123 if (!ld_flag)
4124 {
d9436c7c
PA
4125 ULONGEST reg_rm_val;
4126
99afc88b
OJ
4127 regcache_raw_read_unsigned (aarch64_insn_r->regcache,
4128 bits (aarch64_insn_r->aarch64_insn, 16, 20), &reg_rm_val);
4129 if (bit (aarch64_insn_r->aarch64_insn, 12))
4130 offset = reg_rm_val << size_bits;
4131 else
4132 offset = reg_rm_val;
4133 datasize = 8 << size_bits;
4134 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
4135 &address);
4136 address = address + offset;
4137 record_buf_mem[0] = datasize >> 3;
4138 record_buf_mem[1] = address;
4139 aarch64_insn_r->mem_rec_count = 1;
4140 }
4141 else
4142 {
4143 if (vector_flag)
4144 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
4145 else
4146 record_buf[0] = reg_rt;
4147 aarch64_insn_r->reg_rec_count = 1;
4148 }
4149 }
4150 /* Load/store register (immediate and unprivileged) instructions. */
5d98d3cd
YQ
4151 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
4152 && !insn_bit21)
99afc88b
OJ
4153 {
4154 if (record_debug)
4155 {
b277c936
PL
4156 debug_printf ("Process record: load/store "
4157 "(immediate and unprivileged)\n");
99afc88b
OJ
4158 }
4159 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
4160 if (!(opc >> 1))
4161 if (opc & 0x01)
4162 ld_flag = 0x01;
4163 else
4164 ld_flag = 0x0;
4165 else
4166 if (size_bits != 0x03)
4167 ld_flag = 0x01;
4168 else
4169 return AARCH64_RECORD_UNKNOWN;
4170
4171 if (!ld_flag)
4172 {
4173 uint16_t imm9_off;
4174 imm9_off = bits (aarch64_insn_r->aarch64_insn, 12, 20);
4175 offset = (imm9_off & 0x0100) ? (((~imm9_off) & 0x01ff) + 1) : imm9_off;
4176 datasize = 8 << size_bits;
4177 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
4178 &address);
4179 if (insn_bits10_11 != 0x01)
4180 {
4181 if (imm9_off & 0x0100)
4182 address = address - offset;
4183 else
4184 address = address + offset;
4185 }
4186 record_buf_mem[0] = datasize >> 3;
4187 record_buf_mem[1] = address;
4188 aarch64_insn_r->mem_rec_count = 1;
4189 }
4190 else
4191 {
4192 if (vector_flag)
4193 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
4194 else
4195 record_buf[0] = reg_rt;
4196 aarch64_insn_r->reg_rec_count = 1;
4197 }
4198 if (insn_bits10_11 == 0x01 || insn_bits10_11 == 0x03)
4199 record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
4200 }
4201 /* Advanced SIMD load/store instructions. */
4202 else
4203 return aarch64_record_asimd_load_store (aarch64_insn_r);
4204
4205 MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
4206 record_buf_mem);
4207 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
4208 record_buf);
4209 return AARCH64_RECORD_SUCCESS;
4210}
4211
4212/* Record handler for data processing SIMD and floating point instructions. */
4213
4214static unsigned int
4215aarch64_record_data_proc_simd_fp (insn_decode_record *aarch64_insn_r)
4216{
4217 uint8_t insn_bit21, opcode, rmode, reg_rd;
4218 uint8_t insn_bits24_27, insn_bits28_31, insn_bits10_11, insn_bits12_15;
4219 uint8_t insn_bits11_14;
4220 uint32_t record_buf[2];
4221
4222 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
4223 insn_bits28_31 = bits (aarch64_insn_r->aarch64_insn, 28, 31);
4224 insn_bits10_11 = bits (aarch64_insn_r->aarch64_insn, 10, 11);
4225 insn_bits12_15 = bits (aarch64_insn_r->aarch64_insn, 12, 15);
4226 insn_bits11_14 = bits (aarch64_insn_r->aarch64_insn, 11, 14);
4227 opcode = bits (aarch64_insn_r->aarch64_insn, 16, 18);
4228 rmode = bits (aarch64_insn_r->aarch64_insn, 19, 20);
4229 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
4230 insn_bit21 = bit (aarch64_insn_r->aarch64_insn, 21);
4231
4232 if (record_debug)
b277c936 4233 debug_printf ("Process record: data processing SIMD/FP: ");
99afc88b
OJ
4234
4235 if ((insn_bits28_31 & 0x05) == 0x01 && insn_bits24_27 == 0x0e)
4236 {
4237 /* Floating point - fixed point conversion instructions. */
4238 if (!insn_bit21)
4239 {
4240 if (record_debug)
b277c936 4241 debug_printf ("FP - fixed point conversion");
99afc88b
OJ
4242
4243 if ((opcode >> 1) == 0x0 && rmode == 0x03)
4244 record_buf[0] = reg_rd;
4245 else
4246 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
4247 }
4248 /* Floating point - conditional compare instructions. */
4249 else if (insn_bits10_11 == 0x01)
4250 {
4251 if (record_debug)
b277c936 4252 debug_printf ("FP - conditional compare");
99afc88b
OJ
4253
4254 record_buf[0] = AARCH64_CPSR_REGNUM;
4255 }
4256 /* Floating point - data processing (2-source) and
4257 conditional select instructions. */
4258 else if (insn_bits10_11 == 0x02 || insn_bits10_11 == 0x03)
4259 {
4260 if (record_debug)
b277c936 4261 debug_printf ("FP - DP (2-source)");
99afc88b
OJ
4262
4263 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
4264 }
4265 else if (insn_bits10_11 == 0x00)
4266 {
4267 /* Floating point - immediate instructions. */
4268 if ((insn_bits12_15 & 0x01) == 0x01
4269 || (insn_bits12_15 & 0x07) == 0x04)
4270 {
4271 if (record_debug)
b277c936 4272 debug_printf ("FP - immediate");
99afc88b
OJ
4273 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
4274 }
4275 /* Floating point - compare instructions. */
4276 else if ((insn_bits12_15 & 0x03) == 0x02)
4277 {
4278 if (record_debug)
b277c936 4279 debug_printf ("FP - immediate");
99afc88b
OJ
4280 record_buf[0] = AARCH64_CPSR_REGNUM;
4281 }
4282 /* Floating point - integer conversions instructions. */
f62fce35 4283 else if (insn_bits12_15 == 0x00)
99afc88b
OJ
4284 {
4285 /* Convert float to integer instruction. */
4286 if (!(opcode >> 1) || ((opcode >> 1) == 0x02 && !rmode))
4287 {
4288 if (record_debug)
b277c936 4289 debug_printf ("float to int conversion");
99afc88b
OJ
4290
4291 record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
4292 }
4293 /* Convert integer to float instruction. */
4294 else if ((opcode >> 1) == 0x01 && !rmode)
4295 {
4296 if (record_debug)
b277c936 4297 debug_printf ("int to float conversion");
99afc88b
OJ
4298
4299 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
4300 }
4301 /* Move float to integer instruction. */
4302 else if ((opcode >> 1) == 0x03)
4303 {
4304 if (record_debug)
b277c936 4305 debug_printf ("move float to int");
99afc88b
OJ
4306
4307 if (!(opcode & 0x01))
4308 record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
4309 else
4310 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
4311 }
f62fce35
YQ
4312 else
4313 return AARCH64_RECORD_UNKNOWN;
99afc88b 4314 }
f62fce35
YQ
4315 else
4316 return AARCH64_RECORD_UNKNOWN;
99afc88b 4317 }
f62fce35
YQ
4318 else
4319 return AARCH64_RECORD_UNKNOWN;
99afc88b
OJ
4320 }
4321 else if ((insn_bits28_31 & 0x09) == 0x00 && insn_bits24_27 == 0x0e)
4322 {
4323 if (record_debug)
b277c936 4324 debug_printf ("SIMD copy");
99afc88b
OJ
4325
4326 /* Advanced SIMD copy instructions. */
4327 if (!bits (aarch64_insn_r->aarch64_insn, 21, 23)
4328 && !bit (aarch64_insn_r->aarch64_insn, 15)
4329 && bit (aarch64_insn_r->aarch64_insn, 10))
4330 {
4331 if (insn_bits11_14 == 0x05 || insn_bits11_14 == 0x07)
4332 record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
4333 else
4334 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
4335 }
4336 else
4337 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
4338 }
4339 /* All remaining floating point or advanced SIMD instructions. */
4340 else
4341 {
4342 if (record_debug)
b277c936 4343 debug_printf ("all remain");
99afc88b
OJ
4344
4345 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
4346 }
4347
4348 if (record_debug)
b277c936 4349 debug_printf ("\n");
99afc88b
OJ
4350
4351 aarch64_insn_r->reg_rec_count++;
4352 gdb_assert (aarch64_insn_r->reg_rec_count == 1);
4353 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
4354 record_buf);
4355 return AARCH64_RECORD_SUCCESS;
4356}
4357
4358/* Decodes insns type and invokes its record handler. */
4359
4360static unsigned int
4361aarch64_record_decode_insn_handler (insn_decode_record *aarch64_insn_r)
4362{
4363 uint32_t ins_bit25, ins_bit26, ins_bit27, ins_bit28;
4364
4365 ins_bit25 = bit (aarch64_insn_r->aarch64_insn, 25);
4366 ins_bit26 = bit (aarch64_insn_r->aarch64_insn, 26);
4367 ins_bit27 = bit (aarch64_insn_r->aarch64_insn, 27);
4368 ins_bit28 = bit (aarch64_insn_r->aarch64_insn, 28);
4369
4370 /* Data processing - immediate instructions. */
4371 if (!ins_bit26 && !ins_bit27 && ins_bit28)
4372 return aarch64_record_data_proc_imm (aarch64_insn_r);
4373
4374 /* Branch, exception generation and system instructions. */
4375 if (ins_bit26 && !ins_bit27 && ins_bit28)
4376 return aarch64_record_branch_except_sys (aarch64_insn_r);
4377
4378 /* Load and store instructions. */
4379 if (!ins_bit25 && ins_bit27)
4380 return aarch64_record_load_store (aarch64_insn_r);
4381
4382 /* Data processing - register instructions. */
4383 if (ins_bit25 && !ins_bit26 && ins_bit27)
4384 return aarch64_record_data_proc_reg (aarch64_insn_r);
4385
4386 /* Data processing - SIMD and floating point instructions. */
4387 if (ins_bit25 && ins_bit26 && ins_bit27)
4388 return aarch64_record_data_proc_simd_fp (aarch64_insn_r);
4389
4390 return AARCH64_RECORD_UNSUPPORTED;
4391}
4392
4393/* Cleans up local record registers and memory allocations. */
4394
4395static void
4396deallocate_reg_mem (insn_decode_record *record)
4397{
4398 xfree (record->aarch64_regs);
4399 xfree (record->aarch64_mems);
4400}
4401
1e2b521d
YQ
4402#if GDB_SELF_TEST
4403namespace selftests {
4404
4405static void
4406aarch64_process_record_test (void)
4407{
4408 struct gdbarch_info info;
4409 uint32_t ret;
4410
4411 gdbarch_info_init (&info);
4412 info.bfd_arch_info = bfd_scan_arch ("aarch64");
4413
4414 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
4415 SELF_CHECK (gdbarch != NULL);
4416
4417 insn_decode_record aarch64_record;
4418
4419 memset (&aarch64_record, 0, sizeof (insn_decode_record));
4420 aarch64_record.regcache = NULL;
4421 aarch64_record.this_addr = 0;
4422 aarch64_record.gdbarch = gdbarch;
4423
4424 /* 20 00 80 f9 prfm pldl1keep, [x1] */
4425 aarch64_record.aarch64_insn = 0xf9800020;
4426 ret = aarch64_record_decode_insn_handler (&aarch64_record);
4427 SELF_CHECK (ret == AARCH64_RECORD_SUCCESS);
4428 SELF_CHECK (aarch64_record.reg_rec_count == 0);
4429 SELF_CHECK (aarch64_record.mem_rec_count == 0);
4430
4431 deallocate_reg_mem (&aarch64_record);
4432}
4433
4434} // namespace selftests
4435#endif /* GDB_SELF_TEST */
4436
99afc88b
OJ
4437/* Parse the current instruction and record the values of the registers and
4438 memory that will be changed in current instruction to record_arch_list
4439 return -1 if something is wrong. */
4440
4441int
4442aarch64_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
4443 CORE_ADDR insn_addr)
4444{
4445 uint32_t rec_no = 0;
4446 uint8_t insn_size = 4;
4447 uint32_t ret = 0;
99afc88b
OJ
4448 gdb_byte buf[insn_size];
4449 insn_decode_record aarch64_record;
4450
4451 memset (&buf[0], 0, insn_size);
4452 memset (&aarch64_record, 0, sizeof (insn_decode_record));
4453 target_read_memory (insn_addr, &buf[0], insn_size);
4454 aarch64_record.aarch64_insn
4455 = (uint32_t) extract_unsigned_integer (&buf[0],
4456 insn_size,
4457 gdbarch_byte_order (gdbarch));
4458 aarch64_record.regcache = regcache;
4459 aarch64_record.this_addr = insn_addr;
4460 aarch64_record.gdbarch = gdbarch;
4461
4462 ret = aarch64_record_decode_insn_handler (&aarch64_record);
4463 if (ret == AARCH64_RECORD_UNSUPPORTED)
4464 {
4465 printf_unfiltered (_("Process record does not support instruction "
4466 "0x%0x at address %s.\n"),
4467 aarch64_record.aarch64_insn,
4468 paddress (gdbarch, insn_addr));
4469 ret = -1;
4470 }
4471
4472 if (0 == ret)
4473 {
4474 /* Record registers. */
4475 record_full_arch_list_add_reg (aarch64_record.regcache,
4476 AARCH64_PC_REGNUM);
4477 /* Always record register CPSR. */
4478 record_full_arch_list_add_reg (aarch64_record.regcache,
4479 AARCH64_CPSR_REGNUM);
4480 if (aarch64_record.aarch64_regs)
4481 for (rec_no = 0; rec_no < aarch64_record.reg_rec_count; rec_no++)
4482 if (record_full_arch_list_add_reg (aarch64_record.regcache,
4483 aarch64_record.aarch64_regs[rec_no]))
4484 ret = -1;
4485
4486 /* Record memories. */
4487 if (aarch64_record.aarch64_mems)
4488 for (rec_no = 0; rec_no < aarch64_record.mem_rec_count; rec_no++)
4489 if (record_full_arch_list_add_mem
4490 ((CORE_ADDR)aarch64_record.aarch64_mems[rec_no].addr,
4491 aarch64_record.aarch64_mems[rec_no].len))
4492 ret = -1;
4493
4494 if (record_full_arch_list_add_end ())
4495 ret = -1;
4496 }
4497
4498 deallocate_reg_mem (&aarch64_record);
4499 return ret;
4500}
This page took 0.686769 seconds and 4 git commands to generate.