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