1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd. 3 4 #include <linux/uaccess.h> 5 #include <linux/types.h> 6 7 unsigned long raw_copy_from_user(void *to, const void *from, 8 unsigned long n) 9 { 10 int tmp, nsave; 11 12 __asm__ __volatile__( 13 "0: cmpnei %1, 0 \n" 14 " bf 7f \n" 15 " mov %3, %1 \n" 16 " or %3, %2 \n" 17 " andi %3, 3 \n" 18 " cmpnei %3, 0 \n" 19 " bf 1f \n" 20 " br 5f \n" 21 "1: cmplti %0, 16 \n" 22 " bt 3f \n" 23 "2: ldw %3, (%2, 0) \n" 24 "10: ldw %4, (%2, 4) \n" 25 " stw %3, (%1, 0) \n" 26 " stw %4, (%1, 4) \n" 27 "11: ldw %3, (%2, 8) \n" 28 "12: ldw %4, (%2, 12) \n" 29 " stw %3, (%1, 8) \n" 30 " stw %4, (%1, 12) \n" 31 " addi %2, 16 \n" 32 " addi %1, 16 \n" 33 " subi %0, 16 \n" 34 " br 1b \n" 35 "3: cmplti %0, 4 \n" 36 " bt 5f \n" 37 "4: ldw %3, (%2, 0) \n" 38 " stw %3, (%1, 0) \n" 39 " addi %2, 4 \n" 40 " addi %1, 4 \n" 41 " subi %0, 4 \n" 42 " br 3b \n" 43 "5: cmpnei %0, 0 \n" 44 " bf 7f \n" 45 "6: ldb %3, (%2, 0) \n" 46 " stb %3, (%1, 0) \n" 47 " addi %2, 1 \n" 48 " addi %1, 1 \n" 49 " subi %0, 1 \n" 50 " br 5b \n" 51 "8: stw %3, (%1, 0) \n" 52 " subi %0, 4 \n" 53 " bf 7f \n" 54 "9: subi %0, 8 \n" 55 " bf 7f \n" 56 "13: stw %3, (%1, 8) \n" 57 " subi %0, 12 \n" 58 " bf 7f \n" 59 ".section __ex_table, \"a\" \n" 60 ".align 2 \n" 61 ".long 2b, 7f \n" 62 ".long 4b, 7f \n" 63 ".long 6b, 7f \n" 64 ".long 10b, 8b \n" 65 ".long 11b, 9b \n" 66 ".long 12b,13b \n" 67 ".previous \n" 68 "7: \n" 69 : "=r"(n), "=r"(to), "=r"(from), "=r"(nsave), 70 "=r"(tmp) 71 : "0"(n), "1"(to), "2"(from) 72 : "memory"); 73 74 return n; 75 } 76 EXPORT_SYMBOL(raw_copy_from_user); 77 78 unsigned long raw_copy_to_user(void *to, const void *from, 79 unsigned long n) 80 { 81 int w0, w1, w2, w3; 82 83 __asm__ __volatile__( 84 "0: cmpnei %1, 0 \n" 85 " bf 8f \n" 86 " mov %3, %1 \n" 87 " or %3, %2 \n" 88 " andi %3, 3 \n" 89 " cmpnei %3, 0 \n" 90 " bf 1f \n" 91 " br 5f \n" 92 "1: cmplti %0, 16 \n" /* 4W */ 93 " bt 3f \n" 94 " ldw %3, (%2, 0) \n" 95 " ldw %4, (%2, 4) \n" 96 " ldw %5, (%2, 8) \n" 97 " ldw %6, (%2, 12) \n" 98 "2: stw %3, (%1, 0) \n" 99 "9: stw %4, (%1, 4) \n" 100 "10: stw %5, (%1, 8) \n" 101 "11: stw %6, (%1, 12) \n" 102 " addi %2, 16 \n" 103 " addi %1, 16 \n" 104 " subi %0, 16 \n" 105 " br 1b \n" 106 "3: cmplti %0, 4 \n" /* 1W */ 107 " bt 5f \n" 108 " ldw %3, (%2, 0) \n" 109 "4: stw %3, (%1, 0) \n" 110 " addi %2, 4 \n" 111 " addi %1, 4 \n" 112 " subi %0, 4 \n" 113 " br 3b \n" 114 "5: cmpnei %0, 0 \n" /* 1B */ 115 " bf 13f \n" 116 " ldb %3, (%2, 0) \n" 117 "6: stb %3, (%1, 0) \n" 118 " addi %2, 1 \n" 119 " addi %1, 1 \n" 120 " subi %0, 1 \n" 121 " br 5b \n" 122 "7: subi %0, 4 \n" 123 "8: subi %0, 4 \n" 124 "12: subi %0, 4 \n" 125 " br 13f \n" 126 ".section __ex_table, \"a\" \n" 127 ".align 2 \n" 128 ".long 2b, 13f \n" 129 ".long 4b, 13f \n" 130 ".long 6b, 13f \n" 131 ".long 9b, 12b \n" 132 ".long 10b, 8b \n" 133 ".long 11b, 7b \n" 134 ".previous \n" 135 "13: \n" 136 : "=r"(n), "=r"(to), "=r"(from), "=r"(w0), 137 "=r"(w1), "=r"(w2), "=r"(w3) 138 : "0"(n), "1"(to), "2"(from) 139 : "memory"); 140 141 return n; 142 } 143 EXPORT_SYMBOL(raw_copy_to_user); 144 145 /* 146 * __strncpy_from_user: - Copy a NUL terminated string from userspace, 147 * with less checking. 148 * @dst: Destination address, in kernel space. This buffer must be at 149 * least @count bytes long. 150 * @src: Source address, in user space. 151 * @count: Maximum number of bytes to copy, including the trailing NUL. 152 * 153 * Copies a NUL-terminated string from userspace to kernel space. 154 * Caller must check the specified block with access_ok() before calling 155 * this function. 156 * 157 * On success, returns the length of the string (not including the trailing 158 * NUL). 159 * 160 * If access to userspace fails, returns -EFAULT (some data may have been 161 * copied). 162 * 163 * If @count is smaller than the length of the string, copies @count bytes 164 * and returns @count. 165 */ 166 long __strncpy_from_user(char *dst, const char *src, long count) 167 { 168 long res, faultres; 169 int tmp; 170 171 __asm__ __volatile__( 172 " cmpnei %3, 0 \n" 173 " bf 4f \n" 174 "1: cmpnei %1, 0 \n" 175 " bf 5f \n" 176 "2: ldb %4, (%3, 0) \n" 177 " stb %4, (%2, 0) \n" 178 " cmpnei %4, 0 \n" 179 " bf 3f \n" 180 " addi %3, 1 \n" 181 " addi %2, 1 \n" 182 " subi %1, 1 \n" 183 " br 1b \n" 184 "3: subu %0, %1 \n" 185 " br 5f \n" 186 "4: mov %0, %5 \n" 187 " br 5f \n" 188 ".section __ex_table, \"a\" \n" 189 ".align 2 \n" 190 ".long 2b, 4b \n" 191 ".previous \n" 192 "5: \n" 193 : "=r"(res), "=r"(count), "=r"(dst), 194 "=r"(src), "=r"(tmp), "=r"(faultres) 195 : "5"(-EFAULT), "0"(count), "1"(count), 196 "2"(dst), "3"(src) 197 : "memory"); 198 199 return res; 200 } 201 EXPORT_SYMBOL(__strncpy_from_user); 202 203 /* 204 * strnlen_user: - Get the size of a string in user space. 205 * @str: The string to measure. 206 * @n: The maximum valid length 207 * 208 * Get the size of a NUL-terminated string in user space. 209 * 210 * Returns the size of the string INCLUDING the terminating NUL. 211 * On exception, returns 0. 212 * If the string is too long, returns a value greater than @n. 213 */ 214 long __strnlen_user(const char *s, long n) 215 { 216 unsigned long res, tmp; 217 218 __asm__ __volatile__( 219 " cmpnei %1, 0 \n" 220 " bf 3f \n" 221 "1: cmpnei %0, 0 \n" 222 " bf 3f \n" 223 "2: ldb %3, (%1, 0) \n" 224 " cmpnei %3, 0 \n" 225 " bf 3f \n" 226 " subi %0, 1 \n" 227 " addi %1, 1 \n" 228 " br 1b \n" 229 "3: subu %2, %0 \n" 230 " addi %2, 1 \n" 231 " br 5f \n" 232 "4: movi %0, 0 \n" 233 " br 5f \n" 234 ".section __ex_table, \"a\" \n" 235 ".align 2 \n" 236 ".long 2b, 4b \n" 237 ".previous \n" 238 "5: \n" 239 : "=r"(n), "=r"(s), "=r"(res), "=r"(tmp) 240 : "0"(n), "1"(s), "2"(n) 241 : "memory"); 242 243 return res; 244 } 245 EXPORT_SYMBOL(__strnlen_user); 246 247 /* 248 * __clear_user: - Zero a block of memory in user space, with less checking. 249 * @to: Destination address, in user space. 250 * @n: Number of bytes to zero. 251 * 252 * Zero a block of memory in user space. Caller must check 253 * the specified block with access_ok() before calling this function. 254 * 255 * Returns number of bytes that could not be cleared. 256 * On success, this will be zero. 257 */ 258 unsigned long 259 __clear_user(void __user *to, unsigned long n) 260 { 261 int data, value, tmp; 262 263 __asm__ __volatile__( 264 "0: cmpnei %1, 0 \n" 265 " bf 7f \n" 266 " mov %3, %1 \n" 267 " andi %3, 3 \n" 268 " cmpnei %3, 0 \n" 269 " bf 1f \n" 270 " br 5f \n" 271 "1: cmplti %0, 32 \n" /* 4W */ 272 " bt 3f \n" 273 "8: stw %2, (%1, 0) \n" 274 "10: stw %2, (%1, 4) \n" 275 "11: stw %2, (%1, 8) \n" 276 "12: stw %2, (%1, 12) \n" 277 "13: stw %2, (%1, 16) \n" 278 "14: stw %2, (%1, 20) \n" 279 "15: stw %2, (%1, 24) \n" 280 "16: stw %2, (%1, 28) \n" 281 " addi %1, 32 \n" 282 " subi %0, 32 \n" 283 " br 1b \n" 284 "3: cmplti %0, 4 \n" /* 1W */ 285 " bt 5f \n" 286 "4: stw %2, (%1, 0) \n" 287 " addi %1, 4 \n" 288 " subi %0, 4 \n" 289 " br 3b \n" 290 "5: cmpnei %0, 0 \n" /* 1B */ 291 "9: bf 7f \n" 292 "6: stb %2, (%1, 0) \n" 293 " addi %1, 1 \n" 294 " subi %0, 1 \n" 295 " br 5b \n" 296 ".section __ex_table,\"a\" \n" 297 ".align 2 \n" 298 ".long 8b, 9b \n" 299 ".long 10b, 9b \n" 300 ".long 11b, 9b \n" 301 ".long 12b, 9b \n" 302 ".long 13b, 9b \n" 303 ".long 14b, 9b \n" 304 ".long 15b, 9b \n" 305 ".long 16b, 9b \n" 306 ".long 4b, 9b \n" 307 ".long 6b, 9b \n" 308 ".previous \n" 309 "7: \n" 310 : "=r"(n), "=r" (data), "=r"(value), "=r"(tmp) 311 : "0"(n), "1"(to), "2"(0) 312 : "memory"); 313 314 return n; 315 } 316 EXPORT_SYMBOL(__clear_user); 317