GNU Linux-libre 4.9.287-gnu1
[releases.git] / include / linux / uaccess.h
1 #ifndef __LINUX_UACCESS_H__
2 #define __LINUX_UACCESS_H__
3
4 #include <linux/sched.h>
5
6 #define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS)
7
8 #include <asm/uaccess.h>
9
10 static __always_inline void pagefault_disabled_inc(void)
11 {
12         current->pagefault_disabled++;
13 }
14
15 static __always_inline void pagefault_disabled_dec(void)
16 {
17         current->pagefault_disabled--;
18         WARN_ON(current->pagefault_disabled < 0);
19 }
20
21 /*
22  * These routines enable/disable the pagefault handler. If disabled, it will
23  * not take any locks and go straight to the fixup table.
24  *
25  * User access methods will not sleep when called from a pagefault_disabled()
26  * environment.
27  */
28 static inline void pagefault_disable(void)
29 {
30         pagefault_disabled_inc();
31         /*
32          * make sure to have issued the store before a pagefault
33          * can hit.
34          */
35         barrier();
36 }
37
38 static inline void pagefault_enable(void)
39 {
40         /*
41          * make sure to issue those last loads/stores before enabling
42          * the pagefault handler again.
43          */
44         barrier();
45         pagefault_disabled_dec();
46 }
47
48 /*
49  * Is the pagefault handler disabled? If so, user access methods will not sleep.
50  */
51 #define pagefault_disabled() (current->pagefault_disabled != 0)
52
53 /*
54  * The pagefault handler is in general disabled by pagefault_disable() or
55  * when in irq context (via in_atomic()).
56  *
57  * This function should only be used by the fault handlers. Other users should
58  * stick to pagefault_disabled().
59  * Please NEVER use preempt_disable() to disable the fault handler. With
60  * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
61  * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
62  */
63 #define faulthandler_disabled() (pagefault_disabled() || in_atomic())
64
65 #ifndef ARCH_HAS_NOCACHE_UACCESS
66
67 static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
68                                 const void __user *from, unsigned long n)
69 {
70         return __copy_from_user_inatomic(to, from, n);
71 }
72
73 static inline unsigned long __copy_from_user_nocache(void *to,
74                                 const void __user *from, unsigned long n)
75 {
76         return __copy_from_user(to, from, n);
77 }
78
79 #endif          /* ARCH_HAS_NOCACHE_UACCESS */
80
81 /*
82  * probe_kernel_read(): safely attempt to read from a location
83  * @dst: pointer to the buffer that shall take the data
84  * @src: address to read from
85  * @size: size of the data chunk
86  *
87  * Safely read from address @src to the buffer at @dst.  If a kernel fault
88  * happens, handle that and return -EFAULT.
89  */
90 extern long probe_kernel_read(void *dst, const void *src, size_t size);
91 extern long __probe_kernel_read(void *dst, const void *src, size_t size);
92
93 /*
94  * probe_user_read(): safely attempt to read from a location in user space
95  * @dst: pointer to the buffer that shall take the data
96  * @src: address to read from
97  * @size: size of the data chunk
98  *
99  * Safely read from address @src to the buffer at @dst.  If a kernel fault
100  * happens, handle that and return -EFAULT.
101  */
102 extern long probe_user_read(void *dst, const void __user *src, size_t size);
103
104 /*
105  * probe_kernel_write(): safely attempt to write to a location
106  * @dst: address to write to
107  * @src: pointer to the data that shall be written
108  * @size: size of the data chunk
109  *
110  * Safely write to address @dst from the buffer at @src.  If a kernel fault
111  * happens, handle that and return -EFAULT.
112  */
113 extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
114 extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
115
116 /*
117  * probe_user_write(): safely attempt to write to a location in user space
118  * @dst: address to write to
119  * @src: pointer to the data that shall be written
120  * @size: size of the data chunk
121  *
122  * Safely write to address @dst from the buffer at @src.  If a kernel fault
123  * happens, handle that and return -EFAULT.
124  */
125 extern long notrace probe_user_write(void __user *dst, const void *src, size_t size);
126 extern long notrace __probe_user_write(void __user *dst, const void *src, size_t size);
127
128 extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
129 extern long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr,
130                                      long count);
131 extern long strnlen_unsafe_user(const void __user *unsafe_addr, long count);
132
133 /**
134  * probe_kernel_address(): safely attempt to read from a location
135  * @addr: address to read from
136  * @retval: read into this variable
137  *
138  * Returns 0 on success, or -EFAULT.
139  */
140 #define probe_kernel_address(addr, retval)              \
141         probe_kernel_read(&retval, addr, sizeof(retval))
142
143 #ifndef user_access_begin
144 #define user_access_begin() do { } while (0)
145 #define user_access_end() do { } while (0)
146 #define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0)
147 #define unsafe_put_user(x, ptr, err) do { if (unlikely(__put_user(x, ptr))) goto err; } while (0)
148 #endif
149
150 #endif          /* __LINUX_UACCESS_H__ */