18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * arch/alpha/lib/ev6-stxcpy.S 48c2ecf20Sopenharmony_ci * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copy a null-terminated string from SRC to DST. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * This is an internal routine used by strcpy, stpcpy, and strcat. 98c2ecf20Sopenharmony_ci * As such, it uses special linkage conventions to make implementation 108c2ecf20Sopenharmony_ci * of these public functions more efficient. 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * On input: 138c2ecf20Sopenharmony_ci * t9 = return address 148c2ecf20Sopenharmony_ci * a0 = DST 158c2ecf20Sopenharmony_ci * a1 = SRC 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * On output: 188c2ecf20Sopenharmony_ci * t12 = bitmask (with one bit set) indicating the last byte written 198c2ecf20Sopenharmony_ci * a0 = unaligned address of the last *word* written 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * Furthermore, v0, a3-a5, t11, and t12 are untouched. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * Much of the information about 21264 scheduling/coding comes from: 248c2ecf20Sopenharmony_ci * Compiler Writer's Guide for the Alpha 21264 258c2ecf20Sopenharmony_ci * abbreviated as 'CWG' in other comments here 268c2ecf20Sopenharmony_ci * ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html 278c2ecf20Sopenharmony_ci * Scheduling notation: 288c2ecf20Sopenharmony_ci * E - either cluster 298c2ecf20Sopenharmony_ci * U - upper subcluster; U0 - subcluster U0; U1 - subcluster U1 308c2ecf20Sopenharmony_ci * L - lower subcluster; L0 - subcluster L0; L1 - subcluster L1 318c2ecf20Sopenharmony_ci * Try not to change the actual algorithm if possible for consistency. 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#include <asm/regdef.h> 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci .set noat 378c2ecf20Sopenharmony_ci .set noreorder 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci .text 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that 428c2ecf20Sopenharmony_ci doesn't like putting the entry point for a procedure somewhere in the 438c2ecf20Sopenharmony_ci middle of the procedure descriptor. Work around this by putting the 448c2ecf20Sopenharmony_ci aligned copy in its own procedure descriptor */ 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci .ent stxcpy_aligned 488c2ecf20Sopenharmony_ci .align 4 498c2ecf20Sopenharmony_cistxcpy_aligned: 508c2ecf20Sopenharmony_ci .frame sp, 0, t9 518c2ecf20Sopenharmony_ci .prologue 0 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci /* On entry to this basic block: 548c2ecf20Sopenharmony_ci t0 == the first destination word for masking back in 558c2ecf20Sopenharmony_ci t1 == the first source word. */ 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci /* Create the 1st output word and detect 0's in the 1st input word. */ 588c2ecf20Sopenharmony_ci lda t2, -1 # E : build a mask against false zero 598c2ecf20Sopenharmony_ci mskqh t2, a1, t2 # U : detection in the src word (stall) 608c2ecf20Sopenharmony_ci mskqh t1, a1, t3 # U : 618c2ecf20Sopenharmony_ci ornot t1, t2, t2 # E : (stall) 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci mskql t0, a1, t0 # U : assemble the first output word 648c2ecf20Sopenharmony_ci cmpbge zero, t2, t8 # E : bits set iff null found 658c2ecf20Sopenharmony_ci or t0, t3, t1 # E : (stall) 668c2ecf20Sopenharmony_ci bne t8, $a_eos # U : (stall) 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci /* On entry to this basic block: 698c2ecf20Sopenharmony_ci t0 == the first destination word for masking back in 708c2ecf20Sopenharmony_ci t1 == a source word not containing a null. */ 718c2ecf20Sopenharmony_ci /* Nops here to separate store quads from load quads */ 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci$a_loop: 748c2ecf20Sopenharmony_ci stq_u t1, 0(a0) # L : 758c2ecf20Sopenharmony_ci addq a0, 8, a0 # E : 768c2ecf20Sopenharmony_ci nop 778c2ecf20Sopenharmony_ci nop 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci ldq_u t1, 0(a1) # L : Latency=3 808c2ecf20Sopenharmony_ci addq a1, 8, a1 # E : 818c2ecf20Sopenharmony_ci cmpbge zero, t1, t8 # E : (3 cycle stall) 828c2ecf20Sopenharmony_ci beq t8, $a_loop # U : (stall for t8) 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci /* Take care of the final (partial) word store. 858c2ecf20Sopenharmony_ci On entry to this basic block we have: 868c2ecf20Sopenharmony_ci t1 == the source word containing the null 878c2ecf20Sopenharmony_ci t8 == the cmpbge mask that found it. */ 888c2ecf20Sopenharmony_ci$a_eos: 898c2ecf20Sopenharmony_ci negq t8, t6 # E : find low bit set 908c2ecf20Sopenharmony_ci and t8, t6, t12 # E : (stall) 918c2ecf20Sopenharmony_ci /* For the sake of the cache, don't read a destination word 928c2ecf20Sopenharmony_ci if we're not going to need it. */ 938c2ecf20Sopenharmony_ci and t12, 0x80, t6 # E : (stall) 948c2ecf20Sopenharmony_ci bne t6, 1f # U : (stall) 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci /* We're doing a partial word store and so need to combine 978c2ecf20Sopenharmony_ci our source and original destination words. */ 988c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # L : Latency=3 998c2ecf20Sopenharmony_ci subq t12, 1, t6 # E : 1008c2ecf20Sopenharmony_ci zapnot t1, t6, t1 # U : clear src bytes >= null (stall) 1018c2ecf20Sopenharmony_ci or t12, t6, t8 # E : (stall) 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci zap t0, t8, t0 # E : clear dst bytes <= null 1048c2ecf20Sopenharmony_ci or t0, t1, t1 # E : (stall) 1058c2ecf20Sopenharmony_ci nop 1068c2ecf20Sopenharmony_ci nop 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci1: stq_u t1, 0(a0) # L : 1098c2ecf20Sopenharmony_ci ret (t9) # L0 : Latency=3 1108c2ecf20Sopenharmony_ci nop 1118c2ecf20Sopenharmony_ci nop 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci .end stxcpy_aligned 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci .align 4 1168c2ecf20Sopenharmony_ci .ent __stxcpy 1178c2ecf20Sopenharmony_ci .globl __stxcpy 1188c2ecf20Sopenharmony_ci__stxcpy: 1198c2ecf20Sopenharmony_ci .frame sp, 0, t9 1208c2ecf20Sopenharmony_ci .prologue 0 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci /* Are source and destination co-aligned? */ 1238c2ecf20Sopenharmony_ci xor a0, a1, t0 # E : 1248c2ecf20Sopenharmony_ci unop # E : 1258c2ecf20Sopenharmony_ci and t0, 7, t0 # E : (stall) 1268c2ecf20Sopenharmony_ci bne t0, $unaligned # U : (stall) 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci /* We are co-aligned; take care of a partial first word. */ 1298c2ecf20Sopenharmony_ci ldq_u t1, 0(a1) # L : load first src word 1308c2ecf20Sopenharmony_ci and a0, 7, t0 # E : take care not to load a word ... 1318c2ecf20Sopenharmony_ci addq a1, 8, a1 # E : 1328c2ecf20Sopenharmony_ci beq t0, stxcpy_aligned # U : ... if we wont need it (stall) 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # L : 1358c2ecf20Sopenharmony_ci br stxcpy_aligned # L0 : Latency=3 1368c2ecf20Sopenharmony_ci nop 1378c2ecf20Sopenharmony_ci nop 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci/* The source and destination are not co-aligned. Align the destination 1418c2ecf20Sopenharmony_ci and cope. We have to be very careful about not reading too much and 1428c2ecf20Sopenharmony_ci causing a SEGV. */ 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci .align 4 1458c2ecf20Sopenharmony_ci$u_head: 1468c2ecf20Sopenharmony_ci /* We know just enough now to be able to assemble the first 1478c2ecf20Sopenharmony_ci full source word. We can still find a zero at the end of it 1488c2ecf20Sopenharmony_ci that prevents us from outputting the whole thing. 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci On entry to this basic block: 1518c2ecf20Sopenharmony_ci t0 == the first dest word, for masking back in, if needed else 0 1528c2ecf20Sopenharmony_ci t1 == the low bits of the first source word 1538c2ecf20Sopenharmony_ci t6 == bytemask that is -1 in dest word bytes */ 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci ldq_u t2, 8(a1) # L : 1568c2ecf20Sopenharmony_ci addq a1, 8, a1 # E : 1578c2ecf20Sopenharmony_ci extql t1, a1, t1 # U : (stall on a1) 1588c2ecf20Sopenharmony_ci extqh t2, a1, t4 # U : (stall on a1) 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci mskql t0, a0, t0 # U : 1618c2ecf20Sopenharmony_ci or t1, t4, t1 # E : 1628c2ecf20Sopenharmony_ci mskqh t1, a0, t1 # U : (stall on t1) 1638c2ecf20Sopenharmony_ci or t0, t1, t1 # E : (stall on t1) 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci or t1, t6, t6 # E : 1668c2ecf20Sopenharmony_ci cmpbge zero, t6, t8 # E : (stall) 1678c2ecf20Sopenharmony_ci lda t6, -1 # E : for masking just below 1688c2ecf20Sopenharmony_ci bne t8, $u_final # U : (stall) 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci mskql t6, a1, t6 # U : mask out the bits we have 1718c2ecf20Sopenharmony_ci or t6, t2, t2 # E : already extracted before (stall) 1728c2ecf20Sopenharmony_ci cmpbge zero, t2, t8 # E : testing eos (stall) 1738c2ecf20Sopenharmony_ci bne t8, $u_late_head_exit # U : (stall) 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci /* Finally, we've got all the stupid leading edge cases taken care 1768c2ecf20Sopenharmony_ci of and we can set up to enter the main loop. */ 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci stq_u t1, 0(a0) # L : store first output word 1798c2ecf20Sopenharmony_ci addq a0, 8, a0 # E : 1808c2ecf20Sopenharmony_ci extql t2, a1, t0 # U : position ho-bits of lo word 1818c2ecf20Sopenharmony_ci ldq_u t2, 8(a1) # U : read next high-order source word 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci addq a1, 8, a1 # E : 1848c2ecf20Sopenharmony_ci cmpbge zero, t2, t8 # E : (stall for t2) 1858c2ecf20Sopenharmony_ci nop # E : 1868c2ecf20Sopenharmony_ci bne t8, $u_eos # U : (stall) 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci /* Unaligned copy main loop. In order to avoid reading too much, 1898c2ecf20Sopenharmony_ci the loop is structured to detect zeros in aligned source words. 1908c2ecf20Sopenharmony_ci This has, unfortunately, effectively pulled half of a loop 1918c2ecf20Sopenharmony_ci iteration out into the head and half into the tail, but it does 1928c2ecf20Sopenharmony_ci prevent nastiness from accumulating in the very thing we want 1938c2ecf20Sopenharmony_ci to run as fast as possible. 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci On entry to this basic block: 1968c2ecf20Sopenharmony_ci t0 == the shifted high-order bits from the previous source word 1978c2ecf20Sopenharmony_ci t2 == the unshifted current source word 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci We further know that t2 does not contain a null terminator. */ 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci .align 3 2028c2ecf20Sopenharmony_ci$u_loop: 2038c2ecf20Sopenharmony_ci extqh t2, a1, t1 # U : extract high bits for current word 2048c2ecf20Sopenharmony_ci addq a1, 8, a1 # E : (stall) 2058c2ecf20Sopenharmony_ci extql t2, a1, t3 # U : extract low bits for next time (stall) 2068c2ecf20Sopenharmony_ci addq a0, 8, a0 # E : 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci or t0, t1, t1 # E : current dst word now complete 2098c2ecf20Sopenharmony_ci ldq_u t2, 0(a1) # L : Latency=3 load high word for next time 2108c2ecf20Sopenharmony_ci stq_u t1, -8(a0) # L : save the current word (stall) 2118c2ecf20Sopenharmony_ci mov t3, t0 # E : 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci cmpbge zero, t2, t8 # E : test new word for eos 2148c2ecf20Sopenharmony_ci beq t8, $u_loop # U : (stall) 2158c2ecf20Sopenharmony_ci nop 2168c2ecf20Sopenharmony_ci nop 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci /* We've found a zero somewhere in the source word we just read. 2198c2ecf20Sopenharmony_ci If it resides in the lower half, we have one (probably partial) 2208c2ecf20Sopenharmony_ci word to write out, and if it resides in the upper half, we 2218c2ecf20Sopenharmony_ci have one full and one partial word left to write out. 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci On entry to this basic block: 2248c2ecf20Sopenharmony_ci t0 == the shifted high-order bits from the previous source word 2258c2ecf20Sopenharmony_ci t2 == the unshifted current source word. */ 2268c2ecf20Sopenharmony_ci$u_eos: 2278c2ecf20Sopenharmony_ci extqh t2, a1, t1 # U : 2288c2ecf20Sopenharmony_ci or t0, t1, t1 # E : first (partial) source word complete (stall) 2298c2ecf20Sopenharmony_ci cmpbge zero, t1, t8 # E : is the null in this first bit? (stall) 2308c2ecf20Sopenharmony_ci bne t8, $u_final # U : (stall) 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci$u_late_head_exit: 2338c2ecf20Sopenharmony_ci stq_u t1, 0(a0) # L : the null was in the high-order bits 2348c2ecf20Sopenharmony_ci addq a0, 8, a0 # E : 2358c2ecf20Sopenharmony_ci extql t2, a1, t1 # U : 2368c2ecf20Sopenharmony_ci cmpbge zero, t1, t8 # E : (stall) 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci /* Take care of a final (probably partial) result word. 2398c2ecf20Sopenharmony_ci On entry to this basic block: 2408c2ecf20Sopenharmony_ci t1 == assembled source word 2418c2ecf20Sopenharmony_ci t8 == cmpbge mask that found the null. */ 2428c2ecf20Sopenharmony_ci$u_final: 2438c2ecf20Sopenharmony_ci negq t8, t6 # E : isolate low bit set 2448c2ecf20Sopenharmony_ci and t6, t8, t12 # E : (stall) 2458c2ecf20Sopenharmony_ci and t12, 0x80, t6 # E : avoid dest word load if we can (stall) 2468c2ecf20Sopenharmony_ci bne t6, 1f # U : (stall) 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # E : 2498c2ecf20Sopenharmony_ci subq t12, 1, t6 # E : 2508c2ecf20Sopenharmony_ci or t6, t12, t8 # E : (stall) 2518c2ecf20Sopenharmony_ci zapnot t1, t6, t1 # U : kill source bytes >= null (stall) 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci zap t0, t8, t0 # U : kill dest bytes <= null (2 cycle data stall) 2548c2ecf20Sopenharmony_ci or t0, t1, t1 # E : (stall) 2558c2ecf20Sopenharmony_ci nop 2568c2ecf20Sopenharmony_ci nop 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci1: stq_u t1, 0(a0) # L : 2598c2ecf20Sopenharmony_ci ret (t9) # L0 : Latency=3 2608c2ecf20Sopenharmony_ci nop 2618c2ecf20Sopenharmony_ci nop 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci /* Unaligned copy entry point. */ 2648c2ecf20Sopenharmony_ci .align 4 2658c2ecf20Sopenharmony_ci$unaligned: 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci ldq_u t1, 0(a1) # L : load first source word 2688c2ecf20Sopenharmony_ci and a0, 7, t4 # E : find dest misalignment 2698c2ecf20Sopenharmony_ci and a1, 7, t5 # E : find src misalignment 2708c2ecf20Sopenharmony_ci /* Conditionally load the first destination word and a bytemask 2718c2ecf20Sopenharmony_ci with 0xff indicating that the destination byte is sacrosanct. */ 2728c2ecf20Sopenharmony_ci mov zero, t0 # E : 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci mov zero, t6 # E : 2758c2ecf20Sopenharmony_ci beq t4, 1f # U : 2768c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # L : 2778c2ecf20Sopenharmony_ci lda t6, -1 # E : 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci mskql t6, a0, t6 # U : 2808c2ecf20Sopenharmony_ci nop 2818c2ecf20Sopenharmony_ci nop 2828c2ecf20Sopenharmony_ci nop 2838c2ecf20Sopenharmony_ci1: 2848c2ecf20Sopenharmony_ci subq a1, t4, a1 # E : sub dest misalignment from src addr 2858c2ecf20Sopenharmony_ci /* If source misalignment is larger than dest misalignment, we need 2868c2ecf20Sopenharmony_ci extra startup checks to avoid SEGV. */ 2878c2ecf20Sopenharmony_ci cmplt t4, t5, t12 # E : 2888c2ecf20Sopenharmony_ci beq t12, $u_head # U : 2898c2ecf20Sopenharmony_ci lda t2, -1 # E : mask out leading garbage in source 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci mskqh t2, t5, t2 # U : 2928c2ecf20Sopenharmony_ci ornot t1, t2, t3 # E : (stall) 2938c2ecf20Sopenharmony_ci cmpbge zero, t3, t8 # E : is there a zero? (stall) 2948c2ecf20Sopenharmony_ci beq t8, $u_head # U : (stall) 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci /* At this point we've found a zero in the first partial word of 2978c2ecf20Sopenharmony_ci the source. We need to isolate the valid source data and mask 2988c2ecf20Sopenharmony_ci it into the original destination data. (Incidentally, we know 2998c2ecf20Sopenharmony_ci that we'll need at least one byte of that original dest word.) */ 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # L : 3028c2ecf20Sopenharmony_ci negq t8, t6 # E : build bitmask of bytes <= zero 3038c2ecf20Sopenharmony_ci and t6, t8, t12 # E : (stall) 3048c2ecf20Sopenharmony_ci and a1, 7, t5 # E : 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci subq t12, 1, t6 # E : 3078c2ecf20Sopenharmony_ci or t6, t12, t8 # E : (stall) 3088c2ecf20Sopenharmony_ci srl t12, t5, t12 # U : adjust final null return value 3098c2ecf20Sopenharmony_ci zapnot t2, t8, t2 # U : prepare source word; mirror changes (stall) 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci and t1, t2, t1 # E : to source validity mask 3128c2ecf20Sopenharmony_ci extql t2, a1, t2 # U : 3138c2ecf20Sopenharmony_ci extql t1, a1, t1 # U : (stall) 3148c2ecf20Sopenharmony_ci andnot t0, t2, t0 # .. e1 : zero place for source to reside (stall) 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci or t0, t1, t1 # e1 : and put it there 3178c2ecf20Sopenharmony_ci stq_u t1, 0(a0) # .. e0 : (stall) 3188c2ecf20Sopenharmony_ci ret (t9) # e1 : 3198c2ecf20Sopenharmony_ci nop 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci .end __stxcpy 3228c2ecf20Sopenharmony_ci 323