1/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#ifndef __ASM_AVR32_UACCESS_H
9#define __ASM_AVR32_UACCESS_H
10
11#include <linux/errno.h>
12#include <linux/sched.h>
13
14#define VERIFY_READ	0
15#define VERIFY_WRITE	1
16
17typedef struct {
18	unsigned int is_user_space;
19} mm_segment_t;
20
21/*
22 * The fs value determines whether argument validity checking should be
23 * performed or not.  If get_fs() == USER_DS, checking is performed, with
24 * get_fs() == KERNEL_DS, checking is bypassed.
25 *
26 * For historical reasons (Data Segment Register?), these macros are misnamed.
27 */
28#define MAKE_MM_SEG(s)	((mm_segment_t) { (s) })
29#define segment_eq(a, b)	((a).is_user_space == (b).is_user_space)
30
31#define USER_ADDR_LIMIT 0x80000000
32
33#define KERNEL_DS	MAKE_MM_SEG(0)
34#define USER_DS		MAKE_MM_SEG(1)
35
36#define get_ds()	(KERNEL_DS)
37
38static inline mm_segment_t get_fs(void)
39{
40	return MAKE_MM_SEG(test_thread_flag(TIF_USERSPACE));
41}
42
43static inline void set_fs(mm_segment_t s)
44{
45	if (s.is_user_space)
46		set_thread_flag(TIF_USERSPACE);
47	else
48		clear_thread_flag(TIF_USERSPACE);
49}
50
51/*
52 * Test whether a block of memory is a valid user space address.
53 * Returns 0 if the range is valid, nonzero otherwise.
54 *
55 * We do the following checks:
56 *   1. Is the access from kernel space?
57 *   2. Does (addr + size) set the carry bit?
58 *   3. Is (addr + size) a negative number (i.e. >= 0x80000000)?
59 *
60 * If yes on the first check, access is granted.
61 * If no on any of the others, access is denied.
62 */
63#define __range_ok(addr, size)						\
64	(test_thread_flag(TIF_USERSPACE)				\
65	 && (((unsigned long)(addr) >= 0x80000000)			\
66	     || ((unsigned long)(size) > 0x80000000)			\
67	     || (((unsigned long)(addr) + (unsigned long)(size)) > 0x80000000)))
68
69#define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
70
71/* Generic arbitrary sized copy. Return the number of bytes NOT copied */
72extern __kernel_size_t __copy_user(void *to, const void *from,
73				   __kernel_size_t n);
74
75extern __kernel_size_t copy_to_user(void __user *to, const void *from,
76				    __kernel_size_t n);
77extern __kernel_size_t copy_from_user(void *to, const void __user *from,
78				      __kernel_size_t n);
79
80static inline __kernel_size_t __copy_to_user(void __user *to, const void *from,
81					     __kernel_size_t n)
82{
83	return __copy_user((void __force *)to, from, n);
84}
85static inline __kernel_size_t __copy_from_user(void *to,
86					       const void __user *from,
87					       __kernel_size_t n)
88{
89	return __copy_user(to, (const void __force *)from, n);
90}
91
92#define __copy_to_user_inatomic __copy_to_user
93#define __copy_from_user_inatomic __copy_from_user
94
95/*
96 * put_user: - Write a simple value into user space.
97 * @x:   Value to copy to user space.
98 * @ptr: Destination address, in user space.
99 *
100 * Context: User context only. This function may sleep if pagefaults are
101 *          enabled.
102 *
103 * This macro copies a single simple value from kernel space to user
104 * space.  It supports simple types like char and int, but not larger
105 * data types like structures or arrays.
106 *
107 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
108 * to the result of dereferencing @ptr.
109 *
110 * Returns zero on success, or -EFAULT on error.
111 */
112#define put_user(x, ptr)	\
113	__put_user_check((x), (ptr), sizeof(*(ptr)))
114
115/*
116 * get_user: - Get a simple variable from user space.
117 * @x:   Variable to store result.
118 * @ptr: Source address, in user space.
119 *
120 * Context: User context only. This function may sleep if pagefaults are
121 *          enabled.
122 *
123 * This macro copies a single simple variable from user space to kernel
124 * space.  It supports simple types like char and int, but not larger
125 * data types like structures or arrays.
126 *
127 * @ptr must have pointer-to-simple-variable type, and the result of
128 * dereferencing @ptr must be assignable to @x without a cast.
129 *
130 * Returns zero on success, or -EFAULT on error.
131 * On error, the variable @x is set to zero.
132 */
133#define get_user(x, ptr) \
134	__get_user_check((x), (ptr), sizeof(*(ptr)))
135
136/*
137 * __put_user: - Write a simple value into user space, with less checking.
138 * @x:   Value to copy to user space.
139 * @ptr: Destination address, in user space.
140 *
141 * Context: User context only. This function may sleep if pagefaults are
142 *          enabled.
143 *
144 * This macro copies a single simple value from kernel space to user
145 * space.  It supports simple types like char and int, but not larger
146 * data types like structures or arrays.
147 *
148 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
149 * to the result of dereferencing @ptr.
150 *
151 * Caller must check the pointer with access_ok() before calling this
152 * function.
153 *
154 * Returns zero on success, or -EFAULT on error.
155 */
156#define __put_user(x, ptr) \
157	__put_user_nocheck((x), (ptr), sizeof(*(ptr)))
158
159/*
160 * __get_user: - Get a simple variable from user space, with less checking.
161 * @x:   Variable to store result.
162 * @ptr: Source address, in user space.
163 *
164 * Context: User context only. This function may sleep if pagefaults are
165 *          enabled.
166 *
167 * This macro copies a single simple variable from user space to kernel
168 * space.  It supports simple types like char and int, but not larger
169 * data types like structures or arrays.
170 *
171 * @ptr must have pointer-to-simple-variable type, and the result of
172 * dereferencing @ptr must be assignable to @x without a cast.
173 *
174 * Caller must check the pointer with access_ok() before calling this
175 * function.
176 *
177 * Returns zero on success, or -EFAULT on error.
178 * On error, the variable @x is set to zero.
179 */
180#define __get_user(x, ptr) \
181	__get_user_nocheck((x), (ptr), sizeof(*(ptr)))
182
183extern int __get_user_bad(void);
184extern int __put_user_bad(void);
185
186#define __get_user_nocheck(x, ptr, size)				\
187({									\
188	unsigned long __gu_val = 0;					\
189	int __gu_err = 0;						\
190									\
191	switch (size) {							\
192	case 1: __get_user_asm("ub", __gu_val, ptr, __gu_err); break;	\
193	case 2: __get_user_asm("uh", __gu_val, ptr, __gu_err); break;	\
194	case 4: __get_user_asm("w", __gu_val, ptr, __gu_err); break;	\
195	default: __gu_err = __get_user_bad(); break;			\
196	}								\
197									\
198	x = (__force typeof(*(ptr)))__gu_val;				\
199	__gu_err;							\
200})
201
202#define __get_user_check(x, ptr, size)					\
203({									\
204	unsigned long __gu_val = 0;					\
205	const typeof(*(ptr)) __user * __gu_addr = (ptr);		\
206	int __gu_err = 0;						\
207									\
208	if (access_ok(VERIFY_READ, __gu_addr, size)) {			\
209		switch (size) {						\
210		case 1:							\
211			__get_user_asm("ub", __gu_val, __gu_addr,	\
212				       __gu_err);			\
213			break;						\
214		case 2:							\
215			__get_user_asm("uh", __gu_val, __gu_addr,	\
216				       __gu_err);			\
217			break;						\
218		case 4:							\
219			__get_user_asm("w", __gu_val, __gu_addr,	\
220				       __gu_err);			\
221			break;						\
222		default:						\
223			__gu_err = __get_user_bad();			\
224			break;						\
225		}							\
226	} else {							\
227		__gu_err = -EFAULT;					\
228	}								\
229	x = (__force typeof(*(ptr)))__gu_val;				\
230	__gu_err;							\
231})
232
233#define __get_user_asm(suffix, __gu_val, ptr, __gu_err)			\
234	asm volatile(							\
235		"1:	ld." suffix "	%1, %3			\n"	\
236		"2:						\n"	\
237		"	.subsection 1				\n"	\
238		"3:	mov	%0, %4				\n"	\
239		"	rjmp	2b				\n"	\
240		"	.subsection 0				\n"	\
241		"	.section __ex_table, \"a\"		\n"	\
242		"	.long	1b, 3b				\n"	\
243		"	.previous				\n"	\
244		: "=r"(__gu_err), "=r"(__gu_val)			\
245		: "0"(__gu_err), "m"(*(ptr)), "i"(-EFAULT))
246
247#define __put_user_nocheck(x, ptr, size)				\
248({									\
249	typeof(*(ptr)) __pu_val;					\
250	int __pu_err = 0;						\
251									\
252	__pu_val = (x);							\
253	switch (size) {							\
254	case 1: __put_user_asm("b", ptr, __pu_val, __pu_err); break;	\
255	case 2: __put_user_asm("h", ptr, __pu_val, __pu_err); break;	\
256	case 4: __put_user_asm("w", ptr, __pu_val, __pu_err); break;	\
257	case 8: __put_user_asm("d", ptr, __pu_val, __pu_err); break;	\
258	default: __pu_err = __put_user_bad(); break;			\
259	}								\
260	__pu_err;							\
261})
262
263#define __put_user_check(x, ptr, size)					\
264({									\
265	typeof(*(ptr)) __pu_val;					\
266	typeof(*(ptr)) __user *__pu_addr = (ptr);			\
267	int __pu_err = 0;						\
268									\
269	__pu_val = (x);							\
270	if (access_ok(VERIFY_WRITE, __pu_addr, size)) {			\
271		switch (size) {						\
272		case 1:							\
273			__put_user_asm("b", __pu_addr, __pu_val,	\
274				       __pu_err);			\
275			break;						\
276		case 2:							\
277			__put_user_asm("h", __pu_addr, __pu_val,	\
278				       __pu_err);			\
279			break;						\
280		case 4:							\
281			__put_user_asm("w", __pu_addr, __pu_val,	\
282				       __pu_err);			\
283			break;						\
284		case 8:							\
285			__put_user_asm("d", __pu_addr, __pu_val,	\
286				       __pu_err);			\
287			break;						\
288		default:						\
289			__pu_err = __put_user_bad();			\
290			break;						\
291		}							\
292	} else {							\
293		__pu_err = -EFAULT;					\
294	}								\
295	__pu_err;							\
296})
297
298#define __put_user_asm(suffix, ptr, __pu_val, __gu_err)			\
299	asm volatile(							\
300		"1:	st." suffix "	%1, %3			\n"	\
301		"2:						\n"	\
302		"	.subsection 1				\n"	\
303		"3:	mov	%0, %4				\n"	\
304		"	rjmp	2b				\n"	\
305		"	.subsection 0				\n"	\
306		"	.section __ex_table, \"a\"		\n"	\
307		"	.long	1b, 3b				\n"	\
308		"	.previous				\n"	\
309		: "=r"(__gu_err), "=m"(*(ptr))				\
310		: "0"(__gu_err), "r"(__pu_val), "i"(-EFAULT))
311
312extern __kernel_size_t clear_user(void __user *addr, __kernel_size_t size);
313extern __kernel_size_t __clear_user(void __user *addr, __kernel_size_t size);
314
315extern long strncpy_from_user(char *dst, const char __user *src, long count);
316extern long __strncpy_from_user(char *dst, const char __user *src, long count);
317
318extern long strnlen_user(const char __user *__s, long __n);
319extern long __strnlen_user(const char __user *__s, long __n);
320
321#define strlen_user(s) strnlen_user(s, ~0UL >> 1)
322
323struct exception_table_entry
324{
325	unsigned long insn, fixup;
326};
327
328#endif /* __ASM_AVR32_UACCESS_H */
329