Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __X86_64_UACCESS_H |
| 2 | #define __X86_64_UACCESS_H |
| 3 | |
| 4 | /* |
| 5 | * User space memory access functions |
| 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/compiler.h> |
| 8 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/prefetch.h> |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 10 | #include <linux/lockdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <asm/page.h> |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | /* |
| 14 | * Copy To/From Userspace |
| 15 | */ |
| 16 | |
| 17 | /* Handles exceptions in both to and from, but doesn't do access_ok */ |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 18 | __must_check unsigned long |
| 19 | copy_user_generic(void *to, const void *from, unsigned len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 21 | __must_check unsigned long |
| 22 | copy_to_user(void __user *to, const void *from, unsigned len); |
| 23 | __must_check unsigned long |
| 24 | copy_from_user(void *to, const void __user *from, unsigned len); |
| 25 | __must_check unsigned long |
| 26 | copy_in_user(void __user *to, const void __user *from, unsigned len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 28 | static __always_inline __must_check |
| 29 | int __copy_from_user(void *dst, const void __user *src, unsigned size) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 30 | { |
Andi Kleen | 383d079 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 31 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | if (!__builtin_constant_p(size)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 33 | return copy_user_generic(dst, (__force void *)src, size); |
| 34 | switch (size) { |
| 35 | case 1:__get_user_asm(*(u8 *)dst, (u8 __user *)src, |
| 36 | ret, "b", "b", "=q", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 38 | case 2:__get_user_asm(*(u16 *)dst, (u16 __user *)src, |
| 39 | ret, "w", "w", "=r", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 41 | case 4:__get_user_asm(*(u32 *)dst, (u32 __user *)src, |
| 42 | ret, "l", "k", "=r", 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 44 | case 8:__get_user_asm(*(u64 *)dst, (u64 __user *)src, |
| 45 | ret, "q", "", "=r", 8); |
| 46 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | case 10: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 48 | __get_user_asm(*(u64 *)dst, (u64 __user *)src, |
| 49 | ret, "q", "", "=r", 16); |
| 50 | if (unlikely(ret)) |
| 51 | return ret; |
| 52 | __get_user_asm(*(u16 *)(8 + (char *)dst), |
| 53 | (u16 __user *)(8 + (char __user *)src), |
| 54 | ret, "w", "w", "=r", 2); |
| 55 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | case 16: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 57 | __get_user_asm(*(u64 *)dst, (u64 __user *)src, |
| 58 | ret, "q", "", "=r", 16); |
| 59 | if (unlikely(ret)) |
| 60 | return ret; |
| 61 | __get_user_asm(*(u64 *)(8 + (char *)dst), |
| 62 | (u64 __user *)(8 + (char __user *)src), |
| 63 | ret, "q", "", "=r", 8); |
| 64 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | default: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 66 | return copy_user_generic(dst, (__force void *)src, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 68 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 70 | static __always_inline __must_check |
| 71 | int __copy_to_user(void __user *dst, const void *src, unsigned size) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 72 | { |
Andi Kleen | 383d079 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 73 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (!__builtin_constant_p(size)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 75 | return copy_user_generic((__force void *)dst, src, size); |
| 76 | switch (size) { |
| 77 | case 1:__put_user_asm(*(u8 *)src, (u8 __user *)dst, |
| 78 | ret, "b", "b", "iq", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 80 | case 2:__put_user_asm(*(u16 *)src, (u16 __user *)dst, |
| 81 | ret, "w", "w", "ir", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 83 | case 4:__put_user_asm(*(u32 *)src, (u32 __user *)dst, |
| 84 | ret, "l", "k", "ir", 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 86 | case 8:__put_user_asm(*(u64 *)src, (u64 __user *)dst, |
| 87 | ret, "q", "", "ir", 8); |
| 88 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | case 10: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 90 | __put_user_asm(*(u64 *)src, (u64 __user *)dst, |
| 91 | ret, "q", "", "ir", 10); |
| 92 | if (unlikely(ret)) |
| 93 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | asm("":::"memory"); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 95 | __put_user_asm(4[(u16 *)src], 4 + (u16 __user *)dst, |
| 96 | ret, "w", "w", "ir", 2); |
| 97 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | case 16: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 99 | __put_user_asm(*(u64 *)src, (u64 __user *)dst, |
| 100 | ret, "q", "", "ir", 16); |
| 101 | if (unlikely(ret)) |
| 102 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | asm("":::"memory"); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 104 | __put_user_asm(1[(u64 *)src], 1 + (u64 __user *)dst, |
| 105 | ret, "q", "", "ir", 8); |
| 106 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | default: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 108 | return copy_user_generic((__force void *)dst, src, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 112 | static __always_inline __must_check |
| 113 | int __copy_in_user(void __user *dst, const void __user *src, unsigned size) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 114 | { |
Andi Kleen | 383d079 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 115 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (!__builtin_constant_p(size)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 117 | return copy_user_generic((__force void *)dst, |
| 118 | (__force void *)src, size); |
| 119 | switch (size) { |
| 120 | case 1: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | u8 tmp; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 122 | __get_user_asm(tmp, (u8 __user *)src, |
| 123 | ret, "b", "b", "=q", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (likely(!ret)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 125 | __put_user_asm(tmp, (u8 __user *)dst, |
| 126 | ret, "b", "b", "iq", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return ret; |
| 128 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 129 | case 2: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | u16 tmp; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 131 | __get_user_asm(tmp, (u16 __user *)src, |
| 132 | ret, "w", "w", "=r", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | if (likely(!ret)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 134 | __put_user_asm(tmp, (u16 __user *)dst, |
| 135 | ret, "w", "w", "ir", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return ret; |
| 137 | } |
| 138 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 139 | case 4: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | u32 tmp; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 141 | __get_user_asm(tmp, (u32 __user *)src, |
| 142 | ret, "l", "k", "=r", 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | if (likely(!ret)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 144 | __put_user_asm(tmp, (u32 __user *)dst, |
| 145 | ret, "l", "k", "ir", 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | return ret; |
| 147 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 148 | case 8: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | u64 tmp; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 150 | __get_user_asm(tmp, (u64 __user *)src, |
| 151 | ret, "q", "", "=r", 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (likely(!ret)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 153 | __put_user_asm(tmp, (u64 __user *)dst, |
| 154 | ret, "q", "", "ir", 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | default: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 158 | return copy_user_generic((__force void *)dst, |
| 159 | (__force void *)src, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 163 | __must_check long |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 164 | strncpy_from_user(char *dst, const char __user *src, long count); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 165 | __must_check long |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 166 | __strncpy_from_user(char *dst, const char __user *src, long count); |
| 167 | __must_check long strnlen_user(const char __user *str, long n); |
| 168 | __must_check long __strnlen_user(const char __user *str, long n); |
| 169 | __must_check long strlen_user(const char __user *str); |
| 170 | __must_check unsigned long clear_user(void __user *mem, unsigned long len); |
| 171 | __must_check unsigned long __clear_user(void __user *mem, unsigned long len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 173 | __must_check long __copy_from_user_inatomic(void *dst, const void __user *src, |
| 174 | unsigned size); |
Andi Kleen | b885808 | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 175 | |
| 176 | static __must_check __always_inline int |
| 177 | __copy_to_user_inatomic(void __user *dst, const void *src, unsigned size) |
| 178 | { |
| 179 | return copy_user_generic((__force void *)dst, src, size); |
| 180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 182 | extern long __copy_user_nocache(void *dst, const void __user *src, |
| 183 | unsigned size, int zerorest); |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 184 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 185 | static inline int __copy_from_user_nocache(void *dst, const void __user *src, |
| 186 | unsigned size) |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 187 | { |
| 188 | might_sleep(); |
Al Viro | ecb7524 | 2007-03-14 09:20:20 +0000 | [diff] [blame] | 189 | return __copy_user_nocache(dst, src, size, 1); |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 192 | static inline int __copy_from_user_inatomic_nocache(void *dst, |
| 193 | const void __user *src, |
| 194 | unsigned size) |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 195 | { |
Al Viro | ecb7524 | 2007-03-14 09:20:20 +0000 | [diff] [blame] | 196 | return __copy_user_nocache(dst, src, size, 0); |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Vitaly Mayatskikh | 1129585 | 2008-07-02 15:48:21 +0200 | [diff] [blame] | 199 | unsigned long |
| 200 | copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest); |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | #endif /* __X86_64_UACCESS_H */ |