v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / include / asm-arm / uaccess.h
1 #ifndef _ASMARM_UACCESS_H
2 #define _ASMARM_UACCESS_H
3
4 /*
5  * User space memory access functions
6  */
7 #include <linux/sched.h>
8 #include <asm/errno.h>
9
10 #define VERIFY_READ 0
11 #define VERIFY_WRITE 1
12
13 /*
14  * The exception table consists of pairs of addresses: the first is the
15  * address of an instruction that is allowed to fault, and the second is
16  * the address at which the program should continue.  No registers are
17  * modified, so it is entirely up to the continuation code to figure out
18  * what to do.
19  *
20  * All the routines below use bits of fixup code that are out of line
21  * with the main instruction path.  This means when everything is well,
22  * we don't even have to jump over them.  Further, they do not intrude
23  * on our cache or tlb entries.
24  */
25
26 struct exception_table_entry
27 {
28         unsigned long insn, fixup;
29 };
30
31 /* Returns 0 if exception not found and fixup otherwise.  */
32 extern unsigned long search_exception_table(unsigned long);
33
34 #define get_ds()        (KERNEL_DS)
35 #define get_fs()        (current->addr_limit)
36 #define segment_eq(a,b) ((a) == (b))
37
38 #include <asm/proc/uaccess.h>
39
40 #define access_ok(type,addr,size)       (__range_ok(addr,size) == 0)
41
42 static inline int verify_area(int type, const void * addr, unsigned long size)
43 {
44         return access_ok(type, addr, size) ? 0 : -EFAULT;
45 }
46
47 /*
48  * Single-value transfer routines.  They automatically use the right
49  * size if we just have the right pointer type.  Note that the functions
50  * which read from user space (*get_*) need to take care not to leak
51  * kernel data even if the calling code is buggy and fails to check
52  * the return value.  This means zeroing out the destination variable
53  * or buffer on error.  Normally this is done out of line by the
54  * fixup code, but there are a few places where it intrudes on the
55  * main code path.  When we only write to user space, there is no
56  * problem.
57  *
58  * The "__xxx" versions of the user access functions do not verify the
59  * address space - it must have been done previously with a separate
60  * "access_ok()" call.
61  *
62  * The "xxx_error" versions set the third argument to EFAULT if an
63  * error occurs, and leave it unchanged on success.  Note that these
64  * versions are void (ie, don't return a value as such).
65  */
66 #define get_user(x,p)           __get_user_check((x),(p),sizeof(*(p)))
67 #define __get_user(x,p)         __get_user_nocheck((x),(p),sizeof(*(p)))
68 #define __get_user_error(x,p,e) __get_user_nocheck_error((x),(p),sizeof(*(p)),(e))
69
70 #define put_user(x,p)           __put_user_check((__typeof(*(p)))(x),(p),sizeof(*(p)))
71 #define __put_user(x,p)         __put_user_nocheck((__typeof(*(p)))(x),(p),sizeof(*(p)))
72 #define __put_user_error(x,p,e) __put_user_nocheck_error((x),(p),sizeof(*(p)),(e))
73
74 static __inline__ unsigned long copy_from_user(void *to, const void *from, unsigned long n)
75 {
76         if (access_ok(VERIFY_READ, from, n))
77                 __do_copy_from_user(to, from, n);
78         else /* security hole - plug it */
79                 memzero(to, n);
80         return n;
81 }
82
83 static __inline__ unsigned long __copy_from_user(void *to, const void *from, unsigned long n)
84 {
85         __do_copy_from_user(to, from, n);
86         return n;
87 }
88
89 static __inline__ unsigned long copy_to_user(void *to, const void *from, unsigned long n)
90 {
91         if (access_ok(VERIFY_WRITE, to, n))
92                 __do_copy_to_user(to, from, n);
93         return n;
94 }
95
96 static __inline__ unsigned long __copy_to_user(void *to, const void *from, unsigned long n)
97 {
98         __do_copy_to_user(to, from, n);
99         return n;
100 }
101
102 static __inline__ unsigned long clear_user (void *to, unsigned long n)
103 {
104         if (access_ok(VERIFY_WRITE, to, n))
105                 __do_clear_user(to, n);
106         return n;
107 }
108
109 static __inline__ unsigned long __clear_user (void *to, unsigned long n)
110 {
111         __do_clear_user(to, n);
112         return n;
113 }
114
115 static __inline__ long strncpy_from_user (char *dst, const char *src, long count)
116 {
117         long res = -EFAULT;
118         if (access_ok(VERIFY_READ, src, 1))
119                 __do_strncpy_from_user(dst, src, count, res);
120         return res;
121 }
122
123 static __inline__ long __strncpy_from_user (char *dst, const char *src, long count)
124 {
125         long res;
126         __do_strncpy_from_user(dst, src, count, res);
127         return res;
128 }
129
130 #define strlen_user(s)  strnlen_user(s, ~0UL >> 1)
131
132 static inline long strnlen_user(const char *s, long n)
133 {
134         unsigned long res = 0;
135
136         if (__addr_ok(s))
137                 __do_strnlen_user(s, n, res);
138
139         return res;
140 }
141
142 /*
143  * These are the work horses of the get/put_user functions
144  */
145 #define __get_user_check(x,ptr,size)                                    \
146 ({                                                                      \
147         long __gu_err = -EFAULT, __gu_val = 0;                          \
148         const __typeof__(*(ptr)) *__gu_addr = (ptr);                    \
149         if (access_ok(VERIFY_READ,__gu_addr,size)) {                    \
150                 __gu_err = 0;                                           \
151                 __get_user_size(__gu_val,__gu_addr,(size),__gu_err);    \
152         }                                                               \
153         (x) = (__typeof__(*(ptr)))__gu_val;                             \
154         __gu_err;                                                       \
155 })
156
157 #define __get_user_nocheck(x,ptr,size)                                  \
158 ({                                                                      \
159         long __gu_err = 0, __gu_val;                                    \
160         __get_user_size(__gu_val,(ptr),(size),__gu_err);                \
161         (x) = (__typeof__(*(ptr)))__gu_val;                             \
162         __gu_err;                                                       \
163 })
164
165 #define __get_user_nocheck_error(x,ptr,size,err)                        \
166 ({                                                                      \
167         long __gu_val;                                                  \
168         __get_user_size(__gu_val,(ptr),(size),(err));                   \
169         (x) = (__typeof__(*(ptr)))__gu_val;                             \
170         (void) 0;                                                       \
171 })
172
173 #define __put_user_check(x,ptr,size)                                    \
174 ({                                                                      \
175         long __pu_err = -EFAULT;                                        \
176         __typeof__(*(ptr)) *__pu_addr = (ptr);                          \
177         if (access_ok(VERIFY_WRITE,__pu_addr,size)) {                   \
178                 __pu_err = 0;                                           \
179                 __put_user_size((x),__pu_addr,(size),__pu_err);         \
180         }                                                               \
181         __pu_err;                                                       \
182 })
183
184 #define __put_user_nocheck(x,ptr,size)                                  \
185 ({                                                                      \
186         long __pu_err = 0;                                              \
187         __typeof__(*(ptr)) *__pu_addr = (ptr);                          \
188         __put_user_size((x),__pu_addr,(size),__pu_err);                 \
189         __pu_err;                                                       \
190 })
191
192 #define __put_user_nocheck_error(x,ptr,size,err)                        \
193 ({                                                                      \
194         __put_user_size((x),(ptr),(size),err);                          \
195         (void) 0;                                                       \
196 })
197
198 extern long __get_user_bad(void);
199
200 #define __get_user_size(x,ptr,size,retval)                              \
201 do {                                                                    \
202         switch (size) {                                                 \
203         case 1: __get_user_asm_byte(x,ptr,retval);      break;          \
204         case 2: __get_user_asm_half(x,ptr,retval);      break;          \
205         case 4: __get_user_asm_word(x,ptr,retval);      break;          \
206         default: (x) = __get_user_bad();                                \
207         }                                                               \
208 } while (0)
209
210 extern long __put_user_bad(void);
211
212 #define __put_user_size(x,ptr,size,retval)                              \
213 do {                                                                    \
214         switch (size) {                                                 \
215         case 1: __put_user_asm_byte(x,ptr,retval);      break;          \
216         case 2: __put_user_asm_half(x,ptr,retval);      break;          \
217         case 4: __put_user_asm_word(x,ptr,retval);      break;          \
218         default: __put_user_bad();                                      \
219         }                                                               \
220 } while (0)
221
222 #endif /* _ASMARM_UACCESS_H */