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 */
27 #define __kernel_ok (uaccess_kernel())
30 * Algorithmically, for __user_ok() we want do:
31 * (start < TASK_SIZE) && (start+len < TASK_SIZE)
32 * where TASK_SIZE could either be retrieved from thread_info->addr_limit or
33 * emitted directly in code.
35 * This can however be rewritten as follows:
36 * (len <= TASK_SIZE) && (start+len < TASK_SIZE)
38 * Because it essentially checks if buffer end is within limit and @len is
39 * non-ngeative, which implies that buffer start will be within limit too.
41 * The reason for rewriting being, for majority of cases, @len is generally
42 * compile time constant, causing first sub-expression to be compile time
45 * The second part would generate weird large LIMMs e.g. (0x6000_0000 - 0x10),
46 * so we check for TASK_SIZE using get_fs() since the addr_limit load from mem
47 * would already have been done at this call site for __kernel_ok()
50 #define __user_ok(addr, sz) (((sz) <= TASK_SIZE) && \
51 ((addr) <= (get_fs() - (sz))))
52 #define __access_ok(addr, sz) (unlikely(__kernel_ok) || \
53 likely(__user_ok((addr), (sz))))
55 /*********** Single byte/hword/word copies ******************/
57 #define __get_user_fn(sz, u, k) \
59 long __ret = 0; /* success by default */ \
61 case 1: __arc_get_user_one(*(k), u, "ldb", __ret); break; \
62 case 2: __arc_get_user_one(*(k), u, "ldw", __ret); break; \
63 case 4: __arc_get_user_one(*(k), u, "ld", __ret); break; \
64 case 8: __arc_get_user_one_64(*(k), u, __ret); break; \
70 * Returns 0 on success, -EFAULT if not.
71 * @ret already contains 0 - given that errors will be less likely
72 * (hence +r asm constraint below).
73 * In case of error, fixup code will make it -EFAULT
75 #define __arc_get_user_one(dst, src, op, ret) \
76 __asm__ __volatile__( \
79 " .section .fixup, \"ax\"\n" \
81 "3: # return -EFAULT\n" \
83 " # zero out dst ptr\n" \
87 " .section __ex_table, \"a\"\n" \
92 : "+r" (ret), "=r" (dst) \
93 : "r" (src), "ir" (-EFAULT))
95 #define __arc_get_user_one_64(dst, src, ret) \
96 __asm__ __volatile__( \
98 "4: ld %R1,[%2, 4]\n" \
100 " .section .fixup, \"ax\"\n" \
102 "3: # return -EFAULT\n" \
104 " # zero out dst ptr\n" \
109 " .section __ex_table, \"a\"\n" \
115 : "+r" (ret), "=r" (dst) \
116 : "r" (src), "ir" (-EFAULT))
118 #define __put_user_fn(sz, u, k) \
120 long __ret = 0; /* success by default */ \
122 case 1: __arc_put_user_one(*(k), u, "stb", __ret); break; \
123 case 2: __arc_put_user_one(*(k), u, "stw", __ret); break; \
124 case 4: __arc_put_user_one(*(k), u, "st", __ret); break; \
125 case 8: __arc_put_user_one_64(*(k), u, __ret); break; \
130 #define __arc_put_user_one(src, dst, op, ret) \
131 __asm__ __volatile__( \
132 "1: "op" %1,[%2]\n" \
134 " .section .fixup, \"ax\"\n" \
139 " .section __ex_table, \"a\"\n" \
145 : "r" (src), "r" (dst), "ir" (-EFAULT))
147 #define __arc_put_user_one_64(src, dst, ret) \
148 __asm__ __volatile__( \
150 "4: st %R1,[%2, 4]\n" \
152 " .section .fixup, \"ax\"\n" \
157 " .section __ex_table, \"a\"\n" \
164 : "r" (src), "r" (dst), "ir" (-EFAULT))
167 static inline unsigned long
168 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
172 unsigned long tmp1
, tmp2
, tmp3
, tmp4
;
173 unsigned long orig_n
= n
;
179 if (((unsigned long)to
& 0x3) || ((unsigned long)from
& 0x3)) {
183 __asm__
__volatile__ (
184 " mov.f lp_count, %0 \n"
186 "1: ldb.ab %1, [%3, 1] \n"
187 " stb.ab %1, [%2, 1] \n"
190 " .section .fixup, \"ax\" \n"
194 " .section __ex_table, \"a\" \n"
201 * Note as an '&' earlyclobber operand to make sure the
202 * temporary register inside the loop is not the same as
205 "=&r" (tmp
), "+r" (to
), "+r" (from
)
207 : "lp_count", "memory");
213 * Hand-crafted constant propagation to reduce code sz of the
214 * laddered copy 16x,8,4,2,1
216 if (__builtin_constant_p(orig_n
)) {
220 orig_n
= orig_n
% 16;
222 __asm__
__volatile__(
223 " lsr lp_count, %7,4 \n"
225 "1: ld.ab %3, [%2, 4] \n"
226 "11: ld.ab %4, [%2, 4] \n"
227 "12: ld.ab %5, [%2, 4] \n"
228 "13: ld.ab %6, [%2, 4] \n"
229 " st.ab %3, [%1, 4] \n"
230 " st.ab %4, [%1, 4] \n"
231 " st.ab %5, [%1, 4] \n"
232 " st.ab %6, [%1, 4] \n"
235 " .section .fixup, \"ax\" \n"
239 " .section __ex_table, \"a\" \n"
246 : "+r" (res
), "+r"(to
), "+r"(from
),
247 "=r"(tmp1
), "=r"(tmp2
), "=r"(tmp3
), "=r"(tmp4
)
249 : "lp_count", "memory");
254 __asm__
__volatile__(
255 "14: ld.ab %3, [%2,4] \n"
256 "15: ld.ab %4, [%2,4] \n"
257 " st.ab %3, [%1,4] \n"
258 " st.ab %4, [%1,4] \n"
261 " .section .fixup, \"ax\" \n"
265 " .section __ex_table, \"a\" \n"
270 : "+r" (res
), "+r"(to
), "+r"(from
),
271 "=r"(tmp1
), "=r"(tmp2
)
278 __asm__
__volatile__(
279 "16: ld.ab %3, [%2,4] \n"
280 " st.ab %3, [%1,4] \n"
283 " .section .fixup, \"ax\" \n"
287 " .section __ex_table, \"a\" \n"
291 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
298 __asm__
__volatile__(
299 "17: ldw.ab %3, [%2,2] \n"
300 " stw.ab %3, [%1,2] \n"
303 " .section .fixup, \"ax\" \n"
307 " .section __ex_table, \"a\" \n"
311 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
316 __asm__
__volatile__(
317 "18: ldb.ab %3, [%2,2] \n"
318 " stb.ab %3, [%1,2] \n"
321 " .section .fixup, \"ax\" \n"
325 " .section __ex_table, \"a\" \n"
329 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
333 } else { /* n is NOT constant, so laddered copy of 16x,8,4,2,1 */
335 __asm__
__volatile__(
337 " lsr.f lp_count, %3,4 \n" /* 16x bytes */
339 "1: ld.ab %5, [%2, 4] \n"
340 "11: ld.ab %6, [%2, 4] \n"
341 "12: ld.ab %7, [%2, 4] \n"
342 "13: ld.ab %8, [%2, 4] \n"
343 " st.ab %5, [%1, 4] \n"
344 " st.ab %6, [%1, 4] \n"
345 " st.ab %7, [%1, 4] \n"
346 " st.ab %8, [%1, 4] \n"
348 "3: and.f %3,%3,0xf \n" /* stragglers */
350 " bbit0 %3,3,31f \n" /* 8 bytes left */
351 "14: ld.ab %5, [%2,4] \n"
352 "15: ld.ab %6, [%2,4] \n"
353 " st.ab %5, [%1,4] \n"
354 " st.ab %6, [%1,4] \n"
356 "31: bbit0 %3,2,32f \n" /* 4 bytes left */
357 "16: ld.ab %5, [%2,4] \n"
358 " st.ab %5, [%1,4] \n"
360 "32: bbit0 %3,1,33f \n" /* 2 bytes left */
361 "17: ldw.ab %5, [%2,2] \n"
362 " stw.ab %5, [%1,2] \n"
364 "33: bbit0 %3,0,34f \n"
365 "18: ldb.ab %5, [%2,1] \n" /* 1 byte left */
366 " stb.ab %5, [%1,1] \n"
369 " .section .fixup, \"ax\" \n"
373 " .section __ex_table, \"a\" \n"
385 : "=r" (res
), "+r"(to
), "+r"(from
), "+r"(n
), "=r"(val
),
386 "=r"(tmp1
), "=r"(tmp2
), "=r"(tmp3
), "=r"(tmp4
)
388 : "lp_count", "memory");
394 static inline unsigned long
395 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
399 unsigned long tmp1
, tmp2
, tmp3
, tmp4
;
400 unsigned long orig_n
= n
;
406 if (((unsigned long)to
& 0x3) || ((unsigned long)from
& 0x3)) {
410 __asm__
__volatile__(
411 " mov.f lp_count, %0 \n"
413 " ldb.ab %1, [%3, 1] \n"
414 "1: stb.ab %1, [%2, 1] \n"
417 " .section .fixup, \"ax\" \n"
421 " .section __ex_table, \"a\" \n"
427 /* Note as an '&' earlyclobber operand to make sure the
428 * temporary register inside the loop is not the same as
431 "=&r" (tmp
), "+r" (to
), "+r" (from
)
433 : "lp_count", "memory");
438 if (__builtin_constant_p(orig_n
)) {
442 orig_n
= orig_n
% 16;
444 __asm__
__volatile__(
445 " lsr lp_count, %7,4 \n"
447 " ld.ab %3, [%2, 4] \n"
448 " ld.ab %4, [%2, 4] \n"
449 " ld.ab %5, [%2, 4] \n"
450 " ld.ab %6, [%2, 4] \n"
451 "1: st.ab %3, [%1, 4] \n"
452 "11: st.ab %4, [%1, 4] \n"
453 "12: st.ab %5, [%1, 4] \n"
454 "13: st.ab %6, [%1, 4] \n"
457 " .section .fixup, \"ax\" \n"
461 " .section __ex_table, \"a\" \n"
468 : "+r" (res
), "+r"(to
), "+r"(from
),
469 "=r"(tmp1
), "=r"(tmp2
), "=r"(tmp3
), "=r"(tmp4
)
471 : "lp_count", "memory");
476 __asm__
__volatile__(
477 " ld.ab %3, [%2,4] \n"
478 " ld.ab %4, [%2,4] \n"
479 "14: st.ab %3, [%1,4] \n"
480 "15: st.ab %4, [%1,4] \n"
483 " .section .fixup, \"ax\" \n"
487 " .section __ex_table, \"a\" \n"
492 : "+r" (res
), "+r"(to
), "+r"(from
),
493 "=r"(tmp1
), "=r"(tmp2
)
500 __asm__
__volatile__(
501 " ld.ab %3, [%2,4] \n"
502 "16: st.ab %3, [%1,4] \n"
505 " .section .fixup, \"ax\" \n"
509 " .section __ex_table, \"a\" \n"
513 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
520 __asm__
__volatile__(
521 " ldw.ab %3, [%2,2] \n"
522 "17: stw.ab %3, [%1,2] \n"
525 " .section .fixup, \"ax\" \n"
529 " .section __ex_table, \"a\" \n"
533 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
538 __asm__
__volatile__(
539 " ldb.ab %3, [%2,1] \n"
540 "18: stb.ab %3, [%1,1] \n"
543 " .section .fixup, \"ax\" \n"
547 " .section __ex_table, \"a\" \n"
551 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
555 } else { /* n is NOT constant, so laddered copy of 16x,8,4,2,1 */
557 __asm__
__volatile__(
559 " lsr.f lp_count, %3,4 \n" /* 16x bytes */
561 " ld.ab %5, [%2, 4] \n"
562 " ld.ab %6, [%2, 4] \n"
563 " ld.ab %7, [%2, 4] \n"
564 " ld.ab %8, [%2, 4] \n"
565 "1: st.ab %5, [%1, 4] \n"
566 "11: st.ab %6, [%1, 4] \n"
567 "12: st.ab %7, [%1, 4] \n"
568 "13: st.ab %8, [%1, 4] \n"
570 "3: and.f %3,%3,0xf \n" /* stragglers */
572 " bbit0 %3,3,31f \n" /* 8 bytes left */
573 " ld.ab %5, [%2,4] \n"
574 " ld.ab %6, [%2,4] \n"
575 "14: st.ab %5, [%1,4] \n"
576 "15: st.ab %6, [%1,4] \n"
577 " sub.f %0, %0, 8 \n"
578 "31: bbit0 %3,2,32f \n" /* 4 bytes left */
579 " ld.ab %5, [%2,4] \n"
580 "16: st.ab %5, [%1,4] \n"
581 " sub.f %0, %0, 4 \n"
582 "32: bbit0 %3,1,33f \n" /* 2 bytes left */
583 " ldw.ab %5, [%2,2] \n"
584 "17: stw.ab %5, [%1,2] \n"
585 " sub.f %0, %0, 2 \n"
586 "33: bbit0 %3,0,34f \n"
587 " ldb.ab %5, [%2,1] \n" /* 1 byte left */
588 "18: stb.ab %5, [%1,1] \n"
589 " sub.f %0, %0, 1 \n"
591 " .section .fixup, \"ax\" \n"
595 " .section __ex_table, \"a\" \n"
607 : "=r" (res
), "+r"(to
), "+r"(from
), "+r"(n
), "=r"(val
),
608 "=r"(tmp1
), "=r"(tmp2
), "=r"(tmp3
), "=r"(tmp4
)
610 : "lp_count", "memory");
616 static inline unsigned long __arc_clear_user(void __user
*to
, unsigned long n
)
619 unsigned char *d_char
= to
;
621 __asm__
__volatile__(
622 " bbit0 %0, 0, 1f \n"
623 "75: stb.ab %2, [%0,1] \n"
625 "1: bbit0 %0, 1, 2f \n"
626 "76: stw.ab %2, [%0,2] \n"
628 "2: asr.f lp_count, %1, 2 \n"
630 "77: st.ab %2, [%0,4] \n"
632 "3: bbit0 %1, 1, 4f \n"
633 "78: stw.ab %2, [%0,2] \n"
635 "4: bbit0 %1, 0, 5f \n"
636 "79: stb.ab %2, [%0,1] \n"
639 " .section .fixup, \"ax\" \n"
643 " .section __ex_table, \"a\" \n"
651 : "+r"(d_char
), "+r"(res
)
653 : "lp_count", "memory");
659 __arc_strncpy_from_user(char *dst
, const char __user
*src
, long count
)
667 __asm__
__volatile__(
668 " mov lp_count, %5 \n"
670 "1: ldb.ab %3, [%2, 1] \n"
671 " breq.d %3, 0, 3f \n"
672 " stb.ab %3, [%1, 1] \n"
673 " add %0, %0, 1 # Num of NON NULL bytes copied \n"
675 " .section .fixup, \"ax\" \n"
677 "4: mov %0, %4 # sets @res as -EFAULT \n"
680 " .section __ex_table, \"a\" \n"
684 : "+r"(res
), "+r"(dst
), "+r"(src
), "=r"(val
)
685 : "g"(-EFAULT
), "r"(count
)
686 : "lp_count", "memory");
691 static inline long __arc_strnlen_user(const char __user
*s
, long n
)
696 __asm__
__volatile__(
698 "1: ldb.ab %3, [%0, 1] \n"
699 " breq.d %3, 0, 2f \n"
700 " sub.f %2, %2, 1 \n"
703 "2: sub %0, %1, %2 \n"
705 " .section .fixup, \"ax\" \n"
710 " .section __ex_table, \"a\" \n"
714 : "=r"(res
), "=r"(tmp1
), "=r"(cnt
), "=r"(val
)
721 #ifndef CONFIG_CC_OPTIMIZE_FOR_SIZE
723 #define INLINE_COPY_TO_USER
724 #define INLINE_COPY_FROM_USER
726 #define __clear_user(d, n) __arc_clear_user(d, n)
727 #define __strncpy_from_user(d, s, n) __arc_strncpy_from_user(d, s, n)
728 #define __strnlen_user(s, n) __arc_strnlen_user(s, n)
730 extern unsigned long arc_clear_user_noinline(void __user
*to
,
732 extern long arc_strncpy_from_user_noinline (char *dst
, const char __user
*src
,
734 extern long arc_strnlen_user_noinline(const char __user
*src
, long n
);
736 #define __clear_user(d, n) arc_clear_user_noinline(d, n)
737 #define __strncpy_from_user(d, s, n) arc_strncpy_from_user_noinline(d, s, n)
738 #define __strnlen_user(s, n) arc_strnlen_user_noinline(s, n)
742 #include <asm/segment.h>
743 #include <asm-generic/uaccess.h>