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 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"
28 #include "xml-support.h"
31 #include "arm-linux-tdep.h"
34 #include <sys/ptrace.h>
35 #include <sys/utsname.h>
36 #include <sys/procfs.h>
38 /* Prototypes for supply_gregset etc. */
41 /* Defines ps_err_e, struct ps_prochandle. */
42 #include "gdb_proc_service.h"
44 #ifndef PTRACE_GET_THREAD_AREA
45 #define PTRACE_GET_THREAD_AREA 22
48 #ifndef PTRACE_GETWMMXREGS
49 #define PTRACE_GETWMMXREGS 18
50 #define PTRACE_SETWMMXREGS 19
53 /* A flag for whether the WMMX registers are available. */
54 static int arm_linux_has_wmmx_registers;
56 extern int arm_apcs_32;
58 /* The following variables are used to determine the version of the
59 underlying GNU/Linux operating system. Examples:
61 GNU/Linux 2.0.35 GNU/Linux 2.2.12
62 os_version = 0x00020023 os_version = 0x0002020c
63 os_major = 2 os_major = 2
64 os_minor = 0 os_minor = 2
65 os_release = 35 os_release = 12
67 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
69 These are initialized using get_linux_version() from
70 _initialize_arm_linux_nat(). */
72 static unsigned int os_version, os_major, os_minor, os_release;
74 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
75 case we may be tracing more than one process at a time. In that
76 case, inferior_ptid will contain the main process ID and the
77 individual thread (process) ID. get_thread_id () is used to get
78 the thread id if it's available, and the process id otherwise. */
81 get_thread_id (ptid_t ptid)
83 int tid = TIDGET (ptid);
88 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
90 /* Get the value of a particular register from the floating point
91 state of the process and store it into regcache. */
94 fetch_fpregister (struct regcache *regcache, int regno)
97 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
99 /* Get the thread id for the ptrace call. */
100 tid = GET_THREAD_ID (inferior_ptid);
102 /* Read the floating point state. */
103 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
106 warning (_("Unable to fetch floating point register."));
111 if (ARM_FPS_REGNUM == regno)
112 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
113 fp + NWFPE_FPSR_OFFSET);
115 /* Fetch the floating point register. */
116 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
117 supply_nwfpe_register (regcache, regno, fp);
120 /* Get the whole floating point state of the process and store it
124 fetch_fpregs (struct regcache *regcache)
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 the floating point registers."));
141 regcache_raw_supply (regcache, ARM_FPS_REGNUM,
142 fp + NWFPE_FPSR_OFFSET);
144 /* Fetch the floating point registers. */
145 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
146 supply_nwfpe_register (regcache, regno, fp);
149 /* Save a particular register into the floating point state of the
150 process using the contents from regcache. */
153 store_fpregister (const struct regcache *regcache, int regno)
156 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
158 /* Get the thread id for the ptrace call. */
159 tid = GET_THREAD_ID (inferior_ptid);
161 /* Read the floating point state. */
162 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
165 warning (_("Unable to fetch the floating point registers."));
170 if (ARM_FPS_REGNUM == regno && regcache_valid_p (regcache, ARM_FPS_REGNUM))
171 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
173 /* Store the floating point register. */
174 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
175 collect_nwfpe_register (regcache, regno, fp);
177 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
180 warning (_("Unable to store floating point register."));
185 /* Save the whole floating point state of the process using
186 the contents from regcache. */
189 store_fpregs (const struct regcache *regcache)
192 gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
194 /* Get the thread id for the ptrace call. */
195 tid = GET_THREAD_ID (inferior_ptid);
197 /* Read the floating point state. */
198 ret = ptrace (PT_GETFPREGS, tid, 0, fp);
201 warning (_("Unable to fetch the floating point registers."));
206 if (regcache_valid_p (regcache, ARM_FPS_REGNUM))
207 regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
209 /* Store the floating point registers. */
210 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
211 if (regcache_valid_p (regcache, regno))
212 collect_nwfpe_register (regcache, regno, fp);
214 ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
217 warning (_("Unable to store floating point registers."));
222 /* Fetch a general register of the process and store into
226 fetch_register (struct regcache *regcache, int regno)
231 /* Get the thread id for the ptrace call. */
232 tid = GET_THREAD_ID (inferior_ptid);
234 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
237 warning (_("Unable to fetch general register."));
241 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
242 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
244 if (ARM_PS_REGNUM == regno)
247 regcache_raw_supply (regcache, ARM_PS_REGNUM,
248 (char *) ®s[ARM_CPSR_REGNUM]);
250 regcache_raw_supply (regcache, ARM_PS_REGNUM,
251 (char *) ®s[ARM_PC_REGNUM]);
254 if (ARM_PC_REGNUM == regno)
256 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
257 (current_gdbarch, regs[ARM_PC_REGNUM]);
258 regcache_raw_supply (regcache, ARM_PC_REGNUM,
259 (char *) ®s[ARM_PC_REGNUM]);
263 /* Fetch all general registers of the process and store into
267 fetch_regs (struct regcache *regcache)
272 /* Get the thread id for the ptrace call. */
273 tid = GET_THREAD_ID (inferior_ptid);
275 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
278 warning (_("Unable to fetch general registers."));
282 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
283 regcache_raw_supply (regcache, regno, (char *) ®s[regno]);
286 regcache_raw_supply (regcache, ARM_PS_REGNUM,
287 (char *) ®s[ARM_CPSR_REGNUM]);
289 regcache_raw_supply (regcache, ARM_PS_REGNUM,
290 (char *) ®s[ARM_PC_REGNUM]);
292 regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
293 (current_gdbarch, regs[ARM_PC_REGNUM]);
294 regcache_raw_supply (regcache, ARM_PC_REGNUM,
295 (char *) ®s[ARM_PC_REGNUM]);
298 /* Store all general registers of the process from the values in
302 store_register (const struct regcache *regcache, int regno)
307 if (!regcache_valid_p (regcache, regno))
310 /* Get the thread id for the ptrace call. */
311 tid = GET_THREAD_ID (inferior_ptid);
313 /* Get the general registers from the process. */
314 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
317 warning (_("Unable to fetch general registers."));
321 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
322 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
323 else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
324 regcache_raw_collect (regcache, regno,
325 (char *) ®s[ARM_CPSR_REGNUM]);
326 else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
327 regcache_raw_collect (regcache, ARM_PC_REGNUM,
328 (char *) ®s[ARM_PC_REGNUM]);
330 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
333 warning (_("Unable to store general register."));
339 store_regs (const struct regcache *regcache)
344 /* Get the thread id for the ptrace call. */
345 tid = GET_THREAD_ID (inferior_ptid);
347 /* Fetch the general registers. */
348 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
351 warning (_("Unable to fetch general registers."));
355 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
357 if (regcache_valid_p (regcache, regno))
358 regcache_raw_collect (regcache, regno, (char *) ®s[regno]);
361 if (arm_apcs_32 && regcache_valid_p (regcache, ARM_PS_REGNUM))
362 regcache_raw_collect (regcache, ARM_PS_REGNUM,
363 (char *) ®s[ARM_CPSR_REGNUM]);
365 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
369 warning (_("Unable to store general registers."));
374 /* Fetch all WMMX registers of the process and store into
377 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
380 fetch_wmmx_regs (struct regcache *regcache)
382 char regbuf[IWMMXT_REGS_SIZE];
385 /* Get the thread id for the ptrace call. */
386 tid = GET_THREAD_ID (inferior_ptid);
388 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
391 warning (_("Unable to fetch WMMX registers."));
395 for (regno = 0; regno < 16; regno++)
396 regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
399 for (regno = 0; regno < 2; regno++)
400 regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
401 ®buf[16 * 8 + regno * 4]);
403 for (regno = 0; regno < 4; regno++)
404 regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
405 ®buf[16 * 8 + 2 * 4 + regno * 4]);
409 store_wmmx_regs (const struct regcache *regcache)
411 char regbuf[IWMMXT_REGS_SIZE];
414 /* Get the thread id for the ptrace call. */
415 tid = GET_THREAD_ID (inferior_ptid);
417 ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
420 warning (_("Unable to fetch WMMX registers."));
424 for (regno = 0; regno < 16; regno++)
425 if (regcache_valid_p (regcache, regno + ARM_WR0_REGNUM))
426 regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
429 for (regno = 0; regno < 2; regno++)
430 if (regcache_valid_p (regcache, regno + ARM_WCSSF_REGNUM))
431 regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
432 ®buf[16 * 8 + regno * 4]);
434 for (regno = 0; regno < 4; regno++)
435 if (regcache_valid_p (regcache, regno + ARM_WCGR0_REGNUM))
436 regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
437 ®buf[16 * 8 + 2 * 4 + regno * 4]);
439 ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
443 warning (_("Unable to store WMMX registers."));
448 /* Fetch registers from the child process. Fetch all registers if
449 regno == -1, otherwise fetch all general registers or all floating
450 point registers depending upon the value of regno. */
453 arm_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
457 fetch_regs (regcache);
458 fetch_fpregs (regcache);
459 if (arm_linux_has_wmmx_registers)
460 fetch_wmmx_regs (regcache);
464 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
465 fetch_register (regcache, regno);
466 else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
467 fetch_fpregister (regcache, regno);
468 else if (arm_linux_has_wmmx_registers
469 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
470 fetch_wmmx_regs (regcache);
474 /* Store registers back into the inferior. Store all registers if
475 regno == -1, otherwise store all general registers or all floating
476 point registers depending upon the value of regno. */
479 arm_linux_store_inferior_registers (struct regcache *regcache, int regno)
483 store_regs (regcache);
484 store_fpregs (regcache);
485 if (arm_linux_has_wmmx_registers)
486 store_wmmx_regs (regcache);
490 if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
491 store_register (regcache, regno);
492 else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
493 store_fpregister (regcache, regno);
494 else if (arm_linux_has_wmmx_registers
495 && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
496 store_wmmx_regs (regcache);
500 /* Wrapper functions for the standard regset handling, used by
504 fill_gregset (const struct regcache *regcache,
505 gdb_gregset_t *gregsetp, int regno)
507 arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
511 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
513 arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
517 fill_fpregset (const struct regcache *regcache,
518 gdb_fpregset_t *fpregsetp, int regno)
520 arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
523 /* Fill GDB's register array with the floating-point register values
527 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
529 arm_linux_supply_nwfpe (NULL, 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);