linux-amd64-ipa.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* GNU/Linux/x86-64 specific low level interface, for the in-process
  2. agent library for GDB.
  3. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "server.h"
  16. #include <sys/mman.h>
  17. #include "tracepoint.h"
  18. #include "linux-x86-tdesc.h"
  19. #include "gdbsupport/x86-xstate.h"
  20. /* Defined in auto-generated file amd64-linux.c. */
  21. void init_registers_amd64_linux (void);
  22. extern const struct target_desc *tdesc_amd64_linux;
  23. /* fast tracepoints collect registers. */
  24. #define FT_CR_RIP 0
  25. #define FT_CR_EFLAGS 1
  26. #define FT_CR_R8 2
  27. #define FT_CR_R9 3
  28. #define FT_CR_R10 4
  29. #define FT_CR_R11 5
  30. #define FT_CR_R12 6
  31. #define FT_CR_R13 7
  32. #define FT_CR_R14 8
  33. #define FT_CR_R15 9
  34. #define FT_CR_RAX 10
  35. #define FT_CR_RBX 11
  36. #define FT_CR_RCX 12
  37. #define FT_CR_RDX 13
  38. #define FT_CR_RSI 14
  39. #define FT_CR_RDI 15
  40. #define FT_CR_RBP 16
  41. #define FT_CR_RSP 17
  42. static const int x86_64_ft_collect_regmap[] = {
  43. FT_CR_RAX * 8, FT_CR_RBX * 8, FT_CR_RCX * 8, FT_CR_RDX * 8,
  44. FT_CR_RSI * 8, FT_CR_RDI * 8, FT_CR_RBP * 8, FT_CR_RSP * 8,
  45. FT_CR_R8 * 8, FT_CR_R9 * 8, FT_CR_R10 * 8, FT_CR_R11 * 8,
  46. FT_CR_R12 * 8, FT_CR_R13 * 8, FT_CR_R14 * 8, FT_CR_R15 * 8,
  47. FT_CR_RIP * 8, FT_CR_EFLAGS * 8
  48. };
  49. #define X86_64_NUM_FT_COLLECT_GREGS \
  50. (sizeof (x86_64_ft_collect_regmap) / sizeof(x86_64_ft_collect_regmap[0]))
  51. void
  52. supply_fast_tracepoint_registers (struct regcache *regcache,
  53. const unsigned char *buf)
  54. {
  55. int i;
  56. for (i = 0; i < X86_64_NUM_FT_COLLECT_GREGS; i++)
  57. supply_register (regcache, i,
  58. ((char *) buf) + x86_64_ft_collect_regmap[i]);
  59. }
  60. ULONGEST
  61. get_raw_reg (const unsigned char *raw_regs, int regnum)
  62. {
  63. if (regnum >= X86_64_NUM_FT_COLLECT_GREGS)
  64. return 0;
  65. return *(ULONGEST *) (raw_regs + x86_64_ft_collect_regmap[regnum]);
  66. }
  67. #ifdef HAVE_UST
  68. #include <ust/processor.h>
  69. /* "struct registers" is the UST object type holding the registers at
  70. the time of the static tracepoint marker call. This doesn't
  71. contain RIP, but we know what it must have been (the marker
  72. address). */
  73. #define ST_REGENTRY(REG) \
  74. { \
  75. offsetof (struct registers, REG), \
  76. sizeof (((struct registers *) NULL)->REG) \
  77. }
  78. static struct
  79. {
  80. int offset;
  81. int size;
  82. } x86_64_st_collect_regmap[] =
  83. {
  84. ST_REGENTRY(rax),
  85. ST_REGENTRY(rbx),
  86. ST_REGENTRY(rcx),
  87. ST_REGENTRY(rdx),
  88. ST_REGENTRY(rsi),
  89. ST_REGENTRY(rdi),
  90. ST_REGENTRY(rbp),
  91. ST_REGENTRY(rsp),
  92. ST_REGENTRY(r8),
  93. ST_REGENTRY(r9),
  94. ST_REGENTRY(r10),
  95. ST_REGENTRY(r11),
  96. ST_REGENTRY(r12),
  97. ST_REGENTRY(r13),
  98. ST_REGENTRY(r14),
  99. ST_REGENTRY(r15),
  100. { -1, 0 },
  101. ST_REGENTRY(rflags),
  102. ST_REGENTRY(cs),
  103. ST_REGENTRY(ss),
  104. };
  105. #define X86_64_NUM_ST_COLLECT_GREGS \
  106. (sizeof (x86_64_st_collect_regmap) / sizeof (x86_64_st_collect_regmap[0]))
  107. /* GDB's RIP register number. */
  108. #define AMD64_RIP_REGNUM 16
  109. void
  110. supply_static_tracepoint_registers (struct regcache *regcache,
  111. const unsigned char *buf,
  112. CORE_ADDR pc)
  113. {
  114. int i;
  115. unsigned long newpc = pc;
  116. supply_register (regcache, AMD64_RIP_REGNUM, &newpc);
  117. for (i = 0; i < X86_64_NUM_ST_COLLECT_GREGS; i++)
  118. if (x86_64_st_collect_regmap[i].offset != -1)
  119. {
  120. switch (x86_64_st_collect_regmap[i].size)
  121. {
  122. case 8:
  123. supply_register (regcache, i,
  124. ((char *) buf)
  125. + x86_64_st_collect_regmap[i].offset);
  126. break;
  127. case 2:
  128. {
  129. unsigned long reg
  130. = * (short *) (((char *) buf)
  131. + x86_64_st_collect_regmap[i].offset);
  132. reg &= 0xffff;
  133. supply_register (regcache, i, &reg);
  134. }
  135. break;
  136. default:
  137. internal_error (__FILE__, __LINE__,
  138. "unhandled register size: %d",
  139. x86_64_st_collect_regmap[i].size);
  140. break;
  141. }
  142. }
  143. }
  144. #endif /* HAVE_UST */
  145. #if !defined __ILP32__
  146. /* Map the tdesc index to xcr0 mask. */
  147. static uint64_t idx2mask[X86_TDESC_LAST] = {
  148. X86_XSTATE_X87_MASK,
  149. X86_XSTATE_SSE_MASK,
  150. X86_XSTATE_AVX_MASK,
  151. X86_XSTATE_MPX_MASK,
  152. X86_XSTATE_AVX_MPX_MASK,
  153. X86_XSTATE_AVX_AVX512_MASK,
  154. X86_XSTATE_AVX_MPX_AVX512_PKU_MASK,
  155. };
  156. #endif
  157. /* Return target_desc to use for IPA, given the tdesc index passed by
  158. gdbserver. */
  159. const struct target_desc *
  160. get_ipa_tdesc (int idx)
  161. {
  162. if (idx >= X86_TDESC_LAST)
  163. {
  164. internal_error (__FILE__, __LINE__,
  165. "unknown ipa tdesc index: %d", idx);
  166. }
  167. #if defined __ILP32__
  168. switch (idx)
  169. {
  170. case X86_TDESC_SSE:
  171. return amd64_linux_read_description (X86_XSTATE_SSE_MASK, true);
  172. case X86_TDESC_AVX:
  173. return amd64_linux_read_description (X86_XSTATE_AVX_MASK, true);
  174. case X86_TDESC_AVX_AVX512:
  175. return amd64_linux_read_description (X86_XSTATE_AVX_AVX512_MASK, true);
  176. default:
  177. break;
  178. }
  179. #else
  180. return amd64_linux_read_description (idx2mask[idx], false);
  181. #endif
  182. internal_error (__FILE__, __LINE__,
  183. "unknown ipa tdesc index: %d", idx);
  184. }
  185. /* Allocate buffer for the jump pads. The branch instruction has a
  186. reach of +/- 31-bit, and the executable is loaded at low addresses.
  187. 64-bit: Use MAP_32BIT to allocate in the first 2GB. Shared
  188. libraries, being allocated at the top, are unfortunately out of
  189. luck.
  190. x32: Since MAP_32BIT is 64-bit only, do the placement manually.
  191. Try allocating at '0x80000000 - SIZE' initially, decreasing until
  192. we hit a free area. This ensures the executable is fully covered,
  193. and is as close as possible to the shared libraries, which are
  194. usually mapped at the top of the first 4GB of the address space.
  195. */
  196. void *
  197. alloc_jump_pad_buffer (size_t size)
  198. {
  199. #if __ILP32__
  200. uintptr_t addr;
  201. int pagesize;
  202. pagesize = sysconf (_SC_PAGE_SIZE);
  203. if (pagesize == -1)
  204. perror_with_name ("sysconf");
  205. addr = 0x80000000 - size;
  206. /* size should already be page-aligned, but this can't hurt. */
  207. addr &= ~(pagesize - 1);
  208. /* Search for a free area. If we hit 0, we're out of luck. */
  209. for (; addr; addr -= pagesize)
  210. {
  211. void *res;
  212. /* No MAP_FIXED - we don't want to zap someone's mapping. */
  213. res = mmap ((void *) addr, size,
  214. PROT_READ | PROT_WRITE | PROT_EXEC,
  215. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  216. /* If we got what we wanted, return. */
  217. if ((uintptr_t) res == addr)
  218. return res;
  219. /* If we got a mapping, but at a wrong address, undo it. */
  220. if (res != MAP_FAILED)
  221. munmap (res, size);
  222. }
  223. return NULL;
  224. #else
  225. void *res = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
  226. MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
  227. if (res == MAP_FAILED)
  228. return NULL;
  229. return res;
  230. #endif
  231. }
  232. void
  233. initialize_low_tracepoint (void)
  234. {
  235. #if defined __ILP32__
  236. amd64_linux_read_description (X86_XSTATE_SSE_MASK, true);
  237. amd64_linux_read_description (X86_XSTATE_AVX_MASK, true);
  238. amd64_linux_read_description (X86_XSTATE_AVX_AVX512_MASK, true);
  239. #else
  240. for (auto i = 0; i < X86_TDESC_LAST; i++)
  241. amd64_linux_read_description (idx2mask[i], false);
  242. #endif
  243. }