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/prefetch.h>
20 #include <asm/types.h>
21 #include <asm/extable.h>
22 #include <asm-generic/access_ok.h>
25 * These are the main single-value transfer routines. They
26 * automatically use the right size if we just have the right pointer
29 * This gets kind of ugly. We want to return _two_ values in
30 * "get_user()" and yet we don't want to do any pointers, because that
31 * is too much of a performance impact. Thus we have a few rather ugly
32 * macros here, and hide all the uglyness from the user.
35 * (a) re-use the arguments for side effects (sizeof is ok)
36 * (b) require any knowledge of processes at this stage
38 #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
39 #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
42 * The "__xxx" versions of the user access functions are versions that
43 * do not verify the address space, that must have been done previously
44 * with a separate "access_ok()" call (this is used when we do multiple
45 * accesses to the same area of user memory).
47 #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
48 #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
51 extern long __put_user_bad(void);
53 #define __put_user_nocheck(x, ptr, size) \
56 __put_user_size((x), (ptr), (size), __pu_err); \
60 #define __put_user_check(x, ptr, size) \
62 long __pu_err = -EFAULT; \
63 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
64 if (access_ok(__pu_addr, size)) \
65 __put_user_size((x), __pu_addr, (size), __pu_err); \
69 #define __put_user_size(x, ptr, size, retval) \
74 case 1: __put_user_asm(x, ptr, retval, 1, "s8i", __cb); break; \
75 case 2: __put_user_asm(x, ptr, retval, 2, "s16i", __cb); break; \
76 case 4: __put_user_asm(x, ptr, retval, 4, "s32i", __cb); break; \
78 __typeof__(*ptr) __v64 = x; \
79 retval = __copy_to_user(ptr, &__v64, 8) ? -EFAULT : 0; \
82 default: __put_user_bad(); \
88 * Consider a case of a user single load/store would cause both an
89 * unaligned exception and an MMU-related exception (unaligned
90 * exceptions happen first):
92 * User code passes a bad variable ptr to a system call.
93 * Kernel tries to access the variable.
94 * Unaligned exception occurs.
95 * Unaligned exception handler tries to make aligned accesses.
96 * Double exception occurs for MMU-related cause (e.g., page not mapped).
97 * do_page_fault() thinks the fault address belongs to the kernel, not the
100 * The kernel currently prohibits user unaligned accesses. We use the
101 * __check_align_* macros to check for unaligned addresses before
102 * accessing user space so we don't crash the kernel. Both
103 * __put_user_asm and __get_user_asm use these alignment macros, so
104 * macro-specific labels such as 0f, 1f, %0, %2, and %3 must stay in
108 #define __check_align_1 ""
110 #define __check_align_2 \
111 " _bbci.l %[mem] * 0, 1f \n" \
112 " movi %[err], %[efault] \n" \
115 #define __check_align_4 \
116 " _bbsi.l %[mem] * 0, 0f \n" \
117 " _bbci.l %[mem] * 0 + 1, 1f \n" \
118 "0: movi %[err], %[efault] \n" \
123 * We don't tell gcc that we are accessing memory, but this is OK
124 * because we do not write to any memory gcc knows about, so there
125 * are no aliasing issues.
127 * WARNING: If you modify this macro at all, verify that the
128 * __check_align_* macros still work.
130 #define __put_user_asm(x_, addr_, err_, align, insn, cb)\
131 __asm__ __volatile__( \
132 __check_align_##align \
133 "1: "insn" %[x], %[mem] \n" \
135 " .section .fixup,\"ax\" \n" \
137 " .literal_position \n" \
139 " movi %[tmp], 2b \n" \
140 " movi %[err], %[efault] \n" \
143 " .section __ex_table,\"a\" \n" \
146 :[err] "+r"(err_), [tmp] "=r"(cb), [mem] "=m"(*(addr_)) \
147 :[x] "r"(x_), [efault] "i"(-EFAULT))
149 #define __get_user_nocheck(x, ptr, size) \
152 __get_user_size((x), (ptr), (size), __gu_err); \
156 #define __get_user_check(x, ptr, size) \
158 long __gu_err = -EFAULT; \
159 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
160 if (access_ok(__gu_addr, size)) \
161 __get_user_size((x), __gu_addr, (size), __gu_err); \
163 (x) = (__typeof__(*(ptr)))0; \
167 extern long __get_user_bad(void);
169 #define __get_user_size(x, ptr, size, retval) \
174 case 1: __get_user_asm(x, ptr, retval, 1, "l8ui", __cb); break;\
175 case 2: __get_user_asm(x, ptr, retval, 2, "l16ui", __cb); break;\
176 case 4: __get_user_asm(x, ptr, retval, 4, "l32i", __cb); break;\
179 if (unlikely(__copy_from_user(&__x, ptr, 8))) { \
181 (x) = (__typeof__(*(ptr)))0; \
183 (x) = *(__force __typeof__(*(ptr)) *)&__x; \
188 (x) = (__typeof__(*(ptr)))0; \
195 * WARNING: If you modify this macro at all, verify that the
196 * __check_align_* macros still work.
198 #define __get_user_asm(x_, addr_, err_, align, insn, cb) \
201 __asm__ __volatile__( \
202 __check_align_##align \
203 "1: "insn" %[x], %[mem] \n" \
205 " .section .fixup,\"ax\" \n" \
207 " .literal_position \n" \
209 " movi %[tmp], 2b \n" \
210 " movi %[err], %[efault] \n" \
213 " .section __ex_table,\"a\" \n" \
216 :[err] "+r"(err_), [tmp] "=r"(cb), [x] "+r"(__x) \
217 :[mem] "m"(*(addr_)), [efault] "i"(-EFAULT)); \
218 (x_) = (__force __typeof__(*(addr_)))__x; \
223 * Copy to/from user space
226 extern unsigned __xtensa_copy_user(void *to, const void *from, unsigned n);
228 static inline unsigned long
229 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
232 return __xtensa_copy_user(to, (__force const void *)from, n);
234 static inline unsigned long
235 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
238 return __xtensa_copy_user((__force void *)to, from, n);
240 #define INLINE_COPY_FROM_USER
241 #define INLINE_COPY_TO_USER
244 * We need to return the number of bytes not cleared. Our memset()
245 * returns zero if a problem occurs while accessing user-space memory.
246 * In that event, return no memory cleared. Otherwise, zero for
250 static inline unsigned long
251 __xtensa_clear_user(void __user *addr, unsigned long size)
253 if (!__memset((void __force *)addr, 0, size))
258 static inline unsigned long
259 clear_user(void __user *addr, unsigned long size)
261 if (access_ok(addr, size))
262 return __xtensa_clear_user(addr, size);
263 return size ? -EFAULT : 0;
266 #define __clear_user __xtensa_clear_user
269 #ifdef CONFIG_ARCH_HAS_STRNCPY_FROM_USER
270 extern long __strncpy_user(char *dst, const char __user *src, long count);
273 strncpy_from_user(char *dst, const char __user *src, long count)
275 if (access_ok(src, 1))
276 return __strncpy_user(dst, src, count);
280 long strncpy_from_user(char *dst, const char __user *src, long count);
284 * Return the size of a string (including the ending 0!)
286 extern long __strnlen_user(const char __user *str, long len);
288 static inline long strnlen_user(const char __user *str, long len)
290 if (!access_ok(str, 1))
292 return __strnlen_user(str, len);
295 #endif /* _XTENSA_UACCESS_H */