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 * 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 struct exception_table_entry {
103 unsigned long insn, fixup;
106 extern int fixup_exception(struct pt_regs *state);
108 #if defined(__LITTLE_ENDIAN)
111 #elif defined(__BIG_ENDIAN)
115 #error "Unknown endianness"
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) : \
389 extern unsigned long __must_check __asm_copy_to_user(void __user *to,
390 const void *from, unsigned long n);
391 extern unsigned long __must_check __asm_copy_from_user(void *to,
392 const void __user *from, unsigned long n);
394 static inline unsigned long
395 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
397 return __asm_copy_from_user(to, from, n);
400 static inline unsigned long
401 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
403 return __asm_copy_to_user(to, from, n);
406 extern long strncpy_from_user(char *dest, const char __user *src, long count);
408 extern long __must_check strlen_user(const char __user *str);
409 extern long __must_check strnlen_user(const char __user *str, long n);
412 unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
415 unsigned long __must_check clear_user(void __user *to, unsigned long n)
418 return access_ok(to, n) ?
419 __clear_user(to, n) : n;
423 * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults
424 * will set "err" to -EFAULT, while successful accesses return the previous
427 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
429 __typeof__(ptr) __ptr = (ptr); \
430 __typeof__(*(ptr)) __old = (old); \
431 __typeof__(*(ptr)) __new = (new); \
432 __typeof__(*(ptr)) __ret; \
433 __typeof__(err) __err = 0; \
434 register unsigned int __rc; \
435 __enable_user_access(); \
438 __asm__ __volatile__ ( \
440 " lr.w" #scb " %[ret], %[ptr]\n" \
441 " bne %[ret], %z[old], 1f\n" \
442 " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \
443 " bnez %[rc], 0b\n" \
445 ".section .fixup,\"ax\"\n" \
448 " li %[err], %[efault]\n" \
449 " jump 1b, %[rc]\n" \
451 ".section __ex_table,\"a\"\n" \
452 ".balign " RISCV_SZPTR "\n" \
453 " " RISCV_PTR " 1b, 2b\n" \
455 : [ret] "=&r" (__ret), \
457 [ptr] "+A" (*__ptr), \
458 [err] "=&r" (__err) \
459 : [old] "rJ" (__old), \
460 [new] "rJ" (__new), \
461 [efault] "i" (-EFAULT)); \
464 __asm__ __volatile__ ( \
466 " lr.d" #scb " %[ret], %[ptr]\n" \
467 " bne %[ret], %z[old], 1f\n" \
468 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
469 " bnez %[rc], 0b\n" \
471 ".section .fixup,\"ax\"\n" \
474 " li %[err], %[efault]\n" \
475 " jump 1b, %[rc]\n" \
477 ".section __ex_table,\"a\"\n" \
478 ".balign " RISCV_SZPTR "\n" \
479 " " RISCV_PTR " 1b, 2b\n" \
481 : [ret] "=&r" (__ret), \
483 [ptr] "+A" (*__ptr), \
484 [err] "=&r" (__err) \
485 : [old] "rJ" (__old), \
486 [new] "rJ" (__new), \
487 [efault] "i" (-EFAULT)); \
492 __disable_user_access(); \
497 #endif /* _ASM_RISCV_UACCESS_H */