Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Paul Gortmaker | e683014 | 2016-07-13 20:18:57 -0400 | [diff] [blame] | 9 | #include <linux/export.h> |
Andy Lutomirski | 13d4ea0 | 2016-07-14 13:22:57 -0700 | [diff] [blame] | 10 | #include <linux/uaccess.h> |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 11 | #include <linux/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * Zero Userspace |
| 15 | */ |
| 16 | |
| 17 | unsigned long __clear_user(void __user *addr, unsigned long size) |
| 18 | { |
| 19 | long __d0; |
Nick Piggin | 3ee1afa | 2008-09-10 13:37:17 +0200 | [diff] [blame] | 20 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | /* no memory constraint because it doesn't change any memory gcc knows |
| 22 | about */ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 23 | stac(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | asm volatile( |
| 25 | " testq %[size8],%[size8]\n" |
| 26 | " jz 4f\n" |
Matt Fleming | bb5570a | 2020-06-18 11:20:02 +0100 | [diff] [blame] | 27 | " .align 16\n" |
Alexey Dobriyan | 1153933 | 2018-05-08 00:39:37 +0300 | [diff] [blame] | 28 | "0: movq $0,(%[dst])\n" |
| 29 | " addq $8,%[dst]\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | " decl %%ecx ; jnz 0b\n" |
| 31 | "4: movq %[size1],%%rcx\n" |
| 32 | " testl %%ecx,%%ecx\n" |
| 33 | " jz 2f\n" |
Alexey Dobriyan | 1153933 | 2018-05-08 00:39:37 +0300 | [diff] [blame] | 34 | "1: movb $0,(%[dst])\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | " incq %[dst]\n" |
| 36 | " decl %%ecx ; jnz 1b\n" |
| 37 | "2:\n" |
| 38 | ".section .fixup,\"ax\"\n" |
| 39 | "3: lea 0(%[size1],%[size8],8),%[size8]\n" |
| 40 | " jmp 2b\n" |
| 41 | ".previous\n" |
Jann Horn | 75045f7 | 2018-08-28 22:14:18 +0200 | [diff] [blame] | 42 | _ASM_EXTABLE_UA(0b, 3b) |
| 43 | _ASM_EXTABLE_UA(1b, 2b) |
Andi Kleen | e0a9612 | 2009-01-16 15:22:11 +0100 | [diff] [blame] | 44 | : [size8] "=&c"(size), [dst] "=&D" (__d0) |
Alexey Dobriyan | 1153933 | 2018-05-08 00:39:37 +0300 | [diff] [blame] | 45 | : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr)); |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 46 | clac(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | return size; |
| 48 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 49 | EXPORT_SYMBOL(__clear_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | unsigned long clear_user(void __user *to, unsigned long n) |
| 52 | { |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 53 | if (access_ok(to, n)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | return __clear_user(to, n); |
| 55 | return n; |
| 56 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 57 | EXPORT_SYMBOL(clear_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 59 | #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE |
| 60 | /** |
| 61 | * clean_cache_range - write back a cache range with CLWB |
| 62 | * @vaddr: virtual start address |
| 63 | * @size: number of bytes to write back |
| 64 | * |
| 65 | * Write back a cache range using the CLWB (cache line write back) |
| 66 | * instruction. Note that @size is internally rounded up to be cache |
| 67 | * line size aligned. |
| 68 | */ |
| 69 | static void clean_cache_range(void *addr, size_t size) |
| 70 | { |
| 71 | u16 x86_clflush_size = boot_cpu_data.x86_clflush_size; |
| 72 | unsigned long clflush_mask = x86_clflush_size - 1; |
| 73 | void *vend = addr + size; |
| 74 | void *p; |
| 75 | |
| 76 | for (p = (void *)((unsigned long)addr & ~clflush_mask); |
| 77 | p < vend; p += x86_clflush_size) |
| 78 | clwb(p); |
| 79 | } |
| 80 | |
Dan Williams | 4e4f00a | 2017-05-29 22:40:44 -0700 | [diff] [blame] | 81 | void arch_wb_cache_pmem(void *addr, size_t size) |
| 82 | { |
| 83 | clean_cache_range(addr, size); |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(arch_wb_cache_pmem); |
| 86 | |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 87 | long __copy_user_flushcache(void *dst, const void __user *src, unsigned size) |
| 88 | { |
| 89 | unsigned long flushed, dest = (unsigned long) dst; |
| 90 | long rc = __copy_user_nocache(dst, src, size, 0); |
| 91 | |
| 92 | /* |
| 93 | * __copy_user_nocache() uses non-temporal stores for the bulk |
| 94 | * of the transfer, but we need to manually flush if the |
| 95 | * transfer is unaligned. A cached memory copy is used when |
| 96 | * destination or size is not naturally aligned. That is: |
| 97 | * - Require 8-byte alignment when size is 8 bytes or larger. |
| 98 | * - Require 4-byte alignment when size is 4 bytes. |
| 99 | */ |
| 100 | if (size < 8) { |
| 101 | if (!IS_ALIGNED(dest, 4) || size != 4) |
Mikulas Patocka | a1cd6c2 | 2020-09-25 21:19:24 -0700 | [diff] [blame] | 102 | clean_cache_range(dst, size); |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 103 | } else { |
| 104 | if (!IS_ALIGNED(dest, 8)) { |
| 105 | dest = ALIGN(dest, boot_cpu_data.x86_clflush_size); |
| 106 | clean_cache_range(dst, 1); |
| 107 | } |
| 108 | |
| 109 | flushed = dest - (unsigned long) dst; |
| 110 | if (size > flushed && !IS_ALIGNED(size - flushed, 8)) |
| 111 | clean_cache_range(dst + size - 1, 1); |
| 112 | } |
| 113 | |
| 114 | return rc; |
| 115 | } |
| 116 | |
Mikulas Patocka | 02101c4 | 2018-08-08 17:22:16 -0400 | [diff] [blame] | 117 | void __memcpy_flushcache(void *_dst, const void *_src, size_t size) |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 118 | { |
| 119 | unsigned long dest = (unsigned long) _dst; |
| 120 | unsigned long source = (unsigned long) _src; |
| 121 | |
| 122 | /* cache copy and flush to align dest */ |
| 123 | if (!IS_ALIGNED(dest, 8)) { |
Mikulas Patocka | 80fc453 | 2022-04-19 09:56:23 -0400 | [diff] [blame^] | 124 | size_t len = min_t(size_t, size, ALIGN(dest, 8) - dest); |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 125 | |
| 126 | memcpy((void *) dest, (void *) source, len); |
| 127 | clean_cache_range((void *) dest, len); |
| 128 | dest += len; |
| 129 | source += len; |
| 130 | size -= len; |
| 131 | if (!size) |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | /* 4x8 movnti loop */ |
| 136 | while (size >= 32) { |
| 137 | asm("movq (%0), %%r8\n" |
| 138 | "movq 8(%0), %%r9\n" |
| 139 | "movq 16(%0), %%r10\n" |
| 140 | "movq 24(%0), %%r11\n" |
| 141 | "movnti %%r8, (%1)\n" |
| 142 | "movnti %%r9, 8(%1)\n" |
| 143 | "movnti %%r10, 16(%1)\n" |
| 144 | "movnti %%r11, 24(%1)\n" |
| 145 | :: "r" (source), "r" (dest) |
| 146 | : "memory", "r8", "r9", "r10", "r11"); |
| 147 | dest += 32; |
| 148 | source += 32; |
| 149 | size -= 32; |
| 150 | } |
| 151 | |
| 152 | /* 1x8 movnti loop */ |
| 153 | while (size >= 8) { |
| 154 | asm("movq (%0), %%r8\n" |
| 155 | "movnti %%r8, (%1)\n" |
| 156 | :: "r" (source), "r" (dest) |
| 157 | : "memory", "r8"); |
| 158 | dest += 8; |
| 159 | source += 8; |
| 160 | size -= 8; |
| 161 | } |
| 162 | |
| 163 | /* 1x4 movnti loop */ |
| 164 | while (size >= 4) { |
| 165 | asm("movl (%0), %%r8d\n" |
| 166 | "movnti %%r8d, (%1)\n" |
| 167 | :: "r" (source), "r" (dest) |
| 168 | : "memory", "r8"); |
| 169 | dest += 4; |
| 170 | source += 4; |
| 171 | size -= 4; |
| 172 | } |
| 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 | } |
Mikulas Patocka | 02101c4 | 2018-08-08 17:22:16 -0400 | [diff] [blame] | 180 | EXPORT_SYMBOL_GPL(__memcpy_flushcache); |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 181 | |
| 182 | void memcpy_page_flushcache(char *to, struct page *page, size_t offset, |
| 183 | size_t len) |
| 184 | { |
| 185 | char *from = kmap_atomic(page); |
| 186 | |
| 187 | memcpy_flushcache(to, from + offset, len); |
| 188 | kunmap_atomic(from); |
| 189 | } |
| 190 | #endif |