1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012 ARM Ltd. 4 */ 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> 11 #include <linux/types.h> 12 13 #include <asm/memory.h> 14 #include <asm/ptrace.h> 15 #include <asm/sdei.h> 16 17 enum 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, 24 __NR_STACK_TYPES 25 }; 26 27 struct stack_info { 28 unsigned long low; 29 unsigned long high; 30 enum stack_type type; 31 }; 32 33 /* 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 */ 53 struct 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 64 extern int unwind_frame(struct task_struct *tsk, struct stackframe *frame); 65 extern void walk_stackframe(struct task_struct *tsk, struct stackframe *frame, 66 int (*fn)(struct stackframe *, void *), void *data); 67 extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk); 68 69 DECLARE_PER_CPU(unsigned long *, irq_stack_ptr); 70 71 static inline bool on_stack(unsigned long sp, unsigned long low, 72 unsigned long high, enum stack_type type, 73 struct stack_info *info) 74 { 75 if (!low) 76 return false; 77 78 if (sp < low || sp >= high) 79 return false; 80 81 if (info) { 82 info->low = low; 83 info->high = high; 84 info->type = type; 85 } 86 return true; 87 } 88 89 static inline bool on_irq_stack(unsigned long sp, 90 struct stack_info *info) 91 { 92 unsigned long low = (unsigned long)raw_cpu_read(irq_stack_ptr); 93 unsigned long high = low + IRQ_STACK_SIZE; 94 95 return on_stack(sp, low, high, STACK_TYPE_IRQ, info); 96 } 97 98 static inline bool on_task_stack(const struct task_struct *tsk, 99 unsigned long sp, 100 struct stack_info *info) 101 { 102 unsigned long low = (unsigned long)task_stack_page(tsk); 103 unsigned long high = low + THREAD_SIZE; 104 105 return on_stack(sp, low, high, STACK_TYPE_TASK, info); 106 } 107 108 #ifdef CONFIG_VMAP_STACK 109 DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack); 110 111 static inline bool on_overflow_stack(unsigned long sp, 112 struct stack_info *info) 113 { 114 unsigned long low = (unsigned long)raw_cpu_ptr(overflow_stack); 115 unsigned long high = low + OVERFLOW_STACK_SIZE; 116 117 return on_stack(sp, low, high, STACK_TYPE_OVERFLOW, info); 118 } 119 #else 120 static inline bool on_overflow_stack(unsigned long sp, 121 struct stack_info *info) { return false; } 122 #endif 123 124 125 /* 126 * We can only safely access per-cpu stacks from current in a non-preemptible 127 * context. 128 */ 129 static inline bool on_accessible_stack(const struct task_struct *tsk, 130 unsigned long sp, 131 struct stack_info *info) 132 { 133 if (info) 134 info->type = STACK_TYPE_UNKNOWN; 135 136 if (on_task_stack(tsk, sp, info)) 137 return true; 138 if (tsk != current || preemptible()) 139 return false; 140 if (on_irq_stack(sp, info)) 141 return true; 142 if (on_overflow_stack(sp, info)) 143 return true; 144 if (on_sdei_stack(sp, info)) 145 return true; 146 147 return false; 148 } 149 150 static inline void start_backtrace(struct stackframe *frame, 151 unsigned long fp, unsigned long pc) 152 { 153 frame->fp = fp; 154 frame->pc = pc; 155 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 156 frame->graph = 0; 157 #endif 158 159 /* 160 * Prime the first unwind. 161 * 162 * In unwind_frame() we'll check that the FP points to a valid stack, 163 * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be 164 * treated as a transition to whichever stack that happens to be. The 165 * prev_fp value won't be used, but we set it to 0 such that it is 166 * definitely not an accessible stack address. 167 */ 168 bitmap_zero(frame->stacks_done, __NR_STACK_TYPES); 169 frame->prev_fp = 0; 170 frame->prev_type = STACK_TYPE_UNKNOWN; 171 } 172 173 #endif /* __ASM_STACKTRACE_H */ 174