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