18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Optimized version of the copy_user() routine.
58c2ecf20Sopenharmony_ci * It is used to copy date across the kernel/user boundary.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * The source and destination are always on opposite side of
88c2ecf20Sopenharmony_ci * the boundary. When reading from user space we must catch
98c2ecf20Sopenharmony_ci * faults on loads. When writing to user space we must catch
108c2ecf20Sopenharmony_ci * errors on stores. Note that because of the nature of the copy
118c2ecf20Sopenharmony_ci * we don't need to worry about overlapping regions.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Inputs:
158c2ecf20Sopenharmony_ci *	in0	address of source buffer
168c2ecf20Sopenharmony_ci *	in1	address of destination buffer
178c2ecf20Sopenharmony_ci *	in2	number of bytes to copy
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Outputs:
208c2ecf20Sopenharmony_ci *	ret0	0 in case of success. The number of bytes NOT copied in
218c2ecf20Sopenharmony_ci *		case of error.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Copyright (C) 2000-2001 Hewlett-Packard Co
248c2ecf20Sopenharmony_ci *	Stephane Eranian <eranian@hpl.hp.com>
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * Fixme:
278c2ecf20Sopenharmony_ci *	- handle the case where we have more than 16 bytes and the alignment
288c2ecf20Sopenharmony_ci *	  are different.
298c2ecf20Sopenharmony_ci *	- more benchmarking
308c2ecf20Sopenharmony_ci *	- fix extraneous stop bit introduced by the EX() macro.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <asm/asmmacro.h>
348c2ecf20Sopenharmony_ci#include <asm/export.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci//
378c2ecf20Sopenharmony_ci// Tuneable parameters
388c2ecf20Sopenharmony_ci//
398c2ecf20Sopenharmony_ci#define COPY_BREAK	16	// we do byte copy below (must be >=16)
408c2ecf20Sopenharmony_ci#define PIPE_DEPTH	21	// pipe depth
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define EPI		p[PIPE_DEPTH-1]
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci//
458c2ecf20Sopenharmony_ci// arguments
468c2ecf20Sopenharmony_ci//
478c2ecf20Sopenharmony_ci#define dst		in0
488c2ecf20Sopenharmony_ci#define src		in1
498c2ecf20Sopenharmony_ci#define len		in2
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci//
528c2ecf20Sopenharmony_ci// local registers
538c2ecf20Sopenharmony_ci//
548c2ecf20Sopenharmony_ci#define t1		r2	// rshift in bytes
558c2ecf20Sopenharmony_ci#define t2		r3	// lshift in bytes
568c2ecf20Sopenharmony_ci#define rshift		r14	// right shift in bits
578c2ecf20Sopenharmony_ci#define lshift		r15	// left shift in bits
588c2ecf20Sopenharmony_ci#define word1		r16
598c2ecf20Sopenharmony_ci#define word2		r17
608c2ecf20Sopenharmony_ci#define cnt		r18
618c2ecf20Sopenharmony_ci#define len2		r19
628c2ecf20Sopenharmony_ci#define saved_lc	r20
638c2ecf20Sopenharmony_ci#define saved_pr	r21
648c2ecf20Sopenharmony_ci#define tmp		r22
658c2ecf20Sopenharmony_ci#define val		r23
668c2ecf20Sopenharmony_ci#define src1		r24
678c2ecf20Sopenharmony_ci#define dst1		r25
688c2ecf20Sopenharmony_ci#define src2		r26
698c2ecf20Sopenharmony_ci#define dst2		r27
708c2ecf20Sopenharmony_ci#define len1		r28
718c2ecf20Sopenharmony_ci#define enddst		r29
728c2ecf20Sopenharmony_ci#define endsrc		r30
738c2ecf20Sopenharmony_ci#define saved_pfs	r31
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ciGLOBAL_ENTRY(__copy_user)
768c2ecf20Sopenharmony_ci	.prologue
778c2ecf20Sopenharmony_ci	.save ar.pfs, saved_pfs
788c2ecf20Sopenharmony_ci	alloc saved_pfs=ar.pfs,3,((2*PIPE_DEPTH+7)&~7),0,((2*PIPE_DEPTH+7)&~7)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	.rotr val1[PIPE_DEPTH],val2[PIPE_DEPTH]
818c2ecf20Sopenharmony_ci	.rotp p[PIPE_DEPTH]
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	adds len2=-1,len	// br.ctop is repeat/until
848c2ecf20Sopenharmony_ci	mov ret0=r0
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	;;			// RAW of cfm when len=0
878c2ecf20Sopenharmony_ci	cmp.eq p8,p0=r0,len	// check for zero length
888c2ecf20Sopenharmony_ci	.save ar.lc, saved_lc
898c2ecf20Sopenharmony_ci	mov saved_lc=ar.lc	// preserve ar.lc (slow)
908c2ecf20Sopenharmony_ci(p8)	br.ret.spnt.many rp	// empty mempcy()
918c2ecf20Sopenharmony_ci	;;
928c2ecf20Sopenharmony_ci	add enddst=dst,len	// first byte after end of source
938c2ecf20Sopenharmony_ci	add endsrc=src,len	// first byte after end of destination
948c2ecf20Sopenharmony_ci	.save pr, saved_pr
958c2ecf20Sopenharmony_ci	mov saved_pr=pr		// preserve predicates
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	.body
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	mov dst1=dst		// copy because of rotation
1008c2ecf20Sopenharmony_ci	mov ar.ec=PIPE_DEPTH
1018c2ecf20Sopenharmony_ci	mov pr.rot=1<<16	// p16=true all others are false
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	mov src1=src		// copy because of rotation
1048c2ecf20Sopenharmony_ci	mov ar.lc=len2		// initialize lc for small count
1058c2ecf20Sopenharmony_ci	cmp.lt p10,p7=COPY_BREAK,len	// if len > COPY_BREAK then long copy
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	xor tmp=src,dst		// same alignment test prepare
1088c2ecf20Sopenharmony_ci(p10)	br.cond.dptk .long_copy_user
1098c2ecf20Sopenharmony_ci	;;			// RAW pr.rot/p16 ?
1108c2ecf20Sopenharmony_ci	//
1118c2ecf20Sopenharmony_ci	// Now we do the byte by byte loop with software pipeline
1128c2ecf20Sopenharmony_ci	//
1138c2ecf20Sopenharmony_ci	// p7 is necessarily false by now
1148c2ecf20Sopenharmony_ci1:
1158c2ecf20Sopenharmony_ci	EX(.failure_in_pipe1,(p16) ld1 val1[0]=[src1],1)
1168c2ecf20Sopenharmony_ci	EX(.failure_out,(EPI) st1 [dst1]=val1[PIPE_DEPTH-1],1)
1178c2ecf20Sopenharmony_ci	br.ctop.dptk.few 1b
1188c2ecf20Sopenharmony_ci	;;
1198c2ecf20Sopenharmony_ci	mov ar.lc=saved_lc
1208c2ecf20Sopenharmony_ci	mov pr=saved_pr,0xffffffffffff0000
1218c2ecf20Sopenharmony_ci	mov ar.pfs=saved_pfs		// restore ar.ec
1228c2ecf20Sopenharmony_ci	br.ret.sptk.many rp		// end of short memcpy
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	//
1258c2ecf20Sopenharmony_ci	// Not 8-byte aligned
1268c2ecf20Sopenharmony_ci	//
1278c2ecf20Sopenharmony_ci.diff_align_copy_user:
1288c2ecf20Sopenharmony_ci	// At this point we know we have more than 16 bytes to copy
1298c2ecf20Sopenharmony_ci	// and also that src and dest do _not_ have the same alignment.
1308c2ecf20Sopenharmony_ci	and src2=0x7,src1				// src offset
1318c2ecf20Sopenharmony_ci	and dst2=0x7,dst1				// dst offset
1328c2ecf20Sopenharmony_ci	;;
1338c2ecf20Sopenharmony_ci	// The basic idea is that we copy byte-by-byte at the head so
1348c2ecf20Sopenharmony_ci	// that we can reach 8-byte alignment for both src1 and dst1.
1358c2ecf20Sopenharmony_ci	// Then copy the body using software pipelined 8-byte copy,
1368c2ecf20Sopenharmony_ci	// shifting the two back-to-back words right and left, then copy
1378c2ecf20Sopenharmony_ci	// the tail by copying byte-by-byte.
1388c2ecf20Sopenharmony_ci	//
1398c2ecf20Sopenharmony_ci	// Fault handling. If the byte-by-byte at the head fails on the
1408c2ecf20Sopenharmony_ci	// load, then restart and finish the pipleline by copying zeros
1418c2ecf20Sopenharmony_ci	// to the dst1. Then copy zeros for the rest of dst1.
1428c2ecf20Sopenharmony_ci	// If 8-byte software pipeline fails on the load, do the same as
1438c2ecf20Sopenharmony_ci	// failure_in3 does. If the byte-by-byte at the tail fails, it is
1448c2ecf20Sopenharmony_ci	// handled simply by failure_in_pipe1.
1458c2ecf20Sopenharmony_ci	//
1468c2ecf20Sopenharmony_ci	// The case p14 represents the source has more bytes in the
1478c2ecf20Sopenharmony_ci	// the first word (by the shifted part), whereas the p15 needs to
1488c2ecf20Sopenharmony_ci	// copy some bytes from the 2nd word of the source that has the
1498c2ecf20Sopenharmony_ci	// tail of the 1st of the destination.
1508c2ecf20Sopenharmony_ci	//
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	//
1538c2ecf20Sopenharmony_ci	// Optimization. If dst1 is 8-byte aligned (quite common), we don't need
1548c2ecf20Sopenharmony_ci	// to copy the head to dst1, to start 8-byte copy software pipeline.
1558c2ecf20Sopenharmony_ci	// We know src1 is not 8-byte aligned in this case.
1568c2ecf20Sopenharmony_ci	//
1578c2ecf20Sopenharmony_ci	cmp.eq p14,p15=r0,dst2
1588c2ecf20Sopenharmony_ci(p15)	br.cond.spnt 1f
1598c2ecf20Sopenharmony_ci	;;
1608c2ecf20Sopenharmony_ci	sub t1=8,src2
1618c2ecf20Sopenharmony_ci	mov t2=src2
1628c2ecf20Sopenharmony_ci	;;
1638c2ecf20Sopenharmony_ci	shl rshift=t2,3
1648c2ecf20Sopenharmony_ci	sub len1=len,t1					// set len1
1658c2ecf20Sopenharmony_ci	;;
1668c2ecf20Sopenharmony_ci	sub lshift=64,rshift
1678c2ecf20Sopenharmony_ci	;;
1688c2ecf20Sopenharmony_ci	br.cond.spnt .word_copy_user
1698c2ecf20Sopenharmony_ci	;;
1708c2ecf20Sopenharmony_ci1:
1718c2ecf20Sopenharmony_ci	cmp.leu	p14,p15=src2,dst2
1728c2ecf20Sopenharmony_ci	sub t1=dst2,src2
1738c2ecf20Sopenharmony_ci	;;
1748c2ecf20Sopenharmony_ci	.pred.rel "mutex", p14, p15
1758c2ecf20Sopenharmony_ci(p14)	sub word1=8,src2				// (8 - src offset)
1768c2ecf20Sopenharmony_ci(p15)	sub t1=r0,t1					// absolute value
1778c2ecf20Sopenharmony_ci(p15)	sub word1=8,dst2				// (8 - dst offset)
1788c2ecf20Sopenharmony_ci	;;
1798c2ecf20Sopenharmony_ci	// For the case p14, we don't need to copy the shifted part to
1808c2ecf20Sopenharmony_ci	// the 1st word of destination.
1818c2ecf20Sopenharmony_ci	sub t2=8,t1
1828c2ecf20Sopenharmony_ci(p14)	sub word1=word1,t1
1838c2ecf20Sopenharmony_ci	;;
1848c2ecf20Sopenharmony_ci	sub len1=len,word1				// resulting len
1858c2ecf20Sopenharmony_ci(p15)	shl rshift=t1,3					// in bits
1868c2ecf20Sopenharmony_ci(p14)	shl rshift=t2,3
1878c2ecf20Sopenharmony_ci	;;
1888c2ecf20Sopenharmony_ci(p14)	sub len1=len1,t1
1898c2ecf20Sopenharmony_ci	adds cnt=-1,word1
1908c2ecf20Sopenharmony_ci	;;
1918c2ecf20Sopenharmony_ci	sub lshift=64,rshift
1928c2ecf20Sopenharmony_ci	mov ar.ec=PIPE_DEPTH
1938c2ecf20Sopenharmony_ci	mov pr.rot=1<<16	// p16=true all others are false
1948c2ecf20Sopenharmony_ci	mov ar.lc=cnt
1958c2ecf20Sopenharmony_ci	;;
1968c2ecf20Sopenharmony_ci2:
1978c2ecf20Sopenharmony_ci	EX(.failure_in_pipe2,(p16) ld1 val1[0]=[src1],1)
1988c2ecf20Sopenharmony_ci	EX(.failure_out,(EPI) st1 [dst1]=val1[PIPE_DEPTH-1],1)
1998c2ecf20Sopenharmony_ci	br.ctop.dptk.few 2b
2008c2ecf20Sopenharmony_ci	;;
2018c2ecf20Sopenharmony_ci	clrrrb
2028c2ecf20Sopenharmony_ci	;;
2038c2ecf20Sopenharmony_ci.word_copy_user:
2048c2ecf20Sopenharmony_ci	cmp.gtu p9,p0=16,len1
2058c2ecf20Sopenharmony_ci(p9)	br.cond.spnt 4f			// if (16 > len1) skip 8-byte copy
2068c2ecf20Sopenharmony_ci	;;
2078c2ecf20Sopenharmony_ci	shr.u cnt=len1,3		// number of 64-bit words
2088c2ecf20Sopenharmony_ci	;;
2098c2ecf20Sopenharmony_ci	adds cnt=-1,cnt
2108c2ecf20Sopenharmony_ci	;;
2118c2ecf20Sopenharmony_ci	.pred.rel "mutex", p14, p15
2128c2ecf20Sopenharmony_ci(p14)	sub src1=src1,t2
2138c2ecf20Sopenharmony_ci(p15)	sub src1=src1,t1
2148c2ecf20Sopenharmony_ci	//
2158c2ecf20Sopenharmony_ci	// Now both src1 and dst1 point to an 8-byte aligned address. And
2168c2ecf20Sopenharmony_ci	// we have more than 8 bytes to copy.
2178c2ecf20Sopenharmony_ci	//
2188c2ecf20Sopenharmony_ci	mov ar.lc=cnt
2198c2ecf20Sopenharmony_ci	mov ar.ec=PIPE_DEPTH
2208c2ecf20Sopenharmony_ci	mov pr.rot=1<<16	// p16=true all others are false
2218c2ecf20Sopenharmony_ci	;;
2228c2ecf20Sopenharmony_ci3:
2238c2ecf20Sopenharmony_ci	//
2248c2ecf20Sopenharmony_ci	// The pipleline consists of 3 stages:
2258c2ecf20Sopenharmony_ci	// 1 (p16):	Load a word from src1
2268c2ecf20Sopenharmony_ci	// 2 (EPI_1):	Shift right pair, saving to tmp
2278c2ecf20Sopenharmony_ci	// 3 (EPI):	Store tmp to dst1
2288c2ecf20Sopenharmony_ci	//
2298c2ecf20Sopenharmony_ci	// To make it simple, use at least 2 (p16) loops to set up val1[n]
2308c2ecf20Sopenharmony_ci	// because we need 2 back-to-back val1[] to get tmp.
2318c2ecf20Sopenharmony_ci	// Note that this implies EPI_2 must be p18 or greater.
2328c2ecf20Sopenharmony_ci	//
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci#define EPI_1		p[PIPE_DEPTH-2]
2358c2ecf20Sopenharmony_ci#define SWITCH(pred, shift)	cmp.eq pred,p0=shift,rshift
2368c2ecf20Sopenharmony_ci#define CASE(pred, shift)	\
2378c2ecf20Sopenharmony_ci	(pred)	br.cond.spnt .copy_user_bit##shift
2388c2ecf20Sopenharmony_ci#define BODY(rshift)						\
2398c2ecf20Sopenharmony_ci.copy_user_bit##rshift:						\
2408c2ecf20Sopenharmony_ci1:								\
2418c2ecf20Sopenharmony_ci	EX(.failure_out,(EPI) st8 [dst1]=tmp,8);		\
2428c2ecf20Sopenharmony_ci(EPI_1) shrp tmp=val1[PIPE_DEPTH-2],val1[PIPE_DEPTH-1],rshift;	\
2438c2ecf20Sopenharmony_ci	EX(3f,(p16) ld8 val1[1]=[src1],8);			\
2448c2ecf20Sopenharmony_ci(p16)	mov val1[0]=r0;						\
2458c2ecf20Sopenharmony_ci	br.ctop.dptk 1b;					\
2468c2ecf20Sopenharmony_ci	;;							\
2478c2ecf20Sopenharmony_ci	br.cond.sptk.many .diff_align_do_tail;			\
2488c2ecf20Sopenharmony_ci2:								\
2498c2ecf20Sopenharmony_ci(EPI)	st8 [dst1]=tmp,8;					\
2508c2ecf20Sopenharmony_ci(EPI_1)	shrp tmp=val1[PIPE_DEPTH-2],val1[PIPE_DEPTH-1],rshift;	\
2518c2ecf20Sopenharmony_ci3:								\
2528c2ecf20Sopenharmony_ci(p16)	mov val1[1]=r0;						\
2538c2ecf20Sopenharmony_ci(p16)	mov val1[0]=r0;						\
2548c2ecf20Sopenharmony_ci	br.ctop.dptk 2b;					\
2558c2ecf20Sopenharmony_ci	;;							\
2568c2ecf20Sopenharmony_ci	br.cond.sptk.many .failure_in2
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	//
2598c2ecf20Sopenharmony_ci	// Since the instruction 'shrp' requires a fixed 128-bit value
2608c2ecf20Sopenharmony_ci	// specifying the bits to shift, we need to provide 7 cases
2618c2ecf20Sopenharmony_ci	// below.
2628c2ecf20Sopenharmony_ci	//
2638c2ecf20Sopenharmony_ci	SWITCH(p6, 8)
2648c2ecf20Sopenharmony_ci	SWITCH(p7, 16)
2658c2ecf20Sopenharmony_ci	SWITCH(p8, 24)
2668c2ecf20Sopenharmony_ci	SWITCH(p9, 32)
2678c2ecf20Sopenharmony_ci	SWITCH(p10, 40)
2688c2ecf20Sopenharmony_ci	SWITCH(p11, 48)
2698c2ecf20Sopenharmony_ci	SWITCH(p12, 56)
2708c2ecf20Sopenharmony_ci	;;
2718c2ecf20Sopenharmony_ci	CASE(p6, 8)
2728c2ecf20Sopenharmony_ci	CASE(p7, 16)
2738c2ecf20Sopenharmony_ci	CASE(p8, 24)
2748c2ecf20Sopenharmony_ci	CASE(p9, 32)
2758c2ecf20Sopenharmony_ci	CASE(p10, 40)
2768c2ecf20Sopenharmony_ci	CASE(p11, 48)
2778c2ecf20Sopenharmony_ci	CASE(p12, 56)
2788c2ecf20Sopenharmony_ci	;;
2798c2ecf20Sopenharmony_ci	BODY(8)
2808c2ecf20Sopenharmony_ci	BODY(16)
2818c2ecf20Sopenharmony_ci	BODY(24)
2828c2ecf20Sopenharmony_ci	BODY(32)
2838c2ecf20Sopenharmony_ci	BODY(40)
2848c2ecf20Sopenharmony_ci	BODY(48)
2858c2ecf20Sopenharmony_ci	BODY(56)
2868c2ecf20Sopenharmony_ci	;;
2878c2ecf20Sopenharmony_ci.diff_align_do_tail:
2888c2ecf20Sopenharmony_ci	.pred.rel "mutex", p14, p15
2898c2ecf20Sopenharmony_ci(p14)	sub src1=src1,t1
2908c2ecf20Sopenharmony_ci(p14)	adds dst1=-8,dst1
2918c2ecf20Sopenharmony_ci(p15)	sub dst1=dst1,t1
2928c2ecf20Sopenharmony_ci	;;
2938c2ecf20Sopenharmony_ci4:
2948c2ecf20Sopenharmony_ci	// Tail correction.
2958c2ecf20Sopenharmony_ci	//
2968c2ecf20Sopenharmony_ci	// The problem with this piplelined loop is that the last word is not
2978c2ecf20Sopenharmony_ci	// loaded and thus parf of the last word written is not correct.
2988c2ecf20Sopenharmony_ci	// To fix that, we simply copy the tail byte by byte.
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	sub len1=endsrc,src1,1
3018c2ecf20Sopenharmony_ci	clrrrb
3028c2ecf20Sopenharmony_ci	;;
3038c2ecf20Sopenharmony_ci	mov ar.ec=PIPE_DEPTH
3048c2ecf20Sopenharmony_ci	mov pr.rot=1<<16	// p16=true all others are false
3058c2ecf20Sopenharmony_ci	mov ar.lc=len1
3068c2ecf20Sopenharmony_ci	;;
3078c2ecf20Sopenharmony_ci5:
3088c2ecf20Sopenharmony_ci	EX(.failure_in_pipe1,(p16) ld1 val1[0]=[src1],1)
3098c2ecf20Sopenharmony_ci	EX(.failure_out,(EPI) st1 [dst1]=val1[PIPE_DEPTH-1],1)
3108c2ecf20Sopenharmony_ci	br.ctop.dptk.few 5b
3118c2ecf20Sopenharmony_ci	;;
3128c2ecf20Sopenharmony_ci	mov ar.lc=saved_lc
3138c2ecf20Sopenharmony_ci	mov pr=saved_pr,0xffffffffffff0000
3148c2ecf20Sopenharmony_ci	mov ar.pfs=saved_pfs
3158c2ecf20Sopenharmony_ci	br.ret.sptk.many rp
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	//
3188c2ecf20Sopenharmony_ci	// Beginning of long mempcy (i.e. > 16 bytes)
3198c2ecf20Sopenharmony_ci	//
3208c2ecf20Sopenharmony_ci.long_copy_user:
3218c2ecf20Sopenharmony_ci	tbit.nz p6,p7=src1,0	// odd alignment
3228c2ecf20Sopenharmony_ci	and tmp=7,tmp
3238c2ecf20Sopenharmony_ci	;;
3248c2ecf20Sopenharmony_ci	cmp.eq p10,p8=r0,tmp
3258c2ecf20Sopenharmony_ci	mov len1=len		// copy because of rotation
3268c2ecf20Sopenharmony_ci(p8)	br.cond.dpnt .diff_align_copy_user
3278c2ecf20Sopenharmony_ci	;;
3288c2ecf20Sopenharmony_ci	// At this point we know we have more than 16 bytes to copy
3298c2ecf20Sopenharmony_ci	// and also that both src and dest have the same alignment
3308c2ecf20Sopenharmony_ci	// which may not be the one we want. So for now we must move
3318c2ecf20Sopenharmony_ci	// forward slowly until we reach 16byte alignment: no need to
3328c2ecf20Sopenharmony_ci	// worry about reaching the end of buffer.
3338c2ecf20Sopenharmony_ci	//
3348c2ecf20Sopenharmony_ci	EX(.failure_in1,(p6) ld1 val1[0]=[src1],1)	// 1-byte aligned
3358c2ecf20Sopenharmony_ci(p6)	adds len1=-1,len1;;
3368c2ecf20Sopenharmony_ci	tbit.nz p7,p0=src1,1
3378c2ecf20Sopenharmony_ci	;;
3388c2ecf20Sopenharmony_ci	EX(.failure_in1,(p7) ld2 val1[1]=[src1],2)	// 2-byte aligned
3398c2ecf20Sopenharmony_ci(p7)	adds len1=-2,len1;;
3408c2ecf20Sopenharmony_ci	tbit.nz p8,p0=src1,2
3418c2ecf20Sopenharmony_ci	;;
3428c2ecf20Sopenharmony_ci	//
3438c2ecf20Sopenharmony_ci	// Stop bit not required after ld4 because if we fail on ld4
3448c2ecf20Sopenharmony_ci	// we have never executed the ld1, therefore st1 is not executed.
3458c2ecf20Sopenharmony_ci	//
3468c2ecf20Sopenharmony_ci	EX(.failure_in1,(p8) ld4 val2[0]=[src1],4)	// 4-byte aligned
3478c2ecf20Sopenharmony_ci	;;
3488c2ecf20Sopenharmony_ci	EX(.failure_out,(p6) st1 [dst1]=val1[0],1)
3498c2ecf20Sopenharmony_ci	tbit.nz p9,p0=src1,3
3508c2ecf20Sopenharmony_ci	;;
3518c2ecf20Sopenharmony_ci	//
3528c2ecf20Sopenharmony_ci	// Stop bit not required after ld8 because if we fail on ld8
3538c2ecf20Sopenharmony_ci	// we have never executed the ld2, therefore st2 is not executed.
3548c2ecf20Sopenharmony_ci	//
3558c2ecf20Sopenharmony_ci	EX(.failure_in1,(p9) ld8 val2[1]=[src1],8)	// 8-byte aligned
3568c2ecf20Sopenharmony_ci	EX(.failure_out,(p7) st2 [dst1]=val1[1],2)
3578c2ecf20Sopenharmony_ci(p8)	adds len1=-4,len1
3588c2ecf20Sopenharmony_ci	;;
3598c2ecf20Sopenharmony_ci	EX(.failure_out, (p8) st4 [dst1]=val2[0],4)
3608c2ecf20Sopenharmony_ci(p9)	adds len1=-8,len1;;
3618c2ecf20Sopenharmony_ci	shr.u cnt=len1,4		// number of 128-bit (2x64bit) words
3628c2ecf20Sopenharmony_ci	;;
3638c2ecf20Sopenharmony_ci	EX(.failure_out, (p9) st8 [dst1]=val2[1],8)
3648c2ecf20Sopenharmony_ci	tbit.nz p6,p0=len1,3
3658c2ecf20Sopenharmony_ci	cmp.eq p7,p0=r0,cnt
3668c2ecf20Sopenharmony_ci	adds tmp=-1,cnt			// br.ctop is repeat/until
3678c2ecf20Sopenharmony_ci(p7)	br.cond.dpnt .dotail		// we have less than 16 bytes left
3688c2ecf20Sopenharmony_ci	;;
3698c2ecf20Sopenharmony_ci	adds src2=8,src1
3708c2ecf20Sopenharmony_ci	adds dst2=8,dst1
3718c2ecf20Sopenharmony_ci	mov ar.lc=tmp
3728c2ecf20Sopenharmony_ci	;;
3738c2ecf20Sopenharmony_ci	//
3748c2ecf20Sopenharmony_ci	// 16bytes/iteration
3758c2ecf20Sopenharmony_ci	//
3768c2ecf20Sopenharmony_ci2:
3778c2ecf20Sopenharmony_ci	EX(.failure_in3,(p16) ld8 val1[0]=[src1],16)
3788c2ecf20Sopenharmony_ci(p16)	ld8 val2[0]=[src2],16
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	EX(.failure_out, (EPI)	st8 [dst1]=val1[PIPE_DEPTH-1],16)
3818c2ecf20Sopenharmony_ci(EPI)	st8 [dst2]=val2[PIPE_DEPTH-1],16
3828c2ecf20Sopenharmony_ci	br.ctop.dptk 2b
3838c2ecf20Sopenharmony_ci	;;			// RAW on src1 when fall through from loop
3848c2ecf20Sopenharmony_ci	//
3858c2ecf20Sopenharmony_ci	// Tail correction based on len only
3868c2ecf20Sopenharmony_ci	//
3878c2ecf20Sopenharmony_ci	// No matter where we come from (loop or test) the src1 pointer
3888c2ecf20Sopenharmony_ci	// is 16 byte aligned AND we have less than 16 bytes to copy.
3898c2ecf20Sopenharmony_ci	//
3908c2ecf20Sopenharmony_ci.dotail:
3918c2ecf20Sopenharmony_ci	EX(.failure_in1,(p6) ld8 val1[0]=[src1],8)	// at least 8 bytes
3928c2ecf20Sopenharmony_ci	tbit.nz p7,p0=len1,2
3938c2ecf20Sopenharmony_ci	;;
3948c2ecf20Sopenharmony_ci	EX(.failure_in1,(p7) ld4 val1[1]=[src1],4)	// at least 4 bytes
3958c2ecf20Sopenharmony_ci	tbit.nz p8,p0=len1,1
3968c2ecf20Sopenharmony_ci	;;
3978c2ecf20Sopenharmony_ci	EX(.failure_in1,(p8) ld2 val2[0]=[src1],2)	// at least 2 bytes
3988c2ecf20Sopenharmony_ci	tbit.nz p9,p0=len1,0
3998c2ecf20Sopenharmony_ci	;;
4008c2ecf20Sopenharmony_ci	EX(.failure_out, (p6) st8 [dst1]=val1[0],8)
4018c2ecf20Sopenharmony_ci	;;
4028c2ecf20Sopenharmony_ci	EX(.failure_in1,(p9) ld1 val2[1]=[src1])	// only 1 byte left
4038c2ecf20Sopenharmony_ci	mov ar.lc=saved_lc
4048c2ecf20Sopenharmony_ci	;;
4058c2ecf20Sopenharmony_ci	EX(.failure_out,(p7) st4 [dst1]=val1[1],4)
4068c2ecf20Sopenharmony_ci	mov pr=saved_pr,0xffffffffffff0000
4078c2ecf20Sopenharmony_ci	;;
4088c2ecf20Sopenharmony_ci	EX(.failure_out, (p8)	st2 [dst1]=val2[0],2)
4098c2ecf20Sopenharmony_ci	mov ar.pfs=saved_pfs
4108c2ecf20Sopenharmony_ci	;;
4118c2ecf20Sopenharmony_ci	EX(.failure_out, (p9)	st1 [dst1]=val2[1])
4128c2ecf20Sopenharmony_ci	br.ret.sptk.many rp
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	//
4168c2ecf20Sopenharmony_ci	// Here we handle the case where the byte by byte copy fails
4178c2ecf20Sopenharmony_ci	// on the load.
4188c2ecf20Sopenharmony_ci	// Several factors make the zeroing of the rest of the buffer kind of
4198c2ecf20Sopenharmony_ci	// tricky:
4208c2ecf20Sopenharmony_ci	//	- the pipeline: loads/stores are not in sync (pipeline)
4218c2ecf20Sopenharmony_ci	//
4228c2ecf20Sopenharmony_ci	//	  In the same loop iteration, the dst1 pointer does not directly
4238c2ecf20Sopenharmony_ci	//	  reflect where the faulty load was.
4248c2ecf20Sopenharmony_ci	//
4258c2ecf20Sopenharmony_ci	//	- pipeline effect
4268c2ecf20Sopenharmony_ci	//	  When you get a fault on load, you may have valid data from
4278c2ecf20Sopenharmony_ci	//	  previous loads not yet store in transit. Such data must be
4288c2ecf20Sopenharmony_ci	//	  store normally before moving onto zeroing the rest.
4298c2ecf20Sopenharmony_ci	//
4308c2ecf20Sopenharmony_ci	//	- single/multi dispersal independence.
4318c2ecf20Sopenharmony_ci	//
4328c2ecf20Sopenharmony_ci	// solution:
4338c2ecf20Sopenharmony_ci	//	- we don't disrupt the pipeline, i.e. data in transit in
4348c2ecf20Sopenharmony_ci	//	  the software pipeline will be eventually move to memory.
4358c2ecf20Sopenharmony_ci	//	  We simply replace the load with a simple mov and keep the
4368c2ecf20Sopenharmony_ci	//	  pipeline going. We can't really do this inline because
4378c2ecf20Sopenharmony_ci	//	  p16 is always reset to 1 when lc > 0.
4388c2ecf20Sopenharmony_ci	//
4398c2ecf20Sopenharmony_ci.failure_in_pipe1:
4408c2ecf20Sopenharmony_ci	sub ret0=endsrc,src1	// number of bytes to zero, i.e. not copied
4418c2ecf20Sopenharmony_ci1:
4428c2ecf20Sopenharmony_ci(p16)	mov val1[0]=r0
4438c2ecf20Sopenharmony_ci(EPI)	st1 [dst1]=val1[PIPE_DEPTH-1],1
4448c2ecf20Sopenharmony_ci	br.ctop.dptk 1b
4458c2ecf20Sopenharmony_ci	;;
4468c2ecf20Sopenharmony_ci	mov pr=saved_pr,0xffffffffffff0000
4478c2ecf20Sopenharmony_ci	mov ar.lc=saved_lc
4488c2ecf20Sopenharmony_ci	mov ar.pfs=saved_pfs
4498c2ecf20Sopenharmony_ci	br.ret.sptk.many rp
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	//
4528c2ecf20Sopenharmony_ci	// This is the case where the byte by byte copy fails on the load
4538c2ecf20Sopenharmony_ci	// when we copy the head. We need to finish the pipeline and copy
4548c2ecf20Sopenharmony_ci	// zeros for the rest of the destination. Since this happens
4558c2ecf20Sopenharmony_ci	// at the top we still need to fill the body and tail.
4568c2ecf20Sopenharmony_ci.failure_in_pipe2:
4578c2ecf20Sopenharmony_ci	sub ret0=endsrc,src1	// number of bytes to zero, i.e. not copied
4588c2ecf20Sopenharmony_ci2:
4598c2ecf20Sopenharmony_ci(p16)	mov val1[0]=r0
4608c2ecf20Sopenharmony_ci(EPI)	st1 [dst1]=val1[PIPE_DEPTH-1],1
4618c2ecf20Sopenharmony_ci	br.ctop.dptk 2b
4628c2ecf20Sopenharmony_ci	;;
4638c2ecf20Sopenharmony_ci	sub len=enddst,dst1,1		// precompute len
4648c2ecf20Sopenharmony_ci	br.cond.dptk.many .failure_in1bis
4658c2ecf20Sopenharmony_ci	;;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	//
4688c2ecf20Sopenharmony_ci	// Here we handle the head & tail part when we check for alignment.
4698c2ecf20Sopenharmony_ci	// The following code handles only the load failures. The
4708c2ecf20Sopenharmony_ci	// main diffculty comes from the fact that loads/stores are
4718c2ecf20Sopenharmony_ci	// scheduled. So when you fail on a load, the stores corresponding
4728c2ecf20Sopenharmony_ci	// to previous successful loads must be executed.
4738c2ecf20Sopenharmony_ci	//
4748c2ecf20Sopenharmony_ci	// However some simplifications are possible given the way
4758c2ecf20Sopenharmony_ci	// things work.
4768c2ecf20Sopenharmony_ci	//
4778c2ecf20Sopenharmony_ci	// 1) HEAD
4788c2ecf20Sopenharmony_ci	// Theory of operation:
4798c2ecf20Sopenharmony_ci	//
4808c2ecf20Sopenharmony_ci	//  Page A   | Page B
4818c2ecf20Sopenharmony_ci	//  ---------|-----
4828c2ecf20Sopenharmony_ci	//          1|8 x
4838c2ecf20Sopenharmony_ci	//	  1 2|8 x
4848c2ecf20Sopenharmony_ci	//	    4|8 x
4858c2ecf20Sopenharmony_ci	//	  1 4|8 x
4868c2ecf20Sopenharmony_ci	//        2 4|8 x
4878c2ecf20Sopenharmony_ci	//      1 2 4|8 x
4888c2ecf20Sopenharmony_ci	//	     |1
4898c2ecf20Sopenharmony_ci	//	     |2 x
4908c2ecf20Sopenharmony_ci	//	     |4 x
4918c2ecf20Sopenharmony_ci	//
4928c2ecf20Sopenharmony_ci	// page_size >= 4k (2^12).  (x means 4, 2, 1)
4938c2ecf20Sopenharmony_ci	// Here we suppose Page A exists and Page B does not.
4948c2ecf20Sopenharmony_ci	//
4958c2ecf20Sopenharmony_ci	// As we move towards eight byte alignment we may encounter faults.
4968c2ecf20Sopenharmony_ci	// The numbers on each page show the size of the load (current alignment).
4978c2ecf20Sopenharmony_ci	//
4988c2ecf20Sopenharmony_ci	// Key point:
4998c2ecf20Sopenharmony_ci	//	- if you fail on 1, 2, 4 then you have never executed any smaller
5008c2ecf20Sopenharmony_ci	//	  size loads, e.g. failing ld4 means no ld1 nor ld2 executed
5018c2ecf20Sopenharmony_ci	//	  before.
5028c2ecf20Sopenharmony_ci	//
5038c2ecf20Sopenharmony_ci	// This allows us to simplify the cleanup code, because basically you
5048c2ecf20Sopenharmony_ci	// only have to worry about "pending" stores in the case of a failing
5058c2ecf20Sopenharmony_ci	// ld8(). Given the way the code is written today, this means only
5068c2ecf20Sopenharmony_ci	// worry about st2, st4. There we can use the information encapsulated
5078c2ecf20Sopenharmony_ci	// into the predicates.
5088c2ecf20Sopenharmony_ci	//
5098c2ecf20Sopenharmony_ci	// Other key point:
5108c2ecf20Sopenharmony_ci	//	- if you fail on the ld8 in the head, it means you went straight
5118c2ecf20Sopenharmony_ci	//	  to it, i.e. 8byte alignment within an unexisting page.
5128c2ecf20Sopenharmony_ci	// Again this comes from the fact that if you crossed just for the ld8 then
5138c2ecf20Sopenharmony_ci	// you are 8byte aligned but also 16byte align, therefore you would
5148c2ecf20Sopenharmony_ci	// either go for the 16byte copy loop OR the ld8 in the tail part.
5158c2ecf20Sopenharmony_ci	// The combination ld1, ld2, ld4, ld8 where you fail on ld8 is impossible
5168c2ecf20Sopenharmony_ci	// because it would mean you had 15bytes to copy in which case you
5178c2ecf20Sopenharmony_ci	// would have defaulted to the byte by byte copy.
5188c2ecf20Sopenharmony_ci	//
5198c2ecf20Sopenharmony_ci	//
5208c2ecf20Sopenharmony_ci	// 2) TAIL
5218c2ecf20Sopenharmony_ci	// Here we now we have less than 16 bytes AND we are either 8 or 16 byte
5228c2ecf20Sopenharmony_ci	// aligned.
5238c2ecf20Sopenharmony_ci	//
5248c2ecf20Sopenharmony_ci	// Key point:
5258c2ecf20Sopenharmony_ci	// This means that we either:
5268c2ecf20Sopenharmony_ci	//		- are right on a page boundary
5278c2ecf20Sopenharmony_ci	//	OR
5288c2ecf20Sopenharmony_ci	//		- are at more than 16 bytes from a page boundary with
5298c2ecf20Sopenharmony_ci	//		  at most 15 bytes to copy: no chance of crossing.
5308c2ecf20Sopenharmony_ci	//
5318c2ecf20Sopenharmony_ci	// This allows us to assume that if we fail on a load we haven't possibly
5328c2ecf20Sopenharmony_ci	// executed any of the previous (tail) ones, so we don't need to do
5338c2ecf20Sopenharmony_ci	// any stores. For instance, if we fail on ld2, this means we had
5348c2ecf20Sopenharmony_ci	// 2 or 3 bytes left to copy and we did not execute the ld8 nor ld4.
5358c2ecf20Sopenharmony_ci	//
5368c2ecf20Sopenharmony_ci	// This means that we are in a situation similar the a fault in the
5378c2ecf20Sopenharmony_ci	// head part. That's nice!
5388c2ecf20Sopenharmony_ci	//
5398c2ecf20Sopenharmony_ci.failure_in1:
5408c2ecf20Sopenharmony_ci	sub ret0=endsrc,src1	// number of bytes to zero, i.e. not copied
5418c2ecf20Sopenharmony_ci	sub len=endsrc,src1,1
5428c2ecf20Sopenharmony_ci	//
5438c2ecf20Sopenharmony_ci	// we know that ret0 can never be zero at this point
5448c2ecf20Sopenharmony_ci	// because we failed why trying to do a load, i.e. there is still
5458c2ecf20Sopenharmony_ci	// some work to do.
5468c2ecf20Sopenharmony_ci	// The failure_in1bis and length problem is taken care of at the
5478c2ecf20Sopenharmony_ci	// calling side.
5488c2ecf20Sopenharmony_ci	//
5498c2ecf20Sopenharmony_ci	;;
5508c2ecf20Sopenharmony_ci.failure_in1bis:		// from (.failure_in3)
5518c2ecf20Sopenharmony_ci	mov ar.lc=len		// Continue with a stupid byte store.
5528c2ecf20Sopenharmony_ci	;;
5538c2ecf20Sopenharmony_ci5:
5548c2ecf20Sopenharmony_ci	st1 [dst1]=r0,1
5558c2ecf20Sopenharmony_ci	br.cloop.dptk 5b
5568c2ecf20Sopenharmony_ci	;;
5578c2ecf20Sopenharmony_ci	mov pr=saved_pr,0xffffffffffff0000
5588c2ecf20Sopenharmony_ci	mov ar.lc=saved_lc
5598c2ecf20Sopenharmony_ci	mov ar.pfs=saved_pfs
5608c2ecf20Sopenharmony_ci	br.ret.sptk.many rp
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	//
5638c2ecf20Sopenharmony_ci	// Here we simply restart the loop but instead
5648c2ecf20Sopenharmony_ci	// of doing loads we fill the pipeline with zeroes
5658c2ecf20Sopenharmony_ci	// We can't simply store r0 because we may have valid
5668c2ecf20Sopenharmony_ci	// data in transit in the pipeline.
5678c2ecf20Sopenharmony_ci	// ar.lc and ar.ec are setup correctly at this point
5688c2ecf20Sopenharmony_ci	//
5698c2ecf20Sopenharmony_ci	// we MUST use src1/endsrc here and not dst1/enddst because
5708c2ecf20Sopenharmony_ci	// of the pipeline effect.
5718c2ecf20Sopenharmony_ci	//
5728c2ecf20Sopenharmony_ci.failure_in3:
5738c2ecf20Sopenharmony_ci	sub ret0=endsrc,src1	// number of bytes to zero, i.e. not copied
5748c2ecf20Sopenharmony_ci	;;
5758c2ecf20Sopenharmony_ci2:
5768c2ecf20Sopenharmony_ci(p16)	mov val1[0]=r0
5778c2ecf20Sopenharmony_ci(p16)	mov val2[0]=r0
5788c2ecf20Sopenharmony_ci(EPI)	st8 [dst1]=val1[PIPE_DEPTH-1],16
5798c2ecf20Sopenharmony_ci(EPI)	st8 [dst2]=val2[PIPE_DEPTH-1],16
5808c2ecf20Sopenharmony_ci	br.ctop.dptk 2b
5818c2ecf20Sopenharmony_ci	;;
5828c2ecf20Sopenharmony_ci	cmp.ne p6,p0=dst1,enddst	// Do we need to finish the tail ?
5838c2ecf20Sopenharmony_ci	sub len=enddst,dst1,1		// precompute len
5848c2ecf20Sopenharmony_ci(p6)	br.cond.dptk .failure_in1bis
5858c2ecf20Sopenharmony_ci	;;
5868c2ecf20Sopenharmony_ci	mov pr=saved_pr,0xffffffffffff0000
5878c2ecf20Sopenharmony_ci	mov ar.lc=saved_lc
5888c2ecf20Sopenharmony_ci	mov ar.pfs=saved_pfs
5898c2ecf20Sopenharmony_ci	br.ret.sptk.many rp
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci.failure_in2:
5928c2ecf20Sopenharmony_ci	sub ret0=endsrc,src1
5938c2ecf20Sopenharmony_ci	cmp.ne p6,p0=dst1,enddst	// Do we need to finish the tail ?
5948c2ecf20Sopenharmony_ci	sub len=enddst,dst1,1		// precompute len
5958c2ecf20Sopenharmony_ci(p6)	br.cond.dptk .failure_in1bis
5968c2ecf20Sopenharmony_ci	;;
5978c2ecf20Sopenharmony_ci	mov pr=saved_pr,0xffffffffffff0000
5988c2ecf20Sopenharmony_ci	mov ar.lc=saved_lc
5998c2ecf20Sopenharmony_ci	mov ar.pfs=saved_pfs
6008c2ecf20Sopenharmony_ci	br.ret.sptk.many rp
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	//
6038c2ecf20Sopenharmony_ci	// handling of failures on stores: that's the easy part
6048c2ecf20Sopenharmony_ci	//
6058c2ecf20Sopenharmony_ci.failure_out:
6068c2ecf20Sopenharmony_ci	sub ret0=enddst,dst1
6078c2ecf20Sopenharmony_ci	mov pr=saved_pr,0xffffffffffff0000
6088c2ecf20Sopenharmony_ci	mov ar.lc=saved_lc
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	mov ar.pfs=saved_pfs
6118c2ecf20Sopenharmony_ci	br.ret.sptk.many rp
6128c2ecf20Sopenharmony_ciEND(__copy_user)
6138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__copy_user)
614