xref: /openbmc/linux/arch/xtensa/include/asm/uaccess.h (revision 90b03f5052be92ab0ba0aa36abcf33a207706866)
1367b8112SChris Zankel /*
2367b8112SChris Zankel  * include/asm-xtensa/uaccess.h
3367b8112SChris Zankel  *
4367b8112SChris Zankel  * User space memory access functions
5367b8112SChris Zankel  *
6367b8112SChris Zankel  * These routines provide basic accessing functions to the user memory
7eef35c2dSStefan Weil  * space for the kernel. This header file provides functions such as:
8367b8112SChris Zankel  *
9367b8112SChris Zankel  * This file is subject to the terms and conditions of the GNU General Public
10367b8112SChris Zankel  * License.  See the file "COPYING" in the main directory of this archive
11367b8112SChris Zankel  * for more details.
12367b8112SChris Zankel  *
13367b8112SChris Zankel  * Copyright (C) 2001 - 2005 Tensilica Inc.
14367b8112SChris Zankel  */
15367b8112SChris Zankel 
16367b8112SChris Zankel #ifndef _XTENSA_UACCESS_H
17367b8112SChris Zankel #define _XTENSA_UACCESS_H
18367b8112SChris Zankel 
19367b8112SChris Zankel #include <linux/errno.h>
20*90b03f50SWANG Cong #include <linux/prefetch.h>
21367b8112SChris Zankel 
22367b8112SChris Zankel #define VERIFY_READ    0
23367b8112SChris Zankel #define VERIFY_WRITE   1
24367b8112SChris Zankel 
25367b8112SChris Zankel #ifdef __ASSEMBLY__
26367b8112SChris Zankel 
27367b8112SChris Zankel #include <asm/current.h>
28367b8112SChris Zankel #include <asm/asm-offsets.h>
29367b8112SChris Zankel #include <asm/processor.h>
30367b8112SChris Zankel #include <asm/types.h>
31367b8112SChris Zankel 
32367b8112SChris Zankel /*
33367b8112SChris Zankel  * These assembly macros mirror the C macros that follow below.  They
34367b8112SChris Zankel  * should always have identical functionality.  See
35367b8112SChris Zankel  * arch/xtensa/kernel/sys.S for usage.
36367b8112SChris Zankel  */
37367b8112SChris Zankel 
38367b8112SChris Zankel #define KERNEL_DS	0
39367b8112SChris Zankel #define USER_DS		1
40367b8112SChris Zankel 
41367b8112SChris Zankel #define get_ds		(KERNEL_DS)
42367b8112SChris Zankel 
43367b8112SChris Zankel /*
44367b8112SChris Zankel  * get_fs reads current->thread.current_ds into a register.
45367b8112SChris Zankel  * On Entry:
46367b8112SChris Zankel  * 	<ad>	anything
47367b8112SChris Zankel  * 	<sp>	stack
48367b8112SChris Zankel  * On Exit:
49367b8112SChris Zankel  * 	<ad>	contains current->thread.current_ds
50367b8112SChris Zankel  */
51367b8112SChris Zankel 	.macro	get_fs	ad, sp
52367b8112SChris Zankel 	GET_CURRENT(\ad,\sp)
53367b8112SChris Zankel 	l32i	\ad, \ad, THREAD_CURRENT_DS
54367b8112SChris Zankel 	.endm
55367b8112SChris Zankel 
56367b8112SChris Zankel /*
57367b8112SChris Zankel  * set_fs sets current->thread.current_ds to some value.
58367b8112SChris Zankel  * On Entry:
59367b8112SChris Zankel  *	<at>	anything (temp register)
60367b8112SChris Zankel  *	<av>	value to write
61367b8112SChris Zankel  *	<sp>	stack
62367b8112SChris Zankel  * On Exit:
63367b8112SChris Zankel  *	<at>	destroyed (actually, current)
64367b8112SChris Zankel  *	<av>	preserved, value to write
65367b8112SChris Zankel  */
66367b8112SChris Zankel 	.macro	set_fs	at, av, sp
67367b8112SChris Zankel 	GET_CURRENT(\at,\sp)
68367b8112SChris Zankel 	s32i	\av, \at, THREAD_CURRENT_DS
69367b8112SChris Zankel 	.endm
70367b8112SChris Zankel 
71367b8112SChris Zankel /*
72367b8112SChris Zankel  * kernel_ok determines whether we should bypass addr/size checking.
73367b8112SChris Zankel  * See the equivalent C-macro version below for clarity.
74367b8112SChris Zankel  * On success, kernel_ok branches to a label indicated by parameter
75367b8112SChris Zankel  * <success>.  This implies that the macro falls through to the next
76367b8112SChris Zankel  * insruction on an error.
77367b8112SChris Zankel  *
78367b8112SChris Zankel  * Note that while this macro can be used independently, we designed
79367b8112SChris Zankel  * in for optimal use in the access_ok macro below (i.e., we fall
80367b8112SChris Zankel  * through on error).
81367b8112SChris Zankel  *
82367b8112SChris Zankel  * On Entry:
83367b8112SChris Zankel  * 	<at>		anything (temp register)
84367b8112SChris Zankel  * 	<success>	label to branch to on success; implies
85367b8112SChris Zankel  * 			fall-through macro on error
86367b8112SChris Zankel  * 	<sp>		stack pointer
87367b8112SChris Zankel  * On Exit:
88367b8112SChris Zankel  * 	<at>		destroyed (actually, current->thread.current_ds)
89367b8112SChris Zankel  */
90367b8112SChris Zankel 
91367b8112SChris Zankel #if ((KERNEL_DS != 0) || (USER_DS == 0))
92367b8112SChris Zankel # error Assembly macro kernel_ok fails
93367b8112SChris Zankel #endif
94367b8112SChris Zankel 	.macro	kernel_ok  at, sp, success
95367b8112SChris Zankel 	get_fs	\at, \sp
96367b8112SChris Zankel 	beqz	\at, \success
97367b8112SChris Zankel 	.endm
98367b8112SChris Zankel 
99367b8112SChris Zankel /*
100367b8112SChris Zankel  * user_ok determines whether the access to user-space memory is allowed.
101367b8112SChris Zankel  * See the equivalent C-macro version below for clarity.
102367b8112SChris Zankel  *
103367b8112SChris Zankel  * On error, user_ok branches to a label indicated by parameter
104367b8112SChris Zankel  * <error>.  This implies that the macro falls through to the next
105367b8112SChris Zankel  * instruction on success.
106367b8112SChris Zankel  *
107367b8112SChris Zankel  * Note that while this macro can be used independently, we designed
108367b8112SChris Zankel  * in for optimal use in the access_ok macro below (i.e., we fall
109367b8112SChris Zankel  * through on success).
110367b8112SChris Zankel  *
111367b8112SChris Zankel  * On Entry:
112367b8112SChris Zankel  * 	<aa>	register containing memory address
113367b8112SChris Zankel  * 	<as>	register containing memory size
114367b8112SChris Zankel  * 	<at>	temp register
115367b8112SChris Zankel  * 	<error>	label to branch to on error; implies fall-through
116367b8112SChris Zankel  * 		macro on success
117367b8112SChris Zankel  * On Exit:
118367b8112SChris Zankel  * 	<aa>	preserved
119367b8112SChris Zankel  * 	<as>	preserved
120367b8112SChris Zankel  * 	<at>	destroyed (actually, (TASK_SIZE + 1 - size))
121367b8112SChris Zankel  */
122367b8112SChris Zankel 	.macro	user_ok	aa, as, at, error
123367b8112SChris Zankel 	movi	\at, __XTENSA_UL_CONST(TASK_SIZE)
124367b8112SChris Zankel 	bgeu	\as, \at, \error
125367b8112SChris Zankel 	sub	\at, \at, \as
126367b8112SChris Zankel 	bgeu	\aa, \at, \error
127367b8112SChris Zankel 	.endm
128367b8112SChris Zankel 
129367b8112SChris Zankel /*
130367b8112SChris Zankel  * access_ok determines whether a memory access is allowed.  See the
131367b8112SChris Zankel  * equivalent C-macro version below for clarity.
132367b8112SChris Zankel  *
133367b8112SChris Zankel  * On error, access_ok branches to a label indicated by parameter
134367b8112SChris Zankel  * <error>.  This implies that the macro falls through to the next
135367b8112SChris Zankel  * instruction on success.
136367b8112SChris Zankel  *
137367b8112SChris Zankel  * Note that we assume success is the common case, and we optimize the
138367b8112SChris Zankel  * branch fall-through case on success.
139367b8112SChris Zankel  *
140367b8112SChris Zankel  * On Entry:
141367b8112SChris Zankel  * 	<aa>	register containing memory address
142367b8112SChris Zankel  * 	<as>	register containing memory size
143367b8112SChris Zankel  * 	<at>	temp register
144367b8112SChris Zankel  * 	<sp>
145367b8112SChris Zankel  * 	<error>	label to branch to on error; implies fall-through
146367b8112SChris Zankel  * 		macro on success
147367b8112SChris Zankel  * On Exit:
148367b8112SChris Zankel  * 	<aa>	preserved
149367b8112SChris Zankel  * 	<as>	preserved
150367b8112SChris Zankel  * 	<at>	destroyed
151367b8112SChris Zankel  */
152367b8112SChris Zankel 	.macro	access_ok  aa, as, at, sp, error
153367b8112SChris Zankel 	kernel_ok  \at, \sp, .Laccess_ok_\@
154367b8112SChris Zankel 	user_ok    \aa, \as, \at, \error
155367b8112SChris Zankel .Laccess_ok_\@:
156367b8112SChris Zankel 	.endm
157367b8112SChris Zankel 
158367b8112SChris Zankel #else /* __ASSEMBLY__ not defined */
159367b8112SChris Zankel 
160367b8112SChris Zankel #include <linux/sched.h>
161367b8112SChris Zankel #include <asm/types.h>
162367b8112SChris Zankel 
163367b8112SChris Zankel /*
164367b8112SChris Zankel  * The fs value determines whether argument validity checking should
165367b8112SChris Zankel  * be performed or not.  If get_fs() == USER_DS, checking is
166367b8112SChris Zankel  * performed, with get_fs() == KERNEL_DS, checking is bypassed.
167367b8112SChris Zankel  *
168367b8112SChris Zankel  * For historical reasons (Data Segment Register?), these macros are
169367b8112SChris Zankel  * grossly misnamed.
170367b8112SChris Zankel  */
171367b8112SChris Zankel 
172367b8112SChris Zankel #define KERNEL_DS	((mm_segment_t) { 0 })
173367b8112SChris Zankel #define USER_DS		((mm_segment_t) { 1 })
174367b8112SChris Zankel 
175367b8112SChris Zankel #define get_ds()	(KERNEL_DS)
176367b8112SChris Zankel #define get_fs()	(current->thread.current_ds)
177367b8112SChris Zankel #define set_fs(val)	(current->thread.current_ds = (val))
178367b8112SChris Zankel 
179367b8112SChris Zankel #define segment_eq(a,b)	((a).seg == (b).seg)
180367b8112SChris Zankel 
181367b8112SChris Zankel #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
182367b8112SChris Zankel #define __user_ok(addr,size) (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
183367b8112SChris Zankel #define __access_ok(addr,size) (__kernel_ok || __user_ok((addr),(size)))
184367b8112SChris Zankel #define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))
185367b8112SChris Zankel 
186367b8112SChris Zankel /*
187367b8112SChris Zankel  * These are the main single-value transfer routines.  They
188367b8112SChris Zankel  * automatically use the right size if we just have the right pointer
189367b8112SChris Zankel  * type.
190367b8112SChris Zankel  *
191367b8112SChris Zankel  * This gets kind of ugly. We want to return _two_ values in
192367b8112SChris Zankel  * "get_user()" and yet we don't want to do any pointers, because that
193367b8112SChris Zankel  * is too much of a performance impact. Thus we have a few rather ugly
194367b8112SChris Zankel  * macros here, and hide all the uglyness from the user.
195367b8112SChris Zankel  *
196367b8112SChris Zankel  * Careful to not
197367b8112SChris Zankel  * (a) re-use the arguments for side effects (sizeof is ok)
198367b8112SChris Zankel  * (b) require any knowledge of processes at this stage
199367b8112SChris Zankel  */
200367b8112SChris Zankel #define put_user(x,ptr)	__put_user_check((x),(ptr),sizeof(*(ptr)))
201367b8112SChris Zankel #define get_user(x,ptr) __get_user_check((x),(ptr),sizeof(*(ptr)))
202367b8112SChris Zankel 
203367b8112SChris Zankel /*
204367b8112SChris Zankel  * The "__xxx" versions of the user access functions are versions that
205367b8112SChris Zankel  * do not verify the address space, that must have been done previously
206367b8112SChris Zankel  * with a separate "access_ok()" call (this is used when we do multiple
207367b8112SChris Zankel  * accesses to the same area of user memory).
208367b8112SChris Zankel  */
209367b8112SChris Zankel #define __put_user(x,ptr) __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
210367b8112SChris Zankel #define __get_user(x,ptr) __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
211367b8112SChris Zankel 
212367b8112SChris Zankel 
213367b8112SChris Zankel extern long __put_user_bad(void);
214367b8112SChris Zankel 
215367b8112SChris Zankel #define __put_user_nocheck(x,ptr,size)			\
216367b8112SChris Zankel ({							\
217367b8112SChris Zankel 	long __pu_err;					\
218367b8112SChris Zankel 	__put_user_size((x),(ptr),(size),__pu_err);	\
219367b8112SChris Zankel 	__pu_err;					\
220367b8112SChris Zankel })
221367b8112SChris Zankel 
222367b8112SChris Zankel #define __put_user_check(x,ptr,size)				\
223367b8112SChris Zankel ({								\
224367b8112SChris Zankel 	long __pu_err = -EFAULT;				\
225367b8112SChris Zankel 	__typeof__(*(ptr)) *__pu_addr = (ptr);			\
226367b8112SChris Zankel 	if (access_ok(VERIFY_WRITE,__pu_addr,size))		\
227367b8112SChris Zankel 		__put_user_size((x),__pu_addr,(size),__pu_err);	\
228367b8112SChris Zankel 	__pu_err;						\
229367b8112SChris Zankel })
230367b8112SChris Zankel 
231367b8112SChris Zankel #define __put_user_size(x,ptr,size,retval)				\
232367b8112SChris Zankel do {									\
233367b8112SChris Zankel 	int __cb;							\
234367b8112SChris Zankel 	retval = 0;							\
235367b8112SChris Zankel 	switch (size) {							\
236367b8112SChris Zankel         case 1: __put_user_asm(x,ptr,retval,1,"s8i",__cb);  break;	\
237367b8112SChris Zankel         case 2: __put_user_asm(x,ptr,retval,2,"s16i",__cb); break;	\
238367b8112SChris Zankel         case 4: __put_user_asm(x,ptr,retval,4,"s32i",__cb); break;	\
239367b8112SChris Zankel         case 8: {							\
240367b8112SChris Zankel 		     __typeof__(*ptr) __v64 = x;			\
241367b8112SChris Zankel 		     retval = __copy_to_user(ptr,&__v64,8);		\
242367b8112SChris Zankel 		     break;						\
243367b8112SChris Zankel 	        }							\
244367b8112SChris Zankel 	default: __put_user_bad();					\
245367b8112SChris Zankel 	}								\
246367b8112SChris Zankel } while (0)
247367b8112SChris Zankel 
248367b8112SChris Zankel 
249367b8112SChris Zankel /*
250367b8112SChris Zankel  * Consider a case of a user single load/store would cause both an
251367b8112SChris Zankel  * unaligned exception and an MMU-related exception (unaligned
252367b8112SChris Zankel  * exceptions happen first):
253367b8112SChris Zankel  *
254367b8112SChris Zankel  * User code passes a bad variable ptr to a system call.
255367b8112SChris Zankel  * Kernel tries to access the variable.
256367b8112SChris Zankel  * Unaligned exception occurs.
257367b8112SChris Zankel  * Unaligned exception handler tries to make aligned accesses.
258367b8112SChris Zankel  * Double exception occurs for MMU-related cause (e.g., page not mapped).
259367b8112SChris Zankel  * do_page_fault() thinks the fault address belongs to the kernel, not the
260367b8112SChris Zankel  * user, and panics.
261367b8112SChris Zankel  *
262367b8112SChris Zankel  * The kernel currently prohibits user unaligned accesses.  We use the
263367b8112SChris Zankel  * __check_align_* macros to check for unaligned addresses before
264367b8112SChris Zankel  * accessing user space so we don't crash the kernel.  Both
265367b8112SChris Zankel  * __put_user_asm and __get_user_asm use these alignment macros, so
266367b8112SChris Zankel  * macro-specific labels such as 0f, 1f, %0, %2, and %3 must stay in
267367b8112SChris Zankel  * sync.
268367b8112SChris Zankel  */
269367b8112SChris Zankel 
270367b8112SChris Zankel #define __check_align_1  ""
271367b8112SChris Zankel 
272367b8112SChris Zankel #define __check_align_2				\
273367b8112SChris Zankel 	"   _bbci.l %3,  0, 1f		\n"	\
274367b8112SChris Zankel 	"   movi    %0, %4		\n"	\
275367b8112SChris Zankel 	"   _j      2f			\n"
276367b8112SChris Zankel 
277367b8112SChris Zankel #define __check_align_4				\
278367b8112SChris Zankel 	"   _bbsi.l %3,  0, 0f		\n"	\
279367b8112SChris Zankel 	"   _bbci.l %3,  1, 1f		\n"	\
280367b8112SChris Zankel 	"0: movi    %0, %4		\n"	\
281367b8112SChris Zankel 	"   _j      2f			\n"
282367b8112SChris Zankel 
283367b8112SChris Zankel 
284367b8112SChris Zankel /*
285367b8112SChris Zankel  * We don't tell gcc that we are accessing memory, but this is OK
286367b8112SChris Zankel  * because we do not write to any memory gcc knows about, so there
287367b8112SChris Zankel  * are no aliasing issues.
288367b8112SChris Zankel  *
289367b8112SChris Zankel  * WARNING: If you modify this macro at all, verify that the
290367b8112SChris Zankel  * __check_align_* macros still work.
291367b8112SChris Zankel  */
292367b8112SChris Zankel #define __put_user_asm(x, addr, err, align, insn, cb)	\
293367b8112SChris Zankel    __asm__ __volatile__(				\
294367b8112SChris Zankel 	__check_align_##align				\
295367b8112SChris Zankel 	"1: "insn"  %2, %3, 0		\n"		\
296367b8112SChris Zankel 	"2:				\n"		\
297367b8112SChris Zankel 	"   .section  .fixup,\"ax\"	\n"		\
298367b8112SChris Zankel 	"   .align 4			\n"		\
299367b8112SChris Zankel 	"4:				\n"		\
300367b8112SChris Zankel 	"   .long  2b			\n"		\
301367b8112SChris Zankel 	"5:				\n"		\
302367b8112SChris Zankel 	"   l32r   %1, 4b		\n"		\
303367b8112SChris Zankel         "   movi   %0, %4		\n"		\
304367b8112SChris Zankel         "   jx     %1			\n"		\
305367b8112SChris Zankel 	"   .previous			\n"		\
306367b8112SChris Zankel 	"   .section  __ex_table,\"a\"	\n"		\
307367b8112SChris Zankel 	"   .long	1b, 5b		\n"		\
308367b8112SChris Zankel 	"   .previous"					\
309367b8112SChris Zankel 	:"=r" (err), "=r" (cb)				\
310367b8112SChris Zankel 	:"r" ((int)(x)), "r" (addr), "i" (-EFAULT), "0" (err))
311367b8112SChris Zankel 
312367b8112SChris Zankel #define __get_user_nocheck(x,ptr,size)				\
313367b8112SChris Zankel ({								\
314367b8112SChris Zankel 	long __gu_err, __gu_val;				\
315367b8112SChris Zankel 	__get_user_size(__gu_val,(ptr),(size),__gu_err);	\
316367b8112SChris Zankel 	(x) = (__typeof__(*(ptr)))__gu_val;			\
317367b8112SChris Zankel 	__gu_err;						\
318367b8112SChris Zankel })
319367b8112SChris Zankel 
320367b8112SChris Zankel #define __get_user_check(x,ptr,size)					\
321367b8112SChris Zankel ({									\
322367b8112SChris Zankel 	long __gu_err = -EFAULT, __gu_val = 0;				\
323367b8112SChris Zankel 	const __typeof__(*(ptr)) *__gu_addr = (ptr);			\
324367b8112SChris Zankel 	if (access_ok(VERIFY_READ,__gu_addr,size))			\
325367b8112SChris Zankel 		__get_user_size(__gu_val,__gu_addr,(size),__gu_err);	\
326367b8112SChris Zankel 	(x) = (__typeof__(*(ptr)))__gu_val;				\
327367b8112SChris Zankel 	__gu_err;							\
328367b8112SChris Zankel })
329367b8112SChris Zankel 
330367b8112SChris Zankel extern long __get_user_bad(void);
331367b8112SChris Zankel 
332367b8112SChris Zankel #define __get_user_size(x,ptr,size,retval)				\
333367b8112SChris Zankel do {									\
334367b8112SChris Zankel 	int __cb;							\
335367b8112SChris Zankel 	retval = 0;							\
336367b8112SChris Zankel         switch (size) {							\
337367b8112SChris Zankel           case 1: __get_user_asm(x,ptr,retval,1,"l8ui",__cb);  break;	\
338367b8112SChris Zankel           case 2: __get_user_asm(x,ptr,retval,2,"l16ui",__cb); break;	\
339367b8112SChris Zankel           case 4: __get_user_asm(x,ptr,retval,4,"l32i",__cb);  break;	\
340367b8112SChris Zankel           case 8: retval = __copy_from_user(&x,ptr,8);    break;	\
341367b8112SChris Zankel           default: (x) = __get_user_bad();				\
342367b8112SChris Zankel         }								\
343367b8112SChris Zankel } while (0)
344367b8112SChris Zankel 
345367b8112SChris Zankel 
346367b8112SChris Zankel /*
347367b8112SChris Zankel  * WARNING: If you modify this macro at all, verify that the
348367b8112SChris Zankel  * __check_align_* macros still work.
349367b8112SChris Zankel  */
350367b8112SChris Zankel #define __get_user_asm(x, addr, err, align, insn, cb) \
351367b8112SChris Zankel    __asm__ __volatile__(			\
352367b8112SChris Zankel 	__check_align_##align			\
353367b8112SChris Zankel 	"1: "insn"  %2, %3, 0		\n"	\
354367b8112SChris Zankel 	"2:				\n"	\
355367b8112SChris Zankel 	"   .section  .fixup,\"ax\"	\n"	\
356367b8112SChris Zankel 	"   .align 4			\n"	\
357367b8112SChris Zankel 	"4:				\n"	\
358367b8112SChris Zankel 	"   .long  2b			\n"	\
359367b8112SChris Zankel 	"5:				\n"	\
360367b8112SChris Zankel 	"   l32r   %1, 4b		\n"	\
361367b8112SChris Zankel 	"   movi   %2, 0		\n"	\
362367b8112SChris Zankel         "   movi   %0, %4		\n"	\
363367b8112SChris Zankel         "   jx     %1			\n"	\
364367b8112SChris Zankel 	"   .previous			\n"	\
365367b8112SChris Zankel 	"   .section  __ex_table,\"a\"	\n"	\
366367b8112SChris Zankel 	"   .long	1b, 5b		\n"	\
367367b8112SChris Zankel 	"   .previous"				\
368367b8112SChris Zankel 	:"=r" (err), "=r" (cb), "=r" (x)	\
369367b8112SChris Zankel 	:"r" (addr), "i" (-EFAULT), "0" (err))
370367b8112SChris Zankel 
371367b8112SChris Zankel 
372367b8112SChris Zankel /*
373367b8112SChris Zankel  * Copy to/from user space
374367b8112SChris Zankel  */
375367b8112SChris Zankel 
376367b8112SChris Zankel /*
377367b8112SChris Zankel  * We use a generic, arbitrary-sized copy subroutine.  The Xtensa
378367b8112SChris Zankel  * architecture would cause heavy code bloat if we tried to inline
379367b8112SChris Zankel  * these functions and provide __constant_copy_* equivalents like the
380367b8112SChris Zankel  * i386 versions.  __xtensa_copy_user is quite efficient.  See the
381367b8112SChris Zankel  * .fixup section of __xtensa_copy_user for a discussion on the
382367b8112SChris Zankel  * X_zeroing equivalents for Xtensa.
383367b8112SChris Zankel  */
384367b8112SChris Zankel 
385367b8112SChris Zankel extern unsigned __xtensa_copy_user(void *to, const void *from, unsigned n);
386367b8112SChris Zankel #define __copy_user(to,from,size) __xtensa_copy_user(to,from,size)
387367b8112SChris Zankel 
388367b8112SChris Zankel 
389367b8112SChris Zankel static inline unsigned long
390367b8112SChris Zankel __generic_copy_from_user_nocheck(void *to, const void *from, unsigned long n)
391367b8112SChris Zankel {
392367b8112SChris Zankel 	return __copy_user(to,from,n);
393367b8112SChris Zankel }
394367b8112SChris Zankel 
395367b8112SChris Zankel static inline unsigned long
396367b8112SChris Zankel __generic_copy_to_user_nocheck(void *to, const void *from, unsigned long n)
397367b8112SChris Zankel {
398367b8112SChris Zankel 	return __copy_user(to,from,n);
399367b8112SChris Zankel }
400367b8112SChris Zankel 
401367b8112SChris Zankel static inline unsigned long
402367b8112SChris Zankel __generic_copy_to_user(void *to, const void *from, unsigned long n)
403367b8112SChris Zankel {
404367b8112SChris Zankel 	prefetch(from);
405367b8112SChris Zankel 	if (access_ok(VERIFY_WRITE, to, n))
406367b8112SChris Zankel 		return __copy_user(to,from,n);
407367b8112SChris Zankel 	return n;
408367b8112SChris Zankel }
409367b8112SChris Zankel 
410367b8112SChris Zankel static inline unsigned long
411367b8112SChris Zankel __generic_copy_from_user(void *to, const void *from, unsigned long n)
412367b8112SChris Zankel {
413367b8112SChris Zankel 	prefetchw(to);
414367b8112SChris Zankel 	if (access_ok(VERIFY_READ, from, n))
415367b8112SChris Zankel 		return __copy_user(to,from,n);
416367b8112SChris Zankel 	else
417367b8112SChris Zankel 		memset(to, 0, n);
418367b8112SChris Zankel 	return n;
419367b8112SChris Zankel }
420367b8112SChris Zankel 
421367b8112SChris Zankel #define copy_to_user(to,from,n) __generic_copy_to_user((to),(from),(n))
422367b8112SChris Zankel #define copy_from_user(to,from,n) __generic_copy_from_user((to),(from),(n))
423367b8112SChris Zankel #define __copy_to_user(to,from,n) __generic_copy_to_user_nocheck((to),(from),(n))
424367b8112SChris Zankel #define __copy_from_user(to,from,n) __generic_copy_from_user_nocheck((to),(from),(n))
425367b8112SChris Zankel #define __copy_to_user_inatomic __copy_to_user
426367b8112SChris Zankel #define __copy_from_user_inatomic __copy_from_user
427367b8112SChris Zankel 
428367b8112SChris Zankel 
429367b8112SChris Zankel /*
430367b8112SChris Zankel  * We need to return the number of bytes not cleared.  Our memset()
431367b8112SChris Zankel  * returns zero if a problem occurs while accessing user-space memory.
432367b8112SChris Zankel  * In that event, return no memory cleared.  Otherwise, zero for
433367b8112SChris Zankel  * success.
434367b8112SChris Zankel  */
435367b8112SChris Zankel 
436367b8112SChris Zankel static inline unsigned long
437367b8112SChris Zankel __xtensa_clear_user(void *addr, unsigned long size)
438367b8112SChris Zankel {
439367b8112SChris Zankel 	if ( ! memset(addr, 0, size) )
440367b8112SChris Zankel 		return size;
441367b8112SChris Zankel 	return 0;
442367b8112SChris Zankel }
443367b8112SChris Zankel 
444367b8112SChris Zankel static inline unsigned long
445367b8112SChris Zankel clear_user(void *addr, unsigned long size)
446367b8112SChris Zankel {
447367b8112SChris Zankel 	if (access_ok(VERIFY_WRITE, addr, size))
448367b8112SChris Zankel 		return __xtensa_clear_user(addr, size);
449367b8112SChris Zankel 	return size ? -EFAULT : 0;
450367b8112SChris Zankel }
451367b8112SChris Zankel 
452367b8112SChris Zankel #define __clear_user  __xtensa_clear_user
453367b8112SChris Zankel 
454367b8112SChris Zankel 
455367b8112SChris Zankel extern long __strncpy_user(char *, const char *, long);
456367b8112SChris Zankel #define __strncpy_from_user __strncpy_user
457367b8112SChris Zankel 
458367b8112SChris Zankel static inline long
459367b8112SChris Zankel strncpy_from_user(char *dst, const char *src, long count)
460367b8112SChris Zankel {
461367b8112SChris Zankel 	if (access_ok(VERIFY_READ, src, 1))
462367b8112SChris Zankel 		return __strncpy_from_user(dst, src, count);
463367b8112SChris Zankel 	return -EFAULT;
464367b8112SChris Zankel }
465367b8112SChris Zankel 
466367b8112SChris Zankel 
467367b8112SChris Zankel #define strlen_user(str) strnlen_user((str), TASK_SIZE - 1)
468367b8112SChris Zankel 
469367b8112SChris Zankel /*
470367b8112SChris Zankel  * Return the size of a string (including the ending 0!)
471367b8112SChris Zankel  */
472367b8112SChris Zankel extern long __strnlen_user(const char *, long);
473367b8112SChris Zankel 
474367b8112SChris Zankel static inline long strnlen_user(const char *str, long len)
475367b8112SChris Zankel {
476367b8112SChris Zankel 	unsigned long top = __kernel_ok ? ~0UL : TASK_SIZE - 1;
477367b8112SChris Zankel 
478367b8112SChris Zankel 	if ((unsigned long)str > top)
479367b8112SChris Zankel 		return 0;
480367b8112SChris Zankel 	return __strnlen_user(str, len);
481367b8112SChris Zankel }
482367b8112SChris Zankel 
483367b8112SChris Zankel 
484367b8112SChris Zankel struct exception_table_entry
485367b8112SChris Zankel {
486367b8112SChris Zankel 	unsigned long insn, fixup;
487367b8112SChris Zankel };
488367b8112SChris Zankel 
489367b8112SChris Zankel /* Returns 0 if exception not found and fixup.unit otherwise.  */
490367b8112SChris Zankel 
491367b8112SChris Zankel extern unsigned long search_exception_table(unsigned long addr);
492367b8112SChris Zankel extern void sort_exception_table(void);
493367b8112SChris Zankel 
494367b8112SChris Zankel /* Returns the new pc */
495367b8112SChris Zankel #define fixup_exception(map_reg, fixup_unit, pc)                \
496367b8112SChris Zankel ({                                                              \
497367b8112SChris Zankel 	fixup_unit;                                             \
498367b8112SChris Zankel })
499367b8112SChris Zankel 
500367b8112SChris Zankel #endif	/* __ASSEMBLY__ */
501367b8112SChris Zankel #endif	/* _XTENSA_UACCESS_H */
502