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>
15 #include <linux/string.h>
16 #include <linux/version.h>
17 #include <asm/segment.h>
20 #define VERIFY_WRITE 1
22 static inline int access_ok(int type, const void *addr, unsigned long size)
24 unsigned long limit = current_thread_info()->addr_limit.seg;
26 return (((unsigned long)addr < limit) &&
27 ((unsigned long)(addr + size) < limit));
30 static inline int verify_area(int type, const void *addr, unsigned long size)
32 return access_ok(type, addr, size) ? 0 : -EFAULT;
35 #define __addr_ok(addr) (access_ok(VERIFY_READ, addr, 0))
37 extern int __put_user_bad(void);
40 * Tell gcc we read from memory instead of writing: this is because
41 * we do not write to any memory gcc knows about, so there are no
46 * These are the main single-value transfer routines. They automatically
47 * use the right size if we just have the right pointer type.
49 * This gets kind of ugly. We want to return _two_ values in "get_user()"
50 * and yet we don't want to do any pointers, because that is too much
51 * of a performance impact. Thus we have a few rather ugly macros here,
52 * and hide all the ugliness from the user.
54 * The "__xxx" versions of the user access functions are versions that
55 * do not verify the address space, that must have been done previously
56 * with a separate "access_ok()" call (this is used when we do multiple
57 * accesses to the same area of user memory).
59 * As we use the same address space for kernel and user data on
60 * Ckcore, we can just do these as direct assignments. (Of course, the
61 * exception handling means that it's no longer "just"...)
64 #define put_user(x, ptr) \
65 __put_user_check((x), (ptr), sizeof(*(ptr)))
67 #define __put_user(x, ptr) \
68 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
70 #define __ptr(x) ((unsigned long *)(x))
72 #define get_user(x, ptr) \
73 __get_user_check((x), (ptr), sizeof(*(ptr)))
75 #define __get_user(x, ptr) \
76 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
78 #define __put_user_nocheck(x, ptr, size) \
81 typeof(*(ptr)) *__pu_addr = (ptr); \
82 typeof(*(ptr)) __pu_val = (typeof(*(ptr)))(x); \
84 __put_user_size(__pu_val, (__pu_addr), (size), \
89 #define __put_user_check(x, ptr, size) \
91 long __pu_err = -EFAULT; \
92 typeof(*(ptr)) *__pu_addr = (ptr); \
93 typeof(*(ptr)) __pu_val = (typeof(*(ptr)))(x); \
94 if (access_ok(VERIFY_WRITE, __pu_addr, size) && __pu_addr) \
95 __put_user_size(__pu_val, __pu_addr, (size), __pu_err); \
99 #define __put_user_size(x, ptr, size, retval) \
104 __put_user_asm_b(x, ptr, retval); \
107 __put_user_asm_h(x, ptr, retval); \
110 __put_user_asm_w(x, ptr, retval); \
113 __put_user_asm_64(x, ptr, retval); \
121 * We don't tell gcc that we are accessing memory, but this is OK
122 * because we do not write to any memory gcc knows about, so there
123 * are no aliasing issues.
125 * Note that PC at a fault is the address *after* the faulting
128 #define __put_user_asm_b(x, ptr, err) \
132 "1: stb %1, (%2,0) \n" \
136 ".section __ex_table, \"a\" \n" \
141 : "=r"(err), "=r"(x), "=r"(ptr), "=r"(errcode) \
142 : "0"(err), "1"(x), "2"(ptr), "3"(-EFAULT) \
146 #define __put_user_asm_h(x, ptr, err) \
150 "1: sth %1, (%2,0) \n" \
154 ".section __ex_table, \"a\" \n" \
159 : "=r"(err), "=r"(x), "=r"(ptr), "=r"(errcode) \
160 : "0"(err), "1"(x), "2"(ptr), "3"(-EFAULT) \
164 #define __put_user_asm_w(x, ptr, err) \
168 "1: stw %1, (%2,0) \n" \
172 ".section __ex_table,\"a\" \n" \
177 : "=r"(err), "=r"(x), "=r"(ptr), "=r"(errcode) \
178 : "0"(err), "1"(x), "2"(ptr), "3"(-EFAULT) \
182 #define __put_user_asm_64(x, ptr, err) \
186 typeof(*(ptr))src = (typeof(*(ptr)))x; \
187 typeof(*(ptr))*psrc = &src; \
190 " ldw %3, (%1, 0) \n" \
191 "1: stw %3, (%2, 0) \n" \
192 " ldw %3, (%1, 4) \n" \
193 "2: stw %3, (%2, 4) \n" \
197 ".section __ex_table, \"a\" \n" \
203 : "=r"(err), "=r"(psrc), "=r"(ptr), \
204 "=r"(tmp), "=r"(errcode) \
205 : "0"(err), "1"(psrc), "2"(ptr), "3"(0), "4"(-EFAULT) \
209 #define __get_user_nocheck(x, ptr, size) \
212 __get_user_size(x, (ptr), (size), __gu_err); \
216 #define __get_user_check(x, ptr, size) \
218 int __gu_err = -EFAULT; \
219 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
220 if (access_ok(VERIFY_READ, __gu_ptr, size) && __gu_ptr) \
221 __get_user_size(x, __gu_ptr, size, __gu_err); \
225 #define __get_user_size(x, ptr, size, retval) \
229 __get_user_asm_common((x), ptr, "ldb", retval); \
232 __get_user_asm_common((x), ptr, "ldh", retval); \
235 __get_user_asm_common((x), ptr, "ldw", retval); \
239 (retval) = __get_user_bad(); \
243 #define __get_user_asm_common(x, ptr, ins, err) \
247 "1: " ins " %1, (%4,0) \n" \
253 ".section __ex_table,\"a\" \n" \
258 : "=r"(err), "=r"(x), "=r"(errcode) \
259 : "0"(0), "r"(ptr), "2"(-EFAULT) \
263 extern int __get_user_bad(void);
265 #define __copy_user(to, from, n) \
267 int w0, w1, w2, w3; \
269 "0: cmpnei %1, 0 \n" \
277 "1: cmplti %0, 16 \n" /* 4W */ \
279 " ldw %3, (%2, 0) \n" \
280 " ldw %4, (%2, 4) \n" \
281 " ldw %5, (%2, 8) \n" \
282 " ldw %6, (%2, 12) \n" \
283 "2: stw %3, (%1, 0) \n" \
284 "9: stw %4, (%1, 4) \n" \
285 "10: stw %5, (%1, 8) \n" \
286 "11: stw %6, (%1, 12) \n" \
291 "3: cmplti %0, 4 \n" /* 1W */ \
293 " ldw %3, (%2, 0) \n" \
294 "4: stw %3, (%1, 0) \n" \
299 "5: cmpnei %0, 0 \n" /* 1B */ \
301 " ldb %3, (%2, 0) \n" \
302 "6: stb %3, (%1, 0) \n" \
308 ".section __ex_table, \"a\" \n" \
318 : "=r"(n), "=r"(to), "=r"(from), "=r"(w0), \
319 "=r"(w1), "=r"(w2), "=r"(w3) \
320 : "0"(n), "1"(to), "2"(from) \
324 #define __copy_user_zeroing(to, from, n) \
329 "0: cmpnei %1, 0 \n" \
337 "1: cmplti %0, 16 \n" \
339 "2: ldw %3, (%2, 0) \n" \
340 "10: ldw %4, (%2, 4) \n" \
341 " stw %3, (%1, 0) \n" \
342 " stw %4, (%1, 4) \n" \
343 "11: ldw %3, (%2, 8) \n" \
344 "12: ldw %4, (%2, 12) \n" \
345 " stw %3, (%1, 8) \n" \
346 " stw %4, (%1, 12) \n" \
351 "3: cmplti %0, 4 \n" \
353 "4: ldw %3, (%2, 0) \n" \
354 " stw %3, (%1, 0) \n" \
359 "5: cmpnei %0, 0 \n" \
361 "6: ldb %3, (%2, 0) \n" \
362 " stb %3, (%1, 0) \n" \
369 "9: stb %4, (%1, 0) \n" \
375 ".section __ex_table, \"a\" \n" \
385 : "=r"(n), "=r"(to), "=r"(from), "=r"(nsave), \
387 : "0"(n), "1"(to), "2"(from) \
391 unsigned long raw_copy_from_user(void *to, const void *from, unsigned long n);
392 unsigned long raw_copy_to_user(void *to, const void *from, unsigned long n);
394 unsigned long clear_user(void *to, unsigned long n);
395 unsigned long __clear_user(void __user *to, unsigned long n);
397 long strncpy_from_user(char *dst, const char *src, long count);
398 long __strncpy_from_user(char *dst, const char *src, long count);
401 * Return the size of a string (including the ending 0)
403 * Return 0 on exception, a value greater than N if too long
405 long strnlen_user(const char *src, long n);
407 #define strlen_user(str) strnlen_user(str, 32767)
409 struct exception_table_entry {
411 unsigned long nextinsn;
414 extern int fixup_exception(struct pt_regs *regs);
416 #endif /* __ASM_CSKY_UACCESS_H */