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/>. */
24 #include "linux-nat.h"
25 #include "target-descriptions.h"
28 #include "gdbthread.h"
31 #include "arm-linux-tdep.h"
33 #include <elf/common.h>
35 #include <sys/ptrace.h>
36 #include <sys/utsname.h>
37 #include <sys/procfs.h>
39 #include "nat/linux-ptrace.h"
41 /* Prototypes for supply_gregset etc. */
44 /* Defines ps_err_e, struct ps_prochandle. */
45 #include "gdb_proc_service.h"
47 #ifndef PTRACE_GET_THREAD_AREA
48 #define PTRACE_GET_THREAD_AREA 22
51 #ifndef PTRACE_GETWMMXREGS
52 #define PTRACE_GETWMMXREGS 18
53 #define PTRACE_SETWMMXREGS 19
56 #ifndef PTRACE_GETVFPREGS
57 #define PTRACE_GETVFPREGS 27
58 #define PTRACE_SETVFPREGS 28
61 #ifndef PTRACE_GETHBPREGS
62 #define PTRACE_GETHBPREGS 29
63 #define PTRACE_SETHBPREGS 30
66 extern int arm_apcs_32;
68 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
69 case we may be tracing more than one process at a time. In that
70 case, inferior_ptid will contain the main process ID and the
71 individual thread (process) ID. get_thread_id () is used to get
72 the thread id if it's available, and the process id otherwise. */
75 get_thread_id (ptid_t ptid)
77 int tid = ptid_get_lwp (ptid);
79 tid = ptid_get_pid (ptid);
83 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
85 /* Get the value of a particular register from the floating point
86 state of the process and store it into regcache. */
89 fetch_fpregister (struct regcache *regcache, int regno)
92 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
94 /* Get the thread id for the ptrace call. */
95 tid = GET_THREAD_ID (inferior_ptid);
97 /* Read the floating point state. */
98 if (have_ptrace_getregset == TRIBOOL_TRUE)
103 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
105 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
108 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
112 warning (_("Unable to fetch floating point register."));
117 if (ARM_FPS_REGNUM == regno)
118 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
119 fp + NWFPE_FPSR_OFFSET);
121 /* Fetch the floating point register. */
122 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
123 supply_nwfpe_register (regcache, regno, fp);
126 /* Get the whole floating point state of the process and store it
130 fetch_fpregs (struct regcache *regcache)
133 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
135 /* Get the thread id for the ptrace call. */
136 tid = GET_THREAD_ID (inferior_ptid);
138 /* Read the floating point state. */
139 if (have_ptrace_getregset == TRIBOOL_TRUE)
144 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
146 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
149 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
153 warning (_("Unable to fetch the floating point registers."));
158 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
159 fp + NWFPE_FPSR_OFFSET);
161 /* Fetch the floating point registers. */
162 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
163 supply_nwfpe_register (regcache, regno, fp);
166 /* Save a particular register into the floating point state of the
167 process using the contents from regcache. */
170 store_fpregister (const struct regcache *regcache, int regno)
173 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
175 /* Get the thread id for the ptrace call. */
176 tid = GET_THREAD_ID (inferior_ptid);
178 /* Read the floating point state. */
179 if (have_ptrace_getregset == TRIBOOL_TRUE)
184 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
186 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
189 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
193 warning (_("Unable to fetch the floating point registers."));
198 if (ARM_FPS_REGNUM == regno
199 && REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
200 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
202 /* Store the floating point register. */
203 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
204 collect_nwfpe_register (regcache, regno, fp);
206 if (have_ptrace_getregset == TRIBOOL_TRUE)
211 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
213 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
216 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
220 warning (_("Unable to store floating point register."));
225 /* Save the whole floating point state of the process using
226 the contents from regcache. */
229 store_fpregs (const struct regcache *regcache)
232 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
234 /* Get the thread id for the ptrace call. */
235 tid = GET_THREAD_ID (inferior_ptid);
237 /* Read the floating point state. */
238 if (have_ptrace_getregset == TRIBOOL_TRUE)
240 elf_fpregset_t fpregs;
243 iov.iov_base = &fpregs;
244 iov.iov_len = sizeof (fpregs);
246 ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iov);
249 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
253 warning (_("Unable to fetch the floating point registers."));
258 if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
259 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
261 /* Store the floating point registers. */
262 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
263 if (REG_VALID == regcache_register_status (regcache, regno))
264 collect_nwfpe_register (regcache, regno, fp);
266 if (have_ptrace_getregset == TRIBOOL_TRUE)
271 iov.iov_len = ARM_LINUX_SIZEOF_NWFPE;
273 ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iov);
276 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
280 warning (_("Unable to store floating point registers."));
285 /* Fetch a general register of the process and store into
289 fetch_register (struct regcache *regcache, int regno)
294 /* Get the thread id for the ptrace call. */
295 tid = GET_THREAD_ID (inferior_ptid);
297 if (have_ptrace_getregset == TRIBOOL_TRUE)
301 iov.iov_base = ®s;
302 iov.iov_len = sizeof (regs);
304 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
307 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
311 warning (_("Unable to fetch general register."));
315 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
316 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
318 if (ARM_PS_REGNUM == regno)
321 regcache_raw_supply (regcache, ARM_PS_REGNUM,
322 (char *) ®s[ARM_CPSR_GREGNUM]);
324 regcache_raw_supply (regcache, ARM_PS_REGNUM,
325 (char *) ®s[ARM_PC_REGNUM]);
328 if (ARM_PC_REGNUM == regno)
330 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
331 (get_regcache_arch (regcache),
332 regs[ARM_PC_REGNUM]);
333 regcache_raw_supply (regcache, ARM_PC_REGNUM,
334 (char *) ®s[ARM_PC_REGNUM]);
338 /* Fetch all general registers of the process and store into
342 fetch_regs (struct regcache *regcache)
347 /* Get the thread id for the ptrace call. */
348 tid = GET_THREAD_ID (inferior_ptid);
350 if (have_ptrace_getregset == TRIBOOL_TRUE)
354 iov.iov_base = ®s;
355 iov.iov_len = sizeof (regs);
357 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
360 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
364 warning (_("Unable to fetch general registers."));
368 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
369 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
372 regcache_raw_supply (regcache, ARM_PS_REGNUM,
373 (char *) ®s[ARM_CPSR_GREGNUM]);
375 regcache_raw_supply (regcache, ARM_PS_REGNUM,
376 (char *) ®s[ARM_PC_REGNUM]);
378 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
379 (get_regcache_arch (regcache), regs[ARM_PC_REGNUM]);
380 regcache_raw_supply (regcache, ARM_PC_REGNUM,
381 (char *) ®s[ARM_PC_REGNUM]);
384 /* Store all general registers of the process from the values in
388 store_register (const struct regcache *regcache, int regno)
393 if (REG_VALID != regcache_register_status (regcache, regno))
396 /* Get the thread id for the ptrace call. */
397 tid = GET_THREAD_ID (inferior_ptid);
399 /* Get the general registers from the process. */
400 if (have_ptrace_getregset == TRIBOOL_TRUE)
404 iov.iov_base = ®s;
405 iov.iov_len = sizeof (regs);
407 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
410 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
414 warning (_("Unable to fetch general registers."));
418 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
419 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
420 else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
421 regcache_raw_collect (regcache, regno,
422 (char *) ®s[ARM_CPSR_GREGNUM]);
423 else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
424 regcache_raw_collect (regcache, ARM_PC_REGNUM,
425 (char *) ®s[ARM_PC_REGNUM]);
427 if (have_ptrace_getregset == TRIBOOL_TRUE)
431 iov.iov_base = ®s;
432 iov.iov_len = sizeof (regs);
434 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
437 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
441 warning (_("Unable to store general register."));
447 store_regs (const struct regcache *regcache)
452 /* Get the thread id for the ptrace call. */
453 tid = GET_THREAD_ID (inferior_ptid);
455 /* Fetch the general registers. */
456 if (have_ptrace_getregset == TRIBOOL_TRUE)
460 iov.iov_base = ®s;
461 iov.iov_len = sizeof (regs);
463 ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov);
466 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
470 warning (_("Unable to fetch general registers."));
474 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
476 if (REG_VALID == regcache_register_status (regcache, regno))
477 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
480 if (arm_apcs_32 && REG_VALID == regcache_register_status (regcache, ARM_PS_REGNUM))
481 regcache_raw_collect (regcache, ARM_PS_REGNUM,
482 (char *) ®s[ARM_CPSR_GREGNUM]);
484 if (have_ptrace_getregset == TRIBOOL_TRUE)
488 iov.iov_base = ®s;
489 iov.iov_len = sizeof (regs);
491 ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iov);
494 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
498 warning (_("Unable to store general registers."));
503 /* Fetch all WMMX registers of the process and store into
506 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
509 fetch_wmmx_regs (struct regcache *regcache)
511 char regbuf[IWMMXT_REGS_SIZE];
514 /* Get the thread id for the ptrace call. */
515 tid = GET_THREAD_ID (inferior_ptid);
517 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
520 warning (_("Unable to fetch WMMX registers."));
524 for (regno = 0; regno < 16; regno++)
525 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
528 for (regno = 0; regno < 2; regno++)
529 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
530 ®buf[16 * 8 + regno * 4]);
532 for (regno = 0; regno < 4; regno++)
533 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
534 ®buf[16 * 8 + 2 * 4 + regno * 4]);
538 store_wmmx_regs (const struct regcache *regcache)
540 char regbuf[IWMMXT_REGS_SIZE];
543 /* Get the thread id for the ptrace call. */
544 tid = GET_THREAD_ID (inferior_ptid);
546 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
549 warning (_("Unable to fetch WMMX registers."));
553 for (regno = 0; regno < 16; regno++)
554 if (REG_VALID == regcache_register_status (regcache,
555 regno + ARM_WR0_REGNUM))
556 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
559 for (regno = 0; regno < 2; regno++)
560 if (REG_VALID == regcache_register_status (regcache,
561 regno + ARM_WCSSF_REGNUM))
562 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
563 ®buf[16 * 8 + regno * 4]);
565 for (regno = 0; regno < 4; regno++)
566 if (REG_VALID == regcache_register_status (regcache,
567 regno + ARM_WCGR0_REGNUM))
568 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
569 ®buf[16 * 8 + 2 * 4 + regno * 4]);
571 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
575 warning (_("Unable to store WMMX registers."));
580 /* Fetch and store VFP Registers. The kernel object has space for 32
581 64-bit registers, and the FPSCR. This is even when on a VFPv2 or
583 #define VFP_REGS_SIZE (32 * 8 + 4)
586 fetch_vfp_regs (struct regcache *regcache)
588 char regbuf[VFP_REGS_SIZE];
590 struct gdbarch *gdbarch = get_regcache_arch (regcache);
591 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
593 /* Get the thread id for the ptrace call. */
594 tid = GET_THREAD_ID (inferior_ptid);
596 if (have_ptrace_getregset == TRIBOOL_TRUE)
600 iov.iov_base = regbuf;
601 iov.iov_len = VFP_REGS_SIZE;
602 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
605 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
609 warning (_("Unable to fetch VFP registers."));
613 for (regno = 0; regno < tdep->vfp_register_count; regno++)
614 regcache_raw_supply (regcache, regno + ARM_D0_REGNUM,
615 (char *) regbuf + regno * 8);
617 regcache_raw_supply (regcache, ARM_FPSCR_REGNUM,
618 (char *) regbuf + 32 * 8);
622 store_vfp_regs (const struct regcache *regcache)
624 char regbuf[VFP_REGS_SIZE];
626 struct gdbarch *gdbarch = get_regcache_arch (regcache);
627 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
629 /* Get the thread id for the ptrace call. */
630 tid = GET_THREAD_ID (inferior_ptid);
632 if (have_ptrace_getregset == TRIBOOL_TRUE)
636 iov.iov_base = regbuf;
637 iov.iov_len = VFP_REGS_SIZE;
638 ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iov);
641 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
645 warning (_("Unable to fetch VFP registers (for update)."));
649 for (regno = 0; regno < tdep->vfp_register_count; regno++)
650 regcache_raw_collect (regcache, regno + ARM_D0_REGNUM,
651 (char *) regbuf + regno * 8);
653 regcache_raw_collect (regcache, ARM_FPSCR_REGNUM,
654 (char *) regbuf + 32 * 8);
656 if (have_ptrace_getregset == TRIBOOL_TRUE)
660 iov.iov_base = regbuf;
661 iov.iov_len = VFP_REGS_SIZE;
662 ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iov);
665 ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
669 warning (_("Unable to store VFP registers."));
674 /* Fetch registers from the child process. Fetch all registers if
675 regno == -1, otherwise fetch all general registers or all floating
676 point registers depending upon the value of regno. */
679 arm_linux_fetch_inferior_registers (struct target_ops *ops,
680 struct regcache *regcache, int regno)
682 struct gdbarch *gdbarch = get_regcache_arch (regcache);
683 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
687 fetch_regs (regcache);
688 fetch_fpregs (regcache);
689 if (tdep->have_wmmx_registers)
690 fetch_wmmx_regs (regcache);
691 if (tdep->vfp_register_count > 0)
692 fetch_vfp_regs (regcache);
696 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
697 fetch_register (regcache, regno);
698 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
699 fetch_fpregister (regcache, regno);
700 else if (tdep->have_wmmx_registers
701 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
702 fetch_wmmx_regs (regcache);
703 else if (tdep->vfp_register_count > 0
704 && regno >= ARM_D0_REGNUM
705 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
706 fetch_vfp_regs (regcache);
710 /* Store registers back into the inferior. Store all registers if
711 regno == -1, otherwise store all general registers or all floating
712 point registers depending upon the value of regno. */
715 arm_linux_store_inferior_registers (struct target_ops *ops,
716 struct regcache *regcache, int regno)
718 struct gdbarch *gdbarch = get_regcache_arch (regcache);
719 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
723 store_regs (regcache);
724 store_fpregs (regcache);
725 if (tdep->have_wmmx_registers)
726 store_wmmx_regs (regcache);
727 if (tdep->vfp_register_count > 0)
728 store_vfp_regs (regcache);
732 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
733 store_register (regcache, regno);
734 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
735 store_fpregister (regcache, regno);
736 else if (tdep->have_wmmx_registers
737 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
738 store_wmmx_regs (regcache);
739 else if (tdep->vfp_register_count > 0
740 && regno >= ARM_D0_REGNUM
741 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
742 store_vfp_regs (regcache);
746 /* Wrapper functions for the standard regset handling, used by
750 fill_gregset (const struct regcache *regcache,
751 gdb_gregset_t *gregsetp, int regno)
753 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
757 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
759 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
763 fill_fpregset (const struct regcache *regcache,
764 gdb_fpregset_t *fpregsetp, int regno)
766 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
769 /* Fill GDB's register array with the floating-point register values
773 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
775 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
778 /* Fetch the thread-local storage pointer for libthread_db. */
781 ps_get_thread_area (const struct ps_prochandle *ph,
782 lwpid_t lwpid, int idx, void **base)
784 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
787 /* IDX is the bias from the thread pointer to the beginning of the
788 thread descriptor. It has to be subtracted due to implementation
789 quirks in libthread_db. */
790 *base = (void *) ((char *)*base - idx);
795 static const struct target_desc *
796 arm_linux_read_description (struct target_ops *ops)
798 CORE_ADDR arm_hwcap = 0;
800 if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
802 elf_gregset_t gpregs;
804 int tid = GET_THREAD_ID (inferior_ptid);
806 iov.iov_base = &gpregs;
807 iov.iov_len = sizeof (gpregs);
809 /* Check if PTRACE_GETREGSET works. */
810 if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) < 0)
811 have_ptrace_getregset = TRIBOOL_FALSE;
813 have_ptrace_getregset = TRIBOOL_TRUE;
816 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
818 return ops->beneath->to_read_description (ops->beneath);
821 if (arm_hwcap & HWCAP_IWMMXT)
822 return tdesc_arm_with_iwmmxt;
824 if (arm_hwcap & HWCAP_VFP)
828 const struct target_desc * result = NULL;
830 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
831 Neon with VFPv3-D32. */
832 if (arm_hwcap & HWCAP_NEON)
833 result = tdesc_arm_with_neon;
834 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
835 result = tdesc_arm_with_vfpv3;
837 result = tdesc_arm_with_vfpv2;
839 /* Now make sure that the kernel supports reading these
840 registers. Support was added in 2.6.30. */
841 pid = ptid_get_lwp (inferior_ptid);
843 buf = alloca (VFP_REGS_SIZE);
844 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
851 return ops->beneath->to_read_description (ops->beneath);
854 /* Information describing the hardware breakpoint capabilities. */
855 struct arm_linux_hwbp_cap
858 gdb_byte max_wp_length;
863 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
864 assume a maximum number of supported break-/watchpoints. */
868 /* Get hold of the Hardware Breakpoint information for the target we are
869 attached to. Returns NULL if the kernel doesn't support Hardware
870 breakpoints at all, or a pointer to the information structure. */
871 static const struct arm_linux_hwbp_cap *
872 arm_linux_get_hwbp_cap (void)
874 /* The info structure we return. */
875 static struct arm_linux_hwbp_cap info;
877 /* Is INFO in a good state? -1 means that no attempt has been made to
878 initialize INFO; 0 means an attempt has been made, but it failed; 1
879 means INFO is in an initialized state. */
880 static int available = -1;
887 tid = GET_THREAD_ID (inferior_ptid);
888 if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
892 info.arch = (gdb_byte)((val >> 24) & 0xff);
893 info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
894 info.wp_count = (gdb_byte)((val >> 8) & 0xff);
895 info.bp_count = (gdb_byte)(val & 0xff);
897 if (info.wp_count > MAX_WPTS)
899 warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
900 supports %d"), MAX_WPTS, info.wp_count);
901 info.wp_count = MAX_WPTS;
904 if (info.bp_count > MAX_BPTS)
906 warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
907 supports %d"), MAX_BPTS, info.bp_count);
908 info.bp_count = MAX_BPTS;
910 available = (info.arch != 0);
914 return available == 1 ? &info : NULL;
917 /* How many hardware breakpoints are available? */
919 arm_linux_get_hw_breakpoint_count (void)
921 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
922 return cap != NULL ? cap->bp_count : 0;
925 /* How many hardware watchpoints are available? */
927 arm_linux_get_hw_watchpoint_count (void)
929 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
930 return cap != NULL ? cap->wp_count : 0;
933 /* Have we got a free break-/watch-point available for use? Returns -1 if
934 there is not an appropriate resource available, otherwise returns 1. */
936 arm_linux_can_use_hw_breakpoint (struct target_ops *self,
937 int type, int cnt, int ot)
939 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
940 || type == bp_access_watchpoint || type == bp_watchpoint)
942 int count = arm_linux_get_hw_watchpoint_count ();
946 else if (cnt + ot > count)
949 else if (type == bp_hardware_breakpoint)
951 int count = arm_linux_get_hw_breakpoint_count ();
955 else if (cnt > count)
964 /* Enum describing the different types of ARM hardware break-/watch-points. */
973 /* Type describing an ARM Hardware Breakpoint Control register value. */
974 typedef unsigned int arm_hwbp_control_t;
976 /* Structure used to keep track of hardware break-/watch-points. */
977 struct arm_linux_hw_breakpoint
979 /* Address to break on, or being watched. */
980 unsigned int address;
981 /* Control register for break-/watch- point. */
982 arm_hwbp_control_t control;
985 /* Structure containing arrays of per process hardware break-/watchpoints
986 for caching address and control information.
988 The Linux ptrace interface to hardware break-/watch-points presents the
989 values in a vector centred around 0 (which is used fo generic information).
990 Positive indicies refer to breakpoint addresses/control registers, negative
991 indices to watchpoint addresses/control registers.
993 The Linux vector is indexed as follows:
994 -((i << 1) + 2): Control register for watchpoint i.
995 -((i << 1) + 1): Address register for watchpoint i.
996 0: Information register.
997 ((i << 1) + 1): Address register for breakpoint i.
998 ((i << 1) + 2): Control register for breakpoint i.
1000 This structure is used as a per-thread cache of the state stored by the
1001 kernel, so that we don't need to keep calling into the kernel to find a
1004 We treat break-/watch-points with their enable bit clear as being deleted.
1006 struct arm_linux_debug_reg_state
1008 /* Hardware breakpoints for this process. */
1009 struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
1010 /* Hardware watchpoints for this process. */
1011 struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
1014 /* Per-process arch-specific data we want to keep. */
1015 struct arm_linux_process_info
1018 struct arm_linux_process_info *next;
1019 /* The process identifier. */
1021 /* Hardware break-/watchpoints state information. */
1022 struct arm_linux_debug_reg_state state;
1026 /* Per-thread arch-specific data we want to keep. */
1027 struct arch_lwp_info
1029 /* Non-zero if our copy differs from what's recorded in the thread. */
1030 char bpts_changed[MAX_BPTS];
1031 char wpts_changed[MAX_WPTS];
1034 static struct arm_linux_process_info *arm_linux_process_list = NULL;
1036 /* Find process data for process PID. */
1038 static struct arm_linux_process_info *
1039 arm_linux_find_process_pid (pid_t pid)
1041 struct arm_linux_process_info *proc;
1043 for (proc = arm_linux_process_list; proc; proc = proc->next)
1044 if (proc->pid == pid)
1050 /* Add process data for process PID. Returns newly allocated info
1053 static struct arm_linux_process_info *
1054 arm_linux_add_process (pid_t pid)
1056 struct arm_linux_process_info *proc;
1058 proc = xcalloc (1, sizeof (*proc));
1061 proc->next = arm_linux_process_list;
1062 arm_linux_process_list = proc;
1067 /* Get data specific info for process PID, creating it if necessary.
1068 Never returns NULL. */
1070 static struct arm_linux_process_info *
1071 arm_linux_process_info_get (pid_t pid)
1073 struct arm_linux_process_info *proc;
1075 proc = arm_linux_find_process_pid (pid);
1077 proc = arm_linux_add_process (pid);
1082 /* Called whenever GDB is no longer debugging process PID. It deletes
1083 data structures that keep track of debug register state. */
1086 arm_linux_forget_process (pid_t pid)
1088 struct arm_linux_process_info *proc, **proc_link;
1090 proc = arm_linux_process_list;
1091 proc_link = &arm_linux_process_list;
1093 while (proc != NULL)
1095 if (proc->pid == pid)
1097 *proc_link = proc->next;
1103 proc_link = &proc->next;
1108 /* Get hardware break-/watchpoint state for process PID. */
1110 static struct arm_linux_debug_reg_state *
1111 arm_linux_get_debug_reg_state (pid_t pid)
1113 return &arm_linux_process_info_get (pid)->state;
1116 /* Initialize an ARM hardware break-/watch-point control register value.
1117 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
1118 type of break-/watch-point; ENABLE indicates whether the point is enabled.
1120 static arm_hwbp_control_t
1121 arm_hwbp_control_initialize (unsigned byte_address_select,
1122 arm_hwbp_type hwbp_type,
1125 gdb_assert ((byte_address_select & ~0xffU) == 0);
1126 gdb_assert (hwbp_type != arm_hwbp_break
1127 || ((byte_address_select & 0xfU) != 0));
1129 return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
1132 /* Does the breakpoint control value CONTROL have the enable bit set? */
1134 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
1136 return control & 0x1;
1139 /* Change a breakpoint control word so that it is in the disabled state. */
1140 static arm_hwbp_control_t
1141 arm_hwbp_control_disable (arm_hwbp_control_t control)
1143 return control & ~0x1;
1146 /* Initialise the hardware breakpoint structure P. The breakpoint will be
1147 enabled, and will point to the placed address of BP_TGT. */
1149 arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
1150 struct bp_target_info *bp_tgt,
1151 struct arm_linux_hw_breakpoint *p)
1154 CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;
1156 /* We have to create a mask for the control register which says which bits
1157 of the word pointed to by address to break on. */
1158 if (arm_pc_is_thumb (gdbarch, address))
1169 p->address = (unsigned int) address;
1170 p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
1173 /* Get the ARM hardware breakpoint type from the RW value we're given when
1174 asked to set a watchpoint. */
1175 static arm_hwbp_type
1176 arm_linux_get_hwbp_type (int rw)
1179 return arm_hwbp_load;
1180 else if (rw == hw_write)
1181 return arm_hwbp_store;
1183 return arm_hwbp_access;
1186 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
1187 to LEN. The type of watchpoint is given in RW. */
1189 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len, int rw,
1190 struct arm_linux_hw_breakpoint *p)
1192 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1195 gdb_assert (cap != NULL);
1196 gdb_assert (cap->max_wp_length != 0);
1198 mask = (1 << len) - 1;
1200 p->address = (unsigned int) addr;
1201 p->control = arm_hwbp_control_initialize (mask,
1202 arm_linux_get_hwbp_type (rw), 1);
1205 /* Are two break-/watch-points equal? */
1207 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
1208 const struct arm_linux_hw_breakpoint *p2)
1210 return p1->address == p2->address && p1->control == p2->control;
1213 /* Callback to mark a watch-/breakpoint to be updated in all threads of
1214 the current process. */
1216 struct update_registers_data
1223 update_registers_callback (struct lwp_info *lwp, void *arg)
1225 struct update_registers_data *data = (struct update_registers_data *) arg;
1227 if (lwp->arch_private == NULL)
1228 lwp->arch_private = XCNEW (struct arch_lwp_info);
1230 /* The actual update is done later just before resuming the lwp,
1231 we just mark that the registers need updating. */
1233 lwp->arch_private->wpts_changed[data->index] = 1;
1235 lwp->arch_private->bpts_changed[data->index] = 1;
1237 /* If the lwp isn't stopped, force it to momentarily pause, so
1238 we can update its breakpoint registers. */
1240 linux_stop_lwp (lwp);
1245 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
1246 =1) BPT for thread TID. */
1248 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
1254 struct arm_linux_hw_breakpoint* bpts;
1255 struct update_registers_data data;
1257 pid = ptid_get_pid (inferior_ptid);
1258 pid_ptid = pid_to_ptid (pid);
1262 count = arm_linux_get_hw_watchpoint_count ();
1263 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1267 count = arm_linux_get_hw_breakpoint_count ();
1268 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1271 for (i = 0; i < count; ++i)
1272 if (!arm_hwbp_control_is_enabled (bpts[i].control))
1274 data.watch = watchpoint;
1277 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1281 gdb_assert (i != count);
1284 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1285 (WATCHPOINT = 1) BPT for thread TID. */
1287 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
1293 struct arm_linux_hw_breakpoint* bpts;
1294 struct update_registers_data data;
1296 pid = ptid_get_pid (inferior_ptid);
1297 pid_ptid = pid_to_ptid (pid);
1301 count = arm_linux_get_hw_watchpoint_count ();
1302 bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1306 count = arm_linux_get_hw_breakpoint_count ();
1307 bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1310 for (i = 0; i < count; ++i)
1311 if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
1313 data.watch = watchpoint;
1315 bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
1316 iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1320 gdb_assert (i != count);
1323 /* Insert a Hardware breakpoint. */
1325 arm_linux_insert_hw_breakpoint (struct target_ops *self,
1326 struct gdbarch *gdbarch,
1327 struct bp_target_info *bp_tgt)
1329 struct lwp_info *lp;
1330 struct arm_linux_hw_breakpoint p;
1332 if (arm_linux_get_hw_breakpoint_count () == 0)
1335 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1337 arm_linux_insert_hw_breakpoint1 (&p, 0);
1342 /* Remove a hardware breakpoint. */
1344 arm_linux_remove_hw_breakpoint (struct target_ops *self,
1345 struct gdbarch *gdbarch,
1346 struct bp_target_info *bp_tgt)
1348 struct lwp_info *lp;
1349 struct arm_linux_hw_breakpoint p;
1351 if (arm_linux_get_hw_breakpoint_count () == 0)
1354 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1356 arm_linux_remove_hw_breakpoint1 (&p, 0);
1361 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1364 arm_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
1365 CORE_ADDR addr, int len)
1367 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1368 CORE_ADDR max_wp_length, aligned_addr;
1370 /* Can not set watchpoints for zero or negative lengths. */
1374 /* Need to be able to use the ptrace interface. */
1375 if (cap == NULL || cap->wp_count == 0)
1378 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1379 range covered by a watchpoint. */
1380 max_wp_length = (CORE_ADDR)cap->max_wp_length;
1381 aligned_addr = addr & ~(max_wp_length - 1);
1383 if (aligned_addr + max_wp_length < addr + len)
1386 /* The current ptrace interface can only handle watchpoints that are a
1388 if ((len & (len - 1)) != 0)
1391 /* All tests passed so we must be able to set a watchpoint. */
1395 /* Insert a Hardware breakpoint. */
1397 arm_linux_insert_watchpoint (struct target_ops *self,
1398 CORE_ADDR addr, int len, int rw,
1399 struct expression *cond)
1401 struct lwp_info *lp;
1402 struct arm_linux_hw_breakpoint p;
1404 if (arm_linux_get_hw_watchpoint_count () == 0)
1407 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1409 arm_linux_insert_hw_breakpoint1 (&p, 1);
1414 /* Remove a hardware breakpoint. */
1416 arm_linux_remove_watchpoint (struct target_ops *self,
1417 CORE_ADDR addr, int len, int rw,
1418 struct expression *cond)
1420 struct lwp_info *lp;
1421 struct arm_linux_hw_breakpoint p;
1423 if (arm_linux_get_hw_watchpoint_count () == 0)
1426 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1428 arm_linux_remove_hw_breakpoint1 (&p, 1);
1433 /* What was the data address the target was stopped on accessing. */
1435 arm_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1440 if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
1443 /* This must be a hardware breakpoint. */
1444 if (siginfo.si_signo != SIGTRAP
1445 || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1448 /* We must be able to set hardware watchpoints. */
1449 if (arm_linux_get_hw_watchpoint_count () == 0)
1452 slot = siginfo.si_errno;
1454 /* If we are in a positive slot then we're looking at a breakpoint and not
1459 *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
1463 /* Has the target been stopped by hitting a watchpoint? */
1465 arm_linux_stopped_by_watchpoint (struct target_ops *ops)
1468 return arm_linux_stopped_data_address (ops, &addr);
1472 arm_linux_watchpoint_addr_within_range (struct target_ops *target,
1474 CORE_ADDR start, int length)
1476 return start <= addr && start + length - 1 >= addr;
1479 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1480 in the parent thread to the child thread. */
1482 arm_linux_new_thread (struct lwp_info *lp)
1485 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
1487 /* Mark that all the hardware breakpoint/watchpoint register pairs
1488 for this thread need to be initialized. */
1490 for (i = 0; i < MAX_BPTS; i++)
1492 info->bpts_changed[i] = 1;
1493 info->wpts_changed[i] = 1;
1496 lp->arch_private = info;
1499 /* Called when resuming a thread.
1500 The hardware debug registers are updated when there is any change. */
1503 arm_linux_prepare_to_resume (struct lwp_info *lwp)
1506 struct arm_linux_hw_breakpoint *bpts, *wpts;
1507 struct arch_lwp_info *arm_lwp_info = lwp->arch_private;
1509 pid = ptid_get_lwp (lwp->ptid);
1510 bpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->bpts;
1511 wpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->wpts;
1513 /* NULL means this is the main thread still going through the shell,
1514 or, no watchpoint has been set yet. In that case, there's
1516 if (arm_lwp_info == NULL)
1519 for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
1520 if (arm_lwp_info->bpts_changed[i])
1523 if (arm_hwbp_control_is_enabled (bpts[i].control))
1524 if (ptrace (PTRACE_SETHBPREGS, pid,
1525 (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
1526 perror_with_name (_("Unexpected error setting breakpoint"));
1528 if (bpts[i].control != 0)
1529 if (ptrace (PTRACE_SETHBPREGS, pid,
1530 (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
1531 perror_with_name (_("Unexpected error setting breakpoint"));
1533 arm_lwp_info->bpts_changed[i] = 0;
1536 for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
1537 if (arm_lwp_info->wpts_changed[i])
1540 if (arm_hwbp_control_is_enabled (wpts[i].control))
1541 if (ptrace (PTRACE_SETHBPREGS, pid,
1542 (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
1543 perror_with_name (_("Unexpected error setting watchpoint"));
1545 if (wpts[i].control != 0)
1546 if (ptrace (PTRACE_SETHBPREGS, pid,
1547 (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
1548 perror_with_name (_("Unexpected error setting watchpoint"));
1550 arm_lwp_info->wpts_changed[i] = 0;
1554 /* linux_nat_new_fork hook. */
1557 arm_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
1560 struct arm_linux_debug_reg_state *parent_state;
1561 struct arm_linux_debug_reg_state *child_state;
1563 /* NULL means no watchpoint has ever been set in the parent. In
1564 that case, there's nothing to do. */
1565 if (parent->arch_private == NULL)
1568 /* GDB core assumes the child inherits the watchpoints/hw
1569 breakpoints of the parent, and will remove them all from the
1570 forked off process. Copy the debug registers mirrors into the
1571 new process so that all breakpoints and watchpoints can be
1572 removed together. */
1574 parent_pid = ptid_get_pid (parent->ptid);
1575 parent_state = arm_linux_get_debug_reg_state (parent_pid);
1576 child_state = arm_linux_get_debug_reg_state (child_pid);
1577 *child_state = *parent_state;
1580 void _initialize_arm_linux_nat (void);
1583 _initialize_arm_linux_nat (void)
1585 struct target_ops *t;
1587 /* Fill in the generic GNU/Linux methods. */
1588 t = linux_target ();
1590 /* Add our register access methods. */
1591 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
1592 t->to_store_registers = arm_linux_store_inferior_registers;
1594 /* Add our hardware breakpoint and watchpoint implementation. */
1595 t->to_can_use_hw_breakpoint = arm_linux_can_use_hw_breakpoint;
1596 t->to_insert_hw_breakpoint = arm_linux_insert_hw_breakpoint;
1597 t->to_remove_hw_breakpoint = arm_linux_remove_hw_breakpoint;
1598 t->to_region_ok_for_hw_watchpoint = arm_linux_region_ok_for_hw_watchpoint;
1599 t->to_insert_watchpoint = arm_linux_insert_watchpoint;
1600 t->to_remove_watchpoint = arm_linux_remove_watchpoint;
1601 t->to_stopped_by_watchpoint = arm_linux_stopped_by_watchpoint;
1602 t->to_stopped_data_address = arm_linux_stopped_data_address;
1603 t->to_watchpoint_addr_within_range = arm_linux_watchpoint_addr_within_range;
1605 t->to_read_description = arm_linux_read_description;
1607 /* Register the target. */
1608 linux_nat_add_target (t);
1610 /* Handle thread creation and exit. */
1611 linux_nat_set_new_thread (t, arm_linux_new_thread);
1612 linux_nat_set_prepare_to_resume (t, arm_linux_prepare_to_resume);
1614 /* Handle process creation and exit. */
1615 linux_nat_set_new_fork (t, arm_linux_new_fork);
1616 linux_nat_set_forget_process (t, arm_linux_forget_process);