Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_SCHED_TASK_STACK_H
0003 #define _LINUX_SCHED_TASK_STACK_H
0004 
0005 /*
0006  * task->stack (kernel stack) handling interfaces:
0007  */
0008 
0009 #include <linux/sched.h>
0010 #include <linux/magic.h>
0011 
0012 #ifdef CONFIG_THREAD_INFO_IN_TASK
0013 
0014 /*
0015  * When accessing the stack of a non-current task that might exit, use
0016  * try_get_task_stack() instead.  task_stack_page will return a pointer
0017  * that could get freed out from under you.
0018  */
0019 static __always_inline void *task_stack_page(const struct task_struct *task)
0020 {
0021     return task->stack;
0022 }
0023 
0024 #define setup_thread_stack(new,old) do { } while(0)
0025 
0026 static inline unsigned long *end_of_stack(const struct task_struct *task)
0027 {
0028 #ifdef CONFIG_STACK_GROWSUP
0029     return (unsigned long *)((unsigned long)task->stack + THREAD_SIZE) - 1;
0030 #else
0031     return task->stack;
0032 #endif
0033 }
0034 
0035 #elif !defined(__HAVE_THREAD_FUNCTIONS)
0036 
0037 #define task_stack_page(task)   ((void *)(task)->stack)
0038 
0039 static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
0040 {
0041     *task_thread_info(p) = *task_thread_info(org);
0042     task_thread_info(p)->task = p;
0043 }
0044 
0045 /*
0046  * Return the address of the last usable long on the stack.
0047  *
0048  * When the stack grows down, this is just above the thread
0049  * info struct. Going any lower will corrupt the threadinfo.
0050  *
0051  * When the stack grows up, this is the highest address.
0052  * Beyond that position, we corrupt data on the next page.
0053  */
0054 static inline unsigned long *end_of_stack(struct task_struct *p)
0055 {
0056 #ifdef CONFIG_STACK_GROWSUP
0057     return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
0058 #else
0059     return (unsigned long *)(task_thread_info(p) + 1);
0060 #endif
0061 }
0062 
0063 #endif
0064 
0065 #ifdef CONFIG_THREAD_INFO_IN_TASK
0066 static inline void *try_get_task_stack(struct task_struct *tsk)
0067 {
0068     return refcount_inc_not_zero(&tsk->stack_refcount) ?
0069         task_stack_page(tsk) : NULL;
0070 }
0071 
0072 extern void put_task_stack(struct task_struct *tsk);
0073 #else
0074 static inline void *try_get_task_stack(struct task_struct *tsk)
0075 {
0076     return task_stack_page(tsk);
0077 }
0078 
0079 static inline void put_task_stack(struct task_struct *tsk) {}
0080 #endif
0081 
0082 void exit_task_stack_account(struct task_struct *tsk);
0083 
0084 #define task_stack_end_corrupted(task) \
0085         (*(end_of_stack(task)) != STACK_END_MAGIC)
0086 
0087 static inline int object_is_on_stack(const void *obj)
0088 {
0089     void *stack = task_stack_page(current);
0090 
0091     return (obj >= stack) && (obj < (stack + THREAD_SIZE));
0092 }
0093 
0094 extern void thread_stack_cache_init(void);
0095 
0096 #ifdef CONFIG_DEBUG_STACK_USAGE
0097 static inline unsigned long stack_not_used(struct task_struct *p)
0098 {
0099     unsigned long *n = end_of_stack(p);
0100 
0101     do {    /* Skip over canary */
0102 # ifdef CONFIG_STACK_GROWSUP
0103         n--;
0104 # else
0105         n++;
0106 # endif
0107     } while (!*n);
0108 
0109 # ifdef CONFIG_STACK_GROWSUP
0110     return (unsigned long)end_of_stack(p) - (unsigned long)n;
0111 # else
0112     return (unsigned long)n - (unsigned long)end_of_stack(p);
0113 # endif
0114 }
0115 #endif
0116 extern void set_task_stack_end_magic(struct task_struct *tsk);
0117 
0118 #ifndef __HAVE_ARCH_KSTACK_END
0119 static inline int kstack_end(void *addr)
0120 {
0121     /* Reliable end of stack detection:
0122      * Some APM bios versions misalign the stack
0123      */
0124     return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
0125 }
0126 #endif
0127 
0128 #endif /* _LINUX_SCHED_TASK_STACK_H */