xref: /openbmc/linux/arch/powerpc/include/asm/uaccess.h (revision 8f8d5745bb520c76b81abef4a2cb3023d0313bfd)
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 
10 /*
11  * The fs value determines whether argument validity checking should be
12  * performed or not.  If get_fs() == USER_DS, checking is performed, with
13  * get_fs() == KERNEL_DS, checking is bypassed.
14  *
15  * For historical reasons, these macros are grossly misnamed.
16  *
17  * The fs/ds values are now the highest legal address in the "segment".
18  * This simplifies the checking in the routines below.
19  */
20 
21 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
22 
23 #define KERNEL_DS	MAKE_MM_SEG(~0UL)
24 #ifdef __powerpc64__
25 /* We use TASK_SIZE_USER64 as TASK_SIZE is not constant */
26 #define USER_DS		MAKE_MM_SEG(TASK_SIZE_USER64 - 1)
27 #else
28 #define USER_DS		MAKE_MM_SEG(TASK_SIZE - 1)
29 #endif
30 
31 #define get_fs()	(current->thread.addr_limit)
32 
33 static inline void set_fs(mm_segment_t fs)
34 {
35 	current->thread.addr_limit = fs;
36 	/* On user-mode return check addr_limit (fs) is correct */
37 	set_thread_flag(TIF_FSCHECK);
38 }
39 
40 #define segment_eq(a, b)	((a).seg == (b).seg)
41 
42 #define user_addr_max()	(get_fs().seg)
43 
44 #ifdef __powerpc64__
45 /*
46  * This check is sufficient because there is a large enough
47  * gap between user addresses and the kernel addresses
48  */
49 #define __access_ok(addr, size, segment)	\
50 	(((addr) <= (segment).seg) && ((size) <= (segment).seg))
51 
52 #else
53 
54 static inline int __access_ok(unsigned long addr, unsigned long size,
55 			mm_segment_t seg)
56 {
57 	if (addr > seg.seg)
58 		return 0;
59 	return (size == 0 || size - 1 <= seg.seg - addr);
60 }
61 
62 #endif
63 
64 #define access_ok(addr, size)		\
65 	(__chk_user_ptr(addr),		\
66 	 __access_ok((__force unsigned long)(addr), (size), get_fs()))
67 
68 /*
69  * These are the main single-value transfer routines.  They automatically
70  * use the right size if we just have the right pointer type.
71  *
72  * This gets kind of ugly. We want to return _two_ values in "get_user()"
73  * and yet we don't want to do any pointers, because that is too much
74  * of a performance impact. Thus we have a few rather ugly macros here,
75  * and hide all the ugliness from the user.
76  *
77  * The "__xxx" versions of the user access functions are versions that
78  * do not verify the address space, that must have been done previously
79  * with a separate "access_ok()" call (this is used when we do multiple
80  * accesses to the same area of user memory).
81  *
82  * As we use the same address space for kernel and user data on the
83  * PowerPC, we can just do these as direct assignments.  (Of course, the
84  * exception handling means that it's no longer "just"...)
85  *
86  */
87 #define get_user(x, ptr) \
88 	__get_user_check((x), (ptr), sizeof(*(ptr)))
89 #define put_user(x, ptr) \
90 	__put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
91 
92 #define __get_user(x, ptr) \
93 	__get_user_nocheck((x), (ptr), sizeof(*(ptr)))
94 #define __put_user(x, ptr) \
95 	__put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
96 
97 #define __get_user_inatomic(x, ptr) \
98 	__get_user_nosleep((x), (ptr), sizeof(*(ptr)))
99 #define __put_user_inatomic(x, ptr) \
100 	__put_user_nosleep((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
101 
102 extern long __put_user_bad(void);
103 
104 /*
105  * We don't tell gcc that we are accessing memory, but this is OK
106  * because we do not write to any memory gcc knows about, so there
107  * are no aliasing issues.
108  */
109 #define __put_user_asm(x, addr, err, op)			\
110 	__asm__ __volatile__(					\
111 		"1:	" op " %1,0(%2)	# put_user\n"		\
112 		"2:\n"						\
113 		".section .fixup,\"ax\"\n"			\
114 		"3:	li %0,%3\n"				\
115 		"	b 2b\n"					\
116 		".previous\n"					\
117 		EX_TABLE(1b, 3b)				\
118 		: "=r" (err)					\
119 		: "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
120 
121 #ifdef __powerpc64__
122 #define __put_user_asm2(x, ptr, retval)				\
123 	  __put_user_asm(x, ptr, retval, "std")
124 #else /* __powerpc64__ */
125 #define __put_user_asm2(x, addr, err)				\
126 	__asm__ __volatile__(					\
127 		"1:	stw %1,0(%2)\n"				\
128 		"2:	stw %1+1,4(%2)\n"			\
129 		"3:\n"						\
130 		".section .fixup,\"ax\"\n"			\
131 		"4:	li %0,%3\n"				\
132 		"	b 3b\n"					\
133 		".previous\n"					\
134 		EX_TABLE(1b, 4b)				\
135 		EX_TABLE(2b, 4b)				\
136 		: "=r" (err)					\
137 		: "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
138 #endif /* __powerpc64__ */
139 
140 #define __put_user_size(x, ptr, size, retval)			\
141 do {								\
142 	retval = 0;						\
143 	switch (size) {						\
144 	  case 1: __put_user_asm(x, ptr, retval, "stb"); break;	\
145 	  case 2: __put_user_asm(x, ptr, retval, "sth"); break;	\
146 	  case 4: __put_user_asm(x, ptr, retval, "stw"); break;	\
147 	  case 8: __put_user_asm2(x, ptr, retval); break;	\
148 	  default: __put_user_bad();				\
149 	}							\
150 } while (0)
151 
152 #define __put_user_nocheck(x, ptr, size)			\
153 ({								\
154 	long __pu_err;						\
155 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
156 	if (!is_kernel_addr((unsigned long)__pu_addr))		\
157 		might_fault();					\
158 	__chk_user_ptr(ptr);					\
159 	__put_user_size((x), __pu_addr, (size), __pu_err);	\
160 	__pu_err;						\
161 })
162 
163 #define __put_user_check(x, ptr, size)					\
164 ({									\
165 	long __pu_err = -EFAULT;					\
166 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);			\
167 	might_fault();							\
168 	if (access_ok(__pu_addr, size))			\
169 		__put_user_size((x), __pu_addr, (size), __pu_err);	\
170 	__pu_err;							\
171 })
172 
173 #define __put_user_nosleep(x, ptr, size)			\
174 ({								\
175 	long __pu_err;						\
176 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
177 	__chk_user_ptr(ptr);					\
178 	__put_user_size((x), __pu_addr, (size), __pu_err);	\
179 	__pu_err;						\
180 })
181 
182 
183 extern long __get_user_bad(void);
184 
185 /*
186  * This does an atomic 128 byte aligned load from userspace.
187  * Upto caller to do enable_kernel_vmx() before calling!
188  */
189 #define __get_user_atomic_128_aligned(kaddr, uaddr, err)		\
190 	__asm__ __volatile__(				\
191 		"1:	lvx  0,0,%1	# get user\n"	\
192 		" 	stvx 0,0,%2	# put kernel\n"	\
193 		"2:\n"					\
194 		".section .fixup,\"ax\"\n"		\
195 		"3:	li %0,%3\n"			\
196 		"	b 2b\n"				\
197 		".previous\n"				\
198 		EX_TABLE(1b, 3b)			\
199 		: "=r" (err)			\
200 		: "b" (uaddr), "b" (kaddr), "i" (-EFAULT), "0" (err))
201 
202 #define __get_user_asm(x, addr, err, op)		\
203 	__asm__ __volatile__(				\
204 		"1:	"op" %1,0(%2)	# get_user\n"	\
205 		"2:\n"					\
206 		".section .fixup,\"ax\"\n"		\
207 		"3:	li %0,%3\n"			\
208 		"	li %1,0\n"			\
209 		"	b 2b\n"				\
210 		".previous\n"				\
211 		EX_TABLE(1b, 3b)			\
212 		: "=r" (err), "=r" (x)			\
213 		: "b" (addr), "i" (-EFAULT), "0" (err))
214 
215 #ifdef __powerpc64__
216 #define __get_user_asm2(x, addr, err)			\
217 	__get_user_asm(x, addr, err, "ld")
218 #else /* __powerpc64__ */
219 #define __get_user_asm2(x, addr, err)			\
220 	__asm__ __volatile__(				\
221 		"1:	lwz %1,0(%2)\n"			\
222 		"2:	lwz %1+1,4(%2)\n"		\
223 		"3:\n"					\
224 		".section .fixup,\"ax\"\n"		\
225 		"4:	li %0,%3\n"			\
226 		"	li %1,0\n"			\
227 		"	li %1+1,0\n"			\
228 		"	b 3b\n"				\
229 		".previous\n"				\
230 		EX_TABLE(1b, 4b)			\
231 		EX_TABLE(2b, 4b)			\
232 		: "=r" (err), "=&r" (x)			\
233 		: "b" (addr), "i" (-EFAULT), "0" (err))
234 #endif /* __powerpc64__ */
235 
236 #define __get_user_size(x, ptr, size, retval)			\
237 do {								\
238 	retval = 0;						\
239 	__chk_user_ptr(ptr);					\
240 	if (size > sizeof(x))					\
241 		(x) = __get_user_bad();				\
242 	switch (size) {						\
243 	case 1: __get_user_asm(x, ptr, retval, "lbz"); break;	\
244 	case 2: __get_user_asm(x, ptr, retval, "lhz"); break;	\
245 	case 4: __get_user_asm(x, ptr, retval, "lwz"); break;	\
246 	case 8: __get_user_asm2(x, ptr, retval);  break;	\
247 	default: (x) = __get_user_bad();			\
248 	}							\
249 } while (0)
250 
251 /*
252  * This is a type: either unsigned long, if the argument fits into
253  * that type, or otherwise unsigned long long.
254  */
255 #define __long_type(x) \
256 	__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
257 
258 #define __get_user_nocheck(x, ptr, size)			\
259 ({								\
260 	long __gu_err;						\
261 	__long_type(*(ptr)) __gu_val;				\
262 	__typeof__(*(ptr)) __user *__gu_addr = (ptr);	\
263 	__chk_user_ptr(ptr);					\
264 	if (!is_kernel_addr((unsigned long)__gu_addr))		\
265 		might_fault();					\
266 	barrier_nospec();					\
267 	__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
268 	(x) = (__typeof__(*(ptr)))__gu_val;			\
269 	__gu_err;						\
270 })
271 
272 #define __get_user_check(x, ptr, size)					\
273 ({									\
274 	long __gu_err = -EFAULT;					\
275 	__long_type(*(ptr)) __gu_val = 0;				\
276 	__typeof__(*(ptr)) __user *__gu_addr = (ptr);		\
277 	might_fault();							\
278 	if (access_ok(__gu_addr, (size))) {		\
279 		barrier_nospec();					\
280 		__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
281 	}								\
282 	(x) = (__force __typeof__(*(ptr)))__gu_val;				\
283 	__gu_err;							\
284 })
285 
286 #define __get_user_nosleep(x, ptr, size)			\
287 ({								\
288 	long __gu_err;						\
289 	__long_type(*(ptr)) __gu_val;				\
290 	__typeof__(*(ptr)) __user *__gu_addr = (ptr);	\
291 	__chk_user_ptr(ptr);					\
292 	barrier_nospec();					\
293 	__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
294 	(x) = (__force __typeof__(*(ptr)))__gu_val;			\
295 	__gu_err;						\
296 })
297 
298 
299 /* more complex routines */
300 
301 extern unsigned long __copy_tofrom_user(void __user *to,
302 		const void __user *from, unsigned long size);
303 
304 #ifdef __powerpc64__
305 static inline unsigned long
306 raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
307 {
308 	return __copy_tofrom_user(to, from, n);
309 }
310 #endif /* __powerpc64__ */
311 
312 static inline unsigned long raw_copy_from_user(void *to,
313 		const void __user *from, unsigned long n)
314 {
315 	if (__builtin_constant_p(n) && (n <= 8)) {
316 		unsigned long ret = 1;
317 
318 		switch (n) {
319 		case 1:
320 			barrier_nospec();
321 			__get_user_size(*(u8 *)to, from, 1, ret);
322 			break;
323 		case 2:
324 			barrier_nospec();
325 			__get_user_size(*(u16 *)to, from, 2, ret);
326 			break;
327 		case 4:
328 			barrier_nospec();
329 			__get_user_size(*(u32 *)to, from, 4, ret);
330 			break;
331 		case 8:
332 			barrier_nospec();
333 			__get_user_size(*(u64 *)to, from, 8, ret);
334 			break;
335 		}
336 		if (ret == 0)
337 			return 0;
338 	}
339 
340 	barrier_nospec();
341 	return __copy_tofrom_user((__force void __user *)to, from, n);
342 }
343 
344 static inline unsigned long raw_copy_to_user(void __user *to,
345 		const void *from, unsigned long n)
346 {
347 	if (__builtin_constant_p(n) && (n <= 8)) {
348 		unsigned long ret = 1;
349 
350 		switch (n) {
351 		case 1:
352 			__put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret);
353 			break;
354 		case 2:
355 			__put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret);
356 			break;
357 		case 4:
358 			__put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret);
359 			break;
360 		case 8:
361 			__put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret);
362 			break;
363 		}
364 		if (ret == 0)
365 			return 0;
366 	}
367 
368 	return __copy_tofrom_user(to, (__force const void __user *)from, n);
369 }
370 
371 extern unsigned long __clear_user(void __user *addr, unsigned long size);
372 
373 static inline unsigned long clear_user(void __user *addr, unsigned long size)
374 {
375 	might_fault();
376 	if (likely(access_ok(addr, size)))
377 		return __clear_user(addr, size);
378 	return size;
379 }
380 
381 extern long strncpy_from_user(char *dst, const char __user *src, long count);
382 extern __must_check long strnlen_user(const char __user *str, long n);
383 
384 extern long __copy_from_user_flushcache(void *dst, const void __user *src,
385 		unsigned size);
386 extern void memcpy_page_flushcache(char *to, struct page *page, size_t offset,
387 			   size_t len);
388 
389 #endif	/* _ARCH_POWERPC_UACCESS_H */
390