1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996-2021 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"
30 #include "m68k-tdep.h"
34 #include "nat/gdb_ptrace.h"
36 #include <sys/ioctl.h>
38 #include <sys/procfs.h>
47 #include "floatformat.h"
49 /* Prototypes for supply_gregset etc. */
52 /* Defines ps_err_e, struct ps_prochandle. */
53 #include "gdb_proc_service.h"
55 #include "inf-ptrace.h"
57 #ifndef PTRACE_GET_THREAD_AREA
58 #define PTRACE_GET_THREAD_AREA 25
62 class m68k_linux_nat_target final
: public linux_nat_target
65 /* Add our register access methods. */
66 void fetch_registers (struct regcache
*, int) override
;
67 void store_registers (struct regcache
*, int) override
;
70 static m68k_linux_nat_target the_m68k_linux_nat_target
;
72 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
73 static const int regmap
[] =
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
,
78 /* PT_FP0, ..., PT_FP7 */
79 21, 24, 27, 30, 33, 36, 39, 42,
80 /* PT_FPCR, PT_FPSR, PT_FPIAR */
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)
90 getregs_supplies (int regno
)
92 return 0 <= regno
&& regno
< NUM_GREGS
;
96 getfpregs_supplies (int regno
)
98 return M68K_FP0_REGNUM
<= regno
&& regno
<= M68K_FPI_REGNUM
;
101 /* Does the current host support the GETREGS request? */
102 static int have_ptrace_getregs
=
103 #ifdef HAVE_PTRACE_GETREGS
112 /* Fetching registers directly from the U area, one at a time. */
114 /* Fetch one register. */
117 fetch_register (struct regcache
*regcache
, int regno
)
119 struct gdbarch
*gdbarch
= regcache
->arch ();
122 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
123 pid_t tid
= get_ptrace_pid (regcache
->ptid ());
125 regaddr
= 4 * regmap
[regno
];
126 for (i
= 0; i
< register_size (gdbarch
, regno
); i
+= sizeof (long))
129 val
= ptrace (PTRACE_PEEKUSER
, tid
, regaddr
, 0);
130 memcpy (&buf
[i
], &val
, sizeof (long));
131 regaddr
+= sizeof (long);
133 error (_("Couldn't read register %s (#%d): %s."),
134 gdbarch_register_name (gdbarch
, regno
),
135 regno
, safe_strerror (errno
));
137 regcache
->raw_supply (regno
, buf
);
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). */
145 old_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
149 fetch_register (regcache
, regno
);
154 regno
< gdbarch_num_regs (regcache
->arch ());
157 fetch_register (regcache
, regno
);
162 /* Store one register. */
165 store_register (const struct regcache
*regcache
, int regno
)
167 struct gdbarch
*gdbarch
= regcache
->arch ();
170 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
171 pid_t tid
= get_ptrace_pid (regcache
->ptid ());
173 regaddr
= 4 * regmap
[regno
];
175 /* Put the contents of regno into a local buffer. */
176 regcache
->raw_collect (regno
, buf
);
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))
182 memcpy (&val
, &buf
[i
], sizeof (long));
183 ptrace (PTRACE_POKEUSER
, tid
, regaddr
, val
);
184 regaddr
+= sizeof (long);
186 error (_("Couldn't write register %s (#%d): %s."),
187 gdbarch_register_name (gdbarch
, regno
),
188 regno
, safe_strerror (errno
));
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). */
197 old_store_inferior_registers (const struct regcache
*regcache
, int regno
)
201 store_register (regcache
, regno
);
206 regno
< gdbarch_num_regs (regcache
->arch ());
209 store_register (regcache
, regno
);
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. */
219 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
221 struct gdbarch
*gdbarch
= regcache
->arch ();
222 const elf_greg_t
*regp
= (const elf_greg_t
*) gregsetp
;
225 for (regi
= M68K_D0_REGNUM
;
226 regi
<= gdbarch_sp_regnum (gdbarch
);
228 regcache
->raw_supply (regi
, ®p
[regmap
[regi
]]);
229 regcache
->raw_supply (gdbarch_ps_regnum (gdbarch
), ®p
[PT_SR
]);
230 regcache
->raw_supply (gdbarch_pc_regnum (gdbarch
), ®p
[PT_PC
]);
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. */
237 fill_gregset (const struct regcache
*regcache
,
238 elf_gregset_t
*gregsetp
, int regno
)
240 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
243 for (i
= 0; i
< NUM_GREGS
; i
++)
244 if (regno
== -1 || regno
== i
)
245 regcache
->raw_collect (i
, regp
+ regmap
[i
]);
248 #ifdef HAVE_PTRACE_GETREGS
250 /* Fetch all general-purpose registers from process/thread TID and
251 store their values in GDB's register array. */
254 fetch_regs (struct regcache
*regcache
, int tid
)
258 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
262 /* The kernel we're running on doesn't support the GETREGS
263 request. Reset `have_ptrace_getregs'. */
264 have_ptrace_getregs
= 0;
268 perror_with_name (_("Couldn't get registers"));
271 supply_gregset (regcache
, (const elf_gregset_t
*) ®s
);
274 /* Store all valid general-purpose registers in GDB's register array
275 into the process/thread specified by TID. */
278 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
282 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
283 perror_with_name (_("Couldn't get registers"));
285 fill_gregset (regcache
, ®s
, regno
);
287 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
288 perror_with_name (_("Couldn't write registers"));
293 static void fetch_regs (struct regcache
*regcache
, int tid
)
297 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
)
304 /* Transfering floating-point registers between GDB, inferiors and cores. */
306 /* What is the address of fpN within the floating-point register set F? */
307 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
309 /* Fill GDB's register array with the floating-point register values in
313 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
315 struct gdbarch
*gdbarch
= regcache
->arch ();
318 for (regi
= gdbarch_fp0_regnum (gdbarch
);
319 regi
< gdbarch_fp0_regnum (gdbarch
) + 8; regi
++)
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]);
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. */
332 fill_fpregset (const struct regcache
*regcache
,
333 elf_fpregset_t
*fpregsetp
, int regno
)
335 struct gdbarch
*gdbarch
= regcache
->arch ();
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
)));
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
]);
351 #ifdef HAVE_PTRACE_GETREGS
353 /* Fetch all floating-point registers from process/thread TID and store
354 thier values in GDB's register array. */
357 fetch_fpregs (struct regcache
*regcache
, int tid
)
359 elf_fpregset_t fpregs
;
361 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
362 perror_with_name (_("Couldn't get floating point status"));
364 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
367 /* Store all valid floating-point registers in GDB's register array
368 into the process/thread specified by TID. */
371 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
373 elf_fpregset_t fpregs
;
375 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
376 perror_with_name (_("Couldn't get floating point status"));
378 fill_fpregset (regcache
, &fpregs
, regno
);
380 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
381 perror_with_name (_("Couldn't write floating point status"));
386 static void fetch_fpregs (struct regcache
*regcache
, int tid
)
390 static void store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
396 /* Transferring arbitrary registers between GDB and inferior. */
398 /* Fetch register REGNO from the child process. If REGNO is -1, do
399 this for all registers (including the floating point and SSE
403 m68k_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
407 /* Use the old method of peeking around in `struct user' if the
408 GETREGS request isn't available. */
409 if (! have_ptrace_getregs
)
411 old_fetch_inferior_registers (regcache
, regno
);
415 tid
= get_ptrace_pid (regcache
->ptid ());
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
423 fetch_regs (regcache
, tid
);
425 /* The call above might reset `have_ptrace_getregs'. */
426 if (! have_ptrace_getregs
)
428 old_fetch_inferior_registers (regcache
, -1);
432 fetch_fpregs (regcache
, tid
);
436 if (getregs_supplies (regno
))
438 fetch_regs (regcache
, tid
);
442 if (getfpregs_supplies (regno
))
444 fetch_fpregs (regcache
, tid
);
448 internal_error (__FILE__
, __LINE__
,
449 _("Got request for bad register number %d."), regno
);
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
456 m68k_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
460 /* Use the old method of poking around in `struct user' if the
461 SETREGS request isn't available. */
462 if (! have_ptrace_getregs
)
464 old_store_inferior_registers (regcache
, regno
);
468 tid
= get_ptrace_pid (regcache
->ptid ());
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. */
475 store_regs (regcache
, tid
, regno
);
476 store_fpregs (regcache
, tid
, regno
);
480 if (getregs_supplies (regno
))
482 store_regs (regcache
, tid
, regno
);
486 if (getfpregs_supplies (regno
))
488 store_fpregs (regcache
, tid
, regno
);
492 internal_error (__FILE__
, __LINE__
,
493 _("Got request to store bad register number %d."), regno
);
497 /* Fetch the thread-local storage pointer for libthread_db. */
500 ps_get_thread_area (struct ps_prochandle
*ph
,
501 lwpid_t lwpid
, int idx
, void **base
)
503 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) < 0)
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
;
514 void _initialize_m68k_linux_nat ();
516 _initialize_m68k_linux_nat ()
518 /* Register the target. */
519 linux_target
= &the_m68k_linux_nat_target
;
520 add_inf_child_target (&the_m68k_linux_nat_target
);