| .. | .. |
|---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (C) 2012 Regents of the University of California |
|---|
| 3 | | - * |
|---|
| 4 | | - * This program is free software; you can redistribute it and/or |
|---|
| 5 | | - * modify it under the terms of the GNU General Public License |
|---|
| 6 | | - * as published by the Free Software Foundation, version 2. |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 9 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | | - * GNU General Public License for more details. |
|---|
| 12 | 4 | * |
|---|
| 13 | 5 | * This file was copied from include/asm-generic/uaccess.h |
|---|
| 14 | 6 | */ |
|---|
| .. | .. |
|---|
| 16 | 8 | #ifndef _ASM_RISCV_UACCESS_H |
|---|
| 17 | 9 | #define _ASM_RISCV_UACCESS_H |
|---|
| 18 | 10 | |
|---|
| 11 | +#include <asm/pgtable.h> /* for TASK_SIZE */ |
|---|
| 12 | + |
|---|
| 19 | 13 | /* |
|---|
| 20 | 14 | * User space memory access functions |
|---|
| 21 | 15 | */ |
|---|
| 16 | +#ifdef CONFIG_MMU |
|---|
| 22 | 17 | #include <linux/errno.h> |
|---|
| 23 | 18 | #include <linux/compiler.h> |
|---|
| 24 | 19 | #include <linux/thread_info.h> |
|---|
| 25 | 20 | #include <asm/byteorder.h> |
|---|
| 21 | +#include <asm/extable.h> |
|---|
| 26 | 22 | #include <asm/asm.h> |
|---|
| 27 | 23 | |
|---|
| 28 | 24 | #define __enable_user_access() \ |
|---|
| .. | .. |
|---|
| 30 | 26 | #define __disable_user_access() \ |
|---|
| 31 | 27 | __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory") |
|---|
| 32 | 28 | |
|---|
| 33 | | -/* |
|---|
| 34 | | - * The fs value determines whether argument validity checking should be |
|---|
| 35 | | - * performed or not. If get_fs() == USER_DS, checking is performed, with |
|---|
| 36 | | - * get_fs() == KERNEL_DS, checking is bypassed. |
|---|
| 37 | | - * |
|---|
| 38 | | - * For historical reasons, these macros are grossly misnamed. |
|---|
| 39 | | - */ |
|---|
| 40 | | - |
|---|
| 41 | | -#define KERNEL_DS (~0UL) |
|---|
| 42 | | -#define USER_DS (TASK_SIZE) |
|---|
| 43 | | - |
|---|
| 44 | | -#define get_ds() (KERNEL_DS) |
|---|
| 45 | | -#define get_fs() (current_thread_info()->addr_limit) |
|---|
| 46 | | - |
|---|
| 47 | | -static inline void set_fs(mm_segment_t fs) |
|---|
| 48 | | -{ |
|---|
| 49 | | - current_thread_info()->addr_limit = fs; |
|---|
| 50 | | -} |
|---|
| 51 | | - |
|---|
| 52 | | -#define segment_eq(a, b) ((a) == (b)) |
|---|
| 53 | | - |
|---|
| 54 | | -#define user_addr_max() (get_fs()) |
|---|
| 55 | | - |
|---|
| 56 | | - |
|---|
| 57 | | -#define VERIFY_READ 0 |
|---|
| 58 | | -#define VERIFY_WRITE 1 |
|---|
| 59 | | - |
|---|
| 60 | 29 | /** |
|---|
| 61 | 30 | * access_ok: - Checks if a user space pointer is valid |
|---|
| 62 | | - * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that |
|---|
| 63 | | - * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe |
|---|
| 64 | | - * to write to a block, it is always safe to read from it. |
|---|
| 65 | 31 | * @addr: User space pointer to start of block to check |
|---|
| 66 | 32 | * @size: Size of block to check |
|---|
| 67 | 33 | * |
|---|
| .. | .. |
|---|
| 76 | 42 | * checks that the pointer is in the user space range - after calling |
|---|
| 77 | 43 | * this function, memory access functions may still return -EFAULT. |
|---|
| 78 | 44 | */ |
|---|
| 79 | | -#define access_ok(type, addr, size) ({ \ |
|---|
| 45 | +#define access_ok(addr, size) ({ \ |
|---|
| 80 | 46 | __chk_user_ptr(addr); \ |
|---|
| 81 | 47 | likely(__access_ok((unsigned long __force)(addr), (size))); \ |
|---|
| 82 | 48 | }) |
|---|
| .. | .. |
|---|
| 87 | 53 | */ |
|---|
| 88 | 54 | static inline int __access_ok(unsigned long addr, unsigned long size) |
|---|
| 89 | 55 | { |
|---|
| 90 | | - const mm_segment_t fs = get_fs(); |
|---|
| 91 | | - |
|---|
| 92 | | - return (size <= fs) && (addr <= (fs - size)); |
|---|
| 56 | + return size <= TASK_SIZE && addr <= TASK_SIZE - size; |
|---|
| 93 | 57 | } |
|---|
| 94 | 58 | |
|---|
| 95 | 59 | /* |
|---|
| .. | .. |
|---|
| 105 | 69 | * on our cache or tlb entries. |
|---|
| 106 | 70 | */ |
|---|
| 107 | 71 | |
|---|
| 108 | | -struct exception_table_entry { |
|---|
| 109 | | - unsigned long insn, fixup; |
|---|
| 110 | | -}; |
|---|
| 111 | | - |
|---|
| 112 | | -extern int fixup_exception(struct pt_regs *state); |
|---|
| 113 | | - |
|---|
| 114 | | -#if defined(__LITTLE_ENDIAN) |
|---|
| 115 | | -#define __MSW 1 |
|---|
| 116 | 72 | #define __LSW 0 |
|---|
| 117 | | -#elif defined(__BIG_ENDIAN) |
|---|
| 118 | | -#define __MSW 0 |
|---|
| 119 | | -#define __LSW 1 |
|---|
| 120 | | -#else |
|---|
| 121 | | -#error "Unknown endianness" |
|---|
| 122 | | -#endif |
|---|
| 73 | +#define __MSW 1 |
|---|
| 123 | 74 | |
|---|
| 124 | 75 | /* |
|---|
| 125 | 76 | * The "__xxx" versions of the user access functions do not verify the address |
|---|
| .. | .. |
|---|
| 131 | 82 | do { \ |
|---|
| 132 | 83 | uintptr_t __tmp; \ |
|---|
| 133 | 84 | __typeof__(x) __x; \ |
|---|
| 134 | | - __enable_user_access(); \ |
|---|
| 135 | 85 | __asm__ __volatile__ ( \ |
|---|
| 136 | 86 | "1:\n" \ |
|---|
| 137 | 87 | " " insn " %1, %3\n" \ |
|---|
| .. | .. |
|---|
| 149 | 99 | " .previous" \ |
|---|
| 150 | 100 | : "+r" (err), "=&r" (__x), "=r" (__tmp) \ |
|---|
| 151 | 101 | : "m" (*(ptr)), "i" (-EFAULT)); \ |
|---|
| 152 | | - __disable_user_access(); \ |
|---|
| 153 | 102 | (x) = __x; \ |
|---|
| 154 | 103 | } while (0) |
|---|
| 155 | 104 | |
|---|
| .. | .. |
|---|
| 162 | 111 | u32 __user *__ptr = (u32 __user *)(ptr); \ |
|---|
| 163 | 112 | u32 __lo, __hi; \ |
|---|
| 164 | 113 | uintptr_t __tmp; \ |
|---|
| 165 | | - __enable_user_access(); \ |
|---|
| 166 | 114 | __asm__ __volatile__ ( \ |
|---|
| 167 | 115 | "1:\n" \ |
|---|
| 168 | 116 | " lw %1, %4\n" \ |
|---|
| .. | .. |
|---|
| 186 | 134 | "=r" (__tmp) \ |
|---|
| 187 | 135 | : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \ |
|---|
| 188 | 136 | "i" (-EFAULT)); \ |
|---|
| 189 | | - __disable_user_access(); \ |
|---|
| 190 | 137 | (x) = (__typeof__(x))((__typeof__((x)-(x)))( \ |
|---|
| 191 | 138 | (((u64)__hi << 32) | __lo))); \ |
|---|
| 192 | 139 | } while (0) |
|---|
| 193 | 140 | #endif /* CONFIG_64BIT */ |
|---|
| 194 | 141 | |
|---|
| 142 | +#define __get_user_nocheck(x, __gu_ptr, __gu_err) \ |
|---|
| 143 | +do { \ |
|---|
| 144 | + switch (sizeof(*__gu_ptr)) { \ |
|---|
| 145 | + case 1: \ |
|---|
| 146 | + __get_user_asm("lb", (x), __gu_ptr, __gu_err); \ |
|---|
| 147 | + break; \ |
|---|
| 148 | + case 2: \ |
|---|
| 149 | + __get_user_asm("lh", (x), __gu_ptr, __gu_err); \ |
|---|
| 150 | + break; \ |
|---|
| 151 | + case 4: \ |
|---|
| 152 | + __get_user_asm("lw", (x), __gu_ptr, __gu_err); \ |
|---|
| 153 | + break; \ |
|---|
| 154 | + case 8: \ |
|---|
| 155 | + __get_user_8((x), __gu_ptr, __gu_err); \ |
|---|
| 156 | + break; \ |
|---|
| 157 | + default: \ |
|---|
| 158 | + BUILD_BUG(); \ |
|---|
| 159 | + } \ |
|---|
| 160 | +} while (0) |
|---|
| 195 | 161 | |
|---|
| 196 | 162 | /** |
|---|
| 197 | 163 | * __get_user: - Get a simple variable from user space, with less checking. |
|---|
| .. | .. |
|---|
| 215 | 181 | */ |
|---|
| 216 | 182 | #define __get_user(x, ptr) \ |
|---|
| 217 | 183 | ({ \ |
|---|
| 218 | | - register long __gu_err = 0; \ |
|---|
| 219 | 184 | const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ |
|---|
| 185 | + long __gu_err = 0; \ |
|---|
| 186 | + \ |
|---|
| 220 | 187 | __chk_user_ptr(__gu_ptr); \ |
|---|
| 221 | | - switch (sizeof(*__gu_ptr)) { \ |
|---|
| 222 | | - case 1: \ |
|---|
| 223 | | - __get_user_asm("lb", (x), __gu_ptr, __gu_err); \ |
|---|
| 224 | | - break; \ |
|---|
| 225 | | - case 2: \ |
|---|
| 226 | | - __get_user_asm("lh", (x), __gu_ptr, __gu_err); \ |
|---|
| 227 | | - break; \ |
|---|
| 228 | | - case 4: \ |
|---|
| 229 | | - __get_user_asm("lw", (x), __gu_ptr, __gu_err); \ |
|---|
| 230 | | - break; \ |
|---|
| 231 | | - case 8: \ |
|---|
| 232 | | - __get_user_8((x), __gu_ptr, __gu_err); \ |
|---|
| 233 | | - break; \ |
|---|
| 234 | | - default: \ |
|---|
| 235 | | - BUILD_BUG(); \ |
|---|
| 236 | | - } \ |
|---|
| 188 | + \ |
|---|
| 189 | + __enable_user_access(); \ |
|---|
| 190 | + __get_user_nocheck(x, __gu_ptr, __gu_err); \ |
|---|
| 191 | + __disable_user_access(); \ |
|---|
| 192 | + \ |
|---|
| 237 | 193 | __gu_err; \ |
|---|
| 238 | 194 | }) |
|---|
| 239 | 195 | |
|---|
| .. | .. |
|---|
| 258 | 214 | ({ \ |
|---|
| 259 | 215 | const __typeof__(*(ptr)) __user *__p = (ptr); \ |
|---|
| 260 | 216 | might_fault(); \ |
|---|
| 261 | | - access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \ |
|---|
| 217 | + access_ok(__p, sizeof(*__p)) ? \ |
|---|
| 262 | 218 | __get_user((x), __p) : \ |
|---|
| 263 | | - ((x) = 0, -EFAULT); \ |
|---|
| 219 | + ((x) = (__force __typeof__(x))0, -EFAULT); \ |
|---|
| 264 | 220 | }) |
|---|
| 265 | 221 | |
|---|
| 266 | 222 | #define __put_user_asm(insn, x, ptr, err) \ |
|---|
| 267 | 223 | do { \ |
|---|
| 268 | 224 | uintptr_t __tmp; \ |
|---|
| 269 | 225 | __typeof__(*(ptr)) __x = x; \ |
|---|
| 270 | | - __enable_user_access(); \ |
|---|
| 271 | 226 | __asm__ __volatile__ ( \ |
|---|
| 272 | 227 | "1:\n" \ |
|---|
| 273 | 228 | " " insn " %z3, %2\n" \ |
|---|
| .. | .. |
|---|
| 284 | 239 | " .previous" \ |
|---|
| 285 | 240 | : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \ |
|---|
| 286 | 241 | : "rJ" (__x), "i" (-EFAULT)); \ |
|---|
| 287 | | - __disable_user_access(); \ |
|---|
| 288 | 242 | } while (0) |
|---|
| 289 | 243 | |
|---|
| 290 | 244 | #ifdef CONFIG_64BIT |
|---|
| .. | .. |
|---|
| 296 | 250 | u32 __user *__ptr = (u32 __user *)(ptr); \ |
|---|
| 297 | 251 | u64 __x = (__typeof__((x)-(x)))(x); \ |
|---|
| 298 | 252 | uintptr_t __tmp; \ |
|---|
| 299 | | - __enable_user_access(); \ |
|---|
| 300 | 253 | __asm__ __volatile__ ( \ |
|---|
| 301 | 254 | "1:\n" \ |
|---|
| 302 | 255 | " sw %z4, %2\n" \ |
|---|
| .. | .. |
|---|
| 318 | 271 | "=m" (__ptr[__LSW]), \ |
|---|
| 319 | 272 | "=m" (__ptr[__MSW]) \ |
|---|
| 320 | 273 | : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \ |
|---|
| 321 | | - __disable_user_access(); \ |
|---|
| 322 | 274 | } while (0) |
|---|
| 323 | 275 | #endif /* CONFIG_64BIT */ |
|---|
| 324 | 276 | |
|---|
| 325 | | - |
|---|
| 326 | | -/** |
|---|
| 327 | | - * __put_user: - Write a simple value into user space, with less checking. |
|---|
| 328 | | - * @x: Value to copy to user space. |
|---|
| 329 | | - * @ptr: Destination address, in user space. |
|---|
| 330 | | - * |
|---|
| 331 | | - * Context: User context only. This function may sleep. |
|---|
| 332 | | - * |
|---|
| 333 | | - * This macro copies a single simple value from kernel space to user |
|---|
| 334 | | - * space. It supports simple types like char and int, but not larger |
|---|
| 335 | | - * data types like structures or arrays. |
|---|
| 336 | | - * |
|---|
| 337 | | - * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
|---|
| 338 | | - * to the result of dereferencing @ptr. |
|---|
| 339 | | - * |
|---|
| 340 | | - * Caller must check the pointer with access_ok() before calling this |
|---|
| 341 | | - * function. |
|---|
| 342 | | - * |
|---|
| 343 | | - * Returns zero on success, or -EFAULT on error. |
|---|
| 344 | | - */ |
|---|
| 345 | | -#define __put_user(x, ptr) \ |
|---|
| 346 | | -({ \ |
|---|
| 347 | | - register long __pu_err = 0; \ |
|---|
| 348 | | - __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ |
|---|
| 349 | | - __chk_user_ptr(__gu_ptr); \ |
|---|
| 277 | +#define __put_user_nocheck(x, __gu_ptr, __pu_err) \ |
|---|
| 278 | +do { \ |
|---|
| 350 | 279 | switch (sizeof(*__gu_ptr)) { \ |
|---|
| 351 | 280 | case 1: \ |
|---|
| 352 | 281 | __put_user_asm("sb", (x), __gu_ptr, __pu_err); \ |
|---|
| .. | .. |
|---|
| 363 | 292 | default: \ |
|---|
| 364 | 293 | BUILD_BUG(); \ |
|---|
| 365 | 294 | } \ |
|---|
| 295 | +} while (0) |
|---|
| 296 | + |
|---|
| 297 | +/** |
|---|
| 298 | + * __put_user: - Write a simple value into user space, with less checking. |
|---|
| 299 | + * @x: Value to copy to user space. |
|---|
| 300 | + * @ptr: Destination address, in user space. |
|---|
| 301 | + * |
|---|
| 302 | + * Context: User context only. This function may sleep. |
|---|
| 303 | + * |
|---|
| 304 | + * This macro copies a single simple value from kernel space to user |
|---|
| 305 | + * space. It supports simple types like char and int, but not larger |
|---|
| 306 | + * data types like structures or arrays. |
|---|
| 307 | + * |
|---|
| 308 | + * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
|---|
| 309 | + * to the result of dereferencing @ptr. The value of @x is copied to avoid |
|---|
| 310 | + * re-ordering where @x is evaluated inside the block that enables user-space |
|---|
| 311 | + * access (thus bypassing user space protection if @x is a function). |
|---|
| 312 | + * |
|---|
| 313 | + * Caller must check the pointer with access_ok() before calling this |
|---|
| 314 | + * function. |
|---|
| 315 | + * |
|---|
| 316 | + * Returns zero on success, or -EFAULT on error. |
|---|
| 317 | + */ |
|---|
| 318 | +#define __put_user(x, ptr) \ |
|---|
| 319 | +({ \ |
|---|
| 320 | + __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ |
|---|
| 321 | + __typeof__(*__gu_ptr) __val = (x); \ |
|---|
| 322 | + long __pu_err = 0; \ |
|---|
| 323 | + \ |
|---|
| 324 | + __chk_user_ptr(__gu_ptr); \ |
|---|
| 325 | + \ |
|---|
| 326 | + __enable_user_access(); \ |
|---|
| 327 | + __put_user_nocheck(__val, __gu_ptr, __pu_err); \ |
|---|
| 328 | + __disable_user_access(); \ |
|---|
| 329 | + \ |
|---|
| 366 | 330 | __pu_err; \ |
|---|
| 367 | 331 | }) |
|---|
| 368 | 332 | |
|---|
| .. | .. |
|---|
| 386 | 350 | ({ \ |
|---|
| 387 | 351 | __typeof__(*(ptr)) __user *__p = (ptr); \ |
|---|
| 388 | 352 | might_fault(); \ |
|---|
| 389 | | - access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \ |
|---|
| 353 | + access_ok(__p, sizeof(*__p)) ? \ |
|---|
| 390 | 354 | __put_user((x), __p) : \ |
|---|
| 391 | 355 | -EFAULT; \ |
|---|
| 392 | 356 | }) |
|---|
| 393 | 357 | |
|---|
| 394 | 358 | |
|---|
| 395 | | -extern unsigned long __must_check __asm_copy_to_user(void __user *to, |
|---|
| 359 | +unsigned long __must_check __asm_copy_to_user(void __user *to, |
|---|
| 396 | 360 | const void *from, unsigned long n); |
|---|
| 397 | | -extern unsigned long __must_check __asm_copy_from_user(void *to, |
|---|
| 361 | +unsigned long __must_check __asm_copy_from_user(void *to, |
|---|
| 398 | 362 | const void __user *from, unsigned long n); |
|---|
| 399 | 363 | |
|---|
| 400 | 364 | static inline unsigned long |
|---|
| .. | .. |
|---|
| 421 | 385 | unsigned long __must_check clear_user(void __user *to, unsigned long n) |
|---|
| 422 | 386 | { |
|---|
| 423 | 387 | might_fault(); |
|---|
| 424 | | - return access_ok(VERIFY_WRITE, to, n) ? |
|---|
| 388 | + return access_ok(to, n) ? |
|---|
| 425 | 389 | __clear_user(to, n) : n; |
|---|
| 426 | 390 | } |
|---|
| 427 | 391 | |
|---|
| .. | .. |
|---|
| 500 | 464 | __ret; \ |
|---|
| 501 | 465 | }) |
|---|
| 502 | 466 | |
|---|
| 467 | +#define HAVE_GET_KERNEL_NOFAULT |
|---|
| 468 | + |
|---|
| 469 | +#define __get_kernel_nofault(dst, src, type, err_label) \ |
|---|
| 470 | +do { \ |
|---|
| 471 | + long __kr_err; \ |
|---|
| 472 | + \ |
|---|
| 473 | + __get_user_nocheck(*((type *)(dst)), (type *)(src), __kr_err); \ |
|---|
| 474 | + if (unlikely(__kr_err)) \ |
|---|
| 475 | + goto err_label; \ |
|---|
| 476 | +} while (0) |
|---|
| 477 | + |
|---|
| 478 | +#define __put_kernel_nofault(dst, src, type, err_label) \ |
|---|
| 479 | +do { \ |
|---|
| 480 | + long __kr_err; \ |
|---|
| 481 | + \ |
|---|
| 482 | + __put_user_nocheck(*((type *)(src)), (type *)(dst), __kr_err); \ |
|---|
| 483 | + if (unlikely(__kr_err)) \ |
|---|
| 484 | + goto err_label; \ |
|---|
| 485 | +} while (0) |
|---|
| 486 | + |
|---|
| 487 | +#else /* CONFIG_MMU */ |
|---|
| 488 | +#include <asm-generic/uaccess.h> |
|---|
| 489 | +#endif /* CONFIG_MMU */ |
|---|
| 503 | 490 | #endif /* _ASM_RISCV_UACCESS_H */ |
|---|