Linux 2.6.13-rc4
[linux-2.6/next.git] / arch / x86_64 / lib / usercopy.c
blobdb8abba1ad819901a1a327465cb1d2fe7061cd5f
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 <asm/uaccess.h>
11 * Copy a null terminated string from userspace.
14 #define __do_strncpy_from_user(dst,src,count,res) \
15 do { \
16 long __d0, __d1, __d2; \
17 might_sleep(); \
18 __asm__ __volatile__( \
19 " testq %1,%1\n" \
20 " jz 2f\n" \
21 "0: lodsb\n" \
22 " stosb\n" \
23 " testb %%al,%%al\n" \
24 " jz 1f\n" \
25 " decq %1\n" \
26 " jnz 0b\n" \
27 "1: subq %1,%0\n" \
28 "2:\n" \
29 ".section .fixup,\"ax\"\n" \
30 "3: movq %5,%0\n" \
31 " jmp 2b\n" \
32 ".previous\n" \
33 ".section __ex_table,\"a\"\n" \
34 " .align 8\n" \
35 " .quad 0b,3b\n" \
36 ".previous" \
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;
51 long
52 strncpy_from_user(char *dst, const char __user *src, long count)
54 long res = -EFAULT;
55 if (access_ok(VERIFY_READ, src, 1))
56 __do_strncpy_from_user(dst, src, count, res);
57 return res;
61 * Zero Userspace
64 unsigned long __clear_user(void __user *addr, unsigned long size)
66 long __d0;
67 might_sleep();
68 /* no memory constraint because it doesn't change any memory gcc knows
69 about */
70 asm volatile(
71 " testq %[size8],%[size8]\n"
72 " jz 4f\n"
73 "0: movq %[zero],(%[dst])\n"
74 " addq %[eight],%[dst]\n"
75 " decl %%ecx ; jnz 0b\n"
76 "4: movq %[size1],%%rcx\n"
77 " testl %%ecx,%%ecx\n"
78 " jz 2f\n"
79 "1: movb %b[zero],(%[dst])\n"
80 " incq %[dst]\n"
81 " decl %%ecx ; jnz 1b\n"
82 "2:\n"
83 ".section .fixup,\"ax\"\n"
84 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
85 " jmp 2b\n"
86 ".previous\n"
87 ".section __ex_table,\"a\"\n"
88 " .align 8\n"
89 " .quad 0b,3b\n"
90 " .quad 1b,2b\n"
91 ".previous"
92 : [size8] "=c"(size), [dst] "=&D" (__d0)
93 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
94 [zero] "r" (0UL), [eight] "r" (8UL));
95 return size;
99 unsigned long clear_user(void __user *to, unsigned long n)
101 if (access_ok(VERIFY_WRITE, to, n))
102 return __clear_user(to, n);
103 return n;
107 * Return the size of a string (including the ending 0)
109 * Return 0 on exception, a value greater than N if too long
112 long strnlen_user(const char __user *s, long n)
114 long res = 0;
115 char c;
117 if (!access_ok(VERIFY_READ, s, n))
118 return 0;
120 while (1) {
121 if (res>n)
122 return n+1;
123 if (__get_user(c, s))
124 return 0;
125 if (!c)
126 return res+1;
127 res++;
128 s++;
132 long strlen_user(const char __user *s)
134 long res = 0;
135 char c;
137 for (;;) {
138 if (get_user(c, s))
139 return 0;
140 if (!c)
141 return res+1;
142 res++;
143 s++;
147 unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
149 if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
150 return copy_user_generic((__force void *)to, (__force void *)from, len);
152 return len;