]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/asm-x86/uaccess_32.h
x86: use long instead of int.
[linux-2.6-omap-h63xx.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_not_ok(addr, size)                                      \
60 ({                                                                      \
61         unsigned long flag, roksum;                                     \
62         __chk_user_ptr(addr);                                           \
63         asm("add %3,%1 ; sbb %0,%0; cmp %1,%4; sbb $0,%0"               \
64             :"=&r" (flag), "=r" (roksum)                                \
65             :"1" (addr), "g" ((long)(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_not_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_x(size, x, ptr)                              \
190         asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
191                      :"0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
192
193 #define __put_user_8(x, ptr)                                    \
194         asm volatile("call __put_user_8" : "=a" (__ret_pu)      \
195                      : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
196
197
198 /**
199  * put_user: - Write a simple value into user space.
200  * @x:   Value to copy to user space.
201  * @ptr: Destination address, in user space.
202  *
203  * Context: User context only.  This function may sleep.
204  *
205  * This macro copies a single simple value from kernel space to user
206  * space.  It supports simple types like char and int, but not larger
207  * data types like structures or arrays.
208  *
209  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
210  * to the result of dereferencing @ptr.
211  *
212  * Returns zero on success, or -EFAULT on error.
213  */
214 #ifdef CONFIG_X86_WP_WORKS_OK
215
216 #define put_user(x, ptr)                                        \
217 ({                                                              \
218         int __ret_pu;                                           \
219         __typeof__(*(ptr)) __pu_val;                            \
220         __chk_user_ptr(ptr);                                    \
221         __pu_val = x;                                           \
222         switch (sizeof(*(ptr))) {                               \
223         case 1:                                                 \
224                 __put_user_x(1, __pu_val, ptr);                 \
225                 break;                                          \
226         case 2:                                                 \
227                 __put_user_x(2, __pu_val, ptr);                 \
228                 break;                                          \
229         case 4:                                                 \
230                 __put_user_x(4, __pu_val, ptr);                 \
231                 break;                                          \
232         case 8:                                                 \
233                 __put_user_8(__pu_val, ptr);                    \
234                 break;                                          \
235         default:                                                \
236                 __put_user_x(X, __pu_val, ptr);                 \
237                 break;                                          \
238         }                                                       \
239         __ret_pu;                                               \
240 })
241
242 #else
243 #define put_user(x, ptr)                                        \
244 ({                                                              \
245         int __ret_pu;                                           \
246         __typeof__(*(ptr))__pus_tmp = x;                        \
247         __ret_pu = 0;                                           \
248         if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp,         \
249                                        sizeof(*(ptr))) != 0))   \
250                 __ret_pu = -EFAULT;                             \
251         __ret_pu;                                               \
252 })
253
254
255 #endif
256
257 /**
258  * __get_user: - Get a simple variable from user space, with less checking.
259  * @x:   Variable to store result.
260  * @ptr: Source address, in user space.
261  *
262  * Context: User context only.  This function may sleep.
263  *
264  * This macro copies a single simple variable from user space to kernel
265  * space.  It supports simple types like char and int, but not larger
266  * data types like structures or arrays.
267  *
268  * @ptr must have pointer-to-simple-variable type, and the result of
269  * dereferencing @ptr must be assignable to @x without a cast.
270  *
271  * Caller must check the pointer with access_ok() before calling this
272  * function.
273  *
274  * Returns zero on success, or -EFAULT on error.
275  * On error, the variable @x is set to zero.
276  */
277 #define __get_user(x, ptr)                              \
278         __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
279
280
281 /**
282  * __put_user: - Write a simple value into user space, with less checking.
283  * @x:   Value to copy to user space.
284  * @ptr: Destination address, in user space.
285  *
286  * Context: User context only.  This function may sleep.
287  *
288  * This macro copies a single simple value from kernel space to user
289  * space.  It supports simple types like char and int, but not larger
290  * data types like structures or arrays.
291  *
292  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
293  * to the result of dereferencing @ptr.
294  *
295  * Caller must check the pointer with access_ok() before calling this
296  * function.
297  *
298  * Returns zero on success, or -EFAULT on error.
299  */
300 #define __put_user(x, ptr)                                              \
301         __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
302
303 #define __put_user_nocheck(x, ptr, size)                        \
304 ({                                                              \
305         long __pu_err;                                          \
306         __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
307         __pu_err;                                               \
308 })
309
310
311 #define __put_user_u64(x, addr, err)                                    \
312         asm volatile("1:        movl %%eax,0(%2)\n"                     \
313                      "2:        movl %%edx,4(%2)\n"                     \
314                      "3:\n"                                             \
315                      ".section .fixup,\"ax\"\n"                         \
316                      "4:        movl %3,%0\n"                           \
317                      "  jmp 3b\n"                                       \
318                      ".previous\n"                                      \
319                      _ASM_EXTABLE(1b, 4b)                               \
320                      _ASM_EXTABLE(2b, 4b)                               \
321                      : "=r" (err)                                       \
322                      : "A" (x), "r" (addr), "i" (-EFAULT), "0" (err))
323
324 #ifdef CONFIG_X86_WP_WORKS_OK
325
326 #define __put_user_size(x, ptr, size, retval, errret)                   \
327 do {                                                                    \
328         retval = 0;                                                     \
329         __chk_user_ptr(ptr);                                            \
330         switch (size) {                                                 \
331         case 1:                                                         \
332                 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
333                 break;                                                  \
334         case 2:                                                         \
335                 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
336                 break;                                                  \
337         case 4:                                                         \
338                 __put_user_asm(x, ptr, retval, "l", "",  "ir", errret); \
339                 break;                                                  \
340         case 8:                                                         \
341                 __put_user_u64((__typeof__(*ptr))(x), ptr, retval);     \
342                 break;                                                  \
343         default:                                                        \
344                 __put_user_bad();                                       \
345         }                                                               \
346 } while (0)
347
348 #else
349
350 #define __put_user_size(x, ptr, size, retval, errret)                   \
351 do {                                                                    \
352         __typeof__(*(ptr))__pus_tmp = x;                                \
353         retval = 0;                                                     \
354                                                                         \
355         if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0))    \
356                 retval = errret;                                        \
357 } while (0)
358
359 #endif
360 struct __large_struct { unsigned long buf[100]; };
361 #define __m(x) (*(struct __large_struct __user *)(x))
362
363 /*
364  * Tell gcc we read from memory instead of writing: this is because
365  * we do not write to any memory gcc knows about, so there are no
366  * aliasing issues.
367  */
368 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
369         asm volatile("1:        mov"itype" %"rtype"1,%2\n"              \
370                      "2:\n"                                             \
371                      ".section .fixup,\"ax\"\n"                         \
372                      "3:        movl %3,%0\n"                           \
373                      "  jmp 2b\n"                                       \
374                      ".previous\n"                                      \
375                      _ASM_EXTABLE(1b, 3b)                               \
376                      : "=r"(err)                                        \
377                      : ltype (x), "m" (__m(addr)), "i" (errret), "0" (err))
378
379
380 #define __get_user_nocheck(x, ptr, size)                                \
381 ({                                                                      \
382         long __gu_err;                                                  \
383         unsigned long __gu_val;                                         \
384         __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT);    \
385         (x) = (__typeof__(*(ptr)))__gu_val;                             \
386         __gu_err;                                                       \
387 })
388
389 extern long __get_user_bad(void);
390
391 #define __get_user_size(x, ptr, size, retval, errret)                   \
392 do {                                                                    \
393         retval = 0;                                                     \
394         __chk_user_ptr(ptr);                                            \
395         switch (size) {                                                 \
396         case 1:                                                         \
397                 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
398                 break;                                                  \
399         case 2:                                                         \
400                 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
401                 break;                                                  \
402         case 4:                                                         \
403                 __get_user_asm(x, ptr, retval, "l", "", "=r", errret);  \
404                 break;                                                  \
405         default:                                                        \
406                 (x) = __get_user_bad();                                 \
407         }                                                               \
408 } while (0)
409
410 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
411         asm volatile("1:        mov"itype" %2,%"rtype"1\n"              \
412                      "2:\n"                                             \
413                      ".section .fixup,\"ax\"\n"                         \
414                      "3:        movl %3,%0\n"                           \
415                      "  xor"itype" %"rtype"1,%"rtype"1\n"               \
416                      "  jmp 2b\n"                                       \
417                      ".previous\n"                                      \
418                      _ASM_EXTABLE(1b, 3b)                               \
419                      : "=r" (err), ltype (x)                            \
420                      : "m" (__m(addr)), "i" (errret), "0" (err))
421
422
423 unsigned long __must_check __copy_to_user_ll
424                 (void __user *to, const void *from, unsigned long n);
425 unsigned long __must_check __copy_from_user_ll
426                 (void *to, const void __user *from, unsigned long n);
427 unsigned long __must_check __copy_from_user_ll_nozero
428                 (void *to, const void __user *from, unsigned long n);
429 unsigned long __must_check __copy_from_user_ll_nocache
430                 (void *to, const void __user *from, unsigned long n);
431 unsigned long __must_check __copy_from_user_ll_nocache_nozero
432                 (void *to, const void __user *from, unsigned long n);
433
434 /**
435  * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
436  * @to:   Destination address, in user space.
437  * @from: Source address, in kernel space.
438  * @n:    Number of bytes to copy.
439  *
440  * Context: User context only.
441  *
442  * Copy data from kernel space to user space.  Caller must check
443  * the specified block with access_ok() before calling this function.
444  * The caller should also make sure he pins the user space address
445  * so that the we don't result in page fault and sleep.
446  *
447  * Here we special-case 1, 2 and 4-byte copy_*_user invocations.  On a fault
448  * we return the initial request size (1, 2 or 4), as copy_*_user should do.
449  * If a store crosses a page boundary and gets a fault, the x86 will not write
450  * anything, so this is accurate.
451  */
452
453 static __always_inline unsigned long __must_check
454 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
455 {
456         if (__builtin_constant_p(n)) {
457                 unsigned long ret;
458
459                 switch (n) {
460                 case 1:
461                         __put_user_size(*(u8 *)from, (u8 __user *)to,
462                                         1, ret, 1);
463                         return ret;
464                 case 2:
465                         __put_user_size(*(u16 *)from, (u16 __user *)to,
466                                         2, ret, 2);
467                         return ret;
468                 case 4:
469                         __put_user_size(*(u32 *)from, (u32 __user *)to,
470                                         4, ret, 4);
471                         return ret;
472                 }
473         }
474         return __copy_to_user_ll(to, from, n);
475 }
476
477 /**
478  * __copy_to_user: - Copy a block of data into user space, with less checking.
479  * @to:   Destination address, in user space.
480  * @from: Source address, in kernel space.
481  * @n:    Number of bytes to copy.
482  *
483  * Context: User context only.  This function may sleep.
484  *
485  * Copy data from kernel space to user space.  Caller must check
486  * the specified block with access_ok() before calling this function.
487  *
488  * Returns number of bytes that could not be copied.
489  * On success, this will be zero.
490  */
491 static __always_inline unsigned long __must_check
492 __copy_to_user(void __user *to, const void *from, unsigned long n)
493 {
494        might_sleep();
495        return __copy_to_user_inatomic(to, from, n);
496 }
497
498 static __always_inline unsigned long
499 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
500 {
501         /* Avoid zeroing the tail if the copy fails..
502          * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
503          * but as the zeroing behaviour is only significant when n is not
504          * constant, that shouldn't be a problem.
505          */
506         if (__builtin_constant_p(n)) {
507                 unsigned long ret;
508
509                 switch (n) {
510                 case 1:
511                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
512                         return ret;
513                 case 2:
514                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
515                         return ret;
516                 case 4:
517                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
518                         return ret;
519                 }
520         }
521         return __copy_from_user_ll_nozero(to, from, n);
522 }
523
524 /**
525  * __copy_from_user: - Copy a block of data from user space, with less checking.
526  * @to:   Destination address, in kernel space.
527  * @from: Source address, in user space.
528  * @n:    Number of bytes to copy.
529  *
530  * Context: User context only.  This function may sleep.
531  *
532  * Copy data from user space to kernel space.  Caller must check
533  * the specified block with access_ok() before calling this function.
534  *
535  * Returns number of bytes that could not be copied.
536  * On success, this will be zero.
537  *
538  * If some data could not be copied, this function will pad the copied
539  * data to the requested size using zero bytes.
540  *
541  * An alternate version - __copy_from_user_inatomic() - may be called from
542  * atomic context and will fail rather than sleep.  In this case the
543  * uncopied bytes will *NOT* be padded with zeros.  See fs/filemap.h
544  * for explanation of why this is needed.
545  */
546 static __always_inline unsigned long
547 __copy_from_user(void *to, const void __user *from, unsigned long n)
548 {
549         might_sleep();
550         if (__builtin_constant_p(n)) {
551                 unsigned long ret;
552
553                 switch (n) {
554                 case 1:
555                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
556                         return ret;
557                 case 2:
558                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
559                         return ret;
560                 case 4:
561                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
562                         return ret;
563                 }
564         }
565         return __copy_from_user_ll(to, from, n);
566 }
567
568 #define ARCH_HAS_NOCACHE_UACCESS
569
570 static __always_inline unsigned long __copy_from_user_nocache(void *to,
571                                 const void __user *from, unsigned long n)
572 {
573         might_sleep();
574         if (__builtin_constant_p(n)) {
575                 unsigned long ret;
576
577                 switch (n) {
578                 case 1:
579                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
580                         return ret;
581                 case 2:
582                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
583                         return ret;
584                 case 4:
585                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
586                         return ret;
587                 }
588         }
589         return __copy_from_user_ll_nocache(to, from, n);
590 }
591
592 static __always_inline unsigned long
593 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
594                                   unsigned long n)
595 {
596        return __copy_from_user_ll_nocache_nozero(to, from, n);
597 }
598
599 unsigned long __must_check copy_to_user(void __user *to,
600                                         const void *from, unsigned long n);
601 unsigned long __must_check copy_from_user(void *to,
602                                           const void __user *from,
603                                           unsigned long n);
604 long __must_check strncpy_from_user(char *dst, const char __user *src,
605                                     long count);
606 long __must_check __strncpy_from_user(char *dst,
607                                       const char __user *src, long count);
608
609 /**
610  * strlen_user: - Get the size of a string in user space.
611  * @str: The string to measure.
612  *
613  * Context: User context only.  This function may sleep.
614  *
615  * Get the size of a NUL-terminated string in user space.
616  *
617  * Returns the size of the string INCLUDING the terminating NUL.
618  * On exception, returns 0.
619  *
620  * If there is a limit on the length of a valid string, you may wish to
621  * consider using strnlen_user() instead.
622  */
623 #define strlen_user(str) strnlen_user(str, LONG_MAX)
624
625 long strnlen_user(const char __user *str, long n);
626 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
627 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
628
629 #endif /* __i386_UACCESS_H */