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 #include <asm/ptrace.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 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 Linux operating system. Examples:
68 Linux 2.0.35 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 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
85 get 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 supply_register (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 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
120 fetch_nwfpe_none (unsigned int fn)
122 unsigned int mem[3] =
125 supply_register (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 supply_register (F0_REGNUM + fn, (char *) &mem[0]);
140 fetch_nwfpe_register (int regno, FPA11 * fpa11)
142 int fn = regno - 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 read_register_gen (F0_REGNUM + fn, (char *) &mem[0]);
169 fpa11->fpreg[fn].fSingle = mem[0];
170 fpa11->fType[fn] = typeSingle;
174 store_nwfpe_double (unsigned int fn, FPA11 * fpa11)
178 read_register_gen (F0_REGNUM + fn, (char *) &mem[0]);
179 fpa11->fpreg[fn].fDouble[1] = mem[0];
180 fpa11->fpreg[fn].fDouble[0] = mem[1];
181 fpa11->fType[fn] = typeDouble;
185 store_nwfpe_extended (unsigned int fn, FPA11 * fpa11)
189 read_register_gen (F0_REGNUM + fn, (char *) &mem[0]);
190 fpa11->fpreg[fn].fExtended[0] = mem[0]; /* sign & exponent */
191 fpa11->fpreg[fn].fExtended[2] = mem[1]; /* ls bits */
192 fpa11->fpreg[fn].fExtended[1] = mem[2]; /* ms bits */
193 fpa11->fType[fn] = typeDouble;
197 store_nwfpe_register (int regno, FPA11 * fpa11)
199 if (register_valid[regno])
201 unsigned int fn = regno - F0_REGNUM;
202 switch (fpa11->fType[fn])
205 store_nwfpe_single (fn, fpa11);
209 store_nwfpe_double (fn, fpa11);
213 store_nwfpe_extended (fn, fpa11);
220 /* Get the value of a particular register from the floating point
221 state of the process and store it into registers[]. */
224 fetch_fpregister (int regno)
229 /* Get the thread id for the ptrace call. */
230 tid = GET_THREAD_ID (inferior_ptid);
232 /* Read the floating point state. */
233 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
236 warning ("Unable to fetch floating point register.");
241 if (FPS_REGNUM == regno)
242 supply_register (FPS_REGNUM, (char *) &fp.fpsr);
244 /* Fetch the floating point register. */
245 if (regno >= F0_REGNUM && regno <= F7_REGNUM)
247 int fn = regno - F0_REGNUM;
249 switch (fp.fType[fn])
252 fetch_nwfpe_single (fn, &fp);
256 fetch_nwfpe_double (fn, &fp);
260 fetch_nwfpe_extended (fn, &fp);
264 fetch_nwfpe_none (fn);
269 /* Get the whole floating point state of the process and store it
278 /* Get the thread id for the ptrace call. */
279 tid = GET_THREAD_ID (inferior_ptid);
281 /* Read the floating point state. */
282 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
285 warning ("Unable to fetch the floating point registers.");
290 supply_register (FPS_REGNUM, (char *) &fp.fpsr);
292 /* Fetch the floating point registers. */
293 for (regno = F0_REGNUM; regno <= F7_REGNUM; regno++)
295 int fn = regno - F0_REGNUM;
297 switch (fp.fType[fn])
300 fetch_nwfpe_single (fn, &fp);
304 fetch_nwfpe_double (fn, &fp);
308 fetch_nwfpe_extended (fn, &fp);
312 fetch_nwfpe_none (fn);
317 /* Save a particular register into the floating point state of the
318 process using the contents from registers[]. */
321 store_fpregister (int regno)
326 /* Get the thread id for the ptrace call. */
327 tid = GET_THREAD_ID (inferior_ptid);
329 /* Read the floating point state. */
330 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
333 warning ("Unable to fetch the floating point registers.");
338 if (FPS_REGNUM == regno && register_valid[FPS_REGNUM])
339 read_register_gen (FPS_REGNUM, (char *) &fp.fpsr);
341 /* Store the floating point register. */
342 if (regno >= F0_REGNUM && regno <= F7_REGNUM)
344 store_nwfpe_register (regno, &fp);
347 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
350 warning ("Unable to store floating point register.");
355 /* Save the whole floating point state of the process using
356 the contents from registers[]. */
364 /* Get the thread id for the ptrace call. */
365 tid = GET_THREAD_ID (inferior_ptid);
367 /* Read the floating point state. */
368 ret = ptrace (PT_GETFPREGS, tid, 0, &fp);
371 warning ("Unable to fetch the floating point registers.");
376 if (register_valid[FPS_REGNUM])
377 read_register_gen (FPS_REGNUM, (char *) &fp.fpsr);
379 /* Store the floating point registers. */
380 for (regno = F0_REGNUM; regno <= F7_REGNUM; regno++)
382 fetch_nwfpe_register (regno, &fp);
385 ret = ptrace (PTRACE_SETFPREGS, tid, 0, &fp);
388 warning ("Unable to store floating point registers.");
393 /* Fetch a general register of the process and store into
397 fetch_register (int regno)
402 /* Get the thread id for the ptrace call. */
403 tid = GET_THREAD_ID (inferior_ptid);
405 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
408 warning ("Unable to fetch general register.");
412 if (regno >= A1_REGNUM && regno < PC_REGNUM)
413 supply_register (regno, (char *) ®s.uregs[regno]);
415 if (PS_REGNUM == regno)
418 supply_register (PS_REGNUM, (char *) ®s.uregs[CPSR_REGNUM]);
420 supply_register (PS_REGNUM, (char *) ®s.uregs[PC_REGNUM]);
423 if (PC_REGNUM == regno)
425 regs.uregs[PC_REGNUM] = ADDR_BITS_REMOVE (regs.uregs[PC_REGNUM]);
426 supply_register (PC_REGNUM, (char *) ®s.uregs[PC_REGNUM]);
430 /* Fetch all general registers of the process and store into
439 /* Get the thread id for the ptrace call. */
440 tid = GET_THREAD_ID (inferior_ptid);
442 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
445 warning ("Unable to fetch general registers.");
449 for (regno = A1_REGNUM; regno < PC_REGNUM; regno++)
450 supply_register (regno, (char *) ®s.uregs[regno]);
453 supply_register (PS_REGNUM, (char *) ®s.uregs[CPSR_REGNUM]);
455 supply_register (PS_REGNUM, (char *) ®s.uregs[PC_REGNUM]);
457 regs.uregs[PC_REGNUM] = ADDR_BITS_REMOVE (regs.uregs[PC_REGNUM]);
458 supply_register (PC_REGNUM, (char *) ®s.uregs[PC_REGNUM]);
461 /* Store all general registers of the process from the values in
465 store_register (int regno)
470 if (!register_valid[regno])
473 /* Get the thread id for the ptrace call. */
474 tid = GET_THREAD_ID (inferior_ptid);
476 /* Get the general registers from the process. */
477 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
480 warning ("Unable to fetch general registers.");
484 if (regno >= A1_REGNUM && regno <= PC_REGNUM)
485 read_register_gen (regno, (char *) ®s.uregs[regno]);
487 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
490 warning ("Unable to store general register.");
501 /* Get the thread id for the ptrace call. */
502 tid = GET_THREAD_ID (inferior_ptid);
504 /* Fetch the general registers. */
505 ret = ptrace (PTRACE_GETREGS, tid, 0, ®s);
508 warning ("Unable to fetch general registers.");
512 for (regno = A1_REGNUM; regno <= PC_REGNUM; regno++)
514 if (register_valid[regno])
515 read_register_gen (regno, (char *) ®s.uregs[regno]);
518 ret = ptrace (PTRACE_SETREGS, tid, 0, ®s);
522 warning ("Unable to store general registers.");
527 /* Fetch registers from the child process. Fetch all registers if
528 regno == -1, otherwise fetch all general registers or all floating
529 point registers depending upon the value of regno. */
532 fetch_inferior_registers (int regno)
541 if (regno < F0_REGNUM || regno > FPS_REGNUM)
542 fetch_register (regno);
544 if (regno >= F0_REGNUM && regno <= FPS_REGNUM)
545 fetch_fpregister (regno);
549 /* Store registers back into the inferior. Store all registers if
550 regno == -1, otherwise store all general registers or all floating
551 point registers depending upon the value of regno. */
554 store_inferior_registers (int regno)
563 if ((regno < F0_REGNUM) || (regno > FPS_REGNUM))
564 store_register (regno);
566 if ((regno >= F0_REGNUM) && (regno <= FPS_REGNUM))
567 store_fpregister (regno);
571 /* Fill register regno (if it is a general-purpose register) in
572 *gregsetp with the appropriate value from GDB's register array.
573 If regno is -1, do this for all registers. */
576 fill_gregset (gdb_gregset_t *gregsetp, int regno)
581 for (regnum = A1_REGNUM; regnum <= PC_REGNUM; regnum++)
582 read_register_gen (regnum, (char *) &(*gregsetp)[regnum]);
584 else if (regno >= A1_REGNUM && regno <= PC_REGNUM)
585 read_register_gen (regno, (char *) &(*gregsetp)[regno]);
587 if (PS_REGNUM == regno || -1 == regno)
590 read_register_gen (PS_REGNUM, (char *) &(*gregsetp)[CPSR_REGNUM]);
592 read_register_gen (PC_REGNUM, (char *) &(*gregsetp)[PC_REGNUM]);
596 /* Fill GDB's register array with the general-purpose register values
600 supply_gregset (gdb_gregset_t *gregsetp)
604 for (regno = A1_REGNUM; regno < PC_REGNUM; regno++)
605 supply_register (regno, (char *) &(*gregsetp)[regno]);
608 supply_register (PS_REGNUM, (char *) &(*gregsetp)[CPSR_REGNUM]);
610 supply_register (PS_REGNUM, (char *) &(*gregsetp)[PC_REGNUM]);
612 reg_pc = ADDR_BITS_REMOVE ((CORE_ADDR)(*gregsetp)[PC_REGNUM]);
613 supply_register (PC_REGNUM, (char *) ®_pc);
616 /* Fill register regno (if it is a floating-point register) in
617 *fpregsetp with the appropriate value from GDB's register array.
618 If regno is -1, do this for all registers. */
621 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
623 FPA11 *fp = (FPA11 *) fpregsetp;
628 for (regnum = F0_REGNUM; regnum <= F7_REGNUM; regnum++)
629 store_nwfpe_register (regnum, fp);
631 else if (regno >= F0_REGNUM && regno <= F7_REGNUM)
633 store_nwfpe_register (regno, fp);
638 if (FPS_REGNUM == regno || -1 == regno)
639 read_register_gen (FPS_REGNUM, (char *) &fp->fpsr);
642 /* Fill GDB's register array with the floating-point register values
646 supply_fpregset (gdb_fpregset_t *fpregsetp)
649 FPA11 *fp = (FPA11 *) fpregsetp;
652 supply_register (FPS_REGNUM, (char *) &fp->fpsr);
654 /* Fetch the floating point registers. */
655 for (regno = F0_REGNUM; regno <= F7_REGNUM; regno++)
657 fetch_nwfpe_register (regno, fp);
662 arm_linux_kernel_u_size (void)
664 return (sizeof (struct user));
668 get_linux_version (unsigned int *vmajor,
669 unsigned int *vminor,
670 unsigned int *vrelease)
673 char *pmajor, *pminor, *prelease, *tail;
675 if (-1 == uname (&info))
677 warning ("Unable to determine Linux version.");
681 pmajor = strtok (info.release, ".");
682 pminor = strtok (NULL, ".");
683 prelease = strtok (NULL, ".");
685 *vmajor = (unsigned int) strtoul (pmajor, &tail, 0);
686 *vminor = (unsigned int) strtoul (pminor, &tail, 0);
687 *vrelease = (unsigned int) strtoul (prelease, &tail, 0);
689 return ((*vmajor << 16) | (*vminor << 8) | *vrelease);
693 _initialize_arm_linux_nat (void)
695 os_version = get_linux_version (&os_major, &os_minor, &os_release);