1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
6 * -__clear_user( ) called multiple times during elf load was byte loop
7 * converted to do as much word clear as possible.
10 * -Hand crafted constant propagation for "constant" copy sizes
11 * -stock kernel shrunk by 33K at -O3
14 * -Added option to (UN)inline copy_(to|from)_user to reduce code sz
15 * -kernel shrunk by 200K even at -O3 (gcc 4.2.1)
16 * -Enabled when doing -Os
18 * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
21 #ifndef _ASM_ARC_UACCESS_H
22 #define _ASM_ARC_UACCESS_H
24 #include <linux/string.h> /* for generic string functions */
26 /*********** Single byte/hword/word copies ******************/
28 #define __get_user_fn(sz, u, k) \
30 long __ret = 0; /* success by default */ \
32 case 1: __arc_get_user_one(*(k), u, "ldb", __ret); break; \
33 case 2: __arc_get_user_one(*(k), u, "ldw", __ret); break; \
34 case 4: __arc_get_user_one(*(k), u, "ld", __ret); break; \
35 case 8: __arc_get_user_one_64(*(k), u, __ret); break; \
41 * Returns 0 on success, -EFAULT if not.
42 * @ret already contains 0 - given that errors will be less likely
43 * (hence +r asm constraint below).
44 * In case of error, fixup code will make it -EFAULT
46 #define __arc_get_user_one(dst, src, op, ret) \
47 __asm__ __volatile__( \
50 " .section .fixup, \"ax\"\n" \
52 "3: # return -EFAULT\n" \
54 " # zero out dst ptr\n" \
58 " .section __ex_table, \"a\"\n" \
63 : "+r" (ret), "=r" (dst) \
64 : "r" (src), "ir" (-EFAULT))
66 #define __arc_get_user_one_64(dst, src, ret) \
67 __asm__ __volatile__( \
69 "4: ld %R1,[%2, 4]\n" \
71 " .section .fixup, \"ax\"\n" \
73 "3: # return -EFAULT\n" \
75 " # zero out dst ptr\n" \
80 " .section __ex_table, \"a\"\n" \
86 : "+r" (ret), "=r" (dst) \
87 : "r" (src), "ir" (-EFAULT))
89 #define __put_user_fn(sz, u, k) \
91 long __ret = 0; /* success by default */ \
93 case 1: __arc_put_user_one(*(k), u, "stb", __ret); break; \
94 case 2: __arc_put_user_one(*(k), u, "stw", __ret); break; \
95 case 4: __arc_put_user_one(*(k), u, "st", __ret); break; \
96 case 8: __arc_put_user_one_64(*(k), u, __ret); break; \
101 #define __arc_put_user_one(src, dst, op, ret) \
102 __asm__ __volatile__( \
103 "1: "op" %1,[%2]\n" \
105 " .section .fixup, \"ax\"\n" \
110 " .section __ex_table, \"a\"\n" \
116 : "r" (src), "r" (dst), "ir" (-EFAULT))
118 #define __arc_put_user_one_64(src, dst, ret) \
119 __asm__ __volatile__( \
121 "4: st %R1,[%2, 4]\n" \
123 " .section .fixup, \"ax\"\n" \
128 " .section __ex_table, \"a\"\n" \
135 : "r" (src), "r" (dst), "ir" (-EFAULT))
138 static inline unsigned long
139 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
143 unsigned long tmp1, tmp2, tmp3, tmp4;
144 unsigned long orig_n = n;
150 if (((unsigned long)to & 0x3) || ((unsigned long)from & 0x3)) {
154 __asm__ __volatile__ (
155 " mov.f lp_count, %0 \n"
157 "1: ldb.ab %1, [%3, 1] \n"
158 " stb.ab %1, [%2, 1] \n"
161 " .section .fixup, \"ax\" \n"
165 " .section __ex_table, \"a\" \n"
172 * Note as an '&' earlyclobber operand to make sure the
173 * temporary register inside the loop is not the same as
176 "=&r" (tmp), "+r" (to), "+r" (from)
178 : "lp_count", "memory");
184 * Hand-crafted constant propagation to reduce code sz of the
185 * laddered copy 16x,8,4,2,1
187 if (__builtin_constant_p(orig_n)) {
191 orig_n = orig_n % 16;
193 __asm__ __volatile__(
194 " lsr lp_count, %7,4 \n"
196 "1: ld.ab %3, [%2, 4] \n"
197 "11: ld.ab %4, [%2, 4] \n"
198 "12: ld.ab %5, [%2, 4] \n"
199 "13: ld.ab %6, [%2, 4] \n"
200 " st.ab %3, [%1, 4] \n"
201 " st.ab %4, [%1, 4] \n"
202 " st.ab %5, [%1, 4] \n"
203 " st.ab %6, [%1, 4] \n"
206 " .section .fixup, \"ax\" \n"
210 " .section __ex_table, \"a\" \n"
217 : "+r" (res), "+r"(to), "+r"(from),
218 "=r"(tmp1), "=r"(tmp2), "=r"(tmp3), "=r"(tmp4)
220 : "lp_count", "memory");
225 __asm__ __volatile__(
226 "14: ld.ab %3, [%2,4] \n"
227 "15: ld.ab %4, [%2,4] \n"
228 " st.ab %3, [%1,4] \n"
229 " st.ab %4, [%1,4] \n"
232 " .section .fixup, \"ax\" \n"
236 " .section __ex_table, \"a\" \n"
241 : "+r" (res), "+r"(to), "+r"(from),
242 "=r"(tmp1), "=r"(tmp2)
249 __asm__ __volatile__(
250 "16: ld.ab %3, [%2,4] \n"
251 " st.ab %3, [%1,4] \n"
254 " .section .fixup, \"ax\" \n"
258 " .section __ex_table, \"a\" \n"
262 : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
269 __asm__ __volatile__(
270 "17: ldw.ab %3, [%2,2] \n"
271 " stw.ab %3, [%1,2] \n"
274 " .section .fixup, \"ax\" \n"
278 " .section __ex_table, \"a\" \n"
282 : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
287 __asm__ __volatile__(
288 "18: ldb.ab %3, [%2,2] \n"
289 " stb.ab %3, [%1,2] \n"
292 " .section .fixup, \"ax\" \n"
296 " .section __ex_table, \"a\" \n"
300 : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
304 } else { /* n is NOT constant, so laddered copy of 16x,8,4,2,1 */
306 __asm__ __volatile__(
308 " lsr.f lp_count, %3,4 \n" /* 16x bytes */
310 "1: ld.ab %5, [%2, 4] \n"
311 "11: ld.ab %6, [%2, 4] \n"
312 "12: ld.ab %7, [%2, 4] \n"
313 "13: ld.ab %8, [%2, 4] \n"
314 " st.ab %5, [%1, 4] \n"
315 " st.ab %6, [%1, 4] \n"
316 " st.ab %7, [%1, 4] \n"
317 " st.ab %8, [%1, 4] \n"
319 "3: and.f %3,%3,0xf \n" /* stragglers */
321 " bbit0 %3,3,31f \n" /* 8 bytes left */
322 "14: ld.ab %5, [%2,4] \n"
323 "15: ld.ab %6, [%2,4] \n"
324 " st.ab %5, [%1,4] \n"
325 " st.ab %6, [%1,4] \n"
327 "31: bbit0 %3,2,32f \n" /* 4 bytes left */
328 "16: ld.ab %5, [%2,4] \n"
329 " st.ab %5, [%1,4] \n"
331 "32: bbit0 %3,1,33f \n" /* 2 bytes left */
332 "17: ldw.ab %5, [%2,2] \n"
333 " stw.ab %5, [%1,2] \n"
335 "33: bbit0 %3,0,34f \n"
336 "18: ldb.ab %5, [%2,1] \n" /* 1 byte left */
337 " stb.ab %5, [%1,1] \n"
340 " .section .fixup, \"ax\" \n"
344 " .section __ex_table, \"a\" \n"
356 : "=r" (res), "+r"(to), "+r"(from), "+r"(n), "=r"(val),
357 "=r"(tmp1), "=r"(tmp2), "=r"(tmp3), "=r"(tmp4)
359 : "lp_count", "memory");
365 static inline unsigned long
366 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
370 unsigned long tmp1, tmp2, tmp3, tmp4;
371 unsigned long orig_n = n;
377 if (((unsigned long)to & 0x3) || ((unsigned long)from & 0x3)) {
381 __asm__ __volatile__(
382 " mov.f lp_count, %0 \n"
384 " ldb.ab %1, [%3, 1] \n"
385 "1: stb.ab %1, [%2, 1] \n"
388 " .section .fixup, \"ax\" \n"
392 " .section __ex_table, \"a\" \n"
398 /* Note as an '&' earlyclobber operand to make sure the
399 * temporary register inside the loop is not the same as
402 "=&r" (tmp), "+r" (to), "+r" (from)
404 : "lp_count", "memory");
409 if (__builtin_constant_p(orig_n)) {
413 orig_n = orig_n % 16;
415 __asm__ __volatile__(
416 " lsr lp_count, %7,4 \n"
418 " ld.ab %3, [%2, 4] \n"
419 " ld.ab %4, [%2, 4] \n"
420 " ld.ab %5, [%2, 4] \n"
421 " ld.ab %6, [%2, 4] \n"
422 "1: st.ab %3, [%1, 4] \n"
423 "11: st.ab %4, [%1, 4] \n"
424 "12: st.ab %5, [%1, 4] \n"
425 "13: st.ab %6, [%1, 4] \n"
428 " .section .fixup, \"ax\" \n"
432 " .section __ex_table, \"a\" \n"
439 : "+r" (res), "+r"(to), "+r"(from),
440 "=r"(tmp1), "=r"(tmp2), "=r"(tmp3), "=r"(tmp4)
442 : "lp_count", "memory");
447 __asm__ __volatile__(
448 " ld.ab %3, [%2,4] \n"
449 " ld.ab %4, [%2,4] \n"
450 "14: st.ab %3, [%1,4] \n"
451 "15: st.ab %4, [%1,4] \n"
454 " .section .fixup, \"ax\" \n"
458 " .section __ex_table, \"a\" \n"
463 : "+r" (res), "+r"(to), "+r"(from),
464 "=r"(tmp1), "=r"(tmp2)
471 __asm__ __volatile__(
472 " ld.ab %3, [%2,4] \n"
473 "16: st.ab %3, [%1,4] \n"
476 " .section .fixup, \"ax\" \n"
480 " .section __ex_table, \"a\" \n"
484 : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
491 __asm__ __volatile__(
492 " ldw.ab %3, [%2,2] \n"
493 "17: stw.ab %3, [%1,2] \n"
496 " .section .fixup, \"ax\" \n"
500 " .section __ex_table, \"a\" \n"
504 : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
509 __asm__ __volatile__(
510 " ldb.ab %3, [%2,1] \n"
511 "18: stb.ab %3, [%1,1] \n"
514 " .section .fixup, \"ax\" \n"
518 " .section __ex_table, \"a\" \n"
522 : "+r" (res), "+r"(to), "+r"(from), "=r"(tmp1)
526 } else { /* n is NOT constant, so laddered copy of 16x,8,4,2,1 */
528 __asm__ __volatile__(
530 " lsr.f lp_count, %3,4 \n" /* 16x bytes */
532 " ld.ab %5, [%2, 4] \n"
533 " ld.ab %6, [%2, 4] \n"
534 " ld.ab %7, [%2, 4] \n"
535 " ld.ab %8, [%2, 4] \n"
536 "1: st.ab %5, [%1, 4] \n"
537 "11: st.ab %6, [%1, 4] \n"
538 "12: st.ab %7, [%1, 4] \n"
539 "13: st.ab %8, [%1, 4] \n"
541 "3: and.f %3,%3,0xf \n" /* stragglers */
543 " bbit0 %3,3,31f \n" /* 8 bytes left */
544 " ld.ab %5, [%2,4] \n"
545 " ld.ab %6, [%2,4] \n"
546 "14: st.ab %5, [%1,4] \n"
547 "15: st.ab %6, [%1,4] \n"
548 " sub.f %0, %0, 8 \n"
549 "31: bbit0 %3,2,32f \n" /* 4 bytes left */
550 " ld.ab %5, [%2,4] \n"
551 "16: st.ab %5, [%1,4] \n"
552 " sub.f %0, %0, 4 \n"
553 "32: bbit0 %3,1,33f \n" /* 2 bytes left */
554 " ldw.ab %5, [%2,2] \n"
555 "17: stw.ab %5, [%1,2] \n"
556 " sub.f %0, %0, 2 \n"
557 "33: bbit0 %3,0,34f \n"
558 " ldb.ab %5, [%2,1] \n" /* 1 byte left */
559 "18: stb.ab %5, [%1,1] \n"
560 " sub.f %0, %0, 1 \n"
562 " .section .fixup, \"ax\" \n"
566 " .section __ex_table, \"a\" \n"
578 : "=r" (res), "+r"(to), "+r"(from), "+r"(n), "=r"(val),
579 "=r"(tmp1), "=r"(tmp2), "=r"(tmp3), "=r"(tmp4)
581 : "lp_count", "memory");
587 static inline unsigned long __clear_user(void __user *to, unsigned long n)
590 unsigned char *d_char = to;
592 __asm__ __volatile__(
593 " bbit0 %0, 0, 1f \n"
594 "75: stb.ab %2, [%0,1] \n"
596 "1: bbit0 %0, 1, 2f \n"
597 "76: stw.ab %2, [%0,2] \n"
599 "2: asr.f lp_count, %1, 2 \n"
601 "77: st.ab %2, [%0,4] \n"
603 "3: bbit0 %1, 1, 4f \n"
604 "78: stw.ab %2, [%0,2] \n"
606 "4: bbit0 %1, 0, 5f \n"
607 "79: stb.ab %2, [%0,1] \n"
610 " .section .fixup, \"ax\" \n"
614 " .section __ex_table, \"a\" \n"
622 : "+r"(d_char), "+r"(res)
624 : "lp_count", "memory");
629 #define INLINE_COPY_TO_USER
630 #define INLINE_COPY_FROM_USER
632 #define __clear_user __clear_user
634 #include <asm-generic/uaccess.h>