1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Common arm64 stack unwinder code.
5 * See: arch/arm64/kernel/stacktrace.c for the reference implementation.
7 * Copyright (C) 2012 ARM Ltd.
9 #ifndef __ASM_STACKTRACE_COMMON_H
10 #define __ASM_STACKTRACE_COMMON_H
12 #include <linux/kprobes.h>
13 #include <linux/types.h>
21 * struct unwind_state - state used for robust unwinding.
23 * @fp: The fp value in the frame record (or the real fp)
24 * @pc: The lr value in the frame record (or the real lr)
26 * @kr_cur: When KRETPROBES is selected, holds the kretprobe instance
27 * associated with the most recently encountered replacement lr
30 * @task: The task being unwound.
32 * @stack: The stack currently being unwound.
33 * @stacks: An array of stacks which can be unwound.
34 * @nr_stacks: The number of stacks in @stacks.
39 #ifdef CONFIG_KRETPROBES
40 struct llist_node *kr_cur;
42 struct task_struct *task;
44 struct stack_info stack;
45 struct stack_info *stacks;
49 static inline struct stack_info stackinfo_get_unknown(void)
51 return (struct stack_info) {
57 static inline bool stackinfo_on_stack(const struct stack_info *info,
58 unsigned long sp, unsigned long size)
63 if (sp < info->low || sp + size < sp || sp + size > info->high)
69 static inline void unwind_init_common(struct unwind_state *state,
70 struct task_struct *task)
73 #ifdef CONFIG_KRETPROBES
77 state->stack = stackinfo_get_unknown();
80 static struct stack_info *unwind_find_next_stack(const struct unwind_state *state,
84 for (int i = 0; i < state->nr_stacks; i++) {
85 struct stack_info *info = &state->stacks[i];
87 if (stackinfo_on_stack(info, sp, size))
95 * unwind_consume_stack() - Check if an object is on an accessible stack,
96 * updating stack boundaries so that future unwind steps cannot consume this
99 * @state: the current unwind state.
100 * @sp: the base address of the object.
101 * @size: the size of the object.
103 * Return: 0 upon success, an error code otherwise.
105 static inline int unwind_consume_stack(struct unwind_state *state,
109 struct stack_info *next;
111 if (stackinfo_on_stack(&state->stack, sp, size))
114 next = unwind_find_next_stack(state, sp, size);
119 * Stack transitions are strictly one-way, and once we've
120 * transitioned from one stack to another, it's never valid to
121 * unwind back to the old stack.
123 * Remove the current stack from the list of stacks so that it cannot
124 * be found on a subsequent transition.
126 * Note that stacks can nest in several valid orders, e.g.
128 * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
129 * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
132 * ... so we do not check the specific order of stack
135 state->stack = *next;
136 *next = stackinfo_get_unknown();
140 * Future unwind steps can only consume stack above this frame record.
141 * Update the current stack to start immediately above it.
143 state->stack.low = sp + size;
148 * unwind_next_frame_record() - Unwind to the next frame record.
150 * @state: the current unwind state.
152 * Return: 0 upon success, an error code otherwise.
155 unwind_next_frame_record(struct unwind_state *state)
157 unsigned long fp = state->fp;
163 err = unwind_consume_stack(state, fp, 16);
168 * Record this frame record's values.
170 state->fp = READ_ONCE(*(unsigned long *)(fp));
171 state->pc = READ_ONCE(*(unsigned long *)(fp + 8));
176 #endif /* __ASM_STACKTRACE_COMMON_H */