2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 * -__clear_user( ) called multiple times during elf load was byte loop
10 * converted to do as much word clear as possible.
13 * -Hand crafted constant propagation for "constant" copy sizes
14 * -stock kernel shrunk by 33K at -O3
17 * -Added option to (UN)inline copy_(to|from)_user to reduce code sz
18 * -kernel shrunk by 200K even at -O3 (gcc 4.2.1)
19 * -Enabled when doing -Os
21 * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
24 #ifndef _ASM_ARC_UACCESS_H
25 #define _ASM_ARC_UACCESS_H
27 #include <linux/sched.h>
28 #include <asm/errno.h>
29 #include <linux/string.h> /* for generic string functions */
32 #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
35 * Algorithmically, for __user_ok() we want do:
36 * (start < TASK_SIZE) && (start+len < TASK_SIZE)
37 * where TASK_SIZE could either be retrieved from thread_info->addr_limit or
38 * emitted directly in code.
40 * This can however be rewritten as follows:
41 * (len <= TASK_SIZE) && (start+len < TASK_SIZE)
43 * Because it essentially checks if buffer end is within limit and @len is
44 * non-ngeative, which implies that buffer start will be within limit too.
46 * The reason for rewriting being, for majority of cases, @len is generally
47 * compile time constant, causing first sub-expression to be compile time
50 * The second part would generate weird large LIMMs e.g. (0x6000_0000 - 0x10),
51 * so we check for TASK_SIZE using get_fs() since the addr_limit load from mem
52 * would already have been done at this call site for __kernel_ok()
55 #define __user_ok(addr, sz) (((sz) <= TASK_SIZE) && \
56 ((addr) <= (get_fs() - (sz))))
57 #define __access_ok(addr, sz) (unlikely(__kernel_ok) || \
58 likely(__user_ok((addr), (sz))))
60 /*********** Single byte/hword/word copies ******************/
62 #define __get_user_fn(sz, u, k) \
64 long __ret = 0; /* success by default */ \
66 case 1: __arc_get_user_one(*(k), u, "ldb", __ret); break; \
67 case 2: __arc_get_user_one(*(k), u, "ldw", __ret); break; \
68 case 4: __arc_get_user_one(*(k), u, "ld", __ret); break; \
69 case 8: __arc_get_user_one_64(*(k), u, __ret); break; \
75 * Returns 0 on success, -EFAULT if not.
76 * @ret already contains 0 - given that errors will be less likely
77 * (hence +r asm constraint below).
78 * In case of error, fixup code will make it -EFAULT
80 #define __arc_get_user_one(dst, src, op, ret) \
81 __asm__ __volatile__( \
84 " .section .fixup, \"ax\"\n" \
86 "3: # return -EFAULT\n" \
88 " # zero out dst ptr\n" \
92 " .section __ex_table, \"a\"\n" \
97 : "+r" (ret), "=r" (dst) \
98 : "r" (src), "ir" (-EFAULT))
100 #define __arc_get_user_one_64(dst, src, ret) \
101 __asm__ __volatile__( \
103 "4: ld %R1,[%2, 4]\n" \
105 " .section .fixup, \"ax\"\n" \
107 "3: # return -EFAULT\n" \
109 " # zero out dst ptr\n" \
114 " .section __ex_table, \"a\"\n" \
120 : "+r" (ret), "=r" (dst) \
121 : "r" (src), "ir" (-EFAULT))
123 #define __put_user_fn(sz, u, k) \
125 long __ret = 0; /* success by default */ \
127 case 1: __arc_put_user_one(*(k), u, "stb", __ret); break; \
128 case 2: __arc_put_user_one(*(k), u, "stw", __ret); break; \
129 case 4: __arc_put_user_one(*(k), u, "st", __ret); break; \
130 case 8: __arc_put_user_one_64(*(k), u, __ret); break; \
135 #define __arc_put_user_one(src, dst, op, ret) \
136 __asm__ __volatile__( \
137 "1: "op" %1,[%2]\n" \
139 " .section .fixup, \"ax\"\n" \
144 " .section __ex_table, \"a\"\n" \
150 : "r" (src), "r" (dst), "ir" (-EFAULT))
152 #define __arc_put_user_one_64(src, dst, ret) \
153 __asm__ __volatile__( \
155 "4: st %R1,[%2, 4]\n" \
157 " .section .fixup, \"ax\"\n" \
162 " .section __ex_table, \"a\"\n" \
169 : "r" (src), "r" (dst), "ir" (-EFAULT))
172 static inline unsigned long
173 __arc_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
177 unsigned long tmp1
, tmp2
, tmp3
, tmp4
;
178 unsigned long orig_n
= n
;
184 if (((unsigned long)to
& 0x3) || ((unsigned long)from
& 0x3)) {
188 __asm__
__volatile__ (
189 " mov.f lp_count, %0 \n"
191 "1: ldb.ab %1, [%3, 1] \n"
192 " stb.ab %1, [%2, 1] \n"
195 " .section .fixup, \"ax\" \n"
199 " .section __ex_table, \"a\" \n"
206 * Note as an '&' earlyclobber operand to make sure the
207 * temporary register inside the loop is not the same as
210 "=&r" (tmp
), "+r" (to
), "+r" (from
)
212 : "lp_count", "lp_start", "lp_end", "memory");
218 * Hand-crafted constant propagation to reduce code sz of the
219 * laddered copy 16x,8,4,2,1
221 if (__builtin_constant_p(orig_n
)) {
225 orig_n
= orig_n
% 16;
227 __asm__
__volatile__(
228 " lsr lp_count, %7,4 \n"
230 "1: ld.ab %3, [%2, 4] \n"
231 "11: ld.ab %4, [%2, 4] \n"
232 "12: ld.ab %5, [%2, 4] \n"
233 "13: ld.ab %6, [%2, 4] \n"
234 " st.ab %3, [%1, 4] \n"
235 " st.ab %4, [%1, 4] \n"
236 " st.ab %5, [%1, 4] \n"
237 " st.ab %6, [%1, 4] \n"
240 " .section .fixup, \"ax\" \n"
244 " .section __ex_table, \"a\" \n"
251 : "+r" (res
), "+r"(to
), "+r"(from
),
252 "=r"(tmp1
), "=r"(tmp2
), "=r"(tmp3
), "=r"(tmp4
)
254 : "lp_count", "memory");
259 __asm__
__volatile__(
260 "14: ld.ab %3, [%2,4] \n"
261 "15: ld.ab %4, [%2,4] \n"
262 " st.ab %3, [%1,4] \n"
263 " st.ab %4, [%1,4] \n"
266 " .section .fixup, \"ax\" \n"
270 " .section __ex_table, \"a\" \n"
275 : "+r" (res
), "+r"(to
), "+r"(from
),
276 "=r"(tmp1
), "=r"(tmp2
)
283 __asm__
__volatile__(
284 "16: ld.ab %3, [%2,4] \n"
285 " st.ab %3, [%1,4] \n"
288 " .section .fixup, \"ax\" \n"
292 " .section __ex_table, \"a\" \n"
296 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
303 __asm__
__volatile__(
304 "17: ldw.ab %3, [%2,2] \n"
305 " stw.ab %3, [%1,2] \n"
308 " .section .fixup, \"ax\" \n"
312 " .section __ex_table, \"a\" \n"
316 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
321 __asm__
__volatile__(
322 "18: ldb.ab %3, [%2,2] \n"
323 " stb.ab %3, [%1,2] \n"
326 " .section .fixup, \"ax\" \n"
330 " .section __ex_table, \"a\" \n"
334 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
338 } else { /* n is NOT constant, so laddered copy of 16x,8,4,2,1 */
340 __asm__
__volatile__(
342 " lsr.f lp_count, %3,4 \n" /* 16x bytes */
344 "1: ld.ab %5, [%2, 4] \n"
345 "11: ld.ab %6, [%2, 4] \n"
346 "12: ld.ab %7, [%2, 4] \n"
347 "13: ld.ab %8, [%2, 4] \n"
348 " st.ab %5, [%1, 4] \n"
349 " st.ab %6, [%1, 4] \n"
350 " st.ab %7, [%1, 4] \n"
351 " st.ab %8, [%1, 4] \n"
353 "3: and.f %3,%3,0xf \n" /* stragglers */
355 " bbit0 %3,3,31f \n" /* 8 bytes left */
356 "14: ld.ab %5, [%2,4] \n"
357 "15: ld.ab %6, [%2,4] \n"
358 " st.ab %5, [%1,4] \n"
359 " st.ab %6, [%1,4] \n"
361 "31: bbit0 %3,2,32f \n" /* 4 bytes left */
362 "16: ld.ab %5, [%2,4] \n"
363 " st.ab %5, [%1,4] \n"
365 "32: bbit0 %3,1,33f \n" /* 2 bytes left */
366 "17: ldw.ab %5, [%2,2] \n"
367 " stw.ab %5, [%1,2] \n"
369 "33: bbit0 %3,0,34f \n"
370 "18: ldb.ab %5, [%2,1] \n" /* 1 byte left */
371 " stb.ab %5, [%1,1] \n"
374 " .section .fixup, \"ax\" \n"
378 " .section __ex_table, \"a\" \n"
390 : "=r" (res
), "+r"(to
), "+r"(from
), "+r"(n
), "=r"(val
),
391 "=r"(tmp1
), "=r"(tmp2
), "=r"(tmp3
), "=r"(tmp4
)
393 : "lp_count", "memory");
399 extern unsigned long slowpath_copy_to_user(void __user
*to
, const void *from
,
402 static inline unsigned long
403 __arc_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
407 unsigned long tmp1
, tmp2
, tmp3
, tmp4
;
408 unsigned long orig_n
= n
;
414 if (((unsigned long)to
& 0x3) || ((unsigned long)from
& 0x3)) {
418 __asm__
__volatile__(
419 " mov.f lp_count, %0 \n"
421 " ldb.ab %1, [%3, 1] \n"
422 "1: stb.ab %1, [%2, 1] \n"
425 " .section .fixup, \"ax\" \n"
429 " .section __ex_table, \"a\" \n"
435 /* Note as an '&' earlyclobber operand to make sure the
436 * temporary register inside the loop is not the same as
439 "=&r" (tmp
), "+r" (to
), "+r" (from
)
441 : "lp_count", "lp_start", "lp_end", "memory");
446 if (__builtin_constant_p(orig_n
)) {
450 orig_n
= orig_n
% 16;
452 __asm__
__volatile__(
453 " lsr lp_count, %7,4 \n"
455 " ld.ab %3, [%2, 4] \n"
456 " ld.ab %4, [%2, 4] \n"
457 " ld.ab %5, [%2, 4] \n"
458 " ld.ab %6, [%2, 4] \n"
459 "1: st.ab %3, [%1, 4] \n"
460 "11: st.ab %4, [%1, 4] \n"
461 "12: st.ab %5, [%1, 4] \n"
462 "13: st.ab %6, [%1, 4] \n"
465 " .section .fixup, \"ax\" \n"
469 " .section __ex_table, \"a\" \n"
476 : "+r" (res
), "+r"(to
), "+r"(from
),
477 "=r"(tmp1
), "=r"(tmp2
), "=r"(tmp3
), "=r"(tmp4
)
479 : "lp_count", "memory");
484 __asm__
__volatile__(
485 " ld.ab %3, [%2,4] \n"
486 " ld.ab %4, [%2,4] \n"
487 "14: st.ab %3, [%1,4] \n"
488 "15: st.ab %4, [%1,4] \n"
491 " .section .fixup, \"ax\" \n"
495 " .section __ex_table, \"a\" \n"
500 : "+r" (res
), "+r"(to
), "+r"(from
),
501 "=r"(tmp1
), "=r"(tmp2
)
508 __asm__
__volatile__(
509 " ld.ab %3, [%2,4] \n"
510 "16: st.ab %3, [%1,4] \n"
513 " .section .fixup, \"ax\" \n"
517 " .section __ex_table, \"a\" \n"
521 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
528 __asm__
__volatile__(
529 " ldw.ab %3, [%2,2] \n"
530 "17: stw.ab %3, [%1,2] \n"
533 " .section .fixup, \"ax\" \n"
537 " .section __ex_table, \"a\" \n"
541 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
546 __asm__
__volatile__(
547 " ldb.ab %3, [%2,1] \n"
548 "18: stb.ab %3, [%1,1] \n"
551 " .section .fixup, \"ax\" \n"
555 " .section __ex_table, \"a\" \n"
559 : "+r" (res
), "+r"(to
), "+r"(from
), "=r"(tmp1
)
563 } else { /* n is NOT constant, so laddered copy of 16x,8,4,2,1 */
565 __asm__
__volatile__(
567 " lsr.f lp_count, %3,4 \n" /* 16x bytes */
569 " ld.ab %5, [%2, 4] \n"
570 " ld.ab %6, [%2, 4] \n"
571 " ld.ab %7, [%2, 4] \n"
572 " ld.ab %8, [%2, 4] \n"
573 "1: st.ab %5, [%1, 4] \n"
574 "11: st.ab %6, [%1, 4] \n"
575 "12: st.ab %7, [%1, 4] \n"
576 "13: st.ab %8, [%1, 4] \n"
578 "3: and.f %3,%3,0xf \n" /* stragglers */
580 " bbit0 %3,3,31f \n" /* 8 bytes left */
581 " ld.ab %5, [%2,4] \n"
582 " ld.ab %6, [%2,4] \n"
583 "14: st.ab %5, [%1,4] \n"
584 "15: st.ab %6, [%1,4] \n"
585 " sub.f %0, %0, 8 \n"
586 "31: bbit0 %3,2,32f \n" /* 4 bytes left */
587 " ld.ab %5, [%2,4] \n"
588 "16: st.ab %5, [%1,4] \n"
589 " sub.f %0, %0, 4 \n"
590 "32: bbit0 %3,1,33f \n" /* 2 bytes left */
591 " ldw.ab %5, [%2,2] \n"
592 "17: stw.ab %5, [%1,2] \n"
593 " sub.f %0, %0, 2 \n"
594 "33: bbit0 %3,0,34f \n"
595 " ldb.ab %5, [%2,1] \n" /* 1 byte left */
596 "18: stb.ab %5, [%1,1] \n"
597 " sub.f %0, %0, 1 \n"
599 " .section .fixup, \"ax\" \n"
603 " .section __ex_table, \"a\" \n"
615 : "=r" (res
), "+r"(to
), "+r"(from
), "+r"(n
), "=r"(val
),
616 "=r"(tmp1
), "=r"(tmp2
), "=r"(tmp3
), "=r"(tmp4
)
618 : "lp_count", "memory");
624 static inline unsigned long __arc_clear_user(void __user
*to
, unsigned long n
)
627 unsigned char *d_char
= to
;
629 __asm__
__volatile__(
630 " bbit0 %0, 0, 1f \n"
631 "75: stb.ab %2, [%0,1] \n"
633 "1: bbit0 %0, 1, 2f \n"
634 "76: stw.ab %2, [%0,2] \n"
636 "2: asr.f lp_count, %1, 2 \n"
638 "77: st.ab %2, [%0,4] \n"
640 "3: bbit0 %1, 1, 4f \n"
641 "78: stw.ab %2, [%0,2] \n"
643 "4: bbit0 %1, 0, 5f \n"
644 "79: stb.ab %2, [%0,1] \n"
647 " .section .fixup, \"ax\" \n"
651 " .section __ex_table, \"a\" \n"
659 : "+r"(d_char
), "+r"(res
)
661 : "lp_count", "lp_start", "lp_end", "memory");
667 __arc_strncpy_from_user(char *dst
, const char __user
*src
, long count
)
675 __asm__
__volatile__(
676 " mov lp_count, %5 \n"
678 "1: ldb.ab %3, [%2, 1] \n"
679 " breq.d %3, 0, 3f \n"
680 " stb.ab %3, [%1, 1] \n"
681 " add %0, %0, 1 # Num of NON NULL bytes copied \n"
683 " .section .fixup, \"ax\" \n"
685 "4: mov %0, %4 # sets @res as -EFAULT \n"
688 " .section __ex_table, \"a\" \n"
692 : "+r"(res
), "+r"(dst
), "+r"(src
), "=r"(val
)
693 : "g"(-EFAULT
), "r"(count
)
694 : "lp_count", "lp_start", "lp_end", "memory");
699 static inline long __arc_strnlen_user(const char __user
*s
, long n
)
704 __asm__
__volatile__(
706 "1: ldb.ab %3, [%0, 1] \n"
707 " breq.d %3, 0, 2f \n"
708 " sub.f %2, %2, 1 \n"
711 "2: sub %0, %1, %2 \n"
713 " .section .fixup, \"ax\" \n"
718 " .section __ex_table, \"a\" \n"
722 : "=r"(res
), "=r"(tmp1
), "=r"(cnt
), "=r"(val
)
729 #ifndef CONFIG_CC_OPTIMIZE_FOR_SIZE
730 #define __copy_from_user(t, f, n) __arc_copy_from_user(t, f, n)
731 #define __copy_to_user(t, f, n) __arc_copy_to_user(t, f, n)
732 #define __clear_user(d, n) __arc_clear_user(d, n)
733 #define __strncpy_from_user(d, s, n) __arc_strncpy_from_user(d, s, n)
734 #define __strnlen_user(s, n) __arc_strnlen_user(s, n)
736 extern long arc_copy_from_user_noinline(void *to
, const void __user
* from
,
738 extern long arc_copy_to_user_noinline(void __user
*to
, const void *from
,
740 extern unsigned long arc_clear_user_noinline(void __user
*to
,
742 extern long arc_strncpy_from_user_noinline (char *dst
, const char __user
*src
,
744 extern long arc_strnlen_user_noinline(const char __user
*src
, long n
);
746 #define __copy_from_user(t, f, n) arc_copy_from_user_noinline(t, f, n)
747 #define __copy_to_user(t, f, n) arc_copy_to_user_noinline(t, f, n)
748 #define __clear_user(d, n) arc_clear_user_noinline(d, n)
749 #define __strncpy_from_user(d, s, n) arc_strncpy_from_user_noinline(d, s, n)
750 #define __strnlen_user(s, n) arc_strnlen_user_noinline(s, n)
754 #include <asm-generic/uaccess.h>
756 extern int fixup_exception(struct pt_regs
*regs
);