1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996-2017 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/>. */
27 #include "linux-nat.h"
29 #include "m68k-tdep.h"
33 #include "nat/gdb_ptrace.h"
35 #include <sys/ioctl.h>
37 #include <sys/procfs.h>
46 #include "floatformat.h"
48 /* Prototypes for supply_gregset etc. */
51 /* Defines ps_err_e, struct ps_prochandle. */
52 #include "gdb_proc_service.h"
54 #ifndef PTRACE_GET_THREAD_AREA
55 #define PTRACE_GET_THREAD_AREA 25
58 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
59 static const int regmap[] =
61 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
62 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
64 /* PT_FP0, ..., PT_FP7 */
65 21, 24, 27, 30, 33, 36, 39, 42,
66 /* PT_FPCR, PT_FPSR, PT_FPIAR */
70 /* Which ptrace request retrieves which registers?
71 These apply to the corresponding SET requests as well. */
72 #define NUM_GREGS (18)
73 #define MAX_NUM_REGS (NUM_GREGS + 11)
76 getregs_supplies (int regno)
78 return 0 <= regno && regno < NUM_GREGS;
82 getfpregs_supplies (int regno)
84 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
87 /* Does the current host support the GETREGS request? */
88 static int have_ptrace_getregs =
89 #ifdef HAVE_PTRACE_GETREGS
98 /* Fetching registers directly from the U area, one at a time. */
100 /* Fetch one register. */
103 fetch_register (struct regcache *regcache, int regno)
105 struct gdbarch *gdbarch = get_regcache_arch (regcache);
108 gdb_byte buf[MAX_REGISTER_SIZE];
111 /* Overload thread id onto process id. */
112 tid = ptid_get_lwp (inferior_ptid);
114 tid = ptid_get_pid (inferior_ptid); /* no thread id, just use
117 regaddr = 4 * regmap[regno];
118 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
121 val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
122 memcpy (&buf[i], &val, sizeof (long));
123 regaddr += sizeof (long);
125 error (_("Couldn't read register %s (#%d): %s."),
126 gdbarch_register_name (gdbarch, regno),
127 regno, safe_strerror (errno));
129 regcache_raw_supply (regcache, regno, buf);
132 /* Fetch register values from the inferior.
133 If REGNO is negative, do this for all registers.
134 Otherwise, REGNO specifies which register (so we can save time). */
137 old_fetch_inferior_registers (struct regcache *regcache, int regno)
141 fetch_register (regcache, regno);
146 regno < gdbarch_num_regs (get_regcache_arch (regcache));
149 fetch_register (regcache, regno);
154 /* Store one register. */
157 store_register (const struct regcache *regcache, int regno)
159 struct gdbarch *gdbarch = get_regcache_arch (regcache);
163 gdb_byte buf[MAX_REGISTER_SIZE];
165 /* Overload thread id onto process id. */
166 tid = ptid_get_lwp (inferior_ptid);
168 tid = ptid_get_pid (inferior_ptid); /* no thread id, just use
171 regaddr = 4 * regmap[regno];
173 /* Put the contents of regno into a local buffer. */
174 regcache_raw_collect (regcache, regno, buf);
176 /* Store the local buffer into the inferior a chunk at the time. */
177 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
180 memcpy (&val, &buf[i], sizeof (long));
181 ptrace (PTRACE_POKEUSER, tid, regaddr, val);
182 regaddr += sizeof (long);
184 error (_("Couldn't write register %s (#%d): %s."),
185 gdbarch_register_name (gdbarch, regno),
186 regno, safe_strerror (errno));
190 /* Store our register values back into the inferior.
191 If REGNO is negative, do this for all registers.
192 Otherwise, REGNO specifies which register (so we can save time). */
195 old_store_inferior_registers (const struct regcache *regcache, int regno)
199 store_register (regcache, regno);
204 regno < gdbarch_num_regs (get_regcache_arch (regcache));
207 store_register (regcache, regno);
212 /* Given a pointer to a general register set in /proc format
213 (elf_gregset_t *), unpack the register contents and supply
214 them as gdb's idea of the current register values. */
217 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
219 struct gdbarch *gdbarch = get_regcache_arch (regcache);
220 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
223 for (regi = M68K_D0_REGNUM;
224 regi <= gdbarch_sp_regnum (gdbarch);
226 regcache_raw_supply (regcache, regi, ®p[regmap[regi]]);
227 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
229 regcache_raw_supply (regcache,
230 gdbarch_pc_regnum (gdbarch), ®p[PT_PC]);
233 /* Fill register REGNO (if it is a general-purpose register) in
234 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
235 do this for all registers. */
237 fill_gregset (const struct regcache *regcache,
238 elf_gregset_t *gregsetp, int regno)
240 elf_greg_t *regp = (elf_greg_t *) gregsetp;
243 for (i = 0; i < NUM_GREGS; i++)
244 if (regno == -1 || regno == i)
245 regcache_raw_collect (regcache, i, regp + regmap[i]);
248 #ifdef HAVE_PTRACE_GETREGS
250 /* Fetch all general-purpose registers from process/thread TID and
251 store their values in GDB's register array. */
254 fetch_regs (struct regcache *regcache, int tid)
258 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
262 /* The kernel we're running on doesn't support the GETREGS
263 request. Reset `have_ptrace_getregs'. */
264 have_ptrace_getregs = 0;
268 perror_with_name (_("Couldn't get registers"));
271 supply_gregset (regcache, (const elf_gregset_t *) ®s);
274 /* Store all valid general-purpose registers in GDB's register array
275 into the process/thread specified by TID. */
278 store_regs (const struct regcache *regcache, int tid, int regno)
282 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
283 perror_with_name (_("Couldn't get registers"));
285 fill_gregset (regcache, ®s, regno);
287 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
288 perror_with_name (_("Couldn't write registers"));
293 static void fetch_regs (struct regcache *regcache, int tid)
297 static void store_regs (const struct regcache *regcache, int tid, int regno)
304 /* Transfering floating-point registers between GDB, inferiors and cores. */
306 /* What is the address of fpN within the floating-point register set F? */
307 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
309 /* Fill GDB's register array with the floating-point register values in
313 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
315 struct gdbarch *gdbarch = get_regcache_arch (regcache);
318 for (regi = gdbarch_fp0_regnum (gdbarch);
319 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
320 regcache_raw_supply (regcache, regi,
321 FPREG_ADDR (fpregsetp,
322 regi - gdbarch_fp0_regnum (gdbarch)));
323 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
324 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
325 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
328 /* Fill register REGNO (if it is a floating-point register) in
329 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
330 do this for all registers. */
333 fill_fpregset (const struct regcache *regcache,
334 elf_fpregset_t *fpregsetp, int regno)
336 struct gdbarch *gdbarch = get_regcache_arch (regcache);
339 /* Fill in the floating-point registers. */
340 for (i = gdbarch_fp0_regnum (gdbarch);
341 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
342 if (regno == -1 || regno == i)
343 regcache_raw_collect (regcache, i,
344 FPREG_ADDR (fpregsetp,
345 i - gdbarch_fp0_regnum (gdbarch)));
347 /* Fill in the floating-point control registers. */
348 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
349 if (regno == -1 || regno == i)
350 regcache_raw_collect (regcache, i,
351 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
354 #ifdef HAVE_PTRACE_GETREGS
356 /* Fetch all floating-point registers from process/thread TID and store
357 thier values in GDB's register array. */
360 fetch_fpregs (struct regcache *regcache, int tid)
362 elf_fpregset_t fpregs;
364 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
365 perror_with_name (_("Couldn't get floating point status"));
367 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
370 /* Store all valid floating-point registers in GDB's register array
371 into the process/thread specified by TID. */
374 store_fpregs (const struct regcache *regcache, int tid, int regno)
376 elf_fpregset_t fpregs;
378 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
379 perror_with_name (_("Couldn't get floating point status"));
381 fill_fpregset (regcache, &fpregs, regno);
383 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
384 perror_with_name (_("Couldn't write floating point status"));
389 static void fetch_fpregs (struct regcache *regcache, int tid)
393 static void store_fpregs (const struct regcache *regcache, int tid, int regno)
399 /* Transferring arbitrary registers between GDB and inferior. */
401 /* Fetch register REGNO from the child process. If REGNO is -1, do
402 this for all registers (including the floating point and SSE
406 m68k_linux_fetch_inferior_registers (struct target_ops *ops,
407 struct regcache *regcache, int regno)
411 /* Use the old method of peeking around in `struct user' if the
412 GETREGS request isn't available. */
413 if (! have_ptrace_getregs)
415 old_fetch_inferior_registers (regcache, regno);
419 /* GNU/Linux LWP ID's are process ID's. */
420 tid = ptid_get_lwp (inferior_ptid);
422 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
424 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
425 transfers more registers in one system call, and we'll cache the
426 results. But remember that fetch_fpxregs can fail, and return
430 fetch_regs (regcache, tid);
432 /* The call above might reset `have_ptrace_getregs'. */
433 if (! have_ptrace_getregs)
435 old_fetch_inferior_registers (regcache, -1);
439 fetch_fpregs (regcache, tid);
443 if (getregs_supplies (regno))
445 fetch_regs (regcache, tid);
449 if (getfpregs_supplies (regno))
451 fetch_fpregs (regcache, tid);
455 internal_error (__FILE__, __LINE__,
456 _("Got request for bad register number %d."), regno);
459 /* Store register REGNO back into the child process. If REGNO is -1,
460 do this for all registers (including the floating point and SSE
463 m68k_linux_store_inferior_registers (struct target_ops *ops,
464 struct regcache *regcache, int regno)
468 /* Use the old method of poking around in `struct user' if the
469 SETREGS request isn't available. */
470 if (! have_ptrace_getregs)
472 old_store_inferior_registers (regcache, regno);
476 /* GNU/Linux LWP ID's are process ID's. */
477 tid = ptid_get_lwp (inferior_ptid);
479 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
481 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
482 transfers more registers in one system call. But remember that
483 store_fpregs can fail, and return zero. */
486 store_regs (regcache, tid, regno);
487 store_fpregs (regcache, tid, regno);
491 if (getregs_supplies (regno))
493 store_regs (regcache, tid, regno);
497 if (getfpregs_supplies (regno))
499 store_fpregs (regcache, tid, regno);
503 internal_error (__FILE__, __LINE__,
504 _("Got request to store bad register number %d."), regno);
508 /* Fetch the thread-local storage pointer for libthread_db. */
511 ps_get_thread_area (struct ps_prochandle *ph,
512 lwpid_t lwpid, int idx, void **base)
514 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
517 /* IDX is the bias from the thread pointer to the beginning of the
518 thread descriptor. It has to be subtracted due to implementation
519 quirks in libthread_db. */
520 *base = (char *) *base - idx;
526 void _initialize_m68k_linux_nat (void);
529 _initialize_m68k_linux_nat (void)
531 struct target_ops *t;
533 /* Fill in the generic GNU/Linux methods. */
536 /* Add our register access methods. */
537 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
538 t->to_store_registers = m68k_linux_store_inferior_registers;
540 /* Register the target. */
541 linux_nat_add_target (t);