H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_UACCESS_32_H |
| 2 | #define _ASM_X86_UACCESS_32_H |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | |
| 4 | /* |
| 5 | * User space memory access functions |
| 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/errno.h> |
| 8 | #include <linux/thread_info.h> |
| 9 | #include <linux/prefetch.h> |
| 10 | #include <linux/string.h> |
H. Peter Anvin | 14e6d17 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 11 | #include <asm/asm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/page.h> |
| 13 | |
Joe Perches | b1fcec7 | 2008-03-23 01:03:48 -0700 | [diff] [blame] | 14 | unsigned long __must_check __copy_to_user_ll |
| 15 | (void __user *to, const void *from, unsigned long n); |
| 16 | unsigned long __must_check __copy_from_user_ll |
| 17 | (void *to, const void __user *from, unsigned long n); |
| 18 | unsigned long __must_check __copy_from_user_ll_nozero |
| 19 | (void *to, const void __user *from, unsigned long n); |
| 20 | unsigned long __must_check __copy_from_user_ll_nocache |
| 21 | (void *to, const void __user *from, unsigned long n); |
| 22 | unsigned long __must_check __copy_from_user_ll_nocache_nozero |
| 23 | (void *to, const void __user *from, unsigned long n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Aneesh Kumar K.V | 6d1c426 | 2007-05-02 19:27:06 +0200 | [diff] [blame] | 25 | /** |
| 26 | * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking. |
| 27 | * @to: Destination address, in user space. |
| 28 | * @from: Source address, in kernel space. |
| 29 | * @n: Number of bytes to copy. |
| 30 | * |
| 31 | * Context: User context only. |
| 32 | * |
| 33 | * Copy data from kernel space to user space. Caller must check |
| 34 | * the specified block with access_ok() before calling this function. |
| 35 | * The caller should also make sure he pins the user space address |
| 36 | * so that the we don't result in page fault and sleep. |
| 37 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault |
| 39 | * we return the initial request size (1, 2 or 4), as copy_*_user should do. |
| 40 | * If a store crosses a page boundary and gets a fault, the x86 will not write |
| 41 | * anything, so this is accurate. |
| 42 | */ |
| 43 | |
Ingo Molnar | 652050a | 2006-01-14 13:21:30 -0800 | [diff] [blame] | 44 | static __always_inline unsigned long __must_check |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n) |
| 46 | { |
| 47 | if (__builtin_constant_p(n)) { |
| 48 | unsigned long ret; |
| 49 | |
| 50 | switch (n) { |
| 51 | case 1: |
Joe Perches | b1fcec7 | 2008-03-23 01:03:48 -0700 | [diff] [blame] | 52 | __put_user_size(*(u8 *)from, (u8 __user *)to, |
| 53 | 1, ret, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | return ret; |
| 55 | case 2: |
Joe Perches | b1fcec7 | 2008-03-23 01:03:48 -0700 | [diff] [blame] | 56 | __put_user_size(*(u16 *)from, (u16 __user *)to, |
| 57 | 2, ret, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | return ret; |
| 59 | case 4: |
Joe Perches | b1fcec7 | 2008-03-23 01:03:48 -0700 | [diff] [blame] | 60 | __put_user_size(*(u32 *)from, (u32 __user *)to, |
| 61 | 4, ret, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return ret; |
| 63 | } |
| 64 | } |
| 65 | return __copy_to_user_ll(to, from, n); |
| 66 | } |
| 67 | |
Randy Dunlap | 9c7fff6 | 2006-10-11 01:22:10 -0700 | [diff] [blame] | 68 | /** |
| 69 | * __copy_to_user: - Copy a block of data into user space, with less checking. |
| 70 | * @to: Destination address, in user space. |
| 71 | * @from: Source address, in kernel space. |
| 72 | * @n: Number of bytes to copy. |
| 73 | * |
| 74 | * Context: User context only. This function may sleep. |
| 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 | */ |
Ingo Molnar | 652050a | 2006-01-14 13:21:30 -0800 | [diff] [blame] | 82 | static __always_inline unsigned long __must_check |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | __copy_to_user(void __user *to, const void *from, unsigned long n) |
| 84 | { |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 85 | might_fault(); |
| 86 | return __copy_to_user_inatomic(to, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Ingo Molnar | 652050a | 2006-01-14 13:21:30 -0800 | [diff] [blame] | 89 | static __always_inline unsigned long |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n) |
| 91 | { |
NeilBrown | 7c12d81 | 2006-06-25 05:48:02 -0700 | [diff] [blame] | 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 | } |
Randy Dunlap | 9c7fff6 | 2006-10-11 01:22:10 -0700 | [diff] [blame] | 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. |
| 122 | * |
| 123 | * Copy data from user space to kernel space. Caller must check |
| 124 | * the specified block with access_ok() before calling this function. |
| 125 | * |
| 126 | * Returns number of bytes that could not be copied. |
| 127 | * On success, this will be zero. |
| 128 | * |
| 129 | * If some data could not be copied, this function will pad the copied |
| 130 | * data to the requested size using zero bytes. |
| 131 | * |
| 132 | * An alternate version - __copy_from_user_inatomic() - may be called from |
| 133 | * atomic context and will fail rather than sleep. In this case the |
| 134 | * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h |
| 135 | * for explanation of why this is needed. |
| 136 | */ |
NeilBrown | 7c12d81 | 2006-06-25 05:48:02 -0700 | [diff] [blame] | 137 | static __always_inline unsigned long |
| 138 | __copy_from_user(void *to, const void __user *from, unsigned long n) |
| 139 | { |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 140 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | if (__builtin_constant_p(n)) { |
| 142 | unsigned long ret; |
| 143 | |
| 144 | switch (n) { |
| 145 | case 1: |
| 146 | __get_user_size(*(u8 *)to, from, 1, ret, 1); |
| 147 | return ret; |
| 148 | case 2: |
| 149 | __get_user_size(*(u16 *)to, from, 2, ret, 2); |
| 150 | return ret; |
| 151 | case 4: |
| 152 | __get_user_size(*(u32 *)to, from, 4, ret, 4); |
| 153 | return ret; |
| 154 | } |
| 155 | } |
| 156 | return __copy_from_user_ll(to, from, n); |
| 157 | } |
| 158 | |
NeilBrown | 7c12d81 | 2006-06-25 05:48:02 -0700 | [diff] [blame] | 159 | static __always_inline unsigned long __copy_from_user_nocache(void *to, |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 160 | const void __user *from, unsigned long n) |
| 161 | { |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 162 | might_fault(); |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 163 | if (__builtin_constant_p(n)) { |
| 164 | unsigned long ret; |
| 165 | |
| 166 | switch (n) { |
| 167 | case 1: |
| 168 | __get_user_size(*(u8 *)to, from, 1, ret, 1); |
| 169 | return ret; |
| 170 | case 2: |
| 171 | __get_user_size(*(u16 *)to, from, 2, ret, 2); |
| 172 | return ret; |
| 173 | case 4: |
| 174 | __get_user_size(*(u32 *)to, from, 4, ret, 4); |
| 175 | return ret; |
| 176 | } |
| 177 | } |
| 178 | return __copy_from_user_ll_nocache(to, from, n); |
| 179 | } |
| 180 | |
Ingo Molnar | 652050a | 2006-01-14 13:21:30 -0800 | [diff] [blame] | 181 | static __always_inline unsigned long |
Joe Perches | b1fcec7 | 2008-03-23 01:03:48 -0700 | [diff] [blame] | 182 | __copy_from_user_inatomic_nocache(void *to, const void __user *from, |
| 183 | unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
NeilBrown | 7c12d81 | 2006-06-25 05:48:02 -0700 | [diff] [blame] | 185 | return __copy_from_user_ll_nocache_nozero(to, from, n); |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | unsigned long __must_check copy_to_user(void __user *to, |
Joe Perches | b1fcec7 | 2008-03-23 01:03:48 -0700 | [diff] [blame] | 189 | const void *from, unsigned long n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | unsigned long __must_check copy_from_user(void *to, |
Joe Perches | b1fcec7 | 2008-03-23 01:03:48 -0700 | [diff] [blame] | 191 | const void __user *from, |
| 192 | unsigned long n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | long __must_check strncpy_from_user(char *dst, const char __user *src, |
Joe Perches | b1fcec7 | 2008-03-23 01:03:48 -0700 | [diff] [blame] | 194 | long count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | long __must_check __strncpy_from_user(char *dst, |
Joe Perches | b1fcec7 | 2008-03-23 01:03:48 -0700 | [diff] [blame] | 196 | const char __user *src, long count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * strlen_user: - Get the size of a string in user space. |
| 200 | * @str: The string to measure. |
| 201 | * |
| 202 | * Context: User context only. This function may sleep. |
| 203 | * |
| 204 | * Get the size of a NUL-terminated string in user space. |
| 205 | * |
| 206 | * Returns the size of the string INCLUDING the terminating NUL. |
| 207 | * On exception, returns 0. |
| 208 | * |
| 209 | * If there is a limit on the length of a valid string, you may wish to |
| 210 | * consider using strnlen_user() instead. |
| 211 | */ |
Robert P. J. Day | 48dd934 | 2007-07-21 17:11:26 +0200 | [diff] [blame] | 212 | #define strlen_user(str) strnlen_user(str, LONG_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | long strnlen_user(const char __user *str, long n); |
| 215 | unsigned long __must_check clear_user(void __user *mem, unsigned long len); |
| 216 | unsigned long __must_check __clear_user(void __user *mem, unsigned long len); |
| 217 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 218 | #endif /* _ASM_X86_UACCESS_32_H */ |