linux-x86-tdesc.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* GNU/Linux/x86-64 specific target description, for the remote server
  2. for GDB.
  3. Copyright (C) 2017-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 "tdesc.h"
  17. #include "linux-x86-tdesc.h"
  18. #include "arch/i386.h"
  19. #include "gdbsupport/x86-xstate.h"
  20. #ifdef __x86_64__
  21. #include "arch/amd64.h"
  22. #endif
  23. #include "x86-tdesc.h"
  24. /* Return the right x86_linux_tdesc index for a given XCR0. Return
  25. X86_TDESC_LAST if can't find a match. */
  26. static enum x86_linux_tdesc
  27. xcr0_to_tdesc_idx (uint64_t xcr0, bool is_x32)
  28. {
  29. if (xcr0 & X86_XSTATE_PKRU)
  30. {
  31. if (is_x32)
  32. {
  33. /* No x32 MPX and PKU, fall back to avx_avx512. */
  34. return X86_TDESC_AVX_AVX512;
  35. }
  36. else
  37. return X86_TDESC_AVX_MPX_AVX512_PKU;
  38. }
  39. else if (xcr0 & X86_XSTATE_AVX512)
  40. return X86_TDESC_AVX_AVX512;
  41. else if ((xcr0 & X86_XSTATE_AVX_MPX_MASK) == X86_XSTATE_AVX_MPX_MASK)
  42. {
  43. if (is_x32) /* No MPX on x32. */
  44. return X86_TDESC_AVX;
  45. else
  46. return X86_TDESC_AVX_MPX;
  47. }
  48. else if (xcr0 & X86_XSTATE_MPX)
  49. {
  50. if (is_x32) /* No MPX on x32. */
  51. return X86_TDESC_AVX;
  52. else
  53. return X86_TDESC_MPX;
  54. }
  55. else if (xcr0 & X86_XSTATE_AVX)
  56. return X86_TDESC_AVX;
  57. else if (xcr0 & X86_XSTATE_SSE)
  58. return X86_TDESC_SSE;
  59. else if (xcr0 & X86_XSTATE_X87)
  60. return X86_TDESC_MMX;
  61. else
  62. return X86_TDESC_LAST;
  63. }
  64. #if defined __i386__ || !defined IN_PROCESS_AGENT
  65. static struct target_desc *i386_tdescs[X86_TDESC_LAST] = { };
  66. /* Return the target description according to XCR0. */
  67. const struct target_desc *
  68. i386_linux_read_description (uint64_t xcr0)
  69. {
  70. enum x86_linux_tdesc idx = xcr0_to_tdesc_idx (xcr0, false);
  71. if (idx == X86_TDESC_LAST)
  72. return NULL;
  73. struct target_desc **tdesc = &i386_tdescs[idx];
  74. if (*tdesc == NULL)
  75. {
  76. *tdesc = i386_create_target_description (xcr0, true, false);
  77. init_target_desc (*tdesc, i386_expedite_regs);
  78. }
  79. return *tdesc;;
  80. }
  81. #endif
  82. #ifdef __x86_64__
  83. static target_desc *amd64_tdescs[X86_TDESC_LAST] = { };
  84. static target_desc *x32_tdescs[X86_TDESC_LAST] = { };
  85. const struct target_desc *
  86. amd64_linux_read_description (uint64_t xcr0, bool is_x32)
  87. {
  88. enum x86_linux_tdesc idx = xcr0_to_tdesc_idx (xcr0, is_x32);
  89. if (idx == X86_TDESC_LAST)
  90. return NULL;
  91. struct target_desc **tdesc = NULL;
  92. if (is_x32)
  93. tdesc = &x32_tdescs[idx];
  94. else
  95. tdesc = &amd64_tdescs[idx];
  96. if (*tdesc == NULL)
  97. {
  98. *tdesc = amd64_create_target_description (xcr0, is_x32, true, true);
  99. init_target_desc (*tdesc, amd64_expedite_regs);
  100. }
  101. return *tdesc;
  102. }
  103. #endif
  104. #ifndef IN_PROCESS_AGENT
  105. int
  106. i386_get_ipa_tdesc_idx (const struct target_desc *tdesc)
  107. {
  108. for (int i = 0; i < X86_TDESC_LAST; i++)
  109. {
  110. if (tdesc == i386_tdescs[i])
  111. return i;
  112. }
  113. /* If none tdesc is found, return the one with minimum features. */
  114. return X86_TDESC_MMX;
  115. }
  116. #if defined __x86_64__
  117. int
  118. amd64_get_ipa_tdesc_idx (const struct target_desc *tdesc)
  119. {
  120. for (int i = 0; i < X86_TDESC_LAST; i++)
  121. {
  122. if (tdesc == amd64_tdescs[i])
  123. return i;
  124. }
  125. for (int i = 0; i < X86_TDESC_LAST; i++)
  126. {
  127. if (tdesc == x32_tdescs[i])
  128. return i;
  129. }
  130. return X86_TDESC_SSE;
  131. }
  132. #endif
  133. #endif