[RFC PATCH v1 08/11] riscv: unwind_user: Support Minimal DWARF CFI and FP user unwinding

From: Ian Rogers

Date: Sun Nov 22 2026 - 18:59:42 EST


Wire up CONFIG_HAVE_UNWIND_USER_CFI and CONFIG_HAVE_UNWIND_USER_FP on
MMU-enabled RISC-V kernels by adding arch/riscv/include/asm/unwind_user.h
and unwind_user_sigreturn():

- Define DWARF CFI register mappings for RV64 and RV32/compat user
tasks (sp=2, fp/s0=8, ra=1).
- Provide unwind_user_link_register() reading regs->ra for leaf-frame
unwinding and ARCH_INIT_USER_FP_FRAME matching the RISC-V psABI
frame layout (CFA = s0, RA at s0 - ws, saved s0 at s0 - 2*ws).
- Implement unwind_user_sigreturn() (and compat_unwind_user_sigreturn())
to recover pc, ra, sp, and s0 from rt_sigframe.uc.uc_mcontext.sc_regs
across __vdso_rt_sigreturn (whose vDSO FDE omits register rules so
libgcc takes its instruction-matching sigcontext fallback), supporting
interrupted leaf functions, -fomit-frame-pointer frames, and
SA_ONSTACK alternate signal stacks.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
arch/riscv/Kconfig | 2 +
arch/riscv/include/asm/signal32.h | 8 +++
arch/riscv/include/asm/unwind_user.h | 81 ++++++++++++++++++++++++++++
arch/riscv/kernel/compat_signal.c | 44 +++++++++++++++
arch/riscv/kernel/signal.c | 62 +++++++++++++++++++++
5 files changed, 197 insertions(+)
create mode 100644 arch/riscv/include/asm/unwind_user.h

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index e7891a2a89da..84b61c755db3 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -202,6 +202,8 @@ config RISCV
select HAVE_SAMPLE_FTRACE_DIRECT_MULTI
select HAVE_STACKPROTECTOR
select HAVE_SYSCALL_TRACEPOINTS
+ select HAVE_UNWIND_USER_CFI if MMU
+ select HAVE_UNWIND_USER_FP if MMU
select HOTPLUG_PARALLEL if HOTPLUG_CPU
select IRQ_DOMAIN
select IRQ_FORCED_THREADING
diff --git a/arch/riscv/include/asm/signal32.h b/arch/riscv/include/asm/signal32.h
index 96dc56932e76..2c3e8eae56e5 100644
--- a/arch/riscv/include/asm/signal32.h
+++ b/arch/riscv/include/asm/signal32.h
@@ -3,9 +3,12 @@
#ifndef __ASM_SIGNAL32_H
#define __ASM_SIGNAL32_H

+struct unwind_user_state;
+
#if IS_ENABLED(CONFIG_COMPAT)
int compat_setup_rt_frame(struct ksignal *ksig, sigset_t *set,
struct pt_regs *regs);
+bool compat_unwind_user_sigreturn(struct unwind_user_state *state);
#else
static inline
int compat_setup_rt_frame(struct ksignal *ksig, sigset_t *set,
@@ -13,6 +16,11 @@ int compat_setup_rt_frame(struct ksignal *ksig, sigset_t *set,
{
return -1;
}
+static inline
+bool compat_unwind_user_sigreturn(struct unwind_user_state *state)
+{
+ return false;
+}
#endif

#endif
diff --git a/arch/riscv/include/asm/unwind_user.h b/arch/riscv/include/asm/unwind_user.h
new file mode 100644
index 000000000000..a2c2068df4ad
--- /dev/null
+++ b/arch/riscv/include/asm/unwind_user.h
@@ -0,0 +1,81 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASM_RISCV_UNWIND_USER_H
+#define _ASM_RISCV_UNWIND_USER_H
+
+#ifdef CONFIG_UNWIND_USER
+
+#include <linux/compat.h>
+#include <linux/unwind_dwarf_cfi.h>
+#include <linux/unwind_user_types.h>
+#include <asm/ptrace.h>
+
+/**
+ * unwind_user_word_size() - Return the user-space word size in bytes on RISC-V
+ * @regs: User-mode register state.
+ *
+ * Return: 4 for RV32 compat user tasks, or sizeof(unsigned long) otherwise.
+ */
+static inline int unwind_user_word_size(struct pt_regs *regs)
+{
+ if (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())
+ return 4;
+ return sizeof(unsigned long);
+}
+
+/**
+ * unwind_user_link_register() - Fetch the user link register (ra/x1) from @regs
+ * @regs: User-mode register state.
+ * @ra: Output link register value.
+ *
+ * Return: Always true on RISC-V.
+ */
+static inline bool unwind_user_link_register(struct pt_regs *regs,
+ unsigned long *ra)
+{
+ *ra = regs->ra;
+ return true;
+}
+#define unwind_user_link_register unwind_user_link_register
+
+/**
+ * unwind_user_cfi_arch() - Populate RISC-V DWARF register numbers
+ * @ws: User word size in bytes (4 for RV32, 8 for RV64).
+ * @arch: Output DWARF CFI architecture descriptor.
+ *
+ * Return: true if @ws is 4 or 8, false otherwise.
+ */
+static inline bool unwind_user_cfi_arch(unsigned int ws,
+ struct dwarf_cfi_arch *arch)
+{
+ if (ws == 8 || ws == 4) {
+ *arch = (struct dwarf_cfi_arch){
+ .ws = (u8)ws,
+ .sp_reg = 2, /* sp (x2) */
+ .fp_reg = 8, /* s0/fp (x8) */
+ .ra_reg = 1, /* ra (x1) */
+ };
+ return true;
+ }
+ return false;
+}
+
+bool unwind_user_sigreturn(struct unwind_user_state *state);
+#define unwind_user_sigreturn unwind_user_sigreturn
+
+#endif /* CONFIG_UNWIND_USER */
+
+#ifdef CONFIG_HAVE_UNWIND_USER_FP
+
+/*
+ * Standard RISC-V psABI frame record stores {saved_s0, saved_ra} at
+ * [s0 - 2 * ws, s0), with s0 pointing to the caller's stack pointer (CFA).
+ */
+#define ARCH_INIT_USER_FP_FRAME(ws) \
+ .cfa_off = 0, \
+ .ra_off = -1 * (ws), \
+ .fp_off = -2 * (ws), \
+ .use_fp = true,
+
+#endif /* CONFIG_HAVE_UNWIND_USER_FP */
+
+#endif /* _ASM_RISCV_UNWIND_USER_H */
diff --git a/arch/riscv/kernel/compat_signal.c b/arch/riscv/kernel/compat_signal.c
index cf3eb33a11e4..d549c3f46ae8 100644
--- a/arch/riscv/kernel/compat_signal.c
+++ b/arch/riscv/kernel/compat_signal.c
@@ -5,6 +5,7 @@
#include <linux/uaccess.h>
#include <linux/syscalls.h>
#include <linux/linkage.h>
+#include <linux/unwind_user.h>

#include <asm/csr.h>
#include <asm/signal32.h>
@@ -243,3 +244,46 @@ int compat_setup_rt_frame(struct ksignal *ksig, sigset_t *set,

return 0;
}
+
+#ifdef CONFIG_UNWIND_USER
+/**
+ * compat_unwind_user_sigreturn() - Unwind an RV32 compat vDSO rt_sigreturn frame
+ * @state: Current user unwind state.
+ *
+ * Return: true if @state was at __compat_vdso_rt_sigreturn and advanced to the
+ * interrupted 32-bit user context, false otherwise.
+ */
+bool compat_unwind_user_sigreturn(struct unwind_user_state *state)
+{
+ struct mm_struct *mm = current->mm;
+ struct compat_rt_sigframe __user *frame;
+ struct compat_user_regs_struct cregs;
+ unsigned long sigret;
+
+ if (!mm || !mm->context.vdso || (state->sp & 3UL))
+ return false;
+
+ sigret = (unsigned long)COMPAT_VDSO_SYMBOL(mm->context.vdso,
+ rt_sigreturn);
+ if (state->ip != sigret &&
+ !(state->topmost && state->ip == sigret + 4))
+ return false;
+
+ frame = (struct compat_rt_sigframe __user *)state->sp;
+ if (copy_from_user(&cregs, &frame->uc.uc_mcontext.sc_regs,
+ offsetofend(struct compat_user_regs_struct, s0)))
+ return false;
+
+ if ((cregs.sp & 3U) || !cregs.pc)
+ return false;
+
+ state->ip = cregs.pc;
+ state->ra = cregs.ra;
+ state->sp = cregs.sp;
+ state->fp = cregs.s0;
+ state->ra_valid = true;
+ state->signal = true;
+ state->topmost = false;
+ return true;
+}
+#endif
diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c
index 59784dc117e4..6472c645914d 100644
--- a/arch/riscv/kernel/signal.c
+++ b/arch/riscv/kernel/signal.c
@@ -13,6 +13,7 @@
#include <linux/resume_user_mode.h>
#include <linux/linkage.h>
#include <linux/entry-common.h>
+#include <linux/unwind_user.h>

#include <asm/ucontext.h>
#include <asm/vdso.h>
@@ -587,3 +588,64 @@ bool sigaltstack_size_valid(size_t ss_size)
return ss_size > get_rt_frame_size(false);
}
#endif /* CONFIG_DYNAMIC_SIGFRAME */
+
+#ifdef CONFIG_UNWIND_USER
+/**
+ * unwind_user_sigreturn() - Unwind a RISC-V rt_sigreturn user signal frame
+ * @state: Current user unwind state.
+ *
+ * __vdso_rt_sigreturn carries an empty .cfi_signal_frame FDE without register
+ * rules so user-space unwinders such as libgcc take their instruction-matching
+ * sigcontext fallback. Recover pc, ra, sp, and s0 directly from
+ * &rt_sigframe.uc.uc_mcontext.sc_regs so interrupted leaf functions,
+ * -fomit-frame-pointer code, and alternate signal stacks unwind cleanly.
+ *
+ * Return: true if @state was at the rt_sigreturn trampoline and advanced to
+ * the interrupted user context, false otherwise.
+ */
+bool unwind_user_sigreturn(struct unwind_user_state *state)
+{
+ struct mm_struct *mm = current->mm;
+ struct rt_sigframe __user *frame;
+ struct user_regs_struct sc_regs;
+ unsigned long sigret;
+
+ if (!mm)
+ return false;
+
+ if (IS_ENABLED(CONFIG_COMPAT) && state->ws == 4)
+ return compat_unwind_user_sigreturn(state);
+
+ if (state->ws != sizeof(unsigned long) ||
+ (state->sp & (sizeof(unsigned long) - 1)))
+ return false;
+
+ frame = (struct rt_sigframe __user *)state->sp;
+#ifdef CONFIG_MMU
+ if (!mm->context.vdso)
+ return false;
+ sigret = (unsigned long)VDSO_SYMBOL(mm->context.vdso, rt_sigreturn);
+#else
+ sigret = (unsigned long)&frame->sigreturn_code;
+#endif
+ if (state->ip != sigret &&
+ !(state->topmost && state->ip == sigret + 4))
+ return false;
+
+ if (copy_from_user(&sc_regs, &frame->uc.uc_mcontext.sc_regs,
+ offsetofend(struct user_regs_struct, s0)))
+ return false;
+
+ if ((sc_regs.sp & (sizeof(unsigned long) - 1)) || !sc_regs.pc)
+ return false;
+
+ state->ip = sc_regs.pc;
+ state->ra = sc_regs.ra;
+ state->sp = sc_regs.sp;
+ state->fp = sc_regs.s0;
+ state->ra_valid = true;
+ state->signal = true;
+ state->topmost = false;
+ return true;
+}
+#endif
--
2.56.1.750.gf70dc682cf-goog