x86: clean up/fix 'copy_in_user()' tail zeroing
[linux-2.6-block.git] / arch / x86 / lib / usercopy_64.c
CommitLineData
1da177e4
LT
1/*
2 * User address space access functions.
3 *
4 * Copyright 1997 Andi Kleen <ak@muc.de>
5 * Copyright 1997 Linus Torvalds
6 * Copyright 2002 Andi Kleen <ak@suse.de>
7 */
2ee60e17 8#include <linux/module.h>
1da177e4
LT
9#include <asm/uaccess.h>
10
1da177e4
LT
11/*
12 * Zero Userspace
13 */
14
15unsigned long __clear_user(void __user *addr, unsigned long size)
16{
17 long __d0;
3ee1afa3 18 might_fault();
1da177e4
LT
19 /* no memory constraint because it doesn't change any memory gcc knows
20 about */
63bcff2a 21 stac();
1da177e4
LT
22 asm volatile(
23 " testq %[size8],%[size8]\n"
24 " jz 4f\n"
25 "0: movq %[zero],(%[dst])\n"
26 " addq %[eight],%[dst]\n"
27 " decl %%ecx ; jnz 0b\n"
28 "4: movq %[size1],%%rcx\n"
29 " testl %%ecx,%%ecx\n"
30 " jz 2f\n"
31 "1: movb %b[zero],(%[dst])\n"
32 " incq %[dst]\n"
33 " decl %%ecx ; jnz 1b\n"
34 "2:\n"
35 ".section .fixup,\"ax\"\n"
36 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
37 " jmp 2b\n"
38 ".previous\n"
8da804f2
PA
39 _ASM_EXTABLE(0b,3b)
40 _ASM_EXTABLE(1b,2b)
e0a96129 41 : [size8] "=&c"(size), [dst] "=&D" (__d0)
1da177e4
LT
42 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
43 [zero] "r" (0UL), [eight] "r" (8UL));
63bcff2a 44 clac();
1da177e4
LT
45 return size;
46}
2ee60e17 47EXPORT_SYMBOL(__clear_user);
1da177e4
LT
48
49unsigned long clear_user(void __user *to, unsigned long n)
50{
51 if (access_ok(VERIFY_WRITE, to, n))
52 return __clear_user(to, n);
53 return n;
54}
2ee60e17 55EXPORT_SYMBOL(clear_user);
1da177e4 56
1da177e4
LT
57unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
58{
59 if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
60 return copy_user_generic((__force void *)to, (__force void *)from, len);
61 }
62 return len;
63}
2ee60e17
AK
64EXPORT_SYMBOL(copy_in_user);
65
1129585a
VM
66/*
67 * Try to copy last bytes and clear the rest if needed.
68 * Since protection fault in copy_from/to_user is not a normal situation,
69 * it is not necessary to optimize tail handling.
70 */
277d5b40 71__visible unsigned long
cae2a173 72copy_user_handle_tail(char *to, char *from, unsigned len)
1129585a 73{
66db3feb 74 for (; len; --len, to++) {
cae2a173
LT
75 char c;
76
1129585a
VM
77 if (__get_user_nocheck(c, from++, sizeof(char)))
78 break;
66db3feb 79 if (__put_user_nocheck(c, to, sizeof(char)))
1129585a
VM
80 break;
81 }
63bcff2a 82 clac();
cae2a173
LT
83
84 /* If the destination is a kernel buffer, we always clear the end */
85 if ((unsigned long)to >= TASK_SIZE_MAX)
86 memset(to, 0, len);
1129585a
VM
87 return len;
88}