sim: add bugzilla marking
[deliverable/binutils-gdb.git] / gdb / m68klinux-nat.c
CommitLineData
a4b6fc86
AC
1/* Motorola m68k native support for GNU/Linux.
2
0b302171 3 Copyright (C) 1996, 1998, 2000-2012 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);
335d71d6 110 long regaddr;
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;
335d71d6
AS
125 *(long *) &buf[i] = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
126 regaddr += sizeof (long);
0280a90a 127 if (errno != 0)
335d71d6
AS
128 error (_("Couldn't read register %s (#%d): %s."),
129 gdbarch_register_name (gdbarch, regno),
130 regno, safe_strerror (errno));
0280a90a 131 }
56be3814 132 regcache_raw_supply (regcache, regno, buf);
0280a90a
AS
133}
134
135/* Fetch register values from the inferior.
136 If REGNO is negative, do this for all registers.
025bb325 137 Otherwise, REGNO specifies which register (so we can save time). */
c906108c 138
10d6c8cd 139static void
56be3814 140old_fetch_inferior_registers (struct regcache *regcache, int regno)
0280a90a
AS
141{
142 if (regno >= 0)
143 {
56be3814 144 fetch_register (regcache, regno);
0280a90a
AS
145 }
146 else
147 {
c984b7ff
UW
148 for (regno = 0;
149 regno < gdbarch_num_regs (get_regcache_arch (regcache));
150 regno++)
0280a90a 151 {
56be3814 152 fetch_register (regcache, regno);
0280a90a
AS
153 }
154 }
155}
156
025bb325 157/* Store one register. */
0280a90a
AS
158
159static void
56be3814 160store_register (const struct regcache *regcache, int regno)
0280a90a 161{
f36bf22c 162 struct gdbarch *gdbarch = get_regcache_arch (regcache);
335d71d6 163 long regaddr;
52f0bd74 164 int i;
0280a90a 165 int tid;
d9d9c31f 166 char buf[MAX_REGISTER_SIZE];
0280a90a 167
025bb325 168 /* Overload thread id onto process id. */
8de307e0
AS
169 tid = TIDGET (inferior_ptid);
170 if (tid == 0)
025bb325
MS
171 tid = PIDGET (inferior_ptid); /* no thread id, just use
172 process id. */
0280a90a 173
de732108 174 regaddr = 4 * regmap[regno];
9852326a 175
025bb325 176 /* Put the contents of regno into a local buffer. */
56be3814 177 regcache_raw_collect (regcache, regno, buf);
9852326a 178
025bb325 179 /* Store the local buffer into the inferior a chunk at the time. */
335d71d6 180 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
0280a90a
AS
181 {
182 errno = 0;
335d71d6
AS
183 ptrace (PTRACE_POKEUSER, tid, regaddr, *(long *) &buf[i]);
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
UW
205 for (regno = 0;
206 regno < gdbarch_num_regs (get_regcache_arch (regcache));
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{
c984b7ff 221 struct gdbarch *gdbarch = get_regcache_arch (regcache);
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++)
7f7fe91e 228 regcache_raw_supply (regcache, regi, &regp[regmap[regi]]);
c984b7ff 229 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
3e8c568d
UW
230 &regp[PT_SR]);
231 regcache_raw_supply (regcache,
c984b7ff 232 gdbarch_pc_regnum (gdbarch), &regp[PT_PC]);
0280a90a
AS
233}
234
235/* Fill register REGNO (if it is a general-purpose register) in
236 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
237 do this for all registers. */
238void
7f7fe91e
UW
239fill_gregset (const struct regcache *regcache,
240 elf_gregset_t *gregsetp, int regno)
0280a90a
AS
241{
242 elf_greg_t *regp = (elf_greg_t *) gregsetp;
243 int i;
244
245 for (i = 0; i < NUM_GREGS; i++)
8de307e0 246 if (regno == -1 || regno == i)
7f7fe91e 247 regcache_raw_collect (regcache, i, regp + regmap[i]);
0280a90a
AS
248}
249
250#ifdef HAVE_PTRACE_GETREGS
251
252/* Fetch all general-purpose registers from process/thread TID and
253 store their values in GDB's register array. */
254
255static void
56be3814 256fetch_regs (struct regcache *regcache, int tid)
0280a90a
AS
257{
258 elf_gregset_t regs;
259
260 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
261 {
262 if (errno == EIO)
263 {
264 /* The kernel we're running on doesn't support the GETREGS
265 request. Reset `have_ptrace_getregs'. */
266 have_ptrace_getregs = 0;
267 return;
268 }
269
e2e0b3e5 270 perror_with_name (_("Couldn't get registers"));
0280a90a
AS
271 }
272
56be3814 273 supply_gregset (regcache, (const elf_gregset_t *) &regs);
c906108c
SS
274}
275
0280a90a
AS
276/* Store all valid general-purpose registers in GDB's register array
277 into the process/thread specified by TID. */
278
279static void
56be3814 280store_regs (const struct regcache *regcache, int tid, int regno)
0280a90a
AS
281{
282 elf_gregset_t regs;
283
284 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 285 perror_with_name (_("Couldn't get registers"));
0280a90a 286
56be3814 287 fill_gregset (regcache, &regs, regno);
0280a90a
AS
288
289 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 290 perror_with_name (_("Couldn't write registers"));
0280a90a
AS
291}
292
293#else
294
025bb325
MS
295static void fetch_regs (struct regcache *regcache, int tid)
296{
297}
298
299static void store_regs (const struct regcache *regcache, int tid, int regno)
300{
301}
0280a90a
AS
302
303#endif
304
305\f
306/* Transfering floating-point registers between GDB, inferiors and cores. */
307
308/* What is the address of fpN within the floating-point register set F? */
7f7fe91e 309#define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
0280a90a
AS
310
311/* Fill GDB's register array with the floating-point register values in
312 *FPREGSETP. */
c906108c 313
c5aa993b 314void
7f7fe91e 315supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
c906108c 316{
c984b7ff 317 struct gdbarch *gdbarch = get_regcache_arch (regcache);
c906108c
SS
318 int regi;
319
c984b7ff
UW
320 for (regi = gdbarch_fp0_regnum (gdbarch);
321 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
7f7fe91e 322 regcache_raw_supply (regcache, regi,
3e8c568d 323 FPREG_ADDR (fpregsetp,
c984b7ff 324 regi - gdbarch_fp0_regnum (gdbarch)));
7f7fe91e
UW
325 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
326 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
327 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
c906108c
SS
328}
329
0280a90a
AS
330/* Fill register REGNO (if it is a floating-point register) in
331 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
332 do this for all registers. */
333
334void
7f7fe91e
UW
335fill_fpregset (const struct regcache *regcache,
336 elf_fpregset_t *fpregsetp, int regno)
0280a90a 337{
c984b7ff 338 struct gdbarch *gdbarch = get_regcache_arch (regcache);
0280a90a
AS
339 int i;
340
341 /* Fill in the floating-point registers. */
c984b7ff
UW
342 for (i = gdbarch_fp0_regnum (gdbarch);
343 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
0280a90a 344 if (regno == -1 || regno == i)
7f7fe91e 345 regcache_raw_collect (regcache, i,
3e8c568d 346 FPREG_ADDR (fpregsetp,
c984b7ff 347 i - gdbarch_fp0_regnum (gdbarch)));
0280a90a
AS
348
349 /* Fill in the floating-point control registers. */
32eeb91a 350 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
0280a90a 351 if (regno == -1 || regno == i)
7f7fe91e
UW
352 regcache_raw_collect (regcache, i,
353 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
0280a90a
AS
354}
355
356#ifdef HAVE_PTRACE_GETREGS
357
358/* Fetch all floating-point registers from process/thread TID and store
359 thier values in GDB's register array. */
360
361static void
56be3814 362fetch_fpregs (struct regcache *regcache, int tid)
0280a90a
AS
363{
364 elf_fpregset_t fpregs;
365
366 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 367 perror_with_name (_("Couldn't get floating point status"));
0280a90a 368
56be3814 369 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
0280a90a
AS
370}
371
372/* Store all valid floating-point registers in GDB's register array
373 into the process/thread specified by TID. */
374
375static void
56be3814 376store_fpregs (const struct regcache *regcache, int tid, int regno)
0280a90a
AS
377{
378 elf_fpregset_t fpregs;
379
380 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 381 perror_with_name (_("Couldn't get floating point status"));
0280a90a 382
56be3814 383 fill_fpregset (regcache, &fpregs, regno);
0280a90a
AS
384
385 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 386 perror_with_name (_("Couldn't write floating point status"));
0280a90a
AS
387}
388
389#else
390
025bb325
MS
391static void fetch_fpregs (struct regcache *regcache, int tid)
392{
393}
394
395static void store_fpregs (const struct regcache *regcache, int tid, int regno)
396{
397}
0280a90a 398
c906108c 399#endif
0280a90a
AS
400\f
401/* Transferring arbitrary registers between GDB and inferior. */
402
403/* Fetch register REGNO from the child process. If REGNO is -1, do
404 this for all registers (including the floating point and SSE
405 registers). */
406
10d6c8cd 407static void
28439f5e
PA
408m68k_linux_fetch_inferior_registers (struct target_ops *ops,
409 struct regcache *regcache, int regno)
0280a90a
AS
410{
411 int tid;
412
413 /* Use the old method of peeking around in `struct user' if the
414 GETREGS request isn't available. */
415 if (! have_ptrace_getregs)
416 {
56be3814 417 old_fetch_inferior_registers (regcache, regno);
0280a90a
AS
418 return;
419 }
420
a4b6fc86 421 /* GNU/Linux LWP ID's are process ID's. */
8de307e0
AS
422 tid = TIDGET (inferior_ptid);
423 if (tid == 0)
025bb325 424 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
f175af98 425
0280a90a
AS
426 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
427 transfers more registers in one system call, and we'll cache the
428 results. But remember that fetch_fpxregs can fail, and return
429 zero. */
430 if (regno == -1)
431 {
56be3814 432 fetch_regs (regcache, tid);
0280a90a
AS
433
434 /* The call above might reset `have_ptrace_getregs'. */
435 if (! have_ptrace_getregs)
436 {
56be3814 437 old_fetch_inferior_registers (regcache, -1);
0280a90a
AS
438 return;
439 }
440
56be3814 441 fetch_fpregs (regcache, tid);
0280a90a
AS
442 return;
443 }
444
445 if (getregs_supplies (regno))
446 {
56be3814 447 fetch_regs (regcache, tid);
0280a90a
AS
448 return;
449 }
450
451 if (getfpregs_supplies (regno))
452 {
56be3814 453 fetch_fpregs (regcache, tid);
0280a90a
AS
454 return;
455 }
456
457 internal_error (__FILE__, __LINE__,
e2e0b3e5 458 _("Got request for bad register number %d."), regno);
0280a90a
AS
459}
460
461/* Store register REGNO back into the child process. If REGNO is -1,
462 do this for all registers (including the floating point and SSE
463 registers). */
10d6c8cd 464static void
28439f5e
PA
465m68k_linux_store_inferior_registers (struct target_ops *ops,
466 struct regcache *regcache, int regno)
0280a90a
AS
467{
468 int tid;
469
470 /* Use the old method of poking around in `struct user' if the
471 SETREGS request isn't available. */
472 if (! have_ptrace_getregs)
473 {
56be3814 474 old_store_inferior_registers (regcache, regno);
0280a90a
AS
475 return;
476 }
477
a4b6fc86 478 /* GNU/Linux LWP ID's are process ID's. */
8de307e0
AS
479 tid = TIDGET (inferior_ptid);
480 if (tid == 0)
0280a90a
AS
481 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
482
483 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
484 transfers more registers in one system call. But remember that
485 store_fpregs can fail, and return zero. */
486 if (regno == -1)
487 {
56be3814
UW
488 store_regs (regcache, tid, regno);
489 store_fpregs (regcache, tid, regno);
0280a90a
AS
490 return;
491 }
492
493 if (getregs_supplies (regno))
494 {
56be3814 495 store_regs (regcache, tid, regno);
0280a90a
AS
496 return;
497 }
498
499 if (getfpregs_supplies (regno))
500 {
56be3814 501 store_fpregs (regcache, tid, regno);
0280a90a
AS
502 return;
503 }
504
505 internal_error (__FILE__, __LINE__,
e2e0b3e5 506 _("Got request to store bad register number %d."), regno);
0280a90a 507}
f175af98
DJ
508\f
509/* Interpreting register set info found in core files. */
510
511/* Provide registers to GDB from a core file.
512
513 (We can't use the generic version of this function in
514 core-regset.c, because we need to use elf_gregset_t instead of
515 gregset_t.)
516
517 CORE_REG_SECT points to an array of bytes, which are the contents
518 of a `note' from a core file which BFD thinks might contain
519 register contents. CORE_REG_SIZE is its size.
520
521 WHICH says which register set corelow suspects this is:
522 0 --- the general-purpose register set, in elf_gregset_t format
523 2 --- the floating-point register set, in elf_fpregset_t format
524
a4b6fc86 525 REG_ADDR isn't used on GNU/Linux. */
f175af98
DJ
526
527static void
9eefc95f
UW
528fetch_core_registers (struct regcache *regcache,
529 char *core_reg_sect, unsigned core_reg_size,
f175af98
DJ
530 int which, CORE_ADDR reg_addr)
531{
532 elf_gregset_t gregset;
533 elf_fpregset_t fpregset;
534
535 switch (which)
536 {
537 case 0:
538 if (core_reg_size != sizeof (gregset))
8a3fe4f8 539 warning (_("Wrong size gregset in core file."));
f175af98
DJ
540 else
541 {
542 memcpy (&gregset, core_reg_sect, sizeof (gregset));
9eefc95f 543 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
f175af98
DJ
544 }
545 break;
546
547 case 2:
548 if (core_reg_size != sizeof (fpregset))
8a3fe4f8 549 warning (_("Wrong size fpregset in core file."));
f175af98
DJ
550 else
551 {
552 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
9eefc95f 553 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
f175af98
DJ
554 }
555 break;
556
557 default:
558 /* We've covered all the kinds of registers we know about here,
559 so this must be something we wouldn't know what to do with
560 anyway. Just ignore it. */
561 break;
562 }
563}
c906108c 564\f
c5aa993b 565
7b8b6d6d
AS
566/* Fetch the thread-local storage pointer for libthread_db. */
567
568ps_err_e
569ps_get_thread_area (const struct ps_prochandle *ph,
570 lwpid_t lwpid, int idx, void **base)
571{
572 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
573 return PS_ERR;
574
575 /* IDX is the bias from the thread pointer to the beginning of the
576 thread descriptor. It has to be subtracted due to implementation
577 quirks in libthread_db. */
578 *base = (char *) *base - idx;
579
580 return PS_OK;
581}
582\f
583
a4b6fc86
AC
584/* Register that we are able to handle GNU/Linux ELF core file
585 formats. */
f175af98
DJ
586
587static struct core_fns linux_elf_core_fns =
588{
589 bfd_target_elf_flavour, /* core_flavour */
590 default_check_format, /* check_format */
591 default_core_sniffer, /* core_sniffer */
592 fetch_core_registers, /* core_read_registers */
593 NULL /* next */
594};
595
10d6c8cd
DJ
596void _initialize_m68k_linux_nat (void);
597
f175af98 598void
5ae5f592 599_initialize_m68k_linux_nat (void)
f175af98 600{
10d6c8cd
DJ
601 struct target_ops *t;
602
603 /* Fill in the generic GNU/Linux methods. */
604 t = linux_target ();
605
606 /* Add our register access methods. */
607 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
608 t->to_store_registers = m68k_linux_store_inferior_registers;
609
610 /* Register the target. */
f973ed9c 611 linux_nat_add_target (t);
10d6c8cd 612
00e32a35 613 deprecated_add_core_fns (&linux_elf_core_fns);
f175af98 614}
This page took 1.141609 seconds and 4 git commands to generate.