blob: 6ad30776e984d071134f8762395565859691e0b0 [file] [log] [blame]
Catalin Marinas60ffc302012-03-05 11:49:27 +00001/*
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 Rutlandf60ad4e2017-07-20 12:26:48 +010019#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>
AKASHI Takahirofe13f952015-12-15 17:33:40 +090025
Catalin Marinas60ffc302012-03-05 11:49:27 +000026struct stackframe {
27 unsigned long fp;
Catalin Marinas60ffc302012-03-05 11:49:27 +000028 unsigned long pc;
AKASHI Takahiro20380bb2015-12-15 17:33:41 +090029#ifdef CONFIG_FUNCTION_GRAPH_TRACER
30 unsigned int graph;
31#endif
Catalin Marinas60ffc302012-03-05 11:49:27 +000032};
33
AKASHI Takahirofe13f952015-12-15 17:33:40 +090034extern int unwind_frame(struct task_struct *tsk, struct stackframe *frame);
35extern void walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
Catalin Marinas60ffc302012-03-05 11:49:27 +000036 int (*fn)(struct stackframe *, void *), void *data);
Kefeng Wang1149aad2017-05-09 09:53:37 +080037extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +000038
Mark Rutlandf60fe782017-07-31 21:17:03 +010039DECLARE_PER_CPU(unsigned long *, irq_stack_ptr);
Mark Rutlandf60ad4e2017-07-20 12:26:48 +010040
41static inline bool on_irq_stack(unsigned long sp)
42{
Mark Rutlandf60fe782017-07-31 21:17:03 +010043 unsigned long low = (unsigned long)raw_cpu_read(irq_stack_ptr);
Mark Rutlandf60ad4e2017-07-20 12:26:48 +010044 unsigned long high = low + IRQ_STACK_SIZE;
45
Mark Rutlandf60fe782017-07-31 21:17:03 +010046 if (!low)
47 return false;
48
Mark Rutlandf60ad4e2017-07-20 12:26:48 +010049 return (low <= sp && sp < high);
50}
51
52static inline bool on_task_stack(struct task_struct *tsk, unsigned long sp)
53{
54 unsigned long low = (unsigned long)task_stack_page(tsk);
55 unsigned long high = low + THREAD_SIZE;
56
57 return (low <= sp && sp < high);
58}
59
Mark Rutland872d8322017-07-14 20:30:35 +010060#ifdef CONFIG_VMAP_STACK
61DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack);
62
63static inline bool on_overflow_stack(unsigned long sp)
64{
65 unsigned long low = (unsigned long)raw_cpu_ptr(overflow_stack);
66 unsigned long high = low + OVERFLOW_STACK_SIZE;
67
68 return (low <= sp && sp < high);
69}
70#else
71static inline bool on_overflow_stack(unsigned long sp) { return false; }
72#endif
73
Mark Rutland12964442017-08-01 18:51:15 +010074/*
75 * We can only safely access per-cpu stacks from current in a non-preemptible
76 * context.
77 */
78static inline bool on_accessible_stack(struct task_struct *tsk, unsigned long sp)
79{
80 if (on_task_stack(tsk, sp))
81 return true;
82 if (tsk != current || preemptible())
83 return false;
84 if (on_irq_stack(sp))
85 return true;
Mark Rutland872d8322017-07-14 20:30:35 +010086 if (on_overflow_stack(sp))
87 return true;
Mark Rutland12964442017-08-01 18:51:15 +010088
89 return false;
90}
91
Catalin Marinas60ffc302012-03-05 11:49:27 +000092#endif /* __ASM_STACKTRACE_H */