1 /* GNU/Linux on ARM native support.
2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
30 #include <sys/ptrace.h>
31 #include <sys/utsname.h>
32 #include <sys/procfs.h>
34 /* Prototypes for supply_gregset etc. */
37 extern int arm_apcs_32;
40 #define typeSingle 0x01
41 #define typeDouble 0x02
42 #define typeExtended 0x03
44 #define ARM_CPSR_REGNUM 16
46 typedef union tagFPREG
49 unsigned int fDouble[2];
50 unsigned int fExtended[3];
54 typedef struct tagFPA11
56 FPREG fpreg[8]; /* 8 floating point registers */
57 unsigned int fpsr; /* floating point status register */
58 unsigned int fpcr; /* floating point control register */
59 unsigned char fType[8]; /* type of floating point value held in
60 floating point registers. */
61 int initflag; /* NWFPE initialization flag. */
65 /* The following variables are used to determine the version of the
66 underlying GNU/Linux operating system. Examples:
68 GNU/Linux 2.0.35 GNU/Linux 2.2.12
69 os_version = 0x00020023 os_version = 0x0002020c
70 os_major = 2 os_major = 2
71 os_minor = 0 os_minor = 2
72 os_release = 35 os_release = 12
74 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
76 These are initialized using get_linux_version() from
77 _initialize_arm_linux_nat(). */
79 static unsigned int os_version, os_major, os_minor, os_release;
81 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
82 case we may be tracing more than one process at a time. In that
83 case, inferior_ptid will contain the main process ID and the
84 individual thread (process) ID. get_thread_id () is used to get
85 the thread id if it's available, and the process id otherwise. */
88 get_thread_id (ptid_t ptid)
90 int tid = TIDGET (ptid);
95 #define GET_THREAD_ID(PTID) get_thread_id ((PTID));
98 fetch_nwfpe_single (unsigned int fn, FPA11 * fpa11)
102 mem[0] = fpa11->fpreg[fn].fSingle;
105 regcache_raw_supply (current_regcache, ARM_F0_REGNUM + fn, (char *) &mem[0]);
109 fetch_nwfpe_double (unsigned int fn, FPA11 * fpa11)
113 mem[0] = fpa11->fpreg[fn].fDouble[1];
114 mem[1] = fpa11->fpreg[fn].fDouble[0];
116 regcache_raw_supply (current_regcache, ARM_F0_REGNUM + fn, (char *) &mem[0]);
120 fetch_nwfpe_none (unsigned int fn)
122 unsigned int mem[3] =
125 regcache_raw_supply (current_regcache, ARM_F0_REGNUM + fn, (char *) &mem[0]);
129 fetch_nwfpe_extended (unsigned int fn, FPA11 * fpa11)
133 mem[0] = fpa11->fpreg[fn].fExtended[0]; /* sign & exponent */
134 mem[1] = fpa11->fpreg[fn].fExtended[2]; /* ls bits */
135 mem[2] = fpa11->fpreg[fn].fExtended[1]; /* ms bits */
136 regcache_raw_supply (current_regcache, ARM_F0_REGNUM + fn, (char *) &mem[0]);
140 fetch_nwfpe_register (int regno, FPA11 * fpa11)
142 int fn = regno - ARM_F0_REGNUM;
144 switch (fpa11->fType[fn])
147 fetch_nwfpe_single (fn, fpa11);
151 fetch_nwfpe_double (fn, fpa11);
155 fetch_nwfpe_extended (fn, fpa11);
159 fetch_nwfpe_none (fn);
164 store_nwfpe_single (unsigned int fn, FPA11 *fpa11)
168 regcache_raw_collect (current_regcache, ARM_F0_REGNUM + fn,
170 fpa11->fpreg[fn].fSingle = mem[0];
171 fpa11->fType[fn] = typeSingle;
175 store_nwfpe_double (unsigned int fn, FPA11 *fpa11)
179 regcache_raw_collect (current_regcache, ARM_F0_REGNUM + fn,
181 fpa11->fpreg[fn].fDouble[1] = mem[0];
182 fpa11->fpreg[fn].fDouble[0] = mem[1];
183 fpa11->fType[fn] = typeDouble;
187 store_nwfpe_extended (unsigned int fn, FPA11 *fpa11)
191 regcache_raw_collect (current_regcache, ARM_F0_REGNUM + fn,
193 fpa11->fpreg[fn].fExtended[0] = mem[0]; /* sign & exponent */
194 fpa11->fpreg[fn].fExtended[2] = mem[1]; /* ls bits */
195 fpa11->fpreg[fn].fExtended[1] = mem[2]; /* ms bits */
196 fpa11->fType[fn] = typeDouble;
200 store_nwfpe_register (int regno, FPA11 * fpa11)
202 if (register_cached (regno))
204 unsigned int fn = regno - ARM_F0_REGNUM;
205 switch (fpa11->fType[fn])
208 store_nwfpe_single (fn, fpa11);
212 store_nwfpe_double (fn, fpa11);
216 store_nwfpe_extended (fn, fpa11);
223 /* Get the value of a particular register from the floating point
224 state of the process and store it into regcache. */
227 fetch_fpregister (int regno)
232 /* Get the thread id for the ptrace call. */
233 tid = GET_THREAD_ID (inferior_ptid);
235 /* Read the floating point state. */
236 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
239 warning ("Unable to fetch floating point register.");
244 if (ARM_FPS_REGNUM == regno)
245 regcache_raw_supply (current_regcache, ARM_FPS_REGNUM, (char *) &fp.fpsr);
247 /* Fetch the floating point register. */
248 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
250 int fn = regno - ARM_F0_REGNUM;
252 switch (fp.fType[fn])
255 fetch_nwfpe_single (fn, &fp);
259 fetch_nwfpe_double (fn, &fp);
263 fetch_nwfpe_extended (fn, &fp);
267 fetch_nwfpe_none (fn);
272 /* Get the whole floating point state of the process and store it
281 /* Get the thread id for the ptrace call. */
282 tid = GET_THREAD_ID (inferior_ptid);
284 /* Read the floating point state. */
285 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
288 warning ("Unable to fetch the floating point registers.");
293 regcache_raw_supply (current_regcache, ARM_FPS_REGNUM, (char *) &fp.fpsr);
295 /* Fetch the floating point registers. */
296 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
298 int fn = regno - ARM_F0_REGNUM;
300 switch (fp.fType[fn])
303 fetch_nwfpe_single (fn, &fp);
307 fetch_nwfpe_double (fn, &fp);
311 fetch_nwfpe_extended (fn, &fp);
315 fetch_nwfpe_none (fn);
320 /* Save a particular register into the floating point state of the
321 process using the contents from regcache. */
324 store_fpregister (int regno)
329 /* Get the thread id for the ptrace call. */
330 tid = GET_THREAD_ID (inferior_ptid);
332 /* Read the floating point state. */
333 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
336 warning ("Unable to fetch the floating point registers.");
341 if (ARM_FPS_REGNUM == regno && register_cached (ARM_FPS_REGNUM))
342 regcache_raw_collect (current_regcache, ARM_FPS_REGNUM, (char *) &fp.fpsr);
344 /* Store the floating point register. */
345 if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
347 store_nwfpe_register (regno, &fp);
350 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
353 warning ("Unable to store floating point register.");
358 /* Save the whole floating point state of the process using
359 the contents from regcache. */
367 /* Get the thread id for the ptrace call. */
368 tid = GET_THREAD_ID (inferior_ptid);
370 /* Read the floating point state. */
371 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
374 warning ("Unable to fetch the floating point registers.");
379 if (register_cached (ARM_FPS_REGNUM))
380 regcache_raw_collect (current_regcache, ARM_FPS_REGNUM, (char *) &fp.fpsr);
382 /* Store the floating point registers. */
383 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
385 fetch_nwfpe_register (regno, &fp);
388 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
391 warning ("Unable to store floating point registers.");
396 /* Fetch a general register of the process and store into
400 fetch_register (int regno)
405 /* Get the thread id for the ptrace call. */
406 tid = GET_THREAD_ID (inferior_ptid);
408 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
411 warning ("Unable to fetch general register.");
415 if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
416 regcache_raw_supply (current_regcache, regno, (char *) ®s[regno]);
418 if (ARM_PS_REGNUM == regno)
421 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
422 (char *) ®s[ARM_CPSR_REGNUM]);
424 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
425 (char *) ®s[ARM_PC_REGNUM]);
428 if (ARM_PC_REGNUM == regno)
430 regs[ARM_PC_REGNUM] = ADDR_BITS_REMOVE (regs[ARM_PC_REGNUM]);
431 regcache_raw_supply (current_regcache, ARM_PC_REGNUM,
432 (char *) ®s[ARM_PC_REGNUM]);
436 /* Fetch all general registers of the process and store into
445 /* Get the thread id for the ptrace call. */
446 tid = GET_THREAD_ID (inferior_ptid);
448 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
451 warning ("Unable to fetch general registers.");
455 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
456 regcache_raw_supply (current_regcache, regno, (char *) ®s[regno]);
459 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
460 (char *) ®s[ARM_CPSR_REGNUM]);
462 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
463 (char *) ®s[ARM_PC_REGNUM]);
465 regs[ARM_PC_REGNUM] = ADDR_BITS_REMOVE (regs[ARM_PC_REGNUM]);
466 regcache_raw_supply (current_regcache, ARM_PC_REGNUM,
467 (char *) ®s[ARM_PC_REGNUM]);
470 /* Store all general registers of the process from the values in
474 store_register (int regno)
479 if (!register_cached (regno))
482 /* Get the thread id for the ptrace call. */
483 tid = GET_THREAD_ID (inferior_ptid);
485 /* Get the general registers from the process. */
486 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
489 warning ("Unable to fetch general registers.");
493 if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
494 regcache_raw_collect (current_regcache, regno, (char *) ®s[regno]);
496 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
499 warning ("Unable to store general register.");
510 /* Get the thread id for the ptrace call. */
511 tid = GET_THREAD_ID (inferior_ptid);
513 /* Fetch the general registers. */
514 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
517 warning ("Unable to fetch general registers.");
521 for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
523 if (register_cached (regno))
524 regcache_raw_collect (current_regcache, regno, (char *) ®s[regno]);
527 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
531 warning ("Unable to store general registers.");
536 /* Fetch registers from the child process. Fetch all registers if
537 regno == -1, otherwise fetch all general registers or all floating
538 point registers depending upon the value of regno. */
541 fetch_inferior_registers (int regno)
550 if (regno < ARM_F0_REGNUM || regno > ARM_FPS_REGNUM)
551 fetch_register (regno);
553 if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
554 fetch_fpregister (regno);
558 /* Store registers back into the inferior. Store all registers if
559 regno == -1, otherwise store all general registers or all floating
560 point registers depending upon the value of regno. */
563 store_inferior_registers (int regno)
572 if ((regno < ARM_F0_REGNUM) || (regno > ARM_FPS_REGNUM))
573 store_register (regno);
575 if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
576 store_fpregister (regno);
580 /* Fill register regno (if it is a general-purpose register) in
581 *gregsetp with the appropriate value from GDB's register array.
582 If regno is -1, do this for all registers. */
585 fill_gregset (gdb_gregset_t *gregsetp, int regno)
590 for (regnum = ARM_A1_REGNUM; regnum <= ARM_PC_REGNUM; regnum++)
591 regcache_raw_collect (current_regcache, regnum,
592 (char *) &(*gregsetp)[regnum]);
594 else if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
595 regcache_raw_collect (current_regcache, regno,
596 (char *) &(*gregsetp)[regno]);
598 if (ARM_PS_REGNUM == regno || -1 == regno)
601 regcache_raw_collect (current_regcache, ARM_PS_REGNUM,
602 (char *) &(*gregsetp)[ARM_CPSR_REGNUM]);
604 regcache_raw_collect (current_regcache, ARM_PC_REGNUM,
605 (char *) &(*gregsetp)[ARM_PC_REGNUM]);
609 /* Fill GDB's register array with the general-purpose register values
613 supply_gregset (gdb_gregset_t *gregsetp)
617 for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
618 regcache_raw_supply (current_regcache, regno,
619 (char *) &(*gregsetp)[regno]);
622 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
623 (char *) &(*gregsetp)[ARM_CPSR_REGNUM]);
625 regcache_raw_supply (current_regcache, ARM_PS_REGNUM,
626 (char *) &(*gregsetp)[ARM_PC_REGNUM]);
628 reg_pc = ADDR_BITS_REMOVE ((CORE_ADDR)(*gregsetp)[ARM_PC_REGNUM]);
629 regcache_raw_supply (current_regcache, ARM_PC_REGNUM, (char *) ®_pc);
632 /* Fill register regno (if it is a floating-point register) in
633 *fpregsetp with the appropriate value from GDB's register array.
634 If regno is -1, do this for all registers. */
637 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
639 FPA11 *fp = (FPA11 *) fpregsetp;
644 for (regnum = ARM_F0_REGNUM; regnum <= ARM_F7_REGNUM; regnum++)
645 store_nwfpe_register (regnum, fp);
647 else if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
649 store_nwfpe_register (regno, fp);
654 if (ARM_FPS_REGNUM == regno || -1 == regno)
655 regcache_raw_collect (current_regcache, ARM_FPS_REGNUM,
659 /* Fill GDB's register array with the floating-point register values
663 supply_fpregset (gdb_fpregset_t *fpregsetp)
666 FPA11 *fp = (FPA11 *) fpregsetp;
669 regcache_raw_supply (current_regcache, ARM_FPS_REGNUM, (char *) &fp->fpsr);
671 /* Fetch the floating point registers. */
672 for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
674 fetch_nwfpe_register (regno, fp);
679 arm_linux_kernel_u_size (void)
681 return (sizeof (struct user));
685 get_linux_version (unsigned int *vmajor,
686 unsigned int *vminor,
687 unsigned int *vrelease)
690 char *pmajor, *pminor, *prelease, *tail;
692 if (-1 == uname (&info))
694 warning ("Unable to determine GNU/Linux version.");
698 pmajor = strtok (info.release, ".");
699 pminor = strtok (NULL, ".");
700 prelease = strtok (NULL, ".");
702 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
703 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
704 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
706 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
710 _initialize_arm_linux_nat (void)
712 os_version = get_linux_version (&os_major, &os_minor, &os_release);