x86: some lock annotations for user copy paths
[linux-2.6/mini2440.git] / arch / x86 / lib / usercopy_64.c
blob847d12945998a99c3bca081aa89a56940b911621
1 /*
2 * User address space access functions.
4 * Copyright 1997 Andi Kleen <ak@muc.de>
5 * Copyright 1997 Linus Torvalds
6 * Copyright 2002 Andi Kleen <ak@suse.de>
7 */
8 #include <linux/module.h>
9 #include <asm/uaccess.h>
12 * Copy a null terminated string from userspace.
15 #define __do_strncpy_from_user(dst,src,count,res) \
16 do { \
17 long __d0, __d1, __d2; \
18 might_sleep(); \
19 if (current->mm) \
20 might_lock_read(&current->mm->mmap_sem); \
21 __asm__ __volatile__( \
22 " testq %1,%1\n" \
23 " jz 2f\n" \
24 "0: lodsb\n" \
25 " stosb\n" \
26 " testb %%al,%%al\n" \
27 " jz 1f\n" \
28 " decq %1\n" \
29 " jnz 0b\n" \
30 "1: subq %1,%0\n" \
31 "2:\n" \
32 ".section .fixup,\"ax\"\n" \
33 "3: movq %5,%0\n" \
34 " jmp 2b\n" \
35 ".previous\n" \
36 _ASM_EXTABLE(0b,3b) \
37 : "=r"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
38 "=&D" (__d2) \
39 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
40 : "memory"); \
41 } while (0)
43 long
44 __strncpy_from_user(char *dst, const char __user *src, long count)
46 long res;
47 __do_strncpy_from_user(dst, src, count, res);
48 return res;
50 EXPORT_SYMBOL(__strncpy_from_user);
52 long
53 strncpy_from_user(char *dst, const char __user *src, long count)
55 long res = -EFAULT;
56 if (access_ok(VERIFY_READ, src, 1))
57 return __strncpy_from_user(dst, src, count);
58 return res;
60 EXPORT_SYMBOL(strncpy_from_user);
63 * Zero Userspace
66 unsigned long __clear_user(void __user *addr, unsigned long size)
68 long __d0;
69 might_sleep();
70 if (current->mm)
71 might_lock_read(&current->mm->mmap_sem);
72 /* no memory constraint because it doesn't change any memory gcc knows
73 about */
74 asm volatile(
75 " testq %[size8],%[size8]\n"
76 " jz 4f\n"
77 "0: movq %[zero],(%[dst])\n"
78 " addq %[eight],%[dst]\n"
79 " decl %%ecx ; jnz 0b\n"
80 "4: movq %[size1],%%rcx\n"
81 " testl %%ecx,%%ecx\n"
82 " jz 2f\n"
83 "1: movb %b[zero],(%[dst])\n"
84 " incq %[dst]\n"
85 " decl %%ecx ; jnz 1b\n"
86 "2:\n"
87 ".section .fixup,\"ax\"\n"
88 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
89 " jmp 2b\n"
90 ".previous\n"
91 _ASM_EXTABLE(0b,3b)
92 _ASM_EXTABLE(1b,2b)
93 : [size8] "=c"(size), [dst] "=&D" (__d0)
94 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
95 [zero] "r" (0UL), [eight] "r" (8UL));
96 return size;
98 EXPORT_SYMBOL(__clear_user);
100 unsigned long clear_user(void __user *to, unsigned long n)
102 if (access_ok(VERIFY_WRITE, to, n))
103 return __clear_user(to, n);
104 return n;
106 EXPORT_SYMBOL(clear_user);
109 * Return the size of a string (including the ending 0)
111 * Return 0 on exception, a value greater than N if too long
114 long __strnlen_user(const char __user *s, long n)
116 long res = 0;
117 char c;
119 while (1) {
120 if (res>n)
121 return n+1;
122 if (__get_user(c, s))
123 return 0;
124 if (!c)
125 return res+1;
126 res++;
127 s++;
130 EXPORT_SYMBOL(__strnlen_user);
132 long strnlen_user(const char __user *s, long n)
134 if (!access_ok(VERIFY_READ, s, n))
135 return 0;
136 return __strnlen_user(s, n);
138 EXPORT_SYMBOL(strnlen_user);
140 long strlen_user(const char __user *s)
142 long res = 0;
143 char c;
145 for (;;) {
146 if (get_user(c, s))
147 return 0;
148 if (!c)
149 return res+1;
150 res++;
151 s++;
154 EXPORT_SYMBOL(strlen_user);
156 unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
158 if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
159 return copy_user_generic((__force void *)to, (__force void *)from, len);
161 return len;
163 EXPORT_SYMBOL(copy_in_user);
166 * Try to copy last bytes and clear the rest if needed.
167 * Since protection fault in copy_from/to_user is not a normal situation,
168 * it is not necessary to optimize tail handling.
170 unsigned long
171 copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest)
173 char c;
174 unsigned zero_len;
176 for (; len; --len) {
177 if (__get_user_nocheck(c, from++, sizeof(char)))
178 break;
179 if (__put_user_nocheck(c, to++, sizeof(char)))
180 break;
183 for (c = 0, zero_len = len; zerorest && zero_len; --zero_len)
184 if (__put_user_nocheck(c, to++, sizeof(char)))
185 break;
186 return len;