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