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