xref: /openbmc/linux/arch/riscv/include/asm/uaccess.h (revision 023e4163)
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/asm.h>
27 
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")
32 
33 /*
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.
37  *
38  * For historical reasons, these macros are grossly misnamed.
39  */
40 
41 #define KERNEL_DS	(~0UL)
42 #define USER_DS		(TASK_SIZE)
43 
44 #define get_fs()	(current_thread_info()->addr_limit)
45 
46 static inline void set_fs(mm_segment_t fs)
47 {
48 	current_thread_info()->addr_limit = fs;
49 }
50 
51 #define segment_eq(a, b) ((a) == (b))
52 
53 #define user_addr_max()	(get_fs())
54 
55 
56 /**
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
60  *
61  * Context: User context only.  This function may sleep.
62  *
63  * Checks if a pointer to a block of memory in user space is valid.
64  *
65  * Returns true (nonzero) if the memory block may be valid, false (zero)
66  * if it is definitely invalid.
67  *
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.
71  */
72 #define access_ok(addr, size) ({					\
73 	__chk_user_ptr(addr);						\
74 	likely(__access_ok((unsigned long __force)(addr), (size)));	\
75 })
76 
77 /*
78  * Ensure that the range [addr, addr+size) is within the process's
79  * address space
80  */
81 static inline int __access_ok(unsigned long addr, unsigned long size)
82 {
83 	const mm_segment_t fs = get_fs();
84 
85 	return (size <= fs) && (addr <= (fs - size));
86 }
87 
88 /*
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
93  * what to do.
94  *
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.
99  */
100 
101 struct exception_table_entry {
102 	unsigned long insn, fixup;
103 };
104 
105 extern int fixup_exception(struct pt_regs *state);
106 
107 #if defined(__LITTLE_ENDIAN)
108 #define __MSW	1
109 #define __LSW	0
110 #elif defined(__BIG_ENDIAN)
111 #define __MSW	0
112 #define	__LSW	1
113 #else
114 #error "Unknown endianness"
115 #endif
116 
117 /*
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()"
120  * call.
121  */
122 
123 #define __get_user_asm(insn, x, ptr, err)			\
124 do {								\
125 	uintptr_t __tmp;					\
126 	__typeof__(x) __x;					\
127 	__enable_user_access();					\
128 	__asm__ __volatile__ (					\
129 		"1:\n"						\
130 		"	" insn " %1, %3\n"			\
131 		"2:\n"						\
132 		"	.section .fixup,\"ax\"\n"		\
133 		"	.balign 4\n"				\
134 		"3:\n"						\
135 		"	li %0, %4\n"				\
136 		"	li %1, 0\n"				\
137 		"	jump 2b, %2\n"				\
138 		"	.previous\n"				\
139 		"	.section __ex_table,\"a\"\n"		\
140 		"	.balign " RISCV_SZPTR "\n"			\
141 		"	" RISCV_PTR " 1b, 3b\n"			\
142 		"	.previous"				\
143 		: "+r" (err), "=&r" (__x), "=r" (__tmp)		\
144 		: "m" (*(ptr)), "i" (-EFAULT));			\
145 	__disable_user_access();				\
146 	(x) = __x;						\
147 } while (0)
148 
149 #ifdef CONFIG_64BIT
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)				\
154 do {								\
155 	u32 __user *__ptr = (u32 __user *)(ptr);		\
156 	u32 __lo, __hi;						\
157 	uintptr_t __tmp;					\
158 	__enable_user_access();					\
159 	__asm__ __volatile__ (					\
160 		"1:\n"						\
161 		"	lw %1, %4\n"				\
162 		"2:\n"						\
163 		"	lw %2, %5\n"				\
164 		"3:\n"						\
165 		"	.section .fixup,\"ax\"\n"		\
166 		"	.balign 4\n"				\
167 		"4:\n"						\
168 		"	li %0, %6\n"				\
169 		"	li %1, 0\n"				\
170 		"	li %2, 0\n"				\
171 		"	jump 3b, %3\n"				\
172 		"	.previous\n"				\
173 		"	.section __ex_table,\"a\"\n"		\
174 		"	.balign " RISCV_SZPTR "\n"			\
175 		"	" RISCV_PTR " 1b, 4b\n"			\
176 		"	" RISCV_PTR " 2b, 4b\n"			\
177 		"	.previous"				\
178 		: "+r" (err), "=&r" (__lo), "=r" (__hi),	\
179 			"=r" (__tmp)				\
180 		: "m" (__ptr[__LSW]), "m" (__ptr[__MSW]),	\
181 			"i" (-EFAULT));				\
182 	__disable_user_access();				\
183 	(x) = (__typeof__(x))((__typeof__((x)-(x)))(		\
184 		(((u64)__hi << 32) | __lo)));			\
185 } while (0)
186 #endif /* CONFIG_64BIT */
187 
188 
189 /**
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.
193  *
194  * Context: User context only.  This function may sleep.
195  *
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.
199  *
200  * @ptr must have pointer-to-simple-variable type, and the result of
201  * dereferencing @ptr must be assignable to @x without a cast.
202  *
203  * Caller must check the pointer with access_ok() before calling this
204  * function.
205  *
206  * Returns zero on success, or -EFAULT on error.
207  * On error, the variable @x is set to zero.
208  */
209 #define __get_user(x, ptr)					\
210 ({								\
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)) {				\
215 	case 1:							\
216 		__get_user_asm("lb", (x), __gu_ptr, __gu_err);	\
217 		break;						\
218 	case 2:							\
219 		__get_user_asm("lh", (x), __gu_ptr, __gu_err);	\
220 		break;						\
221 	case 4:							\
222 		__get_user_asm("lw", (x), __gu_ptr, __gu_err);	\
223 		break;						\
224 	case 8:							\
225 		__get_user_8((x), __gu_ptr, __gu_err);	\
226 		break;						\
227 	default:						\
228 		BUILD_BUG();					\
229 	}							\
230 	__gu_err;						\
231 })
232 
233 /**
234  * get_user: - Get a simple variable from user space.
235  * @x:   Variable to store result.
236  * @ptr: Source address, in user space.
237  *
238  * Context: User context only.  This function may sleep.
239  *
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.
243  *
244  * @ptr must have pointer-to-simple-variable type, and the result of
245  * dereferencing @ptr must be assignable to @x without a cast.
246  *
247  * Returns zero on success, or -EFAULT on error.
248  * On error, the variable @x is set to zero.
249  */
250 #define get_user(x, ptr)					\
251 ({								\
252 	const __typeof__(*(ptr)) __user *__p = (ptr);		\
253 	might_fault();						\
254 	access_ok(__p, sizeof(*__p)) ?		\
255 		__get_user((x), __p) :				\
256 		((x) = 0, -EFAULT);				\
257 })
258 
259 #define __put_user_asm(insn, x, ptr, err)			\
260 do {								\
261 	uintptr_t __tmp;					\
262 	__typeof__(*(ptr)) __x = x;				\
263 	__enable_user_access();					\
264 	__asm__ __volatile__ (					\
265 		"1:\n"						\
266 		"	" insn " %z3, %2\n"			\
267 		"2:\n"						\
268 		"	.section .fixup,\"ax\"\n"		\
269 		"	.balign 4\n"				\
270 		"3:\n"						\
271 		"	li %0, %4\n"				\
272 		"	jump 2b, %1\n"				\
273 		"	.previous\n"				\
274 		"	.section __ex_table,\"a\"\n"		\
275 		"	.balign " RISCV_SZPTR "\n"			\
276 		"	" RISCV_PTR " 1b, 3b\n"			\
277 		"	.previous"				\
278 		: "+r" (err), "=r" (__tmp), "=m" (*(ptr))	\
279 		: "rJ" (__x), "i" (-EFAULT));			\
280 	__disable_user_access();				\
281 } while (0)
282 
283 #ifdef CONFIG_64BIT
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)				\
288 do {								\
289 	u32 __user *__ptr = (u32 __user *)(ptr);		\
290 	u64 __x = (__typeof__((x)-(x)))(x);			\
291 	uintptr_t __tmp;					\
292 	__enable_user_access();					\
293 	__asm__ __volatile__ (					\
294 		"1:\n"						\
295 		"	sw %z4, %2\n"				\
296 		"2:\n"						\
297 		"	sw %z5, %3\n"				\
298 		"3:\n"						\
299 		"	.section .fixup,\"ax\"\n"		\
300 		"	.balign 4\n"				\
301 		"4:\n"						\
302 		"	li %0, %6\n"				\
303 		"	jump 2b, %1\n"				\
304 		"	.previous\n"				\
305 		"	.section __ex_table,\"a\"\n"		\
306 		"	.balign " RISCV_SZPTR "\n"			\
307 		"	" RISCV_PTR " 1b, 4b\n"			\
308 		"	" RISCV_PTR " 2b, 4b\n"			\
309 		"	.previous"				\
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();				\
315 } while (0)
316 #endif /* CONFIG_64BIT */
317 
318 
319 /**
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.
323  *
324  * Context: User context only.  This function may sleep.
325  *
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.
329  *
330  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
331  * to the result of dereferencing @ptr.
332  *
333  * Caller must check the pointer with access_ok() before calling this
334  * function.
335  *
336  * Returns zero on success, or -EFAULT on error.
337  */
338 #define __put_user(x, ptr)					\
339 ({								\
340 	register long __pu_err = 0;				\
341 	__typeof__(*(ptr)) __user *__gu_ptr = (ptr);		\
342 	__chk_user_ptr(__gu_ptr);				\
343 	switch (sizeof(*__gu_ptr)) {				\
344 	case 1:							\
345 		__put_user_asm("sb", (x), __gu_ptr, __pu_err);	\
346 		break;						\
347 	case 2:							\
348 		__put_user_asm("sh", (x), __gu_ptr, __pu_err);	\
349 		break;						\
350 	case 4:							\
351 		__put_user_asm("sw", (x), __gu_ptr, __pu_err);	\
352 		break;						\
353 	case 8:							\
354 		__put_user_8((x), __gu_ptr, __pu_err);	\
355 		break;						\
356 	default:						\
357 		BUILD_BUG();					\
358 	}							\
359 	__pu_err;						\
360 })
361 
362 /**
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.
366  *
367  * Context: User context only.  This function may sleep.
368  *
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.
372  *
373  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
374  * to the result of dereferencing @ptr.
375  *
376  * Returns zero on success, or -EFAULT on error.
377  */
378 #define put_user(x, ptr)					\
379 ({								\
380 	__typeof__(*(ptr)) __user *__p = (ptr);			\
381 	might_fault();						\
382 	access_ok(__p, sizeof(*__p)) ?		\
383 		__put_user((x), __p) :				\
384 		-EFAULT;					\
385 })
386 
387 
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);
392 
393 static inline unsigned long
394 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
395 {
396 	return __asm_copy_from_user(to, from, n);
397 }
398 
399 static inline unsigned long
400 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
401 {
402 	return __asm_copy_to_user(to, from, n);
403 }
404 
405 extern long strncpy_from_user(char *dest, const char __user *src, long count);
406 
407 extern long __must_check strlen_user(const char __user *str);
408 extern long __must_check strnlen_user(const char __user *str, long n);
409 
410 extern
411 unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
412 
413 static inline
414 unsigned long __must_check clear_user(void __user *to, unsigned long n)
415 {
416 	might_fault();
417 	return access_ok(to, n) ?
418 		__clear_user(to, n) : n;
419 }
420 
421 /*
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
424  * value.
425  */
426 #define __cmpxchg_user(ptr, old, new, err, size, lrb, scb)	\
427 ({								\
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();					\
435 	switch (size) {						\
436 	case 4:							\
437 		__asm__ __volatile__ (				\
438 		"0:\n"						\
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"		\
443 		"1:\n"						\
444 		".section .fixup,\"ax\"\n"			\
445 		".balign 4\n"					\
446 		"2:\n"						\
447 		"	li %[err], %[efault]\n"			\
448 		"	jump 1b, %[rc]\n"			\
449 		".previous\n"					\
450 		".section __ex_table,\"a\"\n"			\
451 		".balign " RISCV_SZPTR "\n"			\
452 		"	" RISCV_PTR " 1b, 2b\n"			\
453 		".previous\n"					\
454 			: [ret] "=&r" (__ret),			\
455 			  [rc]  "=&r" (__rc),			\
456 			  [ptr] "+A" (*__ptr),			\
457 			  [err] "=&r" (__err)			\
458 			: [old] "rJ" (__old),			\
459 			  [new] "rJ" (__new),			\
460 			  [efault] "i" (-EFAULT));		\
461 		break;						\
462 	case 8:							\
463 		__asm__ __volatile__ (				\
464 		"0:\n"						\
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"		\
469 		"1:\n"						\
470 		".section .fixup,\"ax\"\n"			\
471 		".balign 4\n"					\
472 		"2:\n"						\
473 		"	li %[err], %[efault]\n"			\
474 		"	jump 1b, %[rc]\n"			\
475 		".previous\n"					\
476 		".section __ex_table,\"a\"\n"			\
477 		".balign " RISCV_SZPTR "\n"			\
478 		"	" RISCV_PTR " 1b, 2b\n"			\
479 		".previous\n"					\
480 			: [ret] "=&r" (__ret),			\
481 			  [rc]  "=&r" (__rc),			\
482 			  [ptr] "+A" (*__ptr),			\
483 			  [err] "=&r" (__err)			\
484 			: [old] "rJ" (__old),			\
485 			  [new] "rJ" (__new),			\
486 			  [efault] "i" (-EFAULT));		\
487 		break;						\
488 	default:						\
489 		BUILD_BUG();					\
490 	}							\
491 	__disable_user_access();				\
492 	(err) = __err;						\
493 	__ret;							\
494 })
495 
496 #endif /* _ASM_RISCV_UACCESS_H */
497