xref: /linux/arch/riscv/include/asm/uaccess.h (revision bb1f85d6046f0db757ac52ed60a5eba5df394819)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  *
5  * This file was copied from include/asm-generic/uaccess.h
6  */
7 
8 #ifndef _ASM_RISCV_UACCESS_H
9 #define _ASM_RISCV_UACCESS_H
10 
11 #include <asm/pgtable.h>		/* for TASK_SIZE */
12 
13 #define _ASM_EXTABLE(from, to)						\
14 	"	.pushsection	__ex_table, \"a\"\n"			\
15 	"	.balign		4\n"					\
16 	"	.long		(" #from " - .), (" #to " - .)\n"	\
17 	"	.popsection\n"
18 
19 /*
20  * User space memory access functions
21  */
22 #ifdef CONFIG_MMU
23 #include <linux/errno.h>
24 #include <linux/compiler.h>
25 #include <linux/thread_info.h>
26 #include <asm/byteorder.h>
27 #include <asm/extable.h>
28 #include <asm/asm.h>
29 
30 #define __enable_user_access()							\
31 	__asm__ __volatile__ ("csrs sstatus, %0" : : "r" (SR_SUM) : "memory")
32 #define __disable_user_access()							\
33 	__asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory")
34 
35 /**
36  * access_ok: - Checks if a user space pointer is valid
37  * @addr: User space pointer to start of block to check
38  * @size: Size of block to check
39  *
40  * Context: User context only.  This function may sleep.
41  *
42  * Checks if a pointer to a block of memory in user space is valid.
43  *
44  * Returns true (nonzero) if the memory block may be valid, false (zero)
45  * if it is definitely invalid.
46  *
47  * Note that, depending on architecture, this function probably just
48  * checks that the pointer is in the user space range - after calling
49  * this function, memory access functions may still return -EFAULT.
50  */
51 #define access_ok(addr, size) ({					\
52 	__chk_user_ptr(addr);						\
53 	likely(__access_ok((unsigned long __force)(addr), (size)));	\
54 })
55 
56 /*
57  * Ensure that the range [addr, addr+size) is within the process's
58  * address space
59  */
60 static inline int __access_ok(unsigned long addr, unsigned long size)
61 {
62 	return size <= TASK_SIZE && addr <= TASK_SIZE - size;
63 }
64 
65 /*
66  * The exception table consists of pairs of addresses: the first is the
67  * address of an instruction that is allowed to fault, and the second is
68  * the address at which the program should continue.  No registers are
69  * modified, so it is entirely up to the continuation code to figure out
70  * what to do.
71  *
72  * All the routines below use bits of fixup code that are out of line
73  * with the main instruction path.  This means when everything is well,
74  * we don't even have to jump over them.  Further, they do not intrude
75  * on our cache or tlb entries.
76  */
77 
78 #define __LSW	0
79 #define __MSW	1
80 
81 /*
82  * The "__xxx" versions of the user access functions do not verify the address
83  * space - it must have been done previously with a separate "access_ok()"
84  * call.
85  */
86 
87 #define __get_user_asm(insn, x, ptr, err)			\
88 do {								\
89 	uintptr_t __tmp;					\
90 	__typeof__(x) __x;					\
91 	__asm__ __volatile__ (					\
92 		"1:\n"						\
93 		"	" insn " %1, %3\n"			\
94 		"2:\n"						\
95 		"	.section .fixup,\"ax\"\n"		\
96 		"	.balign 4\n"				\
97 		"3:\n"						\
98 		"	li %0, %4\n"				\
99 		"	li %1, 0\n"				\
100 		"	jump 2b, %2\n"				\
101 		"	.previous\n"				\
102 			_ASM_EXTABLE(1b, 3b)			\
103 		: "+r" (err), "=&r" (__x), "=r" (__tmp)		\
104 		: "m" (*(ptr)), "i" (-EFAULT));			\
105 	(x) = __x;						\
106 } while (0)
107 
108 #ifdef CONFIG_64BIT
109 #define __get_user_8(x, ptr, err) \
110 	__get_user_asm("ld", x, ptr, err)
111 #else /* !CONFIG_64BIT */
112 #define __get_user_8(x, ptr, err)				\
113 do {								\
114 	u32 __user *__ptr = (u32 __user *)(ptr);		\
115 	u32 __lo, __hi;						\
116 	uintptr_t __tmp;					\
117 	__asm__ __volatile__ (					\
118 		"1:\n"						\
119 		"	lw %1, %4\n"				\
120 		"2:\n"						\
121 		"	lw %2, %5\n"				\
122 		"3:\n"						\
123 		"	.section .fixup,\"ax\"\n"		\
124 		"	.balign 4\n"				\
125 		"4:\n"						\
126 		"	li %0, %6\n"				\
127 		"	li %1, 0\n"				\
128 		"	li %2, 0\n"				\
129 		"	jump 3b, %3\n"				\
130 		"	.previous\n"				\
131 			_ASM_EXTABLE(1b, 4b)			\
132 			_ASM_EXTABLE(2b, 4b)			\
133 		: "+r" (err), "=&r" (__lo), "=r" (__hi),	\
134 			"=r" (__tmp)				\
135 		: "m" (__ptr[__LSW]), "m" (__ptr[__MSW]),	\
136 			"i" (-EFAULT));				\
137 	(x) = (__typeof__(x))((__typeof__((x)-(x)))(		\
138 		(((u64)__hi << 32) | __lo)));			\
139 } while (0)
140 #endif /* CONFIG_64BIT */
141 
142 #define __get_user_nocheck(x, __gu_ptr, __gu_err)		\
143 do {								\
144 	switch (sizeof(*__gu_ptr)) {				\
145 	case 1:							\
146 		__get_user_asm("lb", (x), __gu_ptr, __gu_err);	\
147 		break;						\
148 	case 2:							\
149 		__get_user_asm("lh", (x), __gu_ptr, __gu_err);	\
150 		break;						\
151 	case 4:							\
152 		__get_user_asm("lw", (x), __gu_ptr, __gu_err);	\
153 		break;						\
154 	case 8:							\
155 		__get_user_8((x), __gu_ptr, __gu_err);	\
156 		break;						\
157 	default:						\
158 		BUILD_BUG();					\
159 	}							\
160 } while (0)
161 
162 /**
163  * __get_user: - Get a simple variable from user space, with less checking.
164  * @x:   Variable to store result.
165  * @ptr: Source address, in user space.
166  *
167  * Context: User context only.  This function may sleep.
168  *
169  * This macro copies a single simple variable from user space to kernel
170  * space.  It supports simple types like char and int, but not larger
171  * data types like structures or arrays.
172  *
173  * @ptr must have pointer-to-simple-variable type, and the result of
174  * dereferencing @ptr must be assignable to @x without a cast.
175  *
176  * Caller must check the pointer with access_ok() before calling this
177  * function.
178  *
179  * Returns zero on success, or -EFAULT on error.
180  * On error, the variable @x is set to zero.
181  */
182 #define __get_user(x, ptr)					\
183 ({								\
184 	const __typeof__(*(ptr)) __user *__gu_ptr = (ptr);	\
185 	long __gu_err = 0;					\
186 								\
187 	__chk_user_ptr(__gu_ptr);				\
188 								\
189 	__enable_user_access();					\
190 	__get_user_nocheck(x, __gu_ptr, __gu_err);		\
191 	__disable_user_access();				\
192 								\
193 	__gu_err;						\
194 })
195 
196 /**
197  * get_user: - Get a simple variable from user space.
198  * @x:   Variable to store result.
199  * @ptr: Source address, in user space.
200  *
201  * Context: User context only.  This function may sleep.
202  *
203  * This macro copies a single simple variable from user space to kernel
204  * space.  It supports simple types like char and int, but not larger
205  * data types like structures or arrays.
206  *
207  * @ptr must have pointer-to-simple-variable type, and the result of
208  * dereferencing @ptr must be assignable to @x without a cast.
209  *
210  * Returns zero on success, or -EFAULT on error.
211  * On error, the variable @x is set to zero.
212  */
213 #define get_user(x, ptr)					\
214 ({								\
215 	const __typeof__(*(ptr)) __user *__p = (ptr);		\
216 	might_fault();						\
217 	access_ok(__p, sizeof(*__p)) ?		\
218 		__get_user((x), __p) :				\
219 		((x) = 0, -EFAULT);				\
220 })
221 
222 #define __put_user_asm(insn, x, ptr, err)			\
223 do {								\
224 	uintptr_t __tmp;					\
225 	__typeof__(*(ptr)) __x = x;				\
226 	__asm__ __volatile__ (					\
227 		"1:\n"						\
228 		"	" insn " %z3, %2\n"			\
229 		"2:\n"						\
230 		"	.section .fixup,\"ax\"\n"		\
231 		"	.balign 4\n"				\
232 		"3:\n"						\
233 		"	li %0, %4\n"				\
234 		"	jump 2b, %1\n"				\
235 		"	.previous\n"				\
236 			_ASM_EXTABLE(1b, 3b)			\
237 		: "+r" (err), "=r" (__tmp), "=m" (*(ptr))	\
238 		: "rJ" (__x), "i" (-EFAULT));			\
239 } while (0)
240 
241 #ifdef CONFIG_64BIT
242 #define __put_user_8(x, ptr, err) \
243 	__put_user_asm("sd", x, ptr, err)
244 #else /* !CONFIG_64BIT */
245 #define __put_user_8(x, ptr, err)				\
246 do {								\
247 	u32 __user *__ptr = (u32 __user *)(ptr);		\
248 	u64 __x = (__typeof__((x)-(x)))(x);			\
249 	uintptr_t __tmp;					\
250 	__asm__ __volatile__ (					\
251 		"1:\n"						\
252 		"	sw %z4, %2\n"				\
253 		"2:\n"						\
254 		"	sw %z5, %3\n"				\
255 		"3:\n"						\
256 		"	.section .fixup,\"ax\"\n"		\
257 		"	.balign 4\n"				\
258 		"4:\n"						\
259 		"	li %0, %6\n"				\
260 		"	jump 3b, %1\n"				\
261 		"	.previous\n"				\
262 			_ASM_EXTABLE(1b, 4b)			\
263 			_ASM_EXTABLE(2b, 4b)			\
264 		: "+r" (err), "=r" (__tmp),			\
265 			"=m" (__ptr[__LSW]),			\
266 			"=m" (__ptr[__MSW])			\
267 		: "rJ" (__x), "rJ" (__x >> 32), "i" (-EFAULT));	\
268 } while (0)
269 #endif /* CONFIG_64BIT */
270 
271 #define __put_user_nocheck(x, __gu_ptr, __pu_err)					\
272 do {								\
273 	switch (sizeof(*__gu_ptr)) {				\
274 	case 1:							\
275 		__put_user_asm("sb", (x), __gu_ptr, __pu_err);	\
276 		break;						\
277 	case 2:							\
278 		__put_user_asm("sh", (x), __gu_ptr, __pu_err);	\
279 		break;						\
280 	case 4:							\
281 		__put_user_asm("sw", (x), __gu_ptr, __pu_err);	\
282 		break;						\
283 	case 8:							\
284 		__put_user_8((x), __gu_ptr, __pu_err);	\
285 		break;						\
286 	default:						\
287 		BUILD_BUG();					\
288 	}							\
289 } while (0)
290 
291 /**
292  * __put_user: - Write a simple value into user space, with less checking.
293  * @x:   Value to copy to user space.
294  * @ptr: Destination address, in user space.
295  *
296  * Context: User context only.  This function may sleep.
297  *
298  * This macro copies a single simple value from kernel space to user
299  * space.  It supports simple types like char and int, but not larger
300  * data types like structures or arrays.
301  *
302  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
303  * to the result of dereferencing @ptr. The value of @x is copied to avoid
304  * re-ordering where @x is evaluated inside the block that enables user-space
305  * access (thus bypassing user space protection if @x is a function).
306  *
307  * Caller must check the pointer with access_ok() before calling this
308  * function.
309  *
310  * Returns zero on success, or -EFAULT on error.
311  */
312 #define __put_user(x, ptr)					\
313 ({								\
314 	__typeof__(*(ptr)) __user *__gu_ptr = (ptr);		\
315 	__typeof__(*__gu_ptr) __val = (x);			\
316 	long __pu_err = 0;					\
317 								\
318 	__chk_user_ptr(__gu_ptr);				\
319 								\
320 	__enable_user_access();					\
321 	__put_user_nocheck(__val, __gu_ptr, __pu_err);		\
322 	__disable_user_access();				\
323 								\
324 	__pu_err;						\
325 })
326 
327 /**
328  * put_user: - Write a simple value into user space.
329  * @x:   Value to copy to user space.
330  * @ptr: Destination address, in user space.
331  *
332  * Context: User context only.  This function may sleep.
333  *
334  * This macro copies a single simple value from kernel space to user
335  * space.  It supports simple types like char and int, but not larger
336  * data types like structures or arrays.
337  *
338  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
339  * to the result of dereferencing @ptr.
340  *
341  * Returns zero on success, or -EFAULT on error.
342  */
343 #define put_user(x, ptr)					\
344 ({								\
345 	__typeof__(*(ptr)) __user *__p = (ptr);			\
346 	might_fault();						\
347 	access_ok(__p, sizeof(*__p)) ?		\
348 		__put_user((x), __p) :				\
349 		-EFAULT;					\
350 })
351 
352 
353 unsigned long __must_check __asm_copy_to_user(void __user *to,
354 	const void *from, unsigned long n);
355 unsigned long __must_check __asm_copy_from_user(void *to,
356 	const void __user *from, unsigned long n);
357 
358 static inline unsigned long
359 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
360 {
361 	return __asm_copy_from_user(to, from, n);
362 }
363 
364 static inline unsigned long
365 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
366 {
367 	return __asm_copy_to_user(to, from, n);
368 }
369 
370 extern long strncpy_from_user(char *dest, const char __user *src, long count);
371 
372 extern long __must_check strnlen_user(const char __user *str, long n);
373 
374 extern
375 unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
376 
377 static inline
378 unsigned long __must_check clear_user(void __user *to, unsigned long n)
379 {
380 	might_fault();
381 	return access_ok(to, n) ?
382 		__clear_user(to, n) : n;
383 }
384 
385 #define HAVE_GET_KERNEL_NOFAULT
386 
387 #define __get_kernel_nofault(dst, src, type, err_label)			\
388 do {									\
389 	long __kr_err;							\
390 									\
391 	__get_user_nocheck(*((type *)(dst)), (type *)(src), __kr_err);	\
392 	if (unlikely(__kr_err))						\
393 		goto err_label;						\
394 } while (0)
395 
396 #define __put_kernel_nofault(dst, src, type, err_label)			\
397 do {									\
398 	long __kr_err;							\
399 									\
400 	__put_user_nocheck(*((type *)(src)), (type *)(dst), __kr_err);	\
401 	if (unlikely(__kr_err))						\
402 		goto err_label;						\
403 } while (0)
404 
405 #else /* CONFIG_MMU */
406 #include <asm-generic/uaccess.h>
407 #endif /* CONFIG_MMU */
408 #endif /* _ASM_RISCV_UACCESS_H */
409