1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
4 * Copyright (C) 2008-2009 PetaLogix
5 * Copyright (C) 2006 Atmark Techno, Inc.
8 #ifndef _ASM_MICROBLAZE_UACCESS_H
9 #define _ASM_MICROBLAZE_UACCESS_H
11 #include <linux/kernel.h>
15 #include <linux/pgtable.h>
16 #include <asm/extable.h>
17 #include <linux/string.h>
20 * On Microblaze the fs value is actually the top of the corresponding
23 * The fs value determines whether argument validity checking should be
24 * performed or not. If get_fs() == USER_DS, checking is performed, with
25 * get_fs() == KERNEL_DS, checking is bypassed.
27 * For historical reasons, these macros are grossly misnamed.
29 * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal.
31 # define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
33 # define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
34 # define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
36 # define get_fs() (current_thread_info()->addr_limit)
37 # define set_fs(val) (current_thread_info()->addr_limit = (val))
39 # define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)
41 static inline int access_ok(const void __user
*addr
, unsigned long size
)
46 if ((get_fs().seg
< ((unsigned long)addr
)) ||
47 (get_fs().seg
< ((unsigned long)addr
+ size
- 1))) {
48 pr_devel("ACCESS fail at 0x%08x (size 0x%x), seg 0x%08x\n",
49 (__force u32
)addr
, (u32
)size
,
54 pr_devel("ACCESS OK at 0x%08x (size 0x%x), seg 0x%08x\n",
55 (__force u32
)addr
, (u32
)size
,
60 # define __FIXUP_SECTION ".section .fixup,\"ax\"\n"
61 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
63 extern unsigned long __copy_tofrom_user(void __user
*to
,
64 const void __user
*from
, unsigned long size
);
66 /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */
67 static inline unsigned long __must_check
__clear_user(void __user
*to
,
70 /* normal memset with two words to __ex_table */
71 __asm__
__volatile__ ( \
73 " addik %0, %0, -1;" \
86 static inline unsigned long __must_check
clear_user(void __user
*to
,
90 if (unlikely(!access_ok(to
, n
)))
93 return __clear_user(to
, n
);
96 /* put_user and get_user macros */
97 extern long __user_bad(void);
99 #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
101 __asm__ __volatile__ ( \
102 "1:" insn " %1, %2, r0;" \
103 " addk %0, r0, r0;" \
107 " addik %0, r0, %3;" \
112 : "=&r"(__gu_err), "=r"(__gu_val) \
113 : "r"(__gu_ptr), "i"(-EFAULT) \
118 * get_user: - Get a simple variable from user space.
119 * @x: Variable to store result.
120 * @ptr: Source address, in user space.
122 * Context: User context only. This function may sleep if pagefaults are
125 * This macro copies a single simple variable from user space to kernel
126 * space. It supports simple types like char and int, but not larger
127 * data types like structures or arrays.
129 * @ptr must have pointer-to-simple-variable type, and the result of
130 * dereferencing @ptr must be assignable to @x without a cast.
132 * Returns zero on success, or -EFAULT on error.
133 * On error, the variable @x is set to zero.
135 #define get_user(x, ptr) ({ \
136 const typeof(*(ptr)) __user *__gu_ptr = (ptr); \
137 access_ok(__gu_ptr, sizeof(*__gu_ptr)) ? \
138 __get_user(x, __gu_ptr) : -EFAULT; \
141 #define __get_user(x, ptr) \
143 unsigned long __gu_val = 0; \
145 switch (sizeof(*(ptr))) { \
147 __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \
150 __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \
153 __get_user_asm("lw", (ptr), __gu_val, __gu_err); \
156 __gu_err = __copy_from_user(&__gu_val, ptr, 8); \
158 __gu_err = -EFAULT; \
161 /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
163 x = (__force __typeof__(*(ptr))) __gu_val; \
168 #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
170 __asm__ __volatile__ ( \
171 "1:" insn " %1, %2, r0;" \
172 " addk %0, r0, r0;" \
176 " addik %0, r0, %3;" \
182 : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
186 #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \
188 __asm__ __volatile__ (" lwi %0, %1, 0;" \
189 "1: swi %0, %2, 0;" \
191 "2: swi %0, %2, 4;" \
192 " addk %0, r0, r0;" \
196 " addik %0, r0, %3;" \
199 ".word 1b,4b,2b,4b;" \
202 : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
207 * put_user: - Write a simple value into user space.
208 * @x: Value to copy to user space.
209 * @ptr: Destination address, in user space.
211 * Context: User context only. This function may sleep if pagefaults are
214 * This macro copies a single simple value from kernel space to user
215 * space. It supports simple types like char and int, but not larger
216 * data types like structures or arrays.
218 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
219 * to the result of dereferencing @ptr.
221 * Returns zero on success, or -EFAULT on error.
223 #define put_user(x, ptr) \
224 __put_user_check((x), (ptr), sizeof(*(ptr)))
226 #define __put_user_check(x, ptr, size) \
228 typeof(*(ptr)) volatile __pu_val = x; \
229 typeof(*(ptr)) __user *__pu_addr = (ptr); \
232 if (access_ok(__pu_addr, size)) { \
235 __put_user_asm("sb", __pu_addr, __pu_val, \
239 __put_user_asm("sh", __pu_addr, __pu_val, \
243 __put_user_asm("sw", __pu_addr, __pu_val, \
247 __put_user_asm_8(__pu_addr, __pu_val, __pu_err);\
250 __pu_err = __user_bad(); \
254 __pu_err = -EFAULT; \
259 #define __put_user(x, ptr) \
261 __typeof__(*(ptr)) volatile __gu_val = (x); \
263 switch (sizeof(__gu_val)) { \
265 __put_user_asm("sb", (ptr), __gu_val, __gu_err); \
268 __put_user_asm("sh", (ptr), __gu_val, __gu_err); \
271 __put_user_asm("sw", (ptr), __gu_val, __gu_err); \
274 __put_user_asm_8((ptr), __gu_val, __gu_err); \
277 /*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \
282 static inline unsigned long
283 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
285 return __copy_tofrom_user((__force
void __user
*)to
, from
, n
);
288 static inline unsigned long
289 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
291 return __copy_tofrom_user(to
, (__force
const void __user
*)from
, n
);
293 #define INLINE_COPY_FROM_USER
294 #define INLINE_COPY_TO_USER
297 * Copy a null terminated string from userspace.
299 extern int __strncpy_user(char *to
, const char __user
*from
, int len
);
302 strncpy_from_user(char *dst
, const char __user
*src
, long count
)
304 if (!access_ok(src
, 1))
306 return __strncpy_user(dst
, src
, count
);
310 * Return the size of a string (including the ending 0)
312 * Return 0 on exception, a value greater than N if too long
314 extern int __strnlen_user(const char __user
*sstr
, int len
);
316 static inline long strnlen_user(const char __user
*src
, long n
)
318 if (!access_ok(src
, 1))
320 return __strnlen_user(src
, n
);
323 #endif /* _ASM_MICROBLAZE_UACCESS_H */