Remove regcache_get_ptid
[external/binutils.git] / gdb / m68k-linux-nat.c
1 /* Motorola m68k native support for GNU/Linux.
2
3    Copyright (C) 1996-2018 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 "regcache.h"
26 #include "target.h"
27 #include "linux-nat.h"
28
29 #include "m68k-tdep.h"
30
31 #include <sys/dir.h>
32 #include <signal.h>
33 #include "nat/gdb_ptrace.h"
34 #include <sys/user.h>
35 #include <sys/ioctl.h>
36 #include <fcntl.h>
37 #include <sys/procfs.h>
38
39 #ifdef HAVE_SYS_REG_H
40 #include <sys/reg.h>
41 #endif
42
43 #include <sys/file.h>
44 #include <sys/stat.h>
45
46 #include "floatformat.h"
47
48 /* Prototypes for supply_gregset etc.  */
49 #include "gregset.h"
50
51 /* Defines ps_err_e, struct ps_prochandle.  */
52 #include "gdb_proc_service.h"
53
54 #include "inf-ptrace.h"
55
56 #ifndef PTRACE_GET_THREAD_AREA
57 #define PTRACE_GET_THREAD_AREA 25
58 #endif
59 \f
60
61 class m68k_linux_nat_target final : public linux_nat_target
62 {
63 public:
64   /* Add our register access methods.  */
65   void fetch_registers (struct regcache *, int) override;
66   void store_registers (struct regcache *, int) override;
67 };
68
69 static m68k_linux_nat_target the_m68k_linux_nat_target;
70
71 /* This table must line up with gdbarch_register_name in "m68k-tdep.c".  */
72 static const int regmap[] =
73 {
74   PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
75   PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
76   PT_SR, PT_PC,
77   /* PT_FP0, ..., PT_FP7 */
78   21, 24, 27, 30, 33, 36, 39, 42,
79   /* PT_FPCR, PT_FPSR, PT_FPIAR */
80   45, 46, 47
81 };
82
83 /* Which ptrace request retrieves which registers?
84    These apply to the corresponding SET requests as well.  */
85 #define NUM_GREGS (18)
86 #define MAX_NUM_REGS (NUM_GREGS + 11)
87
88 static int
89 getregs_supplies (int regno)
90 {
91   return 0 <= regno && regno < NUM_GREGS;
92 }
93
94 static int
95 getfpregs_supplies (int regno)
96 {
97   return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
98 }
99
100 /* Does the current host support the GETREGS request?  */
101 static int have_ptrace_getregs =
102 #ifdef HAVE_PTRACE_GETREGS
103   1
104 #else
105   0
106 #endif
107 ;
108
109 \f
110
111 /* Fetching registers directly from the U area, one at a time.  */
112
113 /* Fetch one register.  */
114
115 static void
116 fetch_register (struct regcache *regcache, int regno)
117 {
118   struct gdbarch *gdbarch = regcache->arch ();
119   long regaddr, val;
120   int i;
121   gdb_byte buf[M68K_MAX_REGISTER_SIZE];
122   pid_t tid = get_ptrace_pid (regcache->ptid ());
123
124   regaddr = 4 * regmap[regno];
125   for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
126     {
127       errno = 0;
128       val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
129       memcpy (&buf[i], &val, sizeof (long));
130       regaddr += sizeof (long);
131       if (errno != 0)
132         error (_("Couldn't read register %s (#%d): %s."), 
133                gdbarch_register_name (gdbarch, regno),
134                regno, safe_strerror (errno));
135     }
136   regcache_raw_supply (regcache, regno, buf);
137 }
138
139 /* Fetch register values from the inferior.
140    If REGNO is negative, do this for all registers.
141    Otherwise, REGNO specifies which register (so we can save time).  */
142
143 static void
144 old_fetch_inferior_registers (struct regcache *regcache, int regno)
145 {
146   if (regno >= 0)
147     {
148       fetch_register (regcache, regno);
149     }
150   else
151     {
152       for (regno = 0;
153            regno < gdbarch_num_regs (regcache->arch ());
154            regno++)
155         {
156           fetch_register (regcache, regno);
157         }
158     }
159 }
160
161 /* Store one register.  */
162
163 static void
164 store_register (const struct regcache *regcache, int regno)
165 {
166   struct gdbarch *gdbarch = regcache->arch ();
167   long regaddr, val;
168   int i;
169   gdb_byte buf[M68K_MAX_REGISTER_SIZE];
170   pid_t tid = get_ptrace_pid (regcache->ptid ());
171
172   regaddr = 4 * regmap[regno];
173
174   /* Put the contents of regno into a local buffer.  */
175   regcache_raw_collect (regcache, regno, buf);
176
177   /* Store the local buffer into the inferior a chunk at the time.  */
178   for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
179     {
180       errno = 0;
181       memcpy (&val, &buf[i], sizeof (long));
182       ptrace (PTRACE_POKEUSER, tid, regaddr, val);
183       regaddr += sizeof (long);
184       if (errno != 0)
185         error (_("Couldn't write register %s (#%d): %s."),
186                gdbarch_register_name (gdbarch, regno),
187                regno, safe_strerror (errno));
188     }
189 }
190
191 /* Store our register values back into the inferior.
192    If REGNO is negative, do this for all registers.
193    Otherwise, REGNO specifies which register (so we can save time).  */
194
195 static void
196 old_store_inferior_registers (const struct regcache *regcache, int regno)
197 {
198   if (regno >= 0)
199     {
200       store_register (regcache, regno);
201     }
202   else
203     {
204       for (regno = 0;
205            regno < gdbarch_num_regs (regcache->arch ());
206            regno++)
207         {
208           store_register (regcache, regno);
209         }
210     }
211 }
212 \f
213 /*  Given a pointer to a general register set in /proc format
214    (elf_gregset_t *), unpack the register contents and supply
215    them as gdb's idea of the current register values.  */
216
217 void
218 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
219 {
220   struct gdbarch *gdbarch = regcache->arch ();
221   const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
222   int regi;
223
224   for (regi = M68K_D0_REGNUM;
225        regi <= gdbarch_sp_regnum (gdbarch);
226        regi++)
227     regcache_raw_supply (regcache, regi, &regp[regmap[regi]]);
228   regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
229                        &regp[PT_SR]);
230   regcache_raw_supply (regcache,
231                        gdbarch_pc_regnum (gdbarch), &regp[PT_PC]);
232 }
233
234 /* Fill register REGNO (if it is a general-purpose register) in
235    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
236    do this for all registers.  */
237 void
238 fill_gregset (const struct regcache *regcache,
239               elf_gregset_t *gregsetp, int regno)
240 {
241   elf_greg_t *regp = (elf_greg_t *) gregsetp;
242   int i;
243
244   for (i = 0; i < NUM_GREGS; i++)
245     if (regno == -1 || regno == i)
246       regcache_raw_collect (regcache, i, regp + regmap[i]);
247 }
248
249 #ifdef HAVE_PTRACE_GETREGS
250
251 /* Fetch all general-purpose registers from process/thread TID and
252    store their values in GDB's register array.  */
253
254 static void
255 fetch_regs (struct regcache *regcache, int tid)
256 {
257   elf_gregset_t regs;
258
259   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
260     {
261       if (errno == EIO)
262         {
263           /* The kernel we're running on doesn't support the GETREGS
264              request.  Reset `have_ptrace_getregs'.  */
265           have_ptrace_getregs = 0;
266           return;
267         }
268
269       perror_with_name (_("Couldn't get registers"));
270     }
271
272   supply_gregset (regcache, (const elf_gregset_t *) &regs);
273 }
274
275 /* Store all valid general-purpose registers in GDB's register array
276    into the process/thread specified by TID.  */
277
278 static void
279 store_regs (const struct regcache *regcache, int tid, int regno)
280 {
281   elf_gregset_t regs;
282
283   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
284     perror_with_name (_("Couldn't get registers"));
285
286   fill_gregset (regcache, &regs, regno);
287
288   if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
289     perror_with_name (_("Couldn't write registers"));
290 }
291
292 #else
293
294 static void fetch_regs (struct regcache *regcache, int tid)
295 {
296 }
297
298 static void store_regs (const struct regcache *regcache, int tid, int regno)
299 {
300 }
301
302 #endif
303
304 \f
305 /* Transfering floating-point registers between GDB, inferiors and cores.  */
306
307 /* What is the address of fpN within the floating-point register set F?  */
308 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
309
310 /* Fill GDB's register array with the floating-point register values in
311    *FPREGSETP.  */
312
313 void
314 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
315 {
316   struct gdbarch *gdbarch = regcache->arch ();
317   int regi;
318
319   for (regi = gdbarch_fp0_regnum (gdbarch);
320        regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
321     regcache_raw_supply (regcache, regi,
322                          FPREG_ADDR (fpregsetp,
323                                      regi - gdbarch_fp0_regnum (gdbarch)));
324   regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
325   regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
326   regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
327 }
328
329 /* Fill register REGNO (if it is a floating-point register) in
330    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
331    do this for all registers.  */
332
333 void
334 fill_fpregset (const struct regcache *regcache,
335                elf_fpregset_t *fpregsetp, int regno)
336 {
337   struct gdbarch *gdbarch = regcache->arch ();
338   int i;
339
340   /* Fill in the floating-point registers.  */
341   for (i = gdbarch_fp0_regnum (gdbarch);
342        i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
343     if (regno == -1 || regno == i)
344       regcache_raw_collect (regcache, i,
345                             FPREG_ADDR (fpregsetp,
346                                         i - gdbarch_fp0_regnum (gdbarch)));
347
348   /* Fill in the floating-point control registers.  */
349   for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
350     if (regno == -1 || regno == i)
351       regcache_raw_collect (regcache, i,
352                             &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
353 }
354
355 #ifdef HAVE_PTRACE_GETREGS
356
357 /* Fetch all floating-point registers from process/thread TID and store
358    thier values in GDB's register array.  */
359
360 static void
361 fetch_fpregs (struct regcache *regcache, int tid)
362 {
363   elf_fpregset_t fpregs;
364
365   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
366     perror_with_name (_("Couldn't get floating point status"));
367
368   supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
369 }
370
371 /* Store all valid floating-point registers in GDB's register array
372    into the process/thread specified by TID.  */
373
374 static void
375 store_fpregs (const struct regcache *regcache, int tid, int regno)
376 {
377   elf_fpregset_t fpregs;
378
379   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
380     perror_with_name (_("Couldn't get floating point status"));
381
382   fill_fpregset (regcache, &fpregs, regno);
383
384   if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
385     perror_with_name (_("Couldn't write floating point status"));
386 }
387
388 #else
389
390 static void fetch_fpregs (struct regcache *regcache, int tid)
391 {
392 }
393
394 static void store_fpregs (const struct regcache *regcache, int tid, int regno)
395 {
396 }
397
398 #endif
399 \f
400 /* Transferring arbitrary registers between GDB and inferior.  */
401
402 /* Fetch register REGNO from the child process.  If REGNO is -1, do
403    this for all registers (including the floating point and SSE
404    registers).  */
405
406 void
407 m68k_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
408 {
409   pid_t tid;
410
411   /* Use the old method of peeking around in `struct user' if the
412      GETREGS request isn't available.  */
413   if (! have_ptrace_getregs)
414     {
415       old_fetch_inferior_registers (regcache, regno);
416       return;
417     }
418
419   tid = get_ptrace_pid (regcache->ptid ());
420
421   /* Use the PTRACE_GETFPXREGS request whenever possible, since it
422      transfers more registers in one system call, and we'll cache the
423      results.  But remember that fetch_fpxregs can fail, and return
424      zero.  */
425   if (regno == -1)
426     {
427       fetch_regs (regcache, tid);
428
429       /* The call above might reset `have_ptrace_getregs'.  */
430       if (! have_ptrace_getregs)
431         {
432           old_fetch_inferior_registers (regcache, -1);
433           return;
434         }
435
436       fetch_fpregs (regcache, tid);
437       return;
438     }
439
440   if (getregs_supplies (regno))
441     {
442       fetch_regs (regcache, tid);
443       return;
444     }
445
446   if (getfpregs_supplies (regno))
447     {
448       fetch_fpregs (regcache, tid);
449       return;
450     }
451
452   internal_error (__FILE__, __LINE__,
453                   _("Got request for bad register number %d."), regno);
454 }
455
456 /* Store register REGNO back into the child process.  If REGNO is -1,
457    do this for all registers (including the floating point and SSE
458    registers).  */
459 void
460 m68k_linux_nat_target::store_registers (struct regcache *regcache, int regno)
461 {
462   pid_t tid;
463
464   /* Use the old method of poking around in `struct user' if the
465      SETREGS request isn't available.  */
466   if (! have_ptrace_getregs)
467     {
468       old_store_inferior_registers (regcache, regno);
469       return;
470     }
471
472   tid = get_ptrace_pid (regcache->ptid ());
473
474   /* Use the PTRACE_SETFPREGS requests whenever possible, since it
475      transfers more registers in one system call.  But remember that
476      store_fpregs can fail, and return zero.  */
477   if (regno == -1)
478     {
479       store_regs (regcache, tid, regno);
480       store_fpregs (regcache, tid, regno);
481       return;
482     }
483
484   if (getregs_supplies (regno))
485     {
486       store_regs (regcache, tid, regno);
487       return;
488     }
489
490   if (getfpregs_supplies (regno))
491     {
492       store_fpregs (regcache, tid, regno);
493       return;
494     }
495
496   internal_error (__FILE__, __LINE__,
497                   _("Got request to store bad register number %d."), regno);
498 }
499 \f
500
501 /* Fetch the thread-local storage pointer for libthread_db.  */
502
503 ps_err_e
504 ps_get_thread_area (struct ps_prochandle *ph, 
505                     lwpid_t lwpid, int idx, void **base)
506 {
507   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
508     return PS_ERR;
509
510   /* IDX is the bias from the thread pointer to the beginning of the
511      thread descriptor.  It has to be subtracted due to implementation
512      quirks in libthread_db.  */
513   *base = (char *) *base - idx;
514
515   return PS_OK;
516 }
517
518 void
519 _initialize_m68k_linux_nat (void)
520 {
521   /* Register the target.  */
522   linux_target = &the_m68k_linux_nat_target;
523   add_inf_child_target (&the_m68k_linux_nat_target);
524 }