Add RISC-V rseq support
[librseq.git] / include / rseq / rseq-riscv.h
CommitLineData
074b1077
MJ
1/* SPDX-License-Identifier: LGPL-2.1-only 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
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
18#if __riscv_xlen == 64
19#define __REG_SEL(a, b) a
20#elif __riscv_xlen == 32
21#define __REG_SEL(a, b) b
22#endif
23
24#define REG_L __REG_SEL("ld ", "lw ")
25#define REG_S __REG_SEL("sd ", "sw ")
26
27#define RISCV_FENCE(p, s) \
28 __asm__ __volatile__ ("fence " #p "," #s : : : "memory")
29#define rseq_smp_mb() RISCV_FENCE(rw, rw)
30#define rseq_smp_rmb() RISCV_FENCE(r, r)
31#define rseq_smp_wmb() RISCV_FENCE(w, w)
32#define RSEQ_ASM_TMP_REG_1 "t6"
33#define RSEQ_ASM_TMP_REG_2 "t5"
34#define RSEQ_ASM_TMP_REG_3 "t4"
35#define RSEQ_ASM_TMP_REG_4 "t3"
36
37#define rseq_smp_load_acquire(p) \
38__extension__ ({ \
39 __typeof(*(p)) ____p1 = RSEQ_READ_ONCE(*(p)); \
40 RISCV_FENCE(r, rw); \
41 ____p1; \
42})
43
44#define rseq_smp_acquire__after_ctrl_dep() rseq_smp_rmb()
45
46#define rseq_smp_store_release(p, v) \
47do { \
48 RISCV_FENCE(rw, w); \
49 RSEQ_WRITE_ONCE(*(p), v); \
50} while (0)
51
52#ifdef RSEQ_SKIP_FASTPATH
53#include "rseq-skip.h"
54#else /* !RSEQ_SKIP_FASTPATH */
55
56#define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, start_ip, \
57 post_commit_offset, abort_ip) \
58 ".pushsection __rseq_cs, \"aw\"\n" \
59 ".balign 32\n" \
60 __rseq_str(label) ":\n" \
61 ".long " __rseq_str(version) ", " __rseq_str(flags) "\n" \
62 ".quad " __rseq_str(start_ip) ", " \
63 __rseq_str(post_commit_offset) ", " \
64 __rseq_str(abort_ip) "\n" \
65 ".popsection\n\t" \
66 ".pushsection __rseq_cs_ptr_array, \"aw\"\n" \
67 ".quad " __rseq_str(label) "b\n" \
68 ".popsection\n"
69
70#define RSEQ_ASM_DEFINE_TABLE(label, start_ip, post_commit_ip, abort_ip) \
71 __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \
72 ((post_commit_ip) - (start_ip)), abort_ip)
73
74/*
75 * Exit points of a rseq critical section consist of all instructions outside
76 * of the critical section where a critical section can either branch to or
77 * reach through the normal course of its execution. The abort IP and the
78 * post-commit IP are already part of the __rseq_cs section and should not be
79 * explicitly defined as additional exit points. Knowing all exit points is
80 * useful to assist debuggers stepping over the critical section.
81 */
82#define RSEQ_ASM_DEFINE_EXIT_POINT(start_ip, exit_ip) \
83 ".pushsection __rseq_exit_point_array, \"aw\"\n" \
84 ".quad " __rseq_str(start_ip) ", " __rseq_str(exit_ip) "\n" \
85 ".popsection\n"
86
87#define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
88 RSEQ_INJECT_ASM(1) \
89 "la " RSEQ_ASM_TMP_REG_1 ", " __rseq_str(cs_label) "\n" \
90 REG_S RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(rseq_cs) "]\n" \
91 __rseq_str(label) ":\n"
92
93#define RSEQ_ASM_DEFINE_ABORT(label, abort_label) \
94 "j 222f\n" \
95 ".balign 4\n" \
96 ".long " __rseq_str(RSEQ_SIG) "\n" \
97 __rseq_str(label) ":\n" \
98 "j %l[" __rseq_str(abort_label) "]\n" \
99 "222:\n"
100
101#define RSEQ_ASM_OP_STORE(value, var) \
102 REG_S "%[" __rseq_str(value) "], %[" __rseq_str(var) "]\n"
103
104#define RSEQ_ASM_OP_CMPEQ(var, expect, label) \
105 REG_L RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
106 "bne " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(expect) "] ," \
107 __rseq_str(label) "\n"
108
109#define RSEQ_ASM_OP_CMPEQ32(var, expect, label) \
110 "lw " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
111 "bne " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(expect) "] ," \
112 __rseq_str(label) "\n"
113
114#define RSEQ_ASM_OP_CMPNE(var, expect, label) \
115 REG_L RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
116 "beq " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(expect) "] ," \
117 __rseq_str(label) "\n"
118
119#define RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, label) \
120 RSEQ_INJECT_ASM(2) \
121 RSEQ_ASM_OP_CMPEQ32(current_cpu_id, cpu_id, label)
122
123#define RSEQ_ASM_OP_R_LOAD(var) \
124 REG_L RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n"
125
126#define RSEQ_ASM_OP_R_STORE(var) \
127 REG_S RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n"
128
129#define RSEQ_ASM_OP_R_LOAD_OFF(offset) \
130 "add " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(offset) "], " \
131 RSEQ_ASM_TMP_REG_1 "\n" \
132 REG_L RSEQ_ASM_TMP_REG_1 ", (" RSEQ_ASM_TMP_REG_1 ")\n"
133
134#define RSEQ_ASM_OP_R_ADD(count) \
135 "add " RSEQ_ASM_TMP_REG_1 ", " RSEQ_ASM_TMP_REG_1 \
136 ", %[" __rseq_str(count) "]\n"
137
138#define RSEQ_ASM_OP_FINAL_STORE(value, var, post_commit_label) \
139 RSEQ_ASM_OP_STORE(value, var) \
140 __rseq_str(post_commit_label) ":\n"
141
142#define RSEQ_ASM_OP_FINAL_STORE_RELEASE(value, var, post_commit_label) \
143 "fence rw, w\n" \
144 RSEQ_ASM_OP_STORE(value, var) \
145 __rseq_str(post_commit_label) ":\n"
146
147#define RSEQ_ASM_OP_R_FINAL_STORE(var, post_commit_label) \
148 REG_S RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(var) "]\n" \
149 __rseq_str(post_commit_label) ":\n"
150
151#define RSEQ_ASM_OP_R_BAD_MEMCPY(dst, src, len) \
152 "beqz %[" __rseq_str(len) "], 333f\n" \
153 "mv " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(len) "]\n" \
154 "mv " RSEQ_ASM_TMP_REG_2 ", %[" __rseq_str(src) "]\n" \
155 "mv " RSEQ_ASM_TMP_REG_3 ", %[" __rseq_str(dst) "]\n" \
156 "222:\n" \
157 "lb " RSEQ_ASM_TMP_REG_4 ", 0(" RSEQ_ASM_TMP_REG_2 ")\n" \
158 "sb " RSEQ_ASM_TMP_REG_4 ", 0(" RSEQ_ASM_TMP_REG_3 ")\n" \
159 "addi " RSEQ_ASM_TMP_REG_1 ", " RSEQ_ASM_TMP_REG_1 ", -1\n" \
160 "addi " RSEQ_ASM_TMP_REG_2 ", " RSEQ_ASM_TMP_REG_2 ", 1\n" \
161 "addi " RSEQ_ASM_TMP_REG_3 ", " RSEQ_ASM_TMP_REG_3 ", 1\n" \
162 "bnez " RSEQ_ASM_TMP_REG_1 ", 222b\n" \
163 "333:\n"
164
165#define RSEQ_ASM_OP_R_DEREF_ADDV(ptr, off, post_commit_label) \
166 "mv " RSEQ_ASM_TMP_REG_1 ", %[" __rseq_str(ptr) "]\n" \
167 RSEQ_ASM_OP_R_ADD(off) \
168 REG_L RSEQ_ASM_TMP_REG_1 ", 0(" RSEQ_ASM_TMP_REG_1 ")\n" \
169 RSEQ_ASM_OP_R_ADD(inc) \
170 __rseq_str(post_commit_label) ":\n"
171
172static inline __attribute__((always_inline))
173int rseq_cmpeqv_storev(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)
174{
175 RSEQ_INJECT_C(9)
176
177 __asm__ __volatile__ goto(RSEQ_ASM_DEFINE_TABLE(1, 2f, 3f, 4f)
178 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[cmpfail]")
179#ifdef RSEQ_COMPARE_TWICE
180 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error1]")
181 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error2]")
182#endif
183 RSEQ_ASM_STORE_RSEQ_CS(2, 1b, rseq_cs)
184 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
185 RSEQ_INJECT_ASM(3)
186 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[cmpfail]")
187 RSEQ_INJECT_ASM(4)
188#ifdef RSEQ_COMPARE_TWICE
189 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, "%l[error1]")
190 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[error2]")
191#endif
192 RSEQ_ASM_OP_FINAL_STORE(newv, v, 3)
193 RSEQ_INJECT_ASM(5)
194 RSEQ_ASM_DEFINE_ABORT(4, abort)
195 : /* gcc asm goto does not allow outputs */
196 : [cpu_id] "r" (cpu),
197 [current_cpu_id] "m" (rseq_get_abi()->cpu_id),
198 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
199 [v] "m" (*v),
200 [expect] "r" (expect),
201 [newv] "r" (newv)
202 RSEQ_INJECT_INPUT
203 : "memory", RSEQ_ASM_TMP_REG_1
204 RSEQ_INJECT_CLOBBER
205 : abort, cmpfail
206#ifdef RSEQ_COMPARE_TWICE
207 , error1, error2
208#endif
209 );
210 rseq_after_asm_goto();
211
212 return 0;
213abort:
214 rseq_after_asm_goto();
215 RSEQ_INJECT_FAILED
216 return -1;
217cmpfail:
218 rseq_after_asm_goto();
219 return 1;
220#ifdef RSEQ_COMPARE_TWICE
221error1:
222 rseq_after_asm_goto();
223 rseq_bug("cpu_id comparison failed");
224error2:
225 rseq_after_asm_goto();
226 rseq_bug("expected value comparison failed");
227#endif
228}
229
230static inline __attribute__((always_inline))
231int rseq_cmpnev_storeoffp_load(intptr_t *v, intptr_t expectnot,
232 off_t voffp, intptr_t *load, int cpu)
233{
234 RSEQ_INJECT_C(9)
235
236 __asm__ __volatile__ goto(RSEQ_ASM_DEFINE_TABLE(1, 2f, 3f, 4f)
237 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[cmpfail]")
238#ifdef RSEQ_COMPARE_TWICE
239 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error1]")
240 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error2]")
241#endif
242 RSEQ_ASM_STORE_RSEQ_CS(2, 1b, rseq_cs)
243 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
244 RSEQ_INJECT_ASM(3)
245 RSEQ_ASM_OP_CMPNE(v, expectnot, "%l[cmpfail]")
246 RSEQ_INJECT_ASM(4)
247#ifdef RSEQ_COMPARE_TWICE
248 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, "%l[error1]")
249 RSEQ_ASM_OP_CMPNE(v, expectnot, "%l[error2]")
250#endif
251 RSEQ_ASM_OP_R_LOAD(v)
252 RSEQ_ASM_OP_R_STORE(load)
253 RSEQ_ASM_OP_R_LOAD_OFF(voffp)
254 RSEQ_ASM_OP_R_FINAL_STORE(v, 3)
255 RSEQ_INJECT_ASM(5)
256 RSEQ_ASM_DEFINE_ABORT(4, abort)
257 : /* gcc asm goto does not allow outputs */
258 : [cpu_id] "r" (cpu),
259 [current_cpu_id] "m" (rseq_get_abi()->cpu_id),
260 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
261 [v] "m" (*v),
262 [expectnot] "r" (expectnot),
263 [load] "m" (*load),
264 [voffp] "r" (voffp)
265 RSEQ_INJECT_INPUT
266 : "memory", RSEQ_ASM_TMP_REG_1
267 RSEQ_INJECT_CLOBBER
268 : abort, cmpfail
269#ifdef RSEQ_COMPARE_TWICE
270 , error1, error2
271#endif
272 );
273 rseq_after_asm_goto();
274 return 0;
275abort:
276 rseq_after_asm_goto();
277 RSEQ_INJECT_FAILED
278 return -1;
279cmpfail:
280 rseq_after_asm_goto();
281 return 1;
282#ifdef RSEQ_COMPARE_TWICE
283error1:
284 rseq_after_asm_goto();
285 rseq_bug("cpu_id comparison failed");
286error2:
287 rseq_after_asm_goto();
288 rseq_bug("expected value comparison failed");
289#endif
290}
291
292static inline __attribute__((always_inline))
293int rseq_addv(intptr_t *v, intptr_t count, int cpu)
294{
295 RSEQ_INJECT_C(9)
296
297 __asm__ __volatile__ goto(RSEQ_ASM_DEFINE_TABLE(1, 2f, 3f, 4f)
298#ifdef RSEQ_COMPARE_TWICE
299 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error1]")
300#endif
301 RSEQ_ASM_STORE_RSEQ_CS(2, 1b, rseq_cs)
302 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
303 RSEQ_INJECT_ASM(3)
304#ifdef RSEQ_COMPARE_TWICE
305 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, "%l[error1]")
306#endif
307 RSEQ_ASM_OP_R_LOAD(v)
308 RSEQ_ASM_OP_R_ADD(count)
309 RSEQ_ASM_OP_R_FINAL_STORE(v, 3)
310 RSEQ_INJECT_ASM(4)
311 RSEQ_ASM_DEFINE_ABORT(4, abort)
312 : /* gcc asm goto does not allow outputs */
313 : [cpu_id] "r" (cpu),
314 [current_cpu_id] "m" (rseq_get_abi()->cpu_id),
315 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
316 [v] "m" (*v),
317 [count] "r" (count)
318 RSEQ_INJECT_INPUT
319 : "memory", RSEQ_ASM_TMP_REG_1
320 RSEQ_INJECT_CLOBBER
321 : abort
322#ifdef RSEQ_COMPARE_TWICE
323 , error1
324#endif
325 );
326 rseq_after_asm_goto();
327 return 0;
328abort:
329 rseq_after_asm_goto();
330 RSEQ_INJECT_FAILED
331 return -1;
332#ifdef RSEQ_COMPARE_TWICE
333error1:
334 rseq_after_asm_goto();
335 rseq_bug("cpu_id comparison failed");
336#endif
337}
338
339static inline __attribute__((always_inline))
340int rseq_cmpeqv_trystorev_storev(intptr_t *v, intptr_t expect,
341 intptr_t *v2, intptr_t newv2,
342 intptr_t newv, int cpu)
343{
344 RSEQ_INJECT_C(9)
345
346 __asm__ __volatile__ goto(RSEQ_ASM_DEFINE_TABLE(1, 2f, 3f, 4f)
347 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[cmpfail]")
348#ifdef RSEQ_COMPARE_TWICE
349 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error1]")
350 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error2]")
351#endif
352 RSEQ_ASM_STORE_RSEQ_CS(2, 1b, rseq_cs)
353 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
354 RSEQ_INJECT_ASM(3)
355 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[cmpfail]")
356 RSEQ_INJECT_ASM(4)
357#ifdef RSEQ_COMPARE_TWICE
358 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, "%l[error1]")
359 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[error2]")
360#endif
361 RSEQ_ASM_OP_STORE(newv2, v2)
362 RSEQ_INJECT_ASM(5)
363 RSEQ_ASM_OP_FINAL_STORE(newv, v, 3)
364 RSEQ_INJECT_ASM(6)
365 RSEQ_ASM_DEFINE_ABORT(4, abort)
366 : /* gcc asm goto does not allow outputs */
367 : [cpu_id] "r" (cpu),
368 [current_cpu_id] "m" (rseq_get_abi()->cpu_id),
369 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
370 [expect] "r" (expect),
371 [v] "m" (*v),
372 [newv] "r" (newv),
373 [v2] "m" (*v2),
374 [newv2] "r" (newv2)
375 RSEQ_INJECT_INPUT
376 : "memory", RSEQ_ASM_TMP_REG_1
377 RSEQ_INJECT_CLOBBER
378 : abort, cmpfail
379#ifdef RSEQ_COMPARE_TWICE
380 , error1, error2
381#endif
382 );
383
384 rseq_after_asm_goto();
385 return 0;
386abort:
387 rseq_after_asm_goto();
388 RSEQ_INJECT_FAILED
389 return -1;
390cmpfail:
391 rseq_after_asm_goto();
392 return 1;
393#ifdef RSEQ_COMPARE_TWICE
394error1:
395 rseq_after_asm_goto();
396 rseq_bug("cpu_id comparison failed");
397error2:
398 rseq_after_asm_goto();
399 rseq_bug("expected value comparison failed");
400#endif
401}
402
403static inline __attribute__((always_inline))
404int rseq_cmpeqv_trystorev_storev_release(intptr_t *v, intptr_t expect,
405 intptr_t *v2, intptr_t newv2,
406 intptr_t newv, int cpu)
407{
408 RSEQ_INJECT_C(9)
409
410 __asm__ __volatile__ goto(RSEQ_ASM_DEFINE_TABLE(1, 2f, 3f, 4f)
411 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[cmpfail]")
412#ifdef RSEQ_COMPARE_TWICE
413 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error1]")
414 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error2]")
415#endif
416 RSEQ_ASM_STORE_RSEQ_CS(2, 1b, rseq_cs)
417 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
418 RSEQ_INJECT_ASM(3)
419 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[cmpfail]")
420 RSEQ_INJECT_ASM(4)
421#ifdef RSEQ_COMPARE_TWICE
422 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, "%l[error1]")
423 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[error2]")
424#endif
425 RSEQ_ASM_OP_STORE(newv2, v2)
426 RSEQ_INJECT_ASM(5)
427 RSEQ_ASM_OP_FINAL_STORE_RELEASE(newv, v, 3)
428 RSEQ_INJECT_ASM(6)
429 RSEQ_ASM_DEFINE_ABORT(4, abort)
430 : /* gcc asm goto does not allow outputs */
431 : [cpu_id] "r" (cpu),
432 [current_cpu_id] "m" (rseq_get_abi()->cpu_id),
433 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
434 [expect] "r" (expect),
435 [v] "m" (*v),
436 [newv] "r" (newv),
437 [v2] "m" (*v2),
438 [newv2] "r" (newv2)
439 RSEQ_INJECT_INPUT
440 : "memory", RSEQ_ASM_TMP_REG_1
441 RSEQ_INJECT_CLOBBER
442 : abort, cmpfail
443#ifdef RSEQ_COMPARE_TWICE
444 , error1, error2
445#endif
446 );
447
448 rseq_after_asm_goto();
449 return 0;
450abort:
451 rseq_after_asm_goto();
452 RSEQ_INJECT_FAILED
453 return -1;
454cmpfail:
455 rseq_after_asm_goto();
456 return 1;
457#ifdef RSEQ_COMPARE_TWICE
458error1:
459 rseq_after_asm_goto();
460 rseq_bug("cpu_id comparison failed");
461error2:
462 rseq_after_asm_goto();
463 rseq_bug("expected value comparison failed");
464#endif
465}
466
467static inline __attribute__((always_inline))
468int rseq_cmpeqv_cmpeqv_storev(intptr_t *v, intptr_t expect,
469 intptr_t *v2, intptr_t expect2,
470 intptr_t newv, int cpu)
471{
472 RSEQ_INJECT_C(9)
473
474 __asm__ __volatile__ goto(RSEQ_ASM_DEFINE_TABLE(1, 2f, 3f, 4f)
475 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[cmpfail]")
476#ifdef RSEQ_COMPARE_TWICE
477 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error1]")
478 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error2]")
479 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error3]")
480#endif
481 RSEQ_ASM_STORE_RSEQ_CS(2, 1b, rseq_cs)
482 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
483 RSEQ_INJECT_ASM(3)
484 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[cmpfail]")
485 RSEQ_INJECT_ASM(4)
486 RSEQ_ASM_OP_CMPEQ(v2, expect2, "%l[cmpfail]")
487 RSEQ_INJECT_ASM(5)
488#ifdef RSEQ_COMPARE_TWICE
489 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, "%l[error1]")
490 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[error2]")
491 RSEQ_ASM_OP_CMPEQ(v2, expect2, "%l[error3]")
492#endif
493 RSEQ_ASM_OP_FINAL_STORE(newv, v, 3)
494 RSEQ_INJECT_ASM(6)
495 RSEQ_ASM_DEFINE_ABORT(4, abort)
496 : /* gcc asm goto does not allow outputs */
497 : [cpu_id] "r" (cpu),
498 [current_cpu_id] "m" (rseq_get_abi()->cpu_id),
499 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
500 [v] "m" (*v),
501 [expect] "r" (expect),
502 [v2] "m" (*v2),
503 [expect2] "r" (expect2),
504 [newv] "r" (newv)
505 RSEQ_INJECT_INPUT
506 : "memory", RSEQ_ASM_TMP_REG_1
507 RSEQ_INJECT_CLOBBER
508 : abort, cmpfail
509#ifdef RSEQ_COMPARE_TWICE
510 , error1, error2, error3
511#endif
512 );
513
514 rseq_after_asm_goto();
515 return 0;
516abort:
517 rseq_after_asm_goto();
518 RSEQ_INJECT_FAILED
519 return -1;
520cmpfail:
521 rseq_after_asm_goto();
522 return 1;
523#ifdef RSEQ_COMPARE_TWICE
524error1:
525 rseq_after_asm_goto();
526 rseq_bug("cpu_id comparison failed");
527error2:
528 rseq_after_asm_goto();
529 rseq_bug("expected value comparison failed");
530error3:
531 rseq_after_asm_goto();
532 rseq_bug("2nd expected value comparison failed");
533#endif
534}
535
536static inline __attribute__((always_inline))
537int rseq_cmpeqv_trymemcpy_storev(intptr_t *v, intptr_t expect,
538 void *dst, void *src, size_t len,
539 intptr_t newv, int cpu)
540{
541 RSEQ_INJECT_C(9)
542 __asm__ __volatile__ goto(RSEQ_ASM_DEFINE_TABLE(1, 2f, 3f, 4f)
543 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[cmpfail]")
544#ifdef RSEQ_COMPARE_TWICE
545 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error1]")
546 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error2]")
547#endif
548 RSEQ_ASM_STORE_RSEQ_CS(2, 1b, rseq_cs)
549 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
550 RSEQ_INJECT_ASM(3)
551 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[cmpfail]")
552 RSEQ_INJECT_ASM(4)
553#ifdef RSEQ_COMPARE_TWICE
554 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, "%l[error1]")
555 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[error2]")
556#endif
557 RSEQ_ASM_OP_R_BAD_MEMCPY(dst, src, len)
558 RSEQ_INJECT_ASM(5)
559 RSEQ_ASM_OP_FINAL_STORE(newv, v, 3)
560 RSEQ_INJECT_ASM(6)
561 RSEQ_ASM_DEFINE_ABORT(4, abort)
562 : /* gcc asm goto does not allow outputs */
563 : [cpu_id] "r" (cpu),
564 [current_cpu_id] "m" (rseq_get_abi()->cpu_id),
565 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
566 [expect] "r" (expect),
567 [v] "m" (*v),
568 [newv] "r" (newv),
569 [dst] "r" (dst),
570 [src] "r" (src),
571 [len] "r" (len)
572 RSEQ_INJECT_INPUT
573 : "memory", RSEQ_ASM_TMP_REG_1, RSEQ_ASM_TMP_REG_2,
574 RSEQ_ASM_TMP_REG_3, RSEQ_ASM_TMP_REG_4
575 RSEQ_INJECT_CLOBBER
576 : abort, cmpfail
577#ifdef RSEQ_COMPARE_TWICE
578 , error1, error2
579#endif
580 );
581
582 rseq_after_asm_goto();
583 return 0;
584abort:
585 rseq_after_asm_goto();
586 RSEQ_INJECT_FAILED
587 return -1;
588cmpfail:
589 rseq_after_asm_goto();
590 return 1;
591#ifdef RSEQ_COMPARE_TWICE
592error1:
593 rseq_after_asm_goto();
594 rseq_bug("cpu_id comparison failed");
595error2:
596 rseq_after_asm_goto();
597 rseq_bug("expected value comparison failed");
598#endif
599}
600
601static inline __attribute__((always_inline))
602int rseq_cmpeqv_trymemcpy_storev_release(intptr_t *v, intptr_t expect,
603 void *dst, void *src, size_t len,
604 intptr_t newv, int cpu)
605{
606 RSEQ_INJECT_C(9)
607
608 __asm__ __volatile__ goto(RSEQ_ASM_DEFINE_TABLE(1, 2f, 3f, 4f)
609 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[cmpfail]")
610#ifdef RSEQ_COMPARE_TWICE
611 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error1]")
612 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error2]")
613#endif
614 RSEQ_ASM_STORE_RSEQ_CS(2, 1b, rseq_cs)
615 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
616 RSEQ_INJECT_ASM(3)
617 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[cmpfail]")
618 RSEQ_INJECT_ASM(4)
619#ifdef RSEQ_COMPARE_TWICE
620 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, "%l[error1]")
621 RSEQ_ASM_OP_CMPEQ(v, expect, "%l[error2]")
622#endif
623 RSEQ_ASM_OP_R_BAD_MEMCPY(dst, src, len)
624 RSEQ_INJECT_ASM(5)
625 RSEQ_ASM_OP_FINAL_STORE_RELEASE(newv, v, 3)
626 RSEQ_INJECT_ASM(6)
627 RSEQ_ASM_DEFINE_ABORT(4, abort)
628 : /* gcc asm goto does not allow outputs */
629 : [cpu_id] "r" (cpu),
630 [current_cpu_id] "m" (rseq_get_abi()->cpu_id),
631 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
632 [expect] "r" (expect),
633 [v] "m" (*v),
634 [newv] "r" (newv),
635 [dst] "r" (dst),
636 [src] "r" (src),
637 [len] "r" (len)
638 RSEQ_INJECT_INPUT
639 : "memory", RSEQ_ASM_TMP_REG_1, RSEQ_ASM_TMP_REG_2,
640 RSEQ_ASM_TMP_REG_3, RSEQ_ASM_TMP_REG_4
641 RSEQ_INJECT_CLOBBER
642 : abort, cmpfail
643#ifdef RSEQ_COMPARE_TWICE
644 , error1, error2
645#endif
646 );
647
648 rseq_after_asm_goto();
649 return 0;
650abort:
651 rseq_after_asm_goto();
652 RSEQ_INJECT_FAILED
653 return -1;
654cmpfail:
655 rseq_after_asm_goto();
656 return 1;
657#ifdef RSEQ_COMPARE_TWICE
658error1:
659 rseq_after_asm_goto();
660 rseq_bug("cpu_id comparison failed");
661error2:
662 rseq_after_asm_goto();
663 rseq_bug("expected value comparison failed");
664#endif
665}
666
667#define RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
668
669/*
670 * pval = *(ptr+off)
671 * *pval += inc;
672 */
673static inline __attribute__((always_inline))
674int rseq_offset_deref_addv(intptr_t *ptr, off_t off, intptr_t inc, int cpu)
675{
676 RSEQ_INJECT_C(9)
677
678 __asm__ __volatile__ goto(RSEQ_ASM_DEFINE_TABLE(1, 2f, 3f, 4f)
679#ifdef RSEQ_COMPARE_TWICE
680 RSEQ_ASM_DEFINE_EXIT_POINT(2f, "%l[error1]")
681#endif
682 RSEQ_ASM_STORE_RSEQ_CS(2, 1b, rseq_cs)
683 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
684 RSEQ_INJECT_ASM(3)
685#ifdef RSEQ_COMPARE_TWICE
686 RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, "%l[error1]")
687#endif
688 RSEQ_ASM_OP_R_DEREF_ADDV(ptr, off, 3)
689 RSEQ_INJECT_ASM(4)
690 RSEQ_ASM_DEFINE_ABORT(4, abort)
691 : /* gcc asm goto does not allow outputs */
692 : [cpu_id] "r" (cpu),
693 [current_cpu_id] "m" (rseq_get_abi()->cpu_id),
694 [rseq_cs] "m" (rseq_get_abi()->rseq_cs.arch.ptr),
695 [ptr] "r" (ptr),
696 [off] "er" (off),
697 [inc] "er" (inc)
698 RSEQ_INJECT_INPUT
699 : "memory", RSEQ_ASM_TMP_REG_1
700 RSEQ_INJECT_CLOBBER
701 : abort
702#ifdef RSEQ_COMPARE_TWICE
703 , error1
704#endif
705 );
706 rseq_after_asm_goto();
707 return 0;
708abort:
709 rseq_after_asm_goto();
710 RSEQ_INJECT_FAILED
711 return -1;
712#ifdef RSEQ_COMPARE_TWICE
713error1:
714 rseq_after_asm_goto();
715 rseq_bug("cpu_id comparison failed");
716#endif
717}
718
719#endif /* !RSEQ_SKIP_FASTPATH */
This page took 0.049558 seconds and 4 git commands to generate.