[PATCH] x86_64 merge: arch + asm
[opensuse:kernel.git] / arch / x86_64 / lib / usercopy.c
1 /* 
2  * User address space access functions.
3  * The non inlined parts of asm-i386/uaccess.h are here.
4  *
5  * Copyright 1997 Andi Kleen <ak@muc.de>
6  * Copyright 1997 Linus Torvalds
7  */
8 #include <linux/config.h>
9 #include <asm/uaccess.h>
10 #include <asm/mmx.h>
11
12 unsigned long
13 __generic_copy_to_user(void *to, const void *from, unsigned long n)
14 {
15         prefetch(from);
16         if (access_ok(VERIFY_WRITE, to, n))
17                 __copy_user(to,from,n);
18         return n;
19 }
20
21 unsigned long
22 __generic_copy_from_user(void *to, const void *from, unsigned long n)
23 {
24         prefetchw(to);
25         if (access_ok(VERIFY_READ, from, n))
26                 __copy_user_zeroing(to,from,n);
27         else
28                 memset(to, 0, n);
29         return n;
30 }
31
32 /*
33  * Copy a null terminated string from userspace.
34  */
35
36 #define __do_strncpy_from_user(dst,src,count,res)                          \
37 do {                                                                       \
38         long __d0, __d1, __d2;                                             \
39         __asm__ __volatile__(                                              \
40                 "       testq %1,%1\n"                                     \
41                 "       jz 2f\n"                                           \
42                 "0:     lodsb\n"                                           \
43                 "       stosb\n"                                           \
44                 "       testb %%al,%%al\n"                                 \
45                 "       jz 1f\n"                                           \
46                 "       decq %1\n"                                         \
47                 "       jnz 0b\n"                                          \
48                 "1:     subq %1,%0\n"                                      \
49                 "2:\n"                                                     \
50                 ".section .fixup,\"ax\"\n"                                 \
51                 "3:     movq %5,%0\n"                                      \
52                 "       jmp 2b\n"                                          \
53                 ".previous\n"                                              \
54                 ".section __ex_table,\"a\"\n"                              \
55                 "       .align 4\n"                                        \
56                 "       .quad 0b,3b\n"                                     \
57                 ".previous"                                                \
58                 : "=r"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1),      \
59                   "=&D" (__d2)                                             \
60                 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
61                 : "memory");                                               \
62 } while (0)
63
64 long
65 __strncpy_from_user(char *dst, const char *src, long count)
66 {
67         long res;
68         __do_strncpy_from_user(dst, src, count, res);
69         return res;
70 }
71
72 long
73 strncpy_from_user(char *dst, const char *src, long count)
74 {
75         long res = -EFAULT;
76         if (access_ok(VERIFY_READ, src, 1))
77                 __do_strncpy_from_user(dst, src, count, res);
78         return res;
79 }
80
81
82 /*
83  * Zero Userspace
84  */
85
86 #define __do_clear_user(addr,size)                                      \
87 do {                                                                    \
88         long __d0;                                                      \
89         __asm__ __volatile__(                                           \
90                 "cld\n" \
91                 "0:     rep; stosl\n"                                   \
92                 "       movq %2,%0\n"                                   \
93                 "1:     rep; stosb\n"                                   \
94                 "2:\n"                                                  \
95                 ".section .fixup,\"ax\"\n"                              \
96                 "3:     lea 0(%2,%0,4),%0\n"                            \
97                 "       jmp 2b\n"                                       \
98                 ".previous\n"                                           \
99                 ".section __ex_table,\"a\"\n"                           \
100                 "       .align 4\n"                                     \
101                 "       .quad 0b,3b\n"                                  \
102                 "       .quad 1b,2b\n"                                  \
103                 ".previous"                                             \
104                 : "=&c"(size), "=&D" (__d0)                             \
105                 : "r"(size & 3), "0"(size / 4), "1"(addr), "a"(0));     \
106 } while (0)
107
108 unsigned long
109 clear_user(void *to, unsigned long n)
110 {
111         if (access_ok(VERIFY_WRITE, to, n))
112                 __do_clear_user(to, n);
113         return n;
114 }
115
116 unsigned long
117 __clear_user(void *to, unsigned long n)
118 {
119         __do_clear_user(to, n);
120         return n;
121 }
122
123 /*
124  * Return the size of a string (including the ending 0)
125  *
126  * Return 0 on exception, a value greater than N if too long
127  */
128
129 long strnlen_user(const char *s, long n)
130 {
131         unsigned long res = 0;
132         char c;
133
134         if (!access_ok(VERIFY_READ, s, n))
135                 return 0;
136
137         while (1) {
138                 if (get_user(c, s))
139                         return 0;
140                 if (!c)
141                         return res+1;
142                 if (res>n)
143                         return n+1;
144                 res++;
145                 s++;
146         }
147 }