1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
4 #include <linux/uaccess.h>
5 #include <linux/types.h>
7 unsigned long raw_copy_from_user(void *to
, const void *from
,
10 ___copy_from_user(to
, from
, n
);
13 EXPORT_SYMBOL(raw_copy_from_user
);
15 unsigned long raw_copy_to_user(void *to
, const void *from
,
18 ___copy_to_user(to
, from
, n
);
21 EXPORT_SYMBOL(raw_copy_to_user
);
25 * copy a null terminated string from userspace.
27 #define __do_strncpy_from_user(dst, src, count, res) \
34 "1: cmpnei %1, 0 \n" \
36 "2: ldb %4, (%3, 0) \n" \
37 " stb %4, (%2, 0) \n" \
48 ".section __ex_table, \"a\" \n" \
53 : "=r"(res), "=r"(count), "=r"(dst), \
54 "=r"(src), "=r"(tmp), "=r"(faultres) \
55 : "5"(-EFAULT), "0"(count), "1"(count), \
61 * __strncpy_from_user: - Copy a NUL terminated string from userspace,
63 * @dst: Destination address, in kernel space. This buffer must be at
64 * least @count bytes long.
65 * @src: Source address, in user space.
66 * @count: Maximum number of bytes to copy, including the trailing NUL.
68 * Copies a NUL-terminated string from userspace to kernel space.
69 * Caller must check the specified block with access_ok() before calling
72 * On success, returns the length of the string (not including the trailing
75 * If access to userspace fails, returns -EFAULT (some data may have been
78 * If @count is smaller than the length of the string, copies @count bytes
81 long __strncpy_from_user(char *dst
, const char *src
, long count
)
85 __do_strncpy_from_user(dst
, src
, count
, res
);
88 EXPORT_SYMBOL(__strncpy_from_user
);
91 * strncpy_from_user: - Copy a NUL terminated string from userspace.
92 * @dst: Destination address, in kernel space. This buffer must be at
93 * least @count bytes long.
94 * @src: Source address, in user space.
95 * @count: Maximum number of bytes to copy, including the trailing NUL.
97 * Copies a NUL-terminated string from userspace to kernel space.
99 * On success, returns the length of the string (not including the trailing
102 * If access to userspace fails, returns -EFAULT (some data may have been
105 * If @count is smaller than the length of the string, copies @count bytes
106 * and returns @count.
108 long strncpy_from_user(char *dst
, const char *src
, long count
)
112 if (access_ok(src
, 1))
113 __do_strncpy_from_user(dst
, src
, count
, res
);
116 EXPORT_SYMBOL(strncpy_from_user
);
119 * strlen_user: - Get the size of a string in user space.
120 * @str: The string to measure.
121 * @n: The maximum valid length
123 * Get the size of a NUL-terminated string in user space.
125 * Returns the size of the string INCLUDING the terminating NUL.
126 * On exception, returns 0.
127 * If the string is too long, returns a value greater than @n.
129 long strnlen_user(const char *s
, long n
)
131 unsigned long res
, tmp
;
141 "2: ldb %3, (%1, 0) \n"
152 ".section __ex_table, \"a\" \n"
157 : "=r"(n
), "=r"(s
), "=r"(res
), "=r"(tmp
)
158 : "0"(n
), "1"(s
), "2"(n
)
163 EXPORT_SYMBOL(strnlen_user
);
165 #define __do_clear_user(addr, size) \
167 int __d0, zvalue, tmp; \
170 "0: cmpnei %1, 0 \n" \
177 "1: cmplti %0, 32 \n" /* 4W */ \
179 "8: stw %2, (%1, 0) \n" \
180 "10: stw %2, (%1, 4) \n" \
181 "11: stw %2, (%1, 8) \n" \
182 "12: stw %2, (%1, 12) \n" \
183 "13: stw %2, (%1, 16) \n" \
184 "14: stw %2, (%1, 20) \n" \
185 "15: stw %2, (%1, 24) \n" \
186 "16: stw %2, (%1, 28) \n" \
190 "3: cmplti %0, 4 \n" /* 1W */ \
192 "4: stw %2, (%1, 0) \n" \
196 "5: cmpnei %0, 0 \n" /* 1B */ \
198 "6: stb %2, (%1, 0) \n" \
202 ".section __ex_table,\"a\" \n" \
216 : "=r"(size), "=r" (__d0), \
217 "=r"(zvalue), "=r"(tmp) \
218 : "0"(size), "1"(addr), "2"(0) \
223 * clear_user: - Zero a block of memory in user space.
224 * @to: Destination address, in user space.
225 * @n: Number of bytes to zero.
227 * Zero a block of memory in user space.
229 * Returns number of bytes that could not be cleared.
230 * On success, this will be zero.
233 clear_user(void __user
*to
, unsigned long n
)
235 if (access_ok(to
, n
))
236 __do_clear_user(to
, n
);
239 EXPORT_SYMBOL(clear_user
);
242 * __clear_user: - Zero a block of memory in user space, with less checking.
243 * @to: Destination address, in user space.
244 * @n: Number of bytes to zero.
246 * Zero a block of memory in user space. Caller must check
247 * the specified block with access_ok() before calling this function.
249 * Returns number of bytes that could not be cleared.
250 * On success, this will be zero.
253 __clear_user(void __user
*to
, unsigned long n
)
255 __do_clear_user(to
, n
);
258 EXPORT_SYMBOL(__clear_user
);