1 /* SPDX-License-Identifier: GPL-2.0 */
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
4 #ifndef __ASM_CSKY_UACCESS_H
5 #define __ASM_CSKY_UACCESS_H
8 * User space memory access functions
10 #include <linux/compiler.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/sched.h>
14 #include <linux/string.h>
15 #include <linux/version.h>
16 #include <asm/segment.h>
18 static inline int access_ok(const void *addr
, unsigned long size
)
20 unsigned long limit
= current_thread_info()->addr_limit
.seg
;
22 return (((unsigned long)addr
< limit
) &&
23 ((unsigned long)(addr
+ size
) < limit
));
26 #define __addr_ok(addr) (access_ok(addr, 0))
28 extern int __put_user_bad(void);
31 * Tell gcc we read from memory instead of writing: this is because
32 * we do not write to any memory gcc knows about, so there are no
37 * These are the main single-value transfer routines. They automatically
38 * use the right size if we just have the right pointer type.
40 * This gets kind of ugly. We want to return _two_ values in "get_user()"
41 * and yet we don't want to do any pointers, because that is too much
42 * of a performance impact. Thus we have a few rather ugly macros here,
43 * and hide all the ugliness from the user.
45 * The "__xxx" versions of the user access functions are versions that
46 * do not verify the address space, that must have been done previously
47 * with a separate "access_ok()" call (this is used when we do multiple
48 * accesses to the same area of user memory).
50 * As we use the same address space for kernel and user data on
51 * Ckcore, we can just do these as direct assignments. (Of course, the
52 * exception handling means that it's no longer "just"...)
55 #define put_user(x, ptr) \
56 __put_user_check((x), (ptr), sizeof(*(ptr)))
58 #define __put_user(x, ptr) \
59 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
61 #define __ptr(x) ((unsigned long *)(x))
63 #define get_user(x, ptr) \
64 __get_user_check((x), (ptr), sizeof(*(ptr)))
66 #define __get_user(x, ptr) \
67 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
69 #define __put_user_nocheck(x, ptr, size) \
72 typeof(*(ptr)) *__pu_addr = (ptr); \
73 typeof(*(ptr)) __pu_val = (typeof(*(ptr)))(x); \
75 __put_user_size(__pu_val, (__pu_addr), (size), \
80 #define __put_user_check(x, ptr, size) \
82 long __pu_err = -EFAULT; \
83 typeof(*(ptr)) *__pu_addr = (ptr); \
84 typeof(*(ptr)) __pu_val = (typeof(*(ptr)))(x); \
85 if (access_ok(__pu_addr, size) && __pu_addr) \
86 __put_user_size(__pu_val, __pu_addr, (size), __pu_err); \
90 #define __put_user_size(x, ptr, size, retval) \
95 __put_user_asm_b(x, ptr, retval); \
98 __put_user_asm_h(x, ptr, retval); \
101 __put_user_asm_w(x, ptr, retval); \
104 __put_user_asm_64(x, ptr, retval); \
112 * We don't tell gcc that we are accessing memory, but this is OK
113 * because we do not write to any memory gcc knows about, so there
114 * are no aliasing issues.
116 * Note that PC at a fault is the address *after* the faulting
119 #define __put_user_asm_b(x, ptr, err) \
123 "1: stb %1, (%2,0) \n" \
127 ".section __ex_table, \"a\" \n" \
132 : "=r"(err), "=r"(x), "=r"(ptr), "=r"(errcode) \
133 : "0"(err), "1"(x), "2"(ptr), "3"(-EFAULT) \
137 #define __put_user_asm_h(x, ptr, err) \
141 "1: sth %1, (%2,0) \n" \
145 ".section __ex_table, \"a\" \n" \
150 : "=r"(err), "=r"(x), "=r"(ptr), "=r"(errcode) \
151 : "0"(err), "1"(x), "2"(ptr), "3"(-EFAULT) \
155 #define __put_user_asm_w(x, ptr, err) \
159 "1: stw %1, (%2,0) \n" \
163 ".section __ex_table,\"a\" \n" \
168 : "=r"(err), "=r"(x), "=r"(ptr), "=r"(errcode) \
169 : "0"(err), "1"(x), "2"(ptr), "3"(-EFAULT) \
173 #define __put_user_asm_64(x, ptr, err) \
177 typeof(*(ptr))src = (typeof(*(ptr)))x; \
178 typeof(*(ptr))*psrc = &src; \
181 " ldw %3, (%1, 0) \n" \
182 "1: stw %3, (%2, 0) \n" \
183 " ldw %3, (%1, 4) \n" \
184 "2: stw %3, (%2, 4) \n" \
188 ".section __ex_table, \"a\" \n" \
194 : "=r"(err), "=r"(psrc), "=r"(ptr), \
195 "=r"(tmp), "=r"(errcode) \
196 : "0"(err), "1"(psrc), "2"(ptr), "3"(0), "4"(-EFAULT) \
200 #define __get_user_nocheck(x, ptr, size) \
203 __get_user_size(x, (ptr), (size), __gu_err); \
207 #define __get_user_check(x, ptr, size) \
209 int __gu_err = -EFAULT; \
210 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
211 if (access_ok(__gu_ptr, size) && __gu_ptr) \
212 __get_user_size(x, __gu_ptr, size, __gu_err); \
216 #define __get_user_size(x, ptr, size, retval) \
220 __get_user_asm_common((x), ptr, "ldb", retval); \
223 __get_user_asm_common((x), ptr, "ldh", retval); \
226 __get_user_asm_common((x), ptr, "ldw", retval); \
230 (retval) = __get_user_bad(); \
234 #define __get_user_asm_common(x, ptr, ins, err) \
238 "1: " ins " %1, (%4,0) \n" \
244 ".section __ex_table,\"a\" \n" \
249 : "=r"(err), "=r"(x), "=r"(errcode) \
250 : "0"(0), "r"(ptr), "2"(-EFAULT) \
254 extern int __get_user_bad(void);
256 #define ___copy_to_user(to, from, n) \
258 int w0, w1, w2, w3; \
260 "0: cmpnei %1, 0 \n" \
268 "1: cmplti %0, 16 \n" /* 4W */ \
270 " ldw %3, (%2, 0) \n" \
271 " ldw %4, (%2, 4) \n" \
272 " ldw %5, (%2, 8) \n" \
273 " ldw %6, (%2, 12) \n" \
274 "2: stw %3, (%1, 0) \n" \
275 "9: stw %4, (%1, 4) \n" \
276 "10: stw %5, (%1, 8) \n" \
277 "11: stw %6, (%1, 12) \n" \
282 "3: cmplti %0, 4 \n" /* 1W */ \
284 " ldw %3, (%2, 0) \n" \
285 "4: stw %3, (%1, 0) \n" \
290 "5: cmpnei %0, 0 \n" /* 1B */ \
292 " ldb %3, (%2, 0) \n" \
293 "6: stb %3, (%1, 0) \n" \
300 "12: subi %0, 4 \n" \
302 ".section __ex_table, \"a\" \n" \
312 : "=r"(n), "=r"(to), "=r"(from), "=r"(w0), \
313 "=r"(w1), "=r"(w2), "=r"(w3) \
314 : "0"(n), "1"(to), "2"(from) \
318 #define ___copy_from_user(to, from, n) \
323 "0: cmpnei %1, 0 \n" \
331 "1: cmplti %0, 16 \n" \
333 "2: ldw %3, (%2, 0) \n" \
334 "10: ldw %4, (%2, 4) \n" \
335 " stw %3, (%1, 0) \n" \
336 " stw %4, (%1, 4) \n" \
337 "11: ldw %3, (%2, 8) \n" \
338 "12: ldw %4, (%2, 12) \n" \
339 " stw %3, (%1, 8) \n" \
340 " stw %4, (%1, 12) \n" \
345 "3: cmplti %0, 4 \n" \
347 "4: ldw %3, (%2, 0) \n" \
348 " stw %3, (%1, 0) \n" \
353 "5: cmpnei %0, 0 \n" \
355 "6: ldb %3, (%2, 0) \n" \
356 " stb %3, (%1, 0) \n" \
361 "8: stw %3, (%1, 0) \n" \
366 "13: stw %3, (%1, 8) \n" \
369 ".section __ex_table, \"a\" \n" \
379 : "=r"(n), "=r"(to), "=r"(from), "=r"(nsave), \
381 : "0"(n), "1"(to), "2"(from) \
385 unsigned long raw_copy_from_user(void *to
, const void *from
, unsigned long n
);
386 unsigned long raw_copy_to_user(void *to
, const void *from
, unsigned long n
);
388 unsigned long clear_user(void *to
, unsigned long n
);
389 unsigned long __clear_user(void __user
*to
, unsigned long n
);
391 long strncpy_from_user(char *dst
, const char *src
, long count
);
392 long __strncpy_from_user(char *dst
, const char *src
, long count
);
395 * Return the size of a string (including the ending 0)
397 * Return 0 on exception, a value greater than N if too long
399 long strnlen_user(const char *src
, long n
);
401 #define strlen_user(str) strnlen_user(str, 32767)
403 struct exception_table_entry
{
405 unsigned long nextinsn
;
408 extern int fixup_exception(struct pt_regs
*regs
);
410 #endif /* __ASM_CSKY_UACCESS_H */