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>
9 #define get_ds() (KERNEL_DS)
10 #define get_fs() (current_thread_info()->addr_limit)
11 #define segment_eq(a, b) ((a).seg == (b).seg)
14 * Is a address valid? This does a straighforward calculation rather
18 * - "addr" doesn't have any high-bits set
19 * - AND "size" doesn't have any high-bits set
20 * - AND "addr+size" doesn't have any high-bits set
21 * - OR we are in kernel mode.
23 * __ua_size() is a trick to avoid runtime checking of positive constant
24 * sizes; for those we already know at compile time that the size is ok.
26 #define __ua_size(size) \
27 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
30 * access_ok: - Checks if a user space pointer is valid
31 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
32 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
33 * to write to a block, it is always safe to read from it.
34 * @addr: User space pointer to start of block to check
35 * @size: Size of block to check
37 * Context: User context only. This function may sleep if pagefaults are
40 * Checks if a pointer to a block of memory in user space is valid.
42 * Returns true (nonzero) if the memory block may be valid, false (zero)
43 * if it is definitely invalid.
45 * Note that, depending on architecture, this function probably just
46 * checks that the pointer is in the user space range - after calling
47 * this function, memory access functions may still return -EFAULT.
50 #define __access_ok(addr, size) \
51 (((long)((get_fs().seg) & \
52 ((addr) | ((addr) + (size)) | \
53 __ua_size(size)))) == 0)
55 #define access_ok(type, addr, size) \
56 likely(__access_ok((unsigned long)(addr), (size)))
59 * put_user: - Write a simple value into user space.
60 * @x: Value to copy to user space.
61 * @ptr: Destination address, in user space.
63 * Context: User context only. This function may sleep if pagefaults are
66 * This macro copies a single simple value from kernel space to user
67 * space. It supports simple types like char and int, but not larger
68 * data types like structures or arrays.
70 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
71 * to the result of dereferencing @ptr.
73 * Returns zero on success, or -EFAULT on error.
75 #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
78 * get_user: - Get a simple variable from user space.
79 * @x: Variable to store result.
80 * @ptr: Source address, in user space.
82 * Context: User context only. This function may sleep if pagefaults are
85 * This macro copies a single simple variable from user space to kernel
86 * space. It supports simple types like char and int, but not larger
87 * data types like structures or arrays.
89 * @ptr must have pointer-to-simple-variable type, and the result of
90 * dereferencing @ptr must be assignable to @x without a cast.
92 * Returns zero on success, or -EFAULT on error.
93 * On error, the variable @x is set to zero.
95 #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
98 * __put_user: - Write a simple value into user space, with less checking.
99 * @x: Value to copy to user space.
100 * @ptr: Destination address, in user space.
102 * Context: User context only. This function may sleep if pagefaults are
105 * This macro copies a single simple value from kernel space to user
106 * space. It supports simple types like char and int, but not larger
107 * data types like structures or arrays.
109 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
110 * to the result of dereferencing @ptr.
112 * Caller must check the pointer with access_ok() before calling this
115 * Returns zero on success, or -EFAULT on error.
117 #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
120 * __get_user: - Get a simple variable from user space, with less checking.
121 * @x: Variable to store result.
122 * @ptr: Source address, in user space.
124 * Context: User context only. This function may sleep if pagefaults are
127 * This macro copies a single simple variable from user space to kernel
128 * space. It supports simple types like char and int, but not larger
129 * data types like structures or arrays.
131 * @ptr must have pointer-to-simple-variable type, and the result of
132 * dereferencing @ptr must be assignable to @x without a cast.
134 * Caller must check the pointer with access_ok() before calling this
137 * Returns zero on success, or -EFAULT on error.
138 * On error, the variable @x is set to zero.
140 #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
142 struct __large_struct { unsigned long buf[100]; };
143 #define __m(x) (*(struct __large_struct __user *)(x))
146 * Yuck. We need two variants, one for 64bit operation and one
147 * for 32 bit mode and old iron.
149 extern void __get_user_unknown(void);
151 #define __get_user_common(val, size, ptr) \
155 __get_user_asm(val, "lb", ptr); \
158 __get_user_asm(val, "lh", ptr); \
161 __get_user_asm(val, "lw", ptr); \
164 if (__copy_from_user((void *)&val, ptr, 8) == 0) \
167 __gu_err = -EFAULT; \
170 __get_user_unknown(); \
175 #define __get_user_nocheck(x, ptr, size) \
178 __get_user_common((x), size, ptr); \
182 #define __get_user_check(x, ptr, size) \
184 long __gu_err = -EFAULT; \
185 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
187 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
188 __get_user_common((x), size, __gu_ptr); \
195 #define __get_user_asm(val, insn, addr) \
199 __asm__ __volatile__( \
200 "1:" insn " %1, %3\n" \
202 ".section .fixup,\"ax\"\n" \
207 ".section __ex_table,\"a\"\n" \
210 : "=r" (__gu_err), "=r" (__gu_tmp) \
211 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
213 (val) = (__typeof__(*(addr))) __gu_tmp; \
217 * Yuck. We need two variants, one for 64bit operation and one
218 * for 32 bit mode and old iron.
220 #define __put_user_nocheck(val, ptr, size) \
222 __typeof__(*(ptr)) __pu_val; \
228 __put_user_asm("sb", ptr); \
231 __put_user_asm("sh", ptr); \
234 __put_user_asm("sw", ptr); \
237 if ((__copy_to_user((void *)ptr, &__pu_val, 8)) == 0) \
240 __pu_err = -EFAULT; \
243 __put_user_unknown(); \
250 #define __put_user_check(val, ptr, size) \
252 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
253 __typeof__(*(ptr)) __pu_val = (val); \
254 long __pu_err = -EFAULT; \
256 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
259 __put_user_asm("sb", __pu_addr); \
262 __put_user_asm("sh", __pu_addr); \
265 __put_user_asm("sw", __pu_addr); \
268 if ((__copy_to_user((void *)__pu_addr, &__pu_val, 8)) == 0)\
271 __pu_err = -EFAULT; \
274 __put_user_unknown(); \
281 #define __put_user_asm(insn, ptr) \
282 __asm__ __volatile__( \
283 "1:" insn " %2, %3\n" \
285 ".section .fixup,\"ax\"\n" \
289 ".section __ex_table,\"a\"\n" \
293 : "0" (0), "r" (__pu_val), "o" (__m(ptr)), \
296 extern void __put_user_unknown(void);
297 extern int __copy_tofrom_user(void *to, const void *from, unsigned long len);
299 static inline unsigned long
300 copy_from_user(void *to, const void *from, unsigned long len)
302 unsigned long res = len;
304 if (likely(access_ok(VERIFY_READ, from, len)))
305 res = __copy_tofrom_user(to, from, len);
308 memset(to + (len - res), 0, res);
313 static inline unsigned long
314 copy_to_user(void *to, const void *from, unsigned long len)
316 if (likely(access_ok(VERIFY_WRITE, to, len)))
317 len = __copy_tofrom_user(to, from, len);
322 static inline unsigned long
323 __copy_from_user(void *to, const void *from, unsigned long len)
325 unsigned long left = __copy_tofrom_user(to, from, len);
327 memset(to + (len - left), 0, left);
331 #define __copy_to_user(to, from, len) \
332 __copy_tofrom_user((to), (from), (len))
334 static inline unsigned long
335 __copy_to_user_inatomic(void *to, const void *from, unsigned long len)
337 return __copy_to_user(to, from, len);
340 static inline unsigned long
341 __copy_from_user_inatomic(void *to, const void *from, unsigned long len)
343 return __copy_tofrom_user(to, from, len);
346 #define __copy_in_user(to, from, len) __copy_tofrom_user(to, from, len)
348 static inline unsigned long
349 copy_in_user(void *to, const void *from, unsigned long len)
351 if (access_ok(VERIFY_READ, from, len) &&
352 access_ok(VERFITY_WRITE, to, len))
353 return __copy_tofrom_user(to, from, len);
357 * __clear_user: - Zero a block of memory in user space, with less checking.
358 * @to: Destination address, in user space.
359 * @n: Number of bytes to zero.
361 * Zero a block of memory in user space. Caller must check
362 * the specified block with access_ok() before calling this function.
364 * Returns number of bytes that could not be cleared.
365 * On success, this will be zero.
367 extern unsigned long __clear_user(void __user *src, unsigned long size);
369 static inline unsigned long clear_user(char *src, unsigned long size)
371 if (access_ok(VERIFY_WRITE, src, size))
372 return __clear_user(src, size);
377 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
378 * @dst: Destination address, in kernel space. This buffer must be at
379 * least @count bytes long.
380 * @src: Source address, in user space.
381 * @count: Maximum number of bytes to copy, including the trailing NUL.
383 * Copies a NUL-terminated string from userspace to kernel space.
384 * Caller must check the specified block with access_ok() before calling
387 * On success, returns the length of the string (not including the trailing
390 * If access to userspace fails, returns -EFAULT (some data may have been
393 * If @count is smaller than the length of the string, copies @count bytes
394 * and returns @count.
396 extern int __strncpy_from_user(char *dst, const char *src, long len);
398 static inline int strncpy_from_user(char *dst, const char *src, long len)
400 if (access_ok(VERIFY_READ, src, 1))
401 return __strncpy_from_user(dst, src, len);
406 extern int __strlen_user(const char *src);
407 static inline long strlen_user(const char __user *src)
409 return __strlen_user(src);
412 extern int __strnlen_user(const char *str, long len);
413 static inline long strnlen_user(const char __user *str, long len)
415 if (!access_ok(VERIFY_READ, str, 0))
418 return __strnlen_user(str, len);
421 #endif /* __SCORE_UACCESS_H */