xref: /openbmc/linux/arch/x86/lib/usercopy_64.c (revision 457c89965399115e5cd8bf38f9c597293405703d)
1*457c8996SThomas 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"
2711539337SAlexey Dobriyan 		"0:	movq $0,(%[dst])\n"
2811539337SAlexey Dobriyan 		"	addq   $8,%[dst]\n"
29185f3d38SThomas Gleixner 		"	decl %%ecx ; jnz   0b\n"
30185f3d38SThomas Gleixner 		"4:	movq  %[size1],%%rcx\n"
31185f3d38SThomas Gleixner 		"	testl %%ecx,%%ecx\n"
32185f3d38SThomas Gleixner 		"	jz     2f\n"
3311539337SAlexey Dobriyan 		"1:	movb   $0,(%[dst])\n"
34185f3d38SThomas Gleixner 		"	incq   %[dst]\n"
35185f3d38SThomas Gleixner 		"	decl %%ecx ; jnz  1b\n"
36185f3d38SThomas Gleixner 		"2:\n"
37185f3d38SThomas Gleixner 		".section .fixup,\"ax\"\n"
38185f3d38SThomas Gleixner 		"3:	lea 0(%[size1],%[size8],8),%[size8]\n"
39185f3d38SThomas Gleixner 		"	jmp 2b\n"
40185f3d38SThomas Gleixner 		".previous\n"
4175045f77SJann Horn 		_ASM_EXTABLE_UA(0b, 3b)
4275045f77SJann Horn 		_ASM_EXTABLE_UA(1b, 2b)
43e0a96129SAndi Kleen 		: [size8] "=&c"(size), [dst] "=&D" (__d0)
4411539337SAlexey Dobriyan 		: [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr));
4563bcff2aSH. Peter Anvin 	clac();
46185f3d38SThomas Gleixner 	return size;
47185f3d38SThomas Gleixner }
48185f3d38SThomas Gleixner EXPORT_SYMBOL(__clear_user);
49185f3d38SThomas Gleixner 
50185f3d38SThomas Gleixner unsigned long clear_user(void __user *to, unsigned long n)
51185f3d38SThomas Gleixner {
5296d4f267SLinus Torvalds 	if (access_ok(to, n))
53185f3d38SThomas Gleixner 		return __clear_user(to, n);
54185f3d38SThomas Gleixner 	return n;
55185f3d38SThomas Gleixner }
56185f3d38SThomas Gleixner EXPORT_SYMBOL(clear_user);
57185f3d38SThomas Gleixner 
581129585aSVitaly Mayatskikh /*
5912c89130SDan Williams  * Similar to copy_user_handle_tail, probe for the write fault point,
6012c89130SDan Williams  * but reuse __memcpy_mcsafe in case a new read error is encountered.
6112c89130SDan Williams  * clac() is handled in _copy_to_iter_mcsafe().
6212c89130SDan Williams  */
6312c89130SDan Williams __visible unsigned long
6412c89130SDan Williams mcsafe_handle_tail(char *to, char *from, unsigned len)
6512c89130SDan Williams {
6612c89130SDan Williams 	for (; len; --len, to++, from++) {
6712c89130SDan Williams 		/*
6812c89130SDan Williams 		 * Call the assembly routine back directly since
6912c89130SDan Williams 		 * memcpy_mcsafe() may silently fallback to memcpy.
7012c89130SDan Williams 		 */
7112c89130SDan Williams 		unsigned long rem = __memcpy_mcsafe(to, from, 1);
7212c89130SDan Williams 
7312c89130SDan Williams 		if (rem)
7412c89130SDan Williams 			break;
7512c89130SDan Williams 	}
7612c89130SDan Williams 	return len;
7712c89130SDan Williams }
7812c89130SDan Williams 
790aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
800aed55afSDan Williams /**
810aed55afSDan Williams  * clean_cache_range - write back a cache range with CLWB
820aed55afSDan Williams  * @vaddr:	virtual start address
830aed55afSDan Williams  * @size:	number of bytes to write back
840aed55afSDan Williams  *
850aed55afSDan Williams  * Write back a cache range using the CLWB (cache line write back)
860aed55afSDan Williams  * instruction. Note that @size is internally rounded up to be cache
870aed55afSDan Williams  * line size aligned.
880aed55afSDan Williams  */
890aed55afSDan Williams static void clean_cache_range(void *addr, size_t size)
900aed55afSDan Williams {
910aed55afSDan Williams 	u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
920aed55afSDan Williams 	unsigned long clflush_mask = x86_clflush_size - 1;
930aed55afSDan Williams 	void *vend = addr + size;
940aed55afSDan Williams 	void *p;
950aed55afSDan Williams 
960aed55afSDan Williams 	for (p = (void *)((unsigned long)addr & ~clflush_mask);
970aed55afSDan Williams 	     p < vend; p += x86_clflush_size)
980aed55afSDan Williams 		clwb(p);
990aed55afSDan Williams }
1000aed55afSDan Williams 
1014e4f00a9SDan Williams void arch_wb_cache_pmem(void *addr, size_t size)
1024e4f00a9SDan Williams {
1034e4f00a9SDan Williams 	clean_cache_range(addr, size);
1044e4f00a9SDan Williams }
1054e4f00a9SDan Williams EXPORT_SYMBOL_GPL(arch_wb_cache_pmem);
1064e4f00a9SDan Williams 
1070aed55afSDan Williams long __copy_user_flushcache(void *dst, const void __user *src, unsigned size)
1080aed55afSDan Williams {
1090aed55afSDan Williams 	unsigned long flushed, dest = (unsigned long) dst;
1100aed55afSDan Williams 	long rc = __copy_user_nocache(dst, src, size, 0);
1110aed55afSDan Williams 
1120aed55afSDan Williams 	/*
1130aed55afSDan Williams 	 * __copy_user_nocache() uses non-temporal stores for the bulk
1140aed55afSDan Williams 	 * of the transfer, but we need to manually flush if the
1150aed55afSDan Williams 	 * transfer is unaligned. A cached memory copy is used when
1160aed55afSDan Williams 	 * destination or size is not naturally aligned. That is:
1170aed55afSDan Williams 	 *   - Require 8-byte alignment when size is 8 bytes or larger.
1180aed55afSDan Williams 	 *   - Require 4-byte alignment when size is 4 bytes.
1190aed55afSDan Williams 	 */
1200aed55afSDan Williams 	if (size < 8) {
1210aed55afSDan Williams 		if (!IS_ALIGNED(dest, 4) || size != 4)
1220aed55afSDan Williams 			clean_cache_range(dst, 1);
1230aed55afSDan Williams 	} else {
1240aed55afSDan Williams 		if (!IS_ALIGNED(dest, 8)) {
1250aed55afSDan Williams 			dest = ALIGN(dest, boot_cpu_data.x86_clflush_size);
1260aed55afSDan Williams 			clean_cache_range(dst, 1);
1270aed55afSDan Williams 		}
1280aed55afSDan Williams 
1290aed55afSDan Williams 		flushed = dest - (unsigned long) dst;
1300aed55afSDan Williams 		if (size > flushed && !IS_ALIGNED(size - flushed, 8))
1310aed55afSDan Williams 			clean_cache_range(dst + size - 1, 1);
1320aed55afSDan Williams 	}
1330aed55afSDan Williams 
1340aed55afSDan Williams 	return rc;
1350aed55afSDan Williams }
1360aed55afSDan Williams 
13702101c45SMikulas Patocka void __memcpy_flushcache(void *_dst, const void *_src, size_t size)
1380aed55afSDan Williams {
1390aed55afSDan Williams 	unsigned long dest = (unsigned long) _dst;
1400aed55afSDan Williams 	unsigned long source = (unsigned long) _src;
1410aed55afSDan Williams 
1420aed55afSDan Williams 	/* cache copy and flush to align dest */
1430aed55afSDan Williams 	if (!IS_ALIGNED(dest, 8)) {
1440aed55afSDan Williams 		unsigned len = min_t(unsigned, size, ALIGN(dest, 8) - dest);
1450aed55afSDan Williams 
1460aed55afSDan Williams 		memcpy((void *) dest, (void *) source, len);
1470aed55afSDan Williams 		clean_cache_range((void *) dest, len);
1480aed55afSDan Williams 		dest += len;
1490aed55afSDan Williams 		source += len;
1500aed55afSDan Williams 		size -= len;
1510aed55afSDan Williams 		if (!size)
1520aed55afSDan Williams 			return;
1530aed55afSDan Williams 	}
1540aed55afSDan Williams 
1550aed55afSDan Williams 	/* 4x8 movnti loop */
1560aed55afSDan Williams 	while (size >= 32) {
1570aed55afSDan Williams 		asm("movq    (%0), %%r8\n"
1580aed55afSDan Williams 		    "movq   8(%0), %%r9\n"
1590aed55afSDan Williams 		    "movq  16(%0), %%r10\n"
1600aed55afSDan Williams 		    "movq  24(%0), %%r11\n"
1610aed55afSDan Williams 		    "movnti  %%r8,   (%1)\n"
1620aed55afSDan Williams 		    "movnti  %%r9,  8(%1)\n"
1630aed55afSDan Williams 		    "movnti %%r10, 16(%1)\n"
1640aed55afSDan Williams 		    "movnti %%r11, 24(%1)\n"
1650aed55afSDan Williams 		    :: "r" (source), "r" (dest)
1660aed55afSDan Williams 		    : "memory", "r8", "r9", "r10", "r11");
1670aed55afSDan Williams 		dest += 32;
1680aed55afSDan Williams 		source += 32;
1690aed55afSDan Williams 		size -= 32;
1700aed55afSDan Williams 	}
1710aed55afSDan Williams 
1720aed55afSDan Williams 	/* 1x8 movnti loop */
1730aed55afSDan Williams 	while (size >= 8) {
1740aed55afSDan Williams 		asm("movq    (%0), %%r8\n"
1750aed55afSDan Williams 		    "movnti  %%r8,   (%1)\n"
1760aed55afSDan Williams 		    :: "r" (source), "r" (dest)
1770aed55afSDan Williams 		    : "memory", "r8");
1780aed55afSDan Williams 		dest += 8;
1790aed55afSDan Williams 		source += 8;
1800aed55afSDan Williams 		size -= 8;
1810aed55afSDan Williams 	}
1820aed55afSDan Williams 
1830aed55afSDan Williams 	/* 1x4 movnti loop */
1840aed55afSDan Williams 	while (size >= 4) {
1850aed55afSDan Williams 		asm("movl    (%0), %%r8d\n"
1860aed55afSDan Williams 		    "movnti  %%r8d,   (%1)\n"
1870aed55afSDan Williams 		    :: "r" (source), "r" (dest)
1880aed55afSDan Williams 		    : "memory", "r8");
1890aed55afSDan Williams 		dest += 4;
1900aed55afSDan Williams 		source += 4;
1910aed55afSDan Williams 		size -= 4;
1920aed55afSDan Williams 	}
1930aed55afSDan Williams 
1940aed55afSDan Williams 	/* cache copy for remaining bytes */
1950aed55afSDan Williams 	if (size) {
1960aed55afSDan Williams 		memcpy((void *) dest, (void *) source, size);
1970aed55afSDan Williams 		clean_cache_range((void *) dest, size);
1980aed55afSDan Williams 	}
1990aed55afSDan Williams }
20002101c45SMikulas Patocka EXPORT_SYMBOL_GPL(__memcpy_flushcache);
2010aed55afSDan Williams 
2020aed55afSDan Williams void memcpy_page_flushcache(char *to, struct page *page, size_t offset,
2030aed55afSDan Williams 		size_t len)
2040aed55afSDan Williams {
2050aed55afSDan Williams 	char *from = kmap_atomic(page);
2060aed55afSDan Williams 
2070aed55afSDan Williams 	memcpy_flushcache(to, from + offset, len);
2080aed55afSDan Williams 	kunmap_atomic(from);
2090aed55afSDan Williams }
2100aed55afSDan Williams #endif
211