H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_UACCESS_64_H |
| 2 | #define _ASM_X86_UACCESS_64_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/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; |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 32 | |
| 33 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | if (!__builtin_constant_p(size)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 35 | return copy_user_generic(dst, (__force void *)src, size); |
| 36 | switch (size) { |
| 37 | case 1:__get_user_asm(*(u8 *)dst, (u8 __user *)src, |
| 38 | ret, "b", "b", "=q", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 40 | case 2:__get_user_asm(*(u16 *)dst, (u16 __user *)src, |
| 41 | ret, "w", "w", "=r", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 43 | case 4:__get_user_asm(*(u32 *)dst, (u32 __user *)src, |
| 44 | ret, "l", "k", "=r", 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 46 | case 8:__get_user_asm(*(u64 *)dst, (u64 __user *)src, |
| 47 | ret, "q", "", "=r", 8); |
| 48 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | case 10: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 50 | __get_user_asm(*(u64 *)dst, (u64 __user *)src, |
Hiroshi Shimamoto | 20a4a23 | 2008-11-13 18:06:04 -0800 | [diff] [blame] | 51 | ret, "q", "", "=r", 10); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 52 | if (unlikely(ret)) |
| 53 | return ret; |
| 54 | __get_user_asm(*(u16 *)(8 + (char *)dst), |
| 55 | (u16 __user *)(8 + (char __user *)src), |
| 56 | ret, "w", "w", "=r", 2); |
| 57 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | case 16: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 59 | __get_user_asm(*(u64 *)dst, (u64 __user *)src, |
| 60 | ret, "q", "", "=r", 16); |
| 61 | if (unlikely(ret)) |
| 62 | return ret; |
| 63 | __get_user_asm(*(u64 *)(8 + (char *)dst), |
| 64 | (u64 __user *)(8 + (char __user *)src), |
| 65 | ret, "q", "", "=r", 8); |
| 66 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | default: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 68 | return copy_user_generic(dst, (__force void *)src, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 70 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 72 | static __always_inline __must_check |
| 73 | int __copy_to_user(void __user *dst, const void *src, unsigned size) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 74 | { |
Andi Kleen | 383d079 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 75 | int ret = 0; |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 76 | |
| 77 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | if (!__builtin_constant_p(size)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 79 | return copy_user_generic((__force void *)dst, src, size); |
| 80 | switch (size) { |
| 81 | case 1:__put_user_asm(*(u8 *)src, (u8 __user *)dst, |
| 82 | ret, "b", "b", "iq", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 84 | case 2:__put_user_asm(*(u16 *)src, (u16 __user *)dst, |
| 85 | ret, "w", "w", "ir", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 87 | case 4:__put_user_asm(*(u32 *)src, (u32 __user *)dst, |
| 88 | ret, "l", "k", "ir", 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | return ret; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 90 | case 8:__put_user_asm(*(u64 *)src, (u64 __user *)dst, |
Uros Bizjak | 155b735 | 2009-07-19 18:06:35 +0200 | [diff] [blame] | 91 | ret, "q", "", "er", 8); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 92 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | case 10: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 94 | __put_user_asm(*(u64 *)src, (u64 __user *)dst, |
Uros Bizjak | 155b735 | 2009-07-19 18:06:35 +0200 | [diff] [blame] | 95 | ret, "q", "", "er", 10); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 96 | if (unlikely(ret)) |
| 97 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | asm("":::"memory"); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 99 | __put_user_asm(4[(u16 *)src], 4 + (u16 __user *)dst, |
| 100 | ret, "w", "w", "ir", 2); |
| 101 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | case 16: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 103 | __put_user_asm(*(u64 *)src, (u64 __user *)dst, |
Uros Bizjak | 155b735 | 2009-07-19 18:06:35 +0200 | [diff] [blame] | 104 | ret, "q", "", "er", 16); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 105 | if (unlikely(ret)) |
| 106 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | asm("":::"memory"); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 108 | __put_user_asm(1[(u64 *)src], 1 + (u64 __user *)dst, |
Uros Bizjak | 155b735 | 2009-07-19 18:06:35 +0200 | [diff] [blame] | 109 | ret, "q", "", "er", 8); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 110 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | default: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 112 | return copy_user_generic((__force void *)dst, src, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 116 | static __always_inline __must_check |
| 117 | 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] | 118 | { |
Andi Kleen | 383d079 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 119 | int ret = 0; |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 120 | |
| 121 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (!__builtin_constant_p(size)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 123 | return copy_user_generic((__force void *)dst, |
| 124 | (__force void *)src, size); |
| 125 | switch (size) { |
| 126 | case 1: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | u8 tmp; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 128 | __get_user_asm(tmp, (u8 __user *)src, |
| 129 | ret, "b", "b", "=q", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (likely(!ret)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 131 | __put_user_asm(tmp, (u8 __user *)dst, |
| 132 | ret, "b", "b", "iq", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return ret; |
| 134 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 135 | case 2: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | u16 tmp; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 137 | __get_user_asm(tmp, (u16 __user *)src, |
| 138 | ret, "w", "w", "=r", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (likely(!ret)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 140 | __put_user_asm(tmp, (u16 __user *)dst, |
| 141 | ret, "w", "w", "ir", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return ret; |
| 143 | } |
| 144 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 145 | case 4: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | u32 tmp; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 147 | __get_user_asm(tmp, (u32 __user *)src, |
| 148 | ret, "l", "k", "=r", 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (likely(!ret)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 150 | __put_user_asm(tmp, (u32 __user *)dst, |
| 151 | ret, "l", "k", "ir", 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return ret; |
| 153 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 154 | case 8: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | u64 tmp; |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 156 | __get_user_asm(tmp, (u64 __user *)src, |
| 157 | ret, "q", "", "=r", 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (likely(!ret)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 159 | __put_user_asm(tmp, (u64 __user *)dst, |
Uros Bizjak | 155b735 | 2009-07-19 18:06:35 +0200 | [diff] [blame] | 160 | ret, "q", "", "er", 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return ret; |
| 162 | } |
| 163 | default: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 164 | return copy_user_generic((__force void *)dst, |
| 165 | (__force void *)src, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 169 | __must_check long |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 170 | strncpy_from_user(char *dst, const char __user *src, long count); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 171 | __must_check long |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 172 | __strncpy_from_user(char *dst, const char __user *src, long count); |
| 173 | __must_check long strnlen_user(const char __user *str, long n); |
| 174 | __must_check long __strnlen_user(const char __user *str, long n); |
| 175 | __must_check long strlen_user(const char __user *str); |
| 176 | __must_check unsigned long clear_user(void __user *mem, unsigned long len); |
| 177 | __must_check unsigned long __clear_user(void __user *mem, unsigned long len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 179 | __must_check long __copy_from_user_inatomic(void *dst, const void __user *src, |
| 180 | unsigned size); |
Andi Kleen | b885808 | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 181 | |
| 182 | static __must_check __always_inline int |
| 183 | __copy_to_user_inatomic(void __user *dst, const void *src, unsigned size) |
| 184 | { |
| 185 | return copy_user_generic((__force void *)dst, src, size); |
| 186 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 188 | extern long __copy_user_nocache(void *dst, const void __user *src, |
| 189 | unsigned size, int zerorest); |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 190 | |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 191 | static inline int |
| 192 | __copy_from_user_nocache(void *dst, const void __user *src, unsigned size) |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 193 | { |
| 194 | might_sleep(); |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 195 | return __copy_user_nocache(dst, src, size, 1); |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 198 | static inline int |
| 199 | __copy_from_user_inatomic_nocache(void *dst, const void __user *src, |
| 200 | unsigned size) |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 201 | { |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 202 | return __copy_user_nocache(dst, src, size, 0); |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Vitaly Mayatskikh | 1129585 | 2008-07-02 15:48:21 +0200 | [diff] [blame] | 205 | unsigned long |
| 206 | copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest); |
| 207 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 208 | #endif /* _ASM_X86_UACCESS_64_H */ |