xref: /OK3568_Linux_fs/kernel/include/linux/sched/task_stack.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_SCHED_TASK_STACK_H
3*4882a593Smuzhiyun #define _LINUX_SCHED_TASK_STACK_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun  * task->stack (kernel stack) handling interfaces:
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/sched.h>
10*4882a593Smuzhiyun #include <linux/magic.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #ifdef CONFIG_THREAD_INFO_IN_TASK
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun  * When accessing the stack of a non-current task that might exit, use
16*4882a593Smuzhiyun  * try_get_task_stack() instead.  task_stack_page will return a pointer
17*4882a593Smuzhiyun  * that could get freed out from under you.
18*4882a593Smuzhiyun  */
task_stack_page(const struct task_struct * task)19*4882a593Smuzhiyun static inline void *task_stack_page(const struct task_struct *task)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	return task->stack;
22*4882a593Smuzhiyun }
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define setup_thread_stack(new,old)	do { } while(0)
25*4882a593Smuzhiyun 
end_of_stack(const struct task_struct * task)26*4882a593Smuzhiyun static inline unsigned long *end_of_stack(const struct task_struct *task)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun #ifdef CONFIG_STACK_GROWSUP
29*4882a593Smuzhiyun 	return (unsigned long *)((unsigned long)task->stack + THREAD_SIZE) - 1;
30*4882a593Smuzhiyun #else
31*4882a593Smuzhiyun 	return task->stack;
32*4882a593Smuzhiyun #endif
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #elif !defined(__HAVE_THREAD_FUNCTIONS)
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define task_stack_page(task)	((void *)(task)->stack)
38*4882a593Smuzhiyun 
setup_thread_stack(struct task_struct * p,struct task_struct * org)39*4882a593Smuzhiyun static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	*task_thread_info(p) = *task_thread_info(org);
42*4882a593Smuzhiyun 	task_thread_info(p)->task = p;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun  * Return the address of the last usable long on the stack.
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * When the stack grows down, this is just above the thread
49*4882a593Smuzhiyun  * info struct. Going any lower will corrupt the threadinfo.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * When the stack grows up, this is the highest address.
52*4882a593Smuzhiyun  * Beyond that position, we corrupt data on the next page.
53*4882a593Smuzhiyun  */
end_of_stack(struct task_struct * p)54*4882a593Smuzhiyun static inline unsigned long *end_of_stack(struct task_struct *p)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun #ifdef CONFIG_STACK_GROWSUP
57*4882a593Smuzhiyun 	return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
58*4882a593Smuzhiyun #else
59*4882a593Smuzhiyun 	return (unsigned long *)(task_thread_info(p) + 1);
60*4882a593Smuzhiyun #endif
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #endif
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #ifdef CONFIG_THREAD_INFO_IN_TASK
try_get_task_stack(struct task_struct * tsk)66*4882a593Smuzhiyun static inline void *try_get_task_stack(struct task_struct *tsk)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	return refcount_inc_not_zero(&tsk->stack_refcount) ?
69*4882a593Smuzhiyun 		task_stack_page(tsk) : NULL;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun extern void put_task_stack(struct task_struct *tsk);
73*4882a593Smuzhiyun #else
try_get_task_stack(struct task_struct * tsk)74*4882a593Smuzhiyun static inline void *try_get_task_stack(struct task_struct *tsk)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	return task_stack_page(tsk);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
put_task_stack(struct task_struct * tsk)79*4882a593Smuzhiyun static inline void put_task_stack(struct task_struct *tsk) {}
80*4882a593Smuzhiyun #endif
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #define task_stack_end_corrupted(task) \
83*4882a593Smuzhiyun 		(*(end_of_stack(task)) != STACK_END_MAGIC)
84*4882a593Smuzhiyun 
object_is_on_stack(const void * obj)85*4882a593Smuzhiyun static inline int object_is_on_stack(const void *obj)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	void *stack = task_stack_page(current);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return (obj >= stack) && (obj < (stack + THREAD_SIZE));
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun extern void thread_stack_cache_init(void);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_STACK_USAGE
stack_not_used(struct task_struct * p)95*4882a593Smuzhiyun static inline unsigned long stack_not_used(struct task_struct *p)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	unsigned long *n = end_of_stack(p);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	do { 	/* Skip over canary */
100*4882a593Smuzhiyun # ifdef CONFIG_STACK_GROWSUP
101*4882a593Smuzhiyun 		n--;
102*4882a593Smuzhiyun # else
103*4882a593Smuzhiyun 		n++;
104*4882a593Smuzhiyun # endif
105*4882a593Smuzhiyun 	} while (!*n);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun # ifdef CONFIG_STACK_GROWSUP
108*4882a593Smuzhiyun 	return (unsigned long)end_of_stack(p) - (unsigned long)n;
109*4882a593Smuzhiyun # else
110*4882a593Smuzhiyun 	return (unsigned long)n - (unsigned long)end_of_stack(p);
111*4882a593Smuzhiyun # endif
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun #endif
114*4882a593Smuzhiyun extern void set_task_stack_end_magic(struct task_struct *tsk);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun #ifndef __HAVE_ARCH_KSTACK_END
kstack_end(void * addr)117*4882a593Smuzhiyun static inline int kstack_end(void *addr)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	/* Reliable end of stack detection:
120*4882a593Smuzhiyun 	 * Some APM bios versions misalign the stack
121*4882a593Smuzhiyun 	 */
122*4882a593Smuzhiyun 	return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun #endif
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun #endif /* _LINUX_SCHED_TASK_STACK_H */
127