xref: /openbmc/linux/arch/x86/lib/usercopy_64.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2185f3d38SThomas Gleixner /*
3185f3d38SThomas Gleixner  * User address space access functions.
4185f3d38SThomas Gleixner  *
5185f3d38SThomas Gleixner  * Copyright 1997 Andi Kleen <ak@muc.de>
6185f3d38SThomas Gleixner  * Copyright 1997 Linus Torvalds
7185f3d38SThomas Gleixner  * Copyright 2002 Andi Kleen <ak@suse.de>
8185f3d38SThomas Gleixner  */
9e683014cSPaul Gortmaker #include <linux/export.h>
1013d4ea09SAndy Lutomirski #include <linux/uaccess.h>
110aed55afSDan Williams #include <linux/highmem.h>
12185f3d38SThomas Gleixner #include <linux/libnvdimm.h>
13185f3d38SThomas Gleixner 
14185f3d38SThomas Gleixner /*
15185f3d38SThomas Gleixner  * Zero Userspace
16185f3d38SThomas Gleixner  */
170aed55afSDan Williams 
180aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
190aed55afSDan Williams /**
200aed55afSDan Williams  * clean_cache_range - write back a cache range with CLWB
210aed55afSDan Williams  * @vaddr:	virtual start address
220aed55afSDan Williams  * @size:	number of bytes to write back
230aed55afSDan Williams  *
240aed55afSDan Williams  * Write back a cache range using the CLWB (cache line write back)
250aed55afSDan Williams  * instruction. Note that @size is internally rounded up to be cache
260aed55afSDan Williams  * line size aligned.
270aed55afSDan Williams  */
clean_cache_range(void * addr,size_t size)280aed55afSDan Williams static void clean_cache_range(void *addr, size_t size)
290aed55afSDan Williams {
300aed55afSDan Williams 	u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
310aed55afSDan Williams 	unsigned long clflush_mask = x86_clflush_size - 1;
320aed55afSDan Williams 	void *vend = addr + size;
330aed55afSDan Williams 	void *p;
340aed55afSDan Williams 
350aed55afSDan Williams 	for (p = (void *)((unsigned long)addr & ~clflush_mask);
360aed55afSDan Williams 	     p < vend; p += x86_clflush_size)
370aed55afSDan Williams 		clwb(p);
380aed55afSDan Williams }
394e4f00a9SDan Williams 
arch_wb_cache_pmem(void * addr,size_t size)404e4f00a9SDan Williams void arch_wb_cache_pmem(void *addr, size_t size)
414e4f00a9SDan Williams {
424e4f00a9SDan Williams 	clean_cache_range(addr, size);
434e4f00a9SDan Williams }
444e4f00a9SDan Williams EXPORT_SYMBOL_GPL(arch_wb_cache_pmem);
450aed55afSDan Williams 
__copy_user_flushcache(void * dst,const void __user * src,unsigned size)460aed55afSDan Williams long __copy_user_flushcache(void *dst, const void __user *src, unsigned size)
470aed55afSDan Williams {
483639a535SLinus Torvalds 	unsigned long flushed, dest = (unsigned long) dst;
493639a535SLinus Torvalds 	long rc;
503639a535SLinus Torvalds 
51*e1f2750eSLinus Torvalds 	stac();
523639a535SLinus Torvalds 	rc = __copy_user_nocache(dst, src, size);
530aed55afSDan Williams 	clac();
540aed55afSDan Williams 
550aed55afSDan Williams 	/*
560aed55afSDan Williams 	 * __copy_user_nocache() uses non-temporal stores for the bulk
570aed55afSDan Williams 	 * of the transfer, but we need to manually flush if the
580aed55afSDan Williams 	 * transfer is unaligned. A cached memory copy is used when
590aed55afSDan Williams 	 * destination or size is not naturally aligned. That is:
600aed55afSDan Williams 	 *   - Require 8-byte alignment when size is 8 bytes or larger.
610aed55afSDan Williams 	 *   - Require 4-byte alignment when size is 4 bytes.
620aed55afSDan Williams 	 */
630aed55afSDan Williams 	if (size < 8) {
64a1cd6c2aSMikulas Patocka 		if (!IS_ALIGNED(dest, 4) || size != 4)
650aed55afSDan Williams 			clean_cache_range(dst, size);
660aed55afSDan Williams 	} else {
670aed55afSDan Williams 		if (!IS_ALIGNED(dest, 8)) {
680aed55afSDan Williams 			dest = ALIGN(dest, boot_cpu_data.x86_clflush_size);
690aed55afSDan Williams 			clean_cache_range(dst, 1);
700aed55afSDan Williams 		}
710aed55afSDan Williams 
720aed55afSDan Williams 		flushed = dest - (unsigned long) dst;
730aed55afSDan Williams 		if (size > flushed && !IS_ALIGNED(size - flushed, 8))
740aed55afSDan Williams 			clean_cache_range(dst + size - 1, 1);
750aed55afSDan Williams 	}
760aed55afSDan Williams 
770aed55afSDan Williams 	return rc;
780aed55afSDan Williams }
7902101c45SMikulas Patocka 
__memcpy_flushcache(void * _dst,const void * _src,size_t size)800aed55afSDan Williams void __memcpy_flushcache(void *_dst, const void *_src, size_t size)
810aed55afSDan Williams {
820aed55afSDan Williams 	unsigned long dest = (unsigned long) _dst;
830aed55afSDan Williams 	unsigned long source = (unsigned long) _src;
840aed55afSDan Williams 
850aed55afSDan Williams 	/* cache copy and flush to align dest */
86a6823e4eSMikulas Patocka 	if (!IS_ALIGNED(dest, 8)) {
870aed55afSDan Williams 		size_t len = min_t(size_t, size, ALIGN(dest, 8) - dest);
880aed55afSDan Williams 
890aed55afSDan Williams 		memcpy((void *) dest, (void *) source, len);
900aed55afSDan Williams 		clean_cache_range((void *) dest, len);
910aed55afSDan Williams 		dest += len;
920aed55afSDan Williams 		source += len;
930aed55afSDan Williams 		size -= len;
940aed55afSDan Williams 		if (!size)
950aed55afSDan Williams 			return;
960aed55afSDan Williams 	}
970aed55afSDan Williams 
980aed55afSDan Williams 	/* 4x8 movnti loop */
990aed55afSDan Williams 	while (size >= 32) {
1000aed55afSDan Williams 		asm("movq    (%0), %%r8\n"
1010aed55afSDan Williams 		    "movq   8(%0), %%r9\n"
1020aed55afSDan Williams 		    "movq  16(%0), %%r10\n"
1030aed55afSDan Williams 		    "movq  24(%0), %%r11\n"
1040aed55afSDan Williams 		    "movnti  %%r8,   (%1)\n"
1050aed55afSDan Williams 		    "movnti  %%r9,  8(%1)\n"
1060aed55afSDan Williams 		    "movnti %%r10, 16(%1)\n"
1070aed55afSDan Williams 		    "movnti %%r11, 24(%1)\n"
1080aed55afSDan Williams 		    :: "r" (source), "r" (dest)
1090aed55afSDan Williams 		    : "memory", "r8", "r9", "r10", "r11");
1100aed55afSDan Williams 		dest += 32;
1110aed55afSDan Williams 		source += 32;
1120aed55afSDan Williams 		size -= 32;
1130aed55afSDan Williams 	}
1140aed55afSDan Williams 
1150aed55afSDan Williams 	/* 1x8 movnti loop */
1160aed55afSDan Williams 	while (size >= 8) {
1170aed55afSDan Williams 		asm("movq    (%0), %%r8\n"
1180aed55afSDan Williams 		    "movnti  %%r8,   (%1)\n"
1190aed55afSDan Williams 		    :: "r" (source), "r" (dest)
1200aed55afSDan Williams 		    : "memory", "r8");
1210aed55afSDan Williams 		dest += 8;
1220aed55afSDan Williams 		source += 8;
1230aed55afSDan Williams 		size -= 8;
1240aed55afSDan Williams 	}
1250aed55afSDan Williams 
1260aed55afSDan Williams 	/* 1x4 movnti loop */
1270aed55afSDan Williams 	while (size >= 4) {
1280aed55afSDan Williams 		asm("movl    (%0), %%r8d\n"
1290aed55afSDan Williams 		    "movnti  %%r8d,   (%1)\n"
1300aed55afSDan Williams 		    :: "r" (source), "r" (dest)
1310aed55afSDan Williams 		    : "memory", "r8");
1320aed55afSDan Williams 		dest += 4;
1330aed55afSDan Williams 		source += 4;
1340aed55afSDan Williams 		size -= 4;
1350aed55afSDan Williams 	}
1360aed55afSDan Williams 
1370aed55afSDan Williams 	/* cache copy for remaining bytes */
1380aed55afSDan Williams 	if (size) {
1390aed55afSDan Williams 		memcpy((void *) dest, (void *) source, size);
1400aed55afSDan Williams 		clean_cache_range((void *) dest, size);
1410aed55afSDan Williams 	}
14202101c45SMikulas Patocka }
1430aed55afSDan Williams EXPORT_SYMBOL_GPL(__memcpy_flushcache);
144 #endif
145