usercopy_64.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * User address space access functions.
  4. *
  5. * Copyright 1997 Andi Kleen <ak@muc.de>
  6. * Copyright 1997 Linus Torvalds
  7. * Copyright 2002 Andi Kleen <ak@suse.de>
  8. */
  9. #include <linux/export.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/highmem.h>
  12. #include <linux/libnvdimm.h>
  13. /*
  14. * Zero Userspace
  15. */
  16. #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
  17. /**
  18. * clean_cache_range - write back a cache range with CLWB
  19. * @vaddr: virtual start address
  20. * @size: number of bytes to write back
  21. *
  22. * Write back a cache range using the CLWB (cache line write back)
  23. * instruction. Note that @size is internally rounded up to be cache
  24. * line size aligned.
  25. */
  26. static void clean_cache_range(void *addr, size_t size)
  27. {
  28. u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
  29. unsigned long clflush_mask = x86_clflush_size - 1;
  30. void *vend = addr + size;
  31. void *p;
  32. for (p = (void *)((unsigned long)addr & ~clflush_mask);
  33. p < vend; p += x86_clflush_size)
  34. clwb(p);
  35. }
  36. void arch_wb_cache_pmem(void *addr, size_t size)
  37. {
  38. clean_cache_range(addr, size);
  39. }
  40. EXPORT_SYMBOL_GPL(arch_wb_cache_pmem);
  41. long __copy_user_flushcache(void *dst, const void __user *src, unsigned size)
  42. {
  43. unsigned long flushed, dest = (unsigned long) dst;
  44. long rc;
  45. stac();
  46. rc = __copy_user_nocache(dst, src, size);
  47. clac();
  48. /*
  49. * __copy_user_nocache() uses non-temporal stores for the bulk
  50. * of the transfer, but we need to manually flush if the
  51. * transfer is unaligned. A cached memory copy is used when
  52. * destination or size is not naturally aligned. That is:
  53. * - Require 8-byte alignment when size is 8 bytes or larger.
  54. * - Require 4-byte alignment when size is 4 bytes.
  55. */
  56. if (size < 8) {
  57. if (!IS_ALIGNED(dest, 4) || size != 4)
  58. clean_cache_range(dst, size);
  59. } else {
  60. if (!IS_ALIGNED(dest, 8)) {
  61. dest = ALIGN(dest, boot_cpu_data.x86_clflush_size);
  62. clean_cache_range(dst, 1);
  63. }
  64. flushed = dest - (unsigned long) dst;
  65. if (size > flushed && !IS_ALIGNED(size - flushed, 8))
  66. clean_cache_range(dst + size - 1, 1);
  67. }
  68. return rc;
  69. }
  70. void __memcpy_flushcache(void *_dst, const void *_src, size_t size)
  71. {
  72. unsigned long dest = (unsigned long) _dst;
  73. unsigned long source = (unsigned long) _src;
  74. /* cache copy and flush to align dest */
  75. if (!IS_ALIGNED(dest, 8)) {
  76. size_t len = min_t(size_t, size, ALIGN(dest, 8) - dest);
  77. memcpy((void *) dest, (void *) source, len);
  78. clean_cache_range((void *) dest, len);
  79. dest += len;
  80. source += len;
  81. size -= len;
  82. if (!size)
  83. return;
  84. }
  85. /* 4x8 movnti loop */
  86. while (size >= 32) {
  87. asm("movq (%0), %%r8\n"
  88. "movq 8(%0), %%r9\n"
  89. "movq 16(%0), %%r10\n"
  90. "movq 24(%0), %%r11\n"
  91. "movnti %%r8, (%1)\n"
  92. "movnti %%r9, 8(%1)\n"
  93. "movnti %%r10, 16(%1)\n"
  94. "movnti %%r11, 24(%1)\n"
  95. :: "r" (source), "r" (dest)
  96. : "memory", "r8", "r9", "r10", "r11");
  97. dest += 32;
  98. source += 32;
  99. size -= 32;
  100. }
  101. /* 1x8 movnti loop */
  102. while (size >= 8) {
  103. asm("movq (%0), %%r8\n"
  104. "movnti %%r8, (%1)\n"
  105. :: "r" (source), "r" (dest)
  106. : "memory", "r8");
  107. dest += 8;
  108. source += 8;
  109. size -= 8;
  110. }
  111. /* 1x4 movnti loop */
  112. while (size >= 4) {
  113. asm("movl (%0), %%r8d\n"
  114. "movnti %%r8d, (%1)\n"
  115. :: "r" (source), "r" (dest)
  116. : "memory", "r8");
  117. dest += 4;
  118. source += 4;
  119. size -= 4;
  120. }
  121. /* cache copy for remaining bytes */
  122. if (size) {
  123. memcpy((void *) dest, (void *) source, size);
  124. clean_cache_range((void *) dest, size);
  125. }
  126. }
  127. EXPORT_SYMBOL_GPL(__memcpy_flushcache);
  128. #endif