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