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