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