xref: /openbmc/linux/arch/ia64/lib/memcpy_mck.S (revision 4e1a33b1)
1/*
2 * Itanium 2-optimized version of memcpy and copy_user function
3 *
4 * Inputs:
5 * 	in0:	destination address
6 *	in1:	source address
7 *	in2:	number of bytes to copy
8 * Output:
9 *	for memcpy:    return dest
10 * 	for copy_user: return 0 if success,
11 *		       or number of byte NOT copied if error occurred.
12 *
13 * Copyright (C) 2002 Intel Corp.
14 * Copyright (C) 2002 Ken Chen <kenneth.w.chen@intel.com>
15 */
16#include <asm/asmmacro.h>
17#include <asm/page.h>
18#include <asm/export.h>
19
20#define EK(y...) EX(y)
21
22/* McKinley specific optimization */
23
24#define retval		r8
25#define saved_pfs	r31
26#define saved_lc	r10
27#define saved_pr	r11
28#define saved_in0	r14
29#define saved_in1	r15
30#define saved_in2	r16
31
32#define src0		r2
33#define src1		r3
34#define dst0		r17
35#define dst1		r18
36#define cnt		r9
37
38/* r19-r30 are temp for each code section */
39#define PREFETCH_DIST	8
40#define src_pre_mem	r19
41#define dst_pre_mem	r20
42#define src_pre_l2	r21
43#define dst_pre_l2	r22
44#define t1		r23
45#define t2		r24
46#define t3		r25
47#define t4		r26
48#define t5		t1	// alias!
49#define t6		t2	// alias!
50#define t7		t3	// alias!
51#define n8		r27
52#define t9		t5	// alias!
53#define t10		t4	// alias!
54#define t11		t7	// alias!
55#define t12		t6	// alias!
56#define t14		t10	// alias!
57#define t13		r28
58#define t15		r29
59#define tmp		r30
60
61/* defines for long_copy block */
62#define	A	0
63#define B	(PREFETCH_DIST)
64#define C	(B + PREFETCH_DIST)
65#define D	(C + 1)
66#define N	(D + 1)
67#define Nrot	((N + 7) & ~7)
68
69/* alias */
70#define in0		r32
71#define in1		r33
72#define in2		r34
73
74GLOBAL_ENTRY(memcpy)
75	and	r28=0x7,in0
76	and	r29=0x7,in1
77	mov	f6=f0
78	mov	retval=in0
79	br.cond.sptk .common_code
80	;;
81END(memcpy)
82EXPORT_SYMBOL(memcpy)
83GLOBAL_ENTRY(__copy_user)
84	.prologue
85// check dest alignment
86	and	r28=0x7,in0
87	and	r29=0x7,in1
88	mov	f6=f1
89	mov	saved_in0=in0	// save dest pointer
90	mov	saved_in1=in1	// save src pointer
91	mov	retval=r0	// initialize return value
92	;;
93.common_code:
94	cmp.gt	p15,p0=8,in2	// check for small size
95	cmp.ne	p13,p0=0,r28	// check dest alignment
96	cmp.ne	p14,p0=0,r29	// check src alignment
97	add	src0=0,in1
98	sub	r30=8,r28	// for .align_dest
99	mov	saved_in2=in2	// save len
100	;;
101	add	dst0=0,in0
102	add	dst1=1,in0	// dest odd index
103	cmp.le	p6,p0 = 1,r30	// for .align_dest
104(p15)	br.cond.dpnt .memcpy_short
105(p13)	br.cond.dpnt .align_dest
106(p14)	br.cond.dpnt .unaligned_src
107	;;
108
109// both dest and src are aligned on 8-byte boundary
110.aligned_src:
111	.save ar.pfs, saved_pfs
112	alloc	saved_pfs=ar.pfs,3,Nrot-3,0,Nrot
113	.save pr, saved_pr
114	mov	saved_pr=pr
115
116	shr.u	cnt=in2,7	// this much cache line
117	;;
118	cmp.lt	p6,p0=2*PREFETCH_DIST,cnt
119	cmp.lt	p7,p8=1,cnt
120	.save ar.lc, saved_lc
121	mov	saved_lc=ar.lc
122	.body
123	add	cnt=-1,cnt
124	add	src_pre_mem=0,in1	// prefetch src pointer
125	add	dst_pre_mem=0,in0	// prefetch dest pointer
126	;;
127(p7)	mov	ar.lc=cnt	// prefetch count
128(p8)	mov	ar.lc=r0
129(p6)	br.cond.dpnt .long_copy
130	;;
131
132.prefetch:
133	lfetch.fault	  [src_pre_mem], 128
134	lfetch.fault.excl [dst_pre_mem], 128
135	br.cloop.dptk.few .prefetch
136	;;
137
138.medium_copy:
139	and	tmp=31,in2	// copy length after iteration
140	shr.u	r29=in2,5	// number of 32-byte iteration
141	add	dst1=8,dst0	// 2nd dest pointer
142	;;
143	add	cnt=-1,r29	// ctop iteration adjustment
144	cmp.eq	p10,p0=r29,r0	// do we really need to loop?
145	add	src1=8,src0	// 2nd src pointer
146	cmp.le	p6,p0=8,tmp
147	;;
148	cmp.le	p7,p0=16,tmp
149	mov	ar.lc=cnt	// loop setup
150	cmp.eq	p16,p17 = r0,r0
151	mov	ar.ec=2
152(p10)	br.dpnt.few .aligned_src_tail
153	;;
154	TEXT_ALIGN(32)
1551:
156EX(.ex_handler, (p16)	ld8	r34=[src0],16)
157EK(.ex_handler, (p16)	ld8	r38=[src1],16)
158EX(.ex_handler, (p17)	st8	[dst0]=r33,16)
159EK(.ex_handler, (p17)	st8	[dst1]=r37,16)
160	;;
161EX(.ex_handler, (p16)	ld8	r32=[src0],16)
162EK(.ex_handler, (p16)	ld8	r36=[src1],16)
163EX(.ex_handler, (p16)	st8	[dst0]=r34,16)
164EK(.ex_handler, (p16)	st8	[dst1]=r38,16)
165	br.ctop.dptk.few 1b
166	;;
167
168.aligned_src_tail:
169EX(.ex_handler, (p6)	ld8	t1=[src0])
170	mov	ar.lc=saved_lc
171	mov	ar.pfs=saved_pfs
172EX(.ex_hndlr_s, (p7)	ld8	t2=[src1],8)
173	cmp.le	p8,p0=24,tmp
174	and	r21=-8,tmp
175	;;
176EX(.ex_hndlr_s, (p8)	ld8	t3=[src1])
177EX(.ex_handler, (p6)	st8	[dst0]=t1)	// store byte 1
178	and	in2=7,tmp	// remaining length
179EX(.ex_hndlr_d, (p7)	st8	[dst1]=t2,8)	// store byte 2
180	add	src0=src0,r21	// setting up src pointer
181	add	dst0=dst0,r21	// setting up dest pointer
182	;;
183EX(.ex_handler, (p8)	st8	[dst1]=t3)	// store byte 3
184	mov	pr=saved_pr,-1
185	br.dptk.many .memcpy_short
186	;;
187
188/* code taken from copy_page_mck */
189.long_copy:
190	.rotr v[2*PREFETCH_DIST]
191	.rotp p[N]
192
193	mov src_pre_mem = src0
194	mov pr.rot = 0x10000
195	mov ar.ec = 1				// special unrolled loop
196
197	mov dst_pre_mem = dst0
198
199	add src_pre_l2 = 8*8, src0
200	add dst_pre_l2 = 8*8, dst0
201	;;
202	add src0 = 8, src_pre_mem		// first t1 src
203	mov ar.lc = 2*PREFETCH_DIST - 1
204	shr.u cnt=in2,7				// number of lines
205	add src1 = 3*8, src_pre_mem		// first t3 src
206	add dst0 = 8, dst_pre_mem		// first t1 dst
207	add dst1 = 3*8, dst_pre_mem		// first t3 dst
208	;;
209	and tmp=127,in2				// remaining bytes after this block
210	add cnt = -(2*PREFETCH_DIST) - 1, cnt
211	// same as .line_copy loop, but with all predicated-off instructions removed:
212.prefetch_loop:
213EX(.ex_hndlr_lcpy_1, (p[A])	ld8 v[A] = [src_pre_mem], 128)		// M0
214EK(.ex_hndlr_lcpy_1, (p[B])	st8 [dst_pre_mem] = v[B], 128)		// M2
215	br.ctop.sptk .prefetch_loop
216	;;
217	cmp.eq p16, p0 = r0, r0			// reset p16 to 1
218	mov ar.lc = cnt
219	mov ar.ec = N				// # of stages in pipeline
220	;;
221.line_copy:
222EX(.ex_handler,	(p[D])	ld8 t2 = [src0], 3*8)			// M0
223EK(.ex_handler,	(p[D])	ld8 t4 = [src1], 3*8)			// M1
224EX(.ex_handler_lcpy,	(p[B])	st8 [dst_pre_mem] = v[B], 128)		// M2 prefetch dst from memory
225EK(.ex_handler_lcpy,	(p[D])	st8 [dst_pre_l2] = n8, 128)		// M3 prefetch dst from L2
226	;;
227EX(.ex_handler_lcpy,	(p[A])	ld8 v[A] = [src_pre_mem], 128)		// M0 prefetch src from memory
228EK(.ex_handler_lcpy,	(p[C])	ld8 n8 = [src_pre_l2], 128)		// M1 prefetch src from L2
229EX(.ex_handler,	(p[D])	st8 [dst0] =  t1, 8)			// M2
230EK(.ex_handler,	(p[D])	st8 [dst1] =  t3, 8)			// M3
231	;;
232EX(.ex_handler,	(p[D])	ld8  t5 = [src0], 8)
233EK(.ex_handler,	(p[D])	ld8  t7 = [src1], 3*8)
234EX(.ex_handler,	(p[D])	st8 [dst0] =  t2, 3*8)
235EK(.ex_handler,	(p[D])	st8 [dst1] =  t4, 3*8)
236	;;
237EX(.ex_handler,	(p[D])	ld8  t6 = [src0], 3*8)
238EK(.ex_handler,	(p[D])	ld8 t10 = [src1], 8)
239EX(.ex_handler,	(p[D])	st8 [dst0] =  t5, 8)
240EK(.ex_handler,	(p[D])	st8 [dst1] =  t7, 3*8)
241	;;
242EX(.ex_handler,	(p[D])	ld8  t9 = [src0], 3*8)
243EK(.ex_handler,	(p[D])	ld8 t11 = [src1], 3*8)
244EX(.ex_handler,	(p[D])	st8 [dst0] =  t6, 3*8)
245EK(.ex_handler,	(p[D])	st8 [dst1] = t10, 8)
246	;;
247EX(.ex_handler,	(p[D])	ld8 t12 = [src0], 8)
248EK(.ex_handler,	(p[D])	ld8 t14 = [src1], 8)
249EX(.ex_handler,	(p[D])	st8 [dst0] =  t9, 3*8)
250EK(.ex_handler,	(p[D])	st8 [dst1] = t11, 3*8)
251	;;
252EX(.ex_handler,	(p[D])	ld8 t13 = [src0], 4*8)
253EK(.ex_handler,	(p[D])	ld8 t15 = [src1], 4*8)
254EX(.ex_handler,	(p[D])	st8 [dst0] = t12, 8)
255EK(.ex_handler,	(p[D])	st8 [dst1] = t14, 8)
256	;;
257EX(.ex_handler,	(p[C])	ld8  t1 = [src0], 8)
258EK(.ex_handler,	(p[C])	ld8  t3 = [src1], 8)
259EX(.ex_handler,	(p[D])	st8 [dst0] = t13, 4*8)
260EK(.ex_handler,	(p[D])	st8 [dst1] = t15, 4*8)
261	br.ctop.sptk .line_copy
262	;;
263
264	add dst0=-8,dst0
265	add src0=-8,src0
266	mov in2=tmp
267	.restore sp
268	br.sptk.many .medium_copy
269	;;
270
271#define BLOCK_SIZE	128*32
272#define blocksize	r23
273#define curlen		r24
274
275// dest is on 8-byte boundary, src is not. We need to do
276// ld8-ld8, shrp, then st8.  Max 8 byte copy per cycle.
277.unaligned_src:
278	.prologue
279	.save ar.pfs, saved_pfs
280	alloc	saved_pfs=ar.pfs,3,5,0,8
281	.save ar.lc, saved_lc
282	mov	saved_lc=ar.lc
283	.save pr, saved_pr
284	mov	saved_pr=pr
285	.body
286.4k_block:
287	mov	saved_in0=dst0	// need to save all input arguments
288	mov	saved_in2=in2
289	mov	blocksize=BLOCK_SIZE
290	;;
291	cmp.lt	p6,p7=blocksize,in2
292	mov	saved_in1=src0
293	;;
294(p6)	mov	in2=blocksize
295	;;
296	shr.u	r21=in2,7	// this much cache line
297	shr.u	r22=in2,4	// number of 16-byte iteration
298	and	curlen=15,in2	// copy length after iteration
299	and	r30=7,src0	// source alignment
300	;;
301	cmp.lt	p7,p8=1,r21
302	add	cnt=-1,r21
303	;;
304
305	add	src_pre_mem=0,src0	// prefetch src pointer
306	add	dst_pre_mem=0,dst0	// prefetch dest pointer
307	and	src0=-8,src0		// 1st src pointer
308(p7)	mov	ar.lc = cnt
309(p8)	mov	ar.lc = r0
310	;;
311	TEXT_ALIGN(32)
3121:	lfetch.fault	  [src_pre_mem], 128
313	lfetch.fault.excl [dst_pre_mem], 128
314	br.cloop.dptk.few 1b
315	;;
316
317	shladd	dst1=r22,3,dst0	// 2nd dest pointer
318	shladd	src1=r22,3,src0	// 2nd src pointer
319	cmp.eq	p8,p9=r22,r0	// do we really need to loop?
320	cmp.le	p6,p7=8,curlen;	// have at least 8 byte remaining?
321	add	cnt=-1,r22	// ctop iteration adjustment
322	;;
323EX(.ex_handler, (p9)	ld8	r33=[src0],8)	// loop primer
324EK(.ex_handler, (p9)	ld8	r37=[src1],8)
325(p8)	br.dpnt.few .noloop
326	;;
327
328// The jump address is calculated based on src alignment. The COPYU
329// macro below need to confine its size to power of two, so an entry
330// can be caulated using shl instead of an expensive multiply. The
331// size is then hard coded by the following #define to match the
332// actual size.  This make it somewhat tedious when COPYU macro gets
333// changed and this need to be adjusted to match.
334#define LOOP_SIZE 6
3351:
336	mov	r29=ip		// jmp_table thread
337	mov	ar.lc=cnt
338	;;
339	add	r29=.jump_table - 1b - (.jmp1-.jump_table), r29
340	shl	r28=r30, LOOP_SIZE	// jmp_table thread
341	mov	ar.ec=2		// loop setup
342	;;
343	add	r29=r29,r28		// jmp_table thread
344	cmp.eq	p16,p17=r0,r0
345	;;
346	mov	b6=r29			// jmp_table thread
347	;;
348	br.cond.sptk.few b6
349
350// for 8-15 byte case
351// We will skip the loop, but need to replicate the side effect
352// that the loop produces.
353.noloop:
354EX(.ex_handler, (p6)	ld8	r37=[src1],8)
355	add	src0=8,src0
356(p6)	shl	r25=r30,3
357	;;
358EX(.ex_handler, (p6)	ld8	r27=[src1])
359(p6)	shr.u	r28=r37,r25
360(p6)	sub	r26=64,r25
361	;;
362(p6)	shl	r27=r27,r26
363	;;
364(p6)	or	r21=r28,r27
365
366.unaligned_src_tail:
367/* check if we have more than blocksize to copy, if so go back */
368	cmp.gt	p8,p0=saved_in2,blocksize
369	;;
370(p8)	add	dst0=saved_in0,blocksize
371(p8)	add	src0=saved_in1,blocksize
372(p8)	sub	in2=saved_in2,blocksize
373(p8)	br.dpnt	.4k_block
374	;;
375
376/* we have up to 15 byte to copy in the tail.
377 * part of work is already done in the jump table code
378 * we are at the following state.
379 * src side:
380 *
381 *   xxxxxx xx                   <----- r21 has xxxxxxxx already
382 * -------- -------- --------
383 * 0        8        16
384 *          ^
385 *          |
386 *          src1
387 *
388 * dst
389 * -------- -------- --------
390 * ^
391 * |
392 * dst1
393 */
394EX(.ex_handler, (p6)	st8	[dst1]=r21,8)	// more than 8 byte to copy
395(p6)	add	curlen=-8,curlen	// update length
396	mov	ar.pfs=saved_pfs
397	;;
398	mov	ar.lc=saved_lc
399	mov	pr=saved_pr,-1
400	mov	in2=curlen	// remaining length
401	mov	dst0=dst1	// dest pointer
402	add	src0=src1,r30	// forward by src alignment
403	;;
404
405// 7 byte or smaller.
406.memcpy_short:
407	cmp.le	p8,p9   = 1,in2
408	cmp.le	p10,p11 = 2,in2
409	cmp.le	p12,p13 = 3,in2
410	cmp.le	p14,p15 = 4,in2
411	add	src1=1,src0	// second src pointer
412	add	dst1=1,dst0	// second dest pointer
413	;;
414
415EX(.ex_handler_short, (p8)	ld1	t1=[src0],2)
416EK(.ex_handler_short, (p10)	ld1	t2=[src1],2)
417(p9)	br.ret.dpnt rp		// 0 byte copy
418	;;
419
420EX(.ex_handler_short, (p8)	st1	[dst0]=t1,2)
421EK(.ex_handler_short, (p10)	st1	[dst1]=t2,2)
422(p11)	br.ret.dpnt rp		// 1 byte copy
423
424EX(.ex_handler_short, (p12)	ld1	t3=[src0],2)
425EK(.ex_handler_short, (p14)	ld1	t4=[src1],2)
426(p13)	br.ret.dpnt rp		// 2 byte copy
427	;;
428
429	cmp.le	p6,p7   = 5,in2
430	cmp.le	p8,p9   = 6,in2
431	cmp.le	p10,p11 = 7,in2
432
433EX(.ex_handler_short, (p12)	st1	[dst0]=t3,2)
434EK(.ex_handler_short, (p14)	st1	[dst1]=t4,2)
435(p15)	br.ret.dpnt rp		// 3 byte copy
436	;;
437
438EX(.ex_handler_short, (p6)	ld1	t5=[src0],2)
439EK(.ex_handler_short, (p8)	ld1	t6=[src1],2)
440(p7)	br.ret.dpnt rp		// 4 byte copy
441	;;
442
443EX(.ex_handler_short, (p6)	st1	[dst0]=t5,2)
444EK(.ex_handler_short, (p8)	st1	[dst1]=t6,2)
445(p9)	br.ret.dptk rp		// 5 byte copy
446
447EX(.ex_handler_short, (p10)	ld1	t7=[src0],2)
448(p11)	br.ret.dptk rp		// 6 byte copy
449	;;
450
451EX(.ex_handler_short, (p10)	st1	[dst0]=t7,2)
452	br.ret.dptk rp		// done all cases
453
454
455/* Align dest to nearest 8-byte boundary. We know we have at
456 * least 7 bytes to copy, enough to crawl to 8-byte boundary.
457 * Actual number of byte to crawl depend on the dest alignment.
458 * 7 byte or less is taken care at .memcpy_short
459
460 * src0 - source even index
461 * src1 - source  odd index
462 * dst0 - dest even index
463 * dst1 - dest  odd index
464 * r30  - distance to 8-byte boundary
465 */
466
467.align_dest:
468	add	src1=1,in1	// source odd index
469	cmp.le	p7,p0 = 2,r30	// for .align_dest
470	cmp.le	p8,p0 = 3,r30	// for .align_dest
471EX(.ex_handler_short, (p6)	ld1	t1=[src0],2)
472	cmp.le	p9,p0 = 4,r30	// for .align_dest
473	cmp.le	p10,p0 = 5,r30
474	;;
475EX(.ex_handler_short, (p7)	ld1	t2=[src1],2)
476EK(.ex_handler_short, (p8)	ld1	t3=[src0],2)
477	cmp.le	p11,p0 = 6,r30
478EX(.ex_handler_short, (p6)	st1	[dst0] = t1,2)
479	cmp.le	p12,p0 = 7,r30
480	;;
481EX(.ex_handler_short, (p9)	ld1	t4=[src1],2)
482EK(.ex_handler_short, (p10)	ld1	t5=[src0],2)
483EX(.ex_handler_short, (p7)	st1	[dst1] = t2,2)
484EK(.ex_handler_short, (p8)	st1	[dst0] = t3,2)
485	;;
486EX(.ex_handler_short, (p11)	ld1	t6=[src1],2)
487EK(.ex_handler_short, (p12)	ld1	t7=[src0],2)
488	cmp.eq	p6,p7=r28,r29
489EX(.ex_handler_short, (p9)	st1	[dst1] = t4,2)
490EK(.ex_handler_short, (p10)	st1	[dst0] = t5,2)
491	sub	in2=in2,r30
492	;;
493EX(.ex_handler_short, (p11)	st1	[dst1] = t6,2)
494EK(.ex_handler_short, (p12)	st1	[dst0] = t7)
495	add	dst0=in0,r30	// setup arguments
496	add	src0=in1,r30
497(p6)	br.cond.dptk .aligned_src
498(p7)	br.cond.dpnt .unaligned_src
499	;;
500
501/* main loop body in jump table format */
502#define COPYU(shift)									\
5031:											\
504EX(.ex_handler,  (p16)	ld8	r32=[src0],8);		/* 1 */				\
505EK(.ex_handler,  (p16)	ld8	r36=[src1],8);						\
506		 (p17)	shrp	r35=r33,r34,shift;;	/* 1 */				\
507EX(.ex_handler,  (p6)	ld8	r22=[src1]);	/* common, prime for tail section */	\
508		 nop.m	0;								\
509		 (p16)	shrp	r38=r36,r37,shift;					\
510EX(.ex_handler,  (p17)	st8	[dst0]=r35,8);		/* 1 */				\
511EK(.ex_handler,  (p17)	st8	[dst1]=r39,8);						\
512		 br.ctop.dptk.few 1b;;							\
513		 (p7)	add	src1=-8,src1;	/* back out for <8 byte case */		\
514		 shrp	r21=r22,r38,shift;	/* speculative work */			\
515		 br.sptk.few .unaligned_src_tail /* branch out of jump table */		\
516		 ;;
517	TEXT_ALIGN(32)
518.jump_table:
519	COPYU(8)	// unaligned cases
520.jmp1:
521	COPYU(16)
522	COPYU(24)
523	COPYU(32)
524	COPYU(40)
525	COPYU(48)
526	COPYU(56)
527
528#undef A
529#undef B
530#undef C
531#undef D
532
533/*
534 * Due to lack of local tag support in gcc 2.x assembler, it is not clear which
535 * instruction failed in the bundle.  The exception algorithm is that we
536 * first figure out the faulting address, then detect if there is any
537 * progress made on the copy, if so, redo the copy from last known copied
538 * location up to the faulting address (exclusive). In the copy_from_user
539 * case, remaining byte in kernel buffer will be zeroed.
540 *
541 * Take copy_from_user as an example, in the code there are multiple loads
542 * in a bundle and those multiple loads could span over two pages, the
543 * faulting address is calculated as page_round_down(max(src0, src1)).
544 * This is based on knowledge that if we can access one byte in a page, we
545 * can access any byte in that page.
546 *
547 * predicate used in the exception handler:
548 * p6-p7: direction
549 * p10-p11: src faulting addr calculation
550 * p12-p13: dst faulting addr calculation
551 */
552
553#define A	r19
554#define B	r20
555#define C	r21
556#define D	r22
557#define F	r28
558
559#define memset_arg0	r32
560#define memset_arg2	r33
561
562#define saved_retval	loc0
563#define saved_rtlink	loc1
564#define saved_pfs_stack	loc2
565
566.ex_hndlr_s:
567	add	src0=8,src0
568	br.sptk .ex_handler
569	;;
570.ex_hndlr_d:
571	add	dst0=8,dst0
572	br.sptk .ex_handler
573	;;
574.ex_hndlr_lcpy_1:
575	mov	src1=src_pre_mem
576	mov	dst1=dst_pre_mem
577	cmp.gtu	p10,p11=src_pre_mem,saved_in1
578	cmp.gtu	p12,p13=dst_pre_mem,saved_in0
579	;;
580(p10)	add	src0=8,saved_in1
581(p11)	mov	src0=saved_in1
582(p12)	add	dst0=8,saved_in0
583(p13)	mov	dst0=saved_in0
584	br.sptk	.ex_handler
585.ex_handler_lcpy:
586	// in line_copy block, the preload addresses should always ahead
587	// of the other two src/dst pointers.  Furthermore, src1/dst1 should
588	// always ahead of src0/dst0.
589	mov	src1=src_pre_mem
590	mov	dst1=dst_pre_mem
591.ex_handler:
592	mov	pr=saved_pr,-1		// first restore pr, lc, and pfs
593	mov	ar.lc=saved_lc
594	mov	ar.pfs=saved_pfs
595	;;
596.ex_handler_short: // fault occurred in these sections didn't change pr, lc, pfs
597	cmp.ltu	p6,p7=saved_in0, saved_in1	// get the copy direction
598	cmp.ltu	p10,p11=src0,src1
599	cmp.ltu	p12,p13=dst0,dst1
600	fcmp.eq	p8,p0=f6,f0		// is it memcpy?
601	mov	tmp = dst0
602	;;
603(p11)	mov	src1 = src0		// pick the larger of the two
604(p13)	mov	dst0 = dst1		// make dst0 the smaller one
605(p13)	mov	dst1 = tmp		// and dst1 the larger one
606	;;
607(p6)	dep	F = r0,dst1,0,PAGE_SHIFT // usr dst round down to page boundary
608(p7)	dep	F = r0,src1,0,PAGE_SHIFT // usr src round down to page boundary
609	;;
610(p6)	cmp.le	p14,p0=dst0,saved_in0	// no progress has been made on store
611(p7)	cmp.le	p14,p0=src0,saved_in1	// no progress has been made on load
612	mov	retval=saved_in2
613(p8)	ld1	tmp=[src1]		// force an oops for memcpy call
614(p8)	st1	[dst1]=r0		// force an oops for memcpy call
615(p14)	br.ret.sptk.many rp
616
617/*
618 * The remaining byte to copy is calculated as:
619 *
620 * A =	(faulting_addr - orig_src)	-> len to faulting ld address
621 *	or
622 * 	(faulting_addr - orig_dst)	-> len to faulting st address
623 * B =	(cur_dst - orig_dst)		-> len copied so far
624 * C =	A - B				-> len need to be copied
625 * D =	orig_len - A			-> len need to be zeroed
626 */
627(p6)	sub	A = F, saved_in0
628(p7)	sub	A = F, saved_in1
629	clrrrb
630	;;
631	alloc	saved_pfs_stack=ar.pfs,3,3,3,0
632	cmp.lt	p8,p0=A,r0
633	sub	B = dst0, saved_in0	// how many byte copied so far
634	;;
635(p8)	mov	A = 0;			// A shouldn't be negative, cap it
636	;;
637	sub	C = A, B
638	sub	D = saved_in2, A
639	;;
640	cmp.gt	p8,p0=C,r0		// more than 1 byte?
641	add	memset_arg0=saved_in0, A
642(p6)	mov	memset_arg2=0		// copy_to_user should not call memset
643(p7)	mov	memset_arg2=D		// copy_from_user need to have kbuf zeroed
644	mov	r8=0
645	mov	saved_retval = D
646	mov	saved_rtlink = b0
647
648	add	out0=saved_in0, B
649	add	out1=saved_in1, B
650	mov	out2=C
651(p8)	br.call.sptk.few b0=__copy_user	// recursive call
652	;;
653
654	add	saved_retval=saved_retval,r8	// above might return non-zero value
655	cmp.gt	p8,p0=memset_arg2,r0	// more than 1 byte?
656	mov	out0=memset_arg0	// *s
657	mov	out1=r0			// c
658	mov	out2=memset_arg2	// n
659(p8)	br.call.sptk.few b0=memset
660	;;
661
662	mov	retval=saved_retval
663	mov	ar.pfs=saved_pfs_stack
664	mov	b0=saved_rtlink
665	br.ret.sptk.many rp
666
667/* end of McKinley specific optimization */
668END(__copy_user)
669EXPORT_SYMBOL(__copy_user)
670