rseq-riscv.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include <asm/fence.h>
  12. #if defined(__BYTE_ORDER) ? (__BYTE_ORDER == __LITTLE_ENDIAN) : defined(__LITTLE_ENDIAN)
  13. #define RSEQ_SIG 0xf1401073 /* csrr mhartid, x0 */
  14. #else
  15. #error "Currently, RSEQ only supports Little-Endian version"
  16. #endif
  17. #if __riscv_xlen == 64
  18. #define __REG_SEL(a, b) a
  19. #elif __riscv_xlen == 32
  20. #define __REG_SEL(a, b) b
  21. #endif
  22. #define REG_L __REG_SEL("ld ", "lw ")
  23. #define REG_S __REG_SEL("sd ", "sw ")
  24. #define rseq_smp_mb() RISCV_FENCE(rw, rw)
  25. #define rseq_smp_rmb() RISCV_FENCE(r, r)
  26. #define rseq_smp_wmb() RISCV_FENCE(w, w)
  27. #define RSEQ_ASM_TMP_REG_1 "t6"
  28. #define RSEQ_ASM_TMP_REG_2 "t5"
  29. #define RSEQ_ASM_TMP_REG_3 "t4"
  30. #define RSEQ_ASM_TMP_REG_4 "t3"
  31. #define rseq_smp_load_acquire(p) \
  32. __extension__ ({ \
  33. rseq_unqual_scalar_typeof(*(p)) ____p1 = RSEQ_READ_ONCE(*(p)); \
  34. RISCV_FENCE(r, rw); \
  35. ____p1; \
  36. })
  37. #define rseq_smp_acquire__after_ctrl_dep() rseq_smp_rmb()
  38. #define rseq_smp_store_release(p, v) \
  39. do { \
  40. RISCV_FENCE(rw, w); \
  41. RSEQ_WRITE_ONCE(*(p), v); \
  42. } while (0)
  43. #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, start_ip, \
  44. post_commit_offset, abort_ip) \
  45. ".pushsection __rseq_cs, \"aw\"\n" \
  46. ".balign 32\n" \
  47. __rseq_str(label) ":\n" \
  48. ".long " __rseq_str(version) ", " __rseq_str(flags) "\n" \
  49. ".quad " __rseq_str(start_ip) ", " \
  50. __rseq_str(post_commit_offset) ", " \
  51. __rseq_str(abort_ip) "\n" \
  52. ".popsection\n\t" \
  53. ".pushsection __rseq_cs_ptr_array, \"aw\"\n" \
  54. ".quad " __rseq_str(label) "b\n" \
  55. ".popsection\n"
  56. #define RSEQ_ASM_DEFINE_TABLE(label, start_ip, post_commit_ip, abort_ip) \
  57. __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \
  58. ((post_commit_ip) - (start_ip)), abort_ip)
  59. /*
  60. * Exit points of a rseq critical section consist of all instructions outside
  61. * of the critical section where a critical section can either branch to or
  62. * reach through the normal course of its execution. The abort IP and the
  63. * post-commit IP are already part of the __rseq_cs section and should not be
  64. * explicitly defined as additional exit points. Knowing all exit points is
  65. * useful to assist debuggers stepping over the critical section.
  66. */
  67. #define RSEQ_ASM_DEFINE_EXIT_POINT(start_ip, exit_ip) \
  68. ".pushsection __rseq_exit_point_array, \"aw\"\n" \
  69. ".quad " __rseq_str(start_ip) ", " __rseq_str(exit_ip) "\n" \
  70. ".popsection\n"
  71. #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
  72. RSEQ_INJECT_ASM(1) \
  73. "la " RSEQ_ASM_TMP_REG_1 ", " __rseq_str(cs_label) "\n" \
  74. REG_S RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(rseq_cs) "]\n" \
  75. __rseq_str(label) ":\n"
  76. #define RSEQ_ASM_DEFINE_ABORT(label, abort_label) \
  77. "j 222f\n" \
  78. ".balign 4\n" \
  79. ".long " __rseq_str(RSEQ_SIG) "\n" \
  80. __rseq_str(label) ":\n" \
  81. "j %l[" __rseq_str(abort_label) "]\n" \
  82. "222:\n"
  83. #define RSEQ_ASM_OP_STORE(value, var) \
  84. REG_S "%[" __rseq_str(value) "], %[" __rseq_str(var) "]\n"
  85. #define RSEQ_ASM_OP_CMPEQ(var, expect, label) \
  86. REG_L RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
  87. "bne " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(expect) "] ," \
  88. __rseq_str(label) "\n"
  89. #define RSEQ_ASM_OP_CMPEQ32(var, expect, label) \
  90. "lw " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
  91. "bne " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(expect) "] ," \
  92. __rseq_str(label) "\n"
  93. #define RSEQ_ASM_OP_CMPNE(var, expect, label) \
  94. REG_L RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
  95. "beq " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(expect) "] ," \
  96. __rseq_str(label) "\n"
  97. #define RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, label) \
  98. RSEQ_INJECT_ASM(2) \
  99. RSEQ_ASM_OP_CMPEQ32(current_cpu_id, cpu_id, label)
  100. #define RSEQ_ASM_OP_R_LOAD(var) \
  101. REG_L RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n"
  102. #define RSEQ_ASM_OP_R_STORE(var) \
  103. REG_S RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n"
  104. #define RSEQ_ASM_OP_R_LOAD_OFF(offset) \
  105. "add " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(offset) "], " \
  106. RSEQ_ASM_TMP_REG_1 "\n" \
  107. REG_L RSEQ_ASM_TMP_REG_1 ", (" RSEQ_ASM_TMP_REG_1 ")\n"
  108. #define RSEQ_ASM_OP_R_ADD(count) \
  109. "add " RSEQ_ASM_TMP_REG_1 ", " RSEQ_ASM_TMP_REG_1 \
  110. ", %[" __rseq_str(count) "]\n"
  111. #define RSEQ_ASM_OP_FINAL_STORE(value, var, post_commit_label) \
  112. RSEQ_ASM_OP_STORE(value, var) \
  113. __rseq_str(post_commit_label) ":\n"
  114. #define RSEQ_ASM_OP_FINAL_STORE_RELEASE(value, var, post_commit_label) \
  115. "fence rw, w\n" \
  116. RSEQ_ASM_OP_STORE(value, var) \
  117. __rseq_str(post_commit_label) ":\n"
  118. #define RSEQ_ASM_OP_R_FINAL_STORE(var, post_commit_label) \
  119. REG_S RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
  120. __rseq_str(post_commit_label) ":\n"
  121. #define RSEQ_ASM_OP_R_BAD_MEMCPY(dst, src, len) \
  122. "beqz %[" __rseq_str(len) "], 333f\n" \
  123. "mv " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(len) "]\n" \
  124. "mv " RSEQ_ASM_TMP_REG_2 ", %[" __rseq_str(src) "]\n" \
  125. "mv " RSEQ_ASM_TMP_REG_3 ", %[" __rseq_str(dst) "]\n" \
  126. "222:\n" \
  127. "lb " RSEQ_ASM_TMP_REG_4 ", 0(" RSEQ_ASM_TMP_REG_2 ")\n" \
  128. "sb " RSEQ_ASM_TMP_REG_4 ", 0(" RSEQ_ASM_TMP_REG_3 ")\n" \
  129. "addi " RSEQ_ASM_TMP_REG_1 ", " RSEQ_ASM_TMP_REG_1 ", -1\n" \
  130. "addi " RSEQ_ASM_TMP_REG_2 ", " RSEQ_ASM_TMP_REG_2 ", 1\n" \
  131. "addi " RSEQ_ASM_TMP_REG_3 ", " RSEQ_ASM_TMP_REG_3 ", 1\n" \
  132. "bnez " RSEQ_ASM_TMP_REG_1 ", 222b\n" \
  133. "333:\n"
  134. #define RSEQ_ASM_OP_R_DEREF_ADDV(ptr, off, inc, post_commit_label) \
  135. "mv " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(ptr) "]\n" \
  136. RSEQ_ASM_OP_R_ADD(off) \
  137. REG_L RSEQ_ASM_TMP_REG_1 ", 0(" RSEQ_ASM_TMP_REG_1 ")\n" \
  138. RSEQ_ASM_OP_R_ADD(inc) \
  139. __rseq_str(post_commit_label) ":\n"
  140. /* Per-cpu-id indexing. */
  141. #define RSEQ_TEMPLATE_CPU_ID
  142. #define RSEQ_TEMPLATE_MO_RELAXED
  143. #include "rseq-riscv-bits.h"
  144. #undef RSEQ_TEMPLATE_MO_RELAXED
  145. #define RSEQ_TEMPLATE_MO_RELEASE
  146. #include "rseq-riscv-bits.h"
  147. #undef RSEQ_TEMPLATE_MO_RELEASE
  148. #undef RSEQ_TEMPLATE_CPU_ID
  149. /* Per-mm-cid indexing. */
  150. #define RSEQ_TEMPLATE_MM_CID
  151. #define RSEQ_TEMPLATE_MO_RELAXED
  152. #include "rseq-riscv-bits.h"
  153. #undef RSEQ_TEMPLATE_MO_RELAXED
  154. #define RSEQ_TEMPLATE_MO_RELEASE
  155. #include "rseq-riscv-bits.h"
  156. #undef RSEQ_TEMPLATE_MO_RELEASE
  157. #undef RSEQ_TEMPLATE_MM_CID
  158. /* APIs which are not based on cpu ids. */
  159. #define RSEQ_TEMPLATE_CPU_ID_NONE
  160. #define RSEQ_TEMPLATE_MO_RELAXED
  161. #include "rseq-riscv-bits.h"
  162. #undef RSEQ_TEMPLATE_MO_RELAXED
  163. #undef RSEQ_TEMPLATE_CPU_ID_NONE