xref: /openbmc/linux/arch/x86/lib/usercopy_64.c (revision a6823e4e360fe975bd3da4ab156df7c74c8b07f3)
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 
13185f3d38SThomas Gleixner /*
14185f3d38SThomas Gleixner  * Zero Userspace
15185f3d38SThomas Gleixner  */
16185f3d38SThomas Gleixner 
17185f3d38SThomas Gleixner unsigned long __clear_user(void __user *addr, unsigned long size)
18185f3d38SThomas Gleixner {
19185f3d38SThomas Gleixner 	long __d0;
203ee1afa3SNick Piggin 	might_fault();
21185f3d38SThomas Gleixner 	/* no memory constraint because it doesn't change any memory gcc knows
22185f3d38SThomas Gleixner 	   about */
2363bcff2aSH. Peter Anvin 	stac();
24185f3d38SThomas Gleixner 	asm volatile(
25185f3d38SThomas Gleixner 		"	testq  %[size8],%[size8]\n"
26185f3d38SThomas Gleixner 		"	jz     4f\n"
27bb5570adSMatt Fleming 		"	.align 16\n"
2811539337SAlexey Dobriyan 		"0:	movq $0,(%[dst])\n"
2911539337SAlexey Dobriyan 		"	addq   $8,%[dst]\n"
30185f3d38SThomas Gleixner 		"	decl %%ecx ; jnz   0b\n"
31185f3d38SThomas Gleixner 		"4:	movq  %[size1],%%rcx\n"
32185f3d38SThomas Gleixner 		"	testl %%ecx,%%ecx\n"
33185f3d38SThomas Gleixner 		"	jz     2f\n"
3411539337SAlexey Dobriyan 		"1:	movb   $0,(%[dst])\n"
35185f3d38SThomas Gleixner 		"	incq   %[dst]\n"
36185f3d38SThomas Gleixner 		"	decl %%ecx ; jnz  1b\n"
37185f3d38SThomas Gleixner 		"2:\n"
38d5d797dcSPeter Zijlstra 
39d5d797dcSPeter Zijlstra 		_ASM_EXTABLE_TYPE_REG(0b, 2b, EX_TYPE_UCOPY_LEN8, %[size1])
4075045f77SJann Horn 		_ASM_EXTABLE_UA(1b, 2b)
41d5d797dcSPeter Zijlstra 
42e0a96129SAndi Kleen 		: [size8] "=&c"(size), [dst] "=&D" (__d0)
4311539337SAlexey Dobriyan 		: [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr));
4463bcff2aSH. Peter Anvin 	clac();
45185f3d38SThomas Gleixner 	return size;
46185f3d38SThomas Gleixner }
47185f3d38SThomas Gleixner EXPORT_SYMBOL(__clear_user);
48185f3d38SThomas Gleixner 
49185f3d38SThomas Gleixner unsigned long clear_user(void __user *to, unsigned long n)
50185f3d38SThomas Gleixner {
5196d4f267SLinus Torvalds 	if (access_ok(to, n))
52185f3d38SThomas Gleixner 		return __clear_user(to, n);
53185f3d38SThomas Gleixner 	return n;
54185f3d38SThomas Gleixner }
55185f3d38SThomas Gleixner EXPORT_SYMBOL(clear_user);
56185f3d38SThomas Gleixner 
570aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
580aed55afSDan Williams /**
590aed55afSDan Williams  * clean_cache_range - write back a cache range with CLWB
600aed55afSDan Williams  * @vaddr:	virtual start address
610aed55afSDan Williams  * @size:	number of bytes to write back
620aed55afSDan Williams  *
630aed55afSDan Williams  * Write back a cache range using the CLWB (cache line write back)
640aed55afSDan Williams  * instruction. Note that @size is internally rounded up to be cache
650aed55afSDan Williams  * line size aligned.
660aed55afSDan Williams  */
670aed55afSDan Williams static void clean_cache_range(void *addr, size_t size)
680aed55afSDan Williams {
690aed55afSDan Williams 	u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
700aed55afSDan Williams 	unsigned long clflush_mask = x86_clflush_size - 1;
710aed55afSDan Williams 	void *vend = addr + size;
720aed55afSDan Williams 	void *p;
730aed55afSDan Williams 
740aed55afSDan Williams 	for (p = (void *)((unsigned long)addr & ~clflush_mask);
750aed55afSDan Williams 	     p < vend; p += x86_clflush_size)
760aed55afSDan Williams 		clwb(p);
770aed55afSDan Williams }
780aed55afSDan Williams 
794e4f00a9SDan Williams void arch_wb_cache_pmem(void *addr, size_t size)
804e4f00a9SDan Williams {
814e4f00a9SDan Williams 	clean_cache_range(addr, size);
824e4f00a9SDan Williams }
834e4f00a9SDan Williams EXPORT_SYMBOL_GPL(arch_wb_cache_pmem);
844e4f00a9SDan Williams 
850aed55afSDan Williams long __copy_user_flushcache(void *dst, const void __user *src, unsigned size)
860aed55afSDan Williams {
870aed55afSDan Williams 	unsigned long flushed, dest = (unsigned long) dst;
880aed55afSDan Williams 	long rc = __copy_user_nocache(dst, src, size, 0);
890aed55afSDan Williams 
900aed55afSDan Williams 	/*
910aed55afSDan Williams 	 * __copy_user_nocache() uses non-temporal stores for the bulk
920aed55afSDan Williams 	 * of the transfer, but we need to manually flush if the
930aed55afSDan Williams 	 * transfer is unaligned. A cached memory copy is used when
940aed55afSDan Williams 	 * destination or size is not naturally aligned. That is:
950aed55afSDan Williams 	 *   - Require 8-byte alignment when size is 8 bytes or larger.
960aed55afSDan Williams 	 *   - Require 4-byte alignment when size is 4 bytes.
970aed55afSDan Williams 	 */
980aed55afSDan Williams 	if (size < 8) {
990aed55afSDan Williams 		if (!IS_ALIGNED(dest, 4) || size != 4)
100a1cd6c2aSMikulas Patocka 			clean_cache_range(dst, size);
1010aed55afSDan Williams 	} else {
1020aed55afSDan Williams 		if (!IS_ALIGNED(dest, 8)) {
1030aed55afSDan Williams 			dest = ALIGN(dest, boot_cpu_data.x86_clflush_size);
1040aed55afSDan Williams 			clean_cache_range(dst, 1);
1050aed55afSDan Williams 		}
1060aed55afSDan Williams 
1070aed55afSDan Williams 		flushed = dest - (unsigned long) dst;
1080aed55afSDan Williams 		if (size > flushed && !IS_ALIGNED(size - flushed, 8))
1090aed55afSDan Williams 			clean_cache_range(dst + size - 1, 1);
1100aed55afSDan Williams 	}
1110aed55afSDan Williams 
1120aed55afSDan Williams 	return rc;
1130aed55afSDan Williams }
1140aed55afSDan Williams 
11502101c45SMikulas Patocka void __memcpy_flushcache(void *_dst, const void *_src, size_t size)
1160aed55afSDan Williams {
1170aed55afSDan Williams 	unsigned long dest = (unsigned long) _dst;
1180aed55afSDan Williams 	unsigned long source = (unsigned long) _src;
1190aed55afSDan Williams 
1200aed55afSDan Williams 	/* cache copy and flush to align dest */
1210aed55afSDan Williams 	if (!IS_ALIGNED(dest, 8)) {
122*a6823e4eSMikulas Patocka 		size_t len = min_t(size_t, size, ALIGN(dest, 8) - dest);
1230aed55afSDan Williams 
1240aed55afSDan Williams 		memcpy((void *) dest, (void *) source, len);
1250aed55afSDan Williams 		clean_cache_range((void *) dest, len);
1260aed55afSDan Williams 		dest += len;
1270aed55afSDan Williams 		source += len;
1280aed55afSDan Williams 		size -= len;
1290aed55afSDan Williams 		if (!size)
1300aed55afSDan Williams 			return;
1310aed55afSDan Williams 	}
1320aed55afSDan Williams 
1330aed55afSDan Williams 	/* 4x8 movnti loop */
1340aed55afSDan Williams 	while (size >= 32) {
1350aed55afSDan Williams 		asm("movq    (%0), %%r8\n"
1360aed55afSDan Williams 		    "movq   8(%0), %%r9\n"
1370aed55afSDan Williams 		    "movq  16(%0), %%r10\n"
1380aed55afSDan Williams 		    "movq  24(%0), %%r11\n"
1390aed55afSDan Williams 		    "movnti  %%r8,   (%1)\n"
1400aed55afSDan Williams 		    "movnti  %%r9,  8(%1)\n"
1410aed55afSDan Williams 		    "movnti %%r10, 16(%1)\n"
1420aed55afSDan Williams 		    "movnti %%r11, 24(%1)\n"
1430aed55afSDan Williams 		    :: "r" (source), "r" (dest)
1440aed55afSDan Williams 		    : "memory", "r8", "r9", "r10", "r11");
1450aed55afSDan Williams 		dest += 32;
1460aed55afSDan Williams 		source += 32;
1470aed55afSDan Williams 		size -= 32;
1480aed55afSDan Williams 	}
1490aed55afSDan Williams 
1500aed55afSDan Williams 	/* 1x8 movnti loop */
1510aed55afSDan Williams 	while (size >= 8) {
1520aed55afSDan Williams 		asm("movq    (%0), %%r8\n"
1530aed55afSDan Williams 		    "movnti  %%r8,   (%1)\n"
1540aed55afSDan Williams 		    :: "r" (source), "r" (dest)
1550aed55afSDan Williams 		    : "memory", "r8");
1560aed55afSDan Williams 		dest += 8;
1570aed55afSDan Williams 		source += 8;
1580aed55afSDan Williams 		size -= 8;
1590aed55afSDan Williams 	}
1600aed55afSDan Williams 
1610aed55afSDan Williams 	/* 1x4 movnti loop */
1620aed55afSDan Williams 	while (size >= 4) {
1630aed55afSDan Williams 		asm("movl    (%0), %%r8d\n"
1640aed55afSDan Williams 		    "movnti  %%r8d,   (%1)\n"
1650aed55afSDan Williams 		    :: "r" (source), "r" (dest)
1660aed55afSDan Williams 		    : "memory", "r8");
1670aed55afSDan Williams 		dest += 4;
1680aed55afSDan Williams 		source += 4;
1690aed55afSDan Williams 		size -= 4;
1700aed55afSDan Williams 	}
1710aed55afSDan Williams 
1720aed55afSDan Williams 	/* cache copy for remaining bytes */
1730aed55afSDan Williams 	if (size) {
1740aed55afSDan Williams 		memcpy((void *) dest, (void *) source, size);
1750aed55afSDan Williams 		clean_cache_range((void *) dest, size);
1760aed55afSDan Williams 	}
1770aed55afSDan Williams }
17802101c45SMikulas Patocka EXPORT_SYMBOL_GPL(__memcpy_flushcache);
1790aed55afSDan Williams 
1800aed55afSDan Williams void memcpy_page_flushcache(char *to, struct page *page, size_t offset,
1810aed55afSDan Williams 		size_t len)
1820aed55afSDan Williams {
1830aed55afSDan Williams 	char *from = kmap_atomic(page);
1840aed55afSDan Williams 
1850aed55afSDan Williams 	memcpy_flushcache(to, from + offset, len);
1860aed55afSDan Williams 	kunmap_atomic(from);
1870aed55afSDan Williams }
1880aed55afSDan Williams #endif
189