* gas/nios2/nios2.exp: Add copyright.
[deliverable/binutils-gdb.git] / gdb / m68klinux-nat.c
CommitLineData
a4b6fc86
AC
1/* Motorola m68k native support for GNU/Linux.
2
28e7fd62 3 Copyright (C) 1996-2013 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"
32eeb91a 25#include "gdb_string.h"
4e052eda 26#include "regcache.h"
10d6c8cd
DJ
27#include "target.h"
28#include "linux-nat.h"
c906108c 29
32eeb91a
AS
30#include "m68k-tdep.h"
31
c906108c
SS
32#include <sys/param.h>
33#include <sys/dir.h>
34#include <signal.h>
0280a90a 35#include <sys/ptrace.h>
c906108c
SS
36#include <sys/user.h>
37#include <sys/ioctl.h>
38#include <fcntl.h>
39#include <sys/procfs.h>
40
0280a90a
AS
41#ifdef HAVE_SYS_REG_H
42#include <sys/reg.h>
43#endif
44
c906108c
SS
45#include <sys/file.h>
46#include "gdb_stat.h"
47
48#include "floatformat.h"
49
50#include "target.h"
3e00823e 51
025bb325 52/* Prototypes for supply_gregset etc. */
3e00823e 53#include "gregset.h"
7b8b6d6d
AS
54
55/* Defines ps_err_e, struct ps_prochandle. */
56#include "gdb_proc_service.h"
57
58#ifndef PTRACE_GET_THREAD_AREA
59#define PTRACE_GET_THREAD_AREA 25
60#endif
c906108c 61\f
c9f4d572 62/* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
c5aa993b 63static const int regmap[] =
c906108c
SS
64{
65 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
66 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
67 PT_SR, PT_PC,
68 /* PT_FP0, ..., PT_FP7 */
69 21, 24, 27, 30, 33, 36, 39, 42,
70 /* PT_FPCR, PT_FPSR, PT_FPIAR */
71 45, 46, 47
72};
73
0280a90a
AS
74/* Which ptrace request retrieves which registers?
75 These apply to the corresponding SET requests as well. */
76#define NUM_GREGS (18)
77#define MAX_NUM_REGS (NUM_GREGS + 11)
78
0c13fc49 79static int
0280a90a
AS
80getregs_supplies (int regno)
81{
82 return 0 <= regno && regno < NUM_GREGS;
83}
84
0c13fc49 85static int
0280a90a
AS
86getfpregs_supplies (int regno)
87{
4ed226fe 88 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
0280a90a
AS
89}
90
91/* Does the current host support the GETREGS request? */
0c13fc49 92static int have_ptrace_getregs =
0280a90a
AS
93#ifdef HAVE_PTRACE_GETREGS
94 1
95#else
96 0
97#endif
98;
99
100\f
101
0280a90a
AS
102/* Fetching registers directly from the U area, one at a time. */
103
0280a90a
AS
104/* Fetch one register. */
105
106static void
56be3814 107fetch_register (struct regcache *regcache, int regno)
0280a90a 108{
c984b7ff 109 struct gdbarch *gdbarch = get_regcache_arch (regcache);
89e028e2 110 long regaddr, val;
52f0bd74 111 int i;
123a958e 112 char buf[MAX_REGISTER_SIZE];
0280a90a
AS
113 int tid;
114
025bb325 115 /* Overload thread id onto process id. */
8de307e0
AS
116 tid = TIDGET (inferior_ptid);
117 if (tid == 0)
025bb325
MS
118 tid = PIDGET (inferior_ptid); /* no thread id, just use
119 process id. */
0280a90a 120
de732108 121 regaddr = 4 * regmap[regno];
335d71d6 122 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
0280a90a
AS
123 {
124 errno = 0;
89e028e2
AS
125 val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
126 memcpy (&buf[i], &val, sizeof (long));
335d71d6 127 regaddr += sizeof (long);
0280a90a 128 if (errno != 0)
335d71d6
AS
129 error (_("Couldn't read register %s (#%d): %s."),
130 gdbarch_register_name (gdbarch, regno),
131 regno, safe_strerror (errno));
0280a90a 132 }
56be3814 133 regcache_raw_supply (regcache, regno, buf);
0280a90a
AS
134}
135
136/* Fetch register values from the inferior.
137 If REGNO is negative, do this for all registers.
025bb325 138 Otherwise, REGNO specifies which register (so we can save time). */
c906108c 139
10d6c8cd 140static void
56be3814 141old_fetch_inferior_registers (struct regcache *regcache, int regno)
0280a90a
AS
142{
143 if (regno >= 0)
144 {
56be3814 145 fetch_register (regcache, regno);
0280a90a
AS
146 }
147 else
148 {
c984b7ff
UW
149 for (regno = 0;
150 regno < gdbarch_num_regs (get_regcache_arch (regcache));
151 regno++)
0280a90a 152 {
56be3814 153 fetch_register (regcache, regno);
0280a90a
AS
154 }
155 }
156}
157
025bb325 158/* Store one register. */
0280a90a
AS
159
160static void
56be3814 161store_register (const struct regcache *regcache, int regno)
0280a90a 162{
f36bf22c 163 struct gdbarch *gdbarch = get_regcache_arch (regcache);
89e028e2 164 long regaddr, val;
52f0bd74 165 int i;
0280a90a 166 int tid;
d9d9c31f 167 char buf[MAX_REGISTER_SIZE];
0280a90a 168
025bb325 169 /* Overload thread id onto process id. */
8de307e0
AS
170 tid = TIDGET (inferior_ptid);
171 if (tid == 0)
025bb325
MS
172 tid = PIDGET (inferior_ptid); /* no thread id, just use
173 process id. */
0280a90a 174
de732108 175 regaddr = 4 * regmap[regno];
9852326a 176
025bb325 177 /* Put the contents of regno into a local buffer. */
56be3814 178 regcache_raw_collect (regcache, regno, buf);
9852326a 179
025bb325 180 /* Store the local buffer into the inferior a chunk at the time. */
335d71d6 181 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
0280a90a
AS
182 {
183 errno = 0;
89e028e2
AS
184 memcpy (&val, &buf[i], sizeof (long));
185 ptrace (PTRACE_POKEUSER, tid, regaddr, val);
335d71d6 186 regaddr += sizeof (long);
0280a90a 187 if (errno != 0)
335d71d6
AS
188 error (_("Couldn't write register %s (#%d): %s."),
189 gdbarch_register_name (gdbarch, regno),
190 regno, safe_strerror (errno));
0280a90a
AS
191 }
192}
193
194/* Store our register values back into the inferior.
195 If REGNO is negative, do this for all registers.
196 Otherwise, REGNO specifies which register (so we can save time). */
197
10d6c8cd 198static void
56be3814 199old_store_inferior_registers (const struct regcache *regcache, int regno)
0280a90a
AS
200{
201 if (regno >= 0)
202 {
56be3814 203 store_register (regcache, regno);
0280a90a
AS
204 }
205 else
206 {
c984b7ff
UW
207 for (regno = 0;
208 regno < gdbarch_num_regs (get_regcache_arch (regcache));
209 regno++)
0280a90a 210 {
56be3814 211 store_register (regcache, regno);
0280a90a
AS
212 }
213 }
214}
215\f
f175af98
DJ
216/* Given a pointer to a general register set in /proc format
217 (elf_gregset_t *), unpack the register contents and supply
025bb325 218 them as gdb's idea of the current register values. */
c906108c 219
c906108c 220void
7f7fe91e 221supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
c906108c 222{
c984b7ff 223 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7f7fe91e 224 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
c906108c
SS
225 int regi;
226
3e8c568d 227 for (regi = M68K_D0_REGNUM;
c984b7ff 228 regi <= gdbarch_sp_regnum (gdbarch);
3e8c568d 229 regi++)
7f7fe91e 230 regcache_raw_supply (regcache, regi, &regp[regmap[regi]]);
c984b7ff 231 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
3e8c568d
UW
232 &regp[PT_SR]);
233 regcache_raw_supply (regcache,
c984b7ff 234 gdbarch_pc_regnum (gdbarch), &regp[PT_PC]);
0280a90a
AS
235}
236
237/* Fill register REGNO (if it is a general-purpose register) in
238 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
239 do this for all registers. */
240void
7f7fe91e
UW
241fill_gregset (const struct regcache *regcache,
242 elf_gregset_t *gregsetp, int regno)
0280a90a
AS
243{
244 elf_greg_t *regp = (elf_greg_t *) gregsetp;
245 int i;
246
247 for (i = 0; i < NUM_GREGS; i++)
8de307e0 248 if (regno == -1 || regno == i)
7f7fe91e 249 regcache_raw_collect (regcache, i, regp + regmap[i]);
0280a90a
AS
250}
251
252#ifdef HAVE_PTRACE_GETREGS
253
254/* Fetch all general-purpose registers from process/thread TID and
255 store their values in GDB's register array. */
256
257static void
56be3814 258fetch_regs (struct regcache *regcache, int tid)
0280a90a
AS
259{
260 elf_gregset_t regs;
261
262 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
263 {
264 if (errno == EIO)
265 {
266 /* The kernel we're running on doesn't support the GETREGS
267 request. Reset `have_ptrace_getregs'. */
268 have_ptrace_getregs = 0;
269 return;
270 }
271
e2e0b3e5 272 perror_with_name (_("Couldn't get registers"));
0280a90a
AS
273 }
274
56be3814 275 supply_gregset (regcache, (const elf_gregset_t *) &regs);
c906108c
SS
276}
277
0280a90a
AS
278/* Store all valid general-purpose registers in GDB's register array
279 into the process/thread specified by TID. */
280
281static void
56be3814 282store_regs (const struct regcache *regcache, int tid, int regno)
0280a90a
AS
283{
284 elf_gregset_t regs;
285
286 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 287 perror_with_name (_("Couldn't get registers"));
0280a90a 288
56be3814 289 fill_gregset (regcache, &regs, regno);
0280a90a
AS
290
291 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 292 perror_with_name (_("Couldn't write registers"));
0280a90a
AS
293}
294
295#else
296
025bb325
MS
297static void fetch_regs (struct regcache *regcache, int tid)
298{
299}
300
301static void store_regs (const struct regcache *regcache, int tid, int regno)
302{
303}
0280a90a
AS
304
305#endif
306
307\f
308/* Transfering floating-point registers between GDB, inferiors and cores. */
309
310/* What is the address of fpN within the floating-point register set F? */
7f7fe91e 311#define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
0280a90a
AS
312
313/* Fill GDB's register array with the floating-point register values in
314 *FPREGSETP. */
c906108c 315
c5aa993b 316void
7f7fe91e 317supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
c906108c 318{
c984b7ff 319 struct gdbarch *gdbarch = get_regcache_arch (regcache);
c906108c
SS
320 int regi;
321
c984b7ff
UW
322 for (regi = gdbarch_fp0_regnum (gdbarch);
323 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
7f7fe91e 324 regcache_raw_supply (regcache, regi,
3e8c568d 325 FPREG_ADDR (fpregsetp,
c984b7ff 326 regi - gdbarch_fp0_regnum (gdbarch)));
7f7fe91e
UW
327 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
328 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
329 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
c906108c
SS
330}
331
0280a90a
AS
332/* Fill register REGNO (if it is a floating-point register) in
333 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
334 do this for all registers. */
335
336void
7f7fe91e
UW
337fill_fpregset (const struct regcache *regcache,
338 elf_fpregset_t *fpregsetp, int regno)
0280a90a 339{
c984b7ff 340 struct gdbarch *gdbarch = get_regcache_arch (regcache);
0280a90a
AS
341 int i;
342
343 /* Fill in the floating-point registers. */
c984b7ff
UW
344 for (i = gdbarch_fp0_regnum (gdbarch);
345 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
0280a90a 346 if (regno == -1 || regno == i)
7f7fe91e 347 regcache_raw_collect (regcache, i,
3e8c568d 348 FPREG_ADDR (fpregsetp,
c984b7ff 349 i - gdbarch_fp0_regnum (gdbarch)));
0280a90a
AS
350
351 /* Fill in the floating-point control registers. */
32eeb91a 352 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
0280a90a 353 if (regno == -1 || regno == i)
7f7fe91e
UW
354 regcache_raw_collect (regcache, i,
355 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
0280a90a
AS
356}
357
358#ifdef HAVE_PTRACE_GETREGS
359
360/* Fetch all floating-point registers from process/thread TID and store
361 thier values in GDB's register array. */
362
363static void
56be3814 364fetch_fpregs (struct regcache *regcache, int tid)
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 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
0280a90a
AS
372}
373
374/* Store all valid floating-point registers in GDB's register array
375 into the process/thread specified by TID. */
376
377static void
56be3814 378store_fpregs (const struct regcache *regcache, int tid, int regno)
0280a90a
AS
379{
380 elf_fpregset_t fpregs;
381
382 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 383 perror_with_name (_("Couldn't get floating point status"));
0280a90a 384
56be3814 385 fill_fpregset (regcache, &fpregs, regno);
0280a90a
AS
386
387 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 388 perror_with_name (_("Couldn't write floating point status"));
0280a90a
AS
389}
390
391#else
392
025bb325
MS
393static void fetch_fpregs (struct regcache *regcache, int tid)
394{
395}
396
397static void store_fpregs (const struct regcache *regcache, int tid, int regno)
398{
399}
0280a90a 400
c906108c 401#endif
0280a90a
AS
402\f
403/* Transferring arbitrary registers between GDB and inferior. */
404
405/* Fetch register REGNO from the child process. If REGNO is -1, do
406 this for all registers (including the floating point and SSE
407 registers). */
408
10d6c8cd 409static void
28439f5e
PA
410m68k_linux_fetch_inferior_registers (struct target_ops *ops,
411 struct regcache *regcache, int regno)
0280a90a
AS
412{
413 int tid;
414
415 /* Use the old method of peeking around in `struct user' if the
416 GETREGS request isn't available. */
417 if (! have_ptrace_getregs)
418 {
56be3814 419 old_fetch_inferior_registers (regcache, regno);
0280a90a
AS
420 return;
421 }
422
a4b6fc86 423 /* GNU/Linux LWP ID's are process ID's. */
8de307e0
AS
424 tid = TIDGET (inferior_ptid);
425 if (tid == 0)
025bb325 426 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
f175af98 427
0280a90a
AS
428 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
429 transfers more registers in one system call, and we'll cache the
430 results. But remember that fetch_fpxregs can fail, and return
431 zero. */
432 if (regno == -1)
433 {
56be3814 434 fetch_regs (regcache, tid);
0280a90a
AS
435
436 /* The call above might reset `have_ptrace_getregs'. */
437 if (! have_ptrace_getregs)
438 {
56be3814 439 old_fetch_inferior_registers (regcache, -1);
0280a90a
AS
440 return;
441 }
442
56be3814 443 fetch_fpregs (regcache, tid);
0280a90a
AS
444 return;
445 }
446
447 if (getregs_supplies (regno))
448 {
56be3814 449 fetch_regs (regcache, tid);
0280a90a
AS
450 return;
451 }
452
453 if (getfpregs_supplies (regno))
454 {
56be3814 455 fetch_fpregs (regcache, tid);
0280a90a
AS
456 return;
457 }
458
459 internal_error (__FILE__, __LINE__,
e2e0b3e5 460 _("Got request for bad register number %d."), regno);
0280a90a
AS
461}
462
463/* Store register REGNO back into the child process. If REGNO is -1,
464 do this for all registers (including the floating point and SSE
465 registers). */
10d6c8cd 466static void
28439f5e
PA
467m68k_linux_store_inferior_registers (struct target_ops *ops,
468 struct regcache *regcache, int regno)
0280a90a
AS
469{
470 int tid;
471
472 /* Use the old method of poking around in `struct user' if the
473 SETREGS request isn't available. */
474 if (! have_ptrace_getregs)
475 {
56be3814 476 old_store_inferior_registers (regcache, regno);
0280a90a
AS
477 return;
478 }
479
a4b6fc86 480 /* GNU/Linux LWP ID's are process ID's. */
8de307e0
AS
481 tid = TIDGET (inferior_ptid);
482 if (tid == 0)
0280a90a
AS
483 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
484
485 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
486 transfers more registers in one system call. But remember that
487 store_fpregs can fail, and return zero. */
488 if (regno == -1)
489 {
56be3814
UW
490 store_regs (regcache, tid, regno);
491 store_fpregs (regcache, tid, regno);
0280a90a
AS
492 return;
493 }
494
495 if (getregs_supplies (regno))
496 {
56be3814 497 store_regs (regcache, tid, regno);
0280a90a
AS
498 return;
499 }
500
501 if (getfpregs_supplies (regno))
502 {
56be3814 503 store_fpregs (regcache, tid, regno);
0280a90a
AS
504 return;
505 }
506
507 internal_error (__FILE__, __LINE__,
e2e0b3e5 508 _("Got request to store bad register number %d."), regno);
0280a90a 509}
f175af98
DJ
510\f
511/* Interpreting register set info found in core files. */
512
513/* Provide registers to GDB from a core file.
514
515 (We can't use the generic version of this function in
516 core-regset.c, because we need to use elf_gregset_t instead of
517 gregset_t.)
518
519 CORE_REG_SECT points to an array of bytes, which are the contents
520 of a `note' from a core file which BFD thinks might contain
521 register contents. CORE_REG_SIZE is its size.
522
523 WHICH says which register set corelow suspects this is:
524 0 --- the general-purpose register set, in elf_gregset_t format
525 2 --- the floating-point register set, in elf_fpregset_t format
526
a4b6fc86 527 REG_ADDR isn't used on GNU/Linux. */
f175af98
DJ
528
529static void
9eefc95f
UW
530fetch_core_registers (struct regcache *regcache,
531 char *core_reg_sect, unsigned core_reg_size,
f175af98
DJ
532 int which, CORE_ADDR reg_addr)
533{
534 elf_gregset_t gregset;
535 elf_fpregset_t fpregset;
536
537 switch (which)
538 {
539 case 0:
540 if (core_reg_size != sizeof (gregset))
8a3fe4f8 541 warning (_("Wrong size gregset in core file."));
f175af98
DJ
542 else
543 {
544 memcpy (&gregset, core_reg_sect, sizeof (gregset));
9eefc95f 545 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
f175af98
DJ
546 }
547 break;
548
549 case 2:
550 if (core_reg_size != sizeof (fpregset))
8a3fe4f8 551 warning (_("Wrong size fpregset in core file."));
f175af98
DJ
552 else
553 {
554 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
9eefc95f 555 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
f175af98
DJ
556 }
557 break;
558
559 default:
560 /* We've covered all the kinds of registers we know about here,
561 so this must be something we wouldn't know what to do with
562 anyway. Just ignore it. */
563 break;
564 }
565}
c906108c 566\f
c5aa993b 567
7b8b6d6d
AS
568/* Fetch the thread-local storage pointer for libthread_db. */
569
570ps_err_e
571ps_get_thread_area (const struct ps_prochandle *ph,
572 lwpid_t lwpid, int idx, void **base)
573{
574 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
575 return PS_ERR;
576
577 /* IDX is the bias from the thread pointer to the beginning of the
578 thread descriptor. It has to be subtracted due to implementation
579 quirks in libthread_db. */
580 *base = (char *) *base - idx;
581
582 return PS_OK;
583}
584\f
585
a4b6fc86
AC
586/* Register that we are able to handle GNU/Linux ELF core file
587 formats. */
f175af98
DJ
588
589static struct core_fns linux_elf_core_fns =
590{
591 bfd_target_elf_flavour, /* core_flavour */
592 default_check_format, /* check_format */
593 default_core_sniffer, /* core_sniffer */
594 fetch_core_registers, /* core_read_registers */
595 NULL /* next */
596};
597
10d6c8cd
DJ
598void _initialize_m68k_linux_nat (void);
599
f175af98 600void
5ae5f592 601_initialize_m68k_linux_nat (void)
f175af98 602{
10d6c8cd
DJ
603 struct target_ops *t;
604
605 /* Fill in the generic GNU/Linux methods. */
606 t = linux_target ();
607
608 /* Add our register access methods. */
609 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
610 t->to_store_registers = m68k_linux_store_inferior_registers;
611
612 /* Register the target. */
f973ed9c 613 linux_nat_add_target (t);
10d6c8cd 614
00e32a35 615 deprecated_add_core_fns (&linux_elf_core_fns);
f175af98 616}
This page took 1.064785 seconds and 4 git commands to generate.