1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006
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"
33 #include <sys/ptrace.h>
34 #include <sys/utsname.h>
35 #include <sys/procfs.h>
37 /* Prototypes for supply_gregset etc. */
40 /* Defines ps_err_e, struct ps_prochandle. */
41 #include "gdb_proc_service.h"
43 #ifndef PTRACE_GET_THREAD_AREA
44 #define PTRACE_GET_THREAD_AREA 22
47 extern int arm_apcs_32;
50 #define typeSingle 0x01
51 #define typeDouble 0x02
52 #define typeExtended 0x03
54 #define ARM_CPSR_REGNUM 16
56 typedef union tagFPREG
59 unsigned int fDouble[2];
60 unsigned int fExtended[3];
64 typedef struct tagFPA11
66 FPREG fpreg[8]; /* 8 floating point registers */
67 unsigned int fpsr; /* floating point status register */
68 unsigned int fpcr; /* floating point control register */
69 unsigned char fType[8]; /* type of floating point value held in
70 floating point registers. */
71 int initflag; /* NWFPE initialization flag. */
75 /* The following variables are used to determine the version of the
76 underlying GNU/Linux operating system. Examples:
78 GNU/Linux 2.0.35 GNU/Linux 2.2.12
79 os_version = 0x00020023 os_version = 0x0002020c
80 os_major = 2 os_major = 2
81 os_minor = 0 os_minor = 2
82 os_release = 35 os_release = 12
84 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
86 These are initialized using get_linux_version() from
87 _initialize_arm_linux_nat(). */
89 static unsigned int os_version, os_major, os_minor, os_release;
91 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
92 case we may be tracing more than one process at a time. In that
93 case, inferior_ptid will contain the main process ID and the
94 individual thread (process) ID. get_thread_id () is used to get
95 the thread id if it's available, and the process id otherwise. */
98 get_thread_id (ptid_t ptid)
100 int tid = TIDGET (ptid);
105 #define GET_THREAD_ID(PTID) get_thread_id ((PTID));
108 fetch_nwfpe_single (unsigned int fn, FPA11 * fpa11)
112 mem[0] = fpa11->fpreg[fn].fSingle;
115 regcache_raw_supply (current_regcache, ARM_F0_REGNUM + fn, (char *) &mem[0]);
119 fetch_nwfpe_double (unsigned int fn, FPA11 * fpa11)
123 mem[0] = fpa11->fpreg[fn].fDouble[1];
124 mem[1] = fpa11->fpreg[fn].fDouble[0];
126 regcache_raw_supply (current_regcache, ARM_F0_REGNUM + fn, (char *) &mem[0]);
130 fetch_nwfpe_none (unsigned int fn)
132 unsigned int mem[3] =
135 regcache_raw_supply (current_regcache, ARM_F0_REGNUM + fn, (char *) &mem[0]);
139 fetch_nwfpe_extended (unsigned int fn, FPA11 * fpa11)
143 mem[0] = fpa11->fpreg[fn].fExtended[0]; /* sign & exponent */
144 mem[1] = fpa11->fpreg[fn].fExtended[2]; /* ls bits */
145 mem[2] = fpa11->fpreg[fn].fExtended[1]; /* ms bits */
146 regcache_raw_supply (current_regcache, ARM_F0_REGNUM + fn, (char *) &mem[0]);
150 fetch_nwfpe_register (int regno, FPA11 * fpa11)
152 int fn = regno - ARM_F0_REGNUM;
154 switch (fpa11->fType[fn])
157 fetch_nwfpe_single (fn, fpa11);
161 fetch_nwfpe_double (fn, fpa11);
165 fetch_nwfpe_extended (fn, fpa11);
169 fetch_nwfpe_none (fn);
174 store_nwfpe_single (unsigned int fn, FPA11 *fpa11)
178 regcache_raw_collect (current_regcache, ARM_F0_REGNUM + fn,
180 fpa11->fpreg[fn].fSingle = mem[0];
181 fpa11->fType[fn] = typeSingle;
185 store_nwfpe_double (unsigned int fn, FPA11 *fpa11)
189 regcache_raw_collect (current_regcache, ARM_F0_REGNUM + fn,
191 fpa11->fpreg[fn].fDouble[1] = mem[0];
192 fpa11->fpreg[fn].fDouble[0] = mem[1];
193 fpa11->fType[fn] = typeDouble;
197 store_nwfpe_extended (unsigned int fn, FPA11 *fpa11)
201 regcache_raw_collect (current_regcache, ARM_F0_REGNUM + fn,
203 fpa11->fpreg[fn].fExtended[0] = mem[0]; /* sign & exponent */
204 fpa11->fpreg[fn].fExtended[2] = mem[1]; /* ls bits */
205 fpa11->fpreg[fn].fExtended[1] = mem[2]; /* ms bits */
206 fpa11->fType[fn] = typeDouble;
210 store_nwfpe_register (int regno, FPA11 * fpa11)
212 if (register_cached (regno))
214 unsigned int fn = regno - ARM_F0_REGNUM;
215 switch (fpa11->fType[fn])
218 store_nwfpe_single (fn, fpa11);
222 store_nwfpe_double (fn, fpa11);
226 store_nwfpe_extended (fn, fpa11);
233 /* Get the value of a particular register from the floating point
234 state of the process and store it into regcache. */
237 fetch_fpregister (int regno)
242 /* Get the thread id for the ptrace call. */
243 tid = GET_THREAD_ID (inferior_ptid);
245 /* Read the floating point state. */
246 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
249 warning (_("Unable to fetch floating point register."));
254 if (ARM_FPS_REGNUM == regno)
255 regcache_raw_supply (current_regcache, ARM_FPS_REGNUM, (char *) &fp.fpsr);
257 /* Fetch the floating point register. */
258 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
260 int fn = regno - ARM_F0_REGNUM;
262 switch (fp.fType[fn])
265 fetch_nwfpe_single (fn, &fp);
269 fetch_nwfpe_double (fn, &fp);
273 fetch_nwfpe_extended (fn, &fp);
277 fetch_nwfpe_none (fn);
282 /* Get the whole floating point state of the process and store it
291 /* Get the thread id for the ptrace call. */
292 tid = GET_THREAD_ID (inferior_ptid);
294 /* Read the floating point state. */
295 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
298 warning (_("Unable to fetch the floating point registers."));
303 regcache_raw_supply (current_regcache, ARM_FPS_REGNUM, (char *) &fp.fpsr);
305 /* Fetch the floating point registers. */
306 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
308 int fn = regno - ARM_F0_REGNUM;
310 switch (fp.fType[fn])
313 fetch_nwfpe_single (fn, &fp);
317 fetch_nwfpe_double (fn, &fp);
321 fetch_nwfpe_extended (fn, &fp);
325 fetch_nwfpe_none (fn);
330 /* Save a particular register into the floating point state of the
331 process using the contents from regcache. */
334 store_fpregister (int regno)
339 /* Get the thread id for the ptrace call. */
340 tid = GET_THREAD_ID (inferior_ptid);
342 /* Read the floating point state. */
343 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
346 warning (_("Unable to fetch the floating point registers."));
351 if (ARM_FPS_REGNUM == regno && register_cached (ARM_FPS_REGNUM))
352 regcache_raw_collect (current_regcache, ARM_FPS_REGNUM, (char *) &fp.fpsr);
354 /* Store the floating point register. */
355 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
357 store_nwfpe_register (regno, &fp);
360 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
363 warning (_("Unable to store floating point register."));
368 /* Save the whole floating point state of the process using
369 the contents from regcache. */
377 /* Get the thread id for the ptrace call. */
378 tid = GET_THREAD_ID (inferior_ptid);
380 /* Read the floating point state. */
381 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
384 warning (_("Unable to fetch the floating point registers."));
389 if (register_cached (ARM_FPS_REGNUM))
390 regcache_raw_collect (current_regcache, ARM_FPS_REGNUM, (char *) &fp.fpsr);
392 /* Store the floating point registers. */
393 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
395 fetch_nwfpe_register (regno, &fp);
398 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
401 warning (_("Unable to store floating point registers."));
406 /* Fetch a general register of the process and store into
410 fetch_register (int regno)
415 /* Get the thread id for the ptrace call. */
416 tid = GET_THREAD_ID (inferior_ptid);
418 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
421 warning (_("Unable to fetch general register."));
425 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
426 regcache_raw_supply (current_regcache, regno, (char *) ®s[regno]);
428 if (ARM_PS_REGNUM == regno)
431 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
432 (char *) ®s[ARM_CPSR_REGNUM]);
434 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
435 (char *) ®s[ARM_PC_REGNUM]);
438 if (ARM_PC_REGNUM == regno)
440 regs[ARM_PC_REGNUM] = ADDR_BITS_REMOVE (regs[ARM_PC_REGNUM]);
441 regcache_raw_supply (current_regcache, ARM_PC_REGNUM,
442 (char *) ®s[ARM_PC_REGNUM]);
446 /* Fetch all general registers of the process and store into
455 /* Get the thread id for the ptrace call. */
456 tid = GET_THREAD_ID (inferior_ptid);
458 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
461 warning (_("Unable to fetch general registers."));
465 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
466 regcache_raw_supply (current_regcache, regno, (char *) ®s[regno]);
469 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
470 (char *) ®s[ARM_CPSR_REGNUM]);
472 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
473 (char *) ®s[ARM_PC_REGNUM]);
475 regs[ARM_PC_REGNUM] = ADDR_BITS_REMOVE (regs[ARM_PC_REGNUM]);
476 regcache_raw_supply (current_regcache, ARM_PC_REGNUM,
477 (char *) ®s[ARM_PC_REGNUM]);
480 /* Store all general registers of the process from the values in
484 store_register (int regno)
489 if (!register_cached (regno))
492 /* Get the thread id for the ptrace call. */
493 tid = GET_THREAD_ID (inferior_ptid);
495 /* Get the general registers from the process. */
496 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
499 warning (_("Unable to fetch general registers."));
503 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
504 regcache_raw_collect (current_regcache, regno, (char *) ®s[regno]);
505 else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
506 regcache_raw_collect (current_regcache, regno,
507 (char *) ®s[ARM_CPSR_REGNUM]);
508 else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
509 regcache_raw_collect (current_regcache, ARM_PC_REGNUM,
510 (char *) ®s[ARM_PC_REGNUM]);
512 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
515 warning (_("Unable to store general register."));
526 /* Get the thread id for the ptrace call. */
527 tid = GET_THREAD_ID (inferior_ptid);
529 /* Fetch the general registers. */
530 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
533 warning (_("Unable to fetch general registers."));
537 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
539 if (register_cached (regno))
540 regcache_raw_collect (current_regcache, regno, (char *) ®s[regno]);
543 if (arm_apcs_32 && register_cached (ARM_PS_REGNUM))
544 regcache_raw_collect (current_regcache, ARM_PS_REGNUM,
545 (char *) ®s[ARM_CPSR_REGNUM]);
547 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
551 warning (_("Unable to store general registers."));
556 /* Fetch registers from the child process. Fetch all registers if
557 regno == -1, otherwise fetch all general registers or all floating
558 point registers depending upon the value of regno. */
561 arm_linux_fetch_inferior_registers (int regno)
570 if (regno < ARM_F0_REGNUM || regno > ARM_FPS_REGNUM)
571 fetch_register (regno);
573 if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
574 fetch_fpregister (regno);
578 /* Store registers back into the inferior. Store all registers if
579 regno == -1, otherwise store all general registers or all floating
580 point registers depending upon the value of regno. */
583 arm_linux_store_inferior_registers (int regno)
592 if ((regno < ARM_F0_REGNUM) || (regno > ARM_FPS_REGNUM))
593 store_register (regno);
595 if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
596 store_fpregister (regno);
600 /* Fill register regno (if it is a general-purpose register) in
601 *gregsetp with the appropriate value from GDB's register array.
602 If regno is -1, do this for all registers. */
605 fill_gregset (gdb_gregset_t *gregsetp, int regno)
610 for (regnum = ARM_A1_REGNUM; regnum <= ARM_PC_REGNUM; regnum++)
611 regcache_raw_collect (current_regcache, regnum,
612 (char *) &(*gregsetp)[regnum]);
614 else if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
615 regcache_raw_collect (current_regcache, regno,
616 (char *) &(*gregsetp)[regno]);
618 if (ARM_PS_REGNUM == regno || -1 == regno)
621 regcache_raw_collect (current_regcache, ARM_PS_REGNUM,
622 (char *) &(*gregsetp)[ARM_CPSR_REGNUM]);
624 regcache_raw_collect (current_regcache, ARM_PC_REGNUM,
625 (char *) &(*gregsetp)[ARM_PC_REGNUM]);
629 /* Fill GDB's register array with the general-purpose register values
633 supply_gregset (gdb_gregset_t *gregsetp)
637 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
638 regcache_raw_supply (current_regcache, regno,
639 (char *) &(*gregsetp)[regno]);
642 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
643 (char *) &(*gregsetp)[ARM_CPSR_REGNUM]);
645 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
646 (char *) &(*gregsetp)[ARM_PC_REGNUM]);
648 reg_pc = ADDR_BITS_REMOVE ((CORE_ADDR)(*gregsetp)[ARM_PC_REGNUM]);
649 regcache_raw_supply (current_regcache, ARM_PC_REGNUM, (char *) ®_pc);
652 /* Fill register regno (if it is a floating-point register) in
653 *fpregsetp with the appropriate value from GDB's register array.
654 If regno is -1, do this for all registers. */
657 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
659 FPA11 *fp = (FPA11 *) fpregsetp;
664 for (regnum = ARM_F0_REGNUM; regnum <= ARM_F7_REGNUM; regnum++)
665 store_nwfpe_register (regnum, fp);
667 else if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
669 store_nwfpe_register (regno, fp);
674 if (ARM_FPS_REGNUM == regno || -1 == regno)
675 regcache_raw_collect (current_regcache, ARM_FPS_REGNUM,
679 /* Fill GDB's register array with the floating-point register values
683 supply_fpregset (gdb_fpregset_t *fpregsetp)
686 FPA11 *fp = (FPA11 *) fpregsetp;
689 regcache_raw_supply (current_regcache, ARM_FPS_REGNUM, (char *) &fp->fpsr);
691 /* Fetch the floating point registers. */
692 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
694 fetch_nwfpe_register (regno, fp);
699 arm_linux_kernel_u_size (void)
701 return (sizeof (struct user));
704 /* Fetch the thread-local storage pointer for libthread_db. */
707 ps_get_thread_area (const struct ps_prochandle *ph,
708 lwpid_t lwpid, int idx, void **base)
710 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
713 /* IDX is the bias from the thread pointer to the beginning of the
714 thread descriptor. It has to be subtracted due to implementation
715 quirks in libthread_db. */
716 *base = (void *) ((char *)*base - idx);
722 get_linux_version (unsigned int *vmajor,
723 unsigned int *vminor,
724 unsigned int *vrelease)
727 char *pmajor, *pminor, *prelease, *tail;
729 if (-1 == uname (&info))
731 warning (_("Unable to determine GNU/Linux version."));
735 pmajor = strtok (info.release, ".");
736 pminor = strtok (NULL, ".");
737 prelease = strtok (NULL, ".");
739 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
740 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
741 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
743 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
746 void _initialize_arm_linux_nat (void);
749 _initialize_arm_linux_nat (void)
751 struct target_ops *t;
753 os_version = get_linux_version (&os_major, &os_minor, &os_release);
755 /* Fill in the generic GNU/Linux methods. */
758 /* Add our register access methods. */
759 t->to_fetch_registers = arm_linux_fetch_inferior_registers;
760 t->to_store_registers = arm_linux_store_inferior_registers;
762 /* Register the target. */
763 linux_nat_add_target (t);