1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 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 && regcache_valid_p (regcache, ARM_FPS_REGNUM))
194 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
196 /* Store the floating point register. */
197 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
198 collect_nwfpe_register (regcache, regno, fp);
200 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
203 warning (_("Unable to store floating point register."));
208 /* Save the whole floating point state of the process using
209 the contents from regcache. */
212 store_fpregs (const struct regcache *regcache)
215 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
217 /* Get the thread id for the ptrace call. */
218 tid = GET_THREAD_ID (inferior_ptid);
220 /* Read the floating point state. */
221 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
224 warning (_("Unable to fetch the floating point registers."));
229 if (regcache_valid_p (regcache, ARM_FPS_REGNUM))
230 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
232 /* Store the floating point registers. */
233 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
234 if (regcache_valid_p (regcache, regno))
235 collect_nwfpe_register (regcache, regno, fp);
237 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
240 warning (_("Unable to store floating point registers."));
245 /* Fetch a general register of the process and store into
249 fetch_register (struct regcache *regcache, int regno)
254 /* Get the thread id for the ptrace call. */
255 tid = GET_THREAD_ID (inferior_ptid);
257 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
260 warning (_("Unable to fetch general register."));
264 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
265 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
267 if (ARM_PS_REGNUM == regno)
270 regcache_raw_supply (regcache, ARM_PS_REGNUM,
271 (char *) ®s[ARM_CPSR_GREGNUM]);
273 regcache_raw_supply (regcache, ARM_PS_REGNUM,
274 (char *) ®s[ARM_PC_REGNUM]);
277 if (ARM_PC_REGNUM == regno)
279 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
280 (get_regcache_arch (regcache),
281 regs[ARM_PC_REGNUM]);
282 regcache_raw_supply (regcache, ARM_PC_REGNUM,
283 (char *) ®s[ARM_PC_REGNUM]);
287 /* Fetch all general registers of the process and store into
291 fetch_regs (struct regcache *regcache)
296 /* Get the thread id for the ptrace call. */
297 tid = GET_THREAD_ID (inferior_ptid);
299 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
302 warning (_("Unable to fetch general registers."));
306 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
307 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
310 regcache_raw_supply (regcache, ARM_PS_REGNUM,
311 (char *) ®s[ARM_CPSR_GREGNUM]);
313 regcache_raw_supply (regcache, ARM_PS_REGNUM,
314 (char *) ®s[ARM_PC_REGNUM]);
316 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
317 (get_regcache_arch (regcache), regs[ARM_PC_REGNUM]);
318 regcache_raw_supply (regcache, ARM_PC_REGNUM,
319 (char *) ®s[ARM_PC_REGNUM]);
322 /* Store all general registers of the process from the values in
326 store_register (const struct regcache *regcache, int regno)
331 if (!regcache_valid_p (regcache, regno))
334 /* Get the thread id for the ptrace call. */
335 tid = GET_THREAD_ID (inferior_ptid);
337 /* Get the general registers from the process. */
338 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
341 warning (_("Unable to fetch general registers."));
345 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
346 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
347 else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
348 regcache_raw_collect (regcache, regno,
349 (char *) ®s[ARM_CPSR_GREGNUM]);
350 else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
351 regcache_raw_collect (regcache, ARM_PC_REGNUM,
352 (char *) ®s[ARM_PC_REGNUM]);
354 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
357 warning (_("Unable to store general register."));
363 store_regs (const struct regcache *regcache)
368 /* Get the thread id for the ptrace call. */
369 tid = GET_THREAD_ID (inferior_ptid);
371 /* Fetch the general registers. */
372 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
375 warning (_("Unable to fetch general registers."));
379 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
381 if (regcache_valid_p (regcache, regno))
382 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
385 if (arm_apcs_32 && regcache_valid_p (regcache, ARM_PS_REGNUM))
386 regcache_raw_collect (regcache, ARM_PS_REGNUM,
387 (char *) ®s[ARM_CPSR_GREGNUM]);
389 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
393 warning (_("Unable to store general registers."));
398 /* Fetch all WMMX registers of the process and store into
401 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
404 fetch_wmmx_regs (struct regcache *regcache)
406 char regbuf[IWMMXT_REGS_SIZE];
409 /* Get the thread id for the ptrace call. */
410 tid = GET_THREAD_ID (inferior_ptid);
412 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
415 warning (_("Unable to fetch WMMX registers."));
419 for (regno = 0; regno < 16; regno++)
420 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
423 for (regno = 0; regno < 2; regno++)
424 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
425 ®buf[16 * 8 + regno * 4]);
427 for (regno = 0; regno < 4; regno++)
428 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
429 ®buf[16 * 8 + 2 * 4 + regno * 4]);
433 store_wmmx_regs (const struct regcache *regcache)
435 char regbuf[IWMMXT_REGS_SIZE];
438 /* Get the thread id for the ptrace call. */
439 tid = GET_THREAD_ID (inferior_ptid);
441 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
444 warning (_("Unable to fetch WMMX registers."));
448 for (regno = 0; regno < 16; regno++)
449 if (regcache_valid_p (regcache, regno + ARM_WR0_REGNUM))
450 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
453 for (regno = 0; regno < 2; regno++)
454 if (regcache_valid_p (regcache, regno + ARM_WCSSF_REGNUM))
455 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
456 ®buf[16 * 8 + regno * 4]);
458 for (regno = 0; regno < 4; regno++)
459 if (regcache_valid_p (regcache, regno + ARM_WCGR0_REGNUM))
460 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
461 ®buf[16 * 8 + 2 * 4 + regno * 4]);
463 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
467 warning (_("Unable to store WMMX registers."));
472 /* Fetch and store VFP Registers. The kernel object has space for 32
473 64-bit registers, and the FPSCR. This is even when on a VFPv2 or
475 #define VFP_REGS_SIZE (32 * 8 + 4)
478 fetch_vfp_regs (struct regcache *regcache)
480 char regbuf[VFP_REGS_SIZE];
483 /* Get the thread id for the ptrace call. */
484 tid = GET_THREAD_ID (inferior_ptid);
486 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
489 warning (_("Unable to fetch VFP registers."));
493 for (regno = 0; regno < arm_linux_vfp_register_count; regno++)
494 regcache_raw_supply (regcache, regno + ARM_D0_REGNUM,
495 (char *) regbuf + regno * 8);
497 regcache_raw_supply (regcache, ARM_FPSCR_REGNUM,
498 (char *) regbuf + 32 * 8);
502 store_vfp_regs (const struct regcache *regcache)
504 char regbuf[VFP_REGS_SIZE];
507 /* Get the thread id for the ptrace call. */
508 tid = GET_THREAD_ID (inferior_ptid);
510 ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
513 warning (_("Unable to fetch VFP registers (for update)."));
517 for (regno = 0; regno < arm_linux_vfp_register_count; regno++)
518 regcache_raw_collect (regcache, regno + ARM_D0_REGNUM,
519 (char *) regbuf + regno * 8);
521 regcache_raw_collect (regcache, ARM_FPSCR_REGNUM,
522 (char *) regbuf + 32 * 8);
524 ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
528 warning (_("Unable to store VFP registers."));
533 /* Fetch registers from the child process. Fetch all registers if
534 regno == -1, otherwise fetch all general registers or all floating
535 point registers depending upon the value of regno. */
538 arm_linux_fetch_inferior_registers (struct target_ops *ops,
539 struct regcache *regcache, int regno)
543 fetch_regs (regcache);
544 fetch_fpregs (regcache);
545 if (arm_linux_has_wmmx_registers)
546 fetch_wmmx_regs (regcache);
547 if (arm_linux_vfp_register_count > 0)
548 fetch_vfp_regs (regcache);
552 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
553 fetch_register (regcache, regno);
554 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
555 fetch_fpregister (regcache, regno);
556 else if (arm_linux_has_wmmx_registers
557 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
558 fetch_wmmx_regs (regcache);
559 else if (arm_linux_vfp_register_count > 0
560 && regno >= ARM_D0_REGNUM
561 && regno <= ARM_D0_REGNUM + arm_linux_vfp_register_count)
562 fetch_vfp_regs (regcache);
566 /* Store registers back into the inferior. Store all registers if
567 regno == -1, otherwise store all general registers or all floating
568 point registers depending upon the value of regno. */
571 arm_linux_store_inferior_registers (struct target_ops *ops,
572 struct regcache *regcache, int regno)
576 store_regs (regcache);
577 store_fpregs (regcache);
578 if (arm_linux_has_wmmx_registers)
579 store_wmmx_regs (regcache);
580 if (arm_linux_vfp_register_count > 0)
581 store_vfp_regs (regcache);
585 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
586 store_register (regcache, regno);
587 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
588 store_fpregister (regcache, regno);
589 else if (arm_linux_has_wmmx_registers
590 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
591 store_wmmx_regs (regcache);
592 else if (arm_linux_vfp_register_count > 0
593 && regno >= ARM_D0_REGNUM
594 && regno <= ARM_D0_REGNUM + arm_linux_vfp_register_count)
595 store_vfp_regs (regcache);
599 /* Wrapper functions for the standard regset handling, used by
603 fill_gregset (const struct regcache *regcache,
604 gdb_gregset_t *gregsetp, int regno)
606 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
610 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
612 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
616 fill_fpregset (const struct regcache *regcache,
617 gdb_fpregset_t *fpregsetp, int regno)
619 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
622 /* Fill GDB's register array with the floating-point register values
626 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
628 arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
631 /* Fetch the thread-local storage pointer for libthread_db. */
634 ps_get_thread_area (const struct ps_prochandle *ph,
635 lwpid_t lwpid, int idx, void **base)
637 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
640 /* IDX is the bias from the thread pointer to the beginning of the
641 thread descriptor. It has to be subtracted due to implementation
642 quirks in libthread_db. */
643 *base = (void *) ((char *)*base - idx);
649 get_linux_version (unsigned int *vmajor,
650 unsigned int *vminor,
651 unsigned int *vrelease)
654 char *pmajor, *pminor, *prelease, *tail;
656 if (-1 == uname (&info))
658 warning (_("Unable to determine GNU/Linux version."));
662 pmajor = strtok (info.release, ".");
663 pminor = strtok (NULL, ".");
664 prelease = strtok (NULL, ".");
666 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
667 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
668 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
670 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
673 static const struct target_desc *
674 arm_linux_read_description (struct target_ops *ops)
676 CORE_ADDR arm_hwcap = 0;
677 arm_linux_has_wmmx_registers = 0;
678 arm_linux_vfp_register_count = 0;
680 if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
685 if (arm_hwcap & HWCAP_IWMMXT)
687 arm_linux_has_wmmx_registers = 1;
688 if (tdesc_arm_with_iwmmxt == NULL)
689 initialize_tdesc_arm_with_iwmmxt ();
690 return tdesc_arm_with_iwmmxt;
693 if (arm_hwcap & HWCAP_VFP)
697 const struct target_desc * result = NULL;
699 /* NEON implies VFPv3-D32 or no-VFP unit. Say that we only support
700 Neon with VFPv3-D32. */
701 if (arm_hwcap & HWCAP_NEON)
703 arm_linux_vfp_register_count = 32;
704 if (tdesc_arm_with_neon == NULL)
705 initialize_tdesc_arm_with_neon ();
706 result = tdesc_arm_with_neon;
708 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
710 arm_linux_vfp_register_count = 32;
711 if (tdesc_arm_with_vfpv3 == NULL)
712 initialize_tdesc_arm_with_vfpv3 ();
713 result = tdesc_arm_with_vfpv3;
717 arm_linux_vfp_register_count = 16;
718 if (tdesc_arm_with_vfpv2 == NULL)
719 initialize_tdesc_arm_with_vfpv2 ();
720 result = tdesc_arm_with_vfpv2;
723 /* Now make sure that the kernel supports reading these
724 registers. Support was added in 2.6.30. */
725 pid = GET_LWP (inferior_ptid);
727 buf = alloca (VFP_REGS_SIZE);
728 if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
738 void _initialize_arm_linux_nat (void);
741 _initialize_arm_linux_nat (void)
743 struct target_ops *t;
745 os_version = get_linux_version (&os_major, &os_minor, &os_release);
747 /* Fill in the generic GNU/Linux methods. */
750 /* Add our register access methods. */
751 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
752 t->to_store_registers = arm_linux_store_inferior_registers;
754 t->to_read_description = arm_linux_read_description;
756 /* Register the target. */
757 linux_nat_add_target (t);