1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * This file is subject to the terms and conditions of the GNU General Public
4  * License.  See the file "COPYING" in the main directory of this archive
5  * for more details.
6  *
7  * Copyright (C) 2020 Loongson Technology Co., Ltd.
8  */
9 #ifndef _ASM_UACCESS_H
10 #define _ASM_UACCESS_H
11 
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/extable.h>
15 #include <asm/extable.h>
16 #include <asm/asm-extable.h>
17 
18 #ifdef CONFIG_64BIT
19 
20 extern u64 __ua_limit;
21 
22 #define __UA_LIMIT	__ua_limit
23 
24 #define __UA_ADDR	".dword"
25 #define __UA_LA		"la.abs"
26 
27 #endif /* CONFIG_64BIT */
28 
29 /*
30  * Is a address valid? This does a straightforward calculation rather
31  * than tests.
32  *
33  * Address valid if:
34  *  - "addr" doesn't have any high-bits set
35  *  - AND "size" doesn't have any high-bits set
36  *  - AND "addr+size" doesn't have any high-bits set
37  *  - OR we are in kernel mode.
38  *
39  * __ua_size() is a trick to avoid runtime checking of positive constant
40  * sizes; for those we already know at compile time that the size is ok.
41  */
42 #define __ua_size(size)							\
43 	((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
44 
45 /*
46  * access_ok: - Checks if a user space pointer is valid
47  * @addr: User space pointer to start of block to check
48  * @size: Size of block to check
49  *
50  * Context: User context only. This function may sleep if pagefaults are
51  *          enabled.
52  *
53  * Checks if a pointer to a block of memory in user space is valid.
54  *
55  * Returns true (nonzero) if the memory block may be valid, false (zero)
56  * if it is definitely invalid.
57  *
58  * Note that, depending on architecture, this function probably just
59  * checks that the pointer is in the user space range - after calling
60  * this function, memory access functions may still return -EFAULT.
61  */
__access_ok(const void __user *p, unsigned long size)62 static inline int __access_ok(const void __user *p, unsigned long size)
63 {
64 	unsigned long addr = (unsigned long)p;
65 	unsigned long end = addr + size - !!size;
66 
67 	return (__UA_LIMIT & (addr | end | __ua_size(size))) == 0;
68 }
69 
70 #define access_ok(addr, size)					\
71 	likely(__access_ok((addr), (size)))
72 
73 /*
74  * get_user: - Get a simple variable from user space.
75  * @x:	 Variable to store result.
76  * @ptr: Source address, in user space.
77  *
78  * Context: User context only. This function may sleep if pagefaults are
79  *          enabled.
80  *
81  * This macro copies a single simple variable from user space to kernel
82  * space.  It supports simple types like char and int, but not larger
83  * data types like structures or arrays.
84  *
85  * @ptr must have pointer-to-simple-variable type, and the result of
86  * dereferencing @ptr must be assignable to @x without a cast.
87  *
88  * Returns zero on success, or -EFAULT on error.
89  * On error, the variable @x is set to zero.
90  */
91 #define get_user(x,ptr) \
92 ({									\
93 	const __typeof__(*(ptr)) __user *__p = (ptr);			\
94 									\
95 	might_fault();							\
96 	access_ok(__p, sizeof(*__p)) ? __get_user((x), __p) :		\
97 				       ((x) = 0, -EFAULT);		\
98 })
99 
100 /*
101  * put_user: - Write a simple value into user space.
102  * @x:	 Value to copy to user space.
103  * @ptr: Destination address, in user space.
104  *
105  * Context: User context only. This function may sleep if pagefaults are
106  *          enabled.
107  *
108  * This macro copies a single simple value from kernel space to user
109  * space.  It supports simple types like char and int, but not larger
110  * data types like structures or arrays.
111  *
112  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
113  * to the result of dereferencing @ptr.
114  *
115  * Returns zero on success, or -EFAULT on error.
116  */
117 #define put_user(x,ptr) \
118 ({									\
119 	__typeof__(*(ptr)) __user *__p = (ptr);				\
120 									\
121 	might_fault();							\
122 	access_ok(__p, sizeof(*__p)) ? __put_user((x), __p) : -EFAULT;	\
123 })
124 
125 /*
126  * __get_user: - Get a simple variable from user space, with less checking.
127  * @x:	 Variable to store result.
128  * @ptr: Source address, in user space.
129  *
130  * Context: User context only. This function may sleep if pagefaults are
131  *          enabled.
132  *
133  * This macro copies a single simple variable from user space to kernel
134  * space.  It supports simple types like char and int, but not larger
135  * data types like structures or arrays.
136  *
137  * @ptr must have pointer-to-simple-variable type, and the result of
138  * dereferencing @ptr must be assignable to @x without a cast.
139  *
140  * Caller must check the pointer with access_ok() before calling this
141  * function.
142  *
143  * Returns zero on success, or -EFAULT on error.
144  * On error, the variable @x is set to zero.
145  */
146 #define __get_user(x,ptr) \
147 ({									\
148 	int __gu_err = 0;						\
149 									\
150 	__chk_user_ptr(ptr);						\
151 	__get_user_common((x), sizeof(*(ptr)), ptr);			\
152 	__gu_err;							\
153 })
154 
155 /*
156  * __put_user: - Write a simple value into user space, with less checking.
157  * @x:	 Value to copy to user space.
158  * @ptr: Destination address, in user space.
159  *
160  * Context: User context only. This function may sleep if pagefaults are
161  *          enabled.
162  *
163  * This macro copies a single simple value from kernel space to user
164  * space.  It supports simple types like char and int, but not larger
165  * data types like structures or arrays.
166  *
167  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
168  * to the result of dereferencing @ptr.
169  *
170  * Caller must check the pointer with access_ok() before calling this
171  * function.
172  *
173  * Returns zero on success, or -EFAULT on error.
174  */
175 #define __put_user(x,ptr) \
176 ({									\
177 	int __pu_err = 0;						\
178 	__typeof__(*(ptr)) __pu_val;					\
179 									\
180 	__pu_val = (x);							\
181 	__chk_user_ptr(ptr);						\
182 	__put_user_common(ptr, sizeof(*(ptr)));				\
183 	__pu_err;							\
184 })
185 
186 struct __large_struct { unsigned long buf[100]; };
187 #define __m(x) (*(struct __large_struct __user *)(x))
188 
189 #ifdef CONFIG_32BIT
190 #define __GET_DW(val, insn, ptr) __get_data_asm_ll32(val, insn, ptr)
191 #endif
192 #ifdef CONFIG_64BIT
193 #define __GET_DW(val, insn, ptr) __get_data_asm(val, insn, ptr)
194 #endif
195 
196 #define __get_user_common(val, size, ptr)				\
197 do {									\
198 	switch (size) {							\
199 	case 1: __get_data_asm(val, "ld.b", ptr); break;		\
200 	case 2: __get_data_asm(val, "ld.h", ptr); break;		\
201 	case 4: __get_data_asm(val, "ld.w", ptr); break;		\
202 	case 8: __GET_DW(val, "ld.d", ptr); break;			\
203 	default: BUILD_BUG(); break;					\
204 	}								\
205 } while (0)
206 
207 #define __get_kernel_common(val, size, ptr) __get_user_common(val, size, ptr)
208 
209 #define __get_data_asm(val, insn, ptr)					\
210 {									\
211 	long __gu_tmp;							\
212 									\
213 	__asm__ __volatile__(						\
214 	"1:	" insn "	%1, %2				\n"	\
215 	"2:							\n"	\
216 	_ASM_EXTABLE_UACCESS_ERR_ZERO(1b, 2b, %0, %1)			\
217 	: "+r" (__gu_err), "=r" (__gu_tmp)				\
218 	: "m" (__m(ptr)));						\
219 									\
220 	(val) = (__typeof__(*(ptr))) __gu_tmp;				\
221 }
222 
223 /*
224  * Get a long long 64 using 32 bit registers.
225  */
226 #define __get_data_asm_ll32(val, insn, ptr)				\
227 {									\
228 	union {								\
229 		unsigned long long	l;				\
230 		__typeof__(*(addr))	t;				\
231 	} __gu_tmp;							\
232 									\
233 	__asm__ __volatile__(						\
234 	"1:	ld.w	%1, (%2)				\n"	\
235 	"2:	ld.w	%D1, 4(%2)				\n"	\
236 	"3:							\n"	\
237 	"	.section	.fixup,\"ax\"			\n"	\
238 	"4:	li.w	%0, %3					\n"	\
239 	"	slli.d	%1, $r0, 0				\n"	\
240 	"	slli.d	%D1, $r0, 0				\n"	\
241 	"	b	3b					\n"	\
242 	"	.previous					\n"	\
243 	"	.section	__ex_table,\"a\"		\n"	\
244 	"	" __UA_ADDR "	1b, 4b				\n"	\
245 	"	" __UA_ADDR "	2b, 4b				\n"	\
246 	"	.previous					\n"	\
247 	: "+r" (__gu_err), "=&r" (__gu_tmp.l)				\
248 	: "r" (ptr), "i" (-EFAULT));					\
249 									\
250 	(val) = __gu_tmp.t;						\
251 }
252 
253 #ifdef CONFIG_32BIT
254 #define __PUT_DW(insn, ptr) __put_data_asm_ll32(insn, ptr)
255 #endif
256 #ifdef CONFIG_64BIT
257 #define __PUT_DW(insn, ptr) __put_data_asm(insn, ptr)
258 #endif
259 
260 #define __put_user_common(ptr, size)					\
261 do {									\
262 	switch (size) {							\
263 	case 1: __put_data_asm("st.b", ptr); break;			\
264 	case 2: __put_data_asm("st.h", ptr); break;			\
265 	case 4: __put_data_asm("st.w", ptr); break;			\
266 	case 8: __PUT_DW("st.d", ptr); break;				\
267 	default: BUILD_BUG(); break;					\
268 	}								\
269 } while (0)
270 
271 #define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
272 
273 #define __put_data_asm(insn, ptr)					\
274 {									\
275 	__asm__ __volatile__(						\
276 	"1:	" insn "	%z2, %1		# __put_user_asm\n"	\
277 	"2:							\n"	\
278 	_ASM_EXTABLE_UACCESS_ERR(1b, 2b, %0)				\
279 	: "+r" (__pu_err), "=m" (__m(ptr))				\
280 	: "Jr" (__pu_val));						\
281 }
282 
283 #define __put_data_asm_ll32(insn, ptr)					\
284 {									\
285 	__asm__ __volatile__(						\
286 	"1:	st.w	%1, (%2)	# __put_user_asm_ll32	\n"	\
287 	"2:	st.w	%D1, 4(%2)				\n"	\
288 	"3:							\n"	\
289 	"	.section	.fixup,\"ax\"			\n"	\
290 	"4:	li.w	%0, %3					\n"	\
291 	"	b	3b					\n"	\
292 	"	.previous					\n"	\
293 	"	.section	__ex_table,\"a\"		\n"	\
294 	"	" __UA_ADDR "	1b, 4b				\n"	\
295 	"	" __UA_ADDR "	2b, 4b				\n"	\
296 	"	.previous"						\
297 	: "+r" (__pu_err)						\
298 	: "r" (__pu_val), "r" (ptr), "i" (-EFAULT));			\
299 }
300 
301 #define HAVE_GET_KERNEL_NOFAULT
302 
303 #define __get_kernel_nofault(dst, src, type, err_label)			\
304 do {									\
305 	int __gu_err = 0;						\
306 									\
307 	__get_kernel_common(*((type *)(dst)), sizeof(type),		\
308 			    (__force type *)(src));			\
309 	if (unlikely(__gu_err))						\
310 		goto err_label;						\
311 } while (0)
312 
313 #define __put_kernel_nofault(dst, src, type, err_label)			\
314 do {									\
315 	type __pu_val;							\
316 	int __pu_err = 0;						\
317 									\
318 	__pu_val = *(__force type *)(src);				\
319 	__put_kernel_common(((type *)(dst)), sizeof(type));		\
320 	if (unlikely(__pu_err))						\
321 		goto err_label;						\
322 } while (0)
323 
324 extern unsigned long __copy_user(void *to, const void *from, __kernel_size_t n);
325 
326 static inline unsigned long __must_check
raw_copy_from_user(void *to, const void __user *from, unsigned long n)327 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
328 {
329 	return __copy_user(to, (__force const void *)from, n);
330 }
331 
332 static inline unsigned long __must_check
raw_copy_to_user(void __user *to, const void *from, unsigned long n)333 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
334 {
335 	return __copy_user((__force void *)to, from, n);
336 }
337 
338 #define INLINE_COPY_FROM_USER
339 #define INLINE_COPY_TO_USER
340 
341 /*
342  * __clear_user: - Zero a block of memory in user space, with less checking.
343  * @addr: Destination address, in user space.
344  * @to:	  Number of bytes to zero.
345  *
346  * Zero a block of memory in user space.  Caller must check
347  * the specified block with access_ok() before calling this function.
348  *
349  * Returns number of bytes that could not be cleared.
350  * On success, this will be zero.
351  */
352 extern unsigned long __clear_user(void __user *addr, __kernel_size_t size);
353 
354 #define clear_user(addr,n)						\
355 ({									\
356 	void __user * __cl_addr = (addr);				\
357 	unsigned long __cl_size = (n);					\
358 	if (__cl_size && access_ok(__cl_addr, __cl_size))		\
359 		__cl_size = __clear_user(__cl_addr, __cl_size);		\
360 	__cl_size;							\
361 })
362 
363 extern long strncpy_from_user(char *dest, const char __user *src, long count);
364 extern long strnlen_user(const char __user *str, long n);
365 
366 #endif /* _ASM_UACCESS_H */
367