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