xref: /OK3568_Linux_fs/kernel/arch/arm64/include/asm/stacktrace.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2012 ARM Ltd.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun #ifndef __ASM_STACKTRACE_H
6*4882a593Smuzhiyun #define __ASM_STACKTRACE_H
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/percpu.h>
9*4882a593Smuzhiyun #include <linux/sched.h>
10*4882a593Smuzhiyun #include <linux/sched/task_stack.h>
11*4882a593Smuzhiyun #include <linux/types.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <asm/memory.h>
14*4882a593Smuzhiyun #include <asm/ptrace.h>
15*4882a593Smuzhiyun #include <asm/sdei.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun enum stack_type {
18*4882a593Smuzhiyun 	STACK_TYPE_UNKNOWN,
19*4882a593Smuzhiyun 	STACK_TYPE_TASK,
20*4882a593Smuzhiyun 	STACK_TYPE_IRQ,
21*4882a593Smuzhiyun 	STACK_TYPE_OVERFLOW,
22*4882a593Smuzhiyun 	STACK_TYPE_SDEI_NORMAL,
23*4882a593Smuzhiyun 	STACK_TYPE_SDEI_CRITICAL,
24*4882a593Smuzhiyun 	__NR_STACK_TYPES
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun struct stack_info {
28*4882a593Smuzhiyun 	unsigned long low;
29*4882a593Smuzhiyun 	unsigned long high;
30*4882a593Smuzhiyun 	enum stack_type type;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun  * A snapshot of a frame record or fp/lr register values, along with some
35*4882a593Smuzhiyun  * accounting information necessary for robust unwinding.
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * @fp:          The fp value in the frame record (or the real fp)
38*4882a593Smuzhiyun  * @pc:          The fp value in the frame record (or the real lr)
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * @stacks_done: Stacks which have been entirely unwound, for which it is no
41*4882a593Smuzhiyun  *               longer valid to unwind to.
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * @prev_fp:     The fp that pointed to this frame record, or a synthetic value
44*4882a593Smuzhiyun  *               of 0. This is used to ensure that within a stack, each
45*4882a593Smuzhiyun  *               subsequent frame record is at an increasing address.
46*4882a593Smuzhiyun  * @prev_type:   The type of stack this frame record was on, or a synthetic
47*4882a593Smuzhiyun  *               value of STACK_TYPE_UNKNOWN. This is used to detect a
48*4882a593Smuzhiyun  *               transition from one stack to another.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * @graph:       When FUNCTION_GRAPH_TRACER is selected, holds the index of a
51*4882a593Smuzhiyun  *               replacement lr value in the ftrace graph stack.
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun struct stackframe {
54*4882a593Smuzhiyun 	unsigned long fp;
55*4882a593Smuzhiyun 	unsigned long pc;
56*4882a593Smuzhiyun 	DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES);
57*4882a593Smuzhiyun 	unsigned long prev_fp;
58*4882a593Smuzhiyun 	enum stack_type prev_type;
59*4882a593Smuzhiyun #ifdef CONFIG_FUNCTION_GRAPH_TRACER
60*4882a593Smuzhiyun 	int graph;
61*4882a593Smuzhiyun #endif
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun extern int unwind_frame(struct task_struct *tsk, struct stackframe *frame);
65*4882a593Smuzhiyun extern void walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
66*4882a593Smuzhiyun 			    bool (*fn)(void *, unsigned long), void *data);
67*4882a593Smuzhiyun extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
68*4882a593Smuzhiyun 			   const char *loglvl);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun DECLARE_PER_CPU(unsigned long *, irq_stack_ptr);
71*4882a593Smuzhiyun 
on_stack(unsigned long sp,unsigned long low,unsigned long high,enum stack_type type,struct stack_info * info)72*4882a593Smuzhiyun static inline bool on_stack(unsigned long sp, unsigned long low,
73*4882a593Smuzhiyun 				unsigned long high, enum stack_type type,
74*4882a593Smuzhiyun 				struct stack_info *info)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	if (!low)
77*4882a593Smuzhiyun 		return false;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if (sp < low || sp >= high)
80*4882a593Smuzhiyun 		return false;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	if (info) {
83*4882a593Smuzhiyun 		info->low = low;
84*4882a593Smuzhiyun 		info->high = high;
85*4882a593Smuzhiyun 		info->type = type;
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun 	return true;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
on_irq_stack(unsigned long sp,struct stack_info * info)90*4882a593Smuzhiyun static inline bool on_irq_stack(unsigned long sp,
91*4882a593Smuzhiyun 				struct stack_info *info)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	unsigned long low = (unsigned long)raw_cpu_read(irq_stack_ptr);
94*4882a593Smuzhiyun 	unsigned long high = low + IRQ_STACK_SIZE;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	return on_stack(sp, low, high, STACK_TYPE_IRQ, info);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
on_task_stack(const struct task_struct * tsk,unsigned long sp,struct stack_info * info)99*4882a593Smuzhiyun static inline bool on_task_stack(const struct task_struct *tsk,
100*4882a593Smuzhiyun 				 unsigned long sp,
101*4882a593Smuzhiyun 				 struct stack_info *info)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	unsigned long low = (unsigned long)task_stack_page(tsk);
104*4882a593Smuzhiyun 	unsigned long high = low + THREAD_SIZE;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	return on_stack(sp, low, high, STACK_TYPE_TASK, info);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun #ifdef CONFIG_VMAP_STACK
110*4882a593Smuzhiyun DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack);
111*4882a593Smuzhiyun 
on_overflow_stack(unsigned long sp,struct stack_info * info)112*4882a593Smuzhiyun static inline bool on_overflow_stack(unsigned long sp,
113*4882a593Smuzhiyun 				struct stack_info *info)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	unsigned long low = (unsigned long)raw_cpu_ptr(overflow_stack);
116*4882a593Smuzhiyun 	unsigned long high = low + OVERFLOW_STACK_SIZE;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	return on_stack(sp, low, high, STACK_TYPE_OVERFLOW, info);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun #else
on_overflow_stack(unsigned long sp,struct stack_info * info)121*4882a593Smuzhiyun static inline bool on_overflow_stack(unsigned long sp,
122*4882a593Smuzhiyun 			struct stack_info *info) { return false; }
123*4882a593Smuzhiyun #endif
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun  * We can only safely access per-cpu stacks from current in a non-preemptible
128*4882a593Smuzhiyun  * context.
129*4882a593Smuzhiyun  */
on_accessible_stack(const struct task_struct * tsk,unsigned long sp,struct stack_info * info)130*4882a593Smuzhiyun static inline bool on_accessible_stack(const struct task_struct *tsk,
131*4882a593Smuzhiyun 				       unsigned long sp,
132*4882a593Smuzhiyun 				       struct stack_info *info)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	if (info)
135*4882a593Smuzhiyun 		info->type = STACK_TYPE_UNKNOWN;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	if (on_task_stack(tsk, sp, info))
138*4882a593Smuzhiyun 		return true;
139*4882a593Smuzhiyun 	if (tsk != current || preemptible())
140*4882a593Smuzhiyun 		return false;
141*4882a593Smuzhiyun 	if (on_irq_stack(sp, info))
142*4882a593Smuzhiyun 		return true;
143*4882a593Smuzhiyun 	if (on_overflow_stack(sp, info))
144*4882a593Smuzhiyun 		return true;
145*4882a593Smuzhiyun 	if (on_sdei_stack(sp, info))
146*4882a593Smuzhiyun 		return true;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	return false;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
start_backtrace(struct stackframe * frame,unsigned long fp,unsigned long pc)151*4882a593Smuzhiyun static inline void start_backtrace(struct stackframe *frame,
152*4882a593Smuzhiyun 				   unsigned long fp, unsigned long pc)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	frame->fp = fp;
155*4882a593Smuzhiyun 	frame->pc = pc;
156*4882a593Smuzhiyun #ifdef CONFIG_FUNCTION_GRAPH_TRACER
157*4882a593Smuzhiyun 	frame->graph = 0;
158*4882a593Smuzhiyun #endif
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/*
161*4882a593Smuzhiyun 	 * Prime the first unwind.
162*4882a593Smuzhiyun 	 *
163*4882a593Smuzhiyun 	 * In unwind_frame() we'll check that the FP points to a valid stack,
164*4882a593Smuzhiyun 	 * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be
165*4882a593Smuzhiyun 	 * treated as a transition to whichever stack that happens to be. The
166*4882a593Smuzhiyun 	 * prev_fp value won't be used, but we set it to 0 such that it is
167*4882a593Smuzhiyun 	 * definitely not an accessible stack address.
168*4882a593Smuzhiyun 	 */
169*4882a593Smuzhiyun 	bitmap_zero(frame->stacks_done, __NR_STACK_TYPES);
170*4882a593Smuzhiyun 	frame->prev_fp = 0;
171*4882a593Smuzhiyun 	frame->prev_type = STACK_TYPE_UNKNOWN;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun #endif	/* __ASM_STACKTRACE_H */
175