gdb/gdbserver/linux-aarch64-low.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* GNU/Linux/AArch64 specific low level interface, for the remote server for
  2.    GDB.

  3.    Copyright (C) 2009-2015 Free Software Foundation, Inc.
  4.    Contributed by ARM Ltd.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "server.h"
  17. #include "linux-low.h"
  18. #include "elf/common.h"

  19. #include <signal.h>
  20. #include <sys/user.h>
  21. #include <sys/ptrace.h>
  22. #include <asm/ptrace.h>
  23. #include <sys/uio.h>

  24. #include "gdb_proc_service.h"

  25. /* Defined in auto-generated files.  */
  26. void init_registers_aarch64 (void);
  27. extern const struct target_desc *tdesc_aarch64;

  28. #ifdef HAVE_SYS_REG_H
  29. #include <sys/reg.h>
  30. #endif

  31. #define AARCH64_X_REGS_NUM 31
  32. #define AARCH64_V_REGS_NUM 32
  33. #define AARCH64_X0_REGNO    0
  34. #define AARCH64_SP_REGNO   31
  35. #define AARCH64_PC_REGNO   32
  36. #define AARCH64_CPSR_REGNO 33
  37. #define AARCH64_V0_REGNO   34
  38. #define AARCH64_FPSR_REGNO (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM)
  39. #define AARCH64_FPCR_REGNO (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM + 1)

  40. #define AARCH64_NUM_REGS (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM + 2)

  41. static int
  42. aarch64_regmap [] =
  43. {
  44.   /* These offsets correspond to GET/SETREGSET */
  45.   /* x0...  */
  46.    0*81*82*83*84*85*86*87*8,
  47.    8*89*8, 10*8, 11*8, 12*8, 13*8, 14*8, 15*8,
  48.   16*8, 17*8, 18*8, 19*8, 20*8, 21*8, 22*8, 23*8,
  49.   24*8, 25*8, 26*8, 27*8, 28*8,
  50.   29*8,
  51.   30*8,                                /* x30 lr */
  52.   31*8,                                /* x31 sp */
  53.   32*8,                                /*     pc */
  54.   33*8,                                /*     cpsr    4 bytes!*/

  55.   /* FP register offsets correspond to GET/SETFPREGSET */
  56.    0*161*162*163*164*165*166*167*16,
  57.    8*169*16, 10*16, 11*16, 12*16, 13*16, 14*16, 15*16,
  58.   16*16, 17*16, 18*16, 19*16, 20*16, 21*16, 22*16, 23*16,
  59.   24*16, 25*16, 26*16, 27*16, 28*16, 29*16, 30*16, 31*16
  60. };

  61. /* Here starts the macro definitions, data structures, and code for
  62.    the hardware breakpoint and hardware watchpoint support.  The
  63.    following is the abbreviations that are used frequently in the code
  64.    and comment:

  65.    hw - hardware
  66.    bp - breakpoint
  67.    wp - watchpoint  */

  68. /* Maximum number of hardware breakpoint and watchpoint registers.
  69.    Neither of these values may exceed the width of dr_changed_t
  70.    measured in bits.  */

  71. #define AARCH64_HBP_MAX_NUM 16
  72. #define AARCH64_HWP_MAX_NUM 16

  73. /* Alignment requirement in bytes of hardware breakpoint and
  74.    watchpoint address.  This is the requirement for the addresses that
  75.    can be written to the hardware breakpoint/watchpoint value
  76.    registers.  The kernel currently does not do any alignment on
  77.    addresses when receiving a writing request (via ptrace call) to
  78.    these debug registers, and it will reject any address that is
  79.    unaligned.
  80.    Some limited support has been provided in this gdbserver port for
  81.    unaligned watchpoints, so that from a gdb user point of view, an
  82.    unaligned watchpoint can still be set.  This is achieved by
  83.    minimally enlarging the watched area to meet the alignment
  84.    requirement, and if necessary, splitting the watchpoint over
  85.    several hardware watchpoint registers.  */

  86. #define AARCH64_HBP_ALIGNMENT 4
  87. #define AARCH64_HWP_ALIGNMENT 8

  88. /* The maximum length of a memory region that can be watched by one
  89.    hardware watchpoint register.  */

  90. #define AARCH64_HWP_MAX_LEN_PER_REG 8

  91. /* Each bit of a variable of this type is used to indicate whether a
  92.    hardware breakpoint or watchpoint setting has been changed since
  93.    the last updating.  Bit N corresponds to the Nth hardware
  94.    breakpoint or watchpoint setting which is managed in
  95.    aarch64_debug_reg_state.  Where N is valid between 0 and the total
  96.    number of the hardware breakpoint or watchpoint debug registers
  97.    minus 1.  When the bit N is 1, it indicates the corresponding
  98.    breakpoint or watchpoint setting is changed, and thus the
  99.    corresponding hardware debug register needs to be updated via the
  100.    ptrace interface.

  101.    In the per-thread arch-specific data area, we define two such
  102.    variables for per-thread hardware breakpoint and watchpoint
  103.    settings respectively.

  104.    This type is part of the mechanism which helps reduce the number of
  105.    ptrace calls to the kernel, i.e. avoid asking the kernel to write
  106.    to the debug registers with unchanged values.  */

  107. typedef unsigned long long dr_changed_t;

  108. /* Set each of the lower M bits of X to 1; assert X is wide enough.  */

  109. #define DR_MARK_ALL_CHANGED(x, m)                                        \
  110.   do                                                                        \
  111.     {                                                                        \
  112.       gdb_assert (sizeof ((x)) * 8 >= (m));                                \
  113.       (x) = (((dr_changed_t)1 << (m)) - 1);                                \
  114.     } while (0)

  115. #define DR_MARK_N_CHANGED(x, n)                                                \
  116.   do                                                                        \
  117.     {                                                                        \
  118.       (x) |= ((dr_changed_t)1 << (n));                                        \
  119.     } while (0)

  120. #define DR_CLEAR_CHANGED(x)                                                \
  121.   do                                                                        \
  122.     {                                                                        \
  123.       (x) = 0;                                                                \
  124.     } while (0)

  125. #define DR_HAS_CHANGED(x) ((x) != 0)
  126. #define DR_N_HAS_CHANGED(x, n) ((x) & ((dr_changed_t)1 << (n)))

  127. /* Structure for managing the hardware breakpoint/watchpoint resources.
  128.    DR_ADDR_* stores the address, DR_CTRL_* stores the control register
  129.    content, and DR_REF_COUNT_* counts the numbers of references to the
  130.    corresponding bp/wp, by which way the limited hardware resources
  131.    are not wasted on duplicated bp/wp settings (though so far gdb has
  132.    done a good job by not sending duplicated bp/wp requests).  */

  133. struct aarch64_debug_reg_state
  134. {
  135.   /* hardware breakpoint */
  136.   CORE_ADDR dr_addr_bp[AARCH64_HBP_MAX_NUM];
  137.   unsigned int dr_ctrl_bp[AARCH64_HBP_MAX_NUM];
  138.   unsigned int dr_ref_count_bp[AARCH64_HBP_MAX_NUM];

  139.   /* hardware watchpoint */
  140.   CORE_ADDR dr_addr_wp[AARCH64_HWP_MAX_NUM];
  141.   unsigned int dr_ctrl_wp[AARCH64_HWP_MAX_NUM];
  142.   unsigned int dr_ref_count_wp[AARCH64_HWP_MAX_NUM];
  143. };

  144. /* Per-process arch-specific data we want to keep.  */

  145. struct arch_process_info
  146. {
  147.   /* Hardware breakpoint/watchpoint data.
  148.      The reason for them to be per-process rather than per-thread is
  149.      due to the lack of information in the gdbserver environment;
  150.      gdbserver is not told that whether a requested hardware
  151.      breakpoint/watchpoint is thread specific or not, so it has to set
  152.      each hw bp/wp for every thread in the current process.  The
  153.      higher level bp/wp management in gdb will resume a thread if a hw
  154.      bp/wp trap is not expected for it.  Since the hw bp/wp setting is
  155.      same for each thread, it is reasonable for the data to live here.
  156.      */
  157.   struct aarch64_debug_reg_state debug_reg_state;
  158. };

  159. /* Per-thread arch-specific data we want to keep.  */

  160. struct arch_lwp_info
  161. {
  162.   /* When bit N is 1, it indicates the Nth hardware breakpoint or
  163.      watchpoint register pair needs to be updated when the thread is
  164.      resumed; see aarch64_linux_prepare_to_resume.  */
  165.   dr_changed_t dr_changed_bp;
  166.   dr_changed_t dr_changed_wp;
  167. };

  168. /* Number of hardware breakpoints/watchpoints the target supports.
  169.    They are initialized with values obtained via the ptrace calls
  170.    with NT_ARM_HW_BREAK and NT_ARM_HW_WATCH respectively.  */

  171. static int aarch64_num_bp_regs;
  172. static int aarch64_num_wp_regs;

  173. static int
  174. aarch64_cannot_store_register (int regno)
  175. {
  176.   return regno >= AARCH64_NUM_REGS;
  177. }

  178. static int
  179. aarch64_cannot_fetch_register (int regno)
  180. {
  181.   return regno >= AARCH64_NUM_REGS;
  182. }

  183. static void
  184. aarch64_fill_gregset (struct regcache *regcache, void *buf)
  185. {
  186.   struct user_pt_regs *regset = buf;
  187.   int i;

  188.   for (i = 0; i < AARCH64_X_REGS_NUM; i++)
  189.     collect_register (regcache, AARCH64_X0_REGNO + i, &regset->regs[i]);
  190.   collect_register (regcache, AARCH64_SP_REGNO, &regset->sp);
  191.   collect_register (regcache, AARCH64_PC_REGNO, &regset->pc);
  192.   collect_register (regcache, AARCH64_CPSR_REGNO, &regset->pstate);
  193. }

  194. static void
  195. aarch64_store_gregset (struct regcache *regcache, const void *buf)
  196. {
  197.   const struct user_pt_regs *regset = buf;
  198.   int i;

  199.   for (i = 0; i < AARCH64_X_REGS_NUM; i++)
  200.     supply_register (regcache, AARCH64_X0_REGNO + i, &regset->regs[i]);
  201.   supply_register (regcache, AARCH64_SP_REGNO, &regset->sp);
  202.   supply_register (regcache, AARCH64_PC_REGNO, &regset->pc);
  203.   supply_register (regcache, AARCH64_CPSR_REGNO, &regset->pstate);
  204. }

  205. static void
  206. aarch64_fill_fpregset (struct regcache *regcache, void *buf)
  207. {
  208.   struct user_fpsimd_state *regset = buf;
  209.   int i;

  210.   for (i = 0; i < AARCH64_V_REGS_NUM; i++)
  211.     collect_register (regcache, AARCH64_V0_REGNO + i, &regset->vregs[i]);
  212.   collect_register (regcache, AARCH64_FPSR_REGNO, &regset->fpsr);
  213.   collect_register (regcache, AARCH64_FPCR_REGNO, &regset->fpcr);
  214. }

  215. static void
  216. aarch64_store_fpregset (struct regcache *regcache, const void *buf)
  217. {
  218.   const struct user_fpsimd_state *regset = buf;
  219.   int i;

  220.   for (i = 0; i < AARCH64_V_REGS_NUM; i++)
  221.     supply_register (regcache, AARCH64_V0_REGNO + i, &regset->vregs[i]);
  222.   supply_register (regcache, AARCH64_FPSR_REGNO, &regset->fpsr);
  223.   supply_register (regcache, AARCH64_FPCR_REGNO, &regset->fpcr);
  224. }

  225. /* Enable miscellaneous debugging output.  The name is historical - it
  226.    was originally used to debug LinuxThreads support.  */
  227. extern int debug_threads;

  228. static CORE_ADDR
  229. aarch64_get_pc (struct regcache *regcache)
  230. {
  231.   unsigned long pc;

  232.   collect_register_by_name (regcache, "pc", &pc);
  233.   if (debug_threads)
  234.     debug_printf ("stop pc is %08lx\n", pc);
  235.   return pc;
  236. }

  237. static void
  238. aarch64_set_pc (struct regcache *regcache, CORE_ADDR pc)
  239. {
  240.   unsigned long newpc = pc;
  241.   supply_register_by_name (regcache, "pc", &newpc);
  242. }

  243. /* Correct in either endianness.  */

  244. #define aarch64_breakpoint_len 4

  245. static const unsigned long aarch64_breakpoint = 0x00800011;

  246. static int
  247. aarch64_breakpoint_at (CORE_ADDR where)
  248. {
  249.   unsigned long insn;

  250.   (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
  251.   if (insn == aarch64_breakpoint)
  252.     return 1;

  253.   return 0;
  254. }

  255. /* Print the values of the cached breakpoint/watchpoint registers.
  256.    This is enabled via the "set debug-hw-points" monitor command.  */

  257. static void
  258. aarch64_show_debug_reg_state (struct aarch64_debug_reg_state *state,
  259.                               const char *func, CORE_ADDR addr,
  260.                               int len, enum target_hw_bp_type type)
  261. {
  262.   int i;

  263.   fprintf (stderr, "%s", func);
  264.   if (addr || len)
  265.     fprintf (stderr, " (addr=0x%08lx, len=%d, type=%s)",
  266.              (unsigned long) addr, len,
  267.              type == hw_write ? "hw-write-watchpoint"
  268.              : (type == hw_read ? "hw-read-watchpoint"
  269.                 : (type == hw_access ? "hw-access-watchpoint"
  270.                    : (type == hw_execute ? "hw-breakpoint"
  271.                       : "??unknown??"))));
  272.   fprintf (stderr, ":\n");

  273.   fprintf (stderr, "\tBREAKPOINTs:\n");
  274.   for (i = 0; i < aarch64_num_bp_regs; i++)
  275.     fprintf (stderr, "\tBP%d: addr=0x%s, ctrl=0x%08x, ref.count=%d\n",
  276.              i, paddress (state->dr_addr_bp[i]),
  277.              state->dr_ctrl_bp[i], state->dr_ref_count_bp[i]);

  278.   fprintf (stderr, "\tWATCHPOINTs:\n");
  279.   for (i = 0; i < aarch64_num_wp_regs; i++)
  280.     fprintf (stderr, "\tWP%d: addr=0x%s, ctrl=0x%08x, ref.count=%d\n",
  281.              i, paddress (state->dr_addr_wp[i]),
  282.              state->dr_ctrl_wp[i], state->dr_ref_count_wp[i]);
  283. }

  284. static void
  285. aarch64_init_debug_reg_state (struct aarch64_debug_reg_state *state)
  286. {
  287.   int i;

  288.   for (i = 0; i < AARCH64_HBP_MAX_NUM; ++i)
  289.     {
  290.       state->dr_addr_bp[i] = 0;
  291.       state->dr_ctrl_bp[i] = 0;
  292.       state->dr_ref_count_bp[i] = 0;
  293.     }

  294.   for (i = 0; i < AARCH64_HWP_MAX_NUM; ++i)
  295.     {
  296.       state->dr_addr_wp[i] = 0;
  297.       state->dr_ctrl_wp[i] = 0;
  298.       state->dr_ref_count_wp[i] = 0;
  299.     }
  300. }

  301. /* ptrace expects control registers to be formatted as follows:

  302.    31                             13          5      3      1     0
  303.    +--------------------------------+----------+------+------+----+
  304.    |         RESERVED (SBZ)         |  LENGTH  | TYPE | PRIV | EN |
  305.    +--------------------------------+----------+------+------+----+

  306.    The TYPE field is ignored for breakpoints.  */

  307. #define DR_CONTROL_ENABLED(ctrl)        (((ctrl) & 0x1) == 1)
  308. #define DR_CONTROL_LENGTH(ctrl)                (((ctrl) >> 5) & 0xff)

  309. /* Utility function that returns the length in bytes of a watchpoint
  310.    according to the content of a hardware debug control register CTRL.
  311.    Note that the kernel currently only supports the following Byte
  312.    Address Select (BAS) values: 0x1, 0x3, 0xf and 0xff, which means
  313.    that for a hardware watchpoint, its valid length can only be 1
  314.    byte, 2 bytes, 4 bytes or 8 bytes.  */

  315. static inline unsigned int
  316. aarch64_watchpoint_length (unsigned int ctrl)
  317. {
  318.   switch (DR_CONTROL_LENGTH (ctrl))
  319.     {
  320.     case 0x01:
  321.       return 1;
  322.     case 0x03:
  323.       return 2;
  324.     case 0x0f:
  325.       return 4;
  326.     case 0xff:
  327.       return 8;
  328.     default:
  329.       return 0;
  330.     }
  331. }

  332. /* Given the hardware breakpoint or watchpoint type TYPE and its
  333.    length LEN, return the expected encoding for a hardware
  334.    breakpoint/watchpoint control register.  */

  335. static unsigned int
  336. aarch64_point_encode_ctrl_reg (enum target_hw_bp_type type, int len)
  337. {
  338.   unsigned int ctrl, ttype;

  339.   /* type */
  340.   switch (type)
  341.     {
  342.     case hw_write:
  343.       ttype = 2;
  344.       break;
  345.     case hw_read:
  346.       ttype = 1;
  347.       break;
  348.     case hw_access:
  349.       ttype = 3;
  350.       break;
  351.     case hw_execute:
  352.       ttype = 0;
  353.       break;
  354.     default:
  355.       perror_with_name (_("Unrecognized breakpoint/watchpoint type"));
  356.     }

  357.   /* type */
  358.   ctrl = ttype << 3;
  359.   /* length bitmask */
  360.   ctrl |= ((1 << len) - 1) << 5;
  361.   /* enabled at el0 */
  362.   ctrl |= (2 << 1) | 1;

  363.   return ctrl;
  364. }

  365. /* Addresses to be written to the hardware breakpoint and watchpoint
  366.    value registers need to be aligned; the alignment is 4-byte and
  367.    8-type respectively.  Linux kernel rejects any non-aligned address
  368.    it receives from the related ptrace call.  Furthermore, the kernel
  369.    currently only supports the following Byte Address Select (BAS)
  370.    values: 0x1, 0x3, 0xf and 0xff, which means that for a hardware
  371.    watchpoint to be accepted by the kernel (via ptrace call), its
  372.    valid length can only be 1 byte, 2 bytes, 4 bytes or 8 bytes.
  373.    Despite these limitations, the unaligned watchpoint is supported in
  374.    this gdbserver port.

  375.    Return 0 for any non-compliant ADDR and/or LEN; return 1 otherwise.  */

  376. static int
  377. aarch64_point_is_aligned (int is_watchpoint, CORE_ADDR addr, int len)
  378. {
  379.   unsigned int alignment = is_watchpoint ? AARCH64_HWP_ALIGNMENT
  380.     : AARCH64_HBP_ALIGNMENT;

  381.   if (addr & (alignment - 1))
  382.     return 0;

  383.   if (len != 8 && len != 4 && len != 2 && len != 1)
  384.     return 0;

  385.   return 1;
  386. }

  387. /* Given the (potentially unaligned) watchpoint address in ADDR and
  388.    length in LEN, return the aligned address and aligned length in
  389.    *ALIGNED_ADDR_P and *ALIGNED_LEN_P, respectively.  The returned
  390.    aligned address and length will be valid to be written to the
  391.    hardware watchpoint value and control registers.  See the comment
  392.    above aarch64_point_is_aligned for the information about the
  393.    alignment requirement.  The given watchpoint may get truncated if
  394.    more than one hardware register is needed to cover the watched
  395.    region.  *NEXT_ADDR_P and *NEXT_LEN_P, if non-NULL, will return the
  396.    address and length of the remaining part of the watchpoint (which
  397.    can be processed by calling this routine again to generate another
  398.    aligned address and length pair.

  399.    Essentially, unaligned watchpoint is achieved by minimally
  400.    enlarging the watched area to meet the alignment requirement, and
  401.    if necessary, splitting the watchpoint over several hardware
  402.    watchpoint registers.  The trade-off is that there will be
  403.    false-positive hits for the read-type or the access-type hardware
  404.    watchpoints; for the write type, which is more commonly used, there
  405.    will be no such issues, as the higher-level breakpoint management
  406.    in gdb always examines the exact watched region for any content
  407.    change, and transparently resumes a thread from a watchpoint trap
  408.    if there is no change to the watched region.

  409.    Another limitation is that because the watched region is enlarged,
  410.    the watchpoint fault address returned by
  411.    aarch64_stopped_data_address may be outside of the original watched
  412.    region, especially when the triggering instruction is accessing a
  413.    larger region.  When the fault address is not within any known
  414.    range, watchpoints_triggered in gdb will get confused, as the
  415.    higher-level watchpoint management is only aware of original
  416.    watched regions, and will think that some unknown watchpoint has
  417.    been triggered.  In such a case, gdb may stop without displaying
  418.    any detailed information.

  419.    Once the kernel provides the full support for Byte Address Select
  420.    (BAS) in the hardware watchpoint control register, these
  421.    limitations can be largely relaxed with some further work.  */

  422. static void
  423. aarch64_align_watchpoint (CORE_ADDR addr, int len, CORE_ADDR *aligned_addr_p,
  424.                           int *aligned_len_p, CORE_ADDR *next_addr_p,
  425.                           int *next_len_p)
  426. {
  427.   int aligned_len;
  428.   unsigned int offset;
  429.   CORE_ADDR aligned_addr;
  430.   const unsigned int alignment = AARCH64_HWP_ALIGNMENT;
  431.   const unsigned int max_wp_len = AARCH64_HWP_MAX_LEN_PER_REG;

  432.   /* As assumed by the algorithm.  */
  433.   gdb_assert (alignment == max_wp_len);

  434.   if (len <= 0)
  435.     return;

  436.   /* Address to be put into the hardware watchpoint value register
  437.      must be aligned.  */
  438.   offset = addr & (alignment - 1);
  439.   aligned_addr = addr - offset;

  440.   gdb_assert (offset >= 0 && offset < alignment);
  441.   gdb_assert (aligned_addr >= 0 && aligned_addr <= addr);
  442.   gdb_assert ((offset + len) > 0);

  443.   if (offset + len >= max_wp_len)
  444.     {
  445.       /* Need more than one watchpoint registers; truncate it at the
  446.          alignment boundary.  */
  447.       aligned_len = max_wp_len;
  448.       len -= (max_wp_len - offset);
  449.       addr += (max_wp_len - offset);
  450.       gdb_assert ((addr & (alignment - 1)) == 0);
  451.     }
  452.   else
  453.     {
  454.       /* Find the smallest valid length that is large enough to
  455.          accommodate this watchpoint.  */
  456.       static const unsigned char
  457.         aligned_len_array[AARCH64_HWP_MAX_LEN_PER_REG] =
  458.         { 1, 2, 4, 4, 8, 8, 8, 8 };

  459.       aligned_len = aligned_len_array[offset + len - 1];
  460.       addr += len;
  461.       len = 0;
  462.     }

  463.   if (aligned_addr_p != NULL)
  464.     *aligned_addr_p = aligned_addr;
  465.   if (aligned_len_p != NULL)
  466.     *aligned_len_p = aligned_len;
  467.   if (next_addr_p != NULL)
  468.     *next_addr_p = addr;
  469.   if (next_len_p != NULL)
  470.     *next_len_p = len;
  471. }

  472. /* Call ptrace to set the thread TID's hardware breakpoint/watchpoint
  473.    registers with data from *STATE.  */

  474. static void
  475. aarch64_linux_set_debug_regs (const struct aarch64_debug_reg_state *state,
  476.                               int tid, int watchpoint)
  477. {
  478.   int i, count;
  479.   struct iovec iov;
  480.   struct user_hwdebug_state regs;
  481.   const CORE_ADDR *addr;
  482.   const unsigned int *ctrl;

  483.   memset (&regs, 0, sizeof (regs));
  484.   iov.iov_base = &regs;
  485.   count = watchpoint ? aarch64_num_wp_regs : aarch64_num_bp_regs;
  486.   addr = watchpoint ? state->dr_addr_wp : state->dr_addr_bp;
  487.   ctrl = watchpoint ? state->dr_ctrl_wp : state->dr_ctrl_bp;
  488.   if (count == 0)
  489.     return;
  490.   iov.iov_len = (offsetof (struct user_hwdebug_state, dbg_regs[count - 1])
  491.                  + sizeof (regs.dbg_regs [count - 1]));

  492.   for (i = 0; i < count; i++)
  493.     {
  494.       regs.dbg_regs[i].addr = addr[i];
  495.       regs.dbg_regs[i].ctrl = ctrl[i];
  496.     }

  497.   if (ptrace (PTRACE_SETREGSET, tid,
  498.               watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK,
  499.               (void *) &iov))
  500.     error (_("Unexpected error setting hardware debug registers"));
  501. }

  502. struct aarch64_dr_update_callback_param
  503. {
  504.   int pid;
  505.   int is_watchpoint;
  506.   unsigned int idx;
  507. };

  508. /* Callback function which records the information about the change of
  509.    one hardware breakpoint/watchpoint setting for the thread ENTRY.
  510.    The information is passed in via PTR.
  511.    N.B.  The actual updating of hardware debug registers is not
  512.    carried out until the moment the thread is resumed.  */

  513. static int
  514. debug_reg_change_callback (struct inferior_list_entry *entry, void *ptr)
  515. {
  516.   struct thread_info *thread = (struct thread_info *) entry;
  517.   struct lwp_info *lwp = get_thread_lwp (thread);
  518.   struct aarch64_dr_update_callback_param *param_p
  519.     = (struct aarch64_dr_update_callback_param *) ptr;
  520.   int pid = param_p->pid;
  521.   int idx = param_p->idx;
  522.   int is_watchpoint = param_p->is_watchpoint;
  523.   struct arch_lwp_info *info = lwp->arch_private;
  524.   dr_changed_t *dr_changed_ptr;
  525.   dr_changed_t dr_changed;

  526.   if (show_debug_regs)
  527.     {
  528.       fprintf (stderr, "debug_reg_change_callback: \n\tOn entry:\n");
  529.       fprintf (stderr, "\tpid%d, tid: %ld, dr_changed_bp=0x%llx, "
  530.                "dr_changed_wp=0x%llx\n",
  531.                pid, lwpid_of (thread), info->dr_changed_bp,
  532.                info->dr_changed_wp);
  533.     }

  534.   dr_changed_ptr = is_watchpoint ? &info->dr_changed_wp
  535.     : &info->dr_changed_bp;
  536.   dr_changed = *dr_changed_ptr;

  537.   /* Only update the threads of this process.  */
  538.   if (pid_of (thread) == pid)
  539.     {
  540.       gdb_assert (idx >= 0
  541.                   && (idx <= (is_watchpoint ? aarch64_num_wp_regs
  542.                               : aarch64_num_bp_regs)));

  543.       /* The following assertion is not right, as there can be changes
  544.          that have not been made to the hardware debug registers
  545.          before new changes overwrite the old ones.  This can happen,
  546.          for instance, when the breakpoint/watchpoint hit one of the
  547.          threads and the user enters continue; then what happens is:
  548.          1) all breakpoints/watchpoints are removed for all threads;
  549.          2) a single step is carried out for the thread that was hit;
  550.          3) all of the points are inserted again for all threads;
  551.          4) all threads are resumed.
  552.          The 2nd step will only affect the one thread in which the
  553.          bp/wp was hit, which means only that one thread is resumed;
  554.          remember that the actual updating only happen in
  555.          aarch64_linux_prepare_to_resume, so other threads remain
  556.          stopped during the removal and insertion of bp/wp.  Therefore
  557.          for those threads, the change of insertion of the bp/wp
  558.          overwrites that of the earlier removals.  (The situation may
  559.          be different when bp/wp is steppable, or in the non-stop
  560.          mode.)  */
  561.       /* gdb_assert (DR_N_HAS_CHANGED (dr_changed, idx) == 0);  */

  562.       /* The actual update is done later just before resuming the lwp,
  563.          we just mark that one register pair needs updating.  */
  564.       DR_MARK_N_CHANGED (dr_changed, idx);
  565.       *dr_changed_ptr = dr_changed;

  566.       /* If the lwp isn't stopped, force it to momentarily pause, so
  567.          we can update its debug registers.  */
  568.       if (!lwp->stopped)
  569.         linux_stop_lwp (lwp);
  570.     }

  571.   if (show_debug_regs)
  572.     {
  573.       fprintf (stderr, "\tOn exit:\n\tpid%d, tid: %ld, dr_changed_bp=0x%llx, "
  574.                "dr_changed_wp=0x%llx\n",
  575.                pid, lwpid_of (thread), info->dr_changed_bp,
  576.                info->dr_changed_wp);
  577.     }

  578.   return 0;
  579. }

  580. /* Notify each thread that their IDXth breakpoint/watchpoint register
  581.    pair needs to be updated.  The message will be recorded in each
  582.    thread's arch-specific data area, the actual updating will be done
  583.    when the thread is resumed.  */

  584. void
  585. aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state *state,
  586.                                  int is_watchpoint, unsigned int idx)
  587. {
  588.   struct aarch64_dr_update_callback_param param;

  589.   /* Only update the threads of this process.  */
  590.   param.pid = pid_of (current_thread);

  591.   param.is_watchpoint = is_watchpoint;
  592.   param.idx = idx;

  593.   find_inferior (&all_threads, debug_reg_change_callback, (void *) &param);
  594. }


  595. /* Return the pointer to the debug register state structure in the
  596.    current process' arch-specific data area.  */

  597. static struct aarch64_debug_reg_state *
  598. aarch64_get_debug_reg_state ()
  599. {
  600.   struct process_info *proc;

  601.   proc = current_process ();
  602.   return &proc->private->arch_private->debug_reg_state;
  603. }

  604. /* Record the insertion of one breakpoint/watchpoint, as represented
  605.    by ADDR and CTRL, in the process' arch-specific data area *STATE.  */

  606. static int
  607. aarch64_dr_state_insert_one_point (struct aarch64_debug_reg_state *state,
  608.                                    enum target_hw_bp_type type,
  609.                                    CORE_ADDR addr, int len)
  610. {
  611.   int i, idx, num_regs, is_watchpoint;
  612.   unsigned int ctrl, *dr_ctrl_p, *dr_ref_count;
  613.   CORE_ADDR *dr_addr_p;

  614.   /* Set up state pointers.  */
  615.   is_watchpoint = (type != hw_execute);
  616.   gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len));
  617.   if (is_watchpoint)
  618.     {
  619.       num_regs = aarch64_num_wp_regs;
  620.       dr_addr_p = state->dr_addr_wp;
  621.       dr_ctrl_p = state->dr_ctrl_wp;
  622.       dr_ref_count = state->dr_ref_count_wp;
  623.     }
  624.   else
  625.     {
  626.       num_regs = aarch64_num_bp_regs;
  627.       dr_addr_p = state->dr_addr_bp;
  628.       dr_ctrl_p = state->dr_ctrl_bp;
  629.       dr_ref_count = state->dr_ref_count_bp;
  630.     }

  631.   ctrl = aarch64_point_encode_ctrl_reg (type, len);

  632.   /* Find an existing or free register in our cache.  */
  633.   idx = -1;
  634.   for (i = 0; i < num_regs; ++i)
  635.     {
  636.       if ((dr_ctrl_p[i] & 1) == 0)
  637.         {
  638.           gdb_assert (dr_ref_count[i] == 0);
  639.           idx = i;
  640.           /* no break; continue hunting for an exising one.  */
  641.         }
  642.       else if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl)
  643.         {
  644.           gdb_assert (dr_ref_count[i] != 0);
  645.           idx = i;
  646.           break;
  647.         }
  648.     }

  649.   /* No space.  */
  650.   if (idx == -1)
  651.     return -1;

  652.   /* Update our cache.  */
  653.   if ((dr_ctrl_p[idx] & 1) == 0)
  654.     {
  655.       /* new entry */
  656.       dr_addr_p[idx] = addr;
  657.       dr_ctrl_p[idx] = ctrl;
  658.       dr_ref_count[idx] = 1;
  659.       /* Notify the change.  */
  660.       aarch64_notify_debug_reg_change (state, is_watchpoint, idx);
  661.     }
  662.   else
  663.     {
  664.       /* existing entry */
  665.       dr_ref_count[idx]++;
  666.     }

  667.   return 0;
  668. }

  669. /* Record the removal of one breakpoint/watchpoint, as represented by
  670.    ADDR and CTRL, in the process' arch-specific data area *STATE.  */

  671. static int
  672. aarch64_dr_state_remove_one_point (struct aarch64_debug_reg_state *state,
  673.                                    enum target_hw_bp_type type,
  674.                                    CORE_ADDR addr, int len)
  675. {
  676.   int i, num_regs, is_watchpoint;
  677.   unsigned int ctrl, *dr_ctrl_p, *dr_ref_count;
  678.   CORE_ADDR *dr_addr_p;

  679.   /* Set up state pointers.  */
  680.   is_watchpoint = (type != hw_execute);
  681.   gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len));
  682.   if (is_watchpoint)
  683.     {
  684.       num_regs = aarch64_num_wp_regs;
  685.       dr_addr_p = state->dr_addr_wp;
  686.       dr_ctrl_p = state->dr_ctrl_wp;
  687.       dr_ref_count = state->dr_ref_count_wp;
  688.     }
  689.   else
  690.     {
  691.       num_regs = aarch64_num_bp_regs;
  692.       dr_addr_p = state->dr_addr_bp;
  693.       dr_ctrl_p = state->dr_ctrl_bp;
  694.       dr_ref_count = state->dr_ref_count_bp;
  695.     }

  696.   ctrl = aarch64_point_encode_ctrl_reg (type, len);

  697.   /* Find the entry that matches the ADDR and CTRL.  */
  698.   for (i = 0; i < num_regs; ++i)
  699.     if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl)
  700.       {
  701.         gdb_assert (dr_ref_count[i] != 0);
  702.         break;
  703.       }

  704.   /* Not found.  */
  705.   if (i == num_regs)
  706.     return -1;

  707.   /* Clear our cache.  */
  708.   if (--dr_ref_count[i] == 0)
  709.     {
  710.       /* Clear the enable bit.  */
  711.       ctrl &= ~1;
  712.       dr_addr_p[i] = 0;
  713.       dr_ctrl_p[i] = ctrl;
  714.       /* Notify the change.  */
  715.       aarch64_notify_debug_reg_change (state, is_watchpoint, i);
  716.     }

  717.   return 0;
  718. }

  719. static int
  720. aarch64_handle_breakpoint (enum target_hw_bp_type type, CORE_ADDR addr,
  721.                            int len, int is_insert)
  722. {
  723.   struct aarch64_debug_reg_state *state;

  724.   /* The hardware breakpoint on AArch64 should always be 4-byte
  725.      aligned.  */
  726.   if (!aarch64_point_is_aligned (0 /* is_watchpoint */ , addr, len))
  727.     return -1;

  728.   state = aarch64_get_debug_reg_state ();

  729.   if (is_insert)
  730.     return aarch64_dr_state_insert_one_point (state, type, addr, len);
  731.   else
  732.     return aarch64_dr_state_remove_one_point (state, type, addr, len);
  733. }

  734. /* This is essentially the same as aarch64_handle_breakpoint, apart
  735.    from that it is an aligned watchpoint to be handled.  */

  736. static int
  737. aarch64_handle_aligned_watchpoint (enum target_hw_bp_type type,
  738.                                    CORE_ADDR addr, int len, int is_insert)
  739. {
  740.   struct aarch64_debug_reg_state *state;

  741.   state = aarch64_get_debug_reg_state ();

  742.   if (is_insert)
  743.     return aarch64_dr_state_insert_one_point (state, type, addr, len);
  744.   else
  745.     return aarch64_dr_state_remove_one_point (state, type, addr, len);
  746. }

  747. /* Insert/remove unaligned watchpoint by calling
  748.    aarch64_align_watchpoint repeatedly until the whole watched region,
  749.    as represented by ADDR and LEN, has been properly aligned and ready
  750.    to be written to one or more hardware watchpoint registers.
  751.    IS_INSERT indicates whether this is an insertion or a deletion.
  752.    Return 0 if succeed.  */

  753. static int
  754. aarch64_handle_unaligned_watchpoint (enum target_hw_bp_type type,
  755.                                      CORE_ADDR addr, int len, int is_insert)
  756. {
  757.   struct aarch64_debug_reg_state *state
  758.     = aarch64_get_debug_reg_state ();

  759.   while (len > 0)
  760.     {
  761.       CORE_ADDR aligned_addr;
  762.       int aligned_len, ret;

  763.       aarch64_align_watchpoint (addr, len, &aligned_addr, &aligned_len,
  764.                                 &addr, &len);

  765.       if (is_insert)
  766.         ret = aarch64_dr_state_insert_one_point (state, type, aligned_addr,
  767.                                                  aligned_len);
  768.       else
  769.         ret = aarch64_dr_state_remove_one_point (state, type, aligned_addr,
  770.                                                  aligned_len);

  771.       if (show_debug_regs)
  772.         fprintf (stderr,
  773. "handle_unaligned_watchpoint: is_insert: %d\n"
  774. "                             aligned_addr: 0x%s, aligned_len: %d\n"
  775. "                                next_addr: 0x%s,    next_len: %d\n",
  776.                  is_insert, paddress (aligned_addr), aligned_len,
  777.                  paddress (addr), len);

  778.       if (ret != 0)
  779.         return ret;
  780.     }

  781.   return 0;
  782. }

  783. static int
  784. aarch64_handle_watchpoint (enum target_hw_bp_type type, CORE_ADDR addr,
  785.                            int len, int is_insert)
  786. {
  787.   if (aarch64_point_is_aligned (1 /* is_watchpoint */ , addr, len))
  788.     return aarch64_handle_aligned_watchpoint (type, addr, len, is_insert);
  789.   else
  790.     return aarch64_handle_unaligned_watchpoint (type, addr, len, is_insert);
  791. }

  792. static int
  793. aarch64_supports_z_point_type (char z_type)
  794. {
  795.   switch (z_type)
  796.     {
  797.     case Z_PACKET_HW_BP:
  798.     case Z_PACKET_WRITE_WP:
  799.     case Z_PACKET_READ_WP:
  800.     case Z_PACKET_ACCESS_WP:
  801.       return 1;
  802.     default:
  803.       /* Leave the handling of sw breakpoints with the gdb client.  */
  804.       return 0;
  805.     }
  806. }

  807. /* Insert a hardware breakpoint/watchpoint.
  808.    It actually only records the info of the to-be-inserted bp/wp;
  809.    the actual insertion will happen when threads are resumed.

  810.    Return 0 if succeed;
  811.    Return 1 if TYPE is unsupported type;
  812.    Return -1 if an error occurs.  */

  813. static int
  814. aarch64_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
  815.                       int len, struct raw_breakpoint *bp)
  816. {
  817.   int ret;
  818.   enum target_hw_bp_type targ_type;

  819.   if (show_debug_regs)
  820.     fprintf (stderr, "insert_point on entry (addr=0x%08lx, len=%d)\n",
  821.              (unsigned long) addr, len);

  822.   /* Determine the type from the raw breakpoint type.  */
  823.   targ_type = raw_bkpt_type_to_target_hw_bp_type (type);

  824.   if (targ_type != hw_execute)
  825.     ret =
  826.       aarch64_handle_watchpoint (targ_type, addr, len, 1 /* is_insert */);
  827.   else
  828.     ret =
  829.       aarch64_handle_breakpoint (targ_type, addr, len, 1 /* is_insert */);

  830.   if (show_debug_regs > 1)
  831.     aarch64_show_debug_reg_state (aarch64_get_debug_reg_state (),
  832.                                   "insert_point", addr, len, targ_type);

  833.   return ret;
  834. }

  835. /* Remove a hardware breakpoint/watchpoint.
  836.    It actually only records the info of the to-be-removed bp/wp,
  837.    the actual removal will be done when threads are resumed.

  838.    Return 0 if succeed;
  839.    Return 1 if TYPE is an unsupported type;
  840.    Return -1 if an error occurs.  */

  841. static int
  842. aarch64_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
  843.                       int len, struct raw_breakpoint *bp)
  844. {
  845.   int ret;
  846.   enum target_hw_bp_type targ_type;

  847.   if (show_debug_regs)
  848.     fprintf (stderr, "remove_point on entry (addr=0x%08lx, len=%d)\n",
  849.              (unsigned long) addr, len);

  850.   /* Determine the type from the raw breakpoint type.  */
  851.   targ_type = raw_bkpt_type_to_target_hw_bp_type (type);

  852.   /* Set up state pointers.  */
  853.   if (targ_type != hw_execute)
  854.     ret =
  855.       aarch64_handle_watchpoint (targ_type, addr, len, 0 /* is_insert */);
  856.   else
  857.     ret =
  858.       aarch64_handle_breakpoint (targ_type, addr, len, 0 /* is_insert */);

  859.   if (show_debug_regs > 1)
  860.     aarch64_show_debug_reg_state (aarch64_get_debug_reg_state (),
  861.                                   "remove_point", addr, len, targ_type);

  862.   return ret;
  863. }

  864. /* Returns the address associated with the watchpoint that hit, if
  865.    any; returns 0 otherwise.  */

  866. static CORE_ADDR
  867. aarch64_stopped_data_address (void)
  868. {
  869.   siginfo_t siginfo;
  870.   int pid, i;
  871.   struct aarch64_debug_reg_state *state;

  872.   pid = lwpid_of (current_thread);

  873.   /* Get the siginfo.  */
  874.   if (ptrace (PTRACE_GETSIGINFO, pid, NULL, &siginfo) != 0)
  875.     return (CORE_ADDR) 0;

  876.   /* Need to be a hardware breakpoint/watchpoint trap.  */
  877.   if (siginfo.si_signo != SIGTRAP
  878.       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
  879.     return (CORE_ADDR) 0;

  880.   /* Check if the address matches any watched address.  */
  881.   state = aarch64_get_debug_reg_state ();
  882.   for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
  883.     {
  884.       const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
  885.       const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
  886.       const CORE_ADDR addr_watch = state->dr_addr_wp[i];
  887.       if (state->dr_ref_count_wp[i]
  888.           && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
  889.           && addr_trap >= addr_watch
  890.           && addr_trap < addr_watch + len)
  891.         return addr_trap;
  892.     }

  893.   return (CORE_ADDR) 0;
  894. }

  895. /* Returns 1 if target was stopped due to a watchpoint hit, 0
  896.    otherwise.  */

  897. static int
  898. aarch64_stopped_by_watchpoint (void)
  899. {
  900.   if (aarch64_stopped_data_address () != 0)
  901.     return 1;
  902.   else
  903.     return 0;
  904. }

  905. /* Fetch the thread-local storage pointer for libthread_db.  */

  906. ps_err_e
  907. ps_get_thread_area (const struct ps_prochandle *ph,
  908.                     lwpid_t lwpid, int idx, void **base)
  909. {
  910.   struct iovec iovec;
  911.   uint64_t reg;

  912.   iovec.iov_base = &reg;
  913.   iovec.iov_len = sizeof (reg);

  914.   if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0)
  915.     return PS_ERR;

  916.   /* IDX is the bias from the thread pointer to the beginning of the
  917.      thread descriptor.  It has to be subtracted due to implementation
  918.      quirks in libthread_db.  */
  919.   *base = (void *) (reg - idx);

  920.   return PS_OK;
  921. }

  922. /* Called when a new process is created.  */

  923. static struct arch_process_info *
  924. aarch64_linux_new_process (void)
  925. {
  926.   struct arch_process_info *info = xcalloc (1, sizeof (*info));

  927.   aarch64_init_debug_reg_state (&info->debug_reg_state);

  928.   return info;
  929. }

  930. /* Called when a new thread is detected.  */

  931. static struct arch_lwp_info *
  932. aarch64_linux_new_thread (void)
  933. {
  934.   struct arch_lwp_info *info = xcalloc (1, sizeof (*info));

  935.   /* Mark that all the hardware breakpoint/watchpoint register pairs
  936.      for this thread need to be initialized (with data from
  937.      aarch_process_info.debug_reg_state).  */
  938.   DR_MARK_ALL_CHANGED (info->dr_changed_bp, aarch64_num_bp_regs);
  939.   DR_MARK_ALL_CHANGED (info->dr_changed_wp, aarch64_num_wp_regs);

  940.   return info;
  941. }

  942. /* Called when resuming a thread.
  943.    If the debug regs have changed, update the thread's copies.  */

  944. static void
  945. aarch64_linux_prepare_to_resume (struct lwp_info *lwp)
  946. {
  947.   struct thread_info *thread = get_lwp_thread (lwp);
  948.   ptid_t ptid = ptid_of (thread);
  949.   struct arch_lwp_info *info = lwp->arch_private;

  950.   if (DR_HAS_CHANGED (info->dr_changed_bp)
  951.       || DR_HAS_CHANGED (info->dr_changed_wp))
  952.     {
  953.       int tid = ptid_get_lwp (ptid);
  954.       struct process_info *proc = find_process_pid (ptid_get_pid (ptid));
  955.       struct aarch64_debug_reg_state *state
  956.         = &proc->private->arch_private->debug_reg_state;

  957.       if (show_debug_regs)
  958.         fprintf (stderr, "prepare_to_resume thread %ld\n", lwpid_of (thread));

  959.       /* Watchpoints.  */
  960.       if (DR_HAS_CHANGED (info->dr_changed_wp))
  961.         {
  962.           aarch64_linux_set_debug_regs (state, tid, 1);
  963.           DR_CLEAR_CHANGED (info->dr_changed_wp);
  964.         }

  965.       /* Breakpoints.  */
  966.       if (DR_HAS_CHANGED (info->dr_changed_bp))
  967.         {
  968.           aarch64_linux_set_debug_regs (state, tid, 0);
  969.           DR_CLEAR_CHANGED (info->dr_changed_bp);
  970.         }
  971.     }
  972. }

  973. /* ptrace hardware breakpoint resource info is formatted as follows:

  974.    31             24             16               8              0
  975.    +---------------+--------------+---------------+---------------+
  976.    |   RESERVED    |   RESERVED   |   DEBUG_ARCH  |  NUM_SLOTS    |
  977.    +---------------+--------------+---------------+---------------+  */

  978. #define AARCH64_DEBUG_NUM_SLOTS(x) ((x) & 0xff)
  979. #define AARCH64_DEBUG_ARCH(x) (((x) >> 8) & 0xff)
  980. #define AARCH64_DEBUG_ARCH_V8 0x6

  981. static void
  982. aarch64_arch_setup (void)
  983. {
  984.   int pid;
  985.   struct iovec iov;
  986.   struct user_hwdebug_state dreg_state;

  987.   current_process ()->tdesc = tdesc_aarch64;

  988.   pid = lwpid_of (current_thread);
  989.   iov.iov_base = &dreg_state;
  990.   iov.iov_len = sizeof (dreg_state);

  991.   /* Get hardware watchpoint register info.  */
  992.   if (ptrace (PTRACE_GETREGSET, pid, NT_ARM_HW_WATCH, &iov) == 0
  993.       && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8)
  994.     {
  995.       aarch64_num_wp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info);
  996.       if (aarch64_num_wp_regs > AARCH64_HWP_MAX_NUM)
  997.         {
  998.           warning ("Unexpected number of hardware watchpoint registers reported"
  999.                    " by ptrace, got %d, expected %d.",
  1000.                    aarch64_num_wp_regs, AARCH64_HWP_MAX_NUM);
  1001.           aarch64_num_wp_regs = AARCH64_HWP_MAX_NUM;
  1002.         }
  1003.     }
  1004.   else
  1005.     {
  1006.       warning ("Unable to determine the number of hardware watchpoints"
  1007.                " available.");
  1008.       aarch64_num_wp_regs = 0;
  1009.     }

  1010.   /* Get hardware breakpoint register info.  */
  1011.   if (ptrace (PTRACE_GETREGSET, pid, NT_ARM_HW_BREAK, &iov) == 0
  1012.       && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8)
  1013.     {
  1014.       aarch64_num_bp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info);
  1015.       if (aarch64_num_bp_regs > AARCH64_HBP_MAX_NUM)
  1016.         {
  1017.           warning ("Unexpected number of hardware breakpoint registers reported"
  1018.                    " by ptrace, got %d, expected %d.",
  1019.                    aarch64_num_bp_regs, AARCH64_HBP_MAX_NUM);
  1020.           aarch64_num_bp_regs = AARCH64_HBP_MAX_NUM;
  1021.         }
  1022.     }
  1023.   else
  1024.     {
  1025.       warning ("Unable to determine the number of hardware breakpoints"
  1026.                " available.");
  1027.       aarch64_num_bp_regs = 0;
  1028.     }
  1029. }

  1030. static struct regset_info aarch64_regsets[] =
  1031. {
  1032.   { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
  1033.     sizeof (struct user_pt_regs), GENERAL_REGS,
  1034.     aarch64_fill_gregset, aarch64_store_gregset },
  1035.   { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET,
  1036.     sizeof (struct user_fpsimd_state), FP_REGS,
  1037.     aarch64_fill_fpregset, aarch64_store_fpregset
  1038.   },
  1039.   { 0, 0, 0, -1, -1, NULL, NULL }
  1040. };

  1041. static struct regsets_info aarch64_regsets_info =
  1042.   {
  1043.     aarch64_regsets, /* regsets */
  1044.     0, /* num_regsets */
  1045.     NULL, /* disabled_regsets */
  1046.   };

  1047. static struct usrregs_info aarch64_usrregs_info =
  1048.   {
  1049.     AARCH64_NUM_REGS,
  1050.     aarch64_regmap,
  1051.   };

  1052. static struct regs_info regs_info =
  1053.   {
  1054.     NULL, /* regset_bitmap */
  1055.     &aarch64_usrregs_info,
  1056.     &aarch64_regsets_info,
  1057.   };

  1058. static const struct regs_info *
  1059. aarch64_regs_info (void)
  1060. {
  1061.   return &regs_info;
  1062. }

  1063. struct linux_target_ops the_low_target =
  1064. {
  1065.   aarch64_arch_setup,
  1066.   aarch64_regs_info,
  1067.   aarch64_cannot_fetch_register,
  1068.   aarch64_cannot_store_register,
  1069.   NULL,
  1070.   aarch64_get_pc,
  1071.   aarch64_set_pc,
  1072.   (const unsigned char *) &aarch64_breakpoint,
  1073.   aarch64_breakpoint_len,
  1074.   NULL,
  1075.   0,
  1076.   aarch64_breakpoint_at,
  1077.   aarch64_supports_z_point_type,
  1078.   aarch64_insert_point,
  1079.   aarch64_remove_point,
  1080.   aarch64_stopped_by_watchpoint,
  1081.   aarch64_stopped_data_address,
  1082.   NULL,
  1083.   NULL,
  1084.   NULL,
  1085.   aarch64_linux_new_process,
  1086.   aarch64_linux_new_thread,
  1087.   aarch64_linux_prepare_to_resume,
  1088. };

  1089. void
  1090. initialize_low_arch (void)
  1091. {
  1092.   init_registers_aarch64 ();

  1093.   initialize_regsets_info (&aarch64_regsets_info);
  1094. }