xref: /openbmc/linux/arch/powerpc/include/asm/uaccess.h (revision 8ffdff6a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ARCH_POWERPC_UACCESS_H
3 #define _ARCH_POWERPC_UACCESS_H
4 
5 #include <asm/ppc_asm.h>
6 #include <asm/processor.h>
7 #include <asm/page.h>
8 #include <asm/extable.h>
9 #include <asm/kup.h>
10 
11 #ifdef __powerpc64__
12 /* We use TASK_SIZE_USER64 as TASK_SIZE is not constant */
13 #define TASK_SIZE_MAX		TASK_SIZE_USER64
14 #else
15 #define TASK_SIZE_MAX		TASK_SIZE
16 #endif
17 
18 static inline bool __access_ok(unsigned long addr, unsigned long size)
19 {
20 	return addr < TASK_SIZE_MAX && size <= TASK_SIZE_MAX - addr;
21 }
22 
23 #define access_ok(addr, size)		\
24 	(__chk_user_ptr(addr),		\
25 	 __access_ok((unsigned long)(addr), (size)))
26 
27 /*
28  * These are the main single-value transfer routines.  They automatically
29  * use the right size if we just have the right pointer type.
30  *
31  * This gets kind of ugly. We want to return _two_ values in "get_user()"
32  * and yet we don't want to do any pointers, because that is too much
33  * of a performance impact. Thus we have a few rather ugly macros here,
34  * and hide all the ugliness from the user.
35  *
36  * The "__xxx" versions of the user access functions are versions that
37  * do not verify the address space, that must have been done previously
38  * with a separate "access_ok()" call (this is used when we do multiple
39  * accesses to the same area of user memory).
40  *
41  * As we use the same address space for kernel and user data on the
42  * PowerPC, we can just do these as direct assignments.  (Of course, the
43  * exception handling means that it's no longer "just"...)
44  *
45  */
46 #define get_user(x, ptr) \
47 	__get_user_check((x), (ptr), sizeof(*(ptr)))
48 #define put_user(x, ptr) \
49 	__put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
50 
51 #define __get_user(x, ptr) \
52 	__get_user_nocheck((x), (ptr), sizeof(*(ptr)), true)
53 #define __put_user(x, ptr) \
54 	__put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
55 
56 #define __get_user_allowed(x, ptr) \
57 	__get_user_nocheck((x), (ptr), sizeof(*(ptr)), false)
58 
59 #define __get_user_inatomic(x, ptr) \
60 	__get_user_nosleep((x), (ptr), sizeof(*(ptr)))
61 #define __put_user_inatomic(x, ptr) \
62 	__put_user_nosleep((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
63 
64 #ifdef CONFIG_PPC64
65 
66 #define ___get_user_instr(gu_op, dest, ptr)				\
67 ({									\
68 	long __gui_ret = 0;						\
69 	unsigned long __gui_ptr = (unsigned long)ptr;			\
70 	struct ppc_inst __gui_inst;					\
71 	unsigned int __prefix, __suffix;				\
72 	__gui_ret = gu_op(__prefix, (unsigned int __user *)__gui_ptr);	\
73 	if (__gui_ret == 0) {						\
74 		if ((__prefix >> 26) == OP_PREFIX) {			\
75 			__gui_ret = gu_op(__suffix,			\
76 				(unsigned int __user *)__gui_ptr + 1);	\
77 			__gui_inst = ppc_inst_prefix(__prefix,		\
78 						     __suffix);		\
79 		} else {						\
80 			__gui_inst = ppc_inst(__prefix);		\
81 		}							\
82 		if (__gui_ret == 0)					\
83 			(dest) = __gui_inst;				\
84 	}								\
85 	__gui_ret;							\
86 })
87 
88 #define get_user_instr(x, ptr) \
89 	___get_user_instr(get_user, x, ptr)
90 
91 #define __get_user_instr(x, ptr) \
92 	___get_user_instr(__get_user, x, ptr)
93 
94 #define __get_user_instr_inatomic(x, ptr) \
95 	___get_user_instr(__get_user_inatomic, x, ptr)
96 
97 #else /* !CONFIG_PPC64 */
98 #define get_user_instr(x, ptr) \
99 	get_user((x).val, (u32 __user *)(ptr))
100 
101 #define __get_user_instr(x, ptr) \
102 	__get_user_nocheck((x).val, (u32 __user *)(ptr), sizeof(u32), true)
103 
104 #define __get_user_instr_inatomic(x, ptr) \
105 	__get_user_nosleep((x).val, (u32 __user *)(ptr), sizeof(u32))
106 
107 #endif /* CONFIG_PPC64 */
108 
109 extern long __put_user_bad(void);
110 
111 #define __put_user_size(x, ptr, size, retval)			\
112 do {								\
113 	__label__ __pu_failed;					\
114 								\
115 	retval = 0;						\
116 	allow_write_to_user(ptr, size);				\
117 	__put_user_size_goto(x, ptr, size, __pu_failed);	\
118 	prevent_write_to_user(ptr, size);			\
119 	break;							\
120 								\
121 __pu_failed:							\
122 	retval = -EFAULT;					\
123 	prevent_write_to_user(ptr, size);			\
124 } while (0)
125 
126 #define __put_user_nocheck(x, ptr, size)			\
127 ({								\
128 	long __pu_err;						\
129 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
130 	__typeof__(*(ptr)) __pu_val = (x);			\
131 	__typeof__(size) __pu_size = (size);			\
132 								\
133 	if (!is_kernel_addr((unsigned long)__pu_addr))		\
134 		might_fault();					\
135 	__chk_user_ptr(__pu_addr);				\
136 	__put_user_size(__pu_val, __pu_addr, __pu_size, __pu_err);	\
137 								\
138 	__pu_err;						\
139 })
140 
141 #define __put_user_check(x, ptr, size)					\
142 ({									\
143 	long __pu_err = -EFAULT;					\
144 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);			\
145 	__typeof__(*(ptr)) __pu_val = (x);				\
146 	__typeof__(size) __pu_size = (size);				\
147 									\
148 	might_fault();							\
149 	if (access_ok(__pu_addr, __pu_size))				\
150 		__put_user_size(__pu_val, __pu_addr, __pu_size, __pu_err); \
151 									\
152 	__pu_err;							\
153 })
154 
155 #define __put_user_nosleep(x, ptr, size)			\
156 ({								\
157 	long __pu_err;						\
158 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
159 	__typeof__(*(ptr)) __pu_val = (x);			\
160 	__typeof__(size) __pu_size = (size);			\
161 								\
162 	__chk_user_ptr(__pu_addr);				\
163 	__put_user_size(__pu_val, __pu_addr, __pu_size, __pu_err); \
164 								\
165 	__pu_err;						\
166 })
167 
168 
169 /*
170  * We don't tell gcc that we are accessing memory, but this is OK
171  * because we do not write to any memory gcc knows about, so there
172  * are no aliasing issues.
173  */
174 #define __put_user_asm_goto(x, addr, label, op)			\
175 	asm_volatile_goto(					\
176 		"1:	" op "%U1%X1 %0,%1	# put_user\n"	\
177 		EX_TABLE(1b, %l2)				\
178 		:						\
179 		: "r" (x), "m"UPD_CONSTR (*addr)		\
180 		:						\
181 		: label)
182 
183 #ifdef __powerpc64__
184 #define __put_user_asm2_goto(x, ptr, label)			\
185 	__put_user_asm_goto(x, ptr, label, "std")
186 #else /* __powerpc64__ */
187 #define __put_user_asm2_goto(x, addr, label)			\
188 	asm_volatile_goto(					\
189 		"1:	stw%X1 %0, %1\n"			\
190 		"2:	stw%X1 %L0, %L1\n"			\
191 		EX_TABLE(1b, %l2)				\
192 		EX_TABLE(2b, %l2)				\
193 		:						\
194 		: "r" (x), "m" (*addr)				\
195 		:						\
196 		: label)
197 #endif /* __powerpc64__ */
198 
199 #define __put_user_size_goto(x, ptr, size, label)		\
200 do {								\
201 	switch (size) {						\
202 	case 1: __put_user_asm_goto(x, ptr, label, "stb"); break;	\
203 	case 2: __put_user_asm_goto(x, ptr, label, "sth"); break;	\
204 	case 4: __put_user_asm_goto(x, ptr, label, "stw"); break;	\
205 	case 8: __put_user_asm2_goto(x, ptr, label); break;	\
206 	default: __put_user_bad();				\
207 	}							\
208 } while (0)
209 
210 #define __unsafe_put_user_goto(x, ptr, size, label)		\
211 do {								\
212 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
213 	__chk_user_ptr(ptr);					\
214 	__put_user_size_goto((x), __pu_addr, (size), label);	\
215 } while (0)
216 
217 
218 extern long __get_user_bad(void);
219 
220 /*
221  * This does an atomic 128 byte aligned load from userspace.
222  * Upto caller to do enable_kernel_vmx() before calling!
223  */
224 #define __get_user_atomic_128_aligned(kaddr, uaddr, err)		\
225 	__asm__ __volatile__(				\
226 		"1:	lvx  0,0,%1	# get user\n"	\
227 		" 	stvx 0,0,%2	# put kernel\n"	\
228 		"2:\n"					\
229 		".section .fixup,\"ax\"\n"		\
230 		"3:	li %0,%3\n"			\
231 		"	b 2b\n"				\
232 		".previous\n"				\
233 		EX_TABLE(1b, 3b)			\
234 		: "=r" (err)			\
235 		: "b" (uaddr), "b" (kaddr), "i" (-EFAULT), "0" (err))
236 
237 #define __get_user_asm(x, addr, err, op)		\
238 	__asm__ __volatile__(				\
239 		"1:	"op"%U2%X2 %1, %2	# get_user\n"	\
240 		"2:\n"					\
241 		".section .fixup,\"ax\"\n"		\
242 		"3:	li %0,%3\n"			\
243 		"	li %1,0\n"			\
244 		"	b 2b\n"				\
245 		".previous\n"				\
246 		EX_TABLE(1b, 3b)			\
247 		: "=r" (err), "=r" (x)			\
248 		: "m"UPD_CONSTR (*addr), "i" (-EFAULT), "0" (err))
249 
250 #ifdef __powerpc64__
251 #define __get_user_asm2(x, addr, err)			\
252 	__get_user_asm(x, addr, err, "ld")
253 #else /* __powerpc64__ */
254 #define __get_user_asm2(x, addr, err)			\
255 	__asm__ __volatile__(				\
256 		"1:	lwz%X2 %1, %2\n"			\
257 		"2:	lwz%X2 %L1, %L2\n"		\
258 		"3:\n"					\
259 		".section .fixup,\"ax\"\n"		\
260 		"4:	li %0,%3\n"			\
261 		"	li %1,0\n"			\
262 		"	li %1+1,0\n"			\
263 		"	b 3b\n"				\
264 		".previous\n"				\
265 		EX_TABLE(1b, 4b)			\
266 		EX_TABLE(2b, 4b)			\
267 		: "=r" (err), "=&r" (x)			\
268 		: "m" (*addr), "i" (-EFAULT), "0" (err))
269 #endif /* __powerpc64__ */
270 
271 #define __get_user_size_allowed(x, ptr, size, retval)		\
272 do {								\
273 	retval = 0;						\
274 	__chk_user_ptr(ptr);					\
275 	if (size > sizeof(x))					\
276 		(x) = __get_user_bad();				\
277 	switch (size) {						\
278 	case 1: __get_user_asm(x, (u8 __user *)ptr, retval, "lbz"); break;	\
279 	case 2: __get_user_asm(x, (u16 __user *)ptr, retval, "lhz"); break;	\
280 	case 4: __get_user_asm(x, (u32 __user *)ptr, retval, "lwz"); break;	\
281 	case 8: __get_user_asm2(x, (u64 __user *)ptr, retval);  break;	\
282 	default: (x) = __get_user_bad();			\
283 	}							\
284 } while (0)
285 
286 #define __get_user_size(x, ptr, size, retval)			\
287 do {								\
288 	allow_read_from_user(ptr, size);			\
289 	__get_user_size_allowed(x, ptr, size, retval);		\
290 	prevent_read_from_user(ptr, size);			\
291 } while (0)
292 
293 /*
294  * This is a type: either unsigned long, if the argument fits into
295  * that type, or otherwise unsigned long long.
296  */
297 #define __long_type(x) \
298 	__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
299 
300 #define __get_user_nocheck(x, ptr, size, do_allow)			\
301 ({								\
302 	long __gu_err;						\
303 	__long_type(*(ptr)) __gu_val;				\
304 	__typeof__(*(ptr)) __user *__gu_addr = (ptr);	\
305 	__typeof__(size) __gu_size = (size);			\
306 								\
307 	__chk_user_ptr(__gu_addr);				\
308 	if (do_allow && !is_kernel_addr((unsigned long)__gu_addr)) \
309 		might_fault();					\
310 	if (do_allow)								\
311 		__get_user_size(__gu_val, __gu_addr, __gu_size, __gu_err);	\
312 	else									\
313 		__get_user_size_allowed(__gu_val, __gu_addr, __gu_size, __gu_err); \
314 	(x) = (__typeof__(*(ptr)))__gu_val;			\
315 								\
316 	__gu_err;						\
317 })
318 
319 #define __get_user_check(x, ptr, size)					\
320 ({									\
321 	long __gu_err = -EFAULT;					\
322 	__long_type(*(ptr)) __gu_val = 0;				\
323 	__typeof__(*(ptr)) __user *__gu_addr = (ptr);		\
324 	__typeof__(size) __gu_size = (size);				\
325 									\
326 	might_fault();							\
327 	if (access_ok(__gu_addr, __gu_size))				\
328 		__get_user_size(__gu_val, __gu_addr, __gu_size, __gu_err); \
329 	(x) = (__force __typeof__(*(ptr)))__gu_val;				\
330 									\
331 	__gu_err;							\
332 })
333 
334 #define __get_user_nosleep(x, ptr, size)			\
335 ({								\
336 	long __gu_err;						\
337 	__long_type(*(ptr)) __gu_val;				\
338 	__typeof__(*(ptr)) __user *__gu_addr = (ptr);	\
339 	__typeof__(size) __gu_size = (size);			\
340 								\
341 	__chk_user_ptr(__gu_addr);				\
342 	__get_user_size(__gu_val, __gu_addr, __gu_size, __gu_err); \
343 	(x) = (__force __typeof__(*(ptr)))__gu_val;			\
344 								\
345 	__gu_err;						\
346 })
347 
348 
349 /* more complex routines */
350 
351 extern unsigned long __copy_tofrom_user(void __user *to,
352 		const void __user *from, unsigned long size);
353 
354 #ifdef CONFIG_ARCH_HAS_COPY_MC
355 unsigned long __must_check
356 copy_mc_generic(void *to, const void *from, unsigned long size);
357 
358 static inline unsigned long __must_check
359 copy_mc_to_kernel(void *to, const void *from, unsigned long size)
360 {
361 	return copy_mc_generic(to, from, size);
362 }
363 #define copy_mc_to_kernel copy_mc_to_kernel
364 
365 static inline unsigned long __must_check
366 copy_mc_to_user(void __user *to, const void *from, unsigned long n)
367 {
368 	if (likely(check_copy_size(from, n, true))) {
369 		if (access_ok(to, n)) {
370 			allow_write_to_user(to, n);
371 			n = copy_mc_generic((void *)to, from, n);
372 			prevent_write_to_user(to, n);
373 		}
374 	}
375 
376 	return n;
377 }
378 #endif
379 
380 #ifdef __powerpc64__
381 static inline unsigned long
382 raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
383 {
384 	unsigned long ret;
385 
386 	allow_read_write_user(to, from, n);
387 	ret = __copy_tofrom_user(to, from, n);
388 	prevent_read_write_user(to, from, n);
389 	return ret;
390 }
391 #endif /* __powerpc64__ */
392 
393 static inline unsigned long raw_copy_from_user(void *to,
394 		const void __user *from, unsigned long n)
395 {
396 	unsigned long ret;
397 
398 	allow_read_from_user(from, n);
399 	ret = __copy_tofrom_user((__force void __user *)to, from, n);
400 	prevent_read_from_user(from, n);
401 	return ret;
402 }
403 
404 static inline unsigned long
405 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
406 {
407 	unsigned long ret;
408 
409 	allow_write_to_user(to, n);
410 	ret = __copy_tofrom_user(to, (__force const void __user *)from, n);
411 	prevent_write_to_user(to, n);
412 	return ret;
413 }
414 
415 unsigned long __arch_clear_user(void __user *addr, unsigned long size);
416 
417 static inline unsigned long clear_user(void __user *addr, unsigned long size)
418 {
419 	unsigned long ret = size;
420 	might_fault();
421 	if (likely(access_ok(addr, size))) {
422 		allow_write_to_user(addr, size);
423 		ret = __arch_clear_user(addr, size);
424 		prevent_write_to_user(addr, size);
425 	}
426 	return ret;
427 }
428 
429 static inline unsigned long __clear_user(void __user *addr, unsigned long size)
430 {
431 	return clear_user(addr, size);
432 }
433 
434 extern long strncpy_from_user(char *dst, const char __user *src, long count);
435 extern __must_check long strnlen_user(const char __user *str, long n);
436 
437 extern long __copy_from_user_flushcache(void *dst, const void __user *src,
438 		unsigned size);
439 extern void memcpy_page_flushcache(char *to, struct page *page, size_t offset,
440 			   size_t len);
441 
442 static __must_check inline bool user_access_begin(const void __user *ptr, size_t len)
443 {
444 	if (unlikely(!access_ok(ptr, len)))
445 		return false;
446 
447 	might_fault();
448 
449 	allow_read_write_user((void __user *)ptr, ptr, len);
450 	return true;
451 }
452 #define user_access_begin	user_access_begin
453 #define user_access_end		prevent_current_access_user
454 #define user_access_save	prevent_user_access_return
455 #define user_access_restore	restore_user_access
456 
457 static __must_check inline bool
458 user_read_access_begin(const void __user *ptr, size_t len)
459 {
460 	if (unlikely(!access_ok(ptr, len)))
461 		return false;
462 
463 	might_fault();
464 
465 	allow_read_from_user(ptr, len);
466 	return true;
467 }
468 #define user_read_access_begin	user_read_access_begin
469 #define user_read_access_end		prevent_current_read_from_user
470 
471 static __must_check inline bool
472 user_write_access_begin(const void __user *ptr, size_t len)
473 {
474 	if (unlikely(!access_ok(ptr, len)))
475 		return false;
476 
477 	might_fault();
478 
479 	allow_write_to_user((void __user *)ptr, len);
480 	return true;
481 }
482 #define user_write_access_begin	user_write_access_begin
483 #define user_write_access_end		prevent_current_write_to_user
484 
485 #define unsafe_op_wrap(op, err) do { if (unlikely(op)) goto err; } while (0)
486 #define unsafe_get_user(x, p, e) unsafe_op_wrap(__get_user_allowed(x, p), e)
487 #define unsafe_put_user(x, p, e) \
488 	__unsafe_put_user_goto((__typeof__(*(p)))(x), (p), sizeof(*(p)), e)
489 
490 #define unsafe_copy_to_user(d, s, l, e) \
491 do {									\
492 	u8 __user *_dst = (u8 __user *)(d);				\
493 	const u8 *_src = (const u8 *)(s);				\
494 	size_t _len = (l);						\
495 	int _i;								\
496 									\
497 	for (_i = 0; _i < (_len & ~(sizeof(long) - 1)); _i += sizeof(long))		\
498 		unsafe_put_user(*(long*)(_src + _i), (long __user *)(_dst + _i), e); \
499 	if (IS_ENABLED(CONFIG_PPC64) && (_len & 4)) {			\
500 		unsafe_put_user(*(u32*)(_src + _i), (u32 __user *)(_dst + _i), e); \
501 		_i += 4;						\
502 	}								\
503 	if (_len & 2) {							\
504 		unsafe_put_user(*(u16*)(_src + _i), (u16 __user *)(_dst + _i), e); \
505 		_i += 2;						\
506 	}								\
507 	if (_len & 1) \
508 		unsafe_put_user(*(u8*)(_src + _i), (u8 __user *)(_dst + _i), e); \
509 } while (0)
510 
511 #define HAVE_GET_KERNEL_NOFAULT
512 
513 #define __get_kernel_nofault(dst, src, type, err_label)			\
514 do {									\
515 	int __kr_err;							\
516 									\
517 	__get_user_size_allowed(*((type *)(dst)), (__force type __user *)(src),\
518 			sizeof(type), __kr_err);			\
519 	if (unlikely(__kr_err))						\
520 		goto err_label;						\
521 } while (0)
522 
523 #define __put_kernel_nofault(dst, src, type, err_label)			\
524 	__put_user_size_goto(*((type *)(src)),				\
525 		(__force type __user *)(dst), sizeof(type), err_label)
526 
527 #endif	/* _ARCH_POWERPC_UACCESS_H */
528