or1k-linux-nat.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Native-dependent code for GNU/Linux OpenRISC.
  2. Copyright (C) 2021-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 "regcache.h"
  16. #include "gregset.h"
  17. #include "linux-nat.h"
  18. #include "or1k-tdep.h"
  19. #include "or1k-linux-tdep.h"
  20. #include "inferior.h"
  21. #include "elf/common.h"
  22. #include <sys/ptrace.h>
  23. /* OpenRISC Linux native additions to the default linux support. */
  24. class or1k_linux_nat_target final : public linux_nat_target
  25. {
  26. public:
  27. /* Add our register access methods. */
  28. void fetch_registers (struct regcache *regcache, int regnum) override;
  29. void store_registers (struct regcache *regcache, int regnum) override;
  30. /* Read suitable target description. */
  31. const struct target_desc *read_description () override;
  32. };
  33. static or1k_linux_nat_target the_or1k_linux_nat_target;
  34. /* Copy general purpose register REGNUM (or all gp regs if REGNUM == -1)
  35. from regset GREGS into REGCACHE. */
  36. static void
  37. supply_gregset_regnum (struct regcache *regcache, const prgregset_t *gregs,
  38. int regnum)
  39. {
  40. int i;
  41. const elf_greg_t *regp = *gregs;
  42. /* Access all registers */
  43. if (regnum == -1)
  44. {
  45. /* We fill the general purpose registers. */
  46. for (i = OR1K_ZERO_REGNUM + 1; i < OR1K_MAX_GPR_REGS; i++)
  47. regcache->raw_supply (i, regp + i);
  48. /* Supply OR1K_NPC_REGNUM from index 32. */
  49. regcache->raw_supply (OR1K_NPC_REGNUM, regp + 32);
  50. /* Fill the inaccessible zero register with zero. */
  51. regcache->raw_supply_zeroed (0);
  52. }
  53. else if (regnum == OR1K_ZERO_REGNUM)
  54. regcache->raw_supply_zeroed (0);
  55. else if (regnum == OR1K_NPC_REGNUM)
  56. regcache->raw_supply (OR1K_NPC_REGNUM, regp + 32);
  57. else if (regnum > OR1K_ZERO_REGNUM && regnum < OR1K_MAX_GPR_REGS)
  58. regcache->raw_supply (regnum, regp + regnum);
  59. }
  60. /* Copy all general purpose registers from regset GREGS into REGCACHE. */
  61. void
  62. supply_gregset (struct regcache *regcache, const prgregset_t *gregs)
  63. {
  64. supply_gregset_regnum (regcache, gregs, -1);
  65. }
  66. /* Copy general purpose register REGNUM (or all gp regs if REGNUM == -1)
  67. from REGCACHE into regset GREGS. */
  68. void
  69. fill_gregset (const struct regcache *regcache, prgregset_t *gregs, int regnum)
  70. {
  71. elf_greg_t *regp = *gregs;
  72. if (regnum == -1)
  73. {
  74. /* We fill the general purpose registers. */
  75. for (int i = OR1K_ZERO_REGNUM + 1; i < OR1K_MAX_GPR_REGS; i++)
  76. regcache->raw_collect (i, regp + i);
  77. regcache->raw_collect (OR1K_NPC_REGNUM, regp + 32);
  78. }
  79. else if (regnum == OR1K_ZERO_REGNUM)
  80. /* Nothing to do here. */
  81. ;
  82. else if (regnum > OR1K_ZERO_REGNUM && regnum < OR1K_MAX_GPR_REGS)
  83. regcache->raw_collect (regnum, regp + regnum);
  84. else if (regnum == OR1K_NPC_REGNUM)
  85. regcache->raw_collect (OR1K_NPC_REGNUM, regp + 32);
  86. }
  87. /* Transfering floating-point registers between GDB, inferiors and cores.
  88. Since OpenRISC floating-point registers are the same as GPRs these do
  89. nothing. */
  90. void
  91. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
  92. {
  93. }
  94. void
  95. fill_fpregset (const struct regcache *regcache,
  96. gdb_fpregset_t *fpregs, int regno)
  97. {
  98. }
  99. /* Return a target description for the current target. */
  100. const struct target_desc *
  101. or1k_linux_nat_target::read_description ()
  102. {
  103. return tdesc_or1k_linux;
  104. }
  105. /* Fetch REGNUM (or all registers if REGNUM == -1) from the target
  106. into REGCACHE using PTRACE_GETREGSET. */
  107. void
  108. or1k_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  109. {
  110. int tid;
  111. tid = get_ptrace_pid (regcache->ptid());
  112. if ((regnum >= OR1K_ZERO_REGNUM && regnum < OR1K_MAX_GPR_REGS)
  113. || (regnum == OR1K_NPC_REGNUM)
  114. || (regnum == -1))
  115. {
  116. struct iovec iov;
  117. elf_gregset_t regs;
  118. iov.iov_base = &regs;
  119. iov.iov_len = sizeof (regs);
  120. if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS,
  121. (PTRACE_TYPE_ARG3) &iov) == -1)
  122. perror_with_name (_("Couldn't get registers"));
  123. else
  124. supply_gregset_regnum (regcache, &regs, regnum);
  125. }
  126. /* Access to other SPRs has potential security issues, don't support them for
  127. now. */
  128. }
  129. /* Store REGNUM (or all registers if REGNUM == -1) to the target
  130. from REGCACHE using PTRACE_SETREGSET. */
  131. void
  132. or1k_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
  133. {
  134. int tid;
  135. tid = get_ptrace_pid (regcache->ptid ());
  136. if ((regnum >= OR1K_ZERO_REGNUM && regnum < OR1K_MAX_GPR_REGS)
  137. || (regnum == OR1K_NPC_REGNUM)
  138. || (regnum == -1))
  139. {
  140. struct iovec iov;
  141. elf_gregset_t regs;
  142. iov.iov_base = &regs;
  143. iov.iov_len = sizeof (regs);
  144. if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS,
  145. (PTRACE_TYPE_ARG3) &iov) == -1)
  146. perror_with_name (_("Couldn't get registers"));
  147. else
  148. {
  149. fill_gregset (regcache, &regs, regnum);
  150. if (ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS,
  151. (PTRACE_TYPE_ARG3) &iov) == -1)
  152. perror_with_name (_("Couldn't set registers"));
  153. }
  154. }
  155. /* Access to SPRs has potential security issues, don't support them for
  156. now. */
  157. }
  158. /* Initialize OpenRISC Linux native support. */
  159. void _initialize_or1k_linux_nat ();
  160. void
  161. _initialize_or1k_linux_nat ()
  162. {
  163. /* Register the target. */
  164. linux_target = &the_or1k_linux_nat_target;
  165. add_inf_child_target (&the_or1k_linux_nat_target);
  166. }