rseq-arm64.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * rseq-arm64.h
  4. *
  5. * (C) Copyright 2016-2022 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  6. * (C) Copyright 2018 - Will Deacon <will.deacon@arm.com>
  7. */
  8. /*
  9. * aarch64 -mbig-endian generates mixed endianness code vs data:
  10. * little-endian code and big-endian data. Ensure the RSEQ_SIG signature
  11. * matches code endianness.
  12. */
  13. #define RSEQ_SIG_CODE 0xd428bc00 /* BRK #0x45E0. */
  14. #ifdef __AARCH64EB__
  15. #define RSEQ_SIG_DATA 0x00bc28d4 /* BRK #0x45E0. */
  16. #else
  17. #define RSEQ_SIG_DATA RSEQ_SIG_CODE
  18. #endif
  19. #define RSEQ_SIG RSEQ_SIG_DATA
  20. #define rseq_smp_mb() __asm__ __volatile__ ("dmb ish" ::: "memory")
  21. #define rseq_smp_rmb() __asm__ __volatile__ ("dmb ishld" ::: "memory")
  22. #define rseq_smp_wmb() __asm__ __volatile__ ("dmb ishst" ::: "memory")
  23. #define rseq_smp_load_acquire(p) \
  24. __extension__ ({ \
  25. union { rseq_unqual_scalar_typeof(*(p)) __val; char __c[sizeof(*(p))]; } __u; \
  26. switch (sizeof(*(p))) { \
  27. case 1: \
  28. __asm__ __volatile__ ("ldarb %w0, %1" \
  29. : "=r" (*(__u8 *)__u.__c) \
  30. : "Q" (*(p)) : "memory"); \
  31. break; \
  32. case 2: \
  33. __asm__ __volatile__ ("ldarh %w0, %1" \
  34. : "=r" (*(__u16 *)__u.__c) \
  35. : "Q" (*(p)) : "memory"); \
  36. break; \
  37. case 4: \
  38. __asm__ __volatile__ ("ldar %w0, %1" \
  39. : "=r" (*(__u32 *)__u.__c) \
  40. : "Q" (*(p)) : "memory"); \
  41. break; \
  42. case 8: \
  43. __asm__ __volatile__ ("ldar %0, %1" \
  44. : "=r" (*(__u64 *)__u.__c) \
  45. : "Q" (*(p)) : "memory"); \
  46. break; \
  47. } \
  48. (rseq_unqual_scalar_typeof(*(p)))__u.__val; \
  49. })
  50. #define rseq_smp_acquire__after_ctrl_dep() rseq_smp_rmb()
  51. #define rseq_smp_store_release(p, v) \
  52. do { \
  53. union { rseq_unqual_scalar_typeof(*(p)) __val; char __c[sizeof(*(p))]; } __u = \
  54. { .__val = (rseq_unqual_scalar_typeof(*(p))) (v) }; \
  55. switch (sizeof(*(p))) { \
  56. case 1: \
  57. __asm__ __volatile__ ("stlrb %w1, %0" \
  58. : "=Q" (*(p)) \
  59. : "r" (*(__u8 *)__u.__c) \
  60. : "memory"); \
  61. break; \
  62. case 2: \
  63. __asm__ __volatile__ ("stlrh %w1, %0" \
  64. : "=Q" (*(p)) \
  65. : "r" (*(__u16 *)__u.__c) \
  66. : "memory"); \
  67. break; \
  68. case 4: \
  69. __asm__ __volatile__ ("stlr %w1, %0" \
  70. : "=Q" (*(p)) \
  71. : "r" (*(__u32 *)__u.__c) \
  72. : "memory"); \
  73. break; \
  74. case 8: \
  75. __asm__ __volatile__ ("stlr %1, %0" \
  76. : "=Q" (*(p)) \
  77. : "r" (*(__u64 *)__u.__c) \
  78. : "memory"); \
  79. break; \
  80. } \
  81. } while (0)
  82. #define RSEQ_ASM_TMP_REG32 "w15"
  83. #define RSEQ_ASM_TMP_REG "x15"
  84. #define RSEQ_ASM_TMP_REG_2 "x14"
  85. #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, start_ip, \
  86. post_commit_offset, abort_ip) \
  87. " .pushsection __rseq_cs, \"aw\"\n" \
  88. " .balign 32\n" \
  89. __rseq_str(label) ":\n" \
  90. " .long " __rseq_str(version) ", " __rseq_str(flags) "\n" \
  91. " .quad " __rseq_str(start_ip) ", " \
  92. __rseq_str(post_commit_offset) ", " \
  93. __rseq_str(abort_ip) "\n" \
  94. " .popsection\n\t" \
  95. " .pushsection __rseq_cs_ptr_array, \"aw\"\n" \
  96. " .quad " __rseq_str(label) "b\n" \
  97. " .popsection\n"
  98. #define RSEQ_ASM_DEFINE_TABLE(label, start_ip, post_commit_ip, abort_ip) \
  99. __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \
  100. (post_commit_ip - start_ip), abort_ip)
  101. /*
  102. * Exit points of a rseq critical section consist of all instructions outside
  103. * of the critical section where a critical section can either branch to or
  104. * reach through the normal course of its execution. The abort IP and the
  105. * post-commit IP are already part of the __rseq_cs section and should not be
  106. * explicitly defined as additional exit points. Knowing all exit points is
  107. * useful to assist debuggers stepping over the critical section.
  108. */
  109. #define RSEQ_ASM_DEFINE_EXIT_POINT(start_ip, exit_ip) \
  110. " .pushsection __rseq_exit_point_array, \"aw\"\n" \
  111. " .quad " __rseq_str(start_ip) ", " __rseq_str(exit_ip) "\n" \
  112. " .popsection\n"
  113. #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
  114. RSEQ_INJECT_ASM(1) \
  115. " adrp " RSEQ_ASM_TMP_REG ", " __rseq_str(cs_label) "\n" \
  116. " add " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
  117. ", :lo12:" __rseq_str(cs_label) "\n" \
  118. " str " RSEQ_ASM_TMP_REG ", %[" __rseq_str(rseq_cs) "]\n" \
  119. __rseq_str(label) ":\n"
  120. #define RSEQ_ASM_DEFINE_ABORT(label, abort_label) \
  121. " b 222f\n" \
  122. " .inst " __rseq_str(RSEQ_SIG_CODE) "\n" \
  123. __rseq_str(label) ":\n" \
  124. " b %l[" __rseq_str(abort_label) "]\n" \
  125. "222:\n"
  126. #define RSEQ_ASM_OP_STORE(value, var) \
  127. " str %[" __rseq_str(value) "], %[" __rseq_str(var) "]\n"
  128. #define RSEQ_ASM_OP_STORE_RELEASE(value, var) \
  129. " stlr %[" __rseq_str(value) "], %[" __rseq_str(var) "]\n"
  130. #define RSEQ_ASM_OP_FINAL_STORE(value, var, post_commit_label) \
  131. RSEQ_ASM_OP_STORE(value, var) \
  132. __rseq_str(post_commit_label) ":\n"
  133. #define RSEQ_ASM_OP_FINAL_STORE_RELEASE(value, var, post_commit_label) \
  134. RSEQ_ASM_OP_STORE_RELEASE(value, var) \
  135. __rseq_str(post_commit_label) ":\n"
  136. #define RSEQ_ASM_OP_CMPEQ(var, expect, label) \
  137. " ldr " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n" \
  138. " sub " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
  139. ", %[" __rseq_str(expect) "]\n" \
  140. " cbnz " RSEQ_ASM_TMP_REG ", " __rseq_str(label) "\n"
  141. #define RSEQ_ASM_OP_CMPEQ32(var, expect, label) \
  142. " ldr " RSEQ_ASM_TMP_REG32 ", %[" __rseq_str(var) "]\n" \
  143. " sub " RSEQ_ASM_TMP_REG32 ", " RSEQ_ASM_TMP_REG32 \
  144. ", %w[" __rseq_str(expect) "]\n" \
  145. " cbnz " RSEQ_ASM_TMP_REG32 ", " __rseq_str(label) "\n"
  146. #define RSEQ_ASM_OP_CMPNE(var, expect, label) \
  147. " ldr " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n" \
  148. " sub " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
  149. ", %[" __rseq_str(expect) "]\n" \
  150. " cbz " RSEQ_ASM_TMP_REG ", " __rseq_str(label) "\n"
  151. #define RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, label) \
  152. RSEQ_INJECT_ASM(2) \
  153. RSEQ_ASM_OP_CMPEQ32(current_cpu_id, cpu_id, label)
  154. #define RSEQ_ASM_OP_R_LOAD(var) \
  155. " ldr " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n"
  156. #define RSEQ_ASM_OP_R_STORE(var) \
  157. " str " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n"
  158. #define RSEQ_ASM_OP_R_LOAD_OFF(offset) \
  159. " ldr " RSEQ_ASM_TMP_REG ", [" RSEQ_ASM_TMP_REG \
  160. ", %[" __rseq_str(offset) "]]\n"
  161. #define RSEQ_ASM_OP_R_ADD(count) \
  162. " add " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
  163. ", %[" __rseq_str(count) "]\n"
  164. #define RSEQ_ASM_OP_R_FINAL_STORE(var, post_commit_label) \
  165. " str " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n" \
  166. __rseq_str(post_commit_label) ":\n"
  167. #define RSEQ_ASM_OP_R_BAD_MEMCPY(dst, src, len) \
  168. " cbz %[" __rseq_str(len) "], 333f\n" \
  169. " mov " RSEQ_ASM_TMP_REG_2 ", %[" __rseq_str(len) "]\n" \
  170. "222: sub " RSEQ_ASM_TMP_REG_2 ", " RSEQ_ASM_TMP_REG_2 ", #1\n" \
  171. " ldrb " RSEQ_ASM_TMP_REG32 ", [%[" __rseq_str(src) "]" \
  172. ", " RSEQ_ASM_TMP_REG_2 "]\n" \
  173. " strb " RSEQ_ASM_TMP_REG32 ", [%[" __rseq_str(dst) "]" \
  174. ", " RSEQ_ASM_TMP_REG_2 "]\n" \
  175. " cbnz " RSEQ_ASM_TMP_REG_2 ", 222b\n" \
  176. "333:\n"
  177. /* Per-cpu-id indexing. */
  178. #define RSEQ_TEMPLATE_CPU_ID
  179. #define RSEQ_TEMPLATE_MO_RELAXED
  180. #include "rseq-arm64-bits.h"
  181. #undef RSEQ_TEMPLATE_MO_RELAXED
  182. #define RSEQ_TEMPLATE_MO_RELEASE
  183. #include "rseq-arm64-bits.h"
  184. #undef RSEQ_TEMPLATE_MO_RELEASE
  185. #undef RSEQ_TEMPLATE_CPU_ID
  186. /* Per-mm-cid indexing. */
  187. #define RSEQ_TEMPLATE_MM_CID
  188. #define RSEQ_TEMPLATE_MO_RELAXED
  189. #include "rseq-arm64-bits.h"
  190. #undef RSEQ_TEMPLATE_MO_RELAXED
  191. #define RSEQ_TEMPLATE_MO_RELEASE
  192. #include "rseq-arm64-bits.h"
  193. #undef RSEQ_TEMPLATE_MO_RELEASE
  194. #undef RSEQ_TEMPLATE_MM_CID
  195. /* APIs which are not based on cpu ids. */
  196. #define RSEQ_TEMPLATE_CPU_ID_NONE
  197. #define RSEQ_TEMPLATE_MO_RELAXED
  198. #include "rseq-arm64-bits.h"
  199. #undef RSEQ_TEMPLATE_MO_RELAXED
  200. #undef RSEQ_TEMPLATE_CPU_ID_NONE