uaccess_64.h 5.6 KB
Newer Older
H
H. Peter Anvin 已提交
1 2
#ifndef _ASM_X86_UACCESS_64_H
#define _ASM_X86_UACCESS_64_H
L
Linus Torvalds 已提交
3 4 5 6 7 8 9

/*
 * User space memory access functions
 */
#include <linux/compiler.h>
#include <linux/errno.h>
#include <linux/prefetch.h>
10
#include <linux/lockdep.h>
L
Linus Torvalds 已提交
11 12 13 14 15 16 17
#include <asm/page.h>

/*
 * Copy To/From Userspace
 */

/* Handles exceptions in both to and from, but doesn't do access_ok */
18 19 20 21 22 23 24 25 26 27 28 29
__must_check unsigned long
copy_user_generic(void *to, const void *from, unsigned len);

__must_check unsigned long
copy_to_user(void __user *to, const void *from, unsigned len);
__must_check unsigned long
copy_from_user(void *to, const void __user *from, unsigned len);
__must_check unsigned long
copy_in_user(void __user *to, const void __user *from, unsigned len);

static __always_inline __must_check
int __copy_from_user(void *dst, const void __user *src, unsigned size)
30
{
31
	int ret = 0;
L
Linus Torvalds 已提交
32
	if (!__builtin_constant_p(size))
33 34 35 36
		return copy_user_generic(dst, (__force void *)src, size);
	switch (size) {
	case 1:__get_user_asm(*(u8 *)dst, (u8 __user *)src,
			      ret, "b", "b", "=q", 1);
L
Linus Torvalds 已提交
37
		return ret;
38 39
	case 2:__get_user_asm(*(u16 *)dst, (u16 __user *)src,
			      ret, "w", "w", "=r", 2);
L
Linus Torvalds 已提交
40
		return ret;
41 42 43 44 45
	case 4:__get_user_asm(*(u32 *)dst, (u32 __user *)src,
			      ret, "l", "k", "=r", 4);
		return ret;
	case 8:__get_user_asm(*(u64 *)dst, (u64 __user *)src,
			      ret, "q", "", "=r", 8);
L
Linus Torvalds 已提交
46 47
		return ret;
	case 10:
48 49 50 51 52 53 54 55
		__get_user_asm(*(u64 *)dst, (u64 __user *)src,
			       ret, "q", "", "=r", 16);
		if (unlikely(ret))
			return ret;
		__get_user_asm(*(u16 *)(8 + (char *)dst),
			       (u16 __user *)(8 + (char __user *)src),
			       ret, "w", "w", "=r", 2);
		return ret;
L
Linus Torvalds 已提交
56
	case 16:
57 58 59 60 61 62 63 64
		__get_user_asm(*(u64 *)dst, (u64 __user *)src,
			       ret, "q", "", "=r", 16);
		if (unlikely(ret))
			return ret;
		__get_user_asm(*(u64 *)(8 + (char *)dst),
			       (u64 __user *)(8 + (char __user *)src),
			       ret, "q", "", "=r", 8);
		return ret;
L
Linus Torvalds 已提交
65
	default:
66
		return copy_user_generic(dst, (__force void *)src, size);
L
Linus Torvalds 已提交
67
	}
68
}
L
Linus Torvalds 已提交
69

70 71
static __always_inline __must_check
int __copy_to_user(void __user *dst, const void *src, unsigned size)
72
{
73
	int ret = 0;
L
Linus Torvalds 已提交
74
	if (!__builtin_constant_p(size))
75 76 77 78
		return copy_user_generic((__force void *)dst, src, size);
	switch (size) {
	case 1:__put_user_asm(*(u8 *)src, (u8 __user *)dst,
			      ret, "b", "b", "iq", 1);
L
Linus Torvalds 已提交
79
		return ret;
80 81
	case 2:__put_user_asm(*(u16 *)src, (u16 __user *)dst,
			      ret, "w", "w", "ir", 2);
L
Linus Torvalds 已提交
82
		return ret;
83 84 85 86 87
	case 4:__put_user_asm(*(u32 *)src, (u32 __user *)dst,
			      ret, "l", "k", "ir", 4);
		return ret;
	case 8:__put_user_asm(*(u64 *)src, (u64 __user *)dst,
			      ret, "q", "", "ir", 8);
L
Linus Torvalds 已提交
88 89
		return ret;
	case 10:
90 91 92 93
		__put_user_asm(*(u64 *)src, (u64 __user *)dst,
			       ret, "q", "", "ir", 10);
		if (unlikely(ret))
			return ret;
L
Linus Torvalds 已提交
94
		asm("":::"memory");
95 96 97
		__put_user_asm(4[(u16 *)src], 4 + (u16 __user *)dst,
			       ret, "w", "w", "ir", 2);
		return ret;
L
Linus Torvalds 已提交
98
	case 16:
99 100 101 102
		__put_user_asm(*(u64 *)src, (u64 __user *)dst,
			       ret, "q", "", "ir", 16);
		if (unlikely(ret))
			return ret;
L
Linus Torvalds 已提交
103
		asm("":::"memory");
104 105 106
		__put_user_asm(1[(u64 *)src], 1 + (u64 __user *)dst,
			       ret, "q", "", "ir", 8);
		return ret;
L
Linus Torvalds 已提交
107
	default:
108
		return copy_user_generic((__force void *)dst, src, size);
L
Linus Torvalds 已提交
109
	}
110
}
L
Linus Torvalds 已提交
111

112 113
static __always_inline __must_check
int __copy_in_user(void __user *dst, const void __user *src, unsigned size)
114
{
115
	int ret = 0;
L
Linus Torvalds 已提交
116
	if (!__builtin_constant_p(size))
117 118 119 120
		return copy_user_generic((__force void *)dst,
					 (__force void *)src, size);
	switch (size) {
	case 1: {
L
Linus Torvalds 已提交
121
		u8 tmp;
122 123
		__get_user_asm(tmp, (u8 __user *)src,
			       ret, "b", "b", "=q", 1);
L
Linus Torvalds 已提交
124
		if (likely(!ret))
125 126
			__put_user_asm(tmp, (u8 __user *)dst,
				       ret, "b", "b", "iq", 1);
L
Linus Torvalds 已提交
127 128
		return ret;
	}
129
	case 2: {
L
Linus Torvalds 已提交
130
		u16 tmp;
131 132
		__get_user_asm(tmp, (u16 __user *)src,
			       ret, "w", "w", "=r", 2);
L
Linus Torvalds 已提交
133
		if (likely(!ret))
134 135
			__put_user_asm(tmp, (u16 __user *)dst,
				       ret, "w", "w", "ir", 2);
L
Linus Torvalds 已提交
136 137 138
		return ret;
	}

139
	case 4: {
L
Linus Torvalds 已提交
140
		u32 tmp;
141 142
		__get_user_asm(tmp, (u32 __user *)src,
			       ret, "l", "k", "=r", 4);
L
Linus Torvalds 已提交
143
		if (likely(!ret))
144 145
			__put_user_asm(tmp, (u32 __user *)dst,
				       ret, "l", "k", "ir", 4);
L
Linus Torvalds 已提交
146 147
		return ret;
	}
148
	case 8: {
L
Linus Torvalds 已提交
149
		u64 tmp;
150 151
		__get_user_asm(tmp, (u64 __user *)src,
			       ret, "q", "", "=r", 8);
L
Linus Torvalds 已提交
152
		if (likely(!ret))
153 154
			__put_user_asm(tmp, (u64 __user *)dst,
				       ret, "q", "", "ir", 8);
L
Linus Torvalds 已提交
155 156 157
		return ret;
	}
	default:
158 159
		return copy_user_generic((__force void *)dst,
					 (__force void *)src, size);
L
Linus Torvalds 已提交
160
	}
161
}
L
Linus Torvalds 已提交
162

163
__must_check long
164
strncpy_from_user(char *dst, const char __user *src, long count);
165
__must_check long
166 167 168 169 170 171 172
__strncpy_from_user(char *dst, const char __user *src, long count);
__must_check long strnlen_user(const char __user *str, long n);
__must_check long __strnlen_user(const char __user *str, long n);
__must_check long strlen_user(const char __user *str);
__must_check unsigned long clear_user(void __user *mem, unsigned long len);
__must_check unsigned long __clear_user(void __user *mem, unsigned long len);

173 174
__must_check long __copy_from_user_inatomic(void *dst, const void __user *src,
					    unsigned size);
175 176 177 178 179 180

static __must_check __always_inline int
__copy_to_user_inatomic(void __user *dst, const void *src, unsigned size)
{
	return copy_user_generic((__force void *)dst, src, size);
}
L
Linus Torvalds 已提交
181

182 183
extern long __copy_user_nocache(void *dst, const void __user *src,
				unsigned size, int zerorest);
184

185 186
static inline int __copy_from_user_nocache(void *dst, const void __user *src,
					   unsigned size)
187 188
{
	might_sleep();
189
	return __copy_user_nocache(dst, src, size, 1);
190 191
}

192 193 194
static inline int __copy_from_user_inatomic_nocache(void *dst,
						    const void __user *src,
						    unsigned size)
195
{
196
	return __copy_user_nocache(dst, src, size, 0);
197 198
}

199 200 201
unsigned long
copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest);

H
H. Peter Anvin 已提交
202
#endif /* _ASM_X86_UACCESS_64_H */