2 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2008-2009 PetaLogix
4 * Copyright (C) 2006 Atmark Techno, Inc.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
11 #ifndef _ASM_MICROBLAZE_UACCESS_H
12 #define _ASM_MICROBLAZE_UACCESS_H
14 #include <linux/kernel.h>
19 #include <asm/pgtable.h>
20 #include <asm/extable.h>
21 #include <linux/string.h>
24 * On Microblaze the fs value is actually the top of the corresponding
27 * The fs value determines whether argument validity checking should be
28 * performed or not. If get_fs() == USER_DS, checking is performed, with
29 * get_fs() == KERNEL_DS, checking is bypassed.
31 * For historical reasons, these macros are grossly misnamed.
33 * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal.
35 # define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
38 # define KERNEL_DS MAKE_MM_SEG(0)
39 # define USER_DS KERNEL_DS
41 # define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
42 # define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
45 # define get_fs() (current_thread_info()->addr_limit)
46 # define set_fs(val) (current_thread_info()->addr_limit = (val))
48 # define segment_eq(a, b) ((a).seg == (b).seg)
52 /* Check against bounds of physical memory */
53 static inline int ___range_ok(unsigned long addr, unsigned long size)
55 return ((addr < memory_start) ||
56 ((addr + size - 1) > (memory_start + memory_size - 1)));
59 #define __range_ok(addr, size) \
60 ___range_ok((unsigned long)(addr), (unsigned long)(size))
62 #define access_ok(addr, size) (__range_ok((addr), (size)) == 0)
66 static inline int access_ok(const void __user *addr, unsigned long size)
71 if ((get_fs().seg < ((unsigned long)addr)) ||
72 (get_fs().seg < ((unsigned long)addr + size - 1))) {
73 pr_devel("ACCESS fail at 0x%08x (size 0x%x), seg 0x%08x\n",
74 (__force u32)addr, (u32)size,
79 pr_devel("ACCESS OK at 0x%08x (size 0x%x), seg 0x%08x\n",
80 (__force u32)addr, (u32)size,
87 # define __FIXUP_SECTION ".section .fixup,\"ax\"\n"
88 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
90 # define __FIXUP_SECTION ".section .discard,\"ax\"\n"
91 # define __EX_TABLE_SECTION ".section .discard,\"ax\"\n"
94 extern unsigned long __copy_tofrom_user(void __user *to,
95 const void __user *from, unsigned long size);
97 /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */
98 static inline unsigned long __must_check __clear_user(void __user *to,
101 /* normal memset with two words to __ex_table */
102 __asm__ __volatile__ ( \
103 "1: sb r0, %1, r0;" \
104 " addik %0, %0, -1;" \
106 " addik %1, %1, 1;" \
111 : "=r"(n), "=r"(to) \
117 static inline unsigned long __must_check clear_user(void __user *to,
121 if (unlikely(!access_ok(to, n)))
124 return __clear_user(to, n);
127 /* put_user and get_user macros */
128 extern long __user_bad(void);
130 #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
132 __asm__ __volatile__ ( \
133 "1:" insn " %1, %2, r0;" \
134 " addk %0, r0, r0;" \
138 " addik %0, r0, %3;" \
143 : "=&r"(__gu_err), "=r"(__gu_val) \
144 : "r"(__gu_ptr), "i"(-EFAULT) \
149 * get_user: - Get a simple variable from user space.
150 * @x: Variable to store result.
151 * @ptr: Source address, in user space.
153 * Context: User context only. This function may sleep if pagefaults are
156 * This macro copies a single simple variable from user space to kernel
157 * space. It supports simple types like char and int, but not larger
158 * data types like structures or arrays.
160 * @ptr must have pointer-to-simple-variable type, and the result of
161 * dereferencing @ptr must be assignable to @x without a cast.
163 * Returns zero on success, or -EFAULT on error.
164 * On error, the variable @x is set to zero.
166 #define get_user(x, ptr) \
167 __get_user_check((x), (ptr), sizeof(*(ptr)))
169 #define __get_user_check(x, ptr, size) \
171 unsigned long __gu_val = 0; \
172 const typeof(*(ptr)) __user *__gu_addr = (ptr); \
175 if (access_ok(__gu_addr, size)) { \
178 __get_user_asm("lbu", __gu_addr, __gu_val, \
182 __get_user_asm("lhu", __gu_addr, __gu_val, \
186 __get_user_asm("lw", __gu_addr, __gu_val, \
190 __gu_err = __user_bad(); \
194 __gu_err = -EFAULT; \
196 x = (__force typeof(*(ptr)))__gu_val; \
200 #define __get_user(x, ptr) \
202 unsigned long __gu_val = 0; \
203 /*unsigned long __gu_ptr = (unsigned long)(ptr);*/ \
205 switch (sizeof(*(ptr))) { \
207 __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \
210 __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \
213 __get_user_asm("lw", (ptr), __gu_val, __gu_err); \
216 /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
218 x = (__force __typeof__(*(ptr))) __gu_val; \
223 #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
225 __asm__ __volatile__ ( \
226 "1:" insn " %1, %2, r0;" \
227 " addk %0, r0, r0;" \
231 " addik %0, r0, %3;" \
237 : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
241 #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \
243 __asm__ __volatile__ (" lwi %0, %1, 0;" \
244 "1: swi %0, %2, 0;" \
246 "2: swi %0, %2, 4;" \
247 " addk %0, r0, r0;" \
251 " addik %0, r0, %3;" \
254 ".word 1b,4b,2b,4b;" \
257 : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
262 * put_user: - Write a simple value into user space.
263 * @x: Value to copy to user space.
264 * @ptr: Destination address, in user space.
266 * Context: User context only. This function may sleep if pagefaults are
269 * This macro copies a single simple value from kernel space to user
270 * space. It supports simple types like char and int, but not larger
271 * data types like structures or arrays.
273 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
274 * to the result of dereferencing @ptr.
276 * Returns zero on success, or -EFAULT on error.
278 #define put_user(x, ptr) \
279 __put_user_check((x), (ptr), sizeof(*(ptr)))
281 #define __put_user_check(x, ptr, size) \
283 typeof(*(ptr)) volatile __pu_val = x; \
284 typeof(*(ptr)) __user *__pu_addr = (ptr); \
287 if (access_ok(__pu_addr, size)) { \
290 __put_user_asm("sb", __pu_addr, __pu_val, \
294 __put_user_asm("sh", __pu_addr, __pu_val, \
298 __put_user_asm("sw", __pu_addr, __pu_val, \
302 __put_user_asm_8(__pu_addr, __pu_val, __pu_err);\
305 __pu_err = __user_bad(); \
309 __pu_err = -EFAULT; \
314 #define __put_user(x, ptr) \
316 __typeof__(*(ptr)) volatile __gu_val = (x); \
318 switch (sizeof(__gu_val)) { \
320 __put_user_asm("sb", (ptr), __gu_val, __gu_err); \
323 __put_user_asm("sh", (ptr), __gu_val, __gu_err); \
326 __put_user_asm("sw", (ptr), __gu_val, __gu_err); \
329 __put_user_asm_8((ptr), __gu_val, __gu_err); \
332 /*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \
337 static inline unsigned long
338 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
340 return __copy_tofrom_user((__force void __user *)to, from, n);
343 static inline unsigned long
344 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
346 return __copy_tofrom_user(to, (__force const void __user *)from, n);
348 #define INLINE_COPY_FROM_USER
349 #define INLINE_COPY_TO_USER
352 * Copy a null terminated string from userspace.
354 extern int __strncpy_user(char *to, const char __user *from, int len);
357 strncpy_from_user(char *dst, const char __user *src, long count)
359 if (!access_ok(src, 1))
361 return __strncpy_user(dst, src, count);
365 * Return the size of a string (including the ending 0)
367 * Return 0 on exception, a value greater than N if too long
369 extern int __strnlen_user(const char __user *sstr, int len);
371 static inline long strnlen_user(const char __user *src, long n)
373 if (!access_ok(src, 1))
375 return __strnlen_user(src, n);
378 #endif /* _ASM_MICROBLAZE_UACCESS_H */