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>
18 #ifdef CONFIG_X86_INTEL_USERCOPY
20 * Alignment at which movsl is preferred for bulk memory copies.
22 struct movsl_mask movsl_mask __read_mostly
;
25 static inline int __movsl_is_ok(unsigned long a1
, unsigned long a2
, unsigned long n
)
27 #ifdef CONFIG_X86_INTEL_USERCOPY
28 if (n
>= 64 && ((a1
^ a2
) & movsl_mask
.mask
))
33 #define movsl_is_ok(a1, a2, n) \
34 __movsl_is_ok((unsigned long)(a1), (unsigned long)(a2), (n))
40 #define __do_clear_user(addr,size) \
44 __asm__ __volatile__( \
49 ".section .fixup,\"ax\"\n" \
50 "3: lea 0(%2,%0,4),%0\n" \
55 : "=&c"(size), "=&D" (__d0) \
56 : "r"(size & 3), "0"(size / 4), "1"(addr), "a"(0)); \
60 * clear_user: - Zero a block of memory in user space.
61 * @to: Destination address, in user space.
62 * @n: Number of bytes to zero.
64 * Zero a block of memory in user space.
66 * Returns number of bytes that could not be cleared.
67 * On success, this will be zero.
70 clear_user(void __user
*to
, unsigned long n
)
73 if (access_ok(VERIFY_WRITE
, to
, n
))
74 __do_clear_user(to
, n
);
77 EXPORT_SYMBOL(clear_user
);
80 * __clear_user: - Zero a block of memory in user space, with less checking.
81 * @to: Destination address, in user space.
82 * @n: Number of bytes to zero.
84 * Zero a block of memory in user space. Caller must check
85 * the specified block with access_ok() before calling this function.
87 * Returns number of bytes that could not be cleared.
88 * On success, this will be zero.
91 __clear_user(void __user
*to
, unsigned long n
)
93 __do_clear_user(to
, n
);
96 EXPORT_SYMBOL(__clear_user
);
98 #ifdef CONFIG_X86_INTEL_USERCOPY
100 __copy_user_intel(void __user
*to
, const void *from
, unsigned long size
)
103 __asm__
__volatile__(
105 "1: movl 32(%4), %%eax\n"
108 "2: movl 64(%4), %%eax\n"
110 "3: movl 0(%4), %%eax\n"
111 "4: movl 4(%4), %%edx\n"
112 "5: movl %%eax, 0(%3)\n"
113 "6: movl %%edx, 4(%3)\n"
114 "7: movl 8(%4), %%eax\n"
115 "8: movl 12(%4),%%edx\n"
116 "9: movl %%eax, 8(%3)\n"
117 "10: movl %%edx, 12(%3)\n"
118 "11: movl 16(%4), %%eax\n"
119 "12: movl 20(%4), %%edx\n"
120 "13: movl %%eax, 16(%3)\n"
121 "14: movl %%edx, 20(%3)\n"
122 "15: movl 24(%4), %%eax\n"
123 "16: movl 28(%4), %%edx\n"
124 "17: movl %%eax, 24(%3)\n"
125 "18: movl %%edx, 28(%3)\n"
126 "19: movl 32(%4), %%eax\n"
127 "20: movl 36(%4), %%edx\n"
128 "21: movl %%eax, 32(%3)\n"
129 "22: movl %%edx, 36(%3)\n"
130 "23: movl 40(%4), %%eax\n"
131 "24: movl 44(%4), %%edx\n"
132 "25: movl %%eax, 40(%3)\n"
133 "26: movl %%edx, 44(%3)\n"
134 "27: movl 48(%4), %%eax\n"
135 "28: movl 52(%4), %%edx\n"
136 "29: movl %%eax, 48(%3)\n"
137 "30: movl %%edx, 52(%3)\n"
138 "31: movl 56(%4), %%eax\n"
139 "32: movl 60(%4), %%edx\n"
140 "33: movl %%eax, 56(%3)\n"
141 "34: movl %%edx, 60(%3)\n"
147 "35: movl %0, %%eax\n"
152 "36: movl %%eax, %0\n"
155 ".section .fixup,\"ax\"\n"
156 "101: lea 0(%%eax,%0,4),%0\n"
159 _ASM_EXTABLE(1b
,100b
)
160 _ASM_EXTABLE(2b
,100b
)
161 _ASM_EXTABLE(3b
,100b
)
162 _ASM_EXTABLE(4b
,100b
)
163 _ASM_EXTABLE(5b
,100b
)
164 _ASM_EXTABLE(6b
,100b
)
165 _ASM_EXTABLE(7b
,100b
)
166 _ASM_EXTABLE(8b
,100b
)
167 _ASM_EXTABLE(9b
,100b
)
168 _ASM_EXTABLE(10b
,100b
)
169 _ASM_EXTABLE(11b
,100b
)
170 _ASM_EXTABLE(12b
,100b
)
171 _ASM_EXTABLE(13b
,100b
)
172 _ASM_EXTABLE(14b
,100b
)
173 _ASM_EXTABLE(15b
,100b
)
174 _ASM_EXTABLE(16b
,100b
)
175 _ASM_EXTABLE(17b
,100b
)
176 _ASM_EXTABLE(18b
,100b
)
177 _ASM_EXTABLE(19b
,100b
)
178 _ASM_EXTABLE(20b
,100b
)
179 _ASM_EXTABLE(21b
,100b
)
180 _ASM_EXTABLE(22b
,100b
)
181 _ASM_EXTABLE(23b
,100b
)
182 _ASM_EXTABLE(24b
,100b
)
183 _ASM_EXTABLE(25b
,100b
)
184 _ASM_EXTABLE(26b
,100b
)
185 _ASM_EXTABLE(27b
,100b
)
186 _ASM_EXTABLE(28b
,100b
)
187 _ASM_EXTABLE(29b
,100b
)
188 _ASM_EXTABLE(30b
,100b
)
189 _ASM_EXTABLE(31b
,100b
)
190 _ASM_EXTABLE(32b
,100b
)
191 _ASM_EXTABLE(33b
,100b
)
192 _ASM_EXTABLE(34b
,100b
)
193 _ASM_EXTABLE(35b
,100b
)
194 _ASM_EXTABLE(36b
,100b
)
195 _ASM_EXTABLE(37b
,100b
)
196 _ASM_EXTABLE(99b
,101b
)
197 : "=&c"(size
), "=&D" (d0
), "=&S" (d1
)
198 : "1"(to
), "2"(from
), "0"(size
)
199 : "eax", "edx", "memory");
204 __copy_user_zeroing_intel(void *to
, const void __user
*from
, unsigned long size
)
207 __asm__
__volatile__(
209 "0: movl 32(%4), %%eax\n"
212 "1: movl 64(%4), %%eax\n"
214 "2: movl 0(%4), %%eax\n"
215 "21: movl 4(%4), %%edx\n"
216 " movl %%eax, 0(%3)\n"
217 " movl %%edx, 4(%3)\n"
218 "3: movl 8(%4), %%eax\n"
219 "31: movl 12(%4),%%edx\n"
220 " movl %%eax, 8(%3)\n"
221 " movl %%edx, 12(%3)\n"
222 "4: movl 16(%4), %%eax\n"
223 "41: movl 20(%4), %%edx\n"
224 " movl %%eax, 16(%3)\n"
225 " movl %%edx, 20(%3)\n"
226 "10: movl 24(%4), %%eax\n"
227 "51: movl 28(%4), %%edx\n"
228 " movl %%eax, 24(%3)\n"
229 " movl %%edx, 28(%3)\n"
230 "11: movl 32(%4), %%eax\n"
231 "61: movl 36(%4), %%edx\n"
232 " movl %%eax, 32(%3)\n"
233 " movl %%edx, 36(%3)\n"
234 "12: movl 40(%4), %%eax\n"
235 "71: movl 44(%4), %%edx\n"
236 " movl %%eax, 40(%3)\n"
237 " movl %%edx, 44(%3)\n"
238 "13: movl 48(%4), %%eax\n"
239 "81: movl 52(%4), %%edx\n"
240 " movl %%eax, 48(%3)\n"
241 " movl %%edx, 52(%3)\n"
242 "14: movl 56(%4), %%eax\n"
243 "91: movl 60(%4), %%edx\n"
244 " movl %%eax, 56(%3)\n"
245 " movl %%edx, 60(%3)\n"
251 "5: movl %0, %%eax\n"
259 ".section .fixup,\"ax\"\n"
260 "9: lea 0(%%eax,%0,4),%0\n"
263 " xorl %%eax,%%eax\n"
272 _ASM_EXTABLE(21b
,16b
)
274 _ASM_EXTABLE(31b
,16b
)
276 _ASM_EXTABLE(41b
,16b
)
277 _ASM_EXTABLE(10b
,16b
)
278 _ASM_EXTABLE(51b
,16b
)
279 _ASM_EXTABLE(11b
,16b
)
280 _ASM_EXTABLE(61b
,16b
)
281 _ASM_EXTABLE(12b
,16b
)
282 _ASM_EXTABLE(71b
,16b
)
283 _ASM_EXTABLE(13b
,16b
)
284 _ASM_EXTABLE(81b
,16b
)
285 _ASM_EXTABLE(14b
,16b
)
286 _ASM_EXTABLE(91b
,16b
)
289 : "=&c"(size
), "=&D" (d0
), "=&S" (d1
)
290 : "1"(to
), "2"(from
), "0"(size
)
291 : "eax", "edx", "memory");
296 * Non Temporal Hint version of __copy_user_zeroing_intel. It is cache aware.
297 * hyoshiok@miraclelinux.com
300 static unsigned long __copy_user_zeroing_intel_nocache(void *to
,
301 const void __user
*from
, unsigned long size
)
305 __asm__
__volatile__(
307 "0: movl 32(%4), %%eax\n"
310 "1: movl 64(%4), %%eax\n"
312 "2: movl 0(%4), %%eax\n"
313 "21: movl 4(%4), %%edx\n"
314 " movnti %%eax, 0(%3)\n"
315 " movnti %%edx, 4(%3)\n"
316 "3: movl 8(%4), %%eax\n"
317 "31: movl 12(%4),%%edx\n"
318 " movnti %%eax, 8(%3)\n"
319 " movnti %%edx, 12(%3)\n"
320 "4: movl 16(%4), %%eax\n"
321 "41: movl 20(%4), %%edx\n"
322 " movnti %%eax, 16(%3)\n"
323 " movnti %%edx, 20(%3)\n"
324 "10: movl 24(%4), %%eax\n"
325 "51: movl 28(%4), %%edx\n"
326 " movnti %%eax, 24(%3)\n"
327 " movnti %%edx, 28(%3)\n"
328 "11: movl 32(%4), %%eax\n"
329 "61: movl 36(%4), %%edx\n"
330 " movnti %%eax, 32(%3)\n"
331 " movnti %%edx, 36(%3)\n"
332 "12: movl 40(%4), %%eax\n"
333 "71: movl 44(%4), %%edx\n"
334 " movnti %%eax, 40(%3)\n"
335 " movnti %%edx, 44(%3)\n"
336 "13: movl 48(%4), %%eax\n"
337 "81: movl 52(%4), %%edx\n"
338 " movnti %%eax, 48(%3)\n"
339 " movnti %%edx, 52(%3)\n"
340 "14: movl 56(%4), %%eax\n"
341 "91: movl 60(%4), %%edx\n"
342 " movnti %%eax, 56(%3)\n"
343 " movnti %%edx, 60(%3)\n"
350 "5: movl %0, %%eax\n"
358 ".section .fixup,\"ax\"\n"
359 "9: lea 0(%%eax,%0,4),%0\n"
362 " xorl %%eax,%%eax\n"
371 _ASM_EXTABLE(21b
,16b
)
373 _ASM_EXTABLE(31b
,16b
)
375 _ASM_EXTABLE(41b
,16b
)
376 _ASM_EXTABLE(10b
,16b
)
377 _ASM_EXTABLE(51b
,16b
)
378 _ASM_EXTABLE(11b
,16b
)
379 _ASM_EXTABLE(61b
,16b
)
380 _ASM_EXTABLE(12b
,16b
)
381 _ASM_EXTABLE(71b
,16b
)
382 _ASM_EXTABLE(13b
,16b
)
383 _ASM_EXTABLE(81b
,16b
)
384 _ASM_EXTABLE(14b
,16b
)
385 _ASM_EXTABLE(91b
,16b
)
388 : "=&c"(size
), "=&D" (d0
), "=&S" (d1
)
389 : "1"(to
), "2"(from
), "0"(size
)
390 : "eax", "edx", "memory");
394 static unsigned long __copy_user_intel_nocache(void *to
,
395 const void __user
*from
, unsigned long size
)
399 __asm__
__volatile__(
401 "0: movl 32(%4), %%eax\n"
404 "1: movl 64(%4), %%eax\n"
406 "2: movl 0(%4), %%eax\n"
407 "21: movl 4(%4), %%edx\n"
408 " movnti %%eax, 0(%3)\n"
409 " movnti %%edx, 4(%3)\n"
410 "3: movl 8(%4), %%eax\n"
411 "31: movl 12(%4),%%edx\n"
412 " movnti %%eax, 8(%3)\n"
413 " movnti %%edx, 12(%3)\n"
414 "4: movl 16(%4), %%eax\n"
415 "41: movl 20(%4), %%edx\n"
416 " movnti %%eax, 16(%3)\n"
417 " movnti %%edx, 20(%3)\n"
418 "10: movl 24(%4), %%eax\n"
419 "51: movl 28(%4), %%edx\n"
420 " movnti %%eax, 24(%3)\n"
421 " movnti %%edx, 28(%3)\n"
422 "11: movl 32(%4), %%eax\n"
423 "61: movl 36(%4), %%edx\n"
424 " movnti %%eax, 32(%3)\n"
425 " movnti %%edx, 36(%3)\n"
426 "12: movl 40(%4), %%eax\n"
427 "71: movl 44(%4), %%edx\n"
428 " movnti %%eax, 40(%3)\n"
429 " movnti %%edx, 44(%3)\n"
430 "13: movl 48(%4), %%eax\n"
431 "81: movl 52(%4), %%edx\n"
432 " movnti %%eax, 48(%3)\n"
433 " movnti %%edx, 52(%3)\n"
434 "14: movl 56(%4), %%eax\n"
435 "91: movl 60(%4), %%edx\n"
436 " movnti %%eax, 56(%3)\n"
437 " movnti %%edx, 60(%3)\n"
444 "5: movl %0, %%eax\n"
452 ".section .fixup,\"ax\"\n"
453 "9: lea 0(%%eax,%0,4),%0\n"
459 _ASM_EXTABLE(21b
,16b
)
461 _ASM_EXTABLE(31b
,16b
)
463 _ASM_EXTABLE(41b
,16b
)
464 _ASM_EXTABLE(10b
,16b
)
465 _ASM_EXTABLE(51b
,16b
)
466 _ASM_EXTABLE(11b
,16b
)
467 _ASM_EXTABLE(61b
,16b
)
468 _ASM_EXTABLE(12b
,16b
)
469 _ASM_EXTABLE(71b
,16b
)
470 _ASM_EXTABLE(13b
,16b
)
471 _ASM_EXTABLE(81b
,16b
)
472 _ASM_EXTABLE(14b
,16b
)
473 _ASM_EXTABLE(91b
,16b
)
476 : "=&c"(size
), "=&D" (d0
), "=&S" (d1
)
477 : "1"(to
), "2"(from
), "0"(size
)
478 : "eax", "edx", "memory");
485 * Leave these declared but undefined. They should not be any references to
488 unsigned long __copy_user_zeroing_intel(void *to
, const void __user
*from
,
490 unsigned long __copy_user_intel(void __user
*to
, const void *from
,
492 unsigned long __copy_user_zeroing_intel_nocache(void *to
,
493 const void __user
*from
, unsigned long size
);
494 #endif /* CONFIG_X86_INTEL_USERCOPY */
496 /* Generic arbitrary sized copy. */
497 #define __copy_user(to, from, size) \
499 int __d0, __d1, __d2; \
500 __asm__ __volatile__( \
516 ".section .fixup,\"ax\"\n" \
519 "3: lea 0(%3,%0,4),%0\n" \
522 _ASM_EXTABLE(4b,5b) \
523 _ASM_EXTABLE(0b,3b) \
524 _ASM_EXTABLE(1b,2b) \
525 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
526 : "3"(size), "0"(size), "1"(to), "2"(from) \
530 #define __copy_user_zeroing(to, from, size) \
532 int __d0, __d1, __d2; \
533 __asm__ __volatile__( \
549 ".section .fixup,\"ax\"\n" \
552 "3: lea 0(%3,%0,4),%0\n" \
555 " xorl %%eax,%%eax\n" \
561 _ASM_EXTABLE(4b,5b) \
562 _ASM_EXTABLE(0b,3b) \
563 _ASM_EXTABLE(1b,6b) \
564 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
565 : "3"(size), "0"(size), "1"(to), "2"(from) \
569 unsigned long __copy_to_user_ll(void __user
*to
, const void *from
,
572 #ifndef CONFIG_X86_WP_WORKS_OK
573 if (unlikely(boot_cpu_data
.wp_works_ok
== 0) &&
574 ((unsigned long)to
) < TASK_SIZE
) {
576 * When we are in an atomic section (see
577 * mm/filemap.c:file_read_actor), return the full
578 * length to take the slow path.
584 * CPU does not honor the WP bit when writing
585 * from supervisory mode, and due to preemption or SMP,
586 * the page tables can change at any time.
587 * Do it manually. Manfred <manfred@colorfullife.com>
590 unsigned long offset
= ((unsigned long)to
)%PAGE_SIZE
;
591 unsigned long len
= PAGE_SIZE
- offset
;
600 down_read(¤t
->mm
->mmap_sem
);
601 retval
= get_user_pages(current
, current
->mm
,
602 (unsigned long)to
, 1, 1, 0, &pg
, NULL
);
604 if (retval
== -ENOMEM
&& is_global_init(current
)) {
605 up_read(¤t
->mm
->mmap_sem
);
606 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
611 up_read(¤t
->mm
->mmap_sem
);
615 maddr
= kmap_atomic(pg
);
616 memcpy(maddr
+ offset
, from
, len
);
617 kunmap_atomic(maddr
);
618 set_page_dirty_lock(pg
);
620 up_read(¤t
->mm
->mmap_sem
);
629 if (movsl_is_ok(to
, from
, n
))
630 __copy_user(to
, from
, n
);
632 n
= __copy_user_intel(to
, from
, n
);
635 EXPORT_SYMBOL(__copy_to_user_ll
);
637 unsigned long __copy_from_user_ll(void *to
, const void __user
*from
,
640 if (movsl_is_ok(to
, from
, n
))
641 __copy_user_zeroing(to
, from
, n
);
643 n
= __copy_user_zeroing_intel(to
, from
, n
);
646 EXPORT_SYMBOL(__copy_from_user_ll
);
648 unsigned long __copy_from_user_ll_nozero(void *to
, const void __user
*from
,
651 if (movsl_is_ok(to
, from
, n
))
652 __copy_user(to
, from
, n
);
654 n
= __copy_user_intel((void __user
*)to
,
655 (const void *)from
, n
);
658 EXPORT_SYMBOL(__copy_from_user_ll_nozero
);
660 unsigned long __copy_from_user_ll_nocache(void *to
, const void __user
*from
,
663 #ifdef CONFIG_X86_INTEL_USERCOPY
664 if (n
> 64 && cpu_has_xmm2
)
665 n
= __copy_user_zeroing_intel_nocache(to
, from
, n
);
667 __copy_user_zeroing(to
, from
, n
);
669 __copy_user_zeroing(to
, from
, n
);
673 EXPORT_SYMBOL(__copy_from_user_ll_nocache
);
675 unsigned long __copy_from_user_ll_nocache_nozero(void *to
, const void __user
*from
,
678 #ifdef CONFIG_X86_INTEL_USERCOPY
679 if (n
> 64 && cpu_has_xmm2
)
680 n
= __copy_user_intel_nocache(to
, from
, n
);
682 __copy_user(to
, from
, n
);
684 __copy_user(to
, from
, n
);
688 EXPORT_SYMBOL(__copy_from_user_ll_nocache_nozero
);
691 * copy_to_user: - Copy a block of data into user space.
692 * @to: Destination address, in user space.
693 * @from: Source address, in kernel space.
694 * @n: Number of bytes to copy.
696 * Context: User context only. This function may sleep.
698 * Copy data from kernel space to user space.
700 * Returns number of bytes that could not be copied.
701 * On success, this will be zero.
704 copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
706 if (access_ok(VERIFY_WRITE
, to
, n
))
707 n
= __copy_to_user(to
, from
, n
);
710 EXPORT_SYMBOL(copy_to_user
);
713 * copy_from_user: - Copy a block of data from user space.
714 * @to: Destination address, in kernel space.
715 * @from: Source address, in user space.
716 * @n: Number of bytes to copy.
718 * Context: User context only. This function may sleep.
720 * Copy data from user space to kernel space.
722 * Returns number of bytes that could not be copied.
723 * On success, this will be zero.
725 * If some data could not be copied, this function will pad the copied
726 * data to the requested size using zero bytes.
729 _copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
731 if (access_ok(VERIFY_READ
, from
, n
))
732 n
= __copy_from_user(to
, from
, n
);
737 EXPORT_SYMBOL(_copy_from_user
);
739 void copy_from_user_overflow(void)
741 WARN(1, "Buffer overflow detected!\n");
743 EXPORT_SYMBOL(copy_from_user_overflow
);