amd64-ravenscar-thread.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Ravenscar x86-64 target support.
  2. Copyright (C) 2020-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 "gdbarch.h"
  16. #include "gdbcore.h"
  17. #include "regcache.h"
  18. #include "amd64-tdep.h"
  19. #include "inferior.h"
  20. #include "ravenscar-thread.h"
  21. #include "amd64-ravenscar-thread.h"
  22. struct amd64_ravenscar_ops : public ravenscar_arch_ops
  23. {
  24. void fetch_registers (struct regcache *regcache, int regnum) override;
  25. void store_registers (struct regcache *regcache, int regnum) override;
  26. private:
  27. /* Return the offset of the register in the context buffer. */
  28. int register_offset (struct gdbarch *arch, int regnum);
  29. };
  30. /* x86-64 Ravenscar stores registers as:
  31. type Context_Buffer is record
  32. RIP : System.Address;
  33. RFLAGS : EFLAGS;
  34. RSP : System.Address;
  35. RBX : System.Address;
  36. RBP : System.Address;
  37. R12 : System.Address;
  38. R13 : System.Address;
  39. R14 : System.Address;
  40. R15 : System.Address;
  41. end record;
  42. */
  43. static const int register_layout[] =
  44. {
  45. AMD64_RIP_REGNUM,
  46. AMD64_EFLAGS_REGNUM,
  47. AMD64_RSP_REGNUM,
  48. AMD64_RBX_REGNUM,
  49. AMD64_RBP_REGNUM,
  50. AMD64_R12_REGNUM,
  51. AMD64_R13_REGNUM,
  52. AMD64_R14_REGNUM,
  53. AMD64_R15_REGNUM,
  54. };
  55. int
  56. amd64_ravenscar_ops::register_offset (struct gdbarch *arch, int regnum)
  57. {
  58. for (int i = 0; i < ARRAY_SIZE (register_layout); ++i)
  59. if (register_layout[i] == regnum)
  60. return i * 8;
  61. /* Not saved. */
  62. return -1;
  63. }
  64. /* Supply register REGNUM, which has been saved at REGISTER_ADDR, to
  65. the regcache. */
  66. static void
  67. supply_register_at_address (struct regcache *regcache, int regnum,
  68. CORE_ADDR register_addr)
  69. {
  70. struct gdbarch *gdbarch = regcache->arch ();
  71. int buf_size = register_size (gdbarch, regnum);
  72. gdb_byte *buf;
  73. buf = (gdb_byte *) alloca (buf_size);
  74. read_memory (register_addr, buf, buf_size);
  75. regcache->raw_supply (regnum, buf);
  76. }
  77. void
  78. amd64_ravenscar_ops::fetch_registers (struct regcache *regcache, int regnum)
  79. {
  80. struct gdbarch *gdbarch = regcache->arch ();
  81. const int num_regs = gdbarch_num_regs (gdbarch);
  82. int current_regnum;
  83. CORE_ADDR current_address;
  84. CORE_ADDR thread_descriptor_address;
  85. /* The tid is the thread_id field, which is a pointer to the thread. */
  86. thread_descriptor_address = (CORE_ADDR) inferior_ptid.tid ();
  87. /* Read registers. */
  88. for (current_regnum = 0; current_regnum < num_regs; current_regnum++)
  89. {
  90. int offset = register_offset (gdbarch, current_regnum);
  91. if (offset != -1)
  92. {
  93. current_address = thread_descriptor_address + offset;
  94. supply_register_at_address (regcache, current_regnum,
  95. current_address);
  96. }
  97. }
  98. }
  99. void
  100. amd64_ravenscar_ops::store_registers (struct regcache *regcache, int regnum)
  101. {
  102. struct gdbarch *gdbarch = regcache->arch ();
  103. int buf_size = register_size (gdbarch, regnum);
  104. gdb_byte buf[buf_size];
  105. CORE_ADDR register_address;
  106. int offset = register_offset (gdbarch, regnum);
  107. if (offset != -1)
  108. {
  109. register_address = inferior_ptid.tid () + offset;
  110. regcache->raw_collect (regnum, buf);
  111. write_memory (register_address,
  112. buf,
  113. buf_size);
  114. }
  115. }
  116. /* The ravenscar_arch_ops vector for AMD64 targets. */
  117. static struct amd64_ravenscar_ops amd64_ravenscar_ops;
  118. /* Register amd64_ravenscar_ops in GDBARCH. */
  119. void
  120. register_amd64_ravenscar_ops (struct gdbarch *gdbarch)
  121. {
  122. set_gdbarch_ravenscar_ops (gdbarch, &amd64_ravenscar_ops);
  123. }