gdb/aarch64-linux-nat.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Native-dependent code for GNU/Linux AArch64.

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

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"

  16. #include "inferior.h"
  17. #include "gdbcore.h"
  18. #include "regcache.h"
  19. #include "linux-nat.h"
  20. #include "target-descriptions.h"
  21. #include "auxv.h"
  22. #include "gdbcmd.h"
  23. #include "aarch64-tdep.h"
  24. #include "aarch64-linux-tdep.h"
  25. #include "elf/common.h"

  26. #include <sys/ptrace.h>
  27. #include <sys/utsname.h>
  28. #include <asm/ptrace.h>

  29. #include "gregset.h"

  30. #include "features/aarch64.c"

  31. /* Defines ps_err_e, struct ps_prochandle.  */
  32. #include "gdb_proc_service.h"

  33. #ifndef TRAP_HWBKPT
  34. #define TRAP_HWBKPT 0x0004
  35. #endif

  36. /* On GNU/Linux, threads are implemented as pseudo-processes, in which
  37.    case we may be tracing more than one process at a time.  In that
  38.    case, inferior_ptid will contain the main process ID and the
  39.    individual thread (process) ID.  get_thread_id () is used to get
  40.    the thread id if it's available, and the process id otherwise.  */

  41. static int
  42. get_thread_id (ptid_t ptid)
  43. {
  44.   int tid = ptid_get_lwp (ptid);

  45.   if (0 == tid)
  46.     tid = ptid_get_pid (ptid);
  47.   return tid;
  48. }

  49. /* Macro definitions, data structures, and code for the hardware
  50.    breakpoint and hardware watchpoint support follow.  We use the
  51.    following abbreviations throughout the code:

  52.    hw - hardware
  53.    bp - breakpoint
  54.    wp - watchpoint  */

  55. /* Maximum number of hardware breakpoint and watchpoint registers.
  56.    Neither of these values may exceed the width of dr_changed_t
  57.    measured in bits.  */

  58. #define AARCH64_HBP_MAX_NUM 16
  59. #define AARCH64_HWP_MAX_NUM 16

  60. /* Alignment requirement in bytes for addresses written to
  61.    hardware breakpoint and watchpoint value registers.

  62.    A ptrace call attempting to set an address that does not meet the
  63.    alignment criteria will fail.  Limited support has been provided in
  64.    this port for unaligned watchpoints, such that from a GDB user
  65.    perspective, an unaligned watchpoint may be requested.

  66.    This is achieved by minimally enlarging the watched area to meet the
  67.    alignment requirement, and if necessary, splitting the watchpoint
  68.    over several hardware watchpoint registers.  */

  69. #define AARCH64_HBP_ALIGNMENT 4
  70. #define AARCH64_HWP_ALIGNMENT 8

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

  73. #define AARCH64_HWP_MAX_LEN_PER_REG 8

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

  75.    31             24             16               8              0
  76.    +---------------+--------------+---------------+---------------+
  77.    |   RESERVED    |   RESERVED   |   DEBUG_ARCH  |  NUM_SLOTS    |
  78.    +---------------+--------------+---------------+---------------+  */


  79. /* Macros to extract fields from the hardware debug information word.  */
  80. #define AARCH64_DEBUG_NUM_SLOTS(x) ((x) & 0xff)
  81. #define AARCH64_DEBUG_ARCH(x) (((x) >> 8) & 0xff)

  82. /* Macro for the expected version of the ARMv8-A debug architecture.  */
  83. #define AARCH64_DEBUG_ARCH_V8 0x6

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

  87. static int aarch64_num_bp_regs;
  88. static int aarch64_num_wp_regs;

  89. /* Each bit of a variable of this type is used to indicate whether a
  90.    hardware breakpoint or watchpoint setting has been changed since
  91.    the last update.

  92.    Bit N corresponds to the Nth hardware breakpoint or watchpoint
  93.    setting which is managed in aarch64_debug_reg_state, where N is
  94.    valid between 0 and the total number of the hardware breakpoint or
  95.    watchpoint debug registers minus 1.

  96.    When bit N is 1, the corresponding breakpoint or watchpoint setting
  97.    has changed, and therefore the corresponding hardware debug
  98.    register needs to be updated via the ptrace interface.

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

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

  105. typedef ULONGEST dr_changed_t;

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

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

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

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

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

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

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

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

  142. /* Per-process data.  We don't bind this to a per-inferior registry
  143.    because of targets like x86 GNU/Linux that need to keep track of
  144.    processes that aren't bound to any inferior (e.g., fork children,
  145.    checkpoints).  */

  146. struct aarch64_process_info
  147. {
  148.   /* Linked list.  */
  149.   struct aarch64_process_info *next;

  150.   /* The process identifier.  */
  151.   pid_t pid;

  152.   /* Copy of aarch64 hardware debug registers.  */
  153.   struct aarch64_debug_reg_state state;
  154. };

  155. static struct aarch64_process_info *aarch64_process_list = NULL;

  156. /* Find process data for process PID.  */

  157. static struct aarch64_process_info *
  158. aarch64_find_process_pid (pid_t pid)
  159. {
  160.   struct aarch64_process_info *proc;

  161.   for (proc = aarch64_process_list; proc; proc = proc->next)
  162.     if (proc->pid == pid)
  163.       return proc;

  164.   return NULL;
  165. }

  166. /* Add process data for process PID.  Returns newly allocated info
  167.    object.  */

  168. static struct aarch64_process_info *
  169. aarch64_add_process (pid_t pid)
  170. {
  171.   struct aarch64_process_info *proc;

  172.   proc = xcalloc (1, sizeof (*proc));
  173.   proc->pid = pid;

  174.   proc->next = aarch64_process_list;
  175.   aarch64_process_list = proc;

  176.   return proc;
  177. }

  178. /* Get data specific info for process PID, creating it if necessary.
  179.    Never returns NULL.  */

  180. static struct aarch64_process_info *
  181. aarch64_process_info_get (pid_t pid)
  182. {
  183.   struct aarch64_process_info *proc;

  184.   proc = aarch64_find_process_pid (pid);
  185.   if (proc == NULL)
  186.     proc = aarch64_add_process (pid);

  187.   return proc;
  188. }

  189. /* Called whenever GDB is no longer debugging process PID.  It deletes
  190.    data structures that keep track of debug register state.  */

  191. static void
  192. aarch64_forget_process (pid_t pid)
  193. {
  194.   struct aarch64_process_info *proc, **proc_link;

  195.   proc = aarch64_process_list;
  196.   proc_link = &aarch64_process_list;

  197.   while (proc != NULL)
  198.     {
  199.       if (proc->pid == pid)
  200.         {
  201.           *proc_link = proc->next;

  202.           xfree (proc);
  203.           return;
  204.         }

  205.       proc_link = &proc->next;
  206.       proc = *proc_link;
  207.     }
  208. }

  209. /* Get debug registers state for process PID.  */

  210. static struct aarch64_debug_reg_state *
  211. aarch64_get_debug_reg_state (pid_t pid)
  212. {
  213.   return &aarch64_process_info_get (pid)->state;
  214. }

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

  216. struct arch_lwp_info
  217. {
  218.   /* When bit N is 1, it indicates the Nth hardware breakpoint or
  219.      watchpoint register pair needs to be updated when the thread is
  220.      resumed; see aarch64_linux_prepare_to_resume.  */
  221.   dr_changed_t dr_changed_bp;
  222.   dr_changed_t dr_changed_wp;
  223. };

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

  226. static void
  227. aarch64_linux_set_debug_regs (const struct aarch64_debug_reg_state *state,
  228.                               int tid, int watchpoint)
  229. {
  230.   int i, count;
  231.   struct iovec iov;
  232.   struct user_hwdebug_state regs;
  233.   const CORE_ADDR *addr;
  234.   const unsigned int *ctrl;

  235.   memset (&regs, 0, sizeof (regs));
  236.   iov.iov_base = &regs;
  237.   count = watchpoint ? aarch64_num_wp_regs : aarch64_num_bp_regs;
  238.   addr = watchpoint ? state->dr_addr_wp : state->dr_addr_bp;
  239.   ctrl = watchpoint ? state->dr_ctrl_wp : state->dr_ctrl_bp;
  240.   if (count == 0)
  241.     return;
  242.   iov.iov_len = (offsetof (struct user_hwdebug_state, dbg_regs[count - 1])
  243.                  + sizeof (regs.dbg_regs [count - 1]));

  244.   for (i = 0; i < count; i++)
  245.     {
  246.       regs.dbg_regs[i].addr = addr[i];
  247.       regs.dbg_regs[i].ctrl = ctrl[i];
  248.     }

  249.   if (ptrace (PTRACE_SETREGSET, tid,
  250.               watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK,
  251.               (void *) &iov))
  252.     error (_("Unexpected error setting hardware debug registers"));
  253. }

  254. struct aarch64_dr_update_callback_param
  255. {
  256.   int is_watchpoint;
  257.   unsigned int idx;
  258. };

  259. /* Callback for iterate_over_lwps.  Records the
  260.    information about the change of one hardware breakpoint/watchpoint
  261.    setting for the thread LWP.
  262.    The information is passed in via PTR.
  263.    N.B.  The actual updating of hardware debug registers is not
  264.    carried out until the moment the thread is resumed.  */

  265. static int
  266. debug_reg_change_callback (struct lwp_info *lwp, void *ptr)
  267. {
  268.   struct aarch64_dr_update_callback_param *param_p
  269.     = (struct aarch64_dr_update_callback_param *) ptr;
  270.   int pid = get_thread_id (lwp->ptid);
  271.   int idx = param_p->idx;
  272.   int is_watchpoint = param_p->is_watchpoint;
  273.   struct arch_lwp_info *info = lwp->arch_private;
  274.   dr_changed_t *dr_changed_ptr;
  275.   dr_changed_t dr_changed;

  276.   if (info == NULL)
  277.     info = lwp->arch_private = XCNEW (struct arch_lwp_info);

  278.   if (show_debug_regs)
  279.     {
  280.       fprintf_unfiltered (gdb_stdlog,
  281.                           "debug_reg_change_callback: \n\tOn entry:\n");
  282.       fprintf_unfiltered (gdb_stdlog,
  283.                           "\tpid%d, dr_changed_bp=0x%s, "
  284.                           "dr_changed_wp=0x%s\n",
  285.                           pid, phex (info->dr_changed_bp, 8),
  286.                           phex (info->dr_changed_wp, 8));
  287.     }

  288.   dr_changed_ptr = is_watchpoint ? &info->dr_changed_wp
  289.     : &info->dr_changed_bp;
  290.   dr_changed = *dr_changed_ptr;

  291.   gdb_assert (idx >= 0
  292.               && (idx <= (is_watchpoint ? aarch64_num_wp_regs
  293.                           : aarch64_num_bp_regs)));

  294.   /* The actual update is done later just before resuming the lwp,
  295.      we just mark that one register pair needs updating.  */
  296.   DR_MARK_N_CHANGED (dr_changed, idx);
  297.   *dr_changed_ptr = dr_changed;

  298.   /* If the lwp isn't stopped, force it to momentarily pause, so
  299.      we can update its debug registers.  */
  300.   if (!lwp->stopped)
  301.     linux_stop_lwp (lwp);

  302.   if (show_debug_regs)
  303.     {
  304.       fprintf_unfiltered (gdb_stdlog,
  305.                           "\tOn exit:\n\tpid%d, dr_changed_bp=0x%s, "
  306.                           "dr_changed_wp=0x%s\n",
  307.                           pid, phex (info->dr_changed_bp, 8),
  308.                           phex (info->dr_changed_wp, 8));
  309.     }

  310.   /* Continue the iteration.  */
  311.   return 0;
  312. }

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

  317. static void
  318. aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state *state,
  319.                                  int is_watchpoint, unsigned int idx)
  320. {
  321.   struct aarch64_dr_update_callback_param param;
  322.   ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));

  323.   param.is_watchpoint = is_watchpoint;
  324.   param.idx = idx;

  325.   iterate_over_lwps (pid_ptid, debug_reg_change_callback, (void *) &param);
  326. }

  327. /* Print the values of the cached breakpoint/watchpoint registers.  */

  328. static void
  329. aarch64_show_debug_reg_state (struct aarch64_debug_reg_state *state,
  330.                               const char *func, CORE_ADDR addr,
  331.                               int len, int type)
  332. {
  333.   int i;

  334.   fprintf_unfiltered (gdb_stdlog, "%s", func);
  335.   if (addr || len)
  336.     fprintf_unfiltered (gdb_stdlog, " (addr=0x%08lx, len=%d, type=%s)",
  337.                         (unsigned long) addr, len,
  338.                         type == hw_write ? "hw-write-watchpoint"
  339.                         : (type == hw_read ? "hw-read-watchpoint"
  340.                            : (type == hw_access ? "hw-access-watchpoint"
  341.                               : (type == hw_execute ? "hw-breakpoint"
  342.                                  : "??unknown??"))));
  343.   fprintf_unfiltered (gdb_stdlog, ":\n");

  344.   fprintf_unfiltered (gdb_stdlog, "\tBREAKPOINTs:\n");
  345.   for (i = 0; i < aarch64_num_bp_regs; i++)
  346.     fprintf_unfiltered (gdb_stdlog,
  347.                         "\tBP%d: addr=0x%08lx, ctrl=0x%08x, ref.count=%d\n",
  348.                         i, state->dr_addr_bp[i],
  349.                         state->dr_ctrl_bp[i], state->dr_ref_count_bp[i]);

  350.   fprintf_unfiltered (gdb_stdlog, "\tWATCHPOINTs:\n");
  351.   for (i = 0; i < aarch64_num_wp_regs; i++)
  352.     fprintf_unfiltered (gdb_stdlog,
  353.                         "\tWP%d: addr=0x%08lx, ctrl=0x%08x, ref.count=%d\n",
  354.                         i, state->dr_addr_wp[i],
  355.                         state->dr_ctrl_wp[i], state->dr_ref_count_wp[i]);
  356. }

  357. /* Fill GDB's register array with the general-purpose register values
  358.    from the current thread.  */

  359. static void
  360. fetch_gregs_from_thread (struct regcache *regcache)
  361. {
  362.   int ret, regno, tid;
  363.   elf_gregset_t regs;
  364.   struct iovec iovec;

  365.   tid = get_thread_id (inferior_ptid);

  366.   iovec.iov_base = &regs;
  367.   iovec.iov_len = sizeof (regs);

  368.   ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
  369.   if (ret < 0)
  370.     perror_with_name (_("Unable to fetch general registers."));

  371.   for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
  372.     regcache_raw_supply (regcache, regno,
  373.                          (char *) &regs[regno - AARCH64_X0_REGNUM]);
  374. }

  375. /* Store to the current thread the valid general-purpose register
  376.    values in the GDB's register array.  */

  377. static void
  378. store_gregs_to_thread (const struct regcache *regcache)
  379. {
  380.   int ret, regno, tid;
  381.   elf_gregset_t regs;
  382.   struct iovec iovec;

  383.   tid = get_thread_id (inferior_ptid);

  384.   iovec.iov_base = &regs;
  385.   iovec.iov_len = sizeof (regs);

  386.   ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
  387.   if (ret < 0)
  388.     perror_with_name (_("Unable to fetch general registers."));

  389.   for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
  390.     if (REG_VALID == regcache_register_status (regcache, regno))
  391.       regcache_raw_collect (regcache, regno,
  392.                             (char *) &regs[regno - AARCH64_X0_REGNUM]);

  393.   ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec);
  394.   if (ret < 0)
  395.     perror_with_name (_("Unable to store general registers."));
  396. }

  397. /* Fill GDB's register array with the fp/simd register values
  398.    from the current thread.  */

  399. static void
  400. fetch_fpregs_from_thread (struct regcache *regcache)
  401. {
  402.   int ret, regno, tid;
  403.   elf_fpregset_t regs;
  404.   struct iovec iovec;

  405.   tid = get_thread_id (inferior_ptid);

  406.   iovec.iov_base = &regs;
  407.   iovec.iov_len = sizeof (regs);

  408.   ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
  409.   if (ret < 0)
  410.     perror_with_name (_("Unable to fetch FP/SIMD registers."));

  411.   for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
  412.     regcache_raw_supply (regcache, regno,
  413.                          (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);

  414.   regcache_raw_supply (regcache, AARCH64_FPSR_REGNUM, (char *) &regs.fpsr);
  415.   regcache_raw_supply (regcache, AARCH64_FPCR_REGNUM, (char *) &regs.fpcr);
  416. }

  417. /* Store to the current thread the valid fp/simd register
  418.    values in the GDB's register array.  */

  419. static void
  420. store_fpregs_to_thread (const struct regcache *regcache)
  421. {
  422.   int ret, regno, tid;
  423.   elf_fpregset_t regs;
  424.   struct iovec iovec;

  425.   tid = get_thread_id (inferior_ptid);

  426.   iovec.iov_base = &regs;
  427.   iovec.iov_len = sizeof (regs);

  428.   ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
  429.   if (ret < 0)
  430.     perror_with_name (_("Unable to fetch FP/SIMD registers."));

  431.   for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
  432.     if (REG_VALID == regcache_register_status (regcache, regno))
  433.       regcache_raw_collect (regcache, regno,
  434.                             (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);

  435.   if (REG_VALID == regcache_register_status (regcache, AARCH64_FPSR_REGNUM))
  436.     regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM, (char *) &regs.fpsr);
  437.   if (REG_VALID == regcache_register_status (regcache, AARCH64_FPCR_REGNUM))
  438.     regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM, (char *) &regs.fpcr);

  439.   ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec);
  440.   if (ret < 0)
  441.     perror_with_name (_("Unable to store FP/SIMD registers."));
  442. }

  443. /* Implement the "to_fetch_register" target_ops method.  */

  444. static void
  445. aarch64_linux_fetch_inferior_registers (struct target_ops *ops,
  446.                                         struct regcache *regcache,
  447.                                         int regno)
  448. {
  449.   if (regno == -1)
  450.     {
  451.       fetch_gregs_from_thread (regcache);
  452.       fetch_fpregs_from_thread (regcache);
  453.     }
  454.   else if (regno < AARCH64_V0_REGNUM)
  455.     fetch_gregs_from_thread (regcache);
  456.   else
  457.     fetch_fpregs_from_thread (regcache);
  458. }

  459. /* Implement the "to_store_register" target_ops method.  */

  460. static void
  461. aarch64_linux_store_inferior_registers (struct target_ops *ops,
  462.                                         struct regcache *regcache,
  463.                                         int regno)
  464. {
  465.   if (regno == -1)
  466.     {
  467.       store_gregs_to_thread (regcache);
  468.       store_fpregs_to_thread (regcache);
  469.     }
  470.   else if (regno < AARCH64_V0_REGNUM)
  471.     store_gregs_to_thread (regcache);
  472.   else
  473.     store_fpregs_to_thread (regcache);
  474. }

  475. /* Fill register REGNO (if it is a general-purpose register) in
  476.    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
  477.    do this for all registers.  */

  478. void
  479. fill_gregset (const struct regcache *regcache,
  480.               gdb_gregset_t *gregsetp, int regno)
  481. {
  482.   regcache_collect_regset (&aarch64_linux_gregset, regcache,
  483.                            regno, (gdb_byte *) gregsetp,
  484.                            AARCH64_LINUX_SIZEOF_GREGSET);
  485. }

  486. /* Fill GDB's register array with the general-purpose register values
  487.    in *GREGSETP.  */

  488. void
  489. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  490. {
  491.   regcache_supply_regset (&aarch64_linux_gregset, regcache, -1,
  492.                           (const gdb_byte *) gregsetp,
  493.                           AARCH64_LINUX_SIZEOF_GREGSET);
  494. }

  495. /* Fill register REGNO (if it is a floating-point register) in
  496.    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
  497.    do this for all registers.  */

  498. void
  499. fill_fpregset (const struct regcache *regcache,
  500.                gdb_fpregset_t *fpregsetp, int regno)
  501. {
  502.   regcache_collect_regset (&aarch64_linux_fpregset, regcache,
  503.                            regno, (gdb_byte *) fpregsetp,
  504.                            AARCH64_LINUX_SIZEOF_FPREGSET);
  505. }

  506. /* Fill GDB's register array with the floating-point register values
  507.    in *FPREGSETP.  */

  508. void
  509. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
  510. {
  511.   regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1,
  512.                           (const gdb_byte *) fpregsetp,
  513.                           AARCH64_LINUX_SIZEOF_FPREGSET);
  514. }

  515. /* Called when resuming a thread.
  516.    The hardware debug registers are updated when there is any change.  */

  517. static void
  518. aarch64_linux_prepare_to_resume (struct lwp_info *lwp)
  519. {
  520.   struct arch_lwp_info *info = lwp->arch_private;

  521.   /* NULL means this is the main thread still going through the shell,
  522.      or, no watchpoint has been set yet.  In that case, there's
  523.      nothing to do.  */
  524.   if (info == NULL)
  525.     return;

  526.   if (DR_HAS_CHANGED (info->dr_changed_bp)
  527.       || DR_HAS_CHANGED (info->dr_changed_wp))
  528.     {
  529.       int tid = ptid_get_lwp (lwp->ptid);
  530.       struct aarch64_debug_reg_state *state
  531.         = aarch64_get_debug_reg_state (ptid_get_pid (lwp->ptid));

  532.       if (show_debug_regs)
  533.         fprintf_unfiltered (gdb_stdlog, "prepare_to_resume thread %d\n", tid);

  534.       /* Watchpoints.  */
  535.       if (DR_HAS_CHANGED (info->dr_changed_wp))
  536.         {
  537.           aarch64_linux_set_debug_regs (state, tid, 1);
  538.           DR_CLEAR_CHANGED (info->dr_changed_wp);
  539.         }

  540.       /* Breakpoints.  */
  541.       if (DR_HAS_CHANGED (info->dr_changed_bp))
  542.         {
  543.           aarch64_linux_set_debug_regs (state, tid, 0);
  544.           DR_CLEAR_CHANGED (info->dr_changed_bp);
  545.         }
  546.     }
  547. }

  548. static void
  549. aarch64_linux_new_thread (struct lwp_info *lp)
  550. {
  551.   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);

  552.   /* Mark that all the hardware breakpoint/watchpoint register pairs
  553.      for this thread need to be initialized.  */
  554.   DR_MARK_ALL_CHANGED (info->dr_changed_bp, aarch64_num_bp_regs);
  555.   DR_MARK_ALL_CHANGED (info->dr_changed_wp, aarch64_num_wp_regs);

  556.   lp->arch_private = info;
  557. }

  558. /* linux_nat_new_fork hook.   */

  559. static void
  560. aarch64_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
  561. {
  562.   pid_t parent_pid;
  563.   struct aarch64_debug_reg_state *parent_state;
  564.   struct aarch64_debug_reg_state *child_state;

  565.   /* NULL means no watchpoint has ever been set in the parent.  In
  566.      that case, there's nothing to do.  */
  567.   if (parent->arch_private == NULL)
  568.     return;

  569.   /* GDB core assumes the child inherits the watchpoints/hw
  570.      breakpoints of the parent, and will remove them all from the
  571.      forked off process.  Copy the debug registers mirrors into the
  572.      new process so that all breakpoints and watchpoints can be
  573.      removed together.  */

  574.   parent_pid = ptid_get_pid (parent->ptid);
  575.   parent_state = aarch64_get_debug_reg_state (parent_pid);
  576.   child_state = aarch64_get_debug_reg_state (child_pid);
  577.   *child_state = *parent_state;
  578. }


  579. /* Called by libthread_db.  Returns a pointer to the thread local
  580.    storage (or its descriptor).  */

  581. ps_err_e
  582. ps_get_thread_area (const struct ps_prochandle *ph,
  583.                     lwpid_t lwpid, int idx, void **base)
  584. {
  585.   struct iovec iovec;
  586.   uint64_t reg;

  587.   iovec.iov_base = &reg;
  588.   iovec.iov_len = sizeof (reg);

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

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

  595.   return PS_OK;
  596. }


  597. /* Get the hardware debug register capacity information.  */

  598. static void
  599. aarch64_linux_get_debug_reg_capacity (void)
  600. {
  601.   int tid;
  602.   struct iovec iov;
  603.   struct user_hwdebug_state dreg_state;

  604.   tid = get_thread_id (inferior_ptid);
  605.   iov.iov_base = &dreg_state;
  606.   iov.iov_len = sizeof (dreg_state);

  607.   /* Get hardware watchpoint register info.  */
  608.   if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_HW_WATCH, &iov) == 0
  609.       && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8)
  610.     {
  611.       aarch64_num_wp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info);
  612.       if (aarch64_num_wp_regs > AARCH64_HWP_MAX_NUM)
  613.         {
  614.           warning (_("Unexpected number of hardware watchpoint registers"
  615.                      " reported by ptrace, got %d, expected %d."),
  616.                    aarch64_num_wp_regs, AARCH64_HWP_MAX_NUM);
  617.           aarch64_num_wp_regs = AARCH64_HWP_MAX_NUM;
  618.         }
  619.     }
  620.   else
  621.     {
  622.       warning (_("Unable to determine the number of hardware watchpoints"
  623.                  " available."));
  624.       aarch64_num_wp_regs = 0;
  625.     }

  626.   /* Get hardware breakpoint register info.  */
  627.   if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_HW_BREAK, &iov) == 0
  628.       && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8)
  629.     {
  630.       aarch64_num_bp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info);
  631.       if (aarch64_num_bp_regs > AARCH64_HBP_MAX_NUM)
  632.         {
  633.           warning (_("Unexpected number of hardware breakpoint registers"
  634.                      " reported by ptrace, got %d, expected %d."),
  635.                    aarch64_num_bp_regs, AARCH64_HBP_MAX_NUM);
  636.           aarch64_num_bp_regs = AARCH64_HBP_MAX_NUM;
  637.         }
  638.     }
  639.   else
  640.     {
  641.       warning (_("Unable to determine the number of hardware breakpoints"
  642.                  " available."));
  643.       aarch64_num_bp_regs = 0;
  644.     }
  645. }

  646. static void (*super_post_startup_inferior) (struct target_ops *self,
  647.                                             ptid_t ptid);

  648. /* Implement the "to_post_startup_inferior" target_ops method.  */

  649. static void
  650. aarch64_linux_child_post_startup_inferior (struct target_ops *self,
  651.                                            ptid_t ptid)
  652. {
  653.   aarch64_forget_process (ptid_get_pid (ptid));
  654.   aarch64_linux_get_debug_reg_capacity ();
  655.   super_post_startup_inferior (self, ptid);
  656. }

  657. /* Implement the "to_read_description" target_ops method.  */

  658. static const struct target_desc *
  659. aarch64_linux_read_description (struct target_ops *ops)
  660. {
  661.   initialize_tdesc_aarch64 ();
  662.   return tdesc_aarch64;
  663. }

  664. /* Given the (potentially unaligned) watchpoint address in ADDR and
  665.    length in LEN, return the aligned address and aligned length in
  666.    *ALIGNED_ADDR_P and *ALIGNED_LEN_P, respectively.  The returned
  667.    aligned address and length will be valid values to write to the
  668.    hardware watchpoint value and control registers.

  669.    The given watchpoint may get truncated if more than one hardware
  670.    register is needed to cover the watched region.  *NEXT_ADDR_P
  671.    and *NEXT_LEN_P, if non-NULL, will return the address and length
  672.    of the remaining part of the watchpoint (which can be processed
  673.    by calling this routine again to generate another aligned address
  674.    and length pair.

  675.    See the comment above the function of the same name in
  676.    gdbserver/linux-aarch64-low.c for more information.  */

  677. static void
  678. aarch64_align_watchpoint (CORE_ADDR addr, int len, CORE_ADDR *aligned_addr_p,
  679.                           int *aligned_len_p, CORE_ADDR *next_addr_p,
  680.                           int *next_len_p)
  681. {
  682.   int aligned_len;
  683.   unsigned int offset;
  684.   CORE_ADDR aligned_addr;
  685.   const unsigned int alignment = AARCH64_HWP_ALIGNMENT;
  686.   const unsigned int max_wp_len = AARCH64_HWP_MAX_LEN_PER_REG;

  687.   /* As assumed by the algorithm.  */
  688.   gdb_assert (alignment == max_wp_len);

  689.   if (len <= 0)
  690.     return;

  691.   /* Address to be put into the hardware watchpoint value register
  692.      must be aligned.  */
  693.   offset = addr & (alignment - 1);
  694.   aligned_addr = addr - offset;

  695.   gdb_assert (offset >= 0 && offset < alignment);
  696.   gdb_assert (aligned_addr >= 0 && aligned_addr <= addr);
  697.   gdb_assert (offset + len > 0);

  698.   if (offset + len >= max_wp_len)
  699.     {
  700.       /* Need more than one watchpoint registers; truncate it at the
  701.          alignment boundary.  */
  702.       aligned_len = max_wp_len;
  703.       len -= (max_wp_len - offset);
  704.       addr += (max_wp_len - offset);
  705.       gdb_assert ((addr & (alignment - 1)) == 0);
  706.     }
  707.   else
  708.     {
  709.       /* Find the smallest valid length that is large enough to
  710.          accommodate this watchpoint.  */
  711.       static const unsigned char
  712.         aligned_len_array[AARCH64_HWP_MAX_LEN_PER_REG] =
  713.         { 1, 2, 4, 4, 8, 8, 8, 8 };

  714.       aligned_len = aligned_len_array[offset + len - 1];
  715.       addr += len;
  716.       len = 0;
  717.     }

  718.   if (aligned_addr_p)
  719.     *aligned_addr_p = aligned_addr;
  720.   if (aligned_len_p)
  721.     *aligned_len_p = aligned_len;
  722.   if (next_addr_p)
  723.     *next_addr_p = addr;
  724.   if (next_len_p)
  725.     *next_len_p = len;
  726. }

  727. /* Returns the number of hardware watchpoints of type TYPE that we can
  728.    set.  Value is positive if we can set CNT watchpoints, zero if
  729.    setting watchpoints of type TYPE is not supported, and negative if
  730.    CNT is more than the maximum number of watchpoints of type TYPE
  731.    that we can support.  TYPE is one of bp_hardware_watchpoint,
  732.    bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
  733.    CNT is the number of such watchpoints used so far (including this
  734.    one).  OTHERTYPE is non-zero if other types of watchpoints are
  735.    currently enabled.

  736.    We always return 1 here because we don't have enough information
  737.    about possible overlap of addresses that they want to watch.  As an
  738.    extreme example, consider the case where all the watchpoints watch
  739.    the same address and the same region length: then we can handle a
  740.    virtually unlimited number of watchpoints, due to debug register
  741.    sharing implemented via reference counts.  */

  742. static int
  743. aarch64_linux_can_use_hw_breakpoint (struct target_ops *self,
  744.                                      int type, int cnt, int othertype)
  745. {
  746.   return 1;
  747. }

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

  749.    31                             13          5      3      1     0
  750.    +--------------------------------+----------+------+------+----+
  751.    |         RESERVED (SBZ)         |  LENGTH  | TYPE | PRIV | EN |
  752.    +--------------------------------+----------+------+------+----+

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

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

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

  762. static inline unsigned int
  763. aarch64_watchpoint_length (unsigned int ctrl)
  764. {
  765.   switch (DR_CONTROL_LENGTH (ctrl))
  766.     {
  767.     case 0x01:
  768.       return 1;
  769.     case 0x03:
  770.       return 2;
  771.     case 0x0f:
  772.       return 4;
  773.     case 0xff:
  774.       return 8;
  775.     default:
  776.       return 0;
  777.     }
  778. }

  779. /* Given the hardware breakpoint or watchpoint type TYPE and its
  780.    length LEN, return the expected encoding for a hardware
  781.    breakpoint/watchpoint control register.  */

  782. static unsigned int
  783. aarch64_point_encode_ctrl_reg (int type, int len)
  784. {
  785.   unsigned int ctrl, ttype;

  786.   /* type */
  787.   switch (type)
  788.     {
  789.     case hw_write:
  790.       ttype = 2;
  791.       break;
  792.     case hw_read:
  793.       ttype = 1;
  794.       break;
  795.     case hw_access:
  796.       ttype = 3;
  797.       break;
  798.     case hw_execute:
  799.       ttype = 0;
  800.       break;
  801.     default:
  802.       perror_with_name (_("Unrecognized breakpoint/watchpoint type"));
  803.     }
  804.   ctrl = ttype << 3;

  805.   /* length bitmask */
  806.   ctrl |= ((1 << len) - 1) << 5;
  807.   /* enabled at el0 */
  808.   ctrl |= (2 << 1) | 1;

  809.   return ctrl;
  810. }

  811. /* Addresses to be written to the hardware breakpoint and watchpoint
  812.    value registers need to be aligned; the alignment is 4-byte and
  813.    8-type respectively.  Linux kernel rejects any non-aligned address
  814.    it receives from the related ptrace call.  Furthermore, the kernel
  815.    currently only supports the following Byte Address Select (BAS)
  816.    values: 0x1, 0x3, 0xf and 0xff, which means that for a hardware
  817.    watchpoint to be accepted by the kernel (via ptrace call), its
  818.    valid length can only be 1 byte, 2 bytes, 4 bytes or 8 bytes.
  819.    Despite these limitations, the unaligned watchpoint is supported in
  820.    this port.

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

  822. static int
  823. aarch64_point_is_aligned (int is_watchpoint, CORE_ADDR addr, int len)
  824. {
  825.   unsigned int alignment = is_watchpoint ? AARCH64_HWP_ALIGNMENT
  826.     : AARCH64_HBP_ALIGNMENT;

  827.   if (addr & (alignment - 1))
  828.     return 0;

  829.   if (len != 8 && len != 4 && len != 2 && len != 1)
  830.     return 0;

  831.   return 1;
  832. }

  833. /* Record the insertion of one breakpoint/watchpoint, as represented
  834.    by ADDR and CTRL, in the cached debug register state area *STATE.  */

  835. static int
  836. aarch64_dr_state_insert_one_point (struct aarch64_debug_reg_state *state,
  837.                                    int type, CORE_ADDR addr, int len)
  838. {
  839.   int i, idx, num_regs, is_watchpoint;
  840.   unsigned int ctrl, *dr_ctrl_p, *dr_ref_count;
  841.   CORE_ADDR *dr_addr_p;

  842.   /* Set up state pointers.  */
  843.   is_watchpoint = (type != hw_execute);
  844.   gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len));
  845.   if (is_watchpoint)
  846.     {
  847.       num_regs = aarch64_num_wp_regs;
  848.       dr_addr_p = state->dr_addr_wp;
  849.       dr_ctrl_p = state->dr_ctrl_wp;
  850.       dr_ref_count = state->dr_ref_count_wp;
  851.     }
  852.   else
  853.     {
  854.       num_regs = aarch64_num_bp_regs;
  855.       dr_addr_p = state->dr_addr_bp;
  856.       dr_ctrl_p = state->dr_ctrl_bp;
  857.       dr_ref_count = state->dr_ref_count_bp;
  858.     }

  859.   ctrl = aarch64_point_encode_ctrl_reg (type, len);

  860.   /* Find an existing or free register in our cache.  */
  861.   idx = -1;
  862.   for (i = 0; i < num_regs; ++i)
  863.     {
  864.       if ((dr_ctrl_p[i] & 1) == 0)
  865.         {
  866.           gdb_assert (dr_ref_count[i] == 0);
  867.           idx = i;
  868.           /* no break; continue hunting for an existing one.  */
  869.         }
  870.       else if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl)
  871.         {
  872.           gdb_assert (dr_ref_count[i] != 0);
  873.           idx = i;
  874.           break;
  875.         }
  876.     }

  877.   /* No space.  */
  878.   if (idx == -1)
  879.     return -1;

  880.   /* Update our cache.  */
  881.   if ((dr_ctrl_p[idx] & 1) == 0)
  882.     {
  883.       /* new entry */
  884.       dr_addr_p[idx] = addr;
  885.       dr_ctrl_p[idx] = ctrl;
  886.       dr_ref_count[idx] = 1;
  887.       /* Notify the change.  */
  888.       aarch64_notify_debug_reg_change (state, is_watchpoint, idx);
  889.     }
  890.   else
  891.     {
  892.       /* existing entry */
  893.       dr_ref_count[idx]++;
  894.     }

  895.   return 0;
  896. }

  897. /* Record the removal of one breakpoint/watchpoint, as represented by
  898.    ADDR and CTRL, in the cached debug register state area *STATE.  */

  899. static int
  900. aarch64_dr_state_remove_one_point (struct aarch64_debug_reg_state *state,
  901.                                    int type, CORE_ADDR addr, int len)
  902. {
  903.   int i, num_regs, is_watchpoint;
  904.   unsigned int ctrl, *dr_ctrl_p, *dr_ref_count;
  905.   CORE_ADDR *dr_addr_p;

  906.   /* Set up state pointers.  */
  907.   is_watchpoint = (type != hw_execute);
  908.   gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len));
  909.   if (is_watchpoint)
  910.     {
  911.       num_regs = aarch64_num_wp_regs;
  912.       dr_addr_p = state->dr_addr_wp;
  913.       dr_ctrl_p = state->dr_ctrl_wp;
  914.       dr_ref_count = state->dr_ref_count_wp;
  915.     }
  916.   else
  917.     {
  918.       num_regs = aarch64_num_bp_regs;
  919.       dr_addr_p = state->dr_addr_bp;
  920.       dr_ctrl_p = state->dr_ctrl_bp;
  921.       dr_ref_count = state->dr_ref_count_bp;
  922.     }

  923.   ctrl = aarch64_point_encode_ctrl_reg (type, len);

  924.   /* Find the entry that matches the ADDR and CTRL.  */
  925.   for (i = 0; i < num_regs; ++i)
  926.     if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl)
  927.       {
  928.         gdb_assert (dr_ref_count[i] != 0);
  929.         break;
  930.       }

  931.   /* Not found.  */
  932.   if (i == num_regs)
  933.     return -1;

  934.   /* Clear our cache.  */
  935.   if (--dr_ref_count[i] == 0)
  936.     {
  937.       /* Clear the enable bit.  */
  938.       ctrl &= ~1;
  939.       dr_addr_p[i] = 0;
  940.       dr_ctrl_p[i] = ctrl;
  941.       /* Notify the change.  */
  942.       aarch64_notify_debug_reg_change (state, is_watchpoint, i);
  943.     }

  944.   return 0;
  945. }

  946. /* Implement insertion and removal of a single breakpoint.  */

  947. static int
  948. aarch64_handle_breakpoint (int type, CORE_ADDR addr, int len, int is_insert)
  949. {
  950.   struct aarch64_debug_reg_state *state;

  951.   /* The hardware breakpoint on AArch64 should always be 4-byte
  952.      aligned.  */
  953.   if (!aarch64_point_is_aligned (0 /* is_watchpoint */ , addr, len))
  954.     return -1;

  955.   state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));

  956.   if (is_insert)
  957.     return aarch64_dr_state_insert_one_point (state, type, addr, len);
  958.   else
  959.     return aarch64_dr_state_remove_one_point (state, type, addr, len);
  960. }

  961. /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
  962.    Return 0 on success, -1 on failure.  */

  963. static int
  964. aarch64_linux_insert_hw_breakpoint (struct target_ops *self,
  965.                                     struct gdbarch *gdbarch,
  966.                                     struct bp_target_info *bp_tgt)
  967. {
  968.   int ret;
  969.   CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
  970.   const int len = 4;
  971.   const int type = hw_execute;

  972.   if (show_debug_regs)
  973.     fprintf_unfiltered
  974.       (gdb_stdlog,
  975.        "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
  976.        (unsigned long) addr, len);

  977.   ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */);

  978.   if (show_debug_regs)
  979.     {
  980.       struct aarch64_debug_reg_state *state
  981.         = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));

  982.       aarch64_show_debug_reg_state (state,
  983.                                     "insert_hw_watchpoint", addr, len, type);
  984.     }

  985.   return ret;
  986. }

  987. /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
  988.    Return 0 on success, -1 on failure.  */

  989. static int
  990. aarch64_linux_remove_hw_breakpoint (struct target_ops *self,
  991.                                     struct gdbarch *gdbarch,
  992.                                     struct bp_target_info *bp_tgt)
  993. {
  994.   int ret;
  995.   CORE_ADDR addr = bp_tgt->placed_address;
  996.   const int len = 4;
  997.   const int type = hw_execute;

  998.   if (show_debug_regs)
  999.     fprintf_unfiltered
  1000.       (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
  1001.        (unsigned long) addr, len);

  1002.   ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */);

  1003.   if (show_debug_regs)
  1004.     {
  1005.       struct aarch64_debug_reg_state *state
  1006.         = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));

  1007.       aarch64_show_debug_reg_state (state,
  1008.                                     "remove_hw_watchpoint", addr, len, type);
  1009.     }

  1010.   return ret;
  1011. }

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

  1014. static int
  1015. aarch64_handle_aligned_watchpoint (int type, CORE_ADDR addr, int len,
  1016.                                    int is_insert)
  1017. {
  1018.   struct aarch64_debug_reg_state *state
  1019.     = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));

  1020.   if (is_insert)
  1021.     return aarch64_dr_state_insert_one_point (state, type, addr, len);
  1022.   else
  1023.     return aarch64_dr_state_remove_one_point (state, type, addr, len);
  1024. }

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

  1031. static int
  1032. aarch64_handle_unaligned_watchpoint (int type, CORE_ADDR addr, int len,
  1033.                                      int is_insert)
  1034. {
  1035.   struct aarch64_debug_reg_state *state
  1036.     = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));

  1037.   while (len > 0)
  1038.     {
  1039.       CORE_ADDR aligned_addr;
  1040.       int aligned_len, ret;

  1041.       aarch64_align_watchpoint (addr, len, &aligned_addr, &aligned_len,
  1042.                                 &addr, &len);

  1043.       if (is_insert)
  1044.         ret = aarch64_dr_state_insert_one_point (state, type, aligned_addr,
  1045.                                                  aligned_len);
  1046.       else
  1047.         ret = aarch64_dr_state_remove_one_point (state, type, aligned_addr,
  1048.                                                  aligned_len);

  1049.       if (show_debug_regs)
  1050.         fprintf_unfiltered (gdb_stdlog,
  1051. "handle_unaligned_watchpoint: is_insert: %d\n"
  1052. "                             aligned_addr: 0x%08lx, aligned_len: %d\n"
  1053. "                                next_addr: 0x%08lx,    next_len: %d\n",
  1054.                  is_insert, aligned_addr, aligned_len, addr, len);

  1055.       if (ret != 0)
  1056.         return ret;
  1057.     }

  1058.   return 0;
  1059. }

  1060. /* Implements insertion and removal of a single watchpoint.  */

  1061. static int
  1062. aarch64_handle_watchpoint (int type, CORE_ADDR addr, int len, int is_insert)
  1063. {
  1064.   if (aarch64_point_is_aligned (1 /* is_watchpoint */ , addr, len))
  1065.     return aarch64_handle_aligned_watchpoint (type, addr, len, is_insert);
  1066.   else
  1067.     return aarch64_handle_unaligned_watchpoint (type, addr, len, is_insert);
  1068. }

  1069. /* Implement the "to_insert_watchpoint" target_ops method.

  1070.    Insert a watchpoint to watch a memory region which starts at
  1071.    address ADDR and whose length is LEN bytes.  Watch memory accesses
  1072.    of the type TYPE.  Return 0 on success, -1 on failure.  */

  1073. static int
  1074. aarch64_linux_insert_watchpoint (struct target_ops *self,
  1075.                                  CORE_ADDR addr, int len, int type,
  1076.                                  struct expression *cond)
  1077. {
  1078.   int ret;

  1079.   if (show_debug_regs)
  1080.     fprintf_unfiltered (gdb_stdlog,
  1081.                         "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
  1082.                         (unsigned long) addr, len);

  1083.   gdb_assert (type != hw_execute);

  1084.   ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */);

  1085.   if (show_debug_regs)
  1086.     {
  1087.       struct aarch64_debug_reg_state *state
  1088.         = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));

  1089.       aarch64_show_debug_reg_state (state,
  1090.                                     "insert_watchpoint", addr, len, type);
  1091.     }

  1092.   return ret;
  1093. }

  1094. /* Implement the "to_remove_watchpoint" target_ops method.
  1095.    Remove a watchpoint that watched the memory region which starts at
  1096.    address ADDR, whose length is LEN bytes, and for accesses of the
  1097.    type TYPE.  Return 0 on success, -1 on failure.  */

  1098. static int
  1099. aarch64_linux_remove_watchpoint (struct target_ops *self,
  1100.                                  CORE_ADDR addr, int len, int type,
  1101.                                  struct expression *cond)
  1102. {
  1103.   int ret;

  1104.   if (show_debug_regs)
  1105.     fprintf_unfiltered (gdb_stdlog,
  1106.                         "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
  1107.                         (unsigned long) addr, len);

  1108.   gdb_assert (type != hw_execute);

  1109.   ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */);

  1110.   if (show_debug_regs)
  1111.     {
  1112.       struct aarch64_debug_reg_state *state
  1113.         = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));

  1114.       aarch64_show_debug_reg_state (state,
  1115.                                     "remove_watchpoint", addr, len, type);
  1116.     }

  1117.   return ret;
  1118. }

  1119. /* Implement the "to_region_ok_for_hw_watchpoint" target_ops method.  */

  1120. static int
  1121. aarch64_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
  1122.                                            CORE_ADDR addr, int len)
  1123. {
  1124.   CORE_ADDR aligned_addr;

  1125.   /* Can not set watchpoints for zero or negative lengths.  */
  1126.   if (len <= 0)
  1127.     return 0;

  1128.   /* Must have hardware watchpoint debug register(s).  */
  1129.   if (aarch64_num_wp_regs == 0)
  1130.     return 0;

  1131.   /* We support unaligned watchpoint address and arbitrary length,
  1132.      as long as the size of the whole watched area after alignment
  1133.      doesn't exceed size of the total area that all watchpoint debug
  1134.      registers can watch cooperatively.

  1135.      This is a very relaxed rule, but unfortunately there are
  1136.      limitations, e.g. false-positive hits, due to limited support of
  1137.      hardware debug registers in the kernel.  See comment above
  1138.      aarch64_align_watchpoint for more information.  */

  1139.   aligned_addr = addr & ~(AARCH64_HWP_MAX_LEN_PER_REG - 1);
  1140.   if (aligned_addr + aarch64_num_wp_regs * AARCH64_HWP_MAX_LEN_PER_REG
  1141.       < addr + len)
  1142.     return 0;

  1143.   /* All tests passed so we are likely to be able to set the watchpoint.
  1144.      The reason that it is 'likely' rather than 'must' is because
  1145.      we don't check the current usage of the watchpoint registers, and
  1146.      there may not be enough registers available for this watchpoint.
  1147.      Ideally we should check the cached debug register state, however
  1148.      the checking is costly.  */
  1149.   return 1;
  1150. }

  1151. /* Implement the "to_stopped_data_address" target_ops method.  */

  1152. static int
  1153. aarch64_linux_stopped_data_address (struct target_ops *target,
  1154.                                     CORE_ADDR *addr_p)
  1155. {
  1156.   siginfo_t siginfo;
  1157.   int i, tid;
  1158.   struct aarch64_debug_reg_state *state;

  1159.   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
  1160.     return 0;

  1161.   /* This must be a hardware breakpoint.  */
  1162.   if (siginfo.si_signo != SIGTRAP
  1163.       || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
  1164.     return 0;

  1165.   /* Check if the address matches any watched address.  */
  1166.   state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
  1167.   for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
  1168.     {
  1169.       const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
  1170.       const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
  1171.       const CORE_ADDR addr_watch = state->dr_addr_wp[i];

  1172.       if (state->dr_ref_count_wp[i]
  1173.           && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
  1174.           && addr_trap >= addr_watch
  1175.           && addr_trap < addr_watch + len)
  1176.         {
  1177.           *addr_p = addr_trap;
  1178.           return 1;
  1179.         }
  1180.     }

  1181.   return 0;
  1182. }

  1183. /* Implement the "to_stopped_by_watchpoint" target_ops method.  */

  1184. static int
  1185. aarch64_linux_stopped_by_watchpoint (struct target_ops *ops)
  1186. {
  1187.   CORE_ADDR addr;

  1188.   return aarch64_linux_stopped_data_address (ops, &addr);
  1189. }

  1190. /* Implement the "to_watchpoint_addr_within_range" target_ops method.  */

  1191. static int
  1192. aarch64_linux_watchpoint_addr_within_range (struct target_ops *target,
  1193.                                             CORE_ADDR addr,
  1194.                                             CORE_ADDR start, int length)
  1195. {
  1196.   return start <= addr && start + length - 1 >= addr;
  1197. }

  1198. /* Define AArch64 maintenance commands.  */

  1199. static void
  1200. add_show_debug_regs_command (void)
  1201. {
  1202.   /* A maintenance command to enable printing the internal DRi mirror
  1203.      variables.  */
  1204.   add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
  1205.                            &show_debug_regs, _("\
  1206. Set whether to show variables that mirror the AArch64 debug registers."), _("\
  1207. Show whether to show variables that mirror the AArch64 debug registers."), _("\
  1208. Use \"on\" to enable, \"off\" to disable.\n\
  1209. If enabled, the debug registers values are shown when GDB inserts\n\
  1210. or removes a hardware breakpoint or watchpoint, and when the inferior\n\
  1211. triggers a breakpoint or watchpoint."),
  1212.                            NULL,
  1213.                            NULL,
  1214.                            &maintenance_set_cmdlist,
  1215.                            &maintenance_show_cmdlist);
  1216. }

  1217. /* -Wmissing-prototypes.  */
  1218. void _initialize_aarch64_linux_nat (void);

  1219. void
  1220. _initialize_aarch64_linux_nat (void)
  1221. {
  1222.   struct target_ops *t;

  1223.   /* Fill in the generic GNU/Linux methods.  */
  1224.   t = linux_target ();

  1225.   add_show_debug_regs_command ();

  1226.   /* Add our register access methods.  */
  1227.   t->to_fetch_registers = aarch64_linux_fetch_inferior_registers;
  1228.   t->to_store_registers = aarch64_linux_store_inferior_registers;

  1229.   t->to_read_description = aarch64_linux_read_description;

  1230.   t->to_can_use_hw_breakpoint = aarch64_linux_can_use_hw_breakpoint;
  1231.   t->to_insert_hw_breakpoint = aarch64_linux_insert_hw_breakpoint;
  1232.   t->to_remove_hw_breakpoint = aarch64_linux_remove_hw_breakpoint;
  1233.   t->to_region_ok_for_hw_watchpoint =
  1234.     aarch64_linux_region_ok_for_hw_watchpoint;
  1235.   t->to_insert_watchpoint = aarch64_linux_insert_watchpoint;
  1236.   t->to_remove_watchpoint = aarch64_linux_remove_watchpoint;
  1237.   t->to_stopped_by_watchpoint = aarch64_linux_stopped_by_watchpoint;
  1238.   t->to_stopped_data_address = aarch64_linux_stopped_data_address;
  1239.   t->to_watchpoint_addr_within_range =
  1240.     aarch64_linux_watchpoint_addr_within_range;

  1241.   /* Override the GNU/Linux inferior startup hook.  */
  1242.   super_post_startup_inferior = t->to_post_startup_inferior;
  1243.   t->to_post_startup_inferior = aarch64_linux_child_post_startup_inferior;

  1244.   /* Register the target.  */
  1245.   linux_nat_add_target (t);
  1246.   linux_nat_set_new_thread (t, aarch64_linux_new_thread);
  1247.   linux_nat_set_new_fork (t, aarch64_linux_new_fork);
  1248.   linux_nat_set_forget_process (t, aarch64_forget_process);
  1249.   linux_nat_set_prepare_to_resume (t, aarch64_linux_prepare_to_resume);
  1250. }