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