hppa-netbsd-nat.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Native-dependent code for NetBSD/hppa.
  2. Copyright (C) 2008-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 "inferior.h"
  16. #include "regcache.h"
  17. #include <sys/types.h>
  18. #include <sys/ptrace.h>
  19. #include <machine/reg.h>
  20. #include "hppa-tdep.h"
  21. #include "inf-ptrace.h"
  22. #include "netbsd-nat.h"
  23. class hppa_nbsd_nat_target final : public nbsd_nat_target
  24. {
  25. void fetch_registers (struct regcache *, int) override;
  26. void store_registers (struct regcache *, int) override;
  27. };
  28. static hppa_nbsd_nat_target the_hppa_nbsd_nat_target;
  29. static int
  30. hppanbsd_gregset_supplies_p (int regnum)
  31. {
  32. return ((regnum >= HPPA_R0_REGNUM && regnum <= HPPA_R31_REGNUM) ||
  33. (regnum >= HPPA_SAR_REGNUM && regnum <= HPPA_PCSQ_TAIL_REGNUM) ||
  34. regnum == HPPA_IPSW_REGNUM ||
  35. (regnum >= HPPA_SR4_REGNUM && regnum <= HPPA_SR4_REGNUM + 5));
  36. }
  37. static int
  38. hppanbsd_fpregset_supplies_p (int regnum)
  39. {
  40. return (regnum >= HPPA_FP0_REGNUM && regnum <= HPPA_FP31R_REGNUM);
  41. }
  42. /* Supply the general-purpose registers stored in GREGS to REGCACHE. */
  43. static void
  44. hppanbsd_supply_gregset (struct regcache *regcache, const void *gregs)
  45. {
  46. const char *regs = gregs;
  47. const int *r = gregs;
  48. int regnum;
  49. for (regnum = HPPA_R1_REGNUM; regnum <= HPPA_R31_REGNUM; regnum++)
  50. regcache->raw_supply (regnum, regs + regnum * 4);
  51. regcache->raw_supply (HPPA_SAR_REGNUM, regs + 32 * 4);
  52. regcache->raw_supply (HPPA_PCSQ_HEAD_REGNUM, regs + 33 * 4);
  53. regcache->raw_supply (HPPA_PCSQ_TAIL_REGNUM, regs + 34 * 4);
  54. regcache->raw_supply (HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4);
  55. regcache->raw_supply (HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4);
  56. regcache->raw_supply (HPPA_IPSW_REGNUM, regs);
  57. regcache->raw_supply (HPPA_SR4_REGNUM, regs + 41 * 4);
  58. regcache->raw_supply (HPPA_SR4_REGNUM + 1, regs + 37 * 4);
  59. regcache->raw_supply (HPPA_SR4_REGNUM + 2, regs + 38 * 4);
  60. regcache->raw_supply (HPPA_SR4_REGNUM + 3, regs + 39 * 4);
  61. regcache->raw_supply (HPPA_SR4_REGNUM + 4, regs + 40 * 4);
  62. }
  63. /* Supply the floating-point registers stored in FPREGS to REGCACHE. */
  64. static void
  65. hppanbsd_supply_fpregset (struct regcache *regcache, const void *fpregs)
  66. {
  67. const char *regs = fpregs;
  68. int regnum;
  69. for (regnum = HPPA_FP0_REGNUM; regnum <= HPPA_FP31R_REGNUM;
  70. regnum += 2, regs += 8)
  71. {
  72. regcache->raw_supply (regnum, regs);
  73. regcache->raw_supply (regnum + 1, regs + 4);
  74. }
  75. }
  76. /* Collect the general-purpose registers from REGCACHE and store them
  77. in GREGS. */
  78. static void
  79. hppanbsd_collect_gregset (const struct regcache *regcache,
  80. void *gregs, int regnum)
  81. {
  82. char *regs = gregs;
  83. int *r = gregs;
  84. int i;
  85. for (i = HPPA_R1_REGNUM; i <= HPPA_R31_REGNUM; i++)
  86. {
  87. if (regnum == -1 || regnum == i)
  88. regcache->raw_collect (i, regs + i * 4);
  89. }
  90. if (regnum == -1 || regnum == HPPA_IPSW_REGNUM)
  91. regcache->raw_collect (HPPA_IPSW_REGNUM, regs);
  92. if (regnum == -1 || regnum == HPPA_PCOQ_HEAD_REGNUM)
  93. regcache->raw_collect (HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4);
  94. if (regnum == -1 || regnum == HPPA_PCOQ_TAIL_REGNUM)
  95. regcache->raw_collect (HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4);
  96. if (regnum == -1 || regnum == HPPA_SAR_REGNUM)
  97. regcache->raw_collect (HPPA_SAR_REGNUM, regs + 32 * 4);
  98. if (regnum == -1 || regnum == HPPA_PCSQ_HEAD_REGNUM)
  99. regcache->raw_collect (HPPA_PCSQ_HEAD_REGNUM, regs + 33 * 4);
  100. if (regnum == -1 || regnum == HPPA_PCSQ_TAIL_REGNUM)
  101. regcache->raw_collect (HPPA_PCSQ_TAIL_REGNUM, regs + 34 * 4);
  102. if (regnum == -1 || regnum == HPPA_PCOQ_HEAD_REGNUM)
  103. regcache->raw_collect (HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4);
  104. if (regnum == -1 || regnum == HPPA_PCOQ_TAIL_REGNUM)
  105. regcache->raw_collect (HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4);
  106. if (regnum == -1 || regnum == HPPA_IPSW_REGNUM)
  107. regcache->raw_collect (HPPA_IPSW_REGNUM, regs);
  108. if (regnum == -1 || regnum == HPPA_SR4_REGNUM)
  109. regcache->raw_collect (HPPA_SR4_REGNUM, regs + 41 * 4);
  110. if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 1)
  111. regcache->raw_collect (HPPA_SR4_REGNUM + 1, regs + 37 * 4);
  112. if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 2)
  113. regcache->raw_collect (HPPA_SR4_REGNUM + 2, regs + 38 * 4);
  114. if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 3)
  115. regcache->raw_collect (HPPA_SR4_REGNUM + 3, regs + 39 * 4);
  116. if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 4)
  117. regcache->raw_collect (HPPA_SR4_REGNUM + 4, regs + 40 * 4);
  118. }
  119. /* Collect the floating-point registers from REGCACHE and store them
  120. in FPREGS. */
  121. static void
  122. hppanbsd_collect_fpregset (struct regcache *regcache,
  123. void *fpregs, int regnum)
  124. {
  125. char *regs = fpregs;
  126. int i;
  127. for (i = HPPA_FP0_REGNUM; i <= HPPA_FP31R_REGNUM; i += 2, regs += 8)
  128. {
  129. if (regnum == -1 || regnum == i || regnum == i + 1)
  130. {
  131. regcache->raw_collect (i, regs);
  132. regcache->raw_collect (i + 1, regs + 4);
  133. }
  134. }
  135. }
  136. /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
  137. for all registers (including the floating-point registers). */
  138. void
  139. hppa_nbsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  140. {
  141. pid_t pid = regcache->ptid ().pid ();
  142. int lwp = regcache->ptid ().lwp ();
  143. if (regnum == -1 || hppanbsd_gregset_supplies_p (regnum))
  144. {
  145. struct reg regs;
  146. if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
  147. perror_with_name (_("Couldn't get registers"));
  148. hppanbsd_supply_gregset (regcache, &regs);
  149. }
  150. if (regnum == -1 || hppanbsd_fpregset_supplies_p (regnum))
  151. {
  152. struct fpreg fpregs;
  153. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
  154. perror_with_name (_("Couldn't get floating point status"));
  155. hppanbsd_supply_fpregset (regcache, &fpregs);
  156. }
  157. }
  158. /* Store register REGNUM back into the inferior. If REGNUM is -1, do
  159. this for all registers (including the floating-point registers). */
  160. void
  161. hppa_nbsd_nat_target::store_registers (struct regcache *regcache, int regnum)
  162. {
  163. pid_t pid = regcache->ptid ().pid ();
  164. int lwp = regcache->ptid ().lwp ();
  165. if (regnum == -1 || hppanbsd_gregset_supplies_p (regnum))
  166. {
  167. struct reg regs;
  168. if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
  169. perror_with_name (_("Couldn't get registers"));
  170. hppanbsd_collect_gregset (regcache, &regs, regnum);
  171. if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
  172. perror_with_name (_("Couldn't write registers"));
  173. }
  174. if (regnum == -1 || hppanbsd_fpregset_supplies_p (regnum))
  175. {
  176. struct fpreg fpregs;
  177. if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
  178. perror_with_name (_("Couldn't get floating point status"));
  179. hppanbsd_collect_fpregset (regcache, &fpregs, regnum);
  180. if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
  181. perror_with_name (_("Couldn't write floating point status"));
  182. }
  183. }
  184. void _initialize_hppanbsd_nat ();
  185. void
  186. _initialize_hppanbsd_nat ()
  187. {
  188. add_inf_child_target (&the_hppa_nbsd_nat_target);
  189. }