blob: 316708d5af92d2c5ecd2f7e22e0bc1852b003d7d [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_UACCESS_64_H
2#define _ASM_X86_UACCESS_64_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/compiler.h>
8#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/prefetch.h>
Nick Piggin16dbc6c2008-10-02 14:50:12 -070010#include <linux/lockdep.h>
Jan Beulich1b1d9252009-12-18 16:12:56 +000011#include <asm/alternative.h>
12#include <asm/cpufeature.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/page.h>
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015/*
16 * Copy To/From Userspace
17 */
18
19/* Handles exceptions in both to and from, but doesn't do access_ok */
Andi Kleen95912002006-09-26 10:52:39 +020020__must_check unsigned long
Jan Beulich1b1d9252009-12-18 16:12:56 +000021copy_user_generic_string(void *to, const void *from, unsigned len);
22__must_check unsigned long
23copy_user_generic_unrolled(void *to, const void *from, unsigned len);
24
25static __always_inline __must_check unsigned long
26copy_user_generic(void *to, const void *from, unsigned len)
27{
28 unsigned ret;
29
30 alternative_call(copy_user_generic_unrolled,
31 copy_user_generic_string,
32 X86_FEATURE_REP_GOOD,
33 ASM_OUTPUT2("=a" (ret), "=D" (to), "=S" (from),
34 "=d" (len)),
35 "1" (to), "2" (from), "3" (len)
36 : "memory", "rcx", "r8", "r9", "r10", "r11");
37 return ret;
38}
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Andi Kleen95912002006-09-26 10:52:39 +020040__must_check unsigned long
Frederic Weisbecker3c93ca02009-11-16 15:42:18 +010041_copy_to_user(void __user *to, const void *from, unsigned len);
Andi Kleen95912002006-09-26 10:52:39 +020042__must_check unsigned long
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020043_copy_from_user(void *to, const void __user *from, unsigned len);
Andi Kleen95912002006-09-26 10:52:39 +020044__must_check unsigned long
45copy_in_user(void __user *to, const void __user *from, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020047static inline unsigned long __must_check copy_from_user(void *to,
48 const void __user *from,
49 unsigned long n)
50{
51 int sz = __compiletime_object_size(to);
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020052
Frederic Weisbecker3c93ca02009-11-16 15:42:18 +010053 might_fault();
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020054 if (likely(sz == -1 || sz >= n))
Heiko Carstens409d02e2010-01-05 14:19:11 +010055 n = _copy_from_user(to, from, n);
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020056#ifdef CONFIG_DEBUG_VM
57 else
58 WARN(1, "Buffer overflow detected!\n");
59#endif
Heiko Carstens409d02e2010-01-05 14:19:11 +010060 return n;
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020061}
62
Frederic Weisbecker3c93ca02009-11-16 15:42:18 +010063static __always_inline __must_check
64int copy_to_user(void __user *dst, const void *src, unsigned size)
65{
66 might_fault();
67
68 return _copy_to_user(dst, src, size);
69}
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020070
Andi Kleen95912002006-09-26 10:52:39 +020071static __always_inline __must_check
72int __copy_from_user(void *dst, const void __user *src, unsigned size)
Joe Perchesb8963132008-03-23 01:03:49 -070073{
Andi Kleen383d0792006-09-26 10:52:40 +020074 int ret = 0;
Ingo Molnard1a76182008-10-28 16:54:49 +010075
76 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!__builtin_constant_p(size))
Joe Perchesb8963132008-03-23 01:03:49 -070078 return copy_user_generic(dst, (__force void *)src, size);
79 switch (size) {
80 case 1:__get_user_asm(*(u8 *)dst, (u8 __user *)src,
81 ret, "b", "b", "=q", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return ret;
Joe Perchesb8963132008-03-23 01:03:49 -070083 case 2:__get_user_asm(*(u16 *)dst, (u16 __user *)src,
84 ret, "w", "w", "=r", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return ret;
Joe Perchesb8963132008-03-23 01:03:49 -070086 case 4:__get_user_asm(*(u32 *)dst, (u32 __user *)src,
87 ret, "l", "k", "=r", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return ret;
Joe Perchesb8963132008-03-23 01:03:49 -070089 case 8:__get_user_asm(*(u64 *)dst, (u64 __user *)src,
90 ret, "q", "", "=r", 8);
91 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 case 10:
Joe Perchesb8963132008-03-23 01:03:49 -070093 __get_user_asm(*(u64 *)dst, (u64 __user *)src,
Hiroshi Shimamoto20a4a232008-11-13 18:06:04 -080094 ret, "q", "", "=r", 10);
Joe Perchesb8963132008-03-23 01:03:49 -070095 if (unlikely(ret))
96 return ret;
97 __get_user_asm(*(u16 *)(8 + (char *)dst),
98 (u16 __user *)(8 + (char __user *)src),
99 ret, "w", "w", "=r", 2);
100 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 case 16:
Joe Perchesb8963132008-03-23 01:03:49 -0700102 __get_user_asm(*(u64 *)dst, (u64 __user *)src,
103 ret, "q", "", "=r", 16);
104 if (unlikely(ret))
105 return ret;
106 __get_user_asm(*(u64 *)(8 + (char *)dst),
107 (u64 __user *)(8 + (char __user *)src),
108 ret, "q", "", "=r", 8);
109 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 default:
Joe Perchesb8963132008-03-23 01:03:49 -0700111 return copy_user_generic(dst, (__force void *)src, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Joe Perchesb8963132008-03-23 01:03:49 -0700113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Andi Kleen95912002006-09-26 10:52:39 +0200115static __always_inline __must_check
116int __copy_to_user(void __user *dst, const void *src, unsigned size)
Joe Perchesb8963132008-03-23 01:03:49 -0700117{
Andi Kleen383d0792006-09-26 10:52:40 +0200118 int ret = 0;
Ingo Molnard1a76182008-10-28 16:54:49 +0100119
120 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!__builtin_constant_p(size))
Joe Perchesb8963132008-03-23 01:03:49 -0700122 return copy_user_generic((__force void *)dst, src, size);
123 switch (size) {
124 case 1:__put_user_asm(*(u8 *)src, (u8 __user *)dst,
125 ret, "b", "b", "iq", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return ret;
Joe Perchesb8963132008-03-23 01:03:49 -0700127 case 2:__put_user_asm(*(u16 *)src, (u16 __user *)dst,
128 ret, "w", "w", "ir", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return ret;
Joe Perchesb8963132008-03-23 01:03:49 -0700130 case 4:__put_user_asm(*(u32 *)src, (u32 __user *)dst,
131 ret, "l", "k", "ir", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return ret;
Joe Perchesb8963132008-03-23 01:03:49 -0700133 case 8:__put_user_asm(*(u64 *)src, (u64 __user *)dst,
Uros Bizjak155b7352009-07-19 18:06:35 +0200134 ret, "q", "", "er", 8);
Joe Perchesb8963132008-03-23 01:03:49 -0700135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 case 10:
Joe Perchesb8963132008-03-23 01:03:49 -0700137 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
Uros Bizjak155b7352009-07-19 18:06:35 +0200138 ret, "q", "", "er", 10);
Joe Perchesb8963132008-03-23 01:03:49 -0700139 if (unlikely(ret))
140 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 asm("":::"memory");
Joe Perchesb8963132008-03-23 01:03:49 -0700142 __put_user_asm(4[(u16 *)src], 4 + (u16 __user *)dst,
143 ret, "w", "w", "ir", 2);
144 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 case 16:
Joe Perchesb8963132008-03-23 01:03:49 -0700146 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
Uros Bizjak155b7352009-07-19 18:06:35 +0200147 ret, "q", "", "er", 16);
Joe Perchesb8963132008-03-23 01:03:49 -0700148 if (unlikely(ret))
149 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 asm("":::"memory");
Joe Perchesb8963132008-03-23 01:03:49 -0700151 __put_user_asm(1[(u64 *)src], 1 + (u64 __user *)dst,
Uros Bizjak155b7352009-07-19 18:06:35 +0200152 ret, "q", "", "er", 8);
Joe Perchesb8963132008-03-23 01:03:49 -0700153 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 default:
Joe Perchesb8963132008-03-23 01:03:49 -0700155 return copy_user_generic((__force void *)dst, src, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
Joe Perchesb8963132008-03-23 01:03:49 -0700157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Andi Kleen95912002006-09-26 10:52:39 +0200159static __always_inline __must_check
160int __copy_in_user(void __user *dst, const void __user *src, unsigned size)
Joe Perchesb8963132008-03-23 01:03:49 -0700161{
Andi Kleen383d0792006-09-26 10:52:40 +0200162 int ret = 0;
Ingo Molnard1a76182008-10-28 16:54:49 +0100163
164 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!__builtin_constant_p(size))
Joe Perchesb8963132008-03-23 01:03:49 -0700166 return copy_user_generic((__force void *)dst,
167 (__force void *)src, size);
168 switch (size) {
169 case 1: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 u8 tmp;
Joe Perchesb8963132008-03-23 01:03:49 -0700171 __get_user_asm(tmp, (u8 __user *)src,
172 ret, "b", "b", "=q", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (likely(!ret))
Joe Perchesb8963132008-03-23 01:03:49 -0700174 __put_user_asm(tmp, (u8 __user *)dst,
175 ret, "b", "b", "iq", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return ret;
177 }
Joe Perchesb8963132008-03-23 01:03:49 -0700178 case 2: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 u16 tmp;
Joe Perchesb8963132008-03-23 01:03:49 -0700180 __get_user_asm(tmp, (u16 __user *)src,
181 ret, "w", "w", "=r", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (likely(!ret))
Joe Perchesb8963132008-03-23 01:03:49 -0700183 __put_user_asm(tmp, (u16 __user *)dst,
184 ret, "w", "w", "ir", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return ret;
186 }
187
Joe Perchesb8963132008-03-23 01:03:49 -0700188 case 4: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 u32 tmp;
Joe Perchesb8963132008-03-23 01:03:49 -0700190 __get_user_asm(tmp, (u32 __user *)src,
191 ret, "l", "k", "=r", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (likely(!ret))
Joe Perchesb8963132008-03-23 01:03:49 -0700193 __put_user_asm(tmp, (u32 __user *)dst,
194 ret, "l", "k", "ir", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return ret;
196 }
Joe Perchesb8963132008-03-23 01:03:49 -0700197 case 8: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 u64 tmp;
Joe Perchesb8963132008-03-23 01:03:49 -0700199 __get_user_asm(tmp, (u64 __user *)src,
200 ret, "q", "", "=r", 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (likely(!ret))
Joe Perchesb8963132008-03-23 01:03:49 -0700202 __put_user_asm(tmp, (u64 __user *)dst,
Uros Bizjak155b7352009-07-19 18:06:35 +0200203 ret, "q", "", "er", 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return ret;
205 }
206 default:
Joe Perchesb8963132008-03-23 01:03:49 -0700207 return copy_user_generic((__force void *)dst,
208 (__force void *)src, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Joe Perchesb8963132008-03-23 01:03:49 -0700210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Joe Perchesb8963132008-03-23 01:03:49 -0700212__must_check long
Andi Kleen95912002006-09-26 10:52:39 +0200213strncpy_from_user(char *dst, const char __user *src, long count);
Joe Perchesb8963132008-03-23 01:03:49 -0700214__must_check long
Andi Kleen95912002006-09-26 10:52:39 +0200215__strncpy_from_user(char *dst, const char __user *src, long count);
216__must_check long strnlen_user(const char __user *str, long n);
217__must_check long __strnlen_user(const char __user *str, long n);
218__must_check long strlen_user(const char __user *str);
219__must_check unsigned long clear_user(void __user *mem, unsigned long len);
220__must_check unsigned long __clear_user(void __user *mem, unsigned long len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jan Beulich14722482009-11-13 11:56:24 +0000222static __must_check __always_inline int
223__copy_from_user_inatomic(void *dst, const void __user *src, unsigned size)
224{
225 return copy_user_generic(dst, (__force const void *)src, size);
226}
Andi Kleenb8858082006-09-30 01:47:55 +0200227
228static __must_check __always_inline int
229__copy_to_user_inatomic(void __user *dst, const void *src, unsigned size)
230{
231 return copy_user_generic((__force void *)dst, src, size);
232}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Joe Perchesb8963132008-03-23 01:03:49 -0700234extern long __copy_user_nocache(void *dst, const void __user *src,
235 unsigned size, int zerorest);
Andi Kleen0812a572007-02-13 13:26:19 +0100236
Ingo Molnarf1800532009-03-02 11:00:57 +0100237static inline int
238__copy_from_user_nocache(void *dst, const void __user *src, unsigned size)
Andi Kleen0812a572007-02-13 13:26:19 +0100239{
240 might_sleep();
Ingo Molnarf1800532009-03-02 11:00:57 +0100241 return __copy_user_nocache(dst, src, size, 1);
Andi Kleen0812a572007-02-13 13:26:19 +0100242}
243
Ingo Molnarf1800532009-03-02 11:00:57 +0100244static inline int
245__copy_from_user_inatomic_nocache(void *dst, const void __user *src,
246 unsigned size)
Andi Kleen0812a572007-02-13 13:26:19 +0100247{
Ingo Molnarf1800532009-03-02 11:00:57 +0100248 return __copy_user_nocache(dst, src, size, 0);
Andi Kleen0812a572007-02-13 13:26:19 +0100249}
250
Vitaly Mayatskikh11295852008-07-02 15:48:21 +0200251unsigned long
252copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest);
253
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700254#endif /* _ASM_X86_UACCESS_64_H */