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