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