2 * include/asm-xtensa/uaccess.h
4 * User space memory access functions
6 * These routines provide basic accessing functions to the user memory
7 * space for the kernel. This header file provides functions such as:
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
13 * Copyright (C) 2001 - 2005 Tensilica Inc.
16 #ifndef _XTENSA_UACCESS_H
17 #define _XTENSA_UACCESS_H
19 #include <linux/errno.h>
21 #include <linux/prefetch.h>
23 #include <asm/types.h>
26 #define VERIFY_WRITE 1
30 #include <asm/current.h>
31 #include <asm/asm-offsets.h>
32 #include <asm/processor.h>
35 * These assembly macros mirror the C macros that follow below. They
36 * should always have identical functionality. See
37 * arch/xtensa/kernel/sys.S for usage.
43 #define get_ds (KERNEL_DS)
46 * get_fs reads current->thread.current_ds into a register.
51 * <ad> contains current->thread.current_ds
55 l32i
\ad
, \ad
, THREAD_CURRENT_DS
59 * set_fs sets current->thread.current_ds to some value.
61 * <at> anything (temp register)
65 * <at> destroyed (actually, current)
66 * <av> preserved, value to write
68 .macro set_fs at
, av
, sp
70 s32i
\av
, \at
, THREAD_CURRENT_DS
74 * kernel_ok determines whether we should bypass addr/size checking.
75 * See the equivalent C-macro version below for clarity.
76 * On success, kernel_ok branches to a label indicated by parameter
77 * <success>. This implies that the macro falls through to the next
78 * insruction on an error.
80 * Note that while this macro can be used independently, we designed
81 * in for optimal use in the access_ok macro below (i.e., we fall
85 * <at> anything (temp register)
86 * <success> label to branch to on success; implies
87 * fall-through macro on error
90 * <at> destroyed (actually, current->thread.current_ds)
93 #if ((KERNEL_DS != 0) || (USER_DS == 0))
94 # error Assembly macro kernel_ok fails
96 .macro kernel_ok at
, sp
, success
102 * user_ok determines whether the access to user-space memory is allowed.
103 * See the equivalent C-macro version below for clarity.
105 * On error, user_ok branches to a label indicated by parameter
106 * <error>. This implies that the macro falls through to the next
107 * instruction on success.
109 * Note that while this macro can be used independently, we designed
110 * in for optimal use in the access_ok macro below (i.e., we fall
111 * through on success).
114 * <aa> register containing memory address
115 * <as> register containing memory size
117 * <error> label to branch to on error; implies fall-through
122 * <at> destroyed (actually, (TASK_SIZE + 1 - size))
124 .macro user_ok aa
, as
, at
, error
125 movi
\at
, __XTENSA_UL_CONST(TASK_SIZE
)
126 bgeu
\as
, \at
, \error
128 bgeu
\aa
, \at
, \error
132 * access_ok determines whether a memory access is allowed. See the
133 * equivalent C-macro version below for clarity.
135 * On error, access_ok branches to a label indicated by parameter
136 * <error>. This implies that the macro falls through to the next
137 * instruction on success.
139 * Note that we assume success is the common case, and we optimize the
140 * branch fall-through case on success.
143 * <aa> register containing memory address
144 * <as> register containing memory size
147 * <error> label to branch to on error; implies fall-through
154 .macro access_ok aa
, as
, at
, sp
, error
155 kernel_ok
\at
, \sp
, .Laccess_ok_\@
156 user_ok
\aa
, \as
, \at
, \error
160 #else /* __ASSEMBLY__ not defined */
162 #include <linux/sched.h>
165 * The fs value determines whether argument validity checking should
166 * be performed or not. If get_fs() == USER_DS, checking is
167 * performed, with get_fs() == KERNEL_DS, checking is bypassed.
169 * For historical reasons (Data Segment Register?), these macros are
173 #define KERNEL_DS ((mm_segment_t) { 0 })
174 #define USER_DS ((mm_segment_t) { 1 })
176 #define get_ds() (KERNEL_DS)
177 #define get_fs() (current->thread.current_ds)
178 #define set_fs(val) (current->thread.current_ds = (val))
180 #define segment_eq(a,b) ((a).seg == (b).seg)
182 #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
183 #define __user_ok(addr,size) (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
184 #define __access_ok(addr,size) (__kernel_ok || __user_ok((addr),(size)))
185 #define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))
188 * These are the main single-value transfer routines. They
189 * automatically use the right size if we just have the right pointer
192 * This gets kind of ugly. We want to return _two_ values in
193 * "get_user()" and yet we don't want to do any pointers, because that
194 * is too much of a performance impact. Thus we have a few rather ugly
195 * macros here, and hide all the uglyness from the user.
198 * (a) re-use the arguments for side effects (sizeof is ok)
199 * (b) require any knowledge of processes at this stage
201 #define put_user(x,ptr) __put_user_check((x),(ptr),sizeof(*(ptr)))
202 #define get_user(x,ptr) __get_user_check((x),(ptr),sizeof(*(ptr)))
205 * The "__xxx" versions of the user access functions are versions that
206 * do not verify the address space, that must have been done previously
207 * with a separate "access_ok()" call (this is used when we do multiple
208 * accesses to the same area of user memory).
210 #define __put_user(x,ptr) __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
211 #define __get_user(x,ptr) __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
214 extern long __put_user_bad(void);
216 #define __put_user_nocheck(x,ptr,size) \
219 __put_user_size((x),(ptr),(size),__pu_err); \
223 #define __put_user_check(x,ptr,size) \
225 long __pu_err = -EFAULT; \
226 __typeof__(*(ptr)) *__pu_addr = (ptr); \
227 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
228 __put_user_size((x),__pu_addr,(size),__pu_err); \
232 #define __put_user_size(x,ptr,size,retval) \
237 case 1: __put_user_asm(x,ptr,retval,1,"s8i",__cb); break; \
238 case 2: __put_user_asm(x,ptr,retval,2,"s16i",__cb); break; \
239 case 4: __put_user_asm(x,ptr,retval,4,"s32i",__cb); break; \
241 __typeof__(*ptr) __v64 = x; \
242 retval = __copy_to_user(ptr,&__v64,8); \
245 default: __put_user_bad(); \
251 * Consider a case of a user single load/store would cause both an
252 * unaligned exception and an MMU-related exception (unaligned
253 * exceptions happen first):
255 * User code passes a bad variable ptr to a system call.
256 * Kernel tries to access the variable.
257 * Unaligned exception occurs.
258 * Unaligned exception handler tries to make aligned accesses.
259 * Double exception occurs for MMU-related cause (e.g., page not mapped).
260 * do_page_fault() thinks the fault address belongs to the kernel, not the
263 * The kernel currently prohibits user unaligned accesses. We use the
264 * __check_align_* macros to check for unaligned addresses before
265 * accessing user space so we don't crash the kernel. Both
266 * __put_user_asm and __get_user_asm use these alignment macros, so
267 * macro-specific labels such as 0f, 1f, %0, %2, and %3 must stay in
271 #define __check_align_1 ""
273 #define __check_align_2 \
274 " _bbci.l %3, 0, 1f \n" \
278 #define __check_align_4 \
279 " _bbsi.l %3, 0, 0f \n" \
280 " _bbci.l %3, 1, 1f \n" \
281 "0: movi %0, %4 \n" \
286 * We don't tell gcc that we are accessing memory, but this is OK
287 * because we do not write to any memory gcc knows about, so there
288 * are no aliasing issues.
290 * WARNING: If you modify this macro at all, verify that the
291 * __check_align_* macros still work.
293 #define __put_user_asm(x, addr, err, align, insn, cb) \
294 __asm__ __volatile__( \
295 __check_align_##align \
296 "1: "insn" %2, %3, 0 \n" \
298 " .section .fixup,\"ax\" \n" \
307 " .section __ex_table,\"a\" \n" \
310 :"=r" (err), "=r" (cb) \
311 :"r" ((int)(x)), "r" (addr), "i" (-EFAULT), "0" (err))
313 #define __get_user_nocheck(x,ptr,size) \
315 long __gu_err, __gu_val; \
316 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
317 (x) = (__typeof__(*(ptr)))__gu_val; \
321 #define __get_user_check(x,ptr,size) \
323 long __gu_err = -EFAULT, __gu_val = 0; \
324 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
325 if (access_ok(VERIFY_READ,__gu_addr,size)) \
326 __get_user_size(__gu_val,__gu_addr,(size),__gu_err); \
327 (x) = (__typeof__(*(ptr)))__gu_val; \
331 extern long __get_user_bad(void);
333 #define __get_user_size(x,ptr,size,retval) \
338 case 1: __get_user_asm(x,ptr,retval,1,"l8ui",__cb); break; \
339 case 2: __get_user_asm(x,ptr,retval,2,"l16ui",__cb); break; \
340 case 4: __get_user_asm(x,ptr,retval,4,"l32i",__cb); break; \
341 case 8: retval = __copy_from_user(&x,ptr,8); break; \
342 default: (x) = __get_user_bad(); \
348 * WARNING: If you modify this macro at all, verify that the
349 * __check_align_* macros still work.
351 #define __get_user_asm(x, addr, err, align, insn, cb) \
352 __asm__ __volatile__( \
353 __check_align_##align \
354 "1: "insn" %2, %3, 0 \n" \
356 " .section .fixup,\"ax\" \n" \
366 " .section __ex_table,\"a\" \n" \
369 :"=r" (err), "=r" (cb), "=r" (x) \
370 :"r" (addr), "i" (-EFAULT), "0" (err))
374 * Copy to/from user space
378 * We use a generic, arbitrary-sized copy subroutine. The Xtensa
379 * architecture would cause heavy code bloat if we tried to inline
380 * these functions and provide __constant_copy_* equivalents like the
381 * i386 versions. __xtensa_copy_user is quite efficient. See the
382 * .fixup section of __xtensa_copy_user for a discussion on the
383 * X_zeroing equivalents for Xtensa.
386 extern unsigned __xtensa_copy_user(void *to
, const void *from
, unsigned n
);
387 #define __copy_user(to,from,size) __xtensa_copy_user(to,from,size)
390 static inline unsigned long
391 __generic_copy_from_user_nocheck(void *to
, const void *from
, unsigned long n
)
393 return __copy_user(to
,from
,n
);
396 static inline unsigned long
397 __generic_copy_to_user_nocheck(void *to
, const void *from
, unsigned long n
)
399 return __copy_user(to
,from
,n
);
402 static inline unsigned long
403 __generic_copy_to_user(void *to
, const void *from
, unsigned long n
)
406 if (access_ok(VERIFY_WRITE
, to
, n
))
407 return __copy_user(to
,from
,n
);
411 static inline unsigned long
412 __generic_copy_from_user(void *to
, const void *from
, unsigned long n
)
415 if (access_ok(VERIFY_READ
, from
, n
))
416 return __copy_user(to
,from
,n
);
422 #define copy_to_user(to,from,n) __generic_copy_to_user((to),(from),(n))
423 #define copy_from_user(to,from,n) __generic_copy_from_user((to),(from),(n))
424 #define __copy_to_user(to,from,n) __generic_copy_to_user_nocheck((to),(from),(n))
425 #define __copy_from_user(to,from,n) __generic_copy_from_user_nocheck((to),(from),(n))
426 #define __copy_to_user_inatomic __copy_to_user
427 #define __copy_from_user_inatomic __copy_from_user
431 * We need to return the number of bytes not cleared. Our memset()
432 * returns zero if a problem occurs while accessing user-space memory.
433 * In that event, return no memory cleared. Otherwise, zero for
437 static inline unsigned long
438 __xtensa_clear_user(void *addr
, unsigned long size
)
440 if ( ! memset(addr
, 0, size
) )
445 static inline unsigned long
446 clear_user(void *addr
, unsigned long size
)
448 if (access_ok(VERIFY_WRITE
, addr
, size
))
449 return __xtensa_clear_user(addr
, size
);
450 return size
? -EFAULT
: 0;
453 #define __clear_user __xtensa_clear_user
456 extern long __strncpy_user(char *, const char *, long);
457 #define __strncpy_from_user __strncpy_user
460 strncpy_from_user(char *dst
, const char *src
, long count
)
462 if (access_ok(VERIFY_READ
, src
, 1))
463 return __strncpy_from_user(dst
, src
, count
);
468 #define strlen_user(str) strnlen_user((str), TASK_SIZE - 1)
471 * Return the size of a string (including the ending 0!)
473 extern long __strnlen_user(const char *, long);
475 static inline long strnlen_user(const char *str
, long len
)
477 unsigned long top
= __kernel_ok
? ~0UL : TASK_SIZE
- 1;
479 if ((unsigned long)str
> top
)
481 return __strnlen_user(str
, len
);
485 struct exception_table_entry
487 unsigned long insn
, fixup
;
490 /* Returns 0 if exception not found and fixup.unit otherwise. */
492 extern unsigned long search_exception_table(unsigned long addr
);
493 extern void sort_exception_table(void);
495 /* Returns the new pc */
496 #define fixup_exception(map_reg, fixup_unit, pc) \
501 #endif /* __ASSEMBLY__ */
502 #endif /* _XTENSA_UACCESS_H */