xref: /linux/arch/microblaze/include/asm/uaccess.h (revision 3a0e75adecc8da026a5befb2c5828d08c999373c)
1 /*
2  * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
3  * Copyright (C) 2008-2009 PetaLogix
4  * Copyright (C) 2006 Atmark Techno, Inc.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License. See the file "COPYING" in the main directory of this archive
8  * for more details.
9  */
10 
11 #ifndef _ASM_MICROBLAZE_UACCESS_H
12 #define _ASM_MICROBLAZE_UACCESS_H
13 
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 
17 #include <asm/mmu.h>
18 #include <asm/page.h>
19 #include <asm/pgtable.h>
20 #include <linux/string.h>
21 
22 /*
23  * On Microblaze the fs value is actually the top of the corresponding
24  * address space.
25  *
26  * The fs value determines whether argument validity checking should be
27  * performed or not. If get_fs() == USER_DS, checking is performed, with
28  * get_fs() == KERNEL_DS, checking is bypassed.
29  *
30  * For historical reasons, these macros are grossly misnamed.
31  *
32  * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal.
33  */
34 # define MAKE_MM_SEG(s)       ((mm_segment_t) { (s) })
35 
36 #  ifndef CONFIG_MMU
37 #  define KERNEL_DS	MAKE_MM_SEG(0)
38 #  define USER_DS	KERNEL_DS
39 #  else
40 #  define KERNEL_DS	MAKE_MM_SEG(0xFFFFFFFF)
41 #  define USER_DS	MAKE_MM_SEG(TASK_SIZE - 1)
42 #  endif
43 
44 # define get_ds()	(KERNEL_DS)
45 # define get_fs()	(current_thread_info()->addr_limit)
46 # define set_fs(val)	(current_thread_info()->addr_limit = (val))
47 
48 # define segment_eq(a, b)	((a).seg == (b).seg)
49 
50 /*
51  * The exception table consists of pairs of addresses: the first is the
52  * address of an instruction that is allowed to fault, and the second is
53  * the address at which the program should continue. No registers are
54  * modified, so it is entirely up to the continuation code to figure out
55  * what to do.
56  *
57  * All the routines below use bits of fixup code that are out of line
58  * with the main instruction path. This means when everything is well,
59  * we don't even have to jump over them. Further, they do not intrude
60  * on our cache or tlb entries.
61  */
62 struct exception_table_entry {
63 	unsigned long insn, fixup;
64 };
65 
66 #ifndef CONFIG_MMU
67 
68 /* Check against bounds of physical memory */
69 static inline int ___range_ok(unsigned long addr, unsigned long size)
70 {
71 	return ((addr < memory_start) ||
72 		((addr + size - 1) > (memory_start + memory_size - 1)));
73 }
74 
75 #define __range_ok(addr, size) \
76 		___range_ok((unsigned long)(addr), (unsigned long)(size))
77 
78 #define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
79 
80 #else
81 
82 static inline int access_ok(int type, const void __user *addr,
83 							unsigned long size)
84 {
85 	if (!size)
86 		goto ok;
87 
88 	if ((get_fs().seg < ((unsigned long)addr)) ||
89 			(get_fs().seg < ((unsigned long)addr + size - 1))) {
90 		pr_devel("ACCESS fail: %s at 0x%08x (size 0x%x), seg 0x%08x\n",
91 			type ? "WRITE" : "READ ", (__force u32)addr, (u32)size,
92 			(u32)get_fs().seg);
93 		return 0;
94 	}
95 ok:
96 	pr_devel("ACCESS OK: %s at 0x%08x (size 0x%x), seg 0x%08x\n",
97 			type ? "WRITE" : "READ ", (__force u32)addr, (u32)size,
98 			(u32)get_fs().seg);
99 	return 1;
100 }
101 #endif
102 
103 #ifdef CONFIG_MMU
104 # define __FIXUP_SECTION	".section .fixup,\"ax\"\n"
105 # define __EX_TABLE_SECTION	".section __ex_table,\"a\"\n"
106 #else
107 # define __FIXUP_SECTION	".section .discard,\"ax\"\n"
108 # define __EX_TABLE_SECTION	".section .discard,\"ax\"\n"
109 #endif
110 
111 extern unsigned long __copy_tofrom_user(void __user *to,
112 		const void __user *from, unsigned long size);
113 
114 /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */
115 static inline unsigned long __must_check __clear_user(void __user *to,
116 							unsigned long n)
117 {
118 	/* normal memset with two words to __ex_table */
119 	__asm__ __volatile__ (				\
120 			"1:	sb	r0, %1, r0;"	\
121 			"	addik	%0, %0, -1;"	\
122 			"	bneid	%0, 1b;"	\
123 			"	addik	%1, %1, 1;"	\
124 			"2:			"	\
125 			__EX_TABLE_SECTION		\
126 			".word	1b,2b;"			\
127 			".previous;"			\
128 		: "=r"(n), "=r"(to)			\
129 		: "0"(n), "1"(to)
130 	);
131 	return n;
132 }
133 
134 static inline unsigned long __must_check clear_user(void __user *to,
135 							unsigned long n)
136 {
137 	might_fault();
138 	if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
139 		return n;
140 
141 	return __clear_user(to, n);
142 }
143 
144 /* put_user and get_user macros */
145 extern long __user_bad(void);
146 
147 #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err)	\
148 ({								\
149 	__asm__ __volatile__ (					\
150 			"1:"	insn	" %1, %2, r0;"		\
151 			"	addk	%0, r0, r0;"		\
152 			"2:			"		\
153 			__FIXUP_SECTION				\
154 			"3:	brid	2b;"			\
155 			"	addik	%0, r0, %3;"		\
156 			".previous;"				\
157 			__EX_TABLE_SECTION			\
158 			".word	1b,3b;"				\
159 			".previous;"				\
160 		: "=&r"(__gu_err), "=r"(__gu_val)		\
161 		: "r"(__gu_ptr), "i"(-EFAULT)			\
162 	);							\
163 })
164 
165 /**
166  * get_user: - Get a simple variable from user space.
167  * @x:   Variable to store result.
168  * @ptr: Source address, in user space.
169  *
170  * Context: User context only. This function may sleep if pagefaults are
171  *          enabled.
172  *
173  * This macro copies a single simple variable from user space to kernel
174  * space.  It supports simple types like char and int, but not larger
175  * data types like structures or arrays.
176  *
177  * @ptr must have pointer-to-simple-variable type, and the result of
178  * dereferencing @ptr must be assignable to @x without a cast.
179  *
180  * Returns zero on success, or -EFAULT on error.
181  * On error, the variable @x is set to zero.
182  */
183 #define get_user(x, ptr)						\
184 	__get_user_check((x), (ptr), sizeof(*(ptr)))
185 
186 #define __get_user_check(x, ptr, size)					\
187 ({									\
188 	unsigned long __gu_val = 0;					\
189 	const typeof(*(ptr)) __user *__gu_addr = (ptr);			\
190 	int __gu_err = 0;						\
191 									\
192 	if (access_ok(VERIFY_READ, __gu_addr, size)) {			\
193 		switch (size) {						\
194 		case 1:							\
195 			__get_user_asm("lbu", __gu_addr, __gu_val,	\
196 				       __gu_err);			\
197 			break;						\
198 		case 2:							\
199 			__get_user_asm("lhu", __gu_addr, __gu_val,	\
200 				       __gu_err);			\
201 			break;						\
202 		case 4:							\
203 			__get_user_asm("lw", __gu_addr, __gu_val,	\
204 				       __gu_err);			\
205 			break;						\
206 		default:						\
207 			__gu_err = __user_bad();			\
208 			break;						\
209 		}							\
210 	} else {							\
211 		__gu_err = -EFAULT;					\
212 	}								\
213 	x = (__force typeof(*(ptr)))__gu_val;				\
214 	__gu_err;							\
215 })
216 
217 #define __get_user(x, ptr)						\
218 ({									\
219 	unsigned long __gu_val = 0;					\
220 	/*unsigned long __gu_ptr = (unsigned long)(ptr);*/		\
221 	long __gu_err;							\
222 	switch (sizeof(*(ptr))) {					\
223 	case 1:								\
224 		__get_user_asm("lbu", (ptr), __gu_val, __gu_err);	\
225 		break;							\
226 	case 2:								\
227 		__get_user_asm("lhu", (ptr), __gu_val, __gu_err);	\
228 		break;							\
229 	case 4:								\
230 		__get_user_asm("lw", (ptr), __gu_val, __gu_err);	\
231 		break;							\
232 	default:							\
233 		/* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
234 	}								\
235 	x = (__force __typeof__(*(ptr))) __gu_val;			\
236 	__gu_err;							\
237 })
238 
239 
240 #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err)	\
241 ({								\
242 	__asm__ __volatile__ (					\
243 			"1:"	insn	" %1, %2, r0;"		\
244 			"	addk	%0, r0, r0;"		\
245 			"2:			"		\
246 			__FIXUP_SECTION				\
247 			"3:	brid	2b;"			\
248 			"	addik	%0, r0, %3;"		\
249 			".previous;"				\
250 			__EX_TABLE_SECTION			\
251 			".word	1b,3b;"				\
252 			".previous;"				\
253 		: "=&r"(__gu_err)				\
254 		: "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT)	\
255 	);							\
256 })
257 
258 #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err)		\
259 ({								\
260 	__asm__ __volatile__ ("	lwi	%0, %1, 0;"		\
261 			"1:	swi	%0, %2, 0;"		\
262 			"	lwi	%0, %1, 4;"		\
263 			"2:	swi	%0, %2, 4;"		\
264 			"	addk	%0, r0, r0;"		\
265 			"3:			"		\
266 			__FIXUP_SECTION				\
267 			"4:	brid	3b;"			\
268 			"	addik	%0, r0, %3;"		\
269 			".previous;"				\
270 			__EX_TABLE_SECTION			\
271 			".word	1b,4b,2b,4b;"			\
272 			".previous;"				\
273 		: "=&r"(__gu_err)				\
274 		: "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT)	\
275 		);						\
276 })
277 
278 /**
279  * put_user: - Write a simple value into user space.
280  * @x:   Value to copy to user space.
281  * @ptr: Destination address, in user space.
282  *
283  * Context: User context only. This function may sleep if pagefaults are
284  *          enabled.
285  *
286  * This macro copies a single simple value from kernel space to user
287  * space.  It supports simple types like char and int, but not larger
288  * data types like structures or arrays.
289  *
290  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
291  * to the result of dereferencing @ptr.
292  *
293  * Returns zero on success, or -EFAULT on error.
294  */
295 #define put_user(x, ptr)						\
296 	__put_user_check((x), (ptr), sizeof(*(ptr)))
297 
298 #define __put_user_check(x, ptr, size)					\
299 ({									\
300 	typeof(*(ptr)) volatile __pu_val = x;				\
301 	typeof(*(ptr)) __user *__pu_addr = (ptr);			\
302 	int __pu_err = 0;						\
303 									\
304 	if (access_ok(VERIFY_WRITE, __pu_addr, size)) {			\
305 		switch (size) {						\
306 		case 1:							\
307 			__put_user_asm("sb", __pu_addr, __pu_val,	\
308 				       __pu_err);			\
309 			break;						\
310 		case 2:							\
311 			__put_user_asm("sh", __pu_addr, __pu_val,	\
312 				       __pu_err);			\
313 			break;						\
314 		case 4:							\
315 			__put_user_asm("sw", __pu_addr, __pu_val,	\
316 				       __pu_err);			\
317 			break;						\
318 		case 8:							\
319 			__put_user_asm_8(__pu_addr, __pu_val, __pu_err);\
320 			break;						\
321 		default:						\
322 			__pu_err = __user_bad();			\
323 			break;						\
324 		}							\
325 	} else {							\
326 		__pu_err = -EFAULT;					\
327 	}								\
328 	__pu_err;							\
329 })
330 
331 #define __put_user(x, ptr)						\
332 ({									\
333 	__typeof__(*(ptr)) volatile __gu_val = (x);			\
334 	long __gu_err = 0;						\
335 	switch (sizeof(__gu_val)) {					\
336 	case 1:								\
337 		__put_user_asm("sb", (ptr), __gu_val, __gu_err);	\
338 		break;							\
339 	case 2:								\
340 		__put_user_asm("sh", (ptr), __gu_val, __gu_err);	\
341 		break;							\
342 	case 4:								\
343 		__put_user_asm("sw", (ptr), __gu_val, __gu_err);	\
344 		break;							\
345 	case 8:								\
346 		__put_user_asm_8((ptr), __gu_val, __gu_err);		\
347 		break;							\
348 	default:							\
349 		/*__gu_err = -EINVAL;*/	__gu_err = __user_bad();	\
350 	}								\
351 	__gu_err;							\
352 })
353 
354 
355 /* copy_to_from_user */
356 #define __copy_from_user(to, from, n)	\
357 	__copy_tofrom_user((__force void __user *)(to), \
358 				(void __user *)(from), (n))
359 #define __copy_from_user_inatomic(to, from, n) \
360 		__copy_from_user((to), (from), (n))
361 
362 static inline long copy_from_user(void *to,
363 		const void __user *from, unsigned long n)
364 {
365 	unsigned long res = n;
366 	might_fault();
367 	if (likely(access_ok(VERIFY_READ, from, n)))
368 		res = __copy_from_user(to, from, n);
369 	if (unlikely(res))
370 		memset(to + (n - res), 0, res);
371 	return res;
372 }
373 
374 #define __copy_to_user(to, from, n)	\
375 		__copy_tofrom_user((void __user *)(to), \
376 			(__force const void __user *)(from), (n))
377 #define __copy_to_user_inatomic(to, from, n) __copy_to_user((to), (from), (n))
378 
379 static inline long copy_to_user(void __user *to,
380 		const void *from, unsigned long n)
381 {
382 	might_fault();
383 	if (access_ok(VERIFY_WRITE, to, n))
384 		return __copy_to_user(to, from, n);
385 	return n;
386 }
387 
388 /*
389  * Copy a null terminated string from userspace.
390  */
391 extern int __strncpy_user(char *to, const char __user *from, int len);
392 
393 #define __strncpy_from_user	__strncpy_user
394 
395 static inline long
396 strncpy_from_user(char *dst, const char __user *src, long count)
397 {
398 	if (!access_ok(VERIFY_READ, src, 1))
399 		return -EFAULT;
400 	return __strncpy_from_user(dst, src, count);
401 }
402 
403 /*
404  * Return the size of a string (including the ending 0)
405  *
406  * Return 0 on exception, a value greater than N if too long
407  */
408 extern int __strnlen_user(const char __user *sstr, int len);
409 
410 static inline long strnlen_user(const char __user *src, long n)
411 {
412 	if (!access_ok(VERIFY_READ, src, 1))
413 		return 0;
414 	return __strnlen_user(src, n);
415 }
416 
417 #endif /* _ASM_MICROBLAZE_UACCESS_H */
418