2 * arch/xtensa/lib/usercopy.S
4 * Copy to/from user space (derived from arch/xtensa/lib/hal/memcopy.S)
6 * DO NOT COMBINE this function with <arch/xtensa/lib/hal/memcopy.S>.
7 * It needs to remain separate and distinct. The hal files are part
8 * of the Xtensa link-time HAL, and those files may differ per
9 * processor configuration. Patching the kernel for another
10 * processor configuration includes replacing the hal files, and we
11 * could lose the special functionality for accessing user-space
12 * memory during such a patch. We sacrifice a little code space here
13 * in favor to simplify code maintenance.
15 * This file is subject to the terms and conditions of the GNU General
16 * Public License. See the file "COPYING" in the main directory of
17 * this archive for more details.
19 * Copyright (C) 2002 Tensilica Inc.
24 * size_t __xtensa_copy_user (void *dst, const void *src, size_t len);
26 * The returned value is the number of bytes not copied. Implies zero
29 * The general case algorithm is as follows:
30 * If the destination and source are both aligned,
31 * do 16B chunks with a loop, and then finish up with
32 * 8B, 4B, 2B, and 1B copies conditional on the length.
33 * If destination is aligned and source unaligned,
34 * do the same, but use SRC to align the source data.
35 * If destination is unaligned, align it by conditionally
36 * copying 1B and 2B and then retest.
37 * This code tries to use fall-through braches for the common
38 * case of aligned destinations (except for the branches to
39 * the alignment label).
53 * a11/ original length
56 #include <linux/linkage.h>
57 #include <asm/asmmacro.h>
61 ENTRY(__xtensa_copy_user)
63 #if !XCHAL_HAVE_LOOPS && defined(__XTENSA_CALL0_ABI__)
69 # a2/ dst, a3/ src, a4/ len
70 mov a5, a2 # copy dst so that a2 is return value
71 mov a11, a4 # preserve original len for error case
73 bbsi.l a2, 0, .Ldst1mod2 # if dst is 1 mod 2
74 bbsi.l a2, 1, .Ldst2mod4 # if dst is 2 mod 4
75 .Ldstaligned: # return here from .Ldstunaligned when dst is aligned
76 srli a7, a4, 4 # number of loop iterations with 16B
78 movi a8, 3 # if source is also aligned,
79 bnone a3, a8, .Laligned # then use word copy
80 __ssa8 a3 # set shift amount from byte offset
81 bnez a4, .Lsrcunaligned
82 movi a2, 0 # return success for len==0
86 * Destination is unaligned
89 .Ldst1mod2: # dst is only byte aligned
90 bltui a4, 7, .Lbytecopy # do short copies byte by byte
93 EX(10f) l8ui a6, a3, 0
98 bbci.l a5, 1, .Ldstaligned # if dst is now aligned, then
99 # return to main algorithm
100 .Ldst2mod4: # dst 16-bit aligned
102 bltui a4, 6, .Lbytecopy # do short copies byte by byte
103 EX(10f) l8ui a6, a3, 0
104 EX(10f) l8ui a7, a3, 1
106 EX(10f) s8i a6, a5, 0
107 EX(10f) s8i a7, a5, 1
110 j .Ldstaligned # dst is now aligned, return to main algorithm
116 .byte 0 # 1 mod 4 alignment for LOOPNEZ
117 # (0 mod 4 alignment for LBEG)
120 loopnez a4, .Lbytecopydone
121 #else /* !XCHAL_HAVE_LOOPS */
122 beqz a4, .Lbytecopydone
123 add a7, a3, a4 # a7 = end address for source
124 #endif /* !XCHAL_HAVE_LOOPS */
126 EX(10f) l8ui a6, a3, 0
128 EX(10f) s8i a6, a5, 0
130 #if !XCHAL_HAVE_LOOPS
131 blt a3, a7, .Lnextbyte
132 #endif /* !XCHAL_HAVE_LOOPS */
134 movi a2, 0 # return success for len bytes copied
138 * Destination and source are word-aligned.
140 # copy 16 bytes per iteration for word-aligned dst and word-aligned src
141 .align 4 # 1 mod 4 alignment for LOOPNEZ
142 .byte 0 # (0 mod 4 alignment for LBEG)
145 loopnez a7, .Loop1done
146 #else /* !XCHAL_HAVE_LOOPS */
149 add a8, a8, a3 # a8 = end of last 16B source chunk
150 #endif /* !XCHAL_HAVE_LOOPS */
152 EX(10f) l32i a6, a3, 0
153 EX(10f) l32i a7, a3, 4
154 EX(10f) s32i a6, a5, 0
155 EX(10f) l32i a6, a3, 8
156 EX(10f) s32i a7, a5, 4
157 EX(10f) l32i a7, a3, 12
158 EX(10f) s32i a6, a5, 8
160 EX(10f) s32i a7, a5, 12
162 #if !XCHAL_HAVE_LOOPS
164 #endif /* !XCHAL_HAVE_LOOPS */
168 EX(10f) l32i a6, a3, 0
169 EX(10f) l32i a7, a3, 4
171 EX(10f) s32i a6, a5, 0
172 EX(10f) s32i a7, a5, 4
177 EX(10f) l32i a6, a3, 0
179 EX(10f) s32i a6, a5, 0
184 EX(10f) l16ui a6, a3, 0
186 EX(10f) s16i a6, a5, 0
191 EX(10f) l8ui a6, a3, 0
192 EX(10f) s8i a6, a5, 0
194 movi a2, 0 # return success for len bytes copied
198 * Destination is aligned, Source is unaligned
202 .byte 0 # 1 mod 4 alignement for LOOPNEZ
203 # (0 mod 4 alignment for LBEG)
205 # copy 16 bytes per iteration for word-aligned dst and unaligned src
206 and a10, a3, a8 # save unalignment offset for below
207 sub a3, a3, a10 # align a3 (to avoid sim warnings only; not needed for hardware)
208 EX(10f) l32i a6, a3, 0 # load first word
210 loopnez a7, .Loop2done
211 #else /* !XCHAL_HAVE_LOOPS */
213 #if defined(__XTENSA_CALL0_ABI__)
216 add a10, a10, a3 # a10 = end of last 16B source chunk
219 add a12, a12, a3 # a12 = end of last 16B source chunk
221 #endif /* !XCHAL_HAVE_LOOPS */
223 EX(10f) l32i a7, a3, 4
224 EX(10f) l32i a8, a3, 8
226 EX(10f) s32i a6, a5, 0
227 EX(10f) l32i a9, a3, 12
229 EX(10f) s32i a7, a5, 4
230 EX(10f) l32i a6, a3, 16
232 EX(10f) s32i a8, a5, 8
235 EX(10f) s32i a9, a5, 12
237 #if !XCHAL_HAVE_LOOPS
238 #if defined(__XTENSA_CALL0_ABI__)
244 #endif /* !XCHAL_HAVE_LOOPS */
248 EX(10f) l32i a7, a3, 4
249 EX(10f) l32i a8, a3, 8
251 EX(10f) s32i a6, a5, 0
254 EX(10f) s32i a7, a5, 4
260 EX(10f) l32i a7, a3, 4
263 EX(10f) s32i a6, a5, 0
267 add a3, a3, a10 # readjust a3 with correct misalignment
270 EX(10f) l8ui a6, a3, 0
271 EX(10f) l8ui a7, a3, 1
273 EX(10f) s8i a6, a5, 0
274 EX(10f) s8i a7, a5, 1
279 EX(10f) l8ui a6, a3, 0
280 EX(10f) s8i a6, a5, 0
282 movi a2, 0 # return success for len bytes copied
285 ENDPROC(__xtensa_copy_user)
286 EXPORT_SYMBOL(__xtensa_copy_user)
288 .section .fixup, "ax"
291 /* a2 = original dst; a5 = current dst; a11= original len
292 * bytes_copied = a5 - a2
293 * retval = bytes_not_copied = original len - bytes_copied
294 * retval = a11 - (a5 - a2)
299 sub a2, a5, a2 /* a2 <-- bytes copied */
300 sub a2, a11, a2 /* a2 <-- bytes not copied */