1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
27 #include "linux-nat.h"
29 #include "m68k-tdep.h"
33 #include "nat/gdb_ptrace.h"
35 #include <sys/ioctl.h>
37 #include <sys/procfs.h>
46 #include "floatformat.h"
48 /* Prototypes for supply_gregset etc. */
51 /* Defines ps_err_e, struct ps_prochandle. */
52 #include "gdb_proc_service.h"
54 #include "inf-ptrace.h"
56 #ifndef PTRACE_GET_THREAD_AREA
57 #define PTRACE_GET_THREAD_AREA 25
60 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
61 static const int regmap
[] =
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
,
66 /* PT_FP0, ..., PT_FP7 */
67 21, 24, 27, 30, 33, 36, 39, 42,
68 /* PT_FPCR, PT_FPSR, PT_FPIAR */
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)
78 getregs_supplies (int regno
)
80 return 0 <= regno
&& regno
< NUM_GREGS
;
84 getfpregs_supplies (int regno
)
86 return M68K_FP0_REGNUM
<= regno
&& regno
<= M68K_FPI_REGNUM
;
89 /* Does the current host support the GETREGS request? */
90 static int have_ptrace_getregs
=
91 #ifdef HAVE_PTRACE_GETREGS
100 /* Fetching registers directly from the U area, one at a time. */
102 /* Fetch one register. */
105 fetch_register (struct regcache
*regcache
, int regno
)
107 struct gdbarch
*gdbarch
= regcache
->arch ();
110 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
111 pid_t tid
= get_ptrace_pid (regcache_get_ptid (regcache
));
113 regaddr
= 4 * regmap
[regno
];
114 for (i
= 0; i
< register_size (gdbarch
, regno
); i
+= sizeof (long))
117 val
= ptrace (PTRACE_PEEKUSER
, tid
, regaddr
, 0);
118 memcpy (&buf
[i
], &val
, sizeof (long));
119 regaddr
+= sizeof (long);
121 error (_("Couldn't read register %s (#%d): %s."),
122 gdbarch_register_name (gdbarch
, regno
),
123 regno
, safe_strerror (errno
));
125 regcache_raw_supply (regcache
, regno
, buf
);
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). */
133 old_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
137 fetch_register (regcache
, regno
);
142 regno
< gdbarch_num_regs (regcache
->arch ());
145 fetch_register (regcache
, regno
);
150 /* Store one register. */
153 store_register (const struct regcache
*regcache
, int regno
)
155 struct gdbarch
*gdbarch
= regcache
->arch ();
158 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
159 pid_t tid
= get_ptrace_pid (regcache_get_ptid (regcache
));
161 regaddr
= 4 * regmap
[regno
];
163 /* Put the contents of regno into a local buffer. */
164 regcache_raw_collect (regcache
, regno
, buf
);
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))
170 memcpy (&val
, &buf
[i
], sizeof (long));
171 ptrace (PTRACE_POKEUSER
, tid
, regaddr
, val
);
172 regaddr
+= sizeof (long);
174 error (_("Couldn't write register %s (#%d): %s."),
175 gdbarch_register_name (gdbarch
, regno
),
176 regno
, safe_strerror (errno
));
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). */
185 old_store_inferior_registers (const struct regcache
*regcache
, int regno
)
189 store_register (regcache
, regno
);
194 regno
< gdbarch_num_regs (regcache
->arch ());
197 store_register (regcache
, regno
);
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. */
207 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
209 struct gdbarch
*gdbarch
= regcache
->arch ();
210 const elf_greg_t
*regp
= (const elf_greg_t
*) gregsetp
;
213 for (regi
= M68K_D0_REGNUM
;
214 regi
<= gdbarch_sp_regnum (gdbarch
);
216 regcache_raw_supply (regcache
, regi
, ®p
[regmap
[regi
]]);
217 regcache_raw_supply (regcache
, gdbarch_ps_regnum (gdbarch
),
219 regcache_raw_supply (regcache
,
220 gdbarch_pc_regnum (gdbarch
), ®p
[PT_PC
]);
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. */
227 fill_gregset (const struct regcache
*regcache
,
228 elf_gregset_t
*gregsetp
, int regno
)
230 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
233 for (i
= 0; i
< NUM_GREGS
; i
++)
234 if (regno
== -1 || regno
== i
)
235 regcache_raw_collect (regcache
, i
, regp
+ regmap
[i
]);
238 #ifdef HAVE_PTRACE_GETREGS
240 /* Fetch all general-purpose registers from process/thread TID and
241 store their values in GDB's register array. */
244 fetch_regs (struct regcache
*regcache
, int tid
)
248 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
252 /* The kernel we're running on doesn't support the GETREGS
253 request. Reset `have_ptrace_getregs'. */
254 have_ptrace_getregs
= 0;
258 perror_with_name (_("Couldn't get registers"));
261 supply_gregset (regcache
, (const elf_gregset_t
*) ®s
);
264 /* Store all valid general-purpose registers in GDB's register array
265 into the process/thread specified by TID. */
268 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
272 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
273 perror_with_name (_("Couldn't get registers"));
275 fill_gregset (regcache
, ®s
, regno
);
277 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
278 perror_with_name (_("Couldn't write registers"));
283 static void fetch_regs (struct regcache
*regcache
, int tid
)
287 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
)
294 /* Transfering floating-point registers between GDB, inferiors and cores. */
296 /* What is the address of fpN within the floating-point register set F? */
297 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
299 /* Fill GDB's register array with the floating-point register values in
303 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
305 struct gdbarch
*gdbarch
= regcache
->arch ();
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]);
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. */
323 fill_fpregset (const struct regcache
*regcache
,
324 elf_fpregset_t
*fpregsetp
, int regno
)
326 struct gdbarch
*gdbarch
= regcache
->arch ();
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
)));
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
]);
344 #ifdef HAVE_PTRACE_GETREGS
346 /* Fetch all floating-point registers from process/thread TID and store
347 thier values in GDB's register array. */
350 fetch_fpregs (struct regcache
*regcache
, int tid
)
352 elf_fpregset_t fpregs
;
354 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
355 perror_with_name (_("Couldn't get floating point status"));
357 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
360 /* Store all valid floating-point registers in GDB's register array
361 into the process/thread specified by TID. */
364 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
366 elf_fpregset_t fpregs
;
368 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
369 perror_with_name (_("Couldn't get floating point status"));
371 fill_fpregset (regcache
, &fpregs
, regno
);
373 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
374 perror_with_name (_("Couldn't write floating point status"));
379 static void fetch_fpregs (struct regcache
*regcache
, int tid
)
383 static void store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
389 /* Transferring arbitrary registers between GDB and inferior. */
391 /* Fetch register REGNO from the child process. If REGNO is -1, do
392 this for all registers (including the floating point and SSE
396 m68k_linux_fetch_inferior_registers (struct target_ops
*ops
,
397 struct regcache
*regcache
, int regno
)
401 /* Use the old method of peeking around in `struct user' if the
402 GETREGS request isn't available. */
403 if (! have_ptrace_getregs
)
405 old_fetch_inferior_registers (regcache
, regno
);
409 tid
= get_ptrace_pid (regcache_get_ptid (regcache
));
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
417 fetch_regs (regcache
, tid
);
419 /* The call above might reset `have_ptrace_getregs'. */
420 if (! have_ptrace_getregs
)
422 old_fetch_inferior_registers (regcache
, -1);
426 fetch_fpregs (regcache
, tid
);
430 if (getregs_supplies (regno
))
432 fetch_regs (regcache
, tid
);
436 if (getfpregs_supplies (regno
))
438 fetch_fpregs (regcache
, tid
);
442 internal_error (__FILE__
, __LINE__
,
443 _("Got request for bad register number %d."), regno
);
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
450 m68k_linux_store_inferior_registers (struct target_ops
*ops
,
451 struct regcache
*regcache
, int regno
)
455 /* Use the old method of poking around in `struct user' if the
456 SETREGS request isn't available. */
457 if (! have_ptrace_getregs
)
459 old_store_inferior_registers (regcache
, regno
);
463 tid
= get_ptrace_pid (regcache_get_ptid (regcache
));
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. */
470 store_regs (regcache
, tid
, regno
);
471 store_fpregs (regcache
, tid
, regno
);
475 if (getregs_supplies (regno
))
477 store_regs (regcache
, tid
, regno
);
481 if (getfpregs_supplies (regno
))
483 store_fpregs (regcache
, tid
, regno
);
487 internal_error (__FILE__
, __LINE__
,
488 _("Got request to store bad register number %d."), regno
);
492 /* Fetch the thread-local storage pointer for libthread_db. */
495 ps_get_thread_area (struct ps_prochandle
*ph
,
496 lwpid_t lwpid
, int idx
, void **base
)
498 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) < 0)
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
;
510 _initialize_m68k_linux_nat (void)
512 struct target_ops
*t
;
514 /* Fill in the generic GNU/Linux methods. */
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
;
521 /* Register the target. */
522 linux_nat_add_target (t
);