1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 Regents of the University of California
5 * This file was copied from include/asm-generic/uaccess.h
8 #ifndef _ASM_RISCV_UACCESS_H
9 #define _ASM_RISCV_UACCESS_H
11 #include <asm/pgtable.h> /* for TASK_SIZE */
14 * User space memory access functions
17 #include <linux/errno.h>
18 #include <linux/compiler.h>
19 #include <linux/thread_info.h>
20 #include <asm/byteorder.h>
21 #include <asm/extable.h>
24 #define __enable_user_access() \
25 __asm__ __volatile__ ("csrs sstatus, %0" : : "r" (SR_SUM) : "memory")
26 #define __disable_user_access() \
27 __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory")
30 * access_ok: - Checks if a user space pointer is valid
31 * @addr: User space pointer to start of block to check
32 * @size: Size of block to check
34 * Context: User context only. This function may sleep.
36 * Checks if a pointer to a block of memory in user space is valid.
38 * Returns true (nonzero) if the memory block may be valid, false (zero)
39 * if it is definitely invalid.
41 * Note that, depending on architecture, this function probably just
42 * checks that the pointer is in the user space range - after calling
43 * this function, memory access functions may still return -EFAULT.
45 #define access_ok(addr, size) ({ \
46 __chk_user_ptr(addr); \
47 likely(__access_ok((unsigned long __force)(addr), (size))); \
51 * Ensure that the range [addr, addr+size) is within the process's
54 static inline int __access_ok(unsigned long addr, unsigned long size)
56 return size <= TASK_SIZE && addr <= TASK_SIZE - size;
60 * The exception table consists of pairs of addresses: the first is the
61 * address of an instruction that is allowed to fault, and the second is
62 * the address at which the program should continue. No registers are
63 * modified, so it is entirely up to the continuation code to figure out
66 * All the routines below use bits of fixup code that are out of line
67 * with the main instruction path. This means when everything is well,
68 * we don't even have to jump over them. Further, they do not intrude
69 * on our cache or tlb entries.
76 * The "__xxx" versions of the user access functions do not verify the address
77 * space - it must have been done previously with a separate "access_ok()"
81 #define __get_user_asm(insn, x, ptr, err) \
85 __asm__ __volatile__ ( \
87 " " insn " %1, %3\n" \
89 " .section .fixup,\"ax\"\n" \
96 " .section __ex_table,\"a\"\n" \
97 " .balign " RISCV_SZPTR "\n" \
98 " " RISCV_PTR " 1b, 3b\n" \
100 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
101 : "m" (*(ptr)), "i" (-EFAULT)); \
106 #define __get_user_8(x, ptr, err) \
107 __get_user_asm("ld", x, ptr, err)
108 #else /* !CONFIG_64BIT */
109 #define __get_user_8(x, ptr, err) \
111 u32 __user *__ptr = (u32 __user *)(ptr); \
114 __asm__ __volatile__ ( \
120 " .section .fixup,\"ax\"\n" \
128 " .section __ex_table,\"a\"\n" \
129 " .balign " RISCV_SZPTR "\n" \
130 " " RISCV_PTR " 1b, 4b\n" \
131 " " RISCV_PTR " 2b, 4b\n" \
133 : "+r" (err), "=&r" (__lo), "=r" (__hi), \
135 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
137 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
138 (((u64)__hi << 32) | __lo))); \
140 #endif /* CONFIG_64BIT */
142 #define __get_user_nocheck(x, __gu_ptr, __gu_err) \
144 switch (sizeof(*__gu_ptr)) { \
146 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
149 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
152 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
155 __get_user_8((x), __gu_ptr, __gu_err); \
163 * __get_user: - Get a simple variable from user space, with less checking.
164 * @x: Variable to store result.
165 * @ptr: Source address, in user space.
167 * Context: User context only. This function may sleep.
169 * This macro copies a single simple variable from user space to kernel
170 * space. It supports simple types like char and int, but not larger
171 * data types like structures or arrays.
173 * @ptr must have pointer-to-simple-variable type, and the result of
174 * dereferencing @ptr must be assignable to @x without a cast.
176 * Caller must check the pointer with access_ok() before calling this
179 * Returns zero on success, or -EFAULT on error.
180 * On error, the variable @x is set to zero.
182 #define __get_user(x, ptr) \
184 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
187 __chk_user_ptr(__gu_ptr); \
189 __enable_user_access(); \
190 __get_user_nocheck(x, __gu_ptr, __gu_err); \
191 __disable_user_access(); \
197 * get_user: - Get a simple variable from user space.
198 * @x: Variable to store result.
199 * @ptr: Source address, in user space.
201 * Context: User context only. This function may sleep.
203 * This macro copies a single simple variable from user space to kernel
204 * space. It supports simple types like char and int, but not larger
205 * data types like structures or arrays.
207 * @ptr must have pointer-to-simple-variable type, and the result of
208 * dereferencing @ptr must be assignable to @x without a cast.
210 * Returns zero on success, or -EFAULT on error.
211 * On error, the variable @x is set to zero.
213 #define get_user(x, ptr) \
215 const __typeof__(*(ptr)) __user *__p = (ptr); \
217 access_ok(__p, sizeof(*__p)) ? \
218 __get_user((x), __p) : \
219 ((x) = 0, -EFAULT); \
222 #define __put_user_asm(insn, x, ptr, err) \
225 __typeof__(*(ptr)) __x = x; \
226 __asm__ __volatile__ ( \
228 " " insn " %z3, %2\n" \
230 " .section .fixup,\"ax\"\n" \
236 " .section __ex_table,\"a\"\n" \
237 " .balign " RISCV_SZPTR "\n" \
238 " " RISCV_PTR " 1b, 3b\n" \
240 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
241 : "rJ" (__x), "i" (-EFAULT)); \
245 #define __put_user_8(x, ptr, err) \
246 __put_user_asm("sd", x, ptr, err)
247 #else /* !CONFIG_64BIT */
248 #define __put_user_8(x, ptr, err) \
250 u32 __user *__ptr = (u32 __user *)(ptr); \
251 u64 __x = (__typeof__((x)-(x)))(x); \
253 __asm__ __volatile__ ( \
259 " .section .fixup,\"ax\"\n" \
265 " .section __ex_table,\"a\"\n" \
266 " .balign " RISCV_SZPTR "\n" \
267 " " RISCV_PTR " 1b, 4b\n" \
268 " " RISCV_PTR " 2b, 4b\n" \
270 : "+r" (err), "=r" (__tmp), \
271 "=m" (__ptr[__LSW]), \
272 "=m" (__ptr[__MSW]) \
273 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
275 #endif /* CONFIG_64BIT */
277 #define __put_user_nocheck(x, __gu_ptr, __pu_err) \
279 switch (sizeof(*__gu_ptr)) { \
281 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
284 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
287 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
290 __put_user_8((x), __gu_ptr, __pu_err); \
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.
302 * Context: User context only. This function may sleep.
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.
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).
313 * Caller must check the pointer with access_ok() before calling this
316 * Returns zero on success, or -EFAULT on error.
318 #define __put_user(x, ptr) \
320 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
321 __typeof__(*__gu_ptr) __val = (x); \
324 __chk_user_ptr(__gu_ptr); \
326 __enable_user_access(); \
327 __put_user_nocheck(__val, __gu_ptr, __pu_err); \
328 __disable_user_access(); \
334 * put_user: - Write a simple value into user space.
335 * @x: Value to copy to user space.
336 * @ptr: Destination address, in user space.
338 * Context: User context only. This function may sleep.
340 * This macro copies a single simple value from kernel space to user
341 * space. It supports simple types like char and int, but not larger
342 * data types like structures or arrays.
344 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
345 * to the result of dereferencing @ptr.
347 * Returns zero on success, or -EFAULT on error.
349 #define put_user(x, ptr) \
351 __typeof__(*(ptr)) __user *__p = (ptr); \
353 access_ok(__p, sizeof(*__p)) ? \
354 __put_user((x), __p) : \
359 unsigned long __must_check __asm_copy_to_user(void __user *to,
360 const void *from, unsigned long n);
361 unsigned long __must_check __asm_copy_from_user(void *to,
362 const void __user *from, unsigned long n);
364 static inline unsigned long
365 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
367 return __asm_copy_from_user(to, from, n);
370 static inline unsigned long
371 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
373 return __asm_copy_to_user(to, from, n);
376 extern long strncpy_from_user(char *dest, const char __user *src, long count);
378 extern long __must_check strlen_user(const char __user *str);
379 extern long __must_check strnlen_user(const char __user *str, long n);
382 unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
385 unsigned long __must_check clear_user(void __user *to, unsigned long n)
388 return access_ok(to, n) ?
389 __clear_user(to, n) : n;
393 * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults
394 * will set "err" to -EFAULT, while successful accesses return the previous
397 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
399 __typeof__(ptr) __ptr = (ptr); \
400 __typeof__(*(ptr)) __old = (old); \
401 __typeof__(*(ptr)) __new = (new); \
402 __typeof__(*(ptr)) __ret; \
403 __typeof__(err) __err = 0; \
404 register unsigned int __rc; \
405 __enable_user_access(); \
408 __asm__ __volatile__ ( \
410 " lr.w" #scb " %[ret], %[ptr]\n" \
411 " bne %[ret], %z[old], 1f\n" \
412 " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \
413 " bnez %[rc], 0b\n" \
415 ".section .fixup,\"ax\"\n" \
418 " li %[err], %[efault]\n" \
419 " jump 1b, %[rc]\n" \
421 ".section __ex_table,\"a\"\n" \
422 ".balign " RISCV_SZPTR "\n" \
423 " " RISCV_PTR " 1b, 2b\n" \
425 : [ret] "=&r" (__ret), \
427 [ptr] "+A" (*__ptr), \
428 [err] "=&r" (__err) \
429 : [old] "rJ" (__old), \
430 [new] "rJ" (__new), \
431 [efault] "i" (-EFAULT)); \
434 __asm__ __volatile__ ( \
436 " lr.d" #scb " %[ret], %[ptr]\n" \
437 " bne %[ret], %z[old], 1f\n" \
438 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
439 " bnez %[rc], 0b\n" \
441 ".section .fixup,\"ax\"\n" \
444 " li %[err], %[efault]\n" \
445 " jump 1b, %[rc]\n" \
447 ".section __ex_table,\"a\"\n" \
448 ".balign " RISCV_SZPTR "\n" \
449 " " RISCV_PTR " 1b, 2b\n" \
451 : [ret] "=&r" (__ret), \
453 [ptr] "+A" (*__ptr), \
454 [err] "=&r" (__err) \
455 : [old] "rJ" (__old), \
456 [new] "rJ" (__new), \
457 [efault] "i" (-EFAULT)); \
462 __disable_user_access(); \
467 #define HAVE_GET_KERNEL_NOFAULT
469 #define __get_kernel_nofault(dst, src, type, err_label) \
473 __get_user_nocheck(*((type *)(dst)), (type *)(src), __kr_err); \
474 if (unlikely(__kr_err)) \
478 #define __put_kernel_nofault(dst, src, type, err_label) \
482 __put_user_nocheck(*((type *)(src)), (type *)(dst), __kr_err); \
483 if (unlikely(__kr_err)) \
487 #else /* CONFIG_MMU */
488 #include <asm-generic/uaccess.h>
489 #endif /* CONFIG_MMU */
490 #endif /* _ASM_RISCV_UACCESS_H */