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