1 #ifndef _ASMARM_UACCESS_H
2 #define _ASMARM_UACCESS_H
3 
4 /*
5  * User space memory access functions
6  */
7 #include <linux/sched.h>
8 #include <asm/errno.h>
9 
10 #define VERIFY_READ 0
11 #define VERIFY_WRITE 1
12 
13 /*
14  * The exception table consists of pairs of addresses: the first is the
15  * address of an instruction that is allowed to fault, and the second is
16  * the address at which the program should continue.  No registers are
17  * modified, so it is entirely up to the continuation code to figure out
18  * what to do.
19  *
20  * All the routines below use bits of fixup code that are out of line
21  * with the main instruction path.  This means when everything is well,
22  * we don't even have to jump over them.  Further, they do not intrude
23  * on our cache or tlb entries.
24  */
25 
26 struct exception_table_entry
27 {
28 	unsigned long insn, fixup;
29 };
30 
31 /* Returns 0 if exception not found and fixup otherwise.  */
32 extern unsigned long search_exception_table(unsigned long);
33 
34 #define get_ds()	(KERNEL_DS)
35 #define get_fs()	(current->addr_limit)
36 #define segment_eq(a,b)	((a) == (b))
37 
38 #include <asm/proc/uaccess.h>
39 
40 #define access_ok(type,addr,size)	(__range_ok(addr,size) == 0)
41 
verify_area(int type,const void * addr,unsigned long size)42 static inline int verify_area(int type, const void * addr, unsigned long size)
43 {
44 	return access_ok(type, addr, size) ? 0 : -EFAULT;
45 }
46 
47 /*
48  * Single-value transfer routines.  They automatically use the right
49  * size if we just have the right pointer type.  Note that the functions
50  * which read from user space (*get_*) need to take care not to leak
51  * kernel data even if the calling code is buggy and fails to check
52  * the return value.  This means zeroing out the destination variable
53  * or buffer on error.  Normally this is done out of line by the
54  * fixup code, but there are a few places where it intrudes on the
55  * main code path.  When we only write to user space, there is no
56  * problem.
57  *
58  * The "__xxx" versions of the user access functions do not verify the
59  * address space - it must have been done previously with a separate
60  * "access_ok()" call.
61  *
62  * The "xxx_error" versions set the third argument to EFAULT if an
63  * error occurs, and leave it unchanged on success.  Note that these
64  * versions are void (ie, don't return a value as such).
65  */
66 
67 extern int __get_user_1(void *);
68 extern int __get_user_2(void *);
69 extern int __get_user_4(void *);
70 extern int __get_user_8(void *);
71 extern int __get_user_bad(void);
72 
73 #define __get_user_x(__r1,__p,__e,__s,__i...)				\
74 	   __asm__ __volatile__ ("bl	__get_user_" #__s		\
75 		: "=&r" (__e), "=r" (__r1)				\
76 		: "0" (__p)						\
77 		: __i)
78 
79 #define get_user(x,p)							\
80 	({								\
81 		const register typeof(*(p)) *__p asm("r0") = (p);	\
82 		register typeof(*(p)) __r1 asm("r1");			\
83 		register int __e asm("r0");				\
84 		switch (sizeof(*(p))) {					\
85 		case 1:							\
86 			__get_user_x(__r1, __p, __e, 1, "lr");		\
87 	       		break;						\
88 		case 2:							\
89 			__get_user_x(__r1, __p, __e, 2, "r2", "lr");	\
90 			break;						\
91 		case 4:							\
92 	       		__get_user_x(__r1, __p, __e, 4, "lr");		\
93 			break;						\
94 		case 8:							\
95 			__get_user_x(__r1, __p, __e, 8, "lr");		\
96 	       		break;						\
97 		default: __e = __get_user_bad(); break;			\
98 		}							\
99 		x = __r1;						\
100 		__e;							\
101 	})
102 
103 #define __get_user(x,p)		__get_user_nocheck((x),(p),sizeof(*(p)))
104 #define __get_user_error(x,p,e)	__get_user_nocheck_error((x),(p),sizeof(*(p)),(e))
105 
106 extern int __put_user_1(void *, unsigned int);
107 extern int __put_user_2(void *, unsigned int);
108 extern int __put_user_4(void *, unsigned int);
109 extern int __put_user_8(void *, unsigned long long);
110 extern int __put_user_bad(void);
111 
112 #define __put_user_x(__r1,__p,__e,__s,__i...)				\
113 	   __asm__ __volatile__ ("bl	__put_user_" #__s		\
114 		: "=&r" (__e)						\
115 		: "0" (__p), "r" (__r1)					\
116 		: __i)
117 
118 #define put_user(x,p)							\
119 	({								\
120 		const register typeof(*(p)) __r1 asm("r1") = (x);	\
121 		const register typeof(*(p)) *__p asm("r0") = (p);	\
122 		register int __e asm("r0");				\
123 		switch (sizeof(*(p))) {					\
124 		case 1:							\
125 			__put_user_x(__r1, __p, __e, 1, "r2", "lr");	\
126 			break;						\
127 		case 2:							\
128 			__put_user_x(__r1, __p, __e, 2, "r2", "lr");	\
129 			break;						\
130 		case 4:							\
131 			__put_user_x(__r1, __p, __e, 4, "r2", "lr");	\
132 			break;						\
133 		case 8:							\
134 			__put_user_x(__r1, __p, __e, 8, "ip", "lr");	\
135 			break;						\
136 		default: __e = __put_user_bad(); break;			\
137 		}							\
138 		__e;							\
139 	})
140 
141 #define __put_user(x,p)		__put_user_nocheck((__typeof(*(p)))(x),(p),sizeof(*(p)))
142 #define __put_user_error(x,p,e)	__put_user_nocheck_error((x),(p),sizeof(*(p)),(e))
143 
copy_from_user(void * to,const void * from,unsigned long n)144 static __inline__ unsigned long copy_from_user(void *to, const void *from, unsigned long n)
145 {
146 	if (access_ok(VERIFY_READ, from, n))
147 		__do_copy_from_user(to, from, n);
148 	else /* security hole - plug it */
149 		memzero(to, n);
150 	return n;
151 }
152 
__copy_from_user(void * to,const void * from,unsigned long n)153 static __inline__ unsigned long __copy_from_user(void *to, const void *from, unsigned long n)
154 {
155 	__do_copy_from_user(to, from, n);
156 	return n;
157 }
158 
copy_to_user(void * to,const void * from,unsigned long n)159 static __inline__ unsigned long copy_to_user(void *to, const void *from, unsigned long n)
160 {
161 	if (access_ok(VERIFY_WRITE, to, n))
162 		__do_copy_to_user(to, from, n);
163 	return n;
164 }
165 
__copy_to_user(void * to,const void * from,unsigned long n)166 static __inline__ unsigned long __copy_to_user(void *to, const void *from, unsigned long n)
167 {
168 	__do_copy_to_user(to, from, n);
169 	return n;
170 }
171 
clear_user(void * to,unsigned long n)172 static __inline__ unsigned long clear_user (void *to, unsigned long n)
173 {
174 	if (access_ok(VERIFY_WRITE, to, n))
175 		__do_clear_user(to, n);
176 	return n;
177 }
178 
__clear_user(void * to,unsigned long n)179 static __inline__ unsigned long __clear_user (void *to, unsigned long n)
180 {
181 	__do_clear_user(to, n);
182 	return n;
183 }
184 
strncpy_from_user(char * dst,const char * src,long count)185 static __inline__ long strncpy_from_user (char *dst, const char *src, long count)
186 {
187 	long res = -EFAULT;
188 	if (access_ok(VERIFY_READ, src, 1))
189 		__do_strncpy_from_user(dst, src, count, res);
190 	return res;
191 }
192 
__strncpy_from_user(char * dst,const char * src,long count)193 static __inline__ long __strncpy_from_user (char *dst, const char *src, long count)
194 {
195 	long res;
196 	__do_strncpy_from_user(dst, src, count, res);
197 	return res;
198 }
199 
200 #define strlen_user(s)	strnlen_user(s, ~0UL >> 1)
201 
strnlen_user(const char * s,long n)202 static inline long strnlen_user(const char *s, long n)
203 {
204 	unsigned long res = 0;
205 
206 	if (__addr_ok(s))
207 		__do_strnlen_user(s, n, res);
208 
209 	return res;
210 }
211 
212 /*
213  * These are the work horses of the get/put_user functions
214  */
215 #if 0
216 #define __get_user_check(x,ptr,size)					\
217 ({									\
218 	long __gu_err = -EFAULT, __gu_val = 0;				\
219 	const __typeof__(*(ptr)) *__gu_addr = (ptr);			\
220 	if (access_ok(VERIFY_READ,__gu_addr,size)) {			\
221 		__gu_err = 0;						\
222 		__get_user_size(__gu_val,__gu_addr,(size),__gu_err);	\
223 	}								\
224 	(x) = (__typeof__(*(ptr)))__gu_val;				\
225 	__gu_err;							\
226 })
227 #endif
228 
229 #define __get_user_nocheck(x,ptr,size)					\
230 ({									\
231 	long __gu_err = 0, __gu_val;					\
232 	__get_user_size(__gu_val,(ptr),(size),__gu_err);		\
233 	(x) = (__typeof__(*(ptr)))__gu_val;				\
234 	__gu_err;							\
235 })
236 
237 #define __get_user_nocheck_error(x,ptr,size,err)			\
238 ({									\
239 	long __gu_val;							\
240 	__get_user_size(__gu_val,(ptr),(size),(err));			\
241 	(x) = (__typeof__(*(ptr)))__gu_val;				\
242 	(void) 0;							\
243 })
244 
245 #define __put_user_check(x,ptr,size)					\
246 ({									\
247 	long __pu_err = -EFAULT;					\
248 	__typeof__(*(ptr)) *__pu_addr = (ptr);				\
249 	if (access_ok(VERIFY_WRITE,__pu_addr,size)) {			\
250 		__pu_err = 0;						\
251 		__put_user_size((x),__pu_addr,(size),__pu_err);		\
252 	}								\
253 	__pu_err;							\
254 })
255 
256 #define __put_user_nocheck(x,ptr,size)					\
257 ({									\
258 	long __pu_err = 0;						\
259 	__typeof__(*(ptr)) *__pu_addr = (ptr);				\
260 	__put_user_size((x),__pu_addr,(size),__pu_err);			\
261 	__pu_err;							\
262 })
263 
264 #define __put_user_nocheck_error(x,ptr,size,err)			\
265 ({									\
266 	__put_user_size((x),(ptr),(size),err);				\
267 	(void) 0;							\
268 })
269 
270 #define __get_user_size(x,ptr,size,retval)				\
271 do {									\
272 	switch (size) {							\
273 	case 1:	__get_user_asm_byte(x,ptr,retval);	break;		\
274 	case 2:	__get_user_asm_half(x,ptr,retval);	break;		\
275 	case 4:	__get_user_asm_word(x,ptr,retval);	break;		\
276 	default: (x) = __get_user_bad();				\
277 	}								\
278 } while (0)
279 
280 #define __put_user_size(x,ptr,size,retval)				\
281 do {									\
282 	switch (size) {							\
283 	case 1: __put_user_asm_byte(x,ptr,retval);	break;		\
284 	case 2: __put_user_asm_half(x,ptr,retval);	break;		\
285 	case 4: __put_user_asm_word(x,ptr,retval);	break;		\
286 	default: __put_user_bad();					\
287 	}								\
288 } while (0)
289 
290 #endif /* _ASMARM_UACCESS_H */
291