Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / arch / x86 / include / asm / uaccess_32.h
1 #ifndef _ASM_X86_UACCESS_32_H
2 #define _ASM_X86_UACCESS_32_H
3
4 /*
5  * User space memory access functions
6  */
7 #include <linux/errno.h>
8 #include <linux/thread_info.h>
9 #include <linux/string.h>
10 #include <asm/asm.h>
11 #include <asm/page.h>
12
13 unsigned long __must_check __copy_to_user_ll
14                 (void __user *to, const void *from, unsigned long n);
15 unsigned long __must_check __copy_from_user_ll
16                 (void *to, const void __user *from, unsigned long n);
17 unsigned long __must_check __copy_from_user_ll_nozero
18                 (void *to, const void __user *from, unsigned long n);
19 unsigned long __must_check __copy_from_user_ll_nocache
20                 (void *to, const void __user *from, unsigned long n);
21 unsigned long __must_check __copy_from_user_ll_nocache_nozero
22                 (void *to, const void __user *from, unsigned long n);
23
24 /**
25  * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
26  * @to:   Destination address, in user space.
27  * @from: Source address, in kernel space.
28  * @n:    Number of bytes to copy.
29  *
30  * Context: User context only.
31  *
32  * Copy data from kernel space to user space.  Caller must check
33  * the specified block with access_ok() before calling this function.
34  * The caller should also make sure he pins the user space address
35  * so that we don't result in page fault and sleep.
36  *
37  * Here we special-case 1, 2 and 4-byte copy_*_user invocations.  On a fault
38  * we return the initial request size (1, 2 or 4), as copy_*_user should do.
39  * If a store crosses a page boundary and gets a fault, the x86 will not write
40  * anything, so this is accurate.
41  */
42
43 static __always_inline unsigned long __must_check
44 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
45 {
46         if (__builtin_constant_p(n)) {
47                 unsigned long ret;
48
49                 switch (n) {
50                 case 1:
51                         __put_user_size(*(u8 *)from, (u8 __user *)to,
52                                         1, ret, 1);
53                         return ret;
54                 case 2:
55                         __put_user_size(*(u16 *)from, (u16 __user *)to,
56                                         2, ret, 2);
57                         return ret;
58                 case 4:
59                         __put_user_size(*(u32 *)from, (u32 __user *)to,
60                                         4, ret, 4);
61                         return ret;
62                 }
63         }
64         return __copy_to_user_ll(to, from, n);
65 }
66
67 /**
68  * __copy_to_user: - Copy a block of data into user space, with less checking.
69  * @to:   Destination address, in user space.
70  * @from: Source address, in kernel space.
71  * @n:    Number of bytes to copy.
72  *
73  * Context: User context only. This function may sleep if pagefaults are
74  *          enabled.
75  *
76  * Copy data from kernel space to user space.  Caller must check
77  * the specified block with access_ok() before calling this function.
78  *
79  * Returns number of bytes that could not be copied.
80  * On success, this will be zero.
81  */
82 static __always_inline unsigned long __must_check
83 __copy_to_user(void __user *to, const void *from, unsigned long n)
84 {
85         might_fault();
86         return __copy_to_user_inatomic(to, from, n);
87 }
88
89 static __always_inline unsigned long
90 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
91 {
92         /* Avoid zeroing the tail if the copy fails..
93          * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
94          * but as the zeroing behaviour is only significant when n is not
95          * constant, that shouldn't be a problem.
96          */
97         if (__builtin_constant_p(n)) {
98                 unsigned long ret;
99
100                 switch (n) {
101                 case 1:
102                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
103                         return ret;
104                 case 2:
105                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
106                         return ret;
107                 case 4:
108                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
109                         return ret;
110                 }
111         }
112         return __copy_from_user_ll_nozero(to, from, n);
113 }
114
115 /**
116  * __copy_from_user: - Copy a block of data from user space, with less checking.
117  * @to:   Destination address, in kernel space.
118  * @from: Source address, in user space.
119  * @n:    Number of bytes to copy.
120  *
121  * Context: User context only. This function may sleep if pagefaults are
122  *          enabled.
123  *
124  * Copy data from user space to kernel space.  Caller must check
125  * the specified block with access_ok() before calling this function.
126  *
127  * Returns number of bytes that could not be copied.
128  * On success, this will be zero.
129  *
130  * If some data could not be copied, this function will pad the copied
131  * data to the requested size using zero bytes.
132  *
133  * An alternate version - __copy_from_user_inatomic() - may be called from
134  * atomic context and will fail rather than sleep.  In this case the
135  * uncopied bytes will *NOT* be padded with zeros.  See fs/filemap.h
136  * for explanation of why this is needed.
137  */
138 static __always_inline unsigned long
139 __copy_from_user(void *to, const void __user *from, unsigned long n)
140 {
141         might_fault();
142         if (__builtin_constant_p(n)) {
143                 unsigned long ret;
144
145                 switch (n) {
146                 case 1:
147                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
148                         return ret;
149                 case 2:
150                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
151                         return ret;
152                 case 4:
153                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
154                         return ret;
155                 }
156         }
157         return __copy_from_user_ll(to, from, n);
158 }
159
160 static __always_inline unsigned long __copy_from_user_nocache(void *to,
161                                 const void __user *from, unsigned long n)
162 {
163         might_fault();
164         if (__builtin_constant_p(n)) {
165                 unsigned long ret;
166
167                 switch (n) {
168                 case 1:
169                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
170                         return ret;
171                 case 2:
172                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
173                         return ret;
174                 case 4:
175                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
176                         return ret;
177                 }
178         }
179         return __copy_from_user_ll_nocache(to, from, n);
180 }
181
182 static __always_inline unsigned long
183 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
184                                   unsigned long n)
185 {
186        return __copy_from_user_ll_nocache_nozero(to, from, n);
187 }
188
189 #endif /* _ASM_X86_UACCESS_32_H */