2 * Copyright (C) 2004-2006 Atmel Corporation
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.
8 #ifndef __ASM_AVR32_UACCESS_H
9 #define __ASM_AVR32_UACCESS_H
11 #include <linux/errno.h>
12 #include <linux/sched.h>
15 #define VERIFY_WRITE 1
18 unsigned int is_user_space
;
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.
26 * For historical reasons (Data Segment Register?), these macros are misnamed.
28 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
29 #define segment_eq(a, b) ((a).is_user_space == (b).is_user_space)
31 #define USER_ADDR_LIMIT 0x80000000
33 #define KERNEL_DS MAKE_MM_SEG(0)
34 #define USER_DS MAKE_MM_SEG(1)
36 #define get_ds() (KERNEL_DS)
38 static inline mm_segment_t
get_fs(void)
40 return MAKE_MM_SEG(test_thread_flag(TIF_USERSPACE
));
43 static inline void set_fs(mm_segment_t s
)
46 set_thread_flag(TIF_USERSPACE
);
48 clear_thread_flag(TIF_USERSPACE
);
52 * Test whether a block of memory is a valid user space address.
53 * Returns 0 if the range is valid, nonzero otherwise.
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)?
60 * If yes on the first check, access is granted.
61 * If no on any of the others, access is denied.
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)))
69 #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
71 /* Generic arbitrary sized copy. Return the number of bytes NOT copied */
72 extern __kernel_size_t
__copy_user(void *to
, const void *from
,
75 extern __kernel_size_t
copy_to_user(void __user
*to
, const void *from
,
77 extern __kernel_size_t
___copy_from_user(void *to
, const void __user
*from
,
80 static inline __kernel_size_t
__copy_to_user(void __user
*to
, const void *from
,
83 return __copy_user((void __force
*)to
, from
, n
);
85 static inline __kernel_size_t
__copy_from_user(void *to
,
86 const void __user
*from
,
89 return __copy_user(to
, (const void __force
*)from
, n
);
91 static inline __kernel_size_t
copy_from_user(void *to
,
92 const void __user
*from
,
95 size_t res
= ___copy_from_user(to
, from
, n
);
97 memset(to
+ (n
- res
), 0, res
);
101 #define __copy_to_user_inatomic __copy_to_user
102 #define __copy_from_user_inatomic __copy_from_user
105 * put_user: - Write a simple value into user space.
106 * @x: Value to copy to user space.
107 * @ptr: Destination address, in user space.
109 * Context: User context only. This function may sleep if pagefaults are
112 * This macro copies a single simple value from kernel space to user
113 * space. It supports simple types like char and int, but not larger
114 * data types like structures or arrays.
116 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
117 * to the result of dereferencing @ptr.
119 * Returns zero on success, or -EFAULT on error.
121 #define put_user(x, ptr) \
122 __put_user_check((x), (ptr), sizeof(*(ptr)))
125 * get_user: - Get a simple variable from user space.
126 * @x: Variable to store result.
127 * @ptr: Source address, in user space.
129 * Context: User context only. This function may sleep if pagefaults are
132 * This macro copies a single simple variable from user space to kernel
133 * space. It supports simple types like char and int, but not larger
134 * data types like structures or arrays.
136 * @ptr must have pointer-to-simple-variable type, and the result of
137 * dereferencing @ptr must be assignable to @x without a cast.
139 * Returns zero on success, or -EFAULT on error.
140 * On error, the variable @x is set to zero.
142 #define get_user(x, ptr) \
143 __get_user_check((x), (ptr), sizeof(*(ptr)))
146 * __put_user: - Write a simple value into user space, with less checking.
147 * @x: Value to copy to user space.
148 * @ptr: Destination address, in user space.
150 * Context: User context only. This function may sleep if pagefaults are
153 * This macro copies a single simple value from kernel space to user
154 * space. It supports simple types like char and int, but not larger
155 * data types like structures or arrays.
157 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
158 * to the result of dereferencing @ptr.
160 * Caller must check the pointer with access_ok() before calling this
163 * Returns zero on success, or -EFAULT on error.
165 #define __put_user(x, ptr) \
166 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
169 * __get_user: - Get a simple variable from user space, with less checking.
170 * @x: Variable to store result.
171 * @ptr: Source address, in user space.
173 * Context: User context only. This function may sleep if pagefaults are
176 * This macro copies a single simple variable from user space to kernel
177 * space. It supports simple types like char and int, but not larger
178 * data types like structures or arrays.
180 * @ptr must have pointer-to-simple-variable type, and the result of
181 * dereferencing @ptr must be assignable to @x without a cast.
183 * Caller must check the pointer with access_ok() before calling this
186 * Returns zero on success, or -EFAULT on error.
187 * On error, the variable @x is set to zero.
189 #define __get_user(x, ptr) \
190 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
192 extern int __get_user_bad(void);
193 extern int __put_user_bad(void);
195 #define __get_user_nocheck(x, ptr, size) \
197 unsigned long __gu_val = 0; \
201 case 1: __get_user_asm("ub", __gu_val, ptr, __gu_err); break; \
202 case 2: __get_user_asm("uh", __gu_val, ptr, __gu_err); break; \
203 case 4: __get_user_asm("w", __gu_val, ptr, __gu_err); break; \
204 default: __gu_err = __get_user_bad(); break; \
207 x = (__force typeof(*(ptr)))__gu_val; \
211 #define __get_user_check(x, ptr, size) \
213 unsigned long __gu_val = 0; \
214 const typeof(*(ptr)) __user * __gu_addr = (ptr); \
217 if (access_ok(VERIFY_READ, __gu_addr, size)) { \
220 __get_user_asm("ub", __gu_val, __gu_addr, \
224 __get_user_asm("uh", __gu_val, __gu_addr, \
228 __get_user_asm("w", __gu_val, __gu_addr, \
232 __gu_err = __get_user_bad(); \
236 __gu_err = -EFAULT; \
238 x = (__force typeof(*(ptr)))__gu_val; \
242 #define __get_user_asm(suffix, __gu_val, ptr, __gu_err) \
244 "1: ld." suffix " %1, %3 \n" \
246 " .subsection 1 \n" \
249 " .subsection 0 \n" \
250 " .section __ex_table, \"a\" \n" \
253 : "=r"(__gu_err), "=r"(__gu_val) \
254 : "0"(__gu_err), "m"(*(ptr)), "i"(-EFAULT))
256 #define __put_user_nocheck(x, ptr, size) \
258 typeof(*(ptr)) __pu_val; \
263 case 1: __put_user_asm("b", ptr, __pu_val, __pu_err); break; \
264 case 2: __put_user_asm("h", ptr, __pu_val, __pu_err); break; \
265 case 4: __put_user_asm("w", ptr, __pu_val, __pu_err); break; \
266 case 8: __put_user_asm("d", ptr, __pu_val, __pu_err); break; \
267 default: __pu_err = __put_user_bad(); break; \
272 #define __put_user_check(x, ptr, size) \
274 typeof(*(ptr)) __pu_val; \
275 typeof(*(ptr)) __user *__pu_addr = (ptr); \
279 if (access_ok(VERIFY_WRITE, __pu_addr, size)) { \
282 __put_user_asm("b", __pu_addr, __pu_val, \
286 __put_user_asm("h", __pu_addr, __pu_val, \
290 __put_user_asm("w", __pu_addr, __pu_val, \
294 __put_user_asm("d", __pu_addr, __pu_val, \
298 __pu_err = __put_user_bad(); \
302 __pu_err = -EFAULT; \
307 #define __put_user_asm(suffix, ptr, __pu_val, __gu_err) \
309 "1: st." suffix " %1, %3 \n" \
311 " .subsection 1 \n" \
314 " .subsection 0 \n" \
315 " .section __ex_table, \"a\" \n" \
318 : "=r"(__gu_err), "=m"(*(ptr)) \
319 : "0"(__gu_err), "r"(__pu_val), "i"(-EFAULT))
321 extern __kernel_size_t
clear_user(void __user
*addr
, __kernel_size_t size
);
322 extern __kernel_size_t
__clear_user(void __user
*addr
, __kernel_size_t size
);
324 extern long strncpy_from_user(char *dst
, const char __user
*src
, long count
);
325 extern long __strncpy_from_user(char *dst
, const char __user
*src
, long count
);
327 extern long strnlen_user(const char __user
*__s
, long __n
);
328 extern long __strnlen_user(const char __user
*__s
, long __n
);
330 #define strlen_user(s) strnlen_user(s, ~0UL >> 1)
332 struct exception_table_entry
334 unsigned long insn
, fixup
;
337 #endif /* __ASM_AVR32_UACCESS_H */