1 /* MN10300 userspace access functions
3 * Copyright (C) 2007 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 Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
11 #ifndef _ASM_UACCESS_H
12 #define _ASM_UACCESS_H
15 * User space memory access functions
17 #include <linux/thread_info.h>
19 #include <asm/errno.h>
22 #define VERIFY_WRITE 1
25 * The fs value determines whether argument validity checking should be
26 * performed or not. If get_fs() == USER_DS, checking is performed, with
27 * get_fs() == KERNEL_DS, checking is bypassed.
29 * For historical reasons, these macros are grossly misnamed.
31 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
33 #define KERNEL_XDS MAKE_MM_SEG(0xBFFFFFFF)
34 #define KERNEL_DS MAKE_MM_SEG(0x9FFFFFFF)
35 #define USER_DS MAKE_MM_SEG(TASK_SIZE)
37 #define get_ds() (KERNEL_DS)
38 #define get_fs() (current_thread_info()->addr_limit)
39 #define set_fs(x) (current_thread_info()->addr_limit = (x))
40 #define __kernel_ds_p() (current_thread_info()->addr_limit.seg == 0x9FFFFFFF)
42 #define segment_eq(a, b) ((a).seg == (b).seg)
44 #define __addr_ok(addr) \
45 ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
48 * check that a range of addresses falls within the current address limit
50 static inline int ___range_ok(unsigned long addr, unsigned int size)
54 asm(" add %3,%1 \n" /* set C-flag if addr + size > 4Gb */
56 " cmp %4,%1 \n" /* jump if addr+size>limit (error) */
58 " clr %0 \n" /* mark okay */
60 : "=r"(flag), "=&r"(tmp)
61 : "1"(addr), "ir"(size),
62 "r"(current_thread_info()->addr_limit.seg), "0"(flag)
69 #define __range_ok(addr, size) ___range_ok((unsigned long)(addr), (u32)(size))
71 #define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
72 #define __access_ok(addr, size) (__range_ok((addr), (size)) == 0)
74 static inline int verify_area(int type, const void *addr, unsigned long size)
76 return access_ok(type, addr, size) ? 0 : -EFAULT;
81 * The exception table consists of pairs of addresses: the first is the
82 * address of an instruction that is allowed to fault, and the second is
83 * the address at which the program should continue. No registers are
84 * modified, so it is entirely up to the continuation code to figure out
87 * All the routines below use bits of fixup code that are out of line
88 * with the main instruction path. This means when everything is well,
89 * we don't even have to jump over them. Further, they do not intrude
90 * on our cache or tlb entries.
93 struct exception_table_entry
95 unsigned long insn, fixup;
98 /* Returns 0 if exception not found and fixup otherwise. */
99 extern int fixup_exception(struct pt_regs *regs);
101 #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
102 #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
105 * The "__xxx" versions do not do address space checking, useful when
106 * doing multiple accesses to the same area (the user has to do the
107 * checks by hand with "access_ok()")
109 #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
110 #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
113 * The "xxx_ret" versions return constant specified in third argument, if
114 * something bad happens. These macros can be optimized for the
115 * case of just returning from the function xxx_ret is used.
118 #define put_user_ret(x, ptr, ret) \
119 ({ if (put_user((x), (ptr))) return (ret); })
120 #define get_user_ret(x, ptr, ret) \
121 ({ if (get_user((x), (ptr))) return (ret); })
122 #define __put_user_ret(x, ptr, ret) \
123 ({ if (__put_user((x), (ptr))) return (ret); })
124 #define __get_user_ret(x, ptr, ret) \
125 ({ if (__get_user((x), (ptr))) return (ret); })
127 struct __large_struct { unsigned long buf[100]; };
128 #define __m(x) (*(struct __large_struct *)(x))
130 #define __get_user_nocheck(x, ptr, size) \
132 unsigned long __gu_addr; \
134 __gu_addr = (unsigned long) (ptr); \
137 unsigned char __gu_val; \
138 __get_user_asm("bu"); \
139 (x) = *(__force __typeof__(*(ptr))*) &__gu_val; \
143 unsigned short __gu_val; \
144 __get_user_asm("hu"); \
145 (x) = *(__force __typeof__(*(ptr))*) &__gu_val; \
149 unsigned int __gu_val; \
150 __get_user_asm(""); \
151 (x) = *(__force __typeof__(*(ptr))*) &__gu_val; \
155 __get_user_unknown(); \
161 #define __get_user_check(x, ptr, size) \
164 if (likely(__access_ok((unsigned long) (ptr), (size)))) \
165 _e = __get_user_nocheck((x), (ptr), (size)); \
168 (x) = (__typeof__(x))0; \
173 #define __get_user_asm(INSN) \
177 " mov"INSN" %2,%1\n" \
180 " .section .fixup,\"ax\"\n" \
185 " .section __ex_table,\"a\"\n" \
189 : "=&r" (__gu_err), "=&r" (__gu_val) \
190 : "m" (__m(__gu_addr)), "i" (-EFAULT)); \
193 extern int __get_user_unknown(void);
195 #define __put_user_nocheck(x, ptr, size) \
198 __typeof__(*(ptr)) val; \
201 unsigned long __pu_addr; \
203 __pu_val.val = (x); \
204 __pu_addr = (unsigned long) (ptr); \
206 case 1: __put_user_asm("bu"); break; \
207 case 2: __put_user_asm("hu"); break; \
208 case 4: __put_user_asm("" ); break; \
209 case 8: __put_user_asm8(); break; \
210 default: __pu_err = __put_user_unknown(); break; \
215 #define __put_user_check(x, ptr, size) \
218 __typeof__(*(ptr)) val; \
221 unsigned long __pu_addr; \
223 __pu_val.val = (x); \
224 __pu_addr = (unsigned long) (ptr); \
225 if (likely(__access_ok(__pu_addr, size))) { \
227 case 1: __put_user_asm("bu"); break; \
228 case 2: __put_user_asm("hu"); break; \
229 case 4: __put_user_asm("" ); break; \
230 case 8: __put_user_asm8(); break; \
231 default: __pu_err = __put_user_unknown(); break; \
235 __pu_err = -EFAULT; \
240 #define __put_user_asm(INSN) \
244 " mov"INSN" %1,%2\n" \
247 " .section .fixup,\"ax\"\n" \
252 " .section __ex_table,\"a\"\n" \
257 : "r" (__pu_val.val), "m" (__m(__pu_addr)), \
262 #define __put_user_asm8() \
269 " .section .fixup,\"ax\" \n" \
274 " .section __ex_table,\"a\"\n" \
280 : "r" (__pu_val.bits[0]), "r" (__pu_val.bits[1]), \
281 "m" (__m(__pu_addr)), "m" (__m(__pu_addr+4)), \
286 extern int __put_user_unknown(void);
290 * Copy To/From Userspace
292 /* Generic arbitrary sized copy. */
293 #define __copy_user(to, from, size) \
297 const void *__from = from; \
300 "0: movbu (%0),%3;\n" \
301 "1: movbu %3,(%1);\n" \
307 " .section .fixup,\"ax\"\n" \
310 " .section __ex_table,\"a\"\n" \
315 : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
316 : "0"(__from), "1"(__to), "2"(size) \
321 #define __copy_user_zeroing(to, from, size) \
325 const void *__from = from; \
328 "0: movbu (%0),%3;\n" \
329 "1: movbu %3,(%1);\n" \
335 " .section .fixup,\"ax\"\n" \
339 "4: movbu %3,(%1);\n" \
346 " .section __ex_table,\"a\"\n" \
351 : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
352 : "0"(__from), "1"(__to), "2"(size) \
357 /* We let the __ versions of copy_from/to_user inline, because they're often
358 * used in fast paths and have only a small space overhead.
361 unsigned long __generic_copy_from_user_nocheck(void *to, const void *from,
364 __copy_user_zeroing(to, from, n);
369 unsigned long __generic_copy_to_user_nocheck(void *to, const void *from,
372 __copy_user(to, from, n);
378 #error "don't use - these macros don't increment to & from pointers"
379 /* Optimize just a little bit when we know the size of the move. */
380 #define __constant_copy_user(to, from, size) \
384 "0: movbu (%1),d3;\n" \
385 "1: movbu d3,(%2);\n" \
389 ".section .fixup,\"ax\"\n" \
392 ".section __ex_table,\"a\"\n" \
398 : "d"(size), "d"(to), "d"(from) \
402 /* Optimize just a little bit when we know the size of the move. */
403 #define __constant_copy_user_zeroing(to, from, size) \
407 "0: movbu (%1),d3;\n" \
408 "1: movbu d3,(%2);\n" \
412 ".section .fixup,\"ax\"\n" \
415 ".section __ex_table,\"a\"\n" \
421 : "d"(size), "d"(to), "d"(from) \
426 unsigned long __constant_copy_to_user(void *to, const void *from,
429 if (access_ok(VERIFY_WRITE, to, n))
430 __constant_copy_user(to, from, n);
435 unsigned long __constant_copy_from_user(void *to, const void *from,
438 if (access_ok(VERIFY_READ, from, n))
439 __constant_copy_user_zeroing(to, from, n);
444 unsigned long __constant_copy_to_user_nocheck(void *to, const void *from,
447 __constant_copy_user(to, from, n);
452 unsigned long __constant_copy_from_user_nocheck(void *to, const void *from,
455 __constant_copy_user_zeroing(to, from, n);
460 extern unsigned long __generic_copy_to_user(void __user *, const void *,
462 extern unsigned long __generic_copy_from_user(void *, const void __user *,
465 #define __copy_to_user_inatomic(to, from, n) \
466 __generic_copy_to_user_nocheck((to), (from), (n))
467 #define __copy_from_user_inatomic(to, from, n) \
468 __generic_copy_from_user_nocheck((to), (from), (n))
470 #define __copy_to_user(to, from, n) \
473 __copy_to_user_inatomic((to), (from), (n)); \
476 #define __copy_from_user(to, from, n) \
479 __copy_from_user_inatomic((to), (from), (n)); \
483 #define copy_to_user(to, from, n) __generic_copy_to_user((to), (from), (n))
484 #define copy_from_user(to, from, n) __generic_copy_from_user((to), (from), (n))
486 extern long strncpy_from_user(char *dst, const char __user *src, long count);
487 extern long __strncpy_from_user(char *dst, const char __user *src, long count);
488 extern long strnlen_user(const char __user *str, long n);
489 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
490 extern unsigned long clear_user(void __user *mem, unsigned long len);
491 extern unsigned long __clear_user(void __user *mem, unsigned long len);
493 #endif /* _ASM_UACCESS_H */