Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Based on arch/arm/include/asm/uaccess.h |
| 3 | * |
| 4 | * Copyright (C) 2012 ARM Ltd. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #ifndef __ASM_UACCESS_H |
| 19 | #define __ASM_UACCESS_H |
| 20 | |
| 21 | /* |
| 22 | * User space memory access functions |
| 23 | */ |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/thread_info.h> |
| 26 | |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 27 | #include <asm/alternative.h> |
| 28 | #include <asm/cpufeature.h> |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 29 | #include <asm/ptrace.h> |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 30 | #include <asm/sysreg.h> |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 31 | #include <asm/errno.h> |
| 32 | #include <asm/memory.h> |
| 33 | #include <asm/compiler.h> |
| 34 | |
| 35 | #define VERIFY_READ 0 |
| 36 | #define VERIFY_WRITE 1 |
| 37 | |
| 38 | /* |
| 39 | * The exception table consists of pairs of addresses: the first is the |
| 40 | * address of an instruction that is allowed to fault, and the second is |
| 41 | * the address at which the program should continue. No registers are |
| 42 | * modified, so it is entirely up to the continuation code to figure out |
| 43 | * what to do. |
| 44 | * |
| 45 | * All the routines below use bits of fixup code that are out of line |
| 46 | * with the main instruction path. This means when everything is well, |
| 47 | * we don't even have to jump over them. Further, they do not intrude |
| 48 | * on our cache or tlb entries. |
| 49 | */ |
| 50 | |
| 51 | struct exception_table_entry |
| 52 | { |
| 53 | unsigned long insn, fixup; |
| 54 | }; |
| 55 | |
| 56 | extern int fixup_exception(struct pt_regs *regs); |
| 57 | |
| 58 | #define KERNEL_DS (-1UL) |
| 59 | #define get_ds() (KERNEL_DS) |
| 60 | |
| 61 | #define USER_DS TASK_SIZE_64 |
| 62 | #define get_fs() (current_thread_info()->addr_limit) |
| 63 | |
| 64 | static inline void set_fs(mm_segment_t fs) |
| 65 | { |
| 66 | current_thread_info()->addr_limit = fs; |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 67 | |
| 68 | /* |
| 69 | * Enable/disable UAO so that copy_to_user() etc can access |
| 70 | * kernel memory with the unprivileged instructions. |
| 71 | */ |
| 72 | if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS) |
| 73 | asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO)); |
| 74 | else |
| 75 | asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO, |
| 76 | CONFIG_ARM64_UAO)); |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Michael S. Tsirkin | 967f0e5 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 79 | #define segment_eq(a, b) ((a) == (b)) |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * Return 1 if addr < current->addr_limit, 0 otherwise. |
| 83 | */ |
| 84 | #define __addr_ok(addr) \ |
| 85 | ({ \ |
| 86 | unsigned long flag; \ |
| 87 | asm("cmp %1, %0; cset %0, lo" \ |
| 88 | : "=&r" (flag) \ |
| 89 | : "r" (addr), "0" (current_thread_info()->addr_limit) \ |
| 90 | : "cc"); \ |
| 91 | flag; \ |
| 92 | }) |
| 93 | |
| 94 | /* |
| 95 | * Test whether a block of memory is a valid user space address. |
| 96 | * Returns 1 if the range is valid, 0 otherwise. |
| 97 | * |
| 98 | * This is equivalent to the following test: |
Christopher Covington | 31b1e94 | 2014-03-19 16:29:37 +0000 | [diff] [blame] | 99 | * (u65)addr + (u65)size <= current->addr_limit |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 100 | * |
| 101 | * This needs 65-bit arithmetic. |
| 102 | */ |
| 103 | #define __range_ok(addr, size) \ |
| 104 | ({ \ |
| 105 | unsigned long flag, roksum; \ |
| 106 | __chk_user_ptr(addr); \ |
Christopher Covington | 31b1e94 | 2014-03-19 16:29:37 +0000 | [diff] [blame] | 107 | asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 108 | : "=&r" (flag), "=&r" (roksum) \ |
| 109 | : "1" (addr), "Ir" (size), \ |
| 110 | "r" (current_thread_info()->addr_limit) \ |
| 111 | : "cc"); \ |
| 112 | flag; \ |
| 113 | }) |
| 114 | |
| 115 | #define access_ok(type, addr, size) __range_ok(addr, size) |
Will Deacon | 12a0ef7 | 2013-11-06 17:20:22 +0000 | [diff] [blame] | 116 | #define user_addr_max get_fs |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * The "__xxx" versions of the user access functions do not verify the address |
| 120 | * space - it must have been done previously with a separate "access_ok()" |
| 121 | * call. |
| 122 | * |
| 123 | * The "__xxx_error" versions set the third argument to -EFAULT if an error |
| 124 | * occurs, and leave it unchanged on success. |
| 125 | */ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 126 | #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 127 | asm volatile( \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 128 | "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \ |
| 129 | alt_instr " " reg "1, [%2]\n", feature) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 130 | "2:\n" \ |
| 131 | " .section .fixup, \"ax\"\n" \ |
| 132 | " .align 2\n" \ |
| 133 | "3: mov %w0, %3\n" \ |
| 134 | " mov %1, #0\n" \ |
| 135 | " b 2b\n" \ |
| 136 | " .previous\n" \ |
| 137 | " .section __ex_table,\"a\"\n" \ |
| 138 | " .align 3\n" \ |
| 139 | " .quad 1b, 3b\n" \ |
| 140 | " .previous" \ |
| 141 | : "+r" (err), "=&r" (x) \ |
| 142 | : "r" (addr), "i" (-EFAULT)) |
| 143 | |
| 144 | #define __get_user_err(x, ptr, err) \ |
| 145 | do { \ |
| 146 | unsigned long __gu_val; \ |
| 147 | __chk_user_ptr(ptr); \ |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 148 | asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN, \ |
| 149 | CONFIG_ARM64_PAN)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 150 | switch (sizeof(*(ptr))) { \ |
| 151 | case 1: \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 152 | __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \ |
| 153 | (err), ARM64_HAS_UAO); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 154 | break; \ |
| 155 | case 2: \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 156 | __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \ |
| 157 | (err), ARM64_HAS_UAO); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 158 | break; \ |
| 159 | case 4: \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 160 | __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \ |
| 161 | (err), ARM64_HAS_UAO); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 162 | break; \ |
| 163 | case 8: \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 164 | __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \ |
| 165 | (err), ARM64_HAS_UAO); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 166 | break; \ |
| 167 | default: \ |
| 168 | BUILD_BUG(); \ |
| 169 | } \ |
Michael S. Tsirkin | 58fff51 | 2014-12-12 01:56:04 +0200 | [diff] [blame] | 170 | (x) = (__force __typeof__(*(ptr)))__gu_val; \ |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 171 | asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, \ |
| 172 | CONFIG_ARM64_PAN)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 173 | } while (0) |
| 174 | |
| 175 | #define __get_user(x, ptr) \ |
| 176 | ({ \ |
| 177 | int __gu_err = 0; \ |
| 178 | __get_user_err((x), (ptr), __gu_err); \ |
| 179 | __gu_err; \ |
| 180 | }) |
| 181 | |
| 182 | #define __get_user_error(x, ptr, err) \ |
| 183 | ({ \ |
| 184 | __get_user_err((x), (ptr), (err)); \ |
| 185 | (void)0; \ |
| 186 | }) |
| 187 | |
| 188 | #define __get_user_unaligned __get_user |
| 189 | |
| 190 | #define get_user(x, ptr) \ |
| 191 | ({ \ |
AKASHI Takahiro | 1f65c13 | 2013-09-24 10:00:50 +0100 | [diff] [blame] | 192 | __typeof__(*(ptr)) __user *__p = (ptr); \ |
Michael S. Tsirkin | 56d2ef7 | 2013-05-26 17:30:42 +0300 | [diff] [blame] | 193 | might_fault(); \ |
AKASHI Takahiro | 1f65c13 | 2013-09-24 10:00:50 +0100 | [diff] [blame] | 194 | access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \ |
| 195 | __get_user((x), __p) : \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 196 | ((x) = 0, -EFAULT); \ |
| 197 | }) |
| 198 | |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 199 | #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 200 | asm volatile( \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 201 | "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \ |
| 202 | alt_instr " " reg "1, [%2]\n", feature) \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 203 | "2:\n" \ |
| 204 | " .section .fixup,\"ax\"\n" \ |
| 205 | " .align 2\n" \ |
| 206 | "3: mov %w0, %3\n" \ |
| 207 | " b 2b\n" \ |
| 208 | " .previous\n" \ |
| 209 | " .section __ex_table,\"a\"\n" \ |
| 210 | " .align 3\n" \ |
| 211 | " .quad 1b, 3b\n" \ |
| 212 | " .previous" \ |
| 213 | : "+r" (err) \ |
| 214 | : "r" (x), "r" (addr), "i" (-EFAULT)) |
| 215 | |
| 216 | #define __put_user_err(x, ptr, err) \ |
| 217 | do { \ |
| 218 | __typeof__(*(ptr)) __pu_val = (x); \ |
| 219 | __chk_user_ptr(ptr); \ |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 220 | asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN, \ |
| 221 | CONFIG_ARM64_PAN)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 222 | switch (sizeof(*(ptr))) { \ |
| 223 | case 1: \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 224 | __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \ |
| 225 | (err), ARM64_HAS_UAO); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 226 | break; \ |
| 227 | case 2: \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 228 | __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \ |
| 229 | (err), ARM64_HAS_UAO); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 230 | break; \ |
| 231 | case 4: \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 232 | __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \ |
| 233 | (err), ARM64_HAS_UAO); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 234 | break; \ |
| 235 | case 8: \ |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame^] | 236 | __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \ |
| 237 | (err), ARM64_HAS_UAO); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 238 | break; \ |
| 239 | default: \ |
| 240 | BUILD_BUG(); \ |
| 241 | } \ |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 242 | asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, \ |
| 243 | CONFIG_ARM64_PAN)); \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 244 | } while (0) |
| 245 | |
| 246 | #define __put_user(x, ptr) \ |
| 247 | ({ \ |
| 248 | int __pu_err = 0; \ |
| 249 | __put_user_err((x), (ptr), __pu_err); \ |
| 250 | __pu_err; \ |
| 251 | }) |
| 252 | |
| 253 | #define __put_user_error(x, ptr, err) \ |
| 254 | ({ \ |
| 255 | __put_user_err((x), (ptr), (err)); \ |
| 256 | (void)0; \ |
| 257 | }) |
| 258 | |
| 259 | #define __put_user_unaligned __put_user |
| 260 | |
| 261 | #define put_user(x, ptr) \ |
| 262 | ({ \ |
AKASHI Takahiro | 1f65c13 | 2013-09-24 10:00:50 +0100 | [diff] [blame] | 263 | __typeof__(*(ptr)) __user *__p = (ptr); \ |
Michael S. Tsirkin | 56d2ef7 | 2013-05-26 17:30:42 +0300 | [diff] [blame] | 264 | might_fault(); \ |
AKASHI Takahiro | 1f65c13 | 2013-09-24 10:00:50 +0100 | [diff] [blame] | 265 | access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \ |
| 266 | __put_user((x), __p) : \ |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 267 | -EFAULT; \ |
| 268 | }) |
| 269 | |
| 270 | extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n); |
| 271 | extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n); |
| 272 | extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n); |
| 273 | extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n); |
| 274 | |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 275 | static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n) |
| 276 | { |
| 277 | if (access_ok(VERIFY_READ, from, n)) |
| 278 | n = __copy_from_user(to, from, n); |
| 279 | else /* security hole - plug it */ |
| 280 | memset(to, 0, n); |
| 281 | return n; |
| 282 | } |
| 283 | |
| 284 | static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n) |
| 285 | { |
| 286 | if (access_ok(VERIFY_WRITE, to, n)) |
| 287 | n = __copy_to_user(to, from, n); |
| 288 | return n; |
| 289 | } |
| 290 | |
| 291 | static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n) |
| 292 | { |
| 293 | if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n)) |
| 294 | n = __copy_in_user(to, from, n); |
| 295 | return n; |
| 296 | } |
| 297 | |
| 298 | #define __copy_to_user_inatomic __copy_to_user |
| 299 | #define __copy_from_user_inatomic __copy_from_user |
| 300 | |
| 301 | static inline unsigned long __must_check clear_user(void __user *to, unsigned long n) |
| 302 | { |
| 303 | if (access_ok(VERIFY_WRITE, to, n)) |
| 304 | n = __clear_user(to, n); |
| 305 | return n; |
| 306 | } |
| 307 | |
Will Deacon | 12a0ef7 | 2013-11-06 17:20:22 +0000 | [diff] [blame] | 308 | 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] | 309 | |
Will Deacon | 12a0ef7 | 2013-11-06 17:20:22 +0000 | [diff] [blame] | 310 | extern __must_check long strlen_user(const char __user *str); |
| 311 | extern __must_check long strnlen_user(const char __user *str, long n); |
Catalin Marinas | 0aea86a | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 312 | |
| 313 | #endif /* __ASM_UACCESS_H */ |