• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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), (void)(type),		\
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 #define __get_user_asm(x, addr, err, op)		\
178 	__asm__ __volatile__(				\
179 		"1:	"op" %1,0(%2)	# get_user\n"	\
180 		"2:\n"					\
181 		".section .fixup,\"ax\"\n"		\
182 		"3:	li %0,%3\n"			\
183 		"	li %1,0\n"			\
184 		"	b 2b\n"				\
185 		".previous\n"				\
186 		EX_TABLE(1b, 3b)			\
187 		: "=r" (err), "=r" (x)			\
188 		: "b" (addr), "i" (-EFAULT), "0" (err))
189 
190 #ifdef __powerpc64__
191 #define __get_user_asm2(x, addr, err)			\
192 	__get_user_asm(x, addr, err, "ld")
193 #else /* __powerpc64__ */
194 #define __get_user_asm2(x, addr, err)			\
195 	__asm__ __volatile__(				\
196 		"1:	lwz %1,0(%2)\n"			\
197 		"2:	lwz %1+1,4(%2)\n"		\
198 		"3:\n"					\
199 		".section .fixup,\"ax\"\n"		\
200 		"4:	li %0,%3\n"			\
201 		"	li %1,0\n"			\
202 		"	li %1+1,0\n"			\
203 		"	b 3b\n"				\
204 		".previous\n"				\
205 		EX_TABLE(1b, 4b)			\
206 		EX_TABLE(2b, 4b)			\
207 		: "=r" (err), "=&r" (x)			\
208 		: "b" (addr), "i" (-EFAULT), "0" (err))
209 #endif /* __powerpc64__ */
210 
211 #define __get_user_size(x, ptr, size, retval)			\
212 do {								\
213 	retval = 0;						\
214 	__chk_user_ptr(ptr);					\
215 	if (size > sizeof(x))					\
216 		(x) = __get_user_bad();				\
217 	switch (size) {						\
218 	case 1: __get_user_asm(x, ptr, retval, "lbz"); break;	\
219 	case 2: __get_user_asm(x, ptr, retval, "lhz"); break;	\
220 	case 4: __get_user_asm(x, ptr, retval, "lwz"); break;	\
221 	case 8: __get_user_asm2(x, ptr, retval);  break;	\
222 	default: (x) = __get_user_bad();			\
223 	}							\
224 } while (0)
225 
226 /*
227  * This is a type: either unsigned long, if the argument fits into
228  * that type, or otherwise unsigned long long.
229  */
230 #define __long_type(x) \
231 	__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
232 
233 #define __get_user_nocheck(x, ptr, size)			\
234 ({								\
235 	long __gu_err;						\
236 	__long_type(*(ptr)) __gu_val;				\
237 	__typeof__(*(ptr)) __user *__gu_addr = (ptr);	\
238 	__chk_user_ptr(ptr);					\
239 	if (!is_kernel_addr((unsigned long)__gu_addr))		\
240 		might_fault();					\
241 	barrier_nospec();					\
242 	__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
243 	(x) = (__typeof__(*(ptr)))__gu_val;			\
244 	__gu_err;						\
245 })
246 
247 #define __get_user_check(x, ptr, size)					\
248 ({									\
249 	long __gu_err = -EFAULT;					\
250 	__long_type(*(ptr)) __gu_val = 0;				\
251 	__typeof__(*(ptr)) __user *__gu_addr = (ptr);		\
252 	might_fault();							\
253 	if (access_ok(VERIFY_READ, __gu_addr, (size))) {		\
254 		barrier_nospec();					\
255 		__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
256 	}								\
257 	(x) = (__force __typeof__(*(ptr)))__gu_val;				\
258 	__gu_err;							\
259 })
260 
261 #define __get_user_nosleep(x, ptr, size)			\
262 ({								\
263 	long __gu_err;						\
264 	__long_type(*(ptr)) __gu_val;				\
265 	__typeof__(*(ptr)) __user *__gu_addr = (ptr);	\
266 	__chk_user_ptr(ptr);					\
267 	barrier_nospec();					\
268 	__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
269 	(x) = (__force __typeof__(*(ptr)))__gu_val;			\
270 	__gu_err;						\
271 })
272 
273 
274 /* more complex routines */
275 
276 extern unsigned long __copy_tofrom_user(void __user *to,
277 		const void __user *from, unsigned long size);
278 
279 #ifdef __powerpc64__
280 static inline unsigned long
raw_copy_in_user(void __user * to,const void __user * from,unsigned long n)281 raw_copy_in_user(void __user *to, const void __user *from, unsigned long n)
282 {
283 	barrier_nospec();
284 	return __copy_tofrom_user(to, from, n);
285 }
286 #endif /* __powerpc64__ */
287 
raw_copy_from_user(void * to,const void __user * from,unsigned long n)288 static inline unsigned long raw_copy_from_user(void *to,
289 		const void __user *from, unsigned long n)
290 {
291 	if (__builtin_constant_p(n) && (n <= 8)) {
292 		unsigned long ret = 1;
293 
294 		switch (n) {
295 		case 1:
296 			barrier_nospec();
297 			__get_user_size(*(u8 *)to, from, 1, ret);
298 			break;
299 		case 2:
300 			barrier_nospec();
301 			__get_user_size(*(u16 *)to, from, 2, ret);
302 			break;
303 		case 4:
304 			barrier_nospec();
305 			__get_user_size(*(u32 *)to, from, 4, ret);
306 			break;
307 		case 8:
308 			barrier_nospec();
309 			__get_user_size(*(u64 *)to, from, 8, ret);
310 			break;
311 		}
312 		if (ret == 0)
313 			return 0;
314 	}
315 
316 	barrier_nospec();
317 	return __copy_tofrom_user((__force void __user *)to, from, n);
318 }
319 
raw_copy_to_user(void __user * to,const void * from,unsigned long n)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 
clear_user(void __user * addr,unsigned long size)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 #endif	/* _ARCH_POWERPC_UACCESS_H */
361