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 extern int arm_apcs_32
;
43 #define typeSingle 0x01
44 #define typeDouble 0x02
45 #define typeExtended 0x03
47 #define ARM_CPSR_REGNUM 16
49 typedef union tagFPREG
52 unsigned int fDouble
[2];
53 unsigned int fExtended
[3];
57 typedef struct tagFPA11
59 FPREG fpreg
[8]; /* 8 floating point registers */
60 unsigned int fpsr
; /* floating point status register */
61 unsigned int fpcr
; /* floating point control register */
62 unsigned char fType
[8]; /* type of floating point value held in
63 floating point registers. */
64 int initflag
; /* NWFPE initialization flag. */
68 /* The following variables are used to determine the version of the
69 underlying GNU/Linux operating system. Examples:
71 GNU/Linux 2.0.35 GNU/Linux 2.2.12
72 os_version = 0x00020023 os_version = 0x0002020c
73 os_major = 2 os_major = 2
74 os_minor = 0 os_minor = 2
75 os_release = 35 os_release = 12
77 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
79 These are initialized using get_linux_version() from
80 _initialize_arm_linux_nat(). */
82 static unsigned int os_version
, os_major
, os_minor
, os_release
;
84 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
85 case we may be tracing more than one process at a time. In that
86 case, inferior_ptid will contain the main process ID and the
87 individual thread (process) ID. get_thread_id () is used to get
88 the thread id if it's available, and the process id otherwise. */
91 get_thread_id (ptid_t ptid
)
93 int tid
= TIDGET (ptid
);
98 #define GET_THREAD_ID(PTID) get_thread_id ((PTID));
101 fetch_nwfpe_single (unsigned int fn
, FPA11
* fpa11
)
105 mem
[0] = fpa11
->fpreg
[fn
].fSingle
;
108 regcache_raw_supply (current_regcache
, ARM_F0_REGNUM
+ fn
, (char *) &mem
[0]);
112 fetch_nwfpe_double (unsigned int fn
, FPA11
* fpa11
)
116 mem
[0] = fpa11
->fpreg
[fn
].fDouble
[1];
117 mem
[1] = fpa11
->fpreg
[fn
].fDouble
[0];
119 regcache_raw_supply (current_regcache
, ARM_F0_REGNUM
+ fn
, (char *) &mem
[0]);
123 fetch_nwfpe_none (unsigned int fn
)
125 unsigned int mem
[3] =
128 regcache_raw_supply (current_regcache
, ARM_F0_REGNUM
+ fn
, (char *) &mem
[0]);
132 fetch_nwfpe_extended (unsigned int fn
, FPA11
* fpa11
)
136 mem
[0] = fpa11
->fpreg
[fn
].fExtended
[0]; /* sign & exponent */
137 mem
[1] = fpa11
->fpreg
[fn
].fExtended
[2]; /* ls bits */
138 mem
[2] = fpa11
->fpreg
[fn
].fExtended
[1]; /* ms bits */
139 regcache_raw_supply (current_regcache
, ARM_F0_REGNUM
+ fn
, (char *) &mem
[0]);
143 fetch_nwfpe_register (int regno
, FPA11
* fpa11
)
145 int fn
= regno
- ARM_F0_REGNUM
;
147 switch (fpa11
->fType
[fn
])
150 fetch_nwfpe_single (fn
, fpa11
);
154 fetch_nwfpe_double (fn
, fpa11
);
158 fetch_nwfpe_extended (fn
, fpa11
);
162 fetch_nwfpe_none (fn
);
167 store_nwfpe_single (unsigned int fn
, FPA11
*fpa11
)
171 regcache_raw_collect (current_regcache
, ARM_F0_REGNUM
+ fn
,
173 fpa11
->fpreg
[fn
].fSingle
= mem
[0];
174 fpa11
->fType
[fn
] = typeSingle
;
178 store_nwfpe_double (unsigned int fn
, FPA11
*fpa11
)
182 regcache_raw_collect (current_regcache
, ARM_F0_REGNUM
+ fn
,
184 fpa11
->fpreg
[fn
].fDouble
[1] = mem
[0];
185 fpa11
->fpreg
[fn
].fDouble
[0] = mem
[1];
186 fpa11
->fType
[fn
] = typeDouble
;
190 store_nwfpe_extended (unsigned int fn
, FPA11
*fpa11
)
194 regcache_raw_collect (current_regcache
, ARM_F0_REGNUM
+ fn
,
196 fpa11
->fpreg
[fn
].fExtended
[0] = mem
[0]; /* sign & exponent */
197 fpa11
->fpreg
[fn
].fExtended
[2] = mem
[1]; /* ls bits */
198 fpa11
->fpreg
[fn
].fExtended
[1] = mem
[2]; /* ms bits */
199 fpa11
->fType
[fn
] = typeDouble
;
203 store_nwfpe_register (int regno
, FPA11
* fpa11
)
205 if (register_cached (regno
))
207 unsigned int fn
= regno
- ARM_F0_REGNUM
;
208 switch (fpa11
->fType
[fn
])
211 store_nwfpe_single (fn
, fpa11
);
215 store_nwfpe_double (fn
, fpa11
);
219 store_nwfpe_extended (fn
, fpa11
);
226 /* Get the value of a particular register from the floating point
227 state of the process and store it into regcache. */
230 fetch_fpregister (int regno
)
235 /* Get the thread id for the ptrace call. */
236 tid
= GET_THREAD_ID (inferior_ptid
);
238 /* Read the floating point state. */
239 ret
= ptrace (PT_GETFPREGS
, tid
, 0, &fp
);
242 warning (_("Unable to fetch floating point register."));
247 if (ARM_FPS_REGNUM
== regno
)
248 regcache_raw_supply (current_regcache
, ARM_FPS_REGNUM
, (char *) &fp
.fpsr
);
250 /* Fetch the floating point register. */
251 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
253 int fn
= regno
- ARM_F0_REGNUM
;
255 switch (fp
.fType
[fn
])
258 fetch_nwfpe_single (fn
, &fp
);
262 fetch_nwfpe_double (fn
, &fp
);
266 fetch_nwfpe_extended (fn
, &fp
);
270 fetch_nwfpe_none (fn
);
275 /* Get the whole floating point state of the process and store it
284 /* Get the thread id for the ptrace call. */
285 tid
= GET_THREAD_ID (inferior_ptid
);
287 /* Read the floating point state. */
288 ret
= ptrace (PT_GETFPREGS
, tid
, 0, &fp
);
291 warning (_("Unable to fetch the floating point registers."));
296 regcache_raw_supply (current_regcache
, ARM_FPS_REGNUM
, (char *) &fp
.fpsr
);
298 /* Fetch the floating point registers. */
299 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
301 int fn
= regno
- ARM_F0_REGNUM
;
303 switch (fp
.fType
[fn
])
306 fetch_nwfpe_single (fn
, &fp
);
310 fetch_nwfpe_double (fn
, &fp
);
314 fetch_nwfpe_extended (fn
, &fp
);
318 fetch_nwfpe_none (fn
);
323 /* Save a particular register into the floating point state of the
324 process using the contents from regcache. */
327 store_fpregister (int regno
)
332 /* Get the thread id for the ptrace call. */
333 tid
= GET_THREAD_ID (inferior_ptid
);
335 /* Read the floating point state. */
336 ret
= ptrace (PT_GETFPREGS
, tid
, 0, &fp
);
339 warning (_("Unable to fetch the floating point registers."));
344 if (ARM_FPS_REGNUM
== regno
&& register_cached (ARM_FPS_REGNUM
))
345 regcache_raw_collect (current_regcache
, ARM_FPS_REGNUM
, (char *) &fp
.fpsr
);
347 /* Store the floating point register. */
348 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
350 store_nwfpe_register (regno
, &fp
);
353 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, &fp
);
356 warning (_("Unable to store floating point register."));
361 /* Save the whole floating point state of the process using
362 the contents from regcache. */
370 /* Get the thread id for the ptrace call. */
371 tid
= GET_THREAD_ID (inferior_ptid
);
373 /* Read the floating point state. */
374 ret
= ptrace (PT_GETFPREGS
, tid
, 0, &fp
);
377 warning (_("Unable to fetch the floating point registers."));
382 if (register_cached (ARM_FPS_REGNUM
))
383 regcache_raw_collect (current_regcache
, ARM_FPS_REGNUM
, (char *) &fp
.fpsr
);
385 /* Store the floating point registers. */
386 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
388 fetch_nwfpe_register (regno
, &fp
);
391 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, &fp
);
394 warning (_("Unable to store floating point registers."));
399 /* Fetch a general register of the process and store into
403 fetch_register (int regno
)
408 /* Get the thread id for the ptrace call. */
409 tid
= GET_THREAD_ID (inferior_ptid
);
411 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
414 warning (_("Unable to fetch general register."));
418 if (regno
>= ARM_A1_REGNUM
&& regno
< ARM_PC_REGNUM
)
419 regcache_raw_supply (current_regcache
, regno
, (char *) ®s
[regno
]);
421 if (ARM_PS_REGNUM
== regno
)
424 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
425 (char *) ®s
[ARM_CPSR_REGNUM
]);
427 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
428 (char *) ®s
[ARM_PC_REGNUM
]);
431 if (ARM_PC_REGNUM
== regno
)
433 regs
[ARM_PC_REGNUM
] = ADDR_BITS_REMOVE (regs
[ARM_PC_REGNUM
]);
434 regcache_raw_supply (current_regcache
, ARM_PC_REGNUM
,
435 (char *) ®s
[ARM_PC_REGNUM
]);
439 /* Fetch all general registers of the process and store into
448 /* Get the thread id for the ptrace call. */
449 tid
= GET_THREAD_ID (inferior_ptid
);
451 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
454 warning (_("Unable to fetch general registers."));
458 for (regno
= ARM_A1_REGNUM
; regno
< ARM_PC_REGNUM
; regno
++)
459 regcache_raw_supply (current_regcache
, regno
, (char *) ®s
[regno
]);
462 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
463 (char *) ®s
[ARM_CPSR_REGNUM
]);
465 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
466 (char *) ®s
[ARM_PC_REGNUM
]);
468 regs
[ARM_PC_REGNUM
] = ADDR_BITS_REMOVE (regs
[ARM_PC_REGNUM
]);
469 regcache_raw_supply (current_regcache
, ARM_PC_REGNUM
,
470 (char *) ®s
[ARM_PC_REGNUM
]);
473 /* Store all general registers of the process from the values in
477 store_register (int regno
)
482 if (!register_cached (regno
))
485 /* Get the thread id for the ptrace call. */
486 tid
= GET_THREAD_ID (inferior_ptid
);
488 /* Get the general registers from the process. */
489 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
492 warning (_("Unable to fetch general registers."));
496 if (regno
>= ARM_A1_REGNUM
&& regno
<= ARM_PC_REGNUM
)
497 regcache_raw_collect (current_regcache
, regno
, (char *) ®s
[regno
]);
498 else if (arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
499 regcache_raw_collect (current_regcache
, regno
,
500 (char *) ®s
[ARM_CPSR_REGNUM
]);
501 else if (!arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
502 regcache_raw_collect (current_regcache
, ARM_PC_REGNUM
,
503 (char *) ®s
[ARM_PC_REGNUM
]);
505 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
508 warning (_("Unable to store general register."));
519 /* Get the thread id for the ptrace call. */
520 tid
= GET_THREAD_ID (inferior_ptid
);
522 /* Fetch the general registers. */
523 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
526 warning (_("Unable to fetch general registers."));
530 for (regno
= ARM_A1_REGNUM
; regno
<= ARM_PC_REGNUM
; regno
++)
532 if (register_cached (regno
))
533 regcache_raw_collect (current_regcache
, regno
, (char *) ®s
[regno
]);
536 if (arm_apcs_32
&& register_cached (ARM_PS_REGNUM
))
537 regcache_raw_collect (current_regcache
, ARM_PS_REGNUM
,
538 (char *) ®s
[ARM_CPSR_REGNUM
]);
540 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
544 warning (_("Unable to store general registers."));
549 /* Fetch registers from the child process. Fetch all registers if
550 regno == -1, otherwise fetch all general registers or all floating
551 point registers depending upon the value of regno. */
554 arm_linux_fetch_inferior_registers (int regno
)
563 if (regno
< ARM_F0_REGNUM
|| regno
> ARM_FPS_REGNUM
)
564 fetch_register (regno
);
566 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
567 fetch_fpregister (regno
);
571 /* Store registers back into the inferior. Store all registers if
572 regno == -1, otherwise store all general registers or all floating
573 point registers depending upon the value of regno. */
576 arm_linux_store_inferior_registers (int regno
)
585 if ((regno
< ARM_F0_REGNUM
) || (regno
> ARM_FPS_REGNUM
))
586 store_register (regno
);
588 if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
589 store_fpregister (regno
);
593 /* Fill register regno (if it is a general-purpose register) in
594 *gregsetp with the appropriate value from GDB's register array.
595 If regno is -1, do this for all registers. */
598 fill_gregset (gdb_gregset_t
*gregsetp
, int regno
)
603 for (regnum
= ARM_A1_REGNUM
; regnum
<= ARM_PC_REGNUM
; regnum
++)
604 regcache_raw_collect (current_regcache
, regnum
,
605 (char *) &(*gregsetp
)[regnum
]);
607 else if (regno
>= ARM_A1_REGNUM
&& regno
<= ARM_PC_REGNUM
)
608 regcache_raw_collect (current_regcache
, regno
,
609 (char *) &(*gregsetp
)[regno
]);
611 if (ARM_PS_REGNUM
== regno
|| -1 == regno
)
614 regcache_raw_collect (current_regcache
, ARM_PS_REGNUM
,
615 (char *) &(*gregsetp
)[ARM_CPSR_REGNUM
]);
617 regcache_raw_collect (current_regcache
, ARM_PC_REGNUM
,
618 (char *) &(*gregsetp
)[ARM_PC_REGNUM
]);
622 /* Fill GDB's register array with the general-purpose register values
626 supply_gregset (gdb_gregset_t
*gregsetp
)
630 for (regno
= ARM_A1_REGNUM
; regno
< ARM_PC_REGNUM
; regno
++)
631 regcache_raw_supply (current_regcache
, regno
,
632 (char *) &(*gregsetp
)[regno
]);
635 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
636 (char *) &(*gregsetp
)[ARM_CPSR_REGNUM
]);
638 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
639 (char *) &(*gregsetp
)[ARM_PC_REGNUM
]);
641 reg_pc
= ADDR_BITS_REMOVE ((CORE_ADDR
)(*gregsetp
)[ARM_PC_REGNUM
]);
642 regcache_raw_supply (current_regcache
, ARM_PC_REGNUM
, (char *) ®_pc
);
645 /* Fill register regno (if it is a floating-point register) in
646 *fpregsetp with the appropriate value from GDB's register array.
647 If regno is -1, do this for all registers. */
650 fill_fpregset (gdb_fpregset_t
*fpregsetp
, int regno
)
652 FPA11
*fp
= (FPA11
*) fpregsetp
;
657 for (regnum
= ARM_F0_REGNUM
; regnum
<= ARM_F7_REGNUM
; regnum
++)
658 store_nwfpe_register (regnum
, fp
);
660 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
662 store_nwfpe_register (regno
, fp
);
667 if (ARM_FPS_REGNUM
== regno
|| -1 == regno
)
668 regcache_raw_collect (current_regcache
, ARM_FPS_REGNUM
,
672 /* Fill GDB's register array with the floating-point register values
676 supply_fpregset (gdb_fpregset_t
*fpregsetp
)
679 FPA11
*fp
= (FPA11
*) fpregsetp
;
682 regcache_raw_supply (current_regcache
, ARM_FPS_REGNUM
, (char *) &fp
->fpsr
);
684 /* Fetch the floating point registers. */
685 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
687 fetch_nwfpe_register (regno
, fp
);
692 arm_linux_kernel_u_size (void)
694 return (sizeof (struct user
));
698 get_linux_version (unsigned int *vmajor
,
699 unsigned int *vminor
,
700 unsigned int *vrelease
)
703 char *pmajor
, *pminor
, *prelease
, *tail
;
705 if (-1 == uname (&info
))
707 warning (_("Unable to determine GNU/Linux version."));
711 pmajor
= strtok (info
.release
, ".");
712 pminor
= strtok (NULL
, ".");
713 prelease
= strtok (NULL
, ".");
715 *vmajor
= (unsigned int) strtoul (pmajor
, &tail
, 0);
716 *vminor
= (unsigned int) strtoul (pminor
, &tail
, 0);
717 *vrelease
= (unsigned int) strtoul (prelease
, &tail
, 0);
719 return ((*vmajor
<< 16) | (*vminor
<< 8) | *vrelease
);
722 void _initialize_arm_linux_nat (void);
725 _initialize_arm_linux_nat (void)
727 struct target_ops
*t
;
729 os_version
= get_linux_version (&os_major
, &os_minor
, &os_release
);
731 /* Fill in the generic GNU/Linux methods. */
734 /* Add our register access methods. */
735 t
->to_fetch_registers
= arm_linux_fetch_inferior_registers
;
736 t
->to_store_registers
= arm_linux_store_inferior_registers
;
738 /* Register the target. */
739 linux_nat_add_target (t
);