1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Common arm64 stack unwinder code.
5 * To implement a new arm64 stack unwinder:
6 * 1) Include this header
8 * 2) Call into unwind_next_common() from your top level unwind
9 * function, passing it the validation and translation callbacks
10 * (though the later can be NULL if no translation is required).
12 * See: arch/arm64/kernel/stacktrace.c for the reference implementation.
14 * Copyright (C) 2012 ARM Ltd.
16 #ifndef __ASM_STACKTRACE_COMMON_H
17 #define __ASM_STACKTRACE_COMMON_H
19 #include <linux/bitmap.h>
20 #include <linux/bitops.h>
21 #include <linux/kprobes.h>
22 #include <linux/types.h>
29 STACK_TYPE_SDEI_NORMAL,
30 STACK_TYPE_SDEI_CRITICAL,
42 * A snapshot of a frame record or fp/lr register values, along with some
43 * accounting information necessary for robust unwinding.
45 * @fp: The fp value in the frame record (or the real fp)
46 * @pc: The lr value in the frame record (or the real lr)
48 * @stacks_done: Stacks which have been entirely unwound, for which it is no
49 * longer valid to unwind to.
51 * @prev_fp: The fp that pointed to this frame record, or a synthetic value
52 * of 0. This is used to ensure that within a stack, each
53 * subsequent frame record is at an increasing address.
54 * @prev_type: The type of stack this frame record was on, or a synthetic
55 * value of STACK_TYPE_UNKNOWN. This is used to detect a
56 * transition from one stack to another.
58 * @kr_cur: When KRETPROBES is selected, holds the kretprobe instance
59 * associated with the most recently encountered replacement lr
62 * @task: The task being unwound.
67 DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES);
68 unsigned long prev_fp;
69 enum stack_type prev_type;
70 #ifdef CONFIG_KRETPROBES
71 struct llist_node *kr_cur;
73 struct task_struct *task;
76 static inline bool on_stack(unsigned long sp, unsigned long size,
77 unsigned long low, unsigned long high,
78 enum stack_type type, struct stack_info *info)
83 if (sp < low || sp + size < sp || sp + size > high)
94 static inline void unwind_init_common(struct unwind_state *state,
95 struct task_struct *task)
98 #ifdef CONFIG_KRETPROBES
103 * Prime the first unwind.
105 * In unwind_next() we'll check that the FP points to a valid stack,
106 * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be
107 * treated as a transition to whichever stack that happens to be. The
108 * prev_fp value won't be used, but we set it to 0 such that it is
109 * definitely not an accessible stack address.
111 bitmap_zero(state->stacks_done, __NR_STACK_TYPES);
113 state->prev_type = STACK_TYPE_UNKNOWN;
117 * stack_trace_translate_fp_fn() - Translates a non-kernel frame pointer to
120 * @fp: the frame pointer to be updated to its kernel address.
121 * @type: the stack type associated with frame pointer @fp
123 * Returns true and success and @fp is updated to the corresponding
124 * kernel virtual address; otherwise returns false.
126 typedef bool (*stack_trace_translate_fp_fn)(unsigned long *fp,
127 enum stack_type type);
130 * on_accessible_stack_fn() - Check whether a stack range is on any
131 * of the possible stacks.
133 * @tsk: task whose stack is being unwound
134 * @sp: stack address being checked
135 * @size: size of the stack range being checked
136 * @info: stack unwinding context
138 typedef bool (*on_accessible_stack_fn)(const struct task_struct *tsk,
139 unsigned long sp, unsigned long size,
140 struct stack_info *info);
142 static inline int unwind_next_common(struct unwind_state *state,
143 struct stack_info *info,
144 on_accessible_stack_fn accessible,
145 stack_trace_translate_fp_fn translate_fp)
147 unsigned long fp = state->fp, kern_fp = fp;
148 struct task_struct *tsk = state->task;
153 if (!accessible(tsk, fp, 16, info))
156 if (test_bit(info->type, state->stacks_done))
160 * If fp is not from the current address space perform the necessary
161 * translation before dereferencing it to get the next fp.
163 if (translate_fp && !translate_fp(&kern_fp, info->type))
167 * As stacks grow downward, any valid record on the same stack must be
168 * at a strictly higher address than the prior record.
170 * Stacks can nest in several valid orders, e.g.
172 * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
173 * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
176 * ... but the nesting itself is strict. Once we transition from one
177 * stack to another, it's never valid to unwind back to that first
180 if (info->type == state->prev_type) {
181 if (fp <= state->prev_fp)
184 __set_bit(state->prev_type, state->stacks_done);
188 * Record this frame record's values and location. The prev_fp and
189 * prev_type are only meaningful to the next unwind_next() invocation.
191 state->fp = READ_ONCE(*(unsigned long *)(kern_fp));
192 state->pc = READ_ONCE(*(unsigned long *)(kern_fp + 8));
194 state->prev_type = info->type;
199 #endif /* __ASM_STACKTRACE_COMMON_H */