uaccess: drop pointless ifdefs
[linux-block.git] / include / asm-generic / uaccess.h
CommitLineData
eed417dd
AB
1#ifndef __ASM_GENERIC_UACCESS_H
2#define __ASM_GENERIC_UACCESS_H
3
4/*
5 * User space memory access functions, these should work
0a4a6647 6 * on any machine that has kernel and user data in the same
eed417dd
AB
7 * address space, e.g. all NOMMU machines.
8 */
eed417dd
AB
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
27static inline void set_fs(mm_segment_t fs)
28{
29 current_thread_info()->addr_limit = fs;
30}
31#endif
32
10a6007b 33#ifndef segment_eq
eed417dd 34#define segment_eq(a, b) ((a).seg == (b).seg)
10a6007b 35#endif
eed417dd 36
eed417dd
AB
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
44static inline int __access_ok(unsigned long addr, unsigned long size)
45{
46 return 1;
47}
48#endif
49
50/*
51 * The exception table consists of pairs of addresses: the first is the
52 * address of an instruction that is allowed to fault, and the second is
53 * the address at which the program should continue. No registers are
54 * modified, so it is entirely up to the continuation code to figure out
55 * what to do.
56 *
57 * All the routines below use bits of fixup code that are out of line
58 * with the main instruction path. This means when everything is well,
59 * we don't even have to jump over them. Further, they do not intrude
60 * on our cache or tlb entries.
61 */
62
63struct exception_table_entry
64{
65 unsigned long insn, fixup;
66};
67
eed417dd
AB
68/*
69 * architectures with an MMU should override these two
70 */
71#ifndef __copy_from_user
72static inline __must_check long __copy_from_user(void *to,
73 const void __user * from, unsigned long n)
74{
75 if (__builtin_constant_p(n)) {
76 switch(n) {
77 case 1:
78 *(u8 *)to = *(u8 __force *)from;
79 return 0;
80 case 2:
81 *(u16 *)to = *(u16 __force *)from;
82 return 0;
83 case 4:
84 *(u32 *)to = *(u32 __force *)from;
85 return 0;
86#ifdef CONFIG_64BIT
87 case 8:
88 *(u64 *)to = *(u64 __force *)from;
89 return 0;
90#endif
91 default:
92 break;
93 }
94 }
95
96 memcpy(to, (const void __force *)from, n);
97 return 0;
98}
99#endif
100
101#ifndef __copy_to_user
102static inline __must_check long __copy_to_user(void __user *to,
103 const void *from, unsigned long n)
104{
105 if (__builtin_constant_p(n)) {
106 switch(n) {
107 case 1:
108 *(u8 __force *)to = *(u8 *)from;
109 return 0;
110 case 2:
111 *(u16 __force *)to = *(u16 *)from;
112 return 0;
113 case 4:
114 *(u32 __force *)to = *(u32 *)from;
115 return 0;
116#ifdef CONFIG_64BIT
117 case 8:
118 *(u64 __force *)to = *(u64 *)from;
119 return 0;
120#endif
121 default:
122 break;
123 }
124 }
125
126 memcpy((void __force *)to, from, n);
127 return 0;
128}
129#endif
130
131/*
132 * These are the main single-value transfer routines. They automatically
133 * use the right size if we just have the right pointer type.
134 * This version just falls back to copy_{from,to}_user, which should
135 * provide a fast-path for small values.
136 */
137#define __put_user(x, ptr) \
138({ \
139 __typeof__(*(ptr)) __x = (x); \
140 int __pu_err = -EFAULT; \
141 __chk_user_ptr(ptr); \
142 switch (sizeof (*(ptr))) { \
143 case 1: \
144 case 2: \
145 case 4: \
146 case 8: \
147 __pu_err = __put_user_fn(sizeof (*(ptr)), \
148 ptr, &__x); \
149 break; \
150 default: \
151 __put_user_bad(); \
152 break; \
153 } \
154 __pu_err; \
155})
156
157#define put_user(x, ptr) \
158({ \
a02613a4 159 void *__p = (ptr); \
e0acd0bd 160 might_fault(); \
a02613a4
YS
161 access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ? \
162 __put_user((x), ((__typeof__(*(ptr)) *)__p)) : \
eed417dd
AB
163 -EFAULT; \
164})
165
05d88a49
VG
166#ifndef __put_user_fn
167
eed417dd
AB
168static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
169{
170 size = __copy_to_user(ptr, x, size);
171 return size ? -EFAULT : size;
172}
173
05d88a49
VG
174#define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
175
176#endif
177
eed417dd
AB
178extern int __put_user_bad(void) __attribute__((noreturn));
179
180#define __get_user(x, ptr) \
181({ \
182 int __gu_err = -EFAULT; \
183 __chk_user_ptr(ptr); \
184 switch (sizeof(*(ptr))) { \
185 case 1: { \
186 unsigned char __x; \
187 __gu_err = __get_user_fn(sizeof (*(ptr)), \
188 ptr, &__x); \
189 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
190 break; \
191 }; \
192 case 2: { \
193 unsigned short __x; \
194 __gu_err = __get_user_fn(sizeof (*(ptr)), \
195 ptr, &__x); \
196 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
197 break; \
198 }; \
199 case 4: { \
200 unsigned int __x; \
201 __gu_err = __get_user_fn(sizeof (*(ptr)), \
202 ptr, &__x); \
203 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
204 break; \
205 }; \
206 case 8: { \
207 unsigned long long __x; \
208 __gu_err = __get_user_fn(sizeof (*(ptr)), \
209 ptr, &__x); \
210 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
211 break; \
212 }; \
213 default: \
214 __get_user_bad(); \
215 break; \
216 } \
217 __gu_err; \
218})
219
220#define get_user(x, ptr) \
221({ \
a02613a4 222 const void *__p = (ptr); \
e0acd0bd 223 might_fault(); \
a02613a4
YS
224 access_ok(VERIFY_READ, __p, sizeof(*ptr)) ? \
225 __get_user((x), (__typeof__(*(ptr)) *)__p) : \
9ad18b75 226 ((x) = (__typeof__(*(ptr)))0,-EFAULT); \
eed417dd
AB
227})
228
05d88a49 229#ifndef __get_user_fn
eed417dd
AB
230static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
231{
9ad18b75
AV
232 size_t n = __copy_from_user(x, ptr, size);
233 if (unlikely(n)) {
234 memset(x + (size - n), 0, n);
235 return -EFAULT;
236 }
237 return 0;
eed417dd
AB
238}
239
05d88a49
VG
240#define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
241
242#endif
243
eed417dd
AB
244extern int __get_user_bad(void) __attribute__((noreturn));
245
246#ifndef __copy_from_user_inatomic
247#define __copy_from_user_inatomic __copy_from_user
248#endif
249
250#ifndef __copy_to_user_inatomic
251#define __copy_to_user_inatomic __copy_to_user
252#endif
253
254static inline long copy_from_user(void *to,
255 const void __user * from, unsigned long n)
256{
2545e5da 257 unsigned long res = n;
e0acd0bd 258 might_fault();
2545e5da
AV
259 if (likely(access_ok(VERIFY_READ, from, n)))
260 res = __copy_from_user(to, from, n);
261 if (unlikely(res))
262 memset(to + (n - res), 0, res);
263 return res;
eed417dd
AB
264}
265
266static inline long copy_to_user(void __user *to,
267 const void *from, unsigned long n)
268{
e0acd0bd 269 might_fault();
a9ede5b3 270 if (access_ok(VERIFY_WRITE, to, n))
eed417dd
AB
271 return __copy_to_user(to, from, n);
272 else
273 return n;
274}
275
276/*
277 * Copy a null terminated string from userspace.
278 */
279#ifndef __strncpy_from_user
280static inline long
281__strncpy_from_user(char *dst, const char __user *src, long count)
282{
283 char *tmp;
284 strncpy(dst, (const char __force *)src, count);
285 for (tmp = dst; *tmp && count > 0; tmp++, count--)
286 ;
287 return (tmp - dst);
288}
289#endif
290
291static inline long
292strncpy_from_user(char *dst, const char __user *src, long count)
293{
a9ede5b3 294 if (!access_ok(VERIFY_READ, src, 1))
eed417dd
AB
295 return -EFAULT;
296 return __strncpy_from_user(dst, src, count);
297}
298
299/*
300 * Return the size of a string (including the ending 0)
301 *
302 * Return 0 on exception, a value greater than N if too long
303 */
7f509a9e 304#ifndef __strnlen_user
830f5800 305#define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
7f509a9e
G
306#endif
307
830f5800
MS
308/*
309 * Unlike strnlen, strnlen_user includes the nul terminator in
310 * its returned count. Callers should check for a returned value
311 * greater than N as an indication the string is too long.
312 */
eed417dd
AB
313static inline long strnlen_user(const char __user *src, long n)
314{
9844813f
MF
315 if (!access_ok(VERIFY_READ, src, 1))
316 return 0;
7f509a9e 317 return __strnlen_user(src, n);
eed417dd 318}
eed417dd
AB
319
320static inline long strlen_user(const char __user *src)
321{
322 return strnlen_user(src, 32767);
323}
324
325/*
326 * Zero Userspace
327 */
328#ifndef __clear_user
329static inline __must_check unsigned long
330__clear_user(void __user *to, unsigned long n)
331{
332 memset((void __force *)to, 0, n);
333 return 0;
334}
335#endif
336
337static inline __must_check unsigned long
338clear_user(void __user *to, unsigned long n)
339{
e0acd0bd 340 might_fault();
a9ede5b3 341 if (!access_ok(VERIFY_WRITE, to, n))
eed417dd
AB
342 return n;
343
344 return __clear_user(to, n);
345}
346
347#endif /* __ASM_GENERIC_UACCESS_H */