* gdb.asm/m68k.inc: New file.
[deliverable/binutils-gdb.git] / gdb / m68klinux-nat.c
CommitLineData
a4b6fc86
AC
1/* Motorola m68k native support for GNU/Linux.
2
3 Copyright 1996, 1998, 2000, 2001, 2002 Free Software Foundation,
4 Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "frame.h"
25#include "inferior.h"
26#include "language.h"
27#include "gdbcore.h"
32eeb91a 28#include "gdb_string.h"
4e052eda 29#include "regcache.h"
c906108c 30
32eeb91a
AS
31#include "m68k-tdep.h"
32
c906108c
SS
33#ifdef USG
34#include <sys/types.h>
35#endif
36
37#include <sys/param.h>
38#include <sys/dir.h>
39#include <signal.h>
0280a90a 40#include <sys/ptrace.h>
c906108c
SS
41#include <sys/user.h>
42#include <sys/ioctl.h>
43#include <fcntl.h>
44#include <sys/procfs.h>
45
0280a90a
AS
46#ifdef HAVE_SYS_REG_H
47#include <sys/reg.h>
48#endif
49
c906108c
SS
50#include <sys/file.h>
51#include "gdb_stat.h"
52
53#include "floatformat.h"
54
55#include "target.h"
c906108c
SS
56\f
57/* This table must line up with REGISTER_NAMES in tm-m68k.h */
c5aa993b 58static const int regmap[] =
c906108c
SS
59{
60 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
61 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
62 PT_SR, PT_PC,
63 /* PT_FP0, ..., PT_FP7 */
64 21, 24, 27, 30, 33, 36, 39, 42,
65 /* PT_FPCR, PT_FPSR, PT_FPIAR */
66 45, 46, 47
67};
68
0280a90a
AS
69/* Which ptrace request retrieves which registers?
70 These apply to the corresponding SET requests as well. */
71#define NUM_GREGS (18)
72#define MAX_NUM_REGS (NUM_GREGS + 11)
73
74int
75getregs_supplies (int regno)
76{
77 return 0 <= regno && regno < NUM_GREGS;
78}
79
80int
81getfpregs_supplies (int regno)
82{
32eeb91a 83 return FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
0280a90a
AS
84}
85
86/* Does the current host support the GETREGS request? */
87int have_ptrace_getregs =
88#ifdef HAVE_PTRACE_GETREGS
89 1
90#else
91 0
92#endif
93;
94
95\f
96
c906108c
SS
97/* BLOCKEND is the value of u.u_ar0, and points to the place where GS
98 is stored. */
99
100int
fba45db2 101m68k_linux_register_u_addr (int blockend, int regnum)
c906108c 102{
c5aa993b 103 return (blockend + 4 * regmap[regnum]);
c906108c 104}
0280a90a
AS
105\f
106
107/* Fetching registers directly from the U area, one at a time. */
108
109/* FIXME: This duplicates code from `inptrace.c'. The problem is that we
110 define FETCH_INFERIOR_REGISTERS since we want to use our own versions
111 of {fetch,store}_inferior_registers that use the GETREGS request. This
112 means that the code in `infptrace.c' is #ifdef'd out. But we need to
113 fall back on that code when GDB is running on top of a kernel that
114 doesn't support the GETREGS request. */
115
116#ifndef PT_READ_U
117#define PT_READ_U PTRACE_PEEKUSR
118#endif
119#ifndef PT_WRITE_U
120#define PT_WRITE_U PTRACE_POKEUSR
121#endif
122
123/* Default the type of the ptrace transfer to int. */
124#ifndef PTRACE_XFER_TYPE
125#define PTRACE_XFER_TYPE int
126#endif
127
128/* Fetch one register. */
129
130static void
131fetch_register (int regno)
132{
133 /* This isn't really an address. But ptrace thinks of it as one. */
134 CORE_ADDR regaddr;
135 char mess[128]; /* For messages */
136 register int i;
137 unsigned int offset; /* Offset of registers within the u area. */
123a958e 138 char buf[MAX_REGISTER_SIZE];
0280a90a
AS
139 int tid;
140
141 if (CANNOT_FETCH_REGISTER (regno))
142 {
143 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
144 supply_register (regno, buf);
145 return;
146 }
147
148 /* Overload thread id onto process id */
149 if ((tid = TIDGET (inferior_ptid)) == 0)
150 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
151
152 offset = U_REGS_OFFSET;
153
154 regaddr = register_addr (regno, offset);
155 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
156 {
157 errno = 0;
158 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
159 (PTRACE_ARG3_TYPE) regaddr, 0);
160 regaddr += sizeof (PTRACE_XFER_TYPE);
161 if (errno != 0)
162 {
163 sprintf (mess, "reading register %s (#%d)",
164 REGISTER_NAME (regno), regno);
165 perror_with_name (mess);
166 }
167 }
168 supply_register (regno, buf);
169}
170
171/* Fetch register values from the inferior.
172 If REGNO is negative, do this for all registers.
173 Otherwise, REGNO specifies which register (so we can save time). */
c906108c 174
0280a90a
AS
175void
176old_fetch_inferior_registers (int regno)
177{
178 if (regno >= 0)
179 {
180 fetch_register (regno);
181 }
182 else
183 {
184 for (regno = 0; regno < NUM_REGS; regno++)
185 {
186 fetch_register (regno);
187 }
188 }
189}
190
191/* Store one register. */
192
193static void
194store_register (int regno)
195{
196 /* This isn't really an address. But ptrace thinks of it as one. */
197 CORE_ADDR regaddr;
198 char mess[128]; /* For messages */
199 register int i;
200 unsigned int offset; /* Offset of registers within the u area. */
201 int tid;
d9d9c31f 202 char buf[MAX_REGISTER_SIZE];
0280a90a
AS
203
204 if (CANNOT_STORE_REGISTER (regno))
205 {
206 return;
207 }
208
209 /* Overload thread id onto process id */
210 if ((tid = TIDGET (inferior_ptid)) == 0)
211 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
212
213 offset = U_REGS_OFFSET;
214
215 regaddr = register_addr (regno, offset);
9852326a
AS
216
217 /* Put the contents of regno into a local buffer */
218 regcache_collect (regno, buf);
219
220 /* Store the local buffer into the inferior a chunk at the time. */
0280a90a
AS
221 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
222 {
223 errno = 0;
224 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
9852326a 225 *(PTRACE_XFER_TYPE *) (buf + i));
0280a90a
AS
226 regaddr += sizeof (PTRACE_XFER_TYPE);
227 if (errno != 0)
228 {
229 sprintf (mess, "writing register %s (#%d)",
230 REGISTER_NAME (regno), regno);
231 perror_with_name (mess);
232 }
233 }
234}
235
236/* Store our register values back into the inferior.
237 If REGNO is negative, do this for all registers.
238 Otherwise, REGNO specifies which register (so we can save time). */
239
240void
241old_store_inferior_registers (int regno)
242{
243 if (regno >= 0)
244 {
245 store_register (regno);
246 }
247 else
248 {
249 for (regno = 0; regno < NUM_REGS; regno++)
250 {
251 store_register (regno);
252 }
253 }
254}
255\f
f175af98
DJ
256/* Given a pointer to a general register set in /proc format
257 (elf_gregset_t *), unpack the register contents and supply
258 them as gdb's idea of the current register values. */
c906108c
SS
259
260
261/* Note both m68k-tdep.c and m68klinux-nat.c contain definitions
262 for supply_gregset and supply_fpregset. The definitions
263 in m68k-tdep.c are valid if USE_PROC_FS is defined. Otherwise,
264 the definitions in m68klinux-nat.c will be used. This is a
265 bit of a hack. The supply_* routines do not belong in
266 *_tdep.c files. But, there are several lynx ports that currently
c5aa993b 267 depend on these definitions. */
c906108c
SS
268
269#ifndef USE_PROC_FS
270
c60c0f5f
MS
271/* Prototypes for supply_gregset etc. */
272#include "gregset.h"
273
c906108c 274void
f175af98 275supply_gregset (elf_gregset_t *gregsetp)
c906108c 276{
0280a90a 277 elf_greg_t *regp = (elf_greg_t *) gregsetp;
c906108c
SS
278 int regi;
279
32eeb91a 280 for (regi = M68K_D0_REGNUM; regi <= SP_REGNUM; regi++)
0280a90a
AS
281 supply_register (regi, (char *) &regp[regmap[regi]]);
282 supply_register (PS_REGNUM, (char *) &regp[PT_SR]);
283 supply_register (PC_REGNUM, (char *) &regp[PT_PC]);
284}
285
286/* Fill register REGNO (if it is a general-purpose register) in
287 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
288 do this for all registers. */
289void
290fill_gregset (elf_gregset_t *gregsetp, int regno)
291{
292 elf_greg_t *regp = (elf_greg_t *) gregsetp;
293 int i;
294
295 for (i = 0; i < NUM_GREGS; i++)
296 if ((regno == -1 || regno == i))
297 regcache_collect (i, regp + regmap[i]);
298}
299
300#ifdef HAVE_PTRACE_GETREGS
301
302/* Fetch all general-purpose registers from process/thread TID and
303 store their values in GDB's register array. */
304
305static void
306fetch_regs (int tid)
307{
308 elf_gregset_t regs;
309
310 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
311 {
312 if (errno == EIO)
313 {
314 /* The kernel we're running on doesn't support the GETREGS
315 request. Reset `have_ptrace_getregs'. */
316 have_ptrace_getregs = 0;
317 return;
318 }
319
320 perror_with_name ("Couldn't get registers");
321 }
322
323 supply_gregset (&regs);
c906108c
SS
324}
325
0280a90a
AS
326/* Store all valid general-purpose registers in GDB's register array
327 into the process/thread specified by TID. */
328
329static void
330store_regs (int tid, int regno)
331{
332 elf_gregset_t regs;
333
334 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
335 perror_with_name ("Couldn't get registers");
336
337 fill_gregset (&regs, regno);
338
339 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
340 perror_with_name ("Couldn't write registers");
341}
342
343#else
344
345static void fetch_regs (int tid) {}
346static void store_regs (int tid, int regno) {}
347
348#endif
349
350\f
351/* Transfering floating-point registers between GDB, inferiors and cores. */
352
353/* What is the address of fpN within the floating-point register set F? */
354#define FPREG_ADDR(f, n) ((char *) &(f)->fpregs[(n) * 3])
355
356/* Fill GDB's register array with the floating-point register values in
357 *FPREGSETP. */
c906108c 358
c5aa993b 359void
f175af98 360supply_fpregset (elf_fpregset_t *fpregsetp)
c906108c
SS
361{
362 int regi;
363
32eeb91a 364 for (regi = FP0_REGNUM; regi < FP0_REGNUM + 8; regi++)
0280a90a 365 supply_register (regi, FPREG_ADDR (fpregsetp, regi - FP0_REGNUM));
32eeb91a
AS
366 supply_register (M68K_FPC_REGNUM, (char *) &fpregsetp->fpcntl[0]);
367 supply_register (M68K_FPS_REGNUM, (char *) &fpregsetp->fpcntl[1]);
368 supply_register (M68K_FPI_REGNUM, (char *) &fpregsetp->fpcntl[2]);
c906108c
SS
369}
370
0280a90a
AS
371/* Fill register REGNO (if it is a floating-point register) in
372 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
373 do this for all registers. */
374
375void
376fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
377{
378 int i;
379
380 /* Fill in the floating-point registers. */
381 for (i = FP0_REGNUM; i < FP0_REGNUM + 8; i++)
382 if (regno == -1 || regno == i)
a731b831 383 regcache_collect (i, FPREG_ADDR (fpregsetp, i - FP0_REGNUM));
0280a90a
AS
384
385 /* Fill in the floating-point control registers. */
32eeb91a 386 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
0280a90a 387 if (regno == -1 || regno == i)
a731b831 388 regcache_collect (i, (char *) &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
0280a90a
AS
389}
390
391#ifdef HAVE_PTRACE_GETREGS
392
393/* Fetch all floating-point registers from process/thread TID and store
394 thier values in GDB's register array. */
395
396static void
397fetch_fpregs (int tid)
398{
399 elf_fpregset_t fpregs;
400
401 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
402 perror_with_name ("Couldn't get floating point status");
403
404 supply_fpregset (&fpregs);
405}
406
407/* Store all valid floating-point registers in GDB's register array
408 into the process/thread specified by TID. */
409
410static void
411store_fpregs (int tid, int regno)
412{
413 elf_fpregset_t fpregs;
414
415 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
416 perror_with_name ("Couldn't get floating point status");
417
418 fill_fpregset (&fpregs, regno);
419
420 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
421 perror_with_name ("Couldn't write floating point status");
422}
423
424#else
425
426static void fetch_fpregs (int tid) {}
427static void store_fpregs (int tid, int regno) {}
428
429#endif
430
c906108c 431#endif
0280a90a
AS
432\f
433/* Transferring arbitrary registers between GDB and inferior. */
434
435/* Fetch register REGNO from the child process. If REGNO is -1, do
436 this for all registers (including the floating point and SSE
437 registers). */
438
439void
440fetch_inferior_registers (int regno)
441{
442 int tid;
443
444 /* Use the old method of peeking around in `struct user' if the
445 GETREGS request isn't available. */
446 if (! have_ptrace_getregs)
447 {
448 old_fetch_inferior_registers (regno);
449 return;
450 }
451
a4b6fc86 452 /* GNU/Linux LWP ID's are process ID's. */
0280a90a
AS
453 if ((tid = TIDGET (inferior_ptid)) == 0)
454 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
f175af98 455
0280a90a
AS
456 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
457 transfers more registers in one system call, and we'll cache the
458 results. But remember that fetch_fpxregs can fail, and return
459 zero. */
460 if (regno == -1)
461 {
462 fetch_regs (tid);
463
464 /* The call above might reset `have_ptrace_getregs'. */
465 if (! have_ptrace_getregs)
466 {
467 old_fetch_inferior_registers (-1);
468 return;
469 }
470
471 fetch_fpregs (tid);
472 return;
473 }
474
475 if (getregs_supplies (regno))
476 {
477 fetch_regs (tid);
478 return;
479 }
480
481 if (getfpregs_supplies (regno))
482 {
483 fetch_fpregs (tid);
484 return;
485 }
486
487 internal_error (__FILE__, __LINE__,
488 "Got request for bad register number %d.", regno);
489}
490
491/* Store register REGNO back into the child process. If REGNO is -1,
492 do this for all registers (including the floating point and SSE
493 registers). */
494void
495store_inferior_registers (int regno)
496{
497 int tid;
498
499 /* Use the old method of poking around in `struct user' if the
500 SETREGS request isn't available. */
501 if (! have_ptrace_getregs)
502 {
503 old_store_inferior_registers (regno);
504 return;
505 }
506
a4b6fc86 507 /* GNU/Linux LWP ID's are process ID's. */
0280a90a
AS
508 if ((tid = TIDGET (inferior_ptid)) == 0)
509 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
510
511 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
512 transfers more registers in one system call. But remember that
513 store_fpregs can fail, and return zero. */
514 if (regno == -1)
515 {
516 store_regs (tid, regno);
517 store_fpregs (tid, regno);
518 return;
519 }
520
521 if (getregs_supplies (regno))
522 {
523 store_regs (tid, regno);
524 return;
525 }
526
527 if (getfpregs_supplies (regno))
528 {
529 store_fpregs (tid, regno);
530 return;
531 }
532
533 internal_error (__FILE__, __LINE__,
534 "Got request to store bad register number %d.", regno);
535}
f175af98
DJ
536\f
537/* Interpreting register set info found in core files. */
538
539/* Provide registers to GDB from a core file.
540
541 (We can't use the generic version of this function in
542 core-regset.c, because we need to use elf_gregset_t instead of
543 gregset_t.)
544
545 CORE_REG_SECT points to an array of bytes, which are the contents
546 of a `note' from a core file which BFD thinks might contain
547 register contents. CORE_REG_SIZE is its size.
548
549 WHICH says which register set corelow suspects this is:
550 0 --- the general-purpose register set, in elf_gregset_t format
551 2 --- the floating-point register set, in elf_fpregset_t format
552
a4b6fc86 553 REG_ADDR isn't used on GNU/Linux. */
f175af98
DJ
554
555static void
556fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
557 int which, CORE_ADDR reg_addr)
558{
559 elf_gregset_t gregset;
560 elf_fpregset_t fpregset;
561
562 switch (which)
563 {
564 case 0:
565 if (core_reg_size != sizeof (gregset))
566 warning ("Wrong size gregset in core file.");
567 else
568 {
569 memcpy (&gregset, core_reg_sect, sizeof (gregset));
570 supply_gregset (&gregset);
571 }
572 break;
573
574 case 2:
575 if (core_reg_size != sizeof (fpregset))
576 warning ("Wrong size fpregset in core file.");
577 else
578 {
579 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
580 supply_fpregset (&fpregset);
581 }
582 break;
583
584 default:
585 /* We've covered all the kinds of registers we know about here,
586 so this must be something we wouldn't know what to do with
587 anyway. Just ignore it. */
588 break;
589 }
590}
c906108c 591\f
c5aa993b 592
c906108c 593int
fba45db2 594kernel_u_size (void)
c906108c
SS
595{
596 return (sizeof (struct user));
597}
598\f
a4b6fc86
AC
599/* Register that we are able to handle GNU/Linux ELF core file
600 formats. */
f175af98
DJ
601
602static struct core_fns linux_elf_core_fns =
603{
604 bfd_target_elf_flavour, /* core_flavour */
605 default_check_format, /* check_format */
606 default_core_sniffer, /* core_sniffer */
607 fetch_core_registers, /* core_read_registers */
608 NULL /* next */
609};
610
611void
5ae5f592 612_initialize_m68k_linux_nat (void)
f175af98
DJ
613{
614 add_core_fns (&linux_elf_core_fns);
615}
This page took 0.386204 seconds and 4 git commands to generate.