usercopy_64.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /*
  52. * Return the size of a string (including the ending 0)
  53. *
  54. * Return 0 on exception, a value greater than N if too long
  55. */
  56. long __strnlen_user(const char __user *s, long n)
  57. {
  58. long res = 0;
  59. char c;
  60. while (1) {
  61. if (res>n)
  62. return n+1;
  63. if (__get_user(c, s))
  64. return 0;
  65. if (!c)
  66. return res+1;
  67. res++;
  68. s++;
  69. }
  70. }
  71. EXPORT_SYMBOL(__strnlen_user);
  72. long strnlen_user(const char __user *s, long n)
  73. {
  74. if (!access_ok(VERIFY_READ, s, 1))
  75. return 0;
  76. return __strnlen_user(s, n);
  77. }
  78. EXPORT_SYMBOL(strnlen_user);
  79. long strlen_user(const char __user *s)
  80. {
  81. long res = 0;
  82. char c;
  83. for (;;) {
  84. if (get_user(c, s))
  85. return 0;
  86. if (!c)
  87. return res+1;
  88. res++;
  89. s++;
  90. }
  91. }
  92. EXPORT_SYMBOL(strlen_user);
  93. unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
  94. {
  95. if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
  96. return copy_user_generic((__force void *)to, (__force void *)from, len);
  97. }
  98. return len;
  99. }
  100. EXPORT_SYMBOL(copy_in_user);
  101. /*
  102. * Try to copy last bytes and clear the rest if needed.
  103. * Since protection fault in copy_from/to_user is not a normal situation,
  104. * it is not necessary to optimize tail handling.
  105. */
  106. unsigned long
  107. copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest)
  108. {
  109. char c;
  110. unsigned zero_len;
  111. for (; len; --len, to++) {
  112. if (__get_user_nocheck(c, from++, sizeof(char)))
  113. break;
  114. if (__put_user_nocheck(c, to, sizeof(char)))
  115. break;
  116. }
  117. for (c = 0, zero_len = len; zerorest && zero_len; --zero_len)
  118. if (__put_user_nocheck(c, to++, sizeof(char)))
  119. break;
  120. return len;
  121. }