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
8 #include <linux/config.h>
10 #include <linux/highmem.h>
11 #include <linux/blkdev.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; \
33 __asm__ __volatile__( \
38 " testb %%al,%%al\n" \
44 ".section .fixup,\"ax\"\n" \
48 ".section __ex_table,\"a\"\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 __direct_strncpy_from_user(char *dst, const char __user *src, long count)
82 __do_strncpy_from_user(dst, src, count, res);
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.
93 * Copies a NUL-terminated string from userspace to kernel space.
95 * On success, returns the length of the string (not including the trailing
98 * If access to userspace fails, returns -EFAULT (some data may have been
101 * If @count is smaller than the length of the string, copies @count bytes
102 * and returns @count.
105 direct_strncpy_from_user(char *dst, const char __user *src, long count)
108 if (access_ok(VERIFY_READ, src, 1))
109 __do_strncpy_from_user(dst, src, count, res);
118 #define __do_clear_user(addr,size) \
121 __asm__ __volatile__( \
126 ".section .fixup,\"ax\"\n" \
127 "3: lea 0(%2,%0,4),%0\n" \
130 ".section __ex_table,\"a\"\n" \
135 : "=&c"(size), "=&D" (__d0) \
136 : "r"(size & 3), "0"(size / 4), "1"(addr), "a"(0)); \
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.
144 * Zero a block of memory in user space.
146 * Returns number of bytes that could not be cleared.
147 * On success, this will be zero.
150 direct_clear_user(void __user *to, unsigned long n)
153 if (access_ok(VERIFY_WRITE, to, n))
154 __do_clear_user(to, n);
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 __direct_clear_user(void __user *to, unsigned long n)
172 __do_clear_user(to, n);
177 * strlen_user: - Get the size of a string in user space.
178 * @s: The string to measure.
179 * @n: The maximum valid length
181 * Get the size of a NUL-terminated string in user space.
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.
187 long direct_strnlen_user(const char __user *s, long n)
189 unsigned long mask = -__addr_ok(s);
190 unsigned long res, tmp;
194 __asm__ __volatile__(
203 ".section .fixup,\"ax\"\n"
204 "2: xorl %%eax,%%eax\n"
209 ".section __ex_table,\"a\"\n"
213 :"=r" (n), "=D" (s), "=a" (res), "=c" (tmp)
214 :"0" (n), "1" (s), "2" (0), "3" (mask)
219 #ifdef CONFIG_X86_INTEL_USERCOPY
221 __copy_user_intel(void *to, const void *from,unsigned long size)
224 __asm__ __volatile__(
226 "1: movl 32(%4), %%eax\n"
229 "2: movl 64(%4), %%eax\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"
268 "35: movl %0, %%eax\n"
273 "36: movl %%eax, %0\n"
276 ".section .fixup,\"ax\"\n"
277 "101: lea 0(%%eax,%0,4),%0\n"
280 ".section __ex_table,\"a\"\n"
321 : "=&c"(size), "=&D" (d0), "=&S" (d1)
322 : "1"(to), "2"(from), "0"(size)
323 : "eax", "edx", "memory");
328 __copy_user_zeroing_intel(void *to, const void *from, unsigned long size)
331 __asm__ __volatile__(
333 "0: movl 32(%4), %%eax\n"
336 "1: movl 64(%4), %%eax\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"
375 "5: movl %0, %%eax\n"
383 ".section .fixup,\"ax\"\n"
384 "9: lea 0(%%eax,%0,4),%0\n"
387 " xorl %%eax,%%eax\n"
393 ".section __ex_table,\"a\"\n"
416 : "=&c"(size), "=&D" (d0), "=&S" (d1)
417 : "1"(to), "2"(from), "0"(size)
418 : "eax", "edx", "memory");
423 * Leave these declared but undefined. They should not be any references to
427 __copy_user_zeroing_intel(void *to, const void *from, unsigned long size);
429 __copy_user_intel(void *to, const void *from,unsigned long size);
430 #endif /* CONFIG_X86_INTEL_USERCOPY */
432 /* Generic arbitrary sized copy. */
433 #define __copy_user(to,from,size) \
435 int __d0, __d1, __d2; \
436 __asm__ __volatile__( \
452 ".section .fixup,\"ax\"\n" \
455 "3: lea 0(%3,%0,4),%0\n" \
458 ".section __ex_table,\"a\"\n" \
464 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
465 : "3"(size), "0"(size), "1"(to), "2"(from) \
469 #define __copy_user_zeroing(to,from,size) \
471 int __d0, __d1, __d2; \
472 __asm__ __volatile__( \
488 ".section .fixup,\"ax\"\n" \
491 "3: lea 0(%3,%0,4),%0\n" \
494 " xorl %%eax,%%eax\n" \
500 ".section __ex_table,\"a\"\n" \
506 : "=&c"(size), "=&D" (__d0), "=&S" (__d1), "=r"(__d2) \
507 : "3"(size), "0"(size), "1"(to), "2"(from) \
512 unsigned long __copy_to_user_ll(void __user *to, const void *from, unsigned long n)
514 #ifndef CONFIG_X86_WP_WORKS_OK
515 if (unlikely(boot_cpu_data.wp_works_ok == 0) &&
516 ((unsigned long )to) < TASK_SIZE) {
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>
524 unsigned long offset = ((unsigned long)to)%PAGE_SIZE;
525 unsigned long len = PAGE_SIZE - offset;
534 down_read(¤t->mm->mmap_sem);
535 retval = get_user_pages(current, current->mm,
536 (unsigned long )to, 1, 1, 0, &pg, NULL);
538 if (retval == -ENOMEM && current->pid == 1) {
539 up_read(¤t->mm->mmap_sem);
540 blk_congestion_wait(WRITE, HZ/50);
545 up_read(¤t->mm->mmap_sem);
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);
554 up_read(¤t->mm->mmap_sem);
563 if (movsl_is_ok(to, from, n))
564 __copy_user((void *)to, from, n);
566 n = __copy_user_intel((void *)to, from, n);
570 unsigned long __copy_from_user_ll(void *to, const void __user *from, unsigned long n)
572 if (movsl_is_ok(to, from, n))
573 __copy_user_zeroing(to, (const void *) from, n);
575 n = __copy_user_zeroing_intel(to, (const void *) from, n);