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" \
52 : "=d"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
54 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
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.
65 * Copies a NUL-terminated string from userspace to kernel space.
66 * Caller must check the specified block with access_ok() before calling
69 * On success, returns the length of the string (not including the trailing
72 * If access to userspace fails, returns -EFAULT (some data may have been
75 * If @count is smaller than the length of the string, copies @count bytes
79 __strncpy_from_user(char *dst
, const char __user
*src
, long count
)
82 __do_strncpy_from_user(dst
, src
, count
, res
);
85 EXPORT_SYMBOL(__strncpy_from_user
);
88 * strncpy_from_user: - Copy a NUL terminated string from userspace.
89 * @dst: Destination address, in kernel space. This buffer must be at
90 * least @count bytes long.
91 * @src: Source address, in user space.
92 * @count: Maximum number of bytes to copy, including the trailing NUL.
94 * Copies a NUL-terminated string from userspace to kernel space.
96 * On success, returns the length of the string (not including the trailing
99 * If access to userspace fails, returns -EFAULT (some data may have been
102 * If @count is smaller than the length of the string, copies @count bytes
103 * and returns @count.
106 strncpy_from_user(char *dst
, const char __user
*src
, long count
)
109 if (access_ok(VERIFY_READ
, src
, 1))
110 __do_strncpy_from_user(dst
, src
, count
, res
);
113 EXPORT_SYMBOL(strncpy_from_user
);
119 #define __do_clear_user(addr,size) \
123 __asm__ __volatile__( \
128 ".section .fixup,\"ax\"\n" \
129 "3: lea 0(%2,%0,4),%0\n" \
132 _ASM_EXTABLE(0b,3b) \
133 _ASM_EXTABLE(1b,2b) \
134 : "=&c"(size), "=&D" (__d0) \
135 : "r"(size & 3), "0"(size / 4), "1"(addr), "a"(0)); \
139 * clear_user: - Zero a block of memory in user space.
140 * @to: Destination address, in user space.
141 * @n: Number of bytes to zero.
143 * Zero a block of memory in user space.
145 * Returns number of bytes that could not be cleared.
146 * On success, this will be zero.
149 clear_user(void __user
*to
, unsigned long n
)
152 if (access_ok(VERIFY_WRITE
, to
, n
))
153 __do_clear_user(to
, n
);
156 EXPORT_SYMBOL(clear_user
);
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.
163 * Zero a block of memory in user space. Caller must check
164 * the specified block with access_ok() before calling this function.
166 * Returns number of bytes that could not be cleared.
167 * On success, this will be zero.
170 __clear_user(void __user
*to
, unsigned long n
)
172 __do_clear_user(to
, n
);
175 EXPORT_SYMBOL(__clear_user
);
178 * strnlen_user: - Get the size of a string in user space.
179 * @s: The string to measure.
180 * @n: The maximum valid length
182 * Get the size of a NUL-terminated string in user space.
184 * Returns the size of the string INCLUDING the terminating NUL.
185 * On exception, returns 0.
186 * If the string is too long, returns a value greater than @n.
188 long strnlen_user(const char __user
*s
, long n
)
190 unsigned long mask
= -__addr_ok(s
);
191 unsigned long res
, tmp
;
195 __asm__
__volatile__(
204 ".section .fixup,\"ax\"\n"
205 "2: xorl %%eax,%%eax\n"
210 ".section __ex_table,\"a\"\n"
214 :"=r" (n
), "=D" (s
), "=a" (res
), "=c" (tmp
)
215 :"0" (n
), "1" (s
), "2" (0), "3" (mask
)
219 EXPORT_SYMBOL(strnlen_user
);
221 #ifdef CONFIG_X86_INTEL_USERCOPY
223 __copy_user_intel(void __user
*to
, const void *from
, unsigned long size
)
226 __asm__
__volatile__(
228 "1: movl 32(%4), %%eax\n"
231 "2: movl 64(%4), %%eax\n"
233 "3: movl 0(%4), %%eax\n"
234 "4: movl 4(%4), %%edx\n"
235 "5: movl %%eax, 0(%3)\n"
236 "6: movl %%edx, 4(%3)\n"
237 "7: movl 8(%4), %%eax\n"
238 "8: movl 12(%4),%%edx\n"
239 "9: movl %%eax, 8(%3)\n"
240 "10: movl %%edx, 12(%3)\n"
241 "11: movl 16(%4), %%eax\n"
242 "12: movl 20(%4), %%edx\n"
243 "13: movl %%eax, 16(%3)\n"
244 "14: movl %%edx, 20(%3)\n"
245 "15: movl 24(%4), %%eax\n"
246 "16: movl 28(%4), %%edx\n"
247 "17: movl %%eax, 24(%3)\n"
248 "18: movl %%edx, 28(%3)\n"
249 "19: movl 32(%4), %%eax\n"
250 "20: movl 36(%4), %%edx\n"
251 "21: movl %%eax, 32(%3)\n"
252 "22: movl %%edx, 36(%3)\n"
253 "23: movl 40(%4), %%eax\n"
254 "24: movl 44(%4), %%edx\n"
255 "25: movl %%eax, 40(%3)\n"
256 "26: movl %%edx, 44(%3)\n"
257 "27: movl 48(%4), %%eax\n"
258 "28: movl 52(%4), %%edx\n"
259 "29: movl %%eax, 48(%3)\n"
260 "30: movl %%edx, 52(%3)\n"
261 "31: movl 56(%4), %%eax\n"
262 "32: movl 60(%4), %%edx\n"
263 "33: movl %%eax, 56(%3)\n"
264 "34: movl %%edx, 60(%3)\n"
270 "35: movl %0, %%eax\n"
275 "36: movl %%eax, %0\n"
278 ".section .fixup,\"ax\"\n"
279 "101: lea 0(%%eax,%0,4),%0\n"
282 ".section __ex_table,\"a\"\n"
323 : "=&c"(size
), "=&D" (d0
), "=&S" (d1
)
324 : "1"(to
), "2"(from
), "0"(size
)
325 : "eax", "edx", "memory");
330 __copy_user_zeroing_intel(void *to
, const void __user
*from
, unsigned long size
)
333 __asm__
__volatile__(
335 "0: movl 32(%4), %%eax\n"
338 "1: movl 64(%4), %%eax\n"
340 "2: movl 0(%4), %%eax\n"
341 "21: movl 4(%4), %%edx\n"
342 " movl %%eax, 0(%3)\n"
343 " movl %%edx, 4(%3)\n"
344 "3: movl 8(%4), %%eax\n"
345 "31: movl 12(%4),%%edx\n"
346 " movl %%eax, 8(%3)\n"
347 " movl %%edx, 12(%3)\n"
348 "4: movl 16(%4), %%eax\n"
349 "41: movl 20(%4), %%edx\n"
350 " movl %%eax, 16(%3)\n"
351 " movl %%edx, 20(%3)\n"
352 "10: movl 24(%4), %%eax\n"
353 "51: movl 28(%4), %%edx\n"
354 " movl %%eax, 24(%3)\n"
355 " movl %%edx, 28(%3)\n"
356 "11: movl 32(%4), %%eax\n"
357 "61: movl 36(%4), %%edx\n"
358 " movl %%eax, 32(%3)\n"
359 " movl %%edx, 36(%3)\n"
360 "12: movl 40(%4), %%eax\n"
361 "71: movl 44(%4), %%edx\n"
362 " movl %%eax, 40(%3)\n"
363 " movl %%edx, 44(%3)\n"
364 "13: movl 48(%4), %%eax\n"
365 "81: movl 52(%4), %%edx\n"
366 " movl %%eax, 48(%3)\n"
367 " movl %%edx, 52(%3)\n"
368 "14: movl 56(%4), %%eax\n"
369 "91: movl 60(%4), %%edx\n"
370 " movl %%eax, 56(%3)\n"
371 " movl %%edx, 60(%3)\n"
377 "5: movl %0, %%eax\n"
385 ".section .fixup,\"ax\"\n"
386 "9: lea 0(%%eax,%0,4),%0\n"
389 " xorl %%eax,%%eax\n"
395 ".section __ex_table,\"a\"\n"
418 : "=&c"(size
), "=&D" (d0
), "=&S" (d1
)
419 : "1"(to
), "2"(from
), "0"(size
)
420 : "eax", "edx", "memory");
425 * Non Temporal Hint version of __copy_user_zeroing_intel. It is cache aware.
426 * hyoshiok@miraclelinux.com
429 static unsigned long __copy_user_zeroing_intel_nocache(void *to
,
430 const void __user
*from
, unsigned long size
)
434 __asm__
__volatile__(
436 "0: movl 32(%4), %%eax\n"
439 "1: movl 64(%4), %%eax\n"
441 "2: movl 0(%4), %%eax\n"
442 "21: movl 4(%4), %%edx\n"
443 " movnti %%eax, 0(%3)\n"
444 " movnti %%edx, 4(%3)\n"
445 "3: movl 8(%4), %%eax\n"
446 "31: movl 12(%4),%%edx\n"
447 " movnti %%eax, 8(%3)\n"
448 " movnti %%edx, 12(%3)\n"
449 "4: movl 16(%4), %%eax\n"
450 "41: movl 20(%4), %%edx\n"
451 " movnti %%eax, 16(%3)\n"
452 " movnti %%edx, 20(%3)\n"
453 "10: movl 24(%4), %%eax\n"
454 "51: movl 28(%4), %%edx\n"
455 " movnti %%eax, 24(%3)\n"
456 " movnti %%edx, 28(%3)\n"
457 "11: movl 32(%4), %%eax\n"
458 "61: movl 36(%4), %%edx\n"
459 " movnti %%eax, 32(%3)\n"
460 " movnti %%edx, 36(%3)\n"
461 "12: movl 40(%4), %%eax\n"
462 "71: movl 44(%4), %%edx\n"
463 " movnti %%eax, 40(%3)\n"
464 " movnti %%edx, 44(%3)\n"
465 "13: movl 48(%4), %%eax\n"
466 "81: movl 52(%4), %%edx\n"
467 " movnti %%eax, 48(%3)\n"
468 " movnti %%edx, 52(%3)\n"
469 "14: movl 56(%4), %%eax\n"
470 "91: movl 60(%4), %%edx\n"
471 " movnti %%eax, 56(%3)\n"
472 " movnti %%edx, 60(%3)\n"
479 "5: movl %0, %%eax\n"
487 ".section .fixup,\"ax\"\n"
488 "9: lea 0(%%eax,%0,4),%0\n"
491 " xorl %%eax,%%eax\n"
497 ".section __ex_table,\"a\"\n"
520 : "=&c"(size
), "=&D" (d0
), "=&S" (d1
)
521 : "1"(to
), "2"(from
), "0"(size
)
522 : "eax", "edx", "memory");
526 static unsigned long __copy_user_intel_nocache(void *to
,
527 const void __user
*from
, unsigned long size
)
531 __asm__
__volatile__(
533 "0: movl 32(%4), %%eax\n"
536 "1: movl 64(%4), %%eax\n"
538 "2: movl 0(%4), %%eax\n"
539 "21: movl 4(%4), %%edx\n"
540 " movnti %%eax, 0(%3)\n"
541 " movnti %%edx, 4(%3)\n"
542 "3: movl 8(%4), %%eax\n"
543 "31: movl 12(%4),%%edx\n"
544 " movnti %%eax, 8(%3)\n"
545 " movnti %%edx, 12(%3)\n"
546 "4: movl 16(%4), %%eax\n"
547 "41: movl 20(%4), %%edx\n"
548 " movnti %%eax, 16(%3)\n"
549 " movnti %%edx, 20(%3)\n"
550 "10: movl 24(%4), %%eax\n"
551 "51: movl 28(%4), %%edx\n"
552 " movnti %%eax, 24(%3)\n"
553 " movnti %%edx, 28(%3)\n"
554 "11: movl 32(%4), %%eax\n"
555 "61: movl 36(%4), %%edx\n"
556 " movnti %%eax, 32(%3)\n"
557 " movnti %%edx, 36(%3)\n"
558 "12: movl 40(%4), %%eax\n"
559 "71: movl 44(%4), %%edx\n"
560 " movnti %%eax, 40(%3)\n"
561 " movnti %%edx, 44(%3)\n"
562 "13: movl 48(%4), %%eax\n"
563 "81: movl 52(%4), %%edx\n"
564 " movnti %%eax, 48(%3)\n"
565 " movnti %%edx, 52(%3)\n"
566 "14: movl 56(%4), %%eax\n"
567 "91: movl 60(%4), %%edx\n"
568 " movnti %%eax, 56(%3)\n"
569 " movnti %%edx, 60(%3)\n"
576 "5: movl %0, %%eax\n"
584 ".section .fixup,\"ax\"\n"
585 "9: lea 0(%%eax,%0,4),%0\n"
588 ".section __ex_table,\"a\"\n"
611 : "=&c"(size
), "=&D" (d0
), "=&S" (d1
)
612 : "1"(to
), "2"(from
), "0"(size
)
613 : "eax", "edx", "memory");
620 * Leave these declared but undefined. They should not be any references to
623 unsigned long __copy_user_zeroing_intel(void *to
, const void __user
*from
,
625 unsigned long __copy_user_intel(void __user
*to
, const void *from
,
627 unsigned long __copy_user_zeroing_intel_nocache(void *to
,
628 const void __user
*from
, unsigned long size
);
629 #endif /* CONFIG_X86_INTEL_USERCOPY */
631 /* Generic arbitrary sized copy. */
632 #define __copy_user(to,from,size) \
634 int __d0, __d1, __d2; \
635 __asm__ __volatile__( \
651 ".section .fixup,\"ax\"\n" \
654 "3: lea 0(%3,%0,4),%0\n" \
657 ".section __ex_table,\"a\"\n" \
663 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
664 : "3"(size), "0"(size), "1"(to), "2"(from) \
668 #define __copy_user_zeroing(to,from,size) \
670 int __d0, __d1, __d2; \
671 __asm__ __volatile__( \
687 ".section .fixup,\"ax\"\n" \
690 "3: lea 0(%3,%0,4),%0\n" \
693 " xorl %%eax,%%eax\n" \
699 ".section __ex_table,\"a\"\n" \
705 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
706 : "3"(size), "0"(size), "1"(to), "2"(from) \
710 unsigned long __copy_to_user_ll(void __user
*to
, const void *from
,
713 #ifndef CONFIG_X86_WP_WORKS_OK
714 if (unlikely(boot_cpu_data
.wp_works_ok
== 0) &&
715 ((unsigned long )to
) < TASK_SIZE
) {
717 * When we are in an atomic section (see
718 * mm/filemap.c:file_read_actor), return the full
719 * length to take the slow path.
725 * CPU does not honor the WP bit when writing
726 * from supervisory mode, and due to preemption or SMP,
727 * the page tables can change at any time.
728 * Do it manually. Manfred <manfred@colorfullife.com>
731 unsigned long offset
= ((unsigned long)to
)%PAGE_SIZE
;
732 unsigned long len
= PAGE_SIZE
- offset
;
741 down_read(¤t
->mm
->mmap_sem
);
742 retval
= get_user_pages(current
, current
->mm
,
743 (unsigned long )to
, 1, 1, 0, &pg
, NULL
);
745 if (retval
== -ENOMEM
&& is_global_init(current
)) {
746 up_read(¤t
->mm
->mmap_sem
);
747 congestion_wait(WRITE
, HZ
/50);
752 up_read(¤t
->mm
->mmap_sem
);
756 maddr
= kmap_atomic(pg
, KM_USER0
);
757 memcpy(maddr
+ offset
, from
, len
);
758 kunmap_atomic(maddr
, KM_USER0
);
759 set_page_dirty_lock(pg
);
761 up_read(¤t
->mm
->mmap_sem
);
770 if (movsl_is_ok(to
, from
, n
))
771 __copy_user(to
, from
, n
);
773 n
= __copy_user_intel(to
, from
, n
);
776 EXPORT_SYMBOL(__copy_to_user_ll
);
778 unsigned long __copy_from_user_ll(void *to
, const void __user
*from
,
781 if (movsl_is_ok(to
, from
, n
))
782 __copy_user_zeroing(to
, from
, n
);
784 n
= __copy_user_zeroing_intel(to
, from
, n
);
787 EXPORT_SYMBOL(__copy_from_user_ll
);
789 unsigned long __copy_from_user_ll_nozero(void *to
, const void __user
*from
,
792 if (movsl_is_ok(to
, from
, n
))
793 __copy_user(to
, from
, n
);
795 n
= __copy_user_intel((void __user
*)to
,
796 (const void *)from
, n
);
799 EXPORT_SYMBOL(__copy_from_user_ll_nozero
);
801 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
);
814 EXPORT_SYMBOL(__copy_from_user_ll_nocache
);
816 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
);
829 EXPORT_SYMBOL(__copy_from_user_ll_nocache_nozero
);
832 * copy_to_user: - Copy a block of data into user space.
833 * @to: Destination address, in user space.
834 * @from: Source address, in kernel space.
835 * @n: Number of bytes to copy.
837 * Context: User context only. This function may sleep.
839 * Copy data from kernel space to user space.
841 * Returns number of bytes that could not be copied.
842 * On success, this will be zero.
845 copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
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 if (access_ok(VERIFY_READ
, from
, n
))
873 n
= __copy_from_user(to
, from
, n
);
878 EXPORT_SYMBOL(copy_from_user
);