2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
9 /* thread_info has a mm_segment_t in it, so put the definition up here */
14 #include <linux/thread_info.h>
15 #include <linux/errno.h>
16 #include <asm/processor.h>
20 #define VERIFY_WRITE 1
23 * The fs value determines whether argument validity checking should be
24 * performed or not. If get_fs() == USER_DS, checking is performed, with
25 * get_fs() == KERNEL_DS, checking is bypassed.
27 * For historical reasons, these macros are grossly misnamed.
30 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
32 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
33 #define USER_DS MAKE_MM_SEG(TASK_SIZE)
35 #define get_ds() (KERNEL_DS)
36 #define get_fs() (current_thread_info()->addr_limit)
37 #define set_fs(x) (current_thread_info()->addr_limit = (x))
39 #define segment_eq(a, b) ((a).seg == (b).seg)
41 #define __under_task_size(addr, size) \
42 (((unsigned long) (addr) < TASK_SIZE) && \
43 (((unsigned long) (addr) + (size)) < TASK_SIZE))
45 #define __access_ok_vsyscall(type, addr, size) \
46 ((type == VERIFY_READ) && \
47 ((unsigned long) (addr) >= FIXADDR_USER_START) && \
48 ((unsigned long) (addr) + (size) <= FIXADDR_USER_END) && \
49 ((unsigned long) (addr) + (size) >= (unsigned long)(addr)))
51 #define __addr_range_nowrap(addr, size) \
52 ((unsigned long) (addr) <= ((unsigned long) (addr) + (size)))
54 #define access_ok(type, addr, size) \
55 (__addr_range_nowrap(addr, size) && \
56 (__under_task_size(addr, size) || \
57 __access_ok_vsyscall(type, addr, size) || \
58 segment_eq(get_fs(), KERNEL_DS)))
60 extern int copy_from_user(void *to
, const void __user
*from
, int n
);
61 extern int copy_to_user(void __user
*to
, const void *from
, int n
);
64 * strncpy_from_user: - Copy a NUL terminated string from userspace.
65 * @dst: Destination address, in kernel space. This buffer must be at
66 * least @count bytes long.
67 * @src: Source address, in user space.
68 * @count: Maximum number of bytes to copy, including the trailing NUL.
70 * Copies a NUL-terminated string from userspace to kernel space.
72 * On success, returns the length of the string (not including the trailing
75 * If access to userspace fails, returns -EFAULT (some data may have been
78 * If @count is smaller than the length of the string, copies @count bytes
82 extern int strncpy_from_user(char *dst
, const char __user
*src
, int count
);
85 * __clear_user: - Zero a block of memory in user space, with less checking.
86 * @to: Destination address, in user space.
87 * @n: Number of bytes to zero.
89 * Zero a block of memory in user space. Caller must check
90 * the specified block with access_ok() before calling this function.
92 * Returns number of bytes that could not be cleared.
93 * On success, this will be zero.
95 extern int __clear_user(void __user
*mem
, int len
);
98 * clear_user: - Zero a block of memory in user space.
99 * @to: Destination address, in user space.
100 * @n: Number of bytes to zero.
102 * Zero a block of memory in user space.
104 * Returns number of bytes that could not be cleared.
105 * On success, this will be zero.
107 extern int clear_user(void __user
*mem
, int len
);
110 * strlen_user: - Get the size of a string in user space.
111 * @str: The string to measure.
112 * @n: The maximum valid length
114 * Get the size of a NUL-terminated string in user space.
116 * Returns the size of the string INCLUDING the terminating NUL.
117 * On exception, returns 0.
118 * If the string is too long, returns a value greater than @n.
120 extern int strnlen_user(const void __user
*str
, int len
);
122 #define __copy_from_user(to, from, n) copy_from_user(to, from, n)
124 #define __copy_to_user(to, from, n) copy_to_user(to, from, n)
126 #define __copy_to_user_inatomic __copy_to_user
127 #define __copy_from_user_inatomic __copy_from_user
129 #define __get_user(x, ptr) \
131 const __typeof__(*(ptr)) __user *__private_ptr = (ptr); \
132 __typeof__(x) __private_val; \
133 int __private_ret = -EFAULT; \
134 (x) = (__typeof__(*(__private_ptr)))0; \
135 if (__copy_from_user((__force void *)&__private_val, (__private_ptr),\
136 sizeof(*(__private_ptr))) == 0) { \
137 (x) = (__typeof__(*(__private_ptr))) __private_val; \
143 #define get_user(x, ptr) \
145 const __typeof__((*(ptr))) __user *private_ptr = (ptr); \
146 (access_ok(VERIFY_READ, private_ptr, sizeof(*private_ptr)) ? \
147 __get_user(x, private_ptr) : ((x) = (__typeof__(*ptr))0, -EFAULT)); \
150 #define __put_user(x, ptr) \
152 __typeof__(*(ptr)) __user *__private_ptr = ptr; \
153 __typeof__(*(__private_ptr)) __private_val; \
154 int __private_ret = -EFAULT; \
155 __private_val = (__typeof__(*(__private_ptr))) (x); \
156 if (__copy_to_user((__private_ptr), &__private_val, \
157 sizeof(*(__private_ptr))) == 0) { \
163 #define put_user(x, ptr) \
165 __typeof__(*(ptr)) __user *private_ptr = (ptr); \
166 (access_ok(VERIFY_WRITE, private_ptr, sizeof(*private_ptr)) ? \
167 __put_user(x, private_ptr) : -EFAULT); \
170 #define strlen_user(str) strnlen_user(str, ~0U >> 1)
172 struct exception_table_entry