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