Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 ARM Ltd. |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 4 | */ |
| 5 | #ifndef __ASM_STACKTRACE_H |
| 6 | #define __ASM_STACKTRACE_H |
| 7 | |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 8 | #include <linux/percpu.h> |
| 9 | #include <linux/sched.h> |
| 10 | #include <linux/sched/task_stack.h> |
Mark Rutland | 592700f | 2019-07-02 14:07:29 +0100 | [diff] [blame] | 11 | #include <linux/types.h> |
Masami Hiramatsu | cd9bc2c | 2021-10-21 09:55:09 +0900 | [diff] [blame] | 12 | #include <linux/llist.h> |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 13 | |
| 14 | #include <asm/memory.h> |
| 15 | #include <asm/ptrace.h> |
James Morse | f5df269 | 2018-01-08 15:38:12 +0000 | [diff] [blame] | 16 | #include <asm/sdei.h> |
AKASHI Takahiro | fe13f95 | 2015-12-15 17:33:40 +0900 | [diff] [blame] | 17 | |
Laura Abbott | 8a1ccfb | 2018-07-20 14:41:53 -0700 | [diff] [blame] | 18 | enum stack_type { |
| 19 | STACK_TYPE_UNKNOWN, |
| 20 | STACK_TYPE_TASK, |
| 21 | STACK_TYPE_IRQ, |
| 22 | STACK_TYPE_OVERFLOW, |
| 23 | STACK_TYPE_SDEI_NORMAL, |
| 24 | STACK_TYPE_SDEI_CRITICAL, |
Mark Rutland | 592700f | 2019-07-02 14:07:29 +0100 | [diff] [blame] | 25 | __NR_STACK_TYPES |
Laura Abbott | 8a1ccfb | 2018-07-20 14:41:53 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | struct stack_info { |
| 29 | unsigned long low; |
| 30 | unsigned long high; |
| 31 | enum stack_type type; |
| 32 | }; |
| 33 | |
Mark Rutland | 592700f | 2019-07-02 14:07:29 +0100 | [diff] [blame] | 34 | /* |
| 35 | * A snapshot of a frame record or fp/lr register values, along with some |
| 36 | * accounting information necessary for robust unwinding. |
| 37 | * |
| 38 | * @fp: The fp value in the frame record (or the real fp) |
Mark Rutland | 8d5903f | 2021-08-02 17:48:44 +0100 | [diff] [blame] | 39 | * @pc: The lr value in the frame record (or the real lr) |
Mark Rutland | 592700f | 2019-07-02 14:07:29 +0100 | [diff] [blame] | 40 | * |
| 41 | * @stacks_done: Stacks which have been entirely unwound, for which it is no |
| 42 | * longer valid to unwind to. |
| 43 | * |
| 44 | * @prev_fp: The fp that pointed to this frame record, or a synthetic value |
| 45 | * of 0. This is used to ensure that within a stack, each |
| 46 | * subsequent frame record is at an increasing address. |
| 47 | * @prev_type: The type of stack this frame record was on, or a synthetic |
| 48 | * value of STACK_TYPE_UNKNOWN. This is used to detect a |
| 49 | * transition from one stack to another. |
Mark Rutland | 1e5428b | 2021-11-29 14:28:42 +0000 | [diff] [blame] | 50 | * |
| 51 | * @kr_cur: When KRETPROBES is selected, holds the kretprobe instance |
| 52 | * associated with the most recently encountered replacement lr |
| 53 | * value. |
Mark Rutland | 592700f | 2019-07-02 14:07:29 +0100 | [diff] [blame] | 54 | */ |
| 55 | struct stackframe { |
| 56 | unsigned long fp; |
| 57 | unsigned long pc; |
| 58 | DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES); |
| 59 | unsigned long prev_fp; |
| 60 | enum stack_type prev_type; |
Masami Hiramatsu | cd9bc2c | 2021-10-21 09:55:09 +0900 | [diff] [blame] | 61 | #ifdef CONFIG_KRETPROBES |
| 62 | struct llist_node *kr_cur; |
| 63 | #endif |
Mark Rutland | 592700f | 2019-07-02 14:07:29 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Dmitry Safonov | c768983 | 2020-06-08 21:30:23 -0700 | [diff] [blame] | 66 | extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, |
| 67 | const char *loglvl); |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 68 | |
Mark Rutland | f60fe78 | 2017-07-31 21:17:03 +0100 | [diff] [blame] | 69 | DECLARE_PER_CPU(unsigned long *, irq_stack_ptr); |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 70 | |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 71 | static inline bool on_stack(unsigned long sp, unsigned long size, |
| 72 | unsigned long low, unsigned long high, |
| 73 | enum stack_type type, struct stack_info *info) |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 74 | { |
Mark Rutland | f60fe78 | 2017-07-31 21:17:03 +0100 | [diff] [blame] | 75 | if (!low) |
| 76 | return false; |
| 77 | |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 78 | if (sp < low || sp + size < sp || sp + size > high) |
Laura Abbott | 8a1ccfb | 2018-07-20 14:41:53 -0700 | [diff] [blame] | 79 | return false; |
| 80 | |
| 81 | if (info) { |
| 82 | info->low = low; |
| 83 | info->high = high; |
Yunfeng Ye | bd4298c | 2020-05-08 11:15:45 +0800 | [diff] [blame] | 84 | info->type = type; |
Laura Abbott | 8a1ccfb | 2018-07-20 14:41:53 -0700 | [diff] [blame] | 85 | } |
Laura Abbott | 8a1ccfb | 2018-07-20 14:41:53 -0700 | [diff] [blame] | 86 | return true; |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 89 | static inline bool on_irq_stack(unsigned long sp, unsigned long size, |
Yunfeng Ye | bd4298c | 2020-05-08 11:15:45 +0800 | [diff] [blame] | 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 | |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 95 | return on_stack(sp, size, low, high, STACK_TYPE_IRQ, info); |
Yunfeng Ye | bd4298c | 2020-05-08 11:15:45 +0800 | [diff] [blame] | 96 | } |
| 97 | |
Dave Martin | 8caa6e2 | 2019-07-02 14:07:27 +0100 | [diff] [blame] | 98 | static inline bool on_task_stack(const struct task_struct *tsk, |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 99 | unsigned long sp, unsigned long size, |
Dave Martin | 8caa6e2 | 2019-07-02 14:07:27 +0100 | [diff] [blame] | 100 | struct stack_info *info) |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 101 | { |
| 102 | unsigned long low = (unsigned long)task_stack_page(tsk); |
| 103 | unsigned long high = low + THREAD_SIZE; |
| 104 | |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 105 | return on_stack(sp, size, low, high, STACK_TYPE_TASK, info); |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Mark Rutland | 872d832 | 2017-07-14 20:30:35 +0100 | [diff] [blame] | 108 | #ifdef CONFIG_VMAP_STACK |
| 109 | DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack); |
| 110 | |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 111 | static inline bool on_overflow_stack(unsigned long sp, unsigned long size, |
Laura Abbott | 8a1ccfb | 2018-07-20 14:41:53 -0700 | [diff] [blame] | 112 | struct stack_info *info) |
Mark Rutland | 872d832 | 2017-07-14 20:30:35 +0100 | [diff] [blame] | 113 | { |
| 114 | unsigned long low = (unsigned long)raw_cpu_ptr(overflow_stack); |
| 115 | unsigned long high = low + OVERFLOW_STACK_SIZE; |
| 116 | |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 117 | return on_stack(sp, size, low, high, STACK_TYPE_OVERFLOW, info); |
Mark Rutland | 872d832 | 2017-07-14 20:30:35 +0100 | [diff] [blame] | 118 | } |
| 119 | #else |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 120 | static inline bool on_overflow_stack(unsigned long sp, unsigned long size, |
Laura Abbott | 8a1ccfb | 2018-07-20 14:41:53 -0700 | [diff] [blame] | 121 | struct stack_info *info) { return false; } |
Mark Rutland | 872d832 | 2017-07-14 20:30:35 +0100 | [diff] [blame] | 122 | #endif |
| 123 | |
Laura Abbott | 8a1ccfb | 2018-07-20 14:41:53 -0700 | [diff] [blame] | 124 | |
Mark Rutland | 1296444 | 2017-08-01 18:51:15 +0100 | [diff] [blame] | 125 | /* |
| 126 | * We can only safely access per-cpu stacks from current in a non-preemptible |
| 127 | * context. |
| 128 | */ |
Dave Martin | 8caa6e2 | 2019-07-02 14:07:27 +0100 | [diff] [blame] | 129 | static inline bool on_accessible_stack(const struct task_struct *tsk, |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 130 | unsigned long sp, unsigned long size, |
Dave Martin | 8caa6e2 | 2019-07-02 14:07:27 +0100 | [diff] [blame] | 131 | struct stack_info *info) |
Mark Rutland | 1296444 | 2017-08-01 18:51:15 +0100 | [diff] [blame] | 132 | { |
Mark Rutland | 592700f | 2019-07-02 14:07:29 +0100 | [diff] [blame] | 133 | if (info) |
| 134 | info->type = STACK_TYPE_UNKNOWN; |
| 135 | |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 136 | if (on_task_stack(tsk, sp, size, info)) |
Mark Rutland | 1296444 | 2017-08-01 18:51:15 +0100 | [diff] [blame] | 137 | return true; |
| 138 | if (tsk != current || preemptible()) |
| 139 | return false; |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 140 | if (on_irq_stack(sp, size, info)) |
Mark Rutland | 1296444 | 2017-08-01 18:51:15 +0100 | [diff] [blame] | 141 | return true; |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 142 | if (on_overflow_stack(sp, size, info)) |
Mark Rutland | 872d832 | 2017-07-14 20:30:35 +0100 | [diff] [blame] | 143 | return true; |
Peter Collingbourne | 76734d2 | 2021-05-26 10:49:25 -0700 | [diff] [blame] | 144 | if (on_sdei_stack(sp, size, info)) |
James Morse | f5df269 | 2018-01-08 15:38:12 +0000 | [diff] [blame] | 145 | return true; |
Mark Rutland | 1296444 | 2017-08-01 18:51:15 +0100 | [diff] [blame] | 146 | |
| 147 | return false; |
| 148 | } |
| 149 | |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 150 | #endif /* __ASM_STACKTRACE_H */ |