Generic Linux architectural discussions
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	 linux-arch@vger.kernel.org
Cc: jpoimboe@kernel.org, rostedt@goodmis.org, peterz@infradead.org,
	 mingo@redhat.com, tglx@kernel.org, bp@alien8.de,
	dave.hansen@linux.intel.com,  x86@kernel.org,
	catalin.marinas@arm.com, will@kernel.org,  mark.rutland@arm.com,
	linux-arm-kernel@lists.infradead.org, pjw@kernel.org,
	 palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr,
	 linux-riscv@lists.infradead.org, acme@kernel.org,
	namhyung@kernel.org,  linux-perf-users@vger.kernel.org,
	mhiramat@kernel.org, ast@kernel.org,  daniel@iogearbox.net,
	andrii@kernel.org, bpf@vger.kernel.org,  nathan@kernel.org,
	llvm@lists.linux.dev, nsc@kernel.org,
	 linux-kbuild@vger.kernel.org, ojeda@kernel.org,
	 rust-for-linux@vger.kernel.org, ardb@kernel.org,
	linux-efi@vger.kernel.org,  maz@kernel.org, oupton@kernel.org,
	kvmarm@lists.linux.dev, shuah@kernel.org,
	 linux-kselftest@vger.kernel.org, indu.bhagat@oracle.com,
	jremus@linux.ibm.com,  wnliu@google.com, dylanbhatch@google.com,
	Ian Rogers <irogers@google.com>
Subject: [RFC PATCH v2 14/30] arm64: unwind_user: Support Minimal DWARF CFI and FP user unwinding
Date: Tue, 24 Nov 2026 22:15:07 -0800	[thread overview]
Message-ID: <20261125061523.1540234-15-irogers@google.com> (raw)
In-Reply-To: <20261125061523.1540234-1-irogers@google.com>

Wire up CONFIG_HAVE_UNWIND_USER_CFI and CONFIG_HAVE_UNWIND_USER_FP on
arm64 by providing arch/arm64/include/asm/unwind_user.h and
unwind_user_sigreturn():

 - Populate DWARF CFI register mappings for both 64-bit AArch64
   (ws=8, sp=31, fp=29, lr=30) and 32-bit AArch32 compat tasks
   (ws=4, sp=13, fp=11, lr=14).
 - Expose the user frame pointer (x29 / compat_fp) and link register
   (x30 / compat_lr) via unwind_user_frame_pointer() and
   unwind_user_link_register(), and override unwind_user_fp_sets_sp() to
   return false because AArch64 frame records live at the bottom of the
   callee's stack frame rather than at caller_sp - 16.
 - Implement unwind_user_sigreturn() in arch/arm64/kernel/signal.c to
   recover pc, sp, x29, and x30 directly from rt_sigframe.uc.uc_mcontext
   at __kernel_rt_sigreturn when state->sp_valid is true (falling back
   to user->next_frame via FP otherwise), preserving interrupted leaf
   functions and SA_ONSTACK alternate signal stacks.
 - Strip user-space pointer authentication codes via
   ptrauth_strip_user_insn_pac() (and clear Thumb bit 0 for compat user
   tasks) in unwind_user_strip_pac(), and define ARCH_INIT_USER_FP_FRAME
   for both 64-bit AArch64 PCS frame records and 32-bit AArch32
   compat_frame_tail layouts.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 arch/arm64/Kconfig                   |   2 +
 arch/arm64/include/asm/unwind_user.h | 145 +++++++++++++++++++++++++++
 arch/arm64/kernel/signal.c           |  55 ++++++++++
 3 files changed, 202 insertions(+)
 create mode 100644 arch/arm64/include/asm/unwind_user.h

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index ee0f263f477d..918d7e278f7d 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -228,6 +228,8 @@ config ARM64
 	select HAVE_STACKPROTECTOR
 	select HAVE_STATIC_CALL if CFI
 	select HAVE_SYSCALL_TRACEPOINTS
+	select HAVE_UNWIND_USER_CFI
+	select HAVE_UNWIND_USER_FP
 	select HAVE_KPROBES
 	select HAVE_KRETPROBES
 	select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU
diff --git a/arch/arm64/include/asm/unwind_user.h b/arch/arm64/include/asm/unwind_user.h
new file mode 100644
index 000000000000..919fbc406747
--- /dev/null
+++ b/arch/arm64/include/asm/unwind_user.h
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __ASM_ARM64_UNWIND_USER_H
+#define __ASM_ARM64_UNWIND_USER_H
+
+#ifdef CONFIG_UNWIND_USER
+
+#include <linux/sched.h>
+#include <linux/sched/task_stack.h>
+#include <linux/unwind_dwarf_cfi.h>
+#include <linux/unwind_user_types.h>
+#include <asm/compiler.h>
+#include <asm/ptrace.h>
+
+/**
+ * unwind_user_word_size() - Return the user-space word size in bytes on arm64
+ * @regs: User-mode register state.
+ *
+ * Return: 4 for AArch32 compat user mode, or 8 for AArch64 user mode.
+ */
+static inline int unwind_user_word_size(struct pt_regs *regs)
+{
+	return compat_user_mode(regs) ? 4 : 8;
+}
+
+/**
+ * unwind_user_frame_pointer() - Fetch the user frame pointer from @regs
+ * @regs: User-mode register state.
+ *
+ * Return: r11 (compat_fp) for AArch32 compat tasks, or x29 for AArch64 tasks.
+ */
+static inline unsigned long unwind_user_frame_pointer(struct pt_regs *regs)
+{
+	return compat_user_mode(regs) ? regs->compat_fp : frame_pointer(regs);
+}
+
+#define unwind_user_frame_pointer unwind_user_frame_pointer
+
+/**
+ * unwind_user_fp_sets_sp() - Indicate ARM64 FP records do not recover caller SP
+ *
+ * AArch64 PCS places {x29, x30} at the bottom of the callee's frame rather than
+ * adjacent to the caller's stack pointer, so FP unwinding only yields a lower
+ * bound on the caller's SP.
+ *
+ * Return: Always false on arm64.
+ */
+static inline bool unwind_user_fp_sets_sp(void)
+{
+	return false;
+}
+
+#define unwind_user_fp_sets_sp unwind_user_fp_sets_sp
+
+/**
+ * unwind_user_link_register() - Fetch the user link register from @regs
+ * @regs: User-mode register state.
+ * @ra: Output link register value (r14 for AArch32 compat, x30 for AArch64).
+ *
+ * Return: Always true on arm64 because a link register is architecturally
+ * present in both AArch64 and AArch32 execution states.
+ */
+static inline bool unwind_user_link_register(struct pt_regs *regs,
+					     unsigned long *ra)
+{
+	if (compat_user_mode(regs))
+		*ra = regs->compat_lr;
+	else
+		*ra = procedure_link_pointer(regs);
+	return true;
+}
+
+#define unwind_user_link_register unwind_user_link_register
+
+/**
+ * unwind_user_strip_pac() - Normalize user return address on arm64
+ * @ip: Recovered user return address.
+ * @ra_signed: Whether CFI marked the return address as PAC-signed (unused;
+ *             arm64 strips user instruction PAC bits unconditionally via the
+ *             task's configured pointer authentication mask).
+ *
+ * Return: @ip with Thumb bit 0 cleared for AArch32 compat tasks, or @ip with
+ * user instruction PAC bits stripped for AArch64 tasks.
+ */
+static inline unsigned long unwind_user_strip_pac(unsigned long ip,
+						  bool ra_signed)
+{
+	if (IS_ENABLED(CONFIG_COMPAT) &&
+	    compat_user_mode(task_pt_regs(current)))
+		return ip & ~1UL;
+	return ptrauth_strip_user_insn_pac(ip);
+}
+
+#define unwind_user_strip_pac unwind_user_strip_pac
+
+/**
+ * unwind_user_cfi_arch() - Populate AArch64/AArch32 DWARF register numbers
+ * @ws: User word size in bytes (8 for AArch64, 4 for AArch32 compat).
+ * @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) {
+		*arch = (struct dwarf_cfi_arch){
+			.ws	= 8,
+			.sp_reg	= 31,	/* sp */
+			.fp_reg	= 29,	/* x29 */
+			.ra_reg	= 30,	/* x30 (lr) */
+		};
+		return true;
+	}
+	if (ws == 4) {
+		*arch = (struct dwarf_cfi_arch){
+			.ws	= 4,
+			.sp_reg	= 13,	/* r13 (sp) */
+			.fp_reg	= 11,	/* r11 (fp) */
+			.ra_reg	= 14,	/* r14 (lr) */
+		};
+		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
+
+/*
+ * AArch64 PCS stores {fp, lr} at [fp, fp + 16), while 32-bit AArch32 compat
+ * APCS stores struct compat_frame_tail {fp, sp, lr} at [compat_fp - 12, compat_fp).
+ */
+#define ARCH_INIT_USER_FP_FRAME(ws)			\
+	.cfa_off	= (ws) == 8 ? 2 * (ws) : 0,	\
+	.ra_off		= -1 * (ws),			\
+	.fp_off		= (ws) == 8 ? -2 * (ws) : -3 * (ws), \
+	.use_fp		= true,
+
+#endif /* CONFIG_HAVE_UNWIND_USER_FP */
+
+#endif /* __ASM_ARM64_UNWIND_USER_H */
diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index 80398ac83c3b..1cc4f3992e1a 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -21,6 +21,7 @@
 #include <linux/rseq.h>
 #include <linux/syscalls.h>
 #include <linux/pkeys.h>
+#include <linux/unwind_user.h>
 
 #include <asm/daifflags.h>
 #include <asm/debug-monitors.h>
@@ -1705,6 +1706,60 @@ void __init minsigstksz_setup(void)
 		16; /* max alignment padding */
 }
 
+#ifdef CONFIG_UNWIND_USER
+/**
+ * unwind_user_sigreturn() - Unwind an AArch64 vDSO rt_sigreturn signal frame
+ * @state: Current user unwind state.
+ *
+ * __kernel_rt_sigreturn intentionally omits .eh_frame directives in the arm64
+ * vDSO so libgcc uses its sigcontext fallback path, and the synthetic
+ * user->next_frame FP record skips the interrupted PC. Recover the interrupted
+ * PC, SP, x29, and x30 directly from &rt_sigframe.uc.uc_mcontext so leaf
+ * functions, -fomit-frame-pointer code, and alternate signal stacks unwind
+ * accurately.
+ *
+ * Return: true if @state was at __kernel_rt_sigreturn 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 *sf;
+	unsigned long sigtramp;
+	u64 fp_lr[2], sp_pc[2];
+
+	if (!mm || state->ws != 8 || !state->sp_valid || !mm->context.vdso)
+		return false;
+
+	sigtramp = (unsigned long)VDSO_SYMBOL(mm->context.vdso, sigtramp);
+	if (state->ip != sigtramp &&
+	    !(state->topmost && state->ip == sigtramp + 4))
+		return false;
+
+	if (state->sp & 0xfUL)
+		return false;
+
+	sf = (struct rt_sigframe __user *)state->sp;
+	if (copy_from_user(fp_lr, &sf->uc.uc_mcontext.regs[29], sizeof(fp_lr)) ||
+	    copy_from_user(sp_pc, &sf->uc.uc_mcontext.sp, sizeof(sp_pc)))
+		return false;
+
+	sp_pc[1] = ptrauth_strip_user_insn_pac(sp_pc[1]);
+	if ((sp_pc[0] & 7ULL) || !sp_pc[1])
+		return false;
+
+	state->fp = fp_lr[0];
+	state->ra = ptrauth_strip_user_insn_pac(fp_lr[1]);
+	state->sp = sp_pc[0];
+	state->ip = sp_pc[1];
+	state->ra_valid = true;
+	state->sp_valid = true;
+	state->signal = true;
+	state->topmost = false;
+	return true;
+}
+#endif
+
 /*
  * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as
  * changes likely come with new fields that should be added below.
-- 
2.56.1.831.g720f3f5937-goog


  parent reply	other threads:[~2026-11-25  6:16 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  6:14 [RFC PATCH v2 00/30] unwind: Minimal DWARF CFI unwinding for user and kernel space Ian Rogers
2026-11-25  6:14 ` [RFC PATCH v2 01/30] unwind: Add Minimal DWARF CFI bytecode interpreter Ian Rogers
2026-11-25  7:05   ` bot+bpf-ci
2026-11-25  6:14 ` [RFC PATCH v2 02/30] unwind: Add KUnit tests for Minimal DWARF CFI interpreter Ian Rogers
2026-11-25  7:05   ` bot+bpf-ci
2026-11-25  6:14 ` [RFC PATCH v2 03/30] unwind_user: Support Minimal DWARF CFI user-space unwinding Ian Rogers
2026-11-25  7:05   ` bot+bpf-ci
2026-11-25  6:14 ` [RFC PATCH v2 04/30] x86: unwind_user: Support Minimal DWARF CFI user unwinding Ian Rogers
2026-11-25  6:47   ` bot+bpf-ci
2026-11-25  6:14 ` [RFC PATCH v2 05/30] scripts: Add min_eh_frame build-time .eh_frame minifier Ian Rogers
2026-11-25  7:06   ` bot+bpf-ci
2026-11-25  6:14 ` [RFC PATCH v2 06/30] kbuild: Wire up CONFIG_UNWINDER_DWARF_CFI_MINIFY object pass Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 07/30] objtool/ibt: Ignore .eh_frame relocations during ENDBR validation Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 08/30] vmlinux.lds.h: Allow architectures to emit custom RO_DATA sections Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 09/30] efi/libstub: Disable unwind tables in x86 and RISC-V EFI stubs Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 10/30] tracing: Skip internal ftrace frames under CONFIG_UNWINDER_DWARF_CFI Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 11/30] x86: Add DWARF CFI annotations to entry, ftrace, frame, and rethook Ian Rogers
2026-11-25  7:05   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 12/30] x86/unwind: Add Minimal DWARF CFI kernel unwinder Ian Rogers
2026-11-25  7:06   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 13/30] lib/Kconfig.debug: Allow LATENCYTOP and fault-injection filter with DWARF CFI Ian Rogers
2026-11-25  6:15 ` Ian Rogers [this message]
2026-11-25  6:15 ` [RFC PATCH v2 15/30] arm64: Add DWARF CFI annotations to entry, ftrace, EFI, BPF, and KASAN Ian Rogers
2026-11-25  7:06   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 16/30] arm64/unwind: Add Minimal DWARF CFI kernel unwinder Ian Rogers
2026-11-25  7:06   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 17/30] riscv: unwind_user: Support Minimal DWARF CFI and FP user unwinding Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 18/30] riscv: Add DWARF CFI annotations to entry, ftrace, rethook, and BPF JIT Ian Rogers
2026-11-25  7:06   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 19/30] riscv/unwind: Add Minimal DWARF CFI kernel unwinder Ian Rogers
2026-11-25  7:06   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 20/30] selftests/perf_events: Add Minimal DWARF CFI deferred user unwind test Ian Rogers
2026-11-25  7:05   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 21/30] scripts: Add eh_frame_min.py .eh_frame coverage and size analyzer Ian Rogers
2026-11-25  7:05   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 22/30] tools headers: Add linux/kmsan-checks.h copy from the kernel sources Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 23/30] tools headers: Add linux/nospec.h " Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 24/30] tools headers: Add linux/unwind_dwarf_cfi.h " Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 25/30] tools include: Add linux/uaccess.h stub for userspace tools Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 26/30] tools/unwind: Add standalone libLLVM Minimal DWARF CFI verifier Ian Rogers
2026-11-25  7:06   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 27/30] tools/unwind: Add multi-arch symbolic instruction lifters for verify_cfi Ian Rogers
2026-11-25  7:05   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 28/30] tools/unwind: Add CFG instruction abstract execution oracle to verify_cfi Ian Rogers
2026-11-25  7:06   ` bot+bpf-ci
2026-11-25  6:15 ` [RFC PATCH v2 29/30] tools/unwind: Add x86-64 ORC cross-check " Ian Rogers
2026-11-25  6:15 ` [RFC PATCH v2 30/30] MAINTAINERS: Add DWARF CFI stack unwinding entry Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125061523.1540234-15-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=alex@ghiti.fr \
    --cc=andrii@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=ardb@kernel.org \
    --cc=ast@kernel.org \
    --cc=bp@alien8.de \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=dylanbhatch@google.com \
    --cc=indu.bhagat@oracle.com \
    --cc=jpoimboe@kernel.org \
    --cc=jremus@linux.ibm.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=nathan@kernel.org \
    --cc=nsc@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=oupton@kernel.org \
    --cc=palmer@dabbelt.com \
    --cc=peterz@infradead.org \
    --cc=pjw@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=tglx@kernel.org \
    --cc=will@kernel.org \
    --cc=wnliu@google.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox