Fixes for unpredictable nops and 26-bit versions of teq,tst,cmn,cmp.
[binutils-gdb.git] / gdb / arm-linux-nat.c
blobfb65a5d3f9b37ebc8dfea12bde568493cdcacebb
1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
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 "observer.h"
28 #include "gdbthread.h"
30 #include "arm-tdep.h"
31 #include "arm-linux-tdep.h"
32 #include "aarch32-linux-nat.h"
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>
40 #include "nat/linux-ptrace.h"
42 /* Prototypes for supply_gregset etc. */
43 #include "gregset.h"
45 /* Defines ps_err_e, struct ps_prochandle. */
46 #include "gdb_proc_service.h"
48 #ifndef PTRACE_GET_THREAD_AREA
49 #define PTRACE_GET_THREAD_AREA 22
50 #endif
52 #ifndef PTRACE_GETWMMXREGS
53 #define PTRACE_GETWMMXREGS 18
54 #define PTRACE_SETWMMXREGS 19
55 #endif
57 #ifndef PTRACE_GETVFPREGS
58 #define PTRACE_GETVFPREGS 27
59 #define PTRACE_SETVFPREGS 28
60 #endif
62 #ifndef PTRACE_GETHBPREGS
63 #define PTRACE_GETHBPREGS 29
64 #define PTRACE_SETHBPREGS 30
65 #endif
67 extern int arm_apcs_32;
69 /* Get the whole floating point state of the process and store it
70 into regcache. */
72 static void
73 fetch_fpregs (struct regcache *regcache)
75 int ret, regno, tid;
76 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
78 /* Get the thread id for the ptrace call. */
79 tid = ptid_get_lwp (inferior_ptid);
81 /* Read the floating point state. */
82 if (have_ptrace_getregset == TRIBOOL_TRUE)
84 struct iovec iov;
86 iov.iov_base = &fp;
87 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
89 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
91 else
92 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
94 if (ret < 0)
96 warning (_("Unable to fetch the floating point registers."));
97 return;
100 /* Fetch fpsr. */
101 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
102 fp + NWFPE_FPSR_OFFSET);
104 /* Fetch the floating point registers. */
105 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
106 supply_nwfpe_register (regcache, regno, fp);
109 /* Save the whole floating point state of the process using
110 the contents from regcache. */
112 static void
113 store_fpregs (const struct regcache *regcache)
115 int ret, regno, tid;
116 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
118 /* Get the thread id for the ptrace call. */
119 tid = ptid_get_lwp (inferior_ptid);
121 /* Read the floating point state. */
122 if (have_ptrace_getregset == TRIBOOL_TRUE)
124 elf_fpregset_t fpregs;
125 struct iovec iov;
127 iov.iov_base = &fpregs;
128 iov.iov_len = sizeof (fpregs);
130 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
132 else
133 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
135 if (ret < 0)
137 warning (_("Unable to fetch the floating point registers."));
138 return;
141 /* Store fpsr. */
142 if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
143 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
145 /* Store the floating point registers. */
146 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
147 if (REG_VALID == regcache_register_status (regcache, regno))
148 collect_nwfpe_register (regcache, regno, fp);
150 if (have_ptrace_getregset == TRIBOOL_TRUE)
152 struct iovec iov;
154 iov.iov_base = &fp;
155 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
157 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
159 else
160 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
162 if (ret < 0)
164 warning (_("Unable to store floating point registers."));
165 return;
169 /* Fetch all general registers of the process and store into
170 regcache. */
172 static void
173 fetch_regs (struct regcache *regcache)
175 int ret, regno, tid;
176 elf_gregset_t regs;
178 /* Get the thread id for the ptrace call. */
179 tid = ptid_get_lwp (inferior_ptid);
181 if (have_ptrace_getregset == TRIBOOL_TRUE)
183 struct iovec iov;
185 iov.iov_base = &regs;
186 iov.iov_len = sizeof (regs);
188 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
190 else
191 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
193 if (ret < 0)
195 warning (_("Unable to fetch general registers."));
196 return;
199 aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, arm_apcs_32);
202 static void
203 store_regs (const struct regcache *regcache)
205 int ret, regno, tid;
206 elf_gregset_t regs;
208 /* Get the thread id for the ptrace call. */
209 tid = ptid_get_lwp (inferior_ptid);
211 /* Fetch the general registers. */
212 if (have_ptrace_getregset == TRIBOOL_TRUE)
214 struct iovec iov;
216 iov.iov_base = &regs;
217 iov.iov_len = sizeof (regs);
219 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
221 else
222 ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
224 if (ret < 0)
226 warning (_("Unable to fetch general registers."));
227 return;
230 aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, arm_apcs_32);
232 if (have_ptrace_getregset == TRIBOOL_TRUE)
234 struct iovec iov;
236 iov.iov_base = &regs;
237 iov.iov_len = sizeof (regs);
239 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
241 else
242 ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
244 if (ret < 0)
246 warning (_("Unable to store general registers."));
247 return;
251 /* Fetch all WMMX registers of the process and store into
252 regcache. */
254 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
256 static void
257 fetch_wmmx_regs (struct regcache *regcache)
259 char regbuf[IWMMXT_REGS_SIZE];
260 int ret, regno, tid;
262 /* Get the thread id for the ptrace call. */
263 tid = ptid_get_lwp (inferior_ptid);
265 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
266 if (ret < 0)
268 warning (_("Unable to fetch WMMX registers."));
269 return;
272 for (regno = 0; regno < 16; regno++)
273 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
274 &regbuf[regno * 8]);
276 for (regno = 0; regno < 2; regno++)
277 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
278 &regbuf[16 * 8 + regno * 4]);
280 for (regno = 0; regno < 4; regno++)
281 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
282 &regbuf[16 * 8 + 2 * 4 + regno * 4]);
285 static void
286 store_wmmx_regs (const struct regcache *regcache)
288 char regbuf[IWMMXT_REGS_SIZE];
289 int ret, regno, tid;
291 /* Get the thread id for the ptrace call. */
292 tid = ptid_get_lwp (inferior_ptid);
294 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
295 if (ret < 0)
297 warning (_("Unable to fetch WMMX registers."));
298 return;
301 for (regno = 0; regno < 16; regno++)
302 if (REG_VALID == regcache_register_status (regcache,
303 regno + ARM_WR0_REGNUM))
304 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
305 &regbuf[regno * 8]);
307 for (regno = 0; regno < 2; regno++)
308 if (REG_VALID == regcache_register_status (regcache,
309 regno + ARM_WCSSF_REGNUM))
310 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
311 &regbuf[16 * 8 + regno * 4]);
313 for (regno = 0; regno < 4; regno++)
314 if (REG_VALID == regcache_register_status (regcache,
315 regno + ARM_WCGR0_REGNUM))
316 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
317 &regbuf[16 * 8 + 2 * 4 + regno * 4]);
319 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
321 if (ret < 0)
323 warning (_("Unable to store WMMX registers."));
324 return;
328 static void
329 fetch_vfp_regs (struct regcache *regcache)
331 gdb_byte regbuf[VFP_REGS_SIZE];
332 int ret, regno, tid;
333 struct gdbarch *gdbarch = get_regcache_arch (regcache);
334 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
336 /* Get the thread id for the ptrace call. */
337 tid = ptid_get_lwp (inferior_ptid);
339 if (have_ptrace_getregset == TRIBOOL_TRUE)
341 struct iovec iov;
343 iov.iov_base = regbuf;
344 iov.iov_len = VFP_REGS_SIZE;
345 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
347 else
348 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
350 if (ret < 0)
352 warning (_("Unable to fetch VFP registers."));
353 return;
356 aarch32_vfp_regcache_supply (regcache, regbuf,
357 tdep->vfp_register_count);
360 static void
361 store_vfp_regs (const struct regcache *regcache)
363 gdb_byte regbuf[VFP_REGS_SIZE];
364 int ret, regno, tid;
365 struct gdbarch *gdbarch = get_regcache_arch (regcache);
366 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
368 /* Get the thread id for the ptrace call. */
369 tid = ptid_get_lwp (inferior_ptid);
371 if (have_ptrace_getregset == TRIBOOL_TRUE)
373 struct iovec iov;
375 iov.iov_base = regbuf;
376 iov.iov_len = VFP_REGS_SIZE;
377 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
379 else
380 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
382 if (ret < 0)
384 warning (_("Unable to fetch VFP registers (for update)."));
385 return;
388 aarch32_vfp_regcache_collect (regcache, regbuf,
389 tdep->vfp_register_count);
391 if (have_ptrace_getregset == TRIBOOL_TRUE)
393 struct iovec iov;
395 iov.iov_base = regbuf;
396 iov.iov_len = VFP_REGS_SIZE;
397 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iov);
399 else
400 ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
402 if (ret < 0)
404 warning (_("Unable to store VFP registers."));
405 return;
409 /* Fetch registers from the child process. Fetch all registers if
410 regno == -1, otherwise fetch all general registers or all floating
411 point registers depending upon the value of regno. */
413 static void
414 arm_linux_fetch_inferior_registers (struct target_ops *ops,
415 struct regcache *regcache, int regno)
417 struct gdbarch *gdbarch = get_regcache_arch (regcache);
418 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
420 if (-1 == regno)
422 fetch_regs (regcache);
423 fetch_fpregs (regcache);
424 if (tdep->have_wmmx_registers)
425 fetch_wmmx_regs (regcache);
426 if (tdep->vfp_register_count > 0)
427 fetch_vfp_regs (regcache);
429 else
431 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
432 fetch_regs (regcache);
433 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
434 fetch_fpregs (regcache);
435 else if (tdep->have_wmmx_registers
436 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
437 fetch_wmmx_regs (regcache);
438 else if (tdep->vfp_register_count > 0
439 && regno >= ARM_D0_REGNUM
440 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
441 fetch_vfp_regs (regcache);
445 /* Store registers back into the inferior. Store all registers if
446 regno == -1, otherwise store all general registers or all floating
447 point registers depending upon the value of regno. */
449 static void
450 arm_linux_store_inferior_registers (struct target_ops *ops,
451 struct regcache *regcache, int regno)
453 struct gdbarch *gdbarch = get_regcache_arch (regcache);
454 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
456 if (-1 == regno)
458 store_regs (regcache);
459 store_fpregs (regcache);
460 if (tdep->have_wmmx_registers)
461 store_wmmx_regs (regcache);
462 if (tdep->vfp_register_count > 0)
463 store_vfp_regs (regcache);
465 else
467 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
468 store_regs (regcache);
469 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
470 store_fpregs (regcache);
471 else if (tdep->have_wmmx_registers
472 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
473 store_wmmx_regs (regcache);
474 else if (tdep->vfp_register_count > 0
475 && regno >= ARM_D0_REGNUM
476 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
477 store_vfp_regs (regcache);
481 /* Wrapper functions for the standard regset handling, used by
482 thread debugging. */
484 void
485 fill_gregset (const struct regcache *regcache,
486 gdb_gregset_t *gregsetp, int regno)
488 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
491 void
492 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
494 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
497 void
498 fill_fpregset (const struct regcache *regcache,
499 gdb_fpregset_t *fpregsetp, int regno)
501 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
504 /* Fill GDB's register array with the floating-point register values
505 in *fpregsetp. */
507 void
508 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
510 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
513 /* Fetch the thread-local storage pointer for libthread_db. */
515 ps_err_e
516 ps_get_thread_area (const struct ps_prochandle *ph,
517 lwpid_t lwpid, int idx, void **base)
519 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
520 return PS_ERR;
522 /* IDX is the bias from the thread pointer to the beginning of the
523 thread descriptor. It has to be subtracted due to implementation
524 quirks in libthread_db. */
525 *base = (void *) ((char *)*base - idx);
527 return PS_OK;
530 static const struct target_desc *
531 arm_linux_read_description (struct target_ops *ops)
533 CORE_ADDR arm_hwcap = 0;
535 if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
537 elf_gregset_t gpregs;
538 struct iovec iov;
539 int tid = ptid_get_lwp (inferior_ptid);
541 iov.iov_base = &gpregs;
542 iov.iov_len = sizeof (gpregs);
544 /* Check if PTRACE_GETREGSET works. */
545 if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) < 0)
546 have_ptrace_getregset = TRIBOOL_FALSE;
547 else
548 have_ptrace_getregset = TRIBOOL_TRUE;
551 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
553 return ops->beneath->to_read_description (ops->beneath);
556 if (arm_hwcap & HWCAP_IWMMXT)
557 return tdesc_arm_with_iwmmxt;
559 if (arm_hwcap & HWCAP_VFP)
561 int pid;
562 char *buf;
563 const struct target_desc * result = NULL;
565 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
566 Neon with VFPv3-D32. */
567 if (arm_hwcap & HWCAP_NEON)
568 result = tdesc_arm_with_neon;
569 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
570 result = tdesc_arm_with_vfpv3;
571 else
572 result = tdesc_arm_with_vfpv2;
574 /* Now make sure that the kernel supports reading these
575 registers. Support was added in 2.6.30. */
576 pid = ptid_get_lwp (inferior_ptid);
577 errno = 0;
578 buf = alloca (VFP_REGS_SIZE);
579 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
580 && errno == EIO)
581 result = NULL;
583 return result;
586 return ops->beneath->to_read_description (ops->beneath);
589 /* Information describing the hardware breakpoint capabilities. */
590 struct arm_linux_hwbp_cap
592 gdb_byte arch;
593 gdb_byte max_wp_length;
594 gdb_byte wp_count;
595 gdb_byte bp_count;
598 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
599 assume a maximum number of supported break-/watchpoints. */
600 #define MAX_BPTS 16
601 #define MAX_WPTS 16
603 /* Get hold of the Hardware Breakpoint information for the target we are
604 attached to. Returns NULL if the kernel doesn't support Hardware
605 breakpoints at all, or a pointer to the information structure. */
606 static const struct arm_linux_hwbp_cap *
607 arm_linux_get_hwbp_cap (void)
609 /* The info structure we return. */
610 static struct arm_linux_hwbp_cap info;
612 /* Is INFO in a good state? -1 means that no attempt has been made to
613 initialize INFO; 0 means an attempt has been made, but it failed; 1
614 means INFO is in an initialized state. */
615 static int available = -1;
617 if (available == -1)
619 int tid;
620 unsigned int val;
622 tid = ptid_get_lwp (inferior_ptid);
623 if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
624 available = 0;
625 else
627 info.arch = (gdb_byte)((val >> 24) & 0xff);
628 info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
629 info.wp_count = (gdb_byte)((val >> 8) & 0xff);
630 info.bp_count = (gdb_byte)(val & 0xff);
632 if (info.wp_count > MAX_WPTS)
634 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
635 supports %d"), MAX_WPTS, info.wp_count);
636 info.wp_count = MAX_WPTS;
639 if (info.bp_count > MAX_BPTS)
641 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
642 supports %d"), MAX_BPTS, info.bp_count);
643 info.bp_count = MAX_BPTS;
645 available = (info.arch != 0);
649 return available == 1 ? &info : NULL;
652 /* How many hardware breakpoints are available? */
653 static int
654 arm_linux_get_hw_breakpoint_count (void)
656 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
657 return cap != NULL ? cap->bp_count : 0;
660 /* How many hardware watchpoints are available? */
661 static int
662 arm_linux_get_hw_watchpoint_count (void)
664 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
665 return cap != NULL ? cap->wp_count : 0;
668 /* Have we got a free break-/watch-point available for use? Returns -1 if
669 there is not an appropriate resource available, otherwise returns 1. */
670 static int
671 arm_linux_can_use_hw_breakpoint (struct target_ops *self,
672 enum bptype type,
673 int cnt, int ot)
675 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
676 || type == bp_access_watchpoint || type == bp_watchpoint)
678 int count = arm_linux_get_hw_watchpoint_count ();
680 if (count == 0)
681 return 0;
682 else if (cnt + ot > count)
683 return -1;
685 else if (type == bp_hardware_breakpoint)
687 int count = arm_linux_get_hw_breakpoint_count ();
689 if (count == 0)
690 return 0;
691 else if (cnt > count)
692 return -1;
694 else
695 gdb_assert (FALSE);
697 return 1;
700 /* Enum describing the different types of ARM hardware break-/watch-points. */
701 typedef enum
703 arm_hwbp_break = 0,
704 arm_hwbp_load = 1,
705 arm_hwbp_store = 2,
706 arm_hwbp_access = 3
707 } arm_hwbp_type;
709 /* Type describing an ARM Hardware Breakpoint Control register value. */
710 typedef unsigned int arm_hwbp_control_t;
712 /* Structure used to keep track of hardware break-/watch-points. */
713 struct arm_linux_hw_breakpoint
715 /* Address to break on, or being watched. */
716 unsigned int address;
717 /* Control register for break-/watch- point. */
718 arm_hwbp_control_t control;
721 /* Structure containing arrays of per process hardware break-/watchpoints
722 for caching address and control information.
724 The Linux ptrace interface to hardware break-/watch-points presents the
725 values in a vector centred around 0 (which is used fo generic information).
726 Positive indicies refer to breakpoint addresses/control registers, negative
727 indices to watchpoint addresses/control registers.
729 The Linux vector is indexed as follows:
730 -((i << 1) + 2): Control register for watchpoint i.
731 -((i << 1) + 1): Address register for watchpoint i.
732 0: Information register.
733 ((i << 1) + 1): Address register for breakpoint i.
734 ((i << 1) + 2): Control register for breakpoint i.
736 This structure is used as a per-thread cache of the state stored by the
737 kernel, so that we don't need to keep calling into the kernel to find a
738 free breakpoint.
740 We treat break-/watch-points with their enable bit clear as being deleted.
742 struct arm_linux_debug_reg_state
744 /* Hardware breakpoints for this process. */
745 struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
746 /* Hardware watchpoints for this process. */
747 struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
750 /* Per-process arch-specific data we want to keep. */
751 struct arm_linux_process_info
753 /* Linked list. */
754 struct arm_linux_process_info *next;
755 /* The process identifier. */
756 pid_t pid;
757 /* Hardware break-/watchpoints state information. */
758 struct arm_linux_debug_reg_state state;
762 /* Per-thread arch-specific data we want to keep. */
763 struct arch_lwp_info
765 /* Non-zero if our copy differs from what's recorded in the thread. */
766 char bpts_changed[MAX_BPTS];
767 char wpts_changed[MAX_WPTS];
770 static struct arm_linux_process_info *arm_linux_process_list = NULL;
772 /* Find process data for process PID. */
774 static struct arm_linux_process_info *
775 arm_linux_find_process_pid (pid_t pid)
777 struct arm_linux_process_info *proc;
779 for (proc = arm_linux_process_list; proc; proc = proc->next)
780 if (proc->pid == pid)
781 return proc;
783 return NULL;
786 /* Add process data for process PID. Returns newly allocated info
787 object. */
789 static struct arm_linux_process_info *
790 arm_linux_add_process (pid_t pid)
792 struct arm_linux_process_info *proc;
794 proc = xcalloc (1, sizeof (*proc));
795 proc->pid = pid;
797 proc->next = arm_linux_process_list;
798 arm_linux_process_list = proc;
800 return proc;
803 /* Get data specific info for process PID, creating it if necessary.
804 Never returns NULL. */
806 static struct arm_linux_process_info *
807 arm_linux_process_info_get (pid_t pid)
809 struct arm_linux_process_info *proc;
811 proc = arm_linux_find_process_pid (pid);
812 if (proc == NULL)
813 proc = arm_linux_add_process (pid);
815 return proc;
818 /* Called whenever GDB is no longer debugging process PID. It deletes
819 data structures that keep track of debug register state. */
821 static void
822 arm_linux_forget_process (pid_t pid)
824 struct arm_linux_process_info *proc, **proc_link;
826 proc = arm_linux_process_list;
827 proc_link = &arm_linux_process_list;
829 while (proc != NULL)
831 if (proc->pid == pid)
833 *proc_link = proc->next;
835 xfree (proc);
836 return;
839 proc_link = &proc->next;
840 proc = *proc_link;
844 /* Get hardware break-/watchpoint state for process PID. */
846 static struct arm_linux_debug_reg_state *
847 arm_linux_get_debug_reg_state (pid_t pid)
849 return &arm_linux_process_info_get (pid)->state;
852 /* Initialize an ARM hardware break-/watch-point control register value.
853 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
854 type of break-/watch-point; ENABLE indicates whether the point is enabled.
856 static arm_hwbp_control_t
857 arm_hwbp_control_initialize (unsigned byte_address_select,
858 arm_hwbp_type hwbp_type,
859 int enable)
861 gdb_assert ((byte_address_select & ~0xffU) == 0);
862 gdb_assert (hwbp_type != arm_hwbp_break
863 || ((byte_address_select & 0xfU) != 0));
865 return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
868 /* Does the breakpoint control value CONTROL have the enable bit set? */
869 static int
870 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
872 return control & 0x1;
875 /* Change a breakpoint control word so that it is in the disabled state. */
876 static arm_hwbp_control_t
877 arm_hwbp_control_disable (arm_hwbp_control_t control)
879 return control & ~0x1;
882 /* Initialise the hardware breakpoint structure P. The breakpoint will be
883 enabled, and will point to the placed address of BP_TGT. */
884 static void
885 arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
886 struct bp_target_info *bp_tgt,
887 struct arm_linux_hw_breakpoint *p)
889 unsigned mask;
890 CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;
892 /* We have to create a mask for the control register which says which bits
893 of the word pointed to by address to break on. */
894 if (arm_pc_is_thumb (gdbarch, address))
896 mask = 0x3;
897 address &= ~1;
899 else
901 mask = 0xf;
902 address &= ~3;
905 p->address = (unsigned int) address;
906 p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
909 /* Get the ARM hardware breakpoint type from the TYPE value we're
910 given when asked to set a watchpoint. */
911 static arm_hwbp_type
912 arm_linux_get_hwbp_type (enum target_hw_bp_type type)
914 if (type == hw_read)
915 return arm_hwbp_load;
916 else if (type == hw_write)
917 return arm_hwbp_store;
918 else
919 return arm_hwbp_access;
922 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
923 to LEN. The type of watchpoint is given in RW. */
924 static void
925 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len,
926 enum target_hw_bp_type type,
927 struct arm_linux_hw_breakpoint *p)
929 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
930 unsigned mask;
932 gdb_assert (cap != NULL);
933 gdb_assert (cap->max_wp_length != 0);
935 mask = (1 << len) - 1;
937 p->address = (unsigned int) addr;
938 p->control = arm_hwbp_control_initialize (mask,
939 arm_linux_get_hwbp_type (type), 1);
942 /* Are two break-/watch-points equal? */
943 static int
944 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
945 const struct arm_linux_hw_breakpoint *p2)
947 return p1->address == p2->address && p1->control == p2->control;
950 /* Callback to mark a watch-/breakpoint to be updated in all threads of
951 the current process. */
953 struct update_registers_data
955 int watch;
956 int index;
959 static int
960 update_registers_callback (struct lwp_info *lwp, void *arg)
962 struct update_registers_data *data = (struct update_registers_data *) arg;
964 if (lwp->arch_private == NULL)
965 lwp->arch_private = XCNEW (struct arch_lwp_info);
967 /* The actual update is done later just before resuming the lwp,
968 we just mark that the registers need updating. */
969 if (data->watch)
970 lwp->arch_private->wpts_changed[data->index] = 1;
971 else
972 lwp->arch_private->bpts_changed[data->index] = 1;
974 /* If the lwp isn't stopped, force it to momentarily pause, so
975 we can update its breakpoint registers. */
976 if (!lwp->stopped)
977 linux_stop_lwp (lwp);
979 return 0;
982 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
983 =1) BPT for thread TID. */
984 static void
985 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
986 int watchpoint)
988 int pid;
989 ptid_t pid_ptid;
990 gdb_byte count, i;
991 struct arm_linux_hw_breakpoint* bpts;
992 struct update_registers_data data;
994 pid = ptid_get_pid (inferior_ptid);
995 pid_ptid = pid_to_ptid (pid);
997 if (watchpoint)
999 count = arm_linux_get_hw_watchpoint_count ();
1000 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1002 else
1004 count = arm_linux_get_hw_breakpoint_count ();
1005 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1008 for (i = 0; i < count; ++i)
1009 if (!arm_hwbp_control_is_enabled (bpts[i].control))
1011 data.watch = watchpoint;
1012 data.index = i;
1013 bpts[i] = *bpt;
1014 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1015 break;
1018 gdb_assert (i != count);
1021 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1022 (WATCHPOINT = 1) BPT for thread TID. */
1023 static void
1024 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
1025 int watchpoint)
1027 int pid;
1028 gdb_byte count, i;
1029 ptid_t pid_ptid;
1030 struct arm_linux_hw_breakpoint* bpts;
1031 struct update_registers_data data;
1033 pid = ptid_get_pid (inferior_ptid);
1034 pid_ptid = pid_to_ptid (pid);
1036 if (watchpoint)
1038 count = arm_linux_get_hw_watchpoint_count ();
1039 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1041 else
1043 count = arm_linux_get_hw_breakpoint_count ();
1044 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1047 for (i = 0; i < count; ++i)
1048 if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
1050 data.watch = watchpoint;
1051 data.index = i;
1052 bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
1053 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1054 break;
1057 gdb_assert (i != count);
1060 /* Insert a Hardware breakpoint. */
1061 static int
1062 arm_linux_insert_hw_breakpoint (struct target_ops *self,
1063 struct gdbarch *gdbarch,
1064 struct bp_target_info *bp_tgt)
1066 struct lwp_info *lp;
1067 struct arm_linux_hw_breakpoint p;
1069 if (arm_linux_get_hw_breakpoint_count () == 0)
1070 return -1;
1072 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1074 arm_linux_insert_hw_breakpoint1 (&p, 0);
1076 return 0;
1079 /* Remove a hardware breakpoint. */
1080 static int
1081 arm_linux_remove_hw_breakpoint (struct target_ops *self,
1082 struct gdbarch *gdbarch,
1083 struct bp_target_info *bp_tgt)
1085 struct lwp_info *lp;
1086 struct arm_linux_hw_breakpoint p;
1088 if (arm_linux_get_hw_breakpoint_count () == 0)
1089 return -1;
1091 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1093 arm_linux_remove_hw_breakpoint1 (&p, 0);
1095 return 0;
1098 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1099 ADDR? */
1100 static int
1101 arm_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
1102 CORE_ADDR addr, int len)
1104 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1105 CORE_ADDR max_wp_length, aligned_addr;
1107 /* Can not set watchpoints for zero or negative lengths. */
1108 if (len <= 0)
1109 return 0;
1111 /* Need to be able to use the ptrace interface. */
1112 if (cap == NULL || cap->wp_count == 0)
1113 return 0;
1115 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1116 range covered by a watchpoint. */
1117 max_wp_length = (CORE_ADDR)cap->max_wp_length;
1118 aligned_addr = addr & ~(max_wp_length - 1);
1120 if (aligned_addr + max_wp_length < addr + len)
1121 return 0;
1123 /* The current ptrace interface can only handle watchpoints that are a
1124 power of 2. */
1125 if ((len & (len - 1)) != 0)
1126 return 0;
1128 /* All tests passed so we must be able to set a watchpoint. */
1129 return 1;
1132 /* Insert a Hardware breakpoint. */
1133 static int
1134 arm_linux_insert_watchpoint (struct target_ops *self,
1135 CORE_ADDR addr, int len,
1136 enum target_hw_bp_type rw,
1137 struct expression *cond)
1139 struct lwp_info *lp;
1140 struct arm_linux_hw_breakpoint p;
1142 if (arm_linux_get_hw_watchpoint_count () == 0)
1143 return -1;
1145 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1147 arm_linux_insert_hw_breakpoint1 (&p, 1);
1149 return 0;
1152 /* Remove a hardware breakpoint. */
1153 static int
1154 arm_linux_remove_watchpoint (struct target_ops *self, CORE_ADDR addr,
1155 int len, enum target_hw_bp_type rw,
1156 struct expression *cond)
1158 struct lwp_info *lp;
1159 struct arm_linux_hw_breakpoint p;
1161 if (arm_linux_get_hw_watchpoint_count () == 0)
1162 return -1;
1164 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1166 arm_linux_remove_hw_breakpoint1 (&p, 1);
1168 return 0;
1171 /* What was the data address the target was stopped on accessing. */
1172 static int
1173 arm_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1175 siginfo_t siginfo;
1176 int slot;
1178 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
1179 return 0;
1181 /* This must be a hardware breakpoint. */
1182 if (siginfo.si_signo != SIGTRAP
1183 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1184 return 0;
1186 /* We must be able to set hardware watchpoints. */
1187 if (arm_linux_get_hw_watchpoint_count () == 0)
1188 return 0;
1190 slot = siginfo.si_errno;
1192 /* If we are in a positive slot then we're looking at a breakpoint and not
1193 a watchpoint. */
1194 if (slot >= 0)
1195 return 0;
1197 *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
1198 return 1;
1201 /* Has the target been stopped by hitting a watchpoint? */
1202 static int
1203 arm_linux_stopped_by_watchpoint (struct target_ops *ops)
1205 CORE_ADDR addr;
1206 return arm_linux_stopped_data_address (ops, &addr);
1209 static int
1210 arm_linux_watchpoint_addr_within_range (struct target_ops *target,
1211 CORE_ADDR addr,
1212 CORE_ADDR start, int length)
1214 return start <= addr && start + length - 1 >= addr;
1217 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1218 in the parent thread to the child thread. */
1219 static void
1220 arm_linux_new_thread (struct lwp_info *lp)
1222 int i;
1223 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
1225 /* Mark that all the hardware breakpoint/watchpoint register pairs
1226 for this thread need to be initialized. */
1228 for (i = 0; i < MAX_BPTS; i++)
1230 info->bpts_changed[i] = 1;
1231 info->wpts_changed[i] = 1;
1234 lp->arch_private = info;
1237 /* Called when resuming a thread.
1238 The hardware debug registers are updated when there is any change. */
1240 static void
1241 arm_linux_prepare_to_resume (struct lwp_info *lwp)
1243 int pid, i;
1244 struct arm_linux_hw_breakpoint *bpts, *wpts;
1245 struct arch_lwp_info *arm_lwp_info = lwp->arch_private;
1247 pid = ptid_get_lwp (lwp->ptid);
1248 bpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->bpts;
1249 wpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->wpts;
1251 /* NULL means this is the main thread still going through the shell,
1252 or, no watchpoint has been set yet. In that case, there's
1253 nothing to do. */
1254 if (arm_lwp_info == NULL)
1255 return;
1257 for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
1258 if (arm_lwp_info->bpts_changed[i])
1260 errno = 0;
1261 if (arm_hwbp_control_is_enabled (bpts[i].control))
1262 if (ptrace (PTRACE_SETHBPREGS, pid,
1263 (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
1264 perror_with_name (_("Unexpected error setting breakpoint"));
1266 if (bpts[i].control != 0)
1267 if (ptrace (PTRACE_SETHBPREGS, pid,
1268 (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
1269 perror_with_name (_("Unexpected error setting breakpoint"));
1271 arm_lwp_info->bpts_changed[i] = 0;
1274 for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
1275 if (arm_lwp_info->wpts_changed[i])
1277 errno = 0;
1278 if (arm_hwbp_control_is_enabled (wpts[i].control))
1279 if (ptrace (PTRACE_SETHBPREGS, pid,
1280 (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
1281 perror_with_name (_("Unexpected error setting watchpoint"));
1283 if (wpts[i].control != 0)
1284 if (ptrace (PTRACE_SETHBPREGS, pid,
1285 (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
1286 perror_with_name (_("Unexpected error setting watchpoint"));
1288 arm_lwp_info->wpts_changed[i] = 0;
1292 /* linux_nat_new_fork hook. */
1294 static void
1295 arm_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
1297 pid_t parent_pid;
1298 struct arm_linux_debug_reg_state *parent_state;
1299 struct arm_linux_debug_reg_state *child_state;
1301 /* NULL means no watchpoint has ever been set in the parent. In
1302 that case, there's nothing to do. */
1303 if (parent->arch_private == NULL)
1304 return;
1306 /* GDB core assumes the child inherits the watchpoints/hw
1307 breakpoints of the parent, and will remove them all from the
1308 forked off process. Copy the debug registers mirrors into the
1309 new process so that all breakpoints and watchpoints can be
1310 removed together. */
1312 parent_pid = ptid_get_pid (parent->ptid);
1313 parent_state = arm_linux_get_debug_reg_state (parent_pid);
1314 child_state = arm_linux_get_debug_reg_state (child_pid);
1315 *child_state = *parent_state;
1318 void _initialize_arm_linux_nat (void);
1320 void
1321 _initialize_arm_linux_nat (void)
1323 struct target_ops *t;
1325 /* Fill in the generic GNU/Linux methods. */
1326 t = linux_target ();
1328 /* Add our register access methods. */
1329 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
1330 t->to_store_registers = arm_linux_store_inferior_registers;
1332 /* Add our hardware breakpoint and watchpoint implementation. */
1333 t->to_can_use_hw_breakpoint = arm_linux_can_use_hw_breakpoint;
1334 t->to_insert_hw_breakpoint = arm_linux_insert_hw_breakpoint;
1335 t->to_remove_hw_breakpoint = arm_linux_remove_hw_breakpoint;
1336 t->to_region_ok_for_hw_watchpoint = arm_linux_region_ok_for_hw_watchpoint;
1337 t->to_insert_watchpoint = arm_linux_insert_watchpoint;
1338 t->to_remove_watchpoint = arm_linux_remove_watchpoint;
1339 t->to_stopped_by_watchpoint = arm_linux_stopped_by_watchpoint;
1340 t->to_stopped_data_address = arm_linux_stopped_data_address;
1341 t->to_watchpoint_addr_within_range = arm_linux_watchpoint_addr_within_range;
1343 t->to_read_description = arm_linux_read_description;
1345 /* Register the target. */
1346 linux_nat_add_target (t);
1348 /* Handle thread creation and exit. */
1349 linux_nat_set_new_thread (t, arm_linux_new_thread);
1350 linux_nat_set_prepare_to_resume (t, arm_linux_prepare_to_resume);
1352 /* Handle process creation and exit. */
1353 linux_nat_set_new_fork (t, arm_linux_new_fork);
1354 linux_nat_set_forget_process (t, arm_linux_forget_process);