blob: bd8861c811ef1614879a300b5b858c841cff1a65 [file] [log] [blame]
Chris Zankel9a8fd552005-06-23 22:01:26 -07001/*
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
Stefan Weileef35c22010-08-06 21:11:15 +02007 * space for the kernel. This header file provides functions such as:
Chris Zankel9a8fd552005-06-23 22:01:26 -07008 *
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
WANG Cong90b03f52011-07-25 17:11:54 -070019#include <linux/prefetch.h>
Vitaliy Ivanove44ba032011-06-20 16:08:07 +020020#include <asm/types.h>
Chris Zankel9a8fd552005-06-23 22:01:26 -070021
Chris Zankel9a8fd552005-06-23 22:01:26 -070022/*
23 * The fs value determines whether argument validity checking should
24 * be performed or not. If get_fs() == USER_DS, checking is
25 * performed, with get_fs() == KERNEL_DS, checking is bypassed.
26 *
27 * For historical reasons (Data Segment Register?), these macros are
28 * grossly misnamed.
29 */
30
31#define KERNEL_DS ((mm_segment_t) { 0 })
32#define USER_DS ((mm_segment_t) { 1 })
33
34#define get_ds() (KERNEL_DS)
35#define get_fs() (current->thread.current_ds)
36#define set_fs(val) (current->thread.current_ds = (val))
37
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020038#define segment_eq(a, b) ((a).seg == (b).seg)
Chris Zankel9a8fd552005-06-23 22:01:26 -070039
40#define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020041#define __user_ok(addr, size) \
Chris Zankelc4c45942012-11-28 16:53:51 -080042 (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020043#define __access_ok(addr, size) (__kernel_ok || __user_ok((addr), (size)))
44#define access_ok(type, addr, size) __access_ok((unsigned long)(addr), (size))
Chris Zankel9a8fd552005-06-23 22:01:26 -070045
Chris Zankel9a8fd552005-06-23 22:01:26 -070046/*
47 * These are the main single-value transfer routines. They
48 * automatically use the right size if we just have the right pointer
49 * type.
50 *
51 * This gets kind of ugly. We want to return _two_ values in
52 * "get_user()" and yet we don't want to do any pointers, because that
53 * is too much of a performance impact. Thus we have a few rather ugly
54 * macros here, and hide all the uglyness from the user.
55 *
56 * Careful to not
57 * (a) re-use the arguments for side effects (sizeof is ok)
58 * (b) require any knowledge of processes at this stage
59 */
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020060#define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
61#define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
Chris Zankel9a8fd552005-06-23 22:01:26 -070062
63/*
64 * The "__xxx" versions of the user access functions are versions that
65 * do not verify the address space, that must have been done previously
66 * with a separate "access_ok()" call (this is used when we do multiple
67 * accesses to the same area of user memory).
68 */
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020069#define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
70#define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Chris Zankel9a8fd552005-06-23 22:01:26 -070071
72
73extern long __put_user_bad(void);
74
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020075#define __put_user_nocheck(x, ptr, size) \
Chris Zankel9a8fd552005-06-23 22:01:26 -070076({ \
77 long __pu_err; \
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020078 __put_user_size((x), (ptr), (size), __pu_err); \
Chris Zankel9a8fd552005-06-23 22:01:26 -070079 __pu_err; \
80})
81
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020082#define __put_user_check(x, ptr, size) \
83({ \
84 long __pu_err = -EFAULT; \
85 __typeof__(*(ptr)) *__pu_addr = (ptr); \
86 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
87 __put_user_size((x), __pu_addr, (size), __pu_err); \
88 __pu_err; \
Chris Zankel9a8fd552005-06-23 22:01:26 -070089})
90
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020091#define __put_user_size(x, ptr, size, retval) \
Chris Zankel70e137e2007-10-23 10:58:53 -070092do { \
93 int __cb; \
94 retval = 0; \
95 switch (size) { \
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +020096 case 1: __put_user_asm(x, ptr, retval, 1, "s8i", __cb); break; \
97 case 2: __put_user_asm(x, ptr, retval, 2, "s16i", __cb); break; \
98 case 4: __put_user_asm(x, ptr, retval, 4, "s32i", __cb); break; \
Chris Zankelc4c45942012-11-28 16:53:51 -080099 case 8: { \
Chris Zankel70e137e2007-10-23 10:58:53 -0700100 __typeof__(*ptr) __v64 = x; \
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200101 retval = __copy_to_user(ptr, &__v64, 8); \
Chris Zankel70e137e2007-10-23 10:58:53 -0700102 break; \
103 } \
104 default: __put_user_bad(); \
105 } \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700106} while (0)
107
108
109/*
110 * Consider a case of a user single load/store would cause both an
111 * unaligned exception and an MMU-related exception (unaligned
112 * exceptions happen first):
113 *
114 * User code passes a bad variable ptr to a system call.
115 * Kernel tries to access the variable.
116 * Unaligned exception occurs.
117 * Unaligned exception handler tries to make aligned accesses.
118 * Double exception occurs for MMU-related cause (e.g., page not mapped).
119 * do_page_fault() thinks the fault address belongs to the kernel, not the
120 * user, and panics.
121 *
122 * The kernel currently prohibits user unaligned accesses. We use the
123 * __check_align_* macros to check for unaligned addresses before
124 * accessing user space so we don't crash the kernel. Both
125 * __put_user_asm and __get_user_asm use these alignment macros, so
126 * macro-specific labels such as 0f, 1f, %0, %2, and %3 must stay in
127 * sync.
128 */
129
130#define __check_align_1 ""
131
132#define __check_align_2 \
Chris Zankel70e137e2007-10-23 10:58:53 -0700133 " _bbci.l %3, 0, 1f \n" \
134 " movi %0, %4 \n" \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700135 " _j 2f \n"
136
137#define __check_align_4 \
Chris Zankel70e137e2007-10-23 10:58:53 -0700138 " _bbsi.l %3, 0, 0f \n" \
139 " _bbci.l %3, 1, 1f \n" \
140 "0: movi %0, %4 \n" \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700141 " _j 2f \n"
142
143
144/*
145 * We don't tell gcc that we are accessing memory, but this is OK
146 * because we do not write to any memory gcc knows about, so there
147 * are no aliasing issues.
148 *
149 * WARNING: If you modify this macro at all, verify that the
150 * __check_align_* macros still work.
151 */
Chris Zankel70e137e2007-10-23 10:58:53 -0700152#define __put_user_asm(x, addr, err, align, insn, cb) \
Chris Zankelc4c45942012-11-28 16:53:51 -0800153__asm__ __volatile__( \
Chris Zankel70e137e2007-10-23 10:58:53 -0700154 __check_align_##align \
155 "1: "insn" %2, %3, 0 \n" \
156 "2: \n" \
157 " .section .fixup,\"ax\" \n" \
158 " .align 4 \n" \
159 "4: \n" \
160 " .long 2b \n" \
161 "5: \n" \
162 " l32r %1, 4b \n" \
Chris Zankelc4c45942012-11-28 16:53:51 -0800163 " movi %0, %4 \n" \
164 " jx %1 \n" \
Chris Zankel70e137e2007-10-23 10:58:53 -0700165 " .previous \n" \
166 " .section __ex_table,\"a\" \n" \
167 " .long 1b, 5b \n" \
168 " .previous" \
169 :"=r" (err), "=r" (cb) \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700170 :"r" ((int)(x)), "r" (addr), "i" (-EFAULT), "0" (err))
171
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200172#define __get_user_nocheck(x, ptr, size) \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700173({ \
174 long __gu_err, __gu_val; \
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200175 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
176 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700177 __gu_err; \
178})
179
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200180#define __get_user_check(x, ptr, size) \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700181({ \
182 long __gu_err = -EFAULT, __gu_val = 0; \
183 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200184 if (access_ok(VERIFY_READ, __gu_addr, size)) \
185 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
186 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700187 __gu_err; \
188})
189
190extern long __get_user_bad(void);
191
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200192#define __get_user_size(x, ptr, size, retval) \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700193do { \
Chris Zankel70e137e2007-10-23 10:58:53 -0700194 int __cb; \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700195 retval = 0; \
Chris Zankelc4c45942012-11-28 16:53:51 -0800196 switch (size) { \
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200197 case 1: __get_user_asm(x, ptr, retval, 1, "l8ui", __cb); break;\
198 case 2: __get_user_asm(x, ptr, retval, 2, "l16ui", __cb); break;\
199 case 4: __get_user_asm(x, ptr, retval, 4, "l32i", __cb); break;\
200 case 8: retval = __copy_from_user(&x, ptr, 8); break; \
Chris Zankelc4c45942012-11-28 16:53:51 -0800201 default: (x) = __get_user_bad(); \
202 } \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700203} while (0)
204
205
206/*
207 * WARNING: If you modify this macro at all, verify that the
208 * __check_align_* macros still work.
209 */
Chris Zankel70e137e2007-10-23 10:58:53 -0700210#define __get_user_asm(x, addr, err, align, insn, cb) \
Chris Zankelc4c45942012-11-28 16:53:51 -0800211__asm__ __volatile__( \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700212 __check_align_##align \
Chris Zankel70e137e2007-10-23 10:58:53 -0700213 "1: "insn" %2, %3, 0 \n" \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700214 "2: \n" \
215 " .section .fixup,\"ax\" \n" \
216 " .align 4 \n" \
217 "4: \n" \
218 " .long 2b \n" \
219 "5: \n" \
Chris Zankel70e137e2007-10-23 10:58:53 -0700220 " l32r %1, 4b \n" \
221 " movi %2, 0 \n" \
Chris Zankelc4c45942012-11-28 16:53:51 -0800222 " movi %0, %4 \n" \
223 " jx %1 \n" \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700224 " .previous \n" \
225 " .section __ex_table,\"a\" \n" \
226 " .long 1b, 5b \n" \
227 " .previous" \
Chris Zankel70e137e2007-10-23 10:58:53 -0700228 :"=r" (err), "=r" (cb), "=r" (x) \
Chris Zankel9a8fd552005-06-23 22:01:26 -0700229 :"r" (addr), "i" (-EFAULT), "0" (err))
230
231
232/*
233 * Copy to/from user space
234 */
235
236/*
237 * We use a generic, arbitrary-sized copy subroutine. The Xtensa
238 * architecture would cause heavy code bloat if we tried to inline
239 * these functions and provide __constant_copy_* equivalents like the
240 * i386 versions. __xtensa_copy_user is quite efficient. See the
241 * .fixup section of __xtensa_copy_user for a discussion on the
242 * X_zeroing equivalents for Xtensa.
243 */
244
245extern unsigned __xtensa_copy_user(void *to, const void *from, unsigned n);
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200246#define __copy_user(to, from, size) __xtensa_copy_user(to, from, size)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700247
248
249static inline unsigned long
250__generic_copy_from_user_nocheck(void *to, const void *from, unsigned long n)
251{
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200252 return __copy_user(to, from, n);
Chris Zankel9a8fd552005-06-23 22:01:26 -0700253}
254
255static inline unsigned long
256__generic_copy_to_user_nocheck(void *to, const void *from, unsigned long n)
257{
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200258 return __copy_user(to, from, n);
Chris Zankel9a8fd552005-06-23 22:01:26 -0700259}
260
261static inline unsigned long
262__generic_copy_to_user(void *to, const void *from, unsigned long n)
263{
264 prefetch(from);
265 if (access_ok(VERIFY_WRITE, to, n))
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200266 return __copy_user(to, from, n);
Chris Zankel9a8fd552005-06-23 22:01:26 -0700267 return n;
268}
269
270static inline unsigned long
271__generic_copy_from_user(void *to, const void *from, unsigned long n)
272{
273 prefetchw(to);
274 if (access_ok(VERIFY_READ, from, n))
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200275 return __copy_user(to, from, n);
Chris Zankel9a8fd552005-06-23 22:01:26 -0700276 else
277 memset(to, 0, n);
278 return n;
279}
280
Michael S. Tsirkin33a3dcc2015-01-06 15:11:13 +0200281#define copy_to_user(to, from, n) __generic_copy_to_user((to), (from), (n))
282#define copy_from_user(to, from, n) __generic_copy_from_user((to), (from), (n))
283#define __copy_to_user(to, from, n) \
284 __generic_copy_to_user_nocheck((to), (from), (n))
285#define __copy_from_user(to, from, n) \
286 __generic_copy_from_user_nocheck((to), (from), (n))
Chris Zankel9a8fd552005-06-23 22:01:26 -0700287#define __copy_to_user_inatomic __copy_to_user
288#define __copy_from_user_inatomic __copy_from_user
289
290
291/*
292 * We need to return the number of bytes not cleared. Our memset()
293 * returns zero if a problem occurs while accessing user-space memory.
294 * In that event, return no memory cleared. Otherwise, zero for
295 * success.
296 */
297
Adrian Bunkd99cf712005-09-03 15:57:53 -0700298static inline unsigned long
Chris Zankel9a8fd552005-06-23 22:01:26 -0700299__xtensa_clear_user(void *addr, unsigned long size)
300{
301 if ( ! memset(addr, 0, size) )
302 return size;
303 return 0;
304}
305
Adrian Bunkd99cf712005-09-03 15:57:53 -0700306static inline unsigned long
Chris Zankel9a8fd552005-06-23 22:01:26 -0700307clear_user(void *addr, unsigned long size)
308{
309 if (access_ok(VERIFY_WRITE, addr, size))
310 return __xtensa_clear_user(addr, size);
311 return size ? -EFAULT : 0;
312}
313
314#define __clear_user __xtensa_clear_user
315
316
317extern long __strncpy_user(char *, const char *, long);
318#define __strncpy_from_user __strncpy_user
319
Adrian Bunkd99cf712005-09-03 15:57:53 -0700320static inline long
Chris Zankel9a8fd552005-06-23 22:01:26 -0700321strncpy_from_user(char *dst, const char *src, long count)
322{
323 if (access_ok(VERIFY_READ, src, 1))
324 return __strncpy_from_user(dst, src, count);
325 return -EFAULT;
326}
327
328
329#define strlen_user(str) strnlen_user((str), TASK_SIZE - 1)
330
331/*
332 * Return the size of a string (including the ending 0!)
333 */
334extern long __strnlen_user(const char *, long);
335
Adrian Bunkd99cf712005-09-03 15:57:53 -0700336static inline long strnlen_user(const char *str, long len)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700337{
338 unsigned long top = __kernel_ok ? ~0UL : TASK_SIZE - 1;
339
340 if ((unsigned long)str > top)
341 return 0;
342 return __strnlen_user(str, len);
343}
344
345
346struct exception_table_entry
347{
348 unsigned long insn, fixup;
349};
350
Chris Zankel9a8fd552005-06-23 22:01:26 -0700351#endif /* _XTENSA_UACCESS_H */