Skip to contentSkip to conversation

s390: Signal frame user space unwinding

RFC PATCH New Jens Remus 1 participant 6 messages Jan 27

Subsystems (6)
x86 BPF Tracing s390 +2 more subsystems
lore
Share & tools
lore

Jens Remus

author
#0 lore raw

cover s390: Signal frame user space unwinding

This RFC series adds s390 support for unwinding of signal frames in user space using SFrame V3's indication of signal frames.

Prerequirements:

This series applies on top of the latest unwind user sframe s390 series "[PATCH v4 00/12] s390: SFrame user space unwinding":
https://lore.kernel.org/all/[email protected]/

on top of the latest unwind user sframe series "[PATCH v13 00/18] unwind_deferred: Implement sframe handling":
https://lore.kernel.org/all/[email protected]/

Like above series it depends on the upcoming binutils 2.46 release to be used to build executables and libraries (e.g. vDSO) with SFrame V3 on s390 (using the assembler option --gsframe-3).

Overview:

Patches 1 and 2 enables the common unwind user (sframe) frameworks to support signal frames:

- Patch 1 enables unwind user to resort to an architecture-specific signal frame unwinder, if a frame is a signal frame.

- Patch 2 adds support to unwind user sframe to provide the signal frame indication represented in SFrame V3.

Patch 3 annotates the s390 vDSO functions __kernel_[rt_]sigreturn() as signal frames.

Patch 4 moves the s390 struct [rt_]sigframe to asm/sigframe.h so that it can be used in unwind user.

Patch 5 enables signal frame unwinding in user space on s390.

Regards,
Jens

Jens Remus (5):
unwind_user: Enable arch-specific signal frame unwinders unwind_user/sframe: Add support for signal frame indication s390/vdso: Annotate __kernel_[rt_]sigreturn as signal frames s390/signal: Move struct [rt_]sigframe to asm/sigframe.h s390/unwind_user: Enable signal frame unwinding of user space

 arch/s390/include/asm/dwarf.h             |  3 +
 arch/s390/include/asm/sigframe.h          | 78 +++++++++++++++++++++++
 arch/s390/include/asm/unwind_user.h       | 57 +++++++++++++++++
 arch/s390/kernel/signal.c                 | 71 +--------------------
 arch/s390/kernel/vdso/vdso_user_wrapper.S | 15 ++++-
 arch/x86/include/asm/unwind_user.h        |  6 +-
 include/linux/unwind_user.h               |  9 +++
 include/linux/unwind_user_types.h         |  1 +
 kernel/unwind/sframe.c                    |  8 ++-
 kernel/unwind/sframe.h                    |  1 +
 kernel/unwind/user.c                      |  4 ++
 11 files changed, 178 insertions(+), 75 deletions(-)
 create mode 100644 arch/s390/include/asm/sigframe.h
Signature · 1 line
2.51.0
author
#1 lore raw

RFC PATCH 1/5 unwind_user: Enable arch-specific signal frame unwinders

Add a signal flag to struct unwind_user_frame, which indicates whether the frame is a signal frame. If set unwind_user_common_next() uses an architecture-specific unwind_user_signal_next() implementation to unwind the frame.

user unwind sframe will make use of the signal flag in a subsequent commit.

---
 arch/x86/include/asm/unwind_user.h | 6 ++++--
 include/linux/unwind_user.h        | 9 +++++++++
 include/linux/unwind_user_types.h  | 1 +
 kernel/unwind/user.c               | 4 ++++
 4 files changed, 18 insertions(+), 2 deletions(-)
arch/x86/include/asm/unwind_user.h +4 -2
index f5e9fbcdae28..21cb9eeb2503 100644
@@ -67,7 +67,8 @@ static inline int unwind_user_get_reg(unsigned long *val, unsigned int regnum)
 		.offset		= -2*(ws),		\
 			},				\
 	.sp_off		= 0,				\
-	.outermost	= false,
+	.outermost	= false,			\
+	.signal		= false,
 
 #define ARCH_INIT_USER_FP_ENTRY_FRAME(ws)		\
 	.cfa		= {				\
@@ -82,7 +83,8 @@ static inline int unwind_user_get_reg(unsigned long *val, unsigned int regnum)
 		.rule		= UNWIND_USER_RULE_RETAIN,\
 			},				\
 	.sp_off		= 0,				\
-	.outermost	= false,
+	.outermost	= false,			\
+	.signal		= false,
 
 static inline int unwind_user_fp_get_frame(struct unwind_user_state *state,
 					   struct unwind_user_frame *frame)
include/linux/unwind_user.h +9 -0
index f65b0573b3a5..eb5de4cb5bd6 100644
@@ -34,6 +34,15 @@ static inline int unwind_user_get_reg(unsigned long *val, unsigned int regnum)
 #define unwind_user_get_reg unwind_user_get_reg
 #endif
 
+#ifndef unwind_user_signal_next
+static inline int unwind_user_signal_next(struct unwind_user_state *state)
+{
+	WARN_ON_ONCE(1);
+	return -EINVAL;
+}
+#define unwind_user_signal_next unwind_user_signal_next
+#endif
+
 int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries);
 
 #endif /* _LINUX_UNWIND_USER_H */
include/linux/unwind_user_types.h +1 -0
index fac8f470b597..3985706d7851 100644
@@ -68,6 +68,7 @@ struct unwind_user_frame {
 	struct unwind_user_rule_data fp;
 	s32 sp_off;
 	bool outermost;
+	bool signal;
 };
 
 struct unwind_user_state {
kernel/unwind/user.c +4 -0
index a64ceb4a2bf6..b9a3b59e8282 100644
@@ -32,6 +32,10 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 {
 	unsigned long cfa, sp, fp, ra;
 
+	/* Use signal frame unwinder for signal frames. */
+	if (frame->signal)
+		return unwind_user_signal_next(state);
+
 	/* Stop unwinding when reaching an outermost frame. */
 	if (frame->outermost) {
 		state->done = true;
Signature · 1 line
2.51.0
author
#2 lore raw

RFC PATCH 2/5 unwind_user/sframe: Add support for signal frame indication

In reply to Jens Remus

SFrame V3 represents the assembler directive .cfi_signal_frame using a SFrame FDE flag. Note that a SFrame FDE with this particular flag set may have no SFrame FREs.

Introduce a SFRAME_V3_FDE_SIGNAL_P() helper macro. Use it in __find_fre() to populate the signal flag in struct unwind_user_frame, even if there are no FREs.

---
 kernel/unwind/sframe.c | 8 +++++++-
 kernel/unwind/sframe.h | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)
kernel/unwind/sframe.c +7 -1
index 21283e3bda42..9fd2fc1572cc 100644
@@ -446,6 +446,7 @@ static __always_inline int __find_fre(struct sframe_section *sec,
 				      struct unwind_user_frame *frame)
 {
 	unsigned char fde_pctype = SFRAME_V3_FDE_PCTYPE(fde->info);
+	bool signal = SFRAME_V3_FDE_SIGNAL_P(fde->info);
 	struct sframe_fre_internal *fre, *prev_fre = NULL;
 	struct sframe_fre_internal fres[2];
 	unsigned long fre_addr;
@@ -486,8 +487,11 @@ static __always_inline int __find_fre(struct sframe_section *sec,
 		prev_fre = fre;
 	}
 
-	if (!prev_fre)
+	if (!prev_fre) {
+		if (signal)
+			goto signal;
 		return -EINVAL;
+	}
 	fre = prev_fre;
 
 	ret = __read_fre_datawords(sec, fde, fre);
@@ -500,6 +504,8 @@ static __always_inline int __find_fre(struct sframe_section *sec,
 	sframe_init_rule_data(&frame->fp, fre->fp_ctl, fre->fp_off);
 	frame->sp_off  = SFRAME_SP_OFFSET;
 	frame->outermost = SFRAME_V3_FRE_RA_UNDEFINED_P(fre->info);
+signal:
+	frame->signal = signal;
 
 	return 0;
 }
kernel/unwind/sframe.h +1 -0
index 8a5322e95403..78432857f84c 100644
@@ -65,6 +65,7 @@ struct sframe_fda_v3 {
 #define SFRAME_V3_FDE_FRE_TYPE(info)		((info) & 0xf)
 #define SFRAME_V3_FDE_PCTYPE(info)		(((info) >> 4) & 0x1)
 #define SFRAME_V3_AARCH64_FDE_PAUTH_KEY(info)	(((info) >> 5) & 0x1)
+#define SFRAME_V3_FDE_SIGNAL_P(info)		(((info) >> 7) & 0x1)
 
 #define SFRAME_FDE_TYPE_REGULAR			0
 #define SFRAME_FDE_TYPE_FLEXIBLE		1
Signature · 1 line
2.51.0
author
#3 lore raw

RFC PATCH 3/5 s390/vdso: Annotate __kernel_[rt_]sigreturn as signal frames

In reply to Jens Remus

The GNU assembler supports CFI directive .cfi_signal_frame with binutils 2.17+. Use it to annotate the vDSO functions __kernel_sigreturn() and __kernel_rt_sigreturn() as signal frames.

This enables generation of SFrame stack trace information for the vDSO to likewise annotate these functions for use in stack tracers, such as the kernel unwind user sframe.

---
 arch/s390/include/asm/dwarf.h             |  3 +++
 arch/s390/kernel/vdso/vdso_user_wrapper.S | 15 +++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)
arch/s390/include/asm/dwarf.h +3 -0
index 2f148b15fd7d..e5663d6b11db 100644
@@ -35,6 +35,8 @@
 #define CFI_VAL_OFFSET		nocfi
 #endif
 
+#define CFI_SIGNAL_FRAME	.cfi_signal_frame
+
 #else /* !BUILD_VDSO */
 
 /*
@@ -49,6 +51,7 @@
 #define CFI_RESTORE		nocfi
 #define CFI_REL_OFFSET		nocfi
 #define CFI_VAL_OFFSET		nocfi
+#define CFI_SIGNAL_FRAME	nocfi
 
 #endif /* !BUILD_VDSO */
 
arch/s390/kernel/vdso/vdso_user_wrapper.S +13 -2
index aa06c85bcbd3..757503929ab0 100644
@@ -47,6 +47,17 @@ SYM_FUNC_START(__kernel_\func)
 SYM_FUNC_END(__kernel_\func)
 .endm
 
+.macro vdso_syscall_sf func,syscall
+SYM_FUNC_START(__kernel_\func)
+	CFI_STARTPROC simple
+	CFI_SIGNAL_FRAME
+	svc	\syscall
+	/* Trap, if syscall returns, which shouldn't happen */
+	.insn e,0x0000
+	CFI_ENDPROC
+SYM_FUNC_END(__kernel_\func)
+.endm
+
 vdso_syscall restart_syscall,__NR_restart_syscall
-vdso_syscall sigreturn,__NR_sigreturn
-vdso_syscall rt_sigreturn,__NR_rt_sigreturn
+vdso_syscall_sf sigreturn,__NR_sigreturn
+vdso_syscall_sf rt_sigreturn,__NR_rt_sigreturn
Signature · 1 line
2.51.0
author
#4 lore raw

RFC PATCH 4/5 s390/signal: Move struct [rt_]sigframe to asm/sigframe.h

In reply to Jens Remus

This enables use of struct sigframe and struct rt_sigframe elsewhere, such as unwind user.

---
 arch/s390/include/asm/sigframe.h | 78 ++++++++++++++++++++++++++++++++
 arch/s390/kernel/signal.c        | 71 +----------------------------
 2 files changed, 79 insertions(+), 70 deletions(-)
 create mode 100644 arch/s390/include/asm/sigframe.h
arch/s390/include/asm/sigframe.h new file +78 -0
new file mode 100644
index 000000000000..7539fd802675
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0
+#ifndef _ASM_S390_SIGFRAME_H
+#define _ASM_S390_SIGFRAME_H
+
+#include <uapi/asm/sigcontext.h>
+#include <asm/ucontext.h>
+
+/*
+ * Layout of an old-style signal-frame:
+ *	-----------------------------------------
+ *	| save area (_SIGNAL_FRAMESIZE)		|
+ *	-----------------------------------------
+ *	| struct sigcontext			|
+ *	|	oldmask				|
+ *	|	_sigregs *			|
+ *	-----------------------------------------
+ *	| _sigregs with				|
+ *	|	_s390_regs_common		|
+ *	|	_s390_fp_regs			|
+ *	-----------------------------------------
+ *	| int signo				|
+ *	-----------------------------------------
+ *	| _sigregs_ext with			|
+ *	|	gprs_high 64 byte (opt)		|
+ *	|	vxrs_low 128 byte (opt)		|
+ *	|	vxrs_high 256 byte (opt)	|
+ *	|	reserved 128 byte (opt)		|
+ *	-----------------------------------------
+ *	| __u16 svc_insn			|
+ *	-----------------------------------------
+ * The svc_insn entry with the sigreturn system call opcode does not
+ * have a fixed position and moves if gprs_high or vxrs exist.
+ * Future extensions will be added to _sigregs_ext.
+ */
+struct sigframe
+{
+	__u8 callee_used_stack[__SIGNAL_FRAMESIZE];
+	struct sigcontext sc;
+	_sigregs sregs;
+	int signo;
+	_sigregs_ext sregs_ext;
+	__u16 svc_insn;		/* Offset of svc_insn is NOT fixed! */
+};
+
+/*
+ * Layout of an rt signal-frame:
+ *	-----------------------------------------
+ *	| save area (_SIGNAL_FRAMESIZE)		|
+ *	-----------------------------------------
+ *	| svc __NR_rt_sigreturn 2 byte		|
+ *	-----------------------------------------
+ *	| struct siginfo			|
+ *	-----------------------------------------
+ *	| struct ucontext_extended with		|
+ *	|	unsigned long uc_flags		|
+ *	|	struct ucontext *uc_link	|
+ *	|	stack_t uc_stack		|
+ *	|	_sigregs uc_mcontext with	|
+ *	|		_s390_regs_common	|
+ *	|		_s390_fp_regs		|
+ *	|	sigset_t uc_sigmask		|
+ *	|	_sigregs_ext uc_mcontext_ext	|
+ *	|		gprs_high 64 byte (opt)	|
+ *	|		vxrs_low 128 byte (opt)	|
+ *	|		vxrs_high 256 byte (opt)|
+ *	|		reserved 128 byte (opt)	|
+ *	-----------------------------------------
+ * Future extensions will be added to _sigregs_ext.
+ */
+struct rt_sigframe
+{
+	__u8 callee_used_stack[__SIGNAL_FRAMESIZE];
+	__u16 svc_insn;
+	struct siginfo info;
+	struct ucontext_extended uc;
+};
+
+#endif /* _ASM_S390_SIGFRAME_H */
arch/s390/kernel/signal.c +1 -70

79 lines not on this page · raw message

Signature · 1 line
2.51.0
author
#5 lore raw

RFC PATCH 5/5 s390/unwind_user: Enable signal frame unwinding of user space

In reply to Jens Remus

Provide a s390-specific implementation of unwind_user_signal_next(), that unwinds from a (RT) signal frame.

---
 arch/s390/include/asm/unwind_user.h | 57 +++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
arch/s390/include/asm/unwind_user.h +57 -0
index a7b97ea01c26..8f5524a24ae1 100644
@@ -7,6 +7,7 @@
 #include <linux/types.h>
 #include <asm/asm-offsets.h>
 #include <asm/fpu.h>
+#include <asm/sigframe.h>
 #include <asm/stacktrace.h>
 #include <linux/unwind_user_types.h>
 
@@ -67,6 +68,62 @@ static inline int arch_unwind_user_get_reg(unsigned long *val,
 }
 #define unwind_user_get_reg arch_unwind_user_get_reg
 
+#define SVC_OPCODE		0x0a
+#define INSN_SVC_SIGRETURN	((SVC_OPCODE << 8) | __NR_sigreturn)
+#define INSN_SVC_RT_SIGRETURN	((SVC_OPCODE << 8) | __NR_rt_sigreturn)
+
+static inline int unwind_user_signal_next(struct unwind_user_state *state)
+{
+	unsigned short insn;
+	const _sigregs __user *sr;
+	unsigned long sp, fp, ra;
+
+	/* ABI requires IP to be 2-byte aligned. */
+	if (state->ip & 1)
+		return -EINVAL;
+
+	if (__get_user(insn, (unsigned short __user *)state->ip))
+		return -EINVAL;
+
+	/*
+	 * A signal frame has the instruction pointer pointing to
+	 * svc $__NR_sigreturn or svc $__NR_rt_sigreturn
+	 */
+	switch (insn) {
+	case INSN_SVC_SIGRETURN:
+		/* New-style non-RT frame.  */
+		const struct sigframe __user *sf;
+
+		sf = (struct sigframe __user *)state->sp;
+		if (__get_user(sr, (_sigregs __user **)&sf->sc.sregs))
+			return -EINVAL;
+		break;
+	case INSN_SVC_RT_SIGRETURN:
+		/* New-style RT frame.  */
+		const struct rt_sigframe __user *rt_sf;
+
+		rt_sf = (struct rt_sigframe __user *)state->sp;
+		sr = (_sigregs __user *)&rt_sf->uc.uc_mcontext;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (__get_user(sp, (unsigned long __user *)&sr->regs.gprs[15]))
+		return -EINVAL;
+	if (__get_user(fp, (unsigned long __user *)&sr->regs.gprs[11]))
+		return -EINVAL;
+	if (__get_user(ra, (unsigned long __user *)&sr->regs.psw.addr))
+		return -EINVAL;
+
+	state->ip = ra;
+	state->sp = sp;
+	state->fp = fp;
+	state->topmost = false;
+	return 0;
+}
+#define unwind_user_signal_next unwind_user_signal_next
+
 #endif /* CONFIG_UNWIND_USER */
 
 #ifdef CONFIG_HAVE_UNWIND_USER_FP
Signature · 1 line
2.51.0