blob: e5b130bc2d0efb4a026821bd2df605b8e4e9b643 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Andi Kleen2ee60e172006-06-26 13:59:44 +02008#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/uaccess.h>
10
11/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Zero Userspace
13 */
14
15unsigned long __clear_user(void __user *addr, unsigned long size)
16{
17 long __d0;
Nick Piggin3ee1afa2008-09-10 13:37:17 +020018 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 /* no memory constraint because it doesn't change any memory gcc knows
20 about */
21 asm volatile(
22 " testq %[size8],%[size8]\n"
23 " jz 4f\n"
24 "0: movq %[zero],(%[dst])\n"
25 " addq %[eight],%[dst]\n"
26 " decl %%ecx ; jnz 0b\n"
27 "4: movq %[size1],%%rcx\n"
28 " testl %%ecx,%%ecx\n"
29 " jz 2f\n"
30 "1: movb %b[zero],(%[dst])\n"
31 " incq %[dst]\n"
32 " decl %%ecx ; jnz 1b\n"
33 "2:\n"
34 ".section .fixup,\"ax\"\n"
35 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
36 " jmp 2b\n"
37 ".previous\n"
H. Peter Anvin8da804f2008-02-04 16:47:57 +010038 _ASM_EXTABLE(0b,3b)
39 _ASM_EXTABLE(1b,2b)
Andi Kleene0a96122009-01-16 15:22:11 +010040 : [size8] "=&c"(size), [dst] "=&D" (__d0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
42 [zero] "r" (0UL), [eight] "r" (8UL));
43 return size;
44}
Andi Kleen2ee60e172006-06-26 13:59:44 +020045EXPORT_SYMBOL(__clear_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47unsigned long clear_user(void __user *to, unsigned long n)
48{
49 if (access_ok(VERIFY_WRITE, to, n))
50 return __clear_user(to, n);
51 return n;
52}
Andi Kleen2ee60e172006-06-26 13:59:44 +020053EXPORT_SYMBOL(clear_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
56{
57 if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
58 return copy_user_generic((__force void *)to, (__force void *)from, len);
59 }
60 return len;
61}
Andi Kleen2ee60e172006-06-26 13:59:44 +020062EXPORT_SYMBOL(copy_in_user);
63
Vitaly Mayatskikh11295852008-07-02 15:48:21 +020064/*
65 * Try to copy last bytes and clear the rest if needed.
66 * Since protection fault in copy_from/to_user is not a normal situation,
67 * it is not necessary to optimize tail handling.
68 */
69unsigned long
70copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest)
71{
72 char c;
73 unsigned zero_len;
74
75 for (; len; --len) {
76 if (__get_user_nocheck(c, from++, sizeof(char)))
77 break;
78 if (__put_user_nocheck(c, to++, sizeof(char)))
79 break;
80 }
81
82 for (c = 0, zero_len = len; zerorest && zero_len; --zero_len)
83 if (__put_user_nocheck(c, to++, sizeof(char)))
84 break;
85 return len;
86}