1/*
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
4 * for more details.
5 *
6 * Unified implementation of memcpy, memmove and the __copy_user backend.
7 *
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
12 *
13 * Mnemonic names for arguments to memcpy/__copy_user
14 */
15
16#include <asm/asm.h>
17#include <asm/asm-offsets.h>
18#include <asm/export.h>
19#include <asm/regdef.h>
20
21#define dst a0
22#define src a1
23#define len a2
24
25/*
26 * Spec
27 *
28 * memcpy copies len bytes from src to dst and sets v0 to dst.
29 * It assumes that
30 *   - src and dst don't overlap
31 *   - src is readable
32 *   - dst is writable
33 * memcpy uses the standard calling convention
34 *
35 * __copy_user copies up to len bytes from src to dst and sets a2 (len) to
36 * the number of uncopied bytes due to an exception caused by a read or write.
37 * __copy_user assumes that src and dst don't overlap, and that the call is
38 * implementing one of the following:
39 *   copy_to_user
40 *     - src is readable  (no exceptions when reading src)
41 *   copy_from_user
42 *     - dst is writable  (no exceptions when writing dst)
43 * __copy_user uses a non-standard calling convention; see
44 * arch/mips/include/asm/uaccess.h
45 *
46 * When an exception happens on a load, the handler must
47 # ensure that all of the destination buffer is overwritten to prevent
48 * leaking information to user mode programs.
49 */
50
51/*
52 * Implementation
53 */
54
55/*
56 * The exception handler for loads requires that:
57 *  1- AT contain the address of the byte just past the end of the source
58 *     of the copy,
59 *  2- src_entry <= src < AT, and
60 *  3- (dst - src) == (dst_entry - src_entry),
61 * The _entry suffix denotes values when __copy_user was called.
62 *
63 * (1) is set up up by uaccess.h and maintained by not writing AT in copy_user
64 * (2) is met by incrementing src by the number of bytes copied
65 * (3) is met by not doing loads between a pair of increments of dst and src
66 *
67 * The exception handlers for stores adjust len (if necessary) and return.
68 * These handlers do not need to overwrite any data.
69 *
70 * For __rmemcpy and memmove an exception is always a kernel bug, therefore
71 * they're not protected.
72 */
73
74#define EXC(inst_reg,addr,handler)		\
759:	inst_reg, addr;				\
76	.section __ex_table,"a";		\
77	PTR	9b, handler;			\
78	.previous
79
80/*
81 * Only on the 64-bit kernel we can made use of 64-bit registers.
82 */
83
84#define LOAD   ld
85#define LOADL  ldl
86#define LOADR  ldr
87#define STOREL sdl
88#define STORER sdr
89#define STORE  sd
90#define ADD    daddu
91#define SUB    dsubu
92#define SRL    dsrl
93#define SRA    dsra
94#define SLL    dsll
95#define SLLV   dsllv
96#define SRLV   dsrlv
97#define NBYTES 8
98#define LOG_NBYTES 3
99
100/*
101 * As we are sharing code base with the mips32 tree (which use the o32 ABI
102 * register definitions). We need to redefine the register definitions from
103 * the n64 ABI register naming to the o32 ABI register naming.
104 */
105#undef t0
106#undef t1
107#undef t2
108#undef t3
109#define t0	$8
110#define t1	$9
111#define t2	$10
112#define t3	$11
113#define t4	$12
114#define t5	$13
115#define t6	$14
116#define t7	$15
117
118#ifdef CONFIG_CPU_LITTLE_ENDIAN
119#define LDFIRST LOADR
120#define LDREST	LOADL
121#define STFIRST STORER
122#define STREST	STOREL
123#define SHIFT_DISCARD SLLV
124#else
125#define LDFIRST LOADL
126#define LDREST	LOADR
127#define STFIRST STOREL
128#define STREST	STORER
129#define SHIFT_DISCARD SRLV
130#endif
131
132#define FIRST(unit) ((unit)*NBYTES)
133#define REST(unit)  (FIRST(unit)+NBYTES-1)
134#define UNIT(unit)  FIRST(unit)
135
136#define ADDRMASK (NBYTES-1)
137
138	.text
139	.set	noreorder
140	.set	noat
141
142/*
143 * A combined memcpy/__copy_user
144 * __copy_user sets len to 0 for success; else to an upper bound of
145 * the number of uncopied bytes.
146 * memcpy sets v0 to dst.
147 */
148	.align	5
149LEAF(memcpy)					/* a0=dst a1=src a2=len */
150EXPORT_SYMBOL(memcpy)
151	move	v0, dst				/* return value */
152__memcpy:
153FEXPORT(__raw_copy_from_user)
154EXPORT_SYMBOL(__raw_copy_from_user)
155FEXPORT(__raw_copy_to_user)
156EXPORT_SYMBOL(__raw_copy_to_user)
157FEXPORT(__raw_copy_in_user)
158EXPORT_SYMBOL(__raw_copy_in_user)
159	/*
160	 * Note: dst & src may be unaligned, len may be 0
161	 * Temps
162	 */
163	#
164	# Octeon doesn't care if the destination is unaligned. The hardware
165	# can fix it faster than we can special case the assembly.
166	#
167	pref	0, 0(src)
168	sltu	t0, len, NBYTES		# Check if < 1 word
169	bnez	t0, copy_bytes_checklen
170	 and	t0, src, ADDRMASK	# Check if src unaligned
171	bnez	t0, src_unaligned
172	 sltu	t0, len, 4*NBYTES	# Check if < 4 words
173	bnez	t0, less_than_4units
174	 sltu	t0, len, 8*NBYTES	# Check if < 8 words
175	bnez	t0, less_than_8units
176	 sltu	t0, len, 16*NBYTES	# Check if < 16 words
177	bnez	t0, cleanup_both_aligned
178	 sltu	t0, len, 128+1		# Check if len < 129
179	bnez	t0, 1f			# Skip prefetch if len is too short
180	 sltu	t0, len, 256+1		# Check if len < 257
181	bnez	t0, 1f			# Skip prefetch if len is too short
182	 pref	0, 128(src)		# We must not prefetch invalid addresses
183	#
184	# This is where we loop if there is more than 128 bytes left
1852:	pref	0, 256(src)		# We must not prefetch invalid addresses
186	#
187	# This is where we loop if we can't prefetch anymore
1881:
189EXC(	LOAD	t0, UNIT(0)(src),	l_exc)
190EXC(	LOAD	t1, UNIT(1)(src),	l_exc_copy)
191EXC(	LOAD	t2, UNIT(2)(src),	l_exc_copy)
192EXC(	LOAD	t3, UNIT(3)(src),	l_exc_copy)
193	SUB	len, len, 16*NBYTES
194EXC(	STORE	t0, UNIT(0)(dst),	s_exc_p16u)
195EXC(	STORE	t1, UNIT(1)(dst),	s_exc_p15u)
196EXC(	STORE	t2, UNIT(2)(dst),	s_exc_p14u)
197EXC(	STORE	t3, UNIT(3)(dst),	s_exc_p13u)
198EXC(	LOAD	t0, UNIT(4)(src),	l_exc_copy)
199EXC(	LOAD	t1, UNIT(5)(src),	l_exc_copy)
200EXC(	LOAD	t2, UNIT(6)(src),	l_exc_copy)
201EXC(	LOAD	t3, UNIT(7)(src),	l_exc_copy)
202EXC(	STORE	t0, UNIT(4)(dst),	s_exc_p12u)
203EXC(	STORE	t1, UNIT(5)(dst),	s_exc_p11u)
204EXC(	STORE	t2, UNIT(6)(dst),	s_exc_p10u)
205	ADD	src, src, 16*NBYTES
206EXC(	STORE	t3, UNIT(7)(dst),	s_exc_p9u)
207	ADD	dst, dst, 16*NBYTES
208EXC(	LOAD	t0, UNIT(-8)(src),	l_exc_copy_rewind16)
209EXC(	LOAD	t1, UNIT(-7)(src),	l_exc_copy_rewind16)
210EXC(	LOAD	t2, UNIT(-6)(src),	l_exc_copy_rewind16)
211EXC(	LOAD	t3, UNIT(-5)(src),	l_exc_copy_rewind16)
212EXC(	STORE	t0, UNIT(-8)(dst),	s_exc_p8u)
213EXC(	STORE	t1, UNIT(-7)(dst),	s_exc_p7u)
214EXC(	STORE	t2, UNIT(-6)(dst),	s_exc_p6u)
215EXC(	STORE	t3, UNIT(-5)(dst),	s_exc_p5u)
216EXC(	LOAD	t0, UNIT(-4)(src),	l_exc_copy_rewind16)
217EXC(	LOAD	t1, UNIT(-3)(src),	l_exc_copy_rewind16)
218EXC(	LOAD	t2, UNIT(-2)(src),	l_exc_copy_rewind16)
219EXC(	LOAD	t3, UNIT(-1)(src),	l_exc_copy_rewind16)
220EXC(	STORE	t0, UNIT(-4)(dst),	s_exc_p4u)
221EXC(	STORE	t1, UNIT(-3)(dst),	s_exc_p3u)
222EXC(	STORE	t2, UNIT(-2)(dst),	s_exc_p2u)
223EXC(	STORE	t3, UNIT(-1)(dst),	s_exc_p1u)
224	sltu	t0, len, 256+1		# See if we can prefetch more
225	beqz	t0, 2b
226	 sltu	t0, len, 128		# See if we can loop more time
227	beqz	t0, 1b
228	 nop
229	#
230	# Jump here if there are less than 16*NBYTES left.
231	#
232cleanup_both_aligned:
233	beqz	len, done
234	 sltu	t0, len, 8*NBYTES
235	bnez	t0, less_than_8units
236	 nop
237EXC(	LOAD	t0, UNIT(0)(src),	l_exc)
238EXC(	LOAD	t1, UNIT(1)(src),	l_exc_copy)
239EXC(	LOAD	t2, UNIT(2)(src),	l_exc_copy)
240EXC(	LOAD	t3, UNIT(3)(src),	l_exc_copy)
241	SUB	len, len, 8*NBYTES
242EXC(	STORE	t0, UNIT(0)(dst),	s_exc_p8u)
243EXC(	STORE	t1, UNIT(1)(dst),	s_exc_p7u)
244EXC(	STORE	t2, UNIT(2)(dst),	s_exc_p6u)
245EXC(	STORE	t3, UNIT(3)(dst),	s_exc_p5u)
246EXC(	LOAD	t0, UNIT(4)(src),	l_exc_copy)
247EXC(	LOAD	t1, UNIT(5)(src),	l_exc_copy)
248EXC(	LOAD	t2, UNIT(6)(src),	l_exc_copy)
249EXC(	LOAD	t3, UNIT(7)(src),	l_exc_copy)
250EXC(	STORE	t0, UNIT(4)(dst),	s_exc_p4u)
251EXC(	STORE	t1, UNIT(5)(dst),	s_exc_p3u)
252EXC(	STORE	t2, UNIT(6)(dst),	s_exc_p2u)
253EXC(	STORE	t3, UNIT(7)(dst),	s_exc_p1u)
254	ADD	src, src, 8*NBYTES
255	beqz	len, done
256	 ADD	dst, dst, 8*NBYTES
257	#
258	# Jump here if there are less than 8*NBYTES left.
259	#
260less_than_8units:
261	sltu	t0, len, 4*NBYTES
262	bnez	t0, less_than_4units
263	 nop
264EXC(	LOAD	t0, UNIT(0)(src),	l_exc)
265EXC(	LOAD	t1, UNIT(1)(src),	l_exc_copy)
266EXC(	LOAD	t2, UNIT(2)(src),	l_exc_copy)
267EXC(	LOAD	t3, UNIT(3)(src),	l_exc_copy)
268	SUB	len, len, 4*NBYTES
269EXC(	STORE	t0, UNIT(0)(dst),	s_exc_p4u)
270EXC(	STORE	t1, UNIT(1)(dst),	s_exc_p3u)
271EXC(	STORE	t2, UNIT(2)(dst),	s_exc_p2u)
272EXC(	STORE	t3, UNIT(3)(dst),	s_exc_p1u)
273	ADD	src, src, 4*NBYTES
274	beqz	len, done
275	 ADD	dst, dst, 4*NBYTES
276	#
277	# Jump here if there are less than 4*NBYTES left. This means
278	# we may need to copy up to 3 NBYTES words.
279	#
280less_than_4units:
281	sltu	t0, len, 1*NBYTES
282	bnez	t0, copy_bytes_checklen
283	 nop
284	#
285	# 1) Copy NBYTES, then check length again
286	#
287EXC(	LOAD	t0, 0(src),		l_exc)
288	SUB	len, len, NBYTES
289	sltu	t1, len, 8
290EXC(	STORE	t0, 0(dst),		s_exc_p1u)
291	ADD	src, src, NBYTES
292	bnez	t1, copy_bytes_checklen
293	 ADD	dst, dst, NBYTES
294	#
295	# 2) Copy NBYTES, then check length again
296	#
297EXC(	LOAD	t0, 0(src),		l_exc)
298	SUB	len, len, NBYTES
299	sltu	t1, len, 8
300EXC(	STORE	t0, 0(dst),		s_exc_p1u)
301	ADD	src, src, NBYTES
302	bnez	t1, copy_bytes_checklen
303	 ADD	dst, dst, NBYTES
304	#
305	# 3) Copy NBYTES, then check length again
306	#
307EXC(	LOAD	t0, 0(src),		l_exc)
308	SUB	len, len, NBYTES
309	ADD	src, src, NBYTES
310	ADD	dst, dst, NBYTES
311	b copy_bytes_checklen
312EXC(	 STORE	t0, -8(dst),		s_exc_p1u)
313
314src_unaligned:
315#define rem t8
316	SRL	t0, len, LOG_NBYTES+2	 # +2 for 4 units/iter
317	beqz	t0, cleanup_src_unaligned
318	 and	rem, len, (4*NBYTES-1)	 # rem = len % 4*NBYTES
3191:
320/*
321 * Avoid consecutive LD*'s to the same register since some mips
322 * implementations can't issue them in the same cycle.
323 * It's OK to load FIRST(N+1) before REST(N) because the two addresses
324 * are to the same unit (unless src is aligned, but it's not).
325 */
326EXC(	LDFIRST t0, FIRST(0)(src),	l_exc)
327EXC(	LDFIRST t1, FIRST(1)(src),	l_exc_copy)
328	SUB	len, len, 4*NBYTES
329EXC(	LDREST	t0, REST(0)(src),	l_exc_copy)
330EXC(	LDREST	t1, REST(1)(src),	l_exc_copy)
331EXC(	LDFIRST t2, FIRST(2)(src),	l_exc_copy)
332EXC(	LDFIRST t3, FIRST(3)(src),	l_exc_copy)
333EXC(	LDREST	t2, REST(2)(src),	l_exc_copy)
334EXC(	LDREST	t3, REST(3)(src),	l_exc_copy)
335	ADD	src, src, 4*NBYTES
336EXC(	STORE	t0, UNIT(0)(dst),	s_exc_p4u)
337EXC(	STORE	t1, UNIT(1)(dst),	s_exc_p3u)
338EXC(	STORE	t2, UNIT(2)(dst),	s_exc_p2u)
339EXC(	STORE	t3, UNIT(3)(dst),	s_exc_p1u)
340	bne	len, rem, 1b
341	 ADD	dst, dst, 4*NBYTES
342
343cleanup_src_unaligned:
344	beqz	len, done
345	 and	rem, len, NBYTES-1  # rem = len % NBYTES
346	beq	rem, len, copy_bytes
347	 nop
3481:
349EXC(	LDFIRST t0, FIRST(0)(src),	l_exc)
350EXC(	LDREST	t0, REST(0)(src),	l_exc_copy)
351	SUB	len, len, NBYTES
352EXC(	STORE	t0, 0(dst),		s_exc_p1u)
353	ADD	src, src, NBYTES
354	bne	len, rem, 1b
355	 ADD	dst, dst, NBYTES
356
357copy_bytes_checklen:
358	beqz	len, done
359	 nop
360copy_bytes:
361	/* 0 < len < NBYTES  */
362#define COPY_BYTE(N)			\
363EXC(	lb	t0, N(src), l_exc);	\
364	SUB	len, len, 1;		\
365	beqz	len, done;		\
366EXC(	 sb	t0, N(dst), s_exc_p1)
367
368	COPY_BYTE(0)
369	COPY_BYTE(1)
370	COPY_BYTE(2)
371	COPY_BYTE(3)
372	COPY_BYTE(4)
373	COPY_BYTE(5)
374EXC(	lb	t0, NBYTES-2(src), l_exc)
375	SUB	len, len, 1
376	jr	ra
377EXC(	 sb	t0, NBYTES-2(dst), s_exc_p1)
378done:
379	jr	ra
380	 nop
381	END(memcpy)
382
383l_exc_copy_rewind16:
384	/* Rewind src and dst by 16*NBYTES for l_exc_copy */
385	SUB	src, src, 16*NBYTES
386	SUB	dst, dst, 16*NBYTES
387l_exc_copy:
388	/*
389	 * Copy bytes from src until faulting load address (or until a
390	 * lb faults)
391	 *
392	 * When reached by a faulting LDFIRST/LDREST, THREAD_BUADDR($28)
393	 * may be more than a byte beyond the last address.
394	 * Hence, the lb below may get an exception.
395	 *
396	 * Assumes src < THREAD_BUADDR($28)
397	 */
398	LOAD	t0, TI_TASK($28)
399	LOAD	t0, THREAD_BUADDR(t0)
4001:
401EXC(	lb	t1, 0(src),	l_exc)
402	ADD	src, src, 1
403	sb	t1, 0(dst)	# can't fault -- we're copy_from_user
404	bne	src, t0, 1b
405	 ADD	dst, dst, 1
406l_exc:
407	LOAD	t0, TI_TASK($28)
408	LOAD	t0, THREAD_BUADDR(t0)	# t0 is just past last good address
409	SUB	len, AT, t0		# len number of uncopied bytes
410	jr	ra
411	 nop
412
413
414#define SEXC(n)				\
415s_exc_p ## n ## u:			\
416	jr	ra;			\
417	 ADD	len, len, n*NBYTES
418
419SEXC(16)
420SEXC(15)
421SEXC(14)
422SEXC(13)
423SEXC(12)
424SEXC(11)
425SEXC(10)
426SEXC(9)
427SEXC(8)
428SEXC(7)
429SEXC(6)
430SEXC(5)
431SEXC(4)
432SEXC(3)
433SEXC(2)
434SEXC(1)
435
436s_exc_p1:
437	jr	ra
438	 ADD	len, len, 1
439s_exc:
440	jr	ra
441	 nop
442
443	.align	5
444LEAF(memmove)
445EXPORT_SYMBOL(memmove)
446	ADD	t0, a0, a2
447	ADD	t1, a1, a2
448	sltu	t0, a1, t0			# dst + len <= src -> memcpy
449	sltu	t1, a0, t1			# dst >= src + len -> memcpy
450	and	t0, t1
451	beqz	t0, __memcpy
452	 move	v0, a0				/* return value */
453	beqz	a2, r_out
454	END(memmove)
455
456	/* fall through to __rmemcpy */
457LEAF(__rmemcpy)					/* a0=dst a1=src a2=len */
458	 sltu	t0, a1, a0
459	beqz	t0, r_end_bytes_up		# src >= dst
460	 nop
461	ADD	a0, a2				# dst = dst + len
462	ADD	a1, a2				# src = src + len
463
464r_end_bytes:
465	lb	t0, -1(a1)
466	SUB	a2, a2, 0x1
467	sb	t0, -1(a0)
468	SUB	a1, a1, 0x1
469	bnez	a2, r_end_bytes
470	 SUB	a0, a0, 0x1
471
472r_out:
473	jr	ra
474	 move	a2, zero
475
476r_end_bytes_up:
477	lb	t0, (a1)
478	SUB	a2, a2, 0x1
479	sb	t0, (a0)
480	ADD	a1, a1, 0x1
481	bnez	a2, r_end_bytes_up
482	 ADD	a0, a0, 0x1
483
484	jr	ra
485	 move	a2, zero
486	END(__rmemcpy)
487