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 <asm/uaccess.h>
15 static inline int __movsl_is_ok(unsigned long a1, unsigned long a2, unsigned long n)
17 #ifdef CONFIG_X86_INTEL_USERCOPY
18 if (n >= 64 && ((a1 ^ a2) & movsl_mask.mask))
23 #define movsl_is_ok(a1,a2,n) \
24 __movsl_is_ok((unsigned long)(a1),(unsigned long)(a2),(n))
27 * Copy a null terminated string from userspace.
30 #define __do_strncpy_from_user(dst,src,count,res) \
32 int __d0, __d1, __d2; \
34 __asm__ __volatile__( \
39 " testb %%al,%%al\n" \
45 ".section .fixup,\"ax\"\n" \
49 ".section __ex_table,\"a\"\n" \
53 : "=d"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
55 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
60 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
61 * @dst: Destination address, in kernel space. This buffer must be at
62 * least @count bytes long.
63 * @src: Source address, in user space.
64 * @count: Maximum number of bytes to copy, including the trailing NUL.
66 * Copies a NUL-terminated string from userspace to kernel space.
67 * Caller must check the specified block with access_ok() before calling
70 * On success, returns the length of the string (not including the trailing
73 * If access to userspace fails, returns -EFAULT (some data may have been
76 * If @count is smaller than the length of the string, copies @count bytes
80 __strncpy_from_user(char *dst, const char __user *src, long count)
83 __do_strncpy_from_user(dst, src, count, res);
86 EXPORT_SYMBOL(__strncpy_from_user);
89 * strncpy_from_user: - Copy a NUL terminated string from userspace.
90 * @dst: Destination address, in kernel space. This buffer must be at
91 * least @count bytes long.
92 * @src: Source address, in user space.
93 * @count: Maximum number of bytes to copy, including the trailing NUL.
95 * Copies a NUL-terminated string from userspace to kernel space.
97 * On success, returns the length of the string (not including the trailing
100 * If access to userspace fails, returns -EFAULT (some data may have been
103 * If @count is smaller than the length of the string, copies @count bytes
104 * and returns @count.
107 strncpy_from_user(char *dst, const char __user *src, long count)
110 if (access_ok(VERIFY_READ, src, 1))
111 __do_strncpy_from_user(dst, src, count, res);
114 EXPORT_SYMBOL(strncpy_from_user);
120 #define __do_clear_user(addr,size) \
124 __asm__ __volatile__( \
129 ".section .fixup,\"ax\"\n" \
130 "3: lea 0(%2,%0,4),%0\n" \
133 ".section __ex_table,\"a\"\n" \
138 : "=&c"(size), "=&D" (__d0) \
139 : "r"(size & 3), "0"(size / 4), "1"(addr), "a"(0)); \
143 * clear_user: - Zero a block of memory in user space.
144 * @to: Destination address, in user space.
145 * @n: Number of bytes to zero.
147 * Zero a block of memory in user space.
149 * Returns number of bytes that could not be cleared.
150 * On success, this will be zero.
153 clear_user(void __user *to, unsigned long n)
156 if (access_ok(VERIFY_WRITE, to, n))
157 __do_clear_user(to, n);
160 EXPORT_SYMBOL(clear_user);
163 * __clear_user: - Zero a block of memory in user space, with less checking.
164 * @to: Destination address, in user space.
165 * @n: Number of bytes to zero.
167 * Zero a block of memory in user space. Caller must check
168 * the specified block with access_ok() before calling this function.
170 * Returns number of bytes that could not be cleared.
171 * On success, this will be zero.
174 __clear_user(void __user *to, unsigned long n)
176 __do_clear_user(to, n);
179 EXPORT_SYMBOL(__clear_user);
182 * strlen_user: - Get the size of a string in user space.
183 * @s: The string to measure.
184 * @n: The maximum valid length
186 * Get the size of a NUL-terminated string in user space.
188 * Returns the size of the string INCLUDING the terminating NUL.
189 * On exception, returns 0.
190 * If the string is too long, returns a value greater than @n.
192 long strnlen_user(const char __user *s, long n)
194 unsigned long mask = -__addr_ok(s);
195 unsigned long res, tmp;
199 __asm__ __volatile__(
208 ".section .fixup,\"ax\"\n"
209 "2: xorl %%eax,%%eax\n"
214 ".section __ex_table,\"a\"\n"
218 :"=r" (n), "=D" (s), "=a" (res), "=c" (tmp)
219 :"0" (n), "1" (s), "2" (0), "3" (mask)
223 EXPORT_SYMBOL(strnlen_user);
225 #ifdef CONFIG_X86_INTEL_USERCOPY
227 __copy_user_intel(void __user *to, const void *from, unsigned long size)
230 __asm__ __volatile__(
232 "1: movl 32(%4), %%eax\n"
235 "2: movl 64(%4), %%eax\n"
237 "3: movl 0(%4), %%eax\n"
238 "4: movl 4(%4), %%edx\n"
239 "5: movl %%eax, 0(%3)\n"
240 "6: movl %%edx, 4(%3)\n"
241 "7: movl 8(%4), %%eax\n"
242 "8: movl 12(%4),%%edx\n"
243 "9: movl %%eax, 8(%3)\n"
244 "10: movl %%edx, 12(%3)\n"
245 "11: movl 16(%4), %%eax\n"
246 "12: movl 20(%4), %%edx\n"
247 "13: movl %%eax, 16(%3)\n"
248 "14: movl %%edx, 20(%3)\n"
249 "15: movl 24(%4), %%eax\n"
250 "16: movl 28(%4), %%edx\n"
251 "17: movl %%eax, 24(%3)\n"
252 "18: movl %%edx, 28(%3)\n"
253 "19: movl 32(%4), %%eax\n"
254 "20: movl 36(%4), %%edx\n"
255 "21: movl %%eax, 32(%3)\n"
256 "22: movl %%edx, 36(%3)\n"
257 "23: movl 40(%4), %%eax\n"
258 "24: movl 44(%4), %%edx\n"
259 "25: movl %%eax, 40(%3)\n"
260 "26: movl %%edx, 44(%3)\n"
261 "27: movl 48(%4), %%eax\n"
262 "28: movl 52(%4), %%edx\n"
263 "29: movl %%eax, 48(%3)\n"
264 "30: movl %%edx, 52(%3)\n"
265 "31: movl 56(%4), %%eax\n"
266 "32: movl 60(%4), %%edx\n"
267 "33: movl %%eax, 56(%3)\n"
268 "34: movl %%edx, 60(%3)\n"
274 "35: movl %0, %%eax\n"
279 "36: movl %%eax, %0\n"
282 ".section .fixup,\"ax\"\n"
283 "101: lea 0(%%eax,%0,4),%0\n"
286 ".section __ex_table,\"a\"\n"
327 : "=&c"(size), "=&D" (d0), "=&S" (d1)
328 : "1"(to), "2"(from), "0"(size)
329 : "eax", "edx", "memory");
334 __copy_user_zeroing_intel(void *to, const void __user *from, unsigned long size)
337 __asm__ __volatile__(
339 "0: movl 32(%4), %%eax\n"
342 "1: movl 64(%4), %%eax\n"
344 "2: movl 0(%4), %%eax\n"
345 "21: movl 4(%4), %%edx\n"
346 " movl %%eax, 0(%3)\n"
347 " movl %%edx, 4(%3)\n"
348 "3: movl 8(%4), %%eax\n"
349 "31: movl 12(%4),%%edx\n"
350 " movl %%eax, 8(%3)\n"
351 " movl %%edx, 12(%3)\n"
352 "4: movl 16(%4), %%eax\n"
353 "41: movl 20(%4), %%edx\n"
354 " movl %%eax, 16(%3)\n"
355 " movl %%edx, 20(%3)\n"
356 "10: movl 24(%4), %%eax\n"
357 "51: movl 28(%4), %%edx\n"
358 " movl %%eax, 24(%3)\n"
359 " movl %%edx, 28(%3)\n"
360 "11: movl 32(%4), %%eax\n"
361 "61: movl 36(%4), %%edx\n"
362 " movl %%eax, 32(%3)\n"
363 " movl %%edx, 36(%3)\n"
364 "12: movl 40(%4), %%eax\n"
365 "71: movl 44(%4), %%edx\n"
366 " movl %%eax, 40(%3)\n"
367 " movl %%edx, 44(%3)\n"
368 "13: movl 48(%4), %%eax\n"
369 "81: movl 52(%4), %%edx\n"
370 " movl %%eax, 48(%3)\n"
371 " movl %%edx, 52(%3)\n"
372 "14: movl 56(%4), %%eax\n"
373 "91: movl 60(%4), %%edx\n"
374 " movl %%eax, 56(%3)\n"
375 " movl %%edx, 60(%3)\n"
381 "5: movl %0, %%eax\n"
389 ".section .fixup,\"ax\"\n"
390 "9: lea 0(%%eax,%0,4),%0\n"
393 " xorl %%eax,%%eax\n"
399 ".section __ex_table,\"a\"\n"
422 : "=&c"(size), "=&D" (d0), "=&S" (d1)
423 : "1"(to), "2"(from), "0"(size)
424 : "eax", "edx", "memory");
429 * Non Temporal Hint version of __copy_user_zeroing_intel. It is cache aware.
430 * hyoshiok@miraclelinux.com
433 static unsigned long __copy_user_zeroing_intel_nocache(void *to,
434 const void __user *from, unsigned long size)
438 __asm__ __volatile__(
440 "0: movl 32(%4), %%eax\n"
443 "1: movl 64(%4), %%eax\n"
445 "2: movl 0(%4), %%eax\n"
446 "21: movl 4(%4), %%edx\n"
447 " movnti %%eax, 0(%3)\n"
448 " movnti %%edx, 4(%3)\n"
449 "3: movl 8(%4), %%eax\n"
450 "31: movl 12(%4),%%edx\n"
451 " movnti %%eax, 8(%3)\n"
452 " movnti %%edx, 12(%3)\n"
453 "4: movl 16(%4), %%eax\n"
454 "41: movl 20(%4), %%edx\n"
455 " movnti %%eax, 16(%3)\n"
456 " movnti %%edx, 20(%3)\n"
457 "10: movl 24(%4), %%eax\n"
458 "51: movl 28(%4), %%edx\n"
459 " movnti %%eax, 24(%3)\n"
460 " movnti %%edx, 28(%3)\n"
461 "11: movl 32(%4), %%eax\n"
462 "61: movl 36(%4), %%edx\n"
463 " movnti %%eax, 32(%3)\n"
464 " movnti %%edx, 36(%3)\n"
465 "12: movl 40(%4), %%eax\n"
466 "71: movl 44(%4), %%edx\n"
467 " movnti %%eax, 40(%3)\n"
468 " movnti %%edx, 44(%3)\n"
469 "13: movl 48(%4), %%eax\n"
470 "81: movl 52(%4), %%edx\n"
471 " movnti %%eax, 48(%3)\n"
472 " movnti %%edx, 52(%3)\n"
473 "14: movl 56(%4), %%eax\n"
474 "91: movl 60(%4), %%edx\n"
475 " movnti %%eax, 56(%3)\n"
476 " movnti %%edx, 60(%3)\n"
483 "5: movl %0, %%eax\n"
491 ".section .fixup,\"ax\"\n"
492 "9: lea 0(%%eax,%0,4),%0\n"
495 " xorl %%eax,%%eax\n"
501 ".section __ex_table,\"a\"\n"
524 : "=&c"(size), "=&D" (d0), "=&S" (d1)
525 : "1"(to), "2"(from), "0"(size)
526 : "eax", "edx", "memory");
530 static unsigned long __copy_user_intel_nocache(void *to,
531 const void __user *from, unsigned long size)
535 __asm__ __volatile__(
537 "0: movl 32(%4), %%eax\n"
540 "1: movl 64(%4), %%eax\n"
542 "2: movl 0(%4), %%eax\n"
543 "21: movl 4(%4), %%edx\n"
544 " movnti %%eax, 0(%3)\n"
545 " movnti %%edx, 4(%3)\n"
546 "3: movl 8(%4), %%eax\n"
547 "31: movl 12(%4),%%edx\n"
548 " movnti %%eax, 8(%3)\n"
549 " movnti %%edx, 12(%3)\n"
550 "4: movl 16(%4), %%eax\n"
551 "41: movl 20(%4), %%edx\n"
552 " movnti %%eax, 16(%3)\n"
553 " movnti %%edx, 20(%3)\n"
554 "10: movl 24(%4), %%eax\n"
555 "51: movl 28(%4), %%edx\n"
556 " movnti %%eax, 24(%3)\n"
557 " movnti %%edx, 28(%3)\n"
558 "11: movl 32(%4), %%eax\n"
559 "61: movl 36(%4), %%edx\n"
560 " movnti %%eax, 32(%3)\n"
561 " movnti %%edx, 36(%3)\n"
562 "12: movl 40(%4), %%eax\n"
563 "71: movl 44(%4), %%edx\n"
564 " movnti %%eax, 40(%3)\n"
565 " movnti %%edx, 44(%3)\n"
566 "13: movl 48(%4), %%eax\n"
567 "81: movl 52(%4), %%edx\n"
568 " movnti %%eax, 48(%3)\n"
569 " movnti %%edx, 52(%3)\n"
570 "14: movl 56(%4), %%eax\n"
571 "91: movl 60(%4), %%edx\n"
572 " movnti %%eax, 56(%3)\n"
573 " movnti %%edx, 60(%3)\n"
580 "5: movl %0, %%eax\n"
588 ".section .fixup,\"ax\"\n"
589 "9: lea 0(%%eax,%0,4),%0\n"
592 ".section __ex_table,\"a\"\n"
615 : "=&c"(size), "=&D" (d0), "=&S" (d1)
616 : "1"(to), "2"(from), "0"(size)
617 : "eax", "edx", "memory");
624 * Leave these declared but undefined. They should not be any references to
627 unsigned long __copy_user_zeroing_intel(void *to, const void __user *from,
629 unsigned long __copy_user_intel(void __user *to, const void *from,
631 unsigned long __copy_user_zeroing_intel_nocache(void *to,
632 const void __user *from, unsigned long size);
633 #endif /* CONFIG_X86_INTEL_USERCOPY */
635 /* Generic arbitrary sized copy. */
636 #define __copy_user(to,from,size) \
638 int __d0, __d1, __d2; \
639 __asm__ __volatile__( \
655 ".section .fixup,\"ax\"\n" \
658 "3: lea 0(%3,%0,4),%0\n" \
661 ".section __ex_table,\"a\"\n" \
667 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
668 : "3"(size), "0"(size), "1"(to), "2"(from) \
672 #define __copy_user_zeroing(to,from,size) \
674 int __d0, __d1, __d2; \
675 __asm__ __volatile__( \
691 ".section .fixup,\"ax\"\n" \
694 "3: lea 0(%3,%0,4),%0\n" \
697 " xorl %%eax,%%eax\n" \
703 ".section __ex_table,\"a\"\n" \
709 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
710 : "3"(size), "0"(size), "1"(to), "2"(from) \
714 unsigned long __copy_to_user_ll(void __user *to, const void *from,
717 BUG_ON((long) n < 0);
718 #ifndef CONFIG_X86_WP_WORKS_OK
719 if (unlikely(boot_cpu_data.wp_works_ok == 0) &&
720 ((unsigned long )to) < TASK_SIZE) {
722 * CPU does not honor the WP bit when writing
723 * from supervisory mode, and due to preemption or SMP,
724 * the page tables can change at any time.
725 * Do it manually. Manfred <manfred@colorfullife.com>
728 unsigned long offset = ((unsigned long)to)%PAGE_SIZE;
729 unsigned long len = PAGE_SIZE - offset;
738 down_read(¤t->mm->mmap_sem);
739 retval = get_user_pages(current, current->mm,
740 (unsigned long )to, 1, 1, 0, &pg, NULL);
742 if (retval == -ENOMEM && is_init(current)) {
743 up_read(¤t->mm->mmap_sem);
744 blk_congestion_wait(WRITE, HZ/50);
749 up_read(¤t->mm->mmap_sem);
753 maddr = kmap_atomic(pg, KM_USER0);
754 memcpy(maddr + offset, from, len);
755 kunmap_atomic(maddr, KM_USER0);
756 set_page_dirty_lock(pg);
758 up_read(¤t->mm->mmap_sem);
767 if (movsl_is_ok(to, from, n))
768 __copy_user(to, from, n);
770 n = __copy_user_intel(to, from, n);
773 EXPORT_SYMBOL(__copy_to_user_ll);
775 unsigned long __copy_from_user_ll(void *to, const void __user *from,
779 if (movsl_is_ok(to, from, n))
780 __copy_user_zeroing(to, from, n);
782 n = __copy_user_zeroing_intel(to, from, n);
785 EXPORT_SYMBOL(__copy_from_user_ll);
787 unsigned long __copy_from_user_ll_nozero(void *to, const void __user *from,
791 if (movsl_is_ok(to, from, n))
792 __copy_user(to, from, n);
794 n = __copy_user_intel((void __user *)to,
795 (const void *)from, n);
798 EXPORT_SYMBOL(__copy_from_user_ll_nozero);
800 unsigned long __copy_from_user_ll_nocache(void *to, const void __user *from,
804 #ifdef CONFIG_X86_INTEL_USERCOPY
805 if ( n > 64 && cpu_has_xmm2)
806 n = __copy_user_zeroing_intel_nocache(to, from, n);
808 __copy_user_zeroing(to, from, n);
810 __copy_user_zeroing(to, from, n);
815 unsigned long __copy_from_user_ll_nocache_nozero(void *to, const void __user *from,
819 #ifdef CONFIG_X86_INTEL_USERCOPY
820 if ( n > 64 && cpu_has_xmm2)
821 n = __copy_user_intel_nocache(to, from, n);
823 __copy_user(to, from, n);
825 __copy_user(to, from, n);
831 * copy_to_user: - Copy a block of data into user space.
832 * @to: Destination address, in user space.
833 * @from: Source address, in kernel space.
834 * @n: Number of bytes to copy.
836 * Context: User context only. This function may sleep.
838 * Copy data from kernel space to user space.
840 * Returns number of bytes that could not be copied.
841 * On success, this will be zero.
844 copy_to_user(void __user *to, const void *from, unsigned long n)
846 BUG_ON((long) n < 0);
847 if (access_ok(VERIFY_WRITE, to, n))
848 n = __copy_to_user(to, from, n);
851 EXPORT_SYMBOL(copy_to_user);
854 * copy_from_user: - Copy a block of data from user space.
855 * @to: Destination address, in kernel space.
856 * @from: Source address, in user space.
857 * @n: Number of bytes to copy.
859 * Context: User context only. This function may sleep.
861 * Copy data from user space to kernel space.
863 * Returns number of bytes that could not be copied.
864 * On success, this will be zero.
866 * If some data could not be copied, this function will pad the copied
867 * data to the requested size using zero bytes.
870 copy_from_user(void *to, const void __user *from, unsigned long n)
872 BUG_ON((long) n < 0);
873 if (access_ok(VERIFY_READ, from, n))
874 n = __copy_from_user(to, from, n);
879 EXPORT_SYMBOL(copy_from_user);