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
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/sched.h> /* RLIMIT_FSIZE */
24 #include <asm/pgtable.h>
25 #include <linux/string.h>
28 #define VERIFY_WRITE 1
31 * On Microblaze the fs value is actually the top of the corresponding
34 * The fs value determines whether argument validity checking should be
35 * performed or not. If get_fs() == USER_DS, checking is performed, with
36 * get_fs() == KERNEL_DS, checking is bypassed.
38 * For historical reasons, these macros are grossly misnamed.
40 * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal.
42 # define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
45 # define KERNEL_DS MAKE_MM_SEG(0)
46 # define USER_DS KERNEL_DS
48 # define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
49 # define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
52 # define get_ds() (KERNEL_DS)
53 # define get_fs() (current_thread_info()->addr_limit)
54 # define set_fs(val) (current_thread_info()->addr_limit = (val))
56 # define segment_eq(a, b) ((a).seg == (b).seg)
59 * The exception table consists of pairs of addresses: the first is the
60 * address of an instruction that is allowed to fault, and the second is
61 * the address at which the program should continue. No registers are
62 * modified, so it is entirely up to the continuation code to figure out
65 * All the routines below use bits of fixup code that are out of line
66 * with the main instruction path. This means when everything is well,
67 * we don't even have to jump over them. Further, they do not intrude
68 * on our cache or tlb entries.
70 struct exception_table_entry {
71 unsigned long insn, fixup;
74 /* Returns 0 if exception not found and fixup otherwise. */
75 extern unsigned long search_exception_table(unsigned long);
79 /* Check against bounds of physical memory */
80 static inline int ___range_ok(unsigned long addr, unsigned long size)
82 return ((addr < memory_start) ||
83 ((addr + size - 1) > (memory_start + memory_size - 1)));
86 #define __range_ok(addr, size) \
87 ___range_ok((unsigned long)(addr), (unsigned long)(size))
89 #define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
93 static inline int access_ok(int type, const void __user *addr,
99 if ((get_fs().seg < ((unsigned long)addr)) ||
100 (get_fs().seg < ((unsigned long)addr + size - 1))) {
101 pr_devel("ACCESS fail: %s at 0x%08x (size 0x%x), seg 0x%08x\n",
102 type ? "WRITE" : "READ ", (__force u32)addr, (u32)size,
107 pr_devel("ACCESS OK: %s at 0x%08x (size 0x%x), seg 0x%08x\n",
108 type ? "WRITE" : "READ ", (__force u32)addr, (u32)size,
115 # define __FIXUP_SECTION ".section .fixup,\"ax\"\n"
116 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
118 # define __FIXUP_SECTION ".section .discard,\"ax\"\n"
119 # define __EX_TABLE_SECTION ".section .discard,\"ax\"\n"
122 extern unsigned long __copy_tofrom_user(void __user *to,
123 const void __user *from, unsigned long size);
125 /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */
126 static inline unsigned long __must_check __clear_user(void __user *to,
129 /* normal memset with two words to __ex_table */
130 __asm__ __volatile__ ( \
131 "1: sb r0, %1, r0;" \
132 " addik %0, %0, -1;" \
134 " addik %1, %1, 1;" \
139 : "=r"(n), "=r"(to) \
145 static inline unsigned long __must_check clear_user(void __user *to,
149 if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
152 return __clear_user(to, n);
155 /* put_user and get_user macros */
156 extern long __user_bad(void);
158 #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
160 __asm__ __volatile__ ( \
161 "1:" insn " %1, %2, r0;" \
162 " addk %0, r0, r0;" \
166 " addik %0, r0, %3;" \
171 : "=&r"(__gu_err), "=r"(__gu_val) \
172 : "r"(__gu_ptr), "i"(-EFAULT) \
177 * get_user: - Get a simple variable from user space.
178 * @x: Variable to store result.
179 * @ptr: Source address, in user space.
181 * Context: User context only. This function may sleep if pagefaults are
184 * This macro copies a single simple variable from user space to kernel
185 * space. It supports simple types like char and int, but not larger
186 * data types like structures or arrays.
188 * @ptr must have pointer-to-simple-variable type, and the result of
189 * dereferencing @ptr must be assignable to @x without a cast.
191 * Returns zero on success, or -EFAULT on error.
192 * On error, the variable @x is set to zero.
194 #define get_user(x, ptr) \
195 __get_user_check((x), (ptr), sizeof(*(ptr)))
197 #define __get_user_check(x, ptr, size) \
199 unsigned long __gu_val = 0; \
200 const typeof(*(ptr)) __user *__gu_addr = (ptr); \
203 if (access_ok(VERIFY_READ, __gu_addr, size)) { \
206 __get_user_asm("lbu", __gu_addr, __gu_val, \
210 __get_user_asm("lhu", __gu_addr, __gu_val, \
214 __get_user_asm("lw", __gu_addr, __gu_val, \
218 __gu_err = __user_bad(); \
222 __gu_err = -EFAULT; \
224 x = (__force typeof(*(ptr)))__gu_val; \
228 #define __get_user(x, ptr) \
230 unsigned long __gu_val = 0; \
231 /*unsigned long __gu_ptr = (unsigned long)(ptr);*/ \
233 switch (sizeof(*(ptr))) { \
235 __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \
238 __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \
241 __get_user_asm("lw", (ptr), __gu_val, __gu_err); \
244 /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
246 x = (__force __typeof__(*(ptr))) __gu_val; \
251 #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
253 __asm__ __volatile__ ( \
254 "1:" insn " %1, %2, r0;" \
255 " addk %0, r0, r0;" \
259 " addik %0, r0, %3;" \
265 : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
269 #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \
271 __asm__ __volatile__ (" lwi %0, %1, 0;" \
272 "1: swi %0, %2, 0;" \
274 "2: swi %0, %2, 4;" \
275 " addk %0, r0, r0;" \
279 " addik %0, r0, %3;" \
282 ".word 1b,4b,2b,4b;" \
285 : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
290 * put_user: - Write a simple value into user space.
291 * @x: Value to copy to user space.
292 * @ptr: Destination address, in user space.
294 * Context: User context only. This function may sleep if pagefaults are
297 * This macro copies a single simple value from kernel space to user
298 * space. It supports simple types like char and int, but not larger
299 * data types like structures or arrays.
301 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
302 * to the result of dereferencing @ptr.
304 * Returns zero on success, or -EFAULT on error.
306 #define put_user(x, ptr) \
307 __put_user_check((x), (ptr), sizeof(*(ptr)))
309 #define __put_user_check(x, ptr, size) \
311 typeof(*(ptr)) volatile __pu_val = x; \
312 typeof(*(ptr)) __user *__pu_addr = (ptr); \
315 if (access_ok(VERIFY_WRITE, __pu_addr, size)) { \
318 __put_user_asm("sb", __pu_addr, __pu_val, \
322 __put_user_asm("sh", __pu_addr, __pu_val, \
326 __put_user_asm("sw", __pu_addr, __pu_val, \
330 __put_user_asm_8(__pu_addr, __pu_val, __pu_err);\
333 __pu_err = __user_bad(); \
337 __pu_err = -EFAULT; \
342 #define __put_user(x, ptr) \
344 __typeof__(*(ptr)) volatile __gu_val = (x); \
346 switch (sizeof(__gu_val)) { \
348 __put_user_asm("sb", (ptr), __gu_val, __gu_err); \
351 __put_user_asm("sh", (ptr), __gu_val, __gu_err); \
354 __put_user_asm("sw", (ptr), __gu_val, __gu_err); \
357 __put_user_asm_8((ptr), __gu_val, __gu_err); \
360 /*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \
366 /* copy_to_from_user */
367 #define __copy_from_user(to, from, n) \
368 __copy_tofrom_user((__force void __user *)(to), \
369 (void __user *)(from), (n))
370 #define __copy_from_user_inatomic(to, from, n) \
371 __copy_from_user((to), (from), (n))
373 static inline long copy_from_user(void *to,
374 const void __user *from, unsigned long n)
376 unsigned long res = n;
378 if (likely(access_ok(VERIFY_READ, from, n)))
379 res = __copy_from_user(to, from, n);
381 memset(to + (n - res), 0, res);
385 #define __copy_to_user(to, from, n) \
386 __copy_tofrom_user((void __user *)(to), \
387 (__force const void __user *)(from), (n))
388 #define __copy_to_user_inatomic(to, from, n) __copy_to_user((to), (from), (n))
390 static inline long copy_to_user(void __user *to,
391 const void *from, unsigned long n)
394 if (access_ok(VERIFY_WRITE, to, n))
395 return __copy_to_user(to, from, n);
400 * Copy a null terminated string from userspace.
402 extern int __strncpy_user(char *to, const char __user *from, int len);
404 #define __strncpy_from_user __strncpy_user
407 strncpy_from_user(char *dst, const char __user *src, long count)
409 if (!access_ok(VERIFY_READ, src, 1))
411 return __strncpy_from_user(dst, src, count);
415 * Return the size of a string (including the ending 0)
417 * Return 0 on exception, a value greater than N if too long
419 extern int __strnlen_user(const char __user *sstr, int len);
421 static inline long strnlen_user(const char __user *src, long n)
423 if (!access_ok(VERIFY_READ, src, 1))
425 return __strnlen_user(src, n);
428 #endif /* __ASSEMBLY__ */
429 #endif /* __KERNEL__ */
431 #endif /* _ASM_MICROBLAZE_UACCESS_H */