usercopy_64.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * User address space access functions.
  3. *
  4. * Copyright 1997 Andi Kleen <ak@muc.de>
  5. * Copyright 1997 Linus Torvalds
  6. * Copyright 2002 Andi Kleen <ak@suse.de>
  7. */
  8. #include <linux/module.h>
  9. #include <asm/uaccess.h>
  10. /*
  11. * Zero Userspace
  12. */
  13. unsigned long __clear_user(void __user *addr, unsigned long size)
  14. {
  15. long __d0;
  16. might_fault();
  17. /* no memory constraint because it doesn't change any memory gcc knows
  18. about */
  19. stac();
  20. asm volatile(
  21. " testq %[size8],%[size8]\n"
  22. " jz 4f\n"
  23. "0: movq %[zero],(%[dst])\n"
  24. " addq %[eight],%[dst]\n"
  25. " decl %%ecx ; jnz 0b\n"
  26. "4: movq %[size1],%%rcx\n"
  27. " testl %%ecx,%%ecx\n"
  28. " jz 2f\n"
  29. "1: movb %b[zero],(%[dst])\n"
  30. " incq %[dst]\n"
  31. " decl %%ecx ; jnz 1b\n"
  32. "2:\n"
  33. ".section .fixup,\"ax\"\n"
  34. "3: lea 0(%[size1],%[size8],8),%[size8]\n"
  35. " jmp 2b\n"
  36. ".previous\n"
  37. _ASM_EXTABLE(0b,3b)
  38. _ASM_EXTABLE(1b,2b)
  39. : [size8] "=&c"(size), [dst] "=&D" (__d0)
  40. : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
  41. [zero] "r" (0UL), [eight] "r" (8UL));
  42. clac();
  43. return size;
  44. }
  45. EXPORT_SYMBOL(__clear_user);
  46. unsigned long clear_user(void __user *to, unsigned long n)
  47. {
  48. if (access_ok(VERIFY_WRITE, to, n))
  49. return __clear_user(to, n);
  50. return n;
  51. }
  52. EXPORT_SYMBOL(clear_user);
  53. unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
  54. {
  55. if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
  56. return copy_user_generic((__force void *)to, (__force void *)from, len);
  57. }
  58. return len;
  59. }
  60. EXPORT_SYMBOL(copy_in_user);
  61. /*
  62. * Try to copy last bytes and clear the rest if needed.
  63. * Since protection fault in copy_from/to_user is not a normal situation,
  64. * it is not necessary to optimize tail handling.
  65. */
  66. __visible unsigned long
  67. copy_user_handle_tail(char *to, char *from, unsigned len)
  68. {
  69. for (; len; --len, to++) {
  70. char c;
  71. if (__get_user_nocheck(c, from++, sizeof(char)))
  72. break;
  73. if (__put_user_nocheck(c, to, sizeof(char)))
  74. break;
  75. }
  76. clac();
  77. /* If the destination is a kernel buffer, we always clear the end */
  78. if (!__addr_ok(to))
  79. memset(to, 0, len);
  80. return len;
  81. }