blob: b26f407be5c8d7a2bb3a353128a6a022b7c82ea8 [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#define __LSW 0
Christoph Hellwige28dcc72019-04-15 11:14:34 +0200105#define __MSW 1
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700106
107/*
108 * The "__xxx" versions of the user access functions do not verify the address
109 * space - it must have been done previously with a separate "access_ok()"
110 * call.
111 */
112
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700113#define __get_user_asm(insn, x, ptr, err) \
114do { \
115 uintptr_t __tmp; \
116 __typeof__(x) __x; \
117 __enable_user_access(); \
118 __asm__ __volatile__ ( \
119 "1:\n" \
120 " " insn " %1, %3\n" \
121 "2:\n" \
122 " .section .fixup,\"ax\"\n" \
123 " .balign 4\n" \
124 "3:\n" \
125 " li %0, %4\n" \
126 " li %1, 0\n" \
127 " jump 2b, %2\n" \
128 " .previous\n" \
129 " .section __ex_table,\"a\"\n" \
130 " .balign " RISCV_SZPTR "\n" \
131 " " RISCV_PTR " 1b, 3b\n" \
132 " .previous" \
133 : "+r" (err), "=&r" (__x), "=r" (__tmp) \
134 : "m" (*(ptr)), "i" (-EFAULT)); \
135 __disable_user_access(); \
136 (x) = __x; \
137} while (0)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700138
139#ifdef CONFIG_64BIT
140#define __get_user_8(x, ptr, err) \
141 __get_user_asm("ld", x, ptr, err)
142#else /* !CONFIG_64BIT */
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700143#define __get_user_8(x, ptr, err) \
144do { \
145 u32 __user *__ptr = (u32 __user *)(ptr); \
146 u32 __lo, __hi; \
147 uintptr_t __tmp; \
148 __enable_user_access(); \
149 __asm__ __volatile__ ( \
150 "1:\n" \
151 " lw %1, %4\n" \
152 "2:\n" \
153 " lw %2, %5\n" \
154 "3:\n" \
155 " .section .fixup,\"ax\"\n" \
156 " .balign 4\n" \
157 "4:\n" \
158 " li %0, %6\n" \
159 " li %1, 0\n" \
160 " li %2, 0\n" \
161 " jump 3b, %3\n" \
162 " .previous\n" \
163 " .section __ex_table,\"a\"\n" \
164 " .balign " RISCV_SZPTR "\n" \
165 " " RISCV_PTR " 1b, 4b\n" \
166 " " RISCV_PTR " 2b, 4b\n" \
167 " .previous" \
168 : "+r" (err), "=&r" (__lo), "=r" (__hi), \
169 "=r" (__tmp) \
170 : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]), \
171 "i" (-EFAULT)); \
172 __disable_user_access(); \
173 (x) = (__typeof__(x))((__typeof__((x)-(x)))( \
174 (((u64)__hi << 32) | __lo))); \
175} while (0)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700176#endif /* CONFIG_64BIT */
177
178
179/**
180 * __get_user: - Get a simple variable from user space, with less checking.
181 * @x: Variable to store result.
182 * @ptr: Source address, in user space.
183 *
184 * Context: User context only. This function may sleep.
185 *
186 * This macro copies a single simple variable from user space to kernel
187 * space. It supports simple types like char and int, but not larger
188 * data types like structures or arrays.
189 *
190 * @ptr must have pointer-to-simple-variable type, and the result of
191 * dereferencing @ptr must be assignable to @x without a cast.
192 *
193 * Caller must check the pointer with access_ok() before calling this
194 * function.
195 *
196 * Returns zero on success, or -EFAULT on error.
197 * On error, the variable @x is set to zero.
198 */
199#define __get_user(x, ptr) \
200({ \
201 register long __gu_err = 0; \
202 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
203 __chk_user_ptr(__gu_ptr); \
204 switch (sizeof(*__gu_ptr)) { \
205 case 1: \
206 __get_user_asm("lb", (x), __gu_ptr, __gu_err); \
207 break; \
208 case 2: \
209 __get_user_asm("lh", (x), __gu_ptr, __gu_err); \
210 break; \
211 case 4: \
212 __get_user_asm("lw", (x), __gu_ptr, __gu_err); \
213 break; \
214 case 8: \
215 __get_user_8((x), __gu_ptr, __gu_err); \
216 break; \
217 default: \
218 BUILD_BUG(); \
219 } \
220 __gu_err; \
221})
222
223/**
224 * get_user: - Get a simple variable from user space.
225 * @x: Variable to store result.
226 * @ptr: Source address, in user space.
227 *
228 * Context: User context only. This function may sleep.
229 *
230 * This macro copies a single simple variable from user space to kernel
231 * space. It supports simple types like char and int, but not larger
232 * data types like structures or arrays.
233 *
234 * @ptr must have pointer-to-simple-variable type, and the result of
235 * dereferencing @ptr must be assignable to @x without a cast.
236 *
237 * Returns zero on success, or -EFAULT on error.
238 * On error, the variable @x is set to zero.
239 */
240#define get_user(x, ptr) \
241({ \
242 const __typeof__(*(ptr)) __user *__p = (ptr); \
243 might_fault(); \
Linus Torvalds96d4f262019-01-03 18:57:57 -0800244 access_ok(__p, sizeof(*__p)) ? \
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700245 __get_user((x), __p) : \
246 ((x) = 0, -EFAULT); \
247})
248
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700249#define __put_user_asm(insn, x, ptr, err) \
250do { \
251 uintptr_t __tmp; \
252 __typeof__(*(ptr)) __x = x; \
253 __enable_user_access(); \
254 __asm__ __volatile__ ( \
255 "1:\n" \
256 " " insn " %z3, %2\n" \
257 "2:\n" \
258 " .section .fixup,\"ax\"\n" \
259 " .balign 4\n" \
260 "3:\n" \
261 " li %0, %4\n" \
262 " jump 2b, %1\n" \
263 " .previous\n" \
264 " .section __ex_table,\"a\"\n" \
265 " .balign " RISCV_SZPTR "\n" \
266 " " RISCV_PTR " 1b, 3b\n" \
267 " .previous" \
268 : "+r" (err), "=r" (__tmp), "=m" (*(ptr)) \
269 : "rJ" (__x), "i" (-EFAULT)); \
270 __disable_user_access(); \
271} while (0)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700272
273#ifdef CONFIG_64BIT
274#define __put_user_8(x, ptr, err) \
275 __put_user_asm("sd", x, ptr, err)
276#else /* !CONFIG_64BIT */
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700277#define __put_user_8(x, ptr, err) \
278do { \
279 u32 __user *__ptr = (u32 __user *)(ptr); \
280 u64 __x = (__typeof__((x)-(x)))(x); \
281 uintptr_t __tmp; \
282 __enable_user_access(); \
283 __asm__ __volatile__ ( \
284 "1:\n" \
285 " sw %z4, %2\n" \
286 "2:\n" \
287 " sw %z5, %3\n" \
288 "3:\n" \
289 " .section .fixup,\"ax\"\n" \
290 " .balign 4\n" \
291 "4:\n" \
292 " li %0, %6\n" \
Alan Kaodbee9c92019-03-22 14:37:04 +0800293 " jump 3b, %1\n" \
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700294 " .previous\n" \
295 " .section __ex_table,\"a\"\n" \
296 " .balign " RISCV_SZPTR "\n" \
297 " " RISCV_PTR " 1b, 4b\n" \
298 " " RISCV_PTR " 2b, 4b\n" \
299 " .previous" \
300 : "+r" (err), "=r" (__tmp), \
301 "=m" (__ptr[__LSW]), \
302 "=m" (__ptr[__MSW]) \
303 : "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT)); \
304 __disable_user_access(); \
305} while (0)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700306#endif /* CONFIG_64BIT */
307
308
309/**
310 * __put_user: - Write a simple value into user space, with less checking.
311 * @x: Value to copy to user space.
312 * @ptr: Destination address, in user space.
313 *
314 * Context: User context only. This function may sleep.
315 *
316 * This macro copies a single simple value from kernel space to user
317 * space. It supports simple types like char and int, but not larger
318 * data types like structures or arrays.
319 *
320 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
321 * to the result of dereferencing @ptr.
322 *
323 * Caller must check the pointer with access_ok() before calling this
324 * function.
325 *
326 * Returns zero on success, or -EFAULT on error.
327 */
328#define __put_user(x, ptr) \
329({ \
330 register long __pu_err = 0; \
331 __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
332 __chk_user_ptr(__gu_ptr); \
333 switch (sizeof(*__gu_ptr)) { \
334 case 1: \
335 __put_user_asm("sb", (x), __gu_ptr, __pu_err); \
336 break; \
337 case 2: \
338 __put_user_asm("sh", (x), __gu_ptr, __pu_err); \
339 break; \
340 case 4: \
341 __put_user_asm("sw", (x), __gu_ptr, __pu_err); \
342 break; \
343 case 8: \
344 __put_user_8((x), __gu_ptr, __pu_err); \
345 break; \
346 default: \
347 BUILD_BUG(); \
348 } \
349 __pu_err; \
350})
351
352/**
353 * put_user: - Write a simple value into user space.
354 * @x: Value to copy to user space.
355 * @ptr: Destination address, in user space.
356 *
357 * Context: User context only. This function may sleep.
358 *
359 * This macro copies a single simple value from kernel space to user
360 * space. It supports simple types like char and int, but not larger
361 * data types like structures or arrays.
362 *
363 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
364 * to the result of dereferencing @ptr.
365 *
366 * Returns zero on success, or -EFAULT on error.
367 */
368#define put_user(x, ptr) \
369({ \
370 __typeof__(*(ptr)) __user *__p = (ptr); \
371 might_fault(); \
Linus Torvalds96d4f262019-01-03 18:57:57 -0800372 access_ok(__p, sizeof(*__p)) ? \
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700373 __put_user((x), __p) : \
374 -EFAULT; \
375})
376
377
Luc Van Oostenryck86406d52018-06-09 02:33:51 +0200378extern unsigned long __must_check __asm_copy_to_user(void __user *to,
379 const void *from, unsigned long n);
380extern unsigned long __must_check __asm_copy_from_user(void *to,
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700381 const void __user *from, unsigned long n);
382
383static inline unsigned long
384raw_copy_from_user(void *to, const void __user *from, unsigned long n)
385{
Olof Johansson21f70d42018-11-14 16:27:55 -0800386 return __asm_copy_from_user(to, from, n);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700387}
388
389static inline unsigned long
390raw_copy_to_user(void __user *to, const void *from, unsigned long n)
391{
Olof Johansson21f70d42018-11-14 16:27:55 -0800392 return __asm_copy_to_user(to, from, n);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700393}
394
395extern long strncpy_from_user(char *dest, const char __user *src, long count);
396
397extern long __must_check strlen_user(const char __user *str);
398extern long __must_check strnlen_user(const char __user *str, long n);
399
400extern
401unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
402
403static inline
404unsigned long __must_check clear_user(void __user *to, unsigned long n)
405{
406 might_fault();
Linus Torvalds96d4f262019-01-03 18:57:57 -0800407 return access_ok(to, n) ?
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700408 __clear_user(to, n) : n;
409}
410
411/*
412 * Atomic compare-and-exchange, but with a fixup for userspace faults. Faults
413 * will set "err" to -EFAULT, while successful accesses return the previous
414 * value.
415 */
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700416#define __cmpxchg_user(ptr, old, new, err, size, lrb, scb) \
417({ \
418 __typeof__(ptr) __ptr = (ptr); \
419 __typeof__(*(ptr)) __old = (old); \
420 __typeof__(*(ptr)) __new = (new); \
421 __typeof__(*(ptr)) __ret; \
422 __typeof__(err) __err = 0; \
423 register unsigned int __rc; \
424 __enable_user_access(); \
425 switch (size) { \
426 case 4: \
427 __asm__ __volatile__ ( \
428 "0:\n" \
429 " lr.w" #scb " %[ret], %[ptr]\n" \
430 " bne %[ret], %z[old], 1f\n" \
431 " sc.w" #lrb " %[rc], %z[new], %[ptr]\n" \
432 " bnez %[rc], 0b\n" \
433 "1:\n" \
434 ".section .fixup,\"ax\"\n" \
435 ".balign 4\n" \
436 "2:\n" \
437 " li %[err], %[efault]\n" \
438 " jump 1b, %[rc]\n" \
439 ".previous\n" \
440 ".section __ex_table,\"a\"\n" \
441 ".balign " RISCV_SZPTR "\n" \
442 " " RISCV_PTR " 1b, 2b\n" \
443 ".previous\n" \
444 : [ret] "=&r" (__ret), \
445 [rc] "=&r" (__rc), \
446 [ptr] "+A" (*__ptr), \
447 [err] "=&r" (__err) \
448 : [old] "rJ" (__old), \
449 [new] "rJ" (__new), \
450 [efault] "i" (-EFAULT)); \
451 break; \
452 case 8: \
453 __asm__ __volatile__ ( \
454 "0:\n" \
455 " lr.d" #scb " %[ret], %[ptr]\n" \
456 " bne %[ret], %z[old], 1f\n" \
457 " sc.d" #lrb " %[rc], %z[new], %[ptr]\n" \
458 " bnez %[rc], 0b\n" \
459 "1:\n" \
460 ".section .fixup,\"ax\"\n" \
461 ".balign 4\n" \
462 "2:\n" \
463 " li %[err], %[efault]\n" \
464 " jump 1b, %[rc]\n" \
465 ".previous\n" \
466 ".section __ex_table,\"a\"\n" \
467 ".balign " RISCV_SZPTR "\n" \
468 " " RISCV_PTR " 1b, 2b\n" \
469 ".previous\n" \
470 : [ret] "=&r" (__ret), \
471 [rc] "=&r" (__rc), \
472 [ptr] "+A" (*__ptr), \
473 [err] "=&r" (__err) \
474 : [old] "rJ" (__old), \
475 [new] "rJ" (__new), \
476 [efault] "i" (-EFAULT)); \
477 break; \
478 default: \
479 BUILD_BUG(); \
480 } \
481 __disable_user_access(); \
482 (err) = __err; \
483 __ret; \
484})
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700485
486#endif /* _ASM_RISCV_UACCESS_H */