m32r-linux-nat.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Native-dependent code for GNU/Linux m32r.
  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 "inferior.h"
  16. #include "gdbcore.h"
  17. #include "regcache.h"
  18. #include "linux-nat.h"
  19. #include "target.h"
  20. #include "nat/gdb_ptrace.h"
  21. #include <sys/user.h>
  22. #include <sys/procfs.h>
  23. #include "inf-ptrace.h"
  24. /* Prototypes for supply_gregset etc. */
  25. #include "gregset.h"
  26. #include "m32r-tdep.h"
  27. class m32r_linux_nat_target final : public linux_nat_target
  28. {
  29. public:
  30. /* Add our register access methods. */
  31. void fetch_registers (struct regcache *, int) override;
  32. void store_registers (struct regcache *, int) override;
  33. };
  34. static m32r_linux_nat_target the_m32r_linux_nat_target;
  35. /* Since EVB register is not available for native debug, we reduce
  36. the number of registers. */
  37. #define M32R_LINUX_NUM_REGS (M32R_NUM_REGS - 1)
  38. /* Mapping between the general-purpose registers in `struct user'
  39. format and GDB's register array layout. */
  40. static int regmap[] = {
  41. 4, 5, 6, 7, 0, 1, 2, 8,
  42. 9, 10, 11, 12, 13, 24, 25, 23,
  43. 19, 19, 26, 23, 22, 20, 16, 15
  44. };
  45. #define PSW_REGMAP 19
  46. #define BBPSW_REGMAP 21
  47. #define SPU_REGMAP 23
  48. #define SPI_REGMAP 26
  49. /* Doee (??) apply to the corresponding SET requests as well. */
  50. #define GETREGS_SUPPLIES(regno) (0 <= (regno) \
  51. && (regno) <= M32R_LINUX_NUM_REGS)
  52. /* Transfering the general-purpose registers between GDB, inferiors
  53. and core files. */
  54. /* Fill GDB's register array with the general-purpose register values
  55. in *GREGSETP. */
  56. void
  57. supply_gregset (struct regcache *regcache, const elf_gregset_t * gregsetp)
  58. {
  59. const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
  60. int i;
  61. unsigned long psw, bbpsw;
  62. psw = *(regp + PSW_REGMAP);
  63. bbpsw = *(regp + BBPSW_REGMAP);
  64. for (i = 0; i < M32R_LINUX_NUM_REGS; i++)
  65. {
  66. elf_greg_t regval;
  67. switch (i)
  68. {
  69. case PSW_REGNUM:
  70. regval = ((0x00c1 & bbpsw) << 8) | ((0xc100 & psw) >> 8);
  71. break;
  72. case CBR_REGNUM:
  73. regval = ((psw >> 8) & 1);
  74. break;
  75. default:
  76. regval = *(regp + regmap[i]);
  77. break;
  78. }
  79. if (i != M32R_SP_REGNUM)
  80. regcache->raw_supply (i, &regval);
  81. else if (psw & 0x8000)
  82. regcache->raw_supply (i, regp + SPU_REGMAP);
  83. else
  84. regcache->raw_supply (i, regp + SPI_REGMAP);
  85. }
  86. }
  87. /* Fetch all general-purpose registers from process/thread TID and
  88. store their values in GDB's register array. */
  89. static void
  90. fetch_regs (struct regcache *regcache, int tid)
  91. {
  92. elf_gregset_t regs;
  93. if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
  94. perror_with_name (_("Couldn't get registers"));
  95. supply_gregset (regcache, (const elf_gregset_t *) &regs);
  96. }
  97. /* Fill register REGNO (if it is a general-purpose register) in
  98. *GREGSETPS with the value in GDB's register array. If REGNO is -1,
  99. do this for all registers. */
  100. void
  101. fill_gregset (const struct regcache *regcache,
  102. elf_gregset_t * gregsetp, int regno)
  103. {
  104. elf_greg_t *regp = (elf_greg_t *) gregsetp;
  105. int i;
  106. unsigned long psw, bbpsw, tmp;
  107. psw = *(regp + PSW_REGMAP);
  108. bbpsw = *(regp + BBPSW_REGMAP);
  109. for (i = 0; i < M32R_LINUX_NUM_REGS; i++)
  110. {
  111. if (regno != -1 && regno != i)
  112. continue;
  113. if (i == CBR_REGNUM || i == PSW_REGNUM)
  114. continue;
  115. if (i == SPU_REGNUM || i == SPI_REGNUM)
  116. continue;
  117. if (i != M32R_SP_REGNUM)
  118. regcache->raw_collect (i, regp + regmap[i]);
  119. else if (psw & 0x8000)
  120. regcache->raw_collect (i, regp + SPU_REGMAP);
  121. else
  122. regcache->raw_collect (i, regp + SPI_REGMAP);
  123. }
  124. }
  125. /* Store all valid general-purpose registers in GDB's register array
  126. into the process/thread specified by TID. */
  127. static void
  128. store_regs (const struct regcache *regcache, int tid, int regno)
  129. {
  130. elf_gregset_t regs;
  131. if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
  132. perror_with_name (_("Couldn't get registers"));
  133. fill_gregset (regcache, &regs, regno);
  134. if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
  135. perror_with_name (_("Couldn't write registers"));
  136. }
  137. /* Transfering floating-point registers between GDB, inferiors and cores.
  138. Since M32R has no floating-point registers, these functions do nothing. */
  139. void
  140. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
  141. {
  142. }
  143. void
  144. fill_fpregset (const struct regcache *regcache,
  145. gdb_fpregset_t *fpregs, int regno)
  146. {
  147. }
  148. /* Transferring arbitrary registers between GDB and inferior. */
  149. /* Fetch register REGNO from the child process. If REGNO is -1, do
  150. this for all registers (including the floating point and SSE
  151. registers). */
  152. void
  153. m32r_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
  154. {
  155. pid_t tid = get_ptrace_pid (regcache->ptid ());
  156. /* Use the PTRACE_GETREGS request whenever possible, since it
  157. transfers more registers in one system call, and we'll cache the
  158. results. */
  159. if (regno == -1 || GETREGS_SUPPLIES (regno))
  160. {
  161. fetch_regs (regcache, tid);
  162. return;
  163. }
  164. internal_error (__FILE__, __LINE__,
  165. _("Got request for bad register number %d."), regno);
  166. }
  167. /* Store register REGNO back into the child process. If REGNO is -1,
  168. do this for all registers (including the floating point and SSE
  169. registers). */
  170. void
  171. m32r_linux_nat_target::store_registers (struct regcache *regcache, int regno)
  172. {
  173. pid_t tid = get_ptrace_pid (regcache->ptid ());
  174. /* Use the PTRACE_SETREGS request whenever possible, since it
  175. transfers more registers in one system call. */
  176. if (regno == -1 || GETREGS_SUPPLIES (regno))
  177. {
  178. store_regs (regcache, tid, regno);
  179. return;
  180. }
  181. internal_error (__FILE__, __LINE__,
  182. _("Got request to store bad register number %d."), regno);
  183. }
  184. void _initialize_m32r_linux_nat ();
  185. void
  186. _initialize_m32r_linux_nat ()
  187. {
  188. /* Register the target. */
  189. linux_target = &the_m32r_linux_nat_target;
  190. add_inf_child_target (&the_m32r_linux_nat_target);
  191. }