1 #ifndef __METAG_UACCESS_H
2 #define __METAG_UACCESS_H
5 * User space memory access functions
9 * The fs value determines whether argument validity checking should be
10 * performed or not. If get_fs() == USER_DS, checking is performed, with
11 * get_fs() == KERNEL_DS, checking is bypassed.
13 * For historical reasons, these macros are grossly misnamed.
16 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
18 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
19 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
21 #define get_ds() (KERNEL_DS)
22 #define get_fs() (current_thread_info()->addr_limit)
23 #define set_fs(x) (current_thread_info()->addr_limit = (x))
25 #define segment_eq(a, b) ((a).seg == (b).seg)
27 static inline int __access_ok(unsigned long addr, unsigned long size)
30 * Allow access to the user mapped memory area, but not the system area
31 * before it. The check extends to the top of the address space when
32 * kernel access is allowed (there's no real reason to user copy to the
33 * system area in any case).
35 if (likely(addr >= META_MEMORY_BASE && addr < get_fs().seg &&
36 size <= get_fs().seg - addr))
39 * Explicitly allow NULL pointers here. Parts of the kernel such
40 * as readv/writev use access_ok to validate pointers, but want
41 * to allow NULL pointers for various reasons. NULL pointers are
42 * safe to allow through because the first page is not mappable on
47 /* Allow access to core code memory area... */
48 if (addr >= LINCORE_CODE_BASE && addr <= LINCORE_CODE_LIMIT &&
49 size <= LINCORE_CODE_LIMIT + 1 - addr)
51 /* ... but no other areas. */
55 #define access_ok(type, addr, size) __access_ok((unsigned long)(addr), \
56 (unsigned long)(size))
58 #include <asm/extable.h>
61 * These are the main single-value transfer routines. They automatically
62 * use the right size if we just have the right pointer type.
65 #define put_user(x, ptr) \
66 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
67 #define __put_user(x, ptr) \
68 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
70 extern void __put_user_bad(void);
72 #define __put_user_nocheck(x, ptr, size) \
75 __put_user_size((x), (ptr), (size), __pu_err); \
79 #define __put_user_check(x, ptr, size) \
81 long __pu_err = -EFAULT; \
82 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
83 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
84 __put_user_size((x), __pu_addr, (size), __pu_err); \
88 extern long __put_user_asm_b(unsigned int x, void __user *addr);
89 extern long __put_user_asm_w(unsigned int x, void __user *addr);
90 extern long __put_user_asm_d(unsigned int x, void __user *addr);
91 extern long __put_user_asm_l(unsigned long long x, void __user *addr);
93 #define __put_user_size(x, ptr, size, retval) \
98 retval = __put_user_asm_b((__force unsigned int)x, ptr);\
101 retval = __put_user_asm_w((__force unsigned int)x, ptr);\
104 retval = __put_user_asm_d((__force unsigned int)x, ptr);\
107 retval = __put_user_asm_l((__force unsigned long long)x,\
115 #define get_user(x, ptr) \
116 __get_user_check((x), (ptr), sizeof(*(ptr)))
117 #define __get_user(x, ptr) \
118 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
120 extern long __get_user_bad(void);
122 #define __get_user_nocheck(x, ptr, size) \
125 long long __gu_val; \
126 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
127 (x) = (__force __typeof__(*(ptr)))__gu_val; \
131 #define __get_user_check(x, ptr, size) \
133 long __gu_err = -EFAULT; \
134 long long __gu_val = 0; \
135 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
136 if (access_ok(VERIFY_READ, __gu_addr, size)) \
137 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
138 (x) = (__force __typeof__(*(ptr)))__gu_val; \
142 extern unsigned char __get_user_asm_b(const void __user *addr, long *err);
143 extern unsigned short __get_user_asm_w(const void __user *addr, long *err);
144 extern unsigned int __get_user_asm_d(const void __user *addr, long *err);
145 extern unsigned long long __get_user_asm_l(const void __user *addr, long *err);
147 #define __get_user_size(x, ptr, size, retval) \
152 x = __get_user_asm_b(ptr, &retval); break; \
154 x = __get_user_asm_w(ptr, &retval); break; \
156 x = __get_user_asm_d(ptr, &retval); break; \
158 x = __get_user_asm_l(ptr, &retval); break; \
160 (x) = __get_user_bad(); \
165 * Copy a null terminated string from userspace.
168 * -EFAULT for an exception
169 * count if we hit the buffer limit
170 * bytes copied if we hit a null byte
171 * (without the null byte)
174 extern long __must_check __strncpy_from_user(char *dst, const char __user *src,
178 strncpy_from_user(char *dst, const char __user *src, long count)
180 if (!access_ok(VERIFY_READ, src, 1))
182 return __strncpy_from_user(dst, src, count);
185 * Return the size of a string (including the ending 0)
187 * Return 0 on exception, a value greater than N if too long
189 extern long __must_check strnlen_user(const char __user *src, long count);
191 extern unsigned long raw_copy_from_user(void *to, const void __user *from,
193 extern unsigned long raw_copy_to_user(void __user *to, const void *from,
200 extern unsigned long __must_check __do_clear_user(void __user *to,
203 static inline unsigned long clear_user(void __user *to, unsigned long n)
205 if (access_ok(VERIFY_WRITE, to, n))
206 return __do_clear_user(to, n);
210 #define __clear_user(to, n) __do_clear_user(to, n)
212 #endif /* _METAG_UACCESS_H */