gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdb / m68k-linux-nat.c
CommitLineData
a4b6fc86
AC
1/* Motorola m68k native support for GNU/Linux.
2
b811d2c2 3 Copyright (C) 1996-2020 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "frame.h"
22#include "inferior.h"
23#include "language.h"
24#include "gdbcore.h"
4e052eda 25#include "regcache.h"
10d6c8cd
DJ
26#include "target.h"
27#include "linux-nat.h"
a7cdaa91 28#include "gdbarch.h"
c906108c 29
32eeb91a
AS
30#include "m68k-tdep.h"
31
c906108c
SS
32#include <sys/dir.h>
33#include <signal.h>
5826e159 34#include "nat/gdb_ptrace.h"
c906108c
SS
35#include <sys/user.h>
36#include <sys/ioctl.h>
37#include <fcntl.h>
38#include <sys/procfs.h>
39
0280a90a
AS
40#ifdef HAVE_SYS_REG_H
41#include <sys/reg.h>
42#endif
43
c906108c 44#include <sys/file.h>
53ce3c39 45#include <sys/stat.h>
c906108c
SS
46
47#include "floatformat.h"
48
025bb325 49/* Prototypes for supply_gregset etc. */
3e00823e 50#include "gregset.h"
7b8b6d6d
AS
51
52/* Defines ps_err_e, struct ps_prochandle. */
53#include "gdb_proc_service.h"
54
bcc0c096
SM
55#include "inf-ptrace.h"
56
7b8b6d6d
AS
57#ifndef PTRACE_GET_THREAD_AREA
58#define PTRACE_GET_THREAD_AREA 25
59#endif
c906108c 60\f
f6ac5f3d
PA
61
62class m68k_linux_nat_target final : public linux_nat_target
63{
64public:
65 /* Add our register access methods. */
66 void fetch_registers (struct regcache *, int) override;
67 void store_registers (struct regcache *, int) override;
68};
69
70static m68k_linux_nat_target the_m68k_linux_nat_target;
71
c9f4d572 72/* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
c5aa993b 73static const int regmap[] =
c906108c
SS
74{
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
82};
83
0280a90a
AS
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)
88
0c13fc49 89static int
0280a90a
AS
90getregs_supplies (int regno)
91{
92 return 0 <= regno && regno < NUM_GREGS;
93}
94
0c13fc49 95static int
0280a90a
AS
96getfpregs_supplies (int regno)
97{
4ed226fe 98 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
0280a90a
AS
99}
100
101/* Does the current host support the GETREGS request? */
0c13fc49 102static int have_ptrace_getregs =
0280a90a
AS
103#ifdef HAVE_PTRACE_GETREGS
104 1
105#else
106 0
107#endif
108;
109
110\f
111
0280a90a
AS
112/* Fetching registers directly from the U area, one at a time. */
113
0280a90a
AS
114/* Fetch one register. */
115
116static void
56be3814 117fetch_register (struct regcache *regcache, int regno)
0280a90a 118{
ac7936df 119 struct gdbarch *gdbarch = regcache->arch ();
89e028e2 120 long regaddr, val;
52f0bd74 121 int i;
975c21ab 122 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
222312d3 123 pid_t tid = get_ptrace_pid (regcache->ptid ());
0280a90a 124
de732108 125 regaddr = 4 * regmap[regno];
335d71d6 126 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
0280a90a
AS
127 {
128 errno = 0;
89e028e2
AS
129 val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
130 memcpy (&buf[i], &val, sizeof (long));
335d71d6 131 regaddr += sizeof (long);
0280a90a 132 if (errno != 0)
335d71d6
AS
133 error (_("Couldn't read register %s (#%d): %s."),
134 gdbarch_register_name (gdbarch, regno),
135 regno, safe_strerror (errno));
0280a90a 136 }
73e1c03f 137 regcache->raw_supply (regno, buf);
0280a90a
AS
138}
139
140/* Fetch register values from the inferior.
141 If REGNO is negative, do this for all registers.
025bb325 142 Otherwise, REGNO specifies which register (so we can save time). */
c906108c 143
10d6c8cd 144static void
56be3814 145old_fetch_inferior_registers (struct regcache *regcache, int regno)
0280a90a
AS
146{
147 if (regno >= 0)
148 {
56be3814 149 fetch_register (regcache, regno);
0280a90a
AS
150 }
151 else
152 {
c984b7ff 153 for (regno = 0;
ac7936df 154 regno < gdbarch_num_regs (regcache->arch ());
c984b7ff 155 regno++)
0280a90a 156 {
56be3814 157 fetch_register (regcache, regno);
0280a90a
AS
158 }
159 }
160}
161
025bb325 162/* Store one register. */
0280a90a
AS
163
164static void
56be3814 165store_register (const struct regcache *regcache, int regno)
0280a90a 166{
ac7936df 167 struct gdbarch *gdbarch = regcache->arch ();
89e028e2 168 long regaddr, val;
52f0bd74 169 int i;
975c21ab 170 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
222312d3 171 pid_t tid = get_ptrace_pid (regcache->ptid ());
0280a90a 172
de732108 173 regaddr = 4 * regmap[regno];
9852326a 174
025bb325 175 /* Put the contents of regno into a local buffer. */
34a79281 176 regcache->raw_collect (regno, buf);
9852326a 177
025bb325 178 /* Store the local buffer into the inferior a chunk at the time. */
335d71d6 179 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
0280a90a
AS
180 {
181 errno = 0;
89e028e2
AS
182 memcpy (&val, &buf[i], sizeof (long));
183 ptrace (PTRACE_POKEUSER, tid, regaddr, val);
335d71d6 184 regaddr += sizeof (long);
0280a90a 185 if (errno != 0)
335d71d6
AS
186 error (_("Couldn't write register %s (#%d): %s."),
187 gdbarch_register_name (gdbarch, regno),
188 regno, safe_strerror (errno));
0280a90a
AS
189 }
190}
191
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). */
195
10d6c8cd 196static void
56be3814 197old_store_inferior_registers (const struct regcache *regcache, int regno)
0280a90a
AS
198{
199 if (regno >= 0)
200 {
56be3814 201 store_register (regcache, regno);
0280a90a
AS
202 }
203 else
204 {
c984b7ff 205 for (regno = 0;
ac7936df 206 regno < gdbarch_num_regs (regcache->arch ());
c984b7ff 207 regno++)
0280a90a 208 {
56be3814 209 store_register (regcache, regno);
0280a90a
AS
210 }
211 }
212}
213\f
f175af98
DJ
214/* Given a pointer to a general register set in /proc format
215 (elf_gregset_t *), unpack the register contents and supply
025bb325 216 them as gdb's idea of the current register values. */
c906108c 217
c906108c 218void
7f7fe91e 219supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
c906108c 220{
ac7936df 221 struct gdbarch *gdbarch = regcache->arch ();
7f7fe91e 222 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
c906108c
SS
223 int regi;
224
3e8c568d 225 for (regi = M68K_D0_REGNUM;
c984b7ff 226 regi <= gdbarch_sp_regnum (gdbarch);
3e8c568d 227 regi++)
73e1c03f
SM
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]);
0280a90a
AS
231}
232
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. */
236void
7f7fe91e
UW
237fill_gregset (const struct regcache *regcache,
238 elf_gregset_t *gregsetp, int regno)
0280a90a
AS
239{
240 elf_greg_t *regp = (elf_greg_t *) gregsetp;
241 int i;
242
243 for (i = 0; i < NUM_GREGS; i++)
8de307e0 244 if (regno == -1 || regno == i)
34a79281 245 regcache->raw_collect (i, regp + regmap[i]);
0280a90a
AS
246}
247
248#ifdef HAVE_PTRACE_GETREGS
249
250/* Fetch all general-purpose registers from process/thread TID and
251 store their values in GDB's register array. */
252
253static void
56be3814 254fetch_regs (struct regcache *regcache, int tid)
0280a90a
AS
255{
256 elf_gregset_t regs;
257
258 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
259 {
260 if (errno == EIO)
261 {
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;
266 }
267
e2e0b3e5 268 perror_with_name (_("Couldn't get registers"));
0280a90a
AS
269 }
270
56be3814 271 supply_gregset (regcache, (const elf_gregset_t *) &regs);
c906108c
SS
272}
273
0280a90a
AS
274/* Store all valid general-purpose registers in GDB's register array
275 into the process/thread specified by TID. */
276
277static void
56be3814 278store_regs (const struct regcache *regcache, int tid, int regno)
0280a90a
AS
279{
280 elf_gregset_t regs;
281
282 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 283 perror_with_name (_("Couldn't get registers"));
0280a90a 284
56be3814 285 fill_gregset (regcache, &regs, regno);
0280a90a
AS
286
287 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 288 perror_with_name (_("Couldn't write registers"));
0280a90a
AS
289}
290
291#else
292
025bb325
MS
293static void fetch_regs (struct regcache *regcache, int tid)
294{
295}
296
297static void store_regs (const struct regcache *regcache, int tid, int regno)
298{
299}
0280a90a
AS
300
301#endif
302
303\f
304/* Transfering floating-point registers between GDB, inferiors and cores. */
305
306/* What is the address of fpN within the floating-point register set F? */
7f7fe91e 307#define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
0280a90a
AS
308
309/* Fill GDB's register array with the floating-point register values in
310 *FPREGSETP. */
c906108c 311
c5aa993b 312void
7f7fe91e 313supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
c906108c 314{
ac7936df 315 struct gdbarch *gdbarch = regcache->arch ();
c906108c
SS
316 int regi;
317
c984b7ff
UW
318 for (regi = gdbarch_fp0_regnum (gdbarch);
319 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
73e1c03f
SM
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]);
c906108c
SS
325}
326
0280a90a
AS
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. */
330
331void
7f7fe91e
UW
332fill_fpregset (const struct regcache *regcache,
333 elf_fpregset_t *fpregsetp, int regno)
0280a90a 334{
ac7936df 335 struct gdbarch *gdbarch = regcache->arch ();
0280a90a
AS
336 int i;
337
338 /* Fill in the floating-point registers. */
c984b7ff
UW
339 for (i = gdbarch_fp0_regnum (gdbarch);
340 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
0280a90a 341 if (regno == -1 || regno == i)
34a79281
SM
342 regcache->raw_collect
343 (i, FPREG_ADDR (fpregsetp, i - gdbarch_fp0_regnum (gdbarch)));
0280a90a
AS
344
345 /* Fill in the floating-point control registers. */
32eeb91a 346 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
0280a90a 347 if (regno == -1 || regno == i)
34a79281 348 regcache->raw_collect (i, &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
0280a90a
AS
349}
350
351#ifdef HAVE_PTRACE_GETREGS
352
353/* Fetch all floating-point registers from process/thread TID and store
354 thier values in GDB's register array. */
355
356static void
56be3814 357fetch_fpregs (struct regcache *regcache, int tid)
0280a90a
AS
358{
359 elf_fpregset_t fpregs;
360
361 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 362 perror_with_name (_("Couldn't get floating point status"));
0280a90a 363
56be3814 364 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
0280a90a
AS
365}
366
367/* Store all valid floating-point registers in GDB's register array
368 into the process/thread specified by TID. */
369
370static void
56be3814 371store_fpregs (const struct regcache *regcache, int tid, int regno)
0280a90a
AS
372{
373 elf_fpregset_t fpregs;
374
375 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 376 perror_with_name (_("Couldn't get floating point status"));
0280a90a 377
56be3814 378 fill_fpregset (regcache, &fpregs, regno);
0280a90a
AS
379
380 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 381 perror_with_name (_("Couldn't write floating point status"));
0280a90a
AS
382}
383
384#else
385
025bb325
MS
386static void fetch_fpregs (struct regcache *regcache, int tid)
387{
388}
389
390static void store_fpregs (const struct regcache *regcache, int tid, int regno)
391{
392}
0280a90a 393
c906108c 394#endif
0280a90a
AS
395\f
396/* Transferring arbitrary registers between GDB and inferior. */
397
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). */
401
f6ac5f3d
PA
402void
403m68k_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
0280a90a 404{
bcc0c096 405 pid_t tid;
0280a90a
AS
406
407 /* Use the old method of peeking around in `struct user' if the
408 GETREGS request isn't available. */
409 if (! have_ptrace_getregs)
410 {
56be3814 411 old_fetch_inferior_registers (regcache, regno);
0280a90a
AS
412 return;
413 }
414
222312d3 415 tid = get_ptrace_pid (regcache->ptid ());
f175af98 416
0280a90a
AS
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)
422 {
56be3814 423 fetch_regs (regcache, tid);
0280a90a
AS
424
425 /* The call above might reset `have_ptrace_getregs'. */
426 if (! have_ptrace_getregs)
427 {
56be3814 428 old_fetch_inferior_registers (regcache, -1);
0280a90a
AS
429 return;
430 }
431
56be3814 432 fetch_fpregs (regcache, tid);
0280a90a
AS
433 return;
434 }
435
436 if (getregs_supplies (regno))
437 {
56be3814 438 fetch_regs (regcache, tid);
0280a90a
AS
439 return;
440 }
441
442 if (getfpregs_supplies (regno))
443 {
56be3814 444 fetch_fpregs (regcache, tid);
0280a90a
AS
445 return;
446 }
447
448 internal_error (__FILE__, __LINE__,
e2e0b3e5 449 _("Got request for bad register number %d."), regno);
0280a90a
AS
450}
451
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). */
f6ac5f3d
PA
455void
456m68k_linux_nat_target::store_registers (struct regcache *regcache, int regno)
0280a90a 457{
bcc0c096 458 pid_t tid;
0280a90a
AS
459
460 /* Use the old method of poking around in `struct user' if the
461 SETREGS request isn't available. */
462 if (! have_ptrace_getregs)
463 {
56be3814 464 old_store_inferior_registers (regcache, regno);
0280a90a
AS
465 return;
466 }
467
222312d3 468 tid = get_ptrace_pid (regcache->ptid ());
0280a90a
AS
469
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)
474 {
56be3814
UW
475 store_regs (regcache, tid, regno);
476 store_fpregs (regcache, tid, regno);
0280a90a
AS
477 return;
478 }
479
480 if (getregs_supplies (regno))
481 {
56be3814 482 store_regs (regcache, tid, regno);
0280a90a
AS
483 return;
484 }
485
486 if (getfpregs_supplies (regno))
487 {
56be3814 488 store_fpregs (regcache, tid, regno);
0280a90a
AS
489 return;
490 }
491
492 internal_error (__FILE__, __LINE__,
e2e0b3e5 493 _("Got request to store bad register number %d."), regno);
0280a90a 494}
f175af98 495\f
c5aa993b 496
7b8b6d6d
AS
497/* Fetch the thread-local storage pointer for libthread_db. */
498
499ps_err_e
754653a7 500ps_get_thread_area (struct ps_prochandle *ph,
7b8b6d6d
AS
501 lwpid_t lwpid, int idx, void **base)
502{
503 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
504 return PS_ERR;
505
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;
510
511 return PS_OK;
512}
10d6c8cd 513
6c265988 514void _initialize_m68k_linux_nat ();
f175af98 515void
6c265988 516_initialize_m68k_linux_nat ()
f175af98 517{
10d6c8cd 518 /* Register the target. */
f6ac5f3d 519 linux_target = &the_m68k_linux_nat_target;
d9f719f1 520 add_inf_child_target (&the_m68k_linux_nat_target);
f175af98 521}
This page took 1.764462 seconds and 4 git commands to generate.