]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - include/asm-generic/uaccess.h
generic ...copy_..._user primitives
[mirror_ubuntu-artful-kernel.git] / include / asm-generic / uaccess.h
1 #ifndef __ASM_GENERIC_UACCESS_H
2 #define __ASM_GENERIC_UACCESS_H
3
4 /*
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.
8 */
9 #include <linux/string.h>
10
11 #include <asm/segment.h>
12
13 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
14
15 #ifndef KERNEL_DS
16 #define KERNEL_DS MAKE_MM_SEG(~0UL)
17 #endif
18
19 #ifndef USER_DS
20 #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
21 #endif
22
23 #ifndef get_fs
24 #define get_ds() (KERNEL_DS)
25 #define get_fs() (current_thread_info()->addr_limit)
26
27 static inline void set_fs(mm_segment_t fs)
28 {
29 current_thread_info()->addr_limit = fs;
30 }
31 #endif
32
33 #ifndef segment_eq
34 #define segment_eq(a, b) ((a).seg == (b).seg)
35 #endif
36
37 #define access_ok(type, addr, size) __access_ok((unsigned long)(addr),(size))
38
39 /*
40 * The architecture should really override this if possible, at least
41 * doing a check on the get_fs()
42 */
43 #ifndef __access_ok
44 static inline int __access_ok(unsigned long addr, unsigned long size)
45 {
46 return 1;
47 }
48 #endif
49
50 /*
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.
55 */
56 #define __put_user(x, ptr) \
57 ({ \
58 __typeof__(*(ptr)) __x = (x); \
59 int __pu_err = -EFAULT; \
60 __chk_user_ptr(ptr); \
61 switch (sizeof (*(ptr))) { \
62 case 1: \
63 case 2: \
64 case 4: \
65 case 8: \
66 __pu_err = __put_user_fn(sizeof (*(ptr)), \
67 ptr, &__x); \
68 break; \
69 default: \
70 __put_user_bad(); \
71 break; \
72 } \
73 __pu_err; \
74 })
75
76 #define put_user(x, ptr) \
77 ({ \
78 void *__p = (ptr); \
79 might_fault(); \
80 access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ? \
81 __put_user((x), ((__typeof__(*(ptr)) *)__p)) : \
82 -EFAULT; \
83 })
84
85 #ifndef __put_user_fn
86
87 static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
88 {
89 #ifdef CONFIG_ARCH_HAS_RAW_COPY_USER
90 return unlikely(raw_copy_to_user(ptr, x, size)) ? -EFAULT : 0;
91 #else
92 return unlikely(__copy_to_user(ptr, x, size)) ? -EFAULT : 0;
93 #endif
94 }
95
96 #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
97
98 #endif
99
100 extern int __put_user_bad(void) __attribute__((noreturn));
101
102 #define __get_user(x, ptr) \
103 ({ \
104 int __gu_err = -EFAULT; \
105 __chk_user_ptr(ptr); \
106 switch (sizeof(*(ptr))) { \
107 case 1: { \
108 unsigned char __x = 0; \
109 __gu_err = __get_user_fn(sizeof (*(ptr)), \
110 ptr, &__x); \
111 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
112 break; \
113 }; \
114 case 2: { \
115 unsigned short __x = 0; \
116 __gu_err = __get_user_fn(sizeof (*(ptr)), \
117 ptr, &__x); \
118 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
119 break; \
120 }; \
121 case 4: { \
122 unsigned int __x = 0; \
123 __gu_err = __get_user_fn(sizeof (*(ptr)), \
124 ptr, &__x); \
125 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
126 break; \
127 }; \
128 case 8: { \
129 unsigned long long __x = 0; \
130 __gu_err = __get_user_fn(sizeof (*(ptr)), \
131 ptr, &__x); \
132 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
133 break; \
134 }; \
135 default: \
136 __get_user_bad(); \
137 break; \
138 } \
139 __gu_err; \
140 })
141
142 #define get_user(x, ptr) \
143 ({ \
144 const void *__p = (ptr); \
145 might_fault(); \
146 access_ok(VERIFY_READ, __p, sizeof(*ptr)) ? \
147 __get_user((x), (__typeof__(*(ptr)) *)__p) : \
148 ((x) = (__typeof__(*(ptr)))0,-EFAULT); \
149 })
150
151 #ifndef __get_user_fn
152 static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
153 {
154 #ifdef CONFIG_ARCH_HAS_RAW_COPY_USER
155 return unlikely(raw_copy_from_user(x, ptr, size)) ? -EFAULT : 0;
156 #else
157 return unlikely(__copy_from_user(x, ptr, size)) ? -EFAULT : 0;
158 #endif
159 }
160
161 #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
162
163 #endif
164
165 extern int __get_user_bad(void) __attribute__((noreturn));
166
167 #ifndef CONFIG_ARCH_HAS_RAW_COPY_USER
168
169 #ifndef __copy_from_user_inatomic
170 #define __copy_from_user_inatomic __copy_from_user
171 #endif
172
173 #ifndef __copy_to_user_inatomic
174 #define __copy_to_user_inatomic __copy_to_user
175 #endif
176
177 static inline long copy_from_user(void *to,
178 const void __user * from, unsigned long n)
179 {
180 unsigned long res = n;
181 might_fault();
182 if (likely(access_ok(VERIFY_READ, from, n)))
183 res = __copy_from_user(to, from, n);
184 if (unlikely(res))
185 memset(to + (n - res), 0, res);
186 return res;
187 }
188
189 static inline long copy_to_user(void __user *to,
190 const void *from, unsigned long n)
191 {
192 might_fault();
193 if (access_ok(VERIFY_WRITE, to, n))
194 return __copy_to_user(to, from, n);
195 else
196 return n;
197 }
198 #endif
199
200 /*
201 * Copy a null terminated string from userspace.
202 */
203 #ifndef __strncpy_from_user
204 static inline long
205 __strncpy_from_user(char *dst, const char __user *src, long count)
206 {
207 char *tmp;
208 strncpy(dst, (const char __force *)src, count);
209 for (tmp = dst; *tmp && count > 0; tmp++, count--)
210 ;
211 return (tmp - dst);
212 }
213 #endif
214
215 static inline long
216 strncpy_from_user(char *dst, const char __user *src, long count)
217 {
218 if (!access_ok(VERIFY_READ, src, 1))
219 return -EFAULT;
220 return __strncpy_from_user(dst, src, count);
221 }
222
223 /*
224 * Return the size of a string (including the ending 0)
225 *
226 * Return 0 on exception, a value greater than N if too long
227 */
228 #ifndef __strnlen_user
229 #define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
230 #endif
231
232 /*
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.
236 */
237 static inline long strnlen_user(const char __user *src, long n)
238 {
239 if (!access_ok(VERIFY_READ, src, 1))
240 return 0;
241 return __strnlen_user(src, n);
242 }
243
244 static inline long strlen_user(const char __user *src)
245 {
246 return strnlen_user(src, 32767);
247 }
248
249 /*
250 * Zero Userspace
251 */
252 #ifndef __clear_user
253 static inline __must_check unsigned long
254 __clear_user(void __user *to, unsigned long n)
255 {
256 memset((void __force *)to, 0, n);
257 return 0;
258 }
259 #endif
260
261 static inline __must_check unsigned long
262 clear_user(void __user *to, unsigned long n)
263 {
264 might_fault();
265 if (!access_ok(VERIFY_WRITE, to, n))
266 return n;
267
268 return __clear_user(to, n);
269 }
270
271 #include <asm/extable.h>
272
273 #endif /* __ASM_GENERIC_UACCESS_H */