1 #ifndef __SCORE_UACCESS_H
2 #define __SCORE_UACCESS_H
4 #include <linux/kernel.h>
5 #include <asm/extable.h>
7 #define get_ds() (KERNEL_DS)
8 #define get_fs() (current_thread_info()->addr_limit)
9 #define segment_eq(a, b) ((a).seg == (b).seg)
12 * Is a address valid? This does a straighforward calculation rather
16 * - "addr" doesn't have any high-bits set
17 * - AND "size" doesn't have any high-bits set
18 * - AND "addr+size" doesn't have any high-bits set
19 * - OR we are in kernel mode.
21 * __ua_size() is a trick to avoid runtime checking of positive constant
22 * sizes; for those we already know at compile time that the size is ok.
24 #define __ua_size(size) \
25 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
28 * access_ok: - Checks if a user space pointer is valid
29 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
30 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
31 * to write to a block, it is always safe to read from it.
32 * @addr: User space pointer to start of block to check
33 * @size: Size of block to check
35 * Context: User context only. This function may sleep if pagefaults are
38 * Checks if a pointer to a block of memory in user space is valid.
40 * Returns true (nonzero) if the memory block may be valid, false (zero)
41 * if it is definitely invalid.
43 * Note that, depending on architecture, this function probably just
44 * checks that the pointer is in the user space range - after calling
45 * this function, memory access functions may still return -EFAULT.
48 #define __access_ok(addr, size) \
49 (((long)((get_fs().seg) & \
50 ((addr) | ((addr) + (size)) | \
51 __ua_size(size)))) == 0)
53 #define access_ok(type, addr, size) \
54 likely(__access_ok((unsigned long)(addr), (size)))
57 * put_user: - Write a simple value into user space.
58 * @x: Value to copy to user space.
59 * @ptr: Destination address, in user space.
61 * Context: User context only. This function may sleep if pagefaults are
64 * This macro copies a single simple value from kernel space to user
65 * space. It supports simple types like char and int, but not larger
66 * data types like structures or arrays.
68 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
69 * to the result of dereferencing @ptr.
71 * Returns zero on success, or -EFAULT on error.
73 #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
76 * get_user: - Get a simple variable from user space.
77 * @x: Variable to store result.
78 * @ptr: Source address, in user space.
80 * Context: User context only. This function may sleep if pagefaults are
83 * This macro copies a single simple variable from user space to kernel
84 * space. It supports simple types like char and int, but not larger
85 * data types like structures or arrays.
87 * @ptr must have pointer-to-simple-variable type, and the result of
88 * dereferencing @ptr must be assignable to @x without a cast.
90 * Returns zero on success, or -EFAULT on error.
91 * On error, the variable @x is set to zero.
93 #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
96 * __put_user: - Write a simple value into user space, with less checking.
97 * @x: Value to copy to user space.
98 * @ptr: Destination address, in user space.
100 * Context: User context only. This function may sleep if pagefaults are
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.
107 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
108 * to the result of dereferencing @ptr.
110 * Caller must check the pointer with access_ok() before calling this
113 * Returns zero on success, or -EFAULT on error.
115 #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
118 * __get_user: - Get a simple variable from user space, with less checking.
119 * @x: Variable to store result.
120 * @ptr: Source address, in user space.
122 * Context: User context only. This function may sleep if pagefaults are
125 * This macro copies a single simple variable from user space to kernel
126 * space. It supports simple types like char and int, but not larger
127 * data types like structures or arrays.
129 * @ptr must have pointer-to-simple-variable type, and the result of
130 * dereferencing @ptr must be assignable to @x without a cast.
132 * Caller must check the pointer with access_ok() before calling this
135 * Returns zero on success, or -EFAULT on error.
136 * On error, the variable @x is set to zero.
138 #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
140 struct __large_struct { unsigned long buf[100]; };
141 #define __m(x) (*(struct __large_struct __user *)(x))
144 * Yuck. We need two variants, one for 64bit operation and one
145 * for 32 bit mode and old iron.
147 extern void __get_user_unknown(void);
149 #define __get_user_common(val, size, ptr) \
153 __get_user_asm(val, "lb", ptr); \
156 __get_user_asm(val, "lh", ptr); \
159 __get_user_asm(val, "lw", ptr); \
162 if (__copy_from_user((void *)&val, ptr, 8) == 0) \
165 __gu_err = -EFAULT; \
168 __get_user_unknown(); \
173 #define __get_user_nocheck(x, ptr, size) \
176 __get_user_common((x), size, ptr); \
180 #define __get_user_check(x, ptr, size) \
182 long __gu_err = -EFAULT; \
183 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
185 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
186 __get_user_common((x), size, __gu_ptr); \
193 #define __get_user_asm(val, insn, addr) \
197 __asm__ __volatile__( \
198 "1:" insn " %1, %3\n" \
200 ".section .fixup,\"ax\"\n" \
205 ".section __ex_table,\"a\"\n" \
208 : "=r" (__gu_err), "=r" (__gu_tmp) \
209 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
211 (val) = (__typeof__(*(addr))) __gu_tmp; \
215 * Yuck. We need two variants, one for 64bit operation and one
216 * for 32 bit mode and old iron.
218 #define __put_user_nocheck(val, ptr, size) \
220 __typeof__(*(ptr)) __pu_val; \
226 __put_user_asm("sb", ptr); \
229 __put_user_asm("sh", ptr); \
232 __put_user_asm("sw", ptr); \
235 if ((__copy_to_user((void *)ptr, &__pu_val, 8)) == 0) \
238 __pu_err = -EFAULT; \
241 __put_user_unknown(); \
248 #define __put_user_check(val, ptr, size) \
250 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
251 __typeof__(*(ptr)) __pu_val = (val); \
252 long __pu_err = -EFAULT; \
254 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
257 __put_user_asm("sb", __pu_addr); \
260 __put_user_asm("sh", __pu_addr); \
263 __put_user_asm("sw", __pu_addr); \
266 if ((__copy_to_user((void *)__pu_addr, &__pu_val, 8)) == 0)\
269 __pu_err = -EFAULT; \
272 __put_user_unknown(); \
279 #define __put_user_asm(insn, ptr) \
280 __asm__ __volatile__( \
281 "1:" insn " %2, %3\n" \
283 ".section .fixup,\"ax\"\n" \
287 ".section __ex_table,\"a\"\n" \
291 : "0" (0), "r" (__pu_val), "o" (__m(ptr)), \
294 extern void __put_user_unknown(void);
295 extern int __copy_tofrom_user(void *to, const void *from, unsigned long len);
297 static inline unsigned long
298 copy_from_user(void *to, const void *from, unsigned long len)
300 unsigned long res = len;
302 if (likely(access_ok(VERIFY_READ, from, len)))
303 res = __copy_tofrom_user(to, from, len);
306 memset(to + (len - res), 0, res);
311 static inline unsigned long
312 copy_to_user(void *to, const void *from, unsigned long len)
314 if (likely(access_ok(VERIFY_WRITE, to, len)))
315 len = __copy_tofrom_user(to, from, len);
320 static inline unsigned long
321 __copy_from_user(void *to, const void *from, unsigned long len)
323 unsigned long left = __copy_tofrom_user(to, from, len);
325 memset(to + (len - left), 0, left);
329 #define __copy_to_user(to, from, len) \
330 __copy_tofrom_user((to), (from), (len))
332 static inline unsigned long
333 __copy_to_user_inatomic(void *to, const void *from, unsigned long len)
335 return __copy_to_user(to, from, len);
338 static inline unsigned long
339 __copy_from_user_inatomic(void *to, const void *from, unsigned long len)
341 return __copy_tofrom_user(to, from, len);
344 #define __copy_in_user(to, from, len) __copy_tofrom_user(to, from, len)
346 static inline unsigned long
347 copy_in_user(void *to, const void *from, unsigned long len)
349 if (access_ok(VERIFY_READ, from, len) &&
350 access_ok(VERIFY_WRITE, to, len))
351 return __copy_tofrom_user(to, from, len);
355 * __clear_user: - Zero a block of memory in user space, with less checking.
356 * @to: Destination address, in user space.
357 * @n: Number of bytes to zero.
359 * Zero a block of memory in user space. Caller must check
360 * the specified block with access_ok() before calling this function.
362 * Returns number of bytes that could not be cleared.
363 * On success, this will be zero.
365 extern unsigned long __clear_user(void __user *src, unsigned long size);
367 static inline unsigned long clear_user(char *src, unsigned long size)
369 if (access_ok(VERIFY_WRITE, src, size))
370 return __clear_user(src, size);
375 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
376 * @dst: Destination address, in kernel space. This buffer must be at
377 * least @count bytes long.
378 * @src: Source address, in user space.
379 * @count: Maximum number of bytes to copy, including the trailing NUL.
381 * Copies a NUL-terminated string from userspace to kernel space.
382 * Caller must check the specified block with access_ok() before calling
385 * On success, returns the length of the string (not including the trailing
388 * If access to userspace fails, returns -EFAULT (some data may have been
391 * If @count is smaller than the length of the string, copies @count bytes
392 * and returns @count.
394 extern int __strncpy_from_user(char *dst, const char *src, long len);
396 static inline int strncpy_from_user(char *dst, const char *src, long len)
398 if (access_ok(VERIFY_READ, src, 1))
399 return __strncpy_from_user(dst, src, len);
404 extern int __strlen_user(const char *src);
405 static inline long strlen_user(const char __user *src)
407 return __strlen_user(src);
410 extern int __strnlen_user(const char *str, long len);
411 static inline long strnlen_user(const char __user *str, long len)
413 if (!access_ok(VERIFY_READ, str, 0))
416 return __strnlen_user(str, len);
419 #endif /* __SCORE_UACCESS_H */