x86: merge getuser.
[pandora-kernel.git] / include / asm-x86 / uaccess.h
1 #ifndef _ASM_UACCES_H_
2 #define _ASM_UACCES_H_
3 /*
4  * User space memory access functions
5  */
6 #include <linux/errno.h>
7 #include <linux/compiler.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 #define KERNEL_DS       MAKE_MM_SEG(-1UL)
28 #define USER_DS         MAKE_MM_SEG(PAGE_OFFSET)
29
30 #define get_ds()        (KERNEL_DS)
31 #define get_fs()        (current_thread_info()->addr_limit)
32 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
33
34 #define segment_eq(a, b)        ((a).seg == (b).seg)
35
36 /*
37  * Test whether a block of memory is a valid user space address.
38  * Returns 0 if the range is valid, nonzero otherwise.
39  *
40  * This is equivalent to the following test:
41  * (u33)addr + (u33)size >= (u33)current->addr_limit.seg (u65 for x86_64)
42  *
43  * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry...
44  */
45
46 #define __range_not_ok(addr, size)                                      \
47 ({                                                                      \
48         unsigned long flag, roksum;                                     \
49         __chk_user_ptr(addr);                                           \
50         asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0"             \
51             : "=&r" (flag), "=r" (roksum)                               \
52             : "1" (addr), "g" ((long)(size)),                           \
53               "rm" (current_thread_info()->addr_limit.seg));            \
54         flag;                                                           \
55 })
56
57 /**
58  * access_ok: - Checks if a user space pointer is valid
59  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
60  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
61  *        to write to a block, it is always safe to read from it.
62  * @addr: User space pointer to start of block to check
63  * @size: Size of block to check
64  *
65  * Context: User context only.  This function may sleep.
66  *
67  * Checks if a pointer to a block of memory in user space is valid.
68  *
69  * Returns true (nonzero) if the memory block may be valid, false (zero)
70  * if it is definitely invalid.
71  *
72  * Note that, depending on architecture, this function probably just
73  * checks that the pointer is in the user space range - after calling
74  * this function, memory access functions may still return -EFAULT.
75  */
76 #define access_ok(type, addr, size) (likely(__range_not_ok(addr, size) == 0))
77
78 /*
79  * The exception table consists of pairs of addresses: the first is the
80  * address of an instruction that is allowed to fault, and the second is
81  * the address at which the program should continue.  No registers are
82  * modified, so it is entirely up to the continuation code to figure out
83  * what to do.
84  *
85  * All the routines below use bits of fixup code that are out of line
86  * with the main instruction path.  This means when everything is well,
87  * we don't even have to jump over them.  Further, they do not intrude
88  * on our cache or tlb entries.
89  */
90
91 struct exception_table_entry {
92         unsigned long insn, fixup;
93 };
94
95 extern int fixup_exception(struct pt_regs *regs);
96
97 /*
98  * These are the main single-value transfer routines.  They automatically
99  * use the right size if we just have the right pointer type.
100  *
101  * This gets kind of ugly. We want to return _two_ values in "get_user()"
102  * and yet we don't want to do any pointers, because that is too much
103  * of a performance impact. Thus we have a few rather ugly macros here,
104  * and hide all the ugliness from the user.
105  *
106  * The "__xxx" versions of the user access functions are versions that
107  * do not verify the address space, that must have been done previously
108  * with a separate "access_ok()" call (this is used when we do multiple
109  * accesses to the same area of user memory).
110  */
111
112 extern int __get_user_1(void);
113 extern int __get_user_2(void);
114 extern int __get_user_4(void);
115 extern int __get_user_8(void);
116 extern int __get_user_bad(void);
117
118 #define __get_user_x(size, ret, x, ptr)               \
119         asm volatile("call __get_user_" #size         \
120                      : "=a" (ret),"=d" (x)            \
121                      : "0" (ptr))                     \
122
123 /* Careful: we have to cast the result to the type of the pointer
124  * for sign reasons */
125
126 /**
127  * get_user: - Get a simple variable from user space.
128  * @x:   Variable to store result.
129  * @ptr: Source address, in user space.
130  *
131  * Context: User context only.  This function may sleep.
132  *
133  * This macro copies a single simple variable from user space to kernel
134  * space.  It supports simple types like char and int, but not larger
135  * data types like structures or arrays.
136  *
137  * @ptr must have pointer-to-simple-variable type, and the result of
138  * dereferencing @ptr must be assignable to @x without a cast.
139  *
140  * Returns zero on success, or -EFAULT on error.
141  * On error, the variable @x is set to zero.
142  */
143 #ifdef CONFIG_X86_32
144 #define __get_user_8(__ret_gu, __val_gu, ptr)                           \
145                 __get_user_x(X, __ret_gu, __val_gu, ptr)
146 #else
147 #define __get_user_8(__ret_gu, __val_gu, ptr)                           \
148                 __get_user_x(8, __ret_gu, __val_gu, ptr)
149 #endif
150
151 #define get_user(x, ptr)                                                \
152 ({                                                                      \
153         int __ret_gu;                                                   \
154         unsigned long __val_gu;                                         \
155         __chk_user_ptr(ptr);                                            \
156         switch (sizeof(*(ptr))) {                                       \
157         case 1:                                                         \
158                 __get_user_x(1, __ret_gu, __val_gu, ptr);               \
159                 break;                                                  \
160         case 2:                                                         \
161                 __get_user_x(2, __ret_gu, __val_gu, ptr);               \
162                 break;                                                  \
163         case 4:                                                         \
164                 __get_user_x(4, __ret_gu, __val_gu, ptr);               \
165                 break;                                                  \
166         case 8:                                                         \
167                 __get_user_8(__ret_gu, __val_gu, ptr);                  \
168                 break;                                                  \
169         default:                                                        \
170                 __get_user_x(X, __ret_gu, __val_gu, ptr);               \
171                 break;                                                  \
172         }                                                               \
173         (x) = (__typeof__(*(ptr)))__val_gu;                             \
174         __ret_gu;                                                       \
175 })
176
177
178 #ifdef CONFIG_X86_32
179 # include "uaccess_32.h"
180 #else
181 # include "uaccess_64.h"
182 #endif
183
184 #endif