Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Based on arch/arm/include/asm/uaccess.h |
| 4 | * |
| 5 | * Copyright (C) 2012 ARM Ltd. |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 6 | */ |
| 7 | #ifndef __ASM_UACCESS_H |
| 8 | #define __ASM_UACCESS_H |
| 9 | |
Catalin Marinas | bd38967 | 2016-07-01 14:58:21 +0100 | [diff] [blame] | 10 | #include <asm/alternative.h> |
Catalin Marinas | 4b65a5d | 2016-07-01 16:53:00 +0100 | [diff] [blame] | 11 | #include <asm/kernel-pgtable.h> |
Catalin Marinas | bd38967 | 2016-07-01 14:58:21 +0100 | [diff] [blame] | 12 | #include <asm/sysreg.h> |
| 13 | |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 14 | /* |
| 15 | * User space memory access functions |
| 16 | */ |
Andre Przywara | 87261d1 | 2016-10-19 14:40:54 +0100 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Yang Shi | bffe1ba | 2016-06-08 14:40:56 -0700 | [diff] [blame] | 18 | #include <linux/kasan-checks.h> |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 19 | #include <linux/string.h> |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 20 | |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 21 | #include <asm/cpufeature.h> |
Will Deacon | 5f1f7f6 | 2020-06-30 13:53:07 +0100 | [diff] [blame] | 22 | #include <asm/mmu.h> |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 23 | #include <asm/ptrace.h> |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 24 | #include <asm/memory.h> |
Al Viro | 4658393 | 2016-12-25 14:00:03 -0500 | [diff] [blame] | 25 | #include <asm/extable.h> |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 26 | |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 27 | #define HAVE_GET_KERNEL_NOFAULT |
| 28 | |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 29 | /* |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 30 | * Test whether a block of memory is a valid user space address. |
| 31 | * Returns 1 if the range is valid, 0 otherwise. |
| 32 | * |
| 33 | * This is equivalent to the following test: |
Mark Rutland | 3d2403f | 2020-12-02 13:15:55 +0000 | [diff] [blame] | 34 | * (u65)addr + (u65)size <= (u65)TASK_SIZE_MAX |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 35 | */ |
Robin Murphy | 9085b34 | 2018-02-19 13:38:00 +0000 | [diff] [blame] | 36 | static inline unsigned long __range_ok(const void __user *addr, unsigned long size) |
Robin Murphy | 51369e3 | 2018-02-05 15:34:18 +0000 | [diff] [blame] | 37 | { |
Mark Rutland | 3d2403f | 2020-12-02 13:15:55 +0000 | [diff] [blame] | 38 | unsigned long ret, limit = TASK_SIZE_MAX - 1; |
Robin Murphy | 51369e3 | 2018-02-05 15:34:18 +0000 | [diff] [blame] | 39 | |
Catalin Marinas | df325e0 | 2019-12-05 13:57:36 +0000 | [diff] [blame] | 40 | /* |
| 41 | * Asynchronous I/O running in a kernel thread does not have the |
| 42 | * TIF_TAGGED_ADDR flag of the process owning the mm, so always untag |
| 43 | * the user address before checking. |
| 44 | */ |
Catalin Marinas | 63f0c60 | 2019-07-23 19:58:39 +0200 | [diff] [blame] | 45 | if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI) && |
Catalin Marinas | df325e0 | 2019-12-05 13:57:36 +0000 | [diff] [blame] | 46 | (current->flags & PF_KTHREAD || test_thread_flag(TIF_TAGGED_ADDR))) |
Catalin Marinas | 63f0c60 | 2019-07-23 19:58:39 +0200 | [diff] [blame] | 47 | addr = untagged_addr(addr); |
Andrey Konovalov | 2b835e2 | 2019-07-23 19:58:38 +0200 | [diff] [blame] | 48 | |
Robin Murphy | 51369e3 | 2018-02-05 15:34:18 +0000 | [diff] [blame] | 49 | __chk_user_ptr(addr); |
| 50 | asm volatile( |
| 51 | // A + B <= C + 1 for all A,B,C, in four easy steps: |
| 52 | // 1: X = A + B; X' = X % 2^64 |
Robin Murphy | 9085b34 | 2018-02-19 13:38:00 +0000 | [diff] [blame] | 53 | " adds %0, %3, %2\n" |
Robin Murphy | 51369e3 | 2018-02-05 15:34:18 +0000 | [diff] [blame] | 54 | // 2: Set C = 0 if X > 2^64, to guarantee X' > C in step 4 |
| 55 | " csel %1, xzr, %1, hi\n" |
| 56 | // 3: Set X' = ~0 if X >= 2^64. For X == 2^64, this decrements X' |
| 57 | // to compensate for the carry flag being set in step 4. For |
| 58 | // X > 2^64, X' merely has to remain nonzero, which it does. |
| 59 | " csinv %0, %0, xzr, cc\n" |
| 60 | // 4: For X < 2^64, this gives us X' - C - 1 <= 0, where the -1 |
| 61 | // comes from the carry in being clear. Otherwise, we are |
| 62 | // testing X' - C == 0, subject to the previous adjustments. |
| 63 | " sbcs xzr, %0, %1\n" |
| 64 | " cset %0, ls\n" |
Robin Murphy | 9085b34 | 2018-02-19 13:38:00 +0000 | [diff] [blame] | 65 | : "=&r" (ret), "+r" (limit) : "Ir" (size), "0" (addr) : "cc"); |
Robin Murphy | 51369e3 | 2018-02-05 15:34:18 +0000 | [diff] [blame] | 66 | |
Robin Murphy | 9085b34 | 2018-02-19 13:38:00 +0000 | [diff] [blame] | 67 | return ret; |
Robin Murphy | 51369e3 | 2018-02-05 15:34:18 +0000 | [diff] [blame] | 68 | } |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 69 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 70 | #define access_ok(addr, size) __range_ok(addr, size) |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 71 | |
Ard Biesheuvel | 6c94f27 | 2016-01-01 15:02:12 +0100 | [diff] [blame] | 72 | #define _ASM_EXTABLE(from, to) \ |
| 73 | " .pushsection __ex_table, \"a\"\n" \ |
| 74 | " .align 3\n" \ |
| 75 | " .long (" #from " - .), (" #to " - .)\n" \ |
| 76 | " .popsection\n" |
| 77 | |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 78 | /* |
Catalin Marinas | bd38967 | 2016-07-01 14:58:21 +0100 | [diff] [blame] | 79 | * User access enabling/disabling. |
| 80 | */ |
Catalin Marinas | 4b65a5d | 2016-07-01 16:53:00 +0100 | [diff] [blame] | 81 | #ifdef CONFIG_ARM64_SW_TTBR0_PAN |
| 82 | static inline void __uaccess_ttbr0_disable(void) |
| 83 | { |
Catalin Marinas | 6b88a32 | 2018-01-10 13:18:30 +0000 | [diff] [blame] | 84 | unsigned long flags, ttbr; |
Catalin Marinas | 4b65a5d | 2016-07-01 16:53:00 +0100 | [diff] [blame] | 85 | |
Catalin Marinas | 6b88a32 | 2018-01-10 13:18:30 +0000 | [diff] [blame] | 86 | local_irq_save(flags); |
Will Deacon | 27a921e | 2017-08-10 13:58:16 +0100 | [diff] [blame] | 87 | ttbr = read_sysreg(ttbr1_el1); |
Catalin Marinas | 6b88a32 | 2018-01-10 13:18:30 +0000 | [diff] [blame] | 88 | ttbr &= ~TTBR_ASID_MASK; |
Mark Rutland | 833be85 | 2020-11-03 10:22:29 +0000 | [diff] [blame] | 89 | /* reserved_pg_dir placed before swapper_pg_dir */ |
| 90 | write_sysreg(ttbr - PAGE_SIZE, ttbr0_el1); |
Will Deacon | 27a921e | 2017-08-10 13:58:16 +0100 | [diff] [blame] | 91 | isb(); |
| 92 | /* Set reserved ASID */ |
Will Deacon | 27a921e | 2017-08-10 13:58:16 +0100 | [diff] [blame] | 93 | write_sysreg(ttbr, ttbr1_el1); |
Catalin Marinas | 4b65a5d | 2016-07-01 16:53:00 +0100 | [diff] [blame] | 94 | isb(); |
Catalin Marinas | 6b88a32 | 2018-01-10 13:18:30 +0000 | [diff] [blame] | 95 | local_irq_restore(flags); |
Catalin Marinas | 4b65a5d | 2016-07-01 16:53:00 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static inline void __uaccess_ttbr0_enable(void) |
| 99 | { |
Will Deacon | 27a921e | 2017-08-10 13:58:16 +0100 | [diff] [blame] | 100 | unsigned long flags, ttbr0, ttbr1; |
Catalin Marinas | 4b65a5d | 2016-07-01 16:53:00 +0100 | [diff] [blame] | 101 | |
| 102 | /* |
| 103 | * Disable interrupts to avoid preemption between reading the 'ttbr0' |
| 104 | * variable and the MSR. A context switch could trigger an ASID |
| 105 | * roll-over and an update of 'ttbr0'. |
| 106 | */ |
| 107 | local_irq_save(flags); |
Catalin Marinas | 6b88a32 | 2018-01-10 13:18:30 +0000 | [diff] [blame] | 108 | ttbr0 = READ_ONCE(current_thread_info()->ttbr0); |
Will Deacon | 27a921e | 2017-08-10 13:58:16 +0100 | [diff] [blame] | 109 | |
| 110 | /* Restore active ASID */ |
| 111 | ttbr1 = read_sysreg(ttbr1_el1); |
Catalin Marinas | 6b88a32 | 2018-01-10 13:18:30 +0000 | [diff] [blame] | 112 | ttbr1 &= ~TTBR_ASID_MASK; /* safety measure */ |
Will Deacon | b519538 | 2017-12-01 17:33:48 +0000 | [diff] [blame] | 113 | ttbr1 |= ttbr0 & TTBR_ASID_MASK; |
Will Deacon | 27a921e | 2017-08-10 13:58:16 +0100 | [diff] [blame] | 114 | write_sysreg(ttbr1, ttbr1_el1); |
| 115 | isb(); |
| 116 | |
| 117 | /* Restore user page table */ |
| 118 | write_sysreg(ttbr0, ttbr0_el1); |
Catalin Marinas | 4b65a5d | 2016-07-01 16:53:00 +0100 | [diff] [blame] | 119 | isb(); |
| 120 | local_irq_restore(flags); |
| 121 | } |
| 122 | |
| 123 | static inline bool uaccess_ttbr0_disable(void) |
| 124 | { |
| 125 | if (!system_uses_ttbr0_pan()) |
| 126 | return false; |
| 127 | __uaccess_ttbr0_disable(); |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | static inline bool uaccess_ttbr0_enable(void) |
| 132 | { |
| 133 | if (!system_uses_ttbr0_pan()) |
| 134 | return false; |
| 135 | __uaccess_ttbr0_enable(); |
| 136 | return true; |
| 137 | } |
| 138 | #else |
| 139 | static inline bool uaccess_ttbr0_disable(void) |
| 140 | { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | static inline bool uaccess_ttbr0_enable(void) |
| 145 | { |
| 146 | return false; |
| 147 | } |
| 148 | #endif |
| 149 | |
James Morse | e1281f5 | 2018-01-08 15:38:11 +0000 | [diff] [blame] | 150 | static inline void __uaccess_disable_hw_pan(void) |
| 151 | { |
| 152 | asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN, |
| 153 | CONFIG_ARM64_PAN)); |
| 154 | } |
| 155 | |
| 156 | static inline void __uaccess_enable_hw_pan(void) |
| 157 | { |
| 158 | asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, |
| 159 | CONFIG_ARM64_PAN)); |
| 160 | } |
| 161 | |
Mark Rutland | 923e1e7 | 2020-12-02 13:15:50 +0000 | [diff] [blame] | 162 | static inline void uaccess_disable_privileged(void) |
Catalin Marinas | bd38967 | 2016-07-01 14:58:21 +0100 | [diff] [blame] | 163 | { |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 164 | if (uaccess_ttbr0_disable()) |
| 165 | return; |
| 166 | |
| 167 | __uaccess_enable_hw_pan(); |
Catalin Marinas | bd38967 | 2016-07-01 14:58:21 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Mark Rutland | 923e1e7 | 2020-12-02 13:15:50 +0000 | [diff] [blame] | 170 | static inline void uaccess_enable_privileged(void) |
Catalin Marinas | bd38967 | 2016-07-01 14:58:21 +0100 | [diff] [blame] | 171 | { |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 172 | if (uaccess_ttbr0_enable()) |
| 173 | return; |
Catalin Marinas | bd38967 | 2016-07-01 14:58:21 +0100 | [diff] [blame] | 174 | |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 175 | __uaccess_disable_hw_pan(); |
Catalin Marinas | bd38967 | 2016-07-01 14:58:21 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* |
Mark Rutland | 3d2403f | 2020-12-02 13:15:55 +0000 | [diff] [blame] | 179 | * Sanitise a uaccess pointer such that it becomes NULL if above the maximum |
| 180 | * user address. In case the pointer is tagged (has the top byte set), untag |
| 181 | * the pointer before checking. |
Robin Murphy | 4d8efc2 | 2018-02-05 15:34:19 +0000 | [diff] [blame] | 182 | */ |
| 183 | #define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr) |
| 184 | static inline void __user *__uaccess_mask_ptr(const void __user *ptr) |
| 185 | { |
| 186 | void __user *safe_ptr; |
| 187 | |
| 188 | asm volatile( |
Andrey Konovalov | 2b835e2 | 2019-07-23 19:58:38 +0200 | [diff] [blame] | 189 | " bics xzr, %3, %2\n" |
Robin Murphy | 4d8efc2 | 2018-02-05 15:34:19 +0000 | [diff] [blame] | 190 | " csel %0, %1, xzr, eq\n" |
| 191 | : "=&r" (safe_ptr) |
Mark Rutland | 3d2403f | 2020-12-02 13:15:55 +0000 | [diff] [blame] | 192 | : "r" (ptr), "r" (TASK_SIZE_MAX - 1), |
Andrey Konovalov | 2b835e2 | 2019-07-23 19:58:38 +0200 | [diff] [blame] | 193 | "r" (untagged_addr(ptr)) |
Robin Murphy | 4d8efc2 | 2018-02-05 15:34:19 +0000 | [diff] [blame] | 194 | : "cc"); |
| 195 | |
| 196 | csdb(); |
| 197 | return safe_ptr; |
| 198 | } |
| 199 | |
| 200 | /* |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 201 | * The "__xxx" versions of the user access functions do not verify the address |
| 202 | * space - it must have been done previously with a separate "access_ok()" |
| 203 | * call. |
| 204 | * |
| 205 | * The "__xxx_error" versions set the third argument to -EFAULT if an error |
| 206 | * occurs, and leave it unchanged on success. |
| 207 | */ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 208 | #define __get_mem_asm(load, reg, x, addr, err) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 209 | asm volatile( \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 210 | "1: " load " " reg "1, [%2]\n" \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 211 | "2:\n" \ |
| 212 | " .section .fixup, \"ax\"\n" \ |
| 213 | " .align 2\n" \ |
| 214 | "3: mov %w0, %3\n" \ |
| 215 | " mov %1, #0\n" \ |
| 216 | " b 2b\n" \ |
| 217 | " .previous\n" \ |
Ard Biesheuvel | 6c94f27 | 2016-01-01 15:02:12 +0100 | [diff] [blame] | 218 | _ASM_EXTABLE(1b, 3b) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 219 | : "+r" (err), "=&r" (x) \ |
| 220 | : "r" (addr), "i" (-EFAULT)) |
| 221 | |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 222 | #define __raw_get_mem(ldr, x, ptr, err) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 223 | do { \ |
| 224 | unsigned long __gu_val; \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 225 | switch (sizeof(*(ptr))) { \ |
| 226 | case 1: \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 227 | __get_mem_asm(ldr "b", "%w", __gu_val, (ptr), (err)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 228 | break; \ |
| 229 | case 2: \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 230 | __get_mem_asm(ldr "h", "%w", __gu_val, (ptr), (err)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 231 | break; \ |
| 232 | case 4: \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 233 | __get_mem_asm(ldr, "%w", __gu_val, (ptr), (err)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 234 | break; \ |
| 235 | case 8: \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 236 | __get_mem_asm(ldr, "%x", __gu_val, (ptr), (err)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 237 | break; \ |
| 238 | default: \ |
| 239 | BUILD_BUG(); \ |
| 240 | } \ |
Michael S. Tsirkin | 58fff51 | 2014-12-12 01:56:04 +0200 | [diff] [blame] | 241 | (x) = (__force __typeof__(*(ptr)))__gu_val; \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 242 | } while (0) |
| 243 | |
Mark Rutland | f253d82 | 2020-12-02 13:15:52 +0000 | [diff] [blame] | 244 | #define __raw_get_user(x, ptr, err) \ |
| 245 | do { \ |
| 246 | __chk_user_ptr(ptr); \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 247 | uaccess_ttbr0_enable(); \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 248 | __raw_get_mem("ldtr", x, ptr, err); \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 249 | uaccess_ttbr0_disable(); \ |
Mark Rutland | f253d82 | 2020-12-02 13:15:52 +0000 | [diff] [blame] | 250 | } while (0) |
| 251 | |
Julien Thierry | 13e4cdd | 2019-01-15 13:58:26 +0000 | [diff] [blame] | 252 | #define __get_user_error(x, ptr, err) \ |
| 253 | do { \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 254 | __typeof__(*(ptr)) __user *__p = (ptr); \ |
| 255 | might_fault(); \ |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 256 | if (access_ok(__p, sizeof(*__p))) { \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 257 | __p = uaccess_mask_ptr(__p); \ |
Catalin Marinas | 3cd0ddb | 2019-03-01 14:19:06 +0000 | [diff] [blame] | 258 | __raw_get_user((x), __p, (err)); \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 259 | } else { \ |
Al Viro | 8cfb347 | 2020-05-22 15:23:21 +0100 | [diff] [blame] | 260 | (x) = (__force __typeof__(x))0; (err) = -EFAULT; \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 261 | } \ |
Julien Thierry | 13e4cdd | 2019-01-15 13:58:26 +0000 | [diff] [blame] | 262 | } while (0) |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 263 | |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 264 | #define __get_user(x, ptr) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 265 | ({ \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 266 | int __gu_err = 0; \ |
Julien Thierry | 13e4cdd | 2019-01-15 13:58:26 +0000 | [diff] [blame] | 267 | __get_user_error((x), (ptr), __gu_err); \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 268 | __gu_err; \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 269 | }) |
| 270 | |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 271 | #define get_user __get_user |
| 272 | |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 273 | #define __get_kernel_nofault(dst, src, type, err_label) \ |
| 274 | do { \ |
| 275 | int __gkn_err = 0; \ |
| 276 | \ |
| 277 | __raw_get_mem("ldr", *((type *)(dst)), \ |
| 278 | (__force type *)(src), __gkn_err); \ |
| 279 | if (unlikely(__gkn_err)) \ |
| 280 | goto err_label; \ |
| 281 | } while (0) |
| 282 | |
| 283 | #define __put_mem_asm(store, reg, x, addr, err) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 284 | asm volatile( \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 285 | "1: " store " " reg "1, [%2]\n" \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 286 | "2:\n" \ |
| 287 | " .section .fixup,\"ax\"\n" \ |
| 288 | " .align 2\n" \ |
| 289 | "3: mov %w0, %3\n" \ |
| 290 | " b 2b\n" \ |
| 291 | " .previous\n" \ |
Ard Biesheuvel | 6c94f27 | 2016-01-01 15:02:12 +0100 | [diff] [blame] | 292 | _ASM_EXTABLE(1b, 3b) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 293 | : "+r" (err) \ |
| 294 | : "r" (x), "r" (addr), "i" (-EFAULT)) |
| 295 | |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 296 | #define __raw_put_mem(str, x, ptr, err) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 297 | do { \ |
| 298 | __typeof__(*(ptr)) __pu_val = (x); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 299 | switch (sizeof(*(ptr))) { \ |
| 300 | case 1: \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 301 | __put_mem_asm(str "b", "%w", __pu_val, (ptr), (err)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 302 | break; \ |
| 303 | case 2: \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 304 | __put_mem_asm(str "h", "%w", __pu_val, (ptr), (err)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 305 | break; \ |
| 306 | case 4: \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 307 | __put_mem_asm(str, "%w", __pu_val, (ptr), (err)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 308 | break; \ |
| 309 | case 8: \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 310 | __put_mem_asm(str, "%x", __pu_val, (ptr), (err)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 311 | break; \ |
| 312 | default: \ |
| 313 | BUILD_BUG(); \ |
| 314 | } \ |
Mark Rutland | f253d82 | 2020-12-02 13:15:52 +0000 | [diff] [blame] | 315 | } while (0) |
| 316 | |
| 317 | #define __raw_put_user(x, ptr, err) \ |
| 318 | do { \ |
| 319 | __chk_user_ptr(ptr); \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 320 | uaccess_ttbr0_enable(); \ |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 321 | __raw_put_mem("sttr", x, ptr, err); \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 322 | uaccess_ttbr0_disable(); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 323 | } while (0) |
| 324 | |
Julien Thierry | 13e4cdd | 2019-01-15 13:58:26 +0000 | [diff] [blame] | 325 | #define __put_user_error(x, ptr, err) \ |
| 326 | do { \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 327 | __typeof__(*(ptr)) __user *__p = (ptr); \ |
| 328 | might_fault(); \ |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 329 | if (access_ok(__p, sizeof(*__p))) { \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 330 | __p = uaccess_mask_ptr(__p); \ |
Catalin Marinas | 3cd0ddb | 2019-03-01 14:19:06 +0000 | [diff] [blame] | 331 | __raw_put_user((x), __p, (err)); \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 332 | } else { \ |
| 333 | (err) = -EFAULT; \ |
| 334 | } \ |
Julien Thierry | 13e4cdd | 2019-01-15 13:58:26 +0000 | [diff] [blame] | 335 | } while (0) |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 336 | |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 337 | #define __put_user(x, ptr) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 338 | ({ \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 339 | int __pu_err = 0; \ |
Julien Thierry | 13e4cdd | 2019-01-15 13:58:26 +0000 | [diff] [blame] | 340 | __put_user_error((x), (ptr), __pu_err); \ |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 341 | __pu_err; \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 342 | }) |
| 343 | |
Will Deacon | 8462408 | 2018-02-05 15:34:22 +0000 | [diff] [blame] | 344 | #define put_user __put_user |
| 345 | |
Mark Rutland | fc703d8 | 2020-12-02 13:15:53 +0000 | [diff] [blame] | 346 | #define __put_kernel_nofault(dst, src, type, err_label) \ |
| 347 | do { \ |
| 348 | int __pkn_err = 0; \ |
| 349 | \ |
| 350 | __raw_put_mem("str", *((type *)(src)), \ |
| 351 | (__force type *)(dst), __pkn_err); \ |
| 352 | if (unlikely(__pkn_err)) \ |
| 353 | goto err_label; \ |
| 354 | } while(0) |
| 355 | |
Yang Shi | bffe1ba | 2016-06-08 14:40:56 -0700 | [diff] [blame] | 356 | extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n); |
Will Deacon | f71c2ff | 2018-02-05 15:34:23 +0000 | [diff] [blame] | 357 | #define raw_copy_from_user(to, from, n) \ |
| 358 | ({ \ |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 359 | unsigned long __acfu_ret; \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 360 | uaccess_ttbr0_enable(); \ |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 361 | __acfu_ret = __arch_copy_from_user((to), \ |
| 362 | __uaccess_mask_ptr(from), (n)); \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 363 | uaccess_ttbr0_disable(); \ |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 364 | __acfu_ret; \ |
Will Deacon | f71c2ff | 2018-02-05 15:34:23 +0000 | [diff] [blame] | 365 | }) |
| 366 | |
Yang Shi | bffe1ba | 2016-06-08 14:40:56 -0700 | [diff] [blame] | 367 | extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n); |
Will Deacon | f71c2ff | 2018-02-05 15:34:23 +0000 | [diff] [blame] | 368 | #define raw_copy_to_user(to, from, n) \ |
| 369 | ({ \ |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 370 | unsigned long __actu_ret; \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 371 | uaccess_ttbr0_enable(); \ |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 372 | __actu_ret = __arch_copy_to_user(__uaccess_mask_ptr(to), \ |
| 373 | (from), (n)); \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 374 | uaccess_ttbr0_disable(); \ |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 375 | __actu_ret; \ |
Will Deacon | f71c2ff | 2018-02-05 15:34:23 +0000 | [diff] [blame] | 376 | }) |
| 377 | |
| 378 | extern unsigned long __must_check __arch_copy_in_user(void __user *to, const void __user *from, unsigned long n); |
| 379 | #define raw_copy_in_user(to, from, n) \ |
| 380 | ({ \ |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 381 | unsigned long __aciu_ret; \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 382 | uaccess_ttbr0_enable(); \ |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 383 | __aciu_ret = __arch_copy_in_user(__uaccess_mask_ptr(to), \ |
| 384 | __uaccess_mask_ptr(from), (n)); \ |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 385 | uaccess_ttbr0_disable(); \ |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 386 | __aciu_ret; \ |
Will Deacon | f71c2ff | 2018-02-05 15:34:23 +0000 | [diff] [blame] | 387 | }) |
| 388 | |
Al Viro | 92430da | 2017-03-21 08:40:57 -0400 | [diff] [blame] | 389 | #define INLINE_COPY_TO_USER |
| 390 | #define INLINE_COPY_FROM_USER |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 391 | |
Will Deacon | f71c2ff | 2018-02-05 15:34:23 +0000 | [diff] [blame] | 392 | extern unsigned long __must_check __arch_clear_user(void __user *to, unsigned long n); |
| 393 | static inline unsigned long __must_check __clear_user(void __user *to, unsigned long n) |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 394 | { |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 395 | if (access_ok(to, n)) { |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 396 | uaccess_ttbr0_enable(); |
Will Deacon | f71c2ff | 2018-02-05 15:34:23 +0000 | [diff] [blame] | 397 | n = __arch_clear_user(__uaccess_mask_ptr(to), n); |
Mark Rutland | 7cf283c | 2020-12-02 13:15:57 +0000 | [diff] [blame] | 398 | uaccess_ttbr0_disable(); |
Pavel Tatashin | e50be64 | 2019-11-20 12:07:40 -0500 | [diff] [blame] | 399 | } |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 400 | return n; |
| 401 | } |
Will Deacon | f71c2ff | 2018-02-05 15:34:23 +0000 | [diff] [blame] | 402 | #define clear_user __clear_user |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 403 | |
Will Deacon | 12a0ef7 | 2013-11-06 17:20:22 +0000 | [diff] [blame] | 404 | extern long strncpy_from_user(char *dest, const char __user *src, long count); |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 405 | |
Will Deacon | 12a0ef7 | 2013-11-06 17:20:22 +0000 | [diff] [blame] | 406 | extern __must_check long strnlen_user(const char __user *str, long n); |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 407 | |
Robin Murphy | 5d7bdeb | 2017-07-25 11:55:43 +0100 | [diff] [blame] | 408 | #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE |
| 409 | struct page; |
| 410 | void memcpy_page_flushcache(char *to, struct page *page, size_t offset, size_t len); |
| 411 | extern unsigned long __must_check __copy_user_flushcache(void *to, const void __user *from, unsigned long n); |
| 412 | |
| 413 | static inline int __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size) |
| 414 | { |
| 415 | kasan_check_write(dst, size); |
Will Deacon | f71c2ff | 2018-02-05 15:34:23 +0000 | [diff] [blame] | 416 | return __copy_user_flushcache(dst, __uaccess_mask_ptr(src), size); |
Robin Murphy | 5d7bdeb | 2017-07-25 11:55:43 +0100 | [diff] [blame] | 417 | } |
| 418 | #endif |
| 419 | |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 420 | #endif /* __ASM_UACCESS_H */ |