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