* m68klinux-nat.c: Fix last change to use regcache_collect
[external/binutils.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   char *buf = alloca (MAX_REGISTER_RAW_SIZE);
199
200   if (CANNOT_STORE_REGISTER (regno))
201     {
202       return;
203     }
204
205   /* Overload thread id onto process id */
206   if ((tid = TIDGET (inferior_ptid)) == 0)
207     tid = PIDGET (inferior_ptid);       /* no thread id, just use process id */
208
209   offset = U_REGS_OFFSET;
210
211   regaddr = register_addr (regno, offset);
212
213   /* Put the contents of regno into a local buffer */
214   regcache_collect (regno, buf);
215
216   /* Store the local buffer into the inferior a chunk at the time. */
217   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
218     {
219       errno = 0;
220       ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
221               *(PTRACE_XFER_TYPE *) (buf + i));
222       regaddr += sizeof (PTRACE_XFER_TYPE);
223       if (errno != 0)
224         {
225           sprintf (mess, "writing register %s (#%d)", 
226                    REGISTER_NAME (regno), regno);
227           perror_with_name (mess);
228         }
229     }
230 }
231
232 /* Store our register values back into the inferior.
233    If REGNO is negative, do this for all registers.
234    Otherwise, REGNO specifies which register (so we can save time).  */
235
236 void
237 old_store_inferior_registers (int regno)
238 {
239   if (regno >= 0)
240     {
241       store_register (regno);
242     }
243   else
244     {
245       for (regno = 0; regno < NUM_REGS; regno++)
246         {
247           store_register (regno);
248         }
249     }
250 }
251 \f
252 /*  Given a pointer to a general register set in /proc format
253    (elf_gregset_t *), unpack the register contents and supply
254    them as gdb's idea of the current register values. */
255
256
257 /* Note both m68k-tdep.c and m68klinux-nat.c contain definitions
258    for supply_gregset and supply_fpregset. The definitions
259    in m68k-tdep.c are valid if USE_PROC_FS is defined. Otherwise,
260    the definitions in m68klinux-nat.c will be used. This is a 
261    bit of a hack. The supply_* routines do not belong in 
262    *_tdep.c files. But, there are several lynx ports that currently 
263    depend on these definitions. */
264
265 #ifndef USE_PROC_FS
266
267 /* Prototypes for supply_gregset etc. */
268 #include "gregset.h"
269
270 void
271 supply_gregset (elf_gregset_t *gregsetp)
272 {
273   elf_greg_t *regp = (elf_greg_t *) gregsetp;
274   int regi;
275
276   for (regi = D0_REGNUM; regi <= SP_REGNUM; regi++)
277     supply_register (regi, (char *) &regp[regmap[regi]]);
278   supply_register (PS_REGNUM, (char *) &regp[PT_SR]);
279   supply_register (PC_REGNUM, (char *) &regp[PT_PC]);
280 }
281
282 /* Fill register REGNO (if it is a general-purpose register) in
283    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
284    do this for all registers.  */
285 void
286 fill_gregset (elf_gregset_t *gregsetp, int regno)
287 {
288   elf_greg_t *regp = (elf_greg_t *) gregsetp;
289   int i;
290
291   for (i = 0; i < NUM_GREGS; i++)
292     if ((regno == -1 || regno == i))
293       regcache_collect (i, regp + regmap[i]);
294 }
295
296 #ifdef HAVE_PTRACE_GETREGS
297
298 /* Fetch all general-purpose registers from process/thread TID and
299    store their values in GDB's register array.  */
300
301 static void
302 fetch_regs (int tid)
303 {
304   elf_gregset_t regs;
305
306   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
307     {
308       if (errno == EIO)
309         {
310           /* The kernel we're running on doesn't support the GETREGS
311              request.  Reset `have_ptrace_getregs'.  */
312           have_ptrace_getregs = 0;
313           return;
314         }
315
316       perror_with_name ("Couldn't get registers");
317     }
318
319   supply_gregset (&regs);
320 }
321
322 /* Store all valid general-purpose registers in GDB's register array
323    into the process/thread specified by TID.  */
324
325 static void
326 store_regs (int tid, int regno)
327 {
328   elf_gregset_t regs;
329
330   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
331     perror_with_name ("Couldn't get registers");
332
333   fill_gregset (&regs, regno);
334
335   if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
336     perror_with_name ("Couldn't write registers");
337 }
338
339 #else
340
341 static void fetch_regs (int tid) {}
342 static void store_regs (int tid, int regno) {}
343
344 #endif
345
346 \f
347 /* Transfering floating-point registers between GDB, inferiors and cores.  */
348
349 /* What is the address of fpN within the floating-point register set F?  */
350 #define FPREG_ADDR(f, n) ((char *) &(f)->fpregs[(n) * 3])
351
352 /* Fill GDB's register array with the floating-point register values in
353    *FPREGSETP.  */
354
355 void
356 supply_fpregset (elf_fpregset_t *fpregsetp)
357 {
358   int regi;
359
360   for (regi = FP0_REGNUM; regi < FPC_REGNUM; regi++)
361     supply_register (regi, FPREG_ADDR (fpregsetp, regi - FP0_REGNUM));
362   supply_register (FPC_REGNUM, (char *) &fpregsetp->fpcntl[0]);
363   supply_register (FPS_REGNUM, (char *) &fpregsetp->fpcntl[1]);
364   supply_register (FPI_REGNUM, (char *) &fpregsetp->fpcntl[2]);
365 }
366
367 /* Fill register REGNO (if it is a floating-point register) in
368    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
369    do this for all registers.  */
370
371 void
372 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
373 {
374   int i;
375
376   /* Fill in the floating-point registers.  */
377   for (i = FP0_REGNUM; i < FP0_REGNUM + 8; i++)
378     if (regno == -1 || regno == i)
379       regcache_collect (regno, FPREG_ADDR (fpregsetp, regno - FP0_REGNUM));
380
381   /* Fill in the floating-point control registers.  */
382   for (i = FPC_REGNUM; i <= FPI_REGNUM; i++)
383     if (regno == -1 || regno == i)
384       regcache_collect (regno, fpregsetp->fpcntl[regno - FPC_REGNUM]);
385 }
386
387 #ifdef HAVE_PTRACE_GETREGS
388
389 /* Fetch all floating-point registers from process/thread TID and store
390    thier values in GDB's register array.  */
391
392 static void
393 fetch_fpregs (int tid)
394 {
395   elf_fpregset_t fpregs;
396
397   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
398     perror_with_name ("Couldn't get floating point status");
399
400   supply_fpregset (&fpregs);
401 }
402
403 /* Store all valid floating-point registers in GDB's register array
404    into the process/thread specified by TID.  */
405
406 static void
407 store_fpregs (int tid, int regno)
408 {
409   elf_fpregset_t fpregs;
410
411   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
412     perror_with_name ("Couldn't get floating point status");
413
414   fill_fpregset (&fpregs, regno);
415
416   if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
417     perror_with_name ("Couldn't write floating point status");
418 }
419
420 #else
421
422 static void fetch_fpregs (int tid) {}
423 static void store_fpregs (int tid, int regno) {}
424
425 #endif
426
427 #endif
428 \f
429 /* Transferring arbitrary registers between GDB and inferior.  */
430
431 /* Fetch register REGNO from the child process.  If REGNO is -1, do
432    this for all registers (including the floating point and SSE
433    registers).  */
434
435 void
436 fetch_inferior_registers (int regno)
437 {
438   int tid;
439
440   /* Use the old method of peeking around in `struct user' if the
441      GETREGS request isn't available.  */
442   if (! have_ptrace_getregs)
443     {
444       old_fetch_inferior_registers (regno);
445       return;
446     }
447
448   /* Linux LWP ID's are process ID's.  */
449   if ((tid = TIDGET (inferior_ptid)) == 0)
450     tid = PIDGET (inferior_ptid);               /* Not a threaded program.  */
451
452   /* Use the PTRACE_GETFPXREGS request whenever possible, since it
453      transfers more registers in one system call, and we'll cache the
454      results.  But remember that fetch_fpxregs can fail, and return
455      zero.  */
456   if (regno == -1)
457     {
458       fetch_regs (tid);
459
460       /* The call above might reset `have_ptrace_getregs'.  */
461       if (! have_ptrace_getregs)
462         {
463           old_fetch_inferior_registers (-1);
464           return;
465         }
466
467       fetch_fpregs (tid);
468       return;
469     }
470
471   if (getregs_supplies (regno))
472     {
473       fetch_regs (tid);
474       return;
475     }
476
477   if (getfpregs_supplies (regno))
478     {
479       fetch_fpregs (tid);
480       return;
481     }
482
483   internal_error (__FILE__, __LINE__,
484                   "Got request for bad register number %d.", regno);
485 }
486
487 /* Store register REGNO back into the child process.  If REGNO is -1,
488    do this for all registers (including the floating point and SSE
489    registers).  */
490 void
491 store_inferior_registers (int regno)
492 {
493   int tid;
494
495   /* Use the old method of poking around in `struct user' if the
496      SETREGS request isn't available.  */
497   if (! have_ptrace_getregs)
498     {
499       old_store_inferior_registers (regno);
500       return;
501     }
502
503   /* Linux LWP ID's are process ID's.  */
504   if ((tid = TIDGET (inferior_ptid)) == 0)
505     tid = PIDGET (inferior_ptid);       /* Not a threaded program.  */
506
507   /* Use the PTRACE_SETFPREGS requests whenever possible, since it
508      transfers more registers in one system call.  But remember that
509      store_fpregs can fail, and return zero.  */
510   if (regno == -1)
511     {
512       store_regs (tid, regno);
513       store_fpregs (tid, regno);
514       return;
515     }
516
517   if (getregs_supplies (regno))
518     {
519       store_regs (tid, regno);
520       return;
521     }
522
523   if (getfpregs_supplies (regno))
524     {
525       store_fpregs (tid, regno);
526       return;
527     }
528
529   internal_error (__FILE__, __LINE__,
530                   "Got request to store bad register number %d.", regno);
531 }
532 \f
533 /* Interpreting register set info found in core files.  */
534
535 /* Provide registers to GDB from a core file.
536
537    (We can't use the generic version of this function in
538    core-regset.c, because we need to use elf_gregset_t instead of
539    gregset_t.)
540
541    CORE_REG_SECT points to an array of bytes, which are the contents
542    of a `note' from a core file which BFD thinks might contain
543    register contents.  CORE_REG_SIZE is its size.
544
545    WHICH says which register set corelow suspects this is:
546      0 --- the general-purpose register set, in elf_gregset_t format
547      2 --- the floating-point register set, in elf_fpregset_t format
548
549    REG_ADDR isn't used on Linux.  */
550
551 static void
552 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
553                       int which, CORE_ADDR reg_addr)
554 {
555   elf_gregset_t gregset;
556   elf_fpregset_t fpregset;
557
558   switch (which)
559     {
560     case 0:
561       if (core_reg_size != sizeof (gregset))
562         warning ("Wrong size gregset in core file.");
563       else
564         {
565           memcpy (&gregset, core_reg_sect, sizeof (gregset));
566           supply_gregset (&gregset);
567         }
568       break;
569
570     case 2:
571       if (core_reg_size != sizeof (fpregset))
572         warning ("Wrong size fpregset in core file.");
573       else
574         {
575           memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
576           supply_fpregset (&fpregset);
577         }
578       break;
579
580     default:
581       /* We've covered all the kinds of registers we know about here,
582          so this must be something we wouldn't know what to do with
583          anyway.  Just ignore it.  */
584       break;
585     }
586 }
587 \f
588
589 int
590 kernel_u_size (void)
591 {
592   return (sizeof (struct user));
593 }
594 \f
595 /* Return non-zero if PC points into the signal trampoline.  */
596
597 int
598 in_sigtramp (CORE_ADDR pc)
599 {
600   CORE_ADDR sp;
601   char buf[TARGET_SHORT_BIT / TARGET_CHAR_BIT];
602   int insn;
603
604   sp = read_register (SP_REGNUM);
605   if (pc - 2 < sp)
606     return 0;
607
608   if (read_memory_nobpt (pc, buf, sizeof (buf)))
609     return 0;
610   insn = extract_unsigned_integer (buf, sizeof (buf));
611   if (insn == 0xdefc            /* addaw #,sp */
612       || insn == 0x7077         /* moveq #119,d0 */
613       || insn == 0x4e40         /* trap #0 */
614       || insn == 0x203c /* movel #,d0 */ )
615     return 1;
616
617   if (read_memory_nobpt (pc - 2, buf, sizeof (buf)))
618     return 0;
619   insn = extract_unsigned_integer (buf, sizeof (buf));
620   if (insn == 0xdefc            /* addaw #,sp */
621       || insn == 0x7077         /* moveq #119,d0 */
622       || insn == 0x4e40         /* trap #0 */
623       || insn == 0x203c /* movel #,d0 */ )
624     return 1;
625
626   return 0;
627 }
628
629 \f
630 /* Register that we are able to handle Linux ELF core file formats.  */
631
632 static struct core_fns linux_elf_core_fns =
633 {
634   bfd_target_elf_flavour,               /* core_flavour */
635   default_check_format,                 /* check_format */
636   default_core_sniffer,                 /* core_sniffer */
637   fetch_core_registers,                 /* core_read_registers */
638   NULL                                  /* next */
639 };
640
641 void
642 _initialize_m68k_linux_nat ()
643 {
644   add_core_fns (&linux_elf_core_fns);
645 }