2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Unified implementation of memcpy, memmove and the __copy_user backend.
8 * Copyright (C) 1998, 99, 2000, 01, 2002 Ralf Baechle (ralf@gnu.org)
9 * Copyright (C) 1999, 2000, 01, 2002 Silicon Graphics, Inc.
10 * Copyright (C) 2002 Broadcom, Inc.
11 * memcpy/copy_user author: Mark Vandevoorde
13 * Mnemonic names for arguments to memcpy/__copy_user
17 #include <asm/asm-offsets.h>
18 #include <asm/regdef.h>
27 * memcpy copies len bytes from src to dst and sets v0 to dst.
29 * - src and dst don't overlap
32 * memcpy uses the standard calling convention
34 * __copy_user copies up to len bytes from src to dst and sets a2 (len) to
35 * the number of uncopied bytes due to an exception caused by a read or write.
36 * __copy_user assumes that src and dst don't overlap, and that the call is
37 * implementing one of the following:
39 * - src is readable (no exceptions when reading src)
41 * - dst is writable (no exceptions when writing dst)
42 * __copy_user uses a non-standard calling convention; see
43 * arch/mips/include/asm/uaccess.h
45 * When an exception happens on a load, the handler must
46 # ensure that all of the destination buffer is overwritten to prevent
47 * leaking information to user mode programs.
55 * The exception handler for loads requires that:
56 * 1- AT contain the address of the byte just past the end of the source
58 * 2- src_entry <= src < AT, and
59 * 3- (dst - src) == (dst_entry - src_entry),
60 * The _entry suffix denotes values when __copy_user was called.
62 * (1) is set up up by uaccess.h and maintained by not writing AT in copy_user
63 * (2) is met by incrementing src by the number of bytes copied
64 * (3) is met by not doing loads between a pair of increments of dst and src
66 * The exception handlers for stores adjust len (if necessary) and return.
67 * These handlers do not need to overwrite any data.
69 * For __rmemcpy and memmove an exception is always a kernel bug, therefore
70 * they're not protected.
73 #define EXC(inst_reg,addr,handler) \
75 .section __ex_table,"a"; \
80 * Only on the 64-bit kernel we can made use of 64-bit registers.
105 * As we are sharing code base with the mips32 tree (which use the o32 ABI
106 * register definitions). We need to redefine the register definitions from
107 * the n64 ABI register naming to the o32 ABI register naming.
140 #endif /* USE_DOUBLE */
142 #ifdef CONFIG_CPU_LITTLE_ENDIAN
143 #define LDFIRST LOADR
145 #define STFIRST STORER
146 #define STREST STOREL
147 #define SHIFT_DISCARD SLLV
149 #define LDFIRST LOADL
151 #define STFIRST STOREL
152 #define STREST STORER
153 #define SHIFT_DISCARD SRLV
156 #define FIRST(unit) ((unit)*NBYTES)
157 #define REST(unit) (FIRST(unit)+NBYTES-1)
158 #define UNIT(unit) FIRST(unit)
160 #define ADDRMASK (NBYTES-1)
167 * A combined memcpy/__copy_user
168 * __copy_user sets len to 0 for success; else to an upper bound of
169 * the number of uncopied bytes.
170 * memcpy sets v0 to dst.
173 LEAF(memcpy) /* a0=dst a1=src a2=len */
174 move v0, dst /* return value */
178 * Note: dst & src may be unaligned, len may be 0
182 # Octeon doesn't care if the destination is unaligned. The hardware
183 # can fix it faster than we can special case the assembly.
186 sltu t0, len, NBYTES # Check if < 1 word
187 bnez t0, copy_bytes_checklen
188 and t0, src, ADDRMASK # Check if src unaligned
189 bnez t0, src_unaligned
190 sltu t0, len, 4*NBYTES # Check if < 4 words
191 bnez t0, less_than_4units
192 sltu t0, len, 8*NBYTES # Check if < 8 words
193 bnez t0, less_than_8units
194 sltu t0, len, 16*NBYTES # Check if < 16 words
195 bnez t0, cleanup_both_aligned
196 sltu t0, len, 128+1 # Check if len < 129
197 bnez t0, 1f # Skip prefetch if len is too short
198 sltu t0, len, 256+1 # Check if len < 257
199 bnez t0, 1f # Skip prefetch if len is too short
200 pref 0, 128(src) # We must not prefetch invalid addresses
202 # This is where we loop if there is more than 128 bytes left
203 2: pref 0, 256(src) # We must not prefetch invalid addresses
205 # This is where we loop if we can't prefetch anymore
207 EXC( LOAD t0, UNIT(0)(src), l_exc)
208 EXC( LOAD t1, UNIT(1)(src), l_exc_copy)
209 EXC( LOAD t2, UNIT(2)(src), l_exc_copy)
210 EXC( LOAD t3, UNIT(3)(src), l_exc_copy)
211 SUB len, len, 16*NBYTES
212 EXC( STORE t0, UNIT(0)(dst), s_exc_p16u)
213 EXC( STORE t1, UNIT(1)(dst), s_exc_p15u)
214 EXC( STORE t2, UNIT(2)(dst), s_exc_p14u)
215 EXC( STORE t3, UNIT(3)(dst), s_exc_p13u)
216 EXC( LOAD t0, UNIT(4)(src), l_exc_copy)
217 EXC( LOAD t1, UNIT(5)(src), l_exc_copy)
218 EXC( LOAD t2, UNIT(6)(src), l_exc_copy)
219 EXC( LOAD t3, UNIT(7)(src), l_exc_copy)
220 EXC( STORE t0, UNIT(4)(dst), s_exc_p12u)
221 EXC( STORE t1, UNIT(5)(dst), s_exc_p11u)
222 EXC( STORE t2, UNIT(6)(dst), s_exc_p10u)
223 ADD src, src, 16*NBYTES
224 EXC( STORE t3, UNIT(7)(dst), s_exc_p9u)
225 ADD dst, dst, 16*NBYTES
226 EXC( LOAD t0, UNIT(-8)(src), l_exc_copy)
227 EXC( LOAD t1, UNIT(-7)(src), l_exc_copy)
228 EXC( LOAD t2, UNIT(-6)(src), l_exc_copy)
229 EXC( LOAD t3, UNIT(-5)(src), l_exc_copy)
230 EXC( STORE t0, UNIT(-8)(dst), s_exc_p8u)
231 EXC( STORE t1, UNIT(-7)(dst), s_exc_p7u)
232 EXC( STORE t2, UNIT(-6)(dst), s_exc_p6u)
233 EXC( STORE t3, UNIT(-5)(dst), s_exc_p5u)
234 EXC( LOAD t0, UNIT(-4)(src), l_exc_copy)
235 EXC( LOAD t1, UNIT(-3)(src), l_exc_copy)
236 EXC( LOAD t2, UNIT(-2)(src), l_exc_copy)
237 EXC( LOAD t3, UNIT(-1)(src), l_exc_copy)
238 EXC( STORE t0, UNIT(-4)(dst), s_exc_p4u)
239 EXC( STORE t1, UNIT(-3)(dst), s_exc_p3u)
240 EXC( STORE t2, UNIT(-2)(dst), s_exc_p2u)
241 EXC( STORE t3, UNIT(-1)(dst), s_exc_p1u)
242 sltu t0, len, 256+1 # See if we can prefetch more
244 sltu t0, len, 128 # See if we can loop more time
248 # Jump here if there are less than 16*NBYTES left.
250 cleanup_both_aligned:
252 sltu t0, len, 8*NBYTES
253 bnez t0, less_than_8units
255 EXC( LOAD t0, UNIT(0)(src), l_exc)
256 EXC( LOAD t1, UNIT(1)(src), l_exc_copy)
257 EXC( LOAD t2, UNIT(2)(src), l_exc_copy)
258 EXC( LOAD t3, UNIT(3)(src), l_exc_copy)
259 SUB len, len, 8*NBYTES
260 EXC( STORE t0, UNIT(0)(dst), s_exc_p8u)
261 EXC( STORE t1, UNIT(1)(dst), s_exc_p7u)
262 EXC( STORE t2, UNIT(2)(dst), s_exc_p6u)
263 EXC( STORE t3, UNIT(3)(dst), s_exc_p5u)
264 EXC( LOAD t0, UNIT(4)(src), l_exc_copy)
265 EXC( LOAD t1, UNIT(5)(src), l_exc_copy)
266 EXC( LOAD t2, UNIT(6)(src), l_exc_copy)
267 EXC( LOAD t3, UNIT(7)(src), l_exc_copy)
268 EXC( STORE t0, UNIT(4)(dst), s_exc_p4u)
269 EXC( STORE t1, UNIT(5)(dst), s_exc_p3u)
270 EXC( STORE t2, UNIT(6)(dst), s_exc_p2u)
271 EXC( STORE t3, UNIT(7)(dst), s_exc_p1u)
272 ADD src, src, 8*NBYTES
274 ADD dst, dst, 8*NBYTES
276 # Jump here if there are less than 8*NBYTES left.
279 sltu t0, len, 4*NBYTES
280 bnez t0, less_than_4units
282 EXC( LOAD t0, UNIT(0)(src), l_exc)
283 EXC( LOAD t1, UNIT(1)(src), l_exc_copy)
284 EXC( LOAD t2, UNIT(2)(src), l_exc_copy)
285 EXC( LOAD t3, UNIT(3)(src), l_exc_copy)
286 SUB len, len, 4*NBYTES
287 EXC( STORE t0, UNIT(0)(dst), s_exc_p4u)
288 EXC( STORE t1, UNIT(1)(dst), s_exc_p3u)
289 EXC( STORE t2, UNIT(2)(dst), s_exc_p2u)
290 EXC( STORE t3, UNIT(3)(dst), s_exc_p1u)
291 ADD src, src, 4*NBYTES
293 ADD dst, dst, 4*NBYTES
295 # Jump here if there are less than 4*NBYTES left. This means
296 # we may need to copy up to 3 NBYTES words.
299 sltu t0, len, 1*NBYTES
300 bnez t0, copy_bytes_checklen
303 # 1) Copy NBYTES, then check length again
305 EXC( LOAD t0, 0(src), l_exc)
308 EXC( STORE t0, 0(dst), s_exc_p1u)
310 bnez t1, copy_bytes_checklen
313 # 2) Copy NBYTES, then check length again
315 EXC( LOAD t0, 0(src), l_exc)
318 EXC( STORE t0, 0(dst), s_exc_p1u)
320 bnez t1, copy_bytes_checklen
323 # 3) Copy NBYTES, then check length again
325 EXC( LOAD t0, 0(src), l_exc)
329 b copy_bytes_checklen
330 EXC( STORE t0, -8(dst), s_exc_p1u)
334 SRL t0, len, LOG_NBYTES+2 # +2 for 4 units/iter
335 beqz t0, cleanup_src_unaligned
336 and rem, len, (4*NBYTES-1) # rem = len % 4*NBYTES
339 * Avoid consecutive LD*'s to the same register since some mips
340 * implementations can't issue them in the same cycle.
341 * It's OK to load FIRST(N+1) before REST(N) because the two addresses
342 * are to the same unit (unless src is aligned, but it's not).
344 EXC( LDFIRST t0, FIRST(0)(src), l_exc)
345 EXC( LDFIRST t1, FIRST(1)(src), l_exc_copy)
346 SUB len, len, 4*NBYTES
347 EXC( LDREST t0, REST(0)(src), l_exc_copy)
348 EXC( LDREST t1, REST(1)(src), l_exc_copy)
349 EXC( LDFIRST t2, FIRST(2)(src), l_exc_copy)
350 EXC( LDFIRST t3, FIRST(3)(src), l_exc_copy)
351 EXC( LDREST t2, REST(2)(src), l_exc_copy)
352 EXC( LDREST t3, REST(3)(src), l_exc_copy)
353 ADD src, src, 4*NBYTES
354 EXC( STORE t0, UNIT(0)(dst), s_exc_p4u)
355 EXC( STORE t1, UNIT(1)(dst), s_exc_p3u)
356 EXC( STORE t2, UNIT(2)(dst), s_exc_p2u)
357 EXC( STORE t3, UNIT(3)(dst), s_exc_p1u)
359 ADD dst, dst, 4*NBYTES
361 cleanup_src_unaligned:
363 and rem, len, NBYTES-1 # rem = len % NBYTES
364 beq rem, len, copy_bytes
367 EXC( LDFIRST t0, FIRST(0)(src), l_exc)
368 EXC( LDREST t0, REST(0)(src), l_exc_copy)
370 EXC( STORE t0, 0(dst), s_exc_p1u)
379 /* 0 < len < NBYTES */
380 #define COPY_BYTE(N) \
381 EXC( lb t0, N(src), l_exc); \
384 EXC( sb t0, N(dst), s_exc_p1)
394 EXC( lb t0, NBYTES-2(src), l_exc)
397 EXC( sb t0, NBYTES-2(dst), s_exc_p1)
405 * Copy bytes from src until faulting load address (or until a
408 * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
409 * may be more than a byte beyond the last address.
410 * Hence, the lb below may get an exception.
412 * Assumes src < THREAD_BUADDR($28)
414 LOAD t0, TI_TASK($28)
416 LOAD t0, THREAD_BUADDR(t0)
418 EXC( lb t1, 0(src), l_exc)
420 sb t1, 0(dst) # can't fault -- we're copy_from_user
424 LOAD t0, TI_TASK($28)
426 LOAD t0, THREAD_BUADDR(t0) # t0 is just past last good address
428 SUB len, AT, t0 # len number of uncopied bytes
430 * Here's where we rely on src and dst being incremented in tandem,
432 * dst += (fault addr - src) to put dst at first byte to clear
434 ADD dst, t0 # compute start address in a1
437 * Clear len bytes starting at dst. Can't call __bzero because it
438 * might modify len. An inefficient loop for these rare times...
453 ADD len, len, n*NBYTES
483 sltu t0, a1, t0 # dst + len <= src -> memcpy
484 sltu t1, a0, t1 # dst >= src + len -> memcpy
487 move v0, a0 /* return value */
491 /* fall through to __rmemcpy */
492 LEAF(__rmemcpy) /* a0=dst a1=src a2=len */
494 beqz t0, r_end_bytes_up # src >= dst
496 ADD a0, a2 # dst = dst + len
497 ADD a1, a2 # src = src + len
516 bnez a2, r_end_bytes_up