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"
30 #include "arm-linux-tdep.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 #include "features/arm-with-iwmmxt.c"
45 #ifndef PTRACE_GET_THREAD_AREA
46 #define PTRACE_GET_THREAD_AREA 22
49 #ifndef PTRACE_GETWMMXREGS
50 #define PTRACE_GETWMMXREGS 18
51 #define PTRACE_SETWMMXREGS 19
54 /* A flag for whether the WMMX registers are available. */
55 static int arm_linux_has_wmmx_registers
;
57 extern int arm_apcs_32
;
59 /* The following variables are used to determine the version of the
60 underlying GNU/Linux operating system. Examples:
62 GNU/Linux 2.0.35 GNU/Linux 2.2.12
63 os_version = 0x00020023 os_version = 0x0002020c
64 os_major = 2 os_major = 2
65 os_minor = 0 os_minor = 2
66 os_release = 35 os_release = 12
68 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
70 These are initialized using get_linux_version() from
71 _initialize_arm_linux_nat(). */
73 static unsigned int os_version
, os_major
, os_minor
, os_release
;
75 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
76 case we may be tracing more than one process at a time. In that
77 case, inferior_ptid will contain the main process ID and the
78 individual thread (process) ID. get_thread_id () is used to get
79 the thread id if it's available, and the process id otherwise. */
82 get_thread_id (ptid_t ptid
)
84 int tid
= TIDGET (ptid
);
89 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
91 /* Get the value of a particular register from the floating point
92 state of the process and store it into regcache. */
95 fetch_fpregister (struct regcache
*regcache
, int regno
)
98 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
100 /* Get the thread id for the ptrace call. */
101 tid
= GET_THREAD_ID (inferior_ptid
);
103 /* Read the floating point state. */
104 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
107 warning (_("Unable to fetch floating point register."));
112 if (ARM_FPS_REGNUM
== regno
)
113 regcache_raw_supply (regcache
, ARM_FPS_REGNUM
,
114 fp
+ NWFPE_FPSR_OFFSET
);
116 /* Fetch the floating point register. */
117 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
118 supply_nwfpe_register (regcache
, regno
, fp
);
121 /* Get the whole floating point state of the process and store it
125 fetch_fpregs (struct regcache
*regcache
)
128 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
130 /* Get the thread id for the ptrace call. */
131 tid
= GET_THREAD_ID (inferior_ptid
);
133 /* Read the floating point state. */
134 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
137 warning (_("Unable to fetch the floating point registers."));
142 regcache_raw_supply (regcache
, ARM_FPS_REGNUM
,
143 fp
+ NWFPE_FPSR_OFFSET
);
145 /* Fetch the floating point registers. */
146 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
147 supply_nwfpe_register (regcache
, regno
, fp
);
150 /* Save a particular register into the floating point state of the
151 process using the contents from regcache. */
154 store_fpregister (const struct regcache
*regcache
, int regno
)
157 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
159 /* Get the thread id for the ptrace call. */
160 tid
= GET_THREAD_ID (inferior_ptid
);
162 /* Read the floating point state. */
163 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
166 warning (_("Unable to fetch the floating point registers."));
171 if (ARM_FPS_REGNUM
== regno
&& regcache_valid_p (regcache
, ARM_FPS_REGNUM
))
172 regcache_raw_collect (regcache
, ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
174 /* Store the floating point register. */
175 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
176 collect_nwfpe_register (regcache
, regno
, fp
);
178 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
181 warning (_("Unable to store floating point register."));
186 /* Save the whole floating point state of the process using
187 the contents from regcache. */
190 store_fpregs (const struct regcache
*regcache
)
193 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
195 /* Get the thread id for the ptrace call. */
196 tid
= GET_THREAD_ID (inferior_ptid
);
198 /* Read the floating point state. */
199 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
202 warning (_("Unable to fetch the floating point registers."));
207 if (regcache_valid_p (regcache
, ARM_FPS_REGNUM
))
208 regcache_raw_collect (regcache
, ARM_FPS_REGNUM
, fp
+ NWFPE_FPSR_OFFSET
);
210 /* Store the floating point registers. */
211 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
212 if (regcache_valid_p (regcache
, regno
))
213 collect_nwfpe_register (regcache
, regno
, fp
);
215 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
218 warning (_("Unable to store floating point registers."));
223 /* Fetch a general register of the process and store into
227 fetch_register (struct regcache
*regcache
, int regno
)
232 /* Get the thread id for the ptrace call. */
233 tid
= GET_THREAD_ID (inferior_ptid
);
235 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
238 warning (_("Unable to fetch general register."));
242 if (regno
>= ARM_A1_REGNUM
&& regno
< ARM_PC_REGNUM
)
243 regcache_raw_supply (regcache
, regno
, (char *) ®s
[regno
]);
245 if (ARM_PS_REGNUM
== regno
)
248 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
249 (char *) ®s
[ARM_CPSR_REGNUM
]);
251 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
252 (char *) ®s
[ARM_PC_REGNUM
]);
255 if (ARM_PC_REGNUM
== regno
)
257 regs
[ARM_PC_REGNUM
] = gdbarch_addr_bits_remove
258 (current_gdbarch
, regs
[ARM_PC_REGNUM
]);
259 regcache_raw_supply (regcache
, ARM_PC_REGNUM
,
260 (char *) ®s
[ARM_PC_REGNUM
]);
264 /* Fetch all general registers of the process and store into
268 fetch_regs (struct regcache
*regcache
)
273 /* Get the thread id for the ptrace call. */
274 tid
= GET_THREAD_ID (inferior_ptid
);
276 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
279 warning (_("Unable to fetch general registers."));
283 for (regno
= ARM_A1_REGNUM
; regno
< ARM_PC_REGNUM
; regno
++)
284 regcache_raw_supply (regcache
, regno
, (char *) ®s
[regno
]);
287 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
288 (char *) ®s
[ARM_CPSR_REGNUM
]);
290 regcache_raw_supply (regcache
, ARM_PS_REGNUM
,
291 (char *) ®s
[ARM_PC_REGNUM
]);
293 regs
[ARM_PC_REGNUM
] = gdbarch_addr_bits_remove
294 (current_gdbarch
, regs
[ARM_PC_REGNUM
]);
295 regcache_raw_supply (regcache
, ARM_PC_REGNUM
,
296 (char *) ®s
[ARM_PC_REGNUM
]);
299 /* Store all general registers of the process from the values in
303 store_register (const struct regcache
*regcache
, int regno
)
308 if (!regcache_valid_p (regcache
, regno
))
311 /* Get the thread id for the ptrace call. */
312 tid
= GET_THREAD_ID (inferior_ptid
);
314 /* Get the general registers from the process. */
315 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
318 warning (_("Unable to fetch general registers."));
322 if (regno
>= ARM_A1_REGNUM
&& regno
<= ARM_PC_REGNUM
)
323 regcache_raw_collect (regcache
, regno
, (char *) ®s
[regno
]);
324 else if (arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
325 regcache_raw_collect (regcache
, regno
,
326 (char *) ®s
[ARM_CPSR_REGNUM
]);
327 else if (!arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
328 regcache_raw_collect (regcache
, ARM_PC_REGNUM
,
329 (char *) ®s
[ARM_PC_REGNUM
]);
331 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
334 warning (_("Unable to store general register."));
340 store_regs (const struct regcache
*regcache
)
345 /* Get the thread id for the ptrace call. */
346 tid
= GET_THREAD_ID (inferior_ptid
);
348 /* Fetch the general registers. */
349 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
352 warning (_("Unable to fetch general registers."));
356 for (regno
= ARM_A1_REGNUM
; regno
<= ARM_PC_REGNUM
; regno
++)
358 if (regcache_valid_p (regcache
, regno
))
359 regcache_raw_collect (regcache
, regno
, (char *) ®s
[regno
]);
362 if (arm_apcs_32
&& regcache_valid_p (regcache
, ARM_PS_REGNUM
))
363 regcache_raw_collect (regcache
, ARM_PS_REGNUM
,
364 (char *) ®s
[ARM_CPSR_REGNUM
]);
366 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
370 warning (_("Unable to store general registers."));
375 /* Fetch all WMMX registers of the process and store into
378 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
381 fetch_wmmx_regs (struct regcache
*regcache
)
383 char regbuf
[IWMMXT_REGS_SIZE
];
386 /* Get the thread id for the ptrace call. */
387 tid
= GET_THREAD_ID (inferior_ptid
);
389 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
392 warning (_("Unable to fetch WMMX registers."));
396 for (regno
= 0; regno
< 16; regno
++)
397 regcache_raw_supply (regcache
, regno
+ ARM_WR0_REGNUM
,
400 for (regno
= 0; regno
< 2; regno
++)
401 regcache_raw_supply (regcache
, regno
+ ARM_WCSSF_REGNUM
,
402 ®buf
[16 * 8 + regno
* 4]);
404 for (regno
= 0; regno
< 4; regno
++)
405 regcache_raw_supply (regcache
, regno
+ ARM_WCGR0_REGNUM
,
406 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
410 store_wmmx_regs (const struct regcache
*regcache
)
412 char regbuf
[IWMMXT_REGS_SIZE
];
415 /* Get the thread id for the ptrace call. */
416 tid
= GET_THREAD_ID (inferior_ptid
);
418 ret
= ptrace (PTRACE_GETWMMXREGS
, tid
, 0, regbuf
);
421 warning (_("Unable to fetch WMMX registers."));
425 for (regno
= 0; regno
< 16; regno
++)
426 if (regcache_valid_p (regcache
, regno
+ ARM_WR0_REGNUM
))
427 regcache_raw_collect (regcache
, regno
+ ARM_WR0_REGNUM
,
430 for (regno
= 0; regno
< 2; regno
++)
431 if (regcache_valid_p (regcache
, regno
+ ARM_WCSSF_REGNUM
))
432 regcache_raw_collect (regcache
, regno
+ ARM_WCSSF_REGNUM
,
433 ®buf
[16 * 8 + regno
* 4]);
435 for (regno
= 0; regno
< 4; regno
++)
436 if (regcache_valid_p (regcache
, regno
+ ARM_WCGR0_REGNUM
))
437 regcache_raw_collect (regcache
, regno
+ ARM_WCGR0_REGNUM
,
438 ®buf
[16 * 8 + 2 * 4 + regno
* 4]);
440 ret
= ptrace (PTRACE_SETWMMXREGS
, tid
, 0, regbuf
);
444 warning (_("Unable to store WMMX registers."));
449 /* Fetch registers from the child process. Fetch all registers if
450 regno == -1, otherwise fetch all general registers or all floating
451 point registers depending upon the value of regno. */
454 arm_linux_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
458 fetch_regs (regcache
);
459 fetch_fpregs (regcache
);
460 if (arm_linux_has_wmmx_registers
)
461 fetch_wmmx_regs (regcache
);
465 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
466 fetch_register (regcache
, regno
);
467 else if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
468 fetch_fpregister (regcache
, regno
);
469 else if (arm_linux_has_wmmx_registers
470 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
471 fetch_wmmx_regs (regcache
);
475 /* Store registers back into the inferior. Store all registers if
476 regno == -1, otherwise store all general registers or all floating
477 point registers depending upon the value of regno. */
480 arm_linux_store_inferior_registers (struct regcache
*regcache
, int regno
)
484 store_regs (regcache
);
485 store_fpregs (regcache
);
486 if (arm_linux_has_wmmx_registers
)
487 store_wmmx_regs (regcache
);
491 if (regno
< ARM_F0_REGNUM
|| regno
== ARM_PS_REGNUM
)
492 store_register (regcache
, regno
);
493 else if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
494 store_fpregister (regcache
, regno
);
495 else if (arm_linux_has_wmmx_registers
496 && regno
>= ARM_WR0_REGNUM
&& regno
<= ARM_WCGR7_REGNUM
)
497 store_wmmx_regs (regcache
);
501 /* Wrapper functions for the standard regset handling, used by
505 fill_gregset (const struct regcache
*regcache
,
506 gdb_gregset_t
*gregsetp
, int regno
)
508 arm_linux_collect_gregset (NULL
, regcache
, regno
, gregsetp
, 0);
512 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
514 arm_linux_supply_gregset (NULL
, regcache
, -1, gregsetp
, 0);
518 fill_fpregset (const struct regcache
*regcache
,
519 gdb_fpregset_t
*fpregsetp
, int regno
)
521 arm_linux_collect_nwfpe (NULL
, regcache
, regno
, fpregsetp
, 0);
524 /* Fill GDB's register array with the floating-point register values
528 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
530 arm_linux_supply_nwfpe (NULL
, regcache
, -1, fpregsetp
, 0);
533 /* Fetch the thread-local storage pointer for libthread_db. */
536 ps_get_thread_area (const struct ps_prochandle
*ph
,
537 lwpid_t lwpid
, int idx
, void **base
)
539 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
542 /* IDX is the bias from the thread pointer to the beginning of the
543 thread descriptor. It has to be subtracted due to implementation
544 quirks in libthread_db. */
545 *base
= (void *) ((char *)*base
- idx
);
551 get_linux_version (unsigned int *vmajor
,
552 unsigned int *vminor
,
553 unsigned int *vrelease
)
556 char *pmajor
, *pminor
, *prelease
, *tail
;
558 if (-1 == uname (&info
))
560 warning (_("Unable to determine GNU/Linux version."));
564 pmajor
= strtok (info
.release
, ".");
565 pminor
= strtok (NULL
, ".");
566 prelease
= strtok (NULL
, ".");
568 *vmajor
= (unsigned int) strtoul (pmajor
, &tail
, 0);
569 *vminor
= (unsigned int) strtoul (pminor
, &tail
, 0);
570 *vrelease
= (unsigned int) strtoul (prelease
, &tail
, 0);
572 return ((*vmajor
<< 16) | (*vminor
<< 8) | *vrelease
);
575 static const struct target_desc
*
576 arm_linux_read_description (struct target_ops
*ops
)
579 char regbuf
[IWMMXT_REGS_SIZE
];
581 ret
= ptrace (PTRACE_GETWMMXREGS
, GET_THREAD_ID (inferior_ptid
),
584 arm_linux_has_wmmx_registers
= 0;
586 arm_linux_has_wmmx_registers
= 1;
588 if (arm_linux_has_wmmx_registers
)
589 return tdesc_arm_with_iwmmxt
;
594 void _initialize_arm_linux_nat (void);
597 _initialize_arm_linux_nat (void)
599 struct target_ops
*t
;
601 os_version
= get_linux_version (&os_major
, &os_minor
, &os_release
);
603 /* Fill in the generic GNU/Linux methods. */
606 /* Add our register access methods. */
607 t
->to_fetch_registers
= arm_linux_fetch_inferior_registers
;
608 t
->to_store_registers
= arm_linux_store_inferior_registers
;
610 t
->to_read_description
= arm_linux_read_description
;
612 /* Register the target. */
613 linux_nat_add_target (t
);
615 /* Initialize the standard target descriptions. */
616 initialize_tdesc_arm_with_iwmmxt ();