rseq-riscv.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * Select the instruction "csrw mhartid, x0" as the RSEQ_SIG. Unlike
  4. * other architectures, the ebreak instruction has no immediate field for
  5. * distinguishing purposes. Hence, ebreak is not suitable as RSEQ_SIG.
  6. * "csrw mhartid, x0" can also satisfy the RSEQ requirement because it
  7. * is an uncommon instruction and will raise an illegal instruction
  8. * exception when executed in all modes.
  9. */
  10. #include <endian.h>
  11. #if defined(__BYTE_ORDER) ? (__BYTE_ORDER == __LITTLE_ENDIAN) : defined(__LITTLE_ENDIAN)
  12. #define RSEQ_SIG 0xf1401073 /* csrr mhartid, x0 */
  13. #else
  14. #error "Currently, RSEQ only supports Little-Endian version"
  15. #endif
  16. #if __riscv_xlen == 64
  17. #define __REG_SEL(a, b) a
  18. #elif __riscv_xlen == 32
  19. #define __REG_SEL(a, b) b
  20. #endif
  21. #define REG_L __REG_SEL("ld ", "lw ")
  22. #define REG_S __REG_SEL("sd ", "sw ")
  23. #define RISCV_FENCE(p, s) \
  24. __asm__ __volatile__ ("fence " #p "," #s : : : "memory")
  25. #define rseq_smp_mb() RISCV_FENCE(rw, rw)
  26. #define rseq_smp_rmb() RISCV_FENCE(r, r)
  27. #define rseq_smp_wmb() RISCV_FENCE(w, w)
  28. #define RSEQ_ASM_TMP_REG_1 "t6"
  29. #define RSEQ_ASM_TMP_REG_2 "t5"
  30. #define RSEQ_ASM_TMP_REG_3 "t4"
  31. #define RSEQ_ASM_TMP_REG_4 "t3"
  32. #define rseq_smp_load_acquire(p) \
  33. __extension__ ({ \
  34. rseq_unqual_scalar_typeof(*(p)) ____p1 = RSEQ_READ_ONCE(*(p)); \
  35. RISCV_FENCE(r, rw); \
  36. ____p1; \
  37. })
  38. #define rseq_smp_acquire__after_ctrl_dep() rseq_smp_rmb()
  39. #define rseq_smp_store_release(p, v) \
  40. do { \
  41. RISCV_FENCE(rw, w); \
  42. RSEQ_WRITE_ONCE(*(p), v); \
  43. } while (0)
  44. #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, start_ip, \
  45. post_commit_offset, abort_ip) \
  46. ".pushsection __rseq_cs, \"aw\"\n" \
  47. ".balign 32\n" \
  48. __rseq_str(label) ":\n" \
  49. ".long " __rseq_str(version) ", " __rseq_str(flags) "\n" \
  50. ".quad " __rseq_str(start_ip) ", " \
  51. __rseq_str(post_commit_offset) ", " \
  52. __rseq_str(abort_ip) "\n" \
  53. ".popsection\n\t" \
  54. ".pushsection __rseq_cs_ptr_array, \"aw\"\n" \
  55. ".quad " __rseq_str(label) "b\n" \
  56. ".popsection\n"
  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" \
  70. ".quad " __rseq_str(start_ip) ", " __rseq_str(exit_ip) "\n" \
  71. ".popsection\n"
  72. #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
  73. RSEQ_INJECT_ASM(1) \
  74. "la " RSEQ_ASM_TMP_REG_1 ", " __rseq_str(cs_label) "\n" \
  75. REG_S RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(rseq_cs) "]\n" \
  76. __rseq_str(label) ":\n"
  77. #define RSEQ_ASM_DEFINE_ABORT(label, abort_label) \
  78. "j 222f\n" \
  79. ".balign 4\n" \
  80. ".long " __rseq_str(RSEQ_SIG) "\n" \
  81. __rseq_str(label) ":\n" \
  82. "j %l[" __rseq_str(abort_label) "]\n" \
  83. "222:\n"
  84. #define RSEQ_ASM_OP_STORE(value, var) \
  85. REG_S "%[" __rseq_str(value) "], %[" __rseq_str(var) "]\n"
  86. #define RSEQ_ASM_OP_CMPEQ(var, expect, label) \
  87. REG_L RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
  88. "bne " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(expect) "] ," \
  89. __rseq_str(label) "\n"
  90. #define RSEQ_ASM_OP_CMPEQ32(var, expect, label) \
  91. "lw " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
  92. "bne " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(expect) "] ," \
  93. __rseq_str(label) "\n"
  94. #define RSEQ_ASM_OP_CMPNE(var, expect, label) \
  95. REG_L RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
  96. "beq " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(expect) "] ," \
  97. __rseq_str(label) "\n"
  98. #define RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, label) \
  99. RSEQ_INJECT_ASM(2) \
  100. RSEQ_ASM_OP_CMPEQ32(current_cpu_id, cpu_id, label)
  101. #define RSEQ_ASM_OP_R_LOAD(var) \
  102. REG_L RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n"
  103. #define RSEQ_ASM_OP_R_STORE(var) \
  104. REG_S RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n"
  105. #define RSEQ_ASM_OP_R_LOAD_OFF(offset) \
  106. "add " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(offset) "], " \
  107. RSEQ_ASM_TMP_REG_1 "\n" \
  108. REG_L RSEQ_ASM_TMP_REG_1 ", (" RSEQ_ASM_TMP_REG_1 ")\n"
  109. #define RSEQ_ASM_OP_R_ADD(count) \
  110. "add " RSEQ_ASM_TMP_REG_1 ", " RSEQ_ASM_TMP_REG_1 \
  111. ", %[" __rseq_str(count) "]\n"
  112. #define RSEQ_ASM_OP_FINAL_STORE(value, var, post_commit_label) \
  113. RSEQ_ASM_OP_STORE(value, var) \
  114. __rseq_str(post_commit_label) ":\n"
  115. #define RSEQ_ASM_OP_FINAL_STORE_RELEASE(value, var, post_commit_label) \
  116. "fence rw, w\n" \
  117. RSEQ_ASM_OP_STORE(value, var) \
  118. __rseq_str(post_commit_label) ":\n"
  119. #define RSEQ_ASM_OP_R_FINAL_STORE(var, post_commit_label) \
  120. REG_S RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
  121. __rseq_str(post_commit_label) ":\n"
  122. #define RSEQ_ASM_OP_R_BAD_MEMCPY(dst, src, len) \
  123. "beqz %[" __rseq_str(len) "], 333f\n" \
  124. "mv " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(len) "]\n" \
  125. "mv " RSEQ_ASM_TMP_REG_2 ", %[" __rseq_str(src) "]\n" \
  126. "mv " RSEQ_ASM_TMP_REG_3 ", %[" __rseq_str(dst) "]\n" \
  127. "222:\n" \
  128. "lb " RSEQ_ASM_TMP_REG_4 ", 0(" RSEQ_ASM_TMP_REG_2 ")\n" \
  129. "sb " RSEQ_ASM_TMP_REG_4 ", 0(" RSEQ_ASM_TMP_REG_3 ")\n" \
  130. "addi " RSEQ_ASM_TMP_REG_1 ", " RSEQ_ASM_TMP_REG_1 ", -1\n" \
  131. "addi " RSEQ_ASM_TMP_REG_2 ", " RSEQ_ASM_TMP_REG_2 ", 1\n" \
  132. "addi " RSEQ_ASM_TMP_REG_3 ", " RSEQ_ASM_TMP_REG_3 ", 1\n" \
  133. "bnez " RSEQ_ASM_TMP_REG_1 ", 222b\n" \
  134. "333:\n"
  135. #define RSEQ_ASM_OP_R_DEREF_ADDV(ptr, off, inc, post_commit_label) \
  136. "mv " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(ptr) "]\n" \
  137. RSEQ_ASM_OP_R_ADD(off) \
  138. REG_L RSEQ_ASM_TMP_REG_1 ", 0(" RSEQ_ASM_TMP_REG_1 ")\n" \
  139. RSEQ_ASM_OP_R_ADD(inc) \
  140. __rseq_str(post_commit_label) ":\n"
  141. /* Per-cpu-id indexing. */
  142. #define RSEQ_TEMPLATE_CPU_ID
  143. #define RSEQ_TEMPLATE_MO_RELAXED
  144. #include "rseq-riscv-bits.h"
  145. #undef RSEQ_TEMPLATE_MO_RELAXED
  146. #define RSEQ_TEMPLATE_MO_RELEASE
  147. #include "rseq-riscv-bits.h"
  148. #undef RSEQ_TEMPLATE_MO_RELEASE
  149. #undef RSEQ_TEMPLATE_CPU_ID
  150. /* Per-mm-cid indexing. */
  151. #define RSEQ_TEMPLATE_MM_CID
  152. #define RSEQ_TEMPLATE_MO_RELAXED
  153. #include "rseq-riscv-bits.h"
  154. #undef RSEQ_TEMPLATE_MO_RELAXED
  155. #define RSEQ_TEMPLATE_MO_RELEASE
  156. #include "rseq-riscv-bits.h"
  157. #undef RSEQ_TEMPLATE_MO_RELEASE
  158. #undef RSEQ_TEMPLATE_MM_CID
  159. /* APIs which are not based on cpu ids. */
  160. #define RSEQ_TEMPLATE_CPU_ID_NONE
  161. #define RSEQ_TEMPLATE_MO_RELAXED
  162. #include "rseq-riscv-bits.h"
  163. #undef RSEQ_TEMPLATE_MO_RELAXED
  164. #undef RSEQ_TEMPLATE_CPU_ID_NONE