1 /* GNU/Linux on ARM native support.
2 Copyright 1999, 2000, 2001 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"
28 #include <sys/ptrace.h>
29 #include <sys/utsname.h>
30 #include <sys/procfs.h>
32 /* Prototypes for supply_gregset etc. */
35 extern int arm_apcs_32;
38 #define typeSingle 0x01
39 #define typeDouble 0x02
40 #define typeExtended 0x03
42 #define CPSR_REGNUM 16
44 typedef union tagFPREG
47 unsigned int fDouble[2];
48 unsigned int fExtended[3];
52 typedef struct tagFPA11
54 FPREG fpreg[8]; /* 8 floating point registers */
55 unsigned int fpsr; /* floating point status register */
56 unsigned int fpcr; /* floating point control register */
57 unsigned char fType[8]; /* type of floating point value held in
58 floating point registers. */
59 int initflag; /* NWFPE initialization flag. */
63 /* The following variables are used to determine the version of the
64 underlying Linux operating system. Examples:
66 Linux 2.0.35 Linux 2.2.12
67 os_version = 0x00020023 os_version = 0x0002020c
68 os_major = 2 os_major = 2
69 os_minor = 0 os_minor = 2
70 os_release = 35 os_release = 12
72 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
74 These are initialized using get_linux_version() from
75 _initialize_arm_linux_nat(). */
77 static unsigned int os_version, os_major, os_minor, os_release;
79 /* On Linux, threads are implemented as pseudo-processes, in which
80 case we may be tracing more than one process at a time. In that
81 case, inferior_ptid will contain the main process ID and the
82 individual thread (process) ID. get_thread_id () is used to
83 get the thread id if it's available, and the process id otherwise. */
86 get_thread_id (ptid_t ptid)
88 int tid = TIDGET (ptid);
93 #define GET_THREAD_ID(PTID) get_thread_id ((PTID));
96 fetch_nwfpe_single (unsigned int fn, FPA11 * fpa11)
100 mem[0] = fpa11->fpreg[fn].fSingle;
103 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
107 fetch_nwfpe_double (unsigned int fn, FPA11 * fpa11)
111 mem[0] = fpa11->fpreg[fn].fDouble[1];
112 mem[1] = fpa11->fpreg[fn].fDouble[0];
114 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
118 fetch_nwfpe_none (unsigned int fn)
120 unsigned int mem[3] =
123 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
127 fetch_nwfpe_extended (unsigned int fn, FPA11 * fpa11)
131 mem[0] = fpa11->fpreg[fn].fExtended[0]; /* sign & exponent */
132 mem[1] = fpa11->fpreg[fn].fExtended[2]; /* ls bits */
133 mem[2] = fpa11->fpreg[fn].fExtended[1]; /* ms bits */
134 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
138 fetch_nwfpe_register (int regno, FPA11 * fpa11)
140 int fn = regno - F0_REGNUM;
142 switch (fpa11->fType[fn])
145 fetch_nwfpe_single (fn, fpa11);
149 fetch_nwfpe_double (fn, fpa11);
153 fetch_nwfpe_extended (fn, fpa11);
157 fetch_nwfpe_none (fn);
162 store_nwfpe_single (unsigned int fn, FPA11 * fpa11)
166 read_register_gen (F0_REGNUM + fn, (char *) &mem[0]);
167 fpa11->fpreg[fn].fSingle = mem[0];
168 fpa11->fType[fn] = typeSingle;
172 store_nwfpe_double (unsigned int fn, FPA11 * fpa11)
176 read_register_gen (F0_REGNUM + fn, (char *) &mem[0]);
177 fpa11->fpreg[fn].fDouble[1] = mem[0];
178 fpa11->fpreg[fn].fDouble[0] = mem[1];
179 fpa11->fType[fn] = typeDouble;
183 store_nwfpe_extended (unsigned int fn, FPA11 * fpa11)
187 read_register_gen (F0_REGNUM + fn, (char *) &mem[0]);
188 fpa11->fpreg[fn].fExtended[0] = mem[0]; /* sign & exponent */
189 fpa11->fpreg[fn].fExtended[2] = mem[1]; /* ls bits */
190 fpa11->fpreg[fn].fExtended[1] = mem[2]; /* ms bits */
191 fpa11->fType[fn] = typeDouble;
195 store_nwfpe_register (int regno, FPA11 * fpa11)
197 if (register_cached (regno))
199 unsigned int fn = regno - F0_REGNUM;
200 switch (fpa11->fType[fn])
203 store_nwfpe_single (fn, fpa11);
207 store_nwfpe_double (fn, fpa11);
211 store_nwfpe_extended (fn, fpa11);
218 /* Get the value of a particular register from the floating point
219 state of the process and store it into regcache. */
222 fetch_fpregister (int regno)
227 /* Get the thread id for the ptrace call. */
228 tid = GET_THREAD_ID (inferior_ptid);
230 /* Read the floating point state. */
231 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
234 warning ("Unable to fetch floating point register.");
239 if (FPS_REGNUM == regno)
240 supply_register (FPS_REGNUM, (char *) &fp.fpsr);
242 /* Fetch the floating point register. */
243 if (regno >= F0_REGNUM && regno <= F7_REGNUM)
245 int fn = regno - F0_REGNUM;
247 switch (fp.fType[fn])
250 fetch_nwfpe_single (fn, &fp);
254 fetch_nwfpe_double (fn, &fp);
258 fetch_nwfpe_extended (fn, &fp);
262 fetch_nwfpe_none (fn);
267 /* Get the whole floating point state of the process and store it
276 /* Get the thread id for the ptrace call. */
277 tid = GET_THREAD_ID (inferior_ptid);
279 /* Read the floating point state. */
280 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
283 warning ("Unable to fetch the floating point registers.");
288 supply_register (FPS_REGNUM, (char *) &fp.fpsr);
290 /* Fetch the floating point registers. */
291 for (regno = F0_REGNUM; regno <= F7_REGNUM; regno++)
293 int fn = regno - F0_REGNUM;
295 switch (fp.fType[fn])
298 fetch_nwfpe_single (fn, &fp);
302 fetch_nwfpe_double (fn, &fp);
306 fetch_nwfpe_extended (fn, &fp);
310 fetch_nwfpe_none (fn);
315 /* Save a particular register into the floating point state of the
316 process using the contents from regcache. */
319 store_fpregister (int regno)
324 /* Get the thread id for the ptrace call. */
325 tid = GET_THREAD_ID (inferior_ptid);
327 /* Read the floating point state. */
328 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
331 warning ("Unable to fetch the floating point registers.");
336 if (FPS_REGNUM == regno && register_cached (FPS_REGNUM))
337 read_register_gen (FPS_REGNUM, (char *) &fp.fpsr);
339 /* Store the floating point register. */
340 if (regno >= F0_REGNUM && regno <= F7_REGNUM)
342 store_nwfpe_register (regno, &fp);
345 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
348 warning ("Unable to store floating point register.");
353 /* Save the whole floating point state of the process using
354 the contents from regcache. */
362 /* Get the thread id for the ptrace call. */
363 tid = GET_THREAD_ID (inferior_ptid);
365 /* Read the floating point state. */
366 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
369 warning ("Unable to fetch the floating point registers.");
374 if (register_cached (FPS_REGNUM))
375 read_register_gen (FPS_REGNUM, (char *) &fp.fpsr);
377 /* Store the floating point registers. */
378 for (regno = F0_REGNUM; regno <= F7_REGNUM; regno++)
380 fetch_nwfpe_register (regno, &fp);
383 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
386 warning ("Unable to store floating point registers.");
391 /* Fetch a general register of the process and store into
395 fetch_register (int regno)
400 /* Get the thread id for the ptrace call. */
401 tid = GET_THREAD_ID (inferior_ptid);
403 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
406 warning ("Unable to fetch general register.");
410 if (regno >= A1_REGNUM && regno < PC_REGNUM)
411 supply_register (regno, (char *) ®s[regno]);
413 if (PS_REGNUM == regno)
416 supply_register (PS_REGNUM, (char *) ®s[CPSR_REGNUM]);
418 supply_register (PS_REGNUM, (char *) ®s[PC_REGNUM]);
421 if (PC_REGNUM == regno)
423 regs[PC_REGNUM] = ADDR_BITS_REMOVE (regs[PC_REGNUM]);
424 supply_register (PC_REGNUM, (char *) ®s[PC_REGNUM]);
428 /* Fetch all general registers of the process and store into
437 /* Get the thread id for the ptrace call. */
438 tid = GET_THREAD_ID (inferior_ptid);
440 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
443 warning ("Unable to fetch general registers.");
447 for (regno = A1_REGNUM; regno < PC_REGNUM; regno++)
448 supply_register (regno, (char *) ®s[regno]);
451 supply_register (PS_REGNUM, (char *) ®s[CPSR_REGNUM]);
453 supply_register (PS_REGNUM, (char *) ®s[PC_REGNUM]);
455 regs[PC_REGNUM] = ADDR_BITS_REMOVE (regs[PC_REGNUM]);
456 supply_register (PC_REGNUM, (char *) ®s[PC_REGNUM]);
459 /* Store all general registers of the process from the values in
463 store_register (int regno)
468 if (!register_cached (regno))
471 /* Get the thread id for the ptrace call. */
472 tid = GET_THREAD_ID (inferior_ptid);
474 /* Get the general registers from the process. */
475 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
478 warning ("Unable to fetch general registers.");
482 if (regno >= A1_REGNUM && regno <= PC_REGNUM)
483 read_register_gen (regno, (char *) ®s[regno]);
485 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
488 warning ("Unable to store general register.");
499 /* Get the thread id for the ptrace call. */
500 tid = GET_THREAD_ID (inferior_ptid);
502 /* Fetch the general registers. */
503 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
506 warning ("Unable to fetch general registers.");
510 for (regno = A1_REGNUM; regno <= PC_REGNUM; regno++)
512 if (register_cached (regno))
513 read_register_gen (regno, (char *) ®s[regno]);
516 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
520 warning ("Unable to store general registers.");
525 /* Fetch registers from the child process. Fetch all registers if
526 regno == -1, otherwise fetch all general registers or all floating
527 point registers depending upon the value of regno. */
530 fetch_inferior_registers (int regno)
539 if (regno < F0_REGNUM || regno > FPS_REGNUM)
540 fetch_register (regno);
542 if (regno >= F0_REGNUM && regno <= FPS_REGNUM)
543 fetch_fpregister (regno);
547 /* Store registers back into the inferior. Store all registers if
548 regno == -1, otherwise store all general registers or all floating
549 point registers depending upon the value of regno. */
552 store_inferior_registers (int regno)
561 if ((regno < F0_REGNUM) || (regno > FPS_REGNUM))
562 store_register (regno);
564 if ((regno >= F0_REGNUM) && (regno <= FPS_REGNUM))
565 store_fpregister (regno);
569 /* Fill register regno (if it is a general-purpose register) in
570 *gregsetp with the appropriate value from GDB's register array.
571 If regno is -1, do this for all registers. */
574 fill_gregset (gdb_gregset_t *gregsetp, int regno)
579 for (regnum = A1_REGNUM; regnum <= PC_REGNUM; regnum++)
580 read_register_gen (regnum, (char *) &(*gregsetp)[regnum]);
582 else if (regno >= A1_REGNUM && regno <= PC_REGNUM)
583 read_register_gen (regno, (char *) &(*gregsetp)[regno]);
585 if (PS_REGNUM == regno || -1 == regno)
588 read_register_gen (PS_REGNUM, (char *) &(*gregsetp)[CPSR_REGNUM]);
590 read_register_gen (PC_REGNUM, (char *) &(*gregsetp)[PC_REGNUM]);
594 /* Fill GDB's register array with the general-purpose register values
598 supply_gregset (gdb_gregset_t *gregsetp)
602 for (regno = A1_REGNUM; regno < PC_REGNUM; regno++)
603 supply_register (regno, (char *) &(*gregsetp)[regno]);
606 supply_register (PS_REGNUM, (char *) &(*gregsetp)[CPSR_REGNUM]);
608 supply_register (PS_REGNUM, (char *) &(*gregsetp)[PC_REGNUM]);
610 reg_pc = ADDR_BITS_REMOVE ((CORE_ADDR)(*gregsetp)[PC_REGNUM]);
611 supply_register (PC_REGNUM, (char *) ®_pc);
614 /* Fill register regno (if it is a floating-point register) in
615 *fpregsetp with the appropriate value from GDB's register array.
616 If regno is -1, do this for all registers. */
619 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
621 FPA11 *fp = (FPA11 *) fpregsetp;
626 for (regnum = F0_REGNUM; regnum <= F7_REGNUM; regnum++)
627 store_nwfpe_register (regnum, fp);
629 else if (regno >= F0_REGNUM && regno <= F7_REGNUM)
631 store_nwfpe_register (regno, fp);
636 if (FPS_REGNUM == regno || -1 == regno)
637 read_register_gen (FPS_REGNUM, (char *) &fp->fpsr);
640 /* Fill GDB's register array with the floating-point register values
644 supply_fpregset (gdb_fpregset_t *fpregsetp)
647 FPA11 *fp = (FPA11 *) fpregsetp;
650 supply_register (FPS_REGNUM, (char *) &fp->fpsr);
652 /* Fetch the floating point registers. */
653 for (regno = F0_REGNUM; regno <= F7_REGNUM; regno++)
655 fetch_nwfpe_register (regno, fp);
660 arm_linux_kernel_u_size (void)
662 return (sizeof (struct user));
666 get_linux_version (unsigned int *vmajor,
667 unsigned int *vminor,
668 unsigned int *vrelease)
671 char *pmajor, *pminor, *prelease, *tail;
673 if (-1 == uname (&info))
675 warning ("Unable to determine Linux version.");
679 pmajor = strtok (info.release, ".");
680 pminor = strtok (NULL, ".");
681 prelease = strtok (NULL, ".");
683 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
684 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
685 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
687 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
691 _initialize_arm_linux_nat (void)
693 os_version = get_linux_version (&os_major, &os_minor, &os_release);