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 KERNEL_DS (~0UL)
43 #define USER_DS (TASK_SIZE)
45 #define get_fs() (current_thread_info()->addr_limit)
47 static inline void set_fs(mm_segment_t fs)
49 current_thread_info()->addr_limit = fs;
52 #define segment_eq(a, b) ((a) == (b))
54 #define user_addr_max() (get_fs())
58 * access_ok: - Checks if a user space pointer is valid
59 * @addr: User space pointer to start of block to check
60 * @size: Size of block to check
62 * Context: User context only. This function may sleep.
64 * Checks if a pointer to a block of memory in user space is valid.
66 * Returns true (nonzero) if the memory block may be valid, false (zero)
67 * if it is definitely invalid.
69 * Note that, depending on architecture, this function probably just
70 * checks that the pointer is in the user space range - after calling
71 * this function, memory access functions may still return -EFAULT.
73 #define access_ok(addr, size) ({ \
74 __chk_user_ptr(addr); \
75 likely(__access_ok((unsigned long __force)(addr), (size))); \
79 * Ensure that the range [addr, addr+size) is within the process's
82 static inline int __access_ok(unsigned long addr, unsigned long size)
84 const mm_segment_t fs = get_fs();
86 return (size <= fs) && (addr <= (fs - size));
90 * The exception table consists of pairs of addresses: the first is the
91 * address of an instruction that is allowed to fault, and the second is
92 * the address at which the program should continue. No registers are
93 * modified, so it is entirely up to the continuation code to figure out
96 * All the routines below use bits of fixup code that are out of line
97 * with the main instruction path. This means when everything is well,
98 * we don't even have to jump over them. Further, they do not intrude
99 * on our cache or tlb entries.
102 #if defined(__LITTLE_ENDIAN)
105 #elif defined(__BIG_ENDIAN)
109 #error "Unknown endianness"
113 * The "__xxx" versions of the user access functions do not verify the address
114 * space - it must have been done previously with a separate "access_ok()"
118 #define __get_user_asm(insn, x, ptr, err) \
122 __enable_user_access(); \
123 __asm__ __volatile__ ( \
125 " " insn " %1, %3\n" \
127 " .section .fixup,\"ax\"\n" \
134 " .section __ex_table,\"a\"\n" \
135 " .balign " RISCV_SZPTR "\n" \
136 " " RISCV_PTR " 1b, 3b\n" \
138 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
139 : "m" (*(ptr)), "i" (-EFAULT)); \
140 __disable_user_access(); \
145 #define __get_user_8(x, ptr, err) \
146 __get_user_asm("ld", x, ptr, err)
147 #else /* !CONFIG_64BIT */
148 #define __get_user_8(x, ptr, err) \
150 u32 __user *__ptr = (u32 __user *)(ptr); \
153 __enable_user_access(); \
154 __asm__ __volatile__ ( \
160 " .section .fixup,\"ax\"\n" \
168 " .section __ex_table,\"a\"\n" \
169 " .balign " RISCV_SZPTR "\n" \
170 " " RISCV_PTR " 1b, 4b\n" \
171 " " RISCV_PTR " 2b, 4b\n" \
173 : "+r" (err), "=&r" (__lo), "=r" (__hi), \
175 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
177 __disable_user_access(); \
178 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
179 (((u64)__hi << 32) | __lo))); \
181 #endif /* CONFIG_64BIT */
185 * __get_user: - Get a simple variable from user space, with less checking.
186 * @x: Variable to store result.
187 * @ptr: Source address, in user space.
189 * Context: User context only. This function may sleep.
191 * This macro copies a single simple variable from user space to kernel
192 * space. It supports simple types like char and int, but not larger
193 * data types like structures or arrays.
195 * @ptr must have pointer-to-simple-variable type, and the result of
196 * dereferencing @ptr must be assignable to @x without a cast.
198 * Caller must check the pointer with access_ok() before calling this
201 * Returns zero on success, or -EFAULT on error.
202 * On error, the variable @x is set to zero.
204 #define __get_user(x, ptr) \
206 register long __gu_err = 0; \
207 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
208 __chk_user_ptr(__gu_ptr); \
209 switch (sizeof(*__gu_ptr)) { \
211 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
214 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
217 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
220 __get_user_8((x), __gu_ptr, __gu_err); \
229 * get_user: - Get a simple variable from user space.
230 * @x: Variable to store result.
231 * @ptr: Source address, in user space.
233 * Context: User context only. This function may sleep.
235 * This macro copies a single simple variable from user space to kernel
236 * space. It supports simple types like char and int, but not larger
237 * data types like structures or arrays.
239 * @ptr must have pointer-to-simple-variable type, and the result of
240 * dereferencing @ptr must be assignable to @x without a cast.
242 * Returns zero on success, or -EFAULT on error.
243 * On error, the variable @x is set to zero.
245 #define get_user(x, ptr) \
247 const __typeof__(*(ptr)) __user *__p = (ptr); \
249 access_ok(__p, sizeof(*__p)) ? \
250 __get_user((x), __p) : \
251 ((x) = 0, -EFAULT); \
254 #define __put_user_asm(insn, x, ptr, err) \
257 __typeof__(*(ptr)) __x = x; \
258 __enable_user_access(); \
259 __asm__ __volatile__ ( \
261 " " insn " %z3, %2\n" \
263 " .section .fixup,\"ax\"\n" \
269 " .section __ex_table,\"a\"\n" \
270 " .balign " RISCV_SZPTR "\n" \
271 " " RISCV_PTR " 1b, 3b\n" \
273 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
274 : "rJ" (__x), "i" (-EFAULT)); \
275 __disable_user_access(); \
279 #define __put_user_8(x, ptr, err) \
280 __put_user_asm("sd", x, ptr, err)
281 #else /* !CONFIG_64BIT */
282 #define __put_user_8(x, ptr, err) \
284 u32 __user *__ptr = (u32 __user *)(ptr); \
285 u64 __x = (__typeof__((x)-(x)))(x); \
287 __enable_user_access(); \
288 __asm__ __volatile__ ( \
294 " .section .fixup,\"ax\"\n" \
300 " .section __ex_table,\"a\"\n" \
301 " .balign " RISCV_SZPTR "\n" \
302 " " RISCV_PTR " 1b, 4b\n" \
303 " " RISCV_PTR " 2b, 4b\n" \
305 : "+r" (err), "=r" (__tmp), \
306 "=m" (__ptr[__LSW]), \
307 "=m" (__ptr[__MSW]) \
308 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
309 __disable_user_access(); \
311 #endif /* CONFIG_64BIT */
315 * __put_user: - Write a simple value into user space, with less checking.
316 * @x: Value to copy to user space.
317 * @ptr: Destination address, in user space.
319 * Context: User context only. This function may sleep.
321 * This macro copies a single simple value from kernel space to user
322 * space. It supports simple types like char and int, but not larger
323 * data types like structures or arrays.
325 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
326 * to the result of dereferencing @ptr.
328 * Caller must check the pointer with access_ok() before calling this
331 * Returns zero on success, or -EFAULT on error.
333 #define __put_user(x, ptr) \
335 register long __pu_err = 0; \
336 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
337 __chk_user_ptr(__gu_ptr); \
338 switch (sizeof(*__gu_ptr)) { \
340 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
343 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
346 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
349 __put_user_8((x), __gu_ptr, __pu_err); \
358 * put_user: - Write a simple value into user space.
359 * @x: Value to copy to user space.
360 * @ptr: Destination address, in user space.
362 * Context: User context only. This function may sleep.
364 * This macro copies a single simple value from kernel space to user
365 * space. It supports simple types like char and int, but not larger
366 * data types like structures or arrays.
368 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
369 * to the result of dereferencing @ptr.
371 * Returns zero on success, or -EFAULT on error.
373 #define put_user(x, ptr) \
375 __typeof__(*(ptr)) __user *__p = (ptr); \
377 access_ok(__p, sizeof(*__p)) ? \
378 __put_user((x), __p) : \
383 extern unsigned long __must_check __asm_copy_to_user(void __user *to,
384 const void *from, unsigned long n);
385 extern unsigned long __must_check __asm_copy_from_user(void *to,
386 const void __user *from, unsigned long n);
388 static inline unsigned long
389 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
391 return __asm_copy_from_user(to, from, n);
394 static inline unsigned long
395 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
397 return __asm_copy_to_user(to, from, n);
400 extern long strncpy_from_user(char *dest, const char __user *src, long count);
402 extern long __must_check strlen_user(const char __user *str);
403 extern long __must_check strnlen_user(const char __user *str, long n);
406 unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
409 unsigned long __must_check clear_user(void __user *to, unsigned long n)
412 return access_ok(to, n) ?
413 __clear_user(to, n) : n;
417 * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults
418 * will set "err" to -EFAULT, while successful accesses return the previous
421 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
423 __typeof__(ptr) __ptr = (ptr); \
424 __typeof__(*(ptr)) __old = (old); \
425 __typeof__(*(ptr)) __new = (new); \
426 __typeof__(*(ptr)) __ret; \
427 __typeof__(err) __err = 0; \
428 register unsigned int __rc; \
429 __enable_user_access(); \
432 __asm__ __volatile__ ( \
434 " lr.w" #scb " %[ret], %[ptr]\n" \
435 " bne %[ret], %z[old], 1f\n" \
436 " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \
437 " bnez %[rc], 0b\n" \
439 ".section .fixup,\"ax\"\n" \
442 " li %[err], %[efault]\n" \
443 " jump 1b, %[rc]\n" \
445 ".section __ex_table,\"a\"\n" \
446 ".balign " RISCV_SZPTR "\n" \
447 " " RISCV_PTR " 1b, 2b\n" \
449 : [ret] "=&r" (__ret), \
451 [ptr] "+A" (*__ptr), \
452 [err] "=&r" (__err) \
453 : [old] "rJ" (__old), \
454 [new] "rJ" (__new), \
455 [efault] "i" (-EFAULT)); \
458 __asm__ __volatile__ ( \
460 " lr.d" #scb " %[ret], %[ptr]\n" \
461 " bne %[ret], %z[old], 1f\n" \
462 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
463 " bnez %[rc], 0b\n" \
465 ".section .fixup,\"ax\"\n" \
468 " li %[err], %[efault]\n" \
469 " jump 1b, %[rc]\n" \
471 ".section __ex_table,\"a\"\n" \
472 ".balign " RISCV_SZPTR "\n" \
473 " " RISCV_PTR " 1b, 2b\n" \
475 : [ret] "=&r" (__ret), \
477 [ptr] "+A" (*__ptr), \
478 [err] "=&r" (__err) \
479 : [old] "rJ" (__old), \
480 [new] "rJ" (__new), \
481 [efault] "i" (-EFAULT)); \
486 __disable_user_access(); \
491 #endif /* _ASM_RISCV_UACCESS_H */