usercopy_64.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. asm volatile(
  20. " testq %[size8],%[size8]\n"
  21. " jz 4f\n"
  22. "0: movq %[zero],(%[dst])\n"
  23. " addq %[eight],%[dst]\n"
  24. " decl %%ecx ; jnz 0b\n"
  25. "4: movq %[size1],%%rcx\n"
  26. " testl %%ecx,%%ecx\n"
  27. " jz 2f\n"
  28. "1: movb %b[zero],(%[dst])\n"
  29. " incq %[dst]\n"
  30. " decl %%ecx ; jnz 1b\n"
  31. "2:\n"
  32. ".section .fixup,\"ax\"\n"
  33. "3: lea 0(%[size1],%[size8],8),%[size8]\n"
  34. " jmp 2b\n"
  35. ".previous\n"
  36. _ASM_EXTABLE(0b,3b)
  37. _ASM_EXTABLE(1b,2b)
  38. : [size8] "=&c"(size), [dst] "=&D" (__d0)
  39. : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
  40. [zero] "r" (0UL), [eight] "r" (8UL));
  41. return size;
  42. }
  43. EXPORT_SYMBOL(__clear_user);
  44. unsigned long clear_user(void __user *to, unsigned long n)
  45. {
  46. if (access_ok(VERIFY_WRITE, to, n))
  47. return __clear_user(to, n);
  48. return n;
  49. }
  50. EXPORT_SYMBOL(clear_user);
  51. unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
  52. {
  53. if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
  54. return copy_user_generic((__force void *)to, (__force void *)from, len);
  55. }
  56. return len;
  57. }
  58. EXPORT_SYMBOL(copy_in_user);
  59. /*
  60. * Try to copy last bytes and clear the rest if needed.
  61. * Since protection fault in copy_from/to_user is not a normal situation,
  62. * it is not necessary to optimize tail handling.
  63. */
  64. unsigned long
  65. copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest)
  66. {
  67. char c;
  68. unsigned zero_len;
  69. for (; len; --len) {
  70. if (__get_user_nocheck(c, from++, sizeof(char)))
  71. break;
  72. if (__put_user_nocheck(c, to++, sizeof(char)))
  73. break;
  74. }
  75. for (c = 0, zero_len = len; zerorest && zero_len; --zero_len)
  76. if (__put_user_nocheck(c, to++, sizeof(char)))
  77. break;
  78. return len;
  79. }