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 if (access_ok(from
, n
))
11 __copy_user_zeroing(to
, from
, n
);
16 EXPORT_SYMBOL(raw_copy_from_user
);
18 unsigned long raw_copy_to_user(void *to
, const void *from
,
22 __copy_user(to
, from
, n
);
25 EXPORT_SYMBOL(raw_copy_to_user
);
29 * copy a null terminated string from userspace.
31 #define __do_strncpy_from_user(dst, src, count, res) \
38 "1: cmpnei %1, 0 \n" \
40 "2: ldb %4, (%3, 0) \n" \
41 " stb %4, (%2, 0) \n" \
52 ".section __ex_table, \"a\" \n" \
57 : "=r"(res), "=r"(count), "=r"(dst), \
58 "=r"(src), "=r"(tmp), "=r"(faultres) \
59 : "5"(-EFAULT), "0"(count), "1"(count), \
65 * __strncpy_from_user: - Copy a NUL terminated string from userspace,
67 * @dst: Destination address, in kernel space. This buffer must be at
68 * least @count bytes long.
69 * @src: Source address, in user space.
70 * @count: Maximum number of bytes to copy, including the trailing NUL.
72 * Copies a NUL-terminated string from userspace to kernel space.
73 * Caller must check the specified block with access_ok() before calling
76 * On success, returns the length of the string (not including the trailing
79 * If access to userspace fails, returns -EFAULT (some data may have been
82 * If @count is smaller than the length of the string, copies @count bytes
85 long __strncpy_from_user(char *dst
, const char *src
, long count
)
89 __do_strncpy_from_user(dst
, src
, count
, res
);
92 EXPORT_SYMBOL(__strncpy_from_user
);
95 * strncpy_from_user: - Copy a NUL terminated string from userspace.
96 * @dst: Destination address, in kernel space. This buffer must be at
97 * least @count bytes long.
98 * @src: Source address, in user space.
99 * @count: Maximum number of bytes to copy, including the trailing NUL.
101 * Copies a NUL-terminated string from userspace to kernel space.
103 * On success, returns the length of the string (not including the trailing
106 * If access to userspace fails, returns -EFAULT (some data may have been
109 * If @count is smaller than the length of the string, copies @count bytes
110 * and returns @count.
112 long strncpy_from_user(char *dst
, const char *src
, long count
)
116 if (access_ok(src
, 1))
117 __do_strncpy_from_user(dst
, src
, count
, res
);
120 EXPORT_SYMBOL(strncpy_from_user
);
123 * strlen_user: - Get the size of a string in user space.
124 * @str: The string to measure.
125 * @n: The maximum valid length
127 * Get the size of a NUL-terminated string in user space.
129 * Returns the size of the string INCLUDING the terminating NUL.
130 * On exception, returns 0.
131 * If the string is too long, returns a value greater than @n.
133 long strnlen_user(const char *s
, long n
)
135 unsigned long res
, tmp
;
145 "2: ldb %3, (%1, 0) \n"
156 ".section __ex_table, \"a\" \n"
161 : "=r"(n
), "=r"(s
), "=r"(res
), "=r"(tmp
)
162 : "0"(n
), "1"(s
), "2"(n
)
167 EXPORT_SYMBOL(strnlen_user
);
169 #define __do_clear_user(addr, size) \
171 int __d0, zvalue, tmp; \
174 "0: cmpnei %1, 0 \n" \
181 "1: cmplti %0, 32 \n" /* 4W */ \
183 "8: stw %2, (%1, 0) \n" \
184 "10: stw %2, (%1, 4) \n" \
185 "11: stw %2, (%1, 8) \n" \
186 "12: stw %2, (%1, 12) \n" \
187 "13: stw %2, (%1, 16) \n" \
188 "14: stw %2, (%1, 20) \n" \
189 "15: stw %2, (%1, 24) \n" \
190 "16: stw %2, (%1, 28) \n" \
194 "3: cmplti %0, 4 \n" /* 1W */ \
196 "4: stw %2, (%1, 0) \n" \
200 "5: cmpnei %0, 0 \n" /* 1B */ \
202 "6: stb %2, (%1, 0) \n" \
206 ".section __ex_table,\"a\" \n" \
220 : "=r"(size), "=r" (__d0), \
221 "=r"(zvalue), "=r"(tmp) \
222 : "0"(size), "1"(addr), "2"(0) \
227 * clear_user: - Zero a block of memory in user space.
228 * @to: Destination address, in user space.
229 * @n: Number of bytes to zero.
231 * Zero a block of memory in user space.
233 * Returns number of bytes that could not be cleared.
234 * On success, this will be zero.
237 clear_user(void __user
*to
, unsigned long n
)
239 if (access_ok(to
, n
))
240 __do_clear_user(to
, n
);
243 EXPORT_SYMBOL(clear_user
);
246 * __clear_user: - Zero a block of memory in user space, with less checking.
247 * @to: Destination address, in user space.
248 * @n: Number of bytes to zero.
250 * Zero a block of memory in user space. Caller must check
251 * the specified block with access_ok() before calling this function.
253 * Returns number of bytes that could not be cleared.
254 * On success, this will be zero.
257 __clear_user(void __user
*to
, unsigned long n
)
259 __do_clear_user(to
, n
);
262 EXPORT_SYMBOL(__clear_user
);