usercopy_64.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/export.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/highmem.h>
  11. /*
  12. * Zero Userspace
  13. */
  14. unsigned long __clear_user(void __user *addr, unsigned long size)
  15. {
  16. long __d0;
  17. might_fault();
  18. /* no memory constraint because it doesn't change any memory gcc knows
  19. about */
  20. stac();
  21. asm volatile(
  22. " testq %[size8],%[size8]\n"
  23. " jz 4f\n"
  24. " .align 16\n"
  25. "0: movq $0,(%[dst])\n"
  26. " addq $8,%[dst]\n"
  27. " decl %%ecx ; jnz 0b\n"
  28. "4: movq %[size1],%%rcx\n"
  29. " testl %%ecx,%%ecx\n"
  30. " jz 2f\n"
  31. "1: movb $0,(%[dst])\n"
  32. " incq %[dst]\n"
  33. " decl %%ecx ; jnz 1b\n"
  34. "2:\n"
  35. ".section .fixup,\"ax\"\n"
  36. "3: lea 0(%[size1],%[size8],8),%[size8]\n"
  37. " jmp 2b\n"
  38. ".previous\n"
  39. _ASM_EXTABLE(0b,3b)
  40. _ASM_EXTABLE(1b,2b)
  41. : [size8] "=&c"(size), [dst] "=&D" (__d0)
  42. : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr));
  43. clac();
  44. return size;
  45. }
  46. EXPORT_SYMBOL(__clear_user);
  47. unsigned long clear_user(void __user *to, unsigned long n)
  48. {
  49. if (access_ok(VERIFY_WRITE, to, n))
  50. return __clear_user(to, n);
  51. return n;
  52. }
  53. EXPORT_SYMBOL(clear_user);
  54. /*
  55. * Try to copy last bytes and clear the rest if needed.
  56. * Since protection fault in copy_from/to_user is not a normal situation,
  57. * it is not necessary to optimize tail handling.
  58. */
  59. __visible unsigned long
  60. copy_user_handle_tail(char *to, char *from, unsigned len)
  61. {
  62. for (; len; --len, to++) {
  63. char c;
  64. if (__get_user_nocheck(c, from++, sizeof(char)))
  65. break;
  66. if (__put_user_nocheck(c, to, sizeof(char)))
  67. break;
  68. }
  69. clac();
  70. return len;
  71. }
  72. /*
  73. * Similar to copy_user_handle_tail, probe for the write fault point,
  74. * but reuse __memcpy_mcsafe in case a new read error is encountered.
  75. * clac() is handled in _copy_to_iter_mcsafe().
  76. */
  77. __visible unsigned long
  78. mcsafe_handle_tail(char *to, char *from, unsigned len)
  79. {
  80. for (; len; --len, to++, from++) {
  81. /*
  82. * Call the assembly routine back directly since
  83. * memcpy_mcsafe() may silently fallback to memcpy.
  84. */
  85. unsigned long rem = __memcpy_mcsafe(to, from, 1);
  86. if (rem)
  87. break;
  88. }
  89. return len;
  90. }
  91. #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
  92. /**
  93. * clean_cache_range - write back a cache range with CLWB
  94. * @vaddr: virtual start address
  95. * @size: number of bytes to write back
  96. *
  97. * Write back a cache range using the CLWB (cache line write back)
  98. * instruction. Note that @size is internally rounded up to be cache
  99. * line size aligned.
  100. */
  101. static void clean_cache_range(void *addr, size_t size)
  102. {
  103. u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
  104. unsigned long clflush_mask = x86_clflush_size - 1;
  105. void *vend = addr + size;
  106. void *p;
  107. for (p = (void *)((unsigned long)addr & ~clflush_mask);
  108. p < vend; p += x86_clflush_size)
  109. clwb(p);
  110. }
  111. void arch_wb_cache_pmem(void *addr, size_t size)
  112. {
  113. clean_cache_range(addr, size);
  114. }
  115. EXPORT_SYMBOL_GPL(arch_wb_cache_pmem);
  116. long __copy_user_flushcache(void *dst, const void __user *src, unsigned size)
  117. {
  118. unsigned long flushed, dest = (unsigned long) dst;
  119. long rc = __copy_user_nocache(dst, src, size, 0);
  120. /*
  121. * __copy_user_nocache() uses non-temporal stores for the bulk
  122. * of the transfer, but we need to manually flush if the
  123. * transfer is unaligned. A cached memory copy is used when
  124. * destination or size is not naturally aligned. That is:
  125. * - Require 8-byte alignment when size is 8 bytes or larger.
  126. * - Require 4-byte alignment when size is 4 bytes.
  127. */
  128. if (size < 8) {
  129. if (!IS_ALIGNED(dest, 4) || size != 4)
  130. clean_cache_range(dst, size);
  131. } else {
  132. if (!IS_ALIGNED(dest, 8)) {
  133. dest = ALIGN(dest, boot_cpu_data.x86_clflush_size);
  134. clean_cache_range(dst, 1);
  135. }
  136. flushed = dest - (unsigned long) dst;
  137. if (size > flushed && !IS_ALIGNED(size - flushed, 8))
  138. clean_cache_range(dst + size - 1, 1);
  139. }
  140. return rc;
  141. }
  142. void memcpy_flushcache(void *_dst, const void *_src, size_t size)
  143. {
  144. unsigned long dest = (unsigned long) _dst;
  145. unsigned long source = (unsigned long) _src;
  146. /* cache copy and flush to align dest */
  147. if (!IS_ALIGNED(dest, 8)) {
  148. unsigned len = min_t(unsigned, size, ALIGN(dest, 8) - dest);
  149. memcpy((void *) dest, (void *) source, len);
  150. clean_cache_range((void *) dest, len);
  151. dest += len;
  152. source += len;
  153. size -= len;
  154. if (!size)
  155. return;
  156. }
  157. /* 4x8 movnti loop */
  158. while (size >= 32) {
  159. asm("movq (%0), %%r8\n"
  160. "movq 8(%0), %%r9\n"
  161. "movq 16(%0), %%r10\n"
  162. "movq 24(%0), %%r11\n"
  163. "movnti %%r8, (%1)\n"
  164. "movnti %%r9, 8(%1)\n"
  165. "movnti %%r10, 16(%1)\n"
  166. "movnti %%r11, 24(%1)\n"
  167. :: "r" (source), "r" (dest)
  168. : "memory", "r8", "r9", "r10", "r11");
  169. dest += 32;
  170. source += 32;
  171. size -= 32;
  172. }
  173. /* 1x8 movnti loop */
  174. while (size >= 8) {
  175. asm("movq (%0), %%r8\n"
  176. "movnti %%r8, (%1)\n"
  177. :: "r" (source), "r" (dest)
  178. : "memory", "r8");
  179. dest += 8;
  180. source += 8;
  181. size -= 8;
  182. }
  183. /* 1x4 movnti loop */
  184. while (size >= 4) {
  185. asm("movl (%0), %%r8d\n"
  186. "movnti %%r8d, (%1)\n"
  187. :: "r" (source), "r" (dest)
  188. : "memory", "r8");
  189. dest += 4;
  190. source += 4;
  191. size -= 4;
  192. }
  193. /* cache copy for remaining bytes */
  194. if (size) {
  195. memcpy((void *) dest, (void *) source, size);
  196. clean_cache_range((void *) dest, size);
  197. }
  198. }
  199. EXPORT_SYMBOL_GPL(memcpy_flushcache);
  200. void memcpy_page_flushcache(char *to, struct page *page, size_t offset,
  201. size_t len)
  202. {
  203. char *from = kmap_atomic(page);
  204. memcpy_flushcache(to, from + offset, len);
  205. kunmap_atomic(from);
  206. }
  207. #endif