1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996, 1998, 2000-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdb_string.h"
28 #include "linux-nat.h"
30 #include "m68k-tdep.h"
32 #include <sys/param.h>
35 #include <sys/ptrace.h>
37 #include <sys/ioctl.h>
39 #include <sys/procfs.h>
48 #include "floatformat.h"
52 /* Prototypes for supply_gregset etc. */
55 /* Defines ps_err_e, struct ps_prochandle. */
56 #include "gdb_proc_service.h"
58 #ifndef PTRACE_GET_THREAD_AREA
59 #define PTRACE_GET_THREAD_AREA 25
62 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
63 static const int regmap
[] =
65 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
66 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
68 /* PT_FP0, ..., PT_FP7 */
69 21, 24, 27, 30, 33, 36, 39, 42,
70 /* PT_FPCR, PT_FPSR, PT_FPIAR */
74 /* Which ptrace request retrieves which registers?
75 These apply to the corresponding SET requests as well. */
76 #define NUM_GREGS (18)
77 #define MAX_NUM_REGS (NUM_GREGS + 11)
80 getregs_supplies (int regno
)
82 return 0 <= regno
&& regno
< NUM_GREGS
;
86 getfpregs_supplies (int regno
)
88 return M68K_FP0_REGNUM
<= regno
&& regno
<= M68K_FPI_REGNUM
;
91 /* Does the current host support the GETREGS request? */
92 static int have_ptrace_getregs
=
93 #ifdef HAVE_PTRACE_GETREGS
102 /* Fetching registers directly from the U area, one at a time. */
104 /* Fetch one register. */
107 fetch_register (struct regcache
*regcache
, int regno
)
109 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
112 char buf
[MAX_REGISTER_SIZE
];
115 /* Overload thread id onto process id. */
116 tid
= TIDGET (inferior_ptid
);
118 tid
= PIDGET (inferior_ptid
); /* no thread id, just use
121 regaddr
= 4 * regmap
[regno
];
122 for (i
= 0; i
< register_size (gdbarch
, regno
); i
+= sizeof (long))
125 *(long *) &buf
[i
] = ptrace (PTRACE_PEEKUSER
, tid
, regaddr
, 0);
126 regaddr
+= sizeof (long);
128 error (_("Couldn't read register %s (#%d): %s."),
129 gdbarch_register_name (gdbarch
, regno
),
130 regno
, safe_strerror (errno
));
132 regcache_raw_supply (regcache
, regno
, buf
);
135 /* Fetch register values from the inferior.
136 If REGNO is negative, do this for all registers.
137 Otherwise, REGNO specifies which register (so we can save time). */
140 old_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
144 fetch_register (regcache
, regno
);
149 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
152 fetch_register (regcache
, regno
);
157 /* Store one register. */
160 store_register (const struct regcache
*regcache
, int regno
)
162 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
166 char buf
[MAX_REGISTER_SIZE
];
168 /* Overload thread id onto process id. */
169 tid
= TIDGET (inferior_ptid
);
171 tid
= PIDGET (inferior_ptid
); /* no thread id, just use
174 regaddr
= 4 * regmap
[regno
];
176 /* Put the contents of regno into a local buffer. */
177 regcache_raw_collect (regcache
, regno
, buf
);
179 /* Store the local buffer into the inferior a chunk at the time. */
180 for (i
= 0; i
< register_size (gdbarch
, regno
); i
+= sizeof (long))
183 ptrace (PTRACE_POKEUSER
, tid
, regaddr
, *(long *) &buf
[i
]);
184 regaddr
+= sizeof (long);
186 error (_("Couldn't write register %s (#%d): %s."),
187 gdbarch_register_name (gdbarch
, regno
),
188 regno
, safe_strerror (errno
));
192 /* Store our register values back into the inferior.
193 If REGNO is negative, do this for all registers.
194 Otherwise, REGNO specifies which register (so we can save time). */
197 old_store_inferior_registers (const struct regcache
*regcache
, int regno
)
201 store_register (regcache
, regno
);
206 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
209 store_register (regcache
, regno
);
214 /* Given a pointer to a general register set in /proc format
215 (elf_gregset_t *), unpack the register contents and supply
216 them as gdb's idea of the current register values. */
219 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
221 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
222 const elf_greg_t
*regp
= (const elf_greg_t
*) gregsetp
;
225 for (regi
= M68K_D0_REGNUM
;
226 regi
<= gdbarch_sp_regnum (gdbarch
);
228 regcache_raw_supply (regcache
, regi
, ®p
[regmap
[regi
]]);
229 regcache_raw_supply (regcache
, gdbarch_ps_regnum (gdbarch
),
231 regcache_raw_supply (regcache
,
232 gdbarch_pc_regnum (gdbarch
), ®p
[PT_PC
]);
235 /* Fill register REGNO (if it is a general-purpose register) in
236 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
237 do this for all registers. */
239 fill_gregset (const struct regcache
*regcache
,
240 elf_gregset_t
*gregsetp
, int regno
)
242 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
245 for (i
= 0; i
< NUM_GREGS
; i
++)
246 if (regno
== -1 || regno
== i
)
247 regcache_raw_collect (regcache
, i
, regp
+ regmap
[i
]);
250 #ifdef HAVE_PTRACE_GETREGS
252 /* Fetch all general-purpose registers from process/thread TID and
253 store their values in GDB's register array. */
256 fetch_regs (struct regcache
*regcache
, int tid
)
260 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
264 /* The kernel we're running on doesn't support the GETREGS
265 request. Reset `have_ptrace_getregs'. */
266 have_ptrace_getregs
= 0;
270 perror_with_name (_("Couldn't get registers"));
273 supply_gregset (regcache
, (const elf_gregset_t
*) ®s
);
276 /* Store all valid general-purpose registers in GDB's register array
277 into the process/thread specified by TID. */
280 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
284 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
285 perror_with_name (_("Couldn't get registers"));
287 fill_gregset (regcache
, ®s
, regno
);
289 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
290 perror_with_name (_("Couldn't write registers"));
295 static void fetch_regs (struct regcache
*regcache
, int tid
)
299 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
)
306 /* Transfering floating-point registers between GDB, inferiors and cores. */
308 /* What is the address of fpN within the floating-point register set F? */
309 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
311 /* Fill GDB's register array with the floating-point register values in
315 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
317 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
320 for (regi
= gdbarch_fp0_regnum (gdbarch
);
321 regi
< gdbarch_fp0_regnum (gdbarch
) + 8; regi
++)
322 regcache_raw_supply (regcache
, regi
,
323 FPREG_ADDR (fpregsetp
,
324 regi
- gdbarch_fp0_regnum (gdbarch
)));
325 regcache_raw_supply (regcache
, M68K_FPC_REGNUM
, &fpregsetp
->fpcntl
[0]);
326 regcache_raw_supply (regcache
, M68K_FPS_REGNUM
, &fpregsetp
->fpcntl
[1]);
327 regcache_raw_supply (regcache
, M68K_FPI_REGNUM
, &fpregsetp
->fpcntl
[2]);
330 /* Fill register REGNO (if it is a floating-point register) in
331 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
332 do this for all registers. */
335 fill_fpregset (const struct regcache
*regcache
,
336 elf_fpregset_t
*fpregsetp
, int regno
)
338 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
341 /* Fill in the floating-point registers. */
342 for (i
= gdbarch_fp0_regnum (gdbarch
);
343 i
< gdbarch_fp0_regnum (gdbarch
) + 8; i
++)
344 if (regno
== -1 || regno
== i
)
345 regcache_raw_collect (regcache
, i
,
346 FPREG_ADDR (fpregsetp
,
347 i
- gdbarch_fp0_regnum (gdbarch
)));
349 /* Fill in the floating-point control registers. */
350 for (i
= M68K_FPC_REGNUM
; i
<= M68K_FPI_REGNUM
; i
++)
351 if (regno
== -1 || regno
== i
)
352 regcache_raw_collect (regcache
, i
,
353 &fpregsetp
->fpcntl
[i
- M68K_FPC_REGNUM
]);
356 #ifdef HAVE_PTRACE_GETREGS
358 /* Fetch all floating-point registers from process/thread TID and store
359 thier values in GDB's register array. */
362 fetch_fpregs (struct regcache
*regcache
, int tid
)
364 elf_fpregset_t fpregs
;
366 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
367 perror_with_name (_("Couldn't get floating point status"));
369 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
372 /* Store all valid floating-point registers in GDB's register array
373 into the process/thread specified by TID. */
376 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
378 elf_fpregset_t fpregs
;
380 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
381 perror_with_name (_("Couldn't get floating point status"));
383 fill_fpregset (regcache
, &fpregs
, regno
);
385 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
386 perror_with_name (_("Couldn't write floating point status"));
391 static void fetch_fpregs (struct regcache
*regcache
, int tid
)
395 static void store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
401 /* Transferring arbitrary registers between GDB and inferior. */
403 /* Fetch register REGNO from the child process. If REGNO is -1, do
404 this for all registers (including the floating point and SSE
408 m68k_linux_fetch_inferior_registers (struct target_ops
*ops
,
409 struct regcache
*regcache
, int regno
)
413 /* Use the old method of peeking around in `struct user' if the
414 GETREGS request isn't available. */
415 if (! have_ptrace_getregs
)
417 old_fetch_inferior_registers (regcache
, regno
);
421 /* GNU/Linux LWP ID's are process ID's. */
422 tid
= TIDGET (inferior_ptid
);
424 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
426 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
427 transfers more registers in one system call, and we'll cache the
428 results. But remember that fetch_fpxregs can fail, and return
432 fetch_regs (regcache
, tid
);
434 /* The call above might reset `have_ptrace_getregs'. */
435 if (! have_ptrace_getregs
)
437 old_fetch_inferior_registers (regcache
, -1);
441 fetch_fpregs (regcache
, tid
);
445 if (getregs_supplies (regno
))
447 fetch_regs (regcache
, tid
);
451 if (getfpregs_supplies (regno
))
453 fetch_fpregs (regcache
, tid
);
457 internal_error (__FILE__
, __LINE__
,
458 _("Got request for bad register number %d."), regno
);
461 /* Store register REGNO back into the child process. If REGNO is -1,
462 do this for all registers (including the floating point and SSE
465 m68k_linux_store_inferior_registers (struct target_ops
*ops
,
466 struct regcache
*regcache
, int regno
)
470 /* Use the old method of poking around in `struct user' if the
471 SETREGS request isn't available. */
472 if (! have_ptrace_getregs
)
474 old_store_inferior_registers (regcache
, regno
);
478 /* GNU/Linux LWP ID's are process ID's. */
479 tid
= TIDGET (inferior_ptid
);
481 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
483 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
484 transfers more registers in one system call. But remember that
485 store_fpregs can fail, and return zero. */
488 store_regs (regcache
, tid
, regno
);
489 store_fpregs (regcache
, tid
, regno
);
493 if (getregs_supplies (regno
))
495 store_regs (regcache
, tid
, regno
);
499 if (getfpregs_supplies (regno
))
501 store_fpregs (regcache
, tid
, regno
);
505 internal_error (__FILE__
, __LINE__
,
506 _("Got request to store bad register number %d."), regno
);
509 /* Interpreting register set info found in core files. */
511 /* Provide registers to GDB from a core file.
513 (We can't use the generic version of this function in
514 core-regset.c, because we need to use elf_gregset_t instead of
517 CORE_REG_SECT points to an array of bytes, which are the contents
518 of a `note' from a core file which BFD thinks might contain
519 register contents. CORE_REG_SIZE is its size.
521 WHICH says which register set corelow suspects this is:
522 0 --- the general-purpose register set, in elf_gregset_t format
523 2 --- the floating-point register set, in elf_fpregset_t format
525 REG_ADDR isn't used on GNU/Linux. */
528 fetch_core_registers (struct regcache
*regcache
,
529 char *core_reg_sect
, unsigned core_reg_size
,
530 int which
, CORE_ADDR reg_addr
)
532 elf_gregset_t gregset
;
533 elf_fpregset_t fpregset
;
538 if (core_reg_size
!= sizeof (gregset
))
539 warning (_("Wrong size gregset in core file."));
542 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
543 supply_gregset (regcache
, (const elf_gregset_t
*) &gregset
);
548 if (core_reg_size
!= sizeof (fpregset
))
549 warning (_("Wrong size fpregset in core file."));
552 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
553 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregset
);
558 /* We've covered all the kinds of registers we know about here,
559 so this must be something we wouldn't know what to do with
560 anyway. Just ignore it. */
566 /* Fetch the thread-local storage pointer for libthread_db. */
569 ps_get_thread_area (const struct ps_prochandle
*ph
,
570 lwpid_t lwpid
, int idx
, void **base
)
572 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) < 0)
575 /* IDX is the bias from the thread pointer to the beginning of the
576 thread descriptor. It has to be subtracted due to implementation
577 quirks in libthread_db. */
578 *base
= (char *) *base
- idx
;
584 /* Register that we are able to handle GNU/Linux ELF core file
587 static struct core_fns linux_elf_core_fns
=
589 bfd_target_elf_flavour
, /* core_flavour */
590 default_check_format
, /* check_format */
591 default_core_sniffer
, /* core_sniffer */
592 fetch_core_registers
, /* core_read_registers */
596 void _initialize_m68k_linux_nat (void);
599 _initialize_m68k_linux_nat (void)
601 struct target_ops
*t
;
603 /* Fill in the generic GNU/Linux methods. */
606 /* Add our register access methods. */
607 t
->to_fetch_registers
= m68k_linux_fetch_inferior_registers
;
608 t
->to_store_registers
= m68k_linux_store_inferior_registers
;
610 /* Register the target. */
611 linux_nat_add_target (t
);
613 deprecated_add_core_fns (&linux_elf_core_fns
);