sysdep.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Assembler macros for x86.
  2. Copyright (C) 2017-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _X86_SYSDEP_H
  16. #define _X86_SYSDEP_H 1
  17. #include <sysdeps/generic/sysdep.h>
  18. /* The extended state feature IDs in the state component bitmap. */
  19. #define X86_XSTATE_X87_ID 0
  20. #define X86_XSTATE_SSE_ID 1
  21. #define X86_XSTATE_AVX_ID 2
  22. #define X86_XSTATE_BNDREGS_ID 3
  23. #define X86_XSTATE_BNDCFG_ID 4
  24. #define X86_XSTATE_K_ID 5
  25. #define X86_XSTATE_ZMM_H_ID 6
  26. #define X86_XSTATE_ZMM_ID 7
  27. #define X86_XSTATE_PKRU_ID 9
  28. #define X86_XSTATE_TILECFG_ID 17
  29. #define X86_XSTATE_TILEDATA_ID 18
  30. #define X86_XSTATE_APX_F_ID 19
  31. #ifdef __x86_64__
  32. /* Offset for fxsave/xsave area used by _dl_runtime_resolve. Also need
  33. space to preserve RCX, RDX, RSI, RDI, R8, R9 and RAX. It must be
  34. aligned to 16 bytes for fxsave and 64 bytes for xsave. It is non-zero
  35. because MOV, instead of PUSH, is used to save registers onto stack.
  36. +==================+<- stack frame start aligned at 8 or 16 bytes
  37. | |<- paddings for stack realignment of 64 bytes
  38. |------------------|<- xsave buffer end aligned at 64 bytes
  39. | |<-
  40. | |<-
  41. | |<-
  42. |------------------|<- xsave buffer start at STATE_SAVE_OFFSET(%rsp)
  43. | |<- 8-byte padding for 64-byte alignment
  44. | |<- R9
  45. | |<- R8
  46. | |<- RDI
  47. | |<- RSI
  48. | |<- RDX
  49. | |<- RCX
  50. | |<- RAX
  51. +==================+<- RSP aligned at 64 bytes
  52. */
  53. # define STATE_SAVE_OFFSET (8 * 7 + 8)
  54. /* _dl_tlsdesc_dynamic preserves RDI, RSI and RBX before realigning
  55. stack. After realigning stack, it saves RCX, RDX, R8, R9, R10 and
  56. R11. Allocate space for RDI, RSI and RBX to avoid clobbering saved
  57. RDI, RSI and RBX values on stack by xsave.
  58. +==================+<- stack frame start aligned at 8 or 16 bytes
  59. | |<- RDI saved in the red zone
  60. | |<- RSI saved in the red zone
  61. | |<- RBX saved in the red zone
  62. | |<- paddings for stack realignment of 64 bytes
  63. |------------------|<- xsave buffer end aligned at 64 bytes
  64. | |<-
  65. | |<-
  66. | |<-
  67. |------------------|<- xsave buffer start at STATE_SAVE_OFFSET(%rsp)
  68. | |<- 8-byte padding for 64-byte alignment
  69. | |<- 8-byte padding for 64-byte alignment
  70. | |<- R11
  71. | |<- R10
  72. | |<- R9
  73. | |<- R8
  74. | |<- RDX
  75. | |<- RCX
  76. +==================+<- RSP aligned at 64 bytes
  77. Define the total register save area size for all integer registers by
  78. adding 24 to STATE_SAVE_OFFSET since RDI, RSI and RBX are saved onto
  79. stack without adjusting stack pointer first, using the red-zone. */
  80. # define TLSDESC_CALL_REGISTER_SAVE_AREA (STATE_SAVE_OFFSET + 24)
  81. /* Save SSE, AVX, AVX512, mask, bound and APX registers. Bound and APX
  82. registers are mutually exclusive. */
  83. # define STATE_SAVE_MASK \
  84. ((1 << X86_XSTATE_SSE_ID) \
  85. | (1 << X86_XSTATE_AVX_ID) \
  86. | (1 << X86_XSTATE_BNDREGS_ID) \
  87. | (1 << X86_XSTATE_K_ID) \
  88. | (1 << X86_XSTATE_ZMM_H_ID) \
  89. | (1 << X86_XSTATE_ZMM_ID) \
  90. | (1 << X86_XSTATE_APX_F_ID))
  91. /* The maximum supported xstate ID. */
  92. # define X86_XSTATE_MAX_ID X86_XSTATE_APX_F_ID
  93. /* AMX state mask. */
  94. # define AMX_STATE_SAVE_MASK \
  95. ((1 << X86_XSTATE_TILECFG_ID) | (1 << X86_XSTATE_TILEDATA_ID))
  96. /* States to be included in xsave_state_full_size. */
  97. # define FULL_STATE_SAVE_MASK \
  98. (STATE_SAVE_MASK | AMX_STATE_SAVE_MASK)
  99. #else
  100. /* Offset for fxsave/xsave area used by _dl_tlsdesc_dynamic. Since i386
  101. uses PUSH to save registers onto stack, use 0 here. */
  102. # define STATE_SAVE_OFFSET 0
  103. # define TLSDESC_CALL_REGISTER_SAVE_AREA 0
  104. /* Save SSE, AVX, AXV512, mask and bound registers. */
  105. # define STATE_SAVE_MASK \
  106. ((1 << X86_XSTATE_SSE_ID) \
  107. | (1 << X86_XSTATE_AVX_ID) \
  108. | (1 << X86_XSTATE_BNDREGS_ID) \
  109. | (1 << X86_XSTATE_K_ID) \
  110. | (1 << X86_XSTATE_ZMM_H_ID))
  111. /* The maximum supported xstate ID. */
  112. # define X86_XSTATE_MAX_ID X86_XSTATE_ZMM_H_ID
  113. /* States to be included in xsave_state_size. */
  114. # define FULL_STATE_SAVE_MASK STATE_SAVE_MASK
  115. #endif
  116. /* States which should be saved for TLSDESC_CALL and TLS_DESC_CALL.
  117. Compiler assumes that all registers, including AMX and x87 FPU
  118. stack registers, are unchanged after CALL, except for EFLAGS and
  119. RAX/EAX. */
  120. #define TLSDESC_CALL_STATE_SAVE_MASK \
  121. (FULL_STATE_SAVE_MASK | (1 << X86_XSTATE_X87_ID))
  122. /* Constants for bits in __x86_string_control: */
  123. /* Avoid short distance REP MOVSB. */
  124. #define X86_STRING_CONTROL_AVOID_SHORT_DISTANCE_REP_MOVSB (1 << 0)
  125. #ifdef __ASSEMBLER__
  126. /* Syntactic details of assembler. */
  127. /* ELF uses byte-counts for .align, most others use log2 of count of bytes. */
  128. #define ALIGNARG(log2) 1<<log2
  129. #define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
  130. /* Common entry 16 byte aligns. */
  131. #define ENTRY(name) ENTRY_P2ALIGN (name, 4)
  132. #undef END
  133. #define END(name) \
  134. cfi_endproc; \
  135. ASM_SIZE_DIRECTIVE(name)
  136. #define ENTRY_CHK(name) ENTRY (name)
  137. #define END_CHK(name) END (name)
  138. /* Since C identifiers are not normally prefixed with an underscore
  139. on this system, the asm identifier `syscall_error' intrudes on the
  140. C name space. Make sure we use an innocuous name. */
  141. #define syscall_error __syscall_error
  142. #define mcount _mcount
  143. #undef PSEUDO_END
  144. #define PSEUDO_END(name) \
  145. END (name)
  146. /* Local label name for asm code. */
  147. #ifndef L
  148. /* ELF-like local names start with `.L'. */
  149. # define LOCAL_LABEL(name) .L##name
  150. # define L(name) LOCAL_LABEL(name)
  151. #endif
  152. #define atom_text_section .section ".text.atom", "ax"
  153. #ifndef DL_STACK_ALIGNMENT
  154. /* Due to GCC bug:
  155. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
  156. __tls_get_addr may be called with 8-byte/4-byte stack alignment.
  157. Although this bug has been fixed in GCC 4.9.4, 5.3 and 6, we can't
  158. assume that stack will be always aligned at 16 bytes. */
  159. # ifdef __x86_64__
  160. # define DL_STACK_ALIGNMENT 8
  161. # define MINIMUM_ALIGNMENT 16
  162. # else
  163. # define DL_STACK_ALIGNMENT 4
  164. # endif
  165. #endif
  166. /* True if _dl_runtime_resolve/_dl_tlsdesc_dynamic should align stack for
  167. STATE_SAVE or align stack to MINIMUM_ALIGNMENT bytes before calling
  168. _dl_fixup/__tls_get_addr. */
  169. #define DL_RUNTIME_RESOLVE_REALIGN_STACK \
  170. (STATE_SAVE_ALIGNMENT > DL_STACK_ALIGNMENT \
  171. || MINIMUM_ALIGNMENT > DL_STACK_ALIGNMENT)
  172. #endif /* __ASSEMBLER__ */
  173. #endif /* _X86_SYSDEP_H */