Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /* thread_info.h: common low-level thread information accessors
0003  *
0004  * Copyright (C) 2002  David Howells (dhowells@redhat.com)
0005  * - Incorporating suggestions made by Linus Torvalds
0006  */
0007 
0008 #ifndef _LINUX_THREAD_INFO_H
0009 #define _LINUX_THREAD_INFO_H
0010 
0011 #include <linux/types.h>
0012 #include <linux/limits.h>
0013 #include <linux/bug.h>
0014 #include <linux/restart_block.h>
0015 #include <linux/errno.h>
0016 
0017 #ifdef CONFIG_THREAD_INFO_IN_TASK
0018 /*
0019  * For CONFIG_THREAD_INFO_IN_TASK kernels we need <asm/current.h> for the
0020  * definition of current, but for !CONFIG_THREAD_INFO_IN_TASK kernels,
0021  * including <asm/current.h> can cause a circular dependency on some platforms.
0022  */
0023 #include <asm/current.h>
0024 #define current_thread_info() ((struct thread_info *)current)
0025 #endif
0026 
0027 #include <linux/bitops.h>
0028 
0029 /*
0030  * For per-arch arch_within_stack_frames() implementations, defined in
0031  * asm/thread_info.h.
0032  */
0033 enum {
0034     BAD_STACK = -1,
0035     NOT_STACK = 0,
0036     GOOD_FRAME,
0037     GOOD_STACK,
0038 };
0039 
0040 #ifdef CONFIG_GENERIC_ENTRY
0041 enum syscall_work_bit {
0042     SYSCALL_WORK_BIT_SECCOMP,
0043     SYSCALL_WORK_BIT_SYSCALL_TRACEPOINT,
0044     SYSCALL_WORK_BIT_SYSCALL_TRACE,
0045     SYSCALL_WORK_BIT_SYSCALL_EMU,
0046     SYSCALL_WORK_BIT_SYSCALL_AUDIT,
0047     SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH,
0048     SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP,
0049 };
0050 
0051 #define SYSCALL_WORK_SECCOMP        BIT(SYSCALL_WORK_BIT_SECCOMP)
0052 #define SYSCALL_WORK_SYSCALL_TRACEPOINT BIT(SYSCALL_WORK_BIT_SYSCALL_TRACEPOINT)
0053 #define SYSCALL_WORK_SYSCALL_TRACE  BIT(SYSCALL_WORK_BIT_SYSCALL_TRACE)
0054 #define SYSCALL_WORK_SYSCALL_EMU    BIT(SYSCALL_WORK_BIT_SYSCALL_EMU)
0055 #define SYSCALL_WORK_SYSCALL_AUDIT  BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT)
0056 #define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)
0057 #define SYSCALL_WORK_SYSCALL_EXIT_TRAP  BIT(SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP)
0058 #endif
0059 
0060 #include <asm/thread_info.h>
0061 
0062 #ifdef __KERNEL__
0063 
0064 #ifndef arch_set_restart_data
0065 #define arch_set_restart_data(restart) do { } while (0)
0066 #endif
0067 
0068 static inline long set_restart_fn(struct restart_block *restart,
0069                     long (*fn)(struct restart_block *))
0070 {
0071     restart->fn = fn;
0072     arch_set_restart_data(restart);
0073     return -ERESTART_RESTARTBLOCK;
0074 }
0075 
0076 #ifndef THREAD_ALIGN
0077 #define THREAD_ALIGN    THREAD_SIZE
0078 #endif
0079 
0080 #define THREADINFO_GFP      (GFP_KERNEL_ACCOUNT | __GFP_ZERO)
0081 
0082 /*
0083  * flag set/clear/test wrappers
0084  * - pass TIF_xxxx constants to these functions
0085  */
0086 
0087 static inline void set_ti_thread_flag(struct thread_info *ti, int flag)
0088 {
0089     set_bit(flag, (unsigned long *)&ti->flags);
0090 }
0091 
0092 static inline void clear_ti_thread_flag(struct thread_info *ti, int flag)
0093 {
0094     clear_bit(flag, (unsigned long *)&ti->flags);
0095 }
0096 
0097 static inline void update_ti_thread_flag(struct thread_info *ti, int flag,
0098                      bool value)
0099 {
0100     if (value)
0101         set_ti_thread_flag(ti, flag);
0102     else
0103         clear_ti_thread_flag(ti, flag);
0104 }
0105 
0106 static inline int test_and_set_ti_thread_flag(struct thread_info *ti, int flag)
0107 {
0108     return test_and_set_bit(flag, (unsigned long *)&ti->flags);
0109 }
0110 
0111 static inline int test_and_clear_ti_thread_flag(struct thread_info *ti, int flag)
0112 {
0113     return test_and_clear_bit(flag, (unsigned long *)&ti->flags);
0114 }
0115 
0116 static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
0117 {
0118     return test_bit(flag, (unsigned long *)&ti->flags);
0119 }
0120 
0121 /*
0122  * This may be used in noinstr code, and needs to be __always_inline to prevent
0123  * inadvertent instrumentation.
0124  */
0125 static __always_inline unsigned long read_ti_thread_flags(struct thread_info *ti)
0126 {
0127     return READ_ONCE(ti->flags);
0128 }
0129 
0130 #define set_thread_flag(flag) \
0131     set_ti_thread_flag(current_thread_info(), flag)
0132 #define clear_thread_flag(flag) \
0133     clear_ti_thread_flag(current_thread_info(), flag)
0134 #define update_thread_flag(flag, value) \
0135     update_ti_thread_flag(current_thread_info(), flag, value)
0136 #define test_and_set_thread_flag(flag) \
0137     test_and_set_ti_thread_flag(current_thread_info(), flag)
0138 #define test_and_clear_thread_flag(flag) \
0139     test_and_clear_ti_thread_flag(current_thread_info(), flag)
0140 #define test_thread_flag(flag) \
0141     test_ti_thread_flag(current_thread_info(), flag)
0142 #define read_thread_flags() \
0143     read_ti_thread_flags(current_thread_info())
0144 
0145 #define read_task_thread_flags(t) \
0146     read_ti_thread_flags(task_thread_info(t))
0147 
0148 #ifdef CONFIG_GENERIC_ENTRY
0149 #define set_syscall_work(fl) \
0150     set_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
0151 #define test_syscall_work(fl) \
0152     test_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
0153 #define clear_syscall_work(fl) \
0154     clear_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
0155 
0156 #define set_task_syscall_work(t, fl) \
0157     set_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
0158 #define test_task_syscall_work(t, fl) \
0159     test_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
0160 #define clear_task_syscall_work(t, fl) \
0161     clear_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
0162 
0163 #else /* CONFIG_GENERIC_ENTRY */
0164 
0165 #define set_syscall_work(fl)                        \
0166     set_ti_thread_flag(current_thread_info(), TIF_##fl)
0167 #define test_syscall_work(fl) \
0168     test_ti_thread_flag(current_thread_info(), TIF_##fl)
0169 #define clear_syscall_work(fl) \
0170     clear_ti_thread_flag(current_thread_info(), TIF_##fl)
0171 
0172 #define set_task_syscall_work(t, fl) \
0173     set_ti_thread_flag(task_thread_info(t), TIF_##fl)
0174 #define test_task_syscall_work(t, fl) \
0175     test_ti_thread_flag(task_thread_info(t), TIF_##fl)
0176 #define clear_task_syscall_work(t, fl) \
0177     clear_ti_thread_flag(task_thread_info(t), TIF_##fl)
0178 #endif /* !CONFIG_GENERIC_ENTRY */
0179 
0180 #define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
0181 
0182 #ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES
0183 static inline int arch_within_stack_frames(const void * const stack,
0184                        const void * const stackend,
0185                        const void *obj, unsigned long len)
0186 {
0187     return 0;
0188 }
0189 #endif
0190 
0191 #ifdef CONFIG_HARDENED_USERCOPY
0192 extern void __check_object_size(const void *ptr, unsigned long n,
0193                     bool to_user);
0194 
0195 static __always_inline void check_object_size(const void *ptr, unsigned long n,
0196                           bool to_user)
0197 {
0198     if (!__builtin_constant_p(n))
0199         __check_object_size(ptr, n, to_user);
0200 }
0201 #else
0202 static inline void check_object_size(const void *ptr, unsigned long n,
0203                      bool to_user)
0204 { }
0205 #endif /* CONFIG_HARDENED_USERCOPY */
0206 
0207 extern void __compiletime_error("copy source size is too small")
0208 __bad_copy_from(void);
0209 extern void __compiletime_error("copy destination size is too small")
0210 __bad_copy_to(void);
0211 
0212 void __copy_overflow(int size, unsigned long count);
0213 
0214 static inline void copy_overflow(int size, unsigned long count)
0215 {
0216     if (IS_ENABLED(CONFIG_BUG))
0217         __copy_overflow(size, count);
0218 }
0219 
0220 static __always_inline __must_check bool
0221 check_copy_size(const void *addr, size_t bytes, bool is_source)
0222 {
0223     int sz = __builtin_object_size(addr, 0);
0224     if (unlikely(sz >= 0 && sz < bytes)) {
0225         if (!__builtin_constant_p(bytes))
0226             copy_overflow(sz, bytes);
0227         else if (is_source)
0228             __bad_copy_from();
0229         else
0230             __bad_copy_to();
0231         return false;
0232     }
0233     if (WARN_ON_ONCE(bytes > INT_MAX))
0234         return false;
0235     check_object_size(addr, bytes, is_source);
0236     return true;
0237 }
0238 
0239 #ifndef arch_setup_new_exec
0240 static inline void arch_setup_new_exec(void) { }
0241 #endif
0242 
0243 #endif  /* __KERNEL__ */
0244 
0245 #endif /* _LINUX_THREAD_INFO_H */