1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012 Regents of the University of California 4 * 5 * This file was copied from include/asm-generic/uaccess.h 6 */ 7 8 #ifndef _ASM_RISCV_UACCESS_H 9 #define _ASM_RISCV_UACCESS_H 10 11 #include <asm/pgtable.h> /* for TASK_SIZE */ 12 13 /* 14 * User space memory access functions 15 */ 16 17 extern unsigned long __must_check __asm_copy_to_user(void __user *to, 18 const void *from, unsigned long n); 19 extern unsigned long __must_check __asm_copy_from_user(void *to, 20 const void __user *from, unsigned long n); 21 22 static inline unsigned long 23 raw_copy_from_user(void *to, const void __user *from, unsigned long n) 24 { 25 return __asm_copy_from_user(to, from, n); 26 } 27 28 static inline unsigned long 29 raw_copy_to_user(void __user *to, const void *from, unsigned long n) 30 { 31 return __asm_copy_to_user(to, from, n); 32 } 33 34 #ifdef CONFIG_MMU 35 #include <linux/errno.h> 36 #include <linux/compiler.h> 37 #include <linux/thread_info.h> 38 #include <asm/byteorder.h> 39 #include <asm/extable.h> 40 #include <asm/asm.h> 41 42 #define __enable_user_access() \ 43 __asm__ __volatile__ ("csrs sstatus, %0" : : "r" (SR_SUM) : "memory") 44 #define __disable_user_access() \ 45 __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory") 46 47 /* 48 * The fs value determines whether argument validity checking should be 49 * performed or not. If get_fs() == USER_DS, checking is performed, with 50 * get_fs() == KERNEL_DS, checking is bypassed. 51 * 52 * For historical reasons, these macros are grossly misnamed. 53 */ 54 55 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) 56 57 #define KERNEL_DS MAKE_MM_SEG(~0UL) 58 #define USER_DS MAKE_MM_SEG(TASK_SIZE) 59 60 #define get_fs() (current_thread_info()->addr_limit) 61 62 static inline void set_fs(mm_segment_t fs) 63 { 64 current_thread_info()->addr_limit = fs; 65 } 66 67 #define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg) 68 #define user_addr_max() (get_fs().seg) 69 70 /** 71 * access_ok: - Checks if a user space pointer is valid 72 * @addr: User space pointer to start of block to check 73 * @size: Size of block to check 74 * 75 * Context: User context only. This function may sleep. 76 * 77 * Checks if a pointer to a block of memory in user space is valid. 78 * 79 * Returns true (nonzero) if the memory block may be valid, false (zero) 80 * if it is definitely invalid. 81 * 82 * Note that, depending on architecture, this function probably just 83 * checks that the pointer is in the user space range - after calling 84 * this function, memory access functions may still return -EFAULT. 85 */ 86 #define access_ok(addr, size) ({ \ 87 __chk_user_ptr(addr); \ 88 likely(__access_ok((unsigned long __force)(addr), (size))); \ 89 }) 90 91 /* 92 * Ensure that the range [addr, addr+size) is within the process's 93 * address space 94 */ 95 static inline int __access_ok(unsigned long addr, unsigned long size) 96 { 97 const mm_segment_t fs = get_fs(); 98 99 return size <= fs.seg && addr <= fs.seg - size; 100 } 101 102 /* 103 * The exception table consists of pairs of addresses: the first is the 104 * address of an instruction that is allowed to fault, and the second is 105 * the address at which the program should continue. No registers are 106 * modified, so it is entirely up to the continuation code to figure out 107 * what to do. 108 * 109 * All the routines below use bits of fixup code that are out of line 110 * with the main instruction path. This means when everything is well, 111 * we don't even have to jump over them. Further, they do not intrude 112 * on our cache or tlb entries. 113 */ 114 115 #define __LSW 0 116 #define __MSW 1 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 124 #define __get_user_asm(insn, x, ptr, err) \ 125 do { \ 126 uintptr_t __tmp; \ 127 __typeof__(x) __x; \ 128 __enable_user_access(); \ 129 __asm__ __volatile__ ( \ 130 "1:\n" \ 131 " " insn " %1, %3\n" \ 132 "2:\n" \ 133 " .section .fixup,\"ax\"\n" \ 134 " .balign 4\n" \ 135 "3:\n" \ 136 " li %0, %4\n" \ 137 " li %1, 0\n" \ 138 " jump 2b, %2\n" \ 139 " .previous\n" \ 140 " .section __ex_table,\"a\"\n" \ 141 " .balign " RISCV_SZPTR "\n" \ 142 " " RISCV_PTR " 1b, 3b\n" \ 143 " .previous" \ 144 : "+r" (err), "=&r" (__x), "=r" (__tmp) \ 145 : "m" (*(ptr)), "i" (-EFAULT)); \ 146 __disable_user_access(); \ 147 (x) = __x; \ 148 } while (0) 149 150 #ifdef CONFIG_64BIT 151 #define __get_user_8(x, ptr, err) \ 152 __get_user_asm("ld", x, ptr, err) 153 #else /* !CONFIG_64BIT */ 154 #define __get_user_8(x, ptr, err) \ 155 do { \ 156 u32 __user *__ptr = (u32 __user *)(ptr); \ 157 u32 __lo, __hi; \ 158 uintptr_t __tmp; \ 159 __enable_user_access(); \ 160 __asm__ __volatile__ ( \ 161 "1:\n" \ 162 " lw %1, %4\n" \ 163 "2:\n" \ 164 " lw %2, %5\n" \ 165 "3:\n" \ 166 " .section .fixup,\"ax\"\n" \ 167 " .balign 4\n" \ 168 "4:\n" \ 169 " li %0, %6\n" \ 170 " li %1, 0\n" \ 171 " li %2, 0\n" \ 172 " jump 3b, %3\n" \ 173 " .previous\n" \ 174 " .section __ex_table,\"a\"\n" \ 175 " .balign " RISCV_SZPTR "\n" \ 176 " " RISCV_PTR " 1b, 4b\n" \ 177 " " RISCV_PTR " 2b, 4b\n" \ 178 " .previous" \ 179 : "+r" (err), "=&r" (__lo), "=r" (__hi), \ 180 "=r" (__tmp) \ 181 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \ 182 "i" (-EFAULT)); \ 183 __disable_user_access(); \ 184 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \ 185 (((u64)__hi << 32) | __lo))); \ 186 } while (0) 187 #endif /* CONFIG_64BIT */ 188 189 190 /** 191 * __get_user: - Get a simple variable from user space, with less checking. 192 * @x: Variable to store result. 193 * @ptr: Source address, in user space. 194 * 195 * Context: User context only. This function may sleep. 196 * 197 * This macro copies a single simple variable from user space to kernel 198 * space. It supports simple types like char and int, but not larger 199 * data types like structures or arrays. 200 * 201 * @ptr must have pointer-to-simple-variable type, and the result of 202 * dereferencing @ptr must be assignable to @x without a cast. 203 * 204 * Caller must check the pointer with access_ok() before calling this 205 * function. 206 * 207 * Returns zero on success, or -EFAULT on error. 208 * On error, the variable @x is set to zero. 209 */ 210 #define __get_user(x, ptr) \ 211 ({ \ 212 register long __gu_err = 0; \ 213 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ 214 __chk_user_ptr(__gu_ptr); \ 215 switch (sizeof(*__gu_ptr)) { \ 216 case 1: \ 217 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \ 218 break; \ 219 case 2: \ 220 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \ 221 break; \ 222 case 4: \ 223 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \ 224 break; \ 225 case 8: \ 226 __get_user_8((x), __gu_ptr, __gu_err); \ 227 break; \ 228 default: \ 229 BUILD_BUG(); \ 230 } \ 231 __gu_err; \ 232 }) 233 234 /** 235 * get_user: - Get a simple variable from user space. 236 * @x: Variable to store result. 237 * @ptr: Source address, in user space. 238 * 239 * Context: User context only. This function may sleep. 240 * 241 * This macro copies a single simple variable from user space to kernel 242 * space. It supports simple types like char and int, but not larger 243 * data types like structures or arrays. 244 * 245 * @ptr must have pointer-to-simple-variable type, and the result of 246 * dereferencing @ptr must be assignable to @x without a cast. 247 * 248 * Returns zero on success, or -EFAULT on error. 249 * On error, the variable @x is set to zero. 250 */ 251 #define get_user(x, ptr) \ 252 ({ \ 253 const __typeof__(*(ptr)) __user *__p = (ptr); \ 254 might_fault(); \ 255 access_ok(__p, sizeof(*__p)) ? \ 256 __get_user((x), __p) : \ 257 ((x) = 0, -EFAULT); \ 258 }) 259 260 #define __put_user_asm(insn, x, ptr, err) \ 261 do { \ 262 uintptr_t __tmp; \ 263 __typeof__(*(ptr)) __x = x; \ 264 __enable_user_access(); \ 265 __asm__ __volatile__ ( \ 266 "1:\n" \ 267 " " insn " %z3, %2\n" \ 268 "2:\n" \ 269 " .section .fixup,\"ax\"\n" \ 270 " .balign 4\n" \ 271 "3:\n" \ 272 " li %0, %4\n" \ 273 " jump 2b, %1\n" \ 274 " .previous\n" \ 275 " .section __ex_table,\"a\"\n" \ 276 " .balign " RISCV_SZPTR "\n" \ 277 " " RISCV_PTR " 1b, 3b\n" \ 278 " .previous" \ 279 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \ 280 : "rJ" (__x), "i" (-EFAULT)); \ 281 __disable_user_access(); \ 282 } while (0) 283 284 #ifdef CONFIG_64BIT 285 #define __put_user_8(x, ptr, err) \ 286 __put_user_asm("sd", x, ptr, err) 287 #else /* !CONFIG_64BIT */ 288 #define __put_user_8(x, ptr, err) \ 289 do { \ 290 u32 __user *__ptr = (u32 __user *)(ptr); \ 291 u64 __x = (__typeof__((x)-(x)))(x); \ 292 uintptr_t __tmp; \ 293 __enable_user_access(); \ 294 __asm__ __volatile__ ( \ 295 "1:\n" \ 296 " sw %z4, %2\n" \ 297 "2:\n" \ 298 " sw %z5, %3\n" \ 299 "3:\n" \ 300 " .section .fixup,\"ax\"\n" \ 301 " .balign 4\n" \ 302 "4:\n" \ 303 " li %0, %6\n" \ 304 " jump 3b, %1\n" \ 305 " .previous\n" \ 306 " .section __ex_table,\"a\"\n" \ 307 " .balign " RISCV_SZPTR "\n" \ 308 " " RISCV_PTR " 1b, 4b\n" \ 309 " " RISCV_PTR " 2b, 4b\n" \ 310 " .previous" \ 311 : "+r" (err), "=r" (__tmp), \ 312 "=m" (__ptr[__LSW]), \ 313 "=m" (__ptr[__MSW]) \ 314 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \ 315 __disable_user_access(); \ 316 } while (0) 317 #endif /* CONFIG_64BIT */ 318 319 320 /** 321 * __put_user: - Write a simple value into user space, with less checking. 322 * @x: Value to copy to user space. 323 * @ptr: Destination address, in user space. 324 * 325 * Context: User context only. This function may sleep. 326 * 327 * This macro copies a single simple value from kernel space to user 328 * space. It supports simple types like char and int, but not larger 329 * data types like structures or arrays. 330 * 331 * @ptr must have pointer-to-simple-variable type, and @x must be assignable 332 * to the result of dereferencing @ptr. 333 * 334 * Caller must check the pointer with access_ok() before calling this 335 * function. 336 * 337 * Returns zero on success, or -EFAULT on error. 338 */ 339 #define __put_user(x, ptr) \ 340 ({ \ 341 register long __pu_err = 0; \ 342 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ 343 __chk_user_ptr(__gu_ptr); \ 344 switch (sizeof(*__gu_ptr)) { \ 345 case 1: \ 346 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \ 347 break; \ 348 case 2: \ 349 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \ 350 break; \ 351 case 4: \ 352 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \ 353 break; \ 354 case 8: \ 355 __put_user_8((x), __gu_ptr, __pu_err); \ 356 break; \ 357 default: \ 358 BUILD_BUG(); \ 359 } \ 360 __pu_err; \ 361 }) 362 363 /** 364 * put_user: - Write a simple value into user space. 365 * @x: Value to copy to user space. 366 * @ptr: Destination address, in user space. 367 * 368 * Context: User context only. This function may sleep. 369 * 370 * This macro copies a single simple value from kernel space to user 371 * space. It supports simple types like char and int, but not larger 372 * data types like structures or arrays. 373 * 374 * @ptr must have pointer-to-simple-variable type, and @x must be assignable 375 * to the result of dereferencing @ptr. 376 * 377 * Returns zero on success, or -EFAULT on error. 378 */ 379 #define put_user(x, ptr) \ 380 ({ \ 381 __typeof__(*(ptr)) __user *__p = (ptr); \ 382 might_fault(); \ 383 access_ok(__p, sizeof(*__p)) ? \ 384 __put_user((x), __p) : \ 385 -EFAULT; \ 386 }) 387 388 extern long strncpy_from_user(char *dest, const char __user *src, long count); 389 390 extern long __must_check strlen_user(const char __user *str); 391 extern long __must_check strnlen_user(const char __user *str, long n); 392 393 extern 394 unsigned long __must_check __clear_user(void __user *addr, unsigned long n); 395 396 static inline 397 unsigned long __must_check clear_user(void __user *to, unsigned long n) 398 { 399 might_fault(); 400 return access_ok(to, n) ? 401 __clear_user(to, n) : n; 402 } 403 404 /* 405 * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults 406 * will set "err" to -EFAULT, while successful accesses return the previous 407 * value. 408 */ 409 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \ 410 ({ \ 411 __typeof__(ptr) __ptr = (ptr); \ 412 __typeof__(*(ptr)) __old = (old); \ 413 __typeof__(*(ptr)) __new = (new); \ 414 __typeof__(*(ptr)) __ret; \ 415 __typeof__(err) __err = 0; \ 416 register unsigned int __rc; \ 417 __enable_user_access(); \ 418 switch (size) { \ 419 case 4: \ 420 __asm__ __volatile__ ( \ 421 "0:\n" \ 422 " lr.w" #scb " %[ret], %[ptr]\n" \ 423 " bne %[ret], %z[old], 1f\n" \ 424 " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \ 425 " bnez %[rc], 0b\n" \ 426 "1:\n" \ 427 ".section .fixup,\"ax\"\n" \ 428 ".balign 4\n" \ 429 "2:\n" \ 430 " li %[err], %[efault]\n" \ 431 " jump 1b, %[rc]\n" \ 432 ".previous\n" \ 433 ".section __ex_table,\"a\"\n" \ 434 ".balign " RISCV_SZPTR "\n" \ 435 " " RISCV_PTR " 1b, 2b\n" \ 436 ".previous\n" \ 437 : [ret] "=&r" (__ret), \ 438 [rc] "=&r" (__rc), \ 439 [ptr] "+A" (*__ptr), \ 440 [err] "=&r" (__err) \ 441 : [old] "rJ" (__old), \ 442 [new] "rJ" (__new), \ 443 [efault] "i" (-EFAULT)); \ 444 break; \ 445 case 8: \ 446 __asm__ __volatile__ ( \ 447 "0:\n" \ 448 " lr.d" #scb " %[ret], %[ptr]\n" \ 449 " bne %[ret], %z[old], 1f\n" \ 450 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \ 451 " bnez %[rc], 0b\n" \ 452 "1:\n" \ 453 ".section .fixup,\"ax\"\n" \ 454 ".balign 4\n" \ 455 "2:\n" \ 456 " li %[err], %[efault]\n" \ 457 " jump 1b, %[rc]\n" \ 458 ".previous\n" \ 459 ".section __ex_table,\"a\"\n" \ 460 ".balign " RISCV_SZPTR "\n" \ 461 " " RISCV_PTR " 1b, 2b\n" \ 462 ".previous\n" \ 463 : [ret] "=&r" (__ret), \ 464 [rc] "=&r" (__rc), \ 465 [ptr] "+A" (*__ptr), \ 466 [err] "=&r" (__err) \ 467 : [old] "rJ" (__old), \ 468 [new] "rJ" (__new), \ 469 [efault] "i" (-EFAULT)); \ 470 break; \ 471 default: \ 472 BUILD_BUG(); \ 473 } \ 474 __disable_user_access(); \ 475 (err) = __err; \ 476 __ret; \ 477 }) 478 479 #else /* CONFIG_MMU */ 480 #include <asm-generic/uaccess.h> 481 #endif /* CONFIG_MMU */ 482 #endif /* _ASM_RISCV_UACCESS_H */ 483