]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - include/asm-generic/uaccess.h
1 #ifndef __ASM_GENERIC_UACCESS_H
2 #define __ASM_GENERIC_UACCESS_H
5 * User space memory access functions, these should work
6 * on any machine that has kernel and user data in the same
7 * address space, e.g. all NOMMU machines.
9 #include <linux/string.h>
11 #include <asm/segment.h>
13 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
16 #define KERNEL_DS MAKE_MM_SEG(~0UL)
20 #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
24 #define get_ds() (KERNEL_DS)
25 #define get_fs() (current_thread_info()->addr_limit)
27 static inline void set_fs(mm_segment_t fs
)
29 current_thread_info()->addr_limit
= fs
;
34 #define segment_eq(a, b) ((a).seg == (b).seg)
37 #define access_ok(type, addr, size) __access_ok((unsigned long)(addr),(size))
40 * The architecture should really override this if possible, at least
41 * doing a check on the get_fs()
44 static inline int __access_ok(unsigned long addr
, unsigned long size
)
51 * These are the main single-value transfer routines. They automatically
52 * use the right size if we just have the right pointer type.
53 * This version just falls back to copy_{from,to}_user, which should
54 * provide a fast-path for small values.
56 #define __put_user(x, ptr) \
58 __typeof__(*(ptr)) __x = (x); \
59 int __pu_err = -EFAULT; \
60 __chk_user_ptr(ptr); \
61 switch (sizeof (*(ptr))) { \
66 __pu_err = __put_user_fn(sizeof (*(ptr)), \
76 #define put_user(x, ptr) \
80 access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ? \
81 __put_user((x), ((__typeof__(*(ptr)) *)__p)) : \
87 static inline int __put_user_fn(size_t size
, void __user
*ptr
, void *x
)
89 #ifdef CONFIG_ARCH_HAS_RAW_COPY_USER
90 return unlikely(raw_copy_to_user(ptr
, x
, size
)) ? -EFAULT
: 0;
92 return unlikely(__copy_to_user(ptr
, x
, size
)) ? -EFAULT
: 0;
96 #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
100 extern int __put_user_bad(void) __attribute__((noreturn
));
102 #define __get_user(x, ptr) \
104 int __gu_err = -EFAULT; \
105 __chk_user_ptr(ptr); \
106 switch (sizeof(*(ptr))) { \
108 unsigned char __x = 0; \
109 __gu_err = __get_user_fn(sizeof (*(ptr)), \
111 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
115 unsigned short __x = 0; \
116 __gu_err = __get_user_fn(sizeof (*(ptr)), \
118 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
122 unsigned int __x = 0; \
123 __gu_err = __get_user_fn(sizeof (*(ptr)), \
125 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
129 unsigned long long __x = 0; \
130 __gu_err = __get_user_fn(sizeof (*(ptr)), \
132 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
142 #define get_user(x, ptr) \
144 const void *__p = (ptr); \
146 access_ok(VERIFY_READ, __p, sizeof(*ptr)) ? \
147 __get_user((x), (__typeof__(*(ptr)) *)__p) : \
148 ((x) = (__typeof__(*(ptr)))0,-EFAULT); \
151 #ifndef __get_user_fn
152 static inline int __get_user_fn(size_t size
, const void __user
*ptr
, void *x
)
154 #ifdef CONFIG_ARCH_HAS_RAW_COPY_USER
155 return unlikely(raw_copy_from_user(x
, ptr
, size
)) ? -EFAULT
: 0;
157 return unlikely(__copy_from_user(x
, ptr
, size
)) ? -EFAULT
: 0;
161 #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
165 extern int __get_user_bad(void) __attribute__((noreturn
));
167 #ifndef CONFIG_ARCH_HAS_RAW_COPY_USER
169 #ifndef __copy_from_user_inatomic
170 #define __copy_from_user_inatomic __copy_from_user
173 #ifndef __copy_to_user_inatomic
174 #define __copy_to_user_inatomic __copy_to_user
177 static inline long copy_from_user(void *to
,
178 const void __user
* from
, unsigned long n
)
180 unsigned long res
= n
;
182 if (likely(access_ok(VERIFY_READ
, from
, n
)))
183 res
= __copy_from_user(to
, from
, n
);
185 memset(to
+ (n
- res
), 0, res
);
189 static inline long copy_to_user(void __user
*to
,
190 const void *from
, unsigned long n
)
193 if (access_ok(VERIFY_WRITE
, to
, n
))
194 return __copy_to_user(to
, from
, n
);
201 * Copy a null terminated string from userspace.
203 #ifndef __strncpy_from_user
205 __strncpy_from_user(char *dst
, const char __user
*src
, long count
)
208 strncpy(dst
, (const char __force
*)src
, count
);
209 for (tmp
= dst
; *tmp
&& count
> 0; tmp
++, count
--)
216 strncpy_from_user(char *dst
, const char __user
*src
, long count
)
218 if (!access_ok(VERIFY_READ
, src
, 1))
220 return __strncpy_from_user(dst
, src
, count
);
224 * Return the size of a string (including the ending 0)
226 * Return 0 on exception, a value greater than N if too long
228 #ifndef __strnlen_user
229 #define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
233 * Unlike strnlen, strnlen_user includes the nul terminator in
234 * its returned count. Callers should check for a returned value
235 * greater than N as an indication the string is too long.
237 static inline long strnlen_user(const char __user
*src
, long n
)
239 if (!access_ok(VERIFY_READ
, src
, 1))
241 return __strnlen_user(src
, n
);
244 static inline long strlen_user(const char __user
*src
)
246 return strnlen_user(src
, 32767);
253 static inline __must_check
unsigned long
254 __clear_user(void __user
*to
, unsigned long n
)
256 memset((void __force
*)to
, 0, n
);
261 static inline __must_check
unsigned long
262 clear_user(void __user
*to
, unsigned long n
)
265 if (!access_ok(VERIFY_WRITE
, to
, n
))
268 return __clear_user(to
, n
);
271 #include <asm/extable.h>
273 #endif /* __ASM_GENERIC_UACCESS_H */