MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / include / asm-alpha / uaccess.h
blob474d06689a1eb362d23f1fd6f7bc160ac984c86c
1 #ifndef __ALPHA_UACCESS_H
2 #define __ALPHA_UACCESS_H
4 #include <linux/errno.h>
5 #include <linux/sched.h>
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.
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.
17 * For historical reasons, these macros are grossly misnamed.
20 #define KERNEL_DS ((mm_segment_t) { 0UL })
21 #define USER_DS ((mm_segment_t) { -0x40000000000UL })
23 #define VERIFY_READ 0
24 #define VERIFY_WRITE 1
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))
30 #define segment_eq(a,b) ((a).seg == (b).seg)
33 * Is a address valid? This does a straightforward calculation rather
34 * than tests.
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.
42 #define __access_ok(addr,size,segment) \
43 (((segment).seg & (addr | size | (addr+size))) == 0)
45 #define access_ok(type,addr,size) \
46 ({ \
47 __chk_user_ptr(addr); \
48 __access_ok(((unsigned long)(addr)),(size),get_fs()); \
51 extern inline int verify_area(int type, const void __user * addr, unsigned long size)
53 return access_ok(type,addr,size) ? 0 : -EFAULT;
57 * These are the main single-value transfer routines. They automatically
58 * use the right size if we just have the right pointer type.
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"...)
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
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())
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()")
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)))
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.
90 extern void __get_user_unknown(void);
92 #define __get_user_nocheck(x,ptr,size) \
93 ({ \
94 long __gu_err = 0, __gu_val; \
95 __chk_user_ptr(ptr); \
96 switch (size) { \
97 case 1: __get_user_8(ptr); break; \
98 case 2: __get_user_16(ptr); break; \
99 case 4: __get_user_32(ptr); break; \
100 case 8: __get_user_64(ptr); break; \
101 default: __get_user_unknown(); break; \
103 (x) = (__typeof__(*(ptr))) __gu_val; \
104 __gu_err; \
107 #define __get_user_check(x,ptr,size,segment) \
108 ({ \
109 long __gu_err = -EFAULT, __gu_val = 0; \
110 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
111 if (__access_ok((unsigned long)__gu_addr,size,segment)) { \
112 __gu_err = 0; \
113 switch (size) { \
114 case 1: __get_user_8(__gu_addr); break; \
115 case 2: __get_user_16(__gu_addr); break; \
116 case 4: __get_user_32(__gu_addr); break; \
117 case 8: __get_user_64(__gu_addr); break; \
118 default: __get_user_unknown(); break; \
121 (x) = (__typeof__(*(ptr))) __gu_val; \
122 __gu_err; \
125 struct __large_struct { unsigned long buf[100]; };
126 #define __m(x) (*(struct __large_struct *)(x))
128 #define __get_user_64(addr) \
129 __asm__("1: ldq %0,%2\n" \
130 "2:\n" \
131 ".section __ex_table,\"a\"\n" \
132 " .long 1b - .\n" \
133 " lda %0, 2b-1b(%1)\n" \
134 ".previous" \
135 : "=r"(__gu_val), "=r"(__gu_err) \
136 : "m"(__m(addr)), "1"(__gu_err))
138 #define __get_user_32(addr) \
139 __asm__("1: ldl %0,%2\n" \
140 "2:\n" \
141 ".section __ex_table,\"a\"\n" \
142 " .long 1b - .\n" \
143 " lda %0, 2b-1b(%1)\n" \
144 ".previous" \
145 : "=r"(__gu_val), "=r"(__gu_err) \
146 : "m"(__m(addr)), "1"(__gu_err))
148 #ifdef __alpha_bwx__
149 /* Those lucky bastards with ev56 and later CPUs can do byte/word moves. */
151 #define __get_user_16(addr) \
152 __asm__("1: ldwu %0,%2\n" \
153 "2:\n" \
154 ".section __ex_table,\"a\"\n" \
155 " .long 1b - .\n" \
156 " lda %0, 2b-1b(%1)\n" \
157 ".previous" \
158 : "=r"(__gu_val), "=r"(__gu_err) \
159 : "m"(__m(addr)), "1"(__gu_err))
161 #define __get_user_8(addr) \
162 __asm__("1: ldbu %0,%2\n" \
163 "2:\n" \
164 ".section __ex_table,\"a\"\n" \
165 " .long 1b - .\n" \
166 " lda %0, 2b-1b(%1)\n" \
167 ".previous" \
168 : "=r"(__gu_val), "=r"(__gu_err) \
169 : "m"(__m(addr)), "1"(__gu_err))
170 #else
171 /* Unfortunately, we can't get an unaligned access trap for the sub-word
172 load, so we have to do a general unaligned operation. */
174 #define __get_user_16(addr) \
176 long __gu_tmp; \
177 __asm__("1: ldq_u %0,0(%3)\n" \
178 "2: ldq_u %1,1(%3)\n" \
179 " extwl %0,%3,%0\n" \
180 " extwh %1,%3,%1\n" \
181 " or %0,%1,%0\n" \
182 "3:\n" \
183 ".section __ex_table,\"a\"\n" \
184 " .long 1b - .\n" \
185 " lda %0, 3b-1b(%2)\n" \
186 " .long 2b - .\n" \
187 " lda %0, 3b-2b(%2)\n" \
188 ".previous" \
189 : "=&r"(__gu_val), "=&r"(__gu_tmp), "=r"(__gu_err) \
190 : "r"(addr), "2"(__gu_err)); \
193 #define __get_user_8(addr) \
194 __asm__("1: ldq_u %0,0(%2)\n" \
195 " extbl %0,%2,%0\n" \
196 "2:\n" \
197 ".section __ex_table,\"a\"\n" \
198 " .long 1b - .\n" \
199 " lda %0, 2b-1b(%1)\n" \
200 ".previous" \
201 : "=&r"(__gu_val), "=r"(__gu_err) \
202 : "r"(addr), "1"(__gu_err))
203 #endif
205 extern void __put_user_unknown(void);
207 #define __put_user_nocheck(x,ptr,size) \
208 ({ \
209 long __pu_err = 0; \
210 __chk_user_ptr(ptr); \
211 switch (size) { \
212 case 1: __put_user_8(x,ptr); break; \
213 case 2: __put_user_16(x,ptr); break; \
214 case 4: __put_user_32(x,ptr); break; \
215 case 8: __put_user_64(x,ptr); break; \
216 default: __put_user_unknown(); break; \
218 __pu_err; \
221 #define __put_user_check(x,ptr,size,segment) \
222 ({ \
223 long __pu_err = -EFAULT; \
224 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
225 if (__access_ok((unsigned long)__pu_addr,size,segment)) { \
226 __pu_err = 0; \
227 switch (size) { \
228 case 1: __put_user_8(x,__pu_addr); break; \
229 case 2: __put_user_16(x,__pu_addr); break; \
230 case 4: __put_user_32(x,__pu_addr); break; \
231 case 8: __put_user_64(x,__pu_addr); break; \
232 default: __put_user_unknown(); break; \
235 __pu_err; \
239 * The "__put_user_xx()" macros tell gcc they read from memory
240 * instead of writing: this is because they do not write to
241 * any memory gcc knows about, so there are no aliasing issues
243 #define __put_user_64(x,addr) \
244 __asm__ __volatile__("1: stq %r2,%1\n" \
245 "2:\n" \
246 ".section __ex_table,\"a\"\n" \
247 " .long 1b - .\n" \
248 " lda $31,2b-1b(%0)\n" \
249 ".previous" \
250 : "=r"(__pu_err) \
251 : "m" (__m(addr)), "rJ" (x), "0"(__pu_err))
253 #define __put_user_32(x,addr) \
254 __asm__ __volatile__("1: stl %r2,%1\n" \
255 "2:\n" \
256 ".section __ex_table,\"a\"\n" \
257 " .long 1b - .\n" \
258 " lda $31,2b-1b(%0)\n" \
259 ".previous" \
260 : "=r"(__pu_err) \
261 : "m"(__m(addr)), "rJ"(x), "0"(__pu_err))
263 #ifdef __alpha_bwx__
264 /* Those lucky bastards with ev56 and later CPUs can do byte/word moves. */
266 #define __put_user_16(x,addr) \
267 __asm__ __volatile__("1: stw %r2,%1\n" \
268 "2:\n" \
269 ".section __ex_table,\"a\"\n" \
270 " .long 1b - .\n" \
271 " lda $31,2b-1b(%0)\n" \
272 ".previous" \
273 : "=r"(__pu_err) \
274 : "m"(__m(addr)), "rJ"(x), "0"(__pu_err))
276 #define __put_user_8(x,addr) \
277 __asm__ __volatile__("1: stb %r2,%1\n" \
278 "2:\n" \
279 ".section __ex_table,\"a\"\n" \
280 " .long 1b - .\n" \
281 " lda $31,2b-1b(%0)\n" \
282 ".previous" \
283 : "=r"(__pu_err) \
284 : "m"(__m(addr)), "rJ"(x), "0"(__pu_err))
285 #else
286 /* Unfortunately, we can't get an unaligned access trap for the sub-word
287 write, so we have to do a general unaligned operation. */
289 #define __put_user_16(x,addr) \
291 long __pu_tmp1, __pu_tmp2, __pu_tmp3, __pu_tmp4; \
292 __asm__ __volatile__( \
293 "1: ldq_u %2,1(%5)\n" \
294 "2: ldq_u %1,0(%5)\n" \
295 " inswh %6,%5,%4\n" \
296 " inswl %6,%5,%3\n" \
297 " mskwh %2,%5,%2\n" \
298 " mskwl %1,%5,%1\n" \
299 " or %2,%4,%2\n" \
300 " or %1,%3,%1\n" \
301 "3: stq_u %2,1(%5)\n" \
302 "4: stq_u %1,0(%5)\n" \
303 "5:\n" \
304 ".section __ex_table,\"a\"\n" \
305 " .long 1b - .\n" \
306 " lda $31, 5b-1b(%0)\n" \
307 " .long 2b - .\n" \
308 " lda $31, 5b-2b(%0)\n" \
309 " .long 3b - .\n" \
310 " lda $31, 5b-3b(%0)\n" \
311 " .long 4b - .\n" \
312 " lda $31, 5b-4b(%0)\n" \
313 ".previous" \
314 : "=r"(__pu_err), "=&r"(__pu_tmp1), \
315 "=&r"(__pu_tmp2), "=&r"(__pu_tmp3), \
316 "=&r"(__pu_tmp4) \
317 : "r"(addr), "r"((unsigned long)(x)), "0"(__pu_err)); \
320 #define __put_user_8(x,addr) \
322 long __pu_tmp1, __pu_tmp2; \
323 __asm__ __volatile__( \
324 "1: ldq_u %1,0(%4)\n" \
325 " insbl %3,%4,%2\n" \
326 " mskbl %1,%4,%1\n" \
327 " or %1,%2,%1\n" \
328 "2: stq_u %1,0(%4)\n" \
329 "3:\n" \
330 ".section __ex_table,\"a\"\n" \
331 " .long 1b - .\n" \
332 " lda $31, 3b-1b(%0)\n" \
333 " .long 2b - .\n" \
334 " lda $31, 3b-2b(%0)\n" \
335 ".previous" \
336 : "=r"(__pu_err), \
337 "=&r"(__pu_tmp1), "=&r"(__pu_tmp2) \
338 : "r"((unsigned long)(x)), "r"(addr), "0"(__pu_err)); \
340 #endif
344 * Complex access routines
347 /* This little bit of silliness is to get the GP loaded for a function
348 that ordinarily wouldn't. Otherwise we could have it done by the macro
349 directly, which can be optimized the linker. */
350 #ifdef MODULE
351 #define __module_address(sym) "r"(sym),
352 #define __module_call(ra, arg, sym) "jsr $" #ra ",(%" #arg ")," #sym
353 #else
354 #define __module_address(sym)
355 #define __module_call(ra, arg, sym) "bsr $" #ra "," #sym " !samegp"
356 #endif
358 extern void __copy_user(void);
360 extern inline long
361 __copy_tofrom_user_nocheck(void *to, const void *from, long len)
363 register void * __cu_to __asm__("$6") = to;
364 register const void * __cu_from __asm__("$7") = from;
365 register long __cu_len __asm__("$0") = len;
367 __asm__ __volatile__(
368 __module_call(28, 3, __copy_user)
369 : "=r" (__cu_len), "=r" (__cu_from), "=r" (__cu_to)
370 : __module_address(__copy_user)
371 "0" (__cu_len), "1" (__cu_from), "2" (__cu_to)
372 : "$1","$2","$3","$4","$5","$28","memory");
374 return __cu_len;
377 extern inline long
378 __copy_tofrom_user(void *to, const void *from, long len, const void __user *validate)
380 if (__access_ok((unsigned long)validate, len, get_fs()))
381 len = __copy_tofrom_user_nocheck(to, from, len);
382 return len;
385 #define __copy_to_user(to,from,n) \
386 ({ \
387 __chk_user_ptr(to); \
388 __copy_tofrom_user_nocheck((__force void *)(to),(from),(n)); \
390 #define __copy_from_user(to,from,n) \
391 ({ \
392 __chk_user_ptr(from); \
393 __copy_tofrom_user_nocheck((to),(__force void *)(from),(n)); \
396 #define __copy_to_user_inatomic __copy_to_user
397 #define __copy_from_user_inatomic __copy_from_user
400 extern inline long
401 copy_to_user(void __user *to, const void *from, long n)
403 return __copy_tofrom_user((__force void *)to, from, n, to);
406 extern inline long
407 copy_from_user(void *to, const void __user *from, long n)
409 return __copy_tofrom_user(to, (__force void *)from, n, from);
412 extern void __do_clear_user(void);
414 extern inline long
415 __clear_user(void __user *to, long len)
417 register void __user * __cl_to __asm__("$6") = to;
418 register long __cl_len __asm__("$0") = len;
419 __asm__ __volatile__(
420 __module_call(28, 2, __do_clear_user)
421 : "=r"(__cl_len), "=r"(__cl_to)
422 : __module_address(__do_clear_user)
423 "0"(__cl_len), "1"(__cl_to)
424 : "$1","$2","$3","$4","$5","$28","memory");
425 return __cl_len;
428 extern inline long
429 clear_user(void __user *to, long len)
431 if (__access_ok((unsigned long)to, len, get_fs()))
432 len = __clear_user(to, len);
433 return len;
436 #undef __module_address
437 #undef __module_call
439 /* Returns: -EFAULT if exception before terminator, N if the entire
440 buffer filled, else strlen. */
442 extern long __strncpy_from_user(char *__to, const char __user *__from, long __to_len);
444 extern inline long
445 strncpy_from_user(char *to, const char __user *from, long n)
447 long ret = -EFAULT;
448 if (__access_ok((unsigned long)from, 0, get_fs()))
449 ret = __strncpy_from_user(to, from, n);
450 return ret;
453 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
454 extern long __strlen_user(const char __user *);
456 extern inline long strlen_user(const char __user *str)
458 return access_ok(VERIFY_READ,str,0) ? __strlen_user(str) : 0;
461 /* Returns: 0 if exception before NUL or reaching the supplied limit (N),
462 * a value greater than N if the limit would be exceeded, else strlen. */
463 extern long __strnlen_user(const char __user *, long);
465 extern inline long strnlen_user(const char __user *str, long n)
467 return access_ok(VERIFY_READ,str,0) ? __strnlen_user(str, n) : 0;
471 * About the exception table:
473 * - insn is a 32-bit pc-relative offset from the faulting insn.
474 * - nextinsn is a 16-bit offset off of the faulting instruction
475 * (not off of the *next* instruction as branches are).
476 * - errreg is the register in which to place -EFAULT.
477 * - valreg is the final target register for the load sequence
478 * and will be zeroed.
480 * Either errreg or valreg may be $31, in which case nothing happens.
482 * The exception fixup information "just so happens" to be arranged
483 * as in a MEM format instruction. This lets us emit our three
484 * values like so:
486 * lda valreg, nextinsn(errreg)
490 struct exception_table_entry
492 signed int insn;
493 union exception_fixup {
494 unsigned unit;
495 struct {
496 signed int nextinsn : 16;
497 unsigned int errreg : 5;
498 unsigned int valreg : 5;
499 } bits;
500 } fixup;
503 /* Returns the new pc */
504 #define fixup_exception(map_reg, fixup, pc) \
505 ({ \
506 if ((fixup)->fixup.bits.valreg != 31) \
507 map_reg((fixup)->fixup.bits.valreg) = 0; \
508 if ((fixup)->fixup.bits.errreg != 31) \
509 map_reg((fixup)->fixup.bits.errreg) = -EFAULT; \
510 (pc) + (fixup)->fixup.bits.nextinsn; \
514 #endif /* __ALPHA_UACCESS_H */