1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
26 #include "linux-nat.h"
27 #include "target-descriptions.h"
30 #include "gdbthread.h"
33 #include "arm-linux-tdep.h"
35 #include <elf/common.h>
37 #include <sys/ptrace.h>
38 #include <sys/utsname.h>
39 #include <sys/procfs.h>
41 /* Prototypes for supply_gregset etc. */
44 /* Defines ps_err_e, struct ps_prochandle. */
45 #include "gdb_proc_service.h"
47 #include "features/arm-with-iwmmxt.c"
48 #include "features/arm-with-vfpv2.c"
49 #include "features/arm-with-vfpv3.c"
50 #include "features/arm-with-neon.c"
52 #ifndef PTRACE_GET_THREAD_AREA
53 #define PTRACE_GET_THREAD_AREA 22
56 #ifndef PTRACE_GETWMMXREGS
57 #define PTRACE_GETWMMXREGS 18
58 #define PTRACE_SETWMMXREGS 19
61 #ifndef PTRACE_GETVFPREGS
62 #define PTRACE_GETVFPREGS 27
63 #define PTRACE_SETVFPREGS 28
66 #ifndef PTRACE_GETHBPREGS
67 #define PTRACE_GETHBPREGS 29
68 #define PTRACE_SETHBPREGS 30
71 /* These are in <asm/elf.h> in current kernels. */
73 #define HWCAP_IWMMXT 512
74 #define HWCAP_NEON 4096
75 #define HWCAP_VFPv3 8192
76 #define HWCAP_VFPv3D16 16384
78 /* A flag for whether the WMMX registers are available. */
79 static int arm_linux_has_wmmx_registers;
81 /* The number of 64-bit VFP registers we have (expect this to be 0,
83 static int arm_linux_vfp_register_count;
85 extern int arm_apcs_32;
87 /* The following variables are used to determine the version of the
88 underlying GNU/Linux operating system. Examples:
90 GNU/Linux 2.0.35 GNU/Linux 2.2.12
91 os_version = 0x00020023 os_version = 0x0002020c
92 os_major = 2 os_major = 2
93 os_minor = 0 os_minor = 2
94 os_release = 35 os_release = 12
96 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
98 These are initialized using get_linux_version() from
99 _initialize_arm_linux_nat(). */
101 static unsigned int os_version, os_major, os_minor, os_release;
103 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
104 case we may be tracing more than one process at a time. In that
105 case, inferior_ptid will contain the main process ID and the
106 individual thread (process) ID. get_thread_id () is used to get
107 the thread id if it's available, and the process id otherwise. */
110 get_thread_id (ptid_t ptid)
112 int tid = TIDGET (ptid);
118 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
120 /* Get the value of a particular register from the floating point
121 state of the process and store it into regcache. */
124 fetch_fpregister (struct regcache *regcache, int regno)
127 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
129 /* Get the thread id for the ptrace call. */
130 tid = GET_THREAD_ID (inferior_ptid);
132 /* Read the floating point state. */
133 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
136 warning (_("Unable to fetch floating point register."));
141 if (ARM_FPS_REGNUM == regno)
142 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
143 fp + NWFPE_FPSR_OFFSET);
145 /* Fetch the floating point register. */
146 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
147 supply_nwfpe_register (regcache, regno, fp);
150 /* Get the whole floating point state of the process and store it
154 fetch_fpregs (struct regcache *regcache)
157 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
159 /* Get the thread id for the ptrace call. */
160 tid = GET_THREAD_ID (inferior_ptid);
162 /* Read the floating point state. */
163 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
166 warning (_("Unable to fetch the floating point registers."));
171 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
172 fp + NWFPE_FPSR_OFFSET);
174 /* Fetch the floating point registers. */
175 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
176 supply_nwfpe_register (regcache, regno, fp);
179 /* Save a particular register into the floating point state of the
180 process using the contents from regcache. */
183 store_fpregister (const struct regcache *regcache, int regno)
186 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
188 /* Get the thread id for the ptrace call. */
189 tid = GET_THREAD_ID (inferior_ptid);
191 /* Read the floating point state. */
192 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
195 warning (_("Unable to fetch the floating point registers."));
200 if (ARM_FPS_REGNUM == regno
201 && REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
202 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
204 /* Store the floating point register. */
205 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
206 collect_nwfpe_register (regcache, regno, fp);
208 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
211 warning (_("Unable to store floating point register."));
216 /* Save the whole floating point state of the process using
217 the contents from regcache. */
220 store_fpregs (const struct regcache *regcache)
223 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
225 /* Get the thread id for the ptrace call. */
226 tid = GET_THREAD_ID (inferior_ptid);
228 /* Read the floating point state. */
229 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
232 warning (_("Unable to fetch the floating point registers."));
237 if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
238 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
240 /* Store the floating point registers. */
241 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
242 if (REG_VALID == regcache_register_status (regcache, regno))
243 collect_nwfpe_register (regcache, regno, fp);
245 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
248 warning (_("Unable to store floating point registers."));
253 /* Fetch a general register of the process and store into
257 fetch_register (struct regcache *regcache, int regno)
262 /* Get the thread id for the ptrace call. */
263 tid = GET_THREAD_ID (inferior_ptid);
265 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
268 warning (_("Unable to fetch general register."));
272 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
273 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
275 if (ARM_PS_REGNUM == regno)
278 regcache_raw_supply (regcache, ARM_PS_REGNUM,
279 (char *) ®s[ARM_CPSR_GREGNUM]);
281 regcache_raw_supply (regcache, ARM_PS_REGNUM,
282 (char *) ®s[ARM_PC_REGNUM]);
285 if (ARM_PC_REGNUM == regno)
287 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
288 (get_regcache_arch (regcache),
289 regs[ARM_PC_REGNUM]);
290 regcache_raw_supply (regcache, ARM_PC_REGNUM,
291 (char *) ®s[ARM_PC_REGNUM]);
295 /* Fetch all general registers of the process and store into
299 fetch_regs (struct regcache *regcache)
304 /* Get the thread id for the ptrace call. */
305 tid = GET_THREAD_ID (inferior_ptid);
307 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
310 warning (_("Unable to fetch general registers."));
314 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
315 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
318 regcache_raw_supply (regcache, ARM_PS_REGNUM,
319 (char *) ®s[ARM_CPSR_GREGNUM]);
321 regcache_raw_supply (regcache, ARM_PS_REGNUM,
322 (char *) ®s[ARM_PC_REGNUM]);
324 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
325 (get_regcache_arch (regcache), regs[ARM_PC_REGNUM]);
326 regcache_raw_supply (regcache, ARM_PC_REGNUM,
327 (char *) ®s[ARM_PC_REGNUM]);
330 /* Store all general registers of the process from the values in
334 store_register (const struct regcache *regcache, int regno)
339 if (REG_VALID != regcache_register_status (regcache, regno))
342 /* Get the thread id for the ptrace call. */
343 tid = GET_THREAD_ID (inferior_ptid);
345 /* Get the general registers from the process. */
346 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
349 warning (_("Unable to fetch general registers."));
353 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
354 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
355 else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
356 regcache_raw_collect (regcache, regno,
357 (char *) ®s[ARM_CPSR_GREGNUM]);
358 else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
359 regcache_raw_collect (regcache, ARM_PC_REGNUM,
360 (char *) ®s[ARM_PC_REGNUM]);
362 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
365 warning (_("Unable to store general register."));
371 store_regs (const struct regcache *regcache)
376 /* Get the thread id for the ptrace call. */
377 tid = GET_THREAD_ID (inferior_ptid);
379 /* Fetch the general registers. */
380 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
383 warning (_("Unable to fetch general registers."));
387 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
389 if (REG_VALID == regcache_register_status (regcache, regno))
390 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
393 if (arm_apcs_32 && REG_VALID == regcache_register_status (regcache, ARM_PS_REGNUM))
394 regcache_raw_collect (regcache, ARM_PS_REGNUM,
395 (char *) ®s[ARM_CPSR_GREGNUM]);
397 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
401 warning (_("Unable to store general registers."));
406 /* Fetch all WMMX registers of the process and store into
409 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
412 fetch_wmmx_regs (struct regcache *regcache)
414 char regbuf[IWMMXT_REGS_SIZE];
417 /* Get the thread id for the ptrace call. */
418 tid = GET_THREAD_ID (inferior_ptid);
420 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
423 warning (_("Unable to fetch WMMX registers."));
427 for (regno = 0; regno < 16; regno++)
428 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
431 for (regno = 0; regno < 2; regno++)
432 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
433 ®buf[16 * 8 + regno * 4]);
435 for (regno = 0; regno < 4; regno++)
436 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
437 ®buf[16 * 8 + 2 * 4 + regno * 4]);
441 store_wmmx_regs (const struct regcache *regcache)
443 char regbuf[IWMMXT_REGS_SIZE];
446 /* Get the thread id for the ptrace call. */
447 tid = GET_THREAD_ID (inferior_ptid);
449 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
452 warning (_("Unable to fetch WMMX registers."));
456 for (regno = 0; regno < 16; regno++)
457 if (REG_VALID == regcache_register_status (regcache,
458 regno + ARM_WR0_REGNUM))
459 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
462 for (regno = 0; regno < 2; regno++)
463 if (REG_VALID == regcache_register_status (regcache,
464 regno + ARM_WCSSF_REGNUM))
465 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
466 ®buf[16 * 8 + regno * 4]);
468 for (regno = 0; regno < 4; regno++)
469 if (REG_VALID == regcache_register_status (regcache,
470 regno + ARM_WCGR0_REGNUM))
471 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
472 ®buf[16 * 8 + 2 * 4 + regno * 4]);
474 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
478 warning (_("Unable to store WMMX registers."));
483 /* Fetch and store VFP Registers. The kernel object has space for 32
484 64-bit registers, and the FPSCR. This is even when on a VFPv2 or
486 #define VFP_REGS_SIZE (32 * 8 + 4)
489 fetch_vfp_regs (struct regcache *regcache)
491 char regbuf[VFP_REGS_SIZE];
494 /* Get the thread id for the ptrace call. */
495 tid = GET_THREAD_ID (inferior_ptid);
497 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
500 warning (_("Unable to fetch VFP registers."));
504 for (regno = 0; regno < arm_linux_vfp_register_count; regno++)
505 regcache_raw_supply (regcache, regno + ARM_D0_REGNUM,
506 (char *) regbuf + regno * 8);
508 regcache_raw_supply (regcache, ARM_FPSCR_REGNUM,
509 (char *) regbuf + 32 * 8);
513 store_vfp_regs (const struct regcache *regcache)
515 char regbuf[VFP_REGS_SIZE];
518 /* Get the thread id for the ptrace call. */
519 tid = GET_THREAD_ID (inferior_ptid);
521 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
524 warning (_("Unable to fetch VFP registers (for update)."));
528 for (regno = 0; regno < arm_linux_vfp_register_count; regno++)
529 regcache_raw_collect (regcache, regno + ARM_D0_REGNUM,
530 (char *) regbuf + regno * 8);
532 regcache_raw_collect (regcache, ARM_FPSCR_REGNUM,
533 (char *) regbuf + 32 * 8);
535 ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
539 warning (_("Unable to store VFP registers."));
544 /* Fetch registers from the child process. Fetch all registers if
545 regno == -1, otherwise fetch all general registers or all floating
546 point registers depending upon the value of regno. */
549 arm_linux_fetch_inferior_registers (struct target_ops *ops,
550 struct regcache *regcache, int regno)
554 fetch_regs (regcache);
555 fetch_fpregs (regcache);
556 if (arm_linux_has_wmmx_registers)
557 fetch_wmmx_regs (regcache);
558 if (arm_linux_vfp_register_count > 0)
559 fetch_vfp_regs (regcache);
563 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
564 fetch_register (regcache, regno);
565 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
566 fetch_fpregister (regcache, regno);
567 else if (arm_linux_has_wmmx_registers
568 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
569 fetch_wmmx_regs (regcache);
570 else if (arm_linux_vfp_register_count > 0
571 && regno >= ARM_D0_REGNUM
572 && regno <= ARM_D0_REGNUM + arm_linux_vfp_register_count)
573 fetch_vfp_regs (regcache);
577 /* Store registers back into the inferior. Store all registers if
578 regno == -1, otherwise store all general registers or all floating
579 point registers depending upon the value of regno. */
582 arm_linux_store_inferior_registers (struct target_ops *ops,
583 struct regcache *regcache, int regno)
587 store_regs (regcache);
588 store_fpregs (regcache);
589 if (arm_linux_has_wmmx_registers)
590 store_wmmx_regs (regcache);
591 if (arm_linux_vfp_register_count > 0)
592 store_vfp_regs (regcache);
596 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
597 store_register (regcache, regno);
598 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
599 store_fpregister (regcache, regno);
600 else if (arm_linux_has_wmmx_registers
601 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
602 store_wmmx_regs (regcache);
603 else if (arm_linux_vfp_register_count > 0
604 && regno >= ARM_D0_REGNUM
605 && regno <= ARM_D0_REGNUM + arm_linux_vfp_register_count)
606 store_vfp_regs (regcache);
610 /* Wrapper functions for the standard regset handling, used by
614 fill_gregset (const struct regcache *regcache,
615 gdb_gregset_t *gregsetp, int regno)
617 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
621 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
623 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
627 fill_fpregset (const struct regcache *regcache,
628 gdb_fpregset_t *fpregsetp, int regno)
630 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
633 /* Fill GDB's register array with the floating-point register values
637 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
639 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
642 /* Fetch the thread-local storage pointer for libthread_db. */
645 ps_get_thread_area (const struct ps_prochandle *ph,
646 lwpid_t lwpid, int idx, void **base)
648 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
651 /* IDX is the bias from the thread pointer to the beginning of the
652 thread descriptor. It has to be subtracted due to implementation
653 quirks in libthread_db. */
654 *base = (void *) ((char *)*base - idx);
660 get_linux_version (unsigned int *vmajor,
661 unsigned int *vminor,
662 unsigned int *vrelease)
665 char *pmajor, *pminor, *prelease, *tail;
667 if (-1 == uname (&info))
669 warning (_("Unable to determine GNU/Linux version."));
673 pmajor = strtok (info.release, ".");
674 pminor = strtok (NULL, ".");
675 prelease = strtok (NULL, ".");
677 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
678 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
679 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
681 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
684 static const struct target_desc *
685 arm_linux_read_description (struct target_ops *ops)
687 CORE_ADDR arm_hwcap = 0;
688 arm_linux_has_wmmx_registers = 0;
689 arm_linux_vfp_register_count = 0;
691 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
696 if (arm_hwcap & HWCAP_IWMMXT)
698 arm_linux_has_wmmx_registers = 1;
699 if (tdesc_arm_with_iwmmxt == NULL)
700 initialize_tdesc_arm_with_iwmmxt ();
701 return tdesc_arm_with_iwmmxt;
704 if (arm_hwcap & HWCAP_VFP)
708 const struct target_desc * result = NULL;
710 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
711 Neon with VFPv3-D32. */
712 if (arm_hwcap & HWCAP_NEON)
714 arm_linux_vfp_register_count = 32;
715 if (tdesc_arm_with_neon == NULL)
716 initialize_tdesc_arm_with_neon ();
717 result = tdesc_arm_with_neon;
719 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
721 arm_linux_vfp_register_count = 32;
722 if (tdesc_arm_with_vfpv3 == NULL)
723 initialize_tdesc_arm_with_vfpv3 ();
724 result = tdesc_arm_with_vfpv3;
728 arm_linux_vfp_register_count = 16;
729 if (tdesc_arm_with_vfpv2 == NULL)
730 initialize_tdesc_arm_with_vfpv2 ();
731 result = tdesc_arm_with_vfpv2;
734 /* Now make sure that the kernel supports reading these
735 registers. Support was added in 2.6.30. */
736 pid = GET_LWP (inferior_ptid);
738 buf = alloca (VFP_REGS_SIZE);
739 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
749 /* Information describing the hardware breakpoint capabilities. */
750 struct arm_linux_hwbp_cap
753 gdb_byte max_wp_length;
758 /* Get hold of the Hardware Breakpoint information for the target we are
759 attached to. Returns NULL if the kernel doesn't support Hardware
760 breakpoints at all, or a pointer to the information structure. */
761 static const struct arm_linux_hwbp_cap *
762 arm_linux_get_hwbp_cap (void)
764 /* The info structure we return. */
765 static struct arm_linux_hwbp_cap info;
767 /* Is INFO in a good state? -1 means that no attempt has been made to
768 initialize INFO; 0 means an attempt has been made, but it failed; 1
769 means INFO is in an initialized state. */
770 static int available = -1;
777 tid = GET_THREAD_ID (inferior_ptid);
778 if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
782 info.arch = (gdb_byte)((val >> 24) & 0xff);
783 info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
784 info.wp_count = (gdb_byte)((val >> 8) & 0xff);
785 info.bp_count = (gdb_byte)(val & 0xff);
786 available = (info.arch != 0);
790 return available == 1 ? &info : NULL;
793 /* How many hardware breakpoints are available? */
795 arm_linux_get_hw_breakpoint_count (void)
797 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
798 return cap != NULL ? cap->bp_count : 0;
801 /* How many hardware watchpoints are available? */
803 arm_linux_get_hw_watchpoint_count (void)
805 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
806 return cap != NULL ? cap->wp_count : 0;
809 /* Have we got a free break-/watch-point available for use? Returns -1 if
810 there is not an appropriate resource available, otherwise returns 1. */
812 arm_linux_can_use_hw_breakpoint (int type, int cnt, int ot)
814 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
815 || type == bp_access_watchpoint || type == bp_watchpoint)
817 if (cnt + ot > arm_linux_get_hw_watchpoint_count ())
820 else if (type == bp_hardware_breakpoint)
822 if (cnt > arm_linux_get_hw_breakpoint_count ())
831 /* Enum describing the different types of ARM hardware break-/watch-points. */
840 /* Type describing an ARM Hardware Breakpoint Control register value. */
841 typedef unsigned int arm_hwbp_control_t;
843 /* Structure used to keep track of hardware break-/watch-points. */
844 struct arm_linux_hw_breakpoint
846 /* Address to break on, or being watched. */
847 unsigned int address;
848 /* Control register for break-/watch- point. */
849 arm_hwbp_control_t control;
852 /* Structure containing arrays of the break and watch points which are have
853 active in each thread.
855 The Linux ptrace interface to hardware break-/watch-points presents the
856 values in a vector centred around 0 (which is used fo generic information).
857 Positive indicies refer to breakpoint addresses/control registers, negative
858 indices to watchpoint addresses/control registers.
860 The Linux vector is indexed as follows:
861 -((i << 1) + 2): Control register for watchpoint i.
862 -((i << 1) + 1): Address register for watchpoint i.
863 0: Information register.
864 ((i << 1) + 1): Address register for breakpoint i.
865 ((i << 1) + 2): Control register for breakpoint i.
867 This structure is used as a per-thread cache of the state stored by the
868 kernel, so that we don't need to keep calling into the kernel to find a
871 We treat break-/watch-points with their enable bit clear as being deleted.
873 typedef struct arm_linux_thread_points
877 /* Breakpoints for thread. */
878 struct arm_linux_hw_breakpoint *bpts;
879 /* Watchpoint for threads. */
880 struct arm_linux_hw_breakpoint *wpts;
881 } *arm_linux_thread_points_p;
882 DEF_VEC_P (arm_linux_thread_points_p);
884 /* Vector of hardware breakpoints for each thread. */
885 VEC(arm_linux_thread_points_p) *arm_threads = NULL;
887 /* Find the list of hardware break-/watch-points for a thread with id TID.
888 If no list exists for TID we return NULL if ALLOC_NEW is 0, otherwise we
889 create a new list and return that. */
890 static struct arm_linux_thread_points *
891 arm_linux_find_breakpoints_by_tid (int tid, int alloc_new)
894 struct arm_linux_thread_points *t;
896 for (i = 0; VEC_iterate (arm_linux_thread_points_p, arm_threads, i, t); ++i)
906 t = xmalloc (sizeof (struct arm_linux_thread_points));
908 t->bpts = xzalloc (arm_linux_get_hw_breakpoint_count ()
909 * sizeof (struct arm_linux_hw_breakpoint));
910 t->wpts = xzalloc (arm_linux_get_hw_watchpoint_count ()
911 * sizeof (struct arm_linux_hw_breakpoint));
912 VEC_safe_push (arm_linux_thread_points_p, arm_threads, t);
918 /* Initialize an ARM hardware break-/watch-point control register value.
919 BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
920 type of break-/watch-point; ENABLE indicates whether the point is enabled.
922 static arm_hwbp_control_t
923 arm_hwbp_control_initialize (unsigned byte_address_select,
924 arm_hwbp_type hwbp_type,
927 gdb_assert ((byte_address_select & ~0xffU) == 0);
928 gdb_assert (hwbp_type != arm_hwbp_break
929 || ((byte_address_select & 0xfU) != 0));
931 return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
934 /* Does the breakpoint control value CONTROL have the enable bit set? */
936 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
938 return control & 0x1;
941 /* Change a breakpoint control word so that it is in the disabled state. */
942 static arm_hwbp_control_t
943 arm_hwbp_control_disable (arm_hwbp_control_t control)
945 return control & ~0x1;
948 /* Initialise the hardware breakpoint structure P. The breakpoint will be
949 enabled, and will point to the placed address of BP_TGT. */
951 arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
952 struct bp_target_info *bp_tgt,
953 struct arm_linux_hw_breakpoint *p)
956 CORE_ADDR address = bp_tgt->placed_address;
958 /* We have to create a mask for the control register which says which bits
959 of the word pointed to by address to break on. */
960 if (arm_pc_is_thumb (gdbarch, address))
961 mask = 0x3 << (address & 2);
965 p->address = (unsigned int) (address & ~3);
966 p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
969 /* Get the ARM hardware breakpoint type from the RW value we're given when
970 asked to set a watchpoint. */
972 arm_linux_get_hwbp_type (int rw)
975 return arm_hwbp_load;
976 else if (rw == hw_write)
977 return arm_hwbp_store;
979 return arm_hwbp_access;
982 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
983 to LEN. The type of watchpoint is given in RW. */
985 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len, int rw,
986 struct arm_linux_hw_breakpoint *p)
988 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
991 gdb_assert (cap != NULL);
992 gdb_assert (cap->max_wp_length != 0);
994 mask = (1 << len) - 1;
996 p->address = (unsigned int) addr;
997 p->control = arm_hwbp_control_initialize (mask,
998 arm_linux_get_hwbp_type (rw), 1);
1001 /* Are two break-/watch-points equal? */
1003 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
1004 const struct arm_linux_hw_breakpoint *p2)
1006 return p1->address == p2->address && p1->control == p2->control;
1009 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
1010 =1) BPT for thread TID. */
1012 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
1013 int tid, int watchpoint)
1015 struct arm_linux_thread_points *t = arm_linux_find_breakpoints_by_tid (tid, 1);
1017 struct arm_linux_hw_breakpoint* bpts;
1020 gdb_assert (t != NULL);
1024 count = arm_linux_get_hw_watchpoint_count ();
1030 count = arm_linux_get_hw_breakpoint_count ();
1035 for (i = 0; i < count; ++i)
1036 if (!arm_hwbp_control_is_enabled (bpts[i].control))
1039 if (ptrace (PTRACE_SETHBPREGS, tid, dir * ((i << 1) + 1),
1041 perror_with_name (_("Unexpected error setting breakpoint address"));
1042 if (ptrace (PTRACE_SETHBPREGS, tid, dir * ((i << 1) + 2),
1044 perror_with_name (_("Unexpected error setting breakpoint"));
1046 memcpy (bpts + i, bpt, sizeof (struct arm_linux_hw_breakpoint));
1050 gdb_assert (i != count);
1053 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1054 (WATCHPOINT = 1) BPT for thread TID. */
1056 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
1057 int tid, int watchpoint)
1059 struct arm_linux_thread_points *t = arm_linux_find_breakpoints_by_tid (tid, 0);
1061 struct arm_linux_hw_breakpoint *bpts;
1064 gdb_assert (t != NULL);
1068 count = arm_linux_get_hw_watchpoint_count ();
1074 count = arm_linux_get_hw_breakpoint_count ();
1079 for (i = 0; i < count; ++i)
1080 if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
1083 bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
1084 if (ptrace (PTRACE_SETHBPREGS, tid, dir * ((i << 1) + 2),
1085 &bpts[i].control) < 0)
1086 perror_with_name (_("Unexpected error clearing breakpoint"));
1090 gdb_assert (i != count);
1093 /* Insert a Hardware breakpoint. */
1095 arm_linux_insert_hw_breakpoint (struct gdbarch *gdbarch,
1096 struct bp_target_info *bp_tgt)
1099 struct lwp_info *lp;
1100 struct arm_linux_hw_breakpoint p;
1102 if (arm_linux_get_hw_breakpoint_count () == 0)
1105 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1107 arm_linux_insert_hw_breakpoint1 (&p, TIDGET (ptid), 0);
1112 /* Remove a hardware breakpoint. */
1114 arm_linux_remove_hw_breakpoint (struct gdbarch *gdbarch,
1115 struct bp_target_info *bp_tgt)
1118 struct lwp_info *lp;
1119 struct arm_linux_hw_breakpoint p;
1121 if (arm_linux_get_hw_breakpoint_count () == 0)
1124 arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1126 arm_linux_remove_hw_breakpoint1 (&p, TIDGET (ptid), 0);
1131 /* Are we able to use a hardware watchpoint for the LEN bytes starting at
1134 arm_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1136 const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1137 CORE_ADDR max_wp_length, aligned_addr;
1139 /* Can not set watchpoints for zero or negative lengths. */
1143 /* Need to be able to use the ptrace interface. */
1144 if (cap == NULL || cap->wp_count == 0)
1147 /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1148 range covered by a watchpoint. */
1149 max_wp_length = (CORE_ADDR)cap->max_wp_length;
1150 aligned_addr = addr & ~(max_wp_length - 1);
1152 if (aligned_addr + max_wp_length < addr + len)
1155 /* The current ptrace interface can only handle watchpoints that are a
1157 if ((len & (len - 1)) != 0)
1160 /* All tests passed so we must be able to set a watchpoint. */
1164 /* Insert a Hardware breakpoint. */
1166 arm_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw,
1167 struct expression *cond)
1170 struct lwp_info *lp;
1171 struct arm_linux_hw_breakpoint p;
1173 if (arm_linux_get_hw_watchpoint_count () == 0)
1176 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1178 arm_linux_insert_hw_breakpoint1 (&p, TIDGET (ptid), 1);
1183 /* Remove a hardware breakpoint. */
1185 arm_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw,
1186 struct expression *cond)
1189 struct lwp_info *lp;
1190 struct arm_linux_hw_breakpoint p;
1192 if (arm_linux_get_hw_watchpoint_count () == 0)
1195 arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1197 arm_linux_remove_hw_breakpoint1 (&p, TIDGET (ptid), 1);
1202 /* What was the data address the target was stopped on accessing. */
1204 arm_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1206 struct siginfo *siginfo_p = linux_nat_get_siginfo (inferior_ptid);
1207 int slot = siginfo_p->si_errno;
1209 /* This must be a hardware breakpoint. */
1210 if (siginfo_p->si_signo != SIGTRAP
1211 || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1214 /* We must be able to set hardware watchpoints. */
1215 if (arm_linux_get_hw_watchpoint_count () == 0)
1218 /* If we are in a positive slot then we're looking at a breakpoint and not
1223 *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
1227 /* Has the target been stopped by hitting a watchpoint? */
1229 arm_linux_stopped_by_watchpoint (void)
1232 return arm_linux_stopped_data_address (¤t_target, &addr);
1236 arm_linux_watchpoint_addr_within_range (struct target_ops *target,
1238 CORE_ADDR start, int length)
1240 return start <= addr && start + length - 1 >= addr;
1243 /* Handle thread creation. We need to copy the breakpoints and watchpoints
1244 in the parent thread to the child thread. */
1246 arm_linux_new_thread (ptid_t ptid)
1248 int tid = TIDGET (ptid);
1249 const struct arm_linux_hwbp_cap *info = arm_linux_get_hwbp_cap ();
1254 struct arm_linux_thread_points *p;
1255 struct arm_linux_hw_breakpoint *bpts;
1257 if (VEC_empty (arm_linux_thread_points_p, arm_threads))
1260 /* Get a list of breakpoints from any thread. */
1261 p = VEC_last (arm_linux_thread_points_p, arm_threads);
1263 /* Copy that thread's breakpoints and watchpoints to the new thread. */
1264 for (i = 0; i < info->bp_count; i++)
1265 if (arm_hwbp_control_is_enabled (p->bpts[i].control))
1266 arm_linux_insert_hw_breakpoint1 (p->bpts + i, tid, 0);
1267 for (i = 0; i < info->wp_count; i++)
1268 if (arm_hwbp_control_is_enabled (p->wpts[i].control))
1269 arm_linux_insert_hw_breakpoint1 (p->wpts + i, tid, 1);
1273 /* Handle thread exit. Tidy up the memory that has been allocated for the
1276 arm_linux_thread_exit (struct thread_info *tp, int silent)
1278 const struct arm_linux_hwbp_cap *info = arm_linux_get_hwbp_cap ();
1283 int tid = TIDGET (tp->ptid);
1284 struct arm_linux_thread_points *t = NULL, *p;
1287 VEC_iterate (arm_linux_thread_points_p, arm_threads, i, p); i++)
1299 VEC_unordered_remove (arm_linux_thread_points_p, arm_threads, i);
1307 void _initialize_arm_linux_nat (void);
1310 _initialize_arm_linux_nat (void)
1312 struct target_ops *t;
1314 os_version = get_linux_version (&os_major, &os_minor, &os_release);
1316 /* Fill in the generic GNU/Linux methods. */
1317 t = linux_target ();
1319 /* Add our register access methods. */
1320 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
1321 t->to_store_registers = arm_linux_store_inferior_registers;
1323 /* Add our hardware breakpoint and watchpoint implementation. */
1324 t->to_can_use_hw_breakpoint = arm_linux_can_use_hw_breakpoint;
1325 t->to_insert_hw_breakpoint = arm_linux_insert_hw_breakpoint;
1326 t->to_remove_hw_breakpoint = arm_linux_remove_hw_breakpoint;
1327 t->to_region_ok_for_hw_watchpoint = arm_linux_region_ok_for_hw_watchpoint;
1328 t->to_insert_watchpoint = arm_linux_insert_watchpoint;
1329 t->to_remove_watchpoint = arm_linux_remove_watchpoint;
1330 t->to_stopped_by_watchpoint = arm_linux_stopped_by_watchpoint;
1331 t->to_stopped_data_address = arm_linux_stopped_data_address;
1332 t->to_watchpoint_addr_within_range = arm_linux_watchpoint_addr_within_range;
1334 t->to_read_description = arm_linux_read_description;
1336 /* Register the target. */
1337 linux_nat_add_target (t);
1339 /* Handle thread creation and exit */
1340 observer_attach_thread_exit (arm_linux_thread_exit);
1341 linux_nat_set_new_thread (t, arm_linux_new_thread);