Update
[gdb.git] / gdb / m68klinux-nat.c
blob7467190492ae2709ad3beb79d022719b9ef89582
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/>. */
21 #include "defs.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "language.h"
25 #include "gdbcore.h"
26 #include "gdb_string.h"
27 #include "regcache.h"
28 #include "target.h"
29 #include "linux-nat.h"
31 #include "m68k-tdep.h"
33 #include <sys/param.h>
34 #include <sys/dir.h>
35 #include <signal.h>
36 #include <sys/ptrace.h>
37 #include <sys/user.h>
38 #include <sys/ioctl.h>
39 #include <fcntl.h>
40 #include <sys/procfs.h>
42 #ifdef HAVE_SYS_REG_H
43 #include <sys/reg.h>
44 #endif
46 #include <sys/file.h>
47 #include "gdb_stat.h"
49 #include "floatformat.h"
51 #include "target.h"
53 /* Prototypes for supply_gregset etc. */
54 #include "gregset.h"
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,
61 PT_SR, PT_PC,
62 /* PT_FP0, ..., PT_FP7 */
63 21, 24, 27, 30, 33, 36, 39, 42,
64 /* PT_FPCR, PT_FPSR, PT_FPIAR */
65 45, 46, 47
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)
73 int
74 getregs_supplies (int regno)
76 return 0 <= regno && regno < NUM_GREGS;
79 int
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
89 #else
91 #endif
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. */
105 #ifndef PT_READ_U
106 #define PT_READ_U PTRACE_PEEKUSR
107 #endif
108 #ifndef PT_WRITE_U
109 #define PT_WRITE_U PTRACE_POKEUSR
110 #endif
112 /* Fetch one register. */
114 static void
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. */
119 CORE_ADDR regaddr;
120 char mess[128]; /* For messages */
121 int i;
122 char buf[MAX_REGISTER_SIZE];
123 int tid;
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);
129 return;
132 /* Overload thread id onto process id */
133 tid = TIDGET (inferior_ptid);
134 if (tid == 0)
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))
141 errno = 0;
142 *(PTRACE_TYPE_RET *) &buf[i] = ptrace (PT_READ_U, tid,
143 (PTRACE_TYPE_ARG3) regaddr, 0);
144 regaddr += sizeof (PTRACE_TYPE_RET);
145 if (errno != 0)
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). */
159 static void
160 old_fetch_inferior_registers (struct regcache *regcache, int regno)
162 if (regno >= 0)
164 fetch_register (regcache, regno);
166 else
168 for (regno = 0;
169 regno < gdbarch_num_regs (get_regcache_arch (regcache));
170 regno++)
172 fetch_register (regcache, regno);
177 /* Store one register. */
179 static void
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. */
184 CORE_ADDR regaddr;
185 char mess[128]; /* For messages */
186 int i;
187 int tid;
188 char buf[MAX_REGISTER_SIZE];
190 if (gdbarch_cannot_store_register (gdbarch, regno))
191 return;
193 /* Overload thread id onto process id */
194 tid = TIDGET (inferior_ptid);
195 if (tid == 0)
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))
207 errno = 0;
208 ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) regaddr,
209 *(PTRACE_TYPE_RET *) (buf + i));
210 regaddr += sizeof (PTRACE_TYPE_RET);
211 if (errno != 0)
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). */
224 static void
225 old_store_inferior_registers (const struct regcache *regcache, int regno)
227 if (regno >= 0)
229 store_register (regcache, regno);
231 else
233 for (regno = 0;
234 regno < gdbarch_num_regs (get_regcache_arch (regcache));
235 regno++)
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. */
246 void
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;
251 int regi;
253 for (regi = M68K_D0_REGNUM;
254 regi <= gdbarch_sp_regnum (gdbarch);
255 regi++)
256 regcache_raw_supply (regcache, regi, &regp[regmap[regi]]);
257 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
258 &regp[PT_SR]);
259 regcache_raw_supply (regcache,
260 gdbarch_pc_regnum (gdbarch), &regp[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. */
266 void
267 fill_gregset (const struct regcache *regcache,
268 elf_gregset_t *gregsetp, int regno)
270 elf_greg_t *regp = (elf_greg_t *) gregsetp;
271 int i;
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. */
283 static void
284 fetch_regs (struct regcache *regcache, int tid)
286 elf_gregset_t regs;
288 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
290 if (errno == EIO)
292 /* The kernel we're running on doesn't support the GETREGS
293 request. Reset `have_ptrace_getregs'. */
294 have_ptrace_getregs = 0;
295 return;
298 perror_with_name (_("Couldn't get registers"));
301 supply_gregset (regcache, (const elf_gregset_t *) &regs);
304 /* Store all valid general-purpose registers in GDB's register array
305 into the process/thread specified by TID. */
307 static void
308 store_regs (const struct regcache *regcache, int tid, int regno)
310 elf_gregset_t regs;
312 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
313 perror_with_name (_("Couldn't get registers"));
315 fill_gregset (regcache, &regs, regno);
317 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
318 perror_with_name (_("Couldn't write registers"));
321 #else
323 static void fetch_regs (struct regcache *regcache, int tid) {}
324 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
326 #endif
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
335 *FPREGSETP. */
337 void
338 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
340 struct gdbarch *gdbarch = get_regcache_arch (regcache);
341 int regi;
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. */
357 void
358 fill_fpregset (const struct regcache *regcache,
359 elf_fpregset_t *fpregsetp, int regno)
361 struct gdbarch *gdbarch = get_regcache_arch (regcache);
362 int i;
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. */
384 static void
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. */
398 static void
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"));
412 #else
414 static void fetch_fpregs (struct regcache *regcache, int tid) {}
415 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
417 #endif
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
423 registers). */
425 static void
426 m68k_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
428 int tid;
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);
435 return;
438 /* GNU/Linux LWP ID's are process ID's. */
439 tid = TIDGET (inferior_ptid);
440 if (tid == 0)
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
446 zero. */
447 if (regno == -1)
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);
455 return;
458 fetch_fpregs (regcache, tid);
459 return;
462 if (getregs_supplies (regno))
464 fetch_regs (regcache, tid);
465 return;
468 if (getfpregs_supplies (regno))
470 fetch_fpregs (regcache, tid);
471 return;
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
480 registers). */
481 static void
482 m68k_linux_store_inferior_registers (struct regcache *regcache, int regno)
484 int tid;
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);
491 return;
494 /* GNU/Linux LWP ID's are process ID's. */
495 tid = TIDGET (inferior_ptid);
496 if (tid == 0)
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. */
502 if (regno == -1)
504 store_regs (regcache, tid, regno);
505 store_fpregs (regcache, tid, regno);
506 return;
509 if (getregs_supplies (regno))
511 store_regs (regcache, tid, regno);
512 return;
515 if (getfpregs_supplies (regno))
517 store_fpregs (regcache, tid, regno);
518 return;
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
531 gregset_t.)
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. */
543 static void
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;
551 switch (which)
553 case 0:
554 if (core_reg_size != sizeof (gregset))
555 warning (_("Wrong size gregset in core file."));
556 else
558 memcpy (&gregset, core_reg_sect, sizeof (gregset));
559 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
561 break;
563 case 2:
564 if (core_reg_size != sizeof (fpregset))
565 warning (_("Wrong size fpregset in core file."));
566 else
568 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
569 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
571 break;
573 default:
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. */
577 break;
582 /* Register that we are able to handle GNU/Linux ELF core file
583 formats. */
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 */
591 NULL /* next */
594 void _initialize_m68k_linux_nat (void);
596 void
597 _initialize_m68k_linux_nat (void)
599 struct target_ops *t;
601 /* Fill in the generic GNU/Linux methods. */
602 t = linux_target ();
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);