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_fs() (current_thread_info()->addr_limit)
46 static inline void set_fs(mm_segment_t fs
)
48 current_thread_info()->addr_limit
= fs
;
51 #define segment_eq(a, b) ((a) == (b))
53 #define user_addr_max() (get_fs())
57 * access_ok: - Checks if a user space pointer is valid
58 * @addr: User space pointer to start of block to check
59 * @size: Size of block to check
61 * Context: User context only. This function may sleep.
63 * Checks if a pointer to a block of memory in user space is valid.
65 * Returns true (nonzero) if the memory block may be valid, false (zero)
66 * if it is definitely invalid.
68 * Note that, depending on architecture, this function probably just
69 * checks that the pointer is in the user space range - after calling
70 * this function, memory access functions may still return -EFAULT.
72 #define access_ok(addr, size) ({ \
73 __chk_user_ptr(addr); \
74 likely(__access_ok((unsigned long __force)(addr), (size))); \
78 * Ensure that the range [addr, addr+size) is within the process's
81 static inline int __access_ok(unsigned long addr
, unsigned long size
)
83 const mm_segment_t fs
= get_fs();
85 return (size
<= fs
) && (addr
<= (fs
- size
));
89 * The exception table consists of pairs of addresses: the first is the
90 * address of an instruction that is allowed to fault, and the second is
91 * the address at which the program should continue. No registers are
92 * modified, so it is entirely up to the continuation code to figure out
95 * All the routines below use bits of fixup code that are out of line
96 * with the main instruction path. This means when everything is well,
97 * we don't even have to jump over them. Further, they do not intrude
98 * on our cache or tlb entries.
101 struct exception_table_entry
{
102 unsigned long insn
, fixup
;
105 extern int fixup_exception(struct pt_regs
*state
);
107 #if defined(__LITTLE_ENDIAN)
110 #elif defined(__BIG_ENDIAN)
114 #error "Unknown endianness"
118 * The "__xxx" versions of the user access functions do not verify the address
119 * space - it must have been done previously with a separate "access_ok()"
123 #define __get_user_asm(insn, x, ptr, err) \
127 __enable_user_access(); \
128 __asm__ __volatile__ ( \
130 " " insn " %1, %3\n" \
132 " .section .fixup,\"ax\"\n" \
139 " .section __ex_table,\"a\"\n" \
140 " .balign " RISCV_SZPTR "\n" \
141 " " RISCV_PTR " 1b, 3b\n" \
143 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
144 : "m" (*(ptr)), "i" (-EFAULT)); \
145 __disable_user_access(); \
150 #define __get_user_8(x, ptr, err) \
151 __get_user_asm("ld", x, ptr, err)
152 #else /* !CONFIG_64BIT */
153 #define __get_user_8(x, ptr, err) \
155 u32 __user *__ptr = (u32 __user *)(ptr); \
158 __enable_user_access(); \
159 __asm__ __volatile__ ( \
165 " .section .fixup,\"ax\"\n" \
173 " .section __ex_table,\"a\"\n" \
174 " .balign " RISCV_SZPTR "\n" \
175 " " RISCV_PTR " 1b, 4b\n" \
176 " " RISCV_PTR " 2b, 4b\n" \
178 : "+r" (err), "=&r" (__lo), "=r" (__hi), \
180 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
182 __disable_user_access(); \
183 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
184 (((u64)__hi << 32) | __lo))); \
186 #endif /* CONFIG_64BIT */
190 * __get_user: - Get a simple variable from user space, with less checking.
191 * @x: Variable to store result.
192 * @ptr: Source address, in user space.
194 * Context: User context only. This function may sleep.
196 * This macro copies a single simple variable from user space to kernel
197 * space. It supports simple types like char and int, but not larger
198 * data types like structures or arrays.
200 * @ptr must have pointer-to-simple-variable type, and the result of
201 * dereferencing @ptr must be assignable to @x without a cast.
203 * Caller must check the pointer with access_ok() before calling this
206 * Returns zero on success, or -EFAULT on error.
207 * On error, the variable @x is set to zero.
209 #define __get_user(x, ptr) \
211 register long __gu_err = 0; \
212 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
213 __chk_user_ptr(__gu_ptr); \
214 switch (sizeof(*__gu_ptr)) { \
216 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
219 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
222 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
225 __get_user_8((x), __gu_ptr, __gu_err); \
234 * get_user: - Get a simple variable from user space.
235 * @x: Variable to store result.
236 * @ptr: Source address, in user space.
238 * Context: User context only. This function may sleep.
240 * This macro copies a single simple variable from user space to kernel
241 * space. It supports simple types like char and int, but not larger
242 * data types like structures or arrays.
244 * @ptr must have pointer-to-simple-variable type, and the result of
245 * dereferencing @ptr must be assignable to @x without a cast.
247 * Returns zero on success, or -EFAULT on error.
248 * On error, the variable @x is set to zero.
250 #define get_user(x, ptr) \
252 const __typeof__(*(ptr)) __user *__p = (ptr); \
254 access_ok(__p, sizeof(*__p)) ? \
255 __get_user((x), __p) : \
256 ((x) = 0, -EFAULT); \
259 #define __put_user_asm(insn, x, ptr, err) \
262 __typeof__(*(ptr)) __x = x; \
263 __enable_user_access(); \
264 __asm__ __volatile__ ( \
266 " " insn " %z3, %2\n" \
268 " .section .fixup,\"ax\"\n" \
274 " .section __ex_table,\"a\"\n" \
275 " .balign " RISCV_SZPTR "\n" \
276 " " RISCV_PTR " 1b, 3b\n" \
278 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
279 : "rJ" (__x), "i" (-EFAULT)); \
280 __disable_user_access(); \
284 #define __put_user_8(x, ptr, err) \
285 __put_user_asm("sd", x, ptr, err)
286 #else /* !CONFIG_64BIT */
287 #define __put_user_8(x, ptr, err) \
289 u32 __user *__ptr = (u32 __user *)(ptr); \
290 u64 __x = (__typeof__((x)-(x)))(x); \
292 __enable_user_access(); \
293 __asm__ __volatile__ ( \
299 " .section .fixup,\"ax\"\n" \
305 " .section __ex_table,\"a\"\n" \
306 " .balign " RISCV_SZPTR "\n" \
307 " " RISCV_PTR " 1b, 4b\n" \
308 " " RISCV_PTR " 2b, 4b\n" \
310 : "+r" (err), "=r" (__tmp), \
311 "=m" (__ptr[__LSW]), \
312 "=m" (__ptr[__MSW]) \
313 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
314 __disable_user_access(); \
316 #endif /* CONFIG_64BIT */
320 * __put_user: - Write a simple value into user space, with less checking.
321 * @x: Value to copy to user space.
322 * @ptr: Destination address, in user space.
324 * Context: User context only. This function may sleep.
326 * This macro copies a single simple value from kernel space to user
327 * space. It supports simple types like char and int, but not larger
328 * data types like structures or arrays.
330 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
331 * to the result of dereferencing @ptr.
333 * Caller must check the pointer with access_ok() before calling this
336 * Returns zero on success, or -EFAULT on error.
338 #define __put_user(x, ptr) \
340 register long __pu_err = 0; \
341 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
342 __chk_user_ptr(__gu_ptr); \
343 switch (sizeof(*__gu_ptr)) { \
345 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
348 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
351 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
354 __put_user_8((x), __gu_ptr, __pu_err); \
363 * put_user: - Write a simple value into user space.
364 * @x: Value to copy to user space.
365 * @ptr: Destination address, in user space.
367 * Context: User context only. This function may sleep.
369 * This macro copies a single simple value from kernel space to user
370 * space. It supports simple types like char and int, but not larger
371 * data types like structures or arrays.
373 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
374 * to the result of dereferencing @ptr.
376 * Returns zero on success, or -EFAULT on error.
378 #define put_user(x, ptr) \
380 __typeof__(*(ptr)) __user *__p = (ptr); \
382 access_ok(__p, sizeof(*__p)) ? \
383 __put_user((x), __p) : \
388 extern unsigned long __must_check
__asm_copy_to_user(void __user
*to
,
389 const void *from
, unsigned long n
);
390 extern unsigned long __must_check
__asm_copy_from_user(void *to
,
391 const void __user
*from
, unsigned long n
);
393 static inline unsigned long
394 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
396 return __asm_copy_from_user(to
, from
, n
);
399 static inline unsigned long
400 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
402 return __asm_copy_to_user(to
, from
, n
);
405 extern long strncpy_from_user(char *dest
, const char __user
*src
, long count
);
407 extern long __must_check
strlen_user(const char __user
*str
);
408 extern long __must_check
strnlen_user(const char __user
*str
, long n
);
411 unsigned long __must_check
__clear_user(void __user
*addr
, unsigned long n
);
414 unsigned long __must_check
clear_user(void __user
*to
, unsigned long n
)
417 return access_ok(to
, n
) ?
418 __clear_user(to
, n
) : n
;
422 * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults
423 * will set "err" to -EFAULT, while successful accesses return the previous
426 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
428 __typeof__(ptr) __ptr = (ptr); \
429 __typeof__(*(ptr)) __old = (old); \
430 __typeof__(*(ptr)) __new = (new); \
431 __typeof__(*(ptr)) __ret; \
432 __typeof__(err) __err = 0; \
433 register unsigned int __rc; \
434 __enable_user_access(); \
437 __asm__ __volatile__ ( \
439 " lr.w" #scb " %[ret], %[ptr]\n" \
440 " bne %[ret], %z[old], 1f\n" \
441 " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \
442 " bnez %[rc], 0b\n" \
444 ".section .fixup,\"ax\"\n" \
447 " li %[err], %[efault]\n" \
448 " jump 1b, %[rc]\n" \
450 ".section __ex_table,\"a\"\n" \
451 ".balign " RISCV_SZPTR "\n" \
452 " " RISCV_PTR " 1b, 2b\n" \
454 : [ret] "=&r" (__ret), \
456 [ptr] "+A" (*__ptr), \
457 [err] "=&r" (__err) \
458 : [old] "rJ" (__old), \
459 [new] "rJ" (__new), \
460 [efault] "i" (-EFAULT)); \
463 __asm__ __volatile__ ( \
465 " lr.d" #scb " %[ret], %[ptr]\n" \
466 " bne %[ret], %z[old], 1f\n" \
467 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
468 " bnez %[rc], 0b\n" \
470 ".section .fixup,\"ax\"\n" \
473 " li %[err], %[efault]\n" \
474 " jump 1b, %[rc]\n" \
476 ".section __ex_table,\"a\"\n" \
477 ".balign " RISCV_SZPTR "\n" \
478 " " RISCV_PTR " 1b, 2b\n" \
480 : [ret] "=&r" (__ret), \
482 [ptr] "+A" (*__ptr), \
483 [err] "=&r" (__err) \
484 : [old] "rJ" (__old), \
485 [new] "rJ" (__new), \
486 [efault] "i" (-EFAULT)); \
491 __disable_user_access(); \
496 #endif /* _ASM_RISCV_UACCESS_H */