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>
28 #define __enable_user_access() \
29 __asm__ __volatile__ ("csrs sstatus, %0" : : "r" (SR_SUM) : "memory")
30 #define __disable_user_access() \
31 __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory")
34 * The fs value determines whether argument validity checking should be
35 * performed or not. If get_fs() == USER_DS, checking is performed, with
36 * get_fs() == KERNEL_DS, checking is bypassed.
38 * For historical reasons, these macros are grossly misnamed.
41 #define KERNEL_DS (~0UL)
42 #define USER_DS (TASK_SIZE)
44 #define get_ds() (KERNEL_DS)
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 #define VERIFY_WRITE 1
61 * access_ok: - Checks if a user space pointer is valid
62 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
63 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
64 * to write to a block, it is always safe to read from it.
65 * @addr: User space pointer to start of block to check
66 * @size: Size of block to check
68 * Context: User context only. This function may sleep.
70 * Checks if a pointer to a block of memory in user space is valid.
72 * Returns true (nonzero) if the memory block may be valid, false (zero)
73 * if it is definitely invalid.
75 * Note that, depending on architecture, this function probably just
76 * checks that the pointer is in the user space range - after calling
77 * this function, memory access functions may still return -EFAULT.
79 #define access_ok(type, addr, size) ({ \
80 __chk_user_ptr(addr); \
81 likely(__access_ok((unsigned long __force)(addr), (size))); \
85 * Ensure that the range [addr, addr+size) is within the process's
88 static inline int __access_ok(unsigned long addr
, unsigned long size
)
90 const mm_segment_t fs
= get_fs();
92 return (size
<= fs
) && (addr
<= (fs
- size
));
96 * The exception table consists of pairs of addresses: the first is the
97 * address of an instruction that is allowed to fault, and the second is
98 * the address at which the program should continue. No registers are
99 * modified, so it is entirely up to the continuation code to figure out
102 * All the routines below use bits of fixup code that are out of line
103 * with the main instruction path. This means when everything is well,
104 * we don't even have to jump over them. Further, they do not intrude
105 * on our cache or tlb entries.
108 struct exception_table_entry
{
109 unsigned long insn
, fixup
;
112 extern int fixup_exception(struct pt_regs
*state
);
114 #if defined(__LITTLE_ENDIAN)
117 #elif defined(__BIG_ENDIAN)
121 #error "Unknown endianness"
125 * The "__xxx" versions of the user access functions do not verify the address
126 * space - it must have been done previously with a separate "access_ok()"
130 #define __get_user_asm(insn, x, ptr, err) \
134 __enable_user_access(); \
135 __asm__ __volatile__ ( \
137 " " insn " %1, %3\n" \
139 " .section .fixup,\"ax\"\n" \
146 " .section __ex_table,\"a\"\n" \
147 " .balign " RISCV_SZPTR "\n" \
148 " " RISCV_PTR " 1b, 3b\n" \
150 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
151 : "m" (*(ptr)), "i" (-EFAULT)); \
152 __disable_user_access(); \
157 #define __get_user_8(x, ptr, err) \
158 __get_user_asm("ld", x, ptr, err)
159 #else /* !CONFIG_64BIT */
160 #define __get_user_8(x, ptr, err) \
162 u32 __user *__ptr = (u32 __user *)(ptr); \
165 __enable_user_access(); \
166 __asm__ __volatile__ ( \
172 " .section .fixup,\"ax\"\n" \
180 " .section __ex_table,\"a\"\n" \
181 " .balign " RISCV_SZPTR "\n" \
182 " " RISCV_PTR " 1b, 4b\n" \
183 " " RISCV_PTR " 2b, 4b\n" \
185 : "+r" (err), "=&r" (__lo), "=r" (__hi), \
187 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
189 __disable_user_access(); \
190 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
191 (((u64)__hi << 32) | __lo))); \
193 #endif /* CONFIG_64BIT */
197 * __get_user: - Get a simple variable from user space, with less checking.
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 * Caller must check the pointer with access_ok() before calling this
213 * Returns zero on success, or -EFAULT on error.
214 * On error, the variable @x is set to zero.
216 #define __get_user(x, ptr) \
218 register long __gu_err = 0; \
219 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
220 __chk_user_ptr(__gu_ptr); \
221 switch (sizeof(*__gu_ptr)) { \
223 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
226 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
229 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
232 __get_user_8((x), __gu_ptr, __gu_err); \
241 * get_user: - Get a simple variable from user space.
242 * @x: Variable to store result.
243 * @ptr: Source address, in user space.
245 * Context: User context only. This function may sleep.
247 * This macro copies a single simple variable from user space to kernel
248 * space. It supports simple types like char and int, but not larger
249 * data types like structures or arrays.
251 * @ptr must have pointer-to-simple-variable type, and the result of
252 * dereferencing @ptr must be assignable to @x without a cast.
254 * Returns zero on success, or -EFAULT on error.
255 * On error, the variable @x is set to zero.
257 #define get_user(x, ptr) \
259 const __typeof__(*(ptr)) __user *__p = (ptr); \
261 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
262 __get_user((x), __p) : \
263 ((x) = 0, -EFAULT); \
266 #define __put_user_asm(insn, x, ptr, err) \
269 __typeof__(*(ptr)) __x = x; \
270 __enable_user_access(); \
271 __asm__ __volatile__ ( \
273 " " insn " %z3, %2\n" \
275 " .section .fixup,\"ax\"\n" \
281 " .section __ex_table,\"a\"\n" \
282 " .balign " RISCV_SZPTR "\n" \
283 " " RISCV_PTR " 1b, 3b\n" \
285 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
286 : "rJ" (__x), "i" (-EFAULT)); \
287 __disable_user_access(); \
291 #define __put_user_8(x, ptr, err) \
292 __put_user_asm("sd", x, ptr, err)
293 #else /* !CONFIG_64BIT */
294 #define __put_user_8(x, ptr, err) \
296 u32 __user *__ptr = (u32 __user *)(ptr); \
297 u64 __x = (__typeof__((x)-(x)))(x); \
299 __enable_user_access(); \
300 __asm__ __volatile__ ( \
306 " .section .fixup,\"ax\"\n" \
312 " .section __ex_table,\"a\"\n" \
313 " .balign " RISCV_SZPTR "\n" \
314 " " RISCV_PTR " 1b, 4b\n" \
315 " " RISCV_PTR " 2b, 4b\n" \
317 : "+r" (err), "=r" (__tmp), \
318 "=m" (__ptr[__LSW]), \
319 "=m" (__ptr[__MSW]) \
320 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
321 __disable_user_access(); \
323 #endif /* CONFIG_64BIT */
327 * __put_user: - Write a simple value into user space, with less checking.
328 * @x: Value to copy to user space.
329 * @ptr: Destination address, in user space.
331 * Context: User context only. This function may sleep.
333 * This macro copies a single simple value from kernel space to user
334 * space. It supports simple types like char and int, but not larger
335 * data types like structures or arrays.
337 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
338 * to the result of dereferencing @ptr.
340 * Caller must check the pointer with access_ok() before calling this
343 * Returns zero on success, or -EFAULT on error.
345 #define __put_user(x, ptr) \
347 register long __pu_err = 0; \
348 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
349 __chk_user_ptr(__gu_ptr); \
350 switch (sizeof(*__gu_ptr)) { \
352 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
355 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
358 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
361 __put_user_8((x), __gu_ptr, __pu_err); \
370 * put_user: - Write a simple value into user space.
371 * @x: Value to copy to user space.
372 * @ptr: Destination address, in user space.
374 * Context: User context only. This function may sleep.
376 * This macro copies a single simple value from kernel space to user
377 * space. It supports simple types like char and int, but not larger
378 * data types like structures or arrays.
380 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
381 * to the result of dereferencing @ptr.
383 * Returns zero on success, or -EFAULT on error.
385 #define put_user(x, ptr) \
387 __typeof__(*(ptr)) __user *__p = (ptr); \
389 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
390 __put_user((x), __p) : \
395 extern unsigned long __must_check
__copy_user(void __user
*to
,
396 const void __user
*from
, unsigned long n
);
398 static inline unsigned long
399 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
401 return __copy_user(to
, from
, n
);
404 static inline unsigned long
405 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
407 return __copy_user(to
, from
, n
);
410 extern long strncpy_from_user(char *dest
, const char __user
*src
, long count
);
412 extern long __must_check
strlen_user(const char __user
*str
);
413 extern long __must_check
strnlen_user(const char __user
*str
, long n
);
416 unsigned long __must_check
__clear_user(void __user
*addr
, unsigned long n
);
419 unsigned long __must_check
clear_user(void __user
*to
, unsigned long n
)
422 return access_ok(VERIFY_WRITE
, to
, n
) ?
423 __clear_user(to
, n
) : n
;
427 * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults
428 * will set "err" to -EFAULT, while successful accesses return the previous
431 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
433 __typeof__(ptr) __ptr = (ptr); \
434 __typeof__(*(ptr)) __old = (old); \
435 __typeof__(*(ptr)) __new = (new); \
436 __typeof__(*(ptr)) __ret; \
437 __typeof__(err) __err = 0; \
438 register unsigned int __rc; \
439 __enable_user_access(); \
442 __asm__ __volatile__ ( \
444 " lr.w" #scb " %[ret], %[ptr]\n" \
445 " bne %[ret], %z[old], 1f\n" \
446 " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \
447 " bnez %[rc], 0b\n" \
449 ".section .fixup,\"ax\"\n" \
452 " li %[err], %[efault]\n" \
453 " jump 1b, %[rc]\n" \
455 ".section __ex_table,\"a\"\n" \
456 ".balign " RISCV_SZPTR "\n" \
457 " " RISCV_PTR " 1b, 2b\n" \
459 : [ret] "=&r" (__ret), \
461 [ptr] "+A" (*__ptr), \
462 [err] "=&r" (__err) \
463 : [old] "rJ" (__old), \
464 [new] "rJ" (__new), \
465 [efault] "i" (-EFAULT)); \
468 __asm__ __volatile__ ( \
470 " lr.d" #scb " %[ret], %[ptr]\n" \
471 " bne %[ret], %z[old], 1f\n" \
472 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
473 " bnez %[rc], 0b\n" \
475 ".section .fixup,\"ax\"\n" \
478 " li %[err], %[efault]\n" \
479 " jump 1b, %[rc]\n" \
481 ".section __ex_table,\"a\"\n" \
482 ".balign " RISCV_SZPTR "\n" \
483 " " RISCV_PTR " 1b, 2b\n" \
485 : [ret] "=&r" (__ret), \
487 [ptr] "+A" (*__ptr), \
488 [err] "=&r" (__err) \
489 : [old] "rJ" (__old), \
490 [new] "rJ" (__new), \
491 [efault] "i" (-EFAULT)); \
496 __disable_user_access(); \
501 #endif /* _ASM_RISCV_UACCESS_H */