Merge remote-tracking branches 'regulator/topic/act8865', 'regulator/topic/act8945a...
[linux-2.6-block.git] / arch / x86 / include / asm / uaccess_32.h
CommitLineData
1965aae3
PA
1#ifndef _ASM_X86_UACCESS_32_H
2#define _ASM_X86_UACCESS_32_H
1da177e4
LT
3
4/*
5 * User space memory access functions
6 */
1da177e4
LT
7#include <linux/errno.h>
8#include <linux/thread_info.h>
1da177e4 9#include <linux/string.h>
14e6d17d 10#include <asm/asm.h>
1da177e4
LT
11#include <asm/page.h>
12
b1fcec7f
JP
13unsigned long __must_check __copy_to_user_ll
14 (void __user *to, const void *from, unsigned long n);
15unsigned long __must_check __copy_from_user_ll
16 (void *to, const void __user *from, unsigned long n);
17unsigned long __must_check __copy_from_user_ll_nozero
18 (void *to, const void __user *from, unsigned long n);
19unsigned long __must_check __copy_from_user_ll_nocache
20 (void *to, const void __user *from, unsigned long n);
21unsigned long __must_check __copy_from_user_ll_nocache_nozero
22 (void *to, const void __user *from, unsigned long n);
1da177e4 23
6d1c4261
AK
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
4fe48782 35 * so that we don't result in page fault and sleep.
6d1c4261 36 *
1da177e4
LT
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
652050ae 43static __always_inline unsigned long __must_check
1da177e4
LT
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:
de9e478b 51 __uaccess_begin();
b1fcec7f
JP
52 __put_user_size(*(u8 *)from, (u8 __user *)to,
53 1, ret, 1);
de9e478b 54 __uaccess_end();
1da177e4
LT
55 return ret;
56 case 2:
de9e478b 57 __uaccess_begin();
b1fcec7f
JP
58 __put_user_size(*(u16 *)from, (u16 __user *)to,
59 2, ret, 2);
de9e478b 60 __uaccess_end();
1da177e4
LT
61 return ret;
62 case 4:
de9e478b 63 __uaccess_begin();
b1fcec7f
JP
64 __put_user_size(*(u32 *)from, (u32 __user *)to,
65 4, ret, 4);
de9e478b 66 __uaccess_end();
1da177e4 67 return ret;
6a907738 68 case 8:
de9e478b 69 __uaccess_begin();
6a907738
CW
70 __put_user_size(*(u64 *)from, (u64 __user *)to,
71 8, ret, 8);
de9e478b 72 __uaccess_end();
6a907738 73 return ret;
1da177e4
LT
74 }
75 }
76 return __copy_to_user_ll(to, from, n);
77}
78
1da177e4 79/**
9c7fff6e
RD
80 * __copy_to_user: - Copy a block of data into user space, with less checking.
81 * @to: Destination address, in user space.
82 * @from: Source address, in kernel space.
1da177e4
LT
83 * @n: Number of bytes to copy.
84 *
b3c395ef
DH
85 * Context: User context only. This function may sleep if pagefaults are
86 * enabled.
1da177e4 87 *
9c7fff6e 88 * Copy data from kernel space to user space. Caller must check
1da177e4
LT
89 * the specified block with access_ok() before calling this function.
90 *
91 * Returns number of bytes that could not be copied.
92 * On success, this will be zero.
1da177e4 93 */
9c7fff6e
RD
94static __always_inline unsigned long __must_check
95__copy_to_user(void __user *to, const void *from, unsigned long n)
96{
3ee1afa3 97 might_fault();
c10d38dd 98 return __copy_to_user_inatomic(to, from, n);
9c7fff6e
RD
99}
100
652050ae 101static __always_inline unsigned long
1da177e4
LT
102__copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
103{
7c12d811
N
104 /* Avoid zeroing the tail if the copy fails..
105 * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
106 * but as the zeroing behaviour is only significant when n is not
107 * constant, that shouldn't be a problem.
108 */
109 if (__builtin_constant_p(n)) {
110 unsigned long ret;
111
112 switch (n) {
113 case 1:
de9e478b 114 __uaccess_begin();
7c12d811 115 __get_user_size(*(u8 *)to, from, 1, ret, 1);
de9e478b 116 __uaccess_end();
7c12d811
N
117 return ret;
118 case 2:
de9e478b 119 __uaccess_begin();
7c12d811 120 __get_user_size(*(u16 *)to, from, 2, ret, 2);
de9e478b 121 __uaccess_end();
7c12d811
N
122 return ret;
123 case 4:
de9e478b 124 __uaccess_begin();
7c12d811 125 __get_user_size(*(u32 *)to, from, 4, ret, 4);
de9e478b 126 __uaccess_end();
7c12d811
N
127 return ret;
128 }
129 }
130 return __copy_from_user_ll_nozero(to, from, n);
131}
9c7fff6e
RD
132
133/**
134 * __copy_from_user: - Copy a block of data from user space, with less checking.
135 * @to: Destination address, in kernel space.
136 * @from: Source address, in user space.
137 * @n: Number of bytes to copy.
138 *
b3c395ef
DH
139 * Context: User context only. This function may sleep if pagefaults are
140 * enabled.
9c7fff6e
RD
141 *
142 * Copy data from user space to kernel space. Caller must check
143 * the specified block with access_ok() before calling this function.
144 *
145 * Returns number of bytes that could not be copied.
146 * On success, this will be zero.
147 *
148 * If some data could not be copied, this function will pad the copied
149 * data to the requested size using zero bytes.
150 *
151 * An alternate version - __copy_from_user_inatomic() - may be called from
152 * atomic context and will fail rather than sleep. In this case the
153 * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
154 * for explanation of why this is needed.
155 */
7c12d811
N
156static __always_inline unsigned long
157__copy_from_user(void *to, const void __user *from, unsigned long n)
158{
3ee1afa3 159 might_fault();
1da177e4
LT
160 if (__builtin_constant_p(n)) {
161 unsigned long ret;
162
163 switch (n) {
164 case 1:
de9e478b 165 __uaccess_begin();
1da177e4 166 __get_user_size(*(u8 *)to, from, 1, ret, 1);
de9e478b 167 __uaccess_end();
1da177e4
LT
168 return ret;
169 case 2:
de9e478b 170 __uaccess_begin();
1da177e4 171 __get_user_size(*(u16 *)to, from, 2, ret, 2);
de9e478b 172 __uaccess_end();
1da177e4
LT
173 return ret;
174 case 4:
de9e478b 175 __uaccess_begin();
1da177e4 176 __get_user_size(*(u32 *)to, from, 4, ret, 4);
de9e478b 177 __uaccess_end();
1da177e4
LT
178 return ret;
179 }
180 }
181 return __copy_from_user_ll(to, from, n);
182}
183
7c12d811 184static __always_inline unsigned long __copy_from_user_nocache(void *to,
c22ce143
HY
185 const void __user *from, unsigned long n)
186{
3ee1afa3 187 might_fault();
c22ce143
HY
188 if (__builtin_constant_p(n)) {
189 unsigned long ret;
190
191 switch (n) {
192 case 1:
de9e478b 193 __uaccess_begin();
c22ce143 194 __get_user_size(*(u8 *)to, from, 1, ret, 1);
de9e478b 195 __uaccess_end();
c22ce143
HY
196 return ret;
197 case 2:
de9e478b 198 __uaccess_begin();
c22ce143 199 __get_user_size(*(u16 *)to, from, 2, ret, 2);
de9e478b 200 __uaccess_end();
c22ce143
HY
201 return ret;
202 case 4:
de9e478b 203 __uaccess_begin();
c22ce143 204 __get_user_size(*(u32 *)to, from, 4, ret, 4);
de9e478b 205 __uaccess_end();
c22ce143
HY
206 return ret;
207 }
208 }
209 return __copy_from_user_ll_nocache(to, from, n);
210}
211
652050ae 212static __always_inline unsigned long
b1fcec7f
JP
213__copy_from_user_inatomic_nocache(void *to, const void __user *from,
214 unsigned long n)
1da177e4 215{
7c12d811 216 return __copy_from_user_ll_nocache_nozero(to, from, n);
c22ce143
HY
217}
218
1965aae3 219#endif /* _ASM_X86_UACCESS_32_H */