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