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