Merge master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / include / asm-arm / uaccess.h
1 /*
2  *  linux/include/asm-arm/uaccess.h
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #ifndef _ASMARM_UACCESS_H
9 #define _ASMARM_UACCESS_H
10
11 /*
12  * User space memory access functions
13  */
14 #include <linux/sched.h>
15 #include <asm/errno.h>
16 #include <asm/memory.h>
17 #include <asm/domain.h>
18 #include <asm/system.h>
19
20 #define VERIFY_READ 0
21 #define VERIFY_WRITE 1
22
23 /*
24  * The exception table consists of pairs of addresses: the first is the
25  * address of an instruction that is allowed to fault, and the second is
26  * the address at which the program should continue.  No registers are
27  * modified, so it is entirely up to the continuation code to figure out
28  * what to do.
29  *
30  * All the routines below use bits of fixup code that are out of line
31  * with the main instruction path.  This means when everything is well,
32  * we don't even have to jump over them.  Further, they do not intrude
33  * on our cache or tlb entries.
34  */
35
36 struct exception_table_entry
37 {
38         unsigned long insn, fixup;
39 };
40
41 extern int fixup_exception(struct pt_regs *regs);
42
43 /*
44  * Note that this is actually 0x1,0000,0000
45  */
46 #define KERNEL_DS       0x00000000
47 #define USER_DS         TASK_SIZE
48
49 #define get_ds()        (KERNEL_DS)
50 #define get_fs()        (current_thread_info()->addr_limit)
51
52 static inline void set_fs (mm_segment_t fs)
53 {
54         current_thread_info()->addr_limit = fs;
55         modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER);
56 }
57
58 #define segment_eq(a,b) ((a) == (b))
59
60 #define __addr_ok(addr) ({ \
61         unsigned long flag; \
62         __asm__("cmp %2, %0; movlo %0, #0" \
63                 : "=&r" (flag) \
64                 : "0" (current_thread_info()->addr_limit), "r" (addr) \
65                 : "cc"); \
66         (flag == 0); })
67
68 /* We use 33-bit arithmetic here... */
69 #define __range_ok(addr,size) ({ \
70         unsigned long flag, sum; \
71         __chk_user_ptr(addr);   \
72         __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
73                 : "=&r" (flag), "=&r" (sum) \
74                 : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
75                 : "cc"); \
76         flag; })
77
78 #define access_ok(type,addr,size)       (__range_ok(addr,size) == 0)
79
80 /*
81  * Single-value transfer routines.  They automatically use the right
82  * size if we just have the right pointer type.  Note that the functions
83  * which read from user space (*get_*) need to take care not to leak
84  * kernel data even if the calling code is buggy and fails to check
85  * the return value.  This means zeroing out the destination variable
86  * or buffer on error.  Normally this is done out of line by the
87  * fixup code, but there are a few places where it intrudes on the
88  * main code path.  When we only write to user space, there is no
89  * problem.
90  *
91  * The "__xxx" versions of the user access functions do not verify the
92  * address space - it must have been done previously with a separate
93  * "access_ok()" call.
94  *
95  * The "xxx_error" versions set the third argument to EFAULT if an
96  * error occurs, and leave it unchanged on success.  Note that these
97  * versions are void (ie, don't return a value as such).
98  */
99
100 extern int __get_user_1(void *);
101 extern int __get_user_2(void *);
102 extern int __get_user_4(void *);
103 extern int __get_user_8(void *);
104 extern int __get_user_bad(void);
105
106 #define __get_user_x(__r2,__p,__e,__s,__i...)                           \
107            __asm__ __volatile__ (                                       \
108                 __asmeq("%0", "r0") __asmeq("%1", "r2")                 \
109                 "bl     __get_user_" #__s                               \
110                 : "=&r" (__e), "=r" (__r2)                              \
111                 : "0" (__p)                                             \
112                 : __i, "cc")
113
114 #define get_user(x,p)                                                   \
115         ({                                                              \
116                 const register typeof(*(p)) __user *__p asm("r0") = (p);\
117                 register typeof(*(p)) __r2 asm("r2");                   \
118                 register int __e asm("r0");                             \
119                 switch (sizeof(*(__p))) {                               \
120                 case 1:                                                 \
121                         __get_user_x(__r2, __p, __e, 1, "lr");          \
122                         break;                                          \
123                 case 2:                                                 \
124                         __get_user_x(__r2, __p, __e, 2, "r3", "lr");    \
125                         break;                                          \
126                 case 4:                                                 \
127                         __get_user_x(__r2, __p, __e, 4, "lr");          \
128                         break;                                          \
129                 case 8:                                                 \
130                         __get_user_x(__r2, __p, __e, 8, "lr");          \
131                         break;                                          \
132                 default: __e = __get_user_bad(); break;                 \
133                 }                                                       \
134                 x = __r2;                                               \
135                 __e;                                                    \
136         })
137
138 #define __get_user(x,ptr)                                               \
139 ({                                                                      \
140         long __gu_err = 0;                                              \
141         __get_user_err((x),(ptr),__gu_err);                             \
142         __gu_err;                                                       \
143 })
144
145 #define __get_user_error(x,ptr,err)                                     \
146 ({                                                                      \
147         __get_user_err((x),(ptr),err);                                  \
148         (void) 0;                                                       \
149 })
150
151 #define __get_user_err(x,ptr,err)                                       \
152 do {                                                                    \
153         unsigned long __gu_addr = (unsigned long)(ptr);                 \
154         unsigned long __gu_val;                                         \
155         __chk_user_ptr(ptr);                                            \
156         switch (sizeof(*(ptr))) {                                       \
157         case 1: __get_user_asm_byte(__gu_val,__gu_addr,err);    break;  \
158         case 2: __get_user_asm_half(__gu_val,__gu_addr,err);    break;  \
159         case 4: __get_user_asm_word(__gu_val,__gu_addr,err);    break;  \
160         default: (__gu_val) = __get_user_bad();                         \
161         }                                                               \
162         (x) = (__typeof__(*(ptr)))__gu_val;                             \
163 } while (0)
164
165 #define __get_user_asm_byte(x,addr,err)                         \
166         __asm__ __volatile__(                                   \
167         "1:     ldrbt   %1,[%2],#0\n"                           \
168         "2:\n"                                                  \
169         "       .section .fixup,\"ax\"\n"                       \
170         "       .align  2\n"                                    \
171         "3:     mov     %0, %3\n"                               \
172         "       mov     %1, #0\n"                               \
173         "       b       2b\n"                                   \
174         "       .previous\n"                                    \
175         "       .section __ex_table,\"a\"\n"                    \
176         "       .align  3\n"                                    \
177         "       .long   1b, 3b\n"                               \
178         "       .previous"                                      \
179         : "+r" (err), "=&r" (x)                                 \
180         : "r" (addr), "i" (-EFAULT)                             \
181         : "cc")
182
183 #ifndef __ARMEB__
184 #define __get_user_asm_half(x,__gu_addr,err)                    \
185 ({                                                              \
186         unsigned long __b1, __b2;                               \
187         __get_user_asm_byte(__b1, __gu_addr, err);              \
188         __get_user_asm_byte(__b2, __gu_addr + 1, err);          \
189         (x) = __b1 | (__b2 << 8);                               \
190 })
191 #else
192 #define __get_user_asm_half(x,__gu_addr,err)                    \
193 ({                                                              \
194         unsigned long __b1, __b2;                               \
195         __get_user_asm_byte(__b1, __gu_addr, err);              \
196         __get_user_asm_byte(__b2, __gu_addr + 1, err);          \
197         (x) = (__b1 << 8) | __b2;                               \
198 })
199 #endif
200
201 #define __get_user_asm_word(x,addr,err)                         \
202         __asm__ __volatile__(                                   \
203         "1:     ldrt    %1,[%2],#0\n"                           \
204         "2:\n"                                                  \
205         "       .section .fixup,\"ax\"\n"                       \
206         "       .align  2\n"                                    \
207         "3:     mov     %0, %3\n"                               \
208         "       mov     %1, #0\n"                               \
209         "       b       2b\n"                                   \
210         "       .previous\n"                                    \
211         "       .section __ex_table,\"a\"\n"                    \
212         "       .align  3\n"                                    \
213         "       .long   1b, 3b\n"                               \
214         "       .previous"                                      \
215         : "+r" (err), "=&r" (x)                                 \
216         : "r" (addr), "i" (-EFAULT)                             \
217         : "cc")
218
219 extern int __put_user_1(void *, unsigned int);
220 extern int __put_user_2(void *, unsigned int);
221 extern int __put_user_4(void *, unsigned int);
222 extern int __put_user_8(void *, unsigned long long);
223 extern int __put_user_bad(void);
224
225 #define __put_user_x(__r2,__p,__e,__s)                                  \
226            __asm__ __volatile__ (                                       \
227                 __asmeq("%0", "r0") __asmeq("%2", "r2")                 \
228                 "bl     __put_user_" #__s                               \
229                 : "=&r" (__e)                                           \
230                 : "0" (__p), "r" (__r2)                                 \
231                 : "ip", "lr", "cc")
232
233 #define put_user(x,p)                                                   \
234         ({                                                              \
235                 const register typeof(*(p)) __r2 asm("r2") = (x);       \
236                 const register typeof(*(p)) __user *__p asm("r0") = (p);\
237                 register int __e asm("r0");                             \
238                 switch (sizeof(*(__p))) {                               \
239                 case 1:                                                 \
240                         __put_user_x(__r2, __p, __e, 1);                \
241                         break;                                          \
242                 case 2:                                                 \
243                         __put_user_x(__r2, __p, __e, 2);                \
244                         break;                                          \
245                 case 4:                                                 \
246                         __put_user_x(__r2, __p, __e, 4);                \
247                         break;                                          \
248                 case 8:                                                 \
249                         __put_user_x(__r2, __p, __e, 8);                \
250                         break;                                          \
251                 default: __e = __put_user_bad(); break;                 \
252                 }                                                       \
253                 __e;                                                    \
254         })
255
256 #define __put_user(x,ptr)                                               \
257 ({                                                                      \
258         long __pu_err = 0;                                              \
259         __put_user_err((x),(ptr),__pu_err);                             \
260         __pu_err;                                                       \
261 })
262
263 #define __put_user_error(x,ptr,err)                                     \
264 ({                                                                      \
265         __put_user_err((x),(ptr),err);                                  \
266         (void) 0;                                                       \
267 })
268
269 #define __put_user_err(x,ptr,err)                                       \
270 do {                                                                    \
271         unsigned long __pu_addr = (unsigned long)(ptr);                 \
272         __typeof__(*(ptr)) __pu_val = (x);                              \
273         __chk_user_ptr(ptr);                                            \
274         switch (sizeof(*(ptr))) {                                       \
275         case 1: __put_user_asm_byte(__pu_val,__pu_addr,err);    break;  \
276         case 2: __put_user_asm_half(__pu_val,__pu_addr,err);    break;  \
277         case 4: __put_user_asm_word(__pu_val,__pu_addr,err);    break;  \
278         case 8: __put_user_asm_dword(__pu_val,__pu_addr,err);   break;  \
279         default: __put_user_bad();                                      \
280         }                                                               \
281 } while (0)
282
283 #define __put_user_asm_byte(x,__pu_addr,err)                    \
284         __asm__ __volatile__(                                   \
285         "1:     strbt   %1,[%2],#0\n"                           \
286         "2:\n"                                                  \
287         "       .section .fixup,\"ax\"\n"                       \
288         "       .align  2\n"                                    \
289         "3:     mov     %0, %3\n"                               \
290         "       b       2b\n"                                   \
291         "       .previous\n"                                    \
292         "       .section __ex_table,\"a\"\n"                    \
293         "       .align  3\n"                                    \
294         "       .long   1b, 3b\n"                               \
295         "       .previous"                                      \
296         : "+r" (err)                                            \
297         : "r" (x), "r" (__pu_addr), "i" (-EFAULT)               \
298         : "cc")
299
300 #ifndef __ARMEB__
301 #define __put_user_asm_half(x,__pu_addr,err)                    \
302 ({                                                              \
303         unsigned long __temp = (unsigned long)(x);              \
304         __put_user_asm_byte(__temp, __pu_addr, err);            \
305         __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err);   \
306 })
307 #else
308 #define __put_user_asm_half(x,__pu_addr,err)                    \
309 ({                                                              \
310         unsigned long __temp = (unsigned long)(x);              \
311         __put_user_asm_byte(__temp >> 8, __pu_addr, err);       \
312         __put_user_asm_byte(__temp, __pu_addr + 1, err);        \
313 })
314 #endif
315
316 #define __put_user_asm_word(x,__pu_addr,err)                    \
317         __asm__ __volatile__(                                   \
318         "1:     strt    %1,[%2],#0\n"                           \
319         "2:\n"                                                  \
320         "       .section .fixup,\"ax\"\n"                       \
321         "       .align  2\n"                                    \
322         "3:     mov     %0, %3\n"                               \
323         "       b       2b\n"                                   \
324         "       .previous\n"                                    \
325         "       .section __ex_table,\"a\"\n"                    \
326         "       .align  3\n"                                    \
327         "       .long   1b, 3b\n"                               \
328         "       .previous"                                      \
329         : "+r" (err)                                            \
330         : "r" (x), "r" (__pu_addr), "i" (-EFAULT)               \
331         : "cc")
332
333 #ifndef __ARMEB__
334 #define __reg_oper0     "%R2"
335 #define __reg_oper1     "%Q2"
336 #else
337 #define __reg_oper0     "%Q2"
338 #define __reg_oper1     "%R2"
339 #endif
340
341 #define __put_user_asm_dword(x,__pu_addr,err)                   \
342         __asm__ __volatile__(                                   \
343         "1:     strt    " __reg_oper1 ", [%1], #4\n"            \
344         "2:     strt    " __reg_oper0 ", [%1], #0\n"            \
345         "3:\n"                                                  \
346         "       .section .fixup,\"ax\"\n"                       \
347         "       .align  2\n"                                    \
348         "4:     mov     %0, %3\n"                               \
349         "       b       3b\n"                                   \
350         "       .previous\n"                                    \
351         "       .section __ex_table,\"a\"\n"                    \
352         "       .align  3\n"                                    \
353         "       .long   1b, 4b\n"                               \
354         "       .long   2b, 4b\n"                               \
355         "       .previous"                                      \
356         : "+r" (err), "+r" (__pu_addr)                          \
357         : "r" (x), "i" (-EFAULT)                                \
358         : "cc")
359
360 extern unsigned long __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
361 extern unsigned long __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
362 extern unsigned long __arch_clear_user(void __user *addr, unsigned long n);
363 extern unsigned long __arch_strncpy_from_user(char *to, const char __user *from, unsigned long count);
364 extern unsigned long __arch_strnlen_user(const char __user *s, long n);
365
366 static inline unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)
367 {
368         if (access_ok(VERIFY_READ, from, n))
369                 n = __arch_copy_from_user(to, from, n);
370         else /* security hole - plug it */
371                 memzero(to, n);
372         return n;
373 }
374
375 static inline unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n)
376 {
377         return __arch_copy_from_user(to, from, n);
378 }
379
380 static inline unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)
381 {
382         if (access_ok(VERIFY_WRITE, to, n))
383                 n = __arch_copy_to_user(to, from, n);
384         return n;
385 }
386
387 static inline unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
388 {
389         return __arch_copy_to_user(to, from, n);
390 }
391
392 #define __copy_to_user_inatomic __copy_to_user
393 #define __copy_from_user_inatomic __copy_from_user
394
395 static inline unsigned long clear_user (void __user *to, unsigned long n)
396 {
397         if (access_ok(VERIFY_WRITE, to, n))
398                 n = __arch_clear_user(to, n);
399         return n;
400 }
401
402 static inline unsigned long __clear_user (void __user *to, unsigned long n)
403 {
404         return __arch_clear_user(to, n);
405 }
406
407 static inline long strncpy_from_user (char *dst, const char __user *src, long count)
408 {
409         long res = -EFAULT;
410         if (access_ok(VERIFY_READ, src, 1))
411                 res = __arch_strncpy_from_user(dst, src, count);
412         return res;
413 }
414
415 static inline long __strncpy_from_user (char *dst, const char __user *src, long count)
416 {
417         return __arch_strncpy_from_user(dst, src, count);
418 }
419
420 #define strlen_user(s)  strnlen_user(s, ~0UL >> 1)
421
422 static inline long strnlen_user(const char __user *s, long n)
423 {
424         unsigned long res = 0;
425
426         if (__addr_ok(s))
427                 res = __arch_strnlen_user(s, n);
428
429         return res;
430 }
431
432 #endif /* _ASMARM_UACCESS_H */