vserver 1.9.5.x5
[linux-2.6.git] / include / asm-alpha / uaccess.h
1 #ifndef __ALPHA_UACCESS_H
2 #define __ALPHA_UACCESS_H
3
4 #include <linux/errno.h>
5 #include <linux/sched.h>
6
7
8 /*
9  * The fs value determines whether argument validity checking should be
10  * performed or not.  If get_fs() == USER_DS, checking is performed, with
11  * get_fs() == KERNEL_DS, checking is bypassed.
12  *
13  * Or at least it did once upon a time.  Nowadays it is a mask that
14  * defines which bits of the address space are off limits.  This is a
15  * wee bit faster than the above.
16  *
17  * For historical reasons, these macros are grossly misnamed.
18  */
19
20 #define KERNEL_DS       ((mm_segment_t) { 0UL })
21 #define USER_DS         ((mm_segment_t) { -0x40000000000UL })
22
23 #define VERIFY_READ     0
24 #define VERIFY_WRITE    1
25
26 #define get_fs()  (current_thread_info()->addr_limit)
27 #define get_ds()  (KERNEL_DS)
28 #define set_fs(x) (current_thread_info()->addr_limit = (x))
29
30 #define segment_eq(a,b) ((a).seg == (b).seg)
31
32 /*
33  * Is a address valid? This does a straightforward calculation rather
34  * than tests.
35  *
36  * Address valid if:
37  *  - "addr" doesn't have any high-bits set
38  *  - AND "size" doesn't have any high-bits set
39  *  - AND "addr+size" doesn't have any high-bits set
40  *  - OR we are in kernel mode.
41  */
42 #define __access_ok(addr,size,segment) \
43         (((segment).seg & (addr | size | (addr+size))) == 0)
44
45 #define access_ok(type,addr,size)                               \
46 ({                                                              \
47         __chk_user_ptr(addr);                                   \
48         __access_ok(((unsigned long)(addr)),(size),get_fs());   \
49 })
50
51 extern inline int verify_area(int type, const void __user * addr, unsigned long size)
52 {
53         return access_ok(type,addr,size) ? 0 : -EFAULT;
54 }
55
56 /*
57  * These are the main single-value transfer routines.  They automatically
58  * use the right size if we just have the right pointer type.
59  *
60  * As the alpha uses the same address space for kernel and user
61  * data, we can just do these as direct assignments.  (Of course, the
62  * exception handling means that it's no longer "just"...)
63  *
64  * Careful to not
65  * (a) re-use the arguments for side effects (sizeof/typeof is ok)
66  * (b) require any knowledge of processes at this stage
67  */
68 #define put_user(x,ptr) \
69   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)),get_fs())
70 #define get_user(x,ptr) \
71   __get_user_check((x),(ptr),sizeof(*(ptr)),get_fs())
72
73 /*
74  * The "__xxx" versions do not do address space checking, useful when
75  * doing multiple accesses to the same area (the programmer has to do the
76  * checks by hand with "access_ok()")
77  */
78 #define __put_user(x,ptr) \
79   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
80 #define __get_user(x,ptr) \
81   __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
82   
83 /*
84  * The "lda %1, 2b-1b(%0)" bits are magic to get the assembler to
85  * encode the bits we need for resolving the exception.  See the
86  * more extensive comments with fixup_inline_exception below for
87  * more information.
88  */
89
90 extern void __get_user_unknown(void);
91
92 #define __get_user_nocheck(x,ptr,size)                          \
93 ({                                                              \
94         long __gu_err = 0;                                      \
95         unsigned long __gu_val;                                 \
96         __chk_user_ptr(ptr);                                    \
97         switch (size) {                                         \
98           case 1: __get_user_8(ptr); break;                     \
99           case 2: __get_user_16(ptr); break;                    \
100           case 4: __get_user_32(ptr); break;                    \
101           case 8: __get_user_64(ptr); break;                    \
102           default: __get_user_unknown(); break;                 \
103         }                                                       \
104         (x) = (__typeof__(*(ptr))) __gu_val;                    \
105         __gu_err;                                               \
106 })
107
108 #define __get_user_check(x,ptr,size,segment)                            \
109 ({                                                                      \
110         long __gu_err = -EFAULT;                                        \
111         unsigned long __gu_val = 0;                                     \
112         const __typeof__(*(ptr)) __user *__gu_addr = (ptr);             \
113         if (__access_ok((unsigned long)__gu_addr,size,segment)) {       \
114                 __gu_err = 0;                                           \
115                 switch (size) {                                         \
116                   case 1: __get_user_8(__gu_addr); break;               \
117                   case 2: __get_user_16(__gu_addr); break;              \
118                   case 4: __get_user_32(__gu_addr); break;              \
119                   case 8: __get_user_64(__gu_addr); break;              \
120                   default: __get_user_unknown(); break;                 \
121                 }                                                       \
122         }                                                               \
123         (x) = (__typeof__(*(ptr))) __gu_val;                            \
124         __gu_err;                                                       \
125 })
126
127 struct __large_struct { unsigned long buf[100]; };
128 #define __m(x) (*(struct __large_struct __user *)(x))
129
130 #define __get_user_64(addr)                             \
131         __asm__("1: ldq %0,%2\n"                        \
132         "2:\n"                                          \
133         ".section __ex_table,\"a\"\n"                   \
134         "       .long 1b - .\n"                         \
135         "       lda %0, 2b-1b(%1)\n"                    \
136         ".previous"                                     \
137                 : "=r"(__gu_val), "=r"(__gu_err)        \
138                 : "m"(__m(addr)), "1"(__gu_err))
139
140 #define __get_user_32(addr)                             \
141         __asm__("1: ldl %0,%2\n"                        \
142         "2:\n"                                          \
143         ".section __ex_table,\"a\"\n"                   \
144         "       .long 1b - .\n"                         \
145         "       lda %0, 2b-1b(%1)\n"                    \
146         ".previous"                                     \
147                 : "=r"(__gu_val), "=r"(__gu_err)        \
148                 : "m"(__m(addr)), "1"(__gu_err))
149
150 #ifdef __alpha_bwx__
151 /* Those lucky bastards with ev56 and later CPUs can do byte/word moves.  */
152
153 #define __get_user_16(addr)                             \
154         __asm__("1: ldwu %0,%2\n"                       \
155         "2:\n"                                          \
156         ".section __ex_table,\"a\"\n"                   \
157         "       .long 1b - .\n"                         \
158         "       lda %0, 2b-1b(%1)\n"                    \
159         ".previous"                                     \
160                 : "=r"(__gu_val), "=r"(__gu_err)        \
161                 : "m"(__m(addr)), "1"(__gu_err))
162
163 #define __get_user_8(addr)                              \
164         __asm__("1: ldbu %0,%2\n"                       \
165         "2:\n"                                          \
166         ".section __ex_table,\"a\"\n"                   \
167         "       .long 1b - .\n"                         \
168         "       lda %0, 2b-1b(%1)\n"                    \
169         ".previous"                                     \
170                 : "=r"(__gu_val), "=r"(__gu_err)        \
171                 : "m"(__m(addr)), "1"(__gu_err))
172 #else
173 /* Unfortunately, we can't get an unaligned access trap for the sub-word
174    load, so we have to do a general unaligned operation.  */
175
176 #define __get_user_16(addr)                                             \
177 {                                                                       \
178         long __gu_tmp;                                                  \
179         __asm__("1: ldq_u %0,0(%3)\n"                                   \
180         "2:     ldq_u %1,1(%3)\n"                                       \
181         "       extwl %0,%3,%0\n"                                       \
182         "       extwh %1,%3,%1\n"                                       \
183         "       or %0,%1,%0\n"                                          \
184         "3:\n"                                                          \
185         ".section __ex_table,\"a\"\n"                                   \
186         "       .long 1b - .\n"                                         \
187         "       lda %0, 3b-1b(%2)\n"                                    \
188         "       .long 2b - .\n"                                         \
189         "       lda %0, 3b-2b(%2)\n"                                    \
190         ".previous"                                                     \
191                 : "=&r"(__gu_val), "=&r"(__gu_tmp), "=r"(__gu_err)      \
192                 : "r"(addr), "2"(__gu_err));                            \
193 }
194
195 #define __get_user_8(addr)                                              \
196         __asm__("1: ldq_u %0,0(%2)\n"                                   \
197         "       extbl %0,%2,%0\n"                                       \
198         "2:\n"                                                          \
199         ".section __ex_table,\"a\"\n"                                   \
200         "       .long 1b - .\n"                                         \
201         "       lda %0, 2b-1b(%1)\n"                                    \
202         ".previous"                                                     \
203                 : "=&r"(__gu_val), "=r"(__gu_err)                       \
204                 : "r"(addr), "1"(__gu_err))
205 #endif
206
207 extern void __put_user_unknown(void);
208
209 #define __put_user_nocheck(x,ptr,size)                          \
210 ({                                                              \
211         long __pu_err = 0;                                      \
212         __chk_user_ptr(ptr);                                    \
213         switch (size) {                                         \
214           case 1: __put_user_8(x,ptr); break;                   \
215           case 2: __put_user_16(x,ptr); break;                  \
216           case 4: __put_user_32(x,ptr); break;                  \
217           case 8: __put_user_64(x,ptr); break;                  \
218           default: __put_user_unknown(); break;                 \
219         }                                                       \
220         __pu_err;                                               \
221 })
222
223 #define __put_user_check(x,ptr,size,segment)                            \
224 ({                                                                      \
225         long __pu_err = -EFAULT;                                        \
226         __typeof__(*(ptr)) __user *__pu_addr = (ptr);                   \
227         if (__access_ok((unsigned long)__pu_addr,size,segment)) {       \
228                 __pu_err = 0;                                           \
229                 switch (size) {                                         \
230                   case 1: __put_user_8(x,__pu_addr); break;             \
231                   case 2: __put_user_16(x,__pu_addr); break;            \
232                   case 4: __put_user_32(x,__pu_addr); break;            \
233                   case 8: __put_user_64(x,__pu_addr); break;            \
234                   default: __put_user_unknown(); break;                 \
235                 }                                                       \
236         }                                                               \
237         __pu_err;                                                       \
238 })
239
240 /*
241  * The "__put_user_xx()" macros tell gcc they read from memory
242  * instead of writing: this is because they do not write to
243  * any memory gcc knows about, so there are no aliasing issues
244  */
245 #define __put_user_64(x,addr)                                   \
246 __asm__ __volatile__("1: stq %r2,%1\n"                          \
247         "2:\n"                                                  \
248         ".section __ex_table,\"a\"\n"                           \
249         "       .long 1b - .\n"                                 \
250         "       lda $31,2b-1b(%0)\n"                            \
251         ".previous"                                             \
252                 : "=r"(__pu_err)                                \
253                 : "m" (__m(addr)), "rJ" (x), "0"(__pu_err))
254
255 #define __put_user_32(x,addr)                                   \
256 __asm__ __volatile__("1: stl %r2,%1\n"                          \
257         "2:\n"                                                  \
258         ".section __ex_table,\"a\"\n"                           \
259         "       .long 1b - .\n"                                 \
260         "       lda $31,2b-1b(%0)\n"                            \
261         ".previous"                                             \
262                 : "=r"(__pu_err)                                \
263                 : "m"(__m(addr)), "rJ"(x), "0"(__pu_err))
264
265 #ifdef __alpha_bwx__
266 /* Those lucky bastards with ev56 and later CPUs can do byte/word moves.  */
267
268 #define __put_user_16(x,addr)                                   \
269 __asm__ __volatile__("1: stw %r2,%1\n"                          \
270         "2:\n"                                                  \
271         ".section __ex_table,\"a\"\n"                           \
272         "       .long 1b - .\n"                                 \
273         "       lda $31,2b-1b(%0)\n"                            \
274         ".previous"                                             \
275                 : "=r"(__pu_err)                                \
276                 : "m"(__m(addr)), "rJ"(x), "0"(__pu_err))
277
278 #define __put_user_8(x,addr)                                    \
279 __asm__ __volatile__("1: stb %r2,%1\n"                          \
280         "2:\n"                                                  \
281         ".section __ex_table,\"a\"\n"                           \
282         "       .long 1b - .\n"                                 \
283         "       lda $31,2b-1b(%0)\n"                            \
284         ".previous"                                             \
285                 : "=r"(__pu_err)                                \
286                 : "m"(__m(addr)), "rJ"(x), "0"(__pu_err))
287 #else
288 /* Unfortunately, we can't get an unaligned access trap for the sub-word
289    write, so we have to do a general unaligned operation.  */
290
291 #define __put_user_16(x,addr)                                   \
292 {                                                               \
293         long __pu_tmp1, __pu_tmp2, __pu_tmp3, __pu_tmp4;        \
294         __asm__ __volatile__(                                   \
295         "1:     ldq_u %2,1(%5)\n"                               \
296         "2:     ldq_u %1,0(%5)\n"                               \
297         "       inswh %6,%5,%4\n"                               \
298         "       inswl %6,%5,%3\n"                               \
299         "       mskwh %2,%5,%2\n"                               \
300         "       mskwl %1,%5,%1\n"                               \
301         "       or %2,%4,%2\n"                                  \
302         "       or %1,%3,%1\n"                                  \
303         "3:     stq_u %2,1(%5)\n"                               \
304         "4:     stq_u %1,0(%5)\n"                               \
305         "5:\n"                                                  \
306         ".section __ex_table,\"a\"\n"                           \
307         "       .long 1b - .\n"                                 \
308         "       lda $31, 5b-1b(%0)\n"                           \
309         "       .long 2b - .\n"                                 \
310         "       lda $31, 5b-2b(%0)\n"                           \
311         "       .long 3b - .\n"                                 \
312         "       lda $31, 5b-3b(%0)\n"                           \
313         "       .long 4b - .\n"                                 \
314         "       lda $31, 5b-4b(%0)\n"                           \
315         ".previous"                                             \
316                 : "=r"(__pu_err), "=&r"(__pu_tmp1),             \
317                   "=&r"(__pu_tmp2), "=&r"(__pu_tmp3),           \
318                   "=&r"(__pu_tmp4)                              \
319                 : "r"(addr), "r"((unsigned long)(x)), "0"(__pu_err)); \
320 }
321
322 #define __put_user_8(x,addr)                                    \
323 {                                                               \
324         long __pu_tmp1, __pu_tmp2;                              \
325         __asm__ __volatile__(                                   \
326         "1:     ldq_u %1,0(%4)\n"                               \
327         "       insbl %3,%4,%2\n"                               \
328         "       mskbl %1,%4,%1\n"                               \
329         "       or %1,%2,%1\n"                                  \
330         "2:     stq_u %1,0(%4)\n"                               \
331         "3:\n"                                                  \
332         ".section __ex_table,\"a\"\n"                           \
333         "       .long 1b - .\n"                                 \
334         "       lda $31, 3b-1b(%0)\n"                           \
335         "       .long 2b - .\n"                                 \
336         "       lda $31, 3b-2b(%0)\n"                           \
337         ".previous"                                             \
338                 : "=r"(__pu_err),                               \
339                   "=&r"(__pu_tmp1), "=&r"(__pu_tmp2)            \
340                 : "r"((unsigned long)(x)), "r"(addr), "0"(__pu_err)); \
341 }
342 #endif
343
344
345 /*
346  * Complex access routines
347  */
348
349 /* This little bit of silliness is to get the GP loaded for a function
350    that ordinarily wouldn't.  Otherwise we could have it done by the macro
351    directly, which can be optimized the linker.  */
352 #ifdef MODULE
353 #define __module_address(sym)           "r"(sym),
354 #define __module_call(ra, arg, sym)     "jsr $" #ra ",(%" #arg ")," #sym
355 #else
356 #define __module_address(sym)
357 #define __module_call(ra, arg, sym)     "bsr $" #ra "," #sym " !samegp"
358 #endif
359
360 extern void __copy_user(void);
361
362 extern inline long
363 __copy_tofrom_user_nocheck(void *to, const void *from, long len)
364 {
365         register void * __cu_to __asm__("$6") = to;
366         register const void * __cu_from __asm__("$7") = from;
367         register long __cu_len __asm__("$0") = len;
368
369         __asm__ __volatile__(
370                 __module_call(28, 3, __copy_user)
371                 : "=r" (__cu_len), "=r" (__cu_from), "=r" (__cu_to)
372                 : __module_address(__copy_user)
373                   "0" (__cu_len), "1" (__cu_from), "2" (__cu_to)
374                 : "$1","$2","$3","$4","$5","$28","memory");
375
376         return __cu_len;
377 }
378
379 extern inline long
380 __copy_tofrom_user(void *to, const void *from, long len, const void __user *validate)
381 {
382         if (__access_ok((unsigned long)validate, len, get_fs()))
383                 len = __copy_tofrom_user_nocheck(to, from, len);
384         return len;
385 }
386
387 #define __copy_to_user(to,from,n)                                       \
388 ({                                                                      \
389         __chk_user_ptr(to);                                             \
390         __copy_tofrom_user_nocheck((__force void *)(to),(from),(n));    \
391 })
392 #define __copy_from_user(to,from,n)                                     \
393 ({                                                                      \
394         __chk_user_ptr(from);                                           \
395         __copy_tofrom_user_nocheck((to),(__force void *)(from),(n));    \
396 })
397
398 #define __copy_to_user_inatomic __copy_to_user
399 #define __copy_from_user_inatomic __copy_from_user
400
401
402 extern inline long
403 copy_to_user(void __user *to, const void *from, long n)
404 {
405         return __copy_tofrom_user((__force void *)to, from, n, to);
406 }
407
408 extern inline long
409 copy_from_user(void *to, const void __user *from, long n)
410 {
411         return __copy_tofrom_user(to, (__force void *)from, n, from);
412 }
413
414 extern void __do_clear_user(void);
415
416 extern inline long
417 __clear_user(void __user *to, long len)
418 {
419         register void __user * __cl_to __asm__("$6") = to;
420         register long __cl_len __asm__("$0") = len;
421         __asm__ __volatile__(
422                 __module_call(28, 2, __do_clear_user)
423                 : "=r"(__cl_len), "=r"(__cl_to)
424                 : __module_address(__do_clear_user)
425                   "0"(__cl_len), "1"(__cl_to)
426                 : "$1","$2","$3","$4","$5","$28","memory");
427         return __cl_len;
428 }
429
430 extern inline long
431 clear_user(void __user *to, long len)
432 {
433         if (__access_ok((unsigned long)to, len, get_fs()))
434                 len = __clear_user(to, len);
435         return len;
436 }
437
438 #undef __module_address
439 #undef __module_call
440
441 /* Returns: -EFAULT if exception before terminator, N if the entire
442    buffer filled, else strlen.  */
443
444 extern long __strncpy_from_user(char *__to, const char __user *__from, long __to_len);
445
446 extern inline long
447 strncpy_from_user(char *to, const char __user *from, long n)
448 {
449         long ret = -EFAULT;
450         if (__access_ok((unsigned long)from, 0, get_fs()))
451                 ret = __strncpy_from_user(to, from, n);
452         return ret;
453 }
454
455 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
456 extern long __strlen_user(const char __user *);
457
458 extern inline long strlen_user(const char __user *str)
459 {
460         return access_ok(VERIFY_READ,str,0) ? __strlen_user(str) : 0;
461 }
462
463 /* Returns: 0 if exception before NUL or reaching the supplied limit (N),
464  * a value greater than N if the limit would be exceeded, else strlen.  */
465 extern long __strnlen_user(const char __user *, long);
466
467 extern inline long strnlen_user(const char __user *str, long n)
468 {
469         return access_ok(VERIFY_READ,str,0) ? __strnlen_user(str, n) : 0;
470 }
471
472 /*
473  * About the exception table:
474  *
475  * - insn is a 32-bit pc-relative offset from the faulting insn.
476  * - nextinsn is a 16-bit offset off of the faulting instruction
477  *   (not off of the *next* instruction as branches are).
478  * - errreg is the register in which to place -EFAULT.
479  * - valreg is the final target register for the load sequence
480  *   and will be zeroed.
481  *
482  * Either errreg or valreg may be $31, in which case nothing happens.
483  *
484  * The exception fixup information "just so happens" to be arranged
485  * as in a MEM format instruction.  This lets us emit our three
486  * values like so:
487  *
488  *      lda valreg, nextinsn(errreg)
489  *
490  */
491
492 struct exception_table_entry
493 {
494         signed int insn;
495         union exception_fixup {
496                 unsigned unit;
497                 struct {
498                         signed int nextinsn : 16;
499                         unsigned int errreg : 5;
500                         unsigned int valreg : 5;
501                 } bits;
502         } fixup;
503 };
504
505 /* Returns the new pc */
506 #define fixup_exception(map_reg, fixup, pc)                     \
507 ({                                                              \
508         if ((fixup)->fixup.bits.valreg != 31)                   \
509                 map_reg((fixup)->fixup.bits.valreg) = 0;        \
510         if ((fixup)->fixup.bits.errreg != 31)                   \
511                 map_reg((fixup)->fixup.bits.errreg) = -EFAULT;  \
512         (pc) + (fixup)->fixup.bits.nextinsn;                    \
513 })
514
515
516 #endif /* __ALPHA_UACCESS_H */