2 * arch/ubicom32/include/asm/uaccess.h
3 * User space memory access functions for Ubicom32 architecture.
5 * (C) Copyright 2009, Ubicom, Inc.
7 * This file is part of the Ubicom32 Linux Kernel Port.
9 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 2 of the
12 * License, or (at your option) any later version.
14 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with the Ubicom32 Linux Kernel Port. If not,
21 * see <http://www.gnu.org/licenses/>.
23 * Ubicom32 implementation derived from (with many thanks):
29 #ifndef _ASM_UBICOM32_UACCESS_H
30 #define _ASM_UBICOM32_UACCESS_H
33 * User space memory access functions
35 #include <linux/sched.h>
37 #include <linux/string.h>
39 #include <asm/segment.h>
42 #define VERIFY_WRITE 1
45 * The exception table consists of pairs of addresses: the first is the
46 * address of an instruction that is allowed to fault, and the second is
47 * the address at which the program should continue. No registers are
48 * modified, so it is entirely up to the continuation code to figure out
51 * All the routines below use bits of fixup code that are out of line
52 * with the main instruction path. This means when everything is well,
53 * we don't even have to jump over them. Further, they do not intrude
54 * on our cache or tlb entries.
56 struct exception_table_entry
58 unsigned long insn
, fixup
;
62 * Ubicom32 does not currently support the exception table handling.
64 extern unsigned long search_exception_table(unsigned long);
67 #if defined(CONFIG_ACCESS_OK_CHECKS_ENABLED)
68 extern int __access_ok(unsigned long addr
, unsigned long size
);
70 static inline int __access_ok(unsigned long addr
, unsigned long size
)
75 #define access_ok(type, addr, size) \
76 likely(__access_ok((unsigned long)(addr), (size)))
79 * The following functions do not exist. They keep callers
80 * of put_user and get_user from passing unsupported argument
81 * types. They result in a link time error.
83 extern int __put_user_bad(void);
84 extern int __get_user_bad(void);
87 * __put_user_no_check()
88 * Put the requested data into the user space verifying the address
91 * (a) re-use the arguments for side effects (sizeof/typeof is ok)
92 * (b) require any knowledge of processes at this stage
94 #define __put_user_no_check(x, ptr, size) \
97 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
103 *__pu_addr = (__typeof__(*(ptr)))x; \
106 __pu_err = __put_user_bad(); \
114 * Put the requested data into the user space verifying the address
117 * (a) re-use the arguments for side effects (sizeof/typeof is ok)
118 * (b) require any knowledge of processes at this stage
120 * If requested, access_ok() will verify that ptr is a valid user
123 #define __put_user_check(x, ptr, size) \
125 int __pu_err = -EFAULT; \
126 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
127 if (access_ok(VERIFY_WRITE, __pu_addr, size)) { \
134 *__pu_addr = (__typeof__(*(ptr)))x; \
137 __pu_err = __put_user_bad(); \
145 * __get_user_no_check()
146 * Read the value at ptr into x.
148 * If requested, access_ok() will verify that ptr is a valid user
149 * pointer. If the caller passes a modifying argument for ptr (e.g. x++)
150 * this macro will not work.
152 #define __get_user_no_check(x, ptr, size) \
155 __typeof__((x)) __gu_val = 0; \
156 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
162 __gu_val = (__typeof__((x)))*(__gu_addr); \
165 __gu_err = __get_user_bad(); \
175 * Read the value at ptr into x.
177 * If requested, access_ok() will verify that ptr is a valid user
180 #define __get_user_check(x, ptr, size) \
182 int __gu_err = -EFAULT; \
183 __typeof__(x) __gu_val = 0; \
184 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
185 if (access_ok(VERIFY_READ, __gu_addr, size)) { \
192 __gu_val = (__typeof__((x)))*(__gu_addr); \
195 __gu_err = __get_user_bad(); \
205 * The "xxx" versions are allowed to perform some amount of address
206 * space checking. See access_ok().
208 #define put_user(x,ptr) \
209 __put_user_check((__typeof__(*(ptr)))(x),(ptr), sizeof(*(ptr)))
210 #define get_user(x,ptr) \
211 __get_user_check((x), (ptr), sizeof(*(ptr)))
214 * The "__xxx" versions do not do address space checking, useful when
215 * doing multiple accesses to the same area (the programmer has to do the
216 * checks by hand with "access_ok()")
218 #define __put_user(x,ptr) \
219 __put_user_no_check((__typeof__(*(ptr)))(x),(ptr), sizeof(*(ptr)))
220 #define __get_user(x,ptr) \
221 __get_user_no_check((x), (ptr), sizeof(*(ptr)))
224 * __copy_tofrom_user_no_check()
225 * Copy the data either to or from user space.
227 * Return the number of bytes NOT copied.
229 static inline unsigned long
230 __copy_tofrom_user_no_check(void *to
, const void *from
, unsigned long n
)
238 * Copy the kernel data to user space.
240 * Return the number of bytes that were copied.
242 static inline unsigned long
243 copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
245 if (!access_ok(VERIFY_WRITE
, to
, n
)) {
248 return __copy_tofrom_user_no_check((__force
void *)to
, from
, n
);
253 * Copy the user data to kernel space.
255 * Return the number of bytes that were copied. On error, we zero
256 * out the destination.
258 static inline unsigned long
259 copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
261 if (!access_ok(VERIFY_READ
, from
, n
)) {
264 return __copy_tofrom_user_no_check(to
, (__force
void *)from
, n
);
267 #define __copy_to_user(to, from, n) \
268 __copy_tofrom_user_no_check((__force void *)to, from, n)
269 #define __copy_from_user(to, from, n) \
270 __copy_tofrom_user_no_check(to, (__force void *)from, n)
271 #define __copy_to_user_inatomic(to, from, n) \
272 __copy_tofrom_user_no_check((__force void *)to, from, n)
273 #define __copy_from_user_inatomic(to, from, n) \
274 __copy_tofrom_user_no_check(to, (__force void *)from, n)
276 #define copy_to_user_ret(to, from, n, retval) \
277 ({ if (copy_to_user(to, from, n)) return retval; })
279 #define copy_from_user_ret(to, from, n, retval) \
280 ({ if (copy_from_user(to, from, n)) return retval; })
283 * strncpy_from_user()
284 * Copy a null terminated string from userspace.
286 * dst - Destination in kernel space. The buffer must be at least count.
287 * src - Address of string in user space.
288 * count - Maximum number of bytes to copy (including the trailing NULL).
290 * Returns the length of the string (not including the trailing NULL. If
291 * count is smaller than the length of the string, we copy count bytes
295 static inline long strncpy_from_user(char *dst
, const __user
char *src
, long count
)
298 if (!access_ok(VERIFY_READ
, src
, 1)) {
302 strncpy(dst
, src
, count
);
303 for (tmp
= dst
; *tmp
&& count
> 0; tmp
++, count
--) {
311 * Return the size of a string (including the ending 0)
313 * Return -EFAULT on exception, a value greater than <n> if too long
315 static inline long strnlen_user(const __user
char *src
, long n
)
317 if (!access_ok(VERIFY_READ
, src
, 1)) {
320 return(strlen(src
) + 1);
323 #define strlen_user(str) strnlen_user(str, 32767)
329 static inline unsigned long __clear_user(__user
void *to
, unsigned long n
)
337 * Zero user space (check for valid addresses)
339 static inline unsigned long clear_user(__user
void *to
, unsigned long n
)
341 if (!access_ok(VERIFY_WRITE
, to
, n
)) {
344 return __clear_user(to
, n
);
347 #endif /* _ASM_UBICOM32_UACCESS_H */