2 * Copyright (C) 2012 Regents of the University of California
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.
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.
13 * This file was copied from include/asm-generic/uaccess.h
16 #ifndef _ASM_RISCV_UACCESS_H
17 #define _ASM_RISCV_UACCESS_H
20 * User space memory access functions
22 #include <linux/errno.h>
23 #include <linux/compiler.h>
24 #include <linux/thread_info.h>
25 #include <asm/byteorder.h>
26 #include <asm/extable.h>
29 #define __enable_user_access() \
30 __asm__ __volatile__ ("csrs sstatus, %0" : : "r" (SR_SUM) : "memory")
31 #define __disable_user_access() \
32 __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory")
35 * The fs value determines whether argument validity checking should be
36 * performed or not. If get_fs() == USER_DS, checking is performed, with
37 * get_fs() == KERNEL_DS, checking is bypassed.
39 * For historical reasons, these macros are grossly misnamed.
42 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
44 #define KERNEL_DS MAKE_MM_SEG(~0UL)
45 #define USER_DS MAKE_MM_SEG(TASK_SIZE)
47 #define get_fs() (current_thread_info()->addr_limit)
49 static inline void set_fs(mm_segment_t fs)
51 current_thread_info()->addr_limit = fs;
54 #define segment_eq(a, b) ((a).seg == (b).seg)
56 #define user_addr_max() (get_fs().seg)
60 * access_ok: - Checks if a user space pointer is valid
61 * @addr: User space pointer to start of block to check
62 * @size: Size of block to check
64 * Context: User context only. This function may sleep.
66 * Checks if a pointer to a block of memory in user space is valid.
68 * Returns true (nonzero) if the memory block may be valid, false (zero)
69 * if it is definitely invalid.
71 * Note that, depending on architecture, this function probably just
72 * checks that the pointer is in the user space range - after calling
73 * this function, memory access functions may still return -EFAULT.
75 #define access_ok(addr, size) ({ \
76 __chk_user_ptr(addr); \
77 likely(__access_ok((unsigned long __force)(addr), (size))); \
81 * Ensure that the range [addr, addr+size) is within the process's
84 static inline int __access_ok(unsigned long addr, unsigned long size)
86 const mm_segment_t fs = get_fs();
88 return size <= fs.seg && addr <= fs.seg - size;
92 * The exception table consists of pairs of addresses: the first is the
93 * address of an instruction that is allowed to fault, and the second is
94 * the address at which the program should continue. No registers are
95 * modified, so it is entirely up to the continuation code to figure out
98 * All the routines below use bits of fixup code that are out of line
99 * with the main instruction path. This means when everything is well,
100 * we don't even have to jump over them. Further, they do not intrude
101 * on our cache or tlb entries.
104 #if defined(__LITTLE_ENDIAN)
107 #elif defined(__BIG_ENDIAN)
111 #error "Unknown endianness"
115 * The "__xxx" versions of the user access functions do not verify the address
116 * space - it must have been done previously with a separate "access_ok()"
120 #define __get_user_asm(insn, x, ptr, err) \
124 __enable_user_access(); \
125 __asm__ __volatile__ ( \
127 " " insn " %1, %3\n" \
129 " .section .fixup,\"ax\"\n" \
136 " .section __ex_table,\"a\"\n" \
137 " .balign " RISCV_SZPTR "\n" \
138 " " RISCV_PTR " 1b, 3b\n" \
140 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
141 : "m" (*(ptr)), "i" (-EFAULT)); \
142 __disable_user_access(); \
147 #define __get_user_8(x, ptr, err) \
148 __get_user_asm("ld", x, ptr, err)
149 #else /* !CONFIG_64BIT */
150 #define __get_user_8(x, ptr, err) \
152 u32 __user *__ptr = (u32 __user *)(ptr); \
155 __enable_user_access(); \
156 __asm__ __volatile__ ( \
162 " .section .fixup,\"ax\"\n" \
170 " .section __ex_table,\"a\"\n" \
171 " .balign " RISCV_SZPTR "\n" \
172 " " RISCV_PTR " 1b, 4b\n" \
173 " " RISCV_PTR " 2b, 4b\n" \
175 : "+r" (err), "=&r" (__lo), "=r" (__hi), \
177 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
179 __disable_user_access(); \
180 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
181 (((u64)__hi << 32) | __lo))); \
183 #endif /* CONFIG_64BIT */
187 * __get_user: - Get a simple variable from user space, with less checking.
188 * @x: Variable to store result.
189 * @ptr: Source address, in user space.
191 * Context: User context only. This function may sleep.
193 * This macro copies a single simple variable from user space to kernel
194 * space. It supports simple types like char and int, but not larger
195 * data types like structures or arrays.
197 * @ptr must have pointer-to-simple-variable type, and the result of
198 * dereferencing @ptr must be assignable to @x without a cast.
200 * Caller must check the pointer with access_ok() before calling this
203 * Returns zero on success, or -EFAULT on error.
204 * On error, the variable @x is set to zero.
206 #define __get_user(x, ptr) \
208 register long __gu_err = 0; \
209 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
210 __chk_user_ptr(__gu_ptr); \
211 switch (sizeof(*__gu_ptr)) { \
213 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
216 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
219 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
222 __get_user_8((x), __gu_ptr, __gu_err); \
231 * get_user: - Get a simple variable from user space.
232 * @x: Variable to store result.
233 * @ptr: Source address, in user space.
235 * Context: User context only. This function may sleep.
237 * This macro copies a single simple variable from user space to kernel
238 * space. It supports simple types like char and int, but not larger
239 * data types like structures or arrays.
241 * @ptr must have pointer-to-simple-variable type, and the result of
242 * dereferencing @ptr must be assignable to @x without a cast.
244 * Returns zero on success, or -EFAULT on error.
245 * On error, the variable @x is set to zero.
247 #define get_user(x, ptr) \
249 const __typeof__(*(ptr)) __user *__p = (ptr); \
251 access_ok(__p, sizeof(*__p)) ? \
252 __get_user((x), __p) : \
253 ((x) = 0, -EFAULT); \
256 #define __put_user_asm(insn, x, ptr, err) \
259 __typeof__(*(ptr)) __x = x; \
260 __enable_user_access(); \
261 __asm__ __volatile__ ( \
263 " " insn " %z3, %2\n" \
265 " .section .fixup,\"ax\"\n" \
271 " .section __ex_table,\"a\"\n" \
272 " .balign " RISCV_SZPTR "\n" \
273 " " RISCV_PTR " 1b, 3b\n" \
275 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
276 : "rJ" (__x), "i" (-EFAULT)); \
277 __disable_user_access(); \
281 #define __put_user_8(x, ptr, err) \
282 __put_user_asm("sd", x, ptr, err)
283 #else /* !CONFIG_64BIT */
284 #define __put_user_8(x, ptr, err) \
286 u32 __user *__ptr = (u32 __user *)(ptr); \
287 u64 __x = (__typeof__((x)-(x)))(x); \
289 __enable_user_access(); \
290 __asm__ __volatile__ ( \
296 " .section .fixup,\"ax\"\n" \
302 " .section __ex_table,\"a\"\n" \
303 " .balign " RISCV_SZPTR "\n" \
304 " " RISCV_PTR " 1b, 4b\n" \
305 " " RISCV_PTR " 2b, 4b\n" \
307 : "+r" (err), "=r" (__tmp), \
308 "=m" (__ptr[__LSW]), \
309 "=m" (__ptr[__MSW]) \
310 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
311 __disable_user_access(); \
313 #endif /* CONFIG_64BIT */
317 * __put_user: - Write a simple value into user space, with less checking.
318 * @x: Value to copy to user space.
319 * @ptr: Destination address, in user space.
321 * Context: User context only. This function may sleep.
323 * This macro copies a single simple value from kernel space to user
324 * space. It supports simple types like char and int, but not larger
325 * data types like structures or arrays.
327 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
328 * to the result of dereferencing @ptr.
330 * Caller must check the pointer with access_ok() before calling this
333 * Returns zero on success, or -EFAULT on error.
335 #define __put_user(x, ptr) \
337 register long __pu_err = 0; \
338 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
339 __chk_user_ptr(__gu_ptr); \
340 switch (sizeof(*__gu_ptr)) { \
342 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
345 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
348 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
351 __put_user_8((x), __gu_ptr, __pu_err); \
360 * put_user: - Write a simple value into user space.
361 * @x: Value to copy to user space.
362 * @ptr: Destination address, in user space.
364 * Context: User context only. This function may sleep.
366 * This macro copies a single simple value from kernel space to user
367 * space. It supports simple types like char and int, but not larger
368 * data types like structures or arrays.
370 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
371 * to the result of dereferencing @ptr.
373 * Returns zero on success, or -EFAULT on error.
375 #define put_user(x, ptr) \
377 __typeof__(*(ptr)) __user *__p = (ptr); \
379 access_ok(__p, sizeof(*__p)) ? \
380 __put_user((x), __p) : \
385 extern unsigned long __must_check __asm_copy_to_user(void __user *to,
386 const void *from, unsigned long n);
387 extern unsigned long __must_check __asm_copy_from_user(void *to,
388 const void __user *from, unsigned long n);
390 static inline unsigned long
391 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
393 return __asm_copy_from_user(to, from, n);
396 static inline unsigned long
397 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
399 return __asm_copy_to_user(to, from, n);
402 extern long strncpy_from_user(char *dest, const char __user *src, long count);
404 extern long __must_check strlen_user(const char __user *str);
405 extern long __must_check strnlen_user(const char __user *str, long n);
408 unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
411 unsigned long __must_check clear_user(void __user *to, unsigned long n)
414 return access_ok(to, n) ?
415 __clear_user(to, n) : n;
419 * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults
420 * will set "err" to -EFAULT, while successful accesses return the previous
423 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
425 __typeof__(ptr) __ptr = (ptr); \
426 __typeof__(*(ptr)) __old = (old); \
427 __typeof__(*(ptr)) __new = (new); \
428 __typeof__(*(ptr)) __ret; \
429 __typeof__(err) __err = 0; \
430 register unsigned int __rc; \
431 __enable_user_access(); \
434 __asm__ __volatile__ ( \
436 " lr.w" #scb " %[ret], %[ptr]\n" \
437 " bne %[ret], %z[old], 1f\n" \
438 " sc.w" #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)); \
460 __asm__ __volatile__ ( \
462 " lr.d" #scb " %[ret], %[ptr]\n" \
463 " bne %[ret], %z[old], 1f\n" \
464 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
465 " bnez %[rc], 0b\n" \
467 ".section .fixup,\"ax\"\n" \
470 " li %[err], %[efault]\n" \
471 " jump 1b, %[rc]\n" \
473 ".section __ex_table,\"a\"\n" \
474 ".balign " RISCV_SZPTR "\n" \
475 " " RISCV_PTR " 1b, 2b\n" \
477 : [ret] "=&r" (__ret), \
479 [ptr] "+A" (*__ptr), \
480 [err] "=&r" (__err) \
481 : [old] "rJ" (__old), \
482 [new] "rJ" (__new), \
483 [efault] "i" (-EFAULT)); \
488 __disable_user_access(); \
493 #endif /* _ASM_RISCV_UACCESS_H */