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