rseq-arm.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * rseq-arm.h
  4. *
  5. * (C) Copyright 2016-2022 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  6. */
  7. /*
  8. * - ARM little endian
  9. *
  10. * RSEQ_SIG uses the udf A32 instruction with an uncommon immediate operand
  11. * value 0x5de3. This traps if user-space reaches this instruction by mistake,
  12. * and the uncommon operand ensures the kernel does not move the instruction
  13. * pointer to attacker-controlled code on rseq abort.
  14. *
  15. * The instruction pattern in the A32 instruction set is:
  16. *
  17. * e7f5def3 udf #24035 ; 0x5de3
  18. *
  19. * This translates to the following instruction pattern in the T16 instruction
  20. * set:
  21. *
  22. * little endian:
  23. * def3 udf #243 ; 0xf3
  24. * e7f5 b.n <7f5>
  25. *
  26. * - ARMv6+ big endian (BE8):
  27. *
  28. * ARMv6+ -mbig-endian generates mixed endianness code vs data: little-endian
  29. * code and big-endian data. The data value of the signature needs to have its
  30. * byte order reversed to generate the trap instruction:
  31. *
  32. * Data: 0xf3def5e7
  33. *
  34. * Translates to this A32 instruction pattern:
  35. *
  36. * e7f5def3 udf #24035 ; 0x5de3
  37. *
  38. * Translates to this T16 instruction pattern:
  39. *
  40. * def3 udf #243 ; 0xf3
  41. * e7f5 b.n <7f5>
  42. *
  43. * - Prior to ARMv6 big endian (BE32):
  44. *
  45. * Prior to ARMv6, -mbig-endian generates big-endian code and data
  46. * (which match), so the endianness of the data representation of the
  47. * signature should not be reversed. However, the choice between BE32
  48. * and BE8 is done by the linker, so we cannot know whether code and
  49. * data endianness will be mixed before the linker is invoked. So rather
  50. * than try to play tricks with the linker, the rseq signature is simply
  51. * data (not a trap instruction) prior to ARMv6 on big endian. This is
  52. * why the signature is expressed as data (.word) rather than as
  53. * instruction (.inst) in assembler.
  54. */
  55. #ifdef __ARMEB__
  56. #define RSEQ_SIG 0xf3def5e7 /* udf #24035 ; 0x5de3 (ARMv6+) */
  57. #else
  58. #define RSEQ_SIG 0xe7f5def3 /* udf #24035 ; 0x5de3 */
  59. #endif
  60. #define rseq_smp_mb() __asm__ __volatile__ ("dmb" ::: "memory", "cc")
  61. #define rseq_smp_rmb() __asm__ __volatile__ ("dmb" ::: "memory", "cc")
  62. #define rseq_smp_wmb() __asm__ __volatile__ ("dmb" ::: "memory", "cc")
  63. #define rseq_smp_load_acquire(p) \
  64. __extension__ ({ \
  65. rseq_unqual_scalar_typeof(*(p)) ____p1 = RSEQ_READ_ONCE(*(p)); \
  66. rseq_smp_mb(); \
  67. ____p1; \
  68. })
  69. #define rseq_smp_acquire__after_ctrl_dep() rseq_smp_rmb()
  70. #define rseq_smp_store_release(p, v) \
  71. do { \
  72. rseq_smp_mb(); \
  73. RSEQ_WRITE_ONCE(*(p), v); \
  74. } while (0)
  75. #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, start_ip, \
  76. post_commit_offset, abort_ip) \
  77. ".pushsection __rseq_cs, \"aw\"\n\t" \
  78. ".balign 32\n\t" \
  79. __rseq_str(label) ":\n\t" \
  80. ".word " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \
  81. ".word " __rseq_str(start_ip) ", 0x0, " __rseq_str(post_commit_offset) ", 0x0, " __rseq_str(abort_ip) ", 0x0\n\t" \
  82. ".popsection\n\t" \
  83. ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \
  84. ".word " __rseq_str(label) "b, 0x0\n\t" \
  85. ".popsection\n\t"
  86. #define RSEQ_ASM_DEFINE_TABLE(label, start_ip, post_commit_ip, abort_ip) \
  87. __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \
  88. (post_commit_ip - start_ip), abort_ip)
  89. /*
  90. * Exit points of a rseq critical section consist of all instructions outside
  91. * of the critical section where a critical section can either branch to or
  92. * reach through the normal course of its execution. The abort IP and the
  93. * post-commit IP are already part of the __rseq_cs section and should not be
  94. * explicitly defined as additional exit points. Knowing all exit points is
  95. * useful to assist debuggers stepping over the critical section.
  96. */
  97. #define RSEQ_ASM_DEFINE_EXIT_POINT(start_ip, exit_ip) \
  98. ".pushsection __rseq_exit_point_array, \"aw\"\n\t" \
  99. ".word " __rseq_str(start_ip) ", 0x0, " __rseq_str(exit_ip) ", 0x0\n\t" \
  100. ".popsection\n\t"
  101. #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
  102. RSEQ_INJECT_ASM(1) \
  103. "adr r0, " __rseq_str(cs_label) "\n\t" \
  104. "str r0, %[" __rseq_str(rseq_cs) "]\n\t" \
  105. __rseq_str(label) ":\n\t"
  106. #define RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, label) \
  107. RSEQ_INJECT_ASM(2) \
  108. "ldr r0, %[" __rseq_str(current_cpu_id) "]\n\t" \
  109. "cmp %[" __rseq_str(cpu_id) "], r0\n\t" \
  110. "bne " __rseq_str(label) "\n\t"
  111. #define __RSEQ_ASM_DEFINE_ABORT(table_label, label, teardown, \
  112. abort_label, version, flags, \
  113. start_ip, post_commit_offset, abort_ip) \
  114. ".balign 32\n\t" \
  115. __rseq_str(table_label) ":\n\t" \
  116. ".word " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \
  117. ".word " __rseq_str(start_ip) ", 0x0, " __rseq_str(post_commit_offset) ", 0x0, " __rseq_str(abort_ip) ", 0x0\n\t" \
  118. ".word " __rseq_str(RSEQ_SIG) "\n\t" \
  119. __rseq_str(label) ":\n\t" \
  120. teardown \
  121. "b %l[" __rseq_str(abort_label) "]\n\t"
  122. #define RSEQ_ASM_DEFINE_ABORT(table_label, label, teardown, abort_label, \
  123. start_ip, post_commit_ip, abort_ip) \
  124. __RSEQ_ASM_DEFINE_ABORT(table_label, label, teardown, \
  125. abort_label, 0x0, 0x0, start_ip, \
  126. (post_commit_ip - start_ip), abort_ip)
  127. #define RSEQ_ASM_DEFINE_CMPFAIL(label, teardown, cmpfail_label) \
  128. __rseq_str(label) ":\n\t" \
  129. teardown \
  130. "b %l[" __rseq_str(cmpfail_label) "]\n\t"
  131. /* Per-cpu-id indexing. */
  132. #define RSEQ_TEMPLATE_CPU_ID
  133. #define RSEQ_TEMPLATE_MO_RELAXED
  134. #include "rseq-arm-bits.h"
  135. #undef RSEQ_TEMPLATE_MO_RELAXED
  136. #define RSEQ_TEMPLATE_MO_RELEASE
  137. #include "rseq-arm-bits.h"
  138. #undef RSEQ_TEMPLATE_MO_RELEASE
  139. #undef RSEQ_TEMPLATE_CPU_ID
  140. /* Per-mm-cid indexing. */
  141. #define RSEQ_TEMPLATE_MM_CID
  142. #define RSEQ_TEMPLATE_MO_RELAXED
  143. #include "rseq-arm-bits.h"
  144. #undef RSEQ_TEMPLATE_MO_RELAXED
  145. #define RSEQ_TEMPLATE_MO_RELEASE
  146. #include "rseq-arm-bits.h"
  147. #undef RSEQ_TEMPLATE_MO_RELEASE
  148. #undef RSEQ_TEMPLATE_MM_CID
  149. /* APIs which are not based on cpu ids. */
  150. #define RSEQ_TEMPLATE_CPU_ID_NONE
  151. #define RSEQ_TEMPLATE_MO_RELAXED
  152. #include "rseq-arm-bits.h"
  153. #undef RSEQ_TEMPLATE_MO_RELAXED
  154. #undef RSEQ_TEMPLATE_CPU_ID_NONE