Restartable Sequences selftests updates
Subsystems (3)
Mathieu Desnoyers
maintainercover Restartable Sequences selftests updates
Those rseq selftests updates are hereby submitted for feedback.
They change the per-architecture pre-abort signatures to ensure those are valid trap instructions.
The way exit points are presented to debuggers is enhanced, ensuring all exit points are present, so debuggers don't have to disassemble rseq critical section to properly skip over them.
Also, discussions with the glibc community is reaching a concensus of exposing a __rseq_handled symbol from glibc to coexist with rseq early adopters. Update the rseq selftest code to use this symbol.
Feedback is welcome.
Thanks,
Mathieu
Martin Schwidefsky (1):
rseq/selftests: s390: use trap4 for RSEQ_SIG
Mathieu Desnoyers (9):
rseq/selftests: Add __rseq_exit_point_array section for debuggers rseq/selftests: Introduce __rseq_cs_ptr_array, rename __rseq_table to
__rseq_cs
rseq/selftests: Use __rseq_handled symbol to coexist with glibc rseq/selftests: s390: use jg instruction for jumps outside of the asm rseq/selftests: x86: use ud1 instruction as RSEQ_SIG opcode rseq/selftests: arm: use udf instruction for RSEQ_SIG rseq/selftests: aarch64 code signature: handle big-endian environment rseq/selftests: powerpc code signature: generate valid instructions rseq/selftests: mips: use break instruction for RSEQ_SIG
tools/testing/selftests/rseq/rseq-arm.h | 132 ++++++++++++++++++++++++++---- tools/testing/selftests/rseq/rseq-arm64.h | 74 ++++++++++++++++- tools/testing/selftests/rseq/rseq-mips.h | 87 +++++++++++++++++--- tools/testing/selftests/rseq/rseq-ppc.h | 90 ++++++++++++++++++-- tools/testing/selftests/rseq/rseq-s390.h | 78 ++++++++++++++++-- tools/testing/selftests/rseq/rseq-x86.h | 120 +++++++++++++++++++++++++-- tools/testing/selftests/rseq/rseq.c | 55 +++++++++++-- tools/testing/selftests/rseq/rseq.h | 1 + 8 files changed, 583 insertions(+), 54 deletions(-)
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 1/10 rseq/selftests: Add __rseq_exit_point_array section for debuggers
Knowing all exit points is useful to assist debuggers stepping over the rseq critical sections without requiring them to disassemble the content of the critical section to figure out the exit points.
--- tools/testing/selftests/rseq/rseq-arm.h | 52 +++++++++++++++++ tools/testing/selftests/rseq/rseq-arm64.h | 52 +++++++++++++++++ tools/testing/selftests/rseq/rseq-mips.h | 53 +++++++++++++++++ tools/testing/selftests/rseq/rseq-ppc.h | 66 ++++++++++++++++++++++ tools/testing/selftests/rseq/rseq-s390.h | 55 ++++++++++++++++++ tools/testing/selftests/rseq/rseq-x86.h | 94 +++++++++++++++++++++++++++++++ 6 files changed, 372 insertions(+)
tools/testing/selftests/rseq/rseq-arm.h +52 -0
@@ -42,6 +42,19 @@ do { \__RSEQ_ASM_DEFINE_TABLE (0x0 ,0x0 , start_ip, \ (post_commit_ip - start_ip), abort_ip) +/* +* Exit points of a rseq critical section consist of all instructions outside +* of the critical section where a critical section can either branch to or +* reach through the normal course of its execution. The abort IP and the +* post-commit IP are already part of the __rseq_table section and should not +* be explicitly defined as additional exit points. Knowing all exit points is +* useful to assist debuggers stepping over the critical section. + */ +#define RSEQ_ASM_DEFINE_EXIT_POINT (start_ip, exit_ip) \ + ".pushsection __rseq_exit_point_array, \"aw\"\n\t" \ + ".word " __rseq_str(start_ip) ", 0x0, " __rseq_str(exit_ip) ", 0x0\n\t" \ + ".popsection\n\t" +#define RSEQ_ASM_STORE_RSEQ_CS (label, cs_label, rseq_cs) \ RSEQ_INJECT_ASM(1) \ "adr r0, " __rseq_str(cs_label) "\n\t" \
@@ -87,6 +100,11 @@ int rseq_cmpeqv_storev(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( RSEQ_ASM_DEFINE_TABLE(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, 3f, rseq_cs) RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
@@ -149,6 +167,11 @@ int rseq_cmpnev_storeoffp_load(intptr_t *v, intptr_t expectnot,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( RSEQ_ASM_DEFINE_TABLE(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, 3f, rseq_cs) RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
@@ -215,6 +238,9 @@ int rseq_addv(intptr_t *v, intptr_t count, int cpu)rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( RSEQ_ASM_DEFINE_TABLE(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, 3f, rseq_cs) RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
@@ -267,6 +293,11 @@ int rseq_cmpeqv_trystorev_storev(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( RSEQ_ASM_DEFINE_TABLE(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, 3f, rseq_cs) RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
@@ -337,6 +368,11 @@ int rseq_cmpeqv_trystorev_storev_release(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( RSEQ_ASM_DEFINE_TABLE(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, 3f, rseq_cs) RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
@@ -408,6 +444,12 @@ int rseq_cmpeqv_cmpeqv_storev(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( RSEQ_ASM_DEFINE_TABLE(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, 3f, rseq_cs) RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
@@ -486,6 +528,11 @@ int rseq_cmpeqv_trymemcpy_storev(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( RSEQ_ASM_DEFINE_TABLE(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"str %[src], %[rseq_scratch0]\n\t" "str %[dst], %[rseq_scratch1]\n\t" "str %[len], %[rseq_scratch2]\n\t"
@@ -605,6 +652,11 @@ int rseq_cmpeqv_trymemcpy_storev_release(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( RSEQ_ASM_DEFINE_TABLE(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"str %[src], %[rseq_scratch0]\n\t" "str %[dst], %[rseq_scratch1]\n\t" "str %[len], %[rseq_scratch2]\n\t"
tools/testing/selftests/rseq/rseq-arm64.h +52 -0
106 lines not on this page · raw message
tools/testing/selftests/rseq/rseq-mips.h +53 -0
107 lines not on this page · raw message
tools/testing/selftests/rseq/rseq-ppc.h +66 -0
126 lines not on this page · raw message
tools/testing/selftests/rseq/rseq-s390.h +55 -0
103 lines not on this page · raw message
tools/testing/selftests/rseq/rseq-x86.h +94 -0
190 lines not on this page · raw message
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 2/10 rseq/selftests: Introduce __rseq_cs_ptr_array, rename __rseq_table to __rseq_cs
In reply to Mathieu DesnoyersThe entries within __rseq_table are aligned on 32 bytes due to linux/rseq.h struct rseq_cs uapi requirements, but the start of the __rseq_table section is not guaranteed to be 32-byte aligned. It can cause padding to be added at the start of the section, which makes it hard to use as an array of items by debuggers.
Considering that __rseq_table does not really consist of a table due to the presence of padding, rename this section to __rseq_cs.
Create a new __rseq_cs_ptr_array section which contains 64-bit packed pointers to entries within the __rseq_cs section.
--- tools/testing/selftests/rseq/rseq-arm.h | 32 +++++++++++++++++-------------- tools/testing/selftests/rseq/rseq-arm64.h | 9 ++++++--- tools/testing/selftests/rseq/rseq-mips.h | 32 +++++++++++++++++-------------- tools/testing/selftests/rseq/rseq-ppc.h | 22 +++++++++++++-------- tools/testing/selftests/rseq/rseq-s390.h | 18 +++++++++++------ tools/testing/selftests/rseq/rseq-x86.h | 19 ++++++++++++------ 6 files changed, 81 insertions(+), 51 deletions(-)
tools/testing/selftests/rseq/rseq-arm.h +18 -14
@@ -30,24 +30,28 @@ do { \#include "rseq-skip.h" #else /* !RSEQ_SKIP_FASTPATH */ -#define __RSEQ_ASM_DEFINE_TABLE (version, flags, start_ip, \ +#define __RSEQ_ASM_DEFINE_TABLE (label, version, flags, start_ip, \ post_commit_offset, abort_ip) \ - ".pushsection __rseq_table, \"aw\"\n\t" \ + ".pushsection __rseq_cs, \"aw\"\n\t" \ ".balign 32\n\t" \ + __rseq_str(label) ":\n\t" \ ".word " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \ ".word " __rseq_str(start_ip) ", 0x0, " __rseq_str(post_commit_offset) ", 0x0, " __rseq_str(abort_ip) ", 0x0\n\t" \ + ".popsection\n\t" \ + ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \ + ".word " __rseq_str(label) "b, 0x0\n\t" \ ".popsection\n\t" -#define RSEQ_ASM_DEFINE_TABLE (start_ip, post_commit_ip, abort_ip) \ - __RSEQ_ASM_DEFINE_TABLE(0x0, 0x0, start_ip, \ +#define RSEQ_ASM_DEFINE_TABLE (label, start_ip, post_commit_ip, abort_ip) \ + __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \ (post_commit_ip - start_ip), abort_ip)/* * Exit points of a rseq critical section consist of all instructions outside * of the critical section where a critical section can either branch to or * reach through the normal course of its execution. The abort IP and the -* post-commit IP are already part of the __rseq_table section and should not -* be explicitly defined as additional exit points. Knowing all exit points is +* post-commit IP are already part of the __rseq_cs section and should not be +* explicitly defined as additional exit points. Knowing all exit points is * useful to assist debuggers stepping over the critical section. */ #define RSEQ_ASM_DEFINE_EXIT_POINT (start_ip, exit_ip) \
@@ -99,7 +103,7 @@ int rseq_cmpeqv_storev(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -166,7 +170,7 @@ int rseq_cmpnev_storeoffp_load(intptr_t *v, intptr_t expectnot,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -237,7 +241,7 @@ int rseq_addv(intptr_t *v, intptr_t count, int cpu)rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 1f, 2f, 4f) /* start, commit, abort */#ifdef RSEQ_COMPARE_TWICE RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])#endif
@@ -292,7 +296,7 @@ int rseq_cmpeqv_trystorev_storev(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -367,7 +371,7 @@ int rseq_cmpeqv_trystorev_storev_release(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -443,7 +447,7 @@ int rseq_cmpeqv_cmpeqv_storev(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -527,7 +531,7 @@ int rseq_cmpeqv_trymemcpy_storev(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -651,7 +655,7 @@ int rseq_cmpeqv_trymemcpy_storev_release(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
tools/testing/selftests/rseq/rseq-arm64.h +6 -3
@@ -82,13 +82,16 @@ do { \#define __RSEQ_ASM_DEFINE_TABLE (label, version, flags, start_ip, \ post_commit_offset, abort_ip) \ - " .pushsection __rseq_table, \"aw\"\n" \ + " .pushsection __rseq_cs, \"aw\"\n" \ " .balign 32\n" \ __rseq_str(label) ":\n" \ " .long " __rseq_str(version) ", " __rseq_str(flags) "\n" \ " .quad " __rseq_str(start_ip) ", " \ __rseq_str(post_commit_offset) ", " \ __rseq_str(abort_ip) "\n" \ + " .popsection\n\t" \ + " .pushsection __rseq_cs_ptr_array, \"aw\"\n" \ + " .quad " __rseq_str(label) "b\n" \ " .popsection\n"#define RSEQ_ASM_DEFINE_TABLE (label, start_ip, post_commit_ip, abort_ip) \
@@ -99,8 +102,8 @@ do { \* Exit points of a rseq critical section consist of all instructions outside * of the critical section where a critical section can either branch to or * reach through the normal course of its execution. The abort IP and the -* post-commit IP are already part of the __rseq_table section and should not -* be explicitly defined as additional exit points. Knowing all exit points is +* post-commit IP are already part of the __rseq_cs section and should not be +* explicitly defined as additional exit points. Knowing all exit points is * useful to assist debuggers stepping over the critical section. */ #define RSEQ_ASM_DEFINE_EXIT_POINT (start_ip, exit_ip) \
tools/testing/selftests/rseq/rseq-mips.h +18 -14
@@ -54,26 +54,30 @@ do { \# error unsupported _MIPS_SZLONG#endif -#define __RSEQ_ASM_DEFINE_TABLE (version, flags, start_ip, \ +#define __RSEQ_ASM_DEFINE_TABLE (label, version, flags, start_ip, \ post_commit_offset, abort_ip) \ - ".pushsection __rseq_table, \"aw\"\n\t" \ + ".pushsection __rseq_cs, \"aw\"\n\t" \ ".balign 32\n\t" \ + __rseq_str(label) ":\n\t" \ ".word " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \ LONG " " U32_U64_PAD(__rseq_str(start_ip)) "\n\t" \ LONG " " U32_U64_PAD(__rseq_str(post_commit_offset)) "\n\t" \ LONG " " U32_U64_PAD(__rseq_str(abort_ip)) "\n\t" \ + ".popsection\n\t" \ + ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \ + LONG " " U32_U64_PAD(__rseq_str(label) "b") "\n\t" \ ".popsection\n\t" -#define RSEQ_ASM_DEFINE_TABLE (start_ip, post_commit_ip, abort_ip) \ - __RSEQ_ASM_DEFINE_TABLE(0x0, 0x0, start_ip, \ +#define RSEQ_ASM_DEFINE_TABLE (label, start_ip, post_commit_ip, abort_ip) \ + __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \ (post_commit_ip - start_ip), abort_ip)/* * Exit points of a rseq critical section consist of all instructions outside * of the critical section where a critical section can either branch to or * reach through the normal course of its execution. The abort IP and the -* post-commit IP are already part of the __rseq_table section and should not -* be explicitly defined as additional exit points. Knowing all exit points is +* post-commit IP are already part of the __rseq_cs section and should not be +* explicitly defined as additional exit points. Knowing all exit points is * useful to assist debuggers stepping over the critical section. */ #define RSEQ_ASM_DEFINE_EXIT_POINT (start_ip, exit_ip) \
@@ -127,7 +131,7 @@ int rseq_cmpeqv_storev(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -192,7 +196,7 @@ int rseq_cmpnev_storeoffp_load(intptr_t *v, intptr_t expectnot,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -261,7 +265,7 @@ int rseq_addv(intptr_t *v, intptr_t count, int cpu)rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 1f, 2f, 4f) /* start, commit, abort */#ifdef RSEQ_COMPARE_TWICE RSEQ_ASM_DEFINE_EXIT_POINT(1f, %l[error1])#endif
@@ -316,7 +320,7 @@ int rseq_cmpeqv_trystorev_storev(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -389,7 +393,7 @@ int rseq_cmpeqv_trystorev_storev_release(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -463,7 +467,7 @@ int rseq_cmpeqv_cmpeqv_storev(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -543,7 +547,7 @@ int rseq_cmpeqv_trymemcpy_storev(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
@@ -664,7 +668,7 @@ int rseq_cmpeqv_trymemcpy_storev_release(intptr_t *v, intptr_t expect,rseq_workaround_gcc_asm_size_guess (); __asm__ __volatile__ goto ( - RSEQ_ASM_DEFINE_TABLE(1f, 2f, 4f) /* start, commit, abort */ + RSEQ_ASM_DEFINE_TABLE(9, 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])
tools/testing/selftests/rseq/rseq-ppc.h +14 -8
@@ -33,8 +33,8 @@ do { \#else /* !RSEQ_SKIP_FASTPATH */ /* -* The __rseq_table section can be used by debuggers to better handle -* single-stepping through the restartable critical sections. +* The __rseq_cs_ptr_array and __rseq_cs sections can be used by debuggers to +* better handle single-stepping through the restartable critical sections. */ #ifdef __PPC64__
@@ -46,11 +46,14 @@ do { \#define __RSEQ_ASM_DEFINE_TABLE (label, version, flags, \ start_ip, post_commit_offset, abort_ip) \ - ".pushsection __rseq_table, \"aw\"\n\t" \ + ".pushsection __rseq_cs, \"aw\"\n\t" \ ".balign 32\n\t" \ __rseq_str(label) ":\n\t" \ ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \ ".quad " __rseq_str(start_ip) ", " __rseq_str(post_commit_offset) ", " __rseq_str(abort_ip) "\n\t" \ + ".popsection\n\t" \ + ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \ + ".quad " __rseq_str(label) "b\n\t" \ ".popsection\n\t"#define RSEQ_ASM_STORE_RSEQ_CS (label, cs_label, rseq_cs) \
@@ -67,8 +70,8 @@ do { \* Exit points of a rseq critical section consist of all instructions outside * of the critical section where a critical section can either branch to or * reach through the normal course of its execution. The abort IP and the -* post-commit IP are already part of the __rseq_table section and should not -* be explicitly defined as additional exit points. Knowing all exit points is +* post-commit IP are already part of the __rseq_cs section and should not be +* explicitly defined as additional exit points. Knowing all exit points is * useful to assist debuggers stepping over the critical section. */ #define RSEQ_ASM_DEFINE_EXIT_POINT (start_ip, exit_ip) \
@@ -85,20 +88,23 @@ do { \#define __RSEQ_ASM_DEFINE_TABLE (label, version, flags, \ start_ip, post_commit_offset, abort_ip) \ - ".pushsection __rseq_table, \"aw\"\n\t" \ + ".pushsection __rseq_cs, \"aw\"\n\t" \ ".balign 32\n\t" \ __rseq_str(label) ":\n\t" \ ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \/* 32-bit only supported on BE */ \ ".long 0x0, " __rseq_str(start_ip) ", 0x0, " __rseq_str(post_commit_offset) ", 0x0, " __rseq_str(abort_ip) "\n\t" \ +".popsection\n\t" \ +".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \ + ".long 0x0, " __rseq_str(label) "b\n\t" \".popsection\n\t" /* * Exit points of a rseq critical section consist of all instructions outside * of the critical section where a critical section can either branch to or * reach through the normal course of its execution. The abort IP and the -* post-commit IP are already part of the __rseq_table section and should not -* be explicitly defined as additional exit points. Knowing all exit points is +* post-commit IP are already part of the __rseq_cs section and should not be +* explicitly defined as additional exit points. Knowing all exit points is * useful to assist debuggers stepping over the critical section. */ #define RSEQ_ASM_DEFINE_EXIT_POINT(start_ip, exit_ip) \
tools/testing/selftests/rseq/rseq-s390.h +12 -6
@@ -37,19 +37,22 @@ do { \#define __RSEQ_ASM_DEFINE_TABLE (label, version, flags, \ start_ip, post_commit_offset, abort_ip) \ - ".pushsection __rseq_table, \"aw\"\n\t" \ + ".pushsection __rseq_cs, \"aw\"\n\t" \ ".balign 32\n\t" \ __rseq_str(label) ":\n\t" \ ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \ ".quad " __rseq_str(start_ip) ", " __rseq_str(post_commit_offset) ", " __rseq_str(abort_ip) "\n\t" \ + ".popsection\n\t" \ + ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \ + ".quad " __rseq_str(label) "b\n\t" \ ".popsection\n\t"/* * Exit points of a rseq critical section consist of all instructions outside * of the critical section where a critical section can either branch to or * reach through the normal course of its execution. The abort IP and the -* post-commit IP are already part of the __rseq_table section and should not -* be explicitly defined as additional exit points. Knowing all exit points is +* post-commit IP are already part of the __rseq_cs section and should not be +* explicitly defined as additional exit points. Knowing all exit points is * useful to assist debuggers stepping over the critical section. */ #define RSEQ_ASM_DEFINE_EXIT_POINT (start_ip, exit_ip) \
@@ -61,19 +64,22 @@ do { \#define __RSEQ_ASM_DEFINE_TABLE (label, version, flags, \ start_ip, post_commit_offset, abort_ip) \ - ".pushsection __rseq_table, \"aw\"\n\t" \ + ".pushsection __rseq_cs, \"aw\"\n\t" \ ".balign 32\n\t" \ __rseq_str(label) ":\n\t" \ ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \ ".long 0x0, " __rseq_str(start_ip) ", 0x0, " __rseq_str(post_commit_offset) ", 0x0, " __rseq_str(abort_ip) "\n\t" \ + ".popsection\n\t" \ + ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \ + ".long 0x0, " __rseq_str(label) "b\n\t" \ ".popsection\n\t"/* * Exit points of a rseq critical section consist of all instructions outside * of the critical section where a critical section can either branch to or * reach through the normal course of its execution. The abort IP and the -* post-commit IP are already part of the __rseq_table section and should not -* be explicitly defined as additional exit points. Knowing all exit points is +* post-commit IP are already part of the __rseq_cs section and should not be +* explicitly defined as additional exit points. Knowing all exit points is * useful to assist debuggers stepping over the critical section. */ #define RSEQ_ASM_DEFINE_EXIT_POINT (start_ip, exit_ip) \
tools/testing/selftests/rseq/rseq-x86.h +13 -6
@@ -47,13 +47,17 @@ do { \#define __RSEQ_ASM_DEFINE_TABLE (label, version, flags, \ start_ip, post_commit_offset, abort_ip) \ - ".pushsection __rseq_table, \"aw\"\n\t" \ + ".pushsection __rseq_cs, \"aw\"\n\t" \ ".balign 32\n\t" \ __rseq_str(label) ":\n\t" \ ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \ ".quad " __rseq_str(start_ip) ", " __rseq_str(post_commit_offset) ", " __rseq_str(abort_ip) "\n\t" \ + ".popsection\n\t" \ + ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \ + ".quad " __rseq_str(label) "b\n\t" \ ".popsection\n\t" +#define RSEQ_ASM_DEFINE_TABLE (label, start_ip, post_commit_ip, abort_ip) \ __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \ (post_commit_ip - start_ip), abort_ip)
@@ -62,8 +66,8 @@ do { \* Exit points of a rseq critical section consist of all instructions outside * of the critical section where a critical section can either branch to or * reach through the normal course of its execution. The abort IP and the -* post-commit IP are already part of the __rseq_table section and should not -* be explicitly defined as additional exit points. Knowing all exit points is +* post-commit IP are already part of the __rseq_cs section and should not be +* explicitly defined as additional exit points. Knowing all exit points is * useful to assist debuggers stepping over the critical section. */ #define RSEQ_ASM_DEFINE_EXIT_POINT (start_ip, exit_ip) \
@@ -566,11 +570,14 @@ do { \*/ #define __RSEQ_ASM_DEFINE_TABLE (label, version, flags, \ start_ip, post_commit_offset, abort_ip) \ - ".pushsection __rseq_table, \"aw\"\n\t" \ + ".pushsection __rseq_cs, \"aw\"\n\t" \ ".balign 32\n\t" \ __rseq_str(label) ":\n\t" \ ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \ ".long " __rseq_str(start_ip) ", 0x0, " __rseq_str(post_commit_offset) ", 0x0, " __rseq_str(abort_ip) ", 0x0\n\t" \ + ".popsection\n\t" \ + ".pushsection __rseq_cs_ptr_array, \"aw\"\n\t" \ + ".long " __rseq_str(label) "b, 0x0\n\t" \ ".popsection\n\t"#define RSEQ_ASM_DEFINE_TABLE (label, start_ip, post_commit_ip, abort_ip) \
@@ -581,8 +588,8 @@ do { \* Exit points of a rseq critical section consist of all instructions outside * of the critical section where a critical section can either branch to or * reach through the normal course of its execution. The abort IP and the -* post-commit IP are already part of the __rseq_table section and should not -* be explicitly defined as additional exit points. Knowing all exit points is +* post-commit IP are already part of the __rseq_cs section and should not be +* explicitly defined as additional exit points. Knowing all exit points is * useful to assist debuggers stepping over the critical section. */ #define RSEQ_ASM_DEFINE_EXIT_POINT (start_ip, exit_ip) \
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 3/10 rseq/selftests: Use __rseq_handled symbol to coexist with glibc
In reply to Mathieu DesnoyersIn order to integrate rseq into user-space applications, expose a __rseq_handled symbol so many rseq users can be linked into the same application (e.g. librseq and glibc).
The __rseq_refcount TLS variable is static to the librseq library. It ensures that rseq syscall registration/unregistration happens only for the most early/late caller to rseq_{,un}register_current_thread for each thread, thus ensuring that rseq is registered across the lifetime of all rseq users for a given thread.
--- tools/testing/selftests/rseq/rseq.c | 55 +++++++++++++++++++++++++++++++------ tools/testing/selftests/rseq/rseq.h | 1 + 2 files changed, 48 insertions(+), 8 deletions(-)
tools/testing/selftests/rseq/rseq.c +47 -8
@@ -25,18 +25,27 @@#include <syscall.h> #include <assert.h> #include <signal.h> +#include <limits.h> #include "rseq.h" #define ARRAY_SIZE (arr) (sizeof(arr) / sizeof((arr)[0])) -__attribute__((tls_model ("initial-exec" ))) __thread -volatile struct rseq __rseq_abi = { +__threadvolatile struct rseq __rseq_abi = { .cpu_id =RSEQ_CPU_ID_UNINITIALIZED , }; -static __attribute__((tls_model ("initial-exec" ))) __thread -volatile int refcount; +/* +* Shared with other libraries. This library may take rseq ownership if it is +* still 0 when executing the library constructor. Set to 1 by library +* constructor when handling rseq. Set to 0 in destructor if handling rseq. +*/ +int __rseq_handled; + +/* Whether this library have ownership of rseq registration. */ +static int rseq_ownership; + +static __threadvolatile uint32_t __rseq_refcount;static void signal_off_save (sigset_t *oldset) {
@@ -69,8 +78,14 @@ int rseq_register_current_thread(void)int rc, ret =0 ;sigset_t oldset; +if (!rseq_ownership) +return 0 ;signal_off_save (&oldset); -if (refcount++) +if (__rseq_refcount ==UINT_MAX ) { + ret =-1 ; + goto end; + } +if (__rseq_refcount++) goto end; rc =sys_rseq (&__rseq_abi,sizeof (struct rseq ),0 ,RSEQ_SIG );if (!rc) {
@@ -78,9 +93,9 @@ int rseq_register_current_thread(void) goto end; }if (errno !=EBUSY ) - __rseq_abi.cpu_id =-2 ; + __rseq_abi.cpu_id =RSEQ_CPU_ID_REGISTRATION_FAILED ; ret =-1 ; - refcount--; + __rseq_lib_abi.refcount--; end:signal_restore (oldset);return ret;
@@ -91,13 +106,20 @@ int rseq_unregister_current_thread(void)int rc, ret =0 ;sigset_t oldset; +if (!rseq_ownership) +return 0 ;signal_off_save (&oldset); -if (--refcount) +if (!__rseq_refcount) { + ret =-1 ; + goto end; + } +if (--__rseq_refcount) goto end; rc =sys_rseq (&__rseq_abi,sizeof (struct rseq ),RSEQ_FLAG_UNREGISTER ,RSEQ_SIG );if (!rc) goto end; + __rseq_refcount =1 ; ret =-1 ; end:signal_restore (oldset);
@@ -115,3 +137,20 @@ int32_t rseq_fallback_current_cpu(void) }return cpu; } + +void __attribute__((constructor))rseq_init (void ) +{ +/* Check whether rseq is handled by another library. */ +if (__rseq_handled) +return ; + __rseq_handled =1 ; + rseq_ownership =1 ; +} + +void __attribute__((destructor))rseq_fini (void ) +{ +if (!rseq_ownership) +return ; + __rseq_handled =0 ; + rseq_ownership =0 ; +}
tools/testing/selftests/rseq/rseq.h +1 -0
@@ -45,6 +45,7 @@#endif extern __threadvolatile struct rseq __rseq_abi; +extern int __rseq_handled;#define rseq_likely (x) __builtin_expect(!!(x), 1)#define rseq_unlikely (x) __builtin_expect(!!(x), 0)
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 4/10 rseq/selftests: s390: use jg instruction for jumps outside of the asm
In reply to Mathieu DesnoyersThe branch target range of the "j" instruction is 64K, which is not enough for the general case.
--- tools/testing/selftests/rseq/rseq-s390.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/rseq/rseq-s390.h +2 -2
@@ -117,14 +117,14 @@ do { \ ".long " __rseq_str(RSEQ_SIG) "\n\t" \ __rseq_str(label) ":\n\t" \ teardown \ - "j %l[" __rseq_str(abort_label) "]\n\t" \ + "jg %l[" __rseq_str(abort_label) "]\n\t" \ ".popsection\n\t"#define RSEQ_ASM_DEFINE_CMPFAIL (label, teardown, cmpfail_label) \ ".pushsection __rseq_failure, \"ax\"\n\t" \ __rseq_str(label) ":\n\t" \ teardown \ - "j %l[" __rseq_str(cmpfail_label) "]\n\t" \ + "jg %l[" __rseq_str(cmpfail_label) "]\n\t" \ ".popsection\n\t" static inline __attribute__((always_inline))
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 5/10 rseq/selftests: x86: use ud1 instruction as RSEQ_SIG opcode
In reply to Mathieu DesnoyersUse ud1 as the guard instruction for the restartable sequence abort handler. Its benefit compared to nopl is to trap execution if the program ends up trying to execute it by mistake, which makes debugging easier.
The 4-byte signature per se is unchanged (it is the instruction operand). Only the opcode is changed from nopl to ud1.
--- tools/testing/selftests/rseq/rseq-x86.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
tools/testing/selftests/rseq/rseq-x86.h +11 -4
@@ -7,6 +7,13 @@#include <stdint.h> +/* +* RSEQ_SIG is used with the following reserved undefined instructions, which +* trap in user-space: +* +* x86-32: 0f b9 3d 53 30 05 53 ud1 0x53053053,%edi +* x86-64: 0f b9 3d 53 30 05 53 ud1 0x53053053(%rip),%edi +*/ #define RSEQ_SIG 0x53053053/*
@@ -88,8 +95,8 @@ do { \#define RSEQ_ASM_DEFINE_ABORT (label, teardown, abort_label) \ ".pushsection __rseq_failure, \"ax\"\n\t" \ -/* Disassembler-friendly signature: nopl <sig>(%rip). */ \ -".byte 0x0f, 0x1f, 0x05\n\t" \ +/* Disassembler-friendly signature: ud1 <sig>(%rip),%edi. */ \ +".byte 0x0f, 0xb9, 0x3d\n\t" \ ".long " __rseq_str(RSEQ_SIG) "\n\t" \ __rseq_str(label) ":\n\t" \ teardown \
@@ -609,8 +616,8 @@ do { \#define RSEQ_ASM_DEFINE_ABORT (label, teardown, abort_label) \ ".pushsection __rseq_failure, \"ax\"\n\t" \ -/* Disassembler-friendly signature: nopl <sig>. */ \ -".byte 0x0f, 0x1f, 0x05\n\t" \ +/* Disassembler-friendly signature: ud1 <sig>,%edi. */ \ +".byte 0x0f, 0xb9, 0x3d\n\t" \ ".long " __rseq_str(RSEQ_SIG) "\n\t" \ __rseq_str(label) ":\n\t" \ teardown \
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 6/10 rseq/selftests: s390: use trap4 for RSEQ_SIG
In reply to Mathieu DesnoyersFrom: Martin Schwidefsky <[email protected]>
Use trap4 as the guard instruction for the restartable sequence abort handler.
--- tools/testing/selftests/rseq/rseq-s390.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/rseq/rseq-s390.h +8 -1
@@ -1,6 +1,13 @@/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ -#define RSEQ_SIG 0x53053053 +/* +* RSEQ_SIG uses the trap4 instruction. As Linux does not make use of the +* access-register mode nor the linkage stack this instruction will always +* cause a special-operation exception (the trap-enabled bit in the DUCT +* is and will stay 0). The instruction pattern is +* b2 ff 0f ff trap4 4095(%r0) +*/ +#define RSEQ_SIG 0xB2FF0FFF#define rseq_smp_mb () __asm__ __volatile__ ("bcr 15,0" ::: "memory")#define rseq_smp_rmb () rseq_smp_mb()
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 7/10 rseq/selftests: arm: use udf instruction for RSEQ_SIG
In reply to Mathieu DesnoyersUse udf as the guard instruction for the restartable sequence abort handler.
Previously, the chosen signature was not a valid instruction, based on the assumption that it could always sit in a literal pool. However, there are compilation environments in which literal pools are not availble, for instance execute-only code. Therefore, we need to choose a signature value that is also a valid instruction.
Handle compiling with -mbig-endian on ARMv6+, which generates binaries with mixed code vs data endianness (little endian code, big endian data).
Else mismatch between code endianness for the generated signatures and data endianness for the RSEQ_SIG parameter passed to the rseq registration will trigger application segmentation faults when the kernel try to abort rseq critical sections.
Prior to ARMv6, -mbig-endian generates big-endian code and data, so endianness should not be reversed in that case.
--- tools/testing/selftests/rseq/rseq-arm.h | 52 +++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-)
tools/testing/selftests/rseq/rseq-arm.h +50 -2
@@ -5,7 +5,54 @@* (C) Copyright 2016-2018 - Mathieu Desnoyers <[email protected]> */ -#define RSEQ_SIG 0x53053053 +/* +* RSEQ_SIG uses the udf A32 instruction with an uncommon immediate operand +* value 0x5de3. This traps if user-space reaches this instruction by mistake, +* and the uncommon operand ensures the kernel does not move the instruction +* pointer to attacker-controlled code on rseq abort. +* +* The instruction pattern in the A32 instruction set is: +* +* e7f5def3 udf #24035 ; 0x5de3 +* +* This translates to the following instruction pattern in the T16 instruction +* set: +* +* little endian: +* def3 udf #243 ; 0xf3 +* e7f5 b.n <7f5> +* +* pre-ARMv6 big endian code: +* e7f5 b.n <7f5> +* def3 udf #243 ; 0xf3 +* +* ARMv6+ -mbig-endian generates mixed endianness code vs data: little-endian +* code and big-endian data. Ensure the RSEQ_SIG data signature matches code +* endianness. Prior to ARMv6, -mbig-endian generates big-endian code and data +* (which match), so there is no need to reverse the endianness of the data +* representation of the signature. However, the choice between BE32 and BE8 +* is done by the linker, so we cannot know whether code and data endianness +* will be mixed before the linker is invoked. +*/ + +#define RSEQ_SIG_CODE 0xe7f5def3 + +#ifndef __ASSEMBLER__ + +#define RSEQ_SIG_DATA \ + ({ \ + int sig; \ + asm volatile ( "b 2f\n\t" \ + "1: .inst " __rseq_str(RSEQ_SIG_CODE) "\n\t" \ + "2:\n\t" \ + "ldr %[sig], 1b\n\t" \ + : [sig] "=r" (sig)); \ + sig; \ + }) + +#define RSEQ_SIG RSEQ_SIG_DATA + +#endif #define rseq_smp_mb () __asm__ __volatile__ ("dmb" ::: "memory", "cc")#define rseq_smp_rmb () __asm__ __volatile__ ("dmb" ::: "memory", "cc")
@@ -78,7 +125,8 @@ do { \ __rseq_str(table_label) ":\n\t" \ ".word " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \ ".word " __rseq_str(start_ip) ", 0x0, " __rseq_str(post_commit_offset) ", 0x0, " __rseq_str(abort_ip) ", 0x0\n\t" \ - ".word " __rseq_str(RSEQ_SIG) "\n\t" \ +".arm\n\t" \ + ".inst " __rseq_str(RSEQ_SIG_CODE) "\n\t" \ __rseq_str(label) ":\n\t" \ teardown \ "b %l[" __rseq_str(abort_label) "]\n\t"
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 8/10 rseq/selftests: aarch64 code signature: handle big-endian environment
In reply to Mathieu DesnoyersHandle compiling with -mbig-endian on aarch64, which generates binaries with mixed code vs data endianness (little endian code, big endian data).
Else mismatch between code endianness for the generated signatures and data endianness for the RSEQ_SIG parameter passed to the rseq registration will trigger application segmentation faults when the kernel try to abort rseq critical sections.
--- tools/testing/selftests/rseq/rseq-arm64.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
tools/testing/selftests/rseq/rseq-arm64.h +15 -2
@@ -6,7 +6,20 @@* (C) Copyright 2018 - Will Deacon <[email protected]> */ -#define RSEQ_SIG 0xd428bc00/* BRK #0x45E0 */ +/* +* aarch64 -mbig-endian generates mixed endianness code vs data: +* little-endian code and big-endian data. Ensure the RSEQ_SIG signature +* matches code endianness. +*/ +#define RSEQ_SIG_CODE 0xd428bc00/* BRK #0x45E0. */ + +#ifdef __AARCH64EB__ +#define RSEQ_SIG_DATA 0x00bc28d4/* BRK #0x45E0. */ +#else +#define RSEQ_SIG_DATA RSEQ_SIG_CODE +#endif + +#define RSEQ_SIG RSEQ_SIG_DATA#define rseq_smp_mb () __asm__ __volatile__ ("dmb ish" ::: "memory")#define rseq_smp_rmb () __asm__ __volatile__ ("dmb ishld" ::: "memory")
@@ -121,7 +134,7 @@ do { \#define RSEQ_ASM_DEFINE_ABORT (label, abort_label) \ " b 222f\n" \ - " .inst " __rseq_str(RSEQ_SIG) "\n" \ + " .inst " __rseq_str(RSEQ_SIG_CODE) "\n" \ __rseq_str(label) ":\n" \ " b %l[" __rseq_str(abort_label) "]\n" \ "222:\n"
Signature · 1 line
2.11.0
Re: [RFC PATCH for 5.2 08/10] rseq/selftests: aarch64 code signature: handle big-endian environment
Quoted 64 lines · Mathieu Desnoyers
Handle compiling with -mbig-endian on aarch64, which generates binaries with mixed code vs data endianness (little endian code, big endian data).
Else mismatch between code endianness for the generated signatures and data endianness for the
RSEQ_SIGparameter passed to the rseq registration will trigger application segmentation faults when the kernel try to abort rseq critical sections.Signed-off-by Mathieu Desnoyers <[email protected]>CC Peter Zijlstra <[email protected]>CC Thomas Gleixner <[email protected]>CC Joel Fernandes <[email protected]>CC Catalin Marinas <[email protected]>CC Dave Watson <[email protected]>CC Will Deacon <[email protected]>CC Shuah Khan <[email protected]>CC Andi Kleen <[email protected]>CC "H . Peter Anvin" <[email protected]>CC Chris Lameter <[email protected]>CC Russell King <[email protected]>CC Michael Kerrisk <[email protected]>CC "Paul E . McKenney" <[email protected]>CC Paul Turner <[email protected]>CC Boqun Feng <[email protected]>CC Josh Triplett <[email protected]>CC Steven Rostedt <[email protected]>CC Ben Maurer <[email protected]>CC Andy Lutomirski <[email protected]>CC Andrew Morton <[email protected]>CC Linus Torvalds <[email protected]>--- tools/testing/selftests/rseq/rseq-arm64.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)tools/testing/selftests/rseq/rseq-arm64.h +14
-1@@ -6,7 +6,20 @@* (C) Copyright 2018 - Will Deacon <[email protected]> */ -#define RSEQ_SIG 0xd428bc00/* BRK #0x45E0 */ +/* +* aarch64 -mbig-endian generates mixed endianness code vs data: +* little-endian code and big-endian data. Ensure the RSEQ_SIG signature +* matches code endianness. +*/ +#define RSEQ_SIG_CODE 0xd428bc00/* BRK #0x45E0. */ + +#ifdef __AARCH64EB__ +#define RSEQ_SIG_DATA 0x00bc28d4/* BRK #0x45E0. */ +#else +#define RSEQ_SIG_DATA RSEQ_SIG_CODE +#endif + +#define RSEQ_SIG RSEQ_SIG_DATA#define rseq_smp_mb () __asm__ __volatile__ ("dmb ish" ::: "memory")#define rseq_smp_rmb () __asm__ __volatile__ ("dmb ishld" ::: "memory")
tools/testing/selftests/rseq/rseq-arm64.h do { \ · line 137
Quoted 5 lines
tools/testing/selftests/rseq/rseq-arm64.h +1
-1@@ -121,7 +134,7 @@ do { \#define RSEQ_ASM_DEFINE_ABORT (label, abort_label) \ " b 222f\n" \ - " .inst " __rseq_str(RSEQ_SIG) "\n" \ + " .inst " __rseq_str(RSEQ_SIG_CODE) "\n" \
I don't think this is right; the .inst directive _should_ emit the value in the instruction stream endianness (i.e. LE, regardless of the data endianness).
That's certainly the case with the kernel.org crosstool GCC:
[mark@lakrids:/mnt/data/tests/inst-test]% cat test.c void func(void)
{
asm volatile(".inst 0xd4000001");
}[mark@lakrids:/mnt/data/tests/inst-test]% usekorg 8.1.0 aarch64-linux-gcc -c test.c [mark@lakrids:/mnt/data/tests/inst-test]% usekorg 8.1.0 aarch64-linux-objdump -d test.o
test.o: file format elf64-littleaarch64
Disassembly of section .text:
0000000000000000 <func>:
0: d4000001 svc #0x0 4: d503201f nop 8: d65f03c0 ret
[mark@lakrids:/mnt/data/tests/inst-test]% usekorg 8.1.0 aarch64-linux-gcc -mbig-endian -c test.c [mark@lakrids:/mnt/data/tests/inst-test]% usekorg 8.1.0 aarch64-linux-objdump -d test.o
test.o: file format elf64-bigaarch64
Disassembly of section .text:
0000000000000000 <func>:
0: d4000001 svc #0x0 4: d503201f nop 8: d65f03c0 ret
Have you tested this? Is there some toolchain that doesn't get this right?
Thanks,
Mark.
Re: [RFC PATCH for 5.2 08/10] rseq/selftests: aarch64 code signature: handle big-endian environment
Quoted 103 lines · Mark Rutland
Quoted 64 lines · Mathieu Desnoyers
Handle compiling with -mbig-endian on aarch64, which generates binaries with mixed code vs data endianness (little endian code, big endian data).
Else mismatch between code endianness for the generated signatures and data endianness for the
RSEQ_SIGparameter passed to the rseq registration will trigger application segmentation faults when the kernel try to abort rseq critical sections.Signed-off-by Mathieu Desnoyers <[email protected]>CC Peter Zijlstra <[email protected]>CC Thomas Gleixner <[email protected]>CC Joel Fernandes <[email protected]>CC Catalin Marinas <[email protected]>CC Dave Watson <[email protected]>CC Will Deacon <[email protected]>CC Shuah Khan <[email protected]>CC Andi Kleen <[email protected]>CC "H . Peter Anvin" <[email protected]>CC Chris Lameter <[email protected]>CC Russell King <[email protected]>CC Michael Kerrisk <[email protected]>CC "Paul E . McKenney" <[email protected]>CC Paul Turner <[email protected]>CC Boqun Feng <[email protected]>CC Josh Triplett <[email protected]>CC Steven Rostedt <[email protected]>CC Ben Maurer <[email protected]>CC Andy Lutomirski <[email protected]>CC Andrew Morton <[email protected]>CC Linus Torvalds <[email protected]>--- tools/testing/selftests/rseq/rseq-arm64.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)tools/testing/selftests/rseq/rseq-arm64.h +14
-1@@ -6,7 +6,20 @@* (C) Copyright 2018 - Will Deacon <[email protected]> */ -#define RSEQ_SIG 0xd428bc00/* BRK #0x45E0 */ +/* +* aarch64 -mbig-endian generates mixed endianness code vs data: +* little-endian code and big-endian data. Ensure the RSEQ_SIG signature +* matches code endianness. +*/ +#define RSEQ_SIG_CODE 0xd428bc00/* BRK #0x45E0. */ + +#ifdef __AARCH64EB__ +#define RSEQ_SIG_DATA 0x00bc28d4/* BRK #0x45E0. */ +#else +#define RSEQ_SIG_DATA RSEQ_SIG_CODE +#endif + +#define RSEQ_SIG RSEQ_SIG_DATA#define rseq_smp_mb () __asm__ __volatile__ ("dmb ish" ::: "memory")#define rseq_smp_rmb () __asm__ __volatile__ ("dmb ishld" ::: "memory")tools/testing/selftests/rseq/rseq-arm64.h do { \ · line 137
Quoted 5 lines
tools/testing/selftests/rseq/rseq-arm64.h +1
-1@@ -121,7 +134,7 @@ do { \#define RSEQ_ASM_DEFINE_ABORT (label, abort_label) \ " b 222f\n" \ - " .inst " __rseq_str(RSEQ_SIG) "\n" \ + " .inst " __rseq_str(RSEQ_SIG_CODE) "\n" \I don't think this is right; the .inst directive _should_ emit the value in the instruction stream endianness (i.e. LE, regardless of the data endianness).
That's certainly the case with the kernel.org crosstool GCC:
[mark@lakrids:/mnt/data/tests/inst-test]% cat
test.cvoid func(void){ asm volatile(".inst 0xd4000001"); }[mark@lakrids:/mnt/data/tests/inst-test]% usekorg 8.1.0 aarch64-linux-gcc -c
test.c[mark@lakrids:/mnt/data/tests/inst-test]% usekorg 8.1.0 aarch64-linux-objdump -d test.otest.o: file format elf64-littleaarch64
Disassembly of section .text:
0000000000000000 <func>:
0: d4000001 svc #0x0 4: d503201f nop 8: d65f03c0 ret[mark@lakrids:/mnt/data/tests/inst-test]% usekorg 8.1.0 aarch64-linux-gcc -mbig-endian -c
test.c[mark@lakrids:/mnt/data/tests/inst-test]% usekorg 8.1.0 aarch64-linux-objdump -d test.otest.o: file format elf64-bigaarch64
Disassembly of section .text:
0000000000000000 <func>:
Quoted 8 lines
0: d4000001 svc #0x0 4: d503201f nop 8: d65f03c0 retHave you tested this? Is there some toolchain that doesn't get this right?
I think that the issue is that the kernel loads the thing to check the signature. RSEQ_SIG_CODE isn't byte-swapped explicitly and is used with .inst. RSEG_SIG_DATA is byte-swapped to ensure that the value passed into the syscall is consistent with what the kernel will load.
But yeah, I've just spent the last ten minutes confusing myself with this.
Will
Re: [RFC PATCH for 5.2 08/10] rseq/selftests: aarch64 code signature: handle big-endian environment
In reply to Mark RutlandQuoted 20 lines · Mark Rutland
Quoted 19 lines · Mathieu Desnoyers
tools/testing/selftests/rseq/rseq-arm64.h +14
-0+/* +* aarch64 -mbig-endian generates mixed endianness code vs data: +* little-endian code and big-endian data. Ensure the RSEQ_SIG signature +* matches code endianness. +*/ +#define RSEQ_SIG_CODE 0xd428bc00/* BRK #0x45E0. */ + +#ifdef __AARCH64EB__ +#define RSEQ_SIG_DATA 0x00bc28d4/* BRK #0x45E0. */ +#else +#define RSEQ_SIG_DATA RSEQ_SIG_CODE +#endif + +#define RSEQ_SIG RSEQ_SIG_DATA#define rseq_smp_mb () __asm__ __volatile__ ("dmb ish" ::: "memory")#define rseq_smp_rmb () __asm__ __volatile__ ("dmb ishld" ::: "memory")@@ -121,7 +134,7 @@ do { \
Quoted 8 lines
tools/testing/selftests/rseq/rseq-arm64.h do { \ · line 137
Quoted 4 lines
tools/testing/selftests/rseq/rseq-arm64.h +1
-1#define RSEQ_ASM_DEFINE_ABORT (label, abort_label) \ " b 222f\n" \ - " .inst " __rseq_str(RSEQ_SIG) "\n" \ + " .inst " __rseq_str(RSEQ_SIG_CODE) "\n" \I don't think this is right; the .inst directive _should_ emit the value in the instruction stream endianness (i.e. LE, regardless of the data endianness).
Now I see the RSEQ_CODE value isn't endian-swapped as teh RSEQ_DATA value is, so the code above is fine.
Sory for the noise.
Thanks,
Mark.
Re: [RFC PATCH for 5.2 08/10] rseq/selftests: aarch64 code signature: handle big-endian environment
In reply to Mathieu DesnoyersQuoted 45 lines · Mathieu Desnoyers
Handle compiling with -mbig-endian on aarch64, which generates binaries with mixed code vs data endianness (little endian code, big endian data).
Else mismatch between code endianness for the generated signatures and data endianness for the
RSEQ_SIGparameter passed to the rseq registration will trigger application segmentation faults when the kernel try to abort rseq critical sections.Signed-off-by Mathieu Desnoyers <[email protected]>CC Peter Zijlstra <[email protected]>CC Thomas Gleixner <[email protected]>CC Joel Fernandes <[email protected]>CC Catalin Marinas <[email protected]>CC Dave Watson <[email protected]>CC Will Deacon <[email protected]>CC Shuah Khan <[email protected]>CC Andi Kleen <[email protected]>CC "H . Peter Anvin" <[email protected]>CC Chris Lameter <[email protected]>CC Russell King <[email protected]>CC Michael Kerrisk <[email protected]>CC "Paul E . McKenney" <[email protected]>CC Paul Turner <[email protected]>CC Boqun Feng <[email protected]>CC Josh Triplett <[email protected]>CC Steven Rostedt <[email protected]>CC Ben Maurer <[email protected]>CC Andy Lutomirski <[email protected]>CC Andrew Morton <[email protected]>CC Linus Torvalds <[email protected]>--- tools/testing/selftests/rseq/rseq-arm64.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)tools/testing/selftests/rseq/rseq-arm64.h +0
-0@@ -6,7 +6,20 @@* (C) Copyright 2018 - Will Deacon <[email protected]> */
tools/testing/selftests/rseq/rseq-arm64.h line 17
Quoted 10 lines
tools/testing/selftests/rseq/rseq-arm64.h +9
-1-#define RSEQ_SIG 0xd428bc00/* BRK #0x45E0 */ +/* +* aarch64 -mbig-endian generates mixed endianness code vs data: +* little-endian code and big-endian data. Ensure the RSEQ_SIG signature +* matches code endianness. +*/ +#define RSEQ_SIG_CODE 0xd428bc00/* BRK #0x45E0. */ + +#ifdef __AARCH64EB__ +#define RSEQ_SIG_DATA 0x00bc28d4/* BRK #0x45E0. */
It would be neater to implement swab32 and use that with RSEQ_SIG_CODE, but either way:
Will
Re: [RFC PATCH for 5.2 08/10] rseq/selftests: aarch64 code signature: handle big-endian environment
Quoted 12 lines · Will Deacon
tools/testing/selftests/rseq/rseq-arm64.h line 17
Quoted 9 lines · Mathieu Desnoyers
tools/testing/selftests/rseq/rseq-arm64.h +9
-0+/* +* aarch64 -mbig-endian generates mixed endianness code vs data: +* little-endian code and big-endian data. Ensure the RSEQ_SIG signature +* matches code endianness. +*/ +#define RSEQ_SIG_CODE 0xd428bc00/* BRK #0x45E0. */ + +#ifdef __AARCH64EB__ +#define RSEQ_SIG_DATA 0x00bc28d4/* BRK #0x45E0. */ It would be neater to implement swab32 and use that with
RSEQ_SIG_CODE,
If possible, marginally neater than that would be using le32_to_cpu(RSEQ_SIG_CODE), without any ifdeffery necessary.
It looks like that's defined in tools/include/linux/kernel.h, but I'm not sure if that gets pulled into your include path.
Thanks,
Mark.
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 5.2 08/10] rseq/selftests: aarch64 code signature: handle big-endian environment
Quoted 14 lines · ----- On Apr 24, 2019, at 12:51 PM, Mark Rutland [email protected] wrote:
Quoted 12 lines · Will Deacon
tools/testing/selftests/rseq/rseq-arm64.h line 17
Quoted 9 lines · Mathieu Desnoyers
tools/testing/selftests/rseq/rseq-arm64.h +9
-0+/* +* aarch64 -mbig-endian generates mixed endianness code vs data: +* little-endian code and big-endian data. Ensure the RSEQ_SIG signature +* matches code endianness. +*/ +#define RSEQ_SIG_CODE 0xd428bc00/* BRK #0x45E0. */ + +#ifdef __AARCH64EB__ +#define RSEQ_SIG_DATA 0x00bc28d4/* BRK #0x45E0. */ It would be neater to implement swab32 and use that with
RSEQ_SIG_CODE,
Quoted 5 lines
If possible, marginally neater than that would be using
le32_to_cpu(RSEQ_SIG_CODE), without any ifdeffery necessary.It looks like that's defined in
tools/include/linux/kernel.h, but I'm not sure if that gets pulled into your include path.
Considering that those RSEQ_SIG* define will end up in public bits/rseq.h headers within glibc, I'm tempted to keep the amount of dependencies on external headers to a minimum, if it's OK with you.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 5.2 08/10] rseq/selftests: aarch64 code signature: handle big-endian environment
Quoted 19 lines · ----- On Apr 24, 2019, at 1:00 PM, Mathieu Desnoyers…
Quoted 17 lines · ----- On Apr 24, 2019, at 12:51 PM, Mark Rutland [email protected] wrote:
Quoted 12 lines · Will Deacon
tools/testing/selftests/rseq/rseq-arm64.h line 17
Quoted 9 lines · Mathieu Desnoyers
tools/testing/selftests/rseq/rseq-arm64.h +9
-0+/* +* aarch64 -mbig-endian generates mixed endianness code vs data: +* little-endian code and big-endian data. Ensure the RSEQ_SIG signature +* matches code endianness. +*/ +#define RSEQ_SIG_CODE 0xd428bc00/* BRK #0x45E0. */ + +#ifdef __AARCH64EB__ +#define RSEQ_SIG_DATA 0x00bc28d4/* BRK #0x45E0. */ It would be neater to implement swab32 and use that with
RSEQ_SIG_CODE,If possible, marginally neater than that would be using
le32_to_cpu(RSEQ_SIG_CODE), without any ifdeffery necessary.
Quoted 6 lines
Quoted 2 lines
It looks like that's defined in
tools/include/linux/kernel.h, but I'm not sure if that gets pulled into your include path.Considering that those RSEQ_SIG* define will end up in public
bits/rseq.hheaders within glibc, I'm tempted to keep the amount of dependencies on external headers to a minimum, if it's OK with you.
Also, I'm not sure the le32_to_cpu() approach would work in __ASSEMBLER__ builds.
Thanks,
Mathieu
Quoted 9 lines
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Mathieu Desnoyers
maintainerRFC PATCH 9/10 rseq/selftests: powerpc code signature: generate valid instructions
In reply to Mathieu DesnoyersUse "twui" as the guard instruction for the restartable sequence abort handler.
--- tools/testing/selftests/rseq/rseq-ppc.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
tools/testing/selftests/rseq/rseq-ppc.h +9 -1
@@ -6,7 +6,15 @@* (C) Copyright 2016-2018 - Boqun Feng <[email protected]> */ -#define RSEQ_SIG 0x53053053 +/* +* RSEQ_SIG is used with the following trap instruction: +* +* powerpc-be: 0f e5 00 0b twui r5,11 +* powerpc64-le: 0b 00 e5 0f twui r5,11 +* powerpc64-be: 0f e5 00 0b twui r5,11 +*/ + +#define RSEQ_SIG 0x0fe5000b#define rseq_smp_mb () __asm__ __volatile__ ("sync" ::: "memory", "cc")#define rseq_smp_lwsync () __asm__ __volatile__ ("lwsync" ::: "memory", "cc")
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 10/10 rseq/selftests: mips: use break instruction for RSEQ_SIG
In reply to Mathieu DesnoyersUse break as guard instruction for the restartable sequence abort handler.
Previously, the chosen signature was simply data, based on the assumption that it could always sit in a literal pool. However, some compilation environments favor disabling literal pool. Therefore, ensure the signature is a valid uncommon trap instruction.
--- tools/testing/selftests/rseq/rseq-mips.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/rseq/rseq-mips.h +5 -1
@@ -7,7 +7,11 @@* (C) Copyright 2016-2018 - Mathieu Desnoyers <[email protected]> */ -#define RSEQ_SIG 0x53053053 +/* +* RSEQ_SIG uses the break instruction. The instruction pattern is +* 0350000d break 0x350 +*/ +#define RSEQ_SIG 0x0350000d#define rseq_smp_mb () __asm__ __volatile__ ("sync" ::: "memory")#define rseq_smp_rmb () rseq_smp_mb()
Signature · 1 line
2.11.0
Re: [RFC PATCH for 5.2 10/10] rseq/selftests: mips: use break instruction for RSEQ_SIG
Hi Mathieu,
Quoted 5 lines · Mathieu Desnoyers
tools/testing/selftests/rseq/rseq-mips.h +0
-0
tools/testing/selftests/rseq/rseq-mips.h line 14
Quoted 9 lines
tools/testing/selftests/rseq/rseq-mips.h +5
-1@@ -7,7 +7,11 @@* (C) Copyright 2016-2018 - Mathieu Desnoyers <[email protected]> */ -#define RSEQ_SIG 0x53053053 +/* +* RSEQ_SIG uses the break instruction. The instruction pattern is +* 0350000d break 0x350 +*/ +#define RSEQ_SIG 0x0350000d
My apologies for taking a while to get back to you on the various ISAs & endian issues here, but I think we'll want this to be something like:
#if defined(__nanomips__)
# ifdef __MIPSEL__ # define RSEQ_SIG 0x03500010 # else # define RSEQ_SIG 0x00100350 # endif #elif defined(__mips_micromips) # ifdef __MIPSEL__ # define RSEQ_SIG 0xd4070000 # else # define RSEQ_SIG 0x0000d407 # endif #else # define RSEQ_SIG 0x0350000d #endif
For plain old MIPS the .word directive will be fine endian-wise, but for microMIPS & nanoMIPS we need to take into account that the instruction stream is encoded as 16b halfwords & swap those accordingly for little endian.
Thanks,
Paul
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 5.2 10/10] rseq/selftests: mips: use break instruction for RSEQ_SIG
Quoted 38 lines · ----- On Apr 24, 2019, at 6:06 PM, Paul Burton [email protected] wrote:
Hi Mathieu,
Quoted 6 lines · Mathieu Desnoyers
tools/testing/selftests/rseq/rseq-mips.h +0
-0tools/testing/selftests/rseq/rseq-mips.h line 14
Quoted 9 lines
tools/testing/selftests/rseq/rseq-mips.h +5
-1@@ -7,7 +7,11 @@* (C) Copyright 2016-2018 - Mathieu Desnoyers <[email protected]> */ -#define RSEQ_SIG 0x53053053 +/* +* RSEQ_SIG uses the break instruction. The instruction pattern is +* 0350000d break 0x350 +*/ +#define RSEQ_SIG 0x0350000dMy apologies for taking a while to get back to you on the various ISAs & endian issues here, but I think we'll want this to be something like:
#if
defined(__nanomips__)# ifdef __MIPSEL__ # define RSEQ_SIG 0x03500010 # else # define RSEQ_SIG 0x00100350 # endif #elif defined(__mips_micromips) # ifdef __MIPSEL__ # define RSEQ_SIG 0xd4070000 # else # define RSEQ_SIG 0x0000d407 # endif #else # define RSEQ_SIG 0x0350000d #endif
Quoted 4 lines
For plain old MIPS the .word directive will be fine endian-wise, but for microMIPS & nanoMIPS we need to take into account that the instruction stream is encoded as 16b halfwords & swap those accordingly for little endian.
Hi Paul,
Thanks for looking into it!
Does the following comment above the forest of #ifdef work for you ?
/*
* RSEQ_SIG uses the break instruction. The instruction pattern is:
*
* On MIPS:
* 0350000d break 0x350
*
* On nanoMIPS32:
* 00100350 break 0x350
*
* On microMIPS:
* 0000d407 break 0x350
*
* For nanoMIPS32 and microMIPS, the instruction stream is encoded as 16-bit
* halfwords, so the signature halfwords need to be swapped accordingly for
* little-endian.
*/
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [RFC PATCH for 5.2 10/10] rseq/selftests: mips: use break instruction for RSEQ_SIG
Hi Mathieu,
Quoted 18 lines · Mathieu Desnoyers
Does the following comment above the forest of #ifdef work for you ?
/*
*RSEQ_SIGuses the break instruction. The instruction pattern is:
*
* On MIPS:
*0350000dbreak0x350
*
* On nanoMIPS32:
* 00100350 break0x350
*
* On microMIPS:
*0000d407break0x350
*
* For nanoMIPS32 and microMIPS, the instruction stream is encoded as 16-bit
* halfwords, so the signature halfwords need to be swapped accordingly for
* little-endian.
*/
I'd probably just say nanoMIPS rather than nanoMIPS32, because when we get nanoMIPS64 in the future it'll be a superset of nanoMIPS32 & the break encoding will be the same.
But otherwise it looks good to me :)
Thanks,
Paul
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 5.2 10/10] rseq/selftests: mips: use break instruction for RSEQ_SIG
Quoted 22 lines · ----- On Apr 24, 2019, at 7:17 PM, Paul Burton [email protected] wrote:
Hi Mathieu,
Quoted 18 lines · Mathieu Desnoyers
Does the following comment above the forest of #ifdef work for you ?
/*
*RSEQ_SIGuses the break instruction. The instruction pattern is:
*
* On MIPS:
*0350000dbreak0x350
*
* On nanoMIPS32:
* 00100350 break0x350
*
* On microMIPS:
*0000d407break0x350
*
* For nanoMIPS32 and microMIPS, the instruction stream is encoded as 16-bit
* halfwords, so the signature halfwords need to be swapped accordingly for
* little-endian.
*/
Quoted 3 lines
I'd probably just say nanoMIPS rather than nanoMIPS32, because when we get nanoMIPS64 in the future it'll be a superset of nanoMIPS32 & the break encoding will be the same.
Done.
Quoted 2 lines
But otherwise it looks good to me :)
Great! I've added your "Suggested-by" tag to the patch.
Thanks,
Mathieu
Quoted 3 lines
Thanks,
Paul
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 5.2 10/10] rseq/selftests: mips: use break instruction for RSEQ_SIG
In reply to Paul BurtonQuoted 38 lines · ----- On Apr 24, 2019, at 6:06 PM, Paul Burton [email protected] wrote:
Hi Mathieu,
Quoted 6 lines · Mathieu Desnoyers
tools/testing/selftests/rseq/rseq-mips.h +0
-0tools/testing/selftests/rseq/rseq-mips.h line 14
Quoted 9 lines
tools/testing/selftests/rseq/rseq-mips.h +5
-1@@ -7,7 +7,11 @@* (C) Copyright 2016-2018 - Mathieu Desnoyers <[email protected]> */ -#define RSEQ_SIG 0x53053053 +/* +* RSEQ_SIG uses the break instruction. The instruction pattern is +* 0350000d break 0x350 +*/ +#define RSEQ_SIG 0x0350000dMy apologies for taking a while to get back to you on the various ISAs & endian issues here, but I think we'll want this to be something like:
#if
defined(__nanomips__)# ifdef __MIPSEL__ # define RSEQ_SIG 0x03500010 # else # define RSEQ_SIG 0x00100350 # endif #elif defined(__mips_micromips) # ifdef __MIPSEL__ # define RSEQ_SIG 0xd4070000 # else # define RSEQ_SIG 0x0000d407 # endif #else # define RSEQ_SIG 0x0350000d #endif
Quoted 4 lines
For plain old MIPS the .word directive will be fine endian-wise, but for microMIPS & nanoMIPS we need to take into account that the instruction stream is encoded as 16b halfwords & swap those accordingly for little endian.
Considering that we have micromips and nanomips already, I guess something along the lines of "picomips" is not that far away...
I've tried to figure out if we could find a way to have RSEQ_SIG left undefined if it's not on the plain mips environment, but could not find anything that would be #defined on plain mips, but #undefined on both micromips and nanomips.
What I'd like to do is e.g.:
#if defined(__nanomips__)
# ifdef __MIPSEL__ # define RSEQ_SIG 0x03500010 # else # define RSEQ_SIG 0x00100350 # endif #elif defined(__mips_micromips) # ifdef __MIPSEL__ # define RSEQ_SIG 0xd4070000 # else # define RSEQ_SIG 0x0000d407 # endif #elif defined(__mips__) # define RSEQ_SIG 0x0350000d #else /* Leave RSEQ_SIG as is. */ #endif
The idea here is to not allow code targeting future MIPS ISA to compile with the wrong signature.
The delta between compiling without/with -mmicromips on a gcc-8 compiler is only:
Quoted 1 line
#define
__mips_micromips1
Some interesting delta when compiling for plain little-endian mips with gcc-8 compared to the nanomips compiler is:
< #define __mips__ 1
< #define _mips 1
< #define MIPSEL 1
Quoted 1 line
#define
__nanomips__1
< #define __mips_isa_rev 2
Quoted 1 line
#define
__mips_isa_rev6
So let's say we have a picomips introduced in the future, can we rely on it not defining __mips__ like the nanomips compiler does ? If so, my "#elif defined(__mips__)" approach would indeed leave RSEQ_SIG undefined as expected.
Thoughts ?
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [RFC PATCH for 5.2 10/10] rseq/selftests: mips: use break instruction for RSEQ_SIG
Hi Mathieu,
Quoted 42 lines · Mathieu Desnoyers
I've tried to figure out if we could find a way to have
RSEQ_SIGleft undefined if it's not on the plain mips environment, but could not find anything that would be #defined on plain mips, but #undefined on both micromips and nanomips.What I'd like to do is e.g.:
#if
defined(__nanomips__)# ifdef __MIPSEL__ # define RSEQ_SIG 0x03500010 # else # define RSEQ_SIG 0x00100350 # endif #elif defined(__mips_micromips) # ifdef __MIPSEL__ # define RSEQ_SIG 0xd4070000 # else # define RSEQ_SIG 0x0000d407 # endif #elif defined(__mips__) # define RSEQ_SIG 0x0350000d #else /* Leave RSEQ_SIG as is. */ #endifThe idea here is to not allow code targeting future MIPS ISA to compile with the wrong signature.
The delta between compiling without/with -mmicromips on a gcc-8 compiler is only:
Quoted 1 line
#define
__mips_micromips1Some interesting delta when compiling for plain little-endian mips with gcc-8 compared to the nanomips compiler is:
< #define
__mips__1
< #define_mips1
< #define MIPSEL 1Quoted 1 line
#define
__nanomips__1< #define
__mips_isa_rev2
Quoted 8 lines
Quoted 1 line
#define
__mips_isa_rev6So let's say we have a picomips introduced in the future, can we rely on it not defining
__mips__like the nanomips compiler does ? If so, my "#elifdefined(__mips__)" approach would indeed leaveRSEQ_SIGundefined as expected.Thoughts ?
That seems like a reasonable approach to me. I don't think it'll be guaranteed, but it'll give the best odds of the behavior you want.
If I recall correctly the reason for not defining __mips__ in the nanoMIPS compiler was to force people to audit MIPS-specific code given the scale of the changes in nanoMIPS - there are some incompatibilities at the assembly level but more than that the ABI changes in multiple ways from register assignment & calling convention to kernel-user struct layouts & other things. If we were to build existing MIPS-specific code as-is then some of this could lead to brokenness that the tools wouldn't have a good way to detect & reject automatically, so making people audit the code & add in the __nanomips__ check is a sort of safety measure.
So the likelihood of your code above picking up on any future ISA changes will probably depend upon how incompatible they are, which seems pretty sensible.
Thanks,
Paul