m68k-bsd-nat.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Native-dependent code for Motorola 68000 BSD's.
  2. Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* We define this to get types like register_t. */
  15. #define _KERNTYPES
  16. #include "defs.h"
  17. #include "gdbcore.h"
  18. #include "inferior.h"
  19. #include "regcache.h"
  20. #include <sys/types.h>
  21. #include <sys/ptrace.h>
  22. #include <machine/reg.h>
  23. #include "m68k-tdep.h"
  24. #include "inf-ptrace.h"
  25. #include "netbsd-nat.h"
  26. struct m68k_bsd_nat_target final : public nbsd_nat_target
  27. {
  28. void fetch_registers (struct regcache *, int) override;
  29. void store_registers (struct regcache *, int) override;
  30. };
  31. static m68k_bsd_nat_target the_m68k_bsd_nat_target;
  32. static int
  33. m68kbsd_gregset_supplies_p (int regnum)
  34. {
  35. return (regnum >= M68K_D0_REGNUM && regnum <= M68K_PC_REGNUM);
  36. }
  37. static int
  38. m68kbsd_fpregset_supplies_p (int regnum)
  39. {
  40. return (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM);
  41. }
  42. /* Supply the general-purpose registers stored in GREGS to REGCACHE. */
  43. static void
  44. m68kbsd_supply_gregset (struct regcache *regcache, const void *gregs)
  45. {
  46. const gdb_byte *regs = (const gdb_byte *) gregs;
  47. int regnum;
  48. for (regnum = M68K_D0_REGNUM; regnum <= M68K_PC_REGNUM; regnum++)
  49. regcache->raw_supply (regnum, regs + regnum * 4);
  50. }
  51. /* Supply the floating-point registers stored in FPREGS to REGCACHE. */
  52. static void
  53. m68kbsd_supply_fpregset (struct regcache *regcache, const void *fpregs)
  54. {
  55. struct gdbarch *gdbarch = regcache->arch ();
  56. const gdb_byte *regs = (const gdb_byte *) fpregs;
  57. int regnum;
  58. for (regnum = M68K_FP0_REGNUM; regnum <= M68K_FPI_REGNUM; regnum++)
  59. regcache->raw_supply (regnum,
  60. regs + m68kbsd_fpreg_offset (gdbarch, regnum));
  61. }
  62. /* Collect the general-purpose registers from REGCACHE and store them
  63. in GREGS. */
  64. static void
  65. m68kbsd_collect_gregset (const struct regcache *regcache,
  66. void *gregs, int regnum)
  67. {
  68. gdb_byte *regs = (gdb_byte *) gregs;
  69. int i;
  70. for (i = M68K_D0_REGNUM; i <= M68K_PC_REGNUM; i++)
  71. {
  72. if (regnum == -1 || regnum == i)
  73. regcache->raw_collect (i, regs + i * 4);
  74. }
  75. }
  76. /* Collect the floating-point registers from REGCACHE and store them
  77. in FPREGS. */
  78. static void
  79. m68kbsd_collect_fpregset (struct regcache *regcache,
  80. void *fpregs, int regnum)
  81. {
  82. struct gdbarch *gdbarch = regcache->arch ();
  83. char *regs = fpregs;
  84. int i;
  85. for (i = M68K_FP0_REGNUM; i <= M68K_FPI_REGNUM; i++)
  86. {
  87. if (regnum == -1 || regnum == i)
  88. regcache->raw_collect (i, regs + m68kbsd_fpreg_offset (gdbarch, i));
  89. }
  90. }
  91. /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
  92. for all registers (including the floating-point registers). */
  93. void
  94. m68k_bsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  95. {
  96. pid_t pid = regcache->ptid ().pid ();
  97. int lwp = regcache->ptid ().lwp ();
  98. if (regnum == -1 || m68kbsd_gregset_supplies_p (regnum))
  99. {
  100. struct reg regs;
  101. if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
  102. perror_with_name (_("Couldn't get registers"));
  103. m68kbsd_supply_gregset (regcache, &regs);
  104. }
  105. if (regnum == -1 || m68kbsd_fpregset_supplies_p (regnum))
  106. {
  107. struct fpreg fpregs;
  108. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
  109. perror_with_name (_("Couldn't get floating point status"));
  110. m68kbsd_supply_fpregset (regcache, &fpregs);
  111. }
  112. }
  113. /* Store register REGNUM back into the inferior. If REGNUM is -1, do
  114. this for all registers (including the floating-point registers). */
  115. void
  116. m68k_bsd_nat_target::store_registers (struct regcache *regcache, int regnum)
  117. {
  118. pid_t pid = regcache->ptid ().pid ();
  119. int lwp = regcache->ptid ().lwp ();
  120. if (regnum == -1 || m68kbsd_gregset_supplies_p (regnum))
  121. {
  122. struct reg regs;
  123. if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
  124. perror_with_name (_("Couldn't get registers"));
  125. m68kbsd_collect_gregset (regcache, &regs, regnum);
  126. if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
  127. perror_with_name (_("Couldn't write registers"));
  128. }
  129. if (regnum == -1 || m68kbsd_fpregset_supplies_p (regnum))
  130. {
  131. struct fpreg fpregs;
  132. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
  133. perror_with_name (_("Couldn't get floating point status"));
  134. m68kbsd_collect_fpregset (regcache, &fpregs, regnum);
  135. if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
  136. perror_with_name (_("Couldn't write floating point status"));
  137. }
  138. }
  139. /* Support for debugging kernel virtual memory images. */
  140. #include <machine/pcb.h>
  141. #include "bsd-kvm.h"
  142. /* OpenBSD doesn't have these. */
  143. #ifndef PCB_REGS_FP
  144. #define PCB_REGS_FP 10
  145. #endif
  146. #ifndef PCB_REGS_SP
  147. #define PCB_REGS_SP 11
  148. #endif
  149. static int
  150. m68kbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  151. {
  152. int regnum, tmp;
  153. int i = 0;
  154. /* The following is true for NetBSD 1.6.2:
  155. The pcb contains %d2...%d7, %a2...%a7 and %ps. This accounts for
  156. all callee-saved registers. From this information we reconstruct
  157. the register state as it would look when we just returned from
  158. cpu_switch(). */
  159. /* The stack pointer shouldn't be zero. */
  160. if (pcb->pcb_regs[PCB_REGS_SP] == 0)
  161. return 0;
  162. for (regnum = M68K_D2_REGNUM; regnum <= M68K_D7_REGNUM; regnum++)
  163. regcache->raw_supply (regnum, &pcb->pcb_regs[i++]);
  164. for (regnum = M68K_A2_REGNUM; regnum <= M68K_SP_REGNUM; regnum++)
  165. regcache->raw_supply (regnum, &pcb->pcb_regs[i++]);
  166. tmp = pcb->pcb_ps & 0xffff;
  167. regcache->raw_supply (M68K_PS_REGNUM, &tmp);
  168. read_memory (pcb->pcb_regs[PCB_REGS_FP] + 4, (gdb_byte *) &tmp, sizeof tmp);
  169. regcache->raw_supply (M68K_PC_REGNUM, &tmp);
  170. return 1;
  171. }
  172. void _initialize_m68kbsd_nat ();
  173. void
  174. _initialize_m68kbsd_nat ()
  175. {
  176. add_inf_child_target (&the_m68k_bsd_nat_target);
  177. /* Support debugging kernel virtual memory images. */
  178. bsd_kvm_add_target (m68kbsd_supply_pcb);
  179. }