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