Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / include / asm-x86 / uaccess_32.h
1 #ifndef __i386_UACCESS_H
2 #define __i386_UACCESS_H
3
4 /*
5  * User space memory access functions
6  */
7 #include <linux/errno.h>
8 #include <linux/thread_info.h>
9 #include <linux/prefetch.h>
10 #include <linux/string.h>
11 #include <asm/asm.h>
12 #include <asm/page.h>
13
14 #define VERIFY_READ 0
15 #define VERIFY_WRITE 1
16
17 /*
18  * The fs value determines whether argument validity checking should be
19  * performed or not.  If get_fs() == USER_DS, checking is performed, with
20  * get_fs() == KERNEL_DS, checking is bypassed.
21  *
22  * For historical reasons, these macros are grossly misnamed.
23  */
24
25 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
26
27
28 #define KERNEL_DS       MAKE_MM_SEG(0xFFFFFFFFUL)
29 #define USER_DS         MAKE_MM_SEG(PAGE_OFFSET)
30
31 #define get_ds()        (KERNEL_DS)
32 #define get_fs()        (current_thread_info()->addr_limit)
33 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
34
35 #define segment_eq(a, b)        ((a).seg == (b).seg)
36
37 /*
38  * movsl can be slow when source and dest are not both 8-byte aligned
39  */
40 #ifdef CONFIG_X86_INTEL_USERCOPY
41 extern struct movsl_mask {
42         int mask;
43 } ____cacheline_aligned_in_smp movsl_mask;
44 #endif
45
46 #define __addr_ok(addr)                                 \
47         ((unsigned long __force)(addr) <                \
48          (current_thread_info()->addr_limit.seg))
49
50 /*
51  * Test whether a block of memory is a valid user space address.
52  * Returns 0 if the range is valid, nonzero otherwise.
53  *
54  * This is equivalent to the following test:
55  * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
56  *
57  * This needs 33-bit arithmetic. We have a carry...
58  */
59 #define __range_ok(addr, size)                                          \
60 ({                                                                      \
61         unsigned long flag, roksum;                                     \
62         __chk_user_ptr(addr);                                           \
63         asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0"           \
64             :"=&r" (flag), "=r" (roksum)                                \
65             :"1" (addr), "g" ((int)(size)),                             \
66             "rm" (current_thread_info()->addr_limit.seg));              \
67         flag;                                                           \
68 })
69
70 /**
71  * access_ok: - Checks if a user space pointer is valid
72  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
73  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
74  *        to write to a block, it is always safe to read from it.
75  * @addr: User space pointer to start of block to check
76  * @size: Size of block to check
77  *
78  * Context: User context only.  This function may sleep.
79  *
80  * Checks if a pointer to a block of memory in user space is valid.
81  *
82  * Returns true (nonzero) if the memory block may be valid, false (zero)
83  * if it is definitely invalid.
84  *
85  * Note that, depending on architecture, this function probably just
86  * checks that the pointer is in the user space range - after calling
87  * this function, memory access functions may still return -EFAULT.
88  */
89 #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
90
91 /*
92  * The exception table consists of pairs of addresses: the first is the
93  * address of an instruction that is allowed to fault, and the second is
94  * the address at which the program should continue.  No registers are
95  * modified, so it is entirely up to the continuation code to figure out
96  * what to do.
97  *
98  * All the routines below use bits of fixup code that are out of line
99  * with the main instruction path.  This means when everything is well,
100  * we don't even have to jump over them.  Further, they do not intrude
101  * on our cache or tlb entries.
102  */
103
104 struct exception_table_entry {
105         unsigned long insn, fixup;
106 };
107
108 extern int fixup_exception(struct pt_regs *regs);
109
110 /*
111  * These are the main single-value transfer routines.  They automatically
112  * use the right size if we just have the right pointer type.
113  *
114  * This gets kind of ugly. We want to return _two_ values in "get_user()"
115  * and yet we don't want to do any pointers, because that is too much
116  * of a performance impact. Thus we have a few rather ugly macros here,
117  * and hide all the ugliness from the user.
118  *
119  * The "__xxx" versions of the user access functions are versions that
120  * do not verify the address space, that must have been done previously
121  * with a separate "access_ok()" call (this is used when we do multiple
122  * accesses to the same area of user memory).
123  */
124
125 extern void __get_user_1(void);
126 extern void __get_user_2(void);
127 extern void __get_user_4(void);
128
129 #define __get_user_x(size, ret, x, ptr)       \
130         asm volatile("call __get_user_" #size \
131                      :"=a" (ret),"=d" (x)     \
132                      :"0" (ptr))
133
134
135 /* Careful: we have to cast the result to the type of the pointer
136  * for sign reasons */
137
138 /**
139  * get_user: - Get a simple variable from user space.
140  * @x:   Variable to store result.
141  * @ptr: Source address, in user space.
142  *
143  * Context: User context only.  This function may sleep.
144  *
145  * This macro copies a single simple variable from user space to kernel
146  * space.  It supports simple types like char and int, but not larger
147  * data types like structures or arrays.
148  *
149  * @ptr must have pointer-to-simple-variable type, and the result of
150  * dereferencing @ptr must be assignable to @x without a cast.
151  *
152  * Returns zero on success, or -EFAULT on error.
153  * On error, the variable @x is set to zero.
154  */
155 #define get_user(x, ptr)                                                \
156 ({                                                                      \
157         int __ret_gu;                                                   \
158         unsigned long __val_gu;                                         \
159         __chk_user_ptr(ptr);                                            \
160         switch (sizeof(*(ptr))) {                                       \
161         case 1:                                                         \
162                 __get_user_x(1, __ret_gu, __val_gu, ptr);               \
163                 break;                                                  \
164         case 2:                                                         \
165                 __get_user_x(2, __ret_gu, __val_gu, ptr);               \
166                 break;                                                  \
167         case 4:                                                         \
168                 __get_user_x(4, __ret_gu, __val_gu, ptr);               \
169                 break;                                                  \
170         default:                                                        \
171                 __get_user_x(X, __ret_gu, __val_gu, ptr);               \
172                 break;                                                  \
173         }                                                               \
174         (x) = (__typeof__(*(ptr)))__val_gu;                             \
175         __ret_gu;                                                       \
176 })
177
178 extern void __put_user_bad(void);
179
180 /*
181  * Strange magic calling convention: pointer in %ecx,
182  * value in %eax(:%edx), return value in %eax, no clobbers.
183  */
184 extern void __put_user_1(void);
185 extern void __put_user_2(void);
186 extern void __put_user_4(void);
187 extern void __put_user_8(void);
188
189 #define __put_user_1(x, ptr)                                    \
190         asm volatile("call __put_user_1" : "=a" (__ret_pu)      \
191                      : "0" ((typeof(*(ptr)))(x)), "c" (ptr))
192
193 #define __put_user_2(x, ptr)                                    \
194         asm volatile("call __put_user_2" : "=a" (__ret_pu)      \
195                      : "0" ((typeof(*(ptr)))(x)), "c" (ptr))
196
197 #define __put_user_4(x, ptr)                                    \
198         asm volatile("call __put_user_4" : "=a" (__ret_pu)      \
199                      : "0" ((typeof(*(ptr)))(x)), "c" (ptr))
200
201 #define __put_user_8(x, ptr)                                    \
202         asm volatile("call __put_user_8" : "=a" (__ret_pu)      \
203                      : "A" ((typeof(*(ptr)))(x)), "c" (ptr))
204
205 #define __put_user_X(x, ptr)                                    \
206         asm volatile("call __put_user_X" : "=a" (__ret_pu)      \
207                      : "c" (ptr))
208
209 /**
210  * put_user: - Write a simple value into user space.
211  * @x:   Value to copy to user space.
212  * @ptr: Destination address, in user space.
213  *
214  * Context: User context only.  This function may sleep.
215  *
216  * This macro copies a single simple value from kernel space to user
217  * space.  It supports simple types like char and int, but not larger
218  * data types like structures or arrays.
219  *
220  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
221  * to the result of dereferencing @ptr.
222  *
223  * Returns zero on success, or -EFAULT on error.
224  */
225 #ifdef CONFIG_X86_WP_WORKS_OK
226
227 #define put_user(x, ptr)                                        \
228 ({                                                              \
229         int __ret_pu;                                           \
230         __typeof__(*(ptr)) __pu_val;                            \
231         __chk_user_ptr(ptr);                                    \
232         __pu_val = x;                                           \
233         switch (sizeof(*(ptr))) {                               \
234         case 1:                                                 \
235                 __put_user_1(__pu_val, ptr);                    \
236                 break;                                          \
237         case 2:                                                 \
238                 __put_user_2(__pu_val, ptr);                    \
239                 break;                                          \
240         case 4:                                                 \
241                 __put_user_4(__pu_val, ptr);                    \
242                 break;                                          \
243         case 8:                                                 \
244                 __put_user_8(__pu_val, ptr);                    \
245                 break;                                          \
246         default:                                                \
247                 __put_user_X(__pu_val, ptr);                    \
248                 break;                                          \
249         }                                                       \
250         __ret_pu;                                               \
251 })
252
253 #else
254 #define put_user(x, ptr)                                        \
255 ({                                                              \
256         int __ret_pu;                                           \
257         __typeof__(*(ptr))__pus_tmp = x;                        \
258         __ret_pu = 0;                                           \
259         if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp,         \
260                                        sizeof(*(ptr))) != 0))   \
261                 __ret_pu = -EFAULT;                             \
262         __ret_pu;                                               \
263 })
264
265
266 #endif
267
268 /**
269  * __get_user: - Get a simple variable from user space, with less checking.
270  * @x:   Variable to store result.
271  * @ptr: Source address, in user space.
272  *
273  * Context: User context only.  This function may sleep.
274  *
275  * This macro copies a single simple variable from user space to kernel
276  * space.  It supports simple types like char and int, but not larger
277  * data types like structures or arrays.
278  *
279  * @ptr must have pointer-to-simple-variable type, and the result of
280  * dereferencing @ptr must be assignable to @x without a cast.
281  *
282  * Caller must check the pointer with access_ok() before calling this
283  * function.
284  *
285  * Returns zero on success, or -EFAULT on error.
286  * On error, the variable @x is set to zero.
287  */
288 #define __get_user(x, ptr)                              \
289         __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
290
291
292 /**
293  * __put_user: - Write a simple value into user space, with less checking.
294  * @x:   Value to copy to user space.
295  * @ptr: Destination address, in user space.
296  *
297  * Context: User context only.  This function may sleep.
298  *
299  * This macro copies a single simple value from kernel space to user
300  * space.  It supports simple types like char and int, but not larger
301  * data types like structures or arrays.
302  *
303  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
304  * to the result of dereferencing @ptr.
305  *
306  * Caller must check the pointer with access_ok() before calling this
307  * function.
308  *
309  * Returns zero on success, or -EFAULT on error.
310  */
311 #define __put_user(x, ptr)                                              \
312         __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
313
314 #define __put_user_nocheck(x, ptr, size)                        \
315 ({                                                              \
316         long __pu_err;                                          \
317         __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
318         __pu_err;                                               \
319 })
320
321
322 #define __put_user_u64(x, addr, err)                                    \
323         asm volatile("1:        movl %%eax,0(%2)\n"                     \
324                      "2:        movl %%edx,4(%2)\n"                     \
325                      "3:\n"                                             \
326                      ".section .fixup,\"ax\"\n"                         \
327                      "4:        movl %3,%0\n"                           \
328                      "  jmp 3b\n"                                       \
329                      ".previous\n"                                      \
330                      _ASM_EXTABLE(1b, 4b)                               \
331                      _ASM_EXTABLE(2b, 4b)                               \
332                      : "=r" (err)                                       \
333                      : "A" (x), "r" (addr), "i" (-EFAULT), "0" (err))
334
335 #ifdef CONFIG_X86_WP_WORKS_OK
336
337 #define __put_user_size(x, ptr, size, retval, errret)                   \
338 do {                                                                    \
339         retval = 0;                                                     \
340         __chk_user_ptr(ptr);                                            \
341         switch (size) {                                                 \
342         case 1:                                                         \
343                 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
344                 break;                                                  \
345         case 2:                                                         \
346                 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
347                 break;                                                  \
348         case 4:                                                         \
349                 __put_user_asm(x, ptr, retval, "l", "",  "ir", errret); \
350                 break;                                                  \
351         case 8:                                                         \
352                 __put_user_u64((__typeof__(*ptr))(x), ptr, retval);     \
353                 break;                                                  \
354         default:                                                        \
355                 __put_user_bad();                                       \
356         }                                                               \
357 } while (0)
358
359 #else
360
361 #define __put_user_size(x, ptr, size, retval, errret)                   \
362 do {                                                                    \
363         __typeof__(*(ptr))__pus_tmp = x;                                \
364         retval = 0;                                                     \
365                                                                         \
366         if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0))    \
367                 retval = errret;                                        \
368 } while (0)
369
370 #endif
371 struct __large_struct { unsigned long buf[100]; };
372 #define __m(x) (*(struct __large_struct __user *)(x))
373
374 /*
375  * Tell gcc we read from memory instead of writing: this is because
376  * we do not write to any memory gcc knows about, so there are no
377  * aliasing issues.
378  */
379 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
380         asm volatile("1:        mov"itype" %"rtype"1,%2\n"              \
381                      "2:\n"                                             \
382                      ".section .fixup,\"ax\"\n"                         \
383                      "3:        movl %3,%0\n"                           \
384                      "  jmp 2b\n"                                       \
385                      ".previous\n"                                      \
386                      _ASM_EXTABLE(1b, 3b)                               \
387                      : "=r"(err)                                        \
388                      : ltype (x), "m" (__m(addr)), "i" (errret), "0" (err))
389
390
391 #define __get_user_nocheck(x, ptr, size)                                \
392 ({                                                                      \
393         long __gu_err;                                                  \
394         unsigned long __gu_val;                                         \
395         __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT);    \
396         (x) = (__typeof__(*(ptr)))__gu_val;                             \
397         __gu_err;                                                       \
398 })
399
400 extern long __get_user_bad(void);
401
402 #define __get_user_size(x, ptr, size, retval, errret)                   \
403 do {                                                                    \
404         retval = 0;                                                     \
405         __chk_user_ptr(ptr);                                            \
406         switch (size) {                                                 \
407         case 1:                                                         \
408                 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
409                 break;                                                  \
410         case 2:                                                         \
411                 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
412                 break;                                                  \
413         case 4:                                                         \
414                 __get_user_asm(x, ptr, retval, "l", "", "=r", errret);  \
415                 break;                                                  \
416         default:                                                        \
417                 (x) = __get_user_bad();                                 \
418         }                                                               \
419 } while (0)
420
421 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
422         asm volatile("1:        mov"itype" %2,%"rtype"1\n"              \
423                      "2:\n"                                             \
424                      ".section .fixup,\"ax\"\n"                         \
425                      "3:        movl %3,%0\n"                           \
426                      "  xor"itype" %"rtype"1,%"rtype"1\n"               \
427                      "  jmp 2b\n"                                       \
428                      ".previous\n"                                      \
429                      _ASM_EXTABLE(1b, 3b)                               \
430                      : "=r" (err), ltype (x)                            \
431                      : "m" (__m(addr)), "i" (errret), "0" (err))
432
433
434 unsigned long __must_check __copy_to_user_ll
435                 (void __user *to, const void *from, unsigned long n);
436 unsigned long __must_check __copy_from_user_ll
437                 (void *to, const void __user *from, unsigned long n);
438 unsigned long __must_check __copy_from_user_ll_nozero
439                 (void *to, const void __user *from, unsigned long n);
440 unsigned long __must_check __copy_from_user_ll_nocache
441                 (void *to, const void __user *from, unsigned long n);
442 unsigned long __must_check __copy_from_user_ll_nocache_nozero
443                 (void *to, const void __user *from, unsigned long n);
444
445 /**
446  * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
447  * @to:   Destination address, in user space.
448  * @from: Source address, in kernel space.
449  * @n:    Number of bytes to copy.
450  *
451  * Context: User context only.
452  *
453  * Copy data from kernel space to user space.  Caller must check
454  * the specified block with access_ok() before calling this function.
455  * The caller should also make sure he pins the user space address
456  * so that the we don't result in page fault and sleep.
457  *
458  * Here we special-case 1, 2 and 4-byte copy_*_user invocations.  On a fault
459  * we return the initial request size (1, 2 or 4), as copy_*_user should do.
460  * If a store crosses a page boundary and gets a fault, the x86 will not write
461  * anything, so this is accurate.
462  */
463
464 static __always_inline unsigned long __must_check
465 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
466 {
467         if (__builtin_constant_p(n)) {
468                 unsigned long ret;
469
470                 switch (n) {
471                 case 1:
472                         __put_user_size(*(u8 *)from, (u8 __user *)to,
473                                         1, ret, 1);
474                         return ret;
475                 case 2:
476                         __put_user_size(*(u16 *)from, (u16 __user *)to,
477                                         2, ret, 2);
478                         return ret;
479                 case 4:
480                         __put_user_size(*(u32 *)from, (u32 __user *)to,
481                                         4, ret, 4);
482                         return ret;
483                 }
484         }
485         return __copy_to_user_ll(to, from, n);
486 }
487
488 /**
489  * __copy_to_user: - Copy a block of data into user space, with less checking.
490  * @to:   Destination address, in user space.
491  * @from: Source address, in kernel space.
492  * @n:    Number of bytes to copy.
493  *
494  * Context: User context only.  This function may sleep.
495  *
496  * Copy data from kernel space to user space.  Caller must check
497  * the specified block with access_ok() before calling this function.
498  *
499  * Returns number of bytes that could not be copied.
500  * On success, this will be zero.
501  */
502 static __always_inline unsigned long __must_check
503 __copy_to_user(void __user *to, const void *from, unsigned long n)
504 {
505        might_sleep();
506        return __copy_to_user_inatomic(to, from, n);
507 }
508
509 static __always_inline unsigned long
510 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
511 {
512         /* Avoid zeroing the tail if the copy fails..
513          * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
514          * but as the zeroing behaviour is only significant when n is not
515          * constant, that shouldn't be a problem.
516          */
517         if (__builtin_constant_p(n)) {
518                 unsigned long ret;
519
520                 switch (n) {
521                 case 1:
522                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
523                         return ret;
524                 case 2:
525                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
526                         return ret;
527                 case 4:
528                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
529                         return ret;
530                 }
531         }
532         return __copy_from_user_ll_nozero(to, from, n);
533 }
534
535 /**
536  * __copy_from_user: - Copy a block of data from user space, with less checking.
537  * @to:   Destination address, in kernel space.
538  * @from: Source address, in user space.
539  * @n:    Number of bytes to copy.
540  *
541  * Context: User context only.  This function may sleep.
542  *
543  * Copy data from user space to kernel space.  Caller must check
544  * the specified block with access_ok() before calling this function.
545  *
546  * Returns number of bytes that could not be copied.
547  * On success, this will be zero.
548  *
549  * If some data could not be copied, this function will pad the copied
550  * data to the requested size using zero bytes.
551  *
552  * An alternate version - __copy_from_user_inatomic() - may be called from
553  * atomic context and will fail rather than sleep.  In this case the
554  * uncopied bytes will *NOT* be padded with zeros.  See fs/filemap.h
555  * for explanation of why this is needed.
556  */
557 static __always_inline unsigned long
558 __copy_from_user(void *to, const void __user *from, unsigned long n)
559 {
560         might_sleep();
561         if (__builtin_constant_p(n)) {
562                 unsigned long ret;
563
564                 switch (n) {
565                 case 1:
566                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
567                         return ret;
568                 case 2:
569                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
570                         return ret;
571                 case 4:
572                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
573                         return ret;
574                 }
575         }
576         return __copy_from_user_ll(to, from, n);
577 }
578
579 #define ARCH_HAS_NOCACHE_UACCESS
580
581 static __always_inline unsigned long __copy_from_user_nocache(void *to,
582                                 const void __user *from, unsigned long n)
583 {
584         might_sleep();
585         if (__builtin_constant_p(n)) {
586                 unsigned long ret;
587
588                 switch (n) {
589                 case 1:
590                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
591                         return ret;
592                 case 2:
593                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
594                         return ret;
595                 case 4:
596                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
597                         return ret;
598                 }
599         }
600         return __copy_from_user_ll_nocache(to, from, n);
601 }
602
603 static __always_inline unsigned long
604 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
605                                   unsigned long n)
606 {
607        return __copy_from_user_ll_nocache_nozero(to, from, n);
608 }
609
610 unsigned long __must_check copy_to_user(void __user *to,
611                                         const void *from, unsigned long n);
612 unsigned long __must_check copy_from_user(void *to,
613                                           const void __user *from,
614                                           unsigned long n);
615 long __must_check strncpy_from_user(char *dst, const char __user *src,
616                                     long count);
617 long __must_check __strncpy_from_user(char *dst,
618                                       const char __user *src, long count);
619
620 /**
621  * strlen_user: - Get the size of a string in user space.
622  * @str: The string to measure.
623  *
624  * Context: User context only.  This function may sleep.
625  *
626  * Get the size of a NUL-terminated string in user space.
627  *
628  * Returns the size of the string INCLUDING the terminating NUL.
629  * On exception, returns 0.
630  *
631  * If there is a limit on the length of a valid string, you may wish to
632  * consider using strnlen_user() instead.
633  */
634 #define strlen_user(str) strnlen_user(str, LONG_MAX)
635
636 long strnlen_user(const char __user *str, long n);
637 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
638 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
639
640 #endif /* __i386_UACCESS_H */