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