arm-netbsd-nat.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* Native-dependent code for BSD Unix running on ARM's, for GDB.
  2. Copyright (C) 1988-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 "target.h"
  21. #include <sys/types.h>
  22. #include <sys/ptrace.h>
  23. #include <sys/sysctl.h>
  24. #include <machine/reg.h>
  25. #include <machine/frame.h>
  26. #include "arm-tdep.h"
  27. #include "arm-netbsd-tdep.h"
  28. #include "aarch32-tdep.h"
  29. #include "inf-ptrace.h"
  30. #include "netbsd-nat.h"
  31. class arm_netbsd_nat_target final : public nbsd_nat_target
  32. {
  33. public:
  34. /* Add our register access methods. */
  35. void fetch_registers (struct regcache *, int) override;
  36. void store_registers (struct regcache *, int) override;
  37. const struct target_desc *read_description () override;
  38. };
  39. static arm_netbsd_nat_target the_arm_netbsd_nat_target;
  40. static void
  41. arm_supply_vfpregset (struct regcache *regcache, struct fpreg *fpregset)
  42. {
  43. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  44. if (tdep->vfp_register_count == 0)
  45. return;
  46. struct vfpreg &vfp = fpregset->fpr_vfp;
  47. for (int regno = 0; regno <= tdep->vfp_register_count; regno++)
  48. regcache->raw_supply (regno + ARM_D0_REGNUM, (char *) &vfp.vfp_regs[regno]);
  49. regcache->raw_supply (ARM_FPSCR_REGNUM, (char *) &vfp.vfp_fpscr);
  50. }
  51. static void
  52. fetch_register (struct regcache *regcache, int regno)
  53. {
  54. struct reg inferior_registers;
  55. int ret;
  56. int lwp = regcache->ptid ().lwp ();
  57. ret = ptrace (PT_GETREGS, regcache->ptid ().pid (),
  58. (PTRACE_TYPE_ARG3) &inferior_registers, lwp);
  59. if (ret < 0)
  60. {
  61. warning (_("unable to fetch general register"));
  62. return;
  63. }
  64. arm_nbsd_supply_gregset (nullptr, regcache, regno, &inferior_registers,
  65. sizeof (inferior_registers));
  66. }
  67. static void
  68. fetch_fp_register (struct regcache *regcache, int regno)
  69. {
  70. struct fpreg inferior_fp_registers;
  71. int lwp = regcache->ptid ().lwp ();
  72. int ret = ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
  73. (PTRACE_TYPE_ARG3) &inferior_fp_registers, lwp);
  74. struct vfpreg &vfp = inferior_fp_registers.fpr_vfp;
  75. if (ret < 0)
  76. {
  77. warning (_("unable to fetch floating-point register"));
  78. return;
  79. }
  80. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  81. if (regno == ARM_FPSCR_REGNUM && tdep->vfp_register_count != 0)
  82. regcache->raw_supply (ARM_FPSCR_REGNUM, (char *) &vfp.vfp_fpscr);
  83. else if (regno >= ARM_D0_REGNUM
  84. && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
  85. {
  86. regcache->raw_supply (regno,
  87. (char *) &vfp.vfp_regs[regno - ARM_D0_REGNUM]);
  88. }
  89. else
  90. warning (_("Invalid register number."));
  91. }
  92. static void
  93. fetch_fp_regs (struct regcache *regcache)
  94. {
  95. struct fpreg inferior_fp_registers;
  96. int lwp = regcache->ptid ().lwp ();
  97. int ret;
  98. int regno;
  99. ret = ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
  100. (PTRACE_TYPE_ARG3) &inferior_fp_registers, lwp);
  101. if (ret < 0)
  102. {
  103. warning (_("unable to fetch general registers"));
  104. return;
  105. }
  106. arm_supply_vfpregset (regcache, &inferior_fp_registers);
  107. }
  108. void
  109. arm_netbsd_nat_target::fetch_registers (struct regcache *regcache, int regno)
  110. {
  111. if (regno >= 0)
  112. {
  113. if (regno < ARM_F0_REGNUM || regno > ARM_FPS_REGNUM)
  114. fetch_register (regcache, regno);
  115. else
  116. fetch_fp_register (regcache, regno);
  117. }
  118. else
  119. {
  120. fetch_register (regcache, -1);
  121. fetch_fp_regs (regcache);
  122. }
  123. }
  124. static void
  125. store_register (const struct regcache *regcache, int regno)
  126. {
  127. struct gdbarch *gdbarch = regcache->arch ();
  128. struct reg inferior_registers;
  129. int lwp = regcache->ptid ().lwp ();
  130. int ret;
  131. ret = ptrace (PT_GETREGS, regcache->ptid ().pid (),
  132. (PTRACE_TYPE_ARG3) &inferior_registers, lwp);
  133. if (ret < 0)
  134. {
  135. warning (_("unable to fetch general registers"));
  136. return;
  137. }
  138. switch (regno)
  139. {
  140. case ARM_SP_REGNUM:
  141. regcache->raw_collect (ARM_SP_REGNUM, (char *) &inferior_registers.r_sp);
  142. break;
  143. case ARM_LR_REGNUM:
  144. regcache->raw_collect (ARM_LR_REGNUM, (char *) &inferior_registers.r_lr);
  145. break;
  146. case ARM_PC_REGNUM:
  147. if (arm_apcs_32)
  148. regcache->raw_collect (ARM_PC_REGNUM,
  149. (char *) &inferior_registers.r_pc);
  150. else
  151. {
  152. unsigned pc_val;
  153. regcache->raw_collect (ARM_PC_REGNUM, (char *) &pc_val);
  154. pc_val = gdbarch_addr_bits_remove (gdbarch, pc_val);
  155. inferior_registers.r_pc ^= gdbarch_addr_bits_remove
  156. (gdbarch, inferior_registers.r_pc);
  157. inferior_registers.r_pc |= pc_val;
  158. }
  159. break;
  160. case ARM_PS_REGNUM:
  161. if (arm_apcs_32)
  162. regcache->raw_collect (ARM_PS_REGNUM,
  163. (char *) &inferior_registers.r_cpsr);
  164. else
  165. {
  166. unsigned psr_val;
  167. regcache->raw_collect (ARM_PS_REGNUM, (char *) &psr_val);
  168. psr_val ^= gdbarch_addr_bits_remove (gdbarch, psr_val);
  169. inferior_registers.r_pc = gdbarch_addr_bits_remove
  170. (gdbarch, inferior_registers.r_pc);
  171. inferior_registers.r_pc |= psr_val;
  172. }
  173. break;
  174. default:
  175. regcache->raw_collect (regno, (char *) &inferior_registers.r[regno]);
  176. break;
  177. }
  178. ret = ptrace (PT_SETREGS, regcache->ptid ().pid (),
  179. (PTRACE_TYPE_ARG3) &inferior_registers, lwp);
  180. if (ret < 0)
  181. warning (_("unable to write register %d to inferior"), regno);
  182. }
  183. static void
  184. store_regs (const struct regcache *regcache)
  185. {
  186. struct gdbarch *gdbarch = regcache->arch ();
  187. struct reg inferior_registers;
  188. int lwp = regcache->ptid ().lwp ();
  189. int ret;
  190. int regno;
  191. for (regno = ARM_A1_REGNUM; regno < ARM_SP_REGNUM; regno++)
  192. regcache->raw_collect (regno, (char *) &inferior_registers.r[regno]);
  193. regcache->raw_collect (ARM_SP_REGNUM, (char *) &inferior_registers.r_sp);
  194. regcache->raw_collect (ARM_LR_REGNUM, (char *) &inferior_registers.r_lr);
  195. if (arm_apcs_32)
  196. {
  197. regcache->raw_collect (ARM_PC_REGNUM, (char *) &inferior_registers.r_pc);
  198. regcache->raw_collect (ARM_PS_REGNUM,
  199. (char *) &inferior_registers.r_cpsr);
  200. }
  201. else
  202. {
  203. unsigned pc_val;
  204. unsigned psr_val;
  205. regcache->raw_collect (ARM_PC_REGNUM, (char *) &pc_val);
  206. regcache->raw_collect (ARM_PS_REGNUM, (char *) &psr_val);
  207. pc_val = gdbarch_addr_bits_remove (gdbarch, pc_val);
  208. psr_val ^= gdbarch_addr_bits_remove (gdbarch, psr_val);
  209. inferior_registers.r_pc = pc_val | psr_val;
  210. }
  211. ret = ptrace (PT_SETREGS, regcache->ptid ().pid (),
  212. (PTRACE_TYPE_ARG3) &inferior_registers, lwp);
  213. if (ret < 0)
  214. warning (_("unable to store general registers"));
  215. }
  216. static void
  217. store_fp_register (const struct regcache *regcache, int regno)
  218. {
  219. struct fpreg inferior_fp_registers;
  220. int lwp = regcache->ptid ().lwp ();
  221. int ret = ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
  222. (PTRACE_TYPE_ARG3) &inferior_fp_registers, lwp);
  223. struct vfpreg &vfp = inferior_fp_registers.fpr_vfp;
  224. if (ret < 0)
  225. {
  226. warning (_("unable to fetch floating-point registers"));
  227. return;
  228. }
  229. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  230. if (regno == ARM_FPSCR_REGNUM && tdep->vfp_register_count != 0)
  231. regcache->raw_collect (ARM_FPSCR_REGNUM, (char *) &vfp.vfp_fpscr);
  232. else if (regno >= ARM_D0_REGNUM
  233. && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
  234. {
  235. regcache->raw_collect (regno,
  236. (char *) &vfp.vfp_regs[regno - ARM_D0_REGNUM]);
  237. }
  238. else
  239. warning (_("Invalid register number."));
  240. ret = ptrace (PT_SETFPREGS, regcache->ptid ().pid (),
  241. (PTRACE_TYPE_ARG3) &inferior_fp_registers, lwp);
  242. if (ret < 0)
  243. warning (_("unable to write register %d to inferior"), regno);
  244. }
  245. static void
  246. store_fp_regs (const struct regcache *regcache)
  247. {
  248. arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  249. int lwp = regcache->ptid ().lwp ();
  250. if (tdep->vfp_register_count == 0)
  251. return;
  252. struct fpreg fpregs;
  253. for (int regno = 0; regno <= tdep->vfp_register_count; regno++)
  254. regcache->raw_collect
  255. (regno + ARM_D0_REGNUM, (char *) &fpregs.fpr_vfp.vfp_regs[regno]);
  256. regcache->raw_collect (ARM_FPSCR_REGNUM,
  257. (char *) &fpregs.fpr_vfp.vfp_fpscr);
  258. int ret = ptrace (PT_SETFPREGS, regcache->ptid ().pid (),
  259. (PTRACE_TYPE_ARG3) &fpregs, lwp);
  260. if (ret < 0)
  261. warning (_("unable to store floating-point registers"));
  262. }
  263. void
  264. arm_netbsd_nat_target::store_registers (struct regcache *regcache, int regno)
  265. {
  266. if (regno >= 0)
  267. {
  268. if (regno < ARM_F0_REGNUM || regno > ARM_FPS_REGNUM)
  269. store_register (regcache, regno);
  270. else
  271. store_fp_register (regcache, regno);
  272. }
  273. else
  274. {
  275. store_regs (regcache);
  276. store_fp_regs (regcache);
  277. }
  278. }
  279. const struct target_desc *
  280. arm_netbsd_nat_target::read_description ()
  281. {
  282. int flag;
  283. size_t len = sizeof (flag);
  284. if (sysctlbyname("machdep.fpu_present", &flag, &len, NULL, 0) != 0
  285. || !flag)
  286. return arm_read_description (ARM_FP_TYPE_NONE);
  287. len = sizeof(flag);
  288. if (sysctlbyname("machdep.neon_present", &flag, &len, NULL, 0) == 0 && flag)
  289. return aarch32_read_description ();
  290. return arm_read_description (ARM_FP_TYPE_VFPV3);
  291. }
  292. void _initialize_arm_netbsd_nat ();
  293. void
  294. _initialize_arm_netbsd_nat ()
  295. {
  296. add_inf_child_target (&the_arm_netbsd_nat_target);
  297. }