GDB (API)
/home/stan/gdb/src/gdb/aarch64-linux-nat.c
Go to the documentation of this file.
00001 /* Native-dependent code for GNU/Linux AArch64.
00002 
00003    Copyright (C) 2011-2013 Free Software Foundation, Inc.
00004    Contributed by ARM Ltd.
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #include "defs.h"
00022 
00023 #include "inferior.h"
00024 #include "gdbcore.h"
00025 #include "regcache.h"
00026 #include "linux-nat.h"
00027 #include "target-descriptions.h"
00028 #include "auxv.h"
00029 #include "gdbcmd.h"
00030 #include "aarch64-tdep.h"
00031 #include "aarch64-linux-tdep.h"
00032 #include "elf/common.h"
00033 
00034 #include <sys/ptrace.h>
00035 #include <sys/utsname.h>
00036 
00037 #include "gregset.h"
00038 
00039 #include "features/aarch64.c"
00040 
00041 /* Defines ps_err_e, struct ps_prochandle.  */
00042 #include "gdb_proc_service.h"
00043 
00044 #ifndef TRAP_HWBKPT
00045 #define TRAP_HWBKPT 0x0004
00046 #endif
00047 
00048 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
00049    case we may be tracing more than one process at a time.  In that
00050    case, inferior_ptid will contain the main process ID and the
00051    individual thread (process) ID.  get_thread_id () is used to get
00052    the thread id if it's available, and the process id otherwise.  */
00053 
00054 static int
00055 get_thread_id (ptid_t ptid)
00056 {
00057   int tid = ptid_get_lwp (ptid);
00058 
00059   if (0 == tid)
00060     tid = ptid_get_pid (ptid);
00061   return tid;
00062 }
00063 
00064 /* Macro definitions, data structures, and code for the hardware
00065    breakpoint and hardware watchpoint support follow.  We use the
00066    following abbreviations throughout the code:
00067 
00068    hw - hardware
00069    bp - breakpoint
00070    wp - watchpoint  */
00071 
00072 /* Maximum number of hardware breakpoint and watchpoint registers.
00073    Neither of these values may exceed the width of dr_changed_t
00074    measured in bits.  */
00075 
00076 #define AARCH64_HBP_MAX_NUM 16
00077 #define AARCH64_HWP_MAX_NUM 16
00078 
00079 /* Alignment requirement in bytes for addresses written to
00080    hardware breakpoint and watchpoint value registers.
00081 
00082    A ptrace call attempting to set an address that does not meet the
00083    alignment criteria will fail.  Limited support has been provided in
00084    this port for unaligned watchpoints, such that from a GDB user
00085    perspective, an unaligned watchpoint may be requested.
00086 
00087    This is achieved by minimally enlarging the watched area to meet the
00088    alignment requirement, and if necessary, splitting the watchpoint
00089    over several hardware watchpoint registers.  */
00090 
00091 #define AARCH64_HBP_ALIGNMENT 4
00092 #define AARCH64_HWP_ALIGNMENT 8
00093 
00094 /* The maximum length of a memory region that can be watched by one
00095    hardware watchpoint register.  */
00096 
00097 #define AARCH64_HWP_MAX_LEN_PER_REG 8
00098 
00099 /* ptrace hardware breakpoint resource info is formatted as follows:
00100 
00101    31             24             16               8              0
00102    +---------------+--------------+---------------+---------------+
00103    |   RESERVED    |   RESERVED   |   DEBUG_ARCH  |  NUM_SLOTS    |
00104    +---------------+--------------+---------------+---------------+  */
00105 
00106 
00107 /* Macros to extract fields from the hardware debug information word.  */
00108 #define AARCH64_DEBUG_NUM_SLOTS(x) ((x) & 0xff)
00109 #define AARCH64_DEBUG_ARCH(x) (((x) >> 8) & 0xff)
00110 
00111 /* Macro for the expected version of the ARMv8-A debug architecture.  */
00112 #define AARCH64_DEBUG_ARCH_V8 0x6
00113 
00114 /* Number of hardware breakpoints/watchpoints the target supports.
00115    They are initialized with values obtained via the ptrace calls
00116    with NT_ARM_HW_BREAK and NT_ARM_HW_WATCH respectively.  */
00117 
00118 static int aarch64_num_bp_regs;
00119 static int aarch64_num_wp_regs;
00120 
00121 /* Debugging of hardware breakpoint/watchpoint support.  */
00122 
00123 static int debug_hw_points;
00124 
00125 /* Each bit of a variable of this type is used to indicate whether a
00126    hardware breakpoint or watchpoint setting has been changed since
00127    the last update.
00128 
00129    Bit N corresponds to the Nth hardware breakpoint or watchpoint
00130    setting which is managed in aarch64_debug_reg_state, where N is
00131    valid between 0 and the total number of the hardware breakpoint or
00132    watchpoint debug registers minus 1.
00133 
00134    When bit N is 1, the corresponding breakpoint or watchpoint setting
00135    has changed, and therefore the corresponding hardware debug
00136    register needs to be updated via the ptrace interface.
00137 
00138    In the per-thread arch-specific data area, we define two such
00139    variables for per-thread hardware breakpoint and watchpoint
00140    settings respectively.
00141 
00142    This type is part of the mechanism which helps reduce the number of
00143    ptrace calls to the kernel, i.e. avoid asking the kernel to write
00144    to the debug registers with unchanged values.  */
00145 
00146 typedef unsigned LONGEST dr_changed_t;
00147 
00148 /* Set each of the lower M bits of X to 1; assert X is wide enough.  */
00149 
00150 #define DR_MARK_ALL_CHANGED(x, m)                                       \
00151   do                                                                    \
00152     {                                                                   \
00153       gdb_assert (sizeof ((x)) * 8 >= (m));                             \
00154       (x) = (((dr_changed_t)1 << (m)) - 1);                             \
00155     } while (0)
00156 
00157 #define DR_MARK_N_CHANGED(x, n)                                         \
00158   do                                                                    \
00159     {                                                                   \
00160       (x) |= ((dr_changed_t)1 << (n));                                  \
00161     } while (0)
00162 
00163 #define DR_CLEAR_CHANGED(x)                                             \
00164   do                                                                    \
00165     {                                                                   \
00166       (x) = 0;                                                          \
00167     } while (0)
00168 
00169 #define DR_HAS_CHANGED(x) ((x) != 0)
00170 #define DR_N_HAS_CHANGED(x, n) ((x) & ((dr_changed_t)1 << (n)))
00171 
00172 /* Structure for managing the hardware breakpoint/watchpoint resources.
00173    DR_ADDR_* stores the address, DR_CTRL_* stores the control register
00174    content, and DR_REF_COUNT_* counts the numbers of references to the
00175    corresponding bp/wp, by which way the limited hardware resources
00176    are not wasted on duplicated bp/wp settings (though so far gdb has
00177    done a good job by not sending duplicated bp/wp requests).  */
00178 
00179 struct aarch64_debug_reg_state
00180 {
00181   /* hardware breakpoint */
00182   CORE_ADDR dr_addr_bp[AARCH64_HBP_MAX_NUM];
00183   unsigned int dr_ctrl_bp[AARCH64_HBP_MAX_NUM];
00184   unsigned int dr_ref_count_bp[AARCH64_HBP_MAX_NUM];
00185 
00186   /* hardware watchpoint */
00187   CORE_ADDR dr_addr_wp[AARCH64_HWP_MAX_NUM];
00188   unsigned int dr_ctrl_wp[AARCH64_HWP_MAX_NUM];
00189   unsigned int dr_ref_count_wp[AARCH64_HWP_MAX_NUM];
00190 };
00191 
00192 /* Per-process data.  We don't bind this to a per-inferior registry
00193    because of targets like x86 GNU/Linux that need to keep track of
00194    processes that aren't bound to any inferior (e.g., fork children,
00195    checkpoints).  */
00196 
00197 struct aarch64_process_info
00198 {
00199   /* Linked list.  */
00200   struct aarch64_process_info *next;
00201 
00202   /* The process identifier.  */
00203   pid_t pid;
00204 
00205   /* Copy of aarch64 hardware debug registers.  */
00206   struct aarch64_debug_reg_state state;
00207 };
00208 
00209 static struct aarch64_process_info *aarch64_process_list = NULL;
00210 
00211 /* Find process data for process PID.  */
00212 
00213 static struct aarch64_process_info *
00214 aarch64_find_process_pid (pid_t pid)
00215 {
00216   struct aarch64_process_info *proc;
00217 
00218   for (proc = aarch64_process_list; proc; proc = proc->next)
00219     if (proc->pid == pid)
00220       return proc;
00221 
00222   return NULL;
00223 }
00224 
00225 /* Add process data for process PID.  Returns newly allocated info
00226    object.  */
00227 
00228 static struct aarch64_process_info *
00229 aarch64_add_process (pid_t pid)
00230 {
00231   struct aarch64_process_info *proc;
00232 
00233   proc = xcalloc (1, sizeof (*proc));
00234   proc->pid = pid;
00235 
00236   proc->next = aarch64_process_list;
00237   aarch64_process_list = proc;
00238 
00239   return proc;
00240 }
00241 
00242 /* Get data specific info for process PID, creating it if necessary.
00243    Never returns NULL.  */
00244 
00245 static struct aarch64_process_info *
00246 aarch64_process_info_get (pid_t pid)
00247 {
00248   struct aarch64_process_info *proc;
00249 
00250   proc = aarch64_find_process_pid (pid);
00251   if (proc == NULL)
00252     proc = aarch64_add_process (pid);
00253 
00254   return proc;
00255 }
00256 
00257 /* Called whenever GDB is no longer debugging process PID.  It deletes
00258    data structures that keep track of debug register state.  */
00259 
00260 static void
00261 aarch64_forget_process (pid_t pid)
00262 {
00263   struct aarch64_process_info *proc, **proc_link;
00264 
00265   proc = aarch64_process_list;
00266   proc_link = &aarch64_process_list;
00267 
00268   while (proc != NULL)
00269     {
00270       if (proc->pid == pid)
00271         {
00272           *proc_link = proc->next;
00273 
00274           xfree (proc);
00275           return;
00276         }
00277 
00278       proc_link = &proc->next;
00279       proc = *proc_link;
00280     }
00281 }
00282 
00283 /* Get debug registers state for process PID.  */
00284 
00285 static struct aarch64_debug_reg_state *
00286 aarch64_get_debug_reg_state (pid_t pid)
00287 {
00288   return &aarch64_process_info_get (pid)->state;
00289 }
00290 
00291 /* Per-thread arch-specific data we want to keep.  */
00292 
00293 struct arch_lwp_info
00294 {
00295   /* When bit N is 1, it indicates the Nth hardware breakpoint or
00296      watchpoint register pair needs to be updated when the thread is
00297      resumed; see aarch64_linux_prepare_to_resume.  */
00298   dr_changed_t dr_changed_bp;
00299   dr_changed_t dr_changed_wp;
00300 };
00301 
00302 /* Call ptrace to set the thread TID's hardware breakpoint/watchpoint
00303    registers with data from *STATE.  */
00304 
00305 static void
00306 aarch64_linux_set_debug_regs (const struct aarch64_debug_reg_state *state,
00307                               int tid, int watchpoint)
00308 {
00309   int i, count;
00310   struct iovec iov;
00311   struct user_hwdebug_state regs;
00312   const CORE_ADDR *addr;
00313   const unsigned int *ctrl;
00314 
00315   memset (&regs, 0, sizeof (regs));
00316   iov.iov_base = &regs;
00317   iov.iov_len = sizeof (regs);
00318   count = watchpoint ? aarch64_num_wp_regs : aarch64_num_bp_regs;
00319   addr = watchpoint ? state->dr_addr_wp : state->dr_addr_bp;
00320   ctrl = watchpoint ? state->dr_ctrl_wp : state->dr_ctrl_bp;
00321 
00322   for (i = 0; i < count; i++)
00323     {
00324       regs.dbg_regs[i].addr = addr[i];
00325       regs.dbg_regs[i].ctrl = ctrl[i];
00326     }
00327 
00328   if (ptrace (PTRACE_SETREGSET, tid,
00329               watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK,
00330               (void *) &iov))
00331     error (_("Unexpected error setting hardware debug registers"));
00332 }
00333 
00334 struct aarch64_dr_update_callback_param
00335 {
00336   int is_watchpoint;
00337   unsigned int idx;
00338 };
00339 
00340 /* Callback for iterate_over_lwps.  Records the
00341    information about the change of one hardware breakpoint/watchpoint
00342    setting for the thread LWP.
00343    The information is passed in via PTR.
00344    N.B.  The actual updating of hardware debug registers is not
00345    carried out until the moment the thread is resumed.  */
00346 
00347 static int
00348 debug_reg_change_callback (struct lwp_info *lwp, void *ptr)
00349 {
00350   struct aarch64_dr_update_callback_param *param_p
00351     = (struct aarch64_dr_update_callback_param *) ptr;
00352   int pid = get_thread_id (lwp->ptid);
00353   int idx = param_p->idx;
00354   int is_watchpoint = param_p->is_watchpoint;
00355   struct arch_lwp_info *info = lwp->arch_private;
00356   dr_changed_t *dr_changed_ptr;
00357   dr_changed_t dr_changed;
00358 
00359   if (info == NULL)
00360     info = lwp->arch_private = XCNEW (struct arch_lwp_info);
00361 
00362   if (debug_hw_points)
00363     {
00364       fprintf_unfiltered (gdb_stdlog,
00365                           "debug_reg_change_callback: \n\tOn entry:\n");
00366       fprintf_unfiltered (gdb_stdlog,
00367                           "\tpid%d, dr_changed_bp=0x%s, "
00368                           "dr_changed_wp=0x%s\n",
00369                           pid, phex (info->dr_changed_bp, 8),
00370                           phex (info->dr_changed_wp, 8));
00371     }
00372 
00373   dr_changed_ptr = is_watchpoint ? &info->dr_changed_wp
00374     : &info->dr_changed_bp;
00375   dr_changed = *dr_changed_ptr;
00376 
00377   gdb_assert (idx >= 0
00378               && (idx <= (is_watchpoint ? aarch64_num_wp_regs
00379                           : aarch64_num_bp_regs)));
00380 
00381   /* The actual update is done later just before resuming the lwp,
00382      we just mark that one register pair needs updating.  */
00383   DR_MARK_N_CHANGED (dr_changed, idx);
00384   *dr_changed_ptr = dr_changed;
00385 
00386   /* If the lwp isn't stopped, force it to momentarily pause, so
00387      we can update its debug registers.  */
00388   if (!lwp->stopped)
00389     linux_stop_lwp (lwp);
00390 
00391   if (debug_hw_points)
00392     {
00393       fprintf_unfiltered (gdb_stdlog,
00394                           "\tOn exit:\n\tpid%d, dr_changed_bp=0x%s, "
00395                           "dr_changed_wp=0x%s\n",
00396                           pid, phex (info->dr_changed_bp, 8),
00397                           phex (info->dr_changed_wp, 8));
00398     }
00399 
00400   /* Continue the iteration.  */
00401   return 0;
00402 }
00403 
00404 /* Notify each thread that their IDXth breakpoint/watchpoint register
00405    pair needs to be updated.  The message will be recorded in each
00406    thread's arch-specific data area, the actual updating will be done
00407    when the thread is resumed.  */
00408 
00409 static void
00410 aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state *state,
00411                                  int is_watchpoint, unsigned int idx)
00412 {
00413   struct aarch64_dr_update_callback_param param;
00414   ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
00415 
00416   param.is_watchpoint = is_watchpoint;
00417   param.idx = idx;
00418 
00419   iterate_over_lwps (pid_ptid, debug_reg_change_callback, (void *) &param);
00420 }
00421 
00422 /* Print the values of the cached breakpoint/watchpoint registers.  */
00423 
00424 static void
00425 aarch64_show_debug_reg_state (struct aarch64_debug_reg_state *state,
00426                               const char *func, CORE_ADDR addr,
00427                               int len, int type)
00428 {
00429   int i;
00430 
00431   fprintf_unfiltered (gdb_stdlog, "%s", func);
00432   if (addr || len)
00433     fprintf_unfiltered (gdb_stdlog, " (addr=0x%08lx, len=%d, type=%s)",
00434                         (unsigned long) addr, len,
00435                         type == hw_write ? "hw-write-watchpoint"
00436                         : (type == hw_read ? "hw-read-watchpoint"
00437                            : (type == hw_access ? "hw-access-watchpoint"
00438                               : (type == hw_execute ? "hw-breakpoint"
00439                                  : "??unknown??"))));
00440   fprintf_unfiltered (gdb_stdlog, ":\n");
00441 
00442   fprintf_unfiltered (gdb_stdlog, "\tBREAKPOINTs:\n");
00443   for (i = 0; i < aarch64_num_bp_regs; i++)
00444     fprintf_unfiltered (gdb_stdlog,
00445                         "\tBP%d: addr=0x%08lx, ctrl=0x%08x, ref.count=%d\n",
00446                         i, state->dr_addr_bp[i],
00447                         state->dr_ctrl_bp[i], state->dr_ref_count_bp[i]);
00448 
00449   fprintf_unfiltered (gdb_stdlog, "\tWATCHPOINTs:\n");
00450   for (i = 0; i < aarch64_num_wp_regs; i++)
00451     fprintf_unfiltered (gdb_stdlog,
00452                         "\tWP%d: addr=0x%08lx, ctrl=0x%08x, ref.count=%d\n",
00453                         i, state->dr_addr_wp[i],
00454                         state->dr_ctrl_wp[i], state->dr_ref_count_wp[i]);
00455 }
00456 
00457 /* Fill GDB's register array with the general-purpose register values
00458    from the current thread.  */
00459 
00460 static void
00461 fetch_gregs_from_thread (struct regcache *regcache)
00462 {
00463   int ret, regno, tid;
00464   elf_gregset_t regs;
00465   struct iovec iovec;
00466 
00467   tid = get_thread_id (inferior_ptid);
00468 
00469   iovec.iov_base = &regs;
00470   iovec.iov_len = sizeof (regs);
00471 
00472   ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
00473   if (ret < 0)
00474     perror_with_name (_("Unable to fetch general registers."));
00475 
00476   for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
00477     regcache_raw_supply (regcache, regno,
00478                          (char *) &regs[regno - AARCH64_X0_REGNUM]);
00479 }
00480 
00481 /* Store to the current thread the valid general-purpose register
00482    values in the GDB's register array.  */
00483 
00484 static void
00485 store_gregs_to_thread (const struct regcache *regcache)
00486 {
00487   int ret, regno, tid;
00488   elf_gregset_t regs;
00489   struct iovec iovec;
00490 
00491   tid = get_thread_id (inferior_ptid);
00492 
00493   iovec.iov_base = &regs;
00494   iovec.iov_len = sizeof (regs);
00495 
00496   ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec);
00497   if (ret < 0)
00498     perror_with_name (_("Unable to fetch general registers."));
00499 
00500   for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++)
00501     if (REG_VALID == regcache_register_status (regcache, regno))
00502       regcache_raw_collect (regcache, regno,
00503                             (char *) &regs[regno - AARCH64_X0_REGNUM]);
00504 
00505   ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec);
00506   if (ret < 0)
00507     perror_with_name (_("Unable to store general registers."));
00508 }
00509 
00510 /* Fill GDB's register array with the fp/simd register values
00511    from the current thread.  */
00512 
00513 static void
00514 fetch_fpregs_from_thread (struct regcache *regcache)
00515 {
00516   int ret, regno, tid;
00517   elf_fpregset_t regs;
00518   struct iovec iovec;
00519 
00520   tid = get_thread_id (inferior_ptid);
00521 
00522   iovec.iov_base = &regs;
00523   iovec.iov_len = sizeof (regs);
00524 
00525   ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
00526   if (ret < 0)
00527     perror_with_name (_("Unable to fetch FP/SIMD registers."));
00528 
00529   for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
00530     regcache_raw_supply (regcache, regno,
00531                          (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);
00532 
00533   regcache_raw_supply (regcache, AARCH64_FPSR_REGNUM, (char *) &regs.fpsr);
00534   regcache_raw_supply (regcache, AARCH64_FPCR_REGNUM, (char *) &regs.fpcr);
00535 }
00536 
00537 /* Store to the current thread the valid fp/simd register
00538    values in the GDB's register array.  */
00539 
00540 static void
00541 store_fpregs_to_thread (const struct regcache *regcache)
00542 {
00543   int ret, regno, tid;
00544   elf_fpregset_t regs;
00545   struct iovec iovec;
00546 
00547   tid = get_thread_id (inferior_ptid);
00548 
00549   iovec.iov_base = &regs;
00550   iovec.iov_len = sizeof (regs);
00551 
00552   ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec);
00553   if (ret < 0)
00554     perror_with_name (_("Unable to fetch FP/SIMD registers."));
00555 
00556   for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++)
00557     if (REG_VALID == regcache_register_status (regcache, regno))
00558       regcache_raw_collect (regcache, regno,
00559                             (char *) &regs.vregs[regno - AARCH64_V0_REGNUM]);
00560 
00561   if (REG_VALID == regcache_register_status (regcache, AARCH64_FPSR_REGNUM))
00562     regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM, (char *) &regs.fpsr);
00563   if (REG_VALID == regcache_register_status (regcache, AARCH64_FPCR_REGNUM))
00564     regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM, (char *) &regs.fpcr);
00565 
00566   ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec);
00567   if (ret < 0)
00568     perror_with_name (_("Unable to store FP/SIMD registers."));
00569 }
00570 
00571 /* Implement the "to_fetch_register" target_ops method.  */
00572 
00573 static void
00574 aarch64_linux_fetch_inferior_registers (struct target_ops *ops,
00575                                         struct regcache *regcache,
00576                                         int regno)
00577 {
00578   if (regno == -1)
00579     {
00580       fetch_gregs_from_thread (regcache);
00581       fetch_fpregs_from_thread (regcache);
00582     }
00583   else if (regno < AARCH64_V0_REGNUM)
00584     fetch_gregs_from_thread (regcache);
00585   else
00586     fetch_fpregs_from_thread (regcache);
00587 }
00588 
00589 /* Implement the "to_store_register" target_ops method.  */
00590 
00591 static void
00592 aarch64_linux_store_inferior_registers (struct target_ops *ops,
00593                                         struct regcache *regcache,
00594                                         int regno)
00595 {
00596   if (regno == -1)
00597     {
00598       store_gregs_to_thread (regcache);
00599       store_fpregs_to_thread (regcache);
00600     }
00601   else if (regno < AARCH64_V0_REGNUM)
00602     store_gregs_to_thread (regcache);
00603   else
00604     store_fpregs_to_thread (regcache);
00605 }
00606 
00607 /* Fill register REGNO (if it is a general-purpose register) in
00608    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
00609    do this for all registers.  */
00610 
00611 void
00612 fill_gregset (const struct regcache *regcache,
00613               gdb_gregset_t *gregsetp, int regno)
00614 {
00615   gdb_byte *gregs_buf = (gdb_byte *) gregsetp;
00616   int i;
00617 
00618   for (i = AARCH64_X0_REGNUM; i <= AARCH64_CPSR_REGNUM; i++)
00619     if (regno == -1 || regno == i)
00620       regcache_raw_collect (regcache, i,
00621                             gregs_buf + X_REGISTER_SIZE
00622                             * (i - AARCH64_X0_REGNUM));
00623 }
00624 
00625 /* Fill GDB's register array with the general-purpose register values
00626    in *GREGSETP.  */
00627 
00628 void
00629 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
00630 {
00631   aarch64_linux_supply_gregset (regcache, (const gdb_byte *) gregsetp);
00632 }
00633 
00634 /* Fill register REGNO (if it is a floating-point register) in
00635    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
00636    do this for all registers.  */
00637 
00638 void
00639 fill_fpregset (const struct regcache *regcache,
00640                gdb_fpregset_t *fpregsetp, int regno)
00641 {
00642   gdb_byte *fpregs_buf = (gdb_byte *) fpregsetp;
00643   int i;
00644 
00645   for (i = AARCH64_V0_REGNUM; i <= AARCH64_V31_REGNUM; i++)
00646     if (regno == -1 || regno == i)
00647       regcache_raw_collect (regcache, i,
00648                             fpregs_buf + V_REGISTER_SIZE
00649                             * (i - AARCH64_V0_REGNUM));
00650 
00651   if (regno == -1 || regno == AARCH64_FPSR_REGNUM)
00652     regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM,
00653                           fpregs_buf + V_REGISTER_SIZE * 32);
00654 
00655   if (regno == -1 || regno == AARCH64_FPCR_REGNUM)
00656     regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM,
00657                           fpregs_buf + V_REGISTER_SIZE * 32 + 4);
00658 }
00659 
00660 /* Fill GDB's register array with the floating-point register values
00661    in *FPREGSETP.  */
00662 
00663 void
00664 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
00665 {
00666   aarch64_linux_supply_fpregset (regcache, (const gdb_byte *) fpregsetp);
00667 }
00668 
00669 /* Called when resuming a thread.
00670    The hardware debug registers are updated when there is any change.  */
00671 
00672 static void
00673 aarch64_linux_prepare_to_resume (struct lwp_info *lwp)
00674 {
00675   struct arch_lwp_info *info = lwp->arch_private;
00676 
00677   /* NULL means this is the main thread still going through the shell,
00678      or, no watchpoint has been set yet.  In that case, there's
00679      nothing to do.  */
00680   if (info == NULL)
00681     return;
00682 
00683   if (DR_HAS_CHANGED (info->dr_changed_bp)
00684       || DR_HAS_CHANGED (info->dr_changed_wp))
00685     {
00686       int tid = ptid_get_lwp (lwp->ptid);
00687       struct aarch64_debug_reg_state *state
00688         = aarch64_get_debug_reg_state (ptid_get_pid (lwp->ptid));
00689 
00690       if (debug_hw_points)
00691         fprintf_unfiltered (gdb_stdlog, "prepare_to_resume thread %d\n", tid);
00692 
00693       /* Watchpoints.  */
00694       if (DR_HAS_CHANGED (info->dr_changed_wp))
00695         {
00696           aarch64_linux_set_debug_regs (state, tid, 1);
00697           DR_CLEAR_CHANGED (info->dr_changed_wp);
00698         }
00699 
00700       /* Breakpoints.  */
00701       if (DR_HAS_CHANGED (info->dr_changed_bp))
00702         {
00703           aarch64_linux_set_debug_regs (state, tid, 0);
00704           DR_CLEAR_CHANGED (info->dr_changed_bp);
00705         }
00706     }
00707 }
00708 
00709 static void
00710 aarch64_linux_new_thread (struct lwp_info *lp)
00711 {
00712   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
00713 
00714   /* Mark that all the hardware breakpoint/watchpoint register pairs
00715      for this thread need to be initialized.  */
00716   DR_MARK_ALL_CHANGED (info->dr_changed_bp, aarch64_num_bp_regs);
00717   DR_MARK_ALL_CHANGED (info->dr_changed_wp, aarch64_num_wp_regs);
00718 
00719   lp->arch_private = info;
00720 }
00721 
00722 /* linux_nat_new_fork hook.   */
00723 
00724 static void
00725 aarch64_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
00726 {
00727   pid_t parent_pid;
00728   struct aarch64_debug_reg_state *parent_state;
00729   struct aarch64_debug_reg_state *child_state;
00730 
00731   /* NULL means no watchpoint has ever been set in the parent.  In
00732      that case, there's nothing to do.  */
00733   if (parent->arch_private == NULL)
00734     return;
00735 
00736   /* GDB core assumes the child inherits the watchpoints/hw
00737      breakpoints of the parent, and will remove them all from the
00738      forked off process.  Copy the debug registers mirrors into the
00739      new process so that all breakpoints and watchpoints can be
00740      removed together.  */
00741 
00742   parent_pid = ptid_get_pid (parent->ptid);
00743   parent_state = aarch64_get_debug_reg_state (parent_pid);
00744   child_state = aarch64_get_debug_reg_state (child_pid);
00745   *child_state = *parent_state;
00746 }
00747 
00748 
00749 /* Called by libthread_db.  Returns a pointer to the thread local
00750    storage (or its descriptor).  */
00751 
00752 ps_err_e
00753 ps_get_thread_area (const struct ps_prochandle *ph,
00754                     lwpid_t lwpid, int idx, void **base)
00755 {
00756   struct iovec iovec;
00757   uint64_t reg;
00758 
00759   iovec.iov_base = &reg;
00760   iovec.iov_len = sizeof (reg);
00761 
00762   if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0)
00763     return PS_ERR;
00764 
00765   /* IDX is the bias from the thread pointer to the beginning of the
00766      thread descriptor.  It has to be subtracted due to implementation
00767      quirks in libthread_db.  */
00768   *base = (void *) (reg - idx);
00769 
00770   return PS_OK;
00771 }
00772 
00773 
00774 /* Get the hardware debug register capacity information.  */
00775 
00776 static void
00777 aarch64_linux_get_debug_reg_capacity (void)
00778 {
00779   int tid;
00780   struct iovec iov;
00781   struct user_hwdebug_state dreg_state;
00782 
00783   tid = get_thread_id (inferior_ptid);
00784   iov.iov_base = &dreg_state;
00785   iov.iov_len = sizeof (dreg_state);
00786 
00787   /* Get hardware watchpoint register info.  */
00788   if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_HW_WATCH, &iov) == 0
00789       && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8)
00790     {
00791       aarch64_num_wp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info);
00792       if (aarch64_num_wp_regs > AARCH64_HWP_MAX_NUM)
00793         {
00794           warning (_("Unexpected number of hardware watchpoint registers"
00795                      " reported by ptrace, got %d, expected %d."),
00796                    aarch64_num_wp_regs, AARCH64_HWP_MAX_NUM);
00797           aarch64_num_wp_regs = AARCH64_HWP_MAX_NUM;
00798         }
00799     }
00800   else
00801     {
00802       warning (_("Unable to determine the number of hardware watchpoints"
00803                  " available."));
00804       aarch64_num_wp_regs = 0;
00805     }
00806 
00807   /* Get hardware breakpoint register info.  */
00808   if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_HW_BREAK, &iov) == 0
00809       && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8)
00810     {
00811       aarch64_num_bp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info);
00812       if (aarch64_num_bp_regs > AARCH64_HBP_MAX_NUM)
00813         {
00814           warning (_("Unexpected number of hardware breakpoint registers"
00815                      " reported by ptrace, got %d, expected %d."),
00816                    aarch64_num_bp_regs, AARCH64_HBP_MAX_NUM);
00817           aarch64_num_bp_regs = AARCH64_HBP_MAX_NUM;
00818         }
00819     }
00820   else
00821     {
00822       warning (_("Unable to determine the number of hardware breakpoints"
00823                  " available."));
00824       aarch64_num_bp_regs = 0;
00825     }
00826 }
00827 
00828 static void (*super_post_startup_inferior) (ptid_t ptid);
00829 
00830 /* Implement the "to_post_startup_inferior" target_ops method.  */
00831 
00832 static void
00833 aarch64_linux_child_post_startup_inferior (ptid_t ptid)
00834 {
00835   aarch64_forget_process (ptid_get_pid (ptid));
00836   aarch64_linux_get_debug_reg_capacity ();
00837   super_post_startup_inferior (ptid);
00838 }
00839 
00840 /* Implement the "to_read_description" target_ops method.  */
00841 
00842 static const struct target_desc *
00843 aarch64_linux_read_description (struct target_ops *ops)
00844 {
00845   initialize_tdesc_aarch64 ();
00846   return tdesc_aarch64;
00847 }
00848 
00849 /* Given the (potentially unaligned) watchpoint address in ADDR and
00850    length in LEN, return the aligned address and aligned length in
00851    *ALIGNED_ADDR_P and *ALIGNED_LEN_P, respectively.  The returned
00852    aligned address and length will be valid values to write to the
00853    hardware watchpoint value and control registers.
00854 
00855    The given watchpoint may get truncated if more than one hardware
00856    register is needed to cover the watched region.  *NEXT_ADDR_P
00857    and *NEXT_LEN_P, if non-NULL, will return the address and length
00858    of the remaining part of the watchpoint (which can be processed
00859    by calling this routine again to generate another aligned address
00860    and length pair.
00861 
00862    See the comment above the function of the same name in
00863    gdbserver/linux-aarch64-low.c for more information.  */
00864 
00865 static void
00866 aarch64_align_watchpoint (CORE_ADDR addr, int len, CORE_ADDR *aligned_addr_p,
00867                           int *aligned_len_p, CORE_ADDR *next_addr_p,
00868                           int *next_len_p)
00869 {
00870   int aligned_len;
00871   unsigned int offset;
00872   CORE_ADDR aligned_addr;
00873   const unsigned int alignment = AARCH64_HWP_ALIGNMENT;
00874   const unsigned int max_wp_len = AARCH64_HWP_MAX_LEN_PER_REG;
00875 
00876   /* As assumed by the algorithm.  */
00877   gdb_assert (alignment == max_wp_len);
00878 
00879   if (len <= 0)
00880     return;
00881 
00882   /* Address to be put into the hardware watchpoint value register
00883      must be aligned.  */
00884   offset = addr & (alignment - 1);
00885   aligned_addr = addr - offset;
00886 
00887   gdb_assert (offset >= 0 && offset < alignment);
00888   gdb_assert (aligned_addr >= 0 && aligned_addr <= addr);
00889   gdb_assert (offset + len > 0);
00890 
00891   if (offset + len >= max_wp_len)
00892     {
00893       /* Need more than one watchpoint registers; truncate it at the
00894          alignment boundary.  */
00895       aligned_len = max_wp_len;
00896       len -= (max_wp_len - offset);
00897       addr += (max_wp_len - offset);
00898       gdb_assert ((addr & (alignment - 1)) == 0);
00899     }
00900   else
00901     {
00902       /* Find the smallest valid length that is large enough to
00903          accommodate this watchpoint.  */
00904       static const unsigned char
00905         aligned_len_array[AARCH64_HWP_MAX_LEN_PER_REG] =
00906         { 1, 2, 4, 4, 8, 8, 8, 8 };
00907 
00908       aligned_len = aligned_len_array[offset + len - 1];
00909       addr += len;
00910       len = 0;
00911     }
00912 
00913   if (aligned_addr_p)
00914     *aligned_addr_p = aligned_addr;
00915   if (aligned_len_p)
00916     *aligned_len_p = aligned_len;
00917   if (next_addr_p)
00918     *next_addr_p = addr;
00919   if (next_len_p)
00920     *next_len_p = len;
00921 }
00922 
00923 /* Returns the number of hardware watchpoints of type TYPE that we can
00924    set.  Value is positive if we can set CNT watchpoints, zero if
00925    setting watchpoints of type TYPE is not supported, and negative if
00926    CNT is more than the maximum number of watchpoints of type TYPE
00927    that we can support.  TYPE is one of bp_hardware_watchpoint,
00928    bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
00929    CNT is the number of such watchpoints used so far (including this
00930    one).  OTHERTYPE is non-zero if other types of watchpoints are
00931    currently enabled.
00932 
00933    We always return 1 here because we don't have enough information
00934    about possible overlap of addresses that they want to watch.  As an
00935    extreme example, consider the case where all the watchpoints watch
00936    the same address and the same region length: then we can handle a
00937    virtually unlimited number of watchpoints, due to debug register
00938    sharing implemented via reference counts.  */
00939 
00940 static int
00941 aarch64_linux_can_use_hw_breakpoint (int type, int cnt, int othertype)
00942 {
00943   return 1;
00944 }
00945 
00946 /* ptrace expects control registers to be formatted as follows:
00947 
00948    31                             13          5      3      1     0
00949    +--------------------------------+----------+------+------+----+
00950    |         RESERVED (SBZ)         |  LENGTH  | TYPE | PRIV | EN |
00951    +--------------------------------+----------+------+------+----+
00952 
00953    The TYPE field is ignored for breakpoints.  */
00954 
00955 #define DR_CONTROL_ENABLED(ctrl)        (((ctrl) & 0x1) == 1)
00956 #define DR_CONTROL_LENGTH(ctrl)         (((ctrl) >> 5) & 0xff)
00957 
00958 /* Utility function that returns the length in bytes of a watchpoint
00959    according to the content of a hardware debug control register CTRL.
00960    Note that the kernel currently only supports the following Byte
00961    Address Select (BAS) values: 0x1, 0x3, 0xf and 0xff, which means
00962    that for a hardware watchpoint, its valid length can only be 1
00963    byte, 2 bytes, 4 bytes or 8 bytes.  */
00964 
00965 static inline unsigned int
00966 aarch64_watchpoint_length (unsigned int ctrl)
00967 {
00968   switch (DR_CONTROL_LENGTH (ctrl))
00969     {
00970     case 0x01:
00971       return 1;
00972     case 0x03:
00973       return 2;
00974     case 0x0f:
00975       return 4;
00976     case 0xff:
00977       return 8;
00978     default:
00979       return 0;
00980     }
00981 }
00982 
00983 /* Given the hardware breakpoint or watchpoint type TYPE and its
00984    length LEN, return the expected encoding for a hardware
00985    breakpoint/watchpoint control register.  */
00986 
00987 static unsigned int
00988 aarch64_point_encode_ctrl_reg (int type, int len)
00989 {
00990   unsigned int ctrl, ttype;
00991 
00992   /* type */
00993   switch (type)
00994     {
00995     case hw_write:
00996       ttype = 2;
00997       break;
00998     case hw_read:
00999       ttype = 1;
01000       break;
01001     case hw_access:
01002       ttype = 3;
01003       break;
01004     case hw_execute:
01005       ttype = 0;
01006       break;
01007     default:
01008       perror_with_name (_("Unrecognized breakpoint/watchpoint type"));
01009     }
01010   ctrl = ttype << 3;
01011 
01012   /* length bitmask */
01013   ctrl |= ((1 << len) - 1) << 5;
01014   /* enabled at el0 */
01015   ctrl |= (2 << 1) | 1;
01016 
01017   return ctrl;
01018 }
01019 
01020 /* Addresses to be written to the hardware breakpoint and watchpoint
01021    value registers need to be aligned; the alignment is 4-byte and
01022    8-type respectively.  Linux kernel rejects any non-aligned address
01023    it receives from the related ptrace call.  Furthermore, the kernel
01024    currently only supports the following Byte Address Select (BAS)
01025    values: 0x1, 0x3, 0xf and 0xff, which means that for a hardware
01026    watchpoint to be accepted by the kernel (via ptrace call), its
01027    valid length can only be 1 byte, 2 bytes, 4 bytes or 8 bytes.
01028    Despite these limitations, the unaligned watchpoint is supported in
01029    this port.
01030 
01031    Return 0 for any non-compliant ADDR and/or LEN; return 1 otherwise.  */
01032 
01033 static int
01034 aarch64_point_is_aligned (int is_watchpoint, CORE_ADDR addr, int len)
01035 {
01036   unsigned int alignment = is_watchpoint ? AARCH64_HWP_ALIGNMENT
01037     : AARCH64_HBP_ALIGNMENT;
01038 
01039   if (addr & (alignment - 1))
01040     return 0;
01041 
01042   if (len != 8 && len != 4 && len != 2 && len != 1)
01043     return 0;
01044 
01045   return 1;
01046 }
01047 
01048 /* Record the insertion of one breakpoint/watchpoint, as represented
01049    by ADDR and CTRL, in the cached debug register state area *STATE.  */
01050 
01051 static int
01052 aarch64_dr_state_insert_one_point (struct aarch64_debug_reg_state *state,
01053                                    int type, CORE_ADDR addr, int len)
01054 {
01055   int i, idx, num_regs, is_watchpoint;
01056   unsigned int ctrl, *dr_ctrl_p, *dr_ref_count;
01057   CORE_ADDR *dr_addr_p;
01058 
01059   /* Set up state pointers.  */
01060   is_watchpoint = (type != hw_execute);
01061   gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len));
01062   if (is_watchpoint)
01063     {
01064       num_regs = aarch64_num_wp_regs;
01065       dr_addr_p = state->dr_addr_wp;
01066       dr_ctrl_p = state->dr_ctrl_wp;
01067       dr_ref_count = state->dr_ref_count_wp;
01068     }
01069   else
01070     {
01071       num_regs = aarch64_num_bp_regs;
01072       dr_addr_p = state->dr_addr_bp;
01073       dr_ctrl_p = state->dr_ctrl_bp;
01074       dr_ref_count = state->dr_ref_count_bp;
01075     }
01076 
01077   ctrl = aarch64_point_encode_ctrl_reg (type, len);
01078 
01079   /* Find an existing or free register in our cache.  */
01080   idx = -1;
01081   for (i = 0; i < num_regs; ++i)
01082     {
01083       if ((dr_ctrl_p[i] & 1) == 0)
01084         {
01085           gdb_assert (dr_ref_count[i] == 0);
01086           idx = i;
01087           /* no break; continue hunting for an existing one.  */
01088         }
01089       else if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl)
01090         {
01091           gdb_assert (dr_ref_count[i] != 0);
01092           idx = i;
01093           break;
01094         }
01095     }
01096 
01097   /* No space.  */
01098   if (idx == -1)
01099     return -1;
01100 
01101   /* Update our cache.  */
01102   if ((dr_ctrl_p[idx] & 1) == 0)
01103     {
01104       /* new entry */
01105       dr_addr_p[idx] = addr;
01106       dr_ctrl_p[idx] = ctrl;
01107       dr_ref_count[idx] = 1;
01108       /* Notify the change.  */
01109       aarch64_notify_debug_reg_change (state, is_watchpoint, idx);
01110     }
01111   else
01112     {
01113       /* existing entry */
01114       dr_ref_count[idx]++;
01115     }
01116 
01117   return 0;
01118 }
01119 
01120 /* Record the removal of one breakpoint/watchpoint, as represented by
01121    ADDR and CTRL, in the cached debug register state area *STATE.  */
01122 
01123 static int
01124 aarch64_dr_state_remove_one_point (struct aarch64_debug_reg_state *state,
01125                                    int type, CORE_ADDR addr, int len)
01126 {
01127   int i, num_regs, is_watchpoint;
01128   unsigned int ctrl, *dr_ctrl_p, *dr_ref_count;
01129   CORE_ADDR *dr_addr_p;
01130 
01131   /* Set up state pointers.  */
01132   is_watchpoint = (type != hw_execute);
01133   gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len));
01134   if (is_watchpoint)
01135     {
01136       num_regs = aarch64_num_wp_regs;
01137       dr_addr_p = state->dr_addr_wp;
01138       dr_ctrl_p = state->dr_ctrl_wp;
01139       dr_ref_count = state->dr_ref_count_wp;
01140     }
01141   else
01142     {
01143       num_regs = aarch64_num_bp_regs;
01144       dr_addr_p = state->dr_addr_bp;
01145       dr_ctrl_p = state->dr_ctrl_bp;
01146       dr_ref_count = state->dr_ref_count_bp;
01147     }
01148 
01149   ctrl = aarch64_point_encode_ctrl_reg (type, len);
01150 
01151   /* Find the entry that matches the ADDR and CTRL.  */
01152   for (i = 0; i < num_regs; ++i)
01153     if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl)
01154       {
01155         gdb_assert (dr_ref_count[i] != 0);
01156         break;
01157       }
01158 
01159   /* Not found.  */
01160   if (i == num_regs)
01161     return -1;
01162 
01163   /* Clear our cache.  */
01164   if (--dr_ref_count[i] == 0)
01165     {
01166       /* Clear the enable bit.  */
01167       ctrl &= ~1;
01168       dr_addr_p[i] = 0;
01169       dr_ctrl_p[i] = ctrl;
01170       /* Notify the change.  */
01171       aarch64_notify_debug_reg_change (state, is_watchpoint, i);
01172     }
01173 
01174   return 0;
01175 }
01176 
01177 /* Implement insertion and removal of a single breakpoint.  */
01178 
01179 static int
01180 aarch64_handle_breakpoint (int type, CORE_ADDR addr, int len, int is_insert)
01181 {
01182   struct aarch64_debug_reg_state *state;
01183 
01184   /* The hardware breakpoint on AArch64 should always be 4-byte
01185      aligned.  */
01186   if (!aarch64_point_is_aligned (0 /* is_watchpoint */ , addr, len))
01187     return -1;
01188 
01189   state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
01190 
01191   if (is_insert)
01192     return aarch64_dr_state_insert_one_point (state, type, addr, len);
01193   else
01194     return aarch64_dr_state_remove_one_point (state, type, addr, len);
01195 }
01196 
01197 /* Insert a hardware-assisted breakpoint at BP_TGT->placed_address.
01198    Return 0 on success, -1 on failure.  */
01199 
01200 static int
01201 aarch64_linux_insert_hw_breakpoint (struct gdbarch *gdbarch,
01202                                     struct bp_target_info *bp_tgt)
01203 {
01204   int ret;
01205   CORE_ADDR addr = bp_tgt->placed_address;
01206   const int len = 4;
01207   const int type = hw_execute;
01208 
01209   if (debug_hw_points)
01210     fprintf_unfiltered
01211       (gdb_stdlog,
01212        "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
01213        (unsigned long) addr, len);
01214 
01215   ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */);
01216 
01217   if (debug_hw_points > 1)
01218     {
01219       struct aarch64_debug_reg_state *state
01220         = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
01221 
01222       aarch64_show_debug_reg_state (state,
01223                                     "insert_hw_watchpoint", addr, len, type);
01224     }
01225 
01226   return ret;
01227 }
01228 
01229 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
01230    Return 0 on success, -1 on failure.  */
01231 
01232 static int
01233 aarch64_linux_remove_hw_breakpoint (struct gdbarch *gdbarch,
01234                                     struct bp_target_info *bp_tgt)
01235 {
01236   int ret;
01237   CORE_ADDR addr = bp_tgt->placed_address;
01238   const int len = 4;
01239   const int type = hw_execute;
01240 
01241   if (debug_hw_points)
01242     fprintf_unfiltered
01243       (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
01244        (unsigned long) addr, len);
01245 
01246   ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */);
01247 
01248   if (debug_hw_points > 1)
01249     {
01250       struct aarch64_debug_reg_state *state
01251         = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
01252 
01253       aarch64_show_debug_reg_state (state,
01254                                     "remove_hw_watchpoint", addr, len, type);
01255     }
01256 
01257   return ret;
01258 }
01259 
01260 /* This is essentially the same as aarch64_handle_breakpoint, apart
01261    from that it is an aligned watchpoint to be handled.  */
01262 
01263 static int
01264 aarch64_handle_aligned_watchpoint (int type, CORE_ADDR addr, int len,
01265                                    int is_insert)
01266 {
01267   struct aarch64_debug_reg_state *state
01268     = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
01269 
01270   if (is_insert)
01271     return aarch64_dr_state_insert_one_point (state, type, addr, len);
01272   else
01273     return aarch64_dr_state_remove_one_point (state, type, addr, len);
01274 }
01275 
01276 /* Insert/remove unaligned watchpoint by calling
01277    aarch64_align_watchpoint repeatedly until the whole watched region,
01278    as represented by ADDR and LEN, has been properly aligned and ready
01279    to be written to one or more hardware watchpoint registers.
01280    IS_INSERT indicates whether this is an insertion or a deletion.
01281    Return 0 if succeed.  */
01282 
01283 static int
01284 aarch64_handle_unaligned_watchpoint (int type, CORE_ADDR addr, int len,
01285                                      int is_insert)
01286 {
01287   struct aarch64_debug_reg_state *state
01288     = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
01289 
01290   while (len > 0)
01291     {
01292       CORE_ADDR aligned_addr;
01293       int aligned_len, ret;
01294 
01295       aarch64_align_watchpoint (addr, len, &aligned_addr, &aligned_len,
01296                                 &addr, &len);
01297 
01298       if (is_insert)
01299         ret = aarch64_dr_state_insert_one_point (state, type, aligned_addr,
01300                                                  aligned_len);
01301       else
01302         ret = aarch64_dr_state_remove_one_point (state, type, aligned_addr,
01303                                                  aligned_len);
01304 
01305       if (debug_hw_points)
01306         fprintf_unfiltered (gdb_stdlog,
01307 "handle_unaligned_watchpoint: is_insert: %d\n"
01308 "                             aligned_addr: 0x%08lx, aligned_len: %d\n"
01309 "                                next_addr: 0x%08lx,    next_len: %d\n",
01310                  is_insert, aligned_addr, aligned_len, addr, len);
01311 
01312       if (ret != 0)
01313         return ret;
01314     }
01315 
01316   return 0;
01317 }
01318 
01319 /* Implements insertion and removal of a single watchpoint.  */
01320 
01321 static int
01322 aarch64_handle_watchpoint (int type, CORE_ADDR addr, int len, int is_insert)
01323 {
01324   if (aarch64_point_is_aligned (1 /* is_watchpoint */ , addr, len))
01325     return aarch64_handle_aligned_watchpoint (type, addr, len, is_insert);
01326   else
01327     return aarch64_handle_unaligned_watchpoint (type, addr, len, is_insert);
01328 }
01329 
01330 /* Implement the "to_insert_watchpoint" target_ops method.
01331 
01332    Insert a watchpoint to watch a memory region which starts at
01333    address ADDR and whose length is LEN bytes.  Watch memory accesses
01334    of the type TYPE.  Return 0 on success, -1 on failure.  */
01335 
01336 static int
01337 aarch64_linux_insert_watchpoint (CORE_ADDR addr, int len, int type,
01338                                  struct expression *cond)
01339 {
01340   int ret;
01341 
01342   if (debug_hw_points)
01343     fprintf_unfiltered (gdb_stdlog,
01344                         "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
01345                         (unsigned long) addr, len);
01346 
01347   gdb_assert (type != hw_execute);
01348 
01349   ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */);
01350 
01351   if (debug_hw_points > 1)
01352     {
01353       struct aarch64_debug_reg_state *state
01354         = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
01355 
01356       aarch64_show_debug_reg_state (state,
01357                                     "insert_watchpoint", addr, len, type);
01358     }
01359 
01360   return ret;
01361 }
01362 
01363 /* Implement the "to_remove_watchpoint" target_ops method.
01364    Remove a watchpoint that watched the memory region which starts at
01365    address ADDR, whose length is LEN bytes, and for accesses of the
01366    type TYPE.  Return 0 on success, -1 on failure.  */
01367 
01368 static int
01369 aarch64_linux_remove_watchpoint (CORE_ADDR addr, int len, int type,
01370                                  struct expression *cond)
01371 {
01372   int ret;
01373 
01374   if (debug_hw_points)
01375     fprintf_unfiltered (gdb_stdlog,
01376                         "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
01377                         (unsigned long) addr, len);
01378 
01379   gdb_assert (type != hw_execute);
01380 
01381   ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */);
01382 
01383   if (debug_hw_points > 1)
01384     {
01385       struct aarch64_debug_reg_state *state
01386         = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
01387 
01388       aarch64_show_debug_reg_state (state,
01389                                     "remove_watchpoint", addr, len, type);
01390     }
01391 
01392   return ret;
01393 }
01394 
01395 /* Implement the "to_region_ok_for_hw_watchpoint" target_ops method.  */
01396 
01397 static int
01398 aarch64_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
01399 {
01400   CORE_ADDR aligned_addr;
01401 
01402   /* Can not set watchpoints for zero or negative lengths.  */
01403   if (len <= 0)
01404     return 0;
01405 
01406   /* Must have hardware watchpoint debug register(s).  */
01407   if (aarch64_num_wp_regs == 0)
01408     return 0;
01409 
01410   /* We support unaligned watchpoint address and arbitrary length,
01411      as long as the size of the whole watched area after alignment
01412      doesn't exceed size of the total area that all watchpoint debug
01413      registers can watch cooperatively.
01414 
01415      This is a very relaxed rule, but unfortunately there are
01416      limitations, e.g. false-positive hits, due to limited support of
01417      hardware debug registers in the kernel.  See comment above
01418      aarch64_align_watchpoint for more information.  */
01419 
01420   aligned_addr = addr & ~(AARCH64_HWP_MAX_LEN_PER_REG - 1);
01421   if (aligned_addr + aarch64_num_wp_regs * AARCH64_HWP_MAX_LEN_PER_REG
01422       < addr + len)
01423     return 0;
01424 
01425   /* All tests passed so we are likely to be able to set the watchpoint.
01426      The reason that it is 'likely' rather than 'must' is because
01427      we don't check the current usage of the watchpoint registers, and
01428      there may not be enough registers available for this watchpoint.
01429      Ideally we should check the cached debug register state, however
01430      the checking is costly.  */
01431   return 1;
01432 }
01433 
01434 /* Implement the "to_stopped_data_address" target_ops method.  */
01435 
01436 static int
01437 aarch64_linux_stopped_data_address (struct target_ops *target,
01438                                     CORE_ADDR *addr_p)
01439 {
01440   siginfo_t siginfo;
01441   int i, tid;
01442   struct aarch64_debug_reg_state *state;
01443 
01444   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
01445     return 0;
01446 
01447   /* This must be a hardware breakpoint.  */
01448   if (siginfo.si_signo != SIGTRAP
01449       || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
01450     return 0;
01451 
01452   /* Check if the address matches any watched address.  */
01453   state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid));
01454   for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
01455     {
01456       const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
01457       const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
01458       const CORE_ADDR addr_watch = state->dr_addr_wp[i];
01459 
01460       if (state->dr_ref_count_wp[i]
01461           && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
01462           && addr_trap >= addr_watch
01463           && addr_trap < addr_watch + len)
01464         {
01465           *addr_p = addr_trap;
01466           return 1;
01467         }
01468     }
01469 
01470   return 0;
01471 }
01472 
01473 /* Implement the "to_stopped_by_watchpoint" target_ops method.  */
01474 
01475 static int
01476 aarch64_linux_stopped_by_watchpoint (void)
01477 {
01478   CORE_ADDR addr;
01479 
01480   return aarch64_linux_stopped_data_address (&current_target, &addr);
01481 }
01482 
01483 /* Implement the "to_watchpoint_addr_within_range" target_ops method.  */
01484 
01485 static int
01486 aarch64_linux_watchpoint_addr_within_range (struct target_ops *target,
01487                                             CORE_ADDR addr,
01488                                             CORE_ADDR start, int length)
01489 {
01490   return start <= addr && start + length - 1 >= addr;
01491 }
01492 
01493 /* Define AArch64 maintenance commands.  */
01494 
01495 static void
01496 add_show_debug_regs_command (void)
01497 {
01498   /* A maintenance command to enable printing the internal DRi mirror
01499      variables.  */
01500   add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
01501                            &debug_hw_points, _("\
01502 Set whether to show variables that mirror the AArch64 debug registers."), _("\
01503 Show whether to show variables that mirror the AArch64 debug registers."), _("\
01504 Use \"on\" to enable, \"off\" to disable.\n\
01505 If enabled, the debug registers values are shown when GDB inserts\n\
01506 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
01507 triggers a breakpoint or watchpoint."),
01508                            NULL,
01509                            NULL,
01510                            &maintenance_set_cmdlist,
01511                            &maintenance_show_cmdlist);
01512 }
01513 
01514 /* -Wmissing-prototypes.  */
01515 void _initialize_aarch64_linux_nat (void);
01516 
01517 void
01518 _initialize_aarch64_linux_nat (void)
01519 {
01520   struct target_ops *t;
01521 
01522   /* Fill in the generic GNU/Linux methods.  */
01523   t = linux_target ();
01524 
01525   add_show_debug_regs_command ();
01526 
01527   /* Add our register access methods.  */
01528   t->to_fetch_registers = aarch64_linux_fetch_inferior_registers;
01529   t->to_store_registers = aarch64_linux_store_inferior_registers;
01530 
01531   t->to_read_description = aarch64_linux_read_description;
01532 
01533   t->to_can_use_hw_breakpoint = aarch64_linux_can_use_hw_breakpoint;
01534   t->to_insert_hw_breakpoint = aarch64_linux_insert_hw_breakpoint;
01535   t->to_remove_hw_breakpoint = aarch64_linux_remove_hw_breakpoint;
01536   t->to_region_ok_for_hw_watchpoint =
01537     aarch64_linux_region_ok_for_hw_watchpoint;
01538   t->to_insert_watchpoint = aarch64_linux_insert_watchpoint;
01539   t->to_remove_watchpoint = aarch64_linux_remove_watchpoint;
01540   t->to_stopped_by_watchpoint = aarch64_linux_stopped_by_watchpoint;
01541   t->to_stopped_data_address = aarch64_linux_stopped_data_address;
01542   t->to_watchpoint_addr_within_range =
01543     aarch64_linux_watchpoint_addr_within_range;
01544 
01545   /* Override the GNU/Linux inferior startup hook.  */
01546   super_post_startup_inferior = t->to_post_startup_inferior;
01547   t->to_post_startup_inferior = aarch64_linux_child_post_startup_inferior;
01548 
01549   /* Register the target.  */
01550   linux_nat_add_target (t);
01551   linux_nat_set_new_thread (t, aarch64_linux_new_thread);
01552   linux_nat_set_new_fork (t, aarch64_linux_new_fork);
01553   linux_nat_set_forget_process (t, aarch64_forget_process);
01554   linux_nat_set_prepare_to_resume (t, aarch64_linux_prepare_to_resume);
01555 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines