Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / arch / xtensa / include / asm / uaccess.h
CommitLineData
9a8fd558
CZ
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
eef35c2d 7 * space for the kernel. This header file provides functions such as:
9a8fd558
CZ
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
90b03f50 19#include <linux/prefetch.h>
e44ba033 20#include <asm/types.h>
0b46a94e 21#include <asm/extable.h>
9a8fd558 22
9a8fd558
CZ
23/*
24 * The fs value determines whether argument validity checking should
25 * be performed or not. If get_fs() == USER_DS, checking is
26 * performed, with get_fs() == KERNEL_DS, checking is bypassed.
27 *
28 * For historical reasons (Data Segment Register?), these macros are
29 * grossly misnamed.
30 */
31
32#define KERNEL_DS ((mm_segment_t) { 0 })
33#define USER_DS ((mm_segment_t) { 1 })
34
9a8fd558
CZ
35#define get_fs() (current->thread.current_ds)
36#define set_fs(val) (current->thread.current_ds = (val))
37
33a3dcc2 38#define segment_eq(a, b) ((a).seg == (b).seg)
9a8fd558 39
db68ce10 40#define __kernel_ok (uaccess_kernel())
33a3dcc2 41#define __user_ok(addr, size) \
c4c4594b 42 (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
33a3dcc2 43#define __access_ok(addr, size) (__kernel_ok || __user_ok((addr), (size)))
96d4f267 44#define access_ok(addr, size) __access_ok((unsigned long)(addr), (size))
9a8fd558 45
57358ba9
MF
46#define user_addr_max() (uaccess_kernel() ? ~0UL : TASK_SIZE)
47
9a8fd558
CZ
48/*
49 * These are the main single-value transfer routines. They
50 * automatically use the right size if we just have the right pointer
51 * type.
52 *
53 * This gets kind of ugly. We want to return _two_ values in
54 * "get_user()" and yet we don't want to do any pointers, because that
55 * is too much of a performance impact. Thus we have a few rather ugly
56 * macros here, and hide all the uglyness from the user.
57 *
58 * Careful to not
59 * (a) re-use the arguments for side effects (sizeof is ok)
60 * (b) require any knowledge of processes at this stage
61 */
33a3dcc2
MT
62#define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
63#define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
9a8fd558
CZ
64
65/*
66 * The "__xxx" versions of the user access functions are versions that
67 * do not verify the address space, that must have been done previously
68 * with a separate "access_ok()" call (this is used when we do multiple
69 * accesses to the same area of user memory).
70 */
33a3dcc2
MT
71#define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
72#define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
9a8fd558
CZ
73
74
75extern long __put_user_bad(void);
76
33a3dcc2 77#define __put_user_nocheck(x, ptr, size) \
9a8fd558
CZ
78({ \
79 long __pu_err; \
33a3dcc2 80 __put_user_size((x), (ptr), (size), __pu_err); \
9a8fd558
CZ
81 __pu_err; \
82})
83
33a3dcc2
MT
84#define __put_user_check(x, ptr, size) \
85({ \
86 long __pu_err = -EFAULT; \
87 __typeof__(*(ptr)) *__pu_addr = (ptr); \
96d4f267 88 if (access_ok(__pu_addr, size)) \
33a3dcc2
MT
89 __put_user_size((x), __pu_addr, (size), __pu_err); \
90 __pu_err; \
9a8fd558
CZ
91})
92
33a3dcc2 93#define __put_user_size(x, ptr, size, retval) \
70e137eb
CZ
94do { \
95 int __cb; \
96 retval = 0; \
97 switch (size) { \
33a3dcc2
MT
98 case 1: __put_user_asm(x, ptr, retval, 1, "s8i", __cb); break; \
99 case 2: __put_user_asm(x, ptr, retval, 2, "s16i", __cb); break; \
100 case 4: __put_user_asm(x, ptr, retval, 4, "s32i", __cb); break; \
c4c4594b 101 case 8: { \
70e137eb 102 __typeof__(*ptr) __v64 = x; \
6595d144 103 retval = __copy_to_user(ptr, &__v64, 8) ? -EFAULT : 0; \
70e137eb
CZ
104 break; \
105 } \
106 default: __put_user_bad(); \
107 } \
9a8fd558
CZ
108} while (0)
109
110
111/*
112 * Consider a case of a user single load/store would cause both an
113 * unaligned exception and an MMU-related exception (unaligned
114 * exceptions happen first):
115 *
116 * User code passes a bad variable ptr to a system call.
117 * Kernel tries to access the variable.
118 * Unaligned exception occurs.
119 * Unaligned exception handler tries to make aligned accesses.
120 * Double exception occurs for MMU-related cause (e.g., page not mapped).
121 * do_page_fault() thinks the fault address belongs to the kernel, not the
122 * user, and panics.
123 *
124 * The kernel currently prohibits user unaligned accesses. We use the
125 * __check_align_* macros to check for unaligned addresses before
126 * accessing user space so we don't crash the kernel. Both
127 * __put_user_asm and __get_user_asm use these alignment macros, so
128 * macro-specific labels such as 0f, 1f, %0, %2, and %3 must stay in
129 * sync.
130 */
131
132#define __check_align_1 ""
133
134#define __check_align_2 \
c0437642
MF
135 " _bbci.l %[addr], 0, 1f \n" \
136 " movi %[err], %[efault] \n" \
9a8fd558
CZ
137 " _j 2f \n"
138
139#define __check_align_4 \
c0437642
MF
140 " _bbsi.l %[addr], 0, 0f \n" \
141 " _bbci.l %[addr], 1, 1f \n" \
142 "0: movi %[err], %[efault] \n" \
9a8fd558
CZ
143 " _j 2f \n"
144
145
146/*
147 * We don't tell gcc that we are accessing memory, but this is OK
148 * because we do not write to any memory gcc knows about, so there
149 * are no aliasing issues.
150 *
151 * WARNING: If you modify this macro at all, verify that the
152 * __check_align_* macros still work.
153 */
c0437642 154#define __put_user_asm(x_, addr_, err_, align, insn, cb)\
c4c4594b 155__asm__ __volatile__( \
70e137eb 156 __check_align_##align \
c0437642 157 "1: "insn" %[x], %[addr], 0 \n" \
70e137eb
CZ
158 "2: \n" \
159 " .section .fixup,\"ax\" \n" \
160 " .align 4 \n" \
03760270 161 " .literal_position \n" \
70e137eb 162 "5: \n" \
c0437642
MF
163 " movi %[tmp], 2b \n" \
164 " movi %[err], %[efault] \n" \
165 " jx %[tmp] \n" \
70e137eb
CZ
166 " .previous \n" \
167 " .section __ex_table,\"a\" \n" \
168 " .long 1b, 5b \n" \
169 " .previous" \
c0437642
MF
170 :[err] "+r"(err_), [tmp] "=r"(cb) \
171 :[x] "r"(x_), [addr] "r"(addr_), [efault] "i"(-EFAULT))
9a8fd558 172
33a3dcc2 173#define __get_user_nocheck(x, ptr, size) \
9a8fd558 174({ \
c9c63f3c
MF
175 long __gu_err; \
176 __get_user_size((x), (ptr), (size), __gu_err); \
9a8fd558
CZ
177 __gu_err; \
178})
179
33a3dcc2 180#define __get_user_check(x, ptr, size) \
9a8fd558 181({ \
c9c63f3c 182 long __gu_err = -EFAULT; \
9a8fd558 183 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
c9c63f3c
MF
184 if (access_ok(__gu_addr, size)) \
185 __get_user_size((x), __gu_addr, (size), __gu_err); \
186 else \
187 (x) = 0; \
9a8fd558
CZ
188 __gu_err; \
189})
190
191extern long __get_user_bad(void);
192
33a3dcc2 193#define __get_user_size(x, ptr, size, retval) \
9a8fd558 194do { \
70e137eb 195 int __cb; \
9a8fd558 196 retval = 0; \
c4c4594b 197 switch (size) { \
33a3dcc2
MT
198 case 1: __get_user_asm(x, ptr, retval, 1, "l8ui", __cb); break;\
199 case 2: __get_user_asm(x, ptr, retval, 2, "l16ui", __cb); break;\
200 case 4: __get_user_asm(x, ptr, retval, 4, "l32i", __cb); break;\
6595d144
AV
201 case 8: { \
202 u64 __x; \
203 if (unlikely(__copy_from_user(&__x, ptr, 8))) { \
204 retval = -EFAULT; \
205 (x) = 0; \
206 } else { \
207 (x) = *(__force __typeof__((ptr)))&__x; \
208 } \
209 break; \
210 } \
c9c63f3c 211 default: (x) = 0; __get_user_bad(); \
c4c4594b 212 } \
9a8fd558
CZ
213} while (0)
214
215
216/*
217 * WARNING: If you modify this macro at all, verify that the
218 * __check_align_* macros still work.
219 */
c0437642 220#define __get_user_asm(x_, addr_, err_, align, insn, cb) \
c9c63f3c
MF
221do { \
222 u32 __x = 0; \
223 __asm__ __volatile__( \
224 __check_align_##align \
225 "1: "insn" %[x], %[addr], 0 \n" \
226 "2: \n" \
227 " .section .fixup,\"ax\" \n" \
228 " .align 4 \n" \
229 " .literal_position \n" \
230 "5: \n" \
231 " movi %[tmp], 2b \n" \
232 " movi %[err], %[efault] \n" \
233 " jx %[tmp] \n" \
234 " .previous \n" \
235 " .section __ex_table,\"a\" \n" \
236 " .long 1b, 5b \n" \
237 " .previous" \
238 :[err] "+r"(err_), [tmp] "=r"(cb), [x] "+r"(__x) \
239 :[addr] "r"(addr_), [efault] "i"(-EFAULT)); \
240 (x_) = (__force __typeof__(*(addr_)))__x; \
241} while (0)
9a8fd558
CZ
242
243
244/*
245 * Copy to/from user space
246 */
247
9a8fd558 248extern unsigned __xtensa_copy_user(void *to, const void *from, unsigned n);
9a8fd558
CZ
249
250static inline unsigned long
3a0e75ad 251raw_copy_from_user(void *to, const void __user *from, unsigned long n)
9a8fd558 252{
3a0e75ad
AV
253 prefetchw(to);
254 return __xtensa_copy_user(to, (__force const void *)from, n);
9a8fd558 255}
9a8fd558 256static inline unsigned long
3a0e75ad 257raw_copy_to_user(void __user *to, const void *from, unsigned long n)
9a8fd558 258{
7d4914db 259 prefetch(from);
3a0e75ad 260 return __xtensa_copy_user((__force void *)to, from, n);
9a8fd558 261}
3a0e75ad
AV
262#define INLINE_COPY_FROM_USER
263#define INLINE_COPY_TO_USER
9a8fd558
CZ
264
265/*
266 * We need to return the number of bytes not cleared. Our memset()
267 * returns zero if a problem occurs while accessing user-space memory.
268 * In that event, return no memory cleared. Otherwise, zero for
269 * success.
270 */
271
d99cf715 272static inline unsigned long
9a8fd558
CZ
273__xtensa_clear_user(void *addr, unsigned long size)
274{
e0baa014 275 if (!__memset(addr, 0, size))
9a8fd558
CZ
276 return size;
277 return 0;
278}
279
d99cf715 280static inline unsigned long
9a8fd558
CZ
281clear_user(void *addr, unsigned long size)
282{
96d4f267 283 if (access_ok(addr, size))
9a8fd558
CZ
284 return __xtensa_clear_user(addr, size);
285 return size ? -EFAULT : 0;
286}
287
288#define __clear_user __xtensa_clear_user
289
290
57358ba9
MF
291#ifndef CONFIG_GENERIC_STRNCPY_FROM_USER
292
9a8fd558 293extern long __strncpy_user(char *, const char *, long);
9a8fd558 294
d99cf715 295static inline long
9a8fd558
CZ
296strncpy_from_user(char *dst, const char *src, long count)
297{
96d4f267 298 if (access_ok(src, 1))
10503bf9 299 return __strncpy_user(dst, src, count);
9a8fd558
CZ
300 return -EFAULT;
301}
57358ba9
MF
302#else
303long strncpy_from_user(char *dst, const char *src, long count);
304#endif
9a8fd558 305
9a8fd558
CZ
306/*
307 * Return the size of a string (including the ending 0!)
308 */
309extern long __strnlen_user(const char *, long);
310
d99cf715 311static inline long strnlen_user(const char *str, long len)
9a8fd558
CZ
312{
313 unsigned long top = __kernel_ok ? ~0UL : TASK_SIZE - 1;
314
315 if ((unsigned long)str > top)
316 return 0;
317 return __strnlen_user(str, len);
318}
319
9a8fd558 320#endif /* _XTENSA_UACCESS_H */