]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/riscv/include/asm/uaccess.h
cc5b253d4c57b68e7d33f3bb97b02769a45d8fda
[linux.git] / arch / riscv / include / asm / uaccess.h
1 /*
2  * Copyright (C) 2012 Regents of the University of California
3  *
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.
7  *
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.
12  *
13  * This file was copied from include/asm-generic/uaccess.h
14  */
15
16 #ifndef _ASM_RISCV_UACCESS_H
17 #define _ASM_RISCV_UACCESS_H
18
19 /*
20  * User space memory access functions
21  */
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>
27 #include <asm/asm.h>
28
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")
33
34 /*
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.
38  *
39  * For historical reasons, these macros are grossly misnamed.
40  */
41
42 #define KERNEL_DS       (~0UL)
43 #define USER_DS         (TASK_SIZE)
44
45 #define get_fs()        (current_thread_info()->addr_limit)
46
47 static inline void set_fs(mm_segment_t fs)
48 {
49         current_thread_info()->addr_limit = fs;
50 }
51
52 #define segment_eq(a, b) ((a) == (b))
53
54 #define user_addr_max() (get_fs())
55
56
57 /**
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
61  *
62  * Context: User context only.  This function may sleep.
63  *
64  * Checks if a pointer to a block of memory in user space is valid.
65  *
66  * Returns true (nonzero) if the memory block may be valid, false (zero)
67  * if it is definitely invalid.
68  *
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.
72  */
73 #define access_ok(addr, size) ({                                        \
74         __chk_user_ptr(addr);                                           \
75         likely(__access_ok((unsigned long __force)(addr), (size)));     \
76 })
77
78 /*
79  * Ensure that the range [addr, addr+size) is within the process's
80  * address space
81  */
82 static inline int __access_ok(unsigned long addr, unsigned long size)
83 {
84         const mm_segment_t fs = get_fs();
85
86         return (size <= fs) && (addr <= (fs - size));
87 }
88
89 /*
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
94  * what to do.
95  *
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.
100  */
101
102 #if defined(__LITTLE_ENDIAN)
103 #define __MSW   1
104 #define __LSW   0
105 #elif defined(__BIG_ENDIAN)
106 #define __MSW   0
107 #define __LSW   1
108 #else
109 #error "Unknown endianness"
110 #endif
111
112 /*
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()"
115  * call.
116  */
117
118 #define __get_user_asm(insn, x, ptr, err)                       \
119 do {                                                            \
120         uintptr_t __tmp;                                        \
121         __typeof__(x) __x;                                      \
122         __enable_user_access();                                 \
123         __asm__ __volatile__ (                                  \
124                 "1:\n"                                          \
125                 "       " insn " %1, %3\n"                      \
126                 "2:\n"                                          \
127                 "       .section .fixup,\"ax\"\n"               \
128                 "       .balign 4\n"                            \
129                 "3:\n"                                          \
130                 "       li %0, %4\n"                            \
131                 "       li %1, 0\n"                             \
132                 "       jump 2b, %2\n"                          \
133                 "       .previous\n"                            \
134                 "       .section __ex_table,\"a\"\n"            \
135                 "       .balign " RISCV_SZPTR "\n"                      \
136                 "       " RISCV_PTR " 1b, 3b\n"                 \
137                 "       .previous"                              \
138                 : "+r" (err), "=&r" (__x), "=r" (__tmp)         \
139                 : "m" (*(ptr)), "i" (-EFAULT));                 \
140         __disable_user_access();                                \
141         (x) = __x;                                              \
142 } while (0)
143
144 #ifdef CONFIG_64BIT
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)                               \
149 do {                                                            \
150         u32 __user *__ptr = (u32 __user *)(ptr);                \
151         u32 __lo, __hi;                                         \
152         uintptr_t __tmp;                                        \
153         __enable_user_access();                                 \
154         __asm__ __volatile__ (                                  \
155                 "1:\n"                                          \
156                 "       lw %1, %4\n"                            \
157                 "2:\n"                                          \
158                 "       lw %2, %5\n"                            \
159                 "3:\n"                                          \
160                 "       .section .fixup,\"ax\"\n"               \
161                 "       .balign 4\n"                            \
162                 "4:\n"                                          \
163                 "       li %0, %6\n"                            \
164                 "       li %1, 0\n"                             \
165                 "       li %2, 0\n"                             \
166                 "       jump 3b, %3\n"                          \
167                 "       .previous\n"                            \
168                 "       .section __ex_table,\"a\"\n"            \
169                 "       .balign " RISCV_SZPTR "\n"                      \
170                 "       " RISCV_PTR " 1b, 4b\n"                 \
171                 "       " RISCV_PTR " 2b, 4b\n"                 \
172                 "       .previous"                              \
173                 : "+r" (err), "=&r" (__lo), "=r" (__hi),        \
174                         "=r" (__tmp)                            \
175                 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]),       \
176                         "i" (-EFAULT));                         \
177         __disable_user_access();                                \
178         (x) = (__typeof__(x))((__typeof__((x)-(x)))(            \
179                 (((u64)__hi << 32) | __lo)));                   \
180 } while (0)
181 #endif /* CONFIG_64BIT */
182
183
184 /**
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.
188  *
189  * Context: User context only.  This function may sleep.
190  *
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.
194  *
195  * @ptr must have pointer-to-simple-variable type, and the result of
196  * dereferencing @ptr must be assignable to @x without a cast.
197  *
198  * Caller must check the pointer with access_ok() before calling this
199  * function.
200  *
201  * Returns zero on success, or -EFAULT on error.
202  * On error, the variable @x is set to zero.
203  */
204 #define __get_user(x, ptr)                                      \
205 ({                                                              \
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)) {                            \
210         case 1:                                                 \
211                 __get_user_asm("lb", (x), __gu_ptr, __gu_err);  \
212                 break;                                          \
213         case 2:                                                 \
214                 __get_user_asm("lh", (x), __gu_ptr, __gu_err);  \
215                 break;                                          \
216         case 4:                                                 \
217                 __get_user_asm("lw", (x), __gu_ptr, __gu_err);  \
218                 break;                                          \
219         case 8:                                                 \
220                 __get_user_8((x), __gu_ptr, __gu_err);  \
221                 break;                                          \
222         default:                                                \
223                 BUILD_BUG();                                    \
224         }                                                       \
225         __gu_err;                                               \
226 })
227
228 /**
229  * get_user: - Get a simple variable from user space.
230  * @x:   Variable to store result.
231  * @ptr: Source address, in user space.
232  *
233  * Context: User context only.  This function may sleep.
234  *
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.
238  *
239  * @ptr must have pointer-to-simple-variable type, and the result of
240  * dereferencing @ptr must be assignable to @x without a cast.
241  *
242  * Returns zero on success, or -EFAULT on error.
243  * On error, the variable @x is set to zero.
244  */
245 #define get_user(x, ptr)                                        \
246 ({                                                              \
247         const __typeof__(*(ptr)) __user *__p = (ptr);           \
248         might_fault();                                          \
249         access_ok(__p, sizeof(*__p)) ?          \
250                 __get_user((x), __p) :                          \
251                 ((x) = 0, -EFAULT);                             \
252 })
253
254 #define __put_user_asm(insn, x, ptr, err)                       \
255 do {                                                            \
256         uintptr_t __tmp;                                        \
257         __typeof__(*(ptr)) __x = x;                             \
258         __enable_user_access();                                 \
259         __asm__ __volatile__ (                                  \
260                 "1:\n"                                          \
261                 "       " insn " %z3, %2\n"                     \
262                 "2:\n"                                          \
263                 "       .section .fixup,\"ax\"\n"               \
264                 "       .balign 4\n"                            \
265                 "3:\n"                                          \
266                 "       li %0, %4\n"                            \
267                 "       jump 2b, %1\n"                          \
268                 "       .previous\n"                            \
269                 "       .section __ex_table,\"a\"\n"            \
270                 "       .balign " RISCV_SZPTR "\n"                      \
271                 "       " RISCV_PTR " 1b, 3b\n"                 \
272                 "       .previous"                              \
273                 : "+r" (err), "=r" (__tmp), "=m" (*(ptr))       \
274                 : "rJ" (__x), "i" (-EFAULT));                   \
275         __disable_user_access();                                \
276 } while (0)
277
278 #ifdef CONFIG_64BIT
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)                               \
283 do {                                                            \
284         u32 __user *__ptr = (u32 __user *)(ptr);                \
285         u64 __x = (__typeof__((x)-(x)))(x);                     \
286         uintptr_t __tmp;                                        \
287         __enable_user_access();                                 \
288         __asm__ __volatile__ (                                  \
289                 "1:\n"                                          \
290                 "       sw %z4, %2\n"                           \
291                 "2:\n"                                          \
292                 "       sw %z5, %3\n"                           \
293                 "3:\n"                                          \
294                 "       .section .fixup,\"ax\"\n"               \
295                 "       .balign 4\n"                            \
296                 "4:\n"                                          \
297                 "       li %0, %6\n"                            \
298                 "       jump 3b, %1\n"                          \
299                 "       .previous\n"                            \
300                 "       .section __ex_table,\"a\"\n"            \
301                 "       .balign " RISCV_SZPTR "\n"                      \
302                 "       " RISCV_PTR " 1b, 4b\n"                 \
303                 "       " RISCV_PTR " 2b, 4b\n"                 \
304                 "       .previous"                              \
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();                                \
310 } while (0)
311 #endif /* CONFIG_64BIT */
312
313
314 /**
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.
318  *
319  * Context: User context only.  This function may sleep.
320  *
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.
324  *
325  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
326  * to the result of dereferencing @ptr.
327  *
328  * Caller must check the pointer with access_ok() before calling this
329  * function.
330  *
331  * Returns zero on success, or -EFAULT on error.
332  */
333 #define __put_user(x, ptr)                                      \
334 ({                                                              \
335         register long __pu_err = 0;                             \
336         __typeof__(*(ptr)) __user *__gu_ptr = (ptr);            \
337         __chk_user_ptr(__gu_ptr);                               \
338         switch (sizeof(*__gu_ptr)) {                            \
339         case 1:                                                 \
340                 __put_user_asm("sb", (x), __gu_ptr, __pu_err);  \
341                 break;                                          \
342         case 2:                                                 \
343                 __put_user_asm("sh", (x), __gu_ptr, __pu_err);  \
344                 break;                                          \
345         case 4:                                                 \
346                 __put_user_asm("sw", (x), __gu_ptr, __pu_err);  \
347                 break;                                          \
348         case 8:                                                 \
349                 __put_user_8((x), __gu_ptr, __pu_err);  \
350                 break;                                          \
351         default:                                                \
352                 BUILD_BUG();                                    \
353         }                                                       \
354         __pu_err;                                               \
355 })
356
357 /**
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.
361  *
362  * Context: User context only.  This function may sleep.
363  *
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.
367  *
368  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
369  * to the result of dereferencing @ptr.
370  *
371  * Returns zero on success, or -EFAULT on error.
372  */
373 #define put_user(x, ptr)                                        \
374 ({                                                              \
375         __typeof__(*(ptr)) __user *__p = (ptr);                 \
376         might_fault();                                          \
377         access_ok(__p, sizeof(*__p)) ?          \
378                 __put_user((x), __p) :                          \
379                 -EFAULT;                                        \
380 })
381
382
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);
387
388 static inline unsigned long
389 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
390 {
391         return __asm_copy_from_user(to, from, n);
392 }
393
394 static inline unsigned long
395 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
396 {
397         return __asm_copy_to_user(to, from, n);
398 }
399
400 extern long strncpy_from_user(char *dest, const char __user *src, long count);
401
402 extern long __must_check strlen_user(const char __user *str);
403 extern long __must_check strnlen_user(const char __user *str, long n);
404
405 extern
406 unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
407
408 static inline
409 unsigned long __must_check clear_user(void __user *to, unsigned long n)
410 {
411         might_fault();
412         return access_ok(to, n) ?
413                 __clear_user(to, n) : n;
414 }
415
416 /*
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
419  * value.
420  */
421 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb)      \
422 ({                                                              \
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();                                 \
430         switch (size) {                                         \
431         case 4:                                                 \
432                 __asm__ __volatile__ (                          \
433                 "0:\n"                                          \
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"               \
438                 "1:\n"                                          \
439                 ".section .fixup,\"ax\"\n"                      \
440                 ".balign 4\n"                                   \
441                 "2:\n"                                          \
442                 "       li %[err], %[efault]\n"                 \
443                 "       jump 1b, %[rc]\n"                       \
444                 ".previous\n"                                   \
445                 ".section __ex_table,\"a\"\n"                   \
446                 ".balign " RISCV_SZPTR "\n"                     \
447                 "       " RISCV_PTR " 1b, 2b\n"                 \
448                 ".previous\n"                                   \
449                         : [ret] "=&r" (__ret),                  \
450                           [rc]  "=&r" (__rc),                   \
451                           [ptr] "+A" (*__ptr),                  \
452                           [err] "=&r" (__err)                   \
453                         : [old] "rJ" (__old),                   \
454                           [new] "rJ" (__new),                   \
455                           [efault] "i" (-EFAULT));              \
456                 break;                                          \
457         case 8:                                                 \
458                 __asm__ __volatile__ (                          \
459                 "0:\n"                                          \
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"               \
464                 "1:\n"                                          \
465                 ".section .fixup,\"ax\"\n"                      \
466                 ".balign 4\n"                                   \
467                 "2:\n"                                          \
468                 "       li %[err], %[efault]\n"                 \
469                 "       jump 1b, %[rc]\n"                       \
470                 ".previous\n"                                   \
471                 ".section __ex_table,\"a\"\n"                   \
472                 ".balign " RISCV_SZPTR "\n"                     \
473                 "       " RISCV_PTR " 1b, 2b\n"                 \
474                 ".previous\n"                                   \
475                         : [ret] "=&r" (__ret),                  \
476                           [rc]  "=&r" (__rc),                   \
477                           [ptr] "+A" (*__ptr),                  \
478                           [err] "=&r" (__err)                   \
479                         : [old] "rJ" (__old),                   \
480                           [new] "rJ" (__new),                   \
481                           [efault] "i" (-EFAULT));              \
482                 break;                                          \
483         default:                                                \
484                 BUILD_BUG();                                    \
485         }                                                       \
486         __disable_user_access();                                \
487         (err) = __err;                                          \
488         __ret;                                                  \
489 })
490
491 #endif /* _ASM_RISCV_UACCESS_H */