1 /* uaccess.h: userspace accessor functions
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #ifndef _ASM_UACCESS_H
13 #define _ASM_UACCESS_H
16 * User space memory access functions
18 #include <linux/sched.h>
20 #include <asm/segment.h>
21 #include <asm/sections.h>
23 #define HAVE_ARCH_UNMAPPED_AREA /* we decide where to put mmaps */
25 #define __ptr(x) ((unsigned long __force *)(x))
28 #define VERIFY_WRITE 1
31 * check that a range of addresses falls within the current address limit
33 static inline int ___range_ok(unsigned long addr
, unsigned long size
)
36 int flag
= -EFAULT
, tmp
;
39 " addcc %3,%2,%1,icc0 \n" /* set C-flag if addr+size>4GB */
40 " subcc.p %1,%4,gr0,icc1 \n" /* jump if addr+size>limit */
43 " setlos #0,%0 \n" /* mark okay */
45 : "=r"(flag
), "=&r"(tmp
)
46 : "r"(addr
), "r"(size
), "r"(get_addr_limit()), "0"(flag
)
53 if (addr
< memory_start
||
55 size
> memory_end
- memory_start
||
56 addr
+ size
> memory_end
)
63 #define __range_ok(addr,size) ___range_ok((unsigned long) (addr), (unsigned long) (size))
65 #define access_ok(type,addr,size) (__range_ok((void __user *)(addr), (size)) == 0)
66 #define __access_ok(addr,size) (__range_ok((addr), (size)) == 0)
69 * The exception table consists of pairs of addresses: the first is the
70 * address of an instruction that is allowed to fault, and the second is
71 * the address at which the program should continue. No registers are
72 * modified, so it is entirely up to the continuation code to figure out
75 * All the routines below use bits of fixup code that are out of line
76 * with the main instruction path. This means when everything is well,
77 * we don't even have to jump over them. Further, they do not intrude
78 * on our cache or tlb entries.
80 struct exception_table_entry
82 unsigned long insn
, fixup
;
85 /* Returns 0 if exception not found and fixup otherwise. */
86 extern unsigned long search_exception_table(unsigned long);
90 * These are the main single-value transfer routines. They automatically
91 * use the right size if we just have the right pointer type.
93 #define __put_user(x, ptr) \
97 typeof(*(ptr)) __pu_val = (x); \
98 __chk_user_ptr(ptr); \
100 switch (sizeof (*(ptr))) { \
102 __put_user_asm(__pu_err, __pu_val, ptr, "b", "r"); \
105 __put_user_asm(__pu_err, __pu_val, ptr, "h", "r"); \
108 __put_user_asm(__pu_err, __pu_val, ptr, "", "r"); \
111 __put_user_asm(__pu_err, __pu_val, ptr, "d", "e"); \
114 __pu_err = __put_user_bad(); \
120 #define put_user(x, ptr) \
122 typeof(*(ptr)) __user *_p = (ptr); \
125 _e = __range_ok(_p, sizeof(*_p)); \
127 _e = __put_user((x), _p); \
131 extern int __put_user_bad(void);
134 * Tell gcc we read from memory instead of writing: this is because
135 * we do not write to any memory gcc knows about, so there are no
141 #define __put_user_asm(err,x,ptr,dsize,constraint) \
143 asm volatile("1: st"dsize"%I1 %2,%M1 \n" \
146 "3: setlos %3,%0 \n" \
149 ".section __ex_table,\"a\" \n" \
154 : "m" (*__ptr(ptr)), constraint (x), "i"(-EFAULT), "0"(err) \
160 #define __put_user_asm(err,x,ptr,bwl,con) \
162 asm(" st"bwl"%I0 %1,%M0 \n" \
165 : "m" (*__ptr(ptr)), con (x) \
171 /*****************************************************************************/
175 #define __get_user(x, ptr) \
178 __chk_user_ptr(ptr); \
180 switch (sizeof(*(ptr))) { \
182 unsigned char __gu_val; \
183 __get_user_asm(__gu_err, __gu_val, ptr, "ub", "=r"); \
184 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
188 unsigned short __gu_val; \
189 __get_user_asm(__gu_err, __gu_val, ptr, "uh", "=r"); \
190 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
194 unsigned int __gu_val; \
195 __get_user_asm(__gu_err, __gu_val, ptr, "", "=r"); \
196 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
200 unsigned long long __gu_val; \
201 __get_user_asm(__gu_err, __gu_val, ptr, "d", "=e"); \
202 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
206 __gu_err = __get_user_bad(); \
212 #define get_user(x, ptr) \
214 const typeof(*(ptr)) __user *_p = (ptr);\
217 _e = __range_ok(_p, sizeof(*_p)); \
218 if (likely(_e == 0)) \
219 _e = __get_user((x), _p); \
221 (x) = (typeof(x)) 0; \
225 extern int __get_user_bad(void);
229 #define __get_user_asm(err,x,ptr,dtype,constraint) \
231 asm("1: ld"dtype"%I2 %M2,%1 \n" \
234 "3: setlos %3,%0 \n" \
238 ".section __ex_table,\"a\" \n" \
242 : "=r" (err), constraint (x) \
243 : "m" (*__ptr(ptr)), "i"(-EFAULT), "0"(err) \
249 #define __get_user_asm(err,x,ptr,bwl,con) \
250 asm(" ld"bwl"%I1 %M1,%0 \n" \
257 /*****************************************************************************/
261 #define ____force(x) (__force void *)(void __user *)(x)
263 extern long __memset_user(void *dst
, unsigned long count
);
264 extern long __memcpy_user(void *dst
, const void *src
, unsigned long count
);
266 #define __clear_user(dst,count) __memset_user(____force(dst), (count))
267 #define __copy_from_user_inatomic(to, from, n) __memcpy_user((to), ____force(from), (n))
268 #define __copy_to_user_inatomic(to, from, n) __memcpy_user(____force(to), (from), (n))
272 #define __clear_user(dst,count) (memset(____force(dst), 0, (count)), 0)
273 #define __copy_from_user_inatomic(to, from, n) (memcpy((to), ____force(from), (n)), 0)
274 #define __copy_to_user_inatomic(to, from, n) (memcpy(____force(to), (from), (n)), 0)
278 static inline unsigned long __must_check
279 clear_user(void __user
*to
, unsigned long n
)
281 if (likely(__access_ok(to
, n
)))
282 n
= __clear_user(to
, n
);
286 static inline unsigned long __must_check
287 __copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
290 return __copy_to_user_inatomic(to
, from
, n
);
293 static inline unsigned long
294 __copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
297 return __copy_from_user_inatomic(to
, from
, n
);
300 static inline long copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
302 unsigned long ret
= n
;
304 if (likely(__access_ok(from
, n
)))
305 ret
= __copy_from_user(to
, from
, n
);
307 if (unlikely(ret
!= 0))
308 memset(to
+ (n
- ret
), 0, ret
);
313 static inline long copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
315 return likely(__access_ok(to
, n
)) ? __copy_to_user(to
, from
, n
) : n
;
318 extern long strncpy_from_user(char *dst
, const char __user
*src
, long count
);
319 extern long strnlen_user(const char __user
*src
, long count
);
321 #define strlen_user(str) strnlen_user(str, 32767)
323 extern unsigned long search_exception_table(unsigned long addr
);
325 #endif /* _ASM_UACCESS_H */