1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 2008 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "gdb_string.h"
29 #include "linux-nat.h"
31 #include "m68k-tdep.h"
33 #include <sys/param.h>
36 #include <sys/ptrace.h>
38 #include <sys/ioctl.h>
40 #include <sys/procfs.h>
49 #include "floatformat.h"
53 /* Prototypes for supply_gregset etc. */
56 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
57 static const int regmap
[] =
59 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
60 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
62 /* PT_FP0, ..., PT_FP7 */
63 21, 24, 27, 30, 33, 36, 39, 42,
64 /* PT_FPCR, PT_FPSR, PT_FPIAR */
68 /* Which ptrace request retrieves which registers?
69 These apply to the corresponding SET requests as well. */
70 #define NUM_GREGS (18)
71 #define MAX_NUM_REGS (NUM_GREGS + 11)
74 getregs_supplies (int regno
)
76 return 0 <= regno
&& regno
< NUM_GREGS
;
80 getfpregs_supplies (int regno
)
82 return M68K_FP0_REGNUM
<= regno
&& regno
<= M68K_FPI_REGNUM
;
85 /* Does the current host support the GETREGS request? */
86 int have_ptrace_getregs
=
87 #ifdef HAVE_PTRACE_GETREGS
96 /* Fetching registers directly from the U area, one at a time. */
98 /* FIXME: This duplicates code from `inptrace.c'. The problem is that we
99 define FETCH_INFERIOR_REGISTERS since we want to use our own versions
100 of {fetch,store}_inferior_registers that use the GETREGS request. This
101 means that the code in `infptrace.c' is #ifdef'd out. But we need to
102 fall back on that code when GDB is running on top of a kernel that
103 doesn't support the GETREGS request. */
106 #define PT_READ_U PTRACE_PEEKUSR
109 #define PT_WRITE_U PTRACE_POKEUSR
112 /* Fetch one register. */
115 fetch_register (struct regcache
*regcache
, int regno
)
117 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
118 /* This isn't really an address. But ptrace thinks of it as one. */
120 char mess
[128]; /* For messages */
122 char buf
[MAX_REGISTER_SIZE
];
125 if (gdbarch_cannot_fetch_register (gdbarch
, regno
))
127 memset (buf
, '\0', register_size (gdbarch
, regno
)); /* Supply zeroes */
128 regcache_raw_supply (regcache
, regno
, buf
);
132 /* Overload thread id onto process id */
133 tid
= TIDGET (inferior_ptid
);
135 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
137 regaddr
= 4 * regmap
[regno
];
138 for (i
= 0; i
< register_size (gdbarch
, regno
);
139 i
+= sizeof (PTRACE_TYPE_RET
))
142 *(PTRACE_TYPE_RET
*) &buf
[i
] = ptrace (PT_READ_U
, tid
,
143 (PTRACE_TYPE_ARG3
) regaddr
, 0);
144 regaddr
+= sizeof (PTRACE_TYPE_RET
);
147 sprintf (mess
, "reading register %s (#%d)",
148 gdbarch_register_name (gdbarch
, regno
), regno
);
149 perror_with_name (mess
);
152 regcache_raw_supply (regcache
, regno
, buf
);
155 /* Fetch register values from the inferior.
156 If REGNO is negative, do this for all registers.
157 Otherwise, REGNO specifies which register (so we can save time). */
160 old_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
164 fetch_register (regcache
, regno
);
169 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
172 fetch_register (regcache
, regno
);
177 /* Store one register. */
180 store_register (const struct regcache
*regcache
, int regno
)
182 struct gdbarch
*gdbarch
= reg_regcache_arch (regcache
);
183 /* This isn't really an address. But ptrace thinks of it as one. */
185 char mess
[128]; /* For messages */
188 char buf
[MAX_REGISTER_SIZE
];
190 if (gdbarch_cannot_store_register (gdbarch
, regno
))
193 /* Overload thread id onto process id */
194 tid
= TIDGET (inferior_ptid
);
196 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
198 regaddr
= 4 * regmap
[regno
];
200 /* Put the contents of regno into a local buffer */
201 regcache_raw_collect (regcache
, regno
, buf
);
203 /* Store the local buffer into the inferior a chunk at the time. */
204 for (i
= 0; i
< register_size (gdbarch
, regno
);
205 i
+= sizeof (PTRACE_TYPE_RET
))
208 ptrace (PT_WRITE_U
, tid
, (PTRACE_TYPE_ARG3
) regaddr
,
209 *(PTRACE_TYPE_RET
*) (buf
+ i
));
210 regaddr
+= sizeof (PTRACE_TYPE_RET
);
213 sprintf (mess
, "writing register %s (#%d)",
214 gdbarch_register_name (gdbarch
, regno
), regno
);
215 perror_with_name (mess
);
220 /* Store our register values back into the inferior.
221 If REGNO is negative, do this for all registers.
222 Otherwise, REGNO specifies which register (so we can save time). */
225 old_store_inferior_registers (const struct regcache
*regcache
, int regno
)
229 store_register (regcache
, regno
);
234 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
237 store_register (regcache
, regno
);
242 /* Given a pointer to a general register set in /proc format
243 (elf_gregset_t *), unpack the register contents and supply
244 them as gdb's idea of the current register values. */
247 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
249 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
250 const elf_greg_t
*regp
= (const elf_greg_t
*) gregsetp
;
253 for (regi
= M68K_D0_REGNUM
;
254 regi
<= gdbarch_sp_regnum (gdbarch
);
256 regcache_raw_supply (regcache
, regi
, ®p
[regmap
[regi
]]);
257 regcache_raw_supply (regcache
, gdbarch_ps_regnum (gdbarch
),
259 regcache_raw_supply (regcache
,
260 gdbarch_pc_regnum (gdbarch
), ®p
[PT_PC
]);
263 /* Fill register REGNO (if it is a general-purpose register) in
264 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
265 do this for all registers. */
267 fill_gregset (const struct regcache
*regcache
,
268 elf_gregset_t
*gregsetp
, int regno
)
270 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
273 for (i
= 0; i
< NUM_GREGS
; i
++)
274 if (regno
== -1 || regno
== i
)
275 regcache_raw_collect (regcache
, i
, regp
+ regmap
[i
]);
278 #ifdef HAVE_PTRACE_GETREGS
280 /* Fetch all general-purpose registers from process/thread TID and
281 store their values in GDB's register array. */
284 fetch_regs (struct regcache
*regcache
, int tid
)
288 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
292 /* The kernel we're running on doesn't support the GETREGS
293 request. Reset `have_ptrace_getregs'. */
294 have_ptrace_getregs
= 0;
298 perror_with_name (_("Couldn't get registers"));
301 supply_gregset (regcache
, (const elf_gregset_t
*) ®s
);
304 /* Store all valid general-purpose registers in GDB's register array
305 into the process/thread specified by TID. */
308 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
312 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
313 perror_with_name (_("Couldn't get registers"));
315 fill_gregset (regcache
, ®s
, regno
);
317 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
318 perror_with_name (_("Couldn't write registers"));
323 static void fetch_regs (struct regcache
*regcache
, int tid
) {}
324 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
) {}
329 /* Transfering floating-point registers between GDB, inferiors and cores. */
331 /* What is the address of fpN within the floating-point register set F? */
332 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
334 /* Fill GDB's register array with the floating-point register values in
338 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
340 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
343 for (regi
= gdbarch_fp0_regnum (gdbarch
);
344 regi
< gdbarch_fp0_regnum (gdbarch
) + 8; regi
++)
345 regcache_raw_supply (regcache
, regi
,
346 FPREG_ADDR (fpregsetp
,
347 regi
- gdbarch_fp0_regnum (gdbarch
)));
348 regcache_raw_supply (regcache
, M68K_FPC_REGNUM
, &fpregsetp
->fpcntl
[0]);
349 regcache_raw_supply (regcache
, M68K_FPS_REGNUM
, &fpregsetp
->fpcntl
[1]);
350 regcache_raw_supply (regcache
, M68K_FPI_REGNUM
, &fpregsetp
->fpcntl
[2]);
353 /* Fill register REGNO (if it is a floating-point register) in
354 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
355 do this for all registers. */
358 fill_fpregset (const struct regcache
*regcache
,
359 elf_fpregset_t
*fpregsetp
, int regno
)
361 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
364 /* Fill in the floating-point registers. */
365 for (i
= gdbarch_fp0_regnum (gdbarch
);
366 i
< gdbarch_fp0_regnum (gdbarch
) + 8; i
++)
367 if (regno
== -1 || regno
== i
)
368 regcache_raw_collect (regcache
, i
,
369 FPREG_ADDR (fpregsetp
,
370 i
- gdbarch_fp0_regnum (gdbarch
)));
372 /* Fill in the floating-point control registers. */
373 for (i
= M68K_FPC_REGNUM
; i
<= M68K_FPI_REGNUM
; i
++)
374 if (regno
== -1 || regno
== i
)
375 regcache_raw_collect (regcache
, i
,
376 &fpregsetp
->fpcntl
[i
- M68K_FPC_REGNUM
]);
379 #ifdef HAVE_PTRACE_GETREGS
381 /* Fetch all floating-point registers from process/thread TID and store
382 thier values in GDB's register array. */
385 fetch_fpregs (struct regcache
*regcache
, int tid
)
387 elf_fpregset_t fpregs
;
389 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
390 perror_with_name (_("Couldn't get floating point status"));
392 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
395 /* Store all valid floating-point registers in GDB's register array
396 into the process/thread specified by TID. */
399 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
401 elf_fpregset_t fpregs
;
403 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
404 perror_with_name (_("Couldn't get floating point status"));
406 fill_fpregset (regcache
, &fpregs
, regno
);
408 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
409 perror_with_name (_("Couldn't write floating point status"));
414 static void fetch_fpregs (struct regcache
*regcache
, int tid
) {}
415 static void store_fpregs (const struct regcache
*regcache
, int tid
, int regno
) {}
419 /* Transferring arbitrary registers between GDB and inferior. */
421 /* Fetch register REGNO from the child process. If REGNO is -1, do
422 this for all registers (including the floating point and SSE
426 m68k_linux_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
430 /* Use the old method of peeking around in `struct user' if the
431 GETREGS request isn't available. */
432 if (! have_ptrace_getregs
)
434 old_fetch_inferior_registers (regcache
, regno
);
438 /* GNU/Linux LWP ID's are process ID's. */
439 tid
= TIDGET (inferior_ptid
);
441 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
443 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
444 transfers more registers in one system call, and we'll cache the
445 results. But remember that fetch_fpxregs can fail, and return
449 fetch_regs (regcache
, tid
);
451 /* The call above might reset `have_ptrace_getregs'. */
452 if (! have_ptrace_getregs
)
454 old_fetch_inferior_registers (regcache
, -1);
458 fetch_fpregs (regcache
, tid
);
462 if (getregs_supplies (regno
))
464 fetch_regs (regcache
, tid
);
468 if (getfpregs_supplies (regno
))
470 fetch_fpregs (regcache
, tid
);
474 internal_error (__FILE__
, __LINE__
,
475 _("Got request for bad register number %d."), regno
);
478 /* Store register REGNO back into the child process. If REGNO is -1,
479 do this for all registers (including the floating point and SSE
482 m68k_linux_store_inferior_registers (struct regcache
*regcache
, int regno
)
486 /* Use the old method of poking around in `struct user' if the
487 SETREGS request isn't available. */
488 if (! have_ptrace_getregs
)
490 old_store_inferior_registers (regcache
, regno
);
494 /* GNU/Linux LWP ID's are process ID's. */
495 tid
= TIDGET (inferior_ptid
);
497 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
499 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
500 transfers more registers in one system call. But remember that
501 store_fpregs can fail, and return zero. */
504 store_regs (regcache
, tid
, regno
);
505 store_fpregs (regcache
, tid
, regno
);
509 if (getregs_supplies (regno
))
511 store_regs (regcache
, tid
, regno
);
515 if (getfpregs_supplies (regno
))
517 store_fpregs (regcache
, tid
, regno
);
521 internal_error (__FILE__
, __LINE__
,
522 _("Got request to store bad register number %d."), regno
);
525 /* Interpreting register set info found in core files. */
527 /* Provide registers to GDB from a core file.
529 (We can't use the generic version of this function in
530 core-regset.c, because we need to use elf_gregset_t instead of
533 CORE_REG_SECT points to an array of bytes, which are the contents
534 of a `note' from a core file which BFD thinks might contain
535 register contents. CORE_REG_SIZE is its size.
537 WHICH says which register set corelow suspects this is:
538 0 --- the general-purpose register set, in elf_gregset_t format
539 2 --- the floating-point register set, in elf_fpregset_t format
541 REG_ADDR isn't used on GNU/Linux. */
544 fetch_core_registers (struct regcache
*regcache
,
545 char *core_reg_sect
, unsigned core_reg_size
,
546 int which
, CORE_ADDR reg_addr
)
548 elf_gregset_t gregset
;
549 elf_fpregset_t fpregset
;
554 if (core_reg_size
!= sizeof (gregset
))
555 warning (_("Wrong size gregset in core file."));
558 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
559 supply_gregset (regcache
, (const elf_gregset_t
*) &gregset
);
564 if (core_reg_size
!= sizeof (fpregset
))
565 warning (_("Wrong size fpregset in core file."));
568 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
569 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregset
);
574 /* We've covered all the kinds of registers we know about here,
575 so this must be something we wouldn't know what to do with
576 anyway. Just ignore it. */
582 /* Register that we are able to handle GNU/Linux ELF core file
585 static struct core_fns linux_elf_core_fns
=
587 bfd_target_elf_flavour
, /* core_flavour */
588 default_check_format
, /* check_format */
589 default_core_sniffer
, /* core_sniffer */
590 fetch_core_registers
, /* core_read_registers */
594 void _initialize_m68k_linux_nat (void);
597 _initialize_m68k_linux_nat (void)
599 struct target_ops
*t
;
601 /* Fill in the generic GNU/Linux methods. */
604 /* Add our register access methods. */
605 t
->to_fetch_registers
= m68k_linux_fetch_inferior_registers
;
606 t
->to_store_registers
= m68k_linux_store_inferior_registers
;
608 /* Register the target. */
609 linux_nat_add_target (t
);
611 deprecated_add_core_fns (&linux_elf_core_fns
);