This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / i386 / 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 <linux/mm.h>
10 #include <linux/highmem.h>
11 #include <linux/blkdev.h>
12 #include <asm/uaccess.h>
13 #include <asm/mmx.h>
14
15 static inline int __movsl_is_ok(unsigned long a1, unsigned long a2, unsigned long n)
16 {
17 #ifdef CONFIG_X86_INTEL_USERCOPY
18         if (n >= 64 && ((a1 ^ a2) & movsl_mask.mask))
19                 return 0;
20 #endif
21         return 1;
22 }
23 #define movsl_is_ok(a1,a2,n) \
24         __movsl_is_ok((unsigned long)(a1),(unsigned long)(a2),(n))
25
26 /*
27  * Copy a null terminated string from userspace.
28  */
29
30 #define __do_strncpy_from_user(dst,src,count,res)                          \
31 do {                                                                       \
32         int __d0, __d1, __d2;                                              \
33         __asm__ __volatile__(                                              \
34                 "       testl %1,%1\n"                                     \
35                 "       jz 2f\n"                                           \
36                 "0:     lodsb\n"                                           \
37                 "       stosb\n"                                           \
38                 "       testb %%al,%%al\n"                                 \
39                 "       jz 1f\n"                                           \
40                 "       decl %1\n"                                         \
41                 "       jnz 0b\n"                                          \
42                 "1:     subl %1,%0\n"                                      \
43                 "2:\n"                                                     \
44                 ".section .fixup,\"ax\"\n"                                 \
45                 "3:     movl %5,%0\n"                                      \
46                 "       jmp 2b\n"                                          \
47                 ".previous\n"                                              \
48                 ".section __ex_table,\"a\"\n"                              \
49                 "       .align 4\n"                                        \
50                 "       .long 0b,3b\n"                                     \
51                 ".previous"                                                \
52                 : "=d"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1),      \
53                   "=&D" (__d2)                                             \
54                 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
55                 : "memory");                                               \
56 } while (0)
57
58 /**
59  * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
60  * @dst:   Destination address, in kernel space.  This buffer must be at
61  *         least @count bytes long.
62  * @src:   Source address, in user space.
63  * @count: Maximum number of bytes to copy, including the trailing NUL.
64  * 
65  * Copies a NUL-terminated string from userspace to kernel space.
66  * Caller must check the specified block with access_ok() before calling
67  * this function.
68  *
69  * On success, returns the length of the string (not including the trailing
70  * NUL).
71  *
72  * If access to userspace fails, returns -EFAULT (some data may have been
73  * copied).
74  *
75  * If @count is smaller than the length of the string, copies @count bytes
76  * and returns @count.
77  */
78 long
79 __direct_strncpy_from_user(char *dst, const char __user *src, long count)
80 {
81         long res;
82         __do_strncpy_from_user(dst, src, count, res);
83         return res;
84 }
85
86 /**
87  * strncpy_from_user: - Copy a NUL terminated string from userspace.
88  * @dst:   Destination address, in kernel space.  This buffer must be at
89  *         least @count bytes long.
90  * @src:   Source address, in user space.
91  * @count: Maximum number of bytes to copy, including the trailing NUL.
92  * 
93  * Copies a NUL-terminated string from userspace to kernel space.
94  *
95  * On success, returns the length of the string (not including the trailing
96  * NUL).
97  *
98  * If access to userspace fails, returns -EFAULT (some data may have been
99  * copied).
100  *
101  * If @count is smaller than the length of the string, copies @count bytes
102  * and returns @count.
103  */
104 long
105 direct_strncpy_from_user(char *dst, const char __user *src, long count)
106 {
107         long res = -EFAULT;
108         if (access_ok(VERIFY_READ, src, 1))
109                 __do_strncpy_from_user(dst, src, count, res);
110         return res;
111 }
112
113
114 /*
115  * Zero Userspace
116  */
117
118 #define __do_clear_user(addr,size)                                      \
119 do {                                                                    \
120         int __d0;                                                       \
121         __asm__ __volatile__(                                           \
122                 "0:     rep; stosl\n"                                   \
123                 "       movl %2,%0\n"                                   \
124                 "1:     rep; stosb\n"                                   \
125                 "2:\n"                                                  \
126                 ".section .fixup,\"ax\"\n"                              \
127                 "3:     lea 0(%2,%0,4),%0\n"                            \
128                 "       jmp 2b\n"                                       \
129                 ".previous\n"                                           \
130                 ".section __ex_table,\"a\"\n"                           \
131                 "       .align 4\n"                                     \
132                 "       .long 0b,3b\n"                                  \
133                 "       .long 1b,2b\n"                                  \
134                 ".previous"                                             \
135                 : "=&c"(size), "=&D" (__d0)                             \
136                 : "r"(size & 3), "0"(size / 4), "1"(addr), "a"(0));     \
137 } while (0)
138
139 /**
140  * clear_user: - Zero a block of memory in user space.
141  * @to:   Destination address, in user space.
142  * @n:    Number of bytes to zero.
143  *
144  * Zero a block of memory in user space.
145  *
146  * Returns number of bytes that could not be cleared.
147  * On success, this will be zero.
148  */
149 unsigned long
150 direct_clear_user(void __user *to, unsigned long n)
151 {
152         might_sleep();
153         if (access_ok(VERIFY_WRITE, to, n))
154                 __do_clear_user(to, n);
155         return n;
156 }
157
158 /**
159  * __clear_user: - Zero a block of memory in user space, with less checking.
160  * @to:   Destination address, in user space.
161  * @n:    Number of bytes to zero.
162  *
163  * Zero a block of memory in user space.  Caller must check
164  * the specified block with access_ok() before calling this function.
165  *
166  * Returns number of bytes that could not be cleared.
167  * On success, this will be zero.
168  */
169 unsigned long
170 __direct_clear_user(void __user *to, unsigned long n)
171 {
172         __do_clear_user(to, n);
173         return n;
174 }
175
176 /**
177  * strlen_user: - Get the size of a string in user space.
178  * @s: The string to measure.
179  * @n: The maximum valid length
180  *
181  * Get the size of a NUL-terminated string in user space.
182  *
183  * Returns the size of the string INCLUDING the terminating NUL.
184  * On exception, returns 0.
185  * If the string is too long, returns a value greater than @n.
186  */
187 long direct_strnlen_user(const char __user *s, long n)
188 {
189         unsigned long mask = -__addr_ok(s);
190         unsigned long res, tmp;
191
192         might_sleep();
193
194         __asm__ __volatile__(
195                 "       testl %0, %0\n"
196                 "       jz 3f\n"
197                 "       andl %0,%%ecx\n"
198                 "0:     repne; scasb\n"
199                 "       setne %%al\n"
200                 "       subl %%ecx,%0\n"
201                 "       addl %0,%%eax\n"
202                 "1:\n"
203                 ".section .fixup,\"ax\"\n"
204                 "2:     xorl %%eax,%%eax\n"
205                 "       jmp 1b\n"
206                 "3:     movb $1,%%al\n"
207                 "       jmp 1b\n"
208                 ".previous\n"
209                 ".section __ex_table,\"a\"\n"
210                 "       .align 4\n"
211                 "       .long 0b,2b\n"
212                 ".previous"
213                 :"=r" (n), "=D" (s), "=a" (res), "=c" (tmp)
214                 :"0" (n), "1" (s), "2" (0), "3" (mask)
215                 :"cc");
216         return res & mask;
217 }
218
219 #ifdef CONFIG_X86_INTEL_USERCOPY
220 static unsigned long
221 __copy_user_intel(void *to, const void *from,unsigned long size)
222 {
223         int d0, d1;
224         __asm__ __volatile__(
225                        "       .align 2,0x90\n"
226                        "1:     movl 32(%4), %%eax\n"
227                        "       cmpl $67, %0\n"
228                        "       jbe 3f\n"
229                        "2:     movl 64(%4), %%eax\n"
230                        "       .align 2,0x90\n"
231                        "3:     movl 0(%4), %%eax\n"
232                        "4:     movl 4(%4), %%edx\n"
233                        "5:     movl %%eax, 0(%3)\n"
234                        "6:     movl %%edx, 4(%3)\n"
235                        "7:     movl 8(%4), %%eax\n"
236                        "8:     movl 12(%4),%%edx\n"
237                        "9:     movl %%eax, 8(%3)\n"
238                        "10:    movl %%edx, 12(%3)\n"
239                        "11:    movl 16(%4), %%eax\n"
240                        "12:    movl 20(%4), %%edx\n"
241                        "13:    movl %%eax, 16(%3)\n"
242                        "14:    movl %%edx, 20(%3)\n"
243                        "15:    movl 24(%4), %%eax\n"
244                        "16:    movl 28(%4), %%edx\n"
245                        "17:    movl %%eax, 24(%3)\n"
246                        "18:    movl %%edx, 28(%3)\n"
247                        "19:    movl 32(%4), %%eax\n"
248                        "20:    movl 36(%4), %%edx\n"
249                        "21:    movl %%eax, 32(%3)\n"
250                        "22:    movl %%edx, 36(%3)\n"
251                        "23:    movl 40(%4), %%eax\n"
252                        "24:    movl 44(%4), %%edx\n"
253                        "25:    movl %%eax, 40(%3)\n"
254                        "26:    movl %%edx, 44(%3)\n"
255                        "27:    movl 48(%4), %%eax\n"
256                        "28:    movl 52(%4), %%edx\n"
257                        "29:    movl %%eax, 48(%3)\n"
258                        "30:    movl %%edx, 52(%3)\n"
259                        "31:    movl 56(%4), %%eax\n"
260                        "32:    movl 60(%4), %%edx\n"
261                        "33:    movl %%eax, 56(%3)\n"
262                        "34:    movl %%edx, 60(%3)\n"
263                        "       addl $-64, %0\n"
264                        "       addl $64, %4\n"
265                        "       addl $64, %3\n"
266                        "       cmpl $63, %0\n"
267                        "       ja  1b\n"
268                        "35:    movl  %0, %%eax\n"
269                        "       shrl  $2, %0\n"
270                        "       andl  $3, %%eax\n"
271                        "       cld\n"
272                        "99:    rep; movsl\n"
273                        "36:    movl %%eax, %0\n"
274                        "37:    rep; movsb\n"
275                        "100:\n"
276                        ".section .fixup,\"ax\"\n"
277                        "101:   lea 0(%%eax,%0,4),%0\n"
278                        "       jmp 100b\n"
279                        ".previous\n"
280                        ".section __ex_table,\"a\"\n"
281                        "       .align 4\n"
282                        "       .long 1b,100b\n"
283                        "       .long 2b,100b\n"
284                        "       .long 3b,100b\n"
285                        "       .long 4b,100b\n"
286                        "       .long 5b,100b\n"
287                        "       .long 6b,100b\n"
288                        "       .long 7b,100b\n"
289                        "       .long 8b,100b\n"
290                        "       .long 9b,100b\n"
291                        "       .long 10b,100b\n"
292                        "       .long 11b,100b\n"
293                        "       .long 12b,100b\n"
294                        "       .long 13b,100b\n"
295                        "       .long 14b,100b\n"
296                        "       .long 15b,100b\n"
297                        "       .long 16b,100b\n"
298                        "       .long 17b,100b\n"
299                        "       .long 18b,100b\n"
300                        "       .long 19b,100b\n"
301                        "       .long 20b,100b\n"
302                        "       .long 21b,100b\n"
303                        "       .long 22b,100b\n"
304                        "       .long 23b,100b\n"
305                        "       .long 24b,100b\n"
306                        "       .long 25b,100b\n"
307                        "       .long 26b,100b\n"
308                        "       .long 27b,100b\n"
309                        "       .long 28b,100b\n"
310                        "       .long 29b,100b\n"
311                        "       .long 30b,100b\n"
312                        "       .long 31b,100b\n"
313                        "       .long 32b,100b\n"
314                        "       .long 33b,100b\n"
315                        "       .long 34b,100b\n"
316                        "       .long 35b,100b\n"
317                        "       .long 36b,100b\n"
318                        "       .long 37b,100b\n"
319                        "       .long 99b,101b\n"
320                        ".previous"
321                        : "=&c"(size), "=&D" (d0), "=&S" (d1)
322                        :  "1"(to), "2"(from), "0"(size)
323                        : "eax", "edx", "memory");
324         return size;
325 }
326
327 static unsigned long
328 __copy_user_zeroing_intel(void *to, const void *from, unsigned long size)
329 {
330         int d0, d1;
331         __asm__ __volatile__(
332                        "        .align 2,0x90\n"
333                        "0:      movl 32(%4), %%eax\n"
334                        "        cmpl $67, %0\n"      
335                        "        jbe 2f\n"            
336                        "1:      movl 64(%4), %%eax\n"
337                        "        .align 2,0x90\n"     
338                        "2:      movl 0(%4), %%eax\n" 
339                        "21:     movl 4(%4), %%edx\n" 
340                        "        movl %%eax, 0(%3)\n" 
341                        "        movl %%edx, 4(%3)\n" 
342                        "3:      movl 8(%4), %%eax\n" 
343                        "31:     movl 12(%4),%%edx\n" 
344                        "        movl %%eax, 8(%3)\n" 
345                        "        movl %%edx, 12(%3)\n"
346                        "4:      movl 16(%4), %%eax\n"
347                        "41:     movl 20(%4), %%edx\n"
348                        "        movl %%eax, 16(%3)\n"
349                        "        movl %%edx, 20(%3)\n"
350                        "10:     movl 24(%4), %%eax\n"
351                        "51:     movl 28(%4), %%edx\n"
352                        "        movl %%eax, 24(%3)\n"
353                        "        movl %%edx, 28(%3)\n"
354                        "11:     movl 32(%4), %%eax\n"
355                        "61:     movl 36(%4), %%edx\n"
356                        "        movl %%eax, 32(%3)\n"
357                        "        movl %%edx, 36(%3)\n"
358                        "12:     movl 40(%4), %%eax\n"
359                        "71:     movl 44(%4), %%edx\n"
360                        "        movl %%eax, 40(%3)\n"
361                        "        movl %%edx, 44(%3)\n"
362                        "13:     movl 48(%4), %%eax\n"
363                        "81:     movl 52(%4), %%edx\n"
364                        "        movl %%eax, 48(%3)\n"
365                        "        movl %%edx, 52(%3)\n"
366                        "14:     movl 56(%4), %%eax\n"
367                        "91:     movl 60(%4), %%edx\n"
368                        "        movl %%eax, 56(%3)\n"
369                        "        movl %%edx, 60(%3)\n"
370                        "        addl $-64, %0\n"     
371                        "        addl $64, %4\n"      
372                        "        addl $64, %3\n"      
373                        "        cmpl $63, %0\n"      
374                        "        ja  0b\n"            
375                        "5:      movl  %0, %%eax\n"   
376                        "        shrl  $2, %0\n"      
377                        "        andl $3, %%eax\n"    
378                        "        cld\n"               
379                        "6:      rep; movsl\n"   
380                        "        movl %%eax,%0\n"
381                        "7:      rep; movsb\n"   
382                        "8:\n"                   
383                        ".section .fixup,\"ax\"\n"
384                        "9:      lea 0(%%eax,%0,4),%0\n" 
385                        "16:     pushl %0\n"     
386                        "        pushl %%eax\n"  
387                        "        xorl %%eax,%%eax\n"
388                        "        rep; stosb\n"   
389                        "        popl %%eax\n"   
390                        "        popl %0\n"      
391                        "        jmp 8b\n"       
392                        ".previous\n"            
393                        ".section __ex_table,\"a\"\n"
394                        "        .align 4\n"        
395                        "        .long 0b,16b\n"  
396                        "        .long 1b,16b\n"
397                        "        .long 2b,16b\n"
398                        "        .long 21b,16b\n"
399                        "        .long 3b,16b\n" 
400                        "        .long 31b,16b\n"
401                        "        .long 4b,16b\n" 
402                        "        .long 41b,16b\n"
403                        "        .long 10b,16b\n"
404                        "        .long 51b,16b\n"
405                        "        .long 11b,16b\n"
406                        "        .long 61b,16b\n"
407                        "        .long 12b,16b\n"
408                        "        .long 71b,16b\n"
409                        "        .long 13b,16b\n"
410                        "        .long 81b,16b\n"
411                        "        .long 14b,16b\n"
412                        "        .long 91b,16b\n"
413                        "        .long 6b,9b\n"  
414                        "        .long 7b,16b\n" 
415                        ".previous"              
416                        : "=&c"(size), "=&D" (d0), "=&S" (d1)
417                        :  "1"(to), "2"(from), "0"(size)
418                        : "eax", "edx", "memory");
419         return size;
420 }
421 #else
422 /*
423  * Leave these declared but undefined.  They should not be any references to
424  * them
425  */
426 unsigned long
427 __copy_user_zeroing_intel(void *to, const void *from, unsigned long size);
428 unsigned long
429 __copy_user_intel(void *to, const void *from,unsigned long size);
430 #endif /* CONFIG_X86_INTEL_USERCOPY */
431
432 /* Generic arbitrary sized copy.  */
433 #define __copy_user(to,from,size)                                       \
434 do {                                                                    \
435         int __d0, __d1, __d2;                                           \
436         __asm__ __volatile__(                                           \
437                 "       cmp  $7,%0\n"                                   \
438                 "       jbe  1f\n"                                      \
439                 "       movl %1,%0\n"                                   \
440                 "       negl %0\n"                                      \
441                 "       andl $7,%0\n"                                   \
442                 "       subl %0,%3\n"                                   \
443                 "4:     rep; movsb\n"                                   \
444                 "       movl %3,%0\n"                                   \
445                 "       shrl $2,%0\n"                                   \
446                 "       andl $3,%3\n"                                   \
447                 "       .align 2,0x90\n"                                \
448                 "0:     rep; movsl\n"                                   \
449                 "       movl %3,%0\n"                                   \
450                 "1:     rep; movsb\n"                                   \
451                 "2:\n"                                                  \
452                 ".section .fixup,\"ax\"\n"                              \
453                 "5:     addl %3,%0\n"                                   \
454                 "       jmp 2b\n"                                       \
455                 "3:     lea 0(%3,%0,4),%0\n"                            \
456                 "       jmp 2b\n"                                       \
457                 ".previous\n"                                           \
458                 ".section __ex_table,\"a\"\n"                           \
459                 "       .align 4\n"                                     \
460                 "       .long 4b,5b\n"                                  \
461                 "       .long 0b,3b\n"                                  \
462                 "       .long 1b,2b\n"                                  \
463                 ".previous"                                             \
464                 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2)   \
465                 : "3"(size), "0"(size), "1"(to), "2"(from)              \
466                 : "memory");                                            \
467 } while (0)
468
469 #define __copy_user_zeroing(to,from,size)                               \
470 do {                                                                    \
471         int __d0, __d1, __d2;                                           \
472         __asm__ __volatile__(                                           \
473                 "       cmp  $7,%0\n"                                   \
474                 "       jbe  1f\n"                                      \
475                 "       movl %1,%0\n"                                   \
476                 "       negl %0\n"                                      \
477                 "       andl $7,%0\n"                                   \
478                 "       subl %0,%3\n"                                   \
479                 "4:     rep; movsb\n"                                   \
480                 "       movl %3,%0\n"                                   \
481                 "       shrl $2,%0\n"                                   \
482                 "       andl $3,%3\n"                                   \
483                 "       .align 2,0x90\n"                                \
484                 "0:     rep; movsl\n"                                   \
485                 "       movl %3,%0\n"                                   \
486                 "1:     rep; movsb\n"                                   \
487                 "2:\n"                                                  \
488                 ".section .fixup,\"ax\"\n"                              \
489                 "5:     addl %3,%0\n"                                   \
490                 "       jmp 6f\n"                                       \
491                 "3:     lea 0(%3,%0,4),%0\n"                            \
492                 "6:     pushl %0\n"                                     \
493                 "       pushl %%eax\n"                                  \
494                 "       xorl %%eax,%%eax\n"                             \
495                 "       rep; stosb\n"                                   \
496                 "       popl %%eax\n"                                   \
497                 "       popl %0\n"                                      \
498                 "       jmp 2b\n"                                       \
499                 ".previous\n"                                           \
500                 ".section __ex_table,\"a\"\n"                           \
501                 "       .align 4\n"                                     \
502                 "       .long 4b,5b\n"                                  \
503                 "       .long 0b,3b\n"                                  \
504                 "       .long 1b,6b\n"                                  \
505                 ".previous"                                             \
506                 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2)   \
507                 : "3"(size), "0"(size), "1"(to), "2"(from)              \
508                 : "memory");                                            \
509 } while (0)
510
511
512 unsigned long __copy_to_user_ll(void __user *to, const void *from, unsigned long n)
513 {
514 #ifndef CONFIG_X86_WP_WORKS_OK
515         if (unlikely(boot_cpu_data.wp_works_ok == 0) &&
516                         ((unsigned long )to) < TASK_SIZE) {
517                 /* 
518                  * CPU does not honor the WP bit when writing
519                  * from supervisory mode, and due to preemption or SMP,
520                  * the page tables can change at any time.
521                  * Do it manually.      Manfred <manfred@colorfullife.com>
522                  */
523                 while (n) {
524                         unsigned long offset = ((unsigned long)to)%PAGE_SIZE;
525                         unsigned long len = PAGE_SIZE - offset;
526                         int retval;
527                         struct page *pg;
528                         void *maddr;
529                         
530                         if (len > n)
531                                 len = n;
532
533 survive:
534                         down_read(&current->mm->mmap_sem);
535                         retval = get_user_pages(current, current->mm,
536                                         (unsigned long )to, 1, 1, 0, &pg, NULL);
537
538                         if (retval == -ENOMEM && current->pid == 1) {
539                                 up_read(&current->mm->mmap_sem);
540                                 blk_congestion_wait(WRITE, HZ/50);
541                                 goto survive;
542                         }
543
544                         if (retval != 1) {
545                                 up_read(&current->mm->mmap_sem);
546                                 break;
547                         }
548
549                         maddr = kmap_atomic(pg, KM_USER0);
550                         memcpy(maddr + offset, from, len);
551                         kunmap_atomic(maddr, KM_USER0);
552                         set_page_dirty_lock(pg);
553                         put_page(pg);
554                         up_read(&current->mm->mmap_sem);
555
556                         from += len;
557                         to += len;
558                         n -= len;
559                 }
560                 return n;
561         }
562 #endif
563         if (movsl_is_ok(to, from, n))
564                 __copy_user((void *)to, from, n);
565         else
566                 n = __copy_user_intel((void *)to, from, n);
567         return n;
568 }
569
570 unsigned long __copy_from_user_ll(void *to, const void __user *from, unsigned long n)
571 {
572         if (movsl_is_ok(to, from, n))
573                 __copy_user_zeroing(to, (const void *) from, n);
574         else
575                 n = __copy_user_zeroing_intel(to, (const void *) from, n);
576         return n;
577 }
578