usercopy_64.c 5.5 KB

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