ppc-fbsd-nat.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Native-dependent code for PowerPC's running FreeBSD, for GDB.
  2. Copyright (C) 2013-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. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "inferior.h"
  17. #include "regcache.h"
  18. #include <sys/types.h>
  19. #include <sys/procfs.h>
  20. #include <sys/ptrace.h>
  21. #include <sys/signal.h>
  22. #include <machine/frame.h>
  23. #include <machine/pcb.h>
  24. #include <machine/reg.h>
  25. #include "fbsd-nat.h"
  26. #include "gregset.h"
  27. #include "ppc-tdep.h"
  28. #include "ppc-fbsd-tdep.h"
  29. #include "inf-ptrace.h"
  30. #include "bsd-kvm.h"
  31. struct ppc_fbsd_nat_target final : public fbsd_nat_target
  32. {
  33. void fetch_registers (struct regcache *, int) override;
  34. void store_registers (struct regcache *, int) override;
  35. };
  36. static ppc_fbsd_nat_target the_ppc_fbsd_nat_target;
  37. /* Fill GDB's register array with the general-purpose register values
  38. in *GREGSETP. */
  39. void
  40. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  41. {
  42. const struct regset *regset = ppc_fbsd_gregset (sizeof (long));
  43. ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
  44. }
  45. /* Fill register REGNO (if a gpr) in *GREGSETP with the value in GDB's
  46. register array. If REGNO is -1 do it for all registers. */
  47. void
  48. fill_gregset (const struct regcache *regcache,
  49. gdb_gregset_t *gregsetp, int regno)
  50. {
  51. const struct regset *regset = ppc_fbsd_gregset (sizeof (long));
  52. if (regno == -1)
  53. memset (gregsetp, 0, sizeof (*gregsetp));
  54. ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
  55. }
  56. /* Fill GDB's register array with the floating-point register values
  57. in *FPREGSETP. */
  58. void
  59. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
  60. {
  61. const struct regset *regset = ppc_fbsd_fpregset ();
  62. ppc_supply_fpregset (regset, regcache, -1,
  63. fpregsetp, sizeof (*fpregsetp));
  64. }
  65. /* Fill register REGNO in *FGREGSETP with the value in GDB's
  66. register array. If REGNO is -1 do it for all registers. */
  67. void
  68. fill_fpregset (const struct regcache *regcache,
  69. gdb_fpregset_t *fpregsetp, int regno)
  70. {
  71. const struct regset *regset = ppc_fbsd_fpregset ();
  72. ppc_collect_fpregset (regset, regcache, regno,
  73. fpregsetp, sizeof (*fpregsetp));
  74. }
  75. /* Returns true if PT_GETFPREGS fetches this register. */
  76. static int
  77. getfpregs_supplies (struct gdbarch *gdbarch, int regno)
  78. {
  79. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  80. /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating
  81. point registers. Traditionally, GDB's register set has still
  82. listed the floating point registers for such machines, so this
  83. code is harmless. However, the new E500 port actually omits the
  84. floating point registers entirely from the register set --- they
  85. don't even have register numbers assigned to them.
  86. It's not clear to me how best to update this code, so this assert
  87. will alert the first person to encounter the NetBSD/E500
  88. combination to the problem. */
  89. gdb_assert (ppc_floating_point_unit_p (gdbarch));
  90. return ((regno >= tdep->ppc_fp0_regnum
  91. && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
  92. || regno == tdep->ppc_fpscr_regnum);
  93. }
  94. /* Fetch register REGNO from the child process. If REGNO is -1, do it
  95. for all registers. */
  96. void
  97. ppc_fbsd_nat_target::fetch_registers (struct regcache *regcache, int regno)
  98. {
  99. gdb_gregset_t regs;
  100. pid_t pid = regcache->ptid ().lwp ();
  101. if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  102. perror_with_name (_("Couldn't get registers"));
  103. supply_gregset (regcache, &regs);
  104. if (regno == -1 || getfpregs_supplies (regcache->arch (), regno))
  105. {
  106. const struct regset *fpregset = ppc_fbsd_fpregset ();
  107. gdb_fpregset_t fpregs;
  108. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  109. perror_with_name (_("Couldn't get FP registers"));
  110. ppc_supply_fpregset (fpregset, regcache, regno, &fpregs, sizeof fpregs);
  111. }
  112. }
  113. /* Store register REGNO back into the child process. If REGNO is -1,
  114. do this for all registers. */
  115. void
  116. ppc_fbsd_nat_target::store_registers (struct regcache *regcache, int regno)
  117. {
  118. gdb_gregset_t regs;
  119. pid_t pid = regcache->ptid ().lwp ();
  120. if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  121. perror_with_name (_("Couldn't get registers"));
  122. fill_gregset (regcache, &regs, regno);
  123. if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  124. perror_with_name (_("Couldn't write registers"));
  125. if (regno == -1 || getfpregs_supplies (regcache->arch (), regno))
  126. {
  127. gdb_fpregset_t fpregs;
  128. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  129. perror_with_name (_("Couldn't get FP registers"));
  130. fill_fpregset (regcache, &fpregs, regno);
  131. if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  132. perror_with_name (_("Couldn't set FP registers"));
  133. }
  134. }
  135. /* Architecture specific function that reconstructs the
  136. register state from PCB (Process Control Block) and supplies it
  137. to REGCACHE. */
  138. static int
  139. ppcfbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  140. {
  141. struct gdbarch *gdbarch = regcache->arch ();
  142. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  143. int i, regnum;
  144. /* The stack pointer shouldn't be zero. */
  145. if (pcb->pcb_sp == 0)
  146. return 0;
  147. regcache->raw_supply (gdbarch_sp_regnum (gdbarch), &pcb->pcb_sp);
  148. regcache->raw_supply (tdep->ppc_cr_regnum, &pcb->pcb_cr);
  149. regcache->raw_supply (tdep->ppc_lr_regnum, &pcb->pcb_lr);
  150. for (i = 0, regnum = tdep->ppc_gp0_regnum + 14; i < 20; i++, regnum++)
  151. regcache->raw_supply (regnum, &pcb->pcb_context[i]);
  152. return 1;
  153. }
  154. void _initialize_ppcfbsd_nat ();
  155. void
  156. _initialize_ppcfbsd_nat ()
  157. {
  158. add_inf_child_target (&the_ppc_fbsd_nat_target);
  159. /* Support debugging kernel virtual memory images. */
  160. bsd_kvm_add_target (ppcfbsd_supply_pcb);
  161. }