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