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