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