Home | History | Annotate | Line # | Download | only in gdb
arm-linux-nat.c revision 1.9
      1  1.1  christos /* GNU/Linux on ARM native support.
      2  1.9  christos    Copyright (C) 1999-2020 Free Software Foundation, Inc.
      3  1.1  christos 
      4  1.1  christos    This file is part of GDB.
      5  1.1  christos 
      6  1.1  christos    This program is free software; you can redistribute it and/or modify
      7  1.1  christos    it under the terms of the GNU General Public License as published by
      8  1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9  1.1  christos    (at your option) any later version.
     10  1.1  christos 
     11  1.1  christos    This program is distributed in the hope that it will be useful,
     12  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  1.1  christos    GNU General Public License for more details.
     15  1.1  christos 
     16  1.1  christos    You should have received a copy of the GNU General Public License
     17  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18  1.1  christos 
     19  1.1  christos #include "defs.h"
     20  1.1  christos #include "inferior.h"
     21  1.1  christos #include "gdbcore.h"
     22  1.1  christos #include "regcache.h"
     23  1.1  christos #include "target.h"
     24  1.1  christos #include "linux-nat.h"
     25  1.1  christos #include "target-descriptions.h"
     26  1.1  christos #include "auxv.h"
     27  1.8  christos #include "observable.h"
     28  1.1  christos #include "gdbthread.h"
     29  1.1  christos 
     30  1.9  christos #include "aarch32-tdep.h"
     31  1.1  christos #include "arm-tdep.h"
     32  1.1  christos #include "arm-linux-tdep.h"
     33  1.6  christos #include "aarch32-linux-nat.h"
     34  1.1  christos 
     35  1.1  christos #include <elf/common.h>
     36  1.1  christos #include <sys/user.h>
     37  1.6  christos #include "nat/gdb_ptrace.h"
     38  1.1  christos #include <sys/utsname.h>
     39  1.1  christos #include <sys/procfs.h>
     40  1.1  christos 
     41  1.5  christos #include "nat/linux-ptrace.h"
     42  1.9  christos #include "linux-tdep.h"
     43  1.5  christos 
     44  1.1  christos /* Prototypes for supply_gregset etc.  */
     45  1.1  christos #include "gregset.h"
     46  1.1  christos 
     47  1.1  christos /* Defines ps_err_e, struct ps_prochandle.  */
     48  1.1  christos #include "gdb_proc_service.h"
     49  1.1  christos 
     50  1.1  christos #ifndef PTRACE_GET_THREAD_AREA
     51  1.1  christos #define PTRACE_GET_THREAD_AREA 22
     52  1.1  christos #endif
     53  1.1  christos 
     54  1.1  christos #ifndef PTRACE_GETWMMXREGS
     55  1.1  christos #define PTRACE_GETWMMXREGS 18
     56  1.1  christos #define PTRACE_SETWMMXREGS 19
     57  1.1  christos #endif
     58  1.1  christos 
     59  1.1  christos #ifndef PTRACE_GETVFPREGS
     60  1.1  christos #define PTRACE_GETVFPREGS 27
     61  1.1  christos #define PTRACE_SETVFPREGS 28
     62  1.1  christos #endif
     63  1.1  christos 
     64  1.1  christos #ifndef PTRACE_GETHBPREGS
     65  1.1  christos #define PTRACE_GETHBPREGS 29
     66  1.1  christos #define PTRACE_SETHBPREGS 30
     67  1.1  christos #endif
     68  1.1  christos 
     69  1.8  christos class arm_linux_nat_target final : public linux_nat_target
     70  1.8  christos {
     71  1.8  christos public:
     72  1.8  christos   /* Add our register access methods.  */
     73  1.8  christos   void fetch_registers (struct regcache *, int) override;
     74  1.8  christos   void store_registers (struct regcache *, int) override;
     75  1.8  christos 
     76  1.8  christos   /* Add our hardware breakpoint and watchpoint implementation.  */
     77  1.8  christos   int can_use_hw_breakpoint (enum bptype, int, int) override;
     78  1.8  christos 
     79  1.8  christos   int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
     80  1.8  christos 
     81  1.8  christos   int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
     82  1.8  christos 
     83  1.8  christos   int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
     84  1.8  christos 
     85  1.8  christos   int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
     86  1.8  christos 			 struct expression *) override;
     87  1.8  christos 
     88  1.8  christos   int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
     89  1.8  christos 			 struct expression *) override;
     90  1.8  christos   bool stopped_by_watchpoint () override;
     91  1.8  christos 
     92  1.8  christos   bool stopped_data_address (CORE_ADDR *) override;
     93  1.8  christos 
     94  1.8  christos   bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
     95  1.8  christos 
     96  1.8  christos   const struct target_desc *read_description () override;
     97  1.8  christos 
     98  1.8  christos   /* Override linux_nat_target low methods.  */
     99  1.8  christos 
    100  1.8  christos   /* Handle thread creation and exit.  */
    101  1.8  christos   void low_new_thread (struct lwp_info *lp) override;
    102  1.8  christos   void low_delete_thread (struct arch_lwp_info *lp) override;
    103  1.8  christos   void low_prepare_to_resume (struct lwp_info *lp) override;
    104  1.8  christos 
    105  1.8  christos   /* Handle process creation and exit.  */
    106  1.8  christos   void low_new_fork (struct lwp_info *parent, pid_t child_pid) override;
    107  1.8  christos   void low_forget_process (pid_t pid) override;
    108  1.8  christos };
    109  1.8  christos 
    110  1.8  christos static arm_linux_nat_target the_arm_linux_nat_target;
    111  1.8  christos 
    112  1.1  christos /* Get the whole floating point state of the process and store it
    113  1.1  christos    into regcache.  */
    114  1.1  christos 
    115  1.1  christos static void
    116  1.1  christos fetch_fpregs (struct regcache *regcache)
    117  1.1  christos {
    118  1.1  christos   int ret, regno, tid;
    119  1.1  christos   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
    120  1.1  christos 
    121  1.1  christos   /* Get the thread id for the ptrace call.  */
    122  1.8  christos   tid = regcache->ptid ().lwp ();
    123  1.5  christos 
    124  1.1  christos   /* Read the floating point state.  */
    125  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    126  1.5  christos     {
    127  1.5  christos       struct iovec iov;
    128  1.5  christos 
    129  1.5  christos       iov.iov_base = &fp;
    130  1.5  christos       iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
    131  1.5  christos 
    132  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
    133  1.5  christos     }
    134  1.5  christos   else
    135  1.5  christos     ret = ptrace (PT_GETFPREGS, tid, 0, fp);
    136  1.5  christos 
    137  1.1  christos   if (ret < 0)
    138  1.6  christos     perror_with_name (_("Unable to fetch the floating point registers."));
    139  1.1  christos 
    140  1.1  christos   /* Fetch fpsr.  */
    141  1.8  christos   regcache->raw_supply (ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
    142  1.1  christos 
    143  1.1  christos   /* Fetch the floating point registers.  */
    144  1.1  christos   for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
    145  1.1  christos     supply_nwfpe_register (regcache, regno, fp);
    146  1.1  christos }
    147  1.1  christos 
    148  1.1  christos /* Save the whole floating point state of the process using
    149  1.1  christos    the contents from regcache.  */
    150  1.1  christos 
    151  1.1  christos static void
    152  1.1  christos store_fpregs (const struct regcache *regcache)
    153  1.1  christos {
    154  1.1  christos   int ret, regno, tid;
    155  1.1  christos   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
    156  1.1  christos 
    157  1.1  christos   /* Get the thread id for the ptrace call.  */
    158  1.8  christos   tid = regcache->ptid ().lwp ();
    159  1.5  christos 
    160  1.1  christos   /* Read the floating point state.  */
    161  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    162  1.5  christos     {
    163  1.5  christos       elf_fpregset_t fpregs;
    164  1.5  christos       struct iovec iov;
    165  1.5  christos 
    166  1.5  christos       iov.iov_base = &fpregs;
    167  1.5  christos       iov.iov_len = sizeof (fpregs);
    168  1.5  christos 
    169  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
    170  1.5  christos     }
    171  1.5  christos   else
    172  1.5  christos     ret = ptrace (PT_GETFPREGS, tid, 0, fp);
    173  1.5  christos 
    174  1.1  christos   if (ret < 0)
    175  1.6  christos     perror_with_name (_("Unable to fetch the floating point registers."));
    176  1.1  christos 
    177  1.1  christos   /* Store fpsr.  */
    178  1.8  christos   if (REG_VALID == regcache->get_register_status (ARM_FPS_REGNUM))
    179  1.8  christos     regcache->raw_collect (ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
    180  1.1  christos 
    181  1.1  christos   /* Store the floating point registers.  */
    182  1.1  christos   for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
    183  1.8  christos     if (REG_VALID == regcache->get_register_status (regno))
    184  1.1  christos       collect_nwfpe_register (regcache, regno, fp);
    185  1.1  christos 
    186  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    187  1.5  christos     {
    188  1.5  christos       struct iovec iov;
    189  1.5  christos 
    190  1.5  christos       iov.iov_base = &fp;
    191  1.5  christos       iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
    192  1.5  christos 
    193  1.5  christos       ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
    194  1.5  christos     }
    195  1.5  christos   else
    196  1.5  christos     ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
    197  1.5  christos 
    198  1.1  christos   if (ret < 0)
    199  1.6  christos     perror_with_name (_("Unable to store floating point registers."));
    200  1.1  christos }
    201  1.1  christos 
    202  1.1  christos /* Fetch all general registers of the process and store into
    203  1.1  christos    regcache.  */
    204  1.1  christos 
    205  1.1  christos static void
    206  1.1  christos fetch_regs (struct regcache *regcache)
    207  1.1  christos {
    208  1.8  christos   int ret, tid;
    209  1.1  christos   elf_gregset_t regs;
    210  1.1  christos 
    211  1.1  christos   /* Get the thread id for the ptrace call.  */
    212  1.8  christos   tid = regcache->ptid ().lwp ();
    213  1.5  christos 
    214  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    215  1.5  christos     {
    216  1.5  christos       struct iovec iov;
    217  1.5  christos 
    218  1.5  christos       iov.iov_base = &regs;
    219  1.5  christos       iov.iov_len = sizeof (regs);
    220  1.5  christos 
    221  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
    222  1.5  christos     }
    223  1.5  christos   else
    224  1.5  christos     ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
    225  1.5  christos 
    226  1.1  christos   if (ret < 0)
    227  1.6  christos     perror_with_name (_("Unable to fetch general registers."));
    228  1.5  christos 
    229  1.6  christos   aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, arm_apcs_32);
    230  1.1  christos }
    231  1.1  christos 
    232  1.1  christos static void
    233  1.1  christos store_regs (const struct regcache *regcache)
    234  1.1  christos {
    235  1.8  christos   int ret, tid;
    236  1.1  christos   elf_gregset_t regs;
    237  1.1  christos 
    238  1.1  christos   /* Get the thread id for the ptrace call.  */
    239  1.8  christos   tid = regcache->ptid ().lwp ();
    240  1.5  christos 
    241  1.1  christos   /* Fetch the general registers.  */
    242  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    243  1.5  christos     {
    244  1.5  christos       struct iovec iov;
    245  1.5  christos 
    246  1.5  christos       iov.iov_base = &regs;
    247  1.5  christos       iov.iov_len = sizeof (regs);
    248  1.5  christos 
    249  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
    250  1.5  christos     }
    251  1.5  christos   else
    252  1.5  christos     ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
    253  1.5  christos 
    254  1.1  christos   if (ret < 0)
    255  1.6  christos     perror_with_name (_("Unable to fetch general registers."));
    256  1.1  christos 
    257  1.6  christos   aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, arm_apcs_32);
    258  1.1  christos 
    259  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    260  1.5  christos     {
    261  1.5  christos       struct iovec iov;
    262  1.5  christos 
    263  1.5  christos       iov.iov_base = &regs;
    264  1.5  christos       iov.iov_len = sizeof (regs);
    265  1.5  christos 
    266  1.5  christos       ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
    267  1.5  christos     }
    268  1.5  christos   else
    269  1.5  christos     ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
    270  1.1  christos 
    271  1.1  christos   if (ret < 0)
    272  1.6  christos     perror_with_name (_("Unable to store general registers."));
    273  1.1  christos }
    274  1.1  christos 
    275  1.1  christos /* Fetch all WMMX registers of the process and store into
    276  1.1  christos    regcache.  */
    277  1.1  christos 
    278  1.1  christos static void
    279  1.1  christos fetch_wmmx_regs (struct regcache *regcache)
    280  1.1  christos {
    281  1.1  christos   char regbuf[IWMMXT_REGS_SIZE];
    282  1.1  christos   int ret, regno, tid;
    283  1.1  christos 
    284  1.1  christos   /* Get the thread id for the ptrace call.  */
    285  1.8  christos   tid = regcache->ptid ().lwp ();
    286  1.1  christos 
    287  1.1  christos   ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
    288  1.1  christos   if (ret < 0)
    289  1.6  christos     perror_with_name (_("Unable to fetch WMMX registers."));
    290  1.1  christos 
    291  1.1  christos   for (regno = 0; regno < 16; regno++)
    292  1.8  christos     regcache->raw_supply (regno + ARM_WR0_REGNUM, &regbuf[regno * 8]);
    293  1.1  christos 
    294  1.1  christos   for (regno = 0; regno < 2; regno++)
    295  1.8  christos     regcache->raw_supply (regno + ARM_WCSSF_REGNUM,
    296  1.8  christos 			  &regbuf[16 * 8 + regno * 4]);
    297  1.1  christos 
    298  1.1  christos   for (regno = 0; regno < 4; regno++)
    299  1.8  christos     regcache->raw_supply (regno + ARM_WCGR0_REGNUM,
    300  1.8  christos 			  &regbuf[16 * 8 + 2 * 4 + regno * 4]);
    301  1.1  christos }
    302  1.1  christos 
    303  1.1  christos static void
    304  1.1  christos store_wmmx_regs (const struct regcache *regcache)
    305  1.1  christos {
    306  1.1  christos   char regbuf[IWMMXT_REGS_SIZE];
    307  1.1  christos   int ret, regno, tid;
    308  1.1  christos 
    309  1.1  christos   /* Get the thread id for the ptrace call.  */
    310  1.8  christos   tid = regcache->ptid ().lwp ();
    311  1.1  christos 
    312  1.1  christos   ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
    313  1.1  christos   if (ret < 0)
    314  1.6  christos     perror_with_name (_("Unable to fetch WMMX registers."));
    315  1.1  christos 
    316  1.1  christos   for (regno = 0; regno < 16; regno++)
    317  1.8  christos     if (REG_VALID == regcache->get_register_status (regno + ARM_WR0_REGNUM))
    318  1.8  christos       regcache->raw_collect (regno + ARM_WR0_REGNUM, &regbuf[regno * 8]);
    319  1.1  christos 
    320  1.1  christos   for (regno = 0; regno < 2; regno++)
    321  1.8  christos     if (REG_VALID == regcache->get_register_status (regno + ARM_WCSSF_REGNUM))
    322  1.8  christos       regcache->raw_collect (regno + ARM_WCSSF_REGNUM,
    323  1.8  christos 			     &regbuf[16 * 8 + regno * 4]);
    324  1.1  christos 
    325  1.1  christos   for (regno = 0; regno < 4; regno++)
    326  1.8  christos     if (REG_VALID == regcache->get_register_status (regno + ARM_WCGR0_REGNUM))
    327  1.8  christos       regcache->raw_collect (regno + ARM_WCGR0_REGNUM,
    328  1.8  christos 			     &regbuf[16 * 8 + 2 * 4 + regno * 4]);
    329  1.1  christos 
    330  1.1  christos   ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
    331  1.1  christos 
    332  1.1  christos   if (ret < 0)
    333  1.6  christos     perror_with_name (_("Unable to store WMMX registers."));
    334  1.1  christos }
    335  1.1  christos 
    336  1.1  christos static void
    337  1.1  christos fetch_vfp_regs (struct regcache *regcache)
    338  1.1  christos {
    339  1.9  christos   gdb_byte regbuf[ARM_VFP3_REGS_SIZE];
    340  1.8  christos   int ret, tid;
    341  1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
    342  1.5  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    343  1.1  christos 
    344  1.1  christos   /* Get the thread id for the ptrace call.  */
    345  1.8  christos   tid = regcache->ptid ().lwp ();
    346  1.1  christos 
    347  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    348  1.5  christos     {
    349  1.5  christos       struct iovec iov;
    350  1.5  christos 
    351  1.5  christos       iov.iov_base = regbuf;
    352  1.9  christos       iov.iov_len = ARM_VFP3_REGS_SIZE;
    353  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
    354  1.5  christos     }
    355  1.5  christos   else
    356  1.5  christos     ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
    357  1.5  christos 
    358  1.1  christos   if (ret < 0)
    359  1.6  christos     perror_with_name (_("Unable to fetch VFP registers."));
    360  1.1  christos 
    361  1.6  christos   aarch32_vfp_regcache_supply (regcache, regbuf,
    362  1.6  christos 			       tdep->vfp_register_count);
    363  1.1  christos }
    364  1.1  christos 
    365  1.1  christos static void
    366  1.1  christos store_vfp_regs (const struct regcache *regcache)
    367  1.1  christos {
    368  1.9  christos   gdb_byte regbuf[ARM_VFP3_REGS_SIZE];
    369  1.8  christos   int ret, tid;
    370  1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
    371  1.5  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    372  1.1  christos 
    373  1.1  christos   /* Get the thread id for the ptrace call.  */
    374  1.8  christos   tid = regcache->ptid ().lwp ();
    375  1.1  christos 
    376  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    377  1.5  christos     {
    378  1.5  christos       struct iovec iov;
    379  1.5  christos 
    380  1.5  christos       iov.iov_base = regbuf;
    381  1.9  christos       iov.iov_len = ARM_VFP3_REGS_SIZE;
    382  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
    383  1.5  christos     }
    384  1.5  christos   else
    385  1.5  christos     ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
    386  1.5  christos 
    387  1.1  christos   if (ret < 0)
    388  1.6  christos     perror_with_name (_("Unable to fetch VFP registers (for update)."));
    389  1.1  christos 
    390  1.6  christos   aarch32_vfp_regcache_collect (regcache, regbuf,
    391  1.6  christos 				tdep->vfp_register_count);
    392  1.1  christos 
    393  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    394  1.5  christos     {
    395  1.5  christos       struct iovec iov;
    396  1.5  christos 
    397  1.5  christos       iov.iov_base = regbuf;
    398  1.9  christos       iov.iov_len = ARM_VFP3_REGS_SIZE;
    399  1.5  christos       ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iov);
    400  1.5  christos     }
    401  1.5  christos   else
    402  1.5  christos     ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
    403  1.1  christos 
    404  1.1  christos   if (ret < 0)
    405  1.6  christos     perror_with_name (_("Unable to store VFP registers."));
    406  1.1  christos }
    407  1.1  christos 
    408  1.1  christos /* Fetch registers from the child process.  Fetch all registers if
    409  1.1  christos    regno == -1, otherwise fetch all general registers or all floating
    410  1.1  christos    point registers depending upon the value of regno.  */
    411  1.1  christos 
    412  1.8  christos void
    413  1.8  christos arm_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
    414  1.1  christos {
    415  1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
    416  1.5  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    417  1.5  christos 
    418  1.1  christos   if (-1 == regno)
    419  1.1  christos     {
    420  1.1  christos       fetch_regs (regcache);
    421  1.5  christos       if (tdep->have_wmmx_registers)
    422  1.1  christos 	fetch_wmmx_regs (regcache);
    423  1.5  christos       if (tdep->vfp_register_count > 0)
    424  1.1  christos 	fetch_vfp_regs (regcache);
    425  1.7  christos       if (tdep->have_fpa_registers)
    426  1.7  christos 	fetch_fpregs (regcache);
    427  1.1  christos     }
    428  1.7  christos   else
    429  1.1  christos     {
    430  1.1  christos       if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
    431  1.6  christos 	fetch_regs (regcache);
    432  1.1  christos       else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
    433  1.6  christos 	fetch_fpregs (regcache);
    434  1.5  christos       else if (tdep->have_wmmx_registers
    435  1.1  christos 	       && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
    436  1.1  christos 	fetch_wmmx_regs (regcache);
    437  1.5  christos       else if (tdep->vfp_register_count > 0
    438  1.1  christos 	       && regno >= ARM_D0_REGNUM
    439  1.7  christos 	       && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
    440  1.7  christos 		   || regno == ARM_FPSCR_REGNUM))
    441  1.1  christos 	fetch_vfp_regs (regcache);
    442  1.1  christos     }
    443  1.1  christos }
    444  1.1  christos 
    445  1.1  christos /* Store registers back into the inferior.  Store all registers if
    446  1.1  christos    regno == -1, otherwise store all general registers or all floating
    447  1.1  christos    point registers depending upon the value of regno.  */
    448  1.1  christos 
    449  1.8  christos void
    450  1.8  christos arm_linux_nat_target::store_registers (struct regcache *regcache, int regno)
    451  1.1  christos {
    452  1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
    453  1.5  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    454  1.5  christos 
    455  1.1  christos   if (-1 == regno)
    456  1.1  christos     {
    457  1.1  christos       store_regs (regcache);
    458  1.5  christos       if (tdep->have_wmmx_registers)
    459  1.1  christos 	store_wmmx_regs (regcache);
    460  1.5  christos       if (tdep->vfp_register_count > 0)
    461  1.1  christos 	store_vfp_regs (regcache);
    462  1.7  christos       if (tdep->have_fpa_registers)
    463  1.7  christos 	store_fpregs (regcache);
    464  1.1  christos     }
    465  1.1  christos   else
    466  1.1  christos     {
    467  1.1  christos       if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
    468  1.6  christos 	store_regs (regcache);
    469  1.1  christos       else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
    470  1.6  christos 	store_fpregs (regcache);
    471  1.5  christos       else if (tdep->have_wmmx_registers
    472  1.1  christos 	       && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
    473  1.1  christos 	store_wmmx_regs (regcache);
    474  1.5  christos       else if (tdep->vfp_register_count > 0
    475  1.1  christos 	       && regno >= ARM_D0_REGNUM
    476  1.7  christos 	       && (regno < ARM_D0_REGNUM + tdep->vfp_register_count
    477  1.7  christos 		   || regno == ARM_FPSCR_REGNUM))
    478  1.1  christos 	store_vfp_regs (regcache);
    479  1.1  christos     }
    480  1.1  christos }
    481  1.1  christos 
    482  1.1  christos /* Wrapper functions for the standard regset handling, used by
    483  1.1  christos    thread debugging.  */
    484  1.1  christos 
    485  1.1  christos void
    486  1.1  christos fill_gregset (const struct regcache *regcache,
    487  1.1  christos 	      gdb_gregset_t *gregsetp, int regno)
    488  1.1  christos {
    489  1.1  christos   arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
    490  1.1  christos }
    491  1.1  christos 
    492  1.1  christos void
    493  1.1  christos supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
    494  1.1  christos {
    495  1.1  christos   arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
    496  1.1  christos }
    497  1.1  christos 
    498  1.1  christos void
    499  1.1  christos fill_fpregset (const struct regcache *regcache,
    500  1.1  christos 	       gdb_fpregset_t *fpregsetp, int regno)
    501  1.1  christos {
    502  1.1  christos   arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
    503  1.1  christos }
    504  1.1  christos 
    505  1.1  christos /* Fill GDB's register array with the floating-point register values
    506  1.1  christos    in *fpregsetp.  */
    507  1.1  christos 
    508  1.1  christos void
    509  1.1  christos supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
    510  1.1  christos {
    511  1.1  christos   arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
    512  1.1  christos }
    513  1.1  christos 
    514  1.1  christos /* Fetch the thread-local storage pointer for libthread_db.  */
    515  1.1  christos 
    516  1.1  christos ps_err_e
    517  1.6  christos ps_get_thread_area (struct ps_prochandle *ph,
    518  1.1  christos                     lwpid_t lwpid, int idx, void **base)
    519  1.1  christos {
    520  1.1  christos   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
    521  1.1  christos     return PS_ERR;
    522  1.1  christos 
    523  1.1  christos   /* IDX is the bias from the thread pointer to the beginning of the
    524  1.1  christos      thread descriptor.  It has to be subtracted due to implementation
    525  1.1  christos      quirks in libthread_db.  */
    526  1.1  christos   *base = (void *) ((char *)*base - idx);
    527  1.1  christos 
    528  1.1  christos   return PS_OK;
    529  1.1  christos }
    530  1.1  christos 
    531  1.8  christos const struct target_desc *
    532  1.8  christos arm_linux_nat_target::read_description ()
    533  1.1  christos {
    534  1.9  christos   CORE_ADDR arm_hwcap = linux_get_hwcap (this);
    535  1.5  christos 
    536  1.5  christos   if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
    537  1.5  christos     {
    538  1.5  christos       elf_gregset_t gpregs;
    539  1.5  christos       struct iovec iov;
    540  1.8  christos       int tid = inferior_ptid.lwp ();
    541  1.5  christos 
    542  1.5  christos       iov.iov_base = &gpregs;
    543  1.5  christos       iov.iov_len = sizeof (gpregs);
    544  1.5  christos 
    545  1.5  christos       /* Check if PTRACE_GETREGSET works.  */
    546  1.5  christos       if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) < 0)
    547  1.5  christos 	have_ptrace_getregset = TRIBOOL_FALSE;
    548  1.5  christos       else
    549  1.5  christos 	have_ptrace_getregset = TRIBOOL_TRUE;
    550  1.5  christos     }
    551  1.1  christos 
    552  1.1  christos   if (arm_hwcap & HWCAP_IWMMXT)
    553  1.9  christos     return arm_read_description (ARM_FP_TYPE_IWMMXT);
    554  1.1  christos 
    555  1.1  christos   if (arm_hwcap & HWCAP_VFP)
    556  1.1  christos     {
    557  1.9  christos       /* Make sure that the kernel supports reading VFP registers.  Support was
    558  1.9  christos 	 added in 2.6.30.  */
    559  1.9  christos       int pid = inferior_ptid.lwp ();
    560  1.9  christos       errno = 0;
    561  1.9  christos       char *buf = (char *) alloca (ARM_VFP3_REGS_SIZE);
    562  1.9  christos       if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0 && errno == EIO)
    563  1.9  christos 	return nullptr;
    564  1.1  christos 
    565  1.1  christos       /* NEON implies VFPv3-D32 or no-VFP unit.  Say that we only support
    566  1.1  christos 	 Neon with VFPv3-D32.  */
    567  1.1  christos       if (arm_hwcap & HWCAP_NEON)
    568  1.9  christos 	return aarch32_read_description ();
    569  1.1  christos       else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
    570  1.9  christos 	return arm_read_description (ARM_FP_TYPE_VFPV3);
    571  1.1  christos 
    572  1.9  christos       return arm_read_description (ARM_FP_TYPE_VFPV2);
    573  1.1  christos     }
    574  1.1  christos 
    575  1.8  christos   return this->beneath ()->read_description ();
    576  1.1  christos }
    577  1.1  christos 
    578  1.1  christos /* Information describing the hardware breakpoint capabilities.  */
    579  1.1  christos struct arm_linux_hwbp_cap
    580  1.1  christos {
    581  1.1  christos   gdb_byte arch;
    582  1.1  christos   gdb_byte max_wp_length;
    583  1.1  christos   gdb_byte wp_count;
    584  1.1  christos   gdb_byte bp_count;
    585  1.1  christos };
    586  1.1  christos 
    587  1.3  christos /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
    588  1.3  christos    assume a maximum number of supported break-/watchpoints.  */
    589  1.3  christos #define MAX_BPTS 16
    590  1.3  christos #define MAX_WPTS 16
    591  1.3  christos 
    592  1.1  christos /* Get hold of the Hardware Breakpoint information for the target we are
    593  1.1  christos    attached to.  Returns NULL if the kernel doesn't support Hardware
    594  1.1  christos    breakpoints at all, or a pointer to the information structure.  */
    595  1.1  christos static const struct arm_linux_hwbp_cap *
    596  1.1  christos arm_linux_get_hwbp_cap (void)
    597  1.1  christos {
    598  1.1  christos   /* The info structure we return.  */
    599  1.1  christos   static struct arm_linux_hwbp_cap info;
    600  1.1  christos 
    601  1.1  christos   /* Is INFO in a good state?  -1 means that no attempt has been made to
    602  1.1  christos      initialize INFO; 0 means an attempt has been made, but it failed; 1
    603  1.1  christos      means INFO is in an initialized state.  */
    604  1.1  christos   static int available = -1;
    605  1.1  christos 
    606  1.1  christos   if (available == -1)
    607  1.1  christos     {
    608  1.1  christos       int tid;
    609  1.1  christos       unsigned int val;
    610  1.1  christos 
    611  1.8  christos       tid = inferior_ptid.lwp ();
    612  1.1  christos       if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
    613  1.1  christos 	available = 0;
    614  1.1  christos       else
    615  1.1  christos 	{
    616  1.1  christos 	  info.arch = (gdb_byte)((val >> 24) & 0xff);
    617  1.1  christos 	  info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
    618  1.1  christos 	  info.wp_count = (gdb_byte)((val >> 8) & 0xff);
    619  1.1  christos 	  info.bp_count = (gdb_byte)(val & 0xff);
    620  1.3  christos 
    621  1.3  christos       if (info.wp_count > MAX_WPTS)
    622  1.3  christos         {
    623  1.3  christos           warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
    624  1.3  christos                       supports %d"), MAX_WPTS, info.wp_count);
    625  1.3  christos           info.wp_count = MAX_WPTS;
    626  1.3  christos         }
    627  1.3  christos 
    628  1.3  christos       if (info.bp_count > MAX_BPTS)
    629  1.3  christos         {
    630  1.3  christos           warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
    631  1.3  christos                       supports %d"), MAX_BPTS, info.bp_count);
    632  1.3  christos           info.bp_count = MAX_BPTS;
    633  1.3  christos         }
    634  1.1  christos 	  available = (info.arch != 0);
    635  1.1  christos 	}
    636  1.1  christos     }
    637  1.1  christos 
    638  1.1  christos   return available == 1 ? &info : NULL;
    639  1.1  christos }
    640  1.1  christos 
    641  1.1  christos /* How many hardware breakpoints are available?  */
    642  1.1  christos static int
    643  1.1  christos arm_linux_get_hw_breakpoint_count (void)
    644  1.1  christos {
    645  1.1  christos   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
    646  1.1  christos   return cap != NULL ? cap->bp_count : 0;
    647  1.1  christos }
    648  1.1  christos 
    649  1.1  christos /* How many hardware watchpoints are available?  */
    650  1.1  christos static int
    651  1.1  christos arm_linux_get_hw_watchpoint_count (void)
    652  1.1  christos {
    653  1.1  christos   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
    654  1.1  christos   return cap != NULL ? cap->wp_count : 0;
    655  1.1  christos }
    656  1.1  christos 
    657  1.1  christos /* Have we got a free break-/watch-point available for use?  Returns -1 if
    658  1.1  christos    there is not an appropriate resource available, otherwise returns 1.  */
    659  1.8  christos int
    660  1.8  christos arm_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
    661  1.8  christos 					     int cnt, int ot)
    662  1.1  christos {
    663  1.1  christos   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
    664  1.1  christos       || type == bp_access_watchpoint || type == bp_watchpoint)
    665  1.1  christos     {
    666  1.5  christos       int count = arm_linux_get_hw_watchpoint_count ();
    667  1.5  christos 
    668  1.5  christos       if (count == 0)
    669  1.5  christos 	return 0;
    670  1.5  christos       else if (cnt + ot > count)
    671  1.1  christos 	return -1;
    672  1.1  christos     }
    673  1.1  christos   else if (type == bp_hardware_breakpoint)
    674  1.1  christos     {
    675  1.5  christos       int count = arm_linux_get_hw_breakpoint_count ();
    676  1.5  christos 
    677  1.5  christos       if (count == 0)
    678  1.5  christos 	return 0;
    679  1.5  christos       else if (cnt > count)
    680  1.1  christos 	return -1;
    681  1.1  christos     }
    682  1.1  christos   else
    683  1.9  christos     gdb_assert_not_reached ("unknown breakpoint type");
    684  1.1  christos 
    685  1.1  christos   return 1;
    686  1.1  christos }
    687  1.1  christos 
    688  1.1  christos /* Enum describing the different types of ARM hardware break-/watch-points.  */
    689  1.1  christos typedef enum
    690  1.1  christos {
    691  1.1  christos   arm_hwbp_break = 0,
    692  1.1  christos   arm_hwbp_load = 1,
    693  1.1  christos   arm_hwbp_store = 2,
    694  1.1  christos   arm_hwbp_access = 3
    695  1.1  christos } arm_hwbp_type;
    696  1.1  christos 
    697  1.1  christos /* Type describing an ARM Hardware Breakpoint Control register value.  */
    698  1.1  christos typedef unsigned int arm_hwbp_control_t;
    699  1.1  christos 
    700  1.1  christos /* Structure used to keep track of hardware break-/watch-points.  */
    701  1.1  christos struct arm_linux_hw_breakpoint
    702  1.1  christos {
    703  1.1  christos   /* Address to break on, or being watched.  */
    704  1.1  christos   unsigned int address;
    705  1.1  christos   /* Control register for break-/watch- point.  */
    706  1.1  christos   arm_hwbp_control_t control;
    707  1.1  christos };
    708  1.1  christos 
    709  1.3  christos /* Structure containing arrays of per process hardware break-/watchpoints
    710  1.3  christos    for caching address and control information.
    711  1.1  christos 
    712  1.1  christos    The Linux ptrace interface to hardware break-/watch-points presents the
    713  1.1  christos    values in a vector centred around 0 (which is used fo generic information).
    714  1.1  christos    Positive indicies refer to breakpoint addresses/control registers, negative
    715  1.1  christos    indices to watchpoint addresses/control registers.
    716  1.1  christos 
    717  1.1  christos    The Linux vector is indexed as follows:
    718  1.1  christos       -((i << 1) + 2): Control register for watchpoint i.
    719  1.1  christos       -((i << 1) + 1): Address register for watchpoint i.
    720  1.1  christos                     0: Information register.
    721  1.1  christos        ((i << 1) + 1): Address register for breakpoint i.
    722  1.1  christos        ((i << 1) + 2): Control register for breakpoint i.
    723  1.1  christos 
    724  1.1  christos    This structure is used as a per-thread cache of the state stored by the
    725  1.1  christos    kernel, so that we don't need to keep calling into the kernel to find a
    726  1.1  christos    free breakpoint.
    727  1.1  christos 
    728  1.1  christos    We treat break-/watch-points with their enable bit clear as being deleted.
    729  1.1  christos    */
    730  1.3  christos struct arm_linux_debug_reg_state
    731  1.3  christos {
    732  1.3  christos   /* Hardware breakpoints for this process.  */
    733  1.3  christos   struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
    734  1.3  christos   /* Hardware watchpoints for this process.  */
    735  1.3  christos   struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
    736  1.3  christos };
    737  1.3  christos 
    738  1.3  christos /* Per-process arch-specific data we want to keep.  */
    739  1.3  christos struct arm_linux_process_info
    740  1.3  christos {
    741  1.3  christos   /* Linked list.  */
    742  1.3  christos   struct arm_linux_process_info *next;
    743  1.3  christos   /* The process identifier.  */
    744  1.3  christos   pid_t pid;
    745  1.3  christos   /* Hardware break-/watchpoints state information.  */
    746  1.3  christos   struct arm_linux_debug_reg_state state;
    747  1.3  christos 
    748  1.3  christos };
    749  1.3  christos 
    750  1.3  christos /* Per-thread arch-specific data we want to keep.  */
    751  1.3  christos struct arch_lwp_info
    752  1.3  christos {
    753  1.3  christos   /* Non-zero if our copy differs from what's recorded in the thread.  */
    754  1.3  christos   char bpts_changed[MAX_BPTS];
    755  1.3  christos   char wpts_changed[MAX_WPTS];
    756  1.3  christos };
    757  1.3  christos 
    758  1.3  christos static struct arm_linux_process_info *arm_linux_process_list = NULL;
    759  1.3  christos 
    760  1.3  christos /* Find process data for process PID.  */
    761  1.3  christos 
    762  1.3  christos static struct arm_linux_process_info *
    763  1.3  christos arm_linux_find_process_pid (pid_t pid)
    764  1.3  christos {
    765  1.3  christos   struct arm_linux_process_info *proc;
    766  1.3  christos 
    767  1.3  christos   for (proc = arm_linux_process_list; proc; proc = proc->next)
    768  1.3  christos     if (proc->pid == pid)
    769  1.3  christos       return proc;
    770  1.3  christos 
    771  1.3  christos   return NULL;
    772  1.3  christos }
    773  1.3  christos 
    774  1.3  christos /* Add process data for process PID.  Returns newly allocated info
    775  1.3  christos    object.  */
    776  1.3  christos 
    777  1.3  christos static struct arm_linux_process_info *
    778  1.3  christos arm_linux_add_process (pid_t pid)
    779  1.1  christos {
    780  1.3  christos   struct arm_linux_process_info *proc;
    781  1.3  christos 
    782  1.6  christos   proc = XCNEW (struct arm_linux_process_info);
    783  1.3  christos   proc->pid = pid;
    784  1.3  christos 
    785  1.3  christos   proc->next = arm_linux_process_list;
    786  1.3  christos   arm_linux_process_list = proc;
    787  1.3  christos 
    788  1.3  christos   return proc;
    789  1.3  christos }
    790  1.3  christos 
    791  1.3  christos /* Get data specific info for process PID, creating it if necessary.
    792  1.3  christos    Never returns NULL.  */
    793  1.3  christos 
    794  1.3  christos static struct arm_linux_process_info *
    795  1.3  christos arm_linux_process_info_get (pid_t pid)
    796  1.3  christos {
    797  1.3  christos   struct arm_linux_process_info *proc;
    798  1.3  christos 
    799  1.3  christos   proc = arm_linux_find_process_pid (pid);
    800  1.3  christos   if (proc == NULL)
    801  1.3  christos     proc = arm_linux_add_process (pid);
    802  1.3  christos 
    803  1.3  christos   return proc;
    804  1.3  christos }
    805  1.3  christos 
    806  1.3  christos /* Called whenever GDB is no longer debugging process PID.  It deletes
    807  1.3  christos    data structures that keep track of debug register state.  */
    808  1.3  christos 
    809  1.8  christos void
    810  1.8  christos arm_linux_nat_target::low_forget_process (pid_t pid)
    811  1.1  christos {
    812  1.3  christos   struct arm_linux_process_info *proc, **proc_link;
    813  1.3  christos 
    814  1.3  christos   proc = arm_linux_process_list;
    815  1.3  christos   proc_link = &arm_linux_process_list;
    816  1.1  christos 
    817  1.3  christos   while (proc != NULL)
    818  1.3  christos     {
    819  1.3  christos       if (proc->pid == pid)
    820  1.1  christos     {
    821  1.3  christos       *proc_link = proc->next;
    822  1.3  christos 
    823  1.3  christos       xfree (proc);
    824  1.3  christos       return;
    825  1.1  christos     }
    826  1.1  christos 
    827  1.3  christos       proc_link = &proc->next;
    828  1.3  christos       proc = *proc_link;
    829  1.3  christos     }
    830  1.3  christos }
    831  1.1  christos 
    832  1.3  christos /* Get hardware break-/watchpoint state for process PID.  */
    833  1.1  christos 
    834  1.3  christos static struct arm_linux_debug_reg_state *
    835  1.3  christos arm_linux_get_debug_reg_state (pid_t pid)
    836  1.3  christos {
    837  1.3  christos   return &arm_linux_process_info_get (pid)->state;
    838  1.1  christos }
    839  1.1  christos 
    840  1.1  christos /* Initialize an ARM hardware break-/watch-point control register value.
    841  1.1  christos    BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
    842  1.1  christos    type of break-/watch-point; ENABLE indicates whether the point is enabled.
    843  1.1  christos    */
    844  1.1  christos static arm_hwbp_control_t
    845  1.1  christos arm_hwbp_control_initialize (unsigned byte_address_select,
    846  1.1  christos 			     arm_hwbp_type hwbp_type,
    847  1.1  christos 			     int enable)
    848  1.1  christos {
    849  1.1  christos   gdb_assert ((byte_address_select & ~0xffU) == 0);
    850  1.1  christos   gdb_assert (hwbp_type != arm_hwbp_break
    851  1.1  christos 	      || ((byte_address_select & 0xfU) != 0));
    852  1.1  christos 
    853  1.1  christos   return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
    854  1.1  christos }
    855  1.1  christos 
    856  1.1  christos /* Does the breakpoint control value CONTROL have the enable bit set?  */
    857  1.1  christos static int
    858  1.1  christos arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
    859  1.1  christos {
    860  1.1  christos   return control & 0x1;
    861  1.1  christos }
    862  1.1  christos 
    863  1.1  christos /* Change a breakpoint control word so that it is in the disabled state.  */
    864  1.1  christos static arm_hwbp_control_t
    865  1.1  christos arm_hwbp_control_disable (arm_hwbp_control_t control)
    866  1.1  christos {
    867  1.1  christos   return control & ~0x1;
    868  1.1  christos }
    869  1.1  christos 
    870  1.1  christos /* Initialise the hardware breakpoint structure P.  The breakpoint will be
    871  1.1  christos    enabled, and will point to the placed address of BP_TGT.  */
    872  1.1  christos static void
    873  1.1  christos arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
    874  1.1  christos 				    struct bp_target_info *bp_tgt,
    875  1.1  christos 				    struct arm_linux_hw_breakpoint *p)
    876  1.1  christos {
    877  1.1  christos   unsigned mask;
    878  1.3  christos   CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;
    879  1.1  christos 
    880  1.1  christos   /* We have to create a mask for the control register which says which bits
    881  1.1  christos      of the word pointed to by address to break on.  */
    882  1.1  christos   if (arm_pc_is_thumb (gdbarch, address))
    883  1.1  christos     {
    884  1.1  christos       mask = 0x3;
    885  1.1  christos       address &= ~1;
    886  1.1  christos     }
    887  1.1  christos   else
    888  1.1  christos     {
    889  1.1  christos       mask = 0xf;
    890  1.1  christos       address &= ~3;
    891  1.1  christos     }
    892  1.1  christos 
    893  1.1  christos   p->address = (unsigned int) address;
    894  1.1  christos   p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
    895  1.1  christos }
    896  1.1  christos 
    897  1.6  christos /* Get the ARM hardware breakpoint type from the TYPE value we're
    898  1.6  christos    given when asked to set a watchpoint.  */
    899  1.1  christos static arm_hwbp_type
    900  1.6  christos arm_linux_get_hwbp_type (enum target_hw_bp_type type)
    901  1.1  christos {
    902  1.6  christos   if (type == hw_read)
    903  1.1  christos     return arm_hwbp_load;
    904  1.6  christos   else if (type == hw_write)
    905  1.1  christos     return arm_hwbp_store;
    906  1.1  christos   else
    907  1.1  christos     return arm_hwbp_access;
    908  1.1  christos }
    909  1.1  christos 
    910  1.1  christos /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
    911  1.1  christos    to LEN.  The type of watchpoint is given in RW.  */
    912  1.1  christos static void
    913  1.6  christos arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len,
    914  1.6  christos 				    enum target_hw_bp_type type,
    915  1.1  christos 				    struct arm_linux_hw_breakpoint *p)
    916  1.1  christos {
    917  1.1  christos   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
    918  1.1  christos   unsigned mask;
    919  1.1  christos 
    920  1.1  christos   gdb_assert (cap != NULL);
    921  1.1  christos   gdb_assert (cap->max_wp_length != 0);
    922  1.1  christos 
    923  1.1  christos   mask = (1 << len) - 1;
    924  1.1  christos 
    925  1.1  christos   p->address = (unsigned int) addr;
    926  1.1  christos   p->control = arm_hwbp_control_initialize (mask,
    927  1.6  christos 					    arm_linux_get_hwbp_type (type), 1);
    928  1.1  christos }
    929  1.1  christos 
    930  1.1  christos /* Are two break-/watch-points equal?  */
    931  1.1  christos static int
    932  1.1  christos arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
    933  1.1  christos 			       const struct arm_linux_hw_breakpoint *p2)
    934  1.1  christos {
    935  1.1  christos   return p1->address == p2->address && p1->control == p2->control;
    936  1.1  christos }
    937  1.1  christos 
    938  1.3  christos /* Callback to mark a watch-/breakpoint to be updated in all threads of
    939  1.3  christos    the current process.  */
    940  1.3  christos 
    941  1.3  christos static int
    942  1.9  christos update_registers_callback (struct lwp_info *lwp, int watch, int index)
    943  1.3  christos {
    944  1.3  christos   if (lwp->arch_private == NULL)
    945  1.3  christos     lwp->arch_private = XCNEW (struct arch_lwp_info);
    946  1.3  christos 
    947  1.3  christos   /* The actual update is done later just before resuming the lwp,
    948  1.3  christos      we just mark that the registers need updating.  */
    949  1.9  christos   if (watch)
    950  1.9  christos     lwp->arch_private->wpts_changed[index] = 1;
    951  1.3  christos   else
    952  1.9  christos     lwp->arch_private->bpts_changed[index] = 1;
    953  1.3  christos 
    954  1.3  christos   /* If the lwp isn't stopped, force it to momentarily pause, so
    955  1.3  christos      we can update its breakpoint registers.  */
    956  1.3  christos   if (!lwp->stopped)
    957  1.3  christos     linux_stop_lwp (lwp);
    958  1.3  christos 
    959  1.3  christos   return 0;
    960  1.3  christos }
    961  1.3  christos 
    962  1.1  christos /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
    963  1.1  christos    =1) BPT for thread TID.  */
    964  1.1  christos static void
    965  1.1  christos arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
    966  1.3  christos                                  int watchpoint)
    967  1.1  christos {
    968  1.3  christos   int pid;
    969  1.3  christos   ptid_t pid_ptid;
    970  1.1  christos   gdb_byte count, i;
    971  1.1  christos   struct arm_linux_hw_breakpoint* bpts;
    972  1.1  christos 
    973  1.8  christos   pid = inferior_ptid.pid ();
    974  1.8  christos   pid_ptid = ptid_t (pid);
    975  1.1  christos 
    976  1.1  christos   if (watchpoint)
    977  1.1  christos     {
    978  1.1  christos       count = arm_linux_get_hw_watchpoint_count ();
    979  1.3  christos       bpts = arm_linux_get_debug_reg_state (pid)->wpts;
    980  1.1  christos     }
    981  1.1  christos   else
    982  1.1  christos     {
    983  1.1  christos       count = arm_linux_get_hw_breakpoint_count ();
    984  1.3  christos       bpts = arm_linux_get_debug_reg_state (pid)->bpts;
    985  1.1  christos     }
    986  1.1  christos 
    987  1.1  christos   for (i = 0; i < count; ++i)
    988  1.1  christos     if (!arm_hwbp_control_is_enabled (bpts[i].control))
    989  1.1  christos       {
    990  1.3  christos         bpts[i] = *bpt;
    991  1.9  christos         iterate_over_lwps (pid_ptid,
    992  1.9  christos 			   [=] (struct lwp_info *info)
    993  1.9  christos 			   {
    994  1.9  christos 			     return update_registers_callback (info, watchpoint,
    995  1.9  christos 							       i);
    996  1.9  christos 			   });
    997  1.3  christos         break;
    998  1.1  christos       }
    999  1.1  christos 
   1000  1.1  christos   gdb_assert (i != count);
   1001  1.1  christos }
   1002  1.1  christos 
   1003  1.1  christos /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
   1004  1.1  christos    (WATCHPOINT = 1) BPT for thread TID.  */
   1005  1.1  christos static void
   1006  1.1  christos arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
   1007  1.3  christos                                  int watchpoint)
   1008  1.1  christos {
   1009  1.3  christos   int pid;
   1010  1.1  christos   gdb_byte count, i;
   1011  1.3  christos   ptid_t pid_ptid;
   1012  1.3  christos   struct arm_linux_hw_breakpoint* bpts;
   1013  1.1  christos 
   1014  1.8  christos   pid = inferior_ptid.pid ();
   1015  1.8  christos   pid_ptid = ptid_t (pid);
   1016  1.1  christos 
   1017  1.1  christos   if (watchpoint)
   1018  1.1  christos     {
   1019  1.1  christos       count = arm_linux_get_hw_watchpoint_count ();
   1020  1.3  christos       bpts = arm_linux_get_debug_reg_state (pid)->wpts;
   1021  1.1  christos     }
   1022  1.1  christos   else
   1023  1.1  christos     {
   1024  1.1  christos       count = arm_linux_get_hw_breakpoint_count ();
   1025  1.3  christos       bpts = arm_linux_get_debug_reg_state (pid)->bpts;
   1026  1.1  christos     }
   1027  1.1  christos 
   1028  1.1  christos   for (i = 0; i < count; ++i)
   1029  1.1  christos     if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
   1030  1.1  christos       {
   1031  1.3  christos         bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
   1032  1.9  christos 	iterate_over_lwps (pid_ptid,
   1033  1.9  christos 			   [=] (struct lwp_info *info)
   1034  1.9  christos 			   {
   1035  1.9  christos 			     return update_registers_callback (info, watchpoint,
   1036  1.9  christos 							       i);
   1037  1.9  christos 			   });
   1038  1.3  christos         break;
   1039  1.1  christos       }
   1040  1.1  christos 
   1041  1.1  christos   gdb_assert (i != count);
   1042  1.1  christos }
   1043  1.1  christos 
   1044  1.1  christos /* Insert a Hardware breakpoint.  */
   1045  1.8  christos int
   1046  1.8  christos arm_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
   1047  1.8  christos 					    struct bp_target_info *bp_tgt)
   1048  1.1  christos {
   1049  1.1  christos   struct arm_linux_hw_breakpoint p;
   1050  1.1  christos 
   1051  1.1  christos   if (arm_linux_get_hw_breakpoint_count () == 0)
   1052  1.1  christos     return -1;
   1053  1.1  christos 
   1054  1.1  christos   arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
   1055  1.3  christos 
   1056  1.3  christos   arm_linux_insert_hw_breakpoint1 (&p, 0);
   1057  1.1  christos 
   1058  1.1  christos   return 0;
   1059  1.1  christos }
   1060  1.1  christos 
   1061  1.1  christos /* Remove a hardware breakpoint.  */
   1062  1.8  christos int
   1063  1.8  christos arm_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
   1064  1.8  christos 					    struct bp_target_info *bp_tgt)
   1065  1.1  christos {
   1066  1.1  christos   struct arm_linux_hw_breakpoint p;
   1067  1.1  christos 
   1068  1.1  christos   if (arm_linux_get_hw_breakpoint_count () == 0)
   1069  1.1  christos     return -1;
   1070  1.1  christos 
   1071  1.1  christos   arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
   1072  1.3  christos 
   1073  1.3  christos   arm_linux_remove_hw_breakpoint1 (&p, 0);
   1074  1.1  christos 
   1075  1.1  christos   return 0;
   1076  1.1  christos }
   1077  1.1  christos 
   1078  1.1  christos /* Are we able to use a hardware watchpoint for the LEN bytes starting at
   1079  1.1  christos    ADDR?  */
   1080  1.8  christos int
   1081  1.8  christos arm_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
   1082  1.1  christos {
   1083  1.1  christos   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
   1084  1.1  christos   CORE_ADDR max_wp_length, aligned_addr;
   1085  1.1  christos 
   1086  1.1  christos   /* Can not set watchpoints for zero or negative lengths.  */
   1087  1.1  christos   if (len <= 0)
   1088  1.1  christos     return 0;
   1089  1.1  christos 
   1090  1.1  christos   /* Need to be able to use the ptrace interface.  */
   1091  1.1  christos   if (cap == NULL || cap->wp_count == 0)
   1092  1.1  christos     return 0;
   1093  1.1  christos 
   1094  1.1  christos   /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
   1095  1.1  christos      range covered by a watchpoint.  */
   1096  1.1  christos   max_wp_length = (CORE_ADDR)cap->max_wp_length;
   1097  1.1  christos   aligned_addr = addr & ~(max_wp_length - 1);
   1098  1.1  christos 
   1099  1.1  christos   if (aligned_addr + max_wp_length < addr + len)
   1100  1.1  christos     return 0;
   1101  1.1  christos 
   1102  1.1  christos   /* The current ptrace interface can only handle watchpoints that are a
   1103  1.1  christos      power of 2.  */
   1104  1.1  christos   if ((len & (len - 1)) != 0)
   1105  1.1  christos     return 0;
   1106  1.1  christos 
   1107  1.1  christos   /* All tests passed so we must be able to set a watchpoint.  */
   1108  1.1  christos   return 1;
   1109  1.1  christos }
   1110  1.1  christos 
   1111  1.1  christos /* Insert a Hardware breakpoint.  */
   1112  1.8  christos int
   1113  1.8  christos arm_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
   1114  1.8  christos 					 enum target_hw_bp_type rw,
   1115  1.8  christos 					 struct expression *cond)
   1116  1.1  christos {
   1117  1.1  christos   struct arm_linux_hw_breakpoint p;
   1118  1.1  christos 
   1119  1.1  christos   if (arm_linux_get_hw_watchpoint_count () == 0)
   1120  1.1  christos     return -1;
   1121  1.1  christos 
   1122  1.1  christos   arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
   1123  1.3  christos 
   1124  1.3  christos   arm_linux_insert_hw_breakpoint1 (&p, 1);
   1125  1.1  christos 
   1126  1.1  christos   return 0;
   1127  1.1  christos }
   1128  1.1  christos 
   1129  1.1  christos /* Remove a hardware breakpoint.  */
   1130  1.8  christos int
   1131  1.8  christos arm_linux_nat_target::remove_watchpoint (CORE_ADDR addr,
   1132  1.8  christos 					 int len, enum target_hw_bp_type rw,
   1133  1.8  christos 					 struct expression *cond)
   1134  1.1  christos {
   1135  1.1  christos   struct arm_linux_hw_breakpoint p;
   1136  1.1  christos 
   1137  1.1  christos   if (arm_linux_get_hw_watchpoint_count () == 0)
   1138  1.1  christos     return -1;
   1139  1.1  christos 
   1140  1.1  christos   arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
   1141  1.3  christos 
   1142  1.3  christos   arm_linux_remove_hw_breakpoint1 (&p, 1);
   1143  1.1  christos 
   1144  1.1  christos   return 0;
   1145  1.1  christos }
   1146  1.1  christos 
   1147  1.1  christos /* What was the data address the target was stopped on accessing.  */
   1148  1.8  christos bool
   1149  1.8  christos arm_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
   1150  1.1  christos {
   1151  1.1  christos   siginfo_t siginfo;
   1152  1.1  christos   int slot;
   1153  1.1  christos 
   1154  1.1  christos   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
   1155  1.8  christos     return false;
   1156  1.1  christos 
   1157  1.1  christos   /* This must be a hardware breakpoint.  */
   1158  1.1  christos   if (siginfo.si_signo != SIGTRAP
   1159  1.1  christos       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
   1160  1.8  christos     return false;
   1161  1.1  christos 
   1162  1.1  christos   /* We must be able to set hardware watchpoints.  */
   1163  1.1  christos   if (arm_linux_get_hw_watchpoint_count () == 0)
   1164  1.1  christos     return 0;
   1165  1.1  christos 
   1166  1.1  christos   slot = siginfo.si_errno;
   1167  1.1  christos 
   1168  1.1  christos   /* If we are in a positive slot then we're looking at a breakpoint and not
   1169  1.1  christos      a watchpoint.  */
   1170  1.1  christos   if (slot >= 0)
   1171  1.8  christos     return false;
   1172  1.1  christos 
   1173  1.1  christos   *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
   1174  1.8  christos   return true;
   1175  1.1  christos }
   1176  1.1  christos 
   1177  1.1  christos /* Has the target been stopped by hitting a watchpoint?  */
   1178  1.8  christos bool
   1179  1.8  christos arm_linux_nat_target::stopped_by_watchpoint ()
   1180  1.1  christos {
   1181  1.1  christos   CORE_ADDR addr;
   1182  1.8  christos   return stopped_data_address (&addr);
   1183  1.1  christos }
   1184  1.1  christos 
   1185  1.8  christos bool
   1186  1.8  christos arm_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
   1187  1.8  christos 						    CORE_ADDR start,
   1188  1.8  christos 						    int length)
   1189  1.1  christos {
   1190  1.1  christos   return start <= addr && start + length - 1 >= addr;
   1191  1.1  christos }
   1192  1.1  christos 
   1193  1.1  christos /* Handle thread creation.  We need to copy the breakpoints and watchpoints
   1194  1.1  christos    in the parent thread to the child thread.  */
   1195  1.8  christos void
   1196  1.8  christos arm_linux_nat_target::low_new_thread (struct lwp_info *lp)
   1197  1.1  christos {
   1198  1.3  christos   int i;
   1199  1.3  christos   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
   1200  1.1  christos 
   1201  1.3  christos   /* Mark that all the hardware breakpoint/watchpoint register pairs
   1202  1.3  christos      for this thread need to be initialized.  */
   1203  1.3  christos 
   1204  1.3  christos   for (i = 0; i < MAX_BPTS; i++)
   1205  1.1  christos     {
   1206  1.3  christos       info->bpts_changed[i] = 1;
   1207  1.3  christos       info->wpts_changed[i] = 1;
   1208  1.3  christos     }
   1209  1.1  christos 
   1210  1.3  christos   lp->arch_private = info;
   1211  1.3  christos }
   1212  1.1  christos 
   1213  1.8  christos /* Function to call when a thread is being deleted.  */
   1214  1.8  christos 
   1215  1.8  christos void
   1216  1.8  christos arm_linux_nat_target::low_delete_thread (struct arch_lwp_info *arch_lwp)
   1217  1.8  christos {
   1218  1.8  christos   xfree (arch_lwp);
   1219  1.8  christos }
   1220  1.8  christos 
   1221  1.3  christos /* Called when resuming a thread.
   1222  1.3  christos    The hardware debug registers are updated when there is any change.  */
   1223  1.1  christos 
   1224  1.8  christos void
   1225  1.8  christos arm_linux_nat_target::low_prepare_to_resume (struct lwp_info *lwp)
   1226  1.1  christos {
   1227  1.3  christos   int pid, i;
   1228  1.3  christos   struct arm_linux_hw_breakpoint *bpts, *wpts;
   1229  1.3  christos   struct arch_lwp_info *arm_lwp_info = lwp->arch_private;
   1230  1.3  christos 
   1231  1.8  christos   pid = lwp->ptid.lwp ();
   1232  1.8  christos   bpts = arm_linux_get_debug_reg_state (lwp->ptid.pid ())->bpts;
   1233  1.8  christos   wpts = arm_linux_get_debug_reg_state (lwp->ptid.pid ())->wpts;
   1234  1.3  christos 
   1235  1.3  christos   /* NULL means this is the main thread still going through the shell,
   1236  1.3  christos      or, no watchpoint has been set yet.  In that case, there's
   1237  1.3  christos      nothing to do.  */
   1238  1.3  christos   if (arm_lwp_info == NULL)
   1239  1.3  christos     return;
   1240  1.3  christos 
   1241  1.3  christos   for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
   1242  1.3  christos     if (arm_lwp_info->bpts_changed[i])
   1243  1.3  christos       {
   1244  1.3  christos         errno = 0;
   1245  1.3  christos         if (arm_hwbp_control_is_enabled (bpts[i].control))
   1246  1.3  christos           if (ptrace (PTRACE_SETHBPREGS, pid,
   1247  1.3  christos               (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
   1248  1.3  christos             perror_with_name (_("Unexpected error setting breakpoint"));
   1249  1.3  christos 
   1250  1.3  christos         if (bpts[i].control != 0)
   1251  1.3  christos           if (ptrace (PTRACE_SETHBPREGS, pid,
   1252  1.3  christos               (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
   1253  1.3  christos             perror_with_name (_("Unexpected error setting breakpoint"));
   1254  1.3  christos 
   1255  1.3  christos         arm_lwp_info->bpts_changed[i] = 0;
   1256  1.3  christos       }
   1257  1.1  christos 
   1258  1.3  christos   for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
   1259  1.3  christos     if (arm_lwp_info->wpts_changed[i])
   1260  1.3  christos       {
   1261  1.3  christos         errno = 0;
   1262  1.3  christos         if (arm_hwbp_control_is_enabled (wpts[i].control))
   1263  1.3  christos           if (ptrace (PTRACE_SETHBPREGS, pid,
   1264  1.3  christos               (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
   1265  1.3  christos             perror_with_name (_("Unexpected error setting watchpoint"));
   1266  1.3  christos 
   1267  1.3  christos         if (wpts[i].control != 0)
   1268  1.3  christos           if (ptrace (PTRACE_SETHBPREGS, pid,
   1269  1.3  christos               (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
   1270  1.3  christos             perror_with_name (_("Unexpected error setting watchpoint"));
   1271  1.1  christos 
   1272  1.3  christos         arm_lwp_info->wpts_changed[i] = 0;
   1273  1.3  christos       }
   1274  1.3  christos }
   1275  1.1  christos 
   1276  1.3  christos /* linux_nat_new_fork hook.  */
   1277  1.1  christos 
   1278  1.8  christos void
   1279  1.8  christos arm_linux_nat_target::low_new_fork (struct lwp_info *parent, pid_t child_pid)
   1280  1.3  christos {
   1281  1.3  christos   pid_t parent_pid;
   1282  1.3  christos   struct arm_linux_debug_reg_state *parent_state;
   1283  1.3  christos   struct arm_linux_debug_reg_state *child_state;
   1284  1.3  christos 
   1285  1.3  christos   /* NULL means no watchpoint has ever been set in the parent.  In
   1286  1.3  christos      that case, there's nothing to do.  */
   1287  1.3  christos   if (parent->arch_private == NULL)
   1288  1.3  christos     return;
   1289  1.1  christos 
   1290  1.3  christos   /* GDB core assumes the child inherits the watchpoints/hw
   1291  1.3  christos      breakpoints of the parent, and will remove them all from the
   1292  1.3  christos      forked off process.  Copy the debug registers mirrors into the
   1293  1.3  christos      new process so that all breakpoints and watchpoints can be
   1294  1.3  christos      removed together.  */
   1295  1.3  christos 
   1296  1.8  christos   parent_pid = parent->ptid.pid ();
   1297  1.3  christos   parent_state = arm_linux_get_debug_reg_state (parent_pid);
   1298  1.3  christos   child_state = arm_linux_get_debug_reg_state (child_pid);
   1299  1.3  christos   *child_state = *parent_state;
   1300  1.1  christos }
   1301  1.1  christos 
   1302  1.9  christos void _initialize_arm_linux_nat ();
   1303  1.1  christos void
   1304  1.9  christos _initialize_arm_linux_nat ()
   1305  1.1  christos {
   1306  1.1  christos   /* Register the target.  */
   1307  1.8  christos   linux_target = &the_arm_linux_nat_target;
   1308  1.8  christos   add_inf_child_target (&the_arm_linux_nat_target);
   1309  1.1  christos }
   1310