vserver 1.9.5.x5
[linux-2.6.git] / include / asm-i386 / uaccess.h
1 #ifndef __i386_UACCESS_H
2 #define __i386_UACCESS_H
3
4 /*
5  * User space memory access functions
6  */
7 #include <linux/config.h>
8 #include <linux/errno.h>
9 #include <linux/thread_info.h>
10 #include <linux/prefetch.h>
11 #include <linux/string.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) ((unsigned long __force)(addr) < (current_thread_info()->addr_limit.seg))
47
48 /*
49  * Test whether a block of memory is a valid user space address.
50  * Returns 0 if the range is valid, nonzero otherwise.
51  *
52  * This is equivalent to the following test:
53  * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
54  *
55  * This needs 33-bit arithmetic. We have a carry...
56  */
57 #define __range_ok(addr,size) ({ \
58         unsigned long flag,sum; \
59         __chk_user_ptr(addr); \
60         asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
61                 :"=&r" (flag), "=r" (sum) \
62                 :"1" (addr),"g" ((int)(size)),"g" (current_thread_info()->addr_limit.seg)); \
63         flag; })
64
65 /**
66  * access_ok: - Checks if a user space pointer is valid
67  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
68  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
69  *        to write to a block, it is always safe to read from it.
70  * @addr: User space pointer to start of block to check
71  * @size: Size of block to check
72  *
73  * Context: User context only.  This function may sleep.
74  *
75  * Checks if a pointer to a block of memory in user space is valid.
76  *
77  * Returns true (nonzero) if the memory block may be valid, false (zero)
78  * if it is definitely invalid.
79  *
80  * Note that, depending on architecture, this function probably just
81  * checks that the pointer is in the user space range - after calling
82  * this function, memory access functions may still return -EFAULT.
83  */
84 #define access_ok(type,addr,size) (likely(__range_ok(addr,size) == 0))
85
86 /**
87  * verify_area: - Obsolete, use access_ok()
88  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE
89  * @addr: User space pointer to start of block to check
90  * @size: Size of block to check
91  *
92  * Context: User context only.  This function may sleep.
93  *
94  * This function has been replaced by access_ok().
95  *
96  * Checks if a pointer to a block of memory in user space is valid.
97  *
98  * Returns zero if the memory block may be valid, -EFAULT
99  * if it is definitely invalid.
100  *
101  * See access_ok() for more details.
102  */
103 static inline int verify_area(int type, const void __user * addr, unsigned long size)
104 {
105         return access_ok(type,addr,size) ? 0 : -EFAULT;
106 }
107
108
109 /*
110  * The exception table consists of pairs of addresses: the first is the
111  * address of an instruction that is allowed to fault, and the second is
112  * the address at which the program should continue.  No registers are
113  * modified, so it is entirely up to the continuation code to figure out
114  * what to do.
115  *
116  * All the routines below use bits of fixup code that are out of line
117  * with the main instruction path.  This means when everything is well,
118  * we don't even have to jump over them.  Further, they do not intrude
119  * on our cache or tlb entries.
120  */
121
122 struct exception_table_entry
123 {
124         unsigned long insn, fixup;
125 };
126
127 extern int fixup_exception(struct pt_regs *regs);
128
129 /*
130  * These are the main single-value transfer routines.  They automatically
131  * use the right size if we just have the right pointer type.
132  *
133  * This gets kind of ugly. We want to return _two_ values in "get_user()"
134  * and yet we don't want to do any pointers, because that is too much
135  * of a performance impact. Thus we have a few rather ugly macros here,
136  * and hide all the ugliness from the user.
137  *
138  * The "__xxx" versions of the user access functions are versions that
139  * do not verify the address space, that must have been done previously
140  * with a separate "access_ok()" call (this is used when we do multiple
141  * accesses to the same area of user memory).
142  */
143
144 extern void __get_user_1(void);
145 extern void __get_user_2(void);
146 extern void __get_user_4(void);
147
148 #define __get_user_x(size,ret,x,ptr) \
149         __asm__ __volatile__("call __get_user_" #size \
150                 :"=a" (ret),"=d" (x) \
151                 :"0" (ptr))
152
153
154 /* Careful: we have to cast the result to the type of the pointer for sign reasons */
155 /**
156  * get_user: - Get a simple variable from user space.
157  * @x:   Variable to store result.
158  * @ptr: Source address, in user space.
159  *
160  * Context: User context only.  This function may sleep.
161  *
162  * This macro copies a single simple variable from user space to kernel
163  * space.  It supports simple types like char and int, but not larger
164  * data types like structures or arrays.
165  *
166  * @ptr must have pointer-to-simple-variable type, and the result of
167  * dereferencing @ptr must be assignable to @x without a cast.
168  *
169  * Returns zero on success, or -EFAULT on error.
170  * On error, the variable @x is set to zero.
171  */
172 #define get_user(x,ptr)                                                 \
173 ({      int __ret_gu;                                                   \
174         unsigned long __val_gu;                                         \
175         __chk_user_ptr(ptr);                                            \
176         switch(sizeof (*(ptr))) {                                       \
177         case 1:  __get_user_x(1,__ret_gu,__val_gu,ptr); break;          \
178         case 2:  __get_user_x(2,__ret_gu,__val_gu,ptr); break;          \
179         case 4:  __get_user_x(4,__ret_gu,__val_gu,ptr); break;          \
180         default: __get_user_x(X,__ret_gu,__val_gu,ptr); break;          \
181         }                                                               \
182         (x) = (__typeof__(*(ptr)))__val_gu;                             \
183         __ret_gu;                                                       \
184 })
185
186 extern void __put_user_bad(void);
187
188 /**
189  * put_user: - Write a simple value into user space.
190  * @x:   Value to copy to user space.
191  * @ptr: Destination address, in user space.
192  *
193  * Context: User context only.  This function may sleep.
194  *
195  * This macro copies a single simple value from kernel space to user
196  * space.  It supports simple types like char and int, but not larger
197  * data types like structures or arrays.
198  *
199  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
200  * to the result of dereferencing @ptr.
201  *
202  * Returns zero on success, or -EFAULT on error.
203  */
204 #define put_user(x,ptr)                                                 \
205   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
206
207
208 /**
209  * __get_user: - Get a simple variable from user space, with less checking.
210  * @x:   Variable to store result.
211  * @ptr: Source address, in user space.
212  *
213  * Context: User context only.  This function may sleep.
214  *
215  * This macro copies a single simple variable from user space to kernel
216  * space.  It supports simple types like char and int, but not larger
217  * data types like structures or arrays.
218  *
219  * @ptr must have pointer-to-simple-variable type, and the result of
220  * dereferencing @ptr must be assignable to @x without a cast.
221  *
222  * Caller must check the pointer with access_ok() before calling this
223  * function.
224  *
225  * Returns zero on success, or -EFAULT on error.
226  * On error, the variable @x is set to zero.
227  */
228 #define __get_user(x,ptr) \
229   __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
230
231
232 /**
233  * __put_user: - Write a simple value into user space, with less checking.
234  * @x:   Value to copy to user space.
235  * @ptr: Destination address, in user space.
236  *
237  * Context: User context only.  This function may sleep.
238  *
239  * This macro copies a single simple value from kernel space to user
240  * space.  It supports simple types like char and int, but not larger
241  * data types like structures or arrays.
242  *
243  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
244  * to the result of dereferencing @ptr.
245  *
246  * Caller must check the pointer with access_ok() before calling this
247  * function.
248  *
249  * Returns zero on success, or -EFAULT on error.
250  */
251 #define __put_user(x,ptr) \
252   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
253
254 #define __put_user_nocheck(x,ptr,size)                          \
255 ({                                                              \
256         long __pu_err;                                          \
257         __put_user_size((x),(ptr),(size),__pu_err,-EFAULT);     \
258         __pu_err;                                               \
259 })
260
261
262 #define __put_user_check(x,ptr,size)                                    \
263 ({                                                                      \
264         long __pu_err = -EFAULT;                                        \
265         __typeof__(*(ptr)) __user *__pu_addr = (ptr);                   \
266         might_sleep();                                          \
267         if (access_ok(VERIFY_WRITE,__pu_addr,size))                     \
268                 __put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT); \
269         __pu_err;                                                       \
270 })                                                      
271
272 #define __put_user_u64(x, addr, err)                            \
273         __asm__ __volatile__(                                   \
274                 "1:     movl %%eax,0(%2)\n"                     \
275                 "2:     movl %%edx,4(%2)\n"                     \
276                 "3:\n"                                          \
277                 ".section .fixup,\"ax\"\n"                      \
278                 "4:     movl %3,%0\n"                           \
279                 "       jmp 3b\n"                               \
280                 ".previous\n"                                   \
281                 ".section __ex_table,\"a\"\n"                   \
282                 "       .align 4\n"                             \
283                 "       .long 1b,4b\n"                          \
284                 "       .long 2b,4b\n"                          \
285                 ".previous"                                     \
286                 : "=r"(err)                                     \
287                 : "A" (x), "r" (addr), "i"(-EFAULT), "0"(err))
288
289 #ifdef CONFIG_X86_WP_WORKS_OK
290
291 #define __put_user_size(x,ptr,size,retval,errret)                       \
292 do {                                                                    \
293         retval = 0;                                                     \
294         __chk_user_ptr(ptr);                                            \
295         switch (size) {                                                 \
296         case 1: __put_user_asm(x,ptr,retval,"b","b","iq",errret);break; \
297         case 2: __put_user_asm(x,ptr,retval,"w","w","ir",errret);break; \
298         case 4: __put_user_asm(x,ptr,retval,"l","","ir",errret); break; \
299         case 8: __put_user_u64((__typeof__(*ptr))(x),ptr,retval); break;\
300           default: __put_user_bad();                                    \
301         }                                                               \
302 } while (0)
303
304 #else
305
306 #define __put_user_size(x,ptr,size,retval,errret)                       \
307 do {                                                                    \
308         __typeof__(*(ptr)) __pus_tmp = x;                               \
309         retval = 0;                                                     \
310                                                                         \
311         if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0))     \
312                 retval = errret;                                        \
313 } while (0)
314
315 #endif
316 struct __large_struct { unsigned long buf[100]; };
317 #define __m(x) (*(struct __large_struct __user *)(x))
318
319 /*
320  * Tell gcc we read from memory instead of writing: this is because
321  * we do not write to any memory gcc knows about, so there are no
322  * aliasing issues.
323  */
324 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
325         __asm__ __volatile__(                                           \
326                 "1:     mov"itype" %"rtype"1,%2\n"                      \
327                 "2:\n"                                                  \
328                 ".section .fixup,\"ax\"\n"                              \
329                 "3:     movl %3,%0\n"                                   \
330                 "       jmp 2b\n"                                       \
331                 ".previous\n"                                           \
332                 ".section __ex_table,\"a\"\n"                           \
333                 "       .align 4\n"                                     \
334                 "       .long 1b,3b\n"                                  \
335                 ".previous"                                             \
336                 : "=r"(err)                                             \
337                 : ltype (x), "m"(__m(addr)), "i"(errret), "0"(err))
338
339
340 #define __get_user_nocheck(x,ptr,size)                          \
341 ({                                                              \
342         long __gu_err;                                          \
343         unsigned long __gu_val;                                 \
344         __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
345         (x) = (__typeof__(*(ptr)))__gu_val;                     \
346         __gu_err;                                               \
347 })
348
349 extern long __get_user_bad(void);
350
351 #define __get_user_size(x,ptr,size,retval,errret)                       \
352 do {                                                                    \
353         retval = 0;                                                     \
354         __chk_user_ptr(ptr);                                            \
355         switch (size) {                                                 \
356         case 1: __get_user_asm(x,ptr,retval,"b","b","=q",errret);break; \
357         case 2: __get_user_asm(x,ptr,retval,"w","w","=r",errret);break; \
358         case 4: __get_user_asm(x,ptr,retval,"l","","=r",errret);break;  \
359         default: (x) = __get_user_bad();                                \
360         }                                                               \
361 } while (0)
362
363 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
364         __asm__ __volatile__(                                           \
365                 "1:     mov"itype" %2,%"rtype"1\n"                      \
366                 "2:\n"                                                  \
367                 ".section .fixup,\"ax\"\n"                              \
368                 "3:     movl %3,%0\n"                                   \
369                 "       xor"itype" %"rtype"1,%"rtype"1\n"               \
370                 "       jmp 2b\n"                                       \
371                 ".previous\n"                                           \
372                 ".section __ex_table,\"a\"\n"                           \
373                 "       .align 4\n"                                     \
374                 "       .long 1b,3b\n"                                  \
375                 ".previous"                                             \
376                 : "=r"(err), ltype (x)                                  \
377                 : "m"(__m(addr)), "i"(errret), "0"(err))
378
379
380 unsigned long __must_check __copy_to_user_ll(void __user *to,
381                                 const void *from, unsigned long n);
382 unsigned long __must_check __copy_from_user_ll(void *to,
383                                 const void __user *from, unsigned long n);
384
385 /*
386  * Here we special-case 1, 2 and 4-byte copy_*_user invocations.  On a fault
387  * we return the initial request size (1, 2 or 4), as copy_*_user should do.
388  * If a store crosses a page boundary and gets a fault, the x86 will not write
389  * anything, so this is accurate.
390  */
391
392 /**
393  * __copy_to_user: - Copy a block of data into user space, with less checking.
394  * @to:   Destination address, in user space.
395  * @from: Source address, in kernel space.
396  * @n:    Number of bytes to copy.
397  *
398  * Context: User context only.  This function may sleep.
399  *
400  * Copy data from kernel space to user space.  Caller must check
401  * the specified block with access_ok() before calling this function.
402  *
403  * Returns number of bytes that could not be copied.
404  * On success, this will be zero.
405  */
406 static inline unsigned long __must_check
407 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
408 {
409         if (__builtin_constant_p(n)) {
410                 unsigned long ret;
411
412                 switch (n) {
413                 case 1:
414                         __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
415                         return ret;
416                 case 2:
417                         __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
418                         return ret;
419                 case 4:
420                         __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
421                         return ret;
422                 }
423         }
424         return __copy_to_user_ll(to, from, n);
425 }
426
427 static inline unsigned long __must_check
428 __copy_to_user(void __user *to, const void *from, unsigned long n)
429 {
430        might_sleep();
431        return __copy_to_user_inatomic(to, from, n);
432 }
433
434 /**
435  * __copy_from_user: - Copy a block of data from user space, with less checking.
436  * @to:   Destination address, in kernel space.
437  * @from: Source address, in user space.
438  * @n:    Number of bytes to copy.
439  *
440  * Context: User context only.  This function may sleep.
441  *
442  * Copy data from user space to kernel space.  Caller must check
443  * the specified block with access_ok() before calling this function.
444  *
445  * Returns number of bytes that could not be copied.
446  * On success, this will be zero.
447  *
448  * If some data could not be copied, this function will pad the copied
449  * data to the requested size using zero bytes.
450  */
451 static inline unsigned long
452 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
453 {
454         if (__builtin_constant_p(n)) {
455                 unsigned long ret;
456
457                 switch (n) {
458                 case 1:
459                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
460                         return ret;
461                 case 2:
462                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
463                         return ret;
464                 case 4:
465                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
466                         return ret;
467                 }
468         }
469         return __copy_from_user_ll(to, from, n);
470 }
471
472 static inline unsigned long
473 __copy_from_user(void *to, const void __user *from, unsigned long n)
474 {
475        might_sleep();
476        return __copy_from_user_inatomic(to, from, n);
477 }
478 unsigned long __must_check copy_to_user(void __user *to,
479                                 const void *from, unsigned long n);
480 unsigned long __must_check copy_from_user(void *to,
481                                 const void __user *from, unsigned long n);
482 long __must_check strncpy_from_user(char *dst, const char __user *src,
483                                 long count);
484 long __must_check __strncpy_from_user(char *dst,
485                                 const char __user *src, long count);
486
487 /**
488  * strlen_user: - Get the size of a string in user space.
489  * @str: The string to measure.
490  *
491  * Context: User context only.  This function may sleep.
492  *
493  * Get the size of a NUL-terminated string in user space.
494  *
495  * Returns the size of the string INCLUDING the terminating NUL.
496  * On exception, returns 0.
497  *
498  * If there is a limit on the length of a valid string, you may wish to
499  * consider using strnlen_user() instead.
500  */
501 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
502
503 long strnlen_user(const char __user *str, long n);
504 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
505 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
506
507 #endif /* __i386_UACCESS_H */