aarch64-ravenscar-thread.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Ravenscar Aarch64 target support.
  2. Copyright (C) 2017-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 "aarch64-tdep.h"
  18. #include "inferior.h"
  19. #include "ravenscar-thread.h"
  20. #include "aarch64-ravenscar-thread.h"
  21. #include "gdbarch.h"
  22. #define NO_OFFSET -1
  23. /* See aarch64-tdep.h for register numbers. */
  24. static const int aarch64_context_offsets[] =
  25. {
  26. /* X0 - X28 */
  27. NO_OFFSET, NO_OFFSET, NO_OFFSET, NO_OFFSET,
  28. NO_OFFSET, NO_OFFSET, NO_OFFSET, NO_OFFSET,
  29. NO_OFFSET, NO_OFFSET, NO_OFFSET, NO_OFFSET,
  30. NO_OFFSET, NO_OFFSET, NO_OFFSET, NO_OFFSET,
  31. NO_OFFSET, NO_OFFSET, NO_OFFSET, 0,
  32. 8, 16, 24, 32,
  33. 40, 48, 56, 64,
  34. 72,
  35. /* FP, LR, SP, PC, CPSR */
  36. /* Note that as task switch is synchronous, PC is in fact the LR here */
  37. 80, 88, 96, 88,
  38. NO_OFFSET,
  39. /* Q0 - Q31 */
  40. 112, 128, 144, 160,
  41. 176, 192, 208, 224,
  42. 240, 256, 272, 288,
  43. 304, 320, 336, 352,
  44. 368, 384, 400, 416,
  45. 432, 448, 464, 480,
  46. 496, 512, 528, 544,
  47. 560, 576, 592, 608,
  48. /* FPSR, FPCR */
  49. 104, 108,
  50. /* FPU Saved field */
  51. 624
  52. };
  53. /* The register layout info. */
  54. struct ravenscar_reg_info
  55. {
  56. /* A table providing the offset relative to the context structure
  57. where each register is saved. */
  58. const int *context_offsets;
  59. /* The number of elements in the context_offsets table above. */
  60. int context_offsets_size;
  61. };
  62. /* supply register REGNUM, which has been saved on REGISTER_ADDR, to the
  63. regcache. */
  64. static void
  65. supply_register_at_address (struct regcache *regcache, int regnum,
  66. CORE_ADDR register_addr)
  67. {
  68. struct gdbarch *gdbarch = regcache->arch ();
  69. int buf_size = register_size (gdbarch, regnum);
  70. gdb_byte *buf;
  71. buf = (gdb_byte *) alloca (buf_size);
  72. read_memory (register_addr, buf, buf_size);
  73. regcache->raw_supply (regnum, buf);
  74. }
  75. /* Return true if, for a non-running thread, REGNUM has been saved on the
  76. Thread_Descriptor. */
  77. static int
  78. register_in_thread_descriptor_p (const struct ravenscar_reg_info *reg_info,
  79. int regnum)
  80. {
  81. /* Check FPU registers */
  82. return (regnum < reg_info->context_offsets_size
  83. && reg_info->context_offsets[regnum] != NO_OFFSET);
  84. }
  85. /* to_fetch_registers when inferior_ptid is different from the running
  86. thread. */
  87. static void
  88. aarch64_ravenscar_generic_fetch_registers
  89. (const struct ravenscar_reg_info *reg_info,
  90. struct regcache *regcache, int regnum)
  91. {
  92. struct gdbarch *gdbarch = regcache->arch ();
  93. const int num_regs = gdbarch_num_regs (gdbarch);
  94. int current_regnum;
  95. CORE_ADDR current_address;
  96. CORE_ADDR thread_descriptor_address;
  97. /* The tid is the thread_id field, which is a pointer to the thread. */
  98. thread_descriptor_address = (CORE_ADDR) inferior_ptid.tid ();
  99. /* Read registers. */
  100. for (current_regnum = 0; current_regnum < num_regs; current_regnum++)
  101. {
  102. if (register_in_thread_descriptor_p (reg_info, current_regnum))
  103. {
  104. current_address = thread_descriptor_address
  105. + reg_info->context_offsets[current_regnum];
  106. supply_register_at_address (regcache, current_regnum,
  107. current_address);
  108. }
  109. }
  110. }
  111. /* to_store_registers when inferior_ptid is different from the running
  112. thread. */
  113. static void
  114. aarch64_ravenscar_generic_store_registers
  115. (const struct ravenscar_reg_info *reg_info,
  116. struct regcache *regcache, int regnum)
  117. {
  118. struct gdbarch *gdbarch = regcache->arch ();
  119. int buf_size = register_size (gdbarch, regnum);
  120. gdb_byte buf[buf_size];
  121. ULONGEST register_address;
  122. if (register_in_thread_descriptor_p (reg_info, regnum))
  123. register_address
  124. = inferior_ptid.tid () + reg_info->context_offsets [regnum];
  125. else
  126. return;
  127. regcache->raw_collect (regnum, buf);
  128. write_memory (register_address,
  129. buf,
  130. buf_size);
  131. }
  132. /* The ravenscar_reg_info for most Aarch64 targets. */
  133. static const struct ravenscar_reg_info aarch64_reg_info =
  134. {
  135. aarch64_context_offsets,
  136. ARRAY_SIZE (aarch64_context_offsets),
  137. };
  138. struct aarch64_ravenscar_ops : public ravenscar_arch_ops
  139. {
  140. void fetch_registers (struct regcache *regcache, int regnum) override
  141. {
  142. aarch64_ravenscar_generic_fetch_registers
  143. (&aarch64_reg_info, regcache, regnum);
  144. }
  145. void store_registers (struct regcache *regcache, int regnum) override
  146. {
  147. aarch64_ravenscar_generic_store_registers
  148. (&aarch64_reg_info, regcache, regnum);
  149. }
  150. };
  151. /* The ravenscar_arch_ops vector for most Aarch64 targets. */
  152. static struct aarch64_ravenscar_ops aarch64_ravenscar_ops;
  153. /* Register aarch64_ravenscar_ops in GDBARCH. */
  154. void
  155. register_aarch64_ravenscar_ops (struct gdbarch *gdbarch)
  156. {
  157. set_gdbarch_ravenscar_ops (gdbarch, &aarch64_ravenscar_ops);
  158. }