1 /* SPDX-License-Identifier: MIT */
2 /* SPDX-FileCopyrightText: 2016-2024 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> */
3 /* SPDX-FileCopyrightText: 2018 Will Deacon <will.deacon@arm.com> */
10 * RSEQ_ASM_*() macro helpers are internal to the librseq headers. Those
11 * are not part of the public API.
15 * aarch64 -mbig-endian generates mixed endianness code vs data:
16 * little-endian code and big-endian data. Ensure the RSEQ_SIG signature
17 * matches code endianness.
19 #define RSEQ_SIG_CODE 0xd428bc00 /* BRK #0x45E0. */
22 #define RSEQ_SIG_DATA 0x00bc28d4 /* BRK #0x45E0. */
24 #define RSEQ_SIG_DATA RSEQ_SIG_CODE
27 #define RSEQ_SIG RSEQ_SIG_DATA
30 * Refer to the Linux kernel memory model (LKMM) for documentation of
31 * the memory barriers.
34 /* CPU memory barrier. */
35 #define rseq_smp_mb() __asm__ __volatile__ ("dmb ish" ::: "memory")
36 /* CPU read memory barrier */
37 #define rseq_smp_rmb() __asm__ __volatile__ ("dmb ishld" ::: "memory")
38 /* CPU write memory barrier */
39 #define rseq_smp_wmb() __asm__ __volatile__ ("dmb ishst" ::: "memory")
41 /* Acquire: One-way permeable barrier. */
42 #define rseq_smp_load_acquire(p) \
44 union { rseq_unqual_scalar_typeof(*(p)) __val; char __c[sizeof(*(p))]; } __u; \
45 switch (sizeof(*(p))) { \
47 __asm__ __volatile__ ("ldarb %w0, %1" \
48 : "=r" (*(__u8 *)__u.__c) \
49 : "Q" (*(p)) : "memory"); \
52 __asm__ __volatile__ ("ldarh %w0, %1" \
53 : "=r" (*(__u16 *)__u.__c) \
54 : "Q" (*(p)) : "memory"); \
57 __asm__ __volatile__ ("ldar %w0, %1" \
58 : "=r" (*(__u32 *)__u.__c) \
59 : "Q" (*(p)) : "memory"); \
62 __asm__ __volatile__ ("ldar %0, %1" \
63 : "=r" (*(__u64 *)__u.__c) \
64 : "Q" (*(p)) : "memory"); \
67 (rseq_unqual_scalar_typeof(*(p)))__u.__val; \
70 /* Acquire barrier after control dependency. */
71 #define rseq_smp_acquire__after_ctrl_dep() rseq_smp_rmb()
73 /* Release: One-way permeable barrier. */
74 #define rseq_smp_store_release(p, v) \
76 union { rseq_unqual_scalar_typeof(*(p)) __val; char __c[sizeof(*(p))]; } __u = \
77 { .__val = (rseq_unqual_scalar_typeof(*(p))) (v) }; \
78 switch (sizeof(*(p))) { \
80 __asm__ __volatile__ ("stlrb %w1, %0" \
82 : "r" (*(__u8 *)__u.__c) \
86 __asm__ __volatile__ ("stlrh %w1, %0" \
88 : "r" (*(__u16 *)__u.__c) \
92 __asm__ __volatile__ ("stlr %w1, %0" \
94 : "r" (*(__u32 *)__u.__c) \
98 __asm__ __volatile__ ("stlr %1, %0" \
100 : "r" (*(__u64 *)__u.__c) \
106 /* Temporary scratch registers. */
107 #define RSEQ_ASM_TMP_REG32 "w15"
108 #define RSEQ_ASM_TMP_REG "x15"
109 #define RSEQ_ASM_TMP_REG_2 "x14"
111 /* Only used in RSEQ_ASM_DEFINE_TABLE. */
112 #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, start_ip, \
113 post_commit_offset, abort_ip) \
114 " .pushsection __rseq_cs, \"aw\"\n" \
116 __rseq_str(label) ":\n" \
117 " .long " __rseq_str(version) ", " __rseq_str(flags) "\n" \
118 " .quad " __rseq_str(start_ip) ", " \
119 __rseq_str(post_commit_offset) ", " \
120 __rseq_str(abort_ip) "\n" \
122 " .pushsection __rseq_cs_ptr_array, \"aw\"\n" \
123 " .quad " __rseq_str(label) "b\n" \
127 * Define an rseq critical section structure of version 0 with no flags.
130 * Local label for the beginning of the critical section descriptor
133 * Pointer to the first instruction of the sequence of consecutive assembly
136 * Pointer to the instruction after the last instruction of the sequence of
137 * consecutive assembly instructions.
139 * Pointer to the instruction where to move the execution flow in case of
140 * abort of the sequence of consecutive assembly instructions.
142 #define RSEQ_ASM_DEFINE_TABLE(label, start_ip, post_commit_ip, abort_ip) \
143 __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \
144 (post_commit_ip - start_ip), abort_ip)
147 * Define the @exit_ip pointer as an exit point for the sequence of consecutive
148 * assembly instructions at @start_ip.
151 * Pointer to the first instruction of the sequence of consecutive assembly
154 * Pointer to an exit point instruction.
156 * Exit points of a rseq critical section consist of all instructions outside
157 * of the critical section where a critical section can either branch to or
158 * reach through the normal course of its execution. The abort IP and the
159 * post-commit IP are already part of the __rseq_cs section and should not be
160 * explicitly defined as additional exit points. Knowing all exit points is
161 * useful to assist debuggers stepping over the critical section.
163 #define RSEQ_ASM_DEFINE_EXIT_POINT(start_ip, exit_ip) \
164 " .pushsection __rseq_exit_point_array, \"aw\"\n" \
165 " .quad " __rseq_str(start_ip) ", " __rseq_str(exit_ip) "\n" \
169 * Define a critical section abort handler.
172 * Local label to the abort handler.
174 * Sequence of instructions to run on abort.
176 * C label to jump to at the end of the sequence.
178 #define RSEQ_ASM_DEFINE_ABORT(label, teardown, abort_label) \
180 " .inst " __rseq_str(RSEQ_SIG_CODE) "\n" \
181 __rseq_str(label) ":\n" \
183 " b %l[" __rseq_str(abort_label) "]\n" \
186 /* Jump to local label @label when @cpu_id != @current_cpu_id. */
187 #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
189 " adrp " RSEQ_ASM_TMP_REG ", " __rseq_str(cs_label) "\n" \
190 " add " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
191 ", :lo12:" __rseq_str(cs_label) "\n" \
192 " str " RSEQ_ASM_TMP_REG ", %[" __rseq_str(rseq_cs) "]\n" \
193 __rseq_str(label) ":\n"
195 /* Store @value to address @var. */
196 #define RSEQ_ASM_OP_STORE(value, var) \
197 " str %[" __rseq_str(value) "], %[" __rseq_str(var) "]\n"
199 /* Store-release @value to address @var. */
200 #define RSEQ_ASM_OP_STORE_RELEASE(value, var) \
201 " stlr %[" __rseq_str(value) "], %[" __rseq_str(var) "]\n"
204 * End-of-sequence store of @value to address @var. Emit
205 * @post_commit_label label after the store instruction.
207 #define RSEQ_ASM_OP_FINAL_STORE(value, var, post_commit_label) \
208 RSEQ_ASM_OP_STORE(value, var) \
209 __rseq_str(post_commit_label) ":\n"
212 * End-of-sequence store-release of @value to address @var. Emit
213 * @post_commit_label label after the store instruction.
215 #define RSEQ_ASM_OP_FINAL_STORE_RELEASE(value, var, post_commit_label) \
216 RSEQ_ASM_OP_STORE_RELEASE(value, var) \
217 __rseq_str(post_commit_label) ":\n"
219 /* Jump to local label @label when @var != @expect. */
220 #define RSEQ_ASM_OP_CBNE(var, expect, label) \
221 " ldr " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n" \
222 " sub " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
223 ", %[" __rseq_str(expect) "]\n" \
224 " cbnz " RSEQ_ASM_TMP_REG ", " __rseq_str(label) "\n"
227 * Jump to local label @label when @var != @expect (32-bit register
230 #define RSEQ_ASM_OP_CBNE32(var, expect, label) \
231 " ldr " RSEQ_ASM_TMP_REG32 ", %[" __rseq_str(var) "]\n" \
232 " sub " RSEQ_ASM_TMP_REG32 ", " RSEQ_ASM_TMP_REG32 \
233 ", %w[" __rseq_str(expect) "]\n" \
234 " cbnz " RSEQ_ASM_TMP_REG32 ", " __rseq_str(label) "\n"
236 /* Jump to local label @label when @var == @expect. */
237 #define RSEQ_ASM_OP_CBEQ(var, expect, label) \
238 " ldr " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n" \
239 " sub " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
240 ", %[" __rseq_str(expect) "]\n" \
241 " cbz " RSEQ_ASM_TMP_REG ", " __rseq_str(label) "\n"
243 /* Jump to local label @label when @cpu_id != @current_cpu_id. */
244 #define RSEQ_ASM_CBNE_CPU_ID(cpu_id, current_cpu_id, label) \
246 RSEQ_ASM_OP_CBNE32(current_cpu_id, cpu_id, label)
248 /* Load @var into temporary register. */
249 #define RSEQ_ASM_OP_R_LOAD(var) \
250 " ldr " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n"
252 /* Store from temporary register into @var. */
253 #define RSEQ_ASM_OP_R_STORE(var) \
254 " str " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n"
256 /* Load from address in temporary register+@offset into temporary register. */
257 #define RSEQ_ASM_OP_R_LOAD_OFF(offset) \
258 " ldr " RSEQ_ASM_TMP_REG ", [" RSEQ_ASM_TMP_REG \
259 ", %[" __rseq_str(offset) "]]\n"
261 /* Add @count to temporary register. */
262 #define RSEQ_ASM_OP_R_ADD(count) \
263 " add " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
264 ", %[" __rseq_str(count) "]\n"
267 * End-of-sequence store of temporary register to address @var. Emit
268 * @post_commit_label label after the store instruction.
270 #define RSEQ_ASM_OP_R_FINAL_STORE(var, post_commit_label) \
271 " str " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n" \
272 __rseq_str(post_commit_label) ":\n"
275 * Copy @len bytes from @src to @dst. This is an inefficient bytewise
276 * copy and could be improved in the future.
278 #define RSEQ_ASM_OP_R_BYTEWISE_MEMCPY(dst, src, len) \
279 " cbz %[" __rseq_str(len) "], 333f\n" \
280 " mov " RSEQ_ASM_TMP_REG_2 ", %[" __rseq_str(len) "]\n" \
281 "222: sub " RSEQ_ASM_TMP_REG_2 ", " RSEQ_ASM_TMP_REG_2 ", #1\n" \
282 " ldrb " RSEQ_ASM_TMP_REG32 ", [%[" __rseq_str(src) "]" \
283 ", " RSEQ_ASM_TMP_REG_2 "]\n" \
284 " strb " RSEQ_ASM_TMP_REG32 ", [%[" __rseq_str(dst) "]" \
285 ", " RSEQ_ASM_TMP_REG_2 "]\n" \
286 " cbnz " RSEQ_ASM_TMP_REG_2 ", 222b\n" \
289 /* Per-cpu-id indexing. */
291 #define RSEQ_TEMPLATE_INDEX_CPU_ID
292 #define RSEQ_TEMPLATE_MO_RELAXED
293 #include "rseq-arm64-bits.h"
294 #undef RSEQ_TEMPLATE_MO_RELAXED
296 #define RSEQ_TEMPLATE_MO_RELEASE
297 #include "rseq-arm64-bits.h"
298 #undef RSEQ_TEMPLATE_MO_RELEASE
299 #undef RSEQ_TEMPLATE_INDEX_CPU_ID
301 /* Per-mm-cid indexing. */
303 #define RSEQ_TEMPLATE_INDEX_MM_CID
304 #define RSEQ_TEMPLATE_MO_RELAXED
305 #include "rseq-arm64-bits.h"
306 #undef RSEQ_TEMPLATE_MO_RELAXED
308 #define RSEQ_TEMPLATE_MO_RELEASE
309 #include "rseq-arm64-bits.h"
310 #undef RSEQ_TEMPLATE_MO_RELEASE
311 #undef RSEQ_TEMPLATE_INDEX_MM_CID
313 /* APIs which are not indexed. */
315 #define RSEQ_TEMPLATE_INDEX_NONE
316 #define RSEQ_TEMPLATE_MO_RELAXED
317 #include "rseq-arm64-bits.h"
318 #undef RSEQ_TEMPLATE_MO_RELAXED
319 #undef RSEQ_TEMPLATE_INDEX_NONE
This page took 0.060913 seconds and 4 git commands to generate.