Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / include / linux / uaccess.h
1 #ifndef __LINUX_UACCESS_H__
2 #define __LINUX_UACCESS_H__
3
4 #include <linux/sched.h>
5 #include <asm/uaccess.h>
6
7 static __always_inline void pagefault_disabled_inc(void)
8 {
9         current->pagefault_disabled++;
10 }
11
12 static __always_inline void pagefault_disabled_dec(void)
13 {
14         current->pagefault_disabled--;
15         WARN_ON(current->pagefault_disabled < 0);
16 }
17
18 /*
19  * These routines enable/disable the pagefault handler. If disabled, it will
20  * not take any locks and go straight to the fixup table.
21  *
22  * User access methods will not sleep when called from a pagefault_disabled()
23  * environment.
24  */
25 static inline void pagefault_disable(void)
26 {
27         migrate_disable();
28         pagefault_disabled_inc();
29         /*
30          * make sure to have issued the store before a pagefault
31          * can hit.
32          */
33         barrier();
34 }
35
36 static inline void pagefault_enable(void)
37 {
38         /*
39          * make sure to issue those last loads/stores before enabling
40          * the pagefault handler again.
41          */
42         barrier();
43         pagefault_disabled_dec();
44         migrate_enable();
45 }
46
47 /*
48  * Is the pagefault handler disabled? If so, user access methods will not sleep.
49  */
50 #define pagefault_disabled() (current->pagefault_disabled != 0)
51
52 /*
53  * The pagefault handler is in general disabled by pagefault_disable() or
54  * when in irq context (via in_atomic()).
55  *
56  * This function should only be used by the fault handlers. Other users should
57  * stick to pagefault_disabled().
58  * Please NEVER use preempt_disable() to disable the fault handler. With
59  * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
60  * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
61  */
62 #define faulthandler_disabled() (pagefault_disabled() || in_atomic())
63
64 #ifndef ARCH_HAS_NOCACHE_UACCESS
65
66 static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
67                                 const void __user *from, unsigned long n)
68 {
69         return __copy_from_user_inatomic(to, from, n);
70 }
71
72 static inline unsigned long __copy_from_user_nocache(void *to,
73                                 const void __user *from, unsigned long n)
74 {
75         return __copy_from_user(to, from, n);
76 }
77
78 #endif          /* ARCH_HAS_NOCACHE_UACCESS */
79
80 /**
81  * probe_kernel_address(): safely attempt to read from a location
82  * @addr: address to read from - its type is type typeof(retval)*
83  * @retval: read into this variable
84  *
85  * Safely read from address @addr into variable @revtal.  If a kernel fault
86  * happens, handle that and return -EFAULT.
87  * We ensure that the __get_user() is executed in atomic context so that
88  * do_page_fault() doesn't attempt to take mmap_sem.  This makes
89  * probe_kernel_address() suitable for use within regions where the caller
90  * already holds mmap_sem, or other locks which nest inside mmap_sem.
91  * This must be a macro because __get_user() needs to know the types of the
92  * args.
93  *
94  * We don't include enough header files to be able to do the set_fs().  We
95  * require that the probe_kernel_address() caller will do that.
96  */
97 #define probe_kernel_address(addr, retval)              \
98         ({                                              \
99                 long ret;                               \
100                 mm_segment_t old_fs = get_fs();         \
101                                                         \
102                 set_fs(KERNEL_DS);                      \
103                 pagefault_disable();                    \
104                 ret = __copy_from_user_inatomic(&(retval), (__force typeof(retval) __user *)(addr), sizeof(retval));            \
105                 pagefault_enable();                     \
106                 set_fs(old_fs);                         \
107                 ret;                                    \
108         })
109
110 /*
111  * probe_kernel_read(): safely attempt to read from a location
112  * @dst: pointer to the buffer that shall take the data
113  * @src: address to read from
114  * @size: size of the data chunk
115  *
116  * Safely read from address @src to the buffer at @dst.  If a kernel fault
117  * happens, handle that and return -EFAULT.
118  */
119 extern long probe_kernel_read(void *dst, const void *src, size_t size);
120 extern long __probe_kernel_read(void *dst, const void *src, size_t size);
121
122 /*
123  * probe_kernel_write(): safely attempt to write to a location
124  * @dst: address to write to
125  * @src: pointer to the data that shall be written
126  * @size: size of the data chunk
127  *
128  * Safely write to address @dst from the buffer at @src.  If a kernel fault
129  * happens, handle that and return -EFAULT.
130  */
131 extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
132 extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
133
134 #endif          /* __LINUX_UACCESS_H__ */