drivers/connector/cn_proc.c: remove unused local
[pandora-kernel.git] / arch / xtensa / include / asm / uaccess.h
1 /*
2  * include/asm-xtensa/uaccess.h
3  *
4  * User space memory access functions
5  *
6  * These routines provide basic accessing functions to the user memory
7  * space for the kernel. This header file provides functions such as:
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file "COPYING" in the main directory of this archive
11  * for more details.
12  *
13  * Copyright (C) 2001 - 2005 Tensilica Inc.
14  */
15
16 #ifndef _XTENSA_UACCESS_H
17 #define _XTENSA_UACCESS_H
18
19 #include <linux/errno.h>
20 #include <linux/prefetch.h>
21
22 #define VERIFY_READ    0
23 #define VERIFY_WRITE   1
24
25 #ifdef __ASSEMBLY__
26
27 #include <asm/current.h>
28 #include <asm/asm-offsets.h>
29 #include <asm/processor.h>
30 #include <asm/types.h>
31
32 /*
33  * These assembly macros mirror the C macros that follow below.  They
34  * should always have identical functionality.  See
35  * arch/xtensa/kernel/sys.S for usage.
36  */
37
38 #define KERNEL_DS       0
39 #define USER_DS         1
40
41 #define get_ds          (KERNEL_DS)
42
43 /*
44  * get_fs reads current->thread.current_ds into a register.
45  * On Entry:
46  *      <ad>    anything
47  *      <sp>    stack
48  * On Exit:
49  *      <ad>    contains current->thread.current_ds
50  */
51         .macro  get_fs  ad, sp
52         GET_CURRENT(\ad,\sp)
53         l32i    \ad, \ad, THREAD_CURRENT_DS
54         .endm
55
56 /*
57  * set_fs sets current->thread.current_ds to some value.
58  * On Entry:
59  *      <at>    anything (temp register)
60  *      <av>    value to write
61  *      <sp>    stack
62  * On Exit:
63  *      <at>    destroyed (actually, current)
64  *      <av>    preserved, value to write
65  */
66         .macro  set_fs  at, av, sp
67         GET_CURRENT(\at,\sp)
68         s32i    \av, \at, THREAD_CURRENT_DS
69         .endm
70
71 /*
72  * kernel_ok determines whether we should bypass addr/size checking.
73  * See the equivalent C-macro version below for clarity.
74  * On success, kernel_ok branches to a label indicated by parameter
75  * <success>.  This implies that the macro falls through to the next
76  * insruction on an error.
77  *
78  * Note that while this macro can be used independently, we designed
79  * in for optimal use in the access_ok macro below (i.e., we fall
80  * through on error).
81  *
82  * On Entry:
83  *      <at>            anything (temp register)
84  *      <success>       label to branch to on success; implies
85  *                      fall-through macro on error
86  *      <sp>            stack pointer
87  * On Exit:
88  *      <at>            destroyed (actually, current->thread.current_ds)
89  */
90
91 #if ((KERNEL_DS != 0) || (USER_DS == 0))
92 # error Assembly macro kernel_ok fails
93 #endif
94         .macro  kernel_ok  at, sp, success
95         get_fs  \at, \sp
96         beqz    \at, \success
97         .endm
98
99 /*
100  * user_ok determines whether the access to user-space memory is allowed.
101  * See the equivalent C-macro version below for clarity.
102  *
103  * On error, user_ok branches to a label indicated by parameter
104  * <error>.  This implies that the macro falls through to the next
105  * instruction on success.
106  *
107  * Note that while this macro can be used independently, we designed
108  * in for optimal use in the access_ok macro below (i.e., we fall
109  * through on success).
110  *
111  * On Entry:
112  *      <aa>    register containing memory address
113  *      <as>    register containing memory size
114  *      <at>    temp register
115  *      <error> label to branch to on error; implies fall-through
116  *              macro on success
117  * On Exit:
118  *      <aa>    preserved
119  *      <as>    preserved
120  *      <at>    destroyed (actually, (TASK_SIZE + 1 - size))
121  */
122         .macro  user_ok aa, as, at, error
123         movi    \at, __XTENSA_UL_CONST(TASK_SIZE)
124         bgeu    \as, \at, \error
125         sub     \at, \at, \as
126         bgeu    \aa, \at, \error
127         .endm
128
129 /*
130  * access_ok determines whether a memory access is allowed.  See the
131  * equivalent C-macro version below for clarity.
132  *
133  * On error, access_ok branches to a label indicated by parameter
134  * <error>.  This implies that the macro falls through to the next
135  * instruction on success.
136  *
137  * Note that we assume success is the common case, and we optimize the
138  * branch fall-through case on success.
139  *
140  * On Entry:
141  *      <aa>    register containing memory address
142  *      <as>    register containing memory size
143  *      <at>    temp register
144  *      <sp>
145  *      <error> label to branch to on error; implies fall-through
146  *              macro on success
147  * On Exit:
148  *      <aa>    preserved
149  *      <as>    preserved
150  *      <at>    destroyed
151  */
152         .macro  access_ok  aa, as, at, sp, error
153         kernel_ok  \at, \sp, .Laccess_ok_\@
154         user_ok    \aa, \as, \at, \error
155 .Laccess_ok_\@:
156         .endm
157
158 #else /* __ASSEMBLY__ not defined */
159
160 #include <linux/sched.h>
161 #include <asm/types.h>
162
163 /*
164  * The fs value determines whether argument validity checking should
165  * be performed or not.  If get_fs() == USER_DS, checking is
166  * performed, with get_fs() == KERNEL_DS, checking is bypassed.
167  *
168  * For historical reasons (Data Segment Register?), these macros are
169  * grossly misnamed.
170  */
171
172 #define KERNEL_DS       ((mm_segment_t) { 0 })
173 #define USER_DS         ((mm_segment_t) { 1 })
174
175 #define get_ds()        (KERNEL_DS)
176 #define get_fs()        (current->thread.current_ds)
177 #define set_fs(val)     (current->thread.current_ds = (val))
178
179 #define segment_eq(a,b) ((a).seg == (b).seg)
180
181 #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
182 #define __user_ok(addr,size) (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
183 #define __access_ok(addr,size) (__kernel_ok || __user_ok((addr),(size)))
184 #define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))
185
186 /*
187  * These are the main single-value transfer routines.  They
188  * automatically use the right size if we just have the right pointer
189  * type.
190  *
191  * This gets kind of ugly. We want to return _two_ values in
192  * "get_user()" and yet we don't want to do any pointers, because that
193  * is too much of a performance impact. Thus we have a few rather ugly
194  * macros here, and hide all the uglyness from the user.
195  *
196  * Careful to not
197  * (a) re-use the arguments for side effects (sizeof is ok)
198  * (b) require any knowledge of processes at this stage
199  */
200 #define put_user(x,ptr) __put_user_check((x),(ptr),sizeof(*(ptr)))
201 #define get_user(x,ptr) __get_user_check((x),(ptr),sizeof(*(ptr)))
202
203 /*
204  * The "__xxx" versions of the user access functions are versions that
205  * do not verify the address space, that must have been done previously
206  * with a separate "access_ok()" call (this is used when we do multiple
207  * accesses to the same area of user memory).
208  */
209 #define __put_user(x,ptr) __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
210 #define __get_user(x,ptr) __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
211
212
213 extern long __put_user_bad(void);
214
215 #define __put_user_nocheck(x,ptr,size)                  \
216 ({                                                      \
217         long __pu_err;                                  \
218         __put_user_size((x),(ptr),(size),__pu_err);     \
219         __pu_err;                                       \
220 })
221
222 #define __put_user_check(x,ptr,size)                            \
223 ({                                                              \
224         long __pu_err = -EFAULT;                                \
225         __typeof__(*(ptr)) *__pu_addr = (ptr);                  \
226         if (access_ok(VERIFY_WRITE,__pu_addr,size))             \
227                 __put_user_size((x),__pu_addr,(size),__pu_err); \
228         __pu_err;                                               \
229 })
230
231 #define __put_user_size(x,ptr,size,retval)                              \
232 do {                                                                    \
233         int __cb;                                                       \
234         retval = 0;                                                     \
235         switch (size) {                                                 \
236         case 1: __put_user_asm(x,ptr,retval,1,"s8i",__cb);  break;      \
237         case 2: __put_user_asm(x,ptr,retval,2,"s16i",__cb); break;      \
238         case 4: __put_user_asm(x,ptr,retval,4,"s32i",__cb); break;      \
239         case 8: {                                                       \
240                      __typeof__(*ptr) __v64 = x;                        \
241                      retval = __copy_to_user(ptr,&__v64,8);             \
242                      break;                                             \
243                 }                                                       \
244         default: __put_user_bad();                                      \
245         }                                                               \
246 } while (0)
247
248
249 /*
250  * Consider a case of a user single load/store would cause both an
251  * unaligned exception and an MMU-related exception (unaligned
252  * exceptions happen first):
253  *
254  * User code passes a bad variable ptr to a system call.
255  * Kernel tries to access the variable.
256  * Unaligned exception occurs.
257  * Unaligned exception handler tries to make aligned accesses.
258  * Double exception occurs for MMU-related cause (e.g., page not mapped).
259  * do_page_fault() thinks the fault address belongs to the kernel, not the
260  * user, and panics.
261  *
262  * The kernel currently prohibits user unaligned accesses.  We use the
263  * __check_align_* macros to check for unaligned addresses before
264  * accessing user space so we don't crash the kernel.  Both
265  * __put_user_asm and __get_user_asm use these alignment macros, so
266  * macro-specific labels such as 0f, 1f, %0, %2, and %3 must stay in
267  * sync.
268  */
269
270 #define __check_align_1  ""
271
272 #define __check_align_2                         \
273         "   _bbci.l %3,  0, 1f          \n"     \
274         "   movi    %0, %4              \n"     \
275         "   _j      2f                  \n"
276
277 #define __check_align_4                         \
278         "   _bbsi.l %3,  0, 0f          \n"     \
279         "   _bbci.l %3,  1, 1f          \n"     \
280         "0: movi    %0, %4              \n"     \
281         "   _j      2f                  \n"
282
283
284 /*
285  * We don't tell gcc that we are accessing memory, but this is OK
286  * because we do not write to any memory gcc knows about, so there
287  * are no aliasing issues.
288  *
289  * WARNING: If you modify this macro at all, verify that the
290  * __check_align_* macros still work.
291  */
292 #define __put_user_asm(x, addr, err, align, insn, cb)   \
293    __asm__ __volatile__(                                \
294         __check_align_##align                           \
295         "1: "insn"  %2, %3, 0           \n"             \
296         "2:                             \n"             \
297         "   .section  .fixup,\"ax\"     \n"             \
298         "   .align 4                    \n"             \
299         "4:                             \n"             \
300         "   .long  2b                   \n"             \
301         "5:                             \n"             \
302         "   l32r   %1, 4b               \n"             \
303         "   movi   %0, %4               \n"             \
304         "   jx     %1                   \n"             \
305         "   .previous                   \n"             \
306         "   .section  __ex_table,\"a\"  \n"             \
307         "   .long       1b, 5b          \n"             \
308         "   .previous"                                  \
309         :"=r" (err), "=r" (cb)                          \
310         :"r" ((int)(x)), "r" (addr), "i" (-EFAULT), "0" (err))
311
312 #define __get_user_nocheck(x,ptr,size)                          \
313 ({                                                              \
314         long __gu_err, __gu_val;                                \
315         __get_user_size(__gu_val,(ptr),(size),__gu_err);        \
316         (x) = (__typeof__(*(ptr)))__gu_val;                     \
317         __gu_err;                                               \
318 })
319
320 #define __get_user_check(x,ptr,size)                                    \
321 ({                                                                      \
322         long __gu_err = -EFAULT, __gu_val = 0;                          \
323         const __typeof__(*(ptr)) *__gu_addr = (ptr);                    \
324         if (access_ok(VERIFY_READ,__gu_addr,size))                      \
325                 __get_user_size(__gu_val,__gu_addr,(size),__gu_err);    \
326         (x) = (__typeof__(*(ptr)))__gu_val;                             \
327         __gu_err;                                                       \
328 })
329
330 extern long __get_user_bad(void);
331
332 #define __get_user_size(x,ptr,size,retval)                              \
333 do {                                                                    \
334         int __cb;                                                       \
335         retval = 0;                                                     \
336         switch (size) {                                                 \
337           case 1: __get_user_asm(x,ptr,retval,1,"l8ui",__cb);  break;   \
338           case 2: __get_user_asm(x,ptr,retval,2,"l16ui",__cb); break;   \
339           case 4: __get_user_asm(x,ptr,retval,4,"l32i",__cb);  break;   \
340           case 8: retval = __copy_from_user(&x,ptr,8);    break;        \
341           default: (x) = __get_user_bad();                              \
342         }                                                               \
343 } while (0)
344
345
346 /*
347  * WARNING: If you modify this macro at all, verify that the
348  * __check_align_* macros still work.
349  */
350 #define __get_user_asm(x, addr, err, align, insn, cb) \
351    __asm__ __volatile__(                        \
352         __check_align_##align                   \
353         "1: "insn"  %2, %3, 0           \n"     \
354         "2:                             \n"     \
355         "   .section  .fixup,\"ax\"     \n"     \
356         "   .align 4                    \n"     \
357         "4:                             \n"     \
358         "   .long  2b                   \n"     \
359         "5:                             \n"     \
360         "   l32r   %1, 4b               \n"     \
361         "   movi   %2, 0                \n"     \
362         "   movi   %0, %4               \n"     \
363         "   jx     %1                   \n"     \
364         "   .previous                   \n"     \
365         "   .section  __ex_table,\"a\"  \n"     \
366         "   .long       1b, 5b          \n"     \
367         "   .previous"                          \
368         :"=r" (err), "=r" (cb), "=r" (x)        \
369         :"r" (addr), "i" (-EFAULT), "0" (err))
370
371
372 /*
373  * Copy to/from user space
374  */
375
376 /*
377  * We use a generic, arbitrary-sized copy subroutine.  The Xtensa
378  * architecture would cause heavy code bloat if we tried to inline
379  * these functions and provide __constant_copy_* equivalents like the
380  * i386 versions.  __xtensa_copy_user is quite efficient.  See the
381  * .fixup section of __xtensa_copy_user for a discussion on the
382  * X_zeroing equivalents for Xtensa.
383  */
384
385 extern unsigned __xtensa_copy_user(void *to, const void *from, unsigned n);
386 #define __copy_user(to,from,size) __xtensa_copy_user(to,from,size)
387
388
389 static inline unsigned long
390 __generic_copy_from_user_nocheck(void *to, const void *from, unsigned long n)
391 {
392         return __copy_user(to,from,n);
393 }
394
395 static inline unsigned long
396 __generic_copy_to_user_nocheck(void *to, const void *from, unsigned long n)
397 {
398         return __copy_user(to,from,n);
399 }
400
401 static inline unsigned long
402 __generic_copy_to_user(void *to, const void *from, unsigned long n)
403 {
404         prefetch(from);
405         if (access_ok(VERIFY_WRITE, to, n))
406                 return __copy_user(to,from,n);
407         return n;
408 }
409
410 static inline unsigned long
411 __generic_copy_from_user(void *to, const void *from, unsigned long n)
412 {
413         prefetchw(to);
414         if (access_ok(VERIFY_READ, from, n))
415                 return __copy_user(to,from,n);
416         else
417                 memset(to, 0, n);
418         return n;
419 }
420
421 #define copy_to_user(to,from,n) __generic_copy_to_user((to),(from),(n))
422 #define copy_from_user(to,from,n) __generic_copy_from_user((to),(from),(n))
423 #define __copy_to_user(to,from,n) __generic_copy_to_user_nocheck((to),(from),(n))
424 #define __copy_from_user(to,from,n) __generic_copy_from_user_nocheck((to),(from),(n))
425 #define __copy_to_user_inatomic __copy_to_user
426 #define __copy_from_user_inatomic __copy_from_user
427
428
429 /*
430  * We need to return the number of bytes not cleared.  Our memset()
431  * returns zero if a problem occurs while accessing user-space memory.
432  * In that event, return no memory cleared.  Otherwise, zero for
433  * success.
434  */
435
436 static inline unsigned long
437 __xtensa_clear_user(void *addr, unsigned long size)
438 {
439         if ( ! memset(addr, 0, size) )
440                 return size;
441         return 0;
442 }
443
444 static inline unsigned long
445 clear_user(void *addr, unsigned long size)
446 {
447         if (access_ok(VERIFY_WRITE, addr, size))
448                 return __xtensa_clear_user(addr, size);
449         return size ? -EFAULT : 0;
450 }
451
452 #define __clear_user  __xtensa_clear_user
453
454
455 extern long __strncpy_user(char *, const char *, long);
456 #define __strncpy_from_user __strncpy_user
457
458 static inline long
459 strncpy_from_user(char *dst, const char *src, long count)
460 {
461         if (access_ok(VERIFY_READ, src, 1))
462                 return __strncpy_from_user(dst, src, count);
463         return -EFAULT;
464 }
465
466
467 #define strlen_user(str) strnlen_user((str), TASK_SIZE - 1)
468
469 /*
470  * Return the size of a string (including the ending 0!)
471  */
472 extern long __strnlen_user(const char *, long);
473
474 static inline long strnlen_user(const char *str, long len)
475 {
476         unsigned long top = __kernel_ok ? ~0UL : TASK_SIZE - 1;
477
478         if ((unsigned long)str > top)
479                 return 0;
480         return __strnlen_user(str, len);
481 }
482
483
484 struct exception_table_entry
485 {
486         unsigned long insn, fixup;
487 };
488
489 /* Returns 0 if exception not found and fixup.unit otherwise.  */
490
491 extern unsigned long search_exception_table(unsigned long addr);
492 extern void sort_exception_table(void);
493
494 /* Returns the new pc */
495 #define fixup_exception(map_reg, fixup_unit, pc)                \
496 ({                                                              \
497         fixup_unit;                                             \
498 })
499
500 #endif  /* __ASSEMBLY__ */
501 #endif  /* _XTENSA_UACCESS_H */