rseq-x86.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * rseq-x86.h
  4. *
  5. * (C) Copyright 2016-2022 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  6. */
  7. #ifndef RSEQ_H
  8. #error "Never use <rseq-x86.h> directly; include <rseq.h> instead."
  9. #endif
  10. #include <stdint.h>
  11. /*
  12. * RSEQ_SIG is used with the following reserved undefined instructions, which
  13. * trap in user-space:
  14. *
  15. * x86-32: 0f b9 3d 53 30 05 53 ud1 0x53053053,%edi
  16. * x86-64: 0f b9 3d 53 30 05 53 ud1 0x53053053(%rip),%edi
  17. */
  18. #define RSEQ_SIG 0x53053053
  19. /*
  20. * Due to a compiler optimization bug in gcc-8 with asm goto and TLS asm input
  21. * operands, we cannot use "m" input operands, and rather pass the __rseq_abi
  22. * address through a "r" input operand.
  23. */
  24. /* Offset of cpu_id, rseq_cs, and mm_cid fields in struct rseq. */
  25. #define RSEQ_CPU_ID_OFFSET 4
  26. #define RSEQ_CS_OFFSET 8
  27. #define RSEQ_MM_CID_OFFSET 24
  28. #ifdef __x86_64__
  29. #define RSEQ_ASM_TP_SEGMENT %%fs
  30. #define rseq_smp_mb() \
  31. __asm__ __volatile__ ("lock; addl $0,-128(%%rsp)" ::: "memory", "cc")
  32. #define rseq_smp_rmb() rseq_barrier()
  33. #define rseq_smp_wmb() rseq_barrier()
  34. #define rseq_smp_load_acquire(p) \
  35. __extension__ ({ \
  36. rseq_unqual_scalar_typeof(*(p)) ____p1 = RSEQ_READ_ONCE(*(p)); \
  37. rseq_barrier(); \
  38. ____p1; \
  39. })
  40. #define rseq_smp_acquire__after_ctrl_dep() rseq_smp_rmb()
  41. #define rseq_smp_store_release(p, v) \
  42. do { \
  43. rseq_barrier(); \
  44. RSEQ_WRITE_ONCE(*(p), v); \
  45. } while (0)
  46. #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, \
  47. start_ip, post_commit_offset, abort_ip) \
  48. ".pushsection __rseq_cs, \"aw\"\n\t" \
  49. ".balign 32\n\t" \
  50. __rseq_str(label) ":\n\t" \
  51. ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \
  52. ".quad " __rseq_str(start_ip) ", " __rseq_str(post_commit_offset) ", " __rseq_str(abort_ip) "\n\t" \
  53. ".popsection\n\t" \
  54. ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \
  55. ".quad " __rseq_str(label) "b\n\t" \
  56. ".popsection\n\t"
  57. #define RSEQ_ASM_DEFINE_TABLE(label, start_ip, post_commit_ip, abort_ip) \
  58. __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \
  59. (post_commit_ip - start_ip), abort_ip)
  60. /*
  61. * Exit points of a rseq critical section consist of all instructions outside
  62. * of the critical section where a critical section can either branch to or
  63. * reach through the normal course of its execution. The abort IP and the
  64. * post-commit IP are already part of the __rseq_cs section and should not be
  65. * explicitly defined as additional exit points. Knowing all exit points is
  66. * useful to assist debuggers stepping over the critical section.
  67. */
  68. #define RSEQ_ASM_DEFINE_EXIT_POINT(start_ip, exit_ip) \
  69. ".pushsection __rseq_exit_point_array, \"aw\"\n\t" \
  70. ".quad " __rseq_str(start_ip) ", " __rseq_str(exit_ip) "\n\t" \
  71. ".popsection\n\t"
  72. #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
  73. RSEQ_INJECT_ASM(1) \
  74. "leaq " __rseq_str(cs_label) "(%%rip), %%rax\n\t" \
  75. "movq %%rax, " __rseq_str(rseq_cs) "\n\t" \
  76. __rseq_str(label) ":\n\t"
  77. #define RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, label) \
  78. RSEQ_INJECT_ASM(2) \
  79. "cmpl %[" __rseq_str(cpu_id) "], " __rseq_str(current_cpu_id) "\n\t" \
  80. "jnz " __rseq_str(label) "\n\t"
  81. #define RSEQ_ASM_DEFINE_ABORT(label, teardown, abort_label) \
  82. ".pushsection __rseq_failure, \"ax\"\n\t" \
  83. /* Disassembler-friendly signature: ud1 <sig>(%rip),%edi. */ \
  84. ".byte 0x0f, 0xb9, 0x3d\n\t" \
  85. ".long " __rseq_str(RSEQ_SIG) "\n\t" \
  86. __rseq_str(label) ":\n\t" \
  87. teardown \
  88. "jmp %l[" __rseq_str(abort_label) "]\n\t" \
  89. ".popsection\n\t"
  90. #define RSEQ_ASM_DEFINE_CMPFAIL(label, teardown, cmpfail_label) \
  91. ".pushsection __rseq_failure, \"ax\"\n\t" \
  92. __rseq_str(label) ":\n\t" \
  93. teardown \
  94. "jmp %l[" __rseq_str(cmpfail_label) "]\n\t" \
  95. ".popsection\n\t"
  96. #elif defined(__i386__)
  97. #define RSEQ_ASM_TP_SEGMENT %%gs
  98. #define rseq_smp_mb() \
  99. __asm__ __volatile__ ("lock; addl $0,-128(%%esp)" ::: "memory", "cc")
  100. #define rseq_smp_rmb() \
  101. __asm__ __volatile__ ("lock; addl $0,-128(%%esp)" ::: "memory", "cc")
  102. #define rseq_smp_wmb() \
  103. __asm__ __volatile__ ("lock; addl $0,-128(%%esp)" ::: "memory", "cc")
  104. #define rseq_smp_load_acquire(p) \
  105. __extension__ ({ \
  106. __typeof(*p) ____p1 = RSEQ_READ_ONCE(*p); \
  107. rseq_smp_mb(); \
  108. ____p1; \
  109. })
  110. #define rseq_smp_acquire__after_ctrl_dep() rseq_smp_rmb()
  111. #define rseq_smp_store_release(p, v) \
  112. do { \
  113. rseq_smp_mb(); \
  114. RSEQ_WRITE_ONCE(*p, v); \
  115. } while (0)
  116. /*
  117. * Use eax as scratch register and take memory operands as input to
  118. * lessen register pressure. Especially needed when compiling in O0.
  119. */
  120. #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, \
  121. start_ip, post_commit_offset, abort_ip) \
  122. ".pushsection __rseq_cs, \"aw\"\n\t" \
  123. ".balign 32\n\t" \
  124. __rseq_str(label) ":\n\t" \
  125. ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \
  126. ".long " __rseq_str(start_ip) ", 0x0, " __rseq_str(post_commit_offset) ", 0x0, " __rseq_str(abort_ip) ", 0x0\n\t" \
  127. ".popsection\n\t" \
  128. ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \
  129. ".long " __rseq_str(label) "b, 0x0\n\t" \
  130. ".popsection\n\t"
  131. #define RSEQ_ASM_DEFINE_TABLE(label, start_ip, post_commit_ip, abort_ip) \
  132. __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \
  133. (post_commit_ip - start_ip), abort_ip)
  134. /*
  135. * Exit points of a rseq critical section consist of all instructions outside
  136. * of the critical section where a critical section can either branch to or
  137. * reach through the normal course of its execution. The abort IP and the
  138. * post-commit IP are already part of the __rseq_cs section and should not be
  139. * explicitly defined as additional exit points. Knowing all exit points is
  140. * useful to assist debuggers stepping over the critical section.
  141. */
  142. #define RSEQ_ASM_DEFINE_EXIT_POINT(start_ip, exit_ip) \
  143. ".pushsection __rseq_exit_point_array, \"aw\"\n\t" \
  144. ".long " __rseq_str(start_ip) ", 0x0, " __rseq_str(exit_ip) ", 0x0\n\t" \
  145. ".popsection\n\t"
  146. #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
  147. RSEQ_INJECT_ASM(1) \
  148. "movl $" __rseq_str(cs_label) ", " __rseq_str(rseq_cs) "\n\t" \
  149. __rseq_str(label) ":\n\t"
  150. #define RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, label) \
  151. RSEQ_INJECT_ASM(2) \
  152. "cmpl %[" __rseq_str(cpu_id) "], " __rseq_str(current_cpu_id) "\n\t" \
  153. "jnz " __rseq_str(label) "\n\t"
  154. #define RSEQ_ASM_DEFINE_ABORT(label, teardown, abort_label) \
  155. ".pushsection __rseq_failure, \"ax\"\n\t" \
  156. /* Disassembler-friendly signature: ud1 <sig>,%edi. */ \
  157. ".byte 0x0f, 0xb9, 0x3d\n\t" \
  158. ".long " __rseq_str(RSEQ_SIG) "\n\t" \
  159. __rseq_str(label) ":\n\t" \
  160. teardown \
  161. "jmp %l[" __rseq_str(abort_label) "]\n\t" \
  162. ".popsection\n\t"
  163. #define RSEQ_ASM_DEFINE_CMPFAIL(label, teardown, cmpfail_label) \
  164. ".pushsection __rseq_failure, \"ax\"\n\t" \
  165. __rseq_str(label) ":\n\t" \
  166. teardown \
  167. "jmp %l[" __rseq_str(cmpfail_label) "]\n\t" \
  168. ".popsection\n\t"
  169. #endif
  170. /* Per-cpu-id indexing. */
  171. #define RSEQ_TEMPLATE_CPU_ID
  172. #define RSEQ_TEMPLATE_MO_RELAXED
  173. #include "rseq-x86-bits.h"
  174. #undef RSEQ_TEMPLATE_MO_RELAXED
  175. #define RSEQ_TEMPLATE_MO_RELEASE
  176. #include "rseq-x86-bits.h"
  177. #undef RSEQ_TEMPLATE_MO_RELEASE
  178. #undef RSEQ_TEMPLATE_CPU_ID
  179. /* Per-mm-cid indexing. */
  180. #define RSEQ_TEMPLATE_MM_CID
  181. #define RSEQ_TEMPLATE_MO_RELAXED
  182. #include "rseq-x86-bits.h"
  183. #undef RSEQ_TEMPLATE_MO_RELAXED
  184. #define RSEQ_TEMPLATE_MO_RELEASE
  185. #include "rseq-x86-bits.h"
  186. #undef RSEQ_TEMPLATE_MO_RELEASE
  187. #undef RSEQ_TEMPLATE_MM_CID
  188. /* APIs which are not based on cpu ids. */
  189. #define RSEQ_TEMPLATE_CPU_ID_NONE
  190. #define RSEQ_TEMPLATE_MO_RELAXED
  191. #include "rseq-x86-bits.h"
  192. #undef RSEQ_TEMPLATE_MO_RELAXED
  193. #undef RSEQ_TEMPLATE_CPU_ID_NONE