1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 #include "rseq-bits-template.h"
5 #if defined(RSEQ_TEMPLATE_MO_RELAXED) && \
6 (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID))
8 static inline __attribute__((always_inline))
9 int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_storev)(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)
13 __asm__ __volatile__ goto (
14 RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
15 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
16 #ifdef RSEQ_COMPARE_TWICE
17 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
18 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
20 /* Start rseq by storing table entry pointer into rseq_cs. */
21 RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
22 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
24 LONG_CMP " %[expect], %[v]\n\t"
27 #ifdef RSEQ_COMPARE_TWICE
28 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
29 LONG_CMP " %[expect], %[v]\n\t"
33 LONG_S " %[newv], %[v]\n\t"
36 RSEQ_ASM_DEFINE_ABORT(4, "", abort)
37 : /* gcc asm goto does not allow outputs */
39 [current_cpu_id] "m" (rseq_get_abi()->RSEQ_TEMPLATE_CPU_ID_FIELD),
40 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
42 [expect] "r" (expect),
45 : "memory", "cc", "r0"
48 #ifdef RSEQ_COMPARE_TWICE
52 rseq_after_asm_goto();
55 rseq_after_asm_goto();
59 rseq_after_asm_goto();
61 #ifdef RSEQ_COMPARE_TWICE
63 rseq_after_asm_goto();
64 rseq_bug("cpu_id comparison failed");
66 rseq_after_asm_goto();
67 rseq_bug("expected value comparison failed");
72 * Compare @v against @expectnot. When it does _not_ match, load @v
73 * into @load, and store the content of *@v + voffp into @v.
75 static inline __attribute__((always_inline))
76 int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpnev_storeoffp_load)(intptr_t *v, intptr_t expectnot,
77 long voffp, intptr_t *load, int cpu)
81 __asm__ __volatile__ goto (
82 RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
83 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
84 #ifdef RSEQ_COMPARE_TWICE
85 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
86 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
88 /* Start rseq by storing table entry pointer into rseq_cs. */
89 RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
90 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
92 LONG_L " %%r1, %[v]\n\t"
93 LONG_CMP_R " %%r1, %[expectnot]\n\t"
96 #ifdef RSEQ_COMPARE_TWICE
97 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
98 LONG_L " %%r1, %[v]\n\t"
99 LONG_CMP_R " %%r1, %[expectnot]\n\t"
102 LONG_S " %%r1, %[load]\n\t"
103 LONG_ADD_R " %%r1, %[voffp]\n\t"
104 LONG_L " %%r1, 0(%%r1)\n\t"
106 LONG_S " %%r1, %[v]\n\t"
109 RSEQ_ASM_DEFINE_ABORT(4, "", abort)
110 : /* gcc asm goto does not allow outputs */
111 : [cpu_id] "r" (cpu),
112 [current_cpu_id] "m" (rseq_get_abi()->RSEQ_TEMPLATE_CPU_ID_FIELD),
113 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
114 /* final store input */
116 [expectnot] "r" (expectnot),
120 : "memory", "cc", "r0", "r1"
123 #ifdef RSEQ_COMPARE_TWICE
127 rseq_after_asm_goto();
130 rseq_after_asm_goto();
134 rseq_after_asm_goto();
136 #ifdef RSEQ_COMPARE_TWICE
138 rseq_after_asm_goto();
139 rseq_bug("cpu_id comparison failed");
141 rseq_after_asm_goto();
142 rseq_bug("expected value comparison failed");
146 static inline __attribute__((always_inline))
147 int RSEQ_TEMPLATE_IDENTIFIER(rseq_addv)(intptr_t *v, intptr_t count, int cpu)
151 __asm__ __volatile__ goto (
152 RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
153 #ifdef RSEQ_COMPARE_TWICE
154 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
156 /* Start rseq by storing table entry pointer into rseq_cs. */
157 RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
158 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
160 #ifdef RSEQ_COMPARE_TWICE
161 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
163 LONG_L " %%r0, %[v]\n\t"
164 LONG_ADD_R " %%r0, %[count]\n\t"
166 LONG_S " %%r0, %[v]\n\t"
169 RSEQ_ASM_DEFINE_ABORT(4, "", abort)
170 : /* gcc asm goto does not allow outputs */
171 : [cpu_id] "r" (cpu),
172 [current_cpu_id] "m" (rseq_get_abi()->RSEQ_TEMPLATE_CPU_ID_FIELD),
173 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
174 /* final store input */
178 : "memory", "cc", "r0"
181 #ifdef RSEQ_COMPARE_TWICE
185 rseq_after_asm_goto();
188 rseq_after_asm_goto();
191 #ifdef RSEQ_COMPARE_TWICE
193 rseq_after_asm_goto();
194 rseq_bug("cpu_id comparison failed");
198 static inline __attribute__((always_inline))
199 int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_cmpeqv_storev)(intptr_t *v, intptr_t expect,
200 intptr_t *v2, intptr_t expect2,
201 intptr_t newv, int cpu)
205 __asm__ __volatile__ goto (
206 RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
207 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
208 #ifdef RSEQ_COMPARE_TWICE
209 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
210 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
211 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error3])
213 /* Start rseq by storing table entry pointer into rseq_cs. */
214 RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
215 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
217 LONG_CMP " %[expect], %[v]\n\t"
218 "jnz %l[cmpfail]\n\t"
220 LONG_CMP " %[expect2], %[v2]\n\t"
221 "jnz %l[cmpfail]\n\t"
223 #ifdef RSEQ_COMPARE_TWICE
224 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
225 LONG_CMP " %[expect], %[v]\n\t"
227 LONG_CMP " %[expect2], %[v2]\n\t"
231 LONG_S " %[newv], %[v]\n\t"
234 RSEQ_ASM_DEFINE_ABORT(4, "", abort)
235 : /* gcc asm goto does not allow outputs */
236 : [cpu_id] "r" (cpu),
237 [current_cpu_id] "m" (rseq_get_abi()->RSEQ_TEMPLATE_CPU_ID_FIELD),
238 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
241 [expect2] "r" (expect2),
242 /* final store input */
244 [expect] "r" (expect),
247 : "memory", "cc", "r0"
250 #ifdef RSEQ_COMPARE_TWICE
251 , error1, error2, error3
254 rseq_after_asm_goto();
257 rseq_after_asm_goto();
261 rseq_after_asm_goto();
263 #ifdef RSEQ_COMPARE_TWICE
265 rseq_after_asm_goto();
266 rseq_bug("cpu_id comparison failed");
268 rseq_after_asm_goto();
269 rseq_bug("1st expected value comparison failed");
271 rseq_after_asm_goto();
272 rseq_bug("2nd expected value comparison failed");
276 #endif /* #if defined(RSEQ_TEMPLATE_MO_RELAXED) &&
277 (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID)) */
279 #if (defined(RSEQ_TEMPLATE_MO_RELAXED) || defined(RSEQ_TEMPLATE_MO_RELEASE)) && \
280 (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID))
283 static inline __attribute__((always_inline))
284 int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_trystorev_storev)(intptr_t *v, intptr_t expect,
285 intptr_t *v2, intptr_t newv2,
286 intptr_t newv, int cpu)
290 __asm__ __volatile__ goto (
291 RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
292 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
293 #ifdef RSEQ_COMPARE_TWICE
294 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
295 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
297 /* Start rseq by storing table entry pointer into rseq_cs. */
298 RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
299 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
301 LONG_CMP " %[expect], %[v]\n\t"
302 "jnz %l[cmpfail]\n\t"
304 #ifdef RSEQ_COMPARE_TWICE
305 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
306 LONG_CMP " %[expect], %[v]\n\t"
310 LONG_S " %[newv2], %[v2]\n\t"
313 LONG_S " %[newv], %[v]\n\t"
316 RSEQ_ASM_DEFINE_ABORT(4, "", abort)
317 : /* gcc asm goto does not allow outputs */
318 : [cpu_id] "r" (cpu),
319 [current_cpu_id] "m" (rseq_get_abi()->RSEQ_TEMPLATE_CPU_ID_FIELD),
320 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
321 /* try store input */
324 /* final store input */
326 [expect] "r" (expect),
329 : "memory", "cc", "r0"
332 #ifdef RSEQ_COMPARE_TWICE
336 rseq_after_asm_goto();
339 rseq_after_asm_goto();
343 rseq_after_asm_goto();
345 #ifdef RSEQ_COMPARE_TWICE
347 rseq_after_asm_goto();
348 rseq_bug("cpu_id comparison failed");
350 rseq_after_asm_goto();
351 rseq_bug("expected value comparison failed");
356 static inline __attribute__((always_inline))
357 int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_trymemcpy_storev)(intptr_t *v, intptr_t expect,
358 void *dst, void *src, size_t len,
359 intptr_t newv, int cpu)
361 uint64_t rseq_scratch[3];
365 __asm__ __volatile__ goto (
366 RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
367 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
368 #ifdef RSEQ_COMPARE_TWICE
369 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
370 RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
372 LONG_S " %[src], %[rseq_scratch0]\n\t"
373 LONG_S " %[dst], %[rseq_scratch1]\n\t"
374 LONG_S " %[len], %[rseq_scratch2]\n\t"
375 /* Start rseq by storing table entry pointer into rseq_cs. */
376 RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
377 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
379 LONG_CMP " %[expect], %[v]\n\t"
382 #ifdef RSEQ_COMPARE_TWICE
383 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 6f)
384 LONG_CMP " %[expect], %[v]\n\t"
388 LONG_LT_R " %[len], %[len]\n\t"
391 "ic %%r0,0(%[src])\n\t"
392 "stc %%r0,0(%[dst])\n\t"
393 LONG_ADDI " %[src], 1\n\t"
394 LONG_ADDI " %[dst], 1\n\t"
395 LONG_ADDI " %[len], -1\n\t"
400 LONG_S " %[newv], %[v]\n\t"
404 LONG_L " %[len], %[rseq_scratch2]\n\t"
405 LONG_L " %[dst], %[rseq_scratch1]\n\t"
406 LONG_L " %[src], %[rseq_scratch0]\n\t"
407 RSEQ_ASM_DEFINE_ABORT(4,
408 LONG_L " %[len], %[rseq_scratch2]\n\t"
409 LONG_L " %[dst], %[rseq_scratch1]\n\t"
410 LONG_L " %[src], %[rseq_scratch0]\n\t",
412 RSEQ_ASM_DEFINE_CMPFAIL(5,
413 LONG_L " %[len], %[rseq_scratch2]\n\t"
414 LONG_L " %[dst], %[rseq_scratch1]\n\t"
415 LONG_L " %[src], %[rseq_scratch0]\n\t",
417 #ifdef RSEQ_COMPARE_TWICE
418 RSEQ_ASM_DEFINE_CMPFAIL(6,
419 LONG_L " %[len], %[rseq_scratch2]\n\t"
420 LONG_L " %[dst], %[rseq_scratch1]\n\t"
421 LONG_L " %[src], %[rseq_scratch0]\n\t",
423 RSEQ_ASM_DEFINE_CMPFAIL(7,
424 LONG_L " %[len], %[rseq_scratch2]\n\t"
425 LONG_L " %[dst], %[rseq_scratch1]\n\t"
426 LONG_L " %[src], %[rseq_scratch0]\n\t",
429 : /* gcc asm goto does not allow outputs */
430 : [cpu_id] "r" (cpu),
431 [current_cpu_id] "m" (rseq_get_abi()->RSEQ_TEMPLATE_CPU_ID_FIELD),
432 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
433 /* final store input */
435 [expect] "r" (expect),
437 /* try memcpy input */
441 [rseq_scratch0] "m" (rseq_scratch[0]),
442 [rseq_scratch1] "m" (rseq_scratch[1]),
443 [rseq_scratch2] "m" (rseq_scratch[2])
445 : "memory", "cc", "r0"
448 #ifdef RSEQ_COMPARE_TWICE
452 rseq_after_asm_goto();
455 rseq_after_asm_goto();
459 rseq_after_asm_goto();
461 #ifdef RSEQ_COMPARE_TWICE
463 rseq_after_asm_goto();
464 rseq_bug("cpu_id comparison failed");
466 rseq_after_asm_goto();
467 rseq_bug("expected value comparison failed");
471 #endif /* #if (defined(RSEQ_TEMPLATE_MO_RELAXED) || defined(RSEQ_TEMPLATE_MO_RELEASE)) &&
472 (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID)) */
474 #include "rseq-bits-reset.h"