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 #error "Never use <rseq/arch/aarch64.h> directly; include <rseq/rseq.h> instead."
14 * RSEQ_ASM_*() macro helpers are internal to the librseq headers. Those
15 * are not part of the public API.
19 * aarch64 -mbig-endian generates mixed endianness code vs data:
20 * little-endian code and big-endian data. Ensure the RSEQ_SIG signature
21 * matches code endianness.
23 #define RSEQ_SIG_CODE 0xd428bc00 /* BRK #0x45E0. */
26 #define RSEQ_SIG_DATA 0x00bc28d4 /* BRK #0x45E0. */
28 #define RSEQ_SIG_DATA RSEQ_SIG_CODE
31 #define RSEQ_SIG RSEQ_SIG_DATA
34 * Refer to the Linux kernel memory model (LKMM) for documentation of
35 * the memory barriers.
38 /* CPU memory barrier. */
39 #define rseq_smp_mb() __asm__ __volatile__ ("dmb ish" ::: "memory")
40 /* CPU read memory barrier */
41 #define rseq_smp_rmb() __asm__ __volatile__ ("dmb ishld" ::: "memory")
42 /* CPU write memory barrier */
43 #define rseq_smp_wmb() __asm__ __volatile__ ("dmb ishst" ::: "memory")
45 /* Acquire: One-way permeable barrier. */
46 #define rseq_smp_load_acquire(p) \
48 union { rseq_unqual_scalar_typeof(*(p)) __val; char __c[sizeof(*(p))]; } __u; \
49 switch (sizeof(*(p))) { \
51 __asm__ __volatile__ ("ldarb %w0, %1" \
52 : "=r" (*(__u8 *)__u.__c) \
53 : "Q" (*(p)) : "memory"); \
56 __asm__ __volatile__ ("ldarh %w0, %1" \
57 : "=r" (*(__u16 *)__u.__c) \
58 : "Q" (*(p)) : "memory"); \
61 __asm__ __volatile__ ("ldar %w0, %1" \
62 : "=r" (*(__u32 *)__u.__c) \
63 : "Q" (*(p)) : "memory"); \
66 __asm__ __volatile__ ("ldar %0, %1" \
67 : "=r" (*(__u64 *)__u.__c) \
68 : "Q" (*(p)) : "memory"); \
71 (rseq_unqual_scalar_typeof(*(p)))__u.__val; \
74 /* Acquire barrier after control dependency. */
75 #define rseq_smp_acquire__after_ctrl_dep() rseq_smp_rmb()
77 /* Release: One-way permeable barrier. */
78 #define rseq_smp_store_release(p, v) \
80 union { rseq_unqual_scalar_typeof(*(p)) __val; char __c[sizeof(*(p))]; } __u = \
81 { .__val = (rseq_unqual_scalar_typeof(*(p))) (v) }; \
82 switch (sizeof(*(p))) { \
84 __asm__ __volatile__ ("stlrb %w1, %0" \
86 : "r" (*(__u8 *)__u.__c) \
90 __asm__ __volatile__ ("stlrh %w1, %0" \
92 : "r" (*(__u16 *)__u.__c) \
96 __asm__ __volatile__ ("stlr %w1, %0" \
98 : "r" (*(__u32 *)__u.__c) \
102 __asm__ __volatile__ ("stlr %1, %0" \
104 : "r" (*(__u64 *)__u.__c) \
110 /* Temporary scratch registers. */
111 #define RSEQ_ASM_TMP_REG32 "w15"
112 #define RSEQ_ASM_TMP_REG "x15"
113 #define RSEQ_ASM_TMP_REG_2 "x14"
115 /* Only used in RSEQ_ASM_DEFINE_TABLE. */
116 #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, start_ip, \
117 post_commit_offset, abort_ip) \
118 " .pushsection __rseq_cs, \"aw\"\n" \
120 __rseq_str(label) ":\n" \
121 " .long " __rseq_str(version) ", " __rseq_str(flags) "\n" \
122 " .quad " __rseq_str(start_ip) ", " \
123 __rseq_str(post_commit_offset) ", " \
124 __rseq_str(abort_ip) "\n" \
126 " .pushsection __rseq_cs_ptr_array, \"aw\"\n" \
127 " .quad " __rseq_str(label) "b\n" \
131 * Define an rseq critical section structure of version 0 with no flags.
134 * Local label for the beginning of the critical section descriptor
137 * Pointer to the first instruction of the sequence of consecutive assembly
140 * Pointer to the instruction after the last instruction of the sequence of
141 * consecutive assembly instructions.
143 * Pointer to the instruction where to move the execution flow in case of
144 * abort of the sequence of consecutive assembly instructions.
146 #define RSEQ_ASM_DEFINE_TABLE(label, start_ip, post_commit_ip, abort_ip) \
147 __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \
148 (post_commit_ip) - (start_ip), abort_ip)
151 * Define the @exit_ip pointer as an exit point for the sequence of consecutive
152 * assembly instructions at @start_ip.
155 * Pointer to the first instruction of the sequence of consecutive assembly
158 * Pointer to an exit point instruction.
160 * Exit points of a rseq critical section consist of all instructions outside
161 * of the critical section where a critical section can either branch to or
162 * reach through the normal course of its execution. The abort IP and the
163 * post-commit IP are already part of the __rseq_cs section and should not be
164 * explicitly defined as additional exit points. Knowing all exit points is
165 * useful to assist debuggers stepping over the critical section.
167 #define RSEQ_ASM_DEFINE_EXIT_POINT(start_ip, exit_ip) \
168 " .pushsection __rseq_exit_point_array, \"aw\"\n" \
169 " .quad " __rseq_str(start_ip) ", " __rseq_str(exit_ip) "\n" \
173 * Define a critical section abort handler.
176 * Local label to the abort handler.
178 * Sequence of instructions to run on abort.
180 * C label to jump to at the end of the sequence.
182 #define RSEQ_ASM_DEFINE_ABORT(label, teardown, abort_label) \
184 " .inst " __rseq_str(RSEQ_SIG_CODE) "\n" \
185 __rseq_str(label) ":\n" \
187 " b %l[" __rseq_str(abort_label) "]\n" \
190 /* Jump to local label @label when @cpu_id != @current_cpu_id. */
191 #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
193 " adrp " RSEQ_ASM_TMP_REG ", " __rseq_str(cs_label) "\n" \
194 " add " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
195 ", :lo12:" __rseq_str(cs_label) "\n" \
196 " str " RSEQ_ASM_TMP_REG ", %[" __rseq_str(rseq_cs) "]\n" \
197 __rseq_str(label) ":\n"
199 /* Store @value to address @var. */
200 #define RSEQ_ASM_OP_STORE(value, var) \
201 " str %[" __rseq_str(value) "], %[" __rseq_str(var) "]\n"
203 /* Store-release @value to address @var. */
204 #define RSEQ_ASM_OP_STORE_RELEASE(value, var) \
205 " stlr %[" __rseq_str(value) "], %[" __rseq_str(var) "]\n"
208 * End-of-sequence store of @value to address @var. Emit
209 * @post_commit_label label after the store instruction.
211 #define RSEQ_ASM_OP_FINAL_STORE(value, var, post_commit_label) \
212 RSEQ_ASM_OP_STORE(value, var) \
213 __rseq_str(post_commit_label) ":\n"
216 * End-of-sequence store-release of @value to address @var. Emit
217 * @post_commit_label label after the store instruction.
219 #define RSEQ_ASM_OP_FINAL_STORE_RELEASE(value, var, post_commit_label) \
220 RSEQ_ASM_OP_STORE_RELEASE(value, var) \
221 __rseq_str(post_commit_label) ":\n"
223 /* Jump to local label @label when @var != @expect. */
224 #define RSEQ_ASM_OP_CBNE(var, expect, label) \
225 " ldr " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n" \
226 " sub " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
227 ", %[" __rseq_str(expect) "]\n" \
228 " cbnz " RSEQ_ASM_TMP_REG ", " __rseq_str(label) "\n"
231 * Jump to local label @label when @var != @expect (32-bit register
234 #define RSEQ_ASM_OP_CBNE32(var, expect, label) \
235 " ldr " RSEQ_ASM_TMP_REG32 ", %[" __rseq_str(var) "]\n" \
236 " sub " RSEQ_ASM_TMP_REG32 ", " RSEQ_ASM_TMP_REG32 \
237 ", %w[" __rseq_str(expect) "]\n" \
238 " cbnz " RSEQ_ASM_TMP_REG32 ", " __rseq_str(label) "\n"
240 /* Jump to local label @label when @var == @expect. */
241 #define RSEQ_ASM_OP_CBEQ(var, expect, label) \
242 " ldr " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n" \
243 " sub " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
244 ", %[" __rseq_str(expect) "]\n" \
245 " cbz " RSEQ_ASM_TMP_REG ", " __rseq_str(label) "\n"
247 /* Jump to local label @label when @cpu_id != @current_cpu_id. */
248 #define RSEQ_ASM_CBNE_CPU_ID(cpu_id, current_cpu_id, label) \
250 RSEQ_ASM_OP_CBNE32(current_cpu_id, cpu_id, label)
252 /* Load @var into temporary register. */
253 #define RSEQ_ASM_OP_R_LOAD(var) \
254 " ldr " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n"
256 /* Store from temporary register into @var. */
257 #define RSEQ_ASM_OP_R_STORE(var) \
258 " str " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n"
260 /* Load from address in temporary register+@offset into temporary register. */
261 #define RSEQ_ASM_OP_R_LOAD_OFF(offset) \
262 " ldr " RSEQ_ASM_TMP_REG ", [" RSEQ_ASM_TMP_REG \
263 ", %[" __rseq_str(offset) "]]\n"
265 /* Add @count to temporary register. */
266 #define RSEQ_ASM_OP_R_ADD(count) \
267 " add " RSEQ_ASM_TMP_REG ", " RSEQ_ASM_TMP_REG \
268 ", %[" __rseq_str(count) "]\n"
271 * End-of-sequence store of temporary register to address @var. Emit
272 * @post_commit_label label after the store instruction.
274 #define RSEQ_ASM_OP_R_FINAL_STORE(var, post_commit_label) \
275 " str " RSEQ_ASM_TMP_REG ", %[" __rseq_str(var) "]\n" \
276 __rseq_str(post_commit_label) ":\n"
279 * Copy @len bytes from @src to @dst. This is an inefficient bytewise
280 * copy and could be improved in the future.
282 #define RSEQ_ASM_OP_R_BYTEWISE_MEMCPY(dst, src, len) \
283 " cbz %[" __rseq_str(len) "], 333f\n" \
284 " mov " RSEQ_ASM_TMP_REG_2 ", %[" __rseq_str(len) "]\n" \
285 "222: sub " RSEQ_ASM_TMP_REG_2 ", " RSEQ_ASM_TMP_REG_2 ", #1\n" \
286 " ldrb " RSEQ_ASM_TMP_REG32 ", [%[" __rseq_str(src) "]" \
287 ", " RSEQ_ASM_TMP_REG_2 "]\n" \
288 " strb " RSEQ_ASM_TMP_REG32 ", [%[" __rseq_str(dst) "]" \
289 ", " RSEQ_ASM_TMP_REG_2 "]\n" \
290 " cbnz " RSEQ_ASM_TMP_REG_2 ", 222b\n" \
293 /* Per-cpu-id indexing. */
295 #define RSEQ_TEMPLATE_INDEX_CPU_ID
296 #define RSEQ_TEMPLATE_MO_RELAXED
297 #include "rseq/arch/aarch64/bits.h"
298 #undef RSEQ_TEMPLATE_MO_RELAXED
300 #define RSEQ_TEMPLATE_MO_RELEASE
301 #include "rseq/arch/aarch64/bits.h"
302 #undef RSEQ_TEMPLATE_MO_RELEASE
303 #undef RSEQ_TEMPLATE_INDEX_CPU_ID
305 /* Per-mm-cid indexing. */
307 #define RSEQ_TEMPLATE_INDEX_MM_CID
308 #define RSEQ_TEMPLATE_MO_RELAXED
309 #include "rseq/arch/aarch64/bits.h"
310 #undef RSEQ_TEMPLATE_MO_RELAXED
312 #define RSEQ_TEMPLATE_MO_RELEASE
313 #include "rseq/arch/aarch64/bits.h"
314 #undef RSEQ_TEMPLATE_MO_RELEASE
315 #undef RSEQ_TEMPLATE_INDEX_MM_CID
317 /* APIs which are not indexed. */
319 #define RSEQ_TEMPLATE_INDEX_NONE
320 #define RSEQ_TEMPLATE_MO_RELAXED
321 #include "rseq/arch/aarch64/bits.h"
322 #undef RSEQ_TEMPLATE_MO_RELAXED
323 #undef RSEQ_TEMPLATE_INDEX_NONE
This page took 0.041112 seconds and 4 git commands to generate.