OSDN Git Service

PR 11123
[pf3gnuchains/pf3gnuchains3x.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, 2009, 2010 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 /* Fetch one register.  */
99
100 static void
101 fetch_register (struct regcache *regcache, int regno)
102 {
103   struct gdbarch *gdbarch = get_regcache_arch (regcache);
104   long regaddr;
105   int i;
106   char buf[MAX_REGISTER_SIZE];
107   int tid;
108
109   /* Overload thread id onto process id */
110   tid = TIDGET (inferior_ptid);
111   if (tid == 0)
112     tid = PIDGET (inferior_ptid);       /* no thread id, just use process id */
113
114   regaddr = 4 * regmap[regno];
115   for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
116     {
117       errno = 0;
118       *(long *) &buf[i] = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
119       regaddr += sizeof (long);
120       if (errno != 0)
121         error (_("Couldn't read register %s (#%d): %s."), 
122                gdbarch_register_name (gdbarch, regno),
123                regno, safe_strerror (errno));
124     }
125   regcache_raw_supply (regcache, regno, buf);
126 }
127
128 /* Fetch register values from the inferior.
129    If REGNO is negative, do this for all registers.
130    Otherwise, REGNO specifies which register (so we can save time). */
131
132 static void
133 old_fetch_inferior_registers (struct regcache *regcache, int regno)
134 {
135   if (regno >= 0)
136     {
137       fetch_register (regcache, regno);
138     }
139   else
140     {
141       for (regno = 0;
142            regno < gdbarch_num_regs (get_regcache_arch (regcache));
143            regno++)
144         {
145           fetch_register (regcache, regno);
146         }
147     }
148 }
149
150 /* Store one register. */
151
152 static void
153 store_register (const struct regcache *regcache, int regno)
154 {
155   struct gdbarch *gdbarch = get_regcache_arch (regcache);
156   long regaddr;
157   int i;
158   int tid;
159   char buf[MAX_REGISTER_SIZE];
160
161   /* Overload thread id onto process id */
162   tid = TIDGET (inferior_ptid);
163   if (tid == 0)
164     tid = PIDGET (inferior_ptid);       /* no thread id, just use process id */
165
166   regaddr = 4 * regmap[regno];
167
168   /* Put the contents of regno into a local buffer */
169   regcache_raw_collect (regcache, regno, buf);
170
171   /* Store the local buffer into the inferior a chunk at the time. */
172   for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
173     {
174       errno = 0;
175       ptrace (PTRACE_POKEUSER, tid, regaddr, *(long *) &buf[i]);
176       regaddr += sizeof (long);
177       if (errno != 0)
178         error (_("Couldn't write register %s (#%d): %s."),
179                gdbarch_register_name (gdbarch, regno),
180                regno, safe_strerror (errno));
181     }
182 }
183
184 /* Store our register values back into the inferior.
185    If REGNO is negative, do this for all registers.
186    Otherwise, REGNO specifies which register (so we can save time).  */
187
188 static void
189 old_store_inferior_registers (const struct regcache *regcache, int regno)
190 {
191   if (regno >= 0)
192     {
193       store_register (regcache, regno);
194     }
195   else
196     {
197       for (regno = 0;
198            regno < gdbarch_num_regs (get_regcache_arch (regcache));
199            regno++)
200         {
201           store_register (regcache, regno);
202         }
203     }
204 }
205 \f
206 /*  Given a pointer to a general register set in /proc format
207    (elf_gregset_t *), unpack the register contents and supply
208    them as gdb's idea of the current register values. */
209
210 void
211 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
212 {
213   struct gdbarch *gdbarch = get_regcache_arch (regcache);
214   const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
215   int regi;
216
217   for (regi = M68K_D0_REGNUM;
218        regi <= gdbarch_sp_regnum (gdbarch);
219        regi++)
220     regcache_raw_supply (regcache, regi, &regp[regmap[regi]]);
221   regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
222                        &regp[PT_SR]);
223   regcache_raw_supply (regcache,
224                        gdbarch_pc_regnum (gdbarch), &regp[PT_PC]);
225 }
226
227 /* Fill register REGNO (if it is a general-purpose register) in
228    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
229    do this for all registers.  */
230 void
231 fill_gregset (const struct regcache *regcache,
232               elf_gregset_t *gregsetp, int regno)
233 {
234   elf_greg_t *regp = (elf_greg_t *) gregsetp;
235   int i;
236
237   for (i = 0; i < NUM_GREGS; i++)
238     if (regno == -1 || regno == i)
239       regcache_raw_collect (regcache, i, regp + regmap[i]);
240 }
241
242 #ifdef HAVE_PTRACE_GETREGS
243
244 /* Fetch all general-purpose registers from process/thread TID and
245    store their values in GDB's register array.  */
246
247 static void
248 fetch_regs (struct regcache *regcache, int tid)
249 {
250   elf_gregset_t regs;
251
252   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
253     {
254       if (errno == EIO)
255         {
256           /* The kernel we're running on doesn't support the GETREGS
257              request.  Reset `have_ptrace_getregs'.  */
258           have_ptrace_getregs = 0;
259           return;
260         }
261
262       perror_with_name (_("Couldn't get registers"));
263     }
264
265   supply_gregset (regcache, (const elf_gregset_t *) &regs);
266 }
267
268 /* Store all valid general-purpose registers in GDB's register array
269    into the process/thread specified by TID.  */
270
271 static void
272 store_regs (const struct regcache *regcache, int tid, int regno)
273 {
274   elf_gregset_t regs;
275
276   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
277     perror_with_name (_("Couldn't get registers"));
278
279   fill_gregset (regcache, &regs, regno);
280
281   if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
282     perror_with_name (_("Couldn't write registers"));
283 }
284
285 #else
286
287 static void fetch_regs (struct regcache *regcache, int tid) {}
288 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
289
290 #endif
291
292 \f
293 /* Transfering floating-point registers between GDB, inferiors and cores.  */
294
295 /* What is the address of fpN within the floating-point register set F?  */
296 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
297
298 /* Fill GDB's register array with the floating-point register values in
299    *FPREGSETP.  */
300
301 void
302 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
303 {
304   struct gdbarch *gdbarch = get_regcache_arch (regcache);
305   int regi;
306
307   for (regi = gdbarch_fp0_regnum (gdbarch);
308        regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
309     regcache_raw_supply (regcache, regi,
310                          FPREG_ADDR (fpregsetp,
311                                      regi - gdbarch_fp0_regnum (gdbarch)));
312   regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
313   regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
314   regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
315 }
316
317 /* Fill register REGNO (if it is a floating-point register) in
318    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
319    do this for all registers.  */
320
321 void
322 fill_fpregset (const struct regcache *regcache,
323                elf_fpregset_t *fpregsetp, int regno)
324 {
325   struct gdbarch *gdbarch = get_regcache_arch (regcache);
326   int i;
327
328   /* Fill in the floating-point registers.  */
329   for (i = gdbarch_fp0_regnum (gdbarch);
330        i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
331     if (regno == -1 || regno == i)
332       regcache_raw_collect (regcache, i,
333                             FPREG_ADDR (fpregsetp,
334                                         i - gdbarch_fp0_regnum (gdbarch)));
335
336   /* Fill in the floating-point control registers.  */
337   for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
338     if (regno == -1 || regno == i)
339       regcache_raw_collect (regcache, i,
340                             &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
341 }
342
343 #ifdef HAVE_PTRACE_GETREGS
344
345 /* Fetch all floating-point registers from process/thread TID and store
346    thier values in GDB's register array.  */
347
348 static void
349 fetch_fpregs (struct regcache *regcache, int tid)
350 {
351   elf_fpregset_t fpregs;
352
353   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
354     perror_with_name (_("Couldn't get floating point status"));
355
356   supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
357 }
358
359 /* Store all valid floating-point registers in GDB's register array
360    into the process/thread specified by TID.  */
361
362 static void
363 store_fpregs (const struct regcache *regcache, int tid, int regno)
364 {
365   elf_fpregset_t fpregs;
366
367   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
368     perror_with_name (_("Couldn't get floating point status"));
369
370   fill_fpregset (regcache, &fpregs, regno);
371
372   if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
373     perror_with_name (_("Couldn't write floating point status"));
374 }
375
376 #else
377
378 static void fetch_fpregs (struct regcache *regcache, int tid) {}
379 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
380
381 #endif
382 \f
383 /* Transferring arbitrary registers between GDB and inferior.  */
384
385 /* Fetch register REGNO from the child process.  If REGNO is -1, do
386    this for all registers (including the floating point and SSE
387    registers).  */
388
389 static void
390 m68k_linux_fetch_inferior_registers (struct target_ops *ops,
391                                      struct regcache *regcache, int regno)
392 {
393   int tid;
394
395   /* Use the old method of peeking around in `struct user' if the
396      GETREGS request isn't available.  */
397   if (! have_ptrace_getregs)
398     {
399       old_fetch_inferior_registers (regcache, regno);
400       return;
401     }
402
403   /* GNU/Linux LWP ID's are process ID's.  */
404   tid = TIDGET (inferior_ptid);
405   if (tid == 0)
406     tid = PIDGET (inferior_ptid);               /* Not a threaded program.  */
407
408   /* Use the PTRACE_GETFPXREGS request whenever possible, since it
409      transfers more registers in one system call, and we'll cache the
410      results.  But remember that fetch_fpxregs can fail, and return
411      zero.  */
412   if (regno == -1)
413     {
414       fetch_regs (regcache, tid);
415
416       /* The call above might reset `have_ptrace_getregs'.  */
417       if (! have_ptrace_getregs)
418         {
419           old_fetch_inferior_registers (regcache, -1);
420           return;
421         }
422
423       fetch_fpregs (regcache, tid);
424       return;
425     }
426
427   if (getregs_supplies (regno))
428     {
429       fetch_regs (regcache, tid);
430       return;
431     }
432
433   if (getfpregs_supplies (regno))
434     {
435       fetch_fpregs (regcache, tid);
436       return;
437     }
438
439   internal_error (__FILE__, __LINE__,
440                   _("Got request for bad register number %d."), regno);
441 }
442
443 /* Store register REGNO back into the child process.  If REGNO is -1,
444    do this for all registers (including the floating point and SSE
445    registers).  */
446 static void
447 m68k_linux_store_inferior_registers (struct target_ops *ops,
448                                      struct regcache *regcache, int regno)
449 {
450   int tid;
451
452   /* Use the old method of poking around in `struct user' if the
453      SETREGS request isn't available.  */
454   if (! have_ptrace_getregs)
455     {
456       old_store_inferior_registers (regcache, regno);
457       return;
458     }
459
460   /* GNU/Linux LWP ID's are process ID's.  */
461   tid = TIDGET (inferior_ptid);
462   if (tid == 0)
463     tid = PIDGET (inferior_ptid);       /* Not a threaded program.  */
464
465   /* Use the PTRACE_SETFPREGS requests whenever possible, since it
466      transfers more registers in one system call.  But remember that
467      store_fpregs can fail, and return zero.  */
468   if (regno == -1)
469     {
470       store_regs (regcache, tid, regno);
471       store_fpregs (regcache, tid, regno);
472       return;
473     }
474
475   if (getregs_supplies (regno))
476     {
477       store_regs (regcache, tid, regno);
478       return;
479     }
480
481   if (getfpregs_supplies (regno))
482     {
483       store_fpregs (regcache, tid, regno);
484       return;
485     }
486
487   internal_error (__FILE__, __LINE__,
488                   _("Got request to store bad register number %d."), regno);
489 }
490 \f
491 /* Interpreting register set info found in core files.  */
492
493 /* Provide registers to GDB from a core file.
494
495    (We can't use the generic version of this function in
496    core-regset.c, because we need to use elf_gregset_t instead of
497    gregset_t.)
498
499    CORE_REG_SECT points to an array of bytes, which are the contents
500    of a `note' from a core file which BFD thinks might contain
501    register contents.  CORE_REG_SIZE is its size.
502
503    WHICH says which register set corelow suspects this is:
504      0 --- the general-purpose register set, in elf_gregset_t format
505      2 --- the floating-point register set, in elf_fpregset_t format
506
507    REG_ADDR isn't used on GNU/Linux.  */
508
509 static void
510 fetch_core_registers (struct regcache *regcache,
511                       char *core_reg_sect, unsigned core_reg_size,
512                       int which, CORE_ADDR reg_addr)
513 {
514   elf_gregset_t gregset;
515   elf_fpregset_t fpregset;
516
517   switch (which)
518     {
519     case 0:
520       if (core_reg_size != sizeof (gregset))
521         warning (_("Wrong size gregset in core file."));
522       else
523         {
524           memcpy (&gregset, core_reg_sect, sizeof (gregset));
525           supply_gregset (regcache, (const elf_gregset_t *) &gregset);
526         }
527       break;
528
529     case 2:
530       if (core_reg_size != sizeof (fpregset))
531         warning (_("Wrong size fpregset in core file."));
532       else
533         {
534           memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
535           supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
536         }
537       break;
538
539     default:
540       /* We've covered all the kinds of registers we know about here,
541          so this must be something we wouldn't know what to do with
542          anyway.  Just ignore it.  */
543       break;
544     }
545 }
546 \f
547
548 /* Register that we are able to handle GNU/Linux ELF core file
549    formats.  */
550
551 static struct core_fns linux_elf_core_fns =
552 {
553   bfd_target_elf_flavour,               /* core_flavour */
554   default_check_format,                 /* check_format */
555   default_core_sniffer,                 /* core_sniffer */
556   fetch_core_registers,                 /* core_read_registers */
557   NULL                                  /* next */
558 };
559
560 void _initialize_m68k_linux_nat (void);
561
562 void
563 _initialize_m68k_linux_nat (void)
564 {
565   struct target_ops *t;
566
567   /* Fill in the generic GNU/Linux methods.  */
568   t = linux_target ();
569
570   /* Add our register access methods.  */
571   t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
572   t->to_store_registers = m68k_linux_store_inferior_registers;
573
574   /* Register the target.  */
575   linux_nat_add_target (t);
576
577   deprecated_add_core_fns (&linux_elf_core_fns);
578 }