Home | History | Annotate | Line # | Download | only in gdb
arm-linux-nat.c revision 1.5
      1  1.1  christos /* GNU/Linux on ARM native support.
      2  1.3  christos    Copyright (C) 1999-2015 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.1  christos #include "observer.h"
     28  1.1  christos #include "gdbthread.h"
     29  1.1  christos 
     30  1.1  christos #include "arm-tdep.h"
     31  1.1  christos #include "arm-linux-tdep.h"
     32  1.1  christos 
     33  1.1  christos #include <elf/common.h>
     34  1.1  christos #include <sys/user.h>
     35  1.1  christos #include <sys/ptrace.h>
     36  1.1  christos #include <sys/utsname.h>
     37  1.1  christos #include <sys/procfs.h>
     38  1.1  christos 
     39  1.5  christos #include "nat/linux-ptrace.h"
     40  1.5  christos 
     41  1.1  christos /* Prototypes for supply_gregset etc.  */
     42  1.1  christos #include "gregset.h"
     43  1.1  christos 
     44  1.1  christos /* Defines ps_err_e, struct ps_prochandle.  */
     45  1.1  christos #include "gdb_proc_service.h"
     46  1.1  christos 
     47  1.1  christos #ifndef PTRACE_GET_THREAD_AREA
     48  1.1  christos #define PTRACE_GET_THREAD_AREA 22
     49  1.1  christos #endif
     50  1.1  christos 
     51  1.1  christos #ifndef PTRACE_GETWMMXREGS
     52  1.1  christos #define PTRACE_GETWMMXREGS 18
     53  1.1  christos #define PTRACE_SETWMMXREGS 19
     54  1.1  christos #endif
     55  1.1  christos 
     56  1.1  christos #ifndef PTRACE_GETVFPREGS
     57  1.1  christos #define PTRACE_GETVFPREGS 27
     58  1.1  christos #define PTRACE_SETVFPREGS 28
     59  1.1  christos #endif
     60  1.1  christos 
     61  1.1  christos #ifndef PTRACE_GETHBPREGS
     62  1.1  christos #define PTRACE_GETHBPREGS 29
     63  1.1  christos #define PTRACE_SETHBPREGS 30
     64  1.1  christos #endif
     65  1.1  christos 
     66  1.1  christos extern int arm_apcs_32;
     67  1.1  christos 
     68  1.1  christos /* On GNU/Linux, threads are implemented as pseudo-processes, in which
     69  1.1  christos    case we may be tracing more than one process at a time.  In that
     70  1.1  christos    case, inferior_ptid will contain the main process ID and the
     71  1.1  christos    individual thread (process) ID.  get_thread_id () is used to get
     72  1.1  christos    the thread id if it's available, and the process id otherwise.  */
     73  1.1  christos 
     74  1.1  christos static int
     75  1.1  christos get_thread_id (ptid_t ptid)
     76  1.1  christos {
     77  1.1  christos   int tid = ptid_get_lwp (ptid);
     78  1.1  christos   if (0 == tid)
     79  1.1  christos     tid = ptid_get_pid (ptid);
     80  1.1  christos   return tid;
     81  1.1  christos }
     82  1.1  christos 
     83  1.1  christos #define GET_THREAD_ID(PTID)	get_thread_id (PTID)
     84  1.1  christos 
     85  1.1  christos /* Get the value of a particular register from the floating point
     86  1.1  christos    state of the process and store it into regcache.  */
     87  1.1  christos 
     88  1.1  christos static void
     89  1.1  christos fetch_fpregister (struct regcache *regcache, int regno)
     90  1.1  christos {
     91  1.1  christos   int ret, tid;
     92  1.1  christos   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
     93  1.5  christos 
     94  1.1  christos   /* Get the thread id for the ptrace call.  */
     95  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
     96  1.1  christos 
     97  1.1  christos   /* Read the floating point state.  */
     98  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
     99  1.5  christos     {
    100  1.5  christos       struct iovec iov;
    101  1.5  christos 
    102  1.5  christos       iov.iov_base = &fp;
    103  1.5  christos       iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
    104  1.5  christos 
    105  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
    106  1.5  christos     }
    107  1.5  christos   else
    108  1.5  christos     ret = ptrace (PT_GETFPREGS, tid, 0, fp);
    109  1.5  christos 
    110  1.1  christos   if (ret < 0)
    111  1.1  christos     {
    112  1.1  christos       warning (_("Unable to fetch floating point register."));
    113  1.1  christos       return;
    114  1.1  christos     }
    115  1.1  christos 
    116  1.1  christos   /* Fetch fpsr.  */
    117  1.1  christos   if (ARM_FPS_REGNUM == regno)
    118  1.1  christos     regcache_raw_supply (regcache, ARM_FPS_REGNUM,
    119  1.1  christos 			 fp + NWFPE_FPSR_OFFSET);
    120  1.1  christos 
    121  1.1  christos   /* Fetch the floating point register.  */
    122  1.1  christos   if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
    123  1.1  christos     supply_nwfpe_register (regcache, regno, fp);
    124  1.1  christos }
    125  1.1  christos 
    126  1.1  christos /* Get the whole floating point state of the process and store it
    127  1.1  christos    into regcache.  */
    128  1.1  christos 
    129  1.1  christos static void
    130  1.1  christos fetch_fpregs (struct regcache *regcache)
    131  1.1  christos {
    132  1.1  christos   int ret, regno, tid;
    133  1.1  christos   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
    134  1.1  christos 
    135  1.1  christos   /* Get the thread id for the ptrace call.  */
    136  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    137  1.5  christos 
    138  1.1  christos   /* Read the floating point state.  */
    139  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    140  1.5  christos     {
    141  1.5  christos       struct iovec iov;
    142  1.5  christos 
    143  1.5  christos       iov.iov_base = &fp;
    144  1.5  christos       iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
    145  1.5  christos 
    146  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
    147  1.5  christos     }
    148  1.5  christos   else
    149  1.5  christos     ret = ptrace (PT_GETFPREGS, tid, 0, fp);
    150  1.5  christos 
    151  1.1  christos   if (ret < 0)
    152  1.1  christos     {
    153  1.1  christos       warning (_("Unable to fetch the floating point registers."));
    154  1.1  christos       return;
    155  1.1  christos     }
    156  1.1  christos 
    157  1.1  christos   /* Fetch fpsr.  */
    158  1.1  christos   regcache_raw_supply (regcache, ARM_FPS_REGNUM,
    159  1.1  christos 		       fp + NWFPE_FPSR_OFFSET);
    160  1.1  christos 
    161  1.1  christos   /* Fetch the floating point registers.  */
    162  1.1  christos   for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
    163  1.1  christos     supply_nwfpe_register (regcache, regno, fp);
    164  1.1  christos }
    165  1.1  christos 
    166  1.1  christos /* Save a particular register into the floating point state of the
    167  1.1  christos    process using the contents from regcache.  */
    168  1.1  christos 
    169  1.1  christos static void
    170  1.1  christos store_fpregister (const struct regcache *regcache, int regno)
    171  1.1  christos {
    172  1.1  christos   int ret, tid;
    173  1.1  christos   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
    174  1.1  christos 
    175  1.1  christos   /* Get the thread id for the ptrace call.  */
    176  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    177  1.5  christos 
    178  1.1  christos   /* Read the floating point state.  */
    179  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    180  1.5  christos     {
    181  1.5  christos       struct iovec iov;
    182  1.5  christos 
    183  1.5  christos       iov.iov_base = &fp;
    184  1.5  christos       iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
    185  1.5  christos 
    186  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
    187  1.5  christos     }
    188  1.5  christos   else
    189  1.5  christos     ret = ptrace (PT_GETFPREGS, tid, 0, fp);
    190  1.5  christos 
    191  1.1  christos   if (ret < 0)
    192  1.1  christos     {
    193  1.1  christos       warning (_("Unable to fetch the floating point registers."));
    194  1.1  christos       return;
    195  1.1  christos     }
    196  1.1  christos 
    197  1.1  christos   /* Store fpsr.  */
    198  1.1  christos   if (ARM_FPS_REGNUM == regno
    199  1.1  christos       && REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
    200  1.1  christos     regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
    201  1.1  christos 
    202  1.1  christos   /* Store the floating point register.  */
    203  1.1  christos   if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
    204  1.1  christos     collect_nwfpe_register (regcache, regno, fp);
    205  1.1  christos 
    206  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    207  1.5  christos     {
    208  1.5  christos       struct iovec iov;
    209  1.5  christos 
    210  1.5  christos       iov.iov_base = &fp;
    211  1.5  christos       iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
    212  1.5  christos 
    213  1.5  christos       ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
    214  1.5  christos     }
    215  1.5  christos   else
    216  1.5  christos     ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
    217  1.5  christos 
    218  1.1  christos   if (ret < 0)
    219  1.1  christos     {
    220  1.1  christos       warning (_("Unable to store floating point register."));
    221  1.1  christos       return;
    222  1.1  christos     }
    223  1.1  christos }
    224  1.1  christos 
    225  1.1  christos /* Save the whole floating point state of the process using
    226  1.1  christos    the contents from regcache.  */
    227  1.1  christos 
    228  1.1  christos static void
    229  1.1  christos store_fpregs (const struct regcache *regcache)
    230  1.1  christos {
    231  1.1  christos   int ret, regno, tid;
    232  1.1  christos   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
    233  1.1  christos 
    234  1.1  christos   /* Get the thread id for the ptrace call.  */
    235  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    236  1.5  christos 
    237  1.1  christos   /* Read the floating point state.  */
    238  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    239  1.5  christos     {
    240  1.5  christos       elf_fpregset_t fpregs;
    241  1.5  christos       struct iovec iov;
    242  1.5  christos 
    243  1.5  christos       iov.iov_base = &fpregs;
    244  1.5  christos       iov.iov_len = sizeof (fpregs);
    245  1.5  christos 
    246  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
    247  1.5  christos     }
    248  1.5  christos   else
    249  1.5  christos     ret = ptrace (PT_GETFPREGS, tid, 0, fp);
    250  1.5  christos 
    251  1.1  christos   if (ret < 0)
    252  1.1  christos     {
    253  1.1  christos       warning (_("Unable to fetch the floating point registers."));
    254  1.1  christos       return;
    255  1.1  christos     }
    256  1.1  christos 
    257  1.1  christos   /* Store fpsr.  */
    258  1.1  christos   if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
    259  1.1  christos     regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
    260  1.1  christos 
    261  1.1  christos   /* Store the floating point registers.  */
    262  1.1  christos   for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
    263  1.1  christos     if (REG_VALID == regcache_register_status (regcache, regno))
    264  1.1  christos       collect_nwfpe_register (regcache, regno, fp);
    265  1.1  christos 
    266  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    267  1.5  christos     {
    268  1.5  christos       struct iovec iov;
    269  1.5  christos 
    270  1.5  christos       iov.iov_base = &fp;
    271  1.5  christos       iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
    272  1.5  christos 
    273  1.5  christos       ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
    274  1.5  christos     }
    275  1.5  christos   else
    276  1.5  christos     ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
    277  1.5  christos 
    278  1.1  christos   if (ret < 0)
    279  1.1  christos     {
    280  1.1  christos       warning (_("Unable to store floating point registers."));
    281  1.1  christos       return;
    282  1.1  christos     }
    283  1.1  christos }
    284  1.1  christos 
    285  1.1  christos /* Fetch a general register of the process and store into
    286  1.1  christos    regcache.  */
    287  1.1  christos 
    288  1.1  christos static void
    289  1.1  christos fetch_register (struct regcache *regcache, int regno)
    290  1.1  christos {
    291  1.1  christos   int ret, tid;
    292  1.1  christos   elf_gregset_t regs;
    293  1.1  christos 
    294  1.1  christos   /* Get the thread id for the ptrace call.  */
    295  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    296  1.5  christos 
    297  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    298  1.5  christos     {
    299  1.5  christos       struct iovec iov;
    300  1.5  christos 
    301  1.5  christos       iov.iov_base = &regs;
    302  1.5  christos       iov.iov_len = sizeof (regs);
    303  1.5  christos 
    304  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
    305  1.5  christos     }
    306  1.5  christos   else
    307  1.5  christos     ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
    308  1.5  christos 
    309  1.1  christos   if (ret < 0)
    310  1.1  christos     {
    311  1.1  christos       warning (_("Unable to fetch general register."));
    312  1.1  christos       return;
    313  1.1  christos     }
    314  1.1  christos 
    315  1.1  christos   if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
    316  1.1  christos     regcache_raw_supply (regcache, regno, (char *) &regs[regno]);
    317  1.1  christos 
    318  1.1  christos   if (ARM_PS_REGNUM == regno)
    319  1.1  christos     {
    320  1.1  christos       if (arm_apcs_32)
    321  1.1  christos         regcache_raw_supply (regcache, ARM_PS_REGNUM,
    322  1.1  christos 			     (char *) &regs[ARM_CPSR_GREGNUM]);
    323  1.1  christos       else
    324  1.1  christos         regcache_raw_supply (regcache, ARM_PS_REGNUM,
    325  1.1  christos 			     (char *) &regs[ARM_PC_REGNUM]);
    326  1.1  christos     }
    327  1.1  christos 
    328  1.1  christos   if (ARM_PC_REGNUM == regno)
    329  1.1  christos     {
    330  1.1  christos       regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
    331  1.1  christos 			      (get_regcache_arch (regcache),
    332  1.1  christos 			       regs[ARM_PC_REGNUM]);
    333  1.1  christos       regcache_raw_supply (regcache, ARM_PC_REGNUM,
    334  1.1  christos 			   (char *) &regs[ARM_PC_REGNUM]);
    335  1.1  christos     }
    336  1.1  christos }
    337  1.1  christos 
    338  1.1  christos /* Fetch all general registers of the process and store into
    339  1.1  christos    regcache.  */
    340  1.1  christos 
    341  1.1  christos static void
    342  1.1  christos fetch_regs (struct regcache *regcache)
    343  1.1  christos {
    344  1.1  christos   int ret, regno, tid;
    345  1.1  christos   elf_gregset_t regs;
    346  1.1  christos 
    347  1.1  christos   /* Get the thread id for the ptrace call.  */
    348  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    349  1.5  christos 
    350  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    351  1.5  christos     {
    352  1.5  christos       struct iovec iov;
    353  1.5  christos 
    354  1.5  christos       iov.iov_base = &regs;
    355  1.5  christos       iov.iov_len = sizeof (regs);
    356  1.5  christos 
    357  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
    358  1.5  christos     }
    359  1.5  christos   else
    360  1.5  christos     ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
    361  1.5  christos 
    362  1.1  christos   if (ret < 0)
    363  1.1  christos     {
    364  1.1  christos       warning (_("Unable to fetch general registers."));
    365  1.1  christos       return;
    366  1.1  christos     }
    367  1.1  christos 
    368  1.1  christos   for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
    369  1.1  christos     regcache_raw_supply (regcache, regno, (char *) &regs[regno]);
    370  1.1  christos 
    371  1.1  christos   if (arm_apcs_32)
    372  1.1  christos     regcache_raw_supply (regcache, ARM_PS_REGNUM,
    373  1.1  christos 			 (char *) &regs[ARM_CPSR_GREGNUM]);
    374  1.1  christos   else
    375  1.1  christos     regcache_raw_supply (regcache, ARM_PS_REGNUM,
    376  1.1  christos 			 (char *) &regs[ARM_PC_REGNUM]);
    377  1.1  christos 
    378  1.1  christos   regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
    379  1.1  christos 			  (get_regcache_arch (regcache), regs[ARM_PC_REGNUM]);
    380  1.1  christos   regcache_raw_supply (regcache, ARM_PC_REGNUM,
    381  1.1  christos 		       (char *) &regs[ARM_PC_REGNUM]);
    382  1.1  christos }
    383  1.1  christos 
    384  1.1  christos /* Store all general registers of the process from the values in
    385  1.1  christos    regcache.  */
    386  1.1  christos 
    387  1.1  christos static void
    388  1.1  christos store_register (const struct regcache *regcache, int regno)
    389  1.1  christos {
    390  1.1  christos   int ret, tid;
    391  1.1  christos   elf_gregset_t regs;
    392  1.1  christos 
    393  1.1  christos   if (REG_VALID != regcache_register_status (regcache, regno))
    394  1.1  christos     return;
    395  1.1  christos 
    396  1.1  christos   /* Get the thread id for the ptrace call.  */
    397  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    398  1.5  christos 
    399  1.1  christos   /* Get the general registers from the process.  */
    400  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    401  1.5  christos     {
    402  1.5  christos       struct iovec iov;
    403  1.5  christos 
    404  1.5  christos       iov.iov_base = &regs;
    405  1.5  christos       iov.iov_len = sizeof (regs);
    406  1.5  christos 
    407  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
    408  1.5  christos     }
    409  1.5  christos   else
    410  1.5  christos     ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
    411  1.5  christos 
    412  1.1  christos   if (ret < 0)
    413  1.1  christos     {
    414  1.1  christos       warning (_("Unable to fetch general registers."));
    415  1.1  christos       return;
    416  1.1  christos     }
    417  1.1  christos 
    418  1.1  christos   if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
    419  1.1  christos     regcache_raw_collect (regcache, regno, (char *) &regs[regno]);
    420  1.1  christos   else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
    421  1.1  christos     regcache_raw_collect (regcache, regno,
    422  1.1  christos 			 (char *) &regs[ARM_CPSR_GREGNUM]);
    423  1.1  christos   else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
    424  1.1  christos     regcache_raw_collect (regcache, ARM_PC_REGNUM,
    425  1.1  christos 			 (char *) &regs[ARM_PC_REGNUM]);
    426  1.1  christos 
    427  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    428  1.5  christos     {
    429  1.5  christos       struct iovec iov;
    430  1.5  christos 
    431  1.5  christos       iov.iov_base = &regs;
    432  1.5  christos       iov.iov_len = sizeof (regs);
    433  1.5  christos 
    434  1.5  christos       ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
    435  1.5  christos     }
    436  1.5  christos   else
    437  1.5  christos     ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
    438  1.5  christos 
    439  1.1  christos   if (ret < 0)
    440  1.1  christos     {
    441  1.1  christos       warning (_("Unable to store general register."));
    442  1.1  christos       return;
    443  1.1  christos     }
    444  1.1  christos }
    445  1.1  christos 
    446  1.1  christos static void
    447  1.1  christos store_regs (const struct regcache *regcache)
    448  1.1  christos {
    449  1.1  christos   int ret, regno, tid;
    450  1.1  christos   elf_gregset_t regs;
    451  1.1  christos 
    452  1.1  christos   /* Get the thread id for the ptrace call.  */
    453  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    454  1.5  christos 
    455  1.1  christos   /* Fetch the general registers.  */
    456  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    457  1.5  christos     {
    458  1.5  christos       struct iovec iov;
    459  1.5  christos 
    460  1.5  christos       iov.iov_base = &regs;
    461  1.5  christos       iov.iov_len = sizeof (regs);
    462  1.5  christos 
    463  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
    464  1.5  christos     }
    465  1.5  christos   else
    466  1.5  christos     ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
    467  1.5  christos 
    468  1.1  christos   if (ret < 0)
    469  1.1  christos     {
    470  1.1  christos       warning (_("Unable to fetch general registers."));
    471  1.1  christos       return;
    472  1.1  christos     }
    473  1.1  christos 
    474  1.1  christos   for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
    475  1.1  christos     {
    476  1.1  christos       if (REG_VALID == regcache_register_status (regcache, regno))
    477  1.1  christos 	regcache_raw_collect (regcache, regno, (char *) &regs[regno]);
    478  1.1  christos     }
    479  1.1  christos 
    480  1.1  christos   if (arm_apcs_32 && REG_VALID == regcache_register_status (regcache, ARM_PS_REGNUM))
    481  1.1  christos     regcache_raw_collect (regcache, ARM_PS_REGNUM,
    482  1.1  christos 			 (char *) &regs[ARM_CPSR_GREGNUM]);
    483  1.1  christos 
    484  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    485  1.5  christos     {
    486  1.5  christos       struct iovec iov;
    487  1.5  christos 
    488  1.5  christos       iov.iov_base = &regs;
    489  1.5  christos       iov.iov_len = sizeof (regs);
    490  1.5  christos 
    491  1.5  christos       ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
    492  1.5  christos     }
    493  1.5  christos   else
    494  1.5  christos     ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
    495  1.1  christos 
    496  1.1  christos   if (ret < 0)
    497  1.1  christos     {
    498  1.1  christos       warning (_("Unable to store general registers."));
    499  1.1  christos       return;
    500  1.1  christos     }
    501  1.1  christos }
    502  1.1  christos 
    503  1.1  christos /* Fetch all WMMX registers of the process and store into
    504  1.1  christos    regcache.  */
    505  1.1  christos 
    506  1.1  christos #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
    507  1.1  christos 
    508  1.1  christos static void
    509  1.1  christos fetch_wmmx_regs (struct regcache *regcache)
    510  1.1  christos {
    511  1.1  christos   char regbuf[IWMMXT_REGS_SIZE];
    512  1.1  christos   int ret, regno, tid;
    513  1.1  christos 
    514  1.1  christos   /* Get the thread id for the ptrace call.  */
    515  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    516  1.1  christos 
    517  1.1  christos   ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
    518  1.1  christos   if (ret < 0)
    519  1.1  christos     {
    520  1.1  christos       warning (_("Unable to fetch WMMX registers."));
    521  1.1  christos       return;
    522  1.1  christos     }
    523  1.1  christos 
    524  1.1  christos   for (regno = 0; regno < 16; regno++)
    525  1.1  christos     regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
    526  1.1  christos 			 &regbuf[regno * 8]);
    527  1.1  christos 
    528  1.1  christos   for (regno = 0; regno < 2; regno++)
    529  1.1  christos     regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
    530  1.1  christos 			 &regbuf[16 * 8 + regno * 4]);
    531  1.1  christos 
    532  1.1  christos   for (regno = 0; regno < 4; regno++)
    533  1.1  christos     regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
    534  1.1  christos 			 &regbuf[16 * 8 + 2 * 4 + regno * 4]);
    535  1.1  christos }
    536  1.1  christos 
    537  1.1  christos static void
    538  1.1  christos store_wmmx_regs (const struct regcache *regcache)
    539  1.1  christos {
    540  1.1  christos   char regbuf[IWMMXT_REGS_SIZE];
    541  1.1  christos   int ret, regno, tid;
    542  1.1  christos 
    543  1.1  christos   /* Get the thread id for the ptrace call.  */
    544  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    545  1.1  christos 
    546  1.1  christos   ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
    547  1.1  christos   if (ret < 0)
    548  1.1  christos     {
    549  1.1  christos       warning (_("Unable to fetch WMMX registers."));
    550  1.1  christos       return;
    551  1.1  christos     }
    552  1.1  christos 
    553  1.1  christos   for (regno = 0; regno < 16; regno++)
    554  1.1  christos     if (REG_VALID == regcache_register_status (regcache,
    555  1.1  christos 					       regno + ARM_WR0_REGNUM))
    556  1.1  christos       regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
    557  1.1  christos 			    &regbuf[regno * 8]);
    558  1.1  christos 
    559  1.1  christos   for (regno = 0; regno < 2; regno++)
    560  1.1  christos     if (REG_VALID == regcache_register_status (regcache,
    561  1.1  christos 					       regno + ARM_WCSSF_REGNUM))
    562  1.1  christos       regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
    563  1.1  christos 			    &regbuf[16 * 8 + regno * 4]);
    564  1.1  christos 
    565  1.1  christos   for (regno = 0; regno < 4; regno++)
    566  1.1  christos     if (REG_VALID == regcache_register_status (regcache,
    567  1.1  christos 					       regno + ARM_WCGR0_REGNUM))
    568  1.1  christos       regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
    569  1.1  christos 			    &regbuf[16 * 8 + 2 * 4 + regno * 4]);
    570  1.1  christos 
    571  1.1  christos   ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
    572  1.1  christos 
    573  1.1  christos   if (ret < 0)
    574  1.1  christos     {
    575  1.1  christos       warning (_("Unable to store WMMX registers."));
    576  1.1  christos       return;
    577  1.1  christos     }
    578  1.1  christos }
    579  1.1  christos 
    580  1.1  christos /* Fetch and store VFP Registers.  The kernel object has space for 32
    581  1.1  christos    64-bit registers, and the FPSCR.  This is even when on a VFPv2 or
    582  1.1  christos    VFPv3D16 target.  */
    583  1.1  christos #define VFP_REGS_SIZE (32 * 8 + 4)
    584  1.1  christos 
    585  1.1  christos static void
    586  1.1  christos fetch_vfp_regs (struct regcache *regcache)
    587  1.1  christos {
    588  1.1  christos   char regbuf[VFP_REGS_SIZE];
    589  1.1  christos   int ret, regno, tid;
    590  1.5  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    591  1.5  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    592  1.1  christos 
    593  1.1  christos   /* Get the thread id for the ptrace call.  */
    594  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    595  1.1  christos 
    596  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    597  1.5  christos     {
    598  1.5  christos       struct iovec iov;
    599  1.5  christos 
    600  1.5  christos       iov.iov_base = regbuf;
    601  1.5  christos       iov.iov_len = VFP_REGS_SIZE;
    602  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
    603  1.5  christos     }
    604  1.5  christos   else
    605  1.5  christos     ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
    606  1.5  christos 
    607  1.1  christos   if (ret < 0)
    608  1.1  christos     {
    609  1.1  christos       warning (_("Unable to fetch VFP registers."));
    610  1.1  christos       return;
    611  1.1  christos     }
    612  1.1  christos 
    613  1.5  christos   for (regno = 0; regno < tdep->vfp_register_count; regno++)
    614  1.1  christos     regcache_raw_supply (regcache, regno + ARM_D0_REGNUM,
    615  1.1  christos 			 (char *) regbuf + regno * 8);
    616  1.1  christos 
    617  1.1  christos   regcache_raw_supply (regcache, ARM_FPSCR_REGNUM,
    618  1.1  christos 		       (char *) regbuf + 32 * 8);
    619  1.1  christos }
    620  1.1  christos 
    621  1.1  christos static void
    622  1.1  christos store_vfp_regs (const struct regcache *regcache)
    623  1.1  christos {
    624  1.1  christos   char regbuf[VFP_REGS_SIZE];
    625  1.1  christos   int ret, regno, tid;
    626  1.5  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    627  1.5  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    628  1.1  christos 
    629  1.1  christos   /* Get the thread id for the ptrace call.  */
    630  1.1  christos   tid = GET_THREAD_ID (inferior_ptid);
    631  1.1  christos 
    632  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    633  1.5  christos     {
    634  1.5  christos       struct iovec iov;
    635  1.5  christos 
    636  1.5  christos       iov.iov_base = regbuf;
    637  1.5  christos       iov.iov_len = VFP_REGS_SIZE;
    638  1.5  christos       ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
    639  1.5  christos     }
    640  1.5  christos   else
    641  1.5  christos     ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
    642  1.5  christos 
    643  1.1  christos   if (ret < 0)
    644  1.1  christos     {
    645  1.1  christos       warning (_("Unable to fetch VFP registers (for update)."));
    646  1.1  christos       return;
    647  1.1  christos     }
    648  1.1  christos 
    649  1.5  christos   for (regno = 0; regno < tdep->vfp_register_count; regno++)
    650  1.1  christos     regcache_raw_collect (regcache, regno + ARM_D0_REGNUM,
    651  1.1  christos 			  (char *) regbuf + regno * 8);
    652  1.1  christos 
    653  1.1  christos   regcache_raw_collect (regcache, ARM_FPSCR_REGNUM,
    654  1.1  christos 			(char *) regbuf + 32 * 8);
    655  1.1  christos 
    656  1.5  christos   if (have_ptrace_getregset == TRIBOOL_TRUE)
    657  1.5  christos     {
    658  1.5  christos       struct iovec iov;
    659  1.5  christos 
    660  1.5  christos       iov.iov_base = regbuf;
    661  1.5  christos       iov.iov_len = VFP_REGS_SIZE;
    662  1.5  christos       ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iov);
    663  1.5  christos     }
    664  1.5  christos   else
    665  1.5  christos     ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
    666  1.1  christos 
    667  1.1  christos   if (ret < 0)
    668  1.1  christos     {
    669  1.1  christos       warning (_("Unable to store VFP registers."));
    670  1.1  christos       return;
    671  1.1  christos     }
    672  1.1  christos }
    673  1.1  christos 
    674  1.1  christos /* Fetch registers from the child process.  Fetch all registers if
    675  1.1  christos    regno == -1, otherwise fetch all general registers or all floating
    676  1.1  christos    point registers depending upon the value of regno.  */
    677  1.1  christos 
    678  1.1  christos static void
    679  1.1  christos arm_linux_fetch_inferior_registers (struct target_ops *ops,
    680  1.1  christos 				    struct regcache *regcache, int regno)
    681  1.1  christos {
    682  1.5  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    683  1.5  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    684  1.5  christos 
    685  1.1  christos   if (-1 == regno)
    686  1.1  christos     {
    687  1.1  christos       fetch_regs (regcache);
    688  1.1  christos       fetch_fpregs (regcache);
    689  1.5  christos       if (tdep->have_wmmx_registers)
    690  1.1  christos 	fetch_wmmx_regs (regcache);
    691  1.5  christos       if (tdep->vfp_register_count > 0)
    692  1.1  christos 	fetch_vfp_regs (regcache);
    693  1.1  christos     }
    694  1.1  christos   else
    695  1.1  christos     {
    696  1.1  christos       if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
    697  1.1  christos         fetch_register (regcache, regno);
    698  1.1  christos       else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
    699  1.1  christos         fetch_fpregister (regcache, regno);
    700  1.5  christos       else if (tdep->have_wmmx_registers
    701  1.1  christos 	       && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
    702  1.1  christos 	fetch_wmmx_regs (regcache);
    703  1.5  christos       else if (tdep->vfp_register_count > 0
    704  1.1  christos 	       && regno >= ARM_D0_REGNUM
    705  1.5  christos 	       && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
    706  1.1  christos 	fetch_vfp_regs (regcache);
    707  1.1  christos     }
    708  1.1  christos }
    709  1.1  christos 
    710  1.1  christos /* Store registers back into the inferior.  Store all registers if
    711  1.1  christos    regno == -1, otherwise store all general registers or all floating
    712  1.1  christos    point registers depending upon the value of regno.  */
    713  1.1  christos 
    714  1.1  christos static void
    715  1.1  christos arm_linux_store_inferior_registers (struct target_ops *ops,
    716  1.1  christos 				    struct regcache *regcache, int regno)
    717  1.1  christos {
    718  1.5  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    719  1.5  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    720  1.5  christos 
    721  1.1  christos   if (-1 == regno)
    722  1.1  christos     {
    723  1.1  christos       store_regs (regcache);
    724  1.1  christos       store_fpregs (regcache);
    725  1.5  christos       if (tdep->have_wmmx_registers)
    726  1.1  christos 	store_wmmx_regs (regcache);
    727  1.5  christos       if (tdep->vfp_register_count > 0)
    728  1.1  christos 	store_vfp_regs (regcache);
    729  1.1  christos     }
    730  1.1  christos   else
    731  1.1  christos     {
    732  1.1  christos       if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
    733  1.1  christos         store_register (regcache, regno);
    734  1.1  christos       else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
    735  1.1  christos         store_fpregister (regcache, regno);
    736  1.5  christos       else if (tdep->have_wmmx_registers
    737  1.1  christos 	       && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
    738  1.1  christos 	store_wmmx_regs (regcache);
    739  1.5  christos       else if (tdep->vfp_register_count > 0
    740  1.1  christos 	       && regno >= ARM_D0_REGNUM
    741  1.5  christos 	       && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
    742  1.1  christos 	store_vfp_regs (regcache);
    743  1.1  christos     }
    744  1.1  christos }
    745  1.1  christos 
    746  1.1  christos /* Wrapper functions for the standard regset handling, used by
    747  1.1  christos    thread debugging.  */
    748  1.1  christos 
    749  1.1  christos void
    750  1.1  christos fill_gregset (const struct regcache *regcache,
    751  1.1  christos 	      gdb_gregset_t *gregsetp, int regno)
    752  1.1  christos {
    753  1.1  christos   arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
    754  1.1  christos }
    755  1.1  christos 
    756  1.1  christos void
    757  1.1  christos supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
    758  1.1  christos {
    759  1.1  christos   arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
    760  1.1  christos }
    761  1.1  christos 
    762  1.1  christos void
    763  1.1  christos fill_fpregset (const struct regcache *regcache,
    764  1.1  christos 	       gdb_fpregset_t *fpregsetp, int regno)
    765  1.1  christos {
    766  1.1  christos   arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
    767  1.1  christos }
    768  1.1  christos 
    769  1.1  christos /* Fill GDB's register array with the floating-point register values
    770  1.1  christos    in *fpregsetp.  */
    771  1.1  christos 
    772  1.1  christos void
    773  1.1  christos supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
    774  1.1  christos {
    775  1.1  christos   arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
    776  1.1  christos }
    777  1.1  christos 
    778  1.1  christos /* Fetch the thread-local storage pointer for libthread_db.  */
    779  1.1  christos 
    780  1.1  christos ps_err_e
    781  1.1  christos ps_get_thread_area (const struct ps_prochandle *ph,
    782  1.1  christos                     lwpid_t lwpid, int idx, void **base)
    783  1.1  christos {
    784  1.1  christos   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
    785  1.1  christos     return PS_ERR;
    786  1.1  christos 
    787  1.1  christos   /* IDX is the bias from the thread pointer to the beginning of the
    788  1.1  christos      thread descriptor.  It has to be subtracted due to implementation
    789  1.1  christos      quirks in libthread_db.  */
    790  1.1  christos   *base = (void *) ((char *)*base - idx);
    791  1.1  christos 
    792  1.1  christos   return PS_OK;
    793  1.1  christos }
    794  1.1  christos 
    795  1.1  christos static const struct target_desc *
    796  1.1  christos arm_linux_read_description (struct target_ops *ops)
    797  1.1  christos {
    798  1.1  christos   CORE_ADDR arm_hwcap = 0;
    799  1.5  christos 
    800  1.5  christos   if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
    801  1.5  christos     {
    802  1.5  christos       elf_gregset_t gpregs;
    803  1.5  christos       struct iovec iov;
    804  1.5  christos       int tid = GET_THREAD_ID (inferior_ptid);
    805  1.5  christos 
    806  1.5  christos       iov.iov_base = &gpregs;
    807  1.5  christos       iov.iov_len = sizeof (gpregs);
    808  1.5  christos 
    809  1.5  christos       /* Check if PTRACE_GETREGSET works.  */
    810  1.5  christos       if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) < 0)
    811  1.5  christos 	have_ptrace_getregset = TRIBOOL_FALSE;
    812  1.5  christos       else
    813  1.5  christos 	have_ptrace_getregset = TRIBOOL_TRUE;
    814  1.5  christos     }
    815  1.1  christos 
    816  1.1  christos   if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
    817  1.1  christos     {
    818  1.3  christos       return ops->beneath->to_read_description (ops->beneath);
    819  1.1  christos     }
    820  1.1  christos 
    821  1.1  christos   if (arm_hwcap & HWCAP_IWMMXT)
    822  1.5  christos     return tdesc_arm_with_iwmmxt;
    823  1.1  christos 
    824  1.1  christos   if (arm_hwcap & HWCAP_VFP)
    825  1.1  christos     {
    826  1.1  christos       int pid;
    827  1.1  christos       char *buf;
    828  1.1  christos       const struct target_desc * result = NULL;
    829  1.1  christos 
    830  1.1  christos       /* NEON implies VFPv3-D32 or no-VFP unit.  Say that we only support
    831  1.1  christos 	 Neon with VFPv3-D32.  */
    832  1.1  christos       if (arm_hwcap & HWCAP_NEON)
    833  1.5  christos 	result = tdesc_arm_with_neon;
    834  1.1  christos       else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
    835  1.5  christos 	result = tdesc_arm_with_vfpv3;
    836  1.1  christos       else
    837  1.5  christos 	result = tdesc_arm_with_vfpv2;
    838  1.1  christos 
    839  1.1  christos       /* Now make sure that the kernel supports reading these
    840  1.1  christos 	 registers.  Support was added in 2.6.30.  */
    841  1.1  christos       pid = ptid_get_lwp (inferior_ptid);
    842  1.1  christos       errno = 0;
    843  1.1  christos       buf = alloca (VFP_REGS_SIZE);
    844  1.1  christos       if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
    845  1.1  christos 	  && errno == EIO)
    846  1.1  christos 	result = NULL;
    847  1.1  christos 
    848  1.1  christos       return result;
    849  1.1  christos     }
    850  1.1  christos 
    851  1.3  christos   return ops->beneath->to_read_description (ops->beneath);
    852  1.1  christos }
    853  1.1  christos 
    854  1.1  christos /* Information describing the hardware breakpoint capabilities.  */
    855  1.1  christos struct arm_linux_hwbp_cap
    856  1.1  christos {
    857  1.1  christos   gdb_byte arch;
    858  1.1  christos   gdb_byte max_wp_length;
    859  1.1  christos   gdb_byte wp_count;
    860  1.1  christos   gdb_byte bp_count;
    861  1.1  christos };
    862  1.1  christos 
    863  1.3  christos /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
    864  1.3  christos    assume a maximum number of supported break-/watchpoints.  */
    865  1.3  christos #define MAX_BPTS 16
    866  1.3  christos #define MAX_WPTS 16
    867  1.3  christos 
    868  1.1  christos /* Get hold of the Hardware Breakpoint information for the target we are
    869  1.1  christos    attached to.  Returns NULL if the kernel doesn't support Hardware
    870  1.1  christos    breakpoints at all, or a pointer to the information structure.  */
    871  1.1  christos static const struct arm_linux_hwbp_cap *
    872  1.1  christos arm_linux_get_hwbp_cap (void)
    873  1.1  christos {
    874  1.1  christos   /* The info structure we return.  */
    875  1.1  christos   static struct arm_linux_hwbp_cap info;
    876  1.1  christos 
    877  1.1  christos   /* Is INFO in a good state?  -1 means that no attempt has been made to
    878  1.1  christos      initialize INFO; 0 means an attempt has been made, but it failed; 1
    879  1.1  christos      means INFO is in an initialized state.  */
    880  1.1  christos   static int available = -1;
    881  1.1  christos 
    882  1.1  christos   if (available == -1)
    883  1.1  christos     {
    884  1.1  christos       int tid;
    885  1.1  christos       unsigned int val;
    886  1.1  christos 
    887  1.1  christos       tid = GET_THREAD_ID (inferior_ptid);
    888  1.1  christos       if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
    889  1.1  christos 	available = 0;
    890  1.1  christos       else
    891  1.1  christos 	{
    892  1.1  christos 	  info.arch = (gdb_byte)((val >> 24) & 0xff);
    893  1.1  christos 	  info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
    894  1.1  christos 	  info.wp_count = (gdb_byte)((val >> 8) & 0xff);
    895  1.1  christos 	  info.bp_count = (gdb_byte)(val & 0xff);
    896  1.3  christos 
    897  1.3  christos       if (info.wp_count > MAX_WPTS)
    898  1.3  christos         {
    899  1.3  christos           warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
    900  1.3  christos                       supports %d"), MAX_WPTS, info.wp_count);
    901  1.3  christos           info.wp_count = MAX_WPTS;
    902  1.3  christos         }
    903  1.3  christos 
    904  1.3  christos       if (info.bp_count > MAX_BPTS)
    905  1.3  christos         {
    906  1.3  christos           warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
    907  1.3  christos                       supports %d"), MAX_BPTS, info.bp_count);
    908  1.3  christos           info.bp_count = MAX_BPTS;
    909  1.3  christos         }
    910  1.1  christos 	  available = (info.arch != 0);
    911  1.1  christos 	}
    912  1.1  christos     }
    913  1.1  christos 
    914  1.1  christos   return available == 1 ? &info : NULL;
    915  1.1  christos }
    916  1.1  christos 
    917  1.1  christos /* How many hardware breakpoints are available?  */
    918  1.1  christos static int
    919  1.1  christos arm_linux_get_hw_breakpoint_count (void)
    920  1.1  christos {
    921  1.1  christos   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
    922  1.1  christos   return cap != NULL ? cap->bp_count : 0;
    923  1.1  christos }
    924  1.1  christos 
    925  1.1  christos /* How many hardware watchpoints are available?  */
    926  1.1  christos static int
    927  1.1  christos arm_linux_get_hw_watchpoint_count (void)
    928  1.1  christos {
    929  1.1  christos   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
    930  1.1  christos   return cap != NULL ? cap->wp_count : 0;
    931  1.1  christos }
    932  1.1  christos 
    933  1.1  christos /* Have we got a free break-/watch-point available for use?  Returns -1 if
    934  1.1  christos    there is not an appropriate resource available, otherwise returns 1.  */
    935  1.1  christos static int
    936  1.3  christos arm_linux_can_use_hw_breakpoint (struct target_ops *self,
    937  1.3  christos 				 int type, int cnt, int ot)
    938  1.1  christos {
    939  1.1  christos   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
    940  1.1  christos       || type == bp_access_watchpoint || type == bp_watchpoint)
    941  1.1  christos     {
    942  1.5  christos       int count = arm_linux_get_hw_watchpoint_count ();
    943  1.5  christos 
    944  1.5  christos       if (count == 0)
    945  1.5  christos 	return 0;
    946  1.5  christos       else if (cnt + ot > count)
    947  1.1  christos 	return -1;
    948  1.1  christos     }
    949  1.1  christos   else if (type == bp_hardware_breakpoint)
    950  1.1  christos     {
    951  1.5  christos       int count = arm_linux_get_hw_breakpoint_count ();
    952  1.5  christos 
    953  1.5  christos       if (count == 0)
    954  1.5  christos 	return 0;
    955  1.5  christos       else if (cnt > count)
    956  1.1  christos 	return -1;
    957  1.1  christos     }
    958  1.1  christos   else
    959  1.1  christos     gdb_assert (FALSE);
    960  1.1  christos 
    961  1.1  christos   return 1;
    962  1.1  christos }
    963  1.1  christos 
    964  1.1  christos /* Enum describing the different types of ARM hardware break-/watch-points.  */
    965  1.1  christos typedef enum
    966  1.1  christos {
    967  1.1  christos   arm_hwbp_break = 0,
    968  1.1  christos   arm_hwbp_load = 1,
    969  1.1  christos   arm_hwbp_store = 2,
    970  1.1  christos   arm_hwbp_access = 3
    971  1.1  christos } arm_hwbp_type;
    972  1.1  christos 
    973  1.1  christos /* Type describing an ARM Hardware Breakpoint Control register value.  */
    974  1.1  christos typedef unsigned int arm_hwbp_control_t;
    975  1.1  christos 
    976  1.1  christos /* Structure used to keep track of hardware break-/watch-points.  */
    977  1.1  christos struct arm_linux_hw_breakpoint
    978  1.1  christos {
    979  1.1  christos   /* Address to break on, or being watched.  */
    980  1.1  christos   unsigned int address;
    981  1.1  christos   /* Control register for break-/watch- point.  */
    982  1.1  christos   arm_hwbp_control_t control;
    983  1.1  christos };
    984  1.1  christos 
    985  1.3  christos /* Structure containing arrays of per process hardware break-/watchpoints
    986  1.3  christos    for caching address and control information.
    987  1.1  christos 
    988  1.1  christos    The Linux ptrace interface to hardware break-/watch-points presents the
    989  1.1  christos    values in a vector centred around 0 (which is used fo generic information).
    990  1.1  christos    Positive indicies refer to breakpoint addresses/control registers, negative
    991  1.1  christos    indices to watchpoint addresses/control registers.
    992  1.1  christos 
    993  1.1  christos    The Linux vector is indexed as follows:
    994  1.1  christos       -((i << 1) + 2): Control register for watchpoint i.
    995  1.1  christos       -((i << 1) + 1): Address register for watchpoint i.
    996  1.1  christos                     0: Information register.
    997  1.1  christos        ((i << 1) + 1): Address register for breakpoint i.
    998  1.1  christos        ((i << 1) + 2): Control register for breakpoint i.
    999  1.1  christos 
   1000  1.1  christos    This structure is used as a per-thread cache of the state stored by the
   1001  1.1  christos    kernel, so that we don't need to keep calling into the kernel to find a
   1002  1.1  christos    free breakpoint.
   1003  1.1  christos 
   1004  1.1  christos    We treat break-/watch-points with their enable bit clear as being deleted.
   1005  1.1  christos    */
   1006  1.3  christos struct arm_linux_debug_reg_state
   1007  1.3  christos {
   1008  1.3  christos   /* Hardware breakpoints for this process.  */
   1009  1.3  christos   struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
   1010  1.3  christos   /* Hardware watchpoints for this process.  */
   1011  1.3  christos   struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
   1012  1.3  christos };
   1013  1.3  christos 
   1014  1.3  christos /* Per-process arch-specific data we want to keep.  */
   1015  1.3  christos struct arm_linux_process_info
   1016  1.3  christos {
   1017  1.3  christos   /* Linked list.  */
   1018  1.3  christos   struct arm_linux_process_info *next;
   1019  1.3  christos   /* The process identifier.  */
   1020  1.3  christos   pid_t pid;
   1021  1.3  christos   /* Hardware break-/watchpoints state information.  */
   1022  1.3  christos   struct arm_linux_debug_reg_state state;
   1023  1.3  christos 
   1024  1.3  christos };
   1025  1.3  christos 
   1026  1.3  christos /* Per-thread arch-specific data we want to keep.  */
   1027  1.3  christos struct arch_lwp_info
   1028  1.3  christos {
   1029  1.3  christos   /* Non-zero if our copy differs from what's recorded in the thread.  */
   1030  1.3  christos   char bpts_changed[MAX_BPTS];
   1031  1.3  christos   char wpts_changed[MAX_WPTS];
   1032  1.3  christos };
   1033  1.3  christos 
   1034  1.3  christos static struct arm_linux_process_info *arm_linux_process_list = NULL;
   1035  1.3  christos 
   1036  1.3  christos /* Find process data for process PID.  */
   1037  1.3  christos 
   1038  1.3  christos static struct arm_linux_process_info *
   1039  1.3  christos arm_linux_find_process_pid (pid_t pid)
   1040  1.3  christos {
   1041  1.3  christos   struct arm_linux_process_info *proc;
   1042  1.3  christos 
   1043  1.3  christos   for (proc = arm_linux_process_list; proc; proc = proc->next)
   1044  1.3  christos     if (proc->pid == pid)
   1045  1.3  christos       return proc;
   1046  1.3  christos 
   1047  1.3  christos   return NULL;
   1048  1.3  christos }
   1049  1.3  christos 
   1050  1.3  christos /* Add process data for process PID.  Returns newly allocated info
   1051  1.3  christos    object.  */
   1052  1.3  christos 
   1053  1.3  christos static struct arm_linux_process_info *
   1054  1.3  christos arm_linux_add_process (pid_t pid)
   1055  1.1  christos {
   1056  1.3  christos   struct arm_linux_process_info *proc;
   1057  1.3  christos 
   1058  1.3  christos   proc = xcalloc (1, sizeof (*proc));
   1059  1.3  christos   proc->pid = pid;
   1060  1.3  christos 
   1061  1.3  christos   proc->next = arm_linux_process_list;
   1062  1.3  christos   arm_linux_process_list = proc;
   1063  1.3  christos 
   1064  1.3  christos   return proc;
   1065  1.3  christos }
   1066  1.3  christos 
   1067  1.3  christos /* Get data specific info for process PID, creating it if necessary.
   1068  1.3  christos    Never returns NULL.  */
   1069  1.3  christos 
   1070  1.3  christos static struct arm_linux_process_info *
   1071  1.3  christos arm_linux_process_info_get (pid_t pid)
   1072  1.3  christos {
   1073  1.3  christos   struct arm_linux_process_info *proc;
   1074  1.3  christos 
   1075  1.3  christos   proc = arm_linux_find_process_pid (pid);
   1076  1.3  christos   if (proc == NULL)
   1077  1.3  christos     proc = arm_linux_add_process (pid);
   1078  1.3  christos 
   1079  1.3  christos   return proc;
   1080  1.3  christos }
   1081  1.3  christos 
   1082  1.3  christos /* Called whenever GDB is no longer debugging process PID.  It deletes
   1083  1.3  christos    data structures that keep track of debug register state.  */
   1084  1.3  christos 
   1085  1.3  christos static void
   1086  1.3  christos arm_linux_forget_process (pid_t pid)
   1087  1.1  christos {
   1088  1.3  christos   struct arm_linux_process_info *proc, **proc_link;
   1089  1.3  christos 
   1090  1.3  christos   proc = arm_linux_process_list;
   1091  1.3  christos   proc_link = &arm_linux_process_list;
   1092  1.1  christos 
   1093  1.3  christos   while (proc != NULL)
   1094  1.3  christos     {
   1095  1.3  christos       if (proc->pid == pid)
   1096  1.1  christos     {
   1097  1.3  christos       *proc_link = proc->next;
   1098  1.3  christos 
   1099  1.3  christos       xfree (proc);
   1100  1.3  christos       return;
   1101  1.1  christos     }
   1102  1.1  christos 
   1103  1.3  christos       proc_link = &proc->next;
   1104  1.3  christos       proc = *proc_link;
   1105  1.3  christos     }
   1106  1.3  christos }
   1107  1.1  christos 
   1108  1.3  christos /* Get hardware break-/watchpoint state for process PID.  */
   1109  1.1  christos 
   1110  1.3  christos static struct arm_linux_debug_reg_state *
   1111  1.3  christos arm_linux_get_debug_reg_state (pid_t pid)
   1112  1.3  christos {
   1113  1.3  christos   return &arm_linux_process_info_get (pid)->state;
   1114  1.1  christos }
   1115  1.1  christos 
   1116  1.1  christos /* Initialize an ARM hardware break-/watch-point control register value.
   1117  1.1  christos    BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
   1118  1.1  christos    type of break-/watch-point; ENABLE indicates whether the point is enabled.
   1119  1.1  christos    */
   1120  1.1  christos static arm_hwbp_control_t
   1121  1.1  christos arm_hwbp_control_initialize (unsigned byte_address_select,
   1122  1.1  christos 			     arm_hwbp_type hwbp_type,
   1123  1.1  christos 			     int enable)
   1124  1.1  christos {
   1125  1.1  christos   gdb_assert ((byte_address_select & ~0xffU) == 0);
   1126  1.1  christos   gdb_assert (hwbp_type != arm_hwbp_break
   1127  1.1  christos 	      || ((byte_address_select & 0xfU) != 0));
   1128  1.1  christos 
   1129  1.1  christos   return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
   1130  1.1  christos }
   1131  1.1  christos 
   1132  1.1  christos /* Does the breakpoint control value CONTROL have the enable bit set?  */
   1133  1.1  christos static int
   1134  1.1  christos arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
   1135  1.1  christos {
   1136  1.1  christos   return control & 0x1;
   1137  1.1  christos }
   1138  1.1  christos 
   1139  1.1  christos /* Change a breakpoint control word so that it is in the disabled state.  */
   1140  1.1  christos static arm_hwbp_control_t
   1141  1.1  christos arm_hwbp_control_disable (arm_hwbp_control_t control)
   1142  1.1  christos {
   1143  1.1  christos   return control & ~0x1;
   1144  1.1  christos }
   1145  1.1  christos 
   1146  1.1  christos /* Initialise the hardware breakpoint structure P.  The breakpoint will be
   1147  1.1  christos    enabled, and will point to the placed address of BP_TGT.  */
   1148  1.1  christos static void
   1149  1.1  christos arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
   1150  1.1  christos 				    struct bp_target_info *bp_tgt,
   1151  1.1  christos 				    struct arm_linux_hw_breakpoint *p)
   1152  1.1  christos {
   1153  1.1  christos   unsigned mask;
   1154  1.3  christos   CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;
   1155  1.1  christos 
   1156  1.1  christos   /* We have to create a mask for the control register which says which bits
   1157  1.1  christos      of the word pointed to by address to break on.  */
   1158  1.1  christos   if (arm_pc_is_thumb (gdbarch, address))
   1159  1.1  christos     {
   1160  1.1  christos       mask = 0x3;
   1161  1.1  christos       address &= ~1;
   1162  1.1  christos     }
   1163  1.1  christos   else
   1164  1.1  christos     {
   1165  1.1  christos       mask = 0xf;
   1166  1.1  christos       address &= ~3;
   1167  1.1  christos     }
   1168  1.1  christos 
   1169  1.1  christos   p->address = (unsigned int) address;
   1170  1.1  christos   p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
   1171  1.1  christos }
   1172  1.1  christos 
   1173  1.1  christos /* Get the ARM hardware breakpoint type from the RW value we're given when
   1174  1.1  christos    asked to set a watchpoint.  */
   1175  1.1  christos static arm_hwbp_type
   1176  1.1  christos arm_linux_get_hwbp_type (int rw)
   1177  1.1  christos {
   1178  1.1  christos   if (rw == hw_read)
   1179  1.1  christos     return arm_hwbp_load;
   1180  1.1  christos   else if (rw == hw_write)
   1181  1.1  christos     return arm_hwbp_store;
   1182  1.1  christos   else
   1183  1.1  christos     return arm_hwbp_access;
   1184  1.1  christos }
   1185  1.1  christos 
   1186  1.1  christos /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
   1187  1.1  christos    to LEN.  The type of watchpoint is given in RW.  */
   1188  1.1  christos static void
   1189  1.1  christos arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len, int rw,
   1190  1.1  christos 				    struct arm_linux_hw_breakpoint *p)
   1191  1.1  christos {
   1192  1.1  christos   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
   1193  1.1  christos   unsigned mask;
   1194  1.1  christos 
   1195  1.1  christos   gdb_assert (cap != NULL);
   1196  1.1  christos   gdb_assert (cap->max_wp_length != 0);
   1197  1.1  christos 
   1198  1.1  christos   mask = (1 << len) - 1;
   1199  1.1  christos 
   1200  1.1  christos   p->address = (unsigned int) addr;
   1201  1.1  christos   p->control = arm_hwbp_control_initialize (mask,
   1202  1.1  christos 					    arm_linux_get_hwbp_type (rw), 1);
   1203  1.1  christos }
   1204  1.1  christos 
   1205  1.1  christos /* Are two break-/watch-points equal?  */
   1206  1.1  christos static int
   1207  1.1  christos arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
   1208  1.1  christos 			       const struct arm_linux_hw_breakpoint *p2)
   1209  1.1  christos {
   1210  1.1  christos   return p1->address == p2->address && p1->control == p2->control;
   1211  1.1  christos }
   1212  1.1  christos 
   1213  1.3  christos /* Callback to mark a watch-/breakpoint to be updated in all threads of
   1214  1.3  christos    the current process.  */
   1215  1.3  christos 
   1216  1.3  christos struct update_registers_data
   1217  1.3  christos {
   1218  1.3  christos   int watch;
   1219  1.3  christos   int index;
   1220  1.3  christos };
   1221  1.3  christos 
   1222  1.3  christos static int
   1223  1.3  christos update_registers_callback (struct lwp_info *lwp, void *arg)
   1224  1.3  christos {
   1225  1.3  christos   struct update_registers_data *data = (struct update_registers_data *) arg;
   1226  1.3  christos 
   1227  1.3  christos   if (lwp->arch_private == NULL)
   1228  1.3  christos     lwp->arch_private = XCNEW (struct arch_lwp_info);
   1229  1.3  christos 
   1230  1.3  christos   /* The actual update is done later just before resuming the lwp,
   1231  1.3  christos      we just mark that the registers need updating.  */
   1232  1.3  christos   if (data->watch)
   1233  1.3  christos     lwp->arch_private->wpts_changed[data->index] = 1;
   1234  1.3  christos   else
   1235  1.3  christos     lwp->arch_private->bpts_changed[data->index] = 1;
   1236  1.3  christos 
   1237  1.3  christos   /* If the lwp isn't stopped, force it to momentarily pause, so
   1238  1.3  christos      we can update its breakpoint registers.  */
   1239  1.3  christos   if (!lwp->stopped)
   1240  1.3  christos     linux_stop_lwp (lwp);
   1241  1.3  christos 
   1242  1.3  christos   return 0;
   1243  1.3  christos }
   1244  1.3  christos 
   1245  1.1  christos /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
   1246  1.1  christos    =1) BPT for thread TID.  */
   1247  1.1  christos static void
   1248  1.1  christos arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
   1249  1.3  christos                                  int watchpoint)
   1250  1.1  christos {
   1251  1.3  christos   int pid;
   1252  1.3  christos   ptid_t pid_ptid;
   1253  1.1  christos   gdb_byte count, i;
   1254  1.1  christos   struct arm_linux_hw_breakpoint* bpts;
   1255  1.3  christos   struct update_registers_data data;
   1256  1.1  christos 
   1257  1.3  christos   pid = ptid_get_pid (inferior_ptid);
   1258  1.3  christos   pid_ptid = pid_to_ptid (pid);
   1259  1.1  christos 
   1260  1.1  christos   if (watchpoint)
   1261  1.1  christos     {
   1262  1.1  christos       count = arm_linux_get_hw_watchpoint_count ();
   1263  1.3  christos       bpts = arm_linux_get_debug_reg_state (pid)->wpts;
   1264  1.1  christos     }
   1265  1.1  christos   else
   1266  1.1  christos     {
   1267  1.1  christos       count = arm_linux_get_hw_breakpoint_count ();
   1268  1.3  christos       bpts = arm_linux_get_debug_reg_state (pid)->bpts;
   1269  1.1  christos     }
   1270  1.1  christos 
   1271  1.1  christos   for (i = 0; i < count; ++i)
   1272  1.1  christos     if (!arm_hwbp_control_is_enabled (bpts[i].control))
   1273  1.1  christos       {
   1274  1.3  christos         data.watch = watchpoint;
   1275  1.3  christos         data.index = i;
   1276  1.3  christos         bpts[i] = *bpt;
   1277  1.3  christos         iterate_over_lwps (pid_ptid, update_registers_callback, &data);
   1278  1.3  christos         break;
   1279  1.1  christos       }
   1280  1.1  christos 
   1281  1.1  christos   gdb_assert (i != count);
   1282  1.1  christos }
   1283  1.1  christos 
   1284  1.1  christos /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
   1285  1.1  christos    (WATCHPOINT = 1) BPT for thread TID.  */
   1286  1.1  christos static void
   1287  1.1  christos arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
   1288  1.3  christos                                  int watchpoint)
   1289  1.1  christos {
   1290  1.3  christos   int pid;
   1291  1.1  christos   gdb_byte count, i;
   1292  1.3  christos   ptid_t pid_ptid;
   1293  1.3  christos   struct arm_linux_hw_breakpoint* bpts;
   1294  1.3  christos   struct update_registers_data data;
   1295  1.1  christos 
   1296  1.3  christos   pid = ptid_get_pid (inferior_ptid);
   1297  1.3  christos   pid_ptid = pid_to_ptid (pid);
   1298  1.1  christos 
   1299  1.1  christos   if (watchpoint)
   1300  1.1  christos     {
   1301  1.1  christos       count = arm_linux_get_hw_watchpoint_count ();
   1302  1.3  christos       bpts = arm_linux_get_debug_reg_state (pid)->wpts;
   1303  1.1  christos     }
   1304  1.1  christos   else
   1305  1.1  christos     {
   1306  1.1  christos       count = arm_linux_get_hw_breakpoint_count ();
   1307  1.3  christos       bpts = arm_linux_get_debug_reg_state (pid)->bpts;
   1308  1.1  christos     }
   1309  1.1  christos 
   1310  1.1  christos   for (i = 0; i < count; ++i)
   1311  1.1  christos     if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
   1312  1.1  christos       {
   1313  1.3  christos         data.watch = watchpoint;
   1314  1.3  christos         data.index = i;
   1315  1.3  christos         bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
   1316  1.3  christos         iterate_over_lwps (pid_ptid, update_registers_callback, &data);
   1317  1.3  christos         break;
   1318  1.1  christos       }
   1319  1.1  christos 
   1320  1.1  christos   gdb_assert (i != count);
   1321  1.1  christos }
   1322  1.1  christos 
   1323  1.1  christos /* Insert a Hardware breakpoint.  */
   1324  1.1  christos static int
   1325  1.3  christos arm_linux_insert_hw_breakpoint (struct target_ops *self,
   1326  1.3  christos 				struct gdbarch *gdbarch,
   1327  1.1  christos 				struct bp_target_info *bp_tgt)
   1328  1.1  christos {
   1329  1.1  christos   struct lwp_info *lp;
   1330  1.1  christos   struct arm_linux_hw_breakpoint p;
   1331  1.1  christos 
   1332  1.1  christos   if (arm_linux_get_hw_breakpoint_count () == 0)
   1333  1.1  christos     return -1;
   1334  1.1  christos 
   1335  1.1  christos   arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
   1336  1.3  christos 
   1337  1.3  christos   arm_linux_insert_hw_breakpoint1 (&p, 0);
   1338  1.1  christos 
   1339  1.1  christos   return 0;
   1340  1.1  christos }
   1341  1.1  christos 
   1342  1.1  christos /* Remove a hardware breakpoint.  */
   1343  1.1  christos static int
   1344  1.3  christos arm_linux_remove_hw_breakpoint (struct target_ops *self,
   1345  1.3  christos 				struct gdbarch *gdbarch,
   1346  1.1  christos 				struct bp_target_info *bp_tgt)
   1347  1.1  christos {
   1348  1.1  christos   struct lwp_info *lp;
   1349  1.1  christos   struct arm_linux_hw_breakpoint p;
   1350  1.1  christos 
   1351  1.1  christos   if (arm_linux_get_hw_breakpoint_count () == 0)
   1352  1.1  christos     return -1;
   1353  1.1  christos 
   1354  1.1  christos   arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
   1355  1.3  christos 
   1356  1.3  christos   arm_linux_remove_hw_breakpoint1 (&p, 0);
   1357  1.1  christos 
   1358  1.1  christos   return 0;
   1359  1.1  christos }
   1360  1.1  christos 
   1361  1.1  christos /* Are we able to use a hardware watchpoint for the LEN bytes starting at
   1362  1.1  christos    ADDR?  */
   1363  1.1  christos static int
   1364  1.3  christos arm_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
   1365  1.3  christos 				       CORE_ADDR addr, int len)
   1366  1.1  christos {
   1367  1.1  christos   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
   1368  1.1  christos   CORE_ADDR max_wp_length, aligned_addr;
   1369  1.1  christos 
   1370  1.1  christos   /* Can not set watchpoints for zero or negative lengths.  */
   1371  1.1  christos   if (len <= 0)
   1372  1.1  christos     return 0;
   1373  1.1  christos 
   1374  1.1  christos   /* Need to be able to use the ptrace interface.  */
   1375  1.1  christos   if (cap == NULL || cap->wp_count == 0)
   1376  1.1  christos     return 0;
   1377  1.1  christos 
   1378  1.1  christos   /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
   1379  1.1  christos      range covered by a watchpoint.  */
   1380  1.1  christos   max_wp_length = (CORE_ADDR)cap->max_wp_length;
   1381  1.1  christos   aligned_addr = addr & ~(max_wp_length - 1);
   1382  1.1  christos 
   1383  1.1  christos   if (aligned_addr + max_wp_length < addr + len)
   1384  1.1  christos     return 0;
   1385  1.1  christos 
   1386  1.1  christos   /* The current ptrace interface can only handle watchpoints that are a
   1387  1.1  christos      power of 2.  */
   1388  1.1  christos   if ((len & (len - 1)) != 0)
   1389  1.1  christos     return 0;
   1390  1.1  christos 
   1391  1.1  christos   /* All tests passed so we must be able to set a watchpoint.  */
   1392  1.1  christos   return 1;
   1393  1.1  christos }
   1394  1.1  christos 
   1395  1.1  christos /* Insert a Hardware breakpoint.  */
   1396  1.1  christos static int
   1397  1.3  christos arm_linux_insert_watchpoint (struct target_ops *self,
   1398  1.3  christos 			     CORE_ADDR addr, int len, int rw,
   1399  1.1  christos 			     struct expression *cond)
   1400  1.1  christos {
   1401  1.1  christos   struct lwp_info *lp;
   1402  1.1  christos   struct arm_linux_hw_breakpoint p;
   1403  1.1  christos 
   1404  1.1  christos   if (arm_linux_get_hw_watchpoint_count () == 0)
   1405  1.1  christos     return -1;
   1406  1.1  christos 
   1407  1.1  christos   arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
   1408  1.3  christos 
   1409  1.3  christos   arm_linux_insert_hw_breakpoint1 (&p, 1);
   1410  1.1  christos 
   1411  1.1  christos   return 0;
   1412  1.1  christos }
   1413  1.1  christos 
   1414  1.1  christos /* Remove a hardware breakpoint.  */
   1415  1.1  christos static int
   1416  1.3  christos arm_linux_remove_watchpoint (struct target_ops *self,
   1417  1.3  christos 			     CORE_ADDR addr, int len, int rw,
   1418  1.1  christos 			     struct expression *cond)
   1419  1.1  christos {
   1420  1.1  christos   struct lwp_info *lp;
   1421  1.1  christos   struct arm_linux_hw_breakpoint p;
   1422  1.1  christos 
   1423  1.1  christos   if (arm_linux_get_hw_watchpoint_count () == 0)
   1424  1.1  christos     return -1;
   1425  1.1  christos 
   1426  1.1  christos   arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
   1427  1.3  christos 
   1428  1.3  christos   arm_linux_remove_hw_breakpoint1 (&p, 1);
   1429  1.1  christos 
   1430  1.1  christos   return 0;
   1431  1.1  christos }
   1432  1.1  christos 
   1433  1.1  christos /* What was the data address the target was stopped on accessing.  */
   1434  1.1  christos static int
   1435  1.1  christos arm_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
   1436  1.1  christos {
   1437  1.1  christos   siginfo_t siginfo;
   1438  1.1  christos   int slot;
   1439  1.1  christos 
   1440  1.1  christos   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
   1441  1.1  christos     return 0;
   1442  1.1  christos 
   1443  1.1  christos   /* This must be a hardware breakpoint.  */
   1444  1.1  christos   if (siginfo.si_signo != SIGTRAP
   1445  1.1  christos       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
   1446  1.1  christos     return 0;
   1447  1.1  christos 
   1448  1.1  christos   /* We must be able to set hardware watchpoints.  */
   1449  1.1  christos   if (arm_linux_get_hw_watchpoint_count () == 0)
   1450  1.1  christos     return 0;
   1451  1.1  christos 
   1452  1.1  christos   slot = siginfo.si_errno;
   1453  1.1  christos 
   1454  1.1  christos   /* If we are in a positive slot then we're looking at a breakpoint and not
   1455  1.1  christos      a watchpoint.  */
   1456  1.1  christos   if (slot >= 0)
   1457  1.1  christos     return 0;
   1458  1.1  christos 
   1459  1.1  christos   *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
   1460  1.1  christos   return 1;
   1461  1.1  christos }
   1462  1.1  christos 
   1463  1.1  christos /* Has the target been stopped by hitting a watchpoint?  */
   1464  1.1  christos static int
   1465  1.3  christos arm_linux_stopped_by_watchpoint (struct target_ops *ops)
   1466  1.1  christos {
   1467  1.1  christos   CORE_ADDR addr;
   1468  1.3  christos   return arm_linux_stopped_data_address (ops, &addr);
   1469  1.1  christos }
   1470  1.1  christos 
   1471  1.1  christos static int
   1472  1.1  christos arm_linux_watchpoint_addr_within_range (struct target_ops *target,
   1473  1.1  christos 					CORE_ADDR addr,
   1474  1.1  christos 					CORE_ADDR start, int length)
   1475  1.1  christos {
   1476  1.1  christos   return start <= addr && start + length - 1 >= addr;
   1477  1.1  christos }
   1478  1.1  christos 
   1479  1.1  christos /* Handle thread creation.  We need to copy the breakpoints and watchpoints
   1480  1.1  christos    in the parent thread to the child thread.  */
   1481  1.1  christos static void
   1482  1.1  christos arm_linux_new_thread (struct lwp_info *lp)
   1483  1.1  christos {
   1484  1.3  christos   int i;
   1485  1.3  christos   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
   1486  1.1  christos 
   1487  1.3  christos   /* Mark that all the hardware breakpoint/watchpoint register pairs
   1488  1.3  christos      for this thread need to be initialized.  */
   1489  1.3  christos 
   1490  1.3  christos   for (i = 0; i < MAX_BPTS; i++)
   1491  1.1  christos     {
   1492  1.3  christos       info->bpts_changed[i] = 1;
   1493  1.3  christos       info->wpts_changed[i] = 1;
   1494  1.3  christos     }
   1495  1.1  christos 
   1496  1.3  christos   lp->arch_private = info;
   1497  1.3  christos }
   1498  1.1  christos 
   1499  1.3  christos /* Called when resuming a thread.
   1500  1.3  christos    The hardware debug registers are updated when there is any change.  */
   1501  1.1  christos 
   1502  1.1  christos static void
   1503  1.3  christos arm_linux_prepare_to_resume (struct lwp_info *lwp)
   1504  1.1  christos {
   1505  1.3  christos   int pid, i;
   1506  1.3  christos   struct arm_linux_hw_breakpoint *bpts, *wpts;
   1507  1.3  christos   struct arch_lwp_info *arm_lwp_info = lwp->arch_private;
   1508  1.3  christos 
   1509  1.3  christos   pid = ptid_get_lwp (lwp->ptid);
   1510  1.3  christos   bpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->bpts;
   1511  1.3  christos   wpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->wpts;
   1512  1.3  christos 
   1513  1.3  christos   /* NULL means this is the main thread still going through the shell,
   1514  1.3  christos      or, no watchpoint has been set yet.  In that case, there's
   1515  1.3  christos      nothing to do.  */
   1516  1.3  christos   if (arm_lwp_info == NULL)
   1517  1.3  christos     return;
   1518  1.3  christos 
   1519  1.3  christos   for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
   1520  1.3  christos     if (arm_lwp_info->bpts_changed[i])
   1521  1.3  christos       {
   1522  1.3  christos         errno = 0;
   1523  1.3  christos         if (arm_hwbp_control_is_enabled (bpts[i].control))
   1524  1.3  christos           if (ptrace (PTRACE_SETHBPREGS, pid,
   1525  1.3  christos               (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
   1526  1.3  christos             perror_with_name (_("Unexpected error setting breakpoint"));
   1527  1.3  christos 
   1528  1.3  christos         if (bpts[i].control != 0)
   1529  1.3  christos           if (ptrace (PTRACE_SETHBPREGS, pid,
   1530  1.3  christos               (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
   1531  1.3  christos             perror_with_name (_("Unexpected error setting breakpoint"));
   1532  1.3  christos 
   1533  1.3  christos         arm_lwp_info->bpts_changed[i] = 0;
   1534  1.3  christos       }
   1535  1.1  christos 
   1536  1.3  christos   for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
   1537  1.3  christos     if (arm_lwp_info->wpts_changed[i])
   1538  1.3  christos       {
   1539  1.3  christos         errno = 0;
   1540  1.3  christos         if (arm_hwbp_control_is_enabled (wpts[i].control))
   1541  1.3  christos           if (ptrace (PTRACE_SETHBPREGS, pid,
   1542  1.3  christos               (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
   1543  1.3  christos             perror_with_name (_("Unexpected error setting watchpoint"));
   1544  1.3  christos 
   1545  1.3  christos         if (wpts[i].control != 0)
   1546  1.3  christos           if (ptrace (PTRACE_SETHBPREGS, pid,
   1547  1.3  christos               (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
   1548  1.3  christos             perror_with_name (_("Unexpected error setting watchpoint"));
   1549  1.1  christos 
   1550  1.3  christos         arm_lwp_info->wpts_changed[i] = 0;
   1551  1.3  christos       }
   1552  1.3  christos }
   1553  1.1  christos 
   1554  1.3  christos /* linux_nat_new_fork hook.  */
   1555  1.1  christos 
   1556  1.3  christos static void
   1557  1.3  christos arm_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
   1558  1.3  christos {
   1559  1.3  christos   pid_t parent_pid;
   1560  1.3  christos   struct arm_linux_debug_reg_state *parent_state;
   1561  1.3  christos   struct arm_linux_debug_reg_state *child_state;
   1562  1.3  christos 
   1563  1.3  christos   /* NULL means no watchpoint has ever been set in the parent.  In
   1564  1.3  christos      that case, there's nothing to do.  */
   1565  1.3  christos   if (parent->arch_private == NULL)
   1566  1.3  christos     return;
   1567  1.1  christos 
   1568  1.3  christos   /* GDB core assumes the child inherits the watchpoints/hw
   1569  1.3  christos      breakpoints of the parent, and will remove them all from the
   1570  1.3  christos      forked off process.  Copy the debug registers mirrors into the
   1571  1.3  christos      new process so that all breakpoints and watchpoints can be
   1572  1.3  christos      removed together.  */
   1573  1.3  christos 
   1574  1.3  christos   parent_pid = ptid_get_pid (parent->ptid);
   1575  1.3  christos   parent_state = arm_linux_get_debug_reg_state (parent_pid);
   1576  1.3  christos   child_state = arm_linux_get_debug_reg_state (child_pid);
   1577  1.3  christos   *child_state = *parent_state;
   1578  1.1  christos }
   1579  1.1  christos 
   1580  1.1  christos void _initialize_arm_linux_nat (void);
   1581  1.1  christos 
   1582  1.1  christos void
   1583  1.1  christos _initialize_arm_linux_nat (void)
   1584  1.1  christos {
   1585  1.1  christos   struct target_ops *t;
   1586  1.1  christos 
   1587  1.1  christos   /* Fill in the generic GNU/Linux methods.  */
   1588  1.1  christos   t = linux_target ();
   1589  1.1  christos 
   1590  1.1  christos   /* Add our register access methods.  */
   1591  1.1  christos   t->to_fetch_registers = arm_linux_fetch_inferior_registers;
   1592  1.1  christos   t->to_store_registers = arm_linux_store_inferior_registers;
   1593  1.1  christos 
   1594  1.1  christos   /* Add our hardware breakpoint and watchpoint implementation.  */
   1595  1.1  christos   t->to_can_use_hw_breakpoint = arm_linux_can_use_hw_breakpoint;
   1596  1.1  christos   t->to_insert_hw_breakpoint = arm_linux_insert_hw_breakpoint;
   1597  1.1  christos   t->to_remove_hw_breakpoint = arm_linux_remove_hw_breakpoint;
   1598  1.1  christos   t->to_region_ok_for_hw_watchpoint = arm_linux_region_ok_for_hw_watchpoint;
   1599  1.1  christos   t->to_insert_watchpoint = arm_linux_insert_watchpoint;
   1600  1.1  christos   t->to_remove_watchpoint = arm_linux_remove_watchpoint;
   1601  1.1  christos   t->to_stopped_by_watchpoint = arm_linux_stopped_by_watchpoint;
   1602  1.1  christos   t->to_stopped_data_address = arm_linux_stopped_data_address;
   1603  1.1  christos   t->to_watchpoint_addr_within_range = arm_linux_watchpoint_addr_within_range;
   1604  1.1  christos 
   1605  1.1  christos   t->to_read_description = arm_linux_read_description;
   1606  1.1  christos 
   1607  1.1  christos   /* Register the target.  */
   1608  1.1  christos   linux_nat_add_target (t);
   1609  1.1  christos 
   1610  1.3  christos   /* Handle thread creation and exit.  */
   1611  1.1  christos   linux_nat_set_new_thread (t, arm_linux_new_thread);
   1612  1.3  christos   linux_nat_set_prepare_to_resume (t, arm_linux_prepare_to_resume);
   1613  1.3  christos 
   1614  1.3  christos   /* Handle process creation and exit.  */
   1615  1.3  christos   linux_nat_set_new_fork (t, arm_linux_new_fork);
   1616  1.3  christos   linux_nat_set_forget_process (t, arm_linux_forget_process);
   1617  1.1  christos }
   1618