sparc-ravenscar-thread.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Ravenscar SPARC target support.
  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 "sparc-tdep.h"
  18. #include "inferior.h"
  19. #include "ravenscar-thread.h"
  20. #include "sparc-ravenscar-thread.h"
  21. #include "gdbarch.h"
  22. struct sparc_ravenscar_ops : public ravenscar_arch_ops
  23. {
  24. void fetch_registers (struct regcache *, int) override;
  25. void store_registers (struct regcache *, int) override;
  26. };
  27. /* Register offsets from a referenced address (exempli gratia the
  28. Thread_Descriptor). The referenced address depends on the register
  29. number. The Thread_Descriptor layout and the stack layout are documented
  30. in the GNAT sources, in sparc-bb.h. */
  31. static const int sparc_register_offsets[] =
  32. {
  33. /* G0 - G7 */
  34. -1, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
  35. /* O0 - O7 */
  36. 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
  37. /* L0 - L7 */
  38. 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
  39. /* I0 - I7 */
  40. 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
  41. /* F0 - F31 */
  42. 0x50, 0x54, 0x58, 0x5C, 0x60, 0x64, 0x68, 0x6C,
  43. 0x70, 0x74, 0x78, 0x7C, 0x80, 0x84, 0x88, 0x8C,
  44. 0x90, 0x94, 0x99, 0x9C, 0xA0, 0xA4, 0xA8, 0xAC,
  45. 0xB0, 0xB4, 0xBB, 0xBC, 0xC0, 0xC4, 0xC8, 0xCC,
  46. /* Y PSR WIM TBR PC NPC FPSR CPSR */
  47. 0x40, 0x20, 0x44, -1, 0x1C, -1, 0x4C, -1
  48. };
  49. /* supply register REGNUM, which has been saved on REGISTER_ADDR, to the
  50. regcache. */
  51. static void
  52. supply_register_at_address (struct regcache *regcache, int regnum,
  53. CORE_ADDR register_addr)
  54. {
  55. struct gdbarch *gdbarch = regcache->arch ();
  56. int buf_size = register_size (gdbarch, regnum);
  57. gdb_byte *buf;
  58. buf = (gdb_byte *) alloca (buf_size);
  59. read_memory (register_addr, buf, buf_size);
  60. regcache->raw_supply (regnum, buf);
  61. }
  62. /* Return true if, for a non-running thread, REGNUM has been saved on the
  63. stack. */
  64. static int
  65. register_on_stack_p (int regnum)
  66. {
  67. return (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_L7_REGNUM)
  68. || (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I7_REGNUM);
  69. }
  70. /* Return true if, for a non-running thread, REGNUM has been saved on the
  71. Thread_Descriptor. */
  72. static int
  73. register_in_thread_descriptor_p (int regnum)
  74. {
  75. return (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM)
  76. || (regnum == SPARC32_PSR_REGNUM)
  77. || (regnum >= SPARC_G1_REGNUM && regnum <= SPARC_G7_REGNUM)
  78. || (regnum == SPARC32_Y_REGNUM)
  79. || (regnum == SPARC32_WIM_REGNUM)
  80. || (regnum == SPARC32_FSR_REGNUM)
  81. || (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F0_REGNUM + 31)
  82. || (regnum == SPARC32_PC_REGNUM);
  83. }
  84. /* to_fetch_registers when inferior_ptid is different from the running
  85. thread. */
  86. void
  87. sparc_ravenscar_ops::fetch_registers (struct regcache *regcache, int regnum)
  88. {
  89. struct gdbarch *gdbarch = regcache->arch ();
  90. const int sp_regnum = gdbarch_sp_regnum (gdbarch);
  91. const int num_regs = gdbarch_num_regs (gdbarch);
  92. int current_regnum;
  93. CORE_ADDR current_address;
  94. CORE_ADDR thread_descriptor_address;
  95. ULONGEST stack_address;
  96. /* The tid is the thread_id field, which is a pointer to the thread. */
  97. thread_descriptor_address = (CORE_ADDR) inferior_ptid.tid ();
  98. /* Read the saved SP in the context buffer. */
  99. current_address = thread_descriptor_address
  100. + sparc_register_offsets [sp_regnum];
  101. supply_register_at_address (regcache, sp_regnum, current_address);
  102. regcache_cooked_read_unsigned (regcache, sp_regnum, &stack_address);
  103. /* Read registers. */
  104. for (current_regnum = 0; current_regnum < num_regs; current_regnum ++)
  105. {
  106. if (register_in_thread_descriptor_p (current_regnum))
  107. {
  108. current_address = thread_descriptor_address
  109. + sparc_register_offsets [current_regnum];
  110. supply_register_at_address (regcache, current_regnum,
  111. current_address);
  112. }
  113. else if (register_on_stack_p (current_regnum))
  114. {
  115. current_address = stack_address
  116. + sparc_register_offsets [current_regnum];
  117. supply_register_at_address (regcache, current_regnum,
  118. current_address);
  119. }
  120. }
  121. }
  122. /* to_store_registers when inferior_ptid is different from the running
  123. thread. */
  124. void
  125. sparc_ravenscar_ops::store_registers (struct regcache *regcache, int regnum)
  126. {
  127. struct gdbarch *gdbarch = regcache->arch ();
  128. int buf_size = register_size (gdbarch, regnum);
  129. gdb_byte buf[buf_size];
  130. ULONGEST register_address;
  131. if (register_in_thread_descriptor_p (regnum))
  132. register_address =
  133. inferior_ptid.tid () + sparc_register_offsets [regnum];
  134. else if (register_on_stack_p (regnum))
  135. {
  136. regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM,
  137. &register_address);
  138. register_address += sparc_register_offsets [regnum];
  139. }
  140. else
  141. return;
  142. regcache->raw_collect (regnum, buf);
  143. write_memory (register_address,
  144. buf,
  145. buf_size);
  146. }
  147. static struct sparc_ravenscar_ops sparc_ravenscar_ops;
  148. /* Register ravenscar_arch_ops in GDBARCH. */
  149. void
  150. register_sparc_ravenscar_ops (struct gdbarch *gdbarch)
  151. {
  152. set_gdbarch_ravenscar_ops (gdbarch, &sparc_ravenscar_ops);
  153. }