xref: /linux-6.15/arch/s390/include/asm/stacktrace.h (revision 723ac2d6)
178c98f90SMartin Schwidefsky /* SPDX-License-Identifier: GPL-2.0 */
278c98f90SMartin Schwidefsky #ifndef _ASM_S390_STACKTRACE_H
378c98f90SMartin Schwidefsky #define _ASM_S390_STACKTRACE_H
478c98f90SMartin Schwidefsky 
5ebd912ffSHeiko Carstens #include <linux/stacktrace.h>
678c98f90SMartin Schwidefsky #include <linux/uaccess.h>
778c98f90SMartin Schwidefsky #include <linux/ptrace.h>
878c98f90SMartin Schwidefsky 
9504b73d0SHeiko Carstens struct stack_frame_user {
10504b73d0SHeiko Carstens 	unsigned long back_chain;
11504b73d0SHeiko Carstens 	unsigned long empty1[5];
12504b73d0SHeiko Carstens 	unsigned long gprs[10];
13504b73d0SHeiko Carstens 	unsigned long empty2[4];
14504b73d0SHeiko Carstens };
15504b73d0SHeiko Carstens 
16be72ea09SHeiko Carstens struct stack_frame_vdso_wrapper {
17be72ea09SHeiko Carstens 	struct stack_frame_user sf;
18be72ea09SHeiko Carstens 	unsigned long return_address;
19be72ea09SHeiko Carstens };
20be72ea09SHeiko Carstens 
21ebd912ffSHeiko Carstens struct perf_callchain_entry_ctx;
22ebd912ffSHeiko Carstens 
23ebd912ffSHeiko Carstens void arch_stack_walk_user_common(stack_trace_consume_fn consume_entry, void *cookie,
24ebd912ffSHeiko Carstens 				 struct perf_callchain_entry_ctx *entry,
25ebd912ffSHeiko Carstens 				 const struct pt_regs *regs, bool perf);
26ebd912ffSHeiko Carstens 
2778c98f90SMartin Schwidefsky enum stack_type {
2878c98f90SMartin Schwidefsky 	STACK_TYPE_UNKNOWN,
2978c98f90SMartin Schwidefsky 	STACK_TYPE_TASK,
3078c98f90SMartin Schwidefsky 	STACK_TYPE_IRQ,
3178c98f90SMartin Schwidefsky 	STACK_TYPE_NODAT,
3278c98f90SMartin Schwidefsky 	STACK_TYPE_RESTART,
3308edb968SVasily Gorbik 	STACK_TYPE_MCCK,
3478c98f90SMartin Schwidefsky };
3578c98f90SMartin Schwidefsky 
3678c98f90SMartin Schwidefsky struct stack_info {
3778c98f90SMartin Schwidefsky 	enum stack_type type;
3878c98f90SMartin Schwidefsky 	unsigned long begin, end;
3978c98f90SMartin Schwidefsky };
4078c98f90SMartin Schwidefsky 
4178c98f90SMartin Schwidefsky const char *stack_type_name(enum stack_type type);
4278c98f90SMartin Schwidefsky int get_stack_info(unsigned long sp, struct task_struct *task,
4378c98f90SMartin Schwidefsky 		   struct stack_info *info, unsigned long *visit_mask);
4478c98f90SMartin Schwidefsky 
on_stack(struct stack_info * info,unsigned long addr,size_t len)4578c98f90SMartin Schwidefsky static inline bool on_stack(struct stack_info *info,
4678c98f90SMartin Schwidefsky 			    unsigned long addr, size_t len)
4778c98f90SMartin Schwidefsky {
4878c98f90SMartin Schwidefsky 	if (info->type == STACK_TYPE_UNKNOWN)
4978c98f90SMartin Schwidefsky 		return false;
5078c98f90SMartin Schwidefsky 	if (addr + len < addr)
5178c98f90SMartin Schwidefsky 		return false;
520ab0d7acSVasily Gorbik 	return addr >= info->begin && addr + len <= info->end;
5378c98f90SMartin Schwidefsky }
5478c98f90SMartin Schwidefsky 
5578c98f90SMartin Schwidefsky /*
5678c98f90SMartin Schwidefsky  * Stack layout of a C stack frame.
5742b01a55SVasily Gorbik  * Kernel uses the packed stack layout (-mpacked-stack).
5878c98f90SMartin Schwidefsky  */
5978c98f90SMartin Schwidefsky struct stack_frame {
60e0ffcf3fSHeiko Carstens 	union {
61f037acb4SHeiko Carstens 		unsigned long empty[9];
62e0ffcf3fSHeiko Carstens 		struct {
63e0ffcf3fSHeiko Carstens 			unsigned long sie_control_block;
64e0ffcf3fSHeiko Carstens 			unsigned long sie_savearea;
65e0ffcf3fSHeiko Carstens 			unsigned long sie_reason;
66e0ffcf3fSHeiko Carstens 			unsigned long sie_flags;
676b33e68aSNico Boehr 			unsigned long sie_control_block_phys;
68*723ac2d6SClaudio Imbrenda 			unsigned long sie_guest_asce;
69e0ffcf3fSHeiko Carstens 		};
70e0ffcf3fSHeiko Carstens 	};
7178c98f90SMartin Schwidefsky 	unsigned long gprs[10];
7278c98f90SMartin Schwidefsky 	unsigned long back_chain;
7378c98f90SMartin Schwidefsky };
7478c98f90SMartin Schwidefsky 
7575794257SVasily Gorbik /*
7630de14b1SSven Schnelle  * Unlike current_stack_pointer which simply contains the current value of %r15
7775794257SVasily Gorbik  * current_frame_address() returns function stack frame address, which matches
7875794257SVasily Gorbik  * %r15 upon function invocation. It may differ from %r15 later if function
7975794257SVasily Gorbik  * allocates stack for local variables or new stack frame to call other
8075794257SVasily Gorbik  * functions.
8175794257SVasily Gorbik  */
8275794257SVasily Gorbik #define current_frame_address()						\
8375794257SVasily Gorbik 	((unsigned long)__builtin_frame_address(0) -			\
8475794257SVasily Gorbik 	 offsetof(struct stack_frame, back_chain))
8575794257SVasily Gorbik 
get_stack_pointer(struct task_struct * task,struct pt_regs * regs)8688b60426SVasily Gorbik static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
8788b60426SVasily Gorbik 						       struct pt_regs *regs)
8888b60426SVasily Gorbik {
8988b60426SVasily Gorbik 	if (regs)
9088b60426SVasily Gorbik 		return (unsigned long)kernel_stack_pointer(regs);
9188b60426SVasily Gorbik 	if (task == current)
9288b60426SVasily Gorbik 		return current_frame_address();
9388b60426SVasily Gorbik 	return (unsigned long)task->thread.ksp;
9488b60426SVasily Gorbik }
9588b60426SVasily Gorbik 
9667147e96SHeiko Carstens /*
9767147e96SHeiko Carstens  * To keep this simple mark register 2-6 as being changed (volatile)
9867147e96SHeiko Carstens  * by the called function, even though register 6 is saved/nonvolatile.
9967147e96SHeiko Carstens  */
10067147e96SHeiko Carstens #define CALL_FMT_0 "=&d" (r2)
10167147e96SHeiko Carstens #define CALL_FMT_1 "+&d" (r2)
10267147e96SHeiko Carstens #define CALL_FMT_2 CALL_FMT_1, "+&d" (r3)
10367147e96SHeiko Carstens #define CALL_FMT_3 CALL_FMT_2, "+&d" (r4)
10467147e96SHeiko Carstens #define CALL_FMT_4 CALL_FMT_3, "+&d" (r5)
10567147e96SHeiko Carstens #define CALL_FMT_5 CALL_FMT_4, "+&d" (r6)
10678c98f90SMartin Schwidefsky 
10778c98f90SMartin Schwidefsky #define CALL_CLOBBER_5 "0", "1", "14", "cc", "memory"
10878c98f90SMartin Schwidefsky #define CALL_CLOBBER_4 CALL_CLOBBER_5
10978c98f90SMartin Schwidefsky #define CALL_CLOBBER_3 CALL_CLOBBER_4, "5"
11078c98f90SMartin Schwidefsky #define CALL_CLOBBER_2 CALL_CLOBBER_3, "4"
11178c98f90SMartin Schwidefsky #define CALL_CLOBBER_1 CALL_CLOBBER_2, "3"
11278c98f90SMartin Schwidefsky #define CALL_CLOBBER_0 CALL_CLOBBER_1
11378c98f90SMartin Schwidefsky 
11441d71fe5SHeiko Carstens #define CALL_LARGS_0(...)						\
11541d71fe5SHeiko Carstens 	long dummy = 0
11641d71fe5SHeiko Carstens #define CALL_LARGS_1(t1, a1)						\
11741d71fe5SHeiko Carstens 	long arg1  = (long)(t1)(a1)
11841d71fe5SHeiko Carstens #define CALL_LARGS_2(t1, a1, t2, a2)					\
11941d71fe5SHeiko Carstens 	CALL_LARGS_1(t1, a1);						\
12041d71fe5SHeiko Carstens 	long arg2 = (long)(t2)(a2)
12141d71fe5SHeiko Carstens #define CALL_LARGS_3(t1, a1, t2, a2, t3, a3)				\
12241d71fe5SHeiko Carstens 	CALL_LARGS_2(t1, a1, t2, a2);					\
12341d71fe5SHeiko Carstens 	long arg3 = (long)(t3)(a3)
12441d71fe5SHeiko Carstens #define CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4)			\
12541d71fe5SHeiko Carstens 	CALL_LARGS_3(t1, a1, t2, a2, t3, a3);				\
12641d71fe5SHeiko Carstens 	long arg4  = (long)(t4)(a4)
12741d71fe5SHeiko Carstens #define CALL_LARGS_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5)		\
12841d71fe5SHeiko Carstens 	CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4);			\
12941d71fe5SHeiko Carstens 	long arg5 = (long)(t5)(a5)
13041d71fe5SHeiko Carstens 
13141d71fe5SHeiko Carstens #define CALL_REGS_0							\
13241d71fe5SHeiko Carstens 	register long r2 asm("2") = dummy
13341d71fe5SHeiko Carstens #define CALL_REGS_1							\
13441d71fe5SHeiko Carstens 	register long r2 asm("2") = arg1
13541d71fe5SHeiko Carstens #define CALL_REGS_2							\
13641d71fe5SHeiko Carstens 	CALL_REGS_1;							\
13741d71fe5SHeiko Carstens 	register long r3 asm("3") = arg2
13841d71fe5SHeiko Carstens #define CALL_REGS_3							\
13941d71fe5SHeiko Carstens 	CALL_REGS_2;							\
14041d71fe5SHeiko Carstens 	register long r4 asm("4") = arg3
14141d71fe5SHeiko Carstens #define CALL_REGS_4							\
14241d71fe5SHeiko Carstens 	CALL_REGS_3;							\
14341d71fe5SHeiko Carstens 	register long r5 asm("5") = arg4
14441d71fe5SHeiko Carstens #define CALL_REGS_5							\
14541d71fe5SHeiko Carstens 	CALL_REGS_4;							\
14641d71fe5SHeiko Carstens 	register long r6 asm("6") = arg5
14741d71fe5SHeiko Carstens 
14841d71fe5SHeiko Carstens #define CALL_TYPECHECK_0(...)
14941d71fe5SHeiko Carstens #define CALL_TYPECHECK_1(t, a, ...)					\
15041d71fe5SHeiko Carstens 	typecheck(t, a)
15141d71fe5SHeiko Carstens #define CALL_TYPECHECK_2(t, a, ...)					\
15241d71fe5SHeiko Carstens 	CALL_TYPECHECK_1(__VA_ARGS__);					\
15341d71fe5SHeiko Carstens 	typecheck(t, a)
15441d71fe5SHeiko Carstens #define CALL_TYPECHECK_3(t, a, ...)					\
15541d71fe5SHeiko Carstens 	CALL_TYPECHECK_2(__VA_ARGS__);					\
15641d71fe5SHeiko Carstens 	typecheck(t, a)
15741d71fe5SHeiko Carstens #define CALL_TYPECHECK_4(t, a, ...)					\
15841d71fe5SHeiko Carstens 	CALL_TYPECHECK_3(__VA_ARGS__);					\
15941d71fe5SHeiko Carstens 	typecheck(t, a)
16041d71fe5SHeiko Carstens #define CALL_TYPECHECK_5(t, a, ...)					\
16141d71fe5SHeiko Carstens 	CALL_TYPECHECK_4(__VA_ARGS__);					\
16241d71fe5SHeiko Carstens 	typecheck(t, a)
16341d71fe5SHeiko Carstens 
16441d71fe5SHeiko Carstens #define CALL_PARM_0(...) void
16541d71fe5SHeiko Carstens #define CALL_PARM_1(t, a, ...) t
16641d71fe5SHeiko Carstens #define CALL_PARM_2(t, a, ...) t, CALL_PARM_1(__VA_ARGS__)
16741d71fe5SHeiko Carstens #define CALL_PARM_3(t, a, ...) t, CALL_PARM_2(__VA_ARGS__)
16841d71fe5SHeiko Carstens #define CALL_PARM_4(t, a, ...) t, CALL_PARM_3(__VA_ARGS__)
16941d71fe5SHeiko Carstens #define CALL_PARM_5(t, a, ...) t, CALL_PARM_4(__VA_ARGS__)
17041d71fe5SHeiko Carstens #define CALL_PARM_6(t, a, ...) t, CALL_PARM_5(__VA_ARGS__)
17141d71fe5SHeiko Carstens 
17241d71fe5SHeiko Carstens /*
17341d71fe5SHeiko Carstens  * Use call_on_stack() to call a function switching to a specified
17441d71fe5SHeiko Carstens  * stack. Proper sign and zero extension of function arguments is
17541d71fe5SHeiko Carstens  * done. Usage:
17641d71fe5SHeiko Carstens  *
17741d71fe5SHeiko Carstens  * rc = call_on_stack(nr, stack, rettype, fn, t1, a1, t2, a2, ...)
17841d71fe5SHeiko Carstens  *
17941d71fe5SHeiko Carstens  * - nr specifies the number of function arguments of fn.
18041d71fe5SHeiko Carstens  * - stack specifies the stack to be used.
18141d71fe5SHeiko Carstens  * - fn is the function to be called.
18241d71fe5SHeiko Carstens  * - rettype is the return type of fn.
18341d71fe5SHeiko Carstens  * - t1, a1, ... are pairs, where t1 must match the type of the first
18441d71fe5SHeiko Carstens  *   argument of fn, t2 the second, etc. a1 is the corresponding
18541d71fe5SHeiko Carstens  *   first function argument (not name), etc.
18641d71fe5SHeiko Carstens  */
18741d71fe5SHeiko Carstens #define call_on_stack(nr, stack, rettype, fn, ...)			\
18841d71fe5SHeiko Carstens ({									\
18941d71fe5SHeiko Carstens 	rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = fn;		\
19041d71fe5SHeiko Carstens 	unsigned long frame = current_frame_address();			\
19141d71fe5SHeiko Carstens 	unsigned long __stack = stack;					\
19241d71fe5SHeiko Carstens 	unsigned long prev;						\
19341d71fe5SHeiko Carstens 	CALL_LARGS_##nr(__VA_ARGS__);					\
19441d71fe5SHeiko Carstens 	CALL_REGS_##nr;							\
19541d71fe5SHeiko Carstens 									\
19641d71fe5SHeiko Carstens 	CALL_TYPECHECK_##nr(__VA_ARGS__);				\
19741d71fe5SHeiko Carstens 	asm volatile(							\
19841d71fe5SHeiko Carstens 		"	lgr	%[_prev],15\n"				\
19941d71fe5SHeiko Carstens 		"	lg	15,%[_stack]\n"				\
20041d71fe5SHeiko Carstens 		"	stg	%[_frame],%[_bc](15)\n"			\
20141d71fe5SHeiko Carstens 		"	brasl	14,%[_fn]\n"				\
20241d71fe5SHeiko Carstens 		"	lgr	15,%[_prev]\n"				\
20341d71fe5SHeiko Carstens 		: [_prev] "=&d" (prev), CALL_FMT_##nr			\
20441d71fe5SHeiko Carstens 		: [_stack] "R" (__stack),				\
20541d71fe5SHeiko Carstens 		  [_bc] "i" (offsetof(struct stack_frame, back_chain)),	\
20641d71fe5SHeiko Carstens 		  [_frame] "d" (frame),					\
20741d71fe5SHeiko Carstens 		  [_fn] "X" (__fn) : CALL_CLOBBER_##nr);		\
20841d71fe5SHeiko Carstens 	(rettype)r2;							\
20941d71fe5SHeiko Carstens })
21041d71fe5SHeiko Carstens 
21182caf7abSAlexander Gordeev /*
21282caf7abSAlexander Gordeev  * Use call_nodat() to call a function with DAT disabled.
21382caf7abSAlexander Gordeev  * Proper sign and zero extension of function arguments is done.
21482caf7abSAlexander Gordeev  * Usage:
21582caf7abSAlexander Gordeev  *
21682caf7abSAlexander Gordeev  * rc = call_nodat(nr, rettype, fn, t1, a1, t2, a2, ...)
21782caf7abSAlexander Gordeev  *
21882caf7abSAlexander Gordeev  * - nr specifies the number of function arguments of fn.
21982caf7abSAlexander Gordeev  * - fn is the function to be called, where fn is a physical address.
22082caf7abSAlexander Gordeev  * - rettype is the return type of fn.
22182caf7abSAlexander Gordeev  * - t1, a1, ... are pairs, where t1 must match the type of the first
22282caf7abSAlexander Gordeev  *   argument of fn, t2 the second, etc. a1 is the corresponding
22382caf7abSAlexander Gordeev  *   first function argument (not name), etc.
22482caf7abSAlexander Gordeev  *
22582caf7abSAlexander Gordeev  * fn() is called with standard C function call ABI, with the exception
22682caf7abSAlexander Gordeev  * that no useful stackframe or stackpointer is passed via register 15.
22782caf7abSAlexander Gordeev  * Therefore the called function must not use r15 to access the stack.
22882caf7abSAlexander Gordeev  */
22982caf7abSAlexander Gordeev #define call_nodat(nr, rettype, fn, ...)				\
23082caf7abSAlexander Gordeev ({									\
23182caf7abSAlexander Gordeev 	rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = (fn);		\
2322d1b21ecSAlexander Gordeev 	/* aligned since psw_leave must not cross page boundary */	\
2332d1b21ecSAlexander Gordeev 	psw_t __aligned(16) psw_leave;					\
2342d1b21ecSAlexander Gordeev 	psw_t psw_enter;						\
23582caf7abSAlexander Gordeev 	CALL_LARGS_##nr(__VA_ARGS__);					\
23682caf7abSAlexander Gordeev 	CALL_REGS_##nr;							\
23782caf7abSAlexander Gordeev 									\
23882caf7abSAlexander Gordeev 	CALL_TYPECHECK_##nr(__VA_ARGS__);				\
23982caf7abSAlexander Gordeev 	psw_enter.mask = PSW_KERNEL_BITS & ~PSW_MASK_DAT;		\
24082caf7abSAlexander Gordeev 	psw_enter.addr = (unsigned long)__fn;				\
24182caf7abSAlexander Gordeev 	asm volatile(							\
24282caf7abSAlexander Gordeev 		"	epsw	0,1\n"					\
24382caf7abSAlexander Gordeev 		"	risbg	1,0,0,31,32\n"				\
24482caf7abSAlexander Gordeev 		"	larl	7,1f\n"					\
24582caf7abSAlexander Gordeev 		"	stg	1,%[psw_leave]\n"			\
24682caf7abSAlexander Gordeev 		"	stg	7,8+%[psw_leave]\n"			\
24782caf7abSAlexander Gordeev 		"	la	7,%[psw_leave]\n"			\
24882caf7abSAlexander Gordeev 		"	lra	7,0(7)\n"				\
24982caf7abSAlexander Gordeev 		"	larl	1,0f\n"					\
25082caf7abSAlexander Gordeev 		"	lra	14,0(1)\n"				\
25182caf7abSAlexander Gordeev 		"	lpswe	%[psw_enter]\n"				\
25282caf7abSAlexander Gordeev 		"0:	lpswe	0(7)\n"					\
25382caf7abSAlexander Gordeev 		"1:\n"							\
25482caf7abSAlexander Gordeev 		: CALL_FMT_##nr, [psw_leave] "=Q" (psw_leave)		\
25582caf7abSAlexander Gordeev 		: [psw_enter] "Q" (psw_enter)				\
25682caf7abSAlexander Gordeev 		: "7", CALL_CLOBBER_##nr);				\
25782caf7abSAlexander Gordeev 	(rettype)r2;							\
25882caf7abSAlexander Gordeev })
25982caf7abSAlexander Gordeev 
26078c98f90SMartin Schwidefsky #endif /* _ASM_S390_STACKTRACE_H */
261