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
12 * User space memory access functions
15 extern unsigned long __must_check
__asm_copy_to_user(void __user
*to
,
16 const void *from
, unsigned long n
);
17 extern unsigned long __must_check
__asm_copy_from_user(void *to
,
18 const void __user
*from
, unsigned long n
);
20 static inline unsigned long
21 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
23 return __asm_copy_from_user(to
, from
, n
);
26 static inline unsigned long
27 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
29 return __asm_copy_to_user(to
, from
, n
);
33 #include <linux/errno.h>
34 #include <linux/compiler.h>
35 #include <linux/thread_info.h>
36 #include <asm/byteorder.h>
37 #include <asm/extable.h>
40 #define __enable_user_access() \
41 __asm__ __volatile__ ("csrs sstatus, %0" : : "r" (SR_SUM) : "memory")
42 #define __disable_user_access() \
43 __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory")
46 * The fs value determines whether argument validity checking should be
47 * performed or not. If get_fs() == USER_DS, checking is performed, with
48 * get_fs() == KERNEL_DS, checking is bypassed.
50 * For historical reasons, these macros are grossly misnamed.
53 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
55 #define KERNEL_DS MAKE_MM_SEG(~0UL)
56 #define USER_DS MAKE_MM_SEG(TASK_SIZE)
58 #define get_fs() (current_thread_info()->addr_limit)
60 static inline void set_fs(mm_segment_t fs
)
62 current_thread_info()->addr_limit
= fs
;
65 #define segment_eq(a, b) ((a).seg == (b).seg)
67 #define user_addr_max() (get_fs().seg)
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
75 * Context: User context only. This function may sleep.
77 * Checks if a pointer to a block of memory in user space is valid.
79 * Returns true (nonzero) if the memory block may be valid, false (zero)
80 * if it is definitely invalid.
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.
86 #define access_ok(addr, size) ({ \
87 __chk_user_ptr(addr); \
88 likely(__access_ok((unsigned long __force)(addr), (size))); \
92 * Ensure that the range [addr, addr+size) is within the process's
95 static inline int __access_ok(unsigned long addr
, unsigned long size
)
97 const mm_segment_t fs
= get_fs();
99 return size
<= fs
.seg
&& addr
<= fs
.seg
- size
;
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
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.
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()"
124 #define __get_user_asm(insn, x, ptr, err) \
128 __enable_user_access(); \
129 __asm__ __volatile__ ( \
131 " " insn " %1, %3\n" \
133 " .section .fixup,\"ax\"\n" \
140 " .section __ex_table,\"a\"\n" \
141 " .balign " RISCV_SZPTR "\n" \
142 " " RISCV_PTR " 1b, 3b\n" \
144 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
145 : "m" (*(ptr)), "i" (-EFAULT)); \
146 __disable_user_access(); \
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) \
156 u32 __user *__ptr = (u32 __user *)(ptr); \
159 __enable_user_access(); \
160 __asm__ __volatile__ ( \
166 " .section .fixup,\"ax\"\n" \
174 " .section __ex_table,\"a\"\n" \
175 " .balign " RISCV_SZPTR "\n" \
176 " " RISCV_PTR " 1b, 4b\n" \
177 " " RISCV_PTR " 2b, 4b\n" \
179 : "+r" (err), "=&r" (__lo), "=r" (__hi), \
181 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
183 __disable_user_access(); \
184 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
185 (((u64)__hi << 32) | __lo))); \
187 #endif /* CONFIG_64BIT */
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.
195 * Context: User context only. This function may sleep.
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.
201 * @ptr must have pointer-to-simple-variable type, and the result of
202 * dereferencing @ptr must be assignable to @x without a cast.
204 * Caller must check the pointer with access_ok() before calling this
207 * Returns zero on success, or -EFAULT on error.
208 * On error, the variable @x is set to zero.
210 #define __get_user(x, ptr) \
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)) { \
217 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
220 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
223 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
226 __get_user_8((x), __gu_ptr, __gu_err); \
235 * get_user: - Get a simple variable from user space.
236 * @x: Variable to store result.
237 * @ptr: Source address, in user space.
239 * Context: User context only. This function may sleep.
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.
245 * @ptr must have pointer-to-simple-variable type, and the result of
246 * dereferencing @ptr must be assignable to @x without a cast.
248 * Returns zero on success, or -EFAULT on error.
249 * On error, the variable @x is set to zero.
251 #define get_user(x, ptr) \
253 const __typeof__(*(ptr)) __user *__p = (ptr); \
255 access_ok(__p, sizeof(*__p)) ? \
256 __get_user((x), __p) : \
257 ((x) = 0, -EFAULT); \
260 #define __put_user_asm(insn, x, ptr, err) \
263 __typeof__(*(ptr)) __x = x; \
264 __enable_user_access(); \
265 __asm__ __volatile__ ( \
267 " " insn " %z3, %2\n" \
269 " .section .fixup,\"ax\"\n" \
275 " .section __ex_table,\"a\"\n" \
276 " .balign " RISCV_SZPTR "\n" \
277 " " RISCV_PTR " 1b, 3b\n" \
279 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
280 : "rJ" (__x), "i" (-EFAULT)); \
281 __disable_user_access(); \
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) \
290 u32 __user *__ptr = (u32 __user *)(ptr); \
291 u64 __x = (__typeof__((x)-(x)))(x); \
293 __enable_user_access(); \
294 __asm__ __volatile__ ( \
300 " .section .fixup,\"ax\"\n" \
306 " .section __ex_table,\"a\"\n" \
307 " .balign " RISCV_SZPTR "\n" \
308 " " RISCV_PTR " 1b, 4b\n" \
309 " " RISCV_PTR " 2b, 4b\n" \
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(); \
317 #endif /* CONFIG_64BIT */
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.
325 * Context: User context only. This function may sleep.
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.
331 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
332 * to the result of dereferencing @ptr.
334 * Caller must check the pointer with access_ok() before calling this
337 * Returns zero on success, or -EFAULT on error.
339 #define __put_user(x, ptr) \
341 register long __pu_err = 0; \
342 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
343 __chk_user_ptr(__gu_ptr); \
344 switch (sizeof(*__gu_ptr)) { \
346 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
349 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
352 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
355 __put_user_8((x), __gu_ptr, __pu_err); \
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.
368 * Context: User context only. This function may sleep.
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.
374 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
375 * to the result of dereferencing @ptr.
377 * Returns zero on success, or -EFAULT on error.
379 #define put_user(x, ptr) \
381 __typeof__(*(ptr)) __user *__p = (ptr); \
383 access_ok(__p, sizeof(*__p)) ? \
384 __put_user((x), __p) : \
388 extern long strncpy_from_user(char *dest
, const char __user
*src
, long count
);
390 extern long __must_check
strlen_user(const char __user
*str
);
391 extern long __must_check
strnlen_user(const char __user
*str
, long n
);
394 unsigned long __must_check
__clear_user(void __user
*addr
, unsigned long n
);
397 unsigned long __must_check
clear_user(void __user
*to
, unsigned long n
)
400 return access_ok(to
, n
) ?
401 __clear_user(to
, n
) : n
;
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
409 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
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(); \
420 __asm__ __volatile__ ( \
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" \
427 ".section .fixup,\"ax\"\n" \
430 " li %[err], %[efault]\n" \
431 " jump 1b, %[rc]\n" \
433 ".section __ex_table,\"a\"\n" \
434 ".balign " RISCV_SZPTR "\n" \
435 " " RISCV_PTR " 1b, 2b\n" \
437 : [ret] "=&r" (__ret), \
439 [ptr] "+A" (*__ptr), \
440 [err] "=&r" (__err) \
441 : [old] "rJ" (__old), \
442 [new] "rJ" (__new), \
443 [efault] "i" (-EFAULT)); \
446 __asm__ __volatile__ ( \
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" \
453 ".section .fixup,\"ax\"\n" \
456 " li %[err], %[efault]\n" \
457 " jump 1b, %[rc]\n" \
459 ".section __ex_table,\"a\"\n" \
460 ".balign " RISCV_SZPTR "\n" \
461 " " RISCV_PTR " 1b, 2b\n" \
463 : [ret] "=&r" (__ret), \
465 [ptr] "+A" (*__ptr), \
466 [err] "=&r" (__err) \
467 : [old] "rJ" (__old), \
468 [new] "rJ" (__new), \
469 [efault] "i" (-EFAULT)); \
474 __disable_user_access(); \
479 #else /* CONFIG_MMU */
480 #include <asm-generic/uaccess.h>
481 #endif /* CONFIG_MMU */
482 #endif /* _ASM_RISCV_UACCESS_H */