1 /* Common target dependent code for GDB on AArch64 systems.
3 Copyright (C) 2009-2016 Free Software Foundation, Inc.
4 Contributed by ARM Ltd.
6 This file is part of GDB.
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.
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.
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/>. */
29 #include "reggroups.h"
32 #include "arch-utils.h"
34 #include "frame-unwind.h"
35 #include "frame-base.h"
36 #include "trad-frame.h"
38 #include "dwarf2-frame.h"
40 #include "prologue-value.h"
41 #include "target-descriptions.h"
42 #include "user-regs.h"
48 #include "aarch64-tdep.h"
51 #include "elf/aarch64.h"
56 #include "record-full.h"
58 #include "features/aarch64.c"
60 #include "arch/aarch64-insn.h"
62 #include "opcode/aarch64.h"
64 #define submask(x) ((1L << ((x) + 1)) - 1)
65 #define bit(obj,st) (((obj) >> (st)) & 1)
66 #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
68 /* Pseudo register base numbers. */
69 #define AARCH64_Q0_REGNUM 0
70 #define AARCH64_D0_REGNUM (AARCH64_Q0_REGNUM + 32)
71 #define AARCH64_S0_REGNUM (AARCH64_D0_REGNUM + 32)
72 #define AARCH64_H0_REGNUM (AARCH64_S0_REGNUM + 32)
73 #define AARCH64_B0_REGNUM (AARCH64_H0_REGNUM + 32)
75 /* The standard register names, and all the valid aliases for them. */
78 const char *const name;
80 } aarch64_register_aliases[] =
82 /* 64-bit register names. */
83 {"fp", AARCH64_FP_REGNUM},
84 {"lr", AARCH64_LR_REGNUM},
85 {"sp", AARCH64_SP_REGNUM},
87 /* 32-bit register names. */
88 {"w0", AARCH64_X0_REGNUM + 0},
89 {"w1", AARCH64_X0_REGNUM + 1},
90 {"w2", AARCH64_X0_REGNUM + 2},
91 {"w3", AARCH64_X0_REGNUM + 3},
92 {"w4", AARCH64_X0_REGNUM + 4},
93 {"w5", AARCH64_X0_REGNUM + 5},
94 {"w6", AARCH64_X0_REGNUM + 6},
95 {"w7", AARCH64_X0_REGNUM + 7},
96 {"w8", AARCH64_X0_REGNUM + 8},
97 {"w9", AARCH64_X0_REGNUM + 9},
98 {"w10", AARCH64_X0_REGNUM + 10},
99 {"w11", AARCH64_X0_REGNUM + 11},
100 {"w12", AARCH64_X0_REGNUM + 12},
101 {"w13", AARCH64_X0_REGNUM + 13},
102 {"w14", AARCH64_X0_REGNUM + 14},
103 {"w15", AARCH64_X0_REGNUM + 15},
104 {"w16", AARCH64_X0_REGNUM + 16},
105 {"w17", AARCH64_X0_REGNUM + 17},
106 {"w18", AARCH64_X0_REGNUM + 18},
107 {"w19", AARCH64_X0_REGNUM + 19},
108 {"w20", AARCH64_X0_REGNUM + 20},
109 {"w21", AARCH64_X0_REGNUM + 21},
110 {"w22", AARCH64_X0_REGNUM + 22},
111 {"w23", AARCH64_X0_REGNUM + 23},
112 {"w24", AARCH64_X0_REGNUM + 24},
113 {"w25", AARCH64_X0_REGNUM + 25},
114 {"w26", AARCH64_X0_REGNUM + 26},
115 {"w27", AARCH64_X0_REGNUM + 27},
116 {"w28", AARCH64_X0_REGNUM + 28},
117 {"w29", AARCH64_X0_REGNUM + 29},
118 {"w30", AARCH64_X0_REGNUM + 30},
121 {"ip0", AARCH64_X0_REGNUM + 16},
122 {"ip1", AARCH64_X0_REGNUM + 17}
125 /* The required core 'R' registers. */
126 static const char *const aarch64_r_register_names[] =
128 /* These registers must appear in consecutive RAW register number
129 order and they must begin with AARCH64_X0_REGNUM! */
130 "x0", "x1", "x2", "x3",
131 "x4", "x5", "x6", "x7",
132 "x8", "x9", "x10", "x11",
133 "x12", "x13", "x14", "x15",
134 "x16", "x17", "x18", "x19",
135 "x20", "x21", "x22", "x23",
136 "x24", "x25", "x26", "x27",
137 "x28", "x29", "x30", "sp",
141 /* The FP/SIMD 'V' registers. */
142 static const char *const aarch64_v_register_names[] =
144 /* These registers must appear in consecutive RAW register number
145 order and they must begin with AARCH64_V0_REGNUM! */
146 "v0", "v1", "v2", "v3",
147 "v4", "v5", "v6", "v7",
148 "v8", "v9", "v10", "v11",
149 "v12", "v13", "v14", "v15",
150 "v16", "v17", "v18", "v19",
151 "v20", "v21", "v22", "v23",
152 "v24", "v25", "v26", "v27",
153 "v28", "v29", "v30", "v31",
158 /* AArch64 prologue cache structure. */
159 struct aarch64_prologue_cache
161 /* The program counter at the start of the function. It is used to
162 identify this frame as a prologue frame. */
165 /* The program counter at the time this frame was created; i.e. where
166 this function was called from. It is used to identify this frame as a
170 /* The stack pointer at the time this frame was created; i.e. the
171 caller's stack pointer when this function was called. It is used
172 to identify this frame. */
175 /* Is the target available to read from? */
178 /* The frame base for this frame is just prev_sp - frame size.
179 FRAMESIZE is the distance from the frame pointer to the
180 initial stack pointer. */
183 /* The register used to hold the frame pointer for this frame. */
186 /* Saved register offsets. */
187 struct trad_frame_saved_reg *saved_regs;
191 show_aarch64_debug (struct ui_file *file, int from_tty,
192 struct cmd_list_element *c, const char *value)
194 fprintf_filtered (file, _("AArch64 debugging is %s.\n"), value);
197 /* Analyze a prologue, looking for a recognizable stack frame
198 and frame pointer. Scan until we encounter a store that could
199 clobber the stack frame unexpectedly, or an unknown instruction. */
202 aarch64_analyze_prologue (struct gdbarch *gdbarch,
203 CORE_ADDR start, CORE_ADDR limit,
204 struct aarch64_prologue_cache *cache)
206 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
208 pv_t regs[AARCH64_X_REGISTER_COUNT];
209 struct pv_area *stack;
210 struct cleanup *back_to;
212 for (i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
213 regs[i] = pv_register (i, 0);
214 stack = make_pv_area (AARCH64_SP_REGNUM, gdbarch_addr_bit (gdbarch));
215 back_to = make_cleanup_free_pv_area (stack);
217 for (; start < limit; start += 4)
222 insn = read_memory_unsigned_integer (start, 4, byte_order_for_code);
224 if (aarch64_decode_insn (insn, &inst, 1) != 0)
227 if (inst.opcode->iclass == addsub_imm
228 && (inst.opcode->op == OP_ADD
229 || strcmp ("sub", inst.opcode->name) == 0))
231 unsigned rd = inst.operands[0].reg.regno;
232 unsigned rn = inst.operands[1].reg.regno;
234 gdb_assert (aarch64_num_of_operands (inst.opcode) == 3);
235 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd_SP);
236 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rn_SP);
237 gdb_assert (inst.operands[2].type == AARCH64_OPND_AIMM);
239 if (inst.opcode->op == OP_ADD)
241 regs[rd] = pv_add_constant (regs[rn],
242 inst.operands[2].imm.value);
246 regs[rd] = pv_add_constant (regs[rn],
247 -inst.operands[2].imm.value);
250 else if (inst.opcode->iclass == pcreladdr
251 && inst.operands[1].type == AARCH64_OPND_ADDR_ADRP)
253 gdb_assert (aarch64_num_of_operands (inst.opcode) == 2);
254 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
256 regs[inst.operands[0].reg.regno] = pv_unknown ();
258 else if (inst.opcode->iclass == branch_imm)
260 /* Stop analysis on branch. */
263 else if (inst.opcode->iclass == condbranch)
265 /* Stop analysis on branch. */
268 else if (inst.opcode->iclass == branch_reg)
270 /* Stop analysis on branch. */
273 else if (inst.opcode->iclass == compbranch)
275 /* Stop analysis on branch. */
278 else if (inst.opcode->op == OP_MOVZ)
280 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
281 regs[inst.operands[0].reg.regno] = pv_unknown ();
283 else if (inst.opcode->iclass == log_shift
284 && strcmp (inst.opcode->name, "orr") == 0)
286 unsigned rd = inst.operands[0].reg.regno;
287 unsigned rn = inst.operands[1].reg.regno;
288 unsigned rm = inst.operands[2].reg.regno;
290 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
291 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rn);
292 gdb_assert (inst.operands[2].type == AARCH64_OPND_Rm_SFT);
294 if (inst.operands[2].shifter.amount == 0
295 && rn == AARCH64_SP_REGNUM)
301 debug_printf ("aarch64: prologue analysis gave up "
302 "addr=0x%s opcode=0x%x (orr x register)\n",
303 core_addr_to_string_nz (start), insn);
308 else if (inst.opcode->op == OP_STUR)
310 unsigned rt = inst.operands[0].reg.regno;
311 unsigned rn = inst.operands[1].addr.base_regno;
313 = (aarch64_get_qualifier_esize (inst.operands[0].qualifier) == 8);
315 gdb_assert (aarch64_num_of_operands (inst.opcode) == 2);
316 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt);
317 gdb_assert (inst.operands[1].type == AARCH64_OPND_ADDR_SIMM9);
318 gdb_assert (!inst.operands[1].addr.offset.is_reg);
320 pv_area_store (stack, pv_add_constant (regs[rn],
321 inst.operands[1].addr.offset.imm),
322 is64 ? 8 : 4, regs[rt]);
324 else if ((inst.opcode->iclass == ldstpair_off
325 || inst.opcode->iclass == ldstpair_indexed)
326 && inst.operands[2].addr.preind
327 && strcmp ("stp", inst.opcode->name) == 0)
329 unsigned rt1 = inst.operands[0].reg.regno;
330 unsigned rt2 = inst.operands[1].reg.regno;
331 unsigned rn = inst.operands[2].addr.base_regno;
332 int32_t imm = inst.operands[2].addr.offset.imm;
334 gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt);
335 gdb_assert (inst.operands[1].type == AARCH64_OPND_Rt2);
336 gdb_assert (inst.operands[2].type == AARCH64_OPND_ADDR_SIMM7);
337 gdb_assert (!inst.operands[2].addr.offset.is_reg);
339 /* If recording this store would invalidate the store area
340 (perhaps because rn is not known) then we should abandon
341 further prologue analysis. */
342 if (pv_area_store_would_trash (stack,
343 pv_add_constant (regs[rn], imm)))
346 if (pv_area_store_would_trash (stack,
347 pv_add_constant (regs[rn], imm + 8)))
350 pv_area_store (stack, pv_add_constant (regs[rn], imm), 8,
352 pv_area_store (stack, pv_add_constant (regs[rn], imm + 8), 8,
355 if (inst.operands[2].addr.writeback)
356 regs[rn] = pv_add_constant (regs[rn], imm);
359 else if (inst.opcode->iclass == testbranch)
361 /* Stop analysis on branch. */
368 debug_printf ("aarch64: prologue analysis gave up addr=0x%s"
370 core_addr_to_string_nz (start), insn);
378 do_cleanups (back_to);
382 if (pv_is_register (regs[AARCH64_FP_REGNUM], AARCH64_SP_REGNUM))
384 /* Frame pointer is fp. Frame size is constant. */
385 cache->framereg = AARCH64_FP_REGNUM;
386 cache->framesize = -regs[AARCH64_FP_REGNUM].k;
388 else if (pv_is_register (regs[AARCH64_SP_REGNUM], AARCH64_SP_REGNUM))
390 /* Try the stack pointer. */
391 cache->framesize = -regs[AARCH64_SP_REGNUM].k;
392 cache->framereg = AARCH64_SP_REGNUM;
396 /* We're just out of luck. We don't know where the frame is. */
397 cache->framereg = -1;
398 cache->framesize = 0;
401 for (i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
405 if (pv_area_find_reg (stack, gdbarch, i, &offset))
406 cache->saved_regs[i].addr = offset;
409 do_cleanups (back_to);
413 /* Implement the "skip_prologue" gdbarch method. */
416 aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
420 CORE_ADDR func_addr, limit_pc;
421 struct symtab_and_line sal;
423 /* See if we can determine the end of the prologue via the symbol
424 table. If so, then return either PC, or the PC after the
425 prologue, whichever is greater. */
426 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
428 CORE_ADDR post_prologue_pc
429 = skip_prologue_using_sal (gdbarch, func_addr);
431 if (post_prologue_pc != 0)
432 return max (pc, post_prologue_pc);
435 /* Can't determine prologue from the symbol table, need to examine
438 /* Find an upper limit on the function prologue using the debug
439 information. If the debug information could not be used to
440 provide that bound, then use an arbitrary large number as the
442 limit_pc = skip_prologue_using_sal (gdbarch, pc);
444 limit_pc = pc + 128; /* Magic. */
446 /* Try disassembling prologue. */
447 return aarch64_analyze_prologue (gdbarch, pc, limit_pc, NULL);
450 /* Scan the function prologue for THIS_FRAME and populate the prologue
454 aarch64_scan_prologue (struct frame_info *this_frame,
455 struct aarch64_prologue_cache *cache)
457 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
458 CORE_ADDR prologue_start;
459 CORE_ADDR prologue_end;
460 CORE_ADDR prev_pc = get_frame_pc (this_frame);
461 struct gdbarch *gdbarch = get_frame_arch (this_frame);
463 cache->prev_pc = prev_pc;
465 /* Assume we do not find a frame. */
466 cache->framereg = -1;
467 cache->framesize = 0;
469 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
472 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
476 /* No line info so use the current PC. */
477 prologue_end = prev_pc;
479 else if (sal.end < prologue_end)
481 /* The next line begins after the function end. */
482 prologue_end = sal.end;
485 prologue_end = min (prologue_end, prev_pc);
486 aarch64_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
493 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
495 frame_loc = get_frame_register_unsigned (this_frame, AARCH64_FP_REGNUM);
499 cache->framereg = AARCH64_FP_REGNUM;
500 cache->framesize = 16;
501 cache->saved_regs[29].addr = 0;
502 cache->saved_regs[30].addr = 8;
506 /* Fill in *CACHE with information about the prologue of *THIS_FRAME. This
507 function may throw an exception if the inferior's registers or memory is
511 aarch64_make_prologue_cache_1 (struct frame_info *this_frame,
512 struct aarch64_prologue_cache *cache)
514 CORE_ADDR unwound_fp;
517 aarch64_scan_prologue (this_frame, cache);
519 if (cache->framereg == -1)
522 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
526 cache->prev_sp = unwound_fp + cache->framesize;
528 /* Calculate actual addresses of saved registers using offsets
529 determined by aarch64_analyze_prologue. */
530 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
531 if (trad_frame_addr_p (cache->saved_regs, reg))
532 cache->saved_regs[reg].addr += cache->prev_sp;
534 cache->func = get_frame_func (this_frame);
536 cache->available_p = 1;
539 /* Allocate and fill in *THIS_CACHE with information about the prologue of
540 *THIS_FRAME. Do not do this is if *THIS_CACHE was already allocated.
541 Return a pointer to the current aarch64_prologue_cache in
544 static struct aarch64_prologue_cache *
545 aarch64_make_prologue_cache (struct frame_info *this_frame, void **this_cache)
547 struct aarch64_prologue_cache *cache;
549 if (*this_cache != NULL)
550 return (struct aarch64_prologue_cache *) *this_cache;
552 cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
553 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
558 aarch64_make_prologue_cache_1 (this_frame, cache);
560 CATCH (ex, RETURN_MASK_ERROR)
562 if (ex.error != NOT_AVAILABLE_ERROR)
563 throw_exception (ex);
570 /* Implement the "stop_reason" frame_unwind method. */
572 static enum unwind_stop_reason
573 aarch64_prologue_frame_unwind_stop_reason (struct frame_info *this_frame,
576 struct aarch64_prologue_cache *cache
577 = aarch64_make_prologue_cache (this_frame, this_cache);
579 if (!cache->available_p)
580 return UNWIND_UNAVAILABLE;
582 /* Halt the backtrace at "_start". */
583 if (cache->prev_pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
584 return UNWIND_OUTERMOST;
586 /* We've hit a wall, stop. */
587 if (cache->prev_sp == 0)
588 return UNWIND_OUTERMOST;
590 return UNWIND_NO_REASON;
593 /* Our frame ID for a normal frame is the current function's starting
594 PC and the caller's SP when we were called. */
597 aarch64_prologue_this_id (struct frame_info *this_frame,
598 void **this_cache, struct frame_id *this_id)
600 struct aarch64_prologue_cache *cache
601 = aarch64_make_prologue_cache (this_frame, this_cache);
603 if (!cache->available_p)
604 *this_id = frame_id_build_unavailable_stack (cache->func);
606 *this_id = frame_id_build (cache->prev_sp, cache->func);
609 /* Implement the "prev_register" frame_unwind method. */
611 static struct value *
612 aarch64_prologue_prev_register (struct frame_info *this_frame,
613 void **this_cache, int prev_regnum)
615 struct gdbarch *gdbarch = get_frame_arch (this_frame);
616 struct aarch64_prologue_cache *cache
617 = aarch64_make_prologue_cache (this_frame, this_cache);
619 /* If we are asked to unwind the PC, then we need to return the LR
620 instead. The prologue may save PC, but it will point into this
621 frame's prologue, not the next frame's resume location. */
622 if (prev_regnum == AARCH64_PC_REGNUM)
626 lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
627 return frame_unwind_got_constant (this_frame, prev_regnum, lr);
630 /* SP is generally not saved to the stack, but this frame is
631 identified by the next frame's stack pointer at the time of the
632 call. The value was already reconstructed into PREV_SP. */
645 if (prev_regnum == AARCH64_SP_REGNUM)
646 return frame_unwind_got_constant (this_frame, prev_regnum,
649 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
653 /* AArch64 prologue unwinder. */
654 struct frame_unwind aarch64_prologue_unwind =
657 aarch64_prologue_frame_unwind_stop_reason,
658 aarch64_prologue_this_id,
659 aarch64_prologue_prev_register,
661 default_frame_sniffer
664 /* Allocate and fill in *THIS_CACHE with information about the prologue of
665 *THIS_FRAME. Do not do this is if *THIS_CACHE was already allocated.
666 Return a pointer to the current aarch64_prologue_cache in
669 static struct aarch64_prologue_cache *
670 aarch64_make_stub_cache (struct frame_info *this_frame, void **this_cache)
672 struct aarch64_prologue_cache *cache;
674 if (*this_cache != NULL)
675 return (struct aarch64_prologue_cache *) *this_cache;
677 cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
678 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
683 cache->prev_sp = get_frame_register_unsigned (this_frame,
685 cache->prev_pc = get_frame_pc (this_frame);
686 cache->available_p = 1;
688 CATCH (ex, RETURN_MASK_ERROR)
690 if (ex.error != NOT_AVAILABLE_ERROR)
691 throw_exception (ex);
698 /* Implement the "stop_reason" frame_unwind method. */
700 static enum unwind_stop_reason
701 aarch64_stub_frame_unwind_stop_reason (struct frame_info *this_frame,
704 struct aarch64_prologue_cache *cache
705 = aarch64_make_stub_cache (this_frame, this_cache);
707 if (!cache->available_p)
708 return UNWIND_UNAVAILABLE;
710 return UNWIND_NO_REASON;
713 /* Our frame ID for a stub frame is the current SP and LR. */
716 aarch64_stub_this_id (struct frame_info *this_frame,
717 void **this_cache, struct frame_id *this_id)
719 struct aarch64_prologue_cache *cache
720 = aarch64_make_stub_cache (this_frame, this_cache);
722 if (cache->available_p)
723 *this_id = frame_id_build (cache->prev_sp, cache->prev_pc);
725 *this_id = frame_id_build_unavailable_stack (cache->prev_pc);
728 /* Implement the "sniffer" frame_unwind method. */
731 aarch64_stub_unwind_sniffer (const struct frame_unwind *self,
732 struct frame_info *this_frame,
733 void **this_prologue_cache)
735 CORE_ADDR addr_in_block;
738 addr_in_block = get_frame_address_in_block (this_frame);
739 if (in_plt_section (addr_in_block)
740 /* We also use the stub winder if the target memory is unreadable
741 to avoid having the prologue unwinder trying to read it. */
742 || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
748 /* AArch64 stub unwinder. */
749 struct frame_unwind aarch64_stub_unwind =
752 aarch64_stub_frame_unwind_stop_reason,
753 aarch64_stub_this_id,
754 aarch64_prologue_prev_register,
756 aarch64_stub_unwind_sniffer
759 /* Return the frame base address of *THIS_FRAME. */
762 aarch64_normal_frame_base (struct frame_info *this_frame, void **this_cache)
764 struct aarch64_prologue_cache *cache
765 = aarch64_make_prologue_cache (this_frame, this_cache);
767 return cache->prev_sp - cache->framesize;
770 /* AArch64 default frame base information. */
771 struct frame_base aarch64_normal_base =
773 &aarch64_prologue_unwind,
774 aarch64_normal_frame_base,
775 aarch64_normal_frame_base,
776 aarch64_normal_frame_base
779 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
780 dummy frame. The frame ID's base needs to match the TOS value
781 saved by save_dummy_frame_tos () and returned from
782 aarch64_push_dummy_call, and the PC needs to match the dummy
783 frame's breakpoint. */
785 static struct frame_id
786 aarch64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
788 return frame_id_build (get_frame_register_unsigned (this_frame,
790 get_frame_pc (this_frame));
793 /* Implement the "unwind_pc" gdbarch method. */
796 aarch64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
799 = frame_unwind_register_unsigned (this_frame, AARCH64_PC_REGNUM);
804 /* Implement the "unwind_sp" gdbarch method. */
807 aarch64_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
809 return frame_unwind_register_unsigned (this_frame, AARCH64_SP_REGNUM);
812 /* Return the value of the REGNUM register in the previous frame of
815 static struct value *
816 aarch64_dwarf2_prev_register (struct frame_info *this_frame,
817 void **this_cache, int regnum)
819 struct gdbarch *gdbarch = get_frame_arch (this_frame);
824 case AARCH64_PC_REGNUM:
825 lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
826 return frame_unwind_got_constant (this_frame, regnum, lr);
829 internal_error (__FILE__, __LINE__,
830 _("Unexpected register %d"), regnum);
834 /* Implement the "init_reg" dwarf2_frame_ops method. */
837 aarch64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
838 struct dwarf2_frame_state_reg *reg,
839 struct frame_info *this_frame)
843 case AARCH64_PC_REGNUM:
844 reg->how = DWARF2_FRAME_REG_FN;
845 reg->loc.fn = aarch64_dwarf2_prev_register;
847 case AARCH64_SP_REGNUM:
848 reg->how = DWARF2_FRAME_REG_CFA;
853 /* When arguments must be pushed onto the stack, they go on in reverse
854 order. The code below implements a FILO (stack) to do this. */
858 /* Value to pass on stack. It can be NULL if this item is for stack
860 const gdb_byte *data;
862 /* Size in bytes of value to pass on stack. */
866 DEF_VEC_O (stack_item_t);
868 /* Return the alignment (in bytes) of the given type. */
871 aarch64_type_align (struct type *t)
877 t = check_typedef (t);
878 switch (TYPE_CODE (t))
881 /* Should never happen. */
882 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
890 case TYPE_CODE_RANGE:
891 case TYPE_CODE_BITSTRING:
895 return TYPE_LENGTH (t);
897 case TYPE_CODE_ARRAY:
900 /* Use the natural alignment for vector types (the same for
901 scalar type), but the maximum alignment is 128-bit. */
902 if (TYPE_LENGTH (t) > 16)
905 return TYPE_LENGTH (t);
908 return aarch64_type_align (TYPE_TARGET_TYPE (t));
909 case TYPE_CODE_COMPLEX:
910 return aarch64_type_align (TYPE_TARGET_TYPE (t));
912 case TYPE_CODE_STRUCT:
913 case TYPE_CODE_UNION:
915 for (n = 0; n < TYPE_NFIELDS (t); n++)
917 falign = aarch64_type_align (TYPE_FIELD_TYPE (t, n));
925 /* Return 1 if *TY is a homogeneous floating-point aggregate or
926 homogeneous short-vector aggregate as defined in the AAPCS64 ABI
927 document; otherwise return 0. */
930 is_hfa_or_hva (struct type *ty)
932 switch (TYPE_CODE (ty))
934 case TYPE_CODE_ARRAY:
936 struct type *target_ty = TYPE_TARGET_TYPE (ty);
938 if (TYPE_VECTOR (ty))
941 if (TYPE_LENGTH (ty) <= 4 /* HFA or HVA has at most 4 members. */
942 && (TYPE_CODE (target_ty) == TYPE_CODE_FLT /* HFA */
943 || (TYPE_CODE (target_ty) == TYPE_CODE_ARRAY /* HVA */
944 && TYPE_VECTOR (target_ty))))
949 case TYPE_CODE_UNION:
950 case TYPE_CODE_STRUCT:
952 /* HFA or HVA has at most four members. */
953 if (TYPE_NFIELDS (ty) > 0 && TYPE_NFIELDS (ty) <= 4)
955 struct type *member0_type;
957 member0_type = check_typedef (TYPE_FIELD_TYPE (ty, 0));
958 if (TYPE_CODE (member0_type) == TYPE_CODE_FLT
959 || (TYPE_CODE (member0_type) == TYPE_CODE_ARRAY
960 && TYPE_VECTOR (member0_type)))
964 for (i = 0; i < TYPE_NFIELDS (ty); i++)
966 struct type *member1_type;
968 member1_type = check_typedef (TYPE_FIELD_TYPE (ty, i));
969 if (TYPE_CODE (member0_type) != TYPE_CODE (member1_type)
970 || (TYPE_LENGTH (member0_type)
971 != TYPE_LENGTH (member1_type)))
987 /* AArch64 function call information structure. */
988 struct aarch64_call_info
990 /* the current argument number. */
993 /* The next general purpose register number, equivalent to NGRN as
994 described in the AArch64 Procedure Call Standard. */
997 /* The next SIMD and floating point register number, equivalent to
998 NSRN as described in the AArch64 Procedure Call Standard. */
1001 /* The next stacked argument address, equivalent to NSAA as
1002 described in the AArch64 Procedure Call Standard. */
1005 /* Stack item vector. */
1006 VEC(stack_item_t) *si;
1009 /* Pass a value in a sequence of consecutive X registers. The caller
1010 is responsbile for ensuring sufficient registers are available. */
1013 pass_in_x (struct gdbarch *gdbarch, struct regcache *regcache,
1014 struct aarch64_call_info *info, struct type *type,
1017 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1018 int len = TYPE_LENGTH (type);
1019 enum type_code typecode = TYPE_CODE (type);
1020 int regnum = AARCH64_X0_REGNUM + info->ngrn;
1021 const bfd_byte *buf = value_contents (arg);
1027 int partial_len = len < X_REGISTER_SIZE ? len : X_REGISTER_SIZE;
1028 CORE_ADDR regval = extract_unsigned_integer (buf, partial_len,
1032 /* Adjust sub-word struct/union args when big-endian. */
1033 if (byte_order == BFD_ENDIAN_BIG
1034 && partial_len < X_REGISTER_SIZE
1035 && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
1036 regval <<= ((X_REGISTER_SIZE - partial_len) * TARGET_CHAR_BIT);
1040 debug_printf ("arg %d in %s = 0x%s\n", info->argnum,
1041 gdbarch_register_name (gdbarch, regnum),
1042 phex (regval, X_REGISTER_SIZE));
1044 regcache_cooked_write_unsigned (regcache, regnum, regval);
1051 /* Attempt to marshall a value in a V register. Return 1 if
1052 successful, or 0 if insufficient registers are available. This
1053 function, unlike the equivalent pass_in_x() function does not
1054 handle arguments spread across multiple registers. */
1057 pass_in_v (struct gdbarch *gdbarch,
1058 struct regcache *regcache,
1059 struct aarch64_call_info *info,
1060 int len, const bfd_byte *buf)
1064 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1065 int regnum = AARCH64_V0_REGNUM + info->nsrn;
1066 gdb_byte reg[V_REGISTER_SIZE];
1071 memset (reg, 0, sizeof (reg));
1072 /* PCS C.1, the argument is allocated to the least significant
1073 bits of V register. */
1074 memcpy (reg, buf, len);
1075 regcache_cooked_write (regcache, regnum, reg);
1079 debug_printf ("arg %d in %s\n", info->argnum,
1080 gdbarch_register_name (gdbarch, regnum));
1088 /* Marshall an argument onto the stack. */
1091 pass_on_stack (struct aarch64_call_info *info, struct type *type,
1094 const bfd_byte *buf = value_contents (arg);
1095 int len = TYPE_LENGTH (type);
1101 align = aarch64_type_align (type);
1103 /* PCS C.17 Stack should be aligned to the larger of 8 bytes or the
1104 Natural alignment of the argument's type. */
1105 align = align_up (align, 8);
1107 /* The AArch64 PCS requires at most doubleword alignment. */
1113 debug_printf ("arg %d len=%d @ sp + %d\n", info->argnum, len,
1119 VEC_safe_push (stack_item_t, info->si, &item);
1122 if (info->nsaa & (align - 1))
1124 /* Push stack alignment padding. */
1125 int pad = align - (info->nsaa & (align - 1));
1130 VEC_safe_push (stack_item_t, info->si, &item);
1135 /* Marshall an argument into a sequence of one or more consecutive X
1136 registers or, if insufficient X registers are available then onto
1140 pass_in_x_or_stack (struct gdbarch *gdbarch, struct regcache *regcache,
1141 struct aarch64_call_info *info, struct type *type,
1144 int len = TYPE_LENGTH (type);
1145 int nregs = (len + X_REGISTER_SIZE - 1) / X_REGISTER_SIZE;
1147 /* PCS C.13 - Pass in registers if we have enough spare */
1148 if (info->ngrn + nregs <= 8)
1150 pass_in_x (gdbarch, regcache, info, type, arg);
1151 info->ngrn += nregs;
1156 pass_on_stack (info, type, arg);
1160 /* Pass a value in a V register, or on the stack if insufficient are
1164 pass_in_v_or_stack (struct gdbarch *gdbarch,
1165 struct regcache *regcache,
1166 struct aarch64_call_info *info,
1170 if (!pass_in_v (gdbarch, regcache, info, TYPE_LENGTH (type),
1171 value_contents (arg)))
1172 pass_on_stack (info, type, arg);
1175 /* Implement the "push_dummy_call" gdbarch method. */
1178 aarch64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1179 struct regcache *regcache, CORE_ADDR bp_addr,
1181 struct value **args, CORE_ADDR sp, int struct_return,
1182 CORE_ADDR struct_addr)
1188 struct aarch64_call_info info;
1189 struct type *func_type;
1190 struct type *return_type;
1191 int lang_struct_return;
1193 memset (&info, 0, sizeof (info));
1195 /* We need to know what the type of the called function is in order
1196 to determine the number of named/anonymous arguments for the
1197 actual argument placement, and the return type in order to handle
1198 return value correctly.
1200 The generic code above us views the decision of return in memory
1201 or return in registers as a two stage processes. The language
1202 handler is consulted first and may decide to return in memory (eg
1203 class with copy constructor returned by value), this will cause
1204 the generic code to allocate space AND insert an initial leading
1207 If the language code does not decide to pass in memory then the
1208 target code is consulted.
1210 If the language code decides to pass in memory we want to move
1211 the pointer inserted as the initial argument from the argument
1212 list and into X8, the conventional AArch64 struct return pointer
1215 This is slightly awkward, ideally the flag "lang_struct_return"
1216 would be passed to the targets implementation of push_dummy_call.
1217 Rather that change the target interface we call the language code
1218 directly ourselves. */
1220 func_type = check_typedef (value_type (function));
1222 /* Dereference function pointer types. */
1223 if (TYPE_CODE (func_type) == TYPE_CODE_PTR)
1224 func_type = TYPE_TARGET_TYPE (func_type);
1226 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC
1227 || TYPE_CODE (func_type) == TYPE_CODE_METHOD);
1229 /* If language_pass_by_reference () returned true we will have been
1230 given an additional initial argument, a hidden pointer to the
1231 return slot in memory. */
1232 return_type = TYPE_TARGET_TYPE (func_type);
1233 lang_struct_return = language_pass_by_reference (return_type);
1235 /* Set the return address. For the AArch64, the return breakpoint
1236 is always at BP_ADDR. */
1237 regcache_cooked_write_unsigned (regcache, AARCH64_LR_REGNUM, bp_addr);
1239 /* If we were given an initial argument for the return slot because
1240 lang_struct_return was true, lose it. */
1241 if (lang_struct_return)
1247 /* The struct_return pointer occupies X8. */
1248 if (struct_return || lang_struct_return)
1252 debug_printf ("struct return in %s = 0x%s\n",
1253 gdbarch_register_name (gdbarch,
1254 AARCH64_STRUCT_RETURN_REGNUM),
1255 paddress (gdbarch, struct_addr));
1257 regcache_cooked_write_unsigned (regcache, AARCH64_STRUCT_RETURN_REGNUM,
1261 for (argnum = 0; argnum < nargs; argnum++)
1263 struct value *arg = args[argnum];
1264 struct type *arg_type;
1267 arg_type = check_typedef (value_type (arg));
1268 len = TYPE_LENGTH (arg_type);
1270 switch (TYPE_CODE (arg_type))
1273 case TYPE_CODE_BOOL:
1274 case TYPE_CODE_CHAR:
1275 case TYPE_CODE_RANGE:
1276 case TYPE_CODE_ENUM:
1279 /* Promote to 32 bit integer. */
1280 if (TYPE_UNSIGNED (arg_type))
1281 arg_type = builtin_type (gdbarch)->builtin_uint32;
1283 arg_type = builtin_type (gdbarch)->builtin_int32;
1284 arg = value_cast (arg_type, arg);
1286 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
1289 case TYPE_CODE_COMPLEX:
1292 const bfd_byte *buf = value_contents (arg);
1293 struct type *target_type =
1294 check_typedef (TYPE_TARGET_TYPE (arg_type));
1296 pass_in_v (gdbarch, regcache, &info,
1297 TYPE_LENGTH (target_type), buf);
1298 pass_in_v (gdbarch, regcache, &info,
1299 TYPE_LENGTH (target_type),
1300 buf + TYPE_LENGTH (target_type));
1305 pass_on_stack (&info, arg_type, arg);
1309 pass_in_v_or_stack (gdbarch, regcache, &info, arg_type, arg);
1312 case TYPE_CODE_STRUCT:
1313 case TYPE_CODE_ARRAY:
1314 case TYPE_CODE_UNION:
1315 if (is_hfa_or_hva (arg_type))
1317 int elements = TYPE_NFIELDS (arg_type);
1319 /* Homogeneous Aggregates */
1320 if (info.nsrn + elements < 8)
1324 for (i = 0; i < elements; i++)
1326 /* We know that we have sufficient registers
1327 available therefore this will never fallback
1329 struct value *field =
1330 value_primitive_field (arg, 0, i, arg_type);
1331 struct type *field_type =
1332 check_typedef (value_type (field));
1334 pass_in_v_or_stack (gdbarch, regcache, &info,
1341 pass_on_stack (&info, arg_type, arg);
1344 else if (TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
1345 && TYPE_VECTOR (arg_type) && (len == 16 || len == 8))
1347 /* Short vector types are passed in V registers. */
1348 pass_in_v_or_stack (gdbarch, regcache, &info, arg_type, arg);
1352 /* PCS B.7 Aggregates larger than 16 bytes are passed by
1353 invisible reference. */
1355 /* Allocate aligned storage. */
1356 sp = align_down (sp - len, 16);
1358 /* Write the real data into the stack. */
1359 write_memory (sp, value_contents (arg), len);
1361 /* Construct the indirection. */
1362 arg_type = lookup_pointer_type (arg_type);
1363 arg = value_from_pointer (arg_type, sp);
1364 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
1367 /* PCS C.15 / C.18 multiple values pass. */
1368 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
1372 pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
1377 /* Make sure stack retains 16 byte alignment. */
1379 sp -= 16 - (info.nsaa & 15);
1381 while (!VEC_empty (stack_item_t, info.si))
1383 stack_item_t *si = VEC_last (stack_item_t, info.si);
1386 if (si->data != NULL)
1387 write_memory (sp, si->data, si->len);
1388 VEC_pop (stack_item_t, info.si);
1391 VEC_free (stack_item_t, info.si);
1393 /* Finally, update the SP register. */
1394 regcache_cooked_write_unsigned (regcache, AARCH64_SP_REGNUM, sp);
1399 /* Implement the "frame_align" gdbarch method. */
1402 aarch64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
1404 /* Align the stack to sixteen bytes. */
1405 return sp & ~(CORE_ADDR) 15;
1408 /* Return the type for an AdvSISD Q register. */
1410 static struct type *
1411 aarch64_vnq_type (struct gdbarch *gdbarch)
1413 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1415 if (tdep->vnq_type == NULL)
1420 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnq",
1423 elem = builtin_type (gdbarch)->builtin_uint128;
1424 append_composite_type_field (t, "u", elem);
1426 elem = builtin_type (gdbarch)->builtin_int128;
1427 append_composite_type_field (t, "s", elem);
1432 return tdep->vnq_type;
1435 /* Return the type for an AdvSISD D register. */
1437 static struct type *
1438 aarch64_vnd_type (struct gdbarch *gdbarch)
1440 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1442 if (tdep->vnd_type == NULL)
1447 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnd",
1450 elem = builtin_type (gdbarch)->builtin_double;
1451 append_composite_type_field (t, "f", elem);
1453 elem = builtin_type (gdbarch)->builtin_uint64;
1454 append_composite_type_field (t, "u", elem);
1456 elem = builtin_type (gdbarch)->builtin_int64;
1457 append_composite_type_field (t, "s", elem);
1462 return tdep->vnd_type;
1465 /* Return the type for an AdvSISD S register. */
1467 static struct type *
1468 aarch64_vns_type (struct gdbarch *gdbarch)
1470 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1472 if (tdep->vns_type == NULL)
1477 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vns",
1480 elem = builtin_type (gdbarch)->builtin_float;
1481 append_composite_type_field (t, "f", elem);
1483 elem = builtin_type (gdbarch)->builtin_uint32;
1484 append_composite_type_field (t, "u", elem);
1486 elem = builtin_type (gdbarch)->builtin_int32;
1487 append_composite_type_field (t, "s", elem);
1492 return tdep->vns_type;
1495 /* Return the type for an AdvSISD H register. */
1497 static struct type *
1498 aarch64_vnh_type (struct gdbarch *gdbarch)
1500 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1502 if (tdep->vnh_type == NULL)
1507 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnh",
1510 elem = builtin_type (gdbarch)->builtin_uint16;
1511 append_composite_type_field (t, "u", elem);
1513 elem = builtin_type (gdbarch)->builtin_int16;
1514 append_composite_type_field (t, "s", elem);
1519 return tdep->vnh_type;
1522 /* Return the type for an AdvSISD B register. */
1524 static struct type *
1525 aarch64_vnb_type (struct gdbarch *gdbarch)
1527 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1529 if (tdep->vnb_type == NULL)
1534 t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnb",
1537 elem = builtin_type (gdbarch)->builtin_uint8;
1538 append_composite_type_field (t, "u", elem);
1540 elem = builtin_type (gdbarch)->builtin_int8;
1541 append_composite_type_field (t, "s", elem);
1546 return tdep->vnb_type;
1549 /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
1552 aarch64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
1554 if (reg >= AARCH64_DWARF_X0 && reg <= AARCH64_DWARF_X0 + 30)
1555 return AARCH64_X0_REGNUM + reg - AARCH64_DWARF_X0;
1557 if (reg == AARCH64_DWARF_SP)
1558 return AARCH64_SP_REGNUM;
1560 if (reg >= AARCH64_DWARF_V0 && reg <= AARCH64_DWARF_V0 + 31)
1561 return AARCH64_V0_REGNUM + reg - AARCH64_DWARF_V0;
1567 /* Implement the "print_insn" gdbarch method. */
1570 aarch64_gdb_print_insn (bfd_vma memaddr, disassemble_info *info)
1572 info->symbols = NULL;
1573 return print_insn_aarch64 (memaddr, info);
1576 /* AArch64 BRK software debug mode instruction.
1577 Note that AArch64 code is always little-endian.
1578 1101.0100.0010.0000.0000.0000.0000.0000 = 0xd4200000. */
1579 static const gdb_byte aarch64_default_breakpoint[] = {0x00, 0x00, 0x20, 0xd4};
1581 /* Implement the "breakpoint_from_pc" gdbarch method. */
1583 static const gdb_byte *
1584 aarch64_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
1587 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1589 *lenptr = sizeof (aarch64_default_breakpoint);
1590 return aarch64_default_breakpoint;
1593 /* Extract from an array REGS containing the (raw) register state a
1594 function return value of type TYPE, and copy that, in virtual
1595 format, into VALBUF. */
1598 aarch64_extract_return_value (struct type *type, struct regcache *regs,
1601 struct gdbarch *gdbarch = get_regcache_arch (regs);
1602 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1604 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1606 bfd_byte buf[V_REGISTER_SIZE];
1607 int len = TYPE_LENGTH (type);
1609 regcache_cooked_read (regs, AARCH64_V0_REGNUM, buf);
1610 memcpy (valbuf, buf, len);
1612 else if (TYPE_CODE (type) == TYPE_CODE_INT
1613 || TYPE_CODE (type) == TYPE_CODE_CHAR
1614 || TYPE_CODE (type) == TYPE_CODE_BOOL
1615 || TYPE_CODE (type) == TYPE_CODE_PTR
1616 || TYPE_CODE (type) == TYPE_CODE_REF
1617 || TYPE_CODE (type) == TYPE_CODE_ENUM)
1619 /* If the the type is a plain integer, then the access is
1620 straight-forward. Otherwise we have to play around a bit
1622 int len = TYPE_LENGTH (type);
1623 int regno = AARCH64_X0_REGNUM;
1628 /* By using store_unsigned_integer we avoid having to do
1629 anything special for small big-endian values. */
1630 regcache_cooked_read_unsigned (regs, regno++, &tmp);
1631 store_unsigned_integer (valbuf,
1632 (len > X_REGISTER_SIZE
1633 ? X_REGISTER_SIZE : len), byte_order, tmp);
1634 len -= X_REGISTER_SIZE;
1635 valbuf += X_REGISTER_SIZE;
1638 else if (TYPE_CODE (type) == TYPE_CODE_COMPLEX)
1640 int regno = AARCH64_V0_REGNUM;
1641 bfd_byte buf[V_REGISTER_SIZE];
1642 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1643 int len = TYPE_LENGTH (target_type);
1645 regcache_cooked_read (regs, regno, buf);
1646 memcpy (valbuf, buf, len);
1648 regcache_cooked_read (regs, regno + 1, buf);
1649 memcpy (valbuf, buf, len);
1652 else if (is_hfa_or_hva (type))
1654 int elements = TYPE_NFIELDS (type);
1655 struct type *member_type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1656 int len = TYPE_LENGTH (member_type);
1659 for (i = 0; i < elements; i++)
1661 int regno = AARCH64_V0_REGNUM + i;
1662 bfd_byte buf[V_REGISTER_SIZE];
1666 debug_printf ("read HFA or HVA return value element %d from %s\n",
1668 gdbarch_register_name (gdbarch, regno));
1670 regcache_cooked_read (regs, regno, buf);
1672 memcpy (valbuf, buf, len);
1676 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1677 && (TYPE_LENGTH (type) == 16 || TYPE_LENGTH (type) == 8))
1679 /* Short vector is returned in V register. */
1680 gdb_byte buf[V_REGISTER_SIZE];
1682 regcache_cooked_read (regs, AARCH64_V0_REGNUM, buf);
1683 memcpy (valbuf, buf, TYPE_LENGTH (type));
1687 /* For a structure or union the behaviour is as if the value had
1688 been stored to word-aligned memory and then loaded into
1689 registers with 64-bit load instruction(s). */
1690 int len = TYPE_LENGTH (type);
1691 int regno = AARCH64_X0_REGNUM;
1692 bfd_byte buf[X_REGISTER_SIZE];
1696 regcache_cooked_read (regs, regno++, buf);
1697 memcpy (valbuf, buf, len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
1698 len -= X_REGISTER_SIZE;
1699 valbuf += X_REGISTER_SIZE;
1705 /* Will a function return an aggregate type in memory or in a
1706 register? Return 0 if an aggregate type can be returned in a
1707 register, 1 if it must be returned in memory. */
1710 aarch64_return_in_memory (struct gdbarch *gdbarch, struct type *type)
1713 enum type_code code;
1715 type = check_typedef (type);
1717 if (is_hfa_or_hva (type))
1719 /* v0-v7 are used to return values and one register is allocated
1720 for one member. However, HFA or HVA has at most four members. */
1724 if (TYPE_LENGTH (type) > 16)
1726 /* PCS B.6 Aggregates larger than 16 bytes are passed by
1727 invisible reference. */
1735 /* Write into appropriate registers a function return value of type
1736 TYPE, given in virtual format. */
1739 aarch64_store_return_value (struct type *type, struct regcache *regs,
1740 const gdb_byte *valbuf)
1742 struct gdbarch *gdbarch = get_regcache_arch (regs);
1743 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1745 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1747 bfd_byte buf[V_REGISTER_SIZE];
1748 int len = TYPE_LENGTH (type);
1750 memcpy (buf, valbuf, len > V_REGISTER_SIZE ? V_REGISTER_SIZE : len);
1751 regcache_cooked_write (regs, AARCH64_V0_REGNUM, buf);
1753 else if (TYPE_CODE (type) == TYPE_CODE_INT
1754 || TYPE_CODE (type) == TYPE_CODE_CHAR
1755 || TYPE_CODE (type) == TYPE_CODE_BOOL
1756 || TYPE_CODE (type) == TYPE_CODE_PTR
1757 || TYPE_CODE (type) == TYPE_CODE_REF
1758 || TYPE_CODE (type) == TYPE_CODE_ENUM)
1760 if (TYPE_LENGTH (type) <= X_REGISTER_SIZE)
1762 /* Values of one word or less are zero/sign-extended and
1764 bfd_byte tmpbuf[X_REGISTER_SIZE];
1765 LONGEST val = unpack_long (type, valbuf);
1767 store_signed_integer (tmpbuf, X_REGISTER_SIZE, byte_order, val);
1768 regcache_cooked_write (regs, AARCH64_X0_REGNUM, tmpbuf);
1772 /* Integral values greater than one word are stored in
1773 consecutive registers starting with r0. This will always
1774 be a multiple of the regiser size. */
1775 int len = TYPE_LENGTH (type);
1776 int regno = AARCH64_X0_REGNUM;
1780 regcache_cooked_write (regs, regno++, valbuf);
1781 len -= X_REGISTER_SIZE;
1782 valbuf += X_REGISTER_SIZE;
1786 else if (is_hfa_or_hva (type))
1788 int elements = TYPE_NFIELDS (type);
1789 struct type *member_type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1790 int len = TYPE_LENGTH (member_type);
1793 for (i = 0; i < elements; i++)
1795 int regno = AARCH64_V0_REGNUM + i;
1796 bfd_byte tmpbuf[MAX_REGISTER_SIZE];
1800 debug_printf ("write HFA or HVA return value element %d to %s\n",
1802 gdbarch_register_name (gdbarch, regno));
1805 memcpy (tmpbuf, valbuf, len);
1806 regcache_cooked_write (regs, regno, tmpbuf);
1810 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1811 && (TYPE_LENGTH (type) == 8 || TYPE_LENGTH (type) == 16))
1814 gdb_byte buf[V_REGISTER_SIZE];
1816 memcpy (buf, valbuf, TYPE_LENGTH (type));
1817 regcache_cooked_write (regs, AARCH64_V0_REGNUM, buf);
1821 /* For a structure or union the behaviour is as if the value had
1822 been stored to word-aligned memory and then loaded into
1823 registers with 64-bit load instruction(s). */
1824 int len = TYPE_LENGTH (type);
1825 int regno = AARCH64_X0_REGNUM;
1826 bfd_byte tmpbuf[X_REGISTER_SIZE];
1830 memcpy (tmpbuf, valbuf,
1831 len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
1832 regcache_cooked_write (regs, regno++, tmpbuf);
1833 len -= X_REGISTER_SIZE;
1834 valbuf += X_REGISTER_SIZE;
1839 /* Implement the "return_value" gdbarch method. */
1841 static enum return_value_convention
1842 aarch64_return_value (struct gdbarch *gdbarch, struct value *func_value,
1843 struct type *valtype, struct regcache *regcache,
1844 gdb_byte *readbuf, const gdb_byte *writebuf)
1846 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1848 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1849 || TYPE_CODE (valtype) == TYPE_CODE_UNION
1850 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1852 if (aarch64_return_in_memory (gdbarch, valtype))
1855 debug_printf ("return value in memory\n");
1856 return RETURN_VALUE_STRUCT_CONVENTION;
1861 aarch64_store_return_value (valtype, regcache, writebuf);
1864 aarch64_extract_return_value (valtype, regcache, readbuf);
1867 debug_printf ("return value in registers\n");
1869 return RETURN_VALUE_REGISTER_CONVENTION;
1872 /* Implement the "get_longjmp_target" gdbarch method. */
1875 aarch64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
1878 gdb_byte buf[X_REGISTER_SIZE];
1879 struct gdbarch *gdbarch = get_frame_arch (frame);
1880 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1881 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1883 jb_addr = get_frame_register_unsigned (frame, AARCH64_X0_REGNUM);
1885 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
1889 *pc = extract_unsigned_integer (buf, X_REGISTER_SIZE, byte_order);
1893 /* Implement the "gen_return_address" gdbarch method. */
1896 aarch64_gen_return_address (struct gdbarch *gdbarch,
1897 struct agent_expr *ax, struct axs_value *value,
1900 value->type = register_type (gdbarch, AARCH64_LR_REGNUM);
1901 value->kind = axs_lvalue_register;
1902 value->u.reg = AARCH64_LR_REGNUM;
1906 /* Return the pseudo register name corresponding to register regnum. */
1909 aarch64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
1911 static const char *const q_name[] =
1913 "q0", "q1", "q2", "q3",
1914 "q4", "q5", "q6", "q7",
1915 "q8", "q9", "q10", "q11",
1916 "q12", "q13", "q14", "q15",
1917 "q16", "q17", "q18", "q19",
1918 "q20", "q21", "q22", "q23",
1919 "q24", "q25", "q26", "q27",
1920 "q28", "q29", "q30", "q31",
1923 static const char *const d_name[] =
1925 "d0", "d1", "d2", "d3",
1926 "d4", "d5", "d6", "d7",
1927 "d8", "d9", "d10", "d11",
1928 "d12", "d13", "d14", "d15",
1929 "d16", "d17", "d18", "d19",
1930 "d20", "d21", "d22", "d23",
1931 "d24", "d25", "d26", "d27",
1932 "d28", "d29", "d30", "d31",
1935 static const char *const s_name[] =
1937 "s0", "s1", "s2", "s3",
1938 "s4", "s5", "s6", "s7",
1939 "s8", "s9", "s10", "s11",
1940 "s12", "s13", "s14", "s15",
1941 "s16", "s17", "s18", "s19",
1942 "s20", "s21", "s22", "s23",
1943 "s24", "s25", "s26", "s27",
1944 "s28", "s29", "s30", "s31",
1947 static const char *const h_name[] =
1949 "h0", "h1", "h2", "h3",
1950 "h4", "h5", "h6", "h7",
1951 "h8", "h9", "h10", "h11",
1952 "h12", "h13", "h14", "h15",
1953 "h16", "h17", "h18", "h19",
1954 "h20", "h21", "h22", "h23",
1955 "h24", "h25", "h26", "h27",
1956 "h28", "h29", "h30", "h31",
1959 static const char *const b_name[] =
1961 "b0", "b1", "b2", "b3",
1962 "b4", "b5", "b6", "b7",
1963 "b8", "b9", "b10", "b11",
1964 "b12", "b13", "b14", "b15",
1965 "b16", "b17", "b18", "b19",
1966 "b20", "b21", "b22", "b23",
1967 "b24", "b25", "b26", "b27",
1968 "b28", "b29", "b30", "b31",
1971 regnum -= gdbarch_num_regs (gdbarch);
1973 if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
1974 return q_name[regnum - AARCH64_Q0_REGNUM];
1976 if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
1977 return d_name[regnum - AARCH64_D0_REGNUM];
1979 if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
1980 return s_name[regnum - AARCH64_S0_REGNUM];
1982 if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
1983 return h_name[regnum - AARCH64_H0_REGNUM];
1985 if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
1986 return b_name[regnum - AARCH64_B0_REGNUM];
1988 internal_error (__FILE__, __LINE__,
1989 _("aarch64_pseudo_register_name: bad register number %d"),
1993 /* Implement the "pseudo_register_type" tdesc_arch_data method. */
1995 static struct type *
1996 aarch64_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
1998 regnum -= gdbarch_num_regs (gdbarch);
2000 if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
2001 return aarch64_vnq_type (gdbarch);
2003 if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
2004 return aarch64_vnd_type (gdbarch);
2006 if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
2007 return aarch64_vns_type (gdbarch);
2009 if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
2010 return aarch64_vnh_type (gdbarch);
2012 if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
2013 return aarch64_vnb_type (gdbarch);
2015 internal_error (__FILE__, __LINE__,
2016 _("aarch64_pseudo_register_type: bad register number %d"),
2020 /* Implement the "pseudo_register_reggroup_p" tdesc_arch_data method. */
2023 aarch64_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
2024 struct reggroup *group)
2026 regnum -= gdbarch_num_regs (gdbarch);
2028 if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
2029 return group == all_reggroup || group == vector_reggroup;
2030 else if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
2031 return (group == all_reggroup || group == vector_reggroup
2032 || group == float_reggroup);
2033 else if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
2034 return (group == all_reggroup || group == vector_reggroup
2035 || group == float_reggroup);
2036 else if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
2037 return group == all_reggroup || group == vector_reggroup;
2038 else if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
2039 return group == all_reggroup || group == vector_reggroup;
2041 return group == all_reggroup;
2044 /* Implement the "pseudo_register_read_value" gdbarch method. */
2046 static struct value *
2047 aarch64_pseudo_read_value (struct gdbarch *gdbarch,
2048 struct regcache *regcache,
2051 gdb_byte reg_buf[MAX_REGISTER_SIZE];
2052 struct value *result_value;
2055 result_value = allocate_value (register_type (gdbarch, regnum));
2056 VALUE_LVAL (result_value) = lval_register;
2057 VALUE_REGNUM (result_value) = regnum;
2058 buf = value_contents_raw (result_value);
2060 regnum -= gdbarch_num_regs (gdbarch);
2062 if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
2064 enum register_status status;
2067 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_Q0_REGNUM;
2068 status = regcache_raw_read (regcache, v_regnum, reg_buf);
2069 if (status != REG_VALID)
2070 mark_value_bytes_unavailable (result_value, 0,
2071 TYPE_LENGTH (value_type (result_value)));
2073 memcpy (buf, reg_buf, Q_REGISTER_SIZE);
2074 return result_value;
2077 if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
2079 enum register_status status;
2082 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_D0_REGNUM;
2083 status = regcache_raw_read (regcache, v_regnum, reg_buf);
2084 if (status != REG_VALID)
2085 mark_value_bytes_unavailable (result_value, 0,
2086 TYPE_LENGTH (value_type (result_value)));
2088 memcpy (buf, reg_buf, D_REGISTER_SIZE);
2089 return result_value;
2092 if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
2094 enum register_status status;
2097 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_S0_REGNUM;
2098 status = regcache_raw_read (regcache, v_regnum, reg_buf);
2099 if (status != REG_VALID)
2100 mark_value_bytes_unavailable (result_value, 0,
2101 TYPE_LENGTH (value_type (result_value)));
2103 memcpy (buf, reg_buf, S_REGISTER_SIZE);
2104 return result_value;
2107 if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
2109 enum register_status status;
2112 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_H0_REGNUM;
2113 status = regcache_raw_read (regcache, v_regnum, reg_buf);
2114 if (status != REG_VALID)
2115 mark_value_bytes_unavailable (result_value, 0,
2116 TYPE_LENGTH (value_type (result_value)));
2118 memcpy (buf, reg_buf, H_REGISTER_SIZE);
2119 return result_value;
2122 if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
2124 enum register_status status;
2127 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_B0_REGNUM;
2128 status = regcache_raw_read (regcache, v_regnum, reg_buf);
2129 if (status != REG_VALID)
2130 mark_value_bytes_unavailable (result_value, 0,
2131 TYPE_LENGTH (value_type (result_value)));
2133 memcpy (buf, reg_buf, B_REGISTER_SIZE);
2134 return result_value;
2137 gdb_assert_not_reached ("regnum out of bound");
2140 /* Implement the "pseudo_register_write" gdbarch method. */
2143 aarch64_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
2144 int regnum, const gdb_byte *buf)
2146 gdb_byte reg_buf[MAX_REGISTER_SIZE];
2148 /* Ensure the register buffer is zero, we want gdb writes of the
2149 various 'scalar' pseudo registers to behavior like architectural
2150 writes, register width bytes are written the remainder are set to
2152 memset (reg_buf, 0, sizeof (reg_buf));
2154 regnum -= gdbarch_num_regs (gdbarch);
2156 if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
2158 /* pseudo Q registers */
2161 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_Q0_REGNUM;
2162 memcpy (reg_buf, buf, Q_REGISTER_SIZE);
2163 regcache_raw_write (regcache, v_regnum, reg_buf);
2167 if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
2169 /* pseudo D registers */
2172 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_D0_REGNUM;
2173 memcpy (reg_buf, buf, D_REGISTER_SIZE);
2174 regcache_raw_write (regcache, v_regnum, reg_buf);
2178 if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
2182 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_S0_REGNUM;
2183 memcpy (reg_buf, buf, S_REGISTER_SIZE);
2184 regcache_raw_write (regcache, v_regnum, reg_buf);
2188 if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
2190 /* pseudo H registers */
2193 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_H0_REGNUM;
2194 memcpy (reg_buf, buf, H_REGISTER_SIZE);
2195 regcache_raw_write (regcache, v_regnum, reg_buf);
2199 if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
2201 /* pseudo B registers */
2204 v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_B0_REGNUM;
2205 memcpy (reg_buf, buf, B_REGISTER_SIZE);
2206 regcache_raw_write (regcache, v_regnum, reg_buf);
2210 gdb_assert_not_reached ("regnum out of bound");
2213 /* Callback function for user_reg_add. */
2215 static struct value *
2216 value_of_aarch64_user_reg (struct frame_info *frame, const void *baton)
2218 const int *reg_p = (const int *) baton;
2220 return value_of_register (*reg_p, frame);
2224 /* Implement the "software_single_step" gdbarch method, needed to
2225 single step through atomic sequences on AArch64. */
2228 aarch64_software_single_step (struct frame_info *frame)
2230 struct gdbarch *gdbarch = get_frame_arch (frame);
2231 struct address_space *aspace = get_frame_address_space (frame);
2232 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2233 const int insn_size = 4;
2234 const int atomic_sequence_length = 16; /* Instruction sequence length. */
2235 CORE_ADDR pc = get_frame_pc (frame);
2236 CORE_ADDR breaks[2] = { -1, -1 };
2238 CORE_ADDR closing_insn = 0;
2239 uint32_t insn = read_memory_unsigned_integer (loc, insn_size,
2240 byte_order_for_code);
2243 int bc_insn_count = 0; /* Conditional branch instruction count. */
2244 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
2247 if (aarch64_decode_insn (insn, &inst, 1) != 0)
2250 /* Look for a Load Exclusive instruction which begins the sequence. */
2251 if (inst.opcode->iclass != ldstexcl || bit (insn, 22) == 0)
2254 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
2257 insn = read_memory_unsigned_integer (loc, insn_size,
2258 byte_order_for_code);
2260 if (aarch64_decode_insn (insn, &inst, 1) != 0)
2262 /* Check if the instruction is a conditional branch. */
2263 if (inst.opcode->iclass == condbranch)
2265 gdb_assert (inst.operands[0].type == AARCH64_OPND_ADDR_PCREL19);
2267 if (bc_insn_count >= 1)
2270 /* It is, so we'll try to set a breakpoint at the destination. */
2271 breaks[1] = loc + inst.operands[0].imm.value;
2277 /* Look for the Store Exclusive which closes the atomic sequence. */
2278 if (inst.opcode->iclass == ldstexcl && bit (insn, 22) == 0)
2285 /* We didn't find a closing Store Exclusive instruction, fall back. */
2289 /* Insert breakpoint after the end of the atomic sequence. */
2290 breaks[0] = loc + insn_size;
2292 /* Check for duplicated breakpoints, and also check that the second
2293 breakpoint is not within the atomic sequence. */
2295 && (breaks[1] == breaks[0]
2296 || (breaks[1] >= pc && breaks[1] <= closing_insn)))
2297 last_breakpoint = 0;
2299 /* Insert the breakpoint at the end of the sequence, and one at the
2300 destination of the conditional branch, if it exists. */
2301 for (index = 0; index <= last_breakpoint; index++)
2302 insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
2307 struct displaced_step_closure
2309 /* It is true when condition instruction, such as B.CON, TBZ, etc,
2310 is being displaced stepping. */
2313 /* PC adjustment offset after displaced stepping. */
2317 /* Data when visiting instructions for displaced stepping. */
2319 struct aarch64_displaced_step_data
2321 struct aarch64_insn_data base;
2323 /* The address where the instruction will be executed at. */
2325 /* Buffer of instructions to be copied to NEW_ADDR to execute. */
2326 uint32_t insn_buf[DISPLACED_MODIFIED_INSNS];
2327 /* Number of instructions in INSN_BUF. */
2328 unsigned insn_count;
2329 /* Registers when doing displaced stepping. */
2330 struct regcache *regs;
2332 struct displaced_step_closure *dsc;
2335 /* Implementation of aarch64_insn_visitor method "b". */
2338 aarch64_displaced_step_b (const int is_bl, const int32_t offset,
2339 struct aarch64_insn_data *data)
2341 struct aarch64_displaced_step_data *dsd
2342 = (struct aarch64_displaced_step_data *) data;
2343 int32_t new_offset = data->insn_addr - dsd->new_addr + offset;
2345 if (can_encode_int32 (new_offset, 28))
2347 /* Emit B rather than BL, because executing BL on a new address
2348 will get the wrong address into LR. In order to avoid this,
2349 we emit B, and update LR if the instruction is BL. */
2350 emit_b (dsd->insn_buf, 0, new_offset);
2356 emit_nop (dsd->insn_buf);
2358 dsd->dsc->pc_adjust = offset;
2364 regcache_cooked_write_unsigned (dsd->regs, AARCH64_LR_REGNUM,
2365 data->insn_addr + 4);
2369 /* Implementation of aarch64_insn_visitor method "b_cond". */
2372 aarch64_displaced_step_b_cond (const unsigned cond, const int32_t offset,
2373 struct aarch64_insn_data *data)
2375 struct aarch64_displaced_step_data *dsd
2376 = (struct aarch64_displaced_step_data *) data;
2377 int32_t new_offset = data->insn_addr - dsd->new_addr + offset;
2379 /* GDB has to fix up PC after displaced step this instruction
2380 differently according to the condition is true or false. Instead
2381 of checking COND against conditional flags, we can use
2382 the following instructions, and GDB can tell how to fix up PC
2383 according to the PC value.
2385 B.COND TAKEN ; If cond is true, then jump to TAKEN.
2391 emit_bcond (dsd->insn_buf, cond, 8);
2393 dsd->dsc->pc_adjust = offset;
2394 dsd->insn_count = 1;
2397 /* Dynamically allocate a new register. If we know the register
2398 statically, we should make it a global as above instead of using this
2401 static struct aarch64_register
2402 aarch64_register (unsigned num, int is64)
2404 return (struct aarch64_register) { num, is64 };
2407 /* Implementation of aarch64_insn_visitor method "cb". */
2410 aarch64_displaced_step_cb (const int32_t offset, const int is_cbnz,
2411 const unsigned rn, int is64,
2412 struct aarch64_insn_data *data)
2414 struct aarch64_displaced_step_data *dsd
2415 = (struct aarch64_displaced_step_data *) data;
2416 int32_t new_offset = data->insn_addr - dsd->new_addr + offset;
2418 /* The offset is out of range for a compare and branch
2419 instruction. We can use the following instructions instead:
2421 CBZ xn, TAKEN ; xn == 0, then jump to TAKEN.
2426 emit_cb (dsd->insn_buf, is_cbnz, aarch64_register (rn, is64), 8);
2427 dsd->insn_count = 1;
2429 dsd->dsc->pc_adjust = offset;
2432 /* Implementation of aarch64_insn_visitor method "tb". */
2435 aarch64_displaced_step_tb (const int32_t offset, int is_tbnz,
2436 const unsigned rt, unsigned bit,
2437 struct aarch64_insn_data *data)
2439 struct aarch64_displaced_step_data *dsd
2440 = (struct aarch64_displaced_step_data *) data;
2441 int32_t new_offset = data->insn_addr - dsd->new_addr + offset;
2443 /* The offset is out of range for a test bit and branch
2444 instruction We can use the following instructions instead:
2446 TBZ xn, #bit, TAKEN ; xn[bit] == 0, then jump to TAKEN.
2452 emit_tb (dsd->insn_buf, is_tbnz, bit, aarch64_register (rt, 1), 8);
2453 dsd->insn_count = 1;
2455 dsd->dsc->pc_adjust = offset;
2458 /* Implementation of aarch64_insn_visitor method "adr". */
2461 aarch64_displaced_step_adr (const int32_t offset, const unsigned rd,
2462 const int is_adrp, struct aarch64_insn_data *data)
2464 struct aarch64_displaced_step_data *dsd
2465 = (struct aarch64_displaced_step_data *) data;
2466 /* We know exactly the address the ADR{P,} instruction will compute.
2467 We can just write it to the destination register. */
2468 CORE_ADDR address = data->insn_addr + offset;
2472 /* Clear the lower 12 bits of the offset to get the 4K page. */
2473 regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rd,
2477 regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rd,
2480 dsd->dsc->pc_adjust = 4;
2481 emit_nop (dsd->insn_buf);
2482 dsd->insn_count = 1;
2485 /* Implementation of aarch64_insn_visitor method "ldr_literal". */
2488 aarch64_displaced_step_ldr_literal (const int32_t offset, const int is_sw,
2489 const unsigned rt, const int is64,
2490 struct aarch64_insn_data *data)
2492 struct aarch64_displaced_step_data *dsd
2493 = (struct aarch64_displaced_step_data *) data;
2494 CORE_ADDR address = data->insn_addr + offset;
2495 struct aarch64_memory_operand zero = { MEMORY_OPERAND_OFFSET, 0 };
2497 regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rt,
2501 dsd->insn_count = emit_ldrsw (dsd->insn_buf, aarch64_register (rt, 1),
2502 aarch64_register (rt, 1), zero);
2504 dsd->insn_count = emit_ldr (dsd->insn_buf, aarch64_register (rt, is64),
2505 aarch64_register (rt, 1), zero);
2507 dsd->dsc->pc_adjust = 4;
2510 /* Implementation of aarch64_insn_visitor method "others". */
2513 aarch64_displaced_step_others (const uint32_t insn,
2514 struct aarch64_insn_data *data)
2516 struct aarch64_displaced_step_data *dsd
2517 = (struct aarch64_displaced_step_data *) data;
2519 aarch64_emit_insn (dsd->insn_buf, insn);
2520 dsd->insn_count = 1;
2522 if ((insn & 0xfffffc1f) == 0xd65f0000)
2525 dsd->dsc->pc_adjust = 0;
2528 dsd->dsc->pc_adjust = 4;
2531 static const struct aarch64_insn_visitor visitor =
2533 aarch64_displaced_step_b,
2534 aarch64_displaced_step_b_cond,
2535 aarch64_displaced_step_cb,
2536 aarch64_displaced_step_tb,
2537 aarch64_displaced_step_adr,
2538 aarch64_displaced_step_ldr_literal,
2539 aarch64_displaced_step_others,
2542 /* Implement the "displaced_step_copy_insn" gdbarch method. */
2544 struct displaced_step_closure *
2545 aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
2546 CORE_ADDR from, CORE_ADDR to,
2547 struct regcache *regs)
2549 struct displaced_step_closure *dsc = NULL;
2550 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2551 uint32_t insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
2552 struct aarch64_displaced_step_data dsd;
2555 if (aarch64_decode_insn (insn, &inst, 1) != 0)
2558 /* Look for a Load Exclusive instruction which begins the sequence. */
2559 if (inst.opcode->iclass == ldstexcl && bit (insn, 22))
2561 /* We can't displaced step atomic sequences. */
2565 dsc = XCNEW (struct displaced_step_closure);
2566 dsd.base.insn_addr = from;
2571 aarch64_relocate_instruction (insn, &visitor,
2572 (struct aarch64_insn_data *) &dsd);
2573 gdb_assert (dsd.insn_count <= DISPLACED_MODIFIED_INSNS);
2575 if (dsd.insn_count != 0)
2579 /* Instruction can be relocated to scratch pad. Copy
2580 relocated instruction(s) there. */
2581 for (i = 0; i < dsd.insn_count; i++)
2583 if (debug_displaced)
2585 debug_printf ("displaced: writing insn ");
2586 debug_printf ("%.8x", dsd.insn_buf[i]);
2587 debug_printf (" at %s\n", paddress (gdbarch, to + i * 4));
2589 write_memory_unsigned_integer (to + i * 4, 4, byte_order_for_code,
2590 (ULONGEST) dsd.insn_buf[i]);
2602 /* Implement the "displaced_step_fixup" gdbarch method. */
2605 aarch64_displaced_step_fixup (struct gdbarch *gdbarch,
2606 struct displaced_step_closure *dsc,
2607 CORE_ADDR from, CORE_ADDR to,
2608 struct regcache *regs)
2614 regcache_cooked_read_unsigned (regs, AARCH64_PC_REGNUM, &pc);
2617 /* Condition is true. */
2619 else if (pc - to == 4)
2621 /* Condition is false. */
2625 gdb_assert_not_reached ("Unexpected PC value after displaced stepping");
2628 if (dsc->pc_adjust != 0)
2630 if (debug_displaced)
2632 debug_printf ("displaced: fixup: set PC to %s:%d\n",
2633 paddress (gdbarch, from), dsc->pc_adjust);
2635 regcache_cooked_write_unsigned (regs, AARCH64_PC_REGNUM,
2636 from + dsc->pc_adjust);
2640 /* Implement the "displaced_step_hw_singlestep" gdbarch method. */
2643 aarch64_displaced_step_hw_singlestep (struct gdbarch *gdbarch,
2644 struct displaced_step_closure *closure)
2649 /* Initialize the current architecture based on INFO. If possible,
2650 re-use an architecture from ARCHES, which is a list of
2651 architectures already created during this debugging session.
2653 Called e.g. at program startup, when reading a core file, and when
2654 reading a binary file. */
2656 static struct gdbarch *
2657 aarch64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2659 struct gdbarch_tdep *tdep;
2660 struct gdbarch *gdbarch;
2661 struct gdbarch_list *best_arch;
2662 struct tdesc_arch_data *tdesc_data = NULL;
2663 const struct target_desc *tdesc = info.target_desc;
2665 int have_fpa_registers = 1;
2667 const struct tdesc_feature *feature;
2669 int num_pseudo_regs = 0;
2671 /* Ensure we always have a target descriptor. */
2672 if (!tdesc_has_registers (tdesc))
2673 tdesc = tdesc_aarch64;
2677 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.core");
2679 if (feature == NULL)
2682 tdesc_data = tdesc_data_alloc ();
2684 /* Validate the descriptor provides the mandatory core R registers
2685 and allocate their numbers. */
2686 for (i = 0; i < ARRAY_SIZE (aarch64_r_register_names); i++)
2688 tdesc_numbered_register (feature, tdesc_data, AARCH64_X0_REGNUM + i,
2689 aarch64_r_register_names[i]);
2691 num_regs = AARCH64_X0_REGNUM + i;
2693 /* Look for the V registers. */
2694 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.fpu");
2697 /* Validate the descriptor provides the mandatory V registers
2698 and allocate their numbers. */
2699 for (i = 0; i < ARRAY_SIZE (aarch64_v_register_names); i++)
2701 tdesc_numbered_register (feature, tdesc_data, AARCH64_V0_REGNUM + i,
2702 aarch64_v_register_names[i]);
2704 num_regs = AARCH64_V0_REGNUM + i;
2706 num_pseudo_regs += 32; /* add the Qn scalar register pseudos */
2707 num_pseudo_regs += 32; /* add the Dn scalar register pseudos */
2708 num_pseudo_regs += 32; /* add the Sn scalar register pseudos */
2709 num_pseudo_regs += 32; /* add the Hn scalar register pseudos */
2710 num_pseudo_regs += 32; /* add the Bn scalar register pseudos */
2715 tdesc_data_cleanup (tdesc_data);
2719 /* AArch64 code is always little-endian. */
2720 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
2722 /* If there is already a candidate, use it. */
2723 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
2725 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
2727 /* Found a match. */
2731 if (best_arch != NULL)
2733 if (tdesc_data != NULL)
2734 tdesc_data_cleanup (tdesc_data);
2735 return best_arch->gdbarch;
2738 tdep = XCNEW (struct gdbarch_tdep);
2739 gdbarch = gdbarch_alloc (&info, tdep);
2741 /* This should be low enough for everything. */
2742 tdep->lowest_pc = 0x20;
2743 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
2744 tdep->jb_elt_size = 8;
2746 set_gdbarch_push_dummy_call (gdbarch, aarch64_push_dummy_call);
2747 set_gdbarch_frame_align (gdbarch, aarch64_frame_align);
2749 /* Frame handling. */
2750 set_gdbarch_dummy_id (gdbarch, aarch64_dummy_id);
2751 set_gdbarch_unwind_pc (gdbarch, aarch64_unwind_pc);
2752 set_gdbarch_unwind_sp (gdbarch, aarch64_unwind_sp);
2754 /* Advance PC across function entry code. */
2755 set_gdbarch_skip_prologue (gdbarch, aarch64_skip_prologue);
2757 /* The stack grows downward. */
2758 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2760 /* Breakpoint manipulation. */
2761 set_gdbarch_breakpoint_from_pc (gdbarch, aarch64_breakpoint_from_pc);
2762 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
2763 set_gdbarch_software_single_step (gdbarch, aarch64_software_single_step);
2765 /* Information about registers, etc. */
2766 set_gdbarch_sp_regnum (gdbarch, AARCH64_SP_REGNUM);
2767 set_gdbarch_pc_regnum (gdbarch, AARCH64_PC_REGNUM);
2768 set_gdbarch_num_regs (gdbarch, num_regs);
2770 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudo_regs);
2771 set_gdbarch_pseudo_register_read_value (gdbarch, aarch64_pseudo_read_value);
2772 set_gdbarch_pseudo_register_write (gdbarch, aarch64_pseudo_write);
2773 set_tdesc_pseudo_register_name (gdbarch, aarch64_pseudo_register_name);
2774 set_tdesc_pseudo_register_type (gdbarch, aarch64_pseudo_register_type);
2775 set_tdesc_pseudo_register_reggroup_p (gdbarch,
2776 aarch64_pseudo_register_reggroup_p);
2779 set_gdbarch_short_bit (gdbarch, 16);
2780 set_gdbarch_int_bit (gdbarch, 32);
2781 set_gdbarch_float_bit (gdbarch, 32);
2782 set_gdbarch_double_bit (gdbarch, 64);
2783 set_gdbarch_long_double_bit (gdbarch, 128);
2784 set_gdbarch_long_bit (gdbarch, 64);
2785 set_gdbarch_long_long_bit (gdbarch, 64);
2786 set_gdbarch_ptr_bit (gdbarch, 64);
2787 set_gdbarch_char_signed (gdbarch, 0);
2788 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
2789 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
2790 set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
2792 /* Internal <-> external register number maps. */
2793 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, aarch64_dwarf_reg_to_regnum);
2795 /* Returning results. */
2796 set_gdbarch_return_value (gdbarch, aarch64_return_value);
2799 set_gdbarch_print_insn (gdbarch, aarch64_gdb_print_insn);
2801 /* Virtual tables. */
2802 set_gdbarch_vbit_in_delta (gdbarch, 1);
2804 /* Hook in the ABI-specific overrides, if they have been registered. */
2805 info.target_desc = tdesc;
2806 info.tdep_info = (void *) tdesc_data;
2807 gdbarch_init_osabi (info, gdbarch);
2809 dwarf2_frame_set_init_reg (gdbarch, aarch64_dwarf2_frame_init_reg);
2811 /* Add some default predicates. */
2812 frame_unwind_append_unwinder (gdbarch, &aarch64_stub_unwind);
2813 dwarf2_append_unwinders (gdbarch);
2814 frame_unwind_append_unwinder (gdbarch, &aarch64_prologue_unwind);
2816 frame_base_set_default (gdbarch, &aarch64_normal_base);
2818 /* Now we have tuned the configuration, set a few final things,
2819 based on what the OS ABI has told us. */
2821 if (tdep->jb_pc >= 0)
2822 set_gdbarch_get_longjmp_target (gdbarch, aarch64_get_longjmp_target);
2824 set_gdbarch_gen_return_address (gdbarch, aarch64_gen_return_address);
2826 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
2828 /* Add standard register aliases. */
2829 for (i = 0; i < ARRAY_SIZE (aarch64_register_aliases); i++)
2830 user_reg_add (gdbarch, aarch64_register_aliases[i].name,
2831 value_of_aarch64_user_reg,
2832 &aarch64_register_aliases[i].regnum);
2838 aarch64_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
2840 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2845 fprintf_unfiltered (file, _("aarch64_dump_tdep: Lowest pc = 0x%s"),
2846 paddress (gdbarch, tdep->lowest_pc));
2849 /* Suppress warning from -Wmissing-prototypes. */
2850 extern initialize_file_ftype _initialize_aarch64_tdep;
2853 _initialize_aarch64_tdep (void)
2855 gdbarch_register (bfd_arch_aarch64, aarch64_gdbarch_init,
2858 initialize_tdesc_aarch64 ();
2860 /* Debug this file's internals. */
2861 add_setshow_boolean_cmd ("aarch64", class_maintenance, &aarch64_debug, _("\
2862 Set AArch64 debugging."), _("\
2863 Show AArch64 debugging."), _("\
2864 When on, AArch64 specific debugging is enabled."),
2867 &setdebuglist, &showdebuglist);
2870 /* AArch64 process record-replay related structures, defines etc. */
2872 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
2875 unsigned int reg_len = LENGTH; \
2878 REGS = XNEWVEC (uint32_t, reg_len); \
2879 memcpy(®S[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
2884 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
2887 unsigned int mem_len = LENGTH; \
2890 MEMS = XNEWVEC (struct aarch64_mem_r, mem_len); \
2891 memcpy(&MEMS->len, &RECORD_BUF[0], \
2892 sizeof(struct aarch64_mem_r) * LENGTH); \
2897 /* AArch64 record/replay structures and enumerations. */
2899 struct aarch64_mem_r
2901 uint64_t len; /* Record length. */
2902 uint64_t addr; /* Memory address. */
2905 enum aarch64_record_result
2907 AARCH64_RECORD_SUCCESS,
2908 AARCH64_RECORD_FAILURE,
2909 AARCH64_RECORD_UNSUPPORTED,
2910 AARCH64_RECORD_UNKNOWN
2913 typedef struct insn_decode_record_t
2915 struct gdbarch *gdbarch;
2916 struct regcache *regcache;
2917 CORE_ADDR this_addr; /* Address of insn to be recorded. */
2918 uint32_t aarch64_insn; /* Insn to be recorded. */
2919 uint32_t mem_rec_count; /* Count of memory records. */
2920 uint32_t reg_rec_count; /* Count of register records. */
2921 uint32_t *aarch64_regs; /* Registers to be recorded. */
2922 struct aarch64_mem_r *aarch64_mems; /* Memory locations to be recorded. */
2923 } insn_decode_record;
2925 /* Record handler for data processing - register instructions. */
2928 aarch64_record_data_proc_reg (insn_decode_record *aarch64_insn_r)
2930 uint8_t reg_rd, insn_bits24_27, insn_bits21_23;
2931 uint32_t record_buf[4];
2933 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
2934 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
2935 insn_bits21_23 = bits (aarch64_insn_r->aarch64_insn, 21, 23);
2937 if (!bit (aarch64_insn_r->aarch64_insn, 28))
2941 /* Logical (shifted register). */
2942 if (insn_bits24_27 == 0x0a)
2943 setflags = (bits (aarch64_insn_r->aarch64_insn, 29, 30) == 0x03);
2945 else if (insn_bits24_27 == 0x0b)
2946 setflags = bit (aarch64_insn_r->aarch64_insn, 29);
2948 return AARCH64_RECORD_UNKNOWN;
2950 record_buf[0] = reg_rd;
2951 aarch64_insn_r->reg_rec_count = 1;
2953 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
2957 if (insn_bits24_27 == 0x0b)
2959 /* Data-processing (3 source). */
2960 record_buf[0] = reg_rd;
2961 aarch64_insn_r->reg_rec_count = 1;
2963 else if (insn_bits24_27 == 0x0a)
2965 if (insn_bits21_23 == 0x00)
2967 /* Add/subtract (with carry). */
2968 record_buf[0] = reg_rd;
2969 aarch64_insn_r->reg_rec_count = 1;
2970 if (bit (aarch64_insn_r->aarch64_insn, 29))
2972 record_buf[1] = AARCH64_CPSR_REGNUM;
2973 aarch64_insn_r->reg_rec_count = 2;
2976 else if (insn_bits21_23 == 0x02)
2978 /* Conditional compare (register) and conditional compare
2979 (immediate) instructions. */
2980 record_buf[0] = AARCH64_CPSR_REGNUM;
2981 aarch64_insn_r->reg_rec_count = 1;
2983 else if (insn_bits21_23 == 0x04 || insn_bits21_23 == 0x06)
2985 /* CConditional select. */
2986 /* Data-processing (2 source). */
2987 /* Data-processing (1 source). */
2988 record_buf[0] = reg_rd;
2989 aarch64_insn_r->reg_rec_count = 1;
2992 return AARCH64_RECORD_UNKNOWN;
2996 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
2998 return AARCH64_RECORD_SUCCESS;
3001 /* Record handler for data processing - immediate instructions. */
3004 aarch64_record_data_proc_imm (insn_decode_record *aarch64_insn_r)
3006 uint8_t reg_rd, insn_bit28, insn_bit23, insn_bits24_27, setflags;
3007 uint32_t record_buf[4];
3009 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3010 insn_bit28 = bit (aarch64_insn_r->aarch64_insn, 28);
3011 insn_bit23 = bit (aarch64_insn_r->aarch64_insn, 23);
3012 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3014 if (insn_bits24_27 == 0x00 /* PC rel addressing. */
3015 || insn_bits24_27 == 0x03 /* Bitfield and Extract. */
3016 || (insn_bits24_27 == 0x02 && insn_bit23)) /* Move wide (immediate). */
3018 record_buf[0] = reg_rd;
3019 aarch64_insn_r->reg_rec_count = 1;
3021 else if (insn_bits24_27 == 0x01)
3023 /* Add/Subtract (immediate). */
3024 setflags = bit (aarch64_insn_r->aarch64_insn, 29);
3025 record_buf[0] = reg_rd;
3026 aarch64_insn_r->reg_rec_count = 1;
3028 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
3030 else if (insn_bits24_27 == 0x02 && !insn_bit23)
3032 /* Logical (immediate). */
3033 setflags = bits (aarch64_insn_r->aarch64_insn, 29, 30) == 0x03;
3034 record_buf[0] = reg_rd;
3035 aarch64_insn_r->reg_rec_count = 1;
3037 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
3040 return AARCH64_RECORD_UNKNOWN;
3042 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3044 return AARCH64_RECORD_SUCCESS;
3047 /* Record handler for branch, exception generation and system instructions. */
3050 aarch64_record_branch_except_sys (insn_decode_record *aarch64_insn_r)
3052 struct gdbarch_tdep *tdep = gdbarch_tdep (aarch64_insn_r->gdbarch);
3053 uint8_t insn_bits24_27, insn_bits28_31, insn_bits22_23;
3054 uint32_t record_buf[4];
3056 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3057 insn_bits28_31 = bits (aarch64_insn_r->aarch64_insn, 28, 31);
3058 insn_bits22_23 = bits (aarch64_insn_r->aarch64_insn, 22, 23);
3060 if (insn_bits28_31 == 0x0d)
3062 /* Exception generation instructions. */
3063 if (insn_bits24_27 == 0x04)
3065 if (!bits (aarch64_insn_r->aarch64_insn, 2, 4)
3066 && !bits (aarch64_insn_r->aarch64_insn, 21, 23)
3067 && bits (aarch64_insn_r->aarch64_insn, 0, 1) == 0x01)
3069 ULONGEST svc_number;
3071 regcache_raw_read_unsigned (aarch64_insn_r->regcache, 8,
3073 return tdep->aarch64_syscall_record (aarch64_insn_r->regcache,
3077 return AARCH64_RECORD_UNSUPPORTED;
3079 /* System instructions. */
3080 else if (insn_bits24_27 == 0x05 && insn_bits22_23 == 0x00)
3082 uint32_t reg_rt, reg_crn;
3084 reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3085 reg_crn = bits (aarch64_insn_r->aarch64_insn, 12, 15);
3087 /* Record rt in case of sysl and mrs instructions. */
3088 if (bit (aarch64_insn_r->aarch64_insn, 21))
3090 record_buf[0] = reg_rt;
3091 aarch64_insn_r->reg_rec_count = 1;
3093 /* Record cpsr for hint and msr(immediate) instructions. */
3094 else if (reg_crn == 0x02 || reg_crn == 0x04)
3096 record_buf[0] = AARCH64_CPSR_REGNUM;
3097 aarch64_insn_r->reg_rec_count = 1;
3100 /* Unconditional branch (register). */
3101 else if((insn_bits24_27 & 0x0e) == 0x06)
3103 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
3104 if (bits (aarch64_insn_r->aarch64_insn, 21, 22) == 0x01)
3105 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_LR_REGNUM;
3108 return AARCH64_RECORD_UNKNOWN;
3110 /* Unconditional branch (immediate). */
3111 else if ((insn_bits28_31 & 0x07) == 0x01 && (insn_bits24_27 & 0x0c) == 0x04)
3113 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
3114 if (bit (aarch64_insn_r->aarch64_insn, 31))
3115 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_LR_REGNUM;
3118 /* Compare & branch (immediate), Test & branch (immediate) and
3119 Conditional branch (immediate). */
3120 record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
3122 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3124 return AARCH64_RECORD_SUCCESS;
3127 /* Record handler for advanced SIMD load and store instructions. */
3130 aarch64_record_asimd_load_store (insn_decode_record *aarch64_insn_r)
3133 uint64_t addr_offset = 0;
3134 uint32_t record_buf[24];
3135 uint64_t record_buf_mem[24];
3136 uint32_t reg_rn, reg_rt;
3137 uint32_t reg_index = 0, mem_index = 0;
3138 uint8_t opcode_bits, size_bits;
3140 reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3141 reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
3142 size_bits = bits (aarch64_insn_r->aarch64_insn, 10, 11);
3143 opcode_bits = bits (aarch64_insn_r->aarch64_insn, 12, 15);
3144 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn, &address);
3147 debug_printf ("Process record: Advanced SIMD load/store\n");
3149 /* Load/store single structure. */
3150 if (bit (aarch64_insn_r->aarch64_insn, 24))
3152 uint8_t sindex, scale, selem, esize, replicate = 0;
3153 scale = opcode_bits >> 2;
3154 selem = ((opcode_bits & 0x02) |
3155 bit (aarch64_insn_r->aarch64_insn, 21)) + 1;
3159 if (size_bits & 0x01)
3160 return AARCH64_RECORD_UNKNOWN;
3163 if ((size_bits >> 1) & 0x01)
3164 return AARCH64_RECORD_UNKNOWN;
3165 if (size_bits & 0x01)
3167 if (!((opcode_bits >> 1) & 0x01))
3170 return AARCH64_RECORD_UNKNOWN;
3174 if (bit (aarch64_insn_r->aarch64_insn, 22) && !(opcode_bits & 0x01))
3181 return AARCH64_RECORD_UNKNOWN;
3187 for (sindex = 0; sindex < selem; sindex++)
3189 record_buf[reg_index++] = reg_rt + AARCH64_V0_REGNUM;
3190 reg_rt = (reg_rt + 1) % 32;
3194 for (sindex = 0; sindex < selem; sindex++)
3196 if (bit (aarch64_insn_r->aarch64_insn, 22))
3197 record_buf[reg_index++] = reg_rt + AARCH64_V0_REGNUM;
3200 record_buf_mem[mem_index++] = esize / 8;
3201 record_buf_mem[mem_index++] = address + addr_offset;
3203 addr_offset = addr_offset + (esize / 8);
3204 reg_rt = (reg_rt + 1) % 32;
3208 /* Load/store multiple structure. */
3211 uint8_t selem, esize, rpt, elements;
3212 uint8_t eindex, rindex;
3214 esize = 8 << size_bits;
3215 if (bit (aarch64_insn_r->aarch64_insn, 30))
3216 elements = 128 / esize;
3218 elements = 64 / esize;
3220 switch (opcode_bits)
3222 /*LD/ST4 (4 Registers). */
3227 /*LD/ST1 (4 Registers). */
3232 /*LD/ST3 (3 Registers). */
3237 /*LD/ST1 (3 Registers). */
3242 /*LD/ST1 (1 Register). */
3247 /*LD/ST2 (2 Registers). */
3252 /*LD/ST1 (2 Registers). */
3258 return AARCH64_RECORD_UNSUPPORTED;
3261 for (rindex = 0; rindex < rpt; rindex++)
3262 for (eindex = 0; eindex < elements; eindex++)
3264 uint8_t reg_tt, sindex;
3265 reg_tt = (reg_rt + rindex) % 32;
3266 for (sindex = 0; sindex < selem; sindex++)
3268 if (bit (aarch64_insn_r->aarch64_insn, 22))
3269 record_buf[reg_index++] = reg_tt + AARCH64_V0_REGNUM;
3272 record_buf_mem[mem_index++] = esize / 8;
3273 record_buf_mem[mem_index++] = address + addr_offset;
3275 addr_offset = addr_offset + (esize / 8);
3276 reg_tt = (reg_tt + 1) % 32;
3281 if (bit (aarch64_insn_r->aarch64_insn, 23))
3282 record_buf[reg_index++] = reg_rn;
3284 aarch64_insn_r->reg_rec_count = reg_index;
3285 aarch64_insn_r->mem_rec_count = mem_index / 2;
3286 MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
3288 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3290 return AARCH64_RECORD_SUCCESS;
3293 /* Record handler for load and store instructions. */
3296 aarch64_record_load_store (insn_decode_record *aarch64_insn_r)
3298 uint8_t insn_bits24_27, insn_bits28_29, insn_bits10_11;
3299 uint8_t insn_bit23, insn_bit21;
3300 uint8_t opc, size_bits, ld_flag, vector_flag;
3301 uint32_t reg_rn, reg_rt, reg_rt2;
3302 uint64_t datasize, offset;
3303 uint32_t record_buf[8];
3304 uint64_t record_buf_mem[8];
3307 insn_bits10_11 = bits (aarch64_insn_r->aarch64_insn, 10, 11);
3308 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3309 insn_bits28_29 = bits (aarch64_insn_r->aarch64_insn, 28, 29);
3310 insn_bit21 = bit (aarch64_insn_r->aarch64_insn, 21);
3311 insn_bit23 = bit (aarch64_insn_r->aarch64_insn, 23);
3312 ld_flag = bit (aarch64_insn_r->aarch64_insn, 22);
3313 vector_flag = bit (aarch64_insn_r->aarch64_insn, 26);
3314 reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3315 reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
3316 reg_rt2 = bits (aarch64_insn_r->aarch64_insn, 10, 14);
3317 size_bits = bits (aarch64_insn_r->aarch64_insn, 30, 31);
3319 /* Load/store exclusive. */
3320 if (insn_bits24_27 == 0x08 && insn_bits28_29 == 0x00)
3323 debug_printf ("Process record: load/store exclusive\n");
3327 record_buf[0] = reg_rt;
3328 aarch64_insn_r->reg_rec_count = 1;
3331 record_buf[1] = reg_rt2;
3332 aarch64_insn_r->reg_rec_count = 2;
3338 datasize = (8 << size_bits) * 2;
3340 datasize = (8 << size_bits);
3341 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3343 record_buf_mem[0] = datasize / 8;
3344 record_buf_mem[1] = address;
3345 aarch64_insn_r->mem_rec_count = 1;
3348 /* Save register rs. */
3349 record_buf[0] = bits (aarch64_insn_r->aarch64_insn, 16, 20);
3350 aarch64_insn_r->reg_rec_count = 1;
3354 /* Load register (literal) instructions decoding. */
3355 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x01)
3358 debug_printf ("Process record: load register (literal)\n");
3360 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3362 record_buf[0] = reg_rt;
3363 aarch64_insn_r->reg_rec_count = 1;
3365 /* All types of load/store pair instructions decoding. */
3366 else if ((insn_bits24_27 & 0x0a) == 0x08 && insn_bits28_29 == 0x02)
3369 debug_printf ("Process record: load/store pair\n");
3375 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3376 record_buf[1] = reg_rt2 + AARCH64_V0_REGNUM;
3380 record_buf[0] = reg_rt;
3381 record_buf[1] = reg_rt2;
3383 aarch64_insn_r->reg_rec_count = 2;
3388 imm7_off = bits (aarch64_insn_r->aarch64_insn, 15, 21);
3390 size_bits = size_bits >> 1;
3391 datasize = 8 << (2 + size_bits);
3392 offset = (imm7_off & 0x40) ? (~imm7_off & 0x007f) + 1 : imm7_off;
3393 offset = offset << (2 + size_bits);
3394 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3396 if (!((insn_bits24_27 & 0x0b) == 0x08 && insn_bit23))
3398 if (imm7_off & 0x40)
3399 address = address - offset;
3401 address = address + offset;
3404 record_buf_mem[0] = datasize / 8;
3405 record_buf_mem[1] = address;
3406 record_buf_mem[2] = datasize / 8;
3407 record_buf_mem[3] = address + (datasize / 8);
3408 aarch64_insn_r->mem_rec_count = 2;
3410 if (bit (aarch64_insn_r->aarch64_insn, 23))
3411 record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
3413 /* Load/store register (unsigned immediate) instructions. */
3414 else if ((insn_bits24_27 & 0x0b) == 0x09 && insn_bits28_29 == 0x03)
3416 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
3423 if (size_bits != 0x03)
3426 return AARCH64_RECORD_UNKNOWN;
3430 debug_printf ("Process record: load/store (unsigned immediate):"
3431 " size %x V %d opc %x\n", size_bits, vector_flag,
3437 offset = bits (aarch64_insn_r->aarch64_insn, 10, 21);
3438 datasize = 8 << size_bits;
3439 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3441 offset = offset << size_bits;
3442 address = address + offset;
3444 record_buf_mem[0] = datasize >> 3;
3445 record_buf_mem[1] = address;
3446 aarch64_insn_r->mem_rec_count = 1;
3451 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3453 record_buf[0] = reg_rt;
3454 aarch64_insn_r->reg_rec_count = 1;
3457 /* Load/store register (register offset) instructions. */
3458 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
3459 && insn_bits10_11 == 0x02 && insn_bit21)
3462 debug_printf ("Process record: load/store (register offset)\n");
3463 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
3470 if (size_bits != 0x03)
3473 return AARCH64_RECORD_UNKNOWN;
3477 uint64_t reg_rm_val;
3478 regcache_raw_read_unsigned (aarch64_insn_r->regcache,
3479 bits (aarch64_insn_r->aarch64_insn, 16, 20), ®_rm_val);
3480 if (bit (aarch64_insn_r->aarch64_insn, 12))
3481 offset = reg_rm_val << size_bits;
3483 offset = reg_rm_val;
3484 datasize = 8 << size_bits;
3485 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3487 address = address + offset;
3488 record_buf_mem[0] = datasize >> 3;
3489 record_buf_mem[1] = address;
3490 aarch64_insn_r->mem_rec_count = 1;
3495 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3497 record_buf[0] = reg_rt;
3498 aarch64_insn_r->reg_rec_count = 1;
3501 /* Load/store register (immediate and unprivileged) instructions. */
3502 else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
3507 debug_printf ("Process record: load/store "
3508 "(immediate and unprivileged)\n");
3510 opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
3517 if (size_bits != 0x03)
3520 return AARCH64_RECORD_UNKNOWN;
3525 imm9_off = bits (aarch64_insn_r->aarch64_insn, 12, 20);
3526 offset = (imm9_off & 0x0100) ? (((~imm9_off) & 0x01ff) + 1) : imm9_off;
3527 datasize = 8 << size_bits;
3528 regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3530 if (insn_bits10_11 != 0x01)
3532 if (imm9_off & 0x0100)
3533 address = address - offset;
3535 address = address + offset;
3537 record_buf_mem[0] = datasize >> 3;
3538 record_buf_mem[1] = address;
3539 aarch64_insn_r->mem_rec_count = 1;
3544 record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3546 record_buf[0] = reg_rt;
3547 aarch64_insn_r->reg_rec_count = 1;
3549 if (insn_bits10_11 == 0x01 || insn_bits10_11 == 0x03)
3550 record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
3552 /* Advanced SIMD load/store instructions. */
3554 return aarch64_record_asimd_load_store (aarch64_insn_r);
3556 MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
3558 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3560 return AARCH64_RECORD_SUCCESS;
3563 /* Record handler for data processing SIMD and floating point instructions. */
3566 aarch64_record_data_proc_simd_fp (insn_decode_record *aarch64_insn_r)
3568 uint8_t insn_bit21, opcode, rmode, reg_rd;
3569 uint8_t insn_bits24_27, insn_bits28_31, insn_bits10_11, insn_bits12_15;
3570 uint8_t insn_bits11_14;
3571 uint32_t record_buf[2];
3573 insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3574 insn_bits28_31 = bits (aarch64_insn_r->aarch64_insn, 28, 31);
3575 insn_bits10_11 = bits (aarch64_insn_r->aarch64_insn, 10, 11);
3576 insn_bits12_15 = bits (aarch64_insn_r->aarch64_insn, 12, 15);
3577 insn_bits11_14 = bits (aarch64_insn_r->aarch64_insn, 11, 14);
3578 opcode = bits (aarch64_insn_r->aarch64_insn, 16, 18);
3579 rmode = bits (aarch64_insn_r->aarch64_insn, 19, 20);
3580 reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3581 insn_bit21 = bit (aarch64_insn_r->aarch64_insn, 21);
3584 debug_printf ("Process record: data processing SIMD/FP: ");
3586 if ((insn_bits28_31 & 0x05) == 0x01 && insn_bits24_27 == 0x0e)
3588 /* Floating point - fixed point conversion instructions. */
3592 debug_printf ("FP - fixed point conversion");
3594 if ((opcode >> 1) == 0x0 && rmode == 0x03)
3595 record_buf[0] = reg_rd;
3597 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3599 /* Floating point - conditional compare instructions. */
3600 else if (insn_bits10_11 == 0x01)
3603 debug_printf ("FP - conditional compare");
3605 record_buf[0] = AARCH64_CPSR_REGNUM;
3607 /* Floating point - data processing (2-source) and
3608 conditional select instructions. */
3609 else if (insn_bits10_11 == 0x02 || insn_bits10_11 == 0x03)
3612 debug_printf ("FP - DP (2-source)");
3614 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3616 else if (insn_bits10_11 == 0x00)
3618 /* Floating point - immediate instructions. */
3619 if ((insn_bits12_15 & 0x01) == 0x01
3620 || (insn_bits12_15 & 0x07) == 0x04)
3623 debug_printf ("FP - immediate");
3624 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3626 /* Floating point - compare instructions. */
3627 else if ((insn_bits12_15 & 0x03) == 0x02)
3630 debug_printf ("FP - immediate");
3631 record_buf[0] = AARCH64_CPSR_REGNUM;
3633 /* Floating point - integer conversions instructions. */
3634 else if (insn_bits12_15 == 0x00)
3636 /* Convert float to integer instruction. */
3637 if (!(opcode >> 1) || ((opcode >> 1) == 0x02 && !rmode))
3640 debug_printf ("float to int conversion");
3642 record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
3644 /* Convert integer to float instruction. */
3645 else if ((opcode >> 1) == 0x01 && !rmode)
3648 debug_printf ("int to float conversion");
3650 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3652 /* Move float to integer instruction. */
3653 else if ((opcode >> 1) == 0x03)
3656 debug_printf ("move float to int");
3658 if (!(opcode & 0x01))
3659 record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
3661 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3664 return AARCH64_RECORD_UNKNOWN;
3667 return AARCH64_RECORD_UNKNOWN;
3670 return AARCH64_RECORD_UNKNOWN;
3672 else if ((insn_bits28_31 & 0x09) == 0x00 && insn_bits24_27 == 0x0e)
3675 debug_printf ("SIMD copy");
3677 /* Advanced SIMD copy instructions. */
3678 if (!bits (aarch64_insn_r->aarch64_insn, 21, 23)
3679 && !bit (aarch64_insn_r->aarch64_insn, 15)
3680 && bit (aarch64_insn_r->aarch64_insn, 10))
3682 if (insn_bits11_14 == 0x05 || insn_bits11_14 == 0x07)
3683 record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
3685 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3688 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3690 /* All remaining floating point or advanced SIMD instructions. */
3694 debug_printf ("all remain");
3696 record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3700 debug_printf ("\n");
3702 aarch64_insn_r->reg_rec_count++;
3703 gdb_assert (aarch64_insn_r->reg_rec_count == 1);
3704 REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3706 return AARCH64_RECORD_SUCCESS;
3709 /* Decodes insns type and invokes its record handler. */
3712 aarch64_record_decode_insn_handler (insn_decode_record *aarch64_insn_r)
3714 uint32_t ins_bit25, ins_bit26, ins_bit27, ins_bit28;
3716 ins_bit25 = bit (aarch64_insn_r->aarch64_insn, 25);
3717 ins_bit26 = bit (aarch64_insn_r->aarch64_insn, 26);
3718 ins_bit27 = bit (aarch64_insn_r->aarch64_insn, 27);
3719 ins_bit28 = bit (aarch64_insn_r->aarch64_insn, 28);
3721 /* Data processing - immediate instructions. */
3722 if (!ins_bit26 && !ins_bit27 && ins_bit28)
3723 return aarch64_record_data_proc_imm (aarch64_insn_r);
3725 /* Branch, exception generation and system instructions. */
3726 if (ins_bit26 && !ins_bit27 && ins_bit28)
3727 return aarch64_record_branch_except_sys (aarch64_insn_r);
3729 /* Load and store instructions. */
3730 if (!ins_bit25 && ins_bit27)
3731 return aarch64_record_load_store (aarch64_insn_r);
3733 /* Data processing - register instructions. */
3734 if (ins_bit25 && !ins_bit26 && ins_bit27)
3735 return aarch64_record_data_proc_reg (aarch64_insn_r);
3737 /* Data processing - SIMD and floating point instructions. */
3738 if (ins_bit25 && ins_bit26 && ins_bit27)
3739 return aarch64_record_data_proc_simd_fp (aarch64_insn_r);
3741 return AARCH64_RECORD_UNSUPPORTED;
3744 /* Cleans up local record registers and memory allocations. */
3747 deallocate_reg_mem (insn_decode_record *record)
3749 xfree (record->aarch64_regs);
3750 xfree (record->aarch64_mems);
3753 /* Parse the current instruction and record the values of the registers and
3754 memory that will be changed in current instruction to record_arch_list
3755 return -1 if something is wrong. */
3758 aarch64_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
3759 CORE_ADDR insn_addr)
3761 uint32_t rec_no = 0;
3762 uint8_t insn_size = 4;
3764 ULONGEST t_bit = 0, insn_id = 0;
3765 gdb_byte buf[insn_size];
3766 insn_decode_record aarch64_record;
3768 memset (&buf[0], 0, insn_size);
3769 memset (&aarch64_record, 0, sizeof (insn_decode_record));
3770 target_read_memory (insn_addr, &buf[0], insn_size);
3771 aarch64_record.aarch64_insn
3772 = (uint32_t) extract_unsigned_integer (&buf[0],
3774 gdbarch_byte_order (gdbarch));
3775 aarch64_record.regcache = regcache;
3776 aarch64_record.this_addr = insn_addr;
3777 aarch64_record.gdbarch = gdbarch;
3779 ret = aarch64_record_decode_insn_handler (&aarch64_record);
3780 if (ret == AARCH64_RECORD_UNSUPPORTED)
3782 printf_unfiltered (_("Process record does not support instruction "
3783 "0x%0x at address %s.\n"),
3784 aarch64_record.aarch64_insn,
3785 paddress (gdbarch, insn_addr));
3791 /* Record registers. */
3792 record_full_arch_list_add_reg (aarch64_record.regcache,
3794 /* Always record register CPSR. */
3795 record_full_arch_list_add_reg (aarch64_record.regcache,
3796 AARCH64_CPSR_REGNUM);
3797 if (aarch64_record.aarch64_regs)
3798 for (rec_no = 0; rec_no < aarch64_record.reg_rec_count; rec_no++)
3799 if (record_full_arch_list_add_reg (aarch64_record.regcache,
3800 aarch64_record.aarch64_regs[rec_no]))
3803 /* Record memories. */
3804 if (aarch64_record.aarch64_mems)
3805 for (rec_no = 0; rec_no < aarch64_record.mem_rec_count; rec_no++)
3806 if (record_full_arch_list_add_mem
3807 ((CORE_ADDR)aarch64_record.aarch64_mems[rec_no].addr,
3808 aarch64_record.aarch64_mems[rec_no].len))
3811 if (record_full_arch_list_add_end ())
3815 deallocate_reg_mem (&aarch64_record);