Update description of macro keyword argument assignment in assembler documentation.
[binutils-gdb.git] / gdb / m68k-linux-nat.c
blob7f3373986769e78e8ce2d974a7c20b1e53e840bc
1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996-2024 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 "frame.h"
21 #include "inferior.h"
22 #include "language.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "target.h"
26 #include "linux-nat.h"
27 #include "gdbarch.h"
29 #include "m68k-tdep.h"
31 #include <sys/dir.h>
32 #include <signal.h>
33 #include "nat/gdb_ptrace.h"
34 #include <sys/user.h>
35 #include <sys/ioctl.h>
36 #include <fcntl.h>
37 #include <sys/procfs.h>
39 #ifdef HAVE_SYS_REG_H
40 #include <sys/reg.h>
41 #endif
43 #include <sys/file.h>
44 #include <sys/stat.h>
46 #include "floatformat.h"
48 /* Prototypes for supply_gregset etc. */
49 #include "gregset.h"
51 /* Defines ps_err_e, struct ps_prochandle. */
52 #include "gdb_proc_service.h"
54 #include "inf-ptrace.h"
56 #ifndef PTRACE_GET_THREAD_AREA
57 #define PTRACE_GET_THREAD_AREA 25
58 #endif
61 class m68k_linux_nat_target final : public linux_nat_target
63 public:
64 /* Add our register access methods. */
65 void fetch_registers (struct regcache *, int) override;
66 void store_registers (struct regcache *, int) override;
69 static m68k_linux_nat_target the_m68k_linux_nat_target;
71 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
72 static const int regmap[] =
74 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
75 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
76 PT_SR, PT_PC,
77 /* PT_FP0, ..., PT_FP7 */
78 21, 24, 27, 30, 33, 36, 39, 42,
79 /* PT_FPCR, PT_FPSR, PT_FPIAR */
80 45, 46, 47
83 /* Which ptrace request retrieves which registers?
84 These apply to the corresponding SET requests as well. */
85 #define NUM_GREGS (18)
86 #define MAX_NUM_REGS (NUM_GREGS + 11)
88 static int
89 getregs_supplies (int regno)
91 return 0 <= regno && regno < NUM_GREGS;
94 static int
95 getfpregs_supplies (int regno)
97 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
100 /* Does the current host support the GETREGS request? */
101 static int have_ptrace_getregs =
102 #ifdef HAVE_PTRACE_GETREGS
104 #else
106 #endif
111 /* Fetching registers directly from the U area, one at a time. */
113 /* Fetch one register. */
115 static void
116 fetch_register (struct regcache *regcache, int regno)
118 struct gdbarch *gdbarch = regcache->arch ();
119 long regaddr, val;
120 int i;
121 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
122 pid_t tid = get_ptrace_pid (regcache->ptid ());
124 regaddr = 4 * regmap[regno];
125 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
127 errno = 0;
128 val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
129 memcpy (&buf[i], &val, sizeof (long));
130 regaddr += sizeof (long);
131 if (errno != 0)
132 error (_("Couldn't read register %s (#%d): %s."),
133 gdbarch_register_name (gdbarch, regno),
134 regno, safe_strerror (errno));
136 regcache->raw_supply (regno, buf);
139 /* Fetch register values from the inferior.
140 If REGNO is negative, do this for all registers.
141 Otherwise, REGNO specifies which register (so we can save time). */
143 static void
144 old_fetch_inferior_registers (struct regcache *regcache, int regno)
146 if (regno >= 0)
148 fetch_register (regcache, regno);
150 else
152 for (regno = 0;
153 regno < gdbarch_num_regs (regcache->arch ());
154 regno++)
156 fetch_register (regcache, regno);
161 /* Store one register. */
163 static void
164 store_register (const struct regcache *regcache, int regno)
166 struct gdbarch *gdbarch = regcache->arch ();
167 long regaddr, val;
168 int i;
169 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
170 pid_t tid = get_ptrace_pid (regcache->ptid ());
172 regaddr = 4 * regmap[regno];
174 /* Put the contents of regno into a local buffer. */
175 regcache->raw_collect (regno, buf);
177 /* Store the local buffer into the inferior a chunk at the time. */
178 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
180 errno = 0;
181 memcpy (&val, &buf[i], sizeof (long));
182 ptrace (PTRACE_POKEUSER, tid, regaddr, val);
183 regaddr += sizeof (long);
184 if (errno != 0)
185 error (_("Couldn't write register %s (#%d): %s."),
186 gdbarch_register_name (gdbarch, regno),
187 regno, safe_strerror (errno));
191 /* Store our register values back into the inferior.
192 If REGNO is negative, do this for all registers.
193 Otherwise, REGNO specifies which register (so we can save time). */
195 static void
196 old_store_inferior_registers (const struct regcache *regcache, int regno)
198 if (regno >= 0)
200 store_register (regcache, regno);
202 else
204 for (regno = 0;
205 regno < gdbarch_num_regs (regcache->arch ());
206 regno++)
208 store_register (regcache, regno);
213 /* Given a pointer to a general register set in /proc format
214 (elf_gregset_t *), unpack the register contents and supply
215 them as gdb's idea of the current register values. */
217 void
218 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
220 struct gdbarch *gdbarch = regcache->arch ();
221 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
222 int regi;
224 for (regi = M68K_D0_REGNUM;
225 regi <= gdbarch_sp_regnum (gdbarch);
226 regi++)
227 regcache->raw_supply (regi, &regp[regmap[regi]]);
228 regcache->raw_supply (gdbarch_ps_regnum (gdbarch), &regp[PT_SR]);
229 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), &regp[PT_PC]);
232 /* Fill register REGNO (if it is a general-purpose register) in
233 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
234 do this for all registers. */
235 void
236 fill_gregset (const struct regcache *regcache,
237 elf_gregset_t *gregsetp, int regno)
239 elf_greg_t *regp = (elf_greg_t *) gregsetp;
240 int i;
242 for (i = 0; i < NUM_GREGS; i++)
243 if (regno == -1 || regno == i)
244 regcache->raw_collect (i, regp + regmap[i]);
247 #ifdef HAVE_PTRACE_GETREGS
249 /* Fetch all general-purpose registers from process/thread TID and
250 store their values in GDB's register array. */
252 static void
253 fetch_regs (struct regcache *regcache, int tid)
255 elf_gregset_t regs;
257 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
259 if (errno == EIO)
261 /* The kernel we're running on doesn't support the GETREGS
262 request. Reset `have_ptrace_getregs'. */
263 have_ptrace_getregs = 0;
264 return;
267 perror_with_name (_("Couldn't get registers"));
270 supply_gregset (regcache, (const elf_gregset_t *) &regs);
273 /* Store all valid general-purpose registers in GDB's register array
274 into the process/thread specified by TID. */
276 static void
277 store_regs (const struct regcache *regcache, int tid, int regno)
279 elf_gregset_t regs;
281 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
282 perror_with_name (_("Couldn't get registers"));
284 fill_gregset (regcache, &regs, regno);
286 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
287 perror_with_name (_("Couldn't write registers"));
290 #else
292 static void fetch_regs (struct regcache *regcache, int tid)
296 static void store_regs (const struct regcache *regcache, int tid, int regno)
300 #endif
303 /* Transfering floating-point registers between GDB, inferiors and cores. */
305 /* What is the address of fpN within the floating-point register set F? */
306 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
308 /* Fill GDB's register array with the floating-point register values in
309 *FPREGSETP. */
311 void
312 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
314 struct gdbarch *gdbarch = regcache->arch ();
315 int regi;
317 for (regi = gdbarch_fp0_regnum (gdbarch);
318 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
319 regcache->raw_supply
320 (regi, FPREG_ADDR (fpregsetp, regi - gdbarch_fp0_regnum (gdbarch)));
321 regcache->raw_supply (M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
322 regcache->raw_supply (M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
323 regcache->raw_supply (M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
326 /* Fill register REGNO (if it is a floating-point register) in
327 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
328 do this for all registers. */
330 void
331 fill_fpregset (const struct regcache *regcache,
332 elf_fpregset_t *fpregsetp, int regno)
334 struct gdbarch *gdbarch = regcache->arch ();
335 int i;
337 /* Fill in the floating-point registers. */
338 for (i = gdbarch_fp0_regnum (gdbarch);
339 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
340 if (regno == -1 || regno == i)
341 regcache->raw_collect
342 (i, FPREG_ADDR (fpregsetp, i - gdbarch_fp0_regnum (gdbarch)));
344 /* Fill in the floating-point control registers. */
345 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
346 if (regno == -1 || regno == i)
347 regcache->raw_collect (i, &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
350 #ifdef HAVE_PTRACE_GETREGS
352 /* Fetch all floating-point registers from process/thread TID and store
353 thier values in GDB's register array. */
355 static void
356 fetch_fpregs (struct regcache *regcache, int tid)
358 elf_fpregset_t fpregs;
360 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
361 perror_with_name (_("Couldn't get floating point status"));
363 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
366 /* Store all valid floating-point registers in GDB's register array
367 into the process/thread specified by TID. */
369 static void
370 store_fpregs (const struct regcache *regcache, int tid, int regno)
372 elf_fpregset_t fpregs;
374 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
375 perror_with_name (_("Couldn't get floating point status"));
377 fill_fpregset (regcache, &fpregs, regno);
379 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
380 perror_with_name (_("Couldn't write floating point status"));
383 #else
385 static void fetch_fpregs (struct regcache *regcache, int tid)
389 static void store_fpregs (const struct regcache *regcache, int tid, int regno)
393 #endif
395 /* Transferring arbitrary registers between GDB and inferior. */
397 /* Fetch register REGNO from the child process. If REGNO is -1, do
398 this for all registers (including the floating point and SSE
399 registers). */
401 void
402 m68k_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
404 pid_t tid;
406 /* Use the old method of peeking around in `struct user' if the
407 GETREGS request isn't available. */
408 if (! have_ptrace_getregs)
410 old_fetch_inferior_registers (regcache, regno);
411 return;
414 tid = get_ptrace_pid (regcache->ptid ());
416 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
417 transfers more registers in one system call, and we'll cache the
418 results. But remember that fetch_fpxregs can fail, and return
419 zero. */
420 if (regno == -1)
422 fetch_regs (regcache, tid);
424 /* The call above might reset `have_ptrace_getregs'. */
425 if (! have_ptrace_getregs)
427 old_fetch_inferior_registers (regcache, -1);
428 return;
431 fetch_fpregs (regcache, tid);
432 return;
435 if (getregs_supplies (regno))
437 fetch_regs (regcache, tid);
438 return;
441 if (getfpregs_supplies (regno))
443 fetch_fpregs (regcache, tid);
444 return;
447 internal_error (_("Got request for bad register number %d."), regno);
450 /* Store register REGNO back into the child process. If REGNO is -1,
451 do this for all registers (including the floating point and SSE
452 registers). */
453 void
454 m68k_linux_nat_target::store_registers (struct regcache *regcache, int regno)
456 pid_t tid;
458 /* Use the old method of poking around in `struct user' if the
459 SETREGS request isn't available. */
460 if (! have_ptrace_getregs)
462 old_store_inferior_registers (regcache, regno);
463 return;
466 tid = get_ptrace_pid (regcache->ptid ());
468 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
469 transfers more registers in one system call. But remember that
470 store_fpregs can fail, and return zero. */
471 if (regno == -1)
473 store_regs (regcache, tid, regno);
474 store_fpregs (regcache, tid, regno);
475 return;
478 if (getregs_supplies (regno))
480 store_regs (regcache, tid, regno);
481 return;
484 if (getfpregs_supplies (regno))
486 store_fpregs (regcache, tid, regno);
487 return;
490 internal_error (_("Got request to store bad register number %d."), regno);
494 /* Fetch the thread-local storage pointer for libthread_db. */
496 ps_err_e
497 ps_get_thread_area (struct ps_prochandle *ph,
498 lwpid_t lwpid, int idx, void **base)
500 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
501 return PS_ERR;
503 /* IDX is the bias from the thread pointer to the beginning of the
504 thread descriptor. It has to be subtracted due to implementation
505 quirks in libthread_db. */
506 *base = (char *) *base - idx;
508 return PS_OK;
511 void _initialize_m68k_linux_nat ();
512 void
513 _initialize_m68k_linux_nat ()
515 /* Register the target. */
516 linux_target = &the_m68k_linux_nat_target;
517 add_inf_child_target (&the_m68k_linux_nat_target);