1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007
3 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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
25 #include "gdb_string.h"
28 #include "linux-nat.h"
29 #include "target-descriptions.h"
30 #include "xml-support.h"
33 #include "arm-linux-tdep.h"
36 #include <sys/ptrace.h>
37 #include <sys/utsname.h>
38 #include <sys/procfs.h>
40 /* Prototypes for supply_gregset etc. */
43 /* Defines ps_err_e, struct ps_prochandle. */
44 #include "gdb_proc_service.h"
46 #ifndef PTRACE_GET_THREAD_AREA
47 #define PTRACE_GET_THREAD_AREA 22
50 #ifndef PTRACE_GETWMMXREGS
51 #define PTRACE_GETWMMXREGS 18
52 #define PTRACE_SETWMMXREGS 19
55 /* A flag for whether the WMMX registers are available. */
56 static int arm_linux_has_wmmx_registers;
58 extern int arm_apcs_32;
60 /* The following variables are used to determine the version of the
61 underlying GNU/Linux operating system. Examples:
63 GNU/Linux 2.0.35 GNU/Linux 2.2.12
64 os_version = 0x00020023 os_version = 0x0002020c
65 os_major = 2 os_major = 2
66 os_minor = 0 os_minor = 2
67 os_release = 35 os_release = 12
69 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
71 These are initialized using get_linux_version() from
72 _initialize_arm_linux_nat(). */
74 static unsigned int os_version, os_major, os_minor, os_release;
76 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
77 case we may be tracing more than one process at a time. In that
78 case, inferior_ptid will contain the main process ID and the
79 individual thread (process) ID. get_thread_id () is used to get
80 the thread id if it's available, and the process id otherwise. */
83 get_thread_id (ptid_t ptid)
85 int tid = TIDGET (ptid);
90 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
92 /* Get the value of a particular register from the floating point
93 state of the process and store it into regcache. */
96 fetch_fpregister (int regno)
99 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
101 /* Get the thread id for the ptrace call. */
102 tid = GET_THREAD_ID (inferior_ptid);
104 /* Read the floating point state. */
105 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
108 warning (_("Unable to fetch floating point register."));
113 if (ARM_FPS_REGNUM == regno)
114 regcache_raw_supply (current_regcache, ARM_FPS_REGNUM,
115 fp + NWFPE_FPSR_OFFSET);
117 /* Fetch the floating point register. */
118 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
119 supply_nwfpe_register (current_regcache, regno, fp);
122 /* Get the whole floating point state of the process and store it
129 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
131 /* Get the thread id for the ptrace call. */
132 tid = GET_THREAD_ID (inferior_ptid);
134 /* Read the floating point state. */
135 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
138 warning (_("Unable to fetch the floating point registers."));
143 regcache_raw_supply (current_regcache, ARM_FPS_REGNUM,
144 fp + NWFPE_FPSR_OFFSET);
146 /* Fetch the floating point registers. */
147 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
148 supply_nwfpe_register (current_regcache, regno, fp);
151 /* Save a particular register into the floating point state of the
152 process using the contents from regcache. */
155 store_fpregister (int regno)
158 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
160 /* Get the thread id for the ptrace call. */
161 tid = GET_THREAD_ID (inferior_ptid);
163 /* Read the floating point state. */
164 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
167 warning (_("Unable to fetch the floating point registers."));
172 if (ARM_FPS_REGNUM == regno && register_cached (ARM_FPS_REGNUM))
173 regcache_raw_collect (current_regcache, ARM_FPS_REGNUM,
174 fp + NWFPE_FPSR_OFFSET);
176 /* Store the floating point register. */
177 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
178 collect_nwfpe_register (current_regcache, regno, fp);
180 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
183 warning (_("Unable to store floating point register."));
188 /* Save the whole floating point state of the process using
189 the contents from regcache. */
195 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
197 /* Get the thread id for the ptrace call. */
198 tid = GET_THREAD_ID (inferior_ptid);
200 /* Read the floating point state. */
201 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
204 warning (_("Unable to fetch the floating point registers."));
209 if (register_cached (ARM_FPS_REGNUM))
210 regcache_raw_collect (current_regcache, ARM_FPS_REGNUM,
211 fp + NWFPE_FPSR_OFFSET);
213 /* Store the floating point registers. */
214 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
215 if (register_cached (regno))
216 collect_nwfpe_register (current_regcache, regno, fp);
218 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
221 warning (_("Unable to store floating point registers."));
226 /* Fetch a general register of the process and store into
230 fetch_register (int regno)
235 /* Get the thread id for the ptrace call. */
236 tid = GET_THREAD_ID (inferior_ptid);
238 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
241 warning (_("Unable to fetch general register."));
245 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
246 regcache_raw_supply (current_regcache, regno, (char *) ®s[regno]);
248 if (ARM_PS_REGNUM == regno)
251 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
252 (char *) ®s[ARM_CPSR_REGNUM]);
254 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
255 (char *) ®s[ARM_PC_REGNUM]);
258 if (ARM_PC_REGNUM == regno)
260 regs[ARM_PC_REGNUM] = ADDR_BITS_REMOVE (regs[ARM_PC_REGNUM]);
261 regcache_raw_supply (current_regcache, ARM_PC_REGNUM,
262 (char *) ®s[ARM_PC_REGNUM]);
266 /* Fetch all general registers of the process and store into
275 /* Get the thread id for the ptrace call. */
276 tid = GET_THREAD_ID (inferior_ptid);
278 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
281 warning (_("Unable to fetch general registers."));
285 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
286 regcache_raw_supply (current_regcache, regno, (char *) ®s[regno]);
289 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
290 (char *) ®s[ARM_CPSR_REGNUM]);
292 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
293 (char *) ®s[ARM_PC_REGNUM]);
295 regs[ARM_PC_REGNUM] = ADDR_BITS_REMOVE (regs[ARM_PC_REGNUM]);
296 regcache_raw_supply (current_regcache, ARM_PC_REGNUM,
297 (char *) ®s[ARM_PC_REGNUM]);
300 /* Store all general registers of the process from the values in
304 store_register (int regno)
309 if (!register_cached (regno))
312 /* Get the thread id for the ptrace call. */
313 tid = GET_THREAD_ID (inferior_ptid);
315 /* Get the general registers from the process. */
316 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
319 warning (_("Unable to fetch general registers."));
323 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
324 regcache_raw_collect (current_regcache, regno, (char *) ®s[regno]);
325 else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
326 regcache_raw_collect (current_regcache, regno,
327 (char *) ®s[ARM_CPSR_REGNUM]);
328 else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
329 regcache_raw_collect (current_regcache, ARM_PC_REGNUM,
330 (char *) ®s[ARM_PC_REGNUM]);
332 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
335 warning (_("Unable to store general register."));
346 /* Get the thread id for the ptrace call. */
347 tid = GET_THREAD_ID (inferior_ptid);
349 /* Fetch the general registers. */
350 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
353 warning (_("Unable to fetch general registers."));
357 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
359 if (register_cached (regno))
360 regcache_raw_collect (current_regcache, regno, (char *) ®s[regno]);
363 if (arm_apcs_32 && register_cached (ARM_PS_REGNUM))
364 regcache_raw_collect (current_regcache, ARM_PS_REGNUM,
365 (char *) ®s[ARM_CPSR_REGNUM]);
367 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
371 warning (_("Unable to store general registers."));
376 /* Fetch all WMMX registers of the process and store into
379 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
382 fetch_wmmx_regs (void)
384 char regbuf[IWMMXT_REGS_SIZE];
387 /* Get the thread id for the ptrace call. */
388 tid = GET_THREAD_ID (inferior_ptid);
390 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
393 warning (_("Unable to fetch WMMX registers."));
397 for (regno = 0; regno < 16; regno++)
398 regcache_raw_supply (current_regcache, regno + ARM_WR0_REGNUM,
401 for (regno = 0; regno < 2; regno++)
402 regcache_raw_supply (current_regcache, regno + ARM_WCSSF_REGNUM,
403 ®buf[16 * 8 + regno * 4]);
405 for (regno = 0; regno < 4; regno++)
406 regcache_raw_supply (current_regcache, regno + ARM_WCGR0_REGNUM,
407 ®buf[16 * 8 + 2 * 4 + regno * 4]);
411 store_wmmx_regs (void)
413 char regbuf[IWMMXT_REGS_SIZE];
416 /* Get the thread id for the ptrace call. */
417 tid = GET_THREAD_ID (inferior_ptid);
419 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
422 warning (_("Unable to fetch WMMX registers."));
426 for (regno = 0; regno < 16; regno++)
427 if (register_cached (regno + ARM_WR0_REGNUM))
428 regcache_raw_collect (current_regcache, regno + ARM_WR0_REGNUM,
431 for (regno = 0; regno < 2; regno++)
432 if (register_cached (regno + ARM_WCSSF_REGNUM))
433 regcache_raw_collect (current_regcache, regno + ARM_WCSSF_REGNUM,
434 ®buf[16 * 8 + regno * 4]);
436 for (regno = 0; regno < 4; regno++)
437 if (register_cached (regno + ARM_WCGR0_REGNUM))
438 regcache_raw_collect (current_regcache, regno + ARM_WCGR0_REGNUM,
439 ®buf[16 * 8 + 2 * 4 + regno * 4]);
441 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
445 warning (_("Unable to store WMMX registers."));
450 /* Fetch registers from the child process. Fetch all registers if
451 regno == -1, otherwise fetch all general registers or all floating
452 point registers depending upon the value of regno. */
455 arm_linux_fetch_inferior_registers (int regno)
461 if (arm_linux_has_wmmx_registers)
466 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
467 fetch_register (regno);
468 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
469 fetch_fpregister (regno);
470 else if (arm_linux_has_wmmx_registers
471 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
476 /* Store registers back into the inferior. Store all registers if
477 regno == -1, otherwise store all general registers or all floating
478 point registers depending upon the value of regno. */
481 arm_linux_store_inferior_registers (int regno)
487 if (arm_linux_has_wmmx_registers)
492 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
493 store_register (regno);
494 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
495 store_fpregister (regno);
496 else if (arm_linux_has_wmmx_registers
497 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
502 /* Wrapper functions for the standard regset handling, used by
506 fill_gregset (gdb_gregset_t *gregsetp, int regno)
508 arm_linux_collect_gregset (NULL, current_regcache, regno, gregsetp, 0);
512 supply_gregset (gdb_gregset_t *gregsetp)
514 arm_linux_supply_gregset (NULL, current_regcache, -1, gregsetp, 0);
518 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
520 arm_linux_collect_nwfpe (NULL, current_regcache, regno, fpregsetp, 0);
523 /* Fill GDB's register array with the floating-point register values
527 supply_fpregset (gdb_fpregset_t *fpregsetp)
529 arm_linux_supply_nwfpe (NULL, current_regcache, -1, fpregsetp, 0);
532 /* Fetch the thread-local storage pointer for libthread_db. */
535 ps_get_thread_area (const struct ps_prochandle *ph,
536 lwpid_t lwpid, int idx, void **base)
538 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
541 /* IDX is the bias from the thread pointer to the beginning of the
542 thread descriptor. It has to be subtracted due to implementation
543 quirks in libthread_db. */
544 *base = (void *) ((char *)*base - idx);
550 get_linux_version (unsigned int *vmajor,
551 unsigned int *vminor,
552 unsigned int *vrelease)
555 char *pmajor, *pminor, *prelease, *tail;
557 if (-1 == uname (&info))
559 warning (_("Unable to determine GNU/Linux version."));
563 pmajor = strtok (info.release, ".");
564 pminor = strtok (NULL, ".");
565 prelease = strtok (NULL, ".");
567 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
568 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
569 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
571 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
574 static LONGEST (*super_xfer_partial) (struct target_ops *, enum target_object,
575 const char *, gdb_byte *, const gdb_byte *,
579 arm_linux_xfer_partial (struct target_ops *ops,
580 enum target_object object,
582 gdb_byte *readbuf, const gdb_byte *writebuf,
583 ULONGEST offset, LONGEST len)
585 if (object == TARGET_OBJECT_AVAILABLE_FEATURES)
587 if (annex != NULL && strcmp (annex, "target.xml") == 0)
590 char regbuf[IWMMXT_REGS_SIZE];
592 ret = ptrace (PTRACE_GETWMMXREGS, GET_THREAD_ID (inferior_ptid),
595 arm_linux_has_wmmx_registers = 0;
597 arm_linux_has_wmmx_registers = 1;
599 if (arm_linux_has_wmmx_registers)
600 annex = "arm-with-iwmmxt.xml";
605 return xml_builtin_xfer_partial (annex, readbuf, writebuf, offset, len);
608 return super_xfer_partial (ops, object, annex, readbuf, writebuf,
612 void _initialize_arm_linux_nat (void);
615 _initialize_arm_linux_nat (void)
617 struct target_ops *t;
619 os_version = get_linux_version (&os_major, &os_minor, &os_release);
621 /* Fill in the generic GNU/Linux methods. */
624 /* Add our register access methods. */
625 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
626 t->to_store_registers = arm_linux_store_inferior_registers;
628 /* Override the default to_xfer_partial. */
629 super_xfer_partial = t->to_xfer_partial;
630 t->to_xfer_partial = arm_linux_xfer_partial;
632 /* Register the target. */
633 linux_nat_add_target (t);