x86: Template memory ordering and percpu access mode
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 25 Apr 2023 15:43:18 +0000 (11:43 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 25 Apr 2023 22:38:09 +0000 (18:38 -0400)
Introduce a rseq-x86-bits.h template header which is internally included
to generate the static inline functions covering:

- relaxed and release memory ordering,
- per-cpu-id and per-mm-cid per-cpu data access.

This introduces changes to the rseq.h selftests API which require to
update the rseq selftest programs. Similar API/templating changes need
to be done for other architectures.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: If6889ccebedda2f9bc3b1abe7faad1ba626473c3

include/rseq/compiler.h
include/rseq/rseq-bits-reset.h [new file with mode: 0644]
include/rseq/rseq-bits-template.h [new file with mode: 0644]
include/rseq/rseq-x86-bits.h [new file with mode: 0644]
include/rseq/rseq-x86.h
include/rseq/rseq.h

index c1be323d0e5482122ceda9d683fb372f326d74d2..1d86a9920c5ec74bf2f6f207264195d12b2d268c 100644 (file)
  */
 #define rseq_after_asm_goto()  __asm__ __volatile__ ("" : : : "memory")
 
+/* Combine two tokens. */
+#define RSEQ__COMBINE_TOKENS(_tokena, _tokenb) \
+       _tokena##_tokenb
+#define RSEQ_COMBINE_TOKENS(_tokena, _tokenb)  \
+       RSEQ__COMBINE_TOKENS(_tokena, _tokenb)
+
 #if defined(__SIZEOF_LONG__)
 #define RSEQ_BITS_PER_LONG     (__SIZEOF_LONG__ * 8)
 #elif defined(_LP64)
diff --git a/include/rseq/rseq-bits-reset.h b/include/rseq/rseq-bits-reset.h
new file mode 100644 (file)
index 0000000..1a45c2d
--- /dev/null
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * rseq-bits-reset.h
+ *
+ * (C) Copyright 2016-2022 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#undef RSEQ_TEMPLATE_IDENTIFIER
+#undef RSEQ_TEMPLATE_CPU_ID_FIELD
+#undef RSEQ_TEMPLATE_CPU_ID_OFFSET
+#undef RSEQ_TEMPLATE_SUFFIX
diff --git a/include/rseq/rseq-bits-template.h b/include/rseq/rseq-bits-template.h
new file mode 100644 (file)
index 0000000..d65c611
--- /dev/null
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * rseq-bits-template.h
+ *
+ * (C) Copyright 2016-2022 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#ifdef RSEQ_TEMPLATE_CPU_ID
+# define RSEQ_TEMPLATE_CPU_ID_OFFSET   RSEQ_CPU_ID_OFFSET
+# define RSEQ_TEMPLATE_CPU_ID_FIELD    cpu_id
+# ifdef RSEQ_TEMPLATE_MO_RELEASE
+#  define RSEQ_TEMPLATE_SUFFIX         _release_cpu_id
+# elif defined (RSEQ_TEMPLATE_MO_RELAXED)
+#  define RSEQ_TEMPLATE_SUFFIX         _relaxed_cpu_id
+# else
+#  error "Never use <rseq-bits-template.h> directly; include <rseq.h> instead."
+# endif
+#elif defined(RSEQ_TEMPLATE_MM_CID)
+# define RSEQ_TEMPLATE_CPU_ID_OFFSET   RSEQ_MM_CID_OFFSET
+# define RSEQ_TEMPLATE_CPU_ID_FIELD    mm_cid
+# ifdef RSEQ_TEMPLATE_MO_RELEASE
+#  define RSEQ_TEMPLATE_SUFFIX         _release_mm_cid
+# elif defined (RSEQ_TEMPLATE_MO_RELAXED)
+#  define RSEQ_TEMPLATE_SUFFIX         _relaxed_mm_cid
+# else
+#  error "Never use <rseq-bits-template.h> directly; include <rseq.h> instead."
+# endif
+#elif defined (RSEQ_TEMPLATE_CPU_ID_NONE)
+# ifdef RSEQ_TEMPLATE_MO_RELEASE
+#  define RSEQ_TEMPLATE_SUFFIX         _release
+# elif defined (RSEQ_TEMPLATE_MO_RELAXED)
+#  define RSEQ_TEMPLATE_SUFFIX         _relaxed
+# else
+#  error "Never use <rseq-bits-template.h> directly; include <rseq.h> instead."
+# endif
+#else
+# error "Never use <rseq-bits-template.h> directly; include <rseq.h> instead."
+#endif
+
+#define RSEQ_TEMPLATE_IDENTIFIER(x)    RSEQ_COMBINE_TOKENS(x, RSEQ_TEMPLATE_SUFFIX)
+
diff --git a/include/rseq/rseq-x86-bits.h b/include/rseq/rseq-x86-bits.h
new file mode 100644 (file)
index 0000000..8ee71cb
--- /dev/null
@@ -0,0 +1,992 @@
+/* SPDX-License-Identifier: MIT */
+/* SPDX-FileCopyrightText: 2016-2018 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> */
+/*
+ * rseq-x86-bits.h
+ */
+
+#include "rseq-bits-template.h"
+
+#ifdef __x86_64__
+
+#if defined(RSEQ_TEMPLATE_MO_RELAXED) && \
+       (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID))
+
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_storev)(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "cmpq %[v], %[expect]\n\t"
+               "jnz %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(4)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+               "cmpq %[v], %[expect]\n\t"
+               "jnz %l[error2]\n\t"
+#endif
+               /* final store */
+               "movq %[newv], %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(5)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 [v]                   "m" (*v),
+                 [expect]              "r" (expect),
+                 [newv]                "r" (newv)
+               : "memory", "cc", "rax"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("expected value comparison failed");
+#endif
+}
+
+/*
+ * Compare @v against @expectnot. When it does _not_ match, load @v
+ * into @load, and store the content of *@v + voffp into @v.
+ */
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpnev_storeoffp_load)(intptr_t *v, intptr_t expectnot,
+                              long voffp, intptr_t *load, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "movq %[v], %%rbx\n\t"
+               "cmpq %%rbx, %[expectnot]\n\t"
+               "je %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(4)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+               "movq %[v], %%rbx\n\t"
+               "cmpq %%rbx, %[expectnot]\n\t"
+               "je %l[error2]\n\t"
+#endif
+               "movq %%rbx, %[load]\n\t"
+               "addq %[voffp], %%rbx\n\t"
+               "movq (%%rbx), %%rbx\n\t"
+               /* final store */
+               "movq %%rbx, %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(5)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [expectnot]           "r" (expectnot),
+                 [voffp]               "er" (voffp),
+                 [load]                "m" (*load)
+               : "memory", "cc", "rax", "rbx"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("expected value comparison failed");
+#endif
+}
+
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_addv)(intptr_t *v, intptr_t count, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+#endif
+               /* final store */
+               "addq %[count], %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(4)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [count]               "er" (count)
+               : "memory", "cc", "rax"
+                 RSEQ_INJECT_CLOBBER
+               : abort
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+#endif
+}
+
+#define RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
+
+/*
+ *   pval = *(ptr+off)
+ *  *pval += inc;
+ */
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_offset_deref_addv)(intptr_t *ptr, long off, intptr_t inc, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+#endif
+               /* get p+v */
+               "movq %[ptr], %%rbx\n\t"
+               "addq %[off], %%rbx\n\t"
+               /* get pv */
+               "movq (%%rbx), %%rcx\n\t"
+               /* *pv += inc */
+               "addq %[inc], (%%rcx)\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(4)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* final store input */
+                 [ptr]                 "m" (*ptr),
+                 [off]                 "er" (off),
+                 [inc]                 "er" (inc)
+               : "memory", "cc", "rax", "rbx", "rcx"
+                 RSEQ_INJECT_CLOBBER
+               : abort
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1
+#endif
+       );
+       return 0;
+abort:
+       RSEQ_INJECT_FAILED
+       return -1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_bug("cpu_id comparison failed");
+#endif
+}
+
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_cmpeqv_storev)(intptr_t *v, intptr_t expect,
+                             intptr_t *v2, intptr_t expect2,
+                             intptr_t newv, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error3])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "cmpq %[v], %[expect]\n\t"
+               "jnz %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(4)
+               "cmpq %[v2], %[expect2]\n\t"
+               "jnz %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(5)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+               "cmpq %[v], %[expect]\n\t"
+               "jnz %l[error2]\n\t"
+               "cmpq %[v2], %[expect2]\n\t"
+               "jnz %l[error3]\n\t"
+#endif
+               /* final store */
+               "movq %[newv], %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(6)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* cmp2 input */
+                 [v2]                  "m" (*v2),
+                 [expect2]             "r" (expect2),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [expect]              "r" (expect),
+                 [newv]                "r" (newv)
+               : "memory", "cc", "rax"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2, error3
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("1st expected value comparison failed");
+error3:
+       rseq_after_asm_goto();
+       rseq_bug("2nd expected value comparison failed");
+#endif
+}
+
+#endif /* #if defined(RSEQ_TEMPLATE_MO_RELAXED) &&
+       (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID)) */
+
+#if (defined(RSEQ_TEMPLATE_MO_RELAXED) || defined(RSEQ_TEMPLATE_MO_RELEASE)) && \
+       (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID))
+
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_trystorev_storev)(intptr_t *v, intptr_t expect,
+                                intptr_t *v2, intptr_t newv2,
+                                intptr_t newv, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "cmpq %[v], %[expect]\n\t"
+               "jnz %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(4)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+               "cmpq %[v], %[expect]\n\t"
+               "jnz %l[error2]\n\t"
+#endif
+               /* try store */
+               "movq %[newv2], %[v2]\n\t"
+               RSEQ_INJECT_ASM(5)
+               /* final store */
+               "movq %[newv], %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(6)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* try store input */
+                 [v2]                  "m" (*v2),
+                 [newv2]               "r" (newv2),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [expect]              "r" (expect),
+                 [newv]                "r" (newv)
+               : "memory", "cc", "rax"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("expected value comparison failed");
+#endif
+}
+
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_trymemcpy_storev)(intptr_t *v, intptr_t expect,
+                                void *dst, void *src, size_t len,
+                                intptr_t newv, int cpu)
+{
+       uint64_t rseq_scratch[3];
+
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+#endif
+               "movq %[src], %[rseq_scratch0]\n\t"
+               "movq %[dst], %[rseq_scratch1]\n\t"
+               "movq %[len], %[rseq_scratch2]\n\t"
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "cmpq %[v], %[expect]\n\t"
+               "jnz 5f\n\t"
+               RSEQ_INJECT_ASM(4)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 6f)
+               "cmpq %[v], %[expect]\n\t"
+               "jnz 7f\n\t"
+#endif
+               /* try memcpy */
+               "test %[len], %[len]\n\t" \
+               "jz 333f\n\t" \
+               "222:\n\t" \
+               "movb (%[src]), %%al\n\t" \
+               "movb %%al, (%[dst])\n\t" \
+               "inc %[src]\n\t" \
+               "inc %[dst]\n\t" \
+               "dec %[len]\n\t" \
+               "jnz 222b\n\t" \
+               "333:\n\t" \
+               RSEQ_INJECT_ASM(5)
+               /* final store */
+               "movq %[newv], %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(6)
+               /* teardown */
+               "movq %[rseq_scratch2], %[len]\n\t"
+               "movq %[rseq_scratch1], %[dst]\n\t"
+               "movq %[rseq_scratch0], %[src]\n\t"
+               RSEQ_ASM_DEFINE_ABORT(4,
+                       "movq %[rseq_scratch2], %[len]\n\t"
+                       "movq %[rseq_scratch1], %[dst]\n\t"
+                       "movq %[rseq_scratch0], %[src]\n\t",
+                       abort)
+               RSEQ_ASM_DEFINE_CMPFAIL(5,
+                       "movq %[rseq_scratch2], %[len]\n\t"
+                       "movq %[rseq_scratch1], %[dst]\n\t"
+                       "movq %[rseq_scratch0], %[src]\n\t",
+                       cmpfail)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_CMPFAIL(6,
+                       "movq %[rseq_scratch2], %[len]\n\t"
+                       "movq %[rseq_scratch1], %[dst]\n\t"
+                       "movq %[rseq_scratch0], %[src]\n\t",
+                       error1)
+               RSEQ_ASM_DEFINE_CMPFAIL(7,
+                       "movq %[rseq_scratch2], %[len]\n\t"
+                       "movq %[rseq_scratch1], %[dst]\n\t"
+                       "movq %[rseq_scratch0], %[src]\n\t",
+                       error2)
+#endif
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [expect]              "r" (expect),
+                 [newv]                "r" (newv),
+                 /* try memcpy input */
+                 [dst]                 "r" (dst),
+                 [src]                 "r" (src),
+                 [len]                 "r" (len),
+                 [rseq_scratch0]       "m" (rseq_scratch[0]),
+                 [rseq_scratch1]       "m" (rseq_scratch[1]),
+                 [rseq_scratch2]       "m" (rseq_scratch[2])
+               : "memory", "cc", "rax"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("expected value comparison failed");
+#endif
+}
+
+#endif /* #if (defined(RSEQ_TEMPLATE_MO_RELAXED) || defined(RSEQ_TEMPLATE_MO_RELEASE)) &&
+       (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID)) */
+
+#elif defined(__i386__)
+
+#if defined(RSEQ_TEMPLATE_MO_RELAXED) && \
+       (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID))
+
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_storev)(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "cmpl %[v], %[expect]\n\t"
+               "jnz %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(4)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+               "cmpl %[v], %[expect]\n\t"
+               "jnz %l[error2]\n\t"
+#endif
+               /* final store */
+               "movl %[newv], %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(5)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 [v]                   "m" (*v),
+                 [expect]              "r" (expect),
+                 [newv]                "r" (newv)
+               : "memory", "cc", "eax"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("expected value comparison failed");
+#endif
+}
+
+/*
+ * Compare @v against @expectnot. When it does _not_ match, load @v
+ * into @load, and store the content of *@v + voffp into @v.
+ */
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpnev_storeoffp_load)(intptr_t *v, intptr_t expectnot,
+                              long voffp, intptr_t *load, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "movl %[v], %%ebx\n\t"
+               "cmpl %%ebx, %[expectnot]\n\t"
+               "je %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(4)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+               "movl %[v], %%ebx\n\t"
+               "cmpl %%ebx, %[expectnot]\n\t"
+               "je %l[error2]\n\t"
+#endif
+               "movl %%ebx, %[load]\n\t"
+               "addl %[voffp], %%ebx\n\t"
+               "movl (%%ebx), %%ebx\n\t"
+               /* final store */
+               "movl %%ebx, %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(5)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [expectnot]           "r" (expectnot),
+                 [voffp]               "ir" (voffp),
+                 [load]                "m" (*load)
+               : "memory", "cc", "eax", "ebx"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("expected value comparison failed");
+#endif
+}
+
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_addv)(intptr_t *v, intptr_t count, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+#endif
+               /* final store */
+               "addl %[count], %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(4)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [count]               "ir" (count)
+               : "memory", "cc", "eax"
+                 RSEQ_INJECT_CLOBBER
+               : abort
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+#endif
+}
+
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_cmpeqv_storev)(intptr_t *v, intptr_t expect,
+                             intptr_t *v2, intptr_t expect2,
+                             intptr_t newv, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error3])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "cmpl %[v], %[expect]\n\t"
+               "jnz %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(4)
+               "cmpl %[expect2], %[v2]\n\t"
+               "jnz %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(5)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+               "cmpl %[v], %[expect]\n\t"
+               "jnz %l[error2]\n\t"
+               "cmpl %[expect2], %[v2]\n\t"
+               "jnz %l[error3]\n\t"
+#endif
+               "movl %[newv], %%eax\n\t"
+               /* final store */
+               "movl %%eax, %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(6)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* cmp2 input */
+                 [v2]                  "m" (*v2),
+                 [expect2]             "r" (expect2),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [expect]              "r" (expect),
+                 [newv]                "m" (newv)
+               : "memory", "cc", "eax"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2, error3
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("1st expected value comparison failed");
+error3:
+       rseq_after_asm_goto();
+       rseq_bug("2nd expected value comparison failed");
+#endif
+}
+
+#endif /* #if defined(RSEQ_TEMPLATE_MO_RELAXED) &&
+       (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID)) */
+
+#if (defined(RSEQ_TEMPLATE_MO_RELAXED) || defined(RSEQ_TEMPLATE_MO_RELEASE)) && \
+       (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID))
+
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_trystorev_storev)(intptr_t *v, intptr_t expect,
+                                        intptr_t *v2, intptr_t newv2,
+                                        intptr_t newv, int cpu)
+{
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+#endif
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "movl %[expect], %%eax\n\t"
+               "cmpl %[v], %%eax\n\t"
+               "jnz %l[cmpfail]\n\t"
+               RSEQ_INJECT_ASM(4)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
+               "movl %[expect], %%eax\n\t"
+               "cmpl %[v], %%eax\n\t"
+               "jnz %l[error2]\n\t"
+#endif
+               /* try store */
+               "movl %[newv2], %[v2]\n\t"
+               RSEQ_INJECT_ASM(5)
+#ifdef RSEQ_TEMPLATE_MO_RELEASE
+               "lock; addl $0,-128(%%esp)\n\t"
+#endif
+               /* final store */
+               "movl %[newv], %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(6)
+               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* try store input */
+                 [v2]                  "m" (*v2),
+                 [newv2]               "r" (newv2),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [expect]              "m" (expect),
+                 [newv]                "r" (newv)
+               : "memory", "cc", "eax"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("expected value comparison failed");
+#endif
+
+}
+
+/* TODO: implement a faster memcpy. */
+static inline __attribute__((always_inline))
+int RSEQ_TEMPLATE_IDENTIFIER(rseq_cmpeqv_trymemcpy_storev)(intptr_t *v, intptr_t expect,
+                                        void *dst, void *src, size_t len,
+                                        intptr_t newv, int cpu)
+{
+       uint32_t rseq_scratch[3];
+
+       RSEQ_INJECT_C(9)
+
+       __asm__ __volatile__ goto (
+               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
+               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
+#endif
+               "movl %[src], %[rseq_scratch0]\n\t"
+               "movl %[dst], %[rseq_scratch1]\n\t"
+               "movl %[len], %[rseq_scratch2]\n\t"
+               /* Start rseq by storing table entry pointer into rseq_cs. */
+               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 4f)
+               RSEQ_INJECT_ASM(3)
+               "movl %[expect], %%eax\n\t"
+               "cmpl %%eax, %[v]\n\t"
+               "jnz 5f\n\t"
+               RSEQ_INJECT_ASM(4)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_TEMPLATE_CPU_ID_OFFSET(%[rseq_offset]), 6f)
+               "movl %[expect], %%eax\n\t"
+               "cmpl %%eax, %[v]\n\t"
+               "jnz 7f\n\t"
+#endif
+               /* try memcpy */
+               "test %[len], %[len]\n\t" \
+               "jz 333f\n\t" \
+               "222:\n\t" \
+               "movb (%[src]), %%al\n\t" \
+               "movb %%al, (%[dst])\n\t" \
+               "inc %[src]\n\t" \
+               "inc %[dst]\n\t" \
+               "dec %[len]\n\t" \
+               "jnz 222b\n\t" \
+               "333:\n\t" \
+               RSEQ_INJECT_ASM(5)
+#ifdef RSEQ_TEMPLATE_MO_RELEASE
+               "lock; addl $0,-128(%%esp)\n\t"
+#endif
+               "movl %[newv], %%eax\n\t"
+               /* final store */
+               "movl %%eax, %[v]\n\t"
+               "2:\n\t"
+               RSEQ_INJECT_ASM(6)
+               /* teardown */
+               "movl %[rseq_scratch2], %[len]\n\t"
+               "movl %[rseq_scratch1], %[dst]\n\t"
+               "movl %[rseq_scratch0], %[src]\n\t"
+               RSEQ_ASM_DEFINE_ABORT(4,
+                       "movl %[rseq_scratch2], %[len]\n\t"
+                       "movl %[rseq_scratch1], %[dst]\n\t"
+                       "movl %[rseq_scratch0], %[src]\n\t",
+                       abort)
+               RSEQ_ASM_DEFINE_CMPFAIL(5,
+                       "movl %[rseq_scratch2], %[len]\n\t"
+                       "movl %[rseq_scratch1], %[dst]\n\t"
+                       "movl %[rseq_scratch0], %[src]\n\t",
+                       cmpfail)
+#ifdef RSEQ_COMPARE_TWICE
+               RSEQ_ASM_DEFINE_CMPFAIL(6,
+                       "movl %[rseq_scratch2], %[len]\n\t"
+                       "movl %[rseq_scratch1], %[dst]\n\t"
+                       "movl %[rseq_scratch0], %[src]\n\t",
+                       error1)
+               RSEQ_ASM_DEFINE_CMPFAIL(7,
+                       "movl %[rseq_scratch2], %[len]\n\t"
+                       "movl %[rseq_scratch1], %[dst]\n\t"
+                       "movl %[rseq_scratch0], %[src]\n\t",
+                       error2)
+#endif
+               : /* gcc asm goto does not allow outputs */
+               : [cpu_id]              "r" (cpu),
+                 [rseq_offset]         "r" (rseq_offset),
+                 /* final store input */
+                 [v]                   "m" (*v),
+                 [expect]              "m" (expect),
+                 [newv]                "m" (newv),
+                 /* try memcpy input */
+                 [dst]                 "r" (dst),
+                 [src]                 "r" (src),
+                 [len]                 "r" (len),
+                 [rseq_scratch0]       "m" (rseq_scratch[0]),
+                 [rseq_scratch1]       "m" (rseq_scratch[1]),
+                 [rseq_scratch2]       "m" (rseq_scratch[2])
+               : "memory", "cc", "eax"
+                 RSEQ_INJECT_CLOBBER
+               : abort, cmpfail
+#ifdef RSEQ_COMPARE_TWICE
+                 , error1, error2
+#endif
+       );
+       rseq_after_asm_goto();
+       return 0;
+abort:
+       rseq_after_asm_goto();
+       RSEQ_INJECT_FAILED
+       return -1;
+cmpfail:
+       rseq_after_asm_goto();
+       return 1;
+#ifdef RSEQ_COMPARE_TWICE
+error1:
+       rseq_after_asm_goto();
+       rseq_bug("cpu_id comparison failed");
+error2:
+       rseq_after_asm_goto();
+       rseq_bug("expected value comparison failed");
+#endif
+}
+
+#endif /* #if (defined(RSEQ_TEMPLATE_MO_RELAXED) || defined(RSEQ_TEMPLATE_MO_RELEASE)) &&
+       (defined(RSEQ_TEMPLATE_CPU_ID) || defined(RSEQ_TEMPLATE_MM_CID)) */
+
+#endif
+
+#include "rseq-bits-reset.h"
index c28fabbda2bfcf9a2d2bf00c44ee74e080bd09fe..9672cb8b379c98655d8c5800d4f2ef2f169b30bc 100644 (file)
@@ -5,6 +5,10 @@
  * rseq-x86.h
  */
 
+#ifndef RSEQ_H
+#error "Never use <rseq-x86.h> directly; include <rseq.h> instead."
+#endif
+
 #include <stdint.h>
 
 /*
  */
 #define RSEQ_SIG       0x53053053
 
-/* Offset of cpu_id and rseq_cs fields in struct rseq_abi. */
+/*
+ * Due to a compiler optimization bug in gcc-8 with asm goto and TLS asm input
+ * operands, we cannot use "m" input operands, and rather pass the __rseq_abi
+ * address through a "r" input operand.
+ */
+
+/* Offset of cpu_id, rseq_cs, and mm_cid fields in struct rseq. */
 #define RSEQ_CPU_ID_OFFSET     4
 #define RSEQ_CS_OFFSET         8
+#define RSEQ_MM_CID_OFFSET     24
 
 #ifdef __x86_64__
 
@@ -41,7 +52,7 @@ __extension__ ({                                                      \
 #define rseq_smp_store_release(p, v)                                   \
 do {                                                                   \
        rseq_barrier();                                                 \
-       RSEQ_WRITE_ONCE(*(p), v);                                               \
+       RSEQ_WRITE_ONCE(*(p), v);                                       \
 } while (0)
 
 #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags,                 \
@@ -102,582 +113,6 @@ do {                                                                      \
                "jmp %l[" __rseq_str(cmpfail_label) "]\n\t"             \
                ".popsection\n\t"
 
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_storev(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "cmpq %[v], %[expect]\n\t"
-               "jnz %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-               "cmpq %[v], %[expect]\n\t"
-               "jnz %l[error2]\n\t"
-#endif
-               /* final store */
-               "movq %[newv], %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(5)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 [v]                   "m" (*v),
-                 [expect]              "r" (expect),
-                 [newv]                "r" (newv)
-               : "memory", "cc", "rax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-}
-
-/*
- * Compare @v against @expectnot. When it does _not_ match, load @v
- * into @load, and store the content of *@v + voffp into @v.
- */
-static inline __attribute__((always_inline))
-int rseq_cmpnev_storeoffp_load(intptr_t *v, intptr_t expectnot,
-                              long voffp, intptr_t *load, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "movq %[v], %%rbx\n\t"
-               "cmpq %%rbx, %[expectnot]\n\t"
-               "je %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-               "movq %[v], %%rbx\n\t"
-               "cmpq %%rbx, %[expectnot]\n\t"
-               "je %l[error2]\n\t"
-#endif
-               "movq %%rbx, %[load]\n\t"
-               "addq %[voffp], %%rbx\n\t"
-               "movq (%%rbx), %%rbx\n\t"
-               /* final store */
-               "movq %%rbx, %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(5)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expectnot]           "r" (expectnot),
-                 [voffp]               "er" (voffp),
-                 [load]                "m" (*load)
-               : "memory", "cc", "rax", "rbx"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-}
-
-static inline __attribute__((always_inline))
-int rseq_addv(intptr_t *v, intptr_t count, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-#endif
-               /* final store */
-               "addq %[count], %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(4)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [count]               "er" (count)
-               : "memory", "cc", "rax"
-                 RSEQ_INJECT_CLOBBER
-               : abort
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-#endif
-}
-
-#define RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
-
-/*
- *   pval = *(ptr+off)
- *  *pval += inc;
- */
-static inline __attribute__((always_inline))
-int rseq_offset_deref_addv(intptr_t *ptr, long off, intptr_t inc, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-#endif
-               /* get p+v */
-               "movq %[ptr], %%rbx\n\t"
-               "addq %[off], %%rbx\n\t"
-               /* get pv */
-               "movq (%%rbx), %%rcx\n\t"
-               /* *pv += inc */
-               "addq %[inc], (%%rcx)\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(4)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [ptr]                 "m" (*ptr),
-                 [off]                 "er" (off),
-                 [inc]                 "er" (inc)
-               : "memory", "cc", "rax", "rbx", "rcx"
-                 RSEQ_INJECT_CLOBBER
-               : abort
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-#endif
-}
-
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_trystorev_storev(intptr_t *v, intptr_t expect,
-                                intptr_t *v2, intptr_t newv2,
-                                intptr_t newv, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "cmpq %[v], %[expect]\n\t"
-               "jnz %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-               "cmpq %[v], %[expect]\n\t"
-               "jnz %l[error2]\n\t"
-#endif
-               /* try store */
-               "movq %[newv2], %[v2]\n\t"
-               RSEQ_INJECT_ASM(5)
-               /* final store */
-               "movq %[newv], %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(6)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* try store input */
-                 [v2]                  "m" (*v2),
-                 [newv2]               "r" (newv2),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expect]              "r" (expect),
-                 [newv]                "r" (newv)
-               : "memory", "cc", "rax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-}
-
-/* x86-64 is TSO. */
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_trystorev_storev_release(intptr_t *v, intptr_t expect,
-                                        intptr_t *v2, intptr_t newv2,
-                                        intptr_t newv, int cpu)
-{
-       return rseq_cmpeqv_trystorev_storev(v, expect, v2, newv2, newv, cpu);
-}
-
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_cmpeqv_storev(intptr_t *v, intptr_t expect,
-                             intptr_t *v2, intptr_t expect2,
-                             intptr_t newv, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error3])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "cmpq %[v], %[expect]\n\t"
-               "jnz %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(4)
-               "cmpq %[v2], %[expect2]\n\t"
-               "jnz %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(5)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-               "cmpq %[v], %[expect]\n\t"
-               "jnz %l[error2]\n\t"
-               "cmpq %[v2], %[expect2]\n\t"
-               "jnz %l[error3]\n\t"
-#endif
-               /* final store */
-               "movq %[newv], %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(6)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* cmp2 input */
-                 [v2]                  "m" (*v2),
-                 [expect2]             "r" (expect2),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expect]              "r" (expect),
-                 [newv]                "r" (newv)
-               : "memory", "cc", "rax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2, error3
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("1st expected value comparison failed");
-error3:
-       rseq_after_asm_goto();
-       rseq_bug("2nd expected value comparison failed");
-#endif
-}
-
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_trymemcpy_storev(intptr_t *v, intptr_t expect,
-                                void *dst, void *src, size_t len,
-                                intptr_t newv, int cpu)
-{
-       uint64_t rseq_scratch[3];
-
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               "movq %[src], %[rseq_scratch0]\n\t"
-               "movq %[dst], %[rseq_scratch1]\n\t"
-               "movq %[len], %[rseq_scratch2]\n\t"
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "cmpq %[v], %[expect]\n\t"
-               "jnz 5f\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 6f)
-               "cmpq %[v], %[expect]\n\t"
-               "jnz 7f\n\t"
-#endif
-               /* try memcpy */
-               "test %[len], %[len]\n\t" \
-               "jz 333f\n\t" \
-               "222:\n\t" \
-               "movb (%[src]), %%al\n\t" \
-               "movb %%al, (%[dst])\n\t" \
-               "inc %[src]\n\t" \
-               "inc %[dst]\n\t" \
-               "dec %[len]\n\t" \
-               "jnz 222b\n\t" \
-               "333:\n\t" \
-               RSEQ_INJECT_ASM(5)
-               /* final store */
-               "movq %[newv], %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(6)
-               /* teardown */
-               "movq %[rseq_scratch2], %[len]\n\t"
-               "movq %[rseq_scratch1], %[dst]\n\t"
-               "movq %[rseq_scratch0], %[src]\n\t"
-               RSEQ_ASM_DEFINE_ABORT(4,
-                       "movq %[rseq_scratch2], %[len]\n\t"
-                       "movq %[rseq_scratch1], %[dst]\n\t"
-                       "movq %[rseq_scratch0], %[src]\n\t",
-                       abort)
-               RSEQ_ASM_DEFINE_CMPFAIL(5,
-                       "movq %[rseq_scratch2], %[len]\n\t"
-                       "movq %[rseq_scratch1], %[dst]\n\t"
-                       "movq %[rseq_scratch0], %[src]\n\t",
-                       cmpfail)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_CMPFAIL(6,
-                       "movq %[rseq_scratch2], %[len]\n\t"
-                       "movq %[rseq_scratch1], %[dst]\n\t"
-                       "movq %[rseq_scratch0], %[src]\n\t",
-                       error1)
-               RSEQ_ASM_DEFINE_CMPFAIL(7,
-                       "movq %[rseq_scratch2], %[len]\n\t"
-                       "movq %[rseq_scratch1], %[dst]\n\t"
-                       "movq %[rseq_scratch0], %[src]\n\t",
-                       error2)
-#endif
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expect]              "r" (expect),
-                 [newv]                "r" (newv),
-                 /* try memcpy input */
-                 [dst]                 "r" (dst),
-                 [src]                 "r" (src),
-                 [len]                 "r" (len),
-                 [rseq_scratch0]       "m" (rseq_scratch[0]),
-                 [rseq_scratch1]       "m" (rseq_scratch[1]),
-                 [rseq_scratch2]       "m" (rseq_scratch[2])
-               : "memory", "cc", "rax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-}
-
-/* x86-64 is TSO. */
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_trymemcpy_storev_release(intptr_t *v, intptr_t expect,
-                                        void *dst, void *src, size_t len,
-                                        intptr_t newv, int cpu)
-{
-       return rseq_cmpeqv_trymemcpy_storev(v, expect, dst, src, len,
-                                           newv, cpu);
-}
-
-/*
- * Dereference @p. Add voffp to the dereferenced pointer, and load its content
- * into @load.
- */
-static inline __attribute__((always_inline))
-int rseq_deref_loadoffp(intptr_t *p, long voffp, intptr_t *load, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "movq %[p], %%rbx\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-#endif
-               "addq %[voffp], %%rbx\n\t"
-               "movq (%%rbx), %%rbx\n\t"
-               "movq %%rbx, %[load]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(5)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [p]                   "m" (*p),
-                 [voffp]               "er" (voffp),
-                 [load]                "m" (*load)
-               : "memory", "cc", "rax", "rbx"
-                 RSEQ_INJECT_CLOBBER
-               : abort
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-#endif
-}
-
 #elif defined(__i386__)
 
 #define RSEQ_ASM_TP_SEGMENT    %%gs
@@ -701,7 +136,7 @@ __extension__ ({                                                    \
 #define rseq_smp_store_release(p, v)                                   \
 do {                                                                   \
        rseq_smp_mb();                                                  \
-       RSEQ_WRITE_ONCE(*(p), v);                                       \
+       RSEQ_WRITE_ONCE(*p, v);                                         \
 } while (0)
 
 /*
@@ -764,699 +199,36 @@ do {                                                                     \
                "jmp %l[" __rseq_str(cmpfail_label) "]\n\t"             \
                ".popsection\n\t"
 
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_storev(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "cmpl %[v], %[expect]\n\t"
-               "jnz %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-               "cmpl %[v], %[expect]\n\t"
-               "jnz %l[error2]\n\t"
-#endif
-               /* final store */
-               "movl %[newv], %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(5)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 [v]                   "m" (*v),
-                 [expect]              "r" (expect),
-                 [newv]                "r" (newv)
-               : "memory", "cc", "eax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-}
-
-/*
- * Compare @v against @expectnot. When it does _not_ match, load @v
- * into @load, and store the content of *@v + voffp into @v.
- */
-static inline __attribute__((always_inline))
-int rseq_cmpnev_storeoffp_load(intptr_t *v, intptr_t expectnot,
-                              long voffp, intptr_t *load, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "movl %[v], %%ebx\n\t"
-               "cmpl %%ebx, %[expectnot]\n\t"
-               "je %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-               "movl %[v], %%ebx\n\t"
-               "cmpl %%ebx, %[expectnot]\n\t"
-               "je %l[error2]\n\t"
-#endif
-               "movl %%ebx, %[load]\n\t"
-               "addl %[voffp], %%ebx\n\t"
-               "movl (%%ebx), %%ebx\n\t"
-               /* final store */
-               "movl %%ebx, %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(5)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expectnot]           "r" (expectnot),
-                 [voffp]               "ir" (voffp),
-                 [load]                "m" (*load)
-               : "memory", "cc", "eax", "ebx"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-}
-
-static inline __attribute__((always_inline))
-int rseq_addv(intptr_t *v, intptr_t count, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-#endif
-               /* final store */
-               "addl %[count], %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(4)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [count]               "ir" (count)
-               : "memory", "cc", "eax"
-                 RSEQ_INJECT_CLOBBER
-               : abort
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-#endif
-}
-
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_trystorev_storev(intptr_t *v, intptr_t expect,
-                                intptr_t *v2, intptr_t newv2,
-                                intptr_t newv, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "cmpl %[v], %[expect]\n\t"
-               "jnz %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-               "cmpl %[v], %[expect]\n\t"
-               "jnz %l[error2]\n\t"
-#endif
-               /* try store */
-               "movl %[newv2], %%eax\n\t"
-               "movl %%eax, %[v2]\n\t"
-               RSEQ_INJECT_ASM(5)
-               /* final store */
-               "movl %[newv], %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(6)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* try store input */
-                 [v2]                  "m" (*v2),
-                 [newv2]               "m" (newv2),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expect]              "r" (expect),
-                 [newv]                "r" (newv)
-               : "memory", "cc", "eax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-}
-
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_trystorev_storev_release(intptr_t *v, intptr_t expect,
-                                        intptr_t *v2, intptr_t newv2,
-                                        intptr_t newv, int cpu)
-{
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "movl %[expect], %%eax\n\t"
-               "cmpl %[v], %%eax\n\t"
-               "jnz %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-               "movl %[expect], %%eax\n\t"
-               "cmpl %[v], %%eax\n\t"
-               "jnz %l[error2]\n\t"
-#endif
-               /* try store */
-               "movl %[newv2], %[v2]\n\t"
-               RSEQ_INJECT_ASM(5)
-               "lock; addl $0,-128(%%esp)\n\t"
-               /* final store */
-               "movl %[newv], %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(6)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* try store input */
-                 [v2]                  "m" (*v2),
-                 [newv2]               "r" (newv2),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expect]              "m" (expect),
-                 [newv]                "r" (newv)
-               : "memory", "cc", "eax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
 #endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-
-}
 
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_cmpeqv_storev(intptr_t *v, intptr_t expect,
-                             intptr_t *v2, intptr_t expect2,
-                             intptr_t newv, int cpu)
-{
-       RSEQ_INJECT_C(9)
+/* Per-cpu-id indexing. */
 
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error3])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "cmpl %[v], %[expect]\n\t"
-               "jnz %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(4)
-               "cmpl %[expect2], %[v2]\n\t"
-               "jnz %l[cmpfail]\n\t"
-               RSEQ_INJECT_ASM(5)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-               "cmpl %[v], %[expect]\n\t"
-               "jnz %l[error2]\n\t"
-               "cmpl %[expect2], %[v2]\n\t"
-               "jnz %l[error3]\n\t"
-#endif
-               "movl %[newv], %%eax\n\t"
-               /* final store */
-               "movl %%eax, %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(6)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* cmp2 input */
-                 [v2]                  "m" (*v2),
-                 [expect2]             "r" (expect2),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expect]              "r" (expect),
-                 [newv]                "m" (newv)
-               : "memory", "cc", "eax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2, error3
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("1st expected value comparison failed");
-error3:
-       rseq_after_asm_goto();
-       rseq_bug("2nd expected value comparison failed");
-#endif
-}
-
-/* TODO: implement a faster memcpy. */
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_trymemcpy_storev(intptr_t *v, intptr_t expect,
-                                void *dst, void *src, size_t len,
-                                intptr_t newv, int cpu)
-{
-       uint32_t rseq_scratch[3];
-
-       RSEQ_INJECT_C(9)
-
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               "movl %[src], %[rseq_scratch0]\n\t"
-               "movl %[dst], %[rseq_scratch1]\n\t"
-               "movl %[len], %[rseq_scratch2]\n\t"
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "movl %[expect], %%eax\n\t"
-               "cmpl %%eax, %[v]\n\t"
-               "jnz 5f\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 6f)
-               "movl %[expect], %%eax\n\t"
-               "cmpl %%eax, %[v]\n\t"
-               "jnz 7f\n\t"
-#endif
-               /* try memcpy */
-               "test %[len], %[len]\n\t" \
-               "jz 333f\n\t" \
-               "222:\n\t" \
-               "movb (%[src]), %%al\n\t" \
-               "movb %%al, (%[dst])\n\t" \
-               "inc %[src]\n\t" \
-               "inc %[dst]\n\t" \
-               "dec %[len]\n\t" \
-               "jnz 222b\n\t" \
-               "333:\n\t" \
-               RSEQ_INJECT_ASM(5)
-               "movl %[newv], %%eax\n\t"
-               /* final store */
-               "movl %%eax, %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(6)
-               /* teardown */
-               "movl %[rseq_scratch2], %[len]\n\t"
-               "movl %[rseq_scratch1], %[dst]\n\t"
-               "movl %[rseq_scratch0], %[src]\n\t"
-               RSEQ_ASM_DEFINE_ABORT(4,
-                       "movl %[rseq_scratch2], %[len]\n\t"
-                       "movl %[rseq_scratch1], %[dst]\n\t"
-                       "movl %[rseq_scratch0], %[src]\n\t",
-                       abort)
-               RSEQ_ASM_DEFINE_CMPFAIL(5,
-                       "movl %[rseq_scratch2], %[len]\n\t"
-                       "movl %[rseq_scratch1], %[dst]\n\t"
-                       "movl %[rseq_scratch0], %[src]\n\t",
-                       cmpfail)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_CMPFAIL(6,
-                       "movl %[rseq_scratch2], %[len]\n\t"
-                       "movl %[rseq_scratch1], %[dst]\n\t"
-                       "movl %[rseq_scratch0], %[src]\n\t",
-                       error1)
-               RSEQ_ASM_DEFINE_CMPFAIL(7,
-                       "movl %[rseq_scratch2], %[len]\n\t"
-                       "movl %[rseq_scratch1], %[dst]\n\t"
-                       "movl %[rseq_scratch0], %[src]\n\t",
-                       error2)
-#endif
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expect]              "m" (expect),
-                 [newv]                "m" (newv),
-                 /* try memcpy input */
-                 [dst]                 "r" (dst),
-                 [src]                 "r" (src),
-                 [len]                 "r" (len),
-                 [rseq_scratch0]       "m" (rseq_scratch[0]),
-                 [rseq_scratch1]       "m" (rseq_scratch[1]),
-                 [rseq_scratch2]       "m" (rseq_scratch[2])
-               : "memory", "cc", "eax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-}
+#define RSEQ_TEMPLATE_CPU_ID
+#define RSEQ_TEMPLATE_MO_RELAXED
+#include "rseq-x86-bits.h"
+#undef RSEQ_TEMPLATE_MO_RELAXED
 
-/* TODO: implement a faster memcpy. */
-static inline __attribute__((always_inline))
-int rseq_cmpeqv_trymemcpy_storev_release(intptr_t *v, intptr_t expect,
-                                        void *dst, void *src, size_t len,
-                                        intptr_t newv, int cpu)
-{
-       uint32_t rseq_scratch[3];
+#define RSEQ_TEMPLATE_MO_RELEASE
+#include "rseq-x86-bits.h"
+#undef RSEQ_TEMPLATE_MO_RELEASE
+#undef RSEQ_TEMPLATE_CPU_ID
 
-       RSEQ_INJECT_C(9)
+/* Per-mm-cid indexing. */
 
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[cmpfail])
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error2])
-#endif
-               "movl %[src], %[rseq_scratch0]\n\t"
-               "movl %[dst], %[rseq_scratch1]\n\t"
-               "movl %[len], %[rseq_scratch2]\n\t"
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "movl %[expect], %%eax\n\t"
-               "cmpl %%eax, %[v]\n\t"
-               "jnz 5f\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 6f)
-               "movl %[expect], %%eax\n\t"
-               "cmpl %%eax, %[v]\n\t"
-               "jnz 7f\n\t"
-#endif
-               /* try memcpy */
-               "test %[len], %[len]\n\t" \
-               "jz 333f\n\t" \
-               "222:\n\t" \
-               "movb (%[src]), %%al\n\t" \
-               "movb %%al, (%[dst])\n\t" \
-               "inc %[src]\n\t" \
-               "inc %[dst]\n\t" \
-               "dec %[len]\n\t" \
-               "jnz 222b\n\t" \
-               "333:\n\t" \
-               RSEQ_INJECT_ASM(5)
-               "lock; addl $0,-128(%%esp)\n\t"
-               "movl %[newv], %%eax\n\t"
-               /* final store */
-               "movl %%eax, %[v]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(6)
-               /* teardown */
-               "movl %[rseq_scratch2], %[len]\n\t"
-               "movl %[rseq_scratch1], %[dst]\n\t"
-               "movl %[rseq_scratch0], %[src]\n\t"
-               RSEQ_ASM_DEFINE_ABORT(4,
-                       "movl %[rseq_scratch2], %[len]\n\t"
-                       "movl %[rseq_scratch1], %[dst]\n\t"
-                       "movl %[rseq_scratch0], %[src]\n\t",
-                       abort)
-               RSEQ_ASM_DEFINE_CMPFAIL(5,
-                       "movl %[rseq_scratch2], %[len]\n\t"
-                       "movl %[rseq_scratch1], %[dst]\n\t"
-                       "movl %[rseq_scratch0], %[src]\n\t",
-                       cmpfail)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_CMPFAIL(6,
-                       "movl %[rseq_scratch2], %[len]\n\t"
-                       "movl %[rseq_scratch1], %[dst]\n\t"
-                       "movl %[rseq_scratch0], %[src]\n\t",
-                       error1)
-               RSEQ_ASM_DEFINE_CMPFAIL(7,
-                       "movl %[rseq_scratch2], %[len]\n\t"
-                       "movl %[rseq_scratch1], %[dst]\n\t"
-                       "movl %[rseq_scratch0], %[src]\n\t",
-                       error2)
-#endif
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [v]                   "m" (*v),
-                 [expect]              "m" (expect),
-                 [newv]                "m" (newv),
-                 /* try memcpy input */
-                 [dst]                 "r" (dst),
-                 [src]                 "r" (src),
-                 [len]                 "r" (len),
-                 [rseq_scratch0]       "m" (rseq_scratch[0]),
-                 [rseq_scratch1]       "m" (rseq_scratch[1]),
-                 [rseq_scratch2]       "m" (rseq_scratch[2])
-               : "memory", "cc", "eax"
-                 RSEQ_INJECT_CLOBBER
-               : abort, cmpfail
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1, error2
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-cmpfail:
-       rseq_after_asm_goto();
-       return 1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-error2:
-       rseq_after_asm_goto();
-       rseq_bug("expected value comparison failed");
-#endif
-}
+#define RSEQ_TEMPLATE_MM_CID
+#define RSEQ_TEMPLATE_MO_RELAXED
+#include "rseq-x86-bits.h"
+#undef RSEQ_TEMPLATE_MO_RELAXED
 
-/*
- * Dereference @p. Add voffp to the dereferenced pointer, and load its content
- * into @load.
- */
-static inline __attribute__((always_inline))
-int rseq_deref_loadoffp(intptr_t *p, long voffp, intptr_t *load, int cpu)
-{
-       RSEQ_INJECT_C(9)
+#define RSEQ_TEMPLATE_MO_RELEASE
+#include "rseq-x86-bits.h"
+#undef RSEQ_TEMPLATE_MO_RELEASE
+#undef RSEQ_TEMPLATE_MM_CID
 
-       __asm__ __volatile__ goto (
-               RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])
-#endif
-               /* Start rseq by storing table entry pointer into rseq_cs. */
-               RSEQ_ASM_STORE_RSEQ_CS(1, 3b, RSEQ_ASM_TP_SEGMENT:RSEQ_CS_OFFSET(%[rseq_offset]))
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), 4f)
-               RSEQ_INJECT_ASM(3)
-               "movl %[p], %%ebx\n\t"
-               RSEQ_INJECT_ASM(4)
-#ifdef RSEQ_COMPARE_TWICE
-               RSEQ_ASM_CMP_CPU_ID(cpu_id, RSEQ_ASM_TP_SEGMENT:RSEQ_CPU_ID_OFFSET(%[rseq_offset]), %l[error1])
-#endif
-               "addl %[voffp], %%ebx\n\t"
-               "movl (%%ebx), %%ebx\n\t"
-               "movl %%ebx, %[load]\n\t"
-               "2:\n\t"
-               RSEQ_INJECT_ASM(5)
-               RSEQ_ASM_DEFINE_ABORT(4, "", abort)
-               : /* gcc asm goto does not allow outputs */
-               : [cpu_id]              "r" (cpu),
-                 [rseq_offset]         "r" (rseq_offset),
-                 /* final store input */
-                 [p]                   "m" (*p),
-                 [voffp]               "ir" (voffp),
-                 [load]                "m" (*load)
-               : "memory", "cc", "eax", "ebx"
-                 RSEQ_INJECT_CLOBBER
-               : abort
-#ifdef RSEQ_COMPARE_TWICE
-                 , error1
-#endif
-       );
-       rseq_after_asm_goto();
-       return 0;
-abort:
-       rseq_after_asm_goto();
-       RSEQ_INJECT_FAILED
-       return -1;
-#ifdef RSEQ_COMPARE_TWICE
-error1:
-       rseq_after_asm_goto();
-       rseq_bug("cpu_id comparison failed");
-#endif
-}
+/* APIs which are not based on cpu ids. */
 
-#endif
+#define RSEQ_TEMPLATE_CPU_ID_NONE
+#define RSEQ_TEMPLATE_MO_RELAXED
+#include "rseq-x86-bits.h"
+#undef RSEQ_TEMPLATE_MO_RELAXED
+#undef RSEQ_TEMPLATE_CPU_ID_NONE
index 53376aeb6d547d9467c80ce626c73a03ffdd72a6..0612c24ab16c1a57afc7358ddd1230a50acedba4 100644 (file)
@@ -97,6 +97,20 @@ static inline struct rseq_abi *rseq_get_abi(void)
 
 #endif /* RSEQ_GET_ABI_OVERRIDE */
 
+enum rseq_mo {
+       RSEQ_MO_RELAXED = 0,
+       RSEQ_MO_CONSUME = 1,    /* Unused */
+       RSEQ_MO_ACQUIRE = 2,    /* Unused */
+       RSEQ_MO_RELEASE = 3,
+       RSEQ_MO_ACQ_REL = 4,    /* Unused */
+       RSEQ_MO_SEQ_CST = 5,    /* Unused */
+};
+
+enum rseq_percpu_mode {
+       RSEQ_PERCPU_CPU_ID = 0,
+       RSEQ_PERCPU_MM_CID = 1,
+};
+
 #define rseq_likely(x)         __builtin_expect(!!(x), 1)
 #define rseq_unlikely(x)       __builtin_expect(!!(x), 0)
 #define rseq_barrier()         __asm__ __volatile__("" : : : "memory")
@@ -254,6 +268,151 @@ static inline void rseq_prepare_unload(void)
        rseq_clear_rseq_cs();
 }
 
+static inline __attribute__((always_inline))
+int rseq_cmpeqv_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
+                      intptr_t *v, intptr_t expect,
+                      intptr_t newv, int cpu)
+{
+       if (rseq_mo != RSEQ_MO_RELAXED)
+               return -1;
+       switch (percpu_mode) {
+       case RSEQ_PERCPU_CPU_ID:
+               return rseq_cmpeqv_storev_relaxed_cpu_id(v, expect, newv, cpu);
+       case RSEQ_PERCPU_MM_CID:
+               return rseq_cmpeqv_storev_relaxed_mm_cid(v, expect, newv, cpu);
+       }
+       return -1;
+}
+
+/*
+ * Compare @v against @expectnot. When it does _not_ match, load @v
+ * into @load, and store the content of *@v + voffp into @v.
+ */
+static inline __attribute__((always_inline))
+int rseq_cmpnev_storeoffp_load(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
+                              intptr_t *v, intptr_t expectnot, long voffp, intptr_t *load,
+                              int cpu)
+{
+       if (rseq_mo != RSEQ_MO_RELAXED)
+               return -1;
+       switch (percpu_mode) {
+       case RSEQ_PERCPU_CPU_ID:
+               return rseq_cmpnev_storeoffp_load_relaxed_cpu_id(v, expectnot, voffp, load, cpu);
+       case RSEQ_PERCPU_MM_CID:
+               return rseq_cmpnev_storeoffp_load_relaxed_mm_cid(v, expectnot, voffp, load, cpu);
+       }
+       return -1;
+}
+
+static inline __attribute__((always_inline))
+int rseq_addv(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
+             intptr_t *v, intptr_t count, int cpu)
+{
+       if (rseq_mo != RSEQ_MO_RELAXED)
+               return -1;
+       switch (percpu_mode) {
+       case RSEQ_PERCPU_CPU_ID:
+               return rseq_addv_relaxed_cpu_id(v, count, cpu);
+       case RSEQ_PERCPU_MM_CID:
+               return rseq_addv_relaxed_mm_cid(v, count, cpu);
+       }
+       return -1;
+}
+
+#ifdef RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
+/*
+ *   pval = *(ptr+off)
+ *  *pval += inc;
+ */
+static inline __attribute__((always_inline))
+int rseq_offset_deref_addv(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
+                          intptr_t *ptr, long off, intptr_t inc, int cpu)
+{
+       if (rseq_mo != RSEQ_MO_RELAXED)
+               return -1;
+       switch (percpu_mode) {
+       case RSEQ_PERCPU_CPU_ID:
+               return rseq_offset_deref_addv_relaxed_cpu_id(ptr, off, inc, cpu);
+       case RSEQ_PERCPU_MM_CID:
+               return rseq_offset_deref_addv_relaxed_mm_cid(ptr, off, inc, cpu);
+       }
+       return -1;
+}
+#endif
+
+static inline __attribute__((always_inline))
+int rseq_cmpeqv_trystorev_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
+                                intptr_t *v, intptr_t expect,
+                                intptr_t *v2, intptr_t newv2,
+                                intptr_t newv, int cpu)
+{
+       switch (rseq_mo) {
+       case RSEQ_MO_RELAXED:
+               switch (percpu_mode) {
+               case RSEQ_PERCPU_CPU_ID:
+                       return rseq_cmpeqv_trystorev_storev_relaxed_cpu_id(v, expect, v2, newv2, newv, cpu);
+               case RSEQ_PERCPU_MM_CID:
+                       return rseq_cmpeqv_trystorev_storev_relaxed_mm_cid(v, expect, v2, newv2, newv, cpu);
+               }
+               return -1;
+       case RSEQ_MO_RELEASE:
+               switch (percpu_mode) {
+               case RSEQ_PERCPU_CPU_ID:
+                       return rseq_cmpeqv_trystorev_storev_release_cpu_id(v, expect, v2, newv2, newv, cpu);
+               case RSEQ_PERCPU_MM_CID:
+                       return rseq_cmpeqv_trystorev_storev_release_mm_cid(v, expect, v2, newv2, newv, cpu);
+               }
+               return -1;
+       default:
+               return -1;
+       }
+}
+
+static inline __attribute__((always_inline))
+int rseq_cmpeqv_cmpeqv_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
+                             intptr_t *v, intptr_t expect,
+                             intptr_t *v2, intptr_t expect2,
+                             intptr_t newv, int cpu)
+{
+       if (rseq_mo != RSEQ_MO_RELAXED)
+               return -1;
+       switch (percpu_mode) {
+       case RSEQ_PERCPU_CPU_ID:
+               return rseq_cmpeqv_cmpeqv_storev_relaxed_cpu_id(v, expect, v2, expect2, newv, cpu);
+       case RSEQ_PERCPU_MM_CID:
+               return rseq_cmpeqv_cmpeqv_storev_relaxed_mm_cid(v, expect, v2, expect2, newv, cpu);
+       }
+       return -1;
+}
+
+static inline __attribute__((always_inline))
+int rseq_cmpeqv_trymemcpy_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
+                                intptr_t *v, intptr_t expect,
+                                void *dst, void *src, size_t len,
+                                intptr_t newv, int cpu)
+{
+       switch (rseq_mo) {
+       case RSEQ_MO_RELAXED:
+               switch (percpu_mode) {
+               case RSEQ_PERCPU_CPU_ID:
+                       return rseq_cmpeqv_trymemcpy_storev_relaxed_cpu_id(v, expect, dst, src, len, newv, cpu);
+               case RSEQ_PERCPU_MM_CID:
+                       return rseq_cmpeqv_trymemcpy_storev_relaxed_mm_cid(v, expect, dst, src, len, newv, cpu);
+               }
+               return -1;
+       case RSEQ_MO_RELEASE:
+               switch (percpu_mode) {
+               case RSEQ_PERCPU_CPU_ID:
+                       return rseq_cmpeqv_trymemcpy_storev_release_cpu_id(v, expect, dst, src, len, newv, cpu);
+               case RSEQ_PERCPU_MM_CID:
+                       return rseq_cmpeqv_trymemcpy_storev_release_mm_cid(v, expect, dst, src, len, newv, cpu);
+               }
+               return -1;
+       default:
+               return -1;
+       }
+}
+
 #ifdef __cplusplus
 }
 #endif
This page took 0.061385 seconds and 4 git commands to generate.