Skip to contentSkip to conversation

riscv: add support for restartable sequence

PATCH Superseded Vincent Chen 3 participants 9 messages Nov 5, 2019 – Mar 9, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover riscv: add support for restartable sequence

Add RSEQ, restartable sequence, support and related selftest to RISCV. The Kconfig option HAVE_REGS_AND_STACK_ACCESS_API is also required by RSEQ because RSEQ will modify the content of pt_regs.sepc through instruction_pointer_set() during the fixup procedure. In order to select the config HAVE_REGS_AND_STACK_ACCESS_API, the missing APIs for accessing pt_regs are also added in this patch set.

The relevant RSEQ tests in kselftest require the Binutils patch "RISC-V:
Fix linker problems with TLS copy relocs" to avoid placing PREINIT_ARRAY and TLS variable of librseq.so at the same address.
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=3e7bd7f24146f162565edf878840449f36a8d974
A segmental fault will happen if the Binutils misses this patch.

Vincent Chen (3):
riscv: add required functions to enable HAVE_REGS_AND_STACK_ACCESS_API
riscv: Add support for restartable sequence rseq/selftests: Add support for riscv

 arch/riscv/Kconfig                        |   2 +
 arch/riscv/include/asm/ptrace.h           |  29 +-
 arch/riscv/kernel/entry.S                 |   4 +
 arch/riscv/kernel/ptrace.c                |  99 +++++
 arch/riscv/kernel/signal.c                |   3 +
 tools/testing/selftests/rseq/param_test.c |  23 ++
 tools/testing/selftests/rseq/rseq-riscv.h | 622 ++++++++++++++++++++++++++++++
 tools/testing/selftests/rseq/rseq.h       |   2 +
 8 files changed, 783 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/rseq/rseq-riscv.h
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/3 riscv: add required functions to enable HAVE_REGS_AND_STACK_ACCESS_API

In order to select HAVE_REGS_AND_STACK_ACCESS_API, adding the APIs required by kprobes to access pt_regs and stack entries to the RISC-V ports.

---
 arch/riscv/Kconfig              |  1 +
 arch/riscv/include/asm/ptrace.h | 29 +++++++++++-
 arch/riscv/kernel/ptrace.c      | 99 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+), 1 deletion(-)
arch/riscv/Kconfig +1 -0
index 8eebbc8860bb..d5bbf4223fd2 100644
@@ -61,6 +61,7 @@ config RISCV
 	select SPARSEMEM_STATIC if 32BIT
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
 	select HAVE_ARCH_MMAP_RND_BITS
+	select HAVE_REGS_AND_STACK_ACCESS_API
 
 config ARCH_MMAP_RND_BITS_MIN
 	default 18 if 64BIT
arch/riscv/include/asm/ptrace.h +28 -1
index d48d1e13973c..635b7b5506ec 100644
@@ -8,6 +8,7 @@
 
 #include <uapi/asm/ptrace.h>
 #include <asm/csr.h>
+#include <linux/compiler.h>
 
 #ifndef __ASSEMBLY__
 
@@ -59,7 +60,7 @@ struct pt_regs {
 #endif
 
 #define user_mode(regs) (((regs)->sstatus & SR_SPP) == 0)
-
+#define MAX_REG_OFFSET offsetof(struct pt_regs, orig_a0)
 
 /* Helpers for working with the instruction pointer */
 static inline unsigned long instruction_pointer(struct pt_regs *regs)
@@ -79,6 +80,12 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
 {
 	return regs->sp;
 }
+
+static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
+{
+	return regs->sp;
+}
+
 static inline void user_stack_pointer_set(struct pt_regs *regs,
 					  unsigned long val)
 {
@@ -101,6 +108,26 @@ static inline unsigned long regs_return_value(struct pt_regs *regs)
 	return regs->a0;
 }
 
+int regs_query_register_offset(const char *name);
+
+unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n);
+/**
+ * regs_get_register() - get register value from its offset
+ * @regs:	pt_regs from which register value is gotten
+ * @offset:	offset of the register.
+ *
+ * regs_get_register returns the value of a register whose offset from @regs.
+ * The @offset is the offset of the register in struct pt_regs.
+ * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
+ */
+static inline unsigned long regs_get_register(struct pt_regs *regs,
+					      unsigned int offset)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return 0;
+
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_RISCV_PTRACE_H */
arch/riscv/kernel/ptrace.c +99 -0

105 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 1/3] riscv: add required functions to enable HAVE_REGS_AND_STACK_ACCESS_API

Hi Vincent,

Quoted 6 lines · Vincent Chen

In order to select HAVE_REGS_AND_STACK_ACCESS_API, adding the APIs required by kprobes to access pt_regs and stack entries to the RISC-V ports.

As I understand it, this patch hasn't been signed off on by Patrick. I've sent him an E-mail asking him whether he's willing to add his Signed-off-by:, but haven't heard back from it.

From our discussions, I understand that this patch is based partially on some of his earlier, public, kprobes work. In lieu of any response from Patrick, could you please resend this patch and just note in the commit description that it's partially based on one of his patches, add a Link:
line that points to the URL of the patch that it's partially based on, and replace the Signed-off-by: with a Co-developed-by: or something similar?

thanks,

- Paul

#3 lore raw

Re: [PATCH 1/3] riscv: add required functions to enable HAVE_REGS_AND_STACK_ACCESS_API

Quoted 10 lines · Paul Walmsley

As I understand it, this patch hasn't been signed off on by Patrick. I've sent him an E-mail asking him whether he's willing to add his Signed-off-by:, but haven't heard back from it.

From our discussions, I understand that this patch is based partially on some of his earlier, public, kprobes work. In lieu of any response from Patrick, could you please resend this patch and just note in the commit description that it's partially based on one of his patches, add a Link:
line that points to the URL of the patch that it's partially based on, and replace the Signed-off-by: with a Co-developed-by: or something similar?

OK - just looked at the patches more closely, and I think I see what's going on here. This patch looks like a rebased version of this patch:

https://lore.kernel.org/linux-riscv/[email protected]/

So let's just plan to use an updated version of Patrick's original (below). Please let me know if you have any comments on it or if I missed something -

- Paul

From: Patrick Stählin <[email protected]>
Date: Tue, 13 Nov 2018 20:58:03 +0100
Subject: [PATCH] RISC-V: Implement ptrace regs and stack API
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Needed for kprobes support. Copied and adapted from arm64 code.

[[email protected]: updated to apply; fixed checkpatch issues;
fixed regs_get_register() kerneldoc]

---
 arch/riscv/Kconfig              |  1 +
 arch/riscv/include/asm/ptrace.h | 30 ++++++++++
 arch/riscv/kernel/ptrace.c      | 99 +++++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+)
arch/riscv/Kconfig +1 -0
index 8eebbc8860bb..d5bbf4223fd2 100644
@@ -61,6 +61,7 @@ config RISCV
 	select SPARSEMEM_STATIC if 32BIT
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
 	select HAVE_ARCH_MMAP_RND_BITS
+	select HAVE_REGS_AND_STACK_ACCESS_API
 
 config ARCH_MMAP_RND_BITS_MIN
 	default 18 if 64BIT
arch/riscv/include/asm/ptrace.h +30 -0
index d48d1e13973c..50d37b123a61 100644
@@ -8,6 +8,7 @@
 
 #include <uapi/asm/ptrace.h>
 #include <asm/csr.h>
+#include <linux/compiler.h>
 
 #ifndef __ASSEMBLY__
 
@@ -60,6 +61,7 @@ struct pt_regs {
 
 #define user_mode(regs) (((regs)->sstatus & SR_SPP) == 0)
 
+#define MAX_REG_OFFSET offsetof(struct pt_regs, orig_a0)
 
 /* Helpers for working with the instruction pointer */
 static inline unsigned long instruction_pointer(struct pt_regs *regs)
@@ -85,6 +87,12 @@ static inline void user_stack_pointer_set(struct pt_regs *regs,
 	regs->sp =  val;
 }
 
+/* Valid only for Kernel mode traps. */
+static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
+{
+	return regs->sp;
+}
+
 /* Helpers for working with the frame pointer */
 static inline unsigned long frame_pointer(struct pt_regs *regs)
 {
@@ -101,6 +109,28 @@ static inline unsigned long regs_return_value(struct pt_regs *regs)
 	return regs->a0;
 }
 
+int regs_query_register_offset(const char *name);
+unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
+					unsigned int n);
+
+/**
+ * regs_get_register() - get register value from its offset
+ * @regs:	pt_regs from which register value is gotten
+ * @offset:	offset of the register.
+ *
+ * regs_get_register() returns the value from @regs of a register
+ * identified by @offset.  The @offset is the offset of the register
+ * in struct pt_regs.  If @offset is bigger than MAX_REG_OFFSET, this
+ * returns 0.
+ */
+static inline unsigned long regs_get_register(struct pt_regs *regs,
+					      unsigned int offset)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return 0;
+
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_RISCV_PTRACE_H */
arch/riscv/kernel/ptrace.c +99 -0

105 lines not on this page · raw message

Signature · 1 line
2.24.0.rc0
#4 lore raw

Re: [PATCH 1/3] riscv: add required functions to enable HAVE_REGS_AND_STACK_ACCESS_API

Quoted 17 lines · Paul Walmsley
Quoted 10 lines · Paul Walmsley

As I understand it, this patch hasn't been signed off on by Patrick. I've sent him an E-mail asking him whether he's willing to add his Signed-off-by:, but haven't heard back from it.

From our discussions, I understand that this patch is based partially on some of his earlier, public, kprobes work. In lieu of any response from Patrick, could you please resend this patch and just note in the commit description that it's partially based on one of his patches, add a Link:
line that points to the URL of the patch that it's partially based on, and replace the Signed-off-by: with a Co-developed-by: or something similar?

OK - just looked at the patches more closely, and I think I see what's going on here. This patch looks like a rebased version of this patch:

Quoted 9 lines

https://lore.kernel.org/linux-riscv/[email protected]/

So let's just plan to use an updated version of Patrick's original (below). Please let me know if you have any comments on it or if I missed something -

- Paul

Dear Paul,
Due to the rule of mail classification, I do not find this mail in time. I am sorry for that.
I think it is good for me.

Thanks and regards
Vincent

Quoted 208 lines

From: Patrick Stählin <[email protected]>
Date: Tue, 13 Nov 2018 20:58:03 +0100
Subject: [PATCH] RISC-V: Implement ptrace regs and stack API
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Needed for kprobes support. Copied and adapted from arm64 code.

[[email protected]: updated to apply; fixed checkpatch issues;
fixed regs_get_register() kerneldoc]

---
 arch/riscv/Kconfig              |  1 +
 arch/riscv/include/asm/ptrace.h | 30 ++++++++++
 arch/riscv/kernel/ptrace.c      | 99 +++++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+)
arch/riscv/Kconfig +1 -0
index 8eebbc8860bb..d5bbf4223fd2 100644
@@ -61,6 +61,7 @@ config RISCV
        select SPARSEMEM_STATIC if 32BIT
        select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
        select HAVE_ARCH_MMAP_RND_BITS
+       select HAVE_REGS_AND_STACK_ACCESS_API
 
 config ARCH_MMAP_RND_BITS_MIN
        default 18 if 64BIT
arch/riscv/include/asm/ptrace.h +30 -0
index d48d1e13973c..50d37b123a61 100644
@@ -8,6 +8,7 @@
 
 #include <uapi/asm/ptrace.h>
 #include <asm/csr.h>
+#include <linux/compiler.h>
 
 #ifndef __ASSEMBLY__
 
@@ -60,6 +61,7 @@ struct pt_regs {
 
 #define user_mode(regs) (((regs)->sstatus & SR_SPP) == 0)
 
+#define MAX_REG_OFFSET offsetof(struct pt_regs, orig_a0)
 
 /* Helpers for working with the instruction pointer */
 static inline unsigned long instruction_pointer(struct pt_regs *regs)
@@ -85,6 +87,12 @@ static inline void user_stack_pointer_set(struct pt_regs *regs,
        regs->sp =  val;
 }
 
+/* Valid only for Kernel mode traps. */
+static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
+{
+       return regs->sp;
+}
+
 /* Helpers for working with the frame pointer */
 static inline unsigned long frame_pointer(struct pt_regs *regs)
 {
@@ -101,6 +109,28 @@ static inline unsigned long regs_return_value(struct pt_regs *regs)
        return regs->a0;
 }
 
+int regs_query_register_offset(const char *name);
+unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
+                                       unsigned int n);
+
+/**
+ * regs_get_register() - get register value from its offset
+ * @regs:      pt_regs from which register value is gotten
+ * @offset:    offset of the register.
+ *
+ * regs_get_register() returns the value from @regs of a register
+ * identified by @offset.  The @offset is the offset of the register
+ * in struct pt_regs.  If @offset is bigger than MAX_REG_OFFSET, this
+ * returns 0.
+ */
+static inline unsigned long regs_get_register(struct pt_regs *regs,
+                                             unsigned int offset)
+{
+       if (unlikely(offset > MAX_REG_OFFSET))
+               return 0;
+
+       return *(unsigned long *)((unsigned long)regs + offset);
+}
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_RISCV_PTRACE_H */
arch/riscv/kernel/ptrace.c +99 -1
index 1252113ef8b2..5076b30fe18b 100644
@@ -125,6 +125,105 @@ const struct user_regset_view *task_user_regset_view(struct task_struct *task)
        return &riscv_user_native_view;
 }
 
+struct pt_regs_offset {
+       const char *name;
+       int offset;
+};
+
+#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
+#define REG_OFFSET_END {.name = NULL, .offset = 0}
+
+static const struct pt_regs_offset regoffset_table[] = {
+       REG_OFFSET_NAME(sepc),
+       REG_OFFSET_NAME(ra),
+       REG_OFFSET_NAME(sp),
+       REG_OFFSET_NAME(gp),
+       REG_OFFSET_NAME(tp),
+       REG_OFFSET_NAME(t0),
+       REG_OFFSET_NAME(t1),
+       REG_OFFSET_NAME(t2),
+       REG_OFFSET_NAME(s0),
+       REG_OFFSET_NAME(s1),
+       REG_OFFSET_NAME(a0),
+       REG_OFFSET_NAME(a1),
+       REG_OFFSET_NAME(a2),
+       REG_OFFSET_NAME(a3),
+       REG_OFFSET_NAME(a4),
+       REG_OFFSET_NAME(a5),
+       REG_OFFSET_NAME(a6),
+       REG_OFFSET_NAME(a7),
+       REG_OFFSET_NAME(s2),
+       REG_OFFSET_NAME(s3),
+       REG_OFFSET_NAME(s4),
+       REG_OFFSET_NAME(s5),
+       REG_OFFSET_NAME(s6),
+       REG_OFFSET_NAME(s7),
+       REG_OFFSET_NAME(s8),
+       REG_OFFSET_NAME(s9),
+       REG_OFFSET_NAME(s10),
+       REG_OFFSET_NAME(s11),
+       REG_OFFSET_NAME(t3),
+       REG_OFFSET_NAME(t4),
+       REG_OFFSET_NAME(t5),
+       REG_OFFSET_NAME(t6),
+       REG_OFFSET_NAME(sstatus),
+       REG_OFFSET_NAME(sbadaddr),
+       REG_OFFSET_NAME(scause),
+       REG_OFFSET_NAME(orig_a0),
+       REG_OFFSET_END,
+};
+
+/**
+ * regs_query_register_offset() - query register offset from its name
+ * @name:      the name of a register
+ *
+ * regs_query_register_offset() returns the offset of a register in struct
+ * pt_regs from its name. If the name is invalid, this returns -EINVAL;
+ */
+int regs_query_register_offset(const char *name)
+{
+       const struct pt_regs_offset *roff;
+
+       for (roff = regoffset_table; roff->name; roff++)
+               if (!strcmp(roff->name, name))
+                       return roff->offset;
+       return -EINVAL;
+}
+
+/**
+ * regs_within_kernel_stack() - check the address in the stack
+ * @regs:      pt_regs which contains kernel stack pointer.
+ * @addr:      address which is checked.
+ *
+ * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
+ * If @addr is within the kernel stack, it returns true. If not, returns false.
+ */
+static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
+{
+       return (addr & ~(THREAD_SIZE - 1))  ==
+               (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
+}
+
+/**
+ * regs_get_kernel_stack_nth() - get Nth entry of the stack
+ * @regs:      pt_regs which contains kernel stack pointer.
+ * @n:         stack entry number.
+ *
+ * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
+ * is specified by @regs. If the @n th entry is NOT in the kernel stack,
+ * this returns 0.
+ */
+unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
+{
+       unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
+
+       addr += n;
+       if (regs_within_kernel_stack(regs, (unsigned long)addr))
+               return *addr;
+       else
+               return 0;
+}
+
 void ptrace_disable(struct task_struct *child)
 {
        clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
--

2.24.0.rc0

#5 lore raw

PATCH 2/3 riscv: Add support for restartable sequence

In reply to Vincent Chen

Add calls to rseq_signal_deliver(), rseq_handle_notify_resume() and rseq_syscall() to introduce RSEQ support.

1. Call the rseq_handle_notify_resume() function on return to userspace if TIF_NOTIFY_RESUME thread flag is set.

2. Call the rseq_signal_deliver() function to fixup on the pre-signal frame when a signal is delivered on top of a restartable sequence critical section.

3. Check that system calls are not invoked from within rseq critical sections by invoking rseq_signal() from ret_from_syscall(). With CONFIG_DEBUG_RSEQ, such behavior results in termination of the process with SIGSEGV.

---
 arch/riscv/Kconfig         | 1 +
 arch/riscv/kernel/entry.S  | 4 ++++
 arch/riscv/kernel/signal.c | 3 +++
 3 files changed, 8 insertions(+)
arch/riscv/Kconfig +1 -0
index d5bbf4223fd2..58759a4f8dff 100644
@@ -62,6 +62,7 @@ config RISCV
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
 	select HAVE_ARCH_MMAP_RND_BITS
 	select HAVE_REGS_AND_STACK_ACCESS_API
+	select HAVE_RSEQ
 
 config ARCH_MMAP_RND_BITS_MIN
 	default 18 if 64BIT
arch/riscv/kernel/entry.S +4 -0
index 8ca479831142..00e9eaa02969 100644
@@ -212,6 +212,10 @@ ENTRY(handle_exception)
 handle_syscall:
 	 /* save the initial A0 value (needed in signal handlers) */
 	REG_S a0, PT_ORIG_A0(sp)
+#ifdef CONFIG_RSEQ_DEBUG
+	move a0, sp
+	call rseq_syscall
+#endif
 	/*
 	 * Advance SEPC to avoid executing the original
 	 * scall instruction on sret
arch/riscv/kernel/signal.c +3 -0
index d0f6f212f5df..f2f6017f92d0 100644
@@ -219,6 +219,8 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
 	sigset_t *oldset = sigmask_to_save();
 	int ret;
 
+	rseq_signal_deliver(ksig, regs);
+
 	/* Are we from a system call? */
 	if (regs->scause == EXC_SYSCALL) {
 		/* Avoid additional syscall restarting via ret_from_exception */
@@ -302,5 +304,6 @@ asmlinkage __visible void do_notify_resume(struct pt_regs *regs,
 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
 		clear_thread_flag(TIF_NOTIFY_RESUME);
 		tracehook_notify_resume(regs);
+		rseq_handle_notify_resume(NULL, regs);
 	}
 }
Signature · 1 line
2.7.4
#6 lore raw

PATCH 3/3 rseq/selftests: Add support for riscv

In reply to Vincent Chen

Add support for risc-v in the rseq selftests, which covers both 64-bit and 32-bit ISA with little endian mode.

---
 tools/testing/selftests/rseq/param_test.c |  23 ++
 tools/testing/selftests/rseq/rseq-riscv.h | 622 ++++++++++++++++++++++++++++++
 tools/testing/selftests/rseq/rseq.h       |   2 +
 3 files changed, 647 insertions(+)
 create mode 100644 tools/testing/selftests/rseq/rseq-riscv.h
tools/testing/selftests/rseq/param_test.c +23 -0
index eec2663261f2..5e4e7e8fb9c2 100644
@@ -205,6 +205,29 @@ unsigned int yield_mod_cnt, nr_abort;
 	"addiu " INJECT_ASM_REG ", -1\n\t" \
 	"bnez " INJECT_ASM_REG ", 222b\n\t" \
 	"333:\n\t"
+#elif defined(__riscv)
+
+#define RSEQ_INJECT_INPUT \
+	, [loop_cnt_1]"m"(loop_cnt[1]) \
+	, [loop_cnt_2]"m"(loop_cnt[2]) \
+	, [loop_cnt_3]"m"(loop_cnt[3]) \
+	, [loop_cnt_4]"m"(loop_cnt[4]) \
+	, [loop_cnt_5]"m"(loop_cnt[5]) \
+	, [loop_cnt_6]"m"(loop_cnt[6])
+
+#define INJECT_ASM_REG	"t1"
+
+#define RSEQ_INJECT_CLOBBER \
+	, INJECT_ASM_REG
+
+#define RSEQ_INJECT_ASM(n)					\
+	"lw " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t"		\
+	"beqz " INJECT_ASM_REG ", 333f\n\t"			\
+	"222:\n\t"						\
+	"addi  " INJECT_ASM_REG "," INJECT_ASM_REG ", -1\n\t"	\
+	"bnez " INJECT_ASM_REG ", 222b\n\t"			\
+	"333:\n\t"
+
 
 #else
 #error unsupported target
tools/testing/selftests/rseq/rseq-riscv.h new file +622 -0

622 lines not on this page · raw message

tools/testing/selftests/rseq/rseq.h +2 -0
index d40d60e7499e..bc68a6127285 100644
@@ -79,6 +79,8 @@ extern int __rseq_handled;
 #include <rseq-mips.h>
 #elif defined(__s390__)
 #include <rseq-s390.h>
+#elif defined(__riscv)
+#include <rseq-riscv.h>
 #else
 #error unsupported target
 #endif
Signature · 1 line
2.7.4
#7 lore raw
In reply to Vincent Chen
Quoted 20 lines · On Mon, 04 Nov 2019 17:58:31 PST (-0800), [email protected] wrote:

Add RSEQ, restartable sequence, support and related selftest to RISCV. The Kconfig option HAVE_REGS_AND_STACK_ACCESS_API is also required by RSEQ because RSEQ will modify the content of pt_regs.sepc through instruction_pointer_set() during the fixup procedure. In order to select the config HAVE_REGS_AND_STACK_ACCESS_API, the missing APIs for accessing pt_regs are also added in this patch set.

The relevant RSEQ tests in kselftest require the Binutils patch "RISC-V:
Fix linker problems with TLS copy relocs" to avoid placing PREINIT_ARRAY and TLS variable of librseq.so at the same address.
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=3e7bd7f24146f162565edf878840449f36a8d974
A segmental fault will happen if the Binutils misses this patch.

Vincent Chen (3):
riscv: add required functions to enable HAVE_REGS_AND_STACK_ACCESS_API
riscv: Add support for restartable sequence rseq/selftests: Add support for riscv

Quoted 10 lines
 arch/riscv/Kconfig                        |   2 +
 arch/riscv/include/asm/ptrace.h           |  29 +-
 arch/riscv/kernel/entry.S                 |   4 +
 arch/riscv/kernel/ptrace.c                |  99 +++++
 arch/riscv/kernel/signal.c                |   3 +
 tools/testing/selftests/rseq/param_test.c |  23 ++
 tools/testing/selftests/rseq/rseq-riscv.h | 622 ++++++++++++++++++++++++++++++
 tools/testing/selftests/rseq/rseq.h       |   2 +
 8 files changed, 783 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/rseq/rseq-riscv.h

This, with Paul's updated patch 1, isn't building on my end:

In file included from <command-line>:
arch/riscv/kernel/ptrace.c:137:18: error: ‘struct pt_regs’ has no member named ‘sepc’; did you mean ‘epc’?
REG_OFFSET_NAME(sepc),

                 ^~~~
././include/linux/compiler_types.h:129:57: note: in definition of macro ‘__compiler_offsetof’
#define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
                                                        ^
arch/riscv/kernel/ptrace.c:133:51: note: in expansion of macro ‘offsetof’
#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
                                                  ^~~~~~~~

arch/riscv/kernel/ptrace.c:137:2: note: in expansion of macro ‘REG_OFFSET_NAME’
REG_OFFSET_NAME(sepc),
^~~~~~~~~~~~~~~
arch/riscv/kernel/ptrace.c:169:18: error: ‘struct pt_regs’ has no member named ‘sstatus’; did you mean ‘status’?
REG_OFFSET_NAME(sstatus),

                 ^~~~~~~
././include/linux/compiler_types.h:129:57: note: in definition of macro ‘__compiler_offsetof’
#define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
                                                        ^
arch/riscv/kernel/ptrace.c:133:51: note: in expansion of macro ‘offsetof’
#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
                                                  ^~~~~~~~

arch/riscv/kernel/ptrace.c:169:2: note: in expansion of macro ‘REG_OFFSET_NAME’
REG_OFFSET_NAME(sstatus),
^~~~~~~~~~~~~~~
arch/riscv/kernel/ptrace.c:170:18: error: ‘struct pt_regs’ has no member named ‘sbadaddr’; did you mean ‘badaddr’?
REG_OFFSET_NAME(sbadaddr),

                 ^~~~~~~~
././include/linux/compiler_types.h:129:57: note: in definition of macro ‘__compiler_offsetof’
#define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
                                                        ^
arch/riscv/kernel/ptrace.c:133:51: note: in expansion of macro ‘offsetof’
#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
                                                  ^~~~~~~~

arch/riscv/kernel/ptrace.c:170:2: note: in expansion of macro ‘REG_OFFSET_NAME’
REG_OFFSET_NAME(sbadaddr),
^~~~~~~~~~~~~~~
arch/riscv/kernel/ptrace.c:171:18: error: ‘struct pt_regs’ has no member named ‘scause’; did you mean ‘cause’?
REG_OFFSET_NAME(scause),

                 ^~~~~~
././include/linux/compiler_types.h:129:57: note: in definition of macro ‘__compiler_offsetof’
#define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
                                                        ^
arch/riscv/kernel/ptrace.c:133:51: note: in expansion of macro ‘offsetof’
#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
                                                  ^~~~~~~~

arch/riscv/kernel/ptrace.c:171:2: note: in expansion of macro ‘REG_OFFSET_NAME’
REG_OFFSET_NAME(scause),

#8 lore raw
Quoted 81 lines · Palmer Dabbelt
Quoted 20 lines · On Mon, 04 Nov 2019 17:58:31 PST (-0800), [email protected] wrote:

Add RSEQ, restartable sequence, support and related selftest to RISCV. The Kconfig option HAVE_REGS_AND_STACK_ACCESS_API is also required by RSEQ because RSEQ will modify the content of pt_regs.sepc through instruction_pointer_set() during the fixup procedure. In order to select the config HAVE_REGS_AND_STACK_ACCESS_API, the missing APIs for accessing pt_regs are also added in this patch set.

The relevant RSEQ tests in kselftest require the Binutils patch "RISC-V:
Fix linker problems with TLS copy relocs" to avoid placing PREINIT_ARRAY and TLS variable of librseq.so at the same address.
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=3e7bd7f24146f162565edf878840449f36a8d974
A segmental fault will happen if the Binutils misses this patch.

Vincent Chen (3):
riscv: add required functions to enable HAVE_REGS_AND_STACK_ACCESS_API
riscv: Add support for restartable sequence rseq/selftests: Add support for riscv

Quoted 10 lines
 arch/riscv/Kconfig                        |   2 +
 arch/riscv/include/asm/ptrace.h           |  29 +-
 arch/riscv/kernel/entry.S                 |   4 +
 arch/riscv/kernel/ptrace.c                |  99 +++++
 arch/riscv/kernel/signal.c                |   3 +
 tools/testing/selftests/rseq/param_test.c |  23 ++
 tools/testing/selftests/rseq/rseq-riscv.h | 622 ++++++++++++++++++++++++++++++
 tools/testing/selftests/rseq/rseq.h       |   2 +
 8 files changed, 783 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/rseq/rseq-riscv.h

This, with Paul's updated patch 1, isn't building on my end:

In file included from <command-line>:
arch/riscv/kernel/ptrace.c:137:18: error: ‘struct pt_regs’ has no member named ‘sepc’; did you mean ‘epc’?
REG_OFFSET_NAME(sepc),

                  ^~~~
././include/linux/compiler_types.h:129:57: note: in definition of macro ‘__compiler_offsetof’
 #define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
                                                         ^
arch/riscv/kernel/ptrace.c:133:51: note: in expansion of macro ‘offsetof’
 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
                                                   ^~~~~~~~

arch/riscv/kernel/ptrace.c:137:2: note: in expansion of macro ‘REG_OFFSET_NAME’
REG_OFFSET_NAME(sepc),
^~~~~~~~~~~~~~~
arch/riscv/kernel/ptrace.c:169:18: error: ‘struct pt_regs’ has no member named ‘sstatus’; did you mean ‘status’?
REG_OFFSET_NAME(sstatus),

                  ^~~~~~~
././include/linux/compiler_types.h:129:57: note: in definition of macro ‘__compiler_offsetof’
 #define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
                                                         ^
arch/riscv/kernel/ptrace.c:133:51: note: in expansion of macro ‘offsetof’
 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
                                                   ^~~~~~~~

arch/riscv/kernel/ptrace.c:169:2: note: in expansion of macro ‘REG_OFFSET_NAME’
REG_OFFSET_NAME(sstatus),
^~~~~~~~~~~~~~~
arch/riscv/kernel/ptrace.c:170:18: error: ‘struct pt_regs’ has no member named ‘sbadaddr’; did you mean ‘badaddr’?
REG_OFFSET_NAME(sbadaddr),

                  ^~~~~~~~
././include/linux/compiler_types.h:129:57: note: in definition of macro ‘__compiler_offsetof’
 #define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
                                                         ^
arch/riscv/kernel/ptrace.c:133:51: note: in expansion of macro ‘offsetof’
 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
                                                   ^~~~~~~~

arch/riscv/kernel/ptrace.c:170:2: note: in expansion of macro ‘REG_OFFSET_NAME’
REG_OFFSET_NAME(sbadaddr),
^~~~~~~~~~~~~~~
arch/riscv/kernel/ptrace.c:171:18: error: ‘struct pt_regs’ has no member named ‘scause’; did you mean ‘cause’?
REG_OFFSET_NAME(scause),

                  ^~~~~~
././include/linux/compiler_types.h:129:57: note: in definition of macro ‘__compiler_offsetof’
 #define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
                                                         ^
arch/riscv/kernel/ptrace.c:133:51: note: in expansion of macro ‘offsetof’
 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
                                                   ^~~~~~~~
Quoted 2 lines

arch/riscv/kernel/ptrace.c:171:2: note: in expansion of macro ‘REG_OFFSET_NAME’
REG_OFFSET_NAME(scause),

Sorry, I really missed this email. I will fix these errors and resend this patch set. Thank you