1 #ifndef __SCORE_UACCESS_H
2 #define __SCORE_UACCESS_H
4 #include <linux/kernel.h>
5 #include <linux/errno.h>
6 #include <linux/thread_info.h>
7 #include <asm/extable.h>
10 #define VERIFY_WRITE 1
12 #define get_ds() (KERNEL_DS)
13 #define get_fs() (current_thread_info()->addr_limit)
14 #define segment_eq(a, b) ((a).seg == (b).seg)
17 * Is a address valid? This does a straighforward calculation rather
21 * - "addr" doesn't have any high-bits set
22 * - AND "size" doesn't have any high-bits set
23 * - AND "addr+size" doesn't have any high-bits set
24 * - OR we are in kernel mode.
26 * __ua_size() is a trick to avoid runtime checking of positive constant
27 * sizes; for those we already know at compile time that the size is ok.
29 #define __ua_size(size) \
30 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
33 * access_ok: - Checks if a user space pointer is valid
34 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
35 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
36 * to write to a block, it is always safe to read from it.
37 * @addr: User space pointer to start of block to check
38 * @size: Size of block to check
40 * Context: User context only. This function may sleep if pagefaults are
43 * Checks if a pointer to a block of memory in user space is valid.
45 * Returns true (nonzero) if the memory block may be valid, false (zero)
46 * if it is definitely invalid.
48 * Note that, depending on architecture, this function probably just
49 * checks that the pointer is in the user space range - after calling
50 * this function, memory access functions may still return -EFAULT.
53 #define __access_ok(addr, size) \
54 (((long)((get_fs().seg) & \
55 ((addr) | ((addr) + (size)) | \
56 __ua_size(size)))) == 0)
58 #define access_ok(type, addr, size) \
59 likely(__access_ok((unsigned long)(addr), (size)))
62 * put_user: - Write a simple value into user space.
63 * @x: Value to copy to user space.
64 * @ptr: Destination address, in user space.
66 * Context: User context only. This function may sleep if pagefaults are
69 * This macro copies a single simple value from kernel space to user
70 * space. It supports simple types like char and int, but not larger
71 * data types like structures or arrays.
73 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
74 * to the result of dereferencing @ptr.
76 * Returns zero on success, or -EFAULT on error.
78 #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
81 * get_user: - Get a simple variable from user space.
82 * @x: Variable to store result.
83 * @ptr: Source address, in user space.
85 * Context: User context only. This function may sleep if pagefaults are
88 * This macro copies a single simple variable from user space to kernel
89 * space. It supports simple types like char and int, but not larger
90 * data types like structures or arrays.
92 * @ptr must have pointer-to-simple-variable type, and the result of
93 * dereferencing @ptr must be assignable to @x without a cast.
95 * Returns zero on success, or -EFAULT on error.
96 * On error, the variable @x is set to zero.
98 #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
101 * __put_user: - Write a simple value into user space, with less checking.
102 * @x: Value to copy to user space.
103 * @ptr: Destination address, in user space.
105 * Context: User context only. This function may sleep if pagefaults are
108 * This macro copies a single simple value from kernel space to user
109 * space. It supports simple types like char and int, but not larger
110 * data types like structures or arrays.
112 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
113 * to the result of dereferencing @ptr.
115 * Caller must check the pointer with access_ok() before calling this
118 * Returns zero on success, or -EFAULT on error.
120 #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
123 * __get_user: - Get a simple variable from user space, with less checking.
124 * @x: Variable to store result.
125 * @ptr: Source address, in user space.
127 * Context: User context only. This function may sleep if pagefaults are
130 * This macro copies a single simple variable from user space to kernel
131 * space. It supports simple types like char and int, but not larger
132 * data types like structures or arrays.
134 * @ptr must have pointer-to-simple-variable type, and the result of
135 * dereferencing @ptr must be assignable to @x without a cast.
137 * Caller must check the pointer with access_ok() before calling this
140 * Returns zero on success, or -EFAULT on error.
141 * On error, the variable @x is set to zero.
143 #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
145 struct __large_struct
{ unsigned long buf
[100]; };
146 #define __m(x) (*(struct __large_struct __user *)(x))
149 * Yuck. We need two variants, one for 64bit operation and one
150 * for 32 bit mode and old iron.
152 extern void __get_user_unknown(void);
154 #define __get_user_common(val, size, ptr) \
158 __get_user_asm(val, "lb", ptr); \
161 __get_user_asm(val, "lh", ptr); \
164 __get_user_asm(val, "lw", ptr); \
167 if (__copy_from_user((void *)&val, ptr, 8) == 0) \
170 __gu_err = -EFAULT; \
173 __get_user_unknown(); \
178 #define __get_user_nocheck(x, ptr, size) \
181 __get_user_common((x), size, ptr); \
185 #define __get_user_check(x, ptr, size) \
187 long __gu_err = -EFAULT; \
188 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
190 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
191 __get_user_common((x), size, __gu_ptr); \
198 #define __get_user_asm(val, insn, addr) \
202 __asm__ __volatile__( \
203 "1:" insn " %1, %3\n" \
205 ".section .fixup,\"ax\"\n" \
210 ".section __ex_table,\"a\"\n" \
213 : "=r" (__gu_err), "=r" (__gu_tmp) \
214 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
216 (val) = (__typeof__(*(addr))) __gu_tmp; \
220 * Yuck. We need two variants, one for 64bit operation and one
221 * for 32 bit mode and old iron.
223 #define __put_user_nocheck(val, ptr, size) \
225 __typeof__(*(ptr)) __pu_val; \
231 __put_user_asm("sb", ptr); \
234 __put_user_asm("sh", ptr); \
237 __put_user_asm("sw", ptr); \
240 if ((__copy_to_user((void *)ptr, &__pu_val, 8)) == 0) \
243 __pu_err = -EFAULT; \
246 __put_user_unknown(); \
253 #define __put_user_check(val, ptr, size) \
255 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
256 __typeof__(*(ptr)) __pu_val = (val); \
257 long __pu_err = -EFAULT; \
259 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
262 __put_user_asm("sb", __pu_addr); \
265 __put_user_asm("sh", __pu_addr); \
268 __put_user_asm("sw", __pu_addr); \
271 if ((__copy_to_user((void *)__pu_addr, &__pu_val, 8)) == 0)\
274 __pu_err = -EFAULT; \
277 __put_user_unknown(); \
284 #define __put_user_asm(insn, ptr) \
285 __asm__ __volatile__( \
286 "1:" insn " %2, %3\n" \
288 ".section .fixup,\"ax\"\n" \
292 ".section __ex_table,\"a\"\n" \
296 : "0" (0), "r" (__pu_val), "o" (__m(ptr)), \
299 extern void __put_user_unknown(void);
300 extern int __copy_tofrom_user(void *to
, const void *from
, unsigned long len
);
302 static inline unsigned long
303 copy_from_user(void *to
, const void *from
, unsigned long len
)
305 unsigned long res
= len
;
307 if (likely(access_ok(VERIFY_READ
, from
, len
)))
308 res
= __copy_tofrom_user(to
, from
, len
);
311 memset(to
+ (len
- res
), 0, res
);
316 static inline unsigned long
317 copy_to_user(void *to
, const void *from
, unsigned long len
)
319 if (likely(access_ok(VERIFY_WRITE
, to
, len
)))
320 len
= __copy_tofrom_user(to
, from
, len
);
325 static inline unsigned long
326 __copy_from_user(void *to
, const void *from
, unsigned long len
)
328 unsigned long left
= __copy_tofrom_user(to
, from
, len
);
330 memset(to
+ (len
- left
), 0, left
);
334 #define __copy_to_user(to, from, len) \
335 __copy_tofrom_user((to), (from), (len))
337 static inline unsigned long
338 __copy_to_user_inatomic(void *to
, const void *from
, unsigned long len
)
340 return __copy_to_user(to
, from
, len
);
343 static inline unsigned long
344 __copy_from_user_inatomic(void *to
, const void *from
, unsigned long len
)
346 return __copy_tofrom_user(to
, from
, len
);
349 #define __copy_in_user(to, from, len) __copy_tofrom_user(to, from, len)
351 static inline unsigned long
352 copy_in_user(void *to
, const void *from
, unsigned long len
)
354 if (access_ok(VERIFY_READ
, from
, len
) &&
355 access_ok(VERFITY_WRITE
, to
, len
))
356 return __copy_tofrom_user(to
, from
, len
);
360 * __clear_user: - Zero a block of memory in user space, with less checking.
361 * @to: Destination address, in user space.
362 * @n: Number of bytes to zero.
364 * Zero a block of memory in user space. Caller must check
365 * the specified block with access_ok() before calling this function.
367 * Returns number of bytes that could not be cleared.
368 * On success, this will be zero.
370 extern unsigned long __clear_user(void __user
*src
, unsigned long size
);
372 static inline unsigned long clear_user(char *src
, unsigned long size
)
374 if (access_ok(VERIFY_WRITE
, src
, size
))
375 return __clear_user(src
, size
);
380 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
381 * @dst: Destination address, in kernel space. This buffer must be at
382 * least @count bytes long.
383 * @src: Source address, in user space.
384 * @count: Maximum number of bytes to copy, including the trailing NUL.
386 * Copies a NUL-terminated string from userspace to kernel space.
387 * Caller must check the specified block with access_ok() before calling
390 * On success, returns the length of the string (not including the trailing
393 * If access to userspace fails, returns -EFAULT (some data may have been
396 * If @count is smaller than the length of the string, copies @count bytes
397 * and returns @count.
399 extern int __strncpy_from_user(char *dst
, const char *src
, long len
);
401 static inline int strncpy_from_user(char *dst
, const char *src
, long len
)
403 if (access_ok(VERIFY_READ
, src
, 1))
404 return __strncpy_from_user(dst
, src
, len
);
409 extern int __strlen_user(const char *src
);
410 static inline long strlen_user(const char __user
*src
)
412 return __strlen_user(src
);
415 extern int __strnlen_user(const char *str
, long len
);
416 static inline long strnlen_user(const char __user
*str
, long len
)
418 if (!access_ok(VERIFY_READ
, str
, 0))
421 return __strnlen_user(str
, len
);
424 #endif /* __SCORE_UACCESS_H */