Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 ARM Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | #ifndef __ASM_STACKTRACE_H |
| 17 | #define __ASM_STACKTRACE_H |
| 18 | |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 19 | #include <linux/percpu.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/sched/task_stack.h> |
| 22 | |
| 23 | #include <asm/memory.h> |
| 24 | #include <asm/ptrace.h> |
James Morse | f5df269 | 2018-01-08 15:38:12 +0000 | [diff] [blame] | 25 | #include <asm/sdei.h> |
AKASHI Takahiro | fe13f95 | 2015-12-15 17:33:40 +0900 | [diff] [blame] | 26 | |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 27 | struct stackframe { |
| 28 | unsigned long fp; |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 29 | unsigned long pc; |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 30 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Pratyush Anand | 9f41631 | 2018-02-05 14:28:01 +0100 | [diff] [blame] | 31 | int graph; |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 32 | #endif |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
AKASHI Takahiro | fe13f95 | 2015-12-15 17:33:40 +0900 | [diff] [blame] | 35 | extern int unwind_frame(struct task_struct *tsk, struct stackframe *frame); |
| 36 | extern void walk_stackframe(struct task_struct *tsk, struct stackframe *frame, |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 37 | int (*fn)(struct stackframe *, void *), void *data); |
Kefeng Wang | 1149aad | 2017-05-09 09:53:37 +0800 | [diff] [blame] | 38 | extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk); |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 39 | |
Mark Rutland | f60fe78 | 2017-07-31 21:17:03 +0100 | [diff] [blame] | 40 | DECLARE_PER_CPU(unsigned long *, irq_stack_ptr); |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 41 | |
| 42 | static inline bool on_irq_stack(unsigned long sp) |
| 43 | { |
Mark Rutland | f60fe78 | 2017-07-31 21:17:03 +0100 | [diff] [blame] | 44 | unsigned long low = (unsigned long)raw_cpu_read(irq_stack_ptr); |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 45 | unsigned long high = low + IRQ_STACK_SIZE; |
| 46 | |
Mark Rutland | f60fe78 | 2017-07-31 21:17:03 +0100 | [diff] [blame] | 47 | if (!low) |
| 48 | return false; |
| 49 | |
Mark Rutland | f60ad4e | 2017-07-20 12:26:48 +0100 | [diff] [blame] | 50 | return (low <= sp && sp < high); |
| 51 | } |
| 52 | |
| 53 | static inline bool on_task_stack(struct task_struct *tsk, unsigned long sp) |
| 54 | { |
| 55 | unsigned long low = (unsigned long)task_stack_page(tsk); |
| 56 | unsigned long high = low + THREAD_SIZE; |
| 57 | |
| 58 | return (low <= sp && sp < high); |
| 59 | } |
| 60 | |
Mark Rutland | 872d832 | 2017-07-14 20:30:35 +0100 | [diff] [blame] | 61 | #ifdef CONFIG_VMAP_STACK |
| 62 | DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack); |
| 63 | |
| 64 | static inline bool on_overflow_stack(unsigned long sp) |
| 65 | { |
| 66 | unsigned long low = (unsigned long)raw_cpu_ptr(overflow_stack); |
| 67 | unsigned long high = low + OVERFLOW_STACK_SIZE; |
| 68 | |
| 69 | return (low <= sp && sp < high); |
| 70 | } |
| 71 | #else |
| 72 | static inline bool on_overflow_stack(unsigned long sp) { return false; } |
| 73 | #endif |
| 74 | |
Mark Rutland | 1296444 | 2017-08-01 18:51:15 +0100 | [diff] [blame] | 75 | /* |
| 76 | * We can only safely access per-cpu stacks from current in a non-preemptible |
| 77 | * context. |
| 78 | */ |
| 79 | static inline bool on_accessible_stack(struct task_struct *tsk, unsigned long sp) |
| 80 | { |
| 81 | if (on_task_stack(tsk, sp)) |
| 82 | return true; |
| 83 | if (tsk != current || preemptible()) |
| 84 | return false; |
| 85 | if (on_irq_stack(sp)) |
| 86 | return true; |
Mark Rutland | 872d832 | 2017-07-14 20:30:35 +0100 | [diff] [blame] | 87 | if (on_overflow_stack(sp)) |
| 88 | return true; |
James Morse | f5df269 | 2018-01-08 15:38:12 +0000 | [diff] [blame] | 89 | if (on_sdei_stack(sp)) |
| 90 | return true; |
Mark Rutland | 1296444 | 2017-08-01 18:51:15 +0100 | [diff] [blame] | 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 95 | #endif /* __ASM_STACKTRACE_H */ |