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"
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 /* Prototypes for supply_gregset etc. */
42 /* Defines ps_err_e, struct ps_prochandle. */
43 #include "gdb_proc_service.h"
45 #include "features/arm-with-iwmmxt.c"
46 #include "features/arm-with-vfpv2.c"
47 #include "features/arm-with-vfpv3.c"
48 #include "features/arm-with-neon.c"
50 #ifndef PTRACE_GET_THREAD_AREA
51 #define PTRACE_GET_THREAD_AREA 22
54 #ifndef PTRACE_GETWMMXREGS
55 #define PTRACE_GETWMMXREGS 18
56 #define PTRACE_SETWMMXREGS 19
59 #ifndef PTRACE_GETVFPREGS
60 #define PTRACE_GETVFPREGS 27
61 #define PTRACE_SETVFPREGS 28
64 /* These are in <asm/elf.h> in current kernels. */
66 #define HWCAP_IWMMXT 512
67 #define HWCAP_NEON 4096
68 #define HWCAP_VFPv3 8192
69 #define HWCAP_VFPv3D16 16384
71 /* A flag for whether the WMMX registers are available. */
72 static int arm_linux_has_wmmx_registers;
74 /* The number of 64-bit VFP registers we have (expect this to be 0,
76 static int arm_linux_vfp_register_count;
78 extern int arm_apcs_32;
80 /* The following variables are used to determine the version of the
81 underlying GNU/Linux operating system. Examples:
83 GNU/Linux 2.0.35 GNU/Linux 2.2.12
84 os_version = 0x00020023 os_version = 0x0002020c
85 os_major = 2 os_major = 2
86 os_minor = 0 os_minor = 2
87 os_release = 35 os_release = 12
89 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
91 These are initialized using get_linux_version() from
92 _initialize_arm_linux_nat(). */
94 static unsigned int os_version, os_major, os_minor, os_release;
96 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
97 case we may be tracing more than one process at a time. In that
98 case, inferior_ptid will contain the main process ID and the
99 individual thread (process) ID. get_thread_id () is used to get
100 the thread id if it's available, and the process id otherwise. */
103 get_thread_id (ptid_t ptid)
105 int tid = TIDGET (ptid);
111 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
113 /* Get the value of a particular register from the floating point
114 state of the process and store it into regcache. */
117 fetch_fpregister (struct regcache *regcache, int regno)
120 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
122 /* Get the thread id for the ptrace call. */
123 tid = GET_THREAD_ID (inferior_ptid);
125 /* Read the floating point state. */
126 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
129 warning (_("Unable to fetch floating point register."));
134 if (ARM_FPS_REGNUM == regno)
135 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
136 fp + NWFPE_FPSR_OFFSET);
138 /* Fetch the floating point register. */
139 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
140 supply_nwfpe_register (regcache, regno, fp);
143 /* Get the whole floating point state of the process and store it
147 fetch_fpregs (struct regcache *regcache)
150 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
152 /* Get the thread id for the ptrace call. */
153 tid = GET_THREAD_ID (inferior_ptid);
155 /* Read the floating point state. */
156 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
159 warning (_("Unable to fetch the floating point registers."));
164 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
165 fp + NWFPE_FPSR_OFFSET);
167 /* Fetch the floating point registers. */
168 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
169 supply_nwfpe_register (regcache, regno, fp);
172 /* Save a particular register into the floating point state of the
173 process using the contents from regcache. */
176 store_fpregister (const struct regcache *regcache, int regno)
179 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
181 /* Get the thread id for the ptrace call. */
182 tid = GET_THREAD_ID (inferior_ptid);
184 /* Read the floating point state. */
185 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
188 warning (_("Unable to fetch the floating point registers."));
193 if (ARM_FPS_REGNUM == regno
194 && REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
195 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
197 /* Store the floating point register. */
198 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
199 collect_nwfpe_register (regcache, regno, fp);
201 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
204 warning (_("Unable to store floating point register."));
209 /* Save the whole floating point state of the process using
210 the contents from regcache. */
213 store_fpregs (const struct regcache *regcache)
216 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
218 /* Get the thread id for the ptrace call. */
219 tid = GET_THREAD_ID (inferior_ptid);
221 /* Read the floating point state. */
222 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
225 warning (_("Unable to fetch the floating point registers."));
230 if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
231 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
233 /* Store the floating point registers. */
234 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
235 if (REG_VALID == regcache_register_status (regcache, regno))
236 collect_nwfpe_register (regcache, regno, fp);
238 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
241 warning (_("Unable to store floating point registers."));
246 /* Fetch a general register of the process and store into
250 fetch_register (struct regcache *regcache, int regno)
255 /* Get the thread id for the ptrace call. */
256 tid = GET_THREAD_ID (inferior_ptid);
258 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
261 warning (_("Unable to fetch general register."));
265 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
266 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
268 if (ARM_PS_REGNUM == regno)
271 regcache_raw_supply (regcache, ARM_PS_REGNUM,
272 (char *) ®s[ARM_CPSR_GREGNUM]);
274 regcache_raw_supply (regcache, ARM_PS_REGNUM,
275 (char *) ®s[ARM_PC_REGNUM]);
278 if (ARM_PC_REGNUM == regno)
280 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
281 (get_regcache_arch (regcache),
282 regs[ARM_PC_REGNUM]);
283 regcache_raw_supply (regcache, ARM_PC_REGNUM,
284 (char *) ®s[ARM_PC_REGNUM]);
288 /* Fetch all general registers of the process and store into
292 fetch_regs (struct regcache *regcache)
297 /* Get the thread id for the ptrace call. */
298 tid = GET_THREAD_ID (inferior_ptid);
300 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
303 warning (_("Unable to fetch general registers."));
307 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
308 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
311 regcache_raw_supply (regcache, ARM_PS_REGNUM,
312 (char *) ®s[ARM_CPSR_GREGNUM]);
314 regcache_raw_supply (regcache, ARM_PS_REGNUM,
315 (char *) ®s[ARM_PC_REGNUM]);
317 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
318 (get_regcache_arch (regcache), regs[ARM_PC_REGNUM]);
319 regcache_raw_supply (regcache, ARM_PC_REGNUM,
320 (char *) ®s[ARM_PC_REGNUM]);
323 /* Store all general registers of the process from the values in
327 store_register (const struct regcache *regcache, int regno)
332 if (REG_VALID != regcache_register_status (regcache, regno))
335 /* Get the thread id for the ptrace call. */
336 tid = GET_THREAD_ID (inferior_ptid);
338 /* Get the general registers from the process. */
339 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
342 warning (_("Unable to fetch general registers."));
346 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
347 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
348 else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
349 regcache_raw_collect (regcache, regno,
350 (char *) ®s[ARM_CPSR_GREGNUM]);
351 else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
352 regcache_raw_collect (regcache, ARM_PC_REGNUM,
353 (char *) ®s[ARM_PC_REGNUM]);
355 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
358 warning (_("Unable to store general register."));
364 store_regs (const struct regcache *regcache)
369 /* Get the thread id for the ptrace call. */
370 tid = GET_THREAD_ID (inferior_ptid);
372 /* Fetch the general registers. */
373 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
376 warning (_("Unable to fetch general registers."));
380 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
382 if (REG_VALID == regcache_register_status (regcache, regno))
383 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
386 if (arm_apcs_32 && REG_VALID == regcache_register_status (regcache, ARM_PS_REGNUM))
387 regcache_raw_collect (regcache, ARM_PS_REGNUM,
388 (char *) ®s[ARM_CPSR_GREGNUM]);
390 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
394 warning (_("Unable to store general registers."));
399 /* Fetch all WMMX registers of the process and store into
402 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
405 fetch_wmmx_regs (struct regcache *regcache)
407 char regbuf[IWMMXT_REGS_SIZE];
410 /* Get the thread id for the ptrace call. */
411 tid = GET_THREAD_ID (inferior_ptid);
413 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
416 warning (_("Unable to fetch WMMX registers."));
420 for (regno = 0; regno < 16; regno++)
421 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
424 for (regno = 0; regno < 2; regno++)
425 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
426 ®buf[16 * 8 + regno * 4]);
428 for (regno = 0; regno < 4; regno++)
429 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
430 ®buf[16 * 8 + 2 * 4 + regno * 4]);
434 store_wmmx_regs (const struct regcache *regcache)
436 char regbuf[IWMMXT_REGS_SIZE];
439 /* Get the thread id for the ptrace call. */
440 tid = GET_THREAD_ID (inferior_ptid);
442 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
445 warning (_("Unable to fetch WMMX registers."));
449 for (regno = 0; regno < 16; regno++)
450 if (REG_VALID == regcache_register_status (regcache,
451 regno + ARM_WR0_REGNUM))
452 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
455 for (regno = 0; regno < 2; regno++)
456 if (REG_VALID == regcache_register_status (regcache,
457 regno + ARM_WCSSF_REGNUM))
458 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
459 ®buf[16 * 8 + regno * 4]);
461 for (regno = 0; regno < 4; regno++)
462 if (REG_VALID == regcache_register_status (regcache,
463 regno + ARM_WCGR0_REGNUM))
464 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
465 ®buf[16 * 8 + 2 * 4 + regno * 4]);
467 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
471 warning (_("Unable to store WMMX registers."));
476 /* Fetch and store VFP Registers. The kernel object has space for 32
477 64-bit registers, and the FPSCR. This is even when on a VFPv2 or
479 #define VFP_REGS_SIZE (32 * 8 + 4)
482 fetch_vfp_regs (struct regcache *regcache)
484 char regbuf[VFP_REGS_SIZE];
487 /* Get the thread id for the ptrace call. */
488 tid = GET_THREAD_ID (inferior_ptid);
490 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
493 warning (_("Unable to fetch VFP registers."));
497 for (regno = 0; regno < arm_linux_vfp_register_count; regno++)
498 regcache_raw_supply (regcache, regno + ARM_D0_REGNUM,
499 (char *) regbuf + regno * 8);
501 regcache_raw_supply (regcache, ARM_FPSCR_REGNUM,
502 (char *) regbuf + 32 * 8);
506 store_vfp_regs (const struct regcache *regcache)
508 char regbuf[VFP_REGS_SIZE];
511 /* Get the thread id for the ptrace call. */
512 tid = GET_THREAD_ID (inferior_ptid);
514 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
517 warning (_("Unable to fetch VFP registers (for update)."));
521 for (regno = 0; regno < arm_linux_vfp_register_count; regno++)
522 regcache_raw_collect (regcache, regno + ARM_D0_REGNUM,
523 (char *) regbuf + regno * 8);
525 regcache_raw_collect (regcache, ARM_FPSCR_REGNUM,
526 (char *) regbuf + 32 * 8);
528 ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
532 warning (_("Unable to store VFP registers."));
537 /* Fetch registers from the child process. Fetch all registers if
538 regno == -1, otherwise fetch all general registers or all floating
539 point registers depending upon the value of regno. */
542 arm_linux_fetch_inferior_registers (struct target_ops *ops,
543 struct regcache *regcache, int regno)
547 fetch_regs (regcache);
548 fetch_fpregs (regcache);
549 if (arm_linux_has_wmmx_registers)
550 fetch_wmmx_regs (regcache);
551 if (arm_linux_vfp_register_count > 0)
552 fetch_vfp_regs (regcache);
556 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
557 fetch_register (regcache, regno);
558 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
559 fetch_fpregister (regcache, regno);
560 else if (arm_linux_has_wmmx_registers
561 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
562 fetch_wmmx_regs (regcache);
563 else if (arm_linux_vfp_register_count > 0
564 && regno >= ARM_D0_REGNUM
565 && regno <= ARM_D0_REGNUM + arm_linux_vfp_register_count)
566 fetch_vfp_regs (regcache);
570 /* Store registers back into the inferior. Store all registers if
571 regno == -1, otherwise store all general registers or all floating
572 point registers depending upon the value of regno. */
575 arm_linux_store_inferior_registers (struct target_ops *ops,
576 struct regcache *regcache, int regno)
580 store_regs (regcache);
581 store_fpregs (regcache);
582 if (arm_linux_has_wmmx_registers)
583 store_wmmx_regs (regcache);
584 if (arm_linux_vfp_register_count > 0)
585 store_vfp_regs (regcache);
589 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
590 store_register (regcache, regno);
591 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
592 store_fpregister (regcache, regno);
593 else if (arm_linux_has_wmmx_registers
594 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
595 store_wmmx_regs (regcache);
596 else if (arm_linux_vfp_register_count > 0
597 && regno >= ARM_D0_REGNUM
598 && regno <= ARM_D0_REGNUM + arm_linux_vfp_register_count)
599 store_vfp_regs (regcache);
603 /* Wrapper functions for the standard regset handling, used by
607 fill_gregset (const struct regcache *regcache,
608 gdb_gregset_t *gregsetp, int regno)
610 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
614 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
616 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
620 fill_fpregset (const struct regcache *regcache,
621 gdb_fpregset_t *fpregsetp, int regno)
623 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
626 /* Fill GDB's register array with the floating-point register values
630 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
632 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
635 /* Fetch the thread-local storage pointer for libthread_db. */
638 ps_get_thread_area (const struct ps_prochandle *ph,
639 lwpid_t lwpid, int idx, void **base)
641 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
644 /* IDX is the bias from the thread pointer to the beginning of the
645 thread descriptor. It has to be subtracted due to implementation
646 quirks in libthread_db. */
647 *base = (void *) ((char *)*base - idx);
653 get_linux_version (unsigned int *vmajor,
654 unsigned int *vminor,
655 unsigned int *vrelease)
658 char *pmajor, *pminor, *prelease, *tail;
660 if (-1 == uname (&info))
662 warning (_("Unable to determine GNU/Linux version."));
666 pmajor = strtok (info.release, ".");
667 pminor = strtok (NULL, ".");
668 prelease = strtok (NULL, ".");
670 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
671 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
672 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
674 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
677 static const struct target_desc *
678 arm_linux_read_description (struct target_ops *ops)
680 CORE_ADDR arm_hwcap = 0;
681 arm_linux_has_wmmx_registers = 0;
682 arm_linux_vfp_register_count = 0;
684 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
689 if (arm_hwcap & HWCAP_IWMMXT)
691 arm_linux_has_wmmx_registers = 1;
692 if (tdesc_arm_with_iwmmxt == NULL)
693 initialize_tdesc_arm_with_iwmmxt ();
694 return tdesc_arm_with_iwmmxt;
697 if (arm_hwcap & HWCAP_VFP)
701 const struct target_desc * result = NULL;
703 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
704 Neon with VFPv3-D32. */
705 if (arm_hwcap & HWCAP_NEON)
707 arm_linux_vfp_register_count = 32;
708 if (tdesc_arm_with_neon == NULL)
709 initialize_tdesc_arm_with_neon ();
710 result = tdesc_arm_with_neon;
712 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
714 arm_linux_vfp_register_count = 32;
715 if (tdesc_arm_with_vfpv3 == NULL)
716 initialize_tdesc_arm_with_vfpv3 ();
717 result = tdesc_arm_with_vfpv3;
721 arm_linux_vfp_register_count = 16;
722 if (tdesc_arm_with_vfpv2 == NULL)
723 initialize_tdesc_arm_with_vfpv2 ();
724 result = tdesc_arm_with_vfpv2;
727 /* Now make sure that the kernel supports reading these
728 registers. Support was added in 2.6.30. */
729 pid = GET_LWP (inferior_ptid);
731 buf = alloca (VFP_REGS_SIZE);
732 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
742 void _initialize_arm_linux_nat (void);
745 _initialize_arm_linux_nat (void)
747 struct target_ops *t;
749 os_version = get_linux_version (&os_major, &os_minor, &os_release);
751 /* Fill in the generic GNU/Linux methods. */
754 /* Add our register access methods. */
755 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
756 t->to_store_registers = arm_linux_store_inferior_registers;
758 t->to_read_description = arm_linux_read_description;
760 /* Register the target. */
761 linux_nat_add_target (t);