blob: 566e803cc6026a11a1391fd25051717cef3f5979 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_UACCESS_32_H
2#define _ASM_X86_UACCESS_32_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4/*
5 * User space memory access functions
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/errno.h>
8#include <linux/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/string.h>
H. Peter Anvin14e6d172008-02-04 16:47:59 +010010#include <asm/asm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/page.h>
12
Joe Perchesb1fcec72008-03-23 01:03:48 -070013unsigned 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Aneesh Kumar K.V6d1c4262007-05-02 19:27:06 +020024/**
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
Sergey Senozhatsky4fe48782009-09-17 15:54:01 +030035 * so that we don't result in page fault and sleep.
Aneesh Kumar K.V6d1c4262007-05-02 19:27:06 +020036 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * 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
Ingo Molnar652050a2006-01-14 13:21:30 -080043static __always_inline unsigned long __must_check
Linus Torvalds1da177e2005-04-16 15:20:36 -070044__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:
Joe Perchesb1fcec72008-03-23 01:03:48 -070051 __put_user_size(*(u8 *)from, (u8 __user *)to,
52 1, ret, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return ret;
54 case 2:
Joe Perchesb1fcec72008-03-23 01:03:48 -070055 __put_user_size(*(u16 *)from, (u16 __user *)to,
56 2, ret, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return ret;
58 case 4:
Joe Perchesb1fcec72008-03-23 01:03:48 -070059 __put_user_size(*(u32 *)from, (u32 __user *)to,
60 4, ret, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return ret;
62 }
63 }
64 return __copy_to_user_ll(to, from, n);
65}
66
Randy Dunlap9c7fff62006-10-11 01:22:10 -070067/**
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.
74 *
75 * Copy data from kernel space to user space. Caller must check
76 * the specified block with access_ok() before calling this function.
77 *
78 * Returns number of bytes that could not be copied.
79 * On success, this will be zero.
80 */
Ingo Molnar652050a2006-01-14 13:21:30 -080081static __always_inline unsigned long __must_check
Linus Torvalds1da177e2005-04-16 15:20:36 -070082__copy_to_user(void __user *to, const void *from, unsigned long n)
83{
Ingo Molnard1a76182008-10-28 16:54:49 +010084 might_fault();
85 return __copy_to_user_inatomic(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Ingo Molnar652050a2006-01-14 13:21:30 -080088static __always_inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -070089__copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
90{
NeilBrown7c12d812006-06-25 05:48:02 -070091 /* Avoid zeroing the tail if the copy fails..
92 * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
93 * but as the zeroing behaviour is only significant when n is not
94 * constant, that shouldn't be a problem.
95 */
96 if (__builtin_constant_p(n)) {
97 unsigned long ret;
98
99 switch (n) {
100 case 1:
101 __get_user_size(*(u8 *)to, from, 1, ret, 1);
102 return ret;
103 case 2:
104 __get_user_size(*(u16 *)to, from, 2, ret, 2);
105 return ret;
106 case 4:
107 __get_user_size(*(u32 *)to, from, 4, ret, 4);
108 return ret;
109 }
110 }
111 return __copy_from_user_ll_nozero(to, from, n);
112}
Randy Dunlap9c7fff62006-10-11 01:22:10 -0700113
114/**
115 * __copy_from_user: - Copy a block of data from user space, with less checking.
116 * @to: Destination address, in kernel space.
117 * @from: Source address, in user space.
118 * @n: Number of bytes to copy.
119 *
120 * Context: User context only. This function may sleep.
121 *
122 * Copy data from user space to kernel space. Caller must check
123 * the specified block with access_ok() before calling this function.
124 *
125 * Returns number of bytes that could not be copied.
126 * On success, this will be zero.
127 *
128 * If some data could not be copied, this function will pad the copied
129 * data to the requested size using zero bytes.
130 *
131 * An alternate version - __copy_from_user_inatomic() - may be called from
132 * atomic context and will fail rather than sleep. In this case the
133 * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
134 * for explanation of why this is needed.
135 */
NeilBrown7c12d812006-06-25 05:48:02 -0700136static __always_inline unsigned long
137__copy_from_user(void *to, const void __user *from, unsigned long n)
138{
Ingo Molnard1a76182008-10-28 16:54:49 +0100139 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (__builtin_constant_p(n)) {
141 unsigned long ret;
142
143 switch (n) {
144 case 1:
145 __get_user_size(*(u8 *)to, from, 1, ret, 1);
146 return ret;
147 case 2:
148 __get_user_size(*(u16 *)to, from, 2, ret, 2);
149 return ret;
150 case 4:
151 __get_user_size(*(u32 *)to, from, 4, ret, 4);
152 return ret;
153 }
154 }
155 return __copy_from_user_ll(to, from, n);
156}
157
NeilBrown7c12d812006-06-25 05:48:02 -0700158static __always_inline unsigned long __copy_from_user_nocache(void *to,
Hiro Yoshiokac22ce142006-06-23 02:04:16 -0700159 const void __user *from, unsigned long n)
160{
Ingo Molnard1a76182008-10-28 16:54:49 +0100161 might_fault();
Hiro Yoshiokac22ce142006-06-23 02:04:16 -0700162 if (__builtin_constant_p(n)) {
163 unsigned long ret;
164
165 switch (n) {
166 case 1:
167 __get_user_size(*(u8 *)to, from, 1, ret, 1);
168 return ret;
169 case 2:
170 __get_user_size(*(u16 *)to, from, 2, ret, 2);
171 return ret;
172 case 4:
173 __get_user_size(*(u32 *)to, from, 4, ret, 4);
174 return ret;
175 }
176 }
177 return __copy_from_user_ll_nocache(to, from, n);
178}
179
Ingo Molnar652050a2006-01-14 13:21:30 -0800180static __always_inline unsigned long
Joe Perchesb1fcec72008-03-23 01:03:48 -0700181__copy_from_user_inatomic_nocache(void *to, const void __user *from,
182 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
NeilBrown7c12d812006-06-25 05:48:02 -0700184 return __copy_from_user_ll_nocache_nozero(to, from, n);
Hiro Yoshiokac22ce142006-06-23 02:04:16 -0700185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187unsigned long __must_check copy_to_user(void __user *to,
Joe Perchesb1fcec72008-03-23 01:03:48 -0700188 const void *from, unsigned long n);
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +0200189unsigned long __must_check _copy_from_user(void *to,
Joe Perchesb1fcec72008-03-23 01:03:48 -0700190 const void __user *from,
191 unsigned long n);
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +0200192
Arjan van de Ven4a312762009-09-30 13:05:23 +0200193
194extern void copy_from_user_overflow(void)
Arjan van de Ven63312b62009-10-02 07:50:50 -0700195#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
196 __compiletime_error("copy_from_user() buffer size is not provably correct")
197#else
Arjan van de Ven4a312762009-09-30 13:05:23 +0200198 __compiletime_warning("copy_from_user() buffer size is not provably correct")
199#endif
200;
201
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +0200202static inline unsigned long __must_check copy_from_user(void *to,
203 const void __user *from,
204 unsigned long n)
205{
206 int sz = __compiletime_object_size(to);
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +0200207
208 if (likely(sz == -1 || sz >= n))
Heiko Carstens409d02e2010-01-05 14:19:11 +0100209 n = _copy_from_user(to, from, n);
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +0200210 else
Arjan van de Ven4a312762009-09-30 13:05:23 +0200211 copy_from_user_overflow();
212
Heiko Carstens409d02e2010-01-05 14:19:11 +0100213 return n;
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +0200214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216long __must_check strncpy_from_user(char *dst, const char __user *src,
Joe Perchesb1fcec72008-03-23 01:03:48 -0700217 long count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218long __must_check __strncpy_from_user(char *dst,
Joe Perchesb1fcec72008-03-23 01:03:48 -0700219 const char __user *src, long count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/**
222 * strlen_user: - Get the size of a string in user space.
223 * @str: The string to measure.
224 *
225 * Context: User context only. This function may sleep.
226 *
227 * Get the size of a NUL-terminated string in user space.
228 *
229 * Returns the size of the string INCLUDING the terminating NUL.
230 * On exception, returns 0.
231 *
232 * If there is a limit on the length of a valid string, you may wish to
233 * consider using strnlen_user() instead.
234 */
Robert P. J. Day48dd9342007-07-21 17:11:26 +0200235#define strlen_user(str) strnlen_user(str, LONG_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237long strnlen_user(const char __user *str, long n);
238unsigned long __must_check clear_user(void __user *mem, unsigned long len);
239unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
240
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700241#endif /* _ASM_X86_UACCESS_32_H */