hppa-linux-nat.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* Functions specific to running GDB native on HPPA running GNU/Linux.
  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. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "regcache.h"
  17. #include "inferior.h"
  18. #include "target.h"
  19. #include "linux-nat.h"
  20. #include "inf-ptrace.h"
  21. #include "gdbarch.h"
  22. #include <sys/procfs.h>
  23. #include "nat/gdb_ptrace.h"
  24. #include <linux/version.h>
  25. #include <asm/ptrace.h>
  26. #include "hppa-linux-offsets.h"
  27. #include "hppa-tdep.h"
  28. class hppa_linux_nat_target final : public linux_nat_target
  29. {
  30. public:
  31. /* Add our register access methods. */
  32. void fetch_registers (struct regcache *, int) override;
  33. void store_registers (struct regcache *, int) override;
  34. };
  35. static hppa_linux_nat_target the_hppa_linux_nat_target;
  36. /* Prototypes for supply_gregset etc. */
  37. #include "gregset.h"
  38. /* These must match the order of the register names.
  39. Some sort of lookup table is needed because the offsets associated
  40. with the registers are all over the board. */
  41. static const int u_offsets[] =
  42. {
  43. /* general registers */
  44. -1,
  45. PT_GR1,
  46. PT_GR2,
  47. PT_GR3,
  48. PT_GR4,
  49. PT_GR5,
  50. PT_GR6,
  51. PT_GR7,
  52. PT_GR8,
  53. PT_GR9,
  54. PT_GR10,
  55. PT_GR11,
  56. PT_GR12,
  57. PT_GR13,
  58. PT_GR14,
  59. PT_GR15,
  60. PT_GR16,
  61. PT_GR17,
  62. PT_GR18,
  63. PT_GR19,
  64. PT_GR20,
  65. PT_GR21,
  66. PT_GR22,
  67. PT_GR23,
  68. PT_GR24,
  69. PT_GR25,
  70. PT_GR26,
  71. PT_GR27,
  72. PT_GR28,
  73. PT_GR29,
  74. PT_GR30,
  75. PT_GR31,
  76. PT_SAR,
  77. PT_IAOQ0,
  78. PT_IASQ0,
  79. PT_IAOQ1,
  80. PT_IASQ1,
  81. -1, /* eiem */
  82. PT_IIR,
  83. PT_ISR,
  84. PT_IOR,
  85. PT_PSW,
  86. -1, /* goto */
  87. PT_SR4,
  88. PT_SR0,
  89. PT_SR1,
  90. PT_SR2,
  91. PT_SR3,
  92. PT_SR5,
  93. PT_SR6,
  94. PT_SR7,
  95. -1, /* cr0 */
  96. -1, /* pid0 */
  97. -1, /* pid1 */
  98. -1, /* ccr */
  99. -1, /* pid2 */
  100. -1, /* pid3 */
  101. -1, /* cr24 */
  102. -1, /* cr25 */
  103. -1, /* cr26 */
  104. PT_CR27,
  105. -1, /* cr28 */
  106. -1, /* cr29 */
  107. -1, /* cr30 */
  108. /* Floating point regs. */
  109. PT_FR0, PT_FR0 + 4,
  110. PT_FR1, PT_FR1 + 4,
  111. PT_FR2, PT_FR2 + 4,
  112. PT_FR3, PT_FR3 + 4,
  113. PT_FR4, PT_FR4 + 4,
  114. PT_FR5, PT_FR5 + 4,
  115. PT_FR6, PT_FR6 + 4,
  116. PT_FR7, PT_FR7 + 4,
  117. PT_FR8, PT_FR8 + 4,
  118. PT_FR9, PT_FR9 + 4,
  119. PT_FR10, PT_FR10 + 4,
  120. PT_FR11, PT_FR11 + 4,
  121. PT_FR12, PT_FR12 + 4,
  122. PT_FR13, PT_FR13 + 4,
  123. PT_FR14, PT_FR14 + 4,
  124. PT_FR15, PT_FR15 + 4,
  125. PT_FR16, PT_FR16 + 4,
  126. PT_FR17, PT_FR17 + 4,
  127. PT_FR18, PT_FR18 + 4,
  128. PT_FR19, PT_FR19 + 4,
  129. PT_FR20, PT_FR20 + 4,
  130. PT_FR21, PT_FR21 + 4,
  131. PT_FR22, PT_FR22 + 4,
  132. PT_FR23, PT_FR23 + 4,
  133. PT_FR24, PT_FR24 + 4,
  134. PT_FR25, PT_FR25 + 4,
  135. PT_FR26, PT_FR26 + 4,
  136. PT_FR27, PT_FR27 + 4,
  137. PT_FR28, PT_FR28 + 4,
  138. PT_FR29, PT_FR29 + 4,
  139. PT_FR30, PT_FR30 + 4,
  140. PT_FR31, PT_FR31 + 4,
  141. };
  142. static CORE_ADDR
  143. hppa_linux_register_addr (int regno, CORE_ADDR blockend)
  144. {
  145. CORE_ADDR addr;
  146. if ((unsigned) regno >= ARRAY_SIZE (u_offsets))
  147. error (_("Invalid register number %d."), regno);
  148. if (u_offsets[regno] == -1)
  149. addr = 0;
  150. else
  151. {
  152. addr = (CORE_ADDR) u_offsets[regno];
  153. }
  154. return addr;
  155. }
  156. /*
  157. * Registers saved in a coredump:
  158. * gr0..gr31
  159. * sr0..sr7
  160. * iaoq0..iaoq1
  161. * iasq0..iasq1
  162. * sar, iir, isr, ior, ipsw
  163. * cr0, cr24..cr31
  164. * cr8,9,12,13
  165. * cr10, cr15
  166. */
  167. #define GR_REGNUM(_n) (HPPA_R0_REGNUM+_n)
  168. #define TR_REGNUM(_n) (HPPA_TR0_REGNUM+_n)
  169. static const int greg_map[] =
  170. {
  171. GR_REGNUM(0), GR_REGNUM(1), GR_REGNUM(2), GR_REGNUM(3),
  172. GR_REGNUM(4), GR_REGNUM(5), GR_REGNUM(6), GR_REGNUM(7),
  173. GR_REGNUM(8), GR_REGNUM(9), GR_REGNUM(10), GR_REGNUM(11),
  174. GR_REGNUM(12), GR_REGNUM(13), GR_REGNUM(14), GR_REGNUM(15),
  175. GR_REGNUM(16), GR_REGNUM(17), GR_REGNUM(18), GR_REGNUM(19),
  176. GR_REGNUM(20), GR_REGNUM(21), GR_REGNUM(22), GR_REGNUM(23),
  177. GR_REGNUM(24), GR_REGNUM(25), GR_REGNUM(26), GR_REGNUM(27),
  178. GR_REGNUM(28), GR_REGNUM(29), GR_REGNUM(30), GR_REGNUM(31),
  179. HPPA_SR4_REGNUM+1, HPPA_SR4_REGNUM+2, HPPA_SR4_REGNUM+3, HPPA_SR4_REGNUM+4,
  180. HPPA_SR4_REGNUM, HPPA_SR4_REGNUM+5, HPPA_SR4_REGNUM+6, HPPA_SR4_REGNUM+7,
  181. HPPA_PCOQ_HEAD_REGNUM, HPPA_PCOQ_TAIL_REGNUM,
  182. HPPA_PCSQ_HEAD_REGNUM, HPPA_PCSQ_TAIL_REGNUM,
  183. HPPA_SAR_REGNUM, HPPA_IIR_REGNUM, HPPA_ISR_REGNUM, HPPA_IOR_REGNUM,
  184. HPPA_IPSW_REGNUM, HPPA_RCR_REGNUM,
  185. TR_REGNUM(0), TR_REGNUM(1), TR_REGNUM(2), TR_REGNUM(3),
  186. TR_REGNUM(4), TR_REGNUM(5), TR_REGNUM(6), TR_REGNUM(7),
  187. HPPA_PID0_REGNUM, HPPA_PID1_REGNUM, HPPA_PID2_REGNUM, HPPA_PID3_REGNUM,
  188. HPPA_CCR_REGNUM, HPPA_EIEM_REGNUM,
  189. };
  190. /* Fetch one register. */
  191. static void
  192. fetch_register (struct regcache *regcache, int regno)
  193. {
  194. struct gdbarch *gdbarch = regcache->arch ();
  195. pid_t tid;
  196. int val;
  197. if (gdbarch_cannot_fetch_register (gdbarch, regno))
  198. {
  199. regcache->raw_supply (regno, NULL);
  200. return;
  201. }
  202. tid = get_ptrace_pid (regcache->ptid ());
  203. errno = 0;
  204. val = ptrace (PTRACE_PEEKUSER, tid, hppa_linux_register_addr (regno, 0), 0);
  205. if (errno != 0)
  206. error (_("Couldn't read register %s (#%d): %s."),
  207. gdbarch_register_name (gdbarch, regno),
  208. regno, safe_strerror (errno));
  209. regcache->raw_supply (regno, &val);
  210. }
  211. /* Store one register. */
  212. static void
  213. store_register (const struct regcache *regcache, int regno)
  214. {
  215. struct gdbarch *gdbarch = regcache->arch ();
  216. pid_t tid;
  217. int val;
  218. if (gdbarch_cannot_store_register (gdbarch, regno))
  219. return;
  220. tid = get_ptrace_pid (regcache->ptid ());
  221. errno = 0;
  222. regcache->raw_collect (regno, &val);
  223. ptrace (PTRACE_POKEUSER, tid, hppa_linux_register_addr (regno, 0), val);
  224. if (errno != 0)
  225. error (_("Couldn't write register %s (#%d): %s."),
  226. gdbarch_register_name (gdbarch, regno),
  227. regno, safe_strerror (errno));
  228. }
  229. /* Fetch registers from the child process. Fetch all registers if
  230. regno == -1, otherwise fetch all general registers or all floating
  231. point registers depending upon the value of regno. */
  232. void
  233. hppa_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
  234. {
  235. if (-1 == regno)
  236. {
  237. for (regno = 0;
  238. regno < gdbarch_num_regs (regcache->arch ());
  239. regno++)
  240. fetch_register (regcache, regno);
  241. }
  242. else
  243. {
  244. fetch_register (regcache, regno);
  245. }
  246. }
  247. /* Store registers back into the inferior. Store all registers if
  248. regno == -1, otherwise store all general registers or all floating
  249. point registers depending upon the value of regno. */
  250. void
  251. hppa_linux_nat_target::store_registers (struct regcache *regcache, int regno)
  252. {
  253. if (-1 == regno)
  254. {
  255. for (regno = 0;
  256. regno < gdbarch_num_regs (regcache->arch ());
  257. regno++)
  258. store_register (regcache, regno);
  259. }
  260. else
  261. {
  262. store_register (regcache, regno);
  263. }
  264. }
  265. /* Fill GDB's register array with the general-purpose register values
  266. in *gregsetp. */
  267. void
  268. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  269. {
  270. int i;
  271. const greg_t *regp = (const elf_greg_t *) gregsetp;
  272. for (i = 0; i < sizeof (greg_map) / sizeof (greg_map[0]); i++, regp++)
  273. {
  274. int regno = greg_map[i];
  275. regcache->raw_supply (regno, regp);
  276. }
  277. }
  278. /* Fill register regno (if it is a general-purpose register) in
  279. *gregsetp with the appropriate value from GDB's register array.
  280. If regno is -1, do this for all registers. */
  281. void
  282. fill_gregset (const struct regcache *regcache,
  283. gdb_gregset_t *gregsetp, int regno)
  284. {
  285. int i;
  286. for (i = 0; i < sizeof (greg_map) / sizeof (greg_map[0]); i++)
  287. {
  288. int mregno = greg_map[i];
  289. if (regno == -1 || regno == mregno)
  290. regcache->raw_collect (mregno, &(*gregsetp)[i]);
  291. }
  292. }
  293. /* Given a pointer to a floating point register set in /proc format
  294. (fpregset_t *), unpack the register contents and supply them as gdb's
  295. idea of the current floating point register values. */
  296. void
  297. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
  298. {
  299. int regi;
  300. const char *from;
  301. for (regi = 0; regi <= 31; regi++)
  302. {
  303. from = (const char *) &((*fpregsetp)[regi]);
  304. regcache->raw_supply (2*regi + HPPA_FP0_REGNUM, from);
  305. regcache->raw_supply (2*regi + HPPA_FP0_REGNUM + 1, from + 4);
  306. }
  307. }
  308. /* Given a pointer to a floating point register set in /proc format
  309. (fpregset_t *), update the register specified by REGNO from gdb's idea
  310. of the current floating point register set. If REGNO is -1, update
  311. them all. */
  312. void
  313. fill_fpregset (const struct regcache *regcache,
  314. gdb_fpregset_t *fpregsetp, int regno)
  315. {
  316. int i;
  317. for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 32 * 2; i++)
  318. {
  319. /* Gross. fpregset_t is double, registers[x] has single
  320. precision reg. */
  321. char *to = (char *) &((*fpregsetp)[(i - HPPA_FP0_REGNUM) / 2]);
  322. if ((i - HPPA_FP0_REGNUM) & 1)
  323. to += 4;
  324. regcache->raw_collect (i, to);
  325. }
  326. }
  327. void _initialize_hppa_linux_nat ();
  328. void
  329. _initialize_hppa_linux_nat ()
  330. {
  331. /* Register the target. */
  332. linux_target = &the_hppa_linux_nat_target;
  333. add_inf_child_target (&the_hppa_linux_nat_target);
  334. }