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
7  * space for the kernel. This header file provides functions such as:
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_ASM_UACCESS_H
17 #define _XTENSA_ASM_UACCESS_H
18 
19 #include <linux/errno.h>
20 #include <asm/types.h>
21 
22 #include <asm/current.h>
23 #include <asm/asm-offsets.h>
24 #include <asm/processor.h>
25 
26 /*
27  * These assembly macros mirror the C macros in asm/uaccess.h.  They
28  * should always have identical functionality.  See
29  * arch/xtensa/kernel/sys.S for usage.
30  */
31 
32 #define KERNEL_DS	0
33 #define USER_DS		1
34 
35 #define get_ds		(KERNEL_DS)
36 
37 /*
38  * get_fs reads current->thread.current_ds into a register.
39  * On Entry:
40  * 	<ad>	anything
41  * 	<sp>	stack
42  * On Exit:
43  * 	<ad>	contains current->thread.current_ds
44  */
45 	.macro	get_fs	ad, sp
46 	GET_CURRENT(\ad,\sp)
47 #if THREAD_CURRENT_DS > 1020
48 	addi	\ad, \ad, TASK_THREAD
49 	l32i	\ad, \ad, THREAD_CURRENT_DS - TASK_THREAD
50 #else
51 	l32i	\ad, \ad, THREAD_CURRENT_DS
52 #endif
53 	.endm
54 
55 /*
56  * set_fs sets current->thread.current_ds to some value.
57  * On Entry:
58  *	<at>	anything (temp register)
59  *	<av>	value to write
60  *	<sp>	stack
61  * On Exit:
62  *	<at>	destroyed (actually, current)
63  *	<av>	preserved, value to write
64  */
65 	.macro	set_fs	at, av, sp
66 	GET_CURRENT(\at,\sp)
67 	s32i	\av, \at, THREAD_CURRENT_DS
68 	.endm
69 
70 /*
71  * kernel_ok determines whether we should bypass addr/size checking.
72  * See the equivalent C-macro version below for clarity.
73  * On success, kernel_ok branches to a label indicated by parameter
74  * <success>.  This implies that the macro falls through to the next
75  * insruction on an error.
76  *
77  * Note that while this macro can be used independently, we designed
78  * in for optimal use in the access_ok macro below (i.e., we fall
79  * through on error).
80  *
81  * On Entry:
82  * 	<at>		anything (temp register)
83  * 	<success>	label to branch to on success; implies
84  * 			fall-through macro on error
85  * 	<sp>		stack pointer
86  * On Exit:
87  * 	<at>		destroyed (actually, current->thread.current_ds)
88  */
89 
90 #if ((KERNEL_DS != 0) || (USER_DS == 0))
91 # error Assembly macro kernel_ok fails
92 #endif
93 	.macro	kernel_ok  at, sp, success
94 	get_fs	\at, \sp
95 	beqz	\at, \success
96 	.endm
97 
98 /*
99  * user_ok determines whether the access to user-space memory is allowed.
100  * See the equivalent C-macro version below for clarity.
101  *
102  * On error, user_ok branches to a label indicated by parameter
103  * <error>.  This implies that the macro falls through to the next
104  * instruction on success.
105  *
106  * Note that while this macro can be used independently, we designed
107  * in for optimal use in the access_ok macro below (i.e., we fall
108  * through on success).
109  *
110  * On Entry:
111  * 	<aa>	register containing memory address
112  * 	<as>	register containing memory size
113  * 	<at>	temp register
114  * 	<error>	label to branch to on error; implies fall-through
115  * 		macro on success
116  * On Exit:
117  * 	<aa>	preserved
118  * 	<as>	preserved
119  * 	<at>	destroyed (actually, (TASK_SIZE + 1 - size))
120  */
121 	.macro	user_ok	aa, as, at, error
122 	movi	\at, __XTENSA_UL_CONST(TASK_SIZE)
123 	bgeu	\as, \at, \error
124 	sub	\at, \at, \as
125 	bgeu	\aa, \at, \error
126 	.endm
127 
128 /*
129  * access_ok determines whether a memory access is allowed.  See the
130  * equivalent C-macro version below for clarity.
131  *
132  * On error, access_ok branches to a label indicated by parameter
133  * <error>.  This implies that the macro falls through to the next
134  * instruction on success.
135  *
136  * Note that we assume success is the common case, and we optimize the
137  * branch fall-through case on success.
138  *
139  * On Entry:
140  * 	<aa>	register containing memory address
141  * 	<as>	register containing memory size
142  * 	<at>	temp register
143  * 	<sp>
144  * 	<error>	label to branch to on error; implies fall-through
145  * 		macro on success
146  * On Exit:
147  * 	<aa>	preserved
148  * 	<as>	preserved
149  * 	<at>	destroyed
150  */
151 	.macro	access_ok  aa, as, at, sp, error
152 	kernel_ok  \at, \sp, .Laccess_ok_\@
153 	user_ok    \aa, \as, \at, \error
154 .Laccess_ok_\@:
155 	.endm
156 
157 #endif	/* _XTENSA_ASM_UACCESS_H */
158