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> |
Nick Piggin | 16dbc6c | 2008-10-02 14:50:12 -0700 | [diff] [blame] | 8 | #include <linux/lockdep.h> |
Andrey Ryabinin | 1771c6e | 2016-05-20 16:59:31 -0700 | [diff] [blame] | 9 | #include <linux/kasan-checks.h> |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame] | 10 | #include <asm/alternative.h> |
Borislav Petkov | cd4d09e | 2016-01-26 22:12:04 +0100 | [diff] [blame] | 11 | #include <asm/cpufeatures.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/page.h> |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | /* |
| 15 | * Copy To/From Userspace |
| 16 | */ |
| 17 | |
| 18 | /* 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] | 19 | __must_check unsigned long |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 20 | copy_user_enhanced_fast_string(void *to, const void *from, unsigned len); |
| 21 | __must_check unsigned long |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame] | 22 | copy_user_generic_string(void *to, const void *from, unsigned len); |
| 23 | __must_check unsigned long |
| 24 | copy_user_generic_unrolled(void *to, const void *from, unsigned len); |
| 25 | |
| 26 | static __always_inline __must_check unsigned long |
| 27 | copy_user_generic(void *to, const void *from, unsigned len) |
| 28 | { |
| 29 | unsigned ret; |
| 30 | |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 31 | /* |
| 32 | * If CPU has ERMS feature, use copy_user_enhanced_fast_string. |
| 33 | * Otherwise, if CPU has rep_good feature, use copy_user_generic_string. |
| 34 | * Otherwise, use copy_user_generic_unrolled. |
| 35 | */ |
| 36 | alternative_call_2(copy_user_generic_unrolled, |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame] | 37 | copy_user_generic_string, |
| 38 | X86_FEATURE_REP_GOOD, |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 39 | copy_user_enhanced_fast_string, |
| 40 | X86_FEATURE_ERMS, |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame] | 41 | ASM_OUTPUT2("=a" (ret), "=D" (to), "=S" (from), |
| 42 | "=d" (len)), |
| 43 | "1" (to), "2" (from), "3" (len) |
| 44 | : "memory", "rcx", "r8", "r9", "r10", "r11"); |
| 45 | return ret; |
| 46 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Al Viro | beba3a2 | 2017-03-25 19:33:21 -0400 | [diff] [blame] | 48 | static __always_inline __must_check unsigned long |
| 49 | raw_copy_from_user(void *dst, const void __user *src, unsigned long size) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 50 | { |
Andi Kleen | 383d079 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 51 | int ret = 0; |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if (!__builtin_constant_p(size)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 54 | return copy_user_generic(dst, (__force void *)src, size); |
| 55 | switch (size) { |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 56 | case 1: |
| 57 | __uaccess_begin(); |
Al Viro | 122b05d | 2017-03-25 18:36:22 -0400 | [diff] [blame] | 58 | __get_user_asm_nozero(*(u8 *)dst, (u8 __user *)src, |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 59 | ret, "b", "b", "=q", 1); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 60 | __uaccess_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return ret; |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 62 | case 2: |
| 63 | __uaccess_begin(); |
Al Viro | 122b05d | 2017-03-25 18:36:22 -0400 | [diff] [blame] | 64 | __get_user_asm_nozero(*(u16 *)dst, (u16 __user *)src, |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 65 | ret, "w", "w", "=r", 2); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 66 | __uaccess_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return ret; |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 68 | case 4: |
| 69 | __uaccess_begin(); |
Al Viro | 122b05d | 2017-03-25 18:36:22 -0400 | [diff] [blame] | 70 | __get_user_asm_nozero(*(u32 *)dst, (u32 __user *)src, |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 71 | ret, "l", "k", "=r", 4); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 72 | __uaccess_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | return ret; |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 74 | case 8: |
| 75 | __uaccess_begin(); |
Al Viro | 122b05d | 2017-03-25 18:36:22 -0400 | [diff] [blame] | 76 | __get_user_asm_nozero(*(u64 *)dst, (u64 __user *)src, |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 77 | ret, "q", "", "=r", 8); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 78 | __uaccess_end(); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 79 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | case 10: |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 81 | __uaccess_begin(); |
Al Viro | 122b05d | 2017-03-25 18:36:22 -0400 | [diff] [blame] | 82 | __get_user_asm_nozero(*(u64 *)dst, (u64 __user *)src, |
Hiroshi Shimamoto | 20a4a23 | 2008-11-13 18:06:04 -0800 | [diff] [blame] | 83 | ret, "q", "", "=r", 10); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 84 | if (likely(!ret)) |
Al Viro | 122b05d | 2017-03-25 18:36:22 -0400 | [diff] [blame] | 85 | __get_user_asm_nozero(*(u16 *)(8 + (char *)dst), |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 86 | (u16 __user *)(8 + (char __user *)src), |
| 87 | ret, "w", "w", "=r", 2); |
| 88 | __uaccess_end(); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 89 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | case 16: |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 91 | __uaccess_begin(); |
Al Viro | 122b05d | 2017-03-25 18:36:22 -0400 | [diff] [blame] | 92 | __get_user_asm_nozero(*(u64 *)dst, (u64 __user *)src, |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 93 | ret, "q", "", "=r", 16); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 94 | if (likely(!ret)) |
Al Viro | 122b05d | 2017-03-25 18:36:22 -0400 | [diff] [blame] | 95 | __get_user_asm_nozero(*(u64 *)(8 + (char *)dst), |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 96 | (u64 __user *)(8 + (char __user *)src), |
| 97 | ret, "q", "", "=r", 8); |
| 98 | __uaccess_end(); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 99 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | default: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 101 | return copy_user_generic(dst, (__force void *)src, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Al Viro | beba3a2 | 2017-03-25 19:33:21 -0400 | [diff] [blame] | 105 | static __always_inline __must_check unsigned long |
| 106 | raw_copy_to_user(void __user *dst, const void *src, unsigned long size) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 107 | { |
Andi Kleen | 383d079 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 108 | int ret = 0; |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (!__builtin_constant_p(size)) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 111 | return copy_user_generic((__force void *)dst, src, size); |
| 112 | switch (size) { |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 113 | case 1: |
| 114 | __uaccess_begin(); |
| 115 | __put_user_asm(*(u8 *)src, (u8 __user *)dst, |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 116 | ret, "b", "b", "iq", 1); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 117 | __uaccess_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return ret; |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 119 | case 2: |
| 120 | __uaccess_begin(); |
| 121 | __put_user_asm(*(u16 *)src, (u16 __user *)dst, |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 122 | ret, "w", "w", "ir", 2); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 123 | __uaccess_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return ret; |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 125 | case 4: |
| 126 | __uaccess_begin(); |
| 127 | __put_user_asm(*(u32 *)src, (u32 __user *)dst, |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 128 | ret, "l", "k", "ir", 4); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 129 | __uaccess_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | return ret; |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 131 | case 8: |
| 132 | __uaccess_begin(); |
| 133 | __put_user_asm(*(u64 *)src, (u64 __user *)dst, |
Uros Bizjak | 155b735 | 2009-07-19 18:06:35 +0200 | [diff] [blame] | 134 | ret, "q", "", "er", 8); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 135 | __uaccess_end(); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 136 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | case 10: |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 138 | __uaccess_begin(); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 139 | __put_user_asm(*(u64 *)src, (u64 __user *)dst, |
Uros Bizjak | 155b735 | 2009-07-19 18:06:35 +0200 | [diff] [blame] | 140 | ret, "q", "", "er", 10); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 141 | if (likely(!ret)) { |
| 142 | asm("":::"memory"); |
| 143 | __put_user_asm(4[(u16 *)src], 4 + (u16 __user *)dst, |
| 144 | ret, "w", "w", "ir", 2); |
| 145 | } |
| 146 | __uaccess_end(); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 147 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | case 16: |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 149 | __uaccess_begin(); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 150 | __put_user_asm(*(u64 *)src, (u64 __user *)dst, |
Uros Bizjak | 155b735 | 2009-07-19 18:06:35 +0200 | [diff] [blame] | 151 | ret, "q", "", "er", 16); |
Linus Torvalds | 11f1a4b | 2015-12-17 09:45:09 -0800 | [diff] [blame] | 152 | if (likely(!ret)) { |
| 153 | asm("":::"memory"); |
| 154 | __put_user_asm(1[(u64 *)src], 1 + (u64 __user *)dst, |
| 155 | ret, "q", "", "er", 8); |
| 156 | } |
| 157 | __uaccess_end(); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 158 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | default: |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 160 | return copy_user_generic((__force void *)dst, src, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Andi Kleen | 9591200 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 164 | static __always_inline __must_check |
Al Viro | beba3a2 | 2017-03-25 19:33:21 -0400 | [diff] [blame] | 165 | unsigned long raw_copy_in_user(void __user *dst, const void __user *src, unsigned long size) |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 166 | { |
Al Viro | a41e0d7 | 2017-03-25 19:38:23 -0400 | [diff] [blame] | 167 | return copy_user_generic((__force void *)dst, |
| 168 | (__force void *)src, size); |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Joe Perches | b896313 | 2008-03-23 01:03:49 -0700 | [diff] [blame] | 171 | extern long __copy_user_nocache(void *dst, const void __user *src, |
| 172 | unsigned size, int zerorest); |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 173 | |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame^] | 174 | extern long __copy_user_flushcache(void *dst, const void __user *src, unsigned size); |
| 175 | extern void memcpy_page_flushcache(char *to, struct page *page, size_t offset, |
| 176 | size_t len); |
| 177 | |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 178 | static inline int |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 179 | __copy_from_user_inatomic_nocache(void *dst, const void __user *src, |
| 180 | unsigned size) |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 181 | { |
Andrey Ryabinin | 1771c6e | 2016-05-20 16:59:31 -0700 | [diff] [blame] | 182 | kasan_check_write(dst, size); |
Ingo Molnar | f180053 | 2009-03-02 11:00:57 +0100 | [diff] [blame] | 183 | return __copy_user_nocache(dst, src, size, 0); |
Andi Kleen | 0812a57 | 2007-02-13 13:26:19 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame^] | 186 | static inline int |
| 187 | __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size) |
| 188 | { |
| 189 | kasan_check_write(dst, size); |
| 190 | return __copy_user_flushcache(dst, src, size); |
| 191 | } |
| 192 | |
Vitaly Mayatskikh | 1129585 | 2008-07-02 15:48:21 +0200 | [diff] [blame] | 193 | unsigned long |
Linus Torvalds | cae2a17 | 2015-04-06 10:26:17 -0700 | [diff] [blame] | 194 | copy_user_handle_tail(char *to, char *from, unsigned len); |
Vitaly Mayatskikh | 1129585 | 2008-07-02 15:48:21 +0200 | [diff] [blame] | 195 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 196 | #endif /* _ASM_X86_UACCESS_64_H */ |