blob: c51fc8bfbdde3a60493b2e0c75eca19a840b401d [file] [log] [blame]
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -07001/*
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>
Christoph Hellwigdf720962019-04-15 11:14:32 +020026#include <asm/extable.h>
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070027#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
Christoph Hellwig5cfade52019-04-15 11:14:33 +020042#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
43
44#define KERNEL_DS MAKE_MM_SEG(~0UL)
45#define USER_DS MAKE_MM_SEG(TASK_SIZE)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070046
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070047#define get_fs() (current_thread_info()->addr_limit)
48
49static inline void set_fs(mm_segment_t fs)
50{
51 current_thread_info()->addr_limit = fs;
52}
53
Christoph Hellwig5cfade52019-04-15 11:14:33 +020054#define segment_eq(a, b) ((a).seg == (b).seg)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070055
Christoph Hellwig5cfade52019-04-15 11:14:33 +020056#define user_addr_max() (get_fs().seg)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070057
58
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070059/**
60 * access_ok: - Checks if a user space pointer is valid
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070061 * @addr: User space pointer to start of block to check
62 * @size: Size of block to check
63 *
64 * Context: User context only. This function may sleep.
65 *
66 * Checks if a pointer to a block of memory in user space is valid.
67 *
68 * Returns true (nonzero) if the memory block may be valid, false (zero)
69 * if it is definitely invalid.
70 *
71 * Note that, depending on architecture, this function probably just
72 * checks that the pointer is in the user space range - after calling
73 * this function, memory access functions may still return -EFAULT.
74 */
Linus Torvalds96d4f262019-01-03 18:57:57 -080075#define access_ok(addr, size) ({ \
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070076 __chk_user_ptr(addr); \
77 likely(__access_ok((unsigned long __force)(addr), (size))); \
78})
79
80/*
81 * Ensure that the range [addr, addr+size) is within the process's
82 * address space
83 */
84static inline int __access_ok(unsigned long addr, unsigned long size)
85{
86 const mm_segment_t fs = get_fs();
87
Christoph Hellwig5cfade52019-04-15 11:14:33 +020088 return size <= fs.seg && addr <= fs.seg - size;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070089}
90
91/*
92 * The exception table consists of pairs of addresses: the first is the
93 * address of an instruction that is allowed to fault, and the second is
94 * the address at which the program should continue. No registers are
95 * modified, so it is entirely up to the continuation code to figure out
96 * what to do.
97 *
98 * All the routines below use bits of fixup code that are out of line
99 * with the main instruction path. This means when everything is well,
100 * we don't even have to jump over them. Further, they do not intrude
101 * on our cache or tlb entries.
102 */
103
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700104#if defined(__LITTLE_ENDIAN)
105#define __MSW 1
106#define __LSW 0
107#elif defined(__BIG_ENDIAN)
108#define __MSW 0
109#define __LSW 1
110#else
111#error "Unknown endianness"
112#endif
113
114/*
115 * The "__xxx" versions of the user access functions do not verify the address
116 * space - it must have been done previously with a separate "access_ok()"
117 * call.
118 */
119
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700120#define __get_user_asm(insn, x, ptr, err) \
121do { \
122 uintptr_t __tmp; \
123 __typeof__(x) __x; \
124 __enable_user_access(); \
125 __asm__ __volatile__ ( \
126 "1:\n" \
127 " " insn " %1, %3\n" \
128 "2:\n" \
129 " .section .fixup,\"ax\"\n" \
130 " .balign 4\n" \
131 "3:\n" \
132 " li %0, %4\n" \
133 " li %1, 0\n" \
134 " jump 2b, %2\n" \
135 " .previous\n" \
136 " .section __ex_table,\"a\"\n" \
137 " .balign " RISCV_SZPTR "\n" \
138 " " RISCV_PTR " 1b, 3b\n" \
139 " .previous" \
140 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
141 : "m" (*(ptr)), "i" (-EFAULT)); \
142 __disable_user_access(); \
143 (x) = __x; \
144} while (0)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700145
146#ifdef CONFIG_64BIT
147#define __get_user_8(x, ptr, err) \
148 __get_user_asm("ld", x, ptr, err)
149#else /* !CONFIG_64BIT */
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700150#define __get_user_8(x, ptr, err) \
151do { \
152 u32 __user *__ptr = (u32 __user *)(ptr); \
153 u32 __lo, __hi; \
154 uintptr_t __tmp; \
155 __enable_user_access(); \
156 __asm__ __volatile__ ( \
157 "1:\n" \
158 " lw %1, %4\n" \
159 "2:\n" \
160 " lw %2, %5\n" \
161 "3:\n" \
162 " .section .fixup,\"ax\"\n" \
163 " .balign 4\n" \
164 "4:\n" \
165 " li %0, %6\n" \
166 " li %1, 0\n" \
167 " li %2, 0\n" \
168 " jump 3b, %3\n" \
169 " .previous\n" \
170 " .section __ex_table,\"a\"\n" \
171 " .balign " RISCV_SZPTR "\n" \
172 " " RISCV_PTR " 1b, 4b\n" \
173 " " RISCV_PTR " 2b, 4b\n" \
174 " .previous" \
175 : "+r" (err), "=&r" (__lo), "=r" (__hi), \
176 "=r" (__tmp) \
177 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
178 "i" (-EFAULT)); \
179 __disable_user_access(); \
180 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
181 (((u64)__hi << 32) | __lo))); \
182} while (0)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700183#endif /* CONFIG_64BIT */
184
185
186/**
187 * __get_user: - Get a simple variable from user space, with less checking.
188 * @x: Variable to store result.
189 * @ptr: Source address, in user space.
190 *
191 * Context: User context only. This function may sleep.
192 *
193 * This macro copies a single simple variable from user space to kernel
194 * space. It supports simple types like char and int, but not larger
195 * data types like structures or arrays.
196 *
197 * @ptr must have pointer-to-simple-variable type, and the result of
198 * dereferencing @ptr must be assignable to @x without a cast.
199 *
200 * Caller must check the pointer with access_ok() before calling this
201 * function.
202 *
203 * Returns zero on success, or -EFAULT on error.
204 * On error, the variable @x is set to zero.
205 */
206#define __get_user(x, ptr) \
207({ \
208 register long __gu_err = 0; \
209 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
210 __chk_user_ptr(__gu_ptr); \
211 switch (sizeof(*__gu_ptr)) { \
212 case 1: \
213 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
214 break; \
215 case 2: \
216 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
217 break; \
218 case 4: \
219 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
220 break; \
221 case 8: \
222 __get_user_8((x), __gu_ptr, __gu_err); \
223 break; \
224 default: \
225 BUILD_BUG(); \
226 } \
227 __gu_err; \
228})
229
230/**
231 * get_user: - Get a simple variable from user space.
232 * @x: Variable to store result.
233 * @ptr: Source address, in user space.
234 *
235 * Context: User context only. This function may sleep.
236 *
237 * This macro copies a single simple variable from user space to kernel
238 * space. It supports simple types like char and int, but not larger
239 * data types like structures or arrays.
240 *
241 * @ptr must have pointer-to-simple-variable type, and the result of
242 * dereferencing @ptr must be assignable to @x without a cast.
243 *
244 * Returns zero on success, or -EFAULT on error.
245 * On error, the variable @x is set to zero.
246 */
247#define get_user(x, ptr) \
248({ \
249 const __typeof__(*(ptr)) __user *__p = (ptr); \
250 might_fault(); \
Linus Torvalds96d4f262019-01-03 18:57:57 -0800251 access_ok(__p, sizeof(*__p)) ? \
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700252 __get_user((x), __p) : \
253 ((x) = 0, -EFAULT); \
254})
255
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700256#define __put_user_asm(insn, x, ptr, err) \
257do { \
258 uintptr_t __tmp; \
259 __typeof__(*(ptr)) __x = x; \
260 __enable_user_access(); \
261 __asm__ __volatile__ ( \
262 "1:\n" \
263 " " insn " %z3, %2\n" \
264 "2:\n" \
265 " .section .fixup,\"ax\"\n" \
266 " .balign 4\n" \
267 "3:\n" \
268 " li %0, %4\n" \
269 " jump 2b, %1\n" \
270 " .previous\n" \
271 " .section __ex_table,\"a\"\n" \
272 " .balign " RISCV_SZPTR "\n" \
273 " " RISCV_PTR " 1b, 3b\n" \
274 " .previous" \
275 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
276 : "rJ" (__x), "i" (-EFAULT)); \
277 __disable_user_access(); \
278} while (0)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700279
280#ifdef CONFIG_64BIT
281#define __put_user_8(x, ptr, err) \
282 __put_user_asm("sd", x, ptr, err)
283#else /* !CONFIG_64BIT */
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700284#define __put_user_8(x, ptr, err) \
285do { \
286 u32 __user *__ptr = (u32 __user *)(ptr); \
287 u64 __x = (__typeof__((x)-(x)))(x); \
288 uintptr_t __tmp; \
289 __enable_user_access(); \
290 __asm__ __volatile__ ( \
291 "1:\n" \
292 " sw %z4, %2\n" \
293 "2:\n" \
294 " sw %z5, %3\n" \
295 "3:\n" \
296 " .section .fixup,\"ax\"\n" \
297 " .balign 4\n" \
298 "4:\n" \
299 " li %0, %6\n" \
Alan Kaodbee9c92019-03-22 14:37:04 +0800300 " jump 3b, %1\n" \
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700301 " .previous\n" \
302 " .section __ex_table,\"a\"\n" \
303 " .balign " RISCV_SZPTR "\n" \
304 " " RISCV_PTR " 1b, 4b\n" \
305 " " RISCV_PTR " 2b, 4b\n" \
306 " .previous" \
307 : "+r" (err), "=r" (__tmp), \
308 "=m" (__ptr[__LSW]), \
309 "=m" (__ptr[__MSW]) \
310 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
311 __disable_user_access(); \
312} while (0)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700313#endif /* CONFIG_64BIT */
314
315
316/**
317 * __put_user: - Write a simple value into user space, with less checking.
318 * @x: Value to copy to user space.
319 * @ptr: Destination address, in user space.
320 *
321 * Context: User context only. This function may sleep.
322 *
323 * This macro copies a single simple value from kernel space to user
324 * space. It supports simple types like char and int, but not larger
325 * data types like structures or arrays.
326 *
327 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
328 * to the result of dereferencing @ptr.
329 *
330 * Caller must check the pointer with access_ok() before calling this
331 * function.
332 *
333 * Returns zero on success, or -EFAULT on error.
334 */
335#define __put_user(x, ptr) \
336({ \
337 register long __pu_err = 0; \
338 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
339 __chk_user_ptr(__gu_ptr); \
340 switch (sizeof(*__gu_ptr)) { \
341 case 1: \
342 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
343 break; \
344 case 2: \
345 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
346 break; \
347 case 4: \
348 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
349 break; \
350 case 8: \
351 __put_user_8((x), __gu_ptr, __pu_err); \
352 break; \
353 default: \
354 BUILD_BUG(); \
355 } \
356 __pu_err; \
357})
358
359/**
360 * put_user: - Write a simple value into user space.
361 * @x: Value to copy to user space.
362 * @ptr: Destination address, in user space.
363 *
364 * Context: User context only. This function may sleep.
365 *
366 * This macro copies a single simple value from kernel space to user
367 * space. It supports simple types like char and int, but not larger
368 * data types like structures or arrays.
369 *
370 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
371 * to the result of dereferencing @ptr.
372 *
373 * Returns zero on success, or -EFAULT on error.
374 */
375#define put_user(x, ptr) \
376({ \
377 __typeof__(*(ptr)) __user *__p = (ptr); \
378 might_fault(); \
Linus Torvalds96d4f262019-01-03 18:57:57 -0800379 access_ok(__p, sizeof(*__p)) ? \
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700380 __put_user((x), __p) : \
381 -EFAULT; \
382})
383
384
Luc Van Oostenryck86406d52018-06-09 02:33:51 +0200385extern unsigned long __must_check __asm_copy_to_user(void __user *to,
386 const void *from, unsigned long n);
387extern unsigned long __must_check __asm_copy_from_user(void *to,
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700388 const void __user *from, unsigned long n);
389
390static inline unsigned long
391raw_copy_from_user(void *to, const void __user *from, unsigned long n)
392{
Olof Johansson21f70d42018-11-14 16:27:55 -0800393 return __asm_copy_from_user(to, from, n);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700394}
395
396static inline unsigned long
397raw_copy_to_user(void __user *to, const void *from, unsigned long n)
398{
Olof Johansson21f70d42018-11-14 16:27:55 -0800399 return __asm_copy_to_user(to, from, n);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700400}
401
402extern long strncpy_from_user(char *dest, const char __user *src, long count);
403
404extern long __must_check strlen_user(const char __user *str);
405extern long __must_check strnlen_user(const char __user *str, long n);
406
407extern
408unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
409
410static inline
411unsigned long __must_check clear_user(void __user *to, unsigned long n)
412{
413 might_fault();
Linus Torvalds96d4f262019-01-03 18:57:57 -0800414 return access_ok(to, n) ?
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700415 __clear_user(to, n) : n;
416}
417
418/*
419 * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults
420 * will set "err" to -EFAULT, while successful accesses return the previous
421 * value.
422 */
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700423#define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
424({ \
425 __typeof__(ptr) __ptr = (ptr); \
426 __typeof__(*(ptr)) __old = (old); \
427 __typeof__(*(ptr)) __new = (new); \
428 __typeof__(*(ptr)) __ret; \
429 __typeof__(err) __err = 0; \
430 register unsigned int __rc; \
431 __enable_user_access(); \
432 switch (size) { \
433 case 4: \
434 __asm__ __volatile__ ( \
435 "0:\n" \
436 " lr.w" #scb " %[ret], %[ptr]\n" \
437 " bne %[ret], %z[old], 1f\n" \
438 " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \
439 " bnez %[rc], 0b\n" \
440 "1:\n" \
441 ".section .fixup,\"ax\"\n" \
442 ".balign 4\n" \
443 "2:\n" \
444 " li %[err], %[efault]\n" \
445 " jump 1b, %[rc]\n" \
446 ".previous\n" \
447 ".section __ex_table,\"a\"\n" \
448 ".balign " RISCV_SZPTR "\n" \
449 " " RISCV_PTR " 1b, 2b\n" \
450 ".previous\n" \
451 : [ret] "=&r" (__ret), \
452 [rc] "=&r" (__rc), \
453 [ptr] "+A" (*__ptr), \
454 [err] "=&r" (__err) \
455 : [old] "rJ" (__old), \
456 [new] "rJ" (__new), \
457 [efault] "i" (-EFAULT)); \
458 break; \
459 case 8: \
460 __asm__ __volatile__ ( \
461 "0:\n" \
462 " lr.d" #scb " %[ret], %[ptr]\n" \
463 " bne %[ret], %z[old], 1f\n" \
464 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
465 " bnez %[rc], 0b\n" \
466 "1:\n" \
467 ".section .fixup,\"ax\"\n" \
468 ".balign 4\n" \
469 "2:\n" \
470 " li %[err], %[efault]\n" \
471 " jump 1b, %[rc]\n" \
472 ".previous\n" \
473 ".section __ex_table,\"a\"\n" \
474 ".balign " RISCV_SZPTR "\n" \
475 " " RISCV_PTR " 1b, 2b\n" \
476 ".previous\n" \
477 : [ret] "=&r" (__ret), \
478 [rc] "=&r" (__rc), \
479 [ptr] "+A" (*__ptr), \
480 [err] "=&r" (__err) \
481 : [old] "rJ" (__old), \
482 [new] "rJ" (__new), \
483 [efault] "i" (-EFAULT)); \
484 break; \
485 default: \
486 BUILD_BUG(); \
487 } \
488 __disable_user_access(); \
489 (err) = __err; \
490 __ret; \
491})
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700492
493#endif /* _ASM_RISCV_UACCESS_H */