ppc-netbsd-nat.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Native-dependent code for NetBSD/powerpc.
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. Contributed by Wasabi Systems, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* We define this to get types like register_t. */
  16. #define _KERNTYPES
  17. #include "defs.h"
  18. #include <sys/types.h>
  19. #include <sys/ptrace.h>
  20. #include <machine/reg.h>
  21. #include <machine/frame.h>
  22. #include <machine/pcb.h>
  23. #include "gdbcore.h"
  24. #include "inferior.h"
  25. #include "regcache.h"
  26. #include "ppc-tdep.h"
  27. #include "ppc-netbsd-tdep.h"
  28. #include "bsd-kvm.h"
  29. #include "inf-ptrace.h"
  30. #include "netbsd-nat.h"
  31. struct ppc_nbsd_nat_target final : public nbsd_nat_target
  32. {
  33. void fetch_registers (struct regcache *, int) override;
  34. void store_registers (struct regcache *, int) override;
  35. };
  36. static ppc_nbsd_nat_target the_ppc_nbsd_nat_target;
  37. /* Returns true if PT_GETREGS fetches this register. */
  38. static int
  39. getregs_supplies (struct gdbarch *gdbarch, int regnum)
  40. {
  41. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  42. return ((regnum >= tdep->ppc_gp0_regnum
  43. && regnum < tdep->ppc_gp0_regnum + ppc_num_gprs)
  44. || regnum == tdep->ppc_lr_regnum
  45. || regnum == tdep->ppc_cr_regnum
  46. || regnum == tdep->ppc_xer_regnum
  47. || regnum == tdep->ppc_ctr_regnum
  48. || regnum == gdbarch_pc_regnum (gdbarch));
  49. }
  50. /* Like above, but for PT_GETFPREGS. */
  51. static int
  52. getfpregs_supplies (struct gdbarch *gdbarch, int regnum)
  53. {
  54. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  55. /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating
  56. point registers. Traditionally, GDB's register set has still
  57. listed the floating point registers for such machines, so this
  58. code is harmless. However, the new E500 port actually omits the
  59. floating point registers entirely from the register set --- they
  60. don't even have register numbers assigned to them.
  61. It's not clear to me how best to update this code, so this assert
  62. will alert the first person to encounter the NetBSD/E500
  63. combination to the problem. */
  64. gdb_assert (ppc_floating_point_unit_p (gdbarch));
  65. return ((regnum >= tdep->ppc_fp0_regnum
  66. && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs)
  67. || regnum == tdep->ppc_fpscr_regnum);
  68. }
  69. void
  70. ppc_nbsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  71. {
  72. struct gdbarch *gdbarch = regcache->arch ();
  73. pid_t pid = regcache->ptid ().pid ();
  74. int lwp = regcache->ptid ().lwp ();
  75. if (regnum == -1 || getregs_supplies (gdbarch, regnum))
  76. {
  77. struct reg regs;
  78. if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
  79. perror_with_name (_("Couldn't get registers"));
  80. ppc_supply_gregset (&ppcnbsd_gregset, regcache,
  81. regnum, &regs, sizeof regs);
  82. }
  83. if (regnum == -1 || getfpregs_supplies (gdbarch, regnum))
  84. {
  85. struct fpreg fpregs;
  86. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
  87. perror_with_name (_("Couldn't get FP registers"));
  88. ppc_supply_fpregset (&ppcnbsd_fpregset, regcache,
  89. regnum, &fpregs, sizeof fpregs);
  90. }
  91. }
  92. void
  93. ppc_nbsd_nat_target::store_registers (struct regcache *regcache, int regnum)
  94. {
  95. struct gdbarch *gdbarch = regcache->arch ();
  96. pid_t pid = regcache->ptid ().pid ();
  97. int lwp = regcache->ptid ().lwp ();
  98. if (regnum == -1 || getregs_supplies (gdbarch, 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. ppc_collect_gregset (&ppcnbsd_gregset, regcache,
  104. regnum, &regs, sizeof regs);
  105. if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
  106. perror_with_name (_("Couldn't write registers"));
  107. }
  108. if (regnum == -1 || getfpregs_supplies (gdbarch, regnum))
  109. {
  110. struct fpreg fpregs;
  111. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
  112. perror_with_name (_("Couldn't get FP registers"));
  113. ppc_collect_fpregset (&ppcnbsd_fpregset, regcache,
  114. regnum, &fpregs, sizeof fpregs);
  115. if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
  116. perror_with_name (_("Couldn't set FP registers"));
  117. }
  118. }
  119. static int
  120. ppcnbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  121. {
  122. struct switchframe sf;
  123. struct callframe cf;
  124. struct gdbarch *gdbarch = regcache->arch ();
  125. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  126. int i;
  127. /* The stack pointer shouldn't be zero. */
  128. if (pcb->pcb_sp == 0)
  129. return 0;
  130. read_memory (pcb->pcb_sp, (gdb_byte *)&sf, sizeof sf);
  131. regcache->raw_supply (tdep->ppc_cr_regnum, &sf.cr);
  132. regcache->raw_supply (tdep->ppc_gp0_regnum + 2, &sf.fixreg2);
  133. for (i = 0 ; i < 19 ; i++)
  134. regcache->raw_supply (tdep->ppc_gp0_regnum + 13 + i, &sf.fixreg[i]);
  135. read_memory(sf.sp, (gdb_byte *)&cf, sizeof(cf));
  136. regcache->raw_supply (tdep->ppc_gp0_regnum + 30, &cf.r30);
  137. regcache->raw_supply (tdep->ppc_gp0_regnum + 31, &cf.r31);
  138. regcache->raw_supply (tdep->ppc_gp0_regnum + 1, &cf.sp);
  139. read_memory(cf.sp, (gdb_byte *)&cf, sizeof(cf));
  140. regcache->raw_supply (tdep->ppc_lr_regnum, &cf.lr);
  141. regcache->raw_supply (gdbarch_pc_regnum (gdbarch), &cf.lr);
  142. return 1;
  143. }
  144. void _initialize_ppcnbsd_nat ();
  145. void
  146. _initialize_ppcnbsd_nat ()
  147. {
  148. /* Support debugging kernel virtual memory images. */
  149. bsd_kvm_add_target (ppcnbsd_supply_pcb);
  150. add_inf_child_target (&the_ppc_nbsd_nat_target);
  151. }