]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/m68k-linux-nat.c
Update copyright year range in all GDB files
[thirdparty/binutils-gdb.git] / gdb / m68k-linux-nat.c
CommitLineData
a4b6fc86
AC
1/* Motorola m68k native support for GNU/Linux.
2
e2882c85 3 Copyright (C) 1996-2018 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"
c906108c 28
32eeb91a
AS
29#include "m68k-tdep.h"
30
c906108c
SS
31#include <sys/dir.h>
32#include <signal.h>
5826e159 33#include "nat/gdb_ptrace.h"
c906108c
SS
34#include <sys/user.h>
35#include <sys/ioctl.h>
36#include <fcntl.h>
37#include <sys/procfs.h>
38
0280a90a
AS
39#ifdef HAVE_SYS_REG_H
40#include <sys/reg.h>
41#endif
42
c906108c 43#include <sys/file.h>
53ce3c39 44#include <sys/stat.h>
c906108c
SS
45
46#include "floatformat.h"
47
025bb325 48/* Prototypes for supply_gregset etc. */
3e00823e 49#include "gregset.h"
7b8b6d6d
AS
50
51/* Defines ps_err_e, struct ps_prochandle. */
52#include "gdb_proc_service.h"
53
bcc0c096
SM
54#include "inf-ptrace.h"
55
7b8b6d6d
AS
56#ifndef PTRACE_GET_THREAD_AREA
57#define PTRACE_GET_THREAD_AREA 25
58#endif
c906108c 59\f
c9f4d572 60/* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
c5aa993b 61static const int regmap[] =
c906108c
SS
62{
63 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
64 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
65 PT_SR, PT_PC,
66 /* PT_FP0, ..., PT_FP7 */
67 21, 24, 27, 30, 33, 36, 39, 42,
68 /* PT_FPCR, PT_FPSR, PT_FPIAR */
69 45, 46, 47
70};
71
0280a90a
AS
72/* Which ptrace request retrieves which registers?
73 These apply to the corresponding SET requests as well. */
74#define NUM_GREGS (18)
75#define MAX_NUM_REGS (NUM_GREGS + 11)
76
0c13fc49 77static int
0280a90a
AS
78getregs_supplies (int regno)
79{
80 return 0 <= regno && regno < NUM_GREGS;
81}
82
0c13fc49 83static int
0280a90a
AS
84getfpregs_supplies (int regno)
85{
4ed226fe 86 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
0280a90a
AS
87}
88
89/* Does the current host support the GETREGS request? */
0c13fc49 90static int have_ptrace_getregs =
0280a90a
AS
91#ifdef HAVE_PTRACE_GETREGS
92 1
93#else
94 0
95#endif
96;
97
98\f
99
0280a90a
AS
100/* Fetching registers directly from the U area, one at a time. */
101
0280a90a
AS
102/* Fetch one register. */
103
104static void
56be3814 105fetch_register (struct regcache *regcache, int regno)
0280a90a 106{
ac7936df 107 struct gdbarch *gdbarch = regcache->arch ();
89e028e2 108 long regaddr, val;
52f0bd74 109 int i;
975c21ab 110 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
bcc0c096 111 pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
0280a90a 112
de732108 113 regaddr = 4 * regmap[regno];
335d71d6 114 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
0280a90a
AS
115 {
116 errno = 0;
89e028e2
AS
117 val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
118 memcpy (&buf[i], &val, sizeof (long));
335d71d6 119 regaddr += sizeof (long);
0280a90a 120 if (errno != 0)
335d71d6
AS
121 error (_("Couldn't read register %s (#%d): %s."),
122 gdbarch_register_name (gdbarch, regno),
123 regno, safe_strerror (errno));
0280a90a 124 }
56be3814 125 regcache_raw_supply (regcache, regno, buf);
0280a90a
AS
126}
127
128/* Fetch register values from the inferior.
129 If REGNO is negative, do this for all registers.
025bb325 130 Otherwise, REGNO specifies which register (so we can save time). */
c906108c 131
10d6c8cd 132static void
56be3814 133old_fetch_inferior_registers (struct regcache *regcache, int regno)
0280a90a
AS
134{
135 if (regno >= 0)
136 {
56be3814 137 fetch_register (regcache, regno);
0280a90a
AS
138 }
139 else
140 {
c984b7ff 141 for (regno = 0;
ac7936df 142 regno < gdbarch_num_regs (regcache->arch ());
c984b7ff 143 regno++)
0280a90a 144 {
56be3814 145 fetch_register (regcache, regno);
0280a90a
AS
146 }
147 }
148}
149
025bb325 150/* Store one register. */
0280a90a
AS
151
152static void
56be3814 153store_register (const struct regcache *regcache, int regno)
0280a90a 154{
ac7936df 155 struct gdbarch *gdbarch = regcache->arch ();
89e028e2 156 long regaddr, val;
52f0bd74 157 int i;
975c21ab 158 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
bcc0c096 159 pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
0280a90a 160
de732108 161 regaddr = 4 * regmap[regno];
9852326a 162
025bb325 163 /* Put the contents of regno into a local buffer. */
56be3814 164 regcache_raw_collect (regcache, regno, buf);
9852326a 165
025bb325 166 /* Store the local buffer into the inferior a chunk at the time. */
335d71d6 167 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
0280a90a
AS
168 {
169 errno = 0;
89e028e2
AS
170 memcpy (&val, &buf[i], sizeof (long));
171 ptrace (PTRACE_POKEUSER, tid, regaddr, val);
335d71d6 172 regaddr += sizeof (long);
0280a90a 173 if (errno != 0)
335d71d6
AS
174 error (_("Couldn't write register %s (#%d): %s."),
175 gdbarch_register_name (gdbarch, regno),
176 regno, safe_strerror (errno));
0280a90a
AS
177 }
178}
179
180/* Store our register values back into the inferior.
181 If REGNO is negative, do this for all registers.
182 Otherwise, REGNO specifies which register (so we can save time). */
183
10d6c8cd 184static void
56be3814 185old_store_inferior_registers (const struct regcache *regcache, int regno)
0280a90a
AS
186{
187 if (regno >= 0)
188 {
56be3814 189 store_register (regcache, regno);
0280a90a
AS
190 }
191 else
192 {
c984b7ff 193 for (regno = 0;
ac7936df 194 regno < gdbarch_num_regs (regcache->arch ());
c984b7ff 195 regno++)
0280a90a 196 {
56be3814 197 store_register (regcache, regno);
0280a90a
AS
198 }
199 }
200}
201\f
f175af98
DJ
202/* Given a pointer to a general register set in /proc format
203 (elf_gregset_t *), unpack the register contents and supply
025bb325 204 them as gdb's idea of the current register values. */
c906108c 205
c906108c 206void
7f7fe91e 207supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
c906108c 208{
ac7936df 209 struct gdbarch *gdbarch = regcache->arch ();
7f7fe91e 210 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
c906108c
SS
211 int regi;
212
3e8c568d 213 for (regi = M68K_D0_REGNUM;
c984b7ff 214 regi <= gdbarch_sp_regnum (gdbarch);
3e8c568d 215 regi++)
7f7fe91e 216 regcache_raw_supply (regcache, regi, &regp[regmap[regi]]);
c984b7ff 217 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
3e8c568d
UW
218 &regp[PT_SR]);
219 regcache_raw_supply (regcache,
c984b7ff 220 gdbarch_pc_regnum (gdbarch), &regp[PT_PC]);
0280a90a
AS
221}
222
223/* Fill register REGNO (if it is a general-purpose register) in
224 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
225 do this for all registers. */
226void
7f7fe91e
UW
227fill_gregset (const struct regcache *regcache,
228 elf_gregset_t *gregsetp, int regno)
0280a90a
AS
229{
230 elf_greg_t *regp = (elf_greg_t *) gregsetp;
231 int i;
232
233 for (i = 0; i < NUM_GREGS; i++)
8de307e0 234 if (regno == -1 || regno == i)
7f7fe91e 235 regcache_raw_collect (regcache, i, regp + regmap[i]);
0280a90a
AS
236}
237
238#ifdef HAVE_PTRACE_GETREGS
239
240/* Fetch all general-purpose registers from process/thread TID and
241 store their values in GDB's register array. */
242
243static void
56be3814 244fetch_regs (struct regcache *regcache, int tid)
0280a90a
AS
245{
246 elf_gregset_t regs;
247
248 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
249 {
250 if (errno == EIO)
251 {
252 /* The kernel we're running on doesn't support the GETREGS
253 request. Reset `have_ptrace_getregs'. */
254 have_ptrace_getregs = 0;
255 return;
256 }
257
e2e0b3e5 258 perror_with_name (_("Couldn't get registers"));
0280a90a
AS
259 }
260
56be3814 261 supply_gregset (regcache, (const elf_gregset_t *) &regs);
c906108c
SS
262}
263
0280a90a
AS
264/* Store all valid general-purpose registers in GDB's register array
265 into the process/thread specified by TID. */
266
267static void
56be3814 268store_regs (const struct regcache *regcache, int tid, int regno)
0280a90a
AS
269{
270 elf_gregset_t regs;
271
272 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 273 perror_with_name (_("Couldn't get registers"));
0280a90a 274
56be3814 275 fill_gregset (regcache, &regs, regno);
0280a90a
AS
276
277 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 278 perror_with_name (_("Couldn't write registers"));
0280a90a
AS
279}
280
281#else
282
025bb325
MS
283static void fetch_regs (struct regcache *regcache, int tid)
284{
285}
286
287static void store_regs (const struct regcache *regcache, int tid, int regno)
288{
289}
0280a90a
AS
290
291#endif
292
293\f
294/* Transfering floating-point registers between GDB, inferiors and cores. */
295
296/* What is the address of fpN within the floating-point register set F? */
7f7fe91e 297#define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
0280a90a
AS
298
299/* Fill GDB's register array with the floating-point register values in
300 *FPREGSETP. */
c906108c 301
c5aa993b 302void
7f7fe91e 303supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
c906108c 304{
ac7936df 305 struct gdbarch *gdbarch = regcache->arch ();
c906108c
SS
306 int regi;
307
c984b7ff
UW
308 for (regi = gdbarch_fp0_regnum (gdbarch);
309 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
7f7fe91e 310 regcache_raw_supply (regcache, regi,
3e8c568d 311 FPREG_ADDR (fpregsetp,
c984b7ff 312 regi - gdbarch_fp0_regnum (gdbarch)));
7f7fe91e
UW
313 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
314 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
315 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
c906108c
SS
316}
317
0280a90a
AS
318/* Fill register REGNO (if it is a floating-point register) in
319 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
320 do this for all registers. */
321
322void
7f7fe91e
UW
323fill_fpregset (const struct regcache *regcache,
324 elf_fpregset_t *fpregsetp, int regno)
0280a90a 325{
ac7936df 326 struct gdbarch *gdbarch = regcache->arch ();
0280a90a
AS
327 int i;
328
329 /* Fill in the floating-point registers. */
c984b7ff
UW
330 for (i = gdbarch_fp0_regnum (gdbarch);
331 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
0280a90a 332 if (regno == -1 || regno == i)
7f7fe91e 333 regcache_raw_collect (regcache, i,
3e8c568d 334 FPREG_ADDR (fpregsetp,
c984b7ff 335 i - gdbarch_fp0_regnum (gdbarch)));
0280a90a
AS
336
337 /* Fill in the floating-point control registers. */
32eeb91a 338 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
0280a90a 339 if (regno == -1 || regno == i)
7f7fe91e
UW
340 regcache_raw_collect (regcache, i,
341 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
0280a90a
AS
342}
343
344#ifdef HAVE_PTRACE_GETREGS
345
346/* Fetch all floating-point registers from process/thread TID and store
347 thier values in GDB's register array. */
348
349static void
56be3814 350fetch_fpregs (struct regcache *regcache, int tid)
0280a90a
AS
351{
352 elf_fpregset_t fpregs;
353
354 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 355 perror_with_name (_("Couldn't get floating point status"));
0280a90a 356
56be3814 357 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
0280a90a
AS
358}
359
360/* Store all valid floating-point registers in GDB's register array
361 into the process/thread specified by TID. */
362
363static void
56be3814 364store_fpregs (const struct regcache *regcache, int tid, int regno)
0280a90a
AS
365{
366 elf_fpregset_t fpregs;
367
368 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 369 perror_with_name (_("Couldn't get floating point status"));
0280a90a 370
56be3814 371 fill_fpregset (regcache, &fpregs, regno);
0280a90a
AS
372
373 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 374 perror_with_name (_("Couldn't write floating point status"));
0280a90a
AS
375}
376
377#else
378
025bb325
MS
379static void fetch_fpregs (struct regcache *regcache, int tid)
380{
381}
382
383static void store_fpregs (const struct regcache *regcache, int tid, int regno)
384{
385}
0280a90a 386
c906108c 387#endif
0280a90a
AS
388\f
389/* Transferring arbitrary registers between GDB and inferior. */
390
391/* Fetch register REGNO from the child process. If REGNO is -1, do
392 this for all registers (including the floating point and SSE
393 registers). */
394
10d6c8cd 395static void
28439f5e
PA
396m68k_linux_fetch_inferior_registers (struct target_ops *ops,
397 struct regcache *regcache, int regno)
0280a90a 398{
bcc0c096 399 pid_t tid;
0280a90a
AS
400
401 /* Use the old method of peeking around in `struct user' if the
402 GETREGS request isn't available. */
403 if (! have_ptrace_getregs)
404 {
56be3814 405 old_fetch_inferior_registers (regcache, regno);
0280a90a
AS
406 return;
407 }
408
bcc0c096 409 tid = get_ptrace_pid (regcache_get_ptid (regcache));
f175af98 410
0280a90a
AS
411 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
412 transfers more registers in one system call, and we'll cache the
413 results. But remember that fetch_fpxregs can fail, and return
414 zero. */
415 if (regno == -1)
416 {
56be3814 417 fetch_regs (regcache, tid);
0280a90a
AS
418
419 /* The call above might reset `have_ptrace_getregs'. */
420 if (! have_ptrace_getregs)
421 {
56be3814 422 old_fetch_inferior_registers (regcache, -1);
0280a90a
AS
423 return;
424 }
425
56be3814 426 fetch_fpregs (regcache, tid);
0280a90a
AS
427 return;
428 }
429
430 if (getregs_supplies (regno))
431 {
56be3814 432 fetch_regs (regcache, tid);
0280a90a
AS
433 return;
434 }
435
436 if (getfpregs_supplies (regno))
437 {
56be3814 438 fetch_fpregs (regcache, tid);
0280a90a
AS
439 return;
440 }
441
442 internal_error (__FILE__, __LINE__,
e2e0b3e5 443 _("Got request for bad register number %d."), regno);
0280a90a
AS
444}
445
446/* Store register REGNO back into the child process. If REGNO is -1,
447 do this for all registers (including the floating point and SSE
448 registers). */
10d6c8cd 449static void
28439f5e
PA
450m68k_linux_store_inferior_registers (struct target_ops *ops,
451 struct regcache *regcache, int regno)
0280a90a 452{
bcc0c096 453 pid_t tid;
0280a90a
AS
454
455 /* Use the old method of poking around in `struct user' if the
456 SETREGS request isn't available. */
457 if (! have_ptrace_getregs)
458 {
56be3814 459 old_store_inferior_registers (regcache, regno);
0280a90a
AS
460 return;
461 }
462
bcc0c096 463 tid = get_ptrace_pid (regcache_get_ptid (regcache));
0280a90a
AS
464
465 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
466 transfers more registers in one system call. But remember that
467 store_fpregs can fail, and return zero. */
468 if (regno == -1)
469 {
56be3814
UW
470 store_regs (regcache, tid, regno);
471 store_fpregs (regcache, tid, regno);
0280a90a
AS
472 return;
473 }
474
475 if (getregs_supplies (regno))
476 {
56be3814 477 store_regs (regcache, tid, regno);
0280a90a
AS
478 return;
479 }
480
481 if (getfpregs_supplies (regno))
482 {
56be3814 483 store_fpregs (regcache, tid, regno);
0280a90a
AS
484 return;
485 }
486
487 internal_error (__FILE__, __LINE__,
e2e0b3e5 488 _("Got request to store bad register number %d."), regno);
0280a90a 489}
f175af98 490\f
c5aa993b 491
7b8b6d6d
AS
492/* Fetch the thread-local storage pointer for libthread_db. */
493
494ps_err_e
754653a7 495ps_get_thread_area (struct ps_prochandle *ph,
7b8b6d6d
AS
496 lwpid_t lwpid, int idx, void **base)
497{
498 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
499 return PS_ERR;
500
501 /* IDX is the bias from the thread pointer to the beginning of the
502 thread descriptor. It has to be subtracted due to implementation
503 quirks in libthread_db. */
504 *base = (char *) *base - idx;
505
506 return PS_OK;
507}
10d6c8cd 508
f175af98 509void
5ae5f592 510_initialize_m68k_linux_nat (void)
f175af98 511{
10d6c8cd
DJ
512 struct target_ops *t;
513
514 /* Fill in the generic GNU/Linux methods. */
515 t = linux_target ();
516
517 /* Add our register access methods. */
518 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
519 t->to_store_registers = m68k_linux_store_inferior_registers;
520
521 /* Register the target. */
f973ed9c 522 linux_nat_add_target (t);
f175af98 523}