usercopy_64.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 %[zero],(%[dst])\n"
  25. " addq %[eight],%[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 %b[zero],(%[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. [zero] "r" (0UL), [eight] "r" (8UL));
  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. #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
  73. /**
  74. * clean_cache_range - write back a cache range with CLWB
  75. * @vaddr: virtual start address
  76. * @size: number of bytes to write back
  77. *
  78. * Write back a cache range using the CLWB (cache line write back)
  79. * instruction. Note that @size is internally rounded up to be cache
  80. * line size aligned.
  81. */
  82. static void clean_cache_range(void *addr, size_t size)
  83. {
  84. u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
  85. unsigned long clflush_mask = x86_clflush_size - 1;
  86. void *vend = addr + size;
  87. void *p;
  88. for (p = (void *)((unsigned long)addr & ~clflush_mask);
  89. p < vend; p += x86_clflush_size)
  90. clwb(p);
  91. }
  92. void arch_wb_cache_pmem(void *addr, size_t size)
  93. {
  94. clean_cache_range(addr, size);
  95. }
  96. EXPORT_SYMBOL_GPL(arch_wb_cache_pmem);
  97. long __copy_user_flushcache(void *dst, const void __user *src, unsigned size)
  98. {
  99. unsigned long flushed, dest = (unsigned long) dst;
  100. long rc = __copy_user_nocache(dst, src, size, 0);
  101. /*
  102. * __copy_user_nocache() uses non-temporal stores for the bulk
  103. * of the transfer, but we need to manually flush if the
  104. * transfer is unaligned. A cached memory copy is used when
  105. * destination or size is not naturally aligned. That is:
  106. * - Require 8-byte alignment when size is 8 bytes or larger.
  107. * - Require 4-byte alignment when size is 4 bytes.
  108. */
  109. if (size < 8) {
  110. if (!IS_ALIGNED(dest, 4) || size != 4)
  111. clean_cache_range(dst, 1);
  112. } else {
  113. if (!IS_ALIGNED(dest, 8)) {
  114. dest = ALIGN(dest, boot_cpu_data.x86_clflush_size);
  115. clean_cache_range(dst, 1);
  116. }
  117. flushed = dest - (unsigned long) dst;
  118. if (size > flushed && !IS_ALIGNED(size - flushed, 8))
  119. clean_cache_range(dst + size - 1, 1);
  120. }
  121. return rc;
  122. }
  123. void memcpy_flushcache(void *_dst, const void *_src, size_t size)
  124. {
  125. unsigned long dest = (unsigned long) _dst;
  126. unsigned long source = (unsigned long) _src;
  127. /* cache copy and flush to align dest */
  128. if (!IS_ALIGNED(dest, 8)) {
  129. unsigned len = min_t(unsigned, size, ALIGN(dest, 8) - dest);
  130. memcpy((void *) dest, (void *) source, len);
  131. clean_cache_range((void *) dest, len);
  132. dest += len;
  133. source += len;
  134. size -= len;
  135. if (!size)
  136. return;
  137. }
  138. /* 4x8 movnti loop */
  139. while (size >= 32) {
  140. asm("movq (%0), %%r8\n"
  141. "movq 8(%0), %%r9\n"
  142. "movq 16(%0), %%r10\n"
  143. "movq 24(%0), %%r11\n"
  144. "movnti %%r8, (%1)\n"
  145. "movnti %%r9, 8(%1)\n"
  146. "movnti %%r10, 16(%1)\n"
  147. "movnti %%r11, 24(%1)\n"
  148. :: "r" (source), "r" (dest)
  149. : "memory", "r8", "r9", "r10", "r11");
  150. dest += 32;
  151. source += 32;
  152. size -= 32;
  153. }
  154. /* 1x8 movnti loop */
  155. while (size >= 8) {
  156. asm("movq (%0), %%r8\n"
  157. "movnti %%r8, (%1)\n"
  158. :: "r" (source), "r" (dest)
  159. : "memory", "r8");
  160. dest += 8;
  161. source += 8;
  162. size -= 8;
  163. }
  164. /* 1x4 movnti loop */
  165. while (size >= 4) {
  166. asm("movl (%0), %%r8d\n"
  167. "movnti %%r8d, (%1)\n"
  168. :: "r" (source), "r" (dest)
  169. : "memory", "r8");
  170. dest += 4;
  171. source += 4;
  172. size -= 4;
  173. }
  174. /* cache copy for remaining bytes */
  175. if (size) {
  176. memcpy((void *) dest, (void *) source, size);
  177. clean_cache_range((void *) dest, size);
  178. }
  179. }
  180. EXPORT_SYMBOL_GPL(memcpy_flushcache);
  181. void memcpy_page_flushcache(char *to, struct page *page, size_t offset,
  182. size_t len)
  183. {
  184. char *from = kmap_atomic(page);
  185. memcpy_flushcache(to, from + offset, len);
  186. kunmap_atomic(from);
  187. }
  188. #endif