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
19 #include <asm/segment.h>
20 #include <asm/sections.h>
21 #include <asm/extable.h>
23 #define __ptr(x) ((unsigned long __force *)(x))
26 * check that a range of addresses falls within the current address limit
28 static inline int ___range_ok(unsigned long addr
, unsigned long size
)
31 int flag
= -EFAULT
, tmp
;
34 " addcc %3,%2,%1,icc0 \n" /* set C-flag if addr+size>4GB */
35 " subcc.p %1,%4,gr0,icc1 \n" /* jump if addr+size>limit */
38 " setlos #0,%0 \n" /* mark okay */
40 : "=r"(flag
), "=&r"(tmp
)
41 : "r"(addr
), "r"(size
), "r"(get_addr_limit()), "0"(flag
)
48 if (addr
< memory_start
||
50 size
> memory_end
- memory_start
||
51 addr
+ size
> memory_end
)
58 #define __range_ok(addr,size) ___range_ok((unsigned long) (addr), (unsigned long) (size))
60 #define access_ok(type,addr,size) (__range_ok((void __user *)(addr), (size)) == 0)
61 #define __access_ok(addr,size) (__range_ok((addr), (size)) == 0)
65 * These are the main single-value transfer routines. They automatically
66 * use the right size if we just have the right pointer type.
68 #define __put_user(x, ptr) \
72 typeof(*(ptr)) __pu_val = (x); \
73 __chk_user_ptr(ptr); \
75 switch (sizeof (*(ptr))) { \
77 __put_user_asm(__pu_err, __pu_val, ptr, "b", "r"); \
80 __put_user_asm(__pu_err, __pu_val, ptr, "h", "r"); \
83 __put_user_asm(__pu_err, __pu_val, ptr, "", "r"); \
86 __put_user_asm(__pu_err, __pu_val, ptr, "d", "e"); \
89 __pu_err = __put_user_bad(); \
95 #define put_user(x, ptr) \
97 typeof(*(ptr)) __user *_p = (ptr); \
100 _e = __range_ok(_p, sizeof(*_p)); \
102 _e = __put_user((x), _p); \
106 extern int __put_user_bad(void);
109 * Tell gcc we read from memory instead of writing: this is because
110 * we do not write to any memory gcc knows about, so there are no
116 #define __put_user_asm(err,x,ptr,dsize,constraint) \
118 asm volatile("1: st"dsize"%I1 %2,%M1 \n" \
121 "3: setlos %3,%0 \n" \
124 ".section __ex_table,\"a\" \n" \
129 : "m" (*__ptr(ptr)), constraint (x), "i"(-EFAULT), "0"(err) \
135 #define __put_user_asm(err,x,ptr,bwl,con) \
137 asm(" st"bwl"%I0 %1,%M0 \n" \
140 : "m" (*__ptr(ptr)), con (x) \
146 /*****************************************************************************/
150 #define __get_user(x, ptr) \
153 __chk_user_ptr(ptr); \
155 switch (sizeof(*(ptr))) { \
157 unsigned char __gu_val; \
158 __get_user_asm(__gu_err, __gu_val, ptr, "ub", "=r"); \
159 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
163 unsigned short __gu_val; \
164 __get_user_asm(__gu_err, __gu_val, ptr, "uh", "=r"); \
165 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
169 unsigned int __gu_val; \
170 __get_user_asm(__gu_err, __gu_val, ptr, "", "=r"); \
171 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
175 unsigned long long __gu_val; \
176 __get_user_asm(__gu_err, __gu_val, ptr, "d", "=e"); \
177 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
181 __gu_err = __get_user_bad(); \
187 #define get_user(x, ptr) \
189 const typeof(*(ptr)) __user *_p = (ptr);\
192 _e = __range_ok(_p, sizeof(*_p)); \
193 if (likely(_e == 0)) \
194 _e = __get_user((x), _p); \
196 (x) = (typeof(x)) 0; \
200 extern int __get_user_bad(void);
204 #define __get_user_asm(err,x,ptr,dtype,constraint) \
206 asm("1: ld"dtype"%I2 %M2,%1 \n" \
209 "3: setlos %3,%0 \n" \
213 ".section __ex_table,\"a\" \n" \
217 : "=r" (err), constraint (x) \
218 : "m" (*__ptr(ptr)), "i"(-EFAULT), "0"(err) \
224 #define __get_user_asm(err,x,ptr,bwl,con) \
225 asm(" ld"bwl"%I1 %M1,%0 \n" \
232 /*****************************************************************************/
237 #define ____force(x) (__force void *)(void __user *)(x)
239 extern long __memset_user(void *dst
, unsigned long count
);
240 extern long __memcpy_user(void *dst
, const void *src
, unsigned long count
);
242 #define __clear_user(dst,count) __memset_user(____force(dst), (count))
246 #define __clear_user(dst,count) (memset(____force(dst), 0, (count)), 0)
250 static inline unsigned long
251 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
254 return __memcpy_user(to
, (__force
const void *)from
, n
);
256 memcpy(to
, (__force
const void *)from
, n
);
261 static inline unsigned long
262 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
265 return __memcpy_user((__force
void *)to
, from
, n
);
267 memcpy((__force
void *)to
, from
, n
);
271 #define INLINE_COPY_TO_USER
272 #define INLINE_COPY_FROM_USER
274 static inline unsigned long __must_check
275 clear_user(void __user
*to
, unsigned long n
)
277 if (likely(__access_ok(to
, n
)))
278 n
= __clear_user(to
, n
);
282 extern long strncpy_from_user(char *dst
, const char __user
*src
, long count
);
283 extern long strnlen_user(const char __user
*src
, long count
);
285 #endif /* _ASM_UACCESS_H */