Rename gdb/ChangeLog to gdb/ChangeLog-2021
[binutils-gdb.git] / gdb / m68k-linux-nat.c
blobb3c766216211f33143ef78bf3d7e0e5e7cab27f0
1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996-2021 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/>. */
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "language.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "target.h"
27 #include "linux-nat.h"
28 #include "gdbarch.h"
30 #include "m68k-tdep.h"
32 #include <sys/dir.h>
33 #include <signal.h>
34 #include "nat/gdb_ptrace.h"
35 #include <sys/user.h>
36 #include <sys/ioctl.h>
37 #include <fcntl.h>
38 #include <sys/procfs.h>
40 #ifdef HAVE_SYS_REG_H
41 #include <sys/reg.h>
42 #endif
44 #include <sys/file.h>
45 #include <sys/stat.h>
47 #include "floatformat.h"
49 /* Prototypes for supply_gregset etc. */
50 #include "gregset.h"
52 /* Defines ps_err_e, struct ps_prochandle. */
53 #include "gdb_proc_service.h"
55 #include "inf-ptrace.h"
57 #ifndef PTRACE_GET_THREAD_AREA
58 #define PTRACE_GET_THREAD_AREA 25
59 #endif
62 class m68k_linux_nat_target final : public linux_nat_target
64 public:
65 /* Add our register access methods. */
66 void fetch_registers (struct regcache *, int) override;
67 void store_registers (struct regcache *, int) override;
70 static m68k_linux_nat_target the_m68k_linux_nat_target;
72 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
73 static const int regmap[] =
75 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
76 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
77 PT_SR, PT_PC,
78 /* PT_FP0, ..., PT_FP7 */
79 21, 24, 27, 30, 33, 36, 39, 42,
80 /* PT_FPCR, PT_FPSR, PT_FPIAR */
81 45, 46, 47
84 /* Which ptrace request retrieves which registers?
85 These apply to the corresponding SET requests as well. */
86 #define NUM_GREGS (18)
87 #define MAX_NUM_REGS (NUM_GREGS + 11)
89 static int
90 getregs_supplies (int regno)
92 return 0 <= regno && regno < NUM_GREGS;
95 static int
96 getfpregs_supplies (int regno)
98 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
101 /* Does the current host support the GETREGS request? */
102 static int have_ptrace_getregs =
103 #ifdef HAVE_PTRACE_GETREGS
105 #else
107 #endif
112 /* Fetching registers directly from the U area, one at a time. */
114 /* Fetch one register. */
116 static void
117 fetch_register (struct regcache *regcache, int regno)
119 struct gdbarch *gdbarch = regcache->arch ();
120 long regaddr, val;
121 int i;
122 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
123 pid_t tid = get_ptrace_pid (regcache->ptid ());
125 regaddr = 4 * regmap[regno];
126 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
128 errno = 0;
129 val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
130 memcpy (&buf[i], &val, sizeof (long));
131 regaddr += sizeof (long);
132 if (errno != 0)
133 error (_("Couldn't read register %s (#%d): %s."),
134 gdbarch_register_name (gdbarch, regno),
135 regno, safe_strerror (errno));
137 regcache->raw_supply (regno, buf);
140 /* Fetch register values from the inferior.
141 If REGNO is negative, do this for all registers.
142 Otherwise, REGNO specifies which register (so we can save time). */
144 static void
145 old_fetch_inferior_registers (struct regcache *regcache, int regno)
147 if (regno >= 0)
149 fetch_register (regcache, regno);
151 else
153 for (regno = 0;
154 regno < gdbarch_num_regs (regcache->arch ());
155 regno++)
157 fetch_register (regcache, regno);
162 /* Store one register. */
164 static void
165 store_register (const struct regcache *regcache, int regno)
167 struct gdbarch *gdbarch = regcache->arch ();
168 long regaddr, val;
169 int i;
170 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
171 pid_t tid = get_ptrace_pid (regcache->ptid ());
173 regaddr = 4 * regmap[regno];
175 /* Put the contents of regno into a local buffer. */
176 regcache->raw_collect (regno, buf);
178 /* Store the local buffer into the inferior a chunk at the time. */
179 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
181 errno = 0;
182 memcpy (&val, &buf[i], sizeof (long));
183 ptrace (PTRACE_POKEUSER, tid, regaddr, val);
184 regaddr += sizeof (long);
185 if (errno != 0)
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). */
196 static void
197 old_store_inferior_registers (const struct regcache *regcache, int regno)
199 if (regno >= 0)
201 store_register (regcache, regno);
203 else
205 for (regno = 0;
206 regno < gdbarch_num_regs (regcache->arch ());
207 regno++)
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. */
218 void
219 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
221 struct gdbarch *gdbarch = regcache->arch ();
222 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
223 int regi;
225 for (regi = M68K_D0_REGNUM;
226 regi <= gdbarch_sp_regnum (gdbarch);
227 regi++)
228 regcache->raw_supply (regi, &regp[regmap[regi]]);
229 regcache->raw_supply (gdbarch_ps_regnum (gdbarch), &regp[PT_SR]);
230 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), &regp[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. */
236 void
237 fill_gregset (const struct regcache *regcache,
238 elf_gregset_t *gregsetp, int regno)
240 elf_greg_t *regp = (elf_greg_t *) gregsetp;
241 int i;
243 for (i = 0; i < NUM_GREGS; i++)
244 if (regno == -1 || regno == i)
245 regcache->raw_collect (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. */
253 static void
254 fetch_regs (struct regcache *regcache, int tid)
256 elf_gregset_t regs;
258 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
260 if (errno == EIO)
262 /* The kernel we're running on doesn't support the GETREGS
263 request. Reset `have_ptrace_getregs'. */
264 have_ptrace_getregs = 0;
265 return;
268 perror_with_name (_("Couldn't get registers"));
271 supply_gregset (regcache, (const elf_gregset_t *) &regs);
274 /* Store all valid general-purpose registers in GDB's register array
275 into the process/thread specified by TID. */
277 static void
278 store_regs (const struct regcache *regcache, int tid, int regno)
280 elf_gregset_t regs;
282 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
283 perror_with_name (_("Couldn't get registers"));
285 fill_gregset (regcache, &regs, regno);
287 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
288 perror_with_name (_("Couldn't write registers"));
291 #else
293 static void fetch_regs (struct regcache *regcache, int tid)
297 static void store_regs (const struct regcache *regcache, int tid, int regno)
301 #endif
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
310 *FPREGSETP. */
312 void
313 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
315 struct gdbarch *gdbarch = regcache->arch ();
316 int regi;
318 for (regi = gdbarch_fp0_regnum (gdbarch);
319 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
320 regcache->raw_supply
321 (regi, FPREG_ADDR (fpregsetp, regi - gdbarch_fp0_regnum (gdbarch)));
322 regcache->raw_supply (M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
323 regcache->raw_supply (M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
324 regcache->raw_supply (M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
327 /* Fill register REGNO (if it is a floating-point register) in
328 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
329 do this for all registers. */
331 void
332 fill_fpregset (const struct regcache *regcache,
333 elf_fpregset_t *fpregsetp, int regno)
335 struct gdbarch *gdbarch = regcache->arch ();
336 int i;
338 /* Fill in the floating-point registers. */
339 for (i = gdbarch_fp0_regnum (gdbarch);
340 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
341 if (regno == -1 || regno == i)
342 regcache->raw_collect
343 (i, FPREG_ADDR (fpregsetp, i - gdbarch_fp0_regnum (gdbarch)));
345 /* Fill in the floating-point control registers. */
346 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
347 if (regno == -1 || regno == i)
348 regcache->raw_collect (i, &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
351 #ifdef HAVE_PTRACE_GETREGS
353 /* Fetch all floating-point registers from process/thread TID and store
354 thier values in GDB's register array. */
356 static void
357 fetch_fpregs (struct regcache *regcache, int tid)
359 elf_fpregset_t fpregs;
361 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
362 perror_with_name (_("Couldn't get floating point status"));
364 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
367 /* Store all valid floating-point registers in GDB's register array
368 into the process/thread specified by TID. */
370 static void
371 store_fpregs (const struct regcache *regcache, int tid, int regno)
373 elf_fpregset_t fpregs;
375 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
376 perror_with_name (_("Couldn't get floating point status"));
378 fill_fpregset (regcache, &fpregs, regno);
380 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
381 perror_with_name (_("Couldn't write floating point status"));
384 #else
386 static void fetch_fpregs (struct regcache *regcache, int tid)
390 static void store_fpregs (const struct regcache *regcache, int tid, int regno)
394 #endif
396 /* Transferring arbitrary registers between GDB and inferior. */
398 /* Fetch register REGNO from the child process. If REGNO is -1, do
399 this for all registers (including the floating point and SSE
400 registers). */
402 void
403 m68k_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
405 pid_t tid;
407 /* Use the old method of peeking around in `struct user' if the
408 GETREGS request isn't available. */
409 if (! have_ptrace_getregs)
411 old_fetch_inferior_registers (regcache, regno);
412 return;
415 tid = get_ptrace_pid (regcache->ptid ());
417 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
418 transfers more registers in one system call, and we'll cache the
419 results. But remember that fetch_fpxregs can fail, and return
420 zero. */
421 if (regno == -1)
423 fetch_regs (regcache, tid);
425 /* The call above might reset `have_ptrace_getregs'. */
426 if (! have_ptrace_getregs)
428 old_fetch_inferior_registers (regcache, -1);
429 return;
432 fetch_fpregs (regcache, tid);
433 return;
436 if (getregs_supplies (regno))
438 fetch_regs (regcache, tid);
439 return;
442 if (getfpregs_supplies (regno))
444 fetch_fpregs (regcache, tid);
445 return;
448 internal_error (__FILE__, __LINE__,
449 _("Got request for bad register number %d."), regno);
452 /* Store register REGNO back into the child process. If REGNO is -1,
453 do this for all registers (including the floating point and SSE
454 registers). */
455 void
456 m68k_linux_nat_target::store_registers (struct regcache *regcache, int regno)
458 pid_t tid;
460 /* Use the old method of poking around in `struct user' if the
461 SETREGS request isn't available. */
462 if (! have_ptrace_getregs)
464 old_store_inferior_registers (regcache, regno);
465 return;
468 tid = get_ptrace_pid (regcache->ptid ());
470 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
471 transfers more registers in one system call. But remember that
472 store_fpregs can fail, and return zero. */
473 if (regno == -1)
475 store_regs (regcache, tid, regno);
476 store_fpregs (regcache, tid, regno);
477 return;
480 if (getregs_supplies (regno))
482 store_regs (regcache, tid, regno);
483 return;
486 if (getfpregs_supplies (regno))
488 store_fpregs (regcache, tid, regno);
489 return;
492 internal_error (__FILE__, __LINE__,
493 _("Got request to store bad register number %d."), regno);
497 /* Fetch the thread-local storage pointer for libthread_db. */
499 ps_err_e
500 ps_get_thread_area (struct ps_prochandle *ph,
501 lwpid_t lwpid, int idx, void **base)
503 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
504 return PS_ERR;
506 /* IDX is the bias from the thread pointer to the beginning of the
507 thread descriptor. It has to be subtracted due to implementation
508 quirks in libthread_db. */
509 *base = (char *) *base - idx;
511 return PS_OK;
514 void _initialize_m68k_linux_nat ();
515 void
516 _initialize_m68k_linux_nat ()
518 /* Register the target. */
519 linux_target = &the_m68k_linux_nat_target;
520 add_inf_child_target (&the_m68k_linux_nat_target);