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