2 * User address space access functions.
3 * The non inlined parts of asm-i386/uaccess.h are here.
5 * Copyright 1997 Andi Kleen <ak@muc.de>
6 * Copyright 1997 Linus Torvalds
9 #include <linux/highmem.h>
10 #include <linux/blkdev.h>
11 #include <linux/module.h>
12 #include <linux/backing-dev.h>
13 #include <linux/interrupt.h>
14 #include <asm/uaccess.h>
17 static inline int __movsl_is_ok(unsigned long a1, unsigned long a2, unsigned long n)
19 #ifdef CONFIG_X86_INTEL_USERCOPY
20 if (n >= 64 && ((a1 ^ a2) & movsl_mask.mask))
25 #define movsl_is_ok(a1,a2,n) \
26 __movsl_is_ok((unsigned long)(a1),(unsigned long)(a2),(n))
29 * Copy a null terminated string from userspace.
32 #define __do_strncpy_from_user(dst,src,count,res) \
34 int __d0, __d1, __d2; \
36 __asm__ __volatile__( \
41 " testb %%al,%%al\n" \
47 ".section .fixup,\"ax\"\n" \
51 ".section __ex_table,\"a\"\n" \
55 : "=d"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
57 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
62 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
63 * @dst: Destination address, in kernel space. This buffer must be at
64 * least @count bytes long.
65 * @src: Source address, in user space.
66 * @count: Maximum number of bytes to copy, including the trailing NUL.
68 * Copies a NUL-terminated string from userspace to kernel space.
69 * Caller must check the specified block with access_ok() before calling
72 * On success, returns the length of the string (not including the trailing
75 * If access to userspace fails, returns -EFAULT (some data may have been
78 * If @count is smaller than the length of the string, copies @count bytes
82 __strncpy_from_user(char *dst, const char __user *src, long count)
85 __do_strncpy_from_user(dst, src, count, res);
88 EXPORT_SYMBOL(__strncpy_from_user);
91 * strncpy_from_user: - Copy a NUL terminated string from userspace.
92 * @dst: Destination address, in kernel space. This buffer must be at
93 * least @count bytes long.
94 * @src: Source address, in user space.
95 * @count: Maximum number of bytes to copy, including the trailing NUL.
97 * Copies a NUL-terminated string from userspace to kernel space.
99 * On success, returns the length of the string (not including the trailing
102 * If access to userspace fails, returns -EFAULT (some data may have been
105 * If @count is smaller than the length of the string, copies @count bytes
106 * and returns @count.
109 strncpy_from_user(char *dst, const char __user *src, long count)
112 if (access_ok(VERIFY_READ, src, 1))
113 __do_strncpy_from_user(dst, src, count, res);
116 EXPORT_SYMBOL(strncpy_from_user);
122 #define __do_clear_user(addr,size) \
126 __asm__ __volatile__( \
131 ".section .fixup,\"ax\"\n" \
132 "3: lea 0(%2,%0,4),%0\n" \
135 ".section __ex_table,\"a\"\n" \
140 : "=&c"(size), "=&D" (__d0) \
141 : "r"(size & 3), "0"(size / 4), "1"(addr), "a"(0)); \
145 * clear_user: - Zero a block of memory in user space.
146 * @to: Destination address, in user space.
147 * @n: Number of bytes to zero.
149 * Zero a block of memory in user space.
151 * Returns number of bytes that could not be cleared.
152 * On success, this will be zero.
155 clear_user(void __user *to, unsigned long n)
158 if (access_ok(VERIFY_WRITE, to, n))
159 __do_clear_user(to, n);
162 EXPORT_SYMBOL(clear_user);
165 * __clear_user: - Zero a block of memory in user space, with less checking.
166 * @to: Destination address, in user space.
167 * @n: Number of bytes to zero.
169 * Zero a block of memory in user space. Caller must check
170 * the specified block with access_ok() before calling this function.
172 * Returns number of bytes that could not be cleared.
173 * On success, this will be zero.
176 __clear_user(void __user *to, unsigned long n)
178 __do_clear_user(to, n);
181 EXPORT_SYMBOL(__clear_user);
184 * strnlen_user: - Get the size of a string in user space.
185 * @s: The string to measure.
186 * @n: The maximum valid length
188 * Get the size of a NUL-terminated string in user space.
190 * Returns the size of the string INCLUDING the terminating NUL.
191 * On exception, returns 0.
192 * If the string is too long, returns a value greater than @n.
194 long strnlen_user(const char __user *s, long n)
196 unsigned long mask = -__addr_ok(s);
197 unsigned long res, tmp;
201 __asm__ __volatile__(
210 ".section .fixup,\"ax\"\n"
211 "2: xorl %%eax,%%eax\n"
216 ".section __ex_table,\"a\"\n"
220 :"=r" (n), "=D" (s), "=a" (res), "=c" (tmp)
221 :"0" (n), "1" (s), "2" (0), "3" (mask)
225 EXPORT_SYMBOL(strnlen_user);
227 #ifdef CONFIG_X86_INTEL_USERCOPY
229 __copy_user_intel(void __user *to, const void *from, unsigned long size)
232 __asm__ __volatile__(
234 "1: movl 32(%4), %%eax\n"
237 "2: movl 64(%4), %%eax\n"
239 "3: movl 0(%4), %%eax\n"
240 "4: movl 4(%4), %%edx\n"
241 "5: movl %%eax, 0(%3)\n"
242 "6: movl %%edx, 4(%3)\n"
243 "7: movl 8(%4), %%eax\n"
244 "8: movl 12(%4),%%edx\n"
245 "9: movl %%eax, 8(%3)\n"
246 "10: movl %%edx, 12(%3)\n"
247 "11: movl 16(%4), %%eax\n"
248 "12: movl 20(%4), %%edx\n"
249 "13: movl %%eax, 16(%3)\n"
250 "14: movl %%edx, 20(%3)\n"
251 "15: movl 24(%4), %%eax\n"
252 "16: movl 28(%4), %%edx\n"
253 "17: movl %%eax, 24(%3)\n"
254 "18: movl %%edx, 28(%3)\n"
255 "19: movl 32(%4), %%eax\n"
256 "20: movl 36(%4), %%edx\n"
257 "21: movl %%eax, 32(%3)\n"
258 "22: movl %%edx, 36(%3)\n"
259 "23: movl 40(%4), %%eax\n"
260 "24: movl 44(%4), %%edx\n"
261 "25: movl %%eax, 40(%3)\n"
262 "26: movl %%edx, 44(%3)\n"
263 "27: movl 48(%4), %%eax\n"
264 "28: movl 52(%4), %%edx\n"
265 "29: movl %%eax, 48(%3)\n"
266 "30: movl %%edx, 52(%3)\n"
267 "31: movl 56(%4), %%eax\n"
268 "32: movl 60(%4), %%edx\n"
269 "33: movl %%eax, 56(%3)\n"
270 "34: movl %%edx, 60(%3)\n"
276 "35: movl %0, %%eax\n"
281 "36: movl %%eax, %0\n"
284 ".section .fixup,\"ax\"\n"
285 "101: lea 0(%%eax,%0,4),%0\n"
288 ".section __ex_table,\"a\"\n"
329 : "=&c"(size), "=&D" (d0), "=&S" (d1)
330 : "1"(to), "2"(from), "0"(size)
331 : "eax", "edx", "memory");
336 __copy_user_zeroing_intel(void *to, const void __user *from, unsigned long size)
339 __asm__ __volatile__(
341 "0: movl 32(%4), %%eax\n"
344 "1: movl 64(%4), %%eax\n"
346 "2: movl 0(%4), %%eax\n"
347 "21: movl 4(%4), %%edx\n"
348 " movl %%eax, 0(%3)\n"
349 " movl %%edx, 4(%3)\n"
350 "3: movl 8(%4), %%eax\n"
351 "31: movl 12(%4),%%edx\n"
352 " movl %%eax, 8(%3)\n"
353 " movl %%edx, 12(%3)\n"
354 "4: movl 16(%4), %%eax\n"
355 "41: movl 20(%4), %%edx\n"
356 " movl %%eax, 16(%3)\n"
357 " movl %%edx, 20(%3)\n"
358 "10: movl 24(%4), %%eax\n"
359 "51: movl 28(%4), %%edx\n"
360 " movl %%eax, 24(%3)\n"
361 " movl %%edx, 28(%3)\n"
362 "11: movl 32(%4), %%eax\n"
363 "61: movl 36(%4), %%edx\n"
364 " movl %%eax, 32(%3)\n"
365 " movl %%edx, 36(%3)\n"
366 "12: movl 40(%4), %%eax\n"
367 "71: movl 44(%4), %%edx\n"
368 " movl %%eax, 40(%3)\n"
369 " movl %%edx, 44(%3)\n"
370 "13: movl 48(%4), %%eax\n"
371 "81: movl 52(%4), %%edx\n"
372 " movl %%eax, 48(%3)\n"
373 " movl %%edx, 52(%3)\n"
374 "14: movl 56(%4), %%eax\n"
375 "91: movl 60(%4), %%edx\n"
376 " movl %%eax, 56(%3)\n"
377 " movl %%edx, 60(%3)\n"
383 "5: movl %0, %%eax\n"
391 ".section .fixup,\"ax\"\n"
392 "9: lea 0(%%eax,%0,4),%0\n"
395 " xorl %%eax,%%eax\n"
401 ".section __ex_table,\"a\"\n"
424 : "=&c"(size), "=&D" (d0), "=&S" (d1)
425 : "1"(to), "2"(from), "0"(size)
426 : "eax", "edx", "memory");
431 * Non Temporal Hint version of __copy_user_zeroing_intel. It is cache aware.
432 * hyoshiok@miraclelinux.com
435 static unsigned long __copy_user_zeroing_intel_nocache(void *to,
436 const void __user *from, unsigned long size)
440 __asm__ __volatile__(
442 "0: movl 32(%4), %%eax\n"
445 "1: movl 64(%4), %%eax\n"
447 "2: movl 0(%4), %%eax\n"
448 "21: movl 4(%4), %%edx\n"
449 " movnti %%eax, 0(%3)\n"
450 " movnti %%edx, 4(%3)\n"
451 "3: movl 8(%4), %%eax\n"
452 "31: movl 12(%4),%%edx\n"
453 " movnti %%eax, 8(%3)\n"
454 " movnti %%edx, 12(%3)\n"
455 "4: movl 16(%4), %%eax\n"
456 "41: movl 20(%4), %%edx\n"
457 " movnti %%eax, 16(%3)\n"
458 " movnti %%edx, 20(%3)\n"
459 "10: movl 24(%4), %%eax\n"
460 "51: movl 28(%4), %%edx\n"
461 " movnti %%eax, 24(%3)\n"
462 " movnti %%edx, 28(%3)\n"
463 "11: movl 32(%4), %%eax\n"
464 "61: movl 36(%4), %%edx\n"
465 " movnti %%eax, 32(%3)\n"
466 " movnti %%edx, 36(%3)\n"
467 "12: movl 40(%4), %%eax\n"
468 "71: movl 44(%4), %%edx\n"
469 " movnti %%eax, 40(%3)\n"
470 " movnti %%edx, 44(%3)\n"
471 "13: movl 48(%4), %%eax\n"
472 "81: movl 52(%4), %%edx\n"
473 " movnti %%eax, 48(%3)\n"
474 " movnti %%edx, 52(%3)\n"
475 "14: movl 56(%4), %%eax\n"
476 "91: movl 60(%4), %%edx\n"
477 " movnti %%eax, 56(%3)\n"
478 " movnti %%edx, 60(%3)\n"
485 "5: movl %0, %%eax\n"
493 ".section .fixup,\"ax\"\n"
494 "9: lea 0(%%eax,%0,4),%0\n"
497 " xorl %%eax,%%eax\n"
503 ".section __ex_table,\"a\"\n"
526 : "=&c"(size), "=&D" (d0), "=&S" (d1)
527 : "1"(to), "2"(from), "0"(size)
528 : "eax", "edx", "memory");
532 static unsigned long __copy_user_intel_nocache(void *to,
533 const void __user *from, unsigned long size)
537 __asm__ __volatile__(
539 "0: movl 32(%4), %%eax\n"
542 "1: movl 64(%4), %%eax\n"
544 "2: movl 0(%4), %%eax\n"
545 "21: movl 4(%4), %%edx\n"
546 " movnti %%eax, 0(%3)\n"
547 " movnti %%edx, 4(%3)\n"
548 "3: movl 8(%4), %%eax\n"
549 "31: movl 12(%4),%%edx\n"
550 " movnti %%eax, 8(%3)\n"
551 " movnti %%edx, 12(%3)\n"
552 "4: movl 16(%4), %%eax\n"
553 "41: movl 20(%4), %%edx\n"
554 " movnti %%eax, 16(%3)\n"
555 " movnti %%edx, 20(%3)\n"
556 "10: movl 24(%4), %%eax\n"
557 "51: movl 28(%4), %%edx\n"
558 " movnti %%eax, 24(%3)\n"
559 " movnti %%edx, 28(%3)\n"
560 "11: movl 32(%4), %%eax\n"
561 "61: movl 36(%4), %%edx\n"
562 " movnti %%eax, 32(%3)\n"
563 " movnti %%edx, 36(%3)\n"
564 "12: movl 40(%4), %%eax\n"
565 "71: movl 44(%4), %%edx\n"
566 " movnti %%eax, 40(%3)\n"
567 " movnti %%edx, 44(%3)\n"
568 "13: movl 48(%4), %%eax\n"
569 "81: movl 52(%4), %%edx\n"
570 " movnti %%eax, 48(%3)\n"
571 " movnti %%edx, 52(%3)\n"
572 "14: movl 56(%4), %%eax\n"
573 "91: movl 60(%4), %%edx\n"
574 " movnti %%eax, 56(%3)\n"
575 " movnti %%edx, 60(%3)\n"
582 "5: movl %0, %%eax\n"
590 ".section .fixup,\"ax\"\n"
591 "9: lea 0(%%eax,%0,4),%0\n"
594 ".section __ex_table,\"a\"\n"
617 : "=&c"(size), "=&D" (d0), "=&S" (d1)
618 : "1"(to), "2"(from), "0"(size)
619 : "eax", "edx", "memory");
626 * Leave these declared but undefined. They should not be any references to
629 unsigned long __copy_user_zeroing_intel(void *to, const void __user *from,
631 unsigned long __copy_user_intel(void __user *to, const void *from,
633 unsigned long __copy_user_zeroing_intel_nocache(void *to,
634 const void __user *from, unsigned long size);
635 #endif /* CONFIG_X86_INTEL_USERCOPY */
637 /* Generic arbitrary sized copy. */
638 #define __copy_user(to,from,size) \
640 int __d0, __d1, __d2; \
641 __asm__ __volatile__( \
657 ".section .fixup,\"ax\"\n" \
660 "3: lea 0(%3,%0,4),%0\n" \
663 ".section __ex_table,\"a\"\n" \
669 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
670 : "3"(size), "0"(size), "1"(to), "2"(from) \
674 #define __copy_user_zeroing(to,from,size) \
676 int __d0, __d1, __d2; \
677 __asm__ __volatile__( \
693 ".section .fixup,\"ax\"\n" \
696 "3: lea 0(%3,%0,4),%0\n" \
699 " xorl %%eax,%%eax\n" \
705 ".section __ex_table,\"a\"\n" \
711 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
712 : "3"(size), "0"(size), "1"(to), "2"(from) \
716 unsigned long __copy_to_user_ll(void __user *to, const void *from,
719 #ifndef CONFIG_X86_WP_WORKS_OK
720 if (unlikely(boot_cpu_data.wp_works_ok == 0) &&
721 ((unsigned long )to) < TASK_SIZE) {
723 * When we are in an atomic section (see
724 * mm/filemap.c:file_read_actor), return the full
725 * length to take the slow path.
731 * CPU does not honor the WP bit when writing
732 * from supervisory mode, and due to preemption or SMP,
733 * the page tables can change at any time.
734 * Do it manually. Manfred <manfred@colorfullife.com>
737 unsigned long offset = ((unsigned long)to)%PAGE_SIZE;
738 unsigned long len = PAGE_SIZE - offset;
747 down_read(¤t->mm->mmap_sem);
748 retval = get_user_pages(current, current->mm,
749 (unsigned long )to, 1, 1, 0, &pg, NULL);
751 if (retval == -ENOMEM && is_init(current)) {
752 up_read(¤t->mm->mmap_sem);
753 congestion_wait(WRITE, HZ/50);
758 up_read(¤t->mm->mmap_sem);
762 maddr = kmap_atomic(pg, KM_USER0);
763 memcpy(maddr + offset, from, len);
764 kunmap_atomic(maddr, KM_USER0);
765 set_page_dirty_lock(pg);
767 up_read(¤t->mm->mmap_sem);
776 if (movsl_is_ok(to, from, n))
777 __copy_user(to, from, n);
779 n = __copy_user_intel(to, from, n);
782 EXPORT_SYMBOL(__copy_to_user_ll);
784 unsigned long __copy_from_user_ll(void *to, const void __user *from,
787 if (movsl_is_ok(to, from, n))
788 __copy_user_zeroing(to, from, n);
790 n = __copy_user_zeroing_intel(to, from, n);
793 EXPORT_SYMBOL(__copy_from_user_ll);
795 unsigned long __copy_from_user_ll_nozero(void *to, const void __user *from,
798 if (movsl_is_ok(to, from, n))
799 __copy_user(to, from, n);
801 n = __copy_user_intel((void __user *)to,
802 (const void *)from, n);
805 EXPORT_SYMBOL(__copy_from_user_ll_nozero);
807 unsigned long __copy_from_user_ll_nocache(void *to, const void __user *from,
810 #ifdef CONFIG_X86_INTEL_USERCOPY
811 if ( n > 64 && cpu_has_xmm2)
812 n = __copy_user_zeroing_intel_nocache(to, from, n);
814 __copy_user_zeroing(to, from, n);
816 __copy_user_zeroing(to, from, n);
821 unsigned long __copy_from_user_ll_nocache_nozero(void *to, const void __user *from,
824 #ifdef CONFIG_X86_INTEL_USERCOPY
825 if ( n > 64 && cpu_has_xmm2)
826 n = __copy_user_intel_nocache(to, from, n);
828 __copy_user(to, from, n);
830 __copy_user(to, from, n);
836 * copy_to_user: - Copy a block of data into user space.
837 * @to: Destination address, in user space.
838 * @from: Source address, in kernel space.
839 * @n: Number of bytes to copy.
841 * Context: User context only. This function may sleep.
843 * Copy data from kernel space to user space.
845 * Returns number of bytes that could not be copied.
846 * On success, this will be zero.
849 copy_to_user(void __user *to, const void *from, unsigned long n)
851 if (access_ok(VERIFY_WRITE, to, n))
852 n = __copy_to_user(to, from, n);
855 EXPORT_SYMBOL(copy_to_user);
858 * copy_from_user: - Copy a block of data from user space.
859 * @to: Destination address, in kernel space.
860 * @from: Source address, in user space.
861 * @n: Number of bytes to copy.
863 * Context: User context only. This function may sleep.
865 * Copy data from user space to kernel space.
867 * Returns number of bytes that could not be copied.
868 * On success, this will be zero.
870 * If some data could not be copied, this function will pad the copied
871 * data to the requested size using zero bytes.
874 copy_from_user(void *to, const void __user *from, unsigned long n)
876 if (access_ok(VERIFY_READ, from, n))
877 n = __copy_from_user(to, from, n);
882 EXPORT_SYMBOL(copy_from_user);