18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * arch/alpha/lib/stxcpy.S 48c2ecf20Sopenharmony_ci * Contributed by Richard Henderson (rth@tamu.edu) 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 248c2ecf20Sopenharmony_ci#include <asm/regdef.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci .set noat 278c2ecf20Sopenharmony_ci .set noreorder 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci .text 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci/* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that 328c2ecf20Sopenharmony_ci doesn't like putting the entry point for a procedure somewhere in the 338c2ecf20Sopenharmony_ci middle of the procedure descriptor. Work around this by putting the 348c2ecf20Sopenharmony_ci aligned copy in its own procedure descriptor */ 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci .ent stxcpy_aligned 378c2ecf20Sopenharmony_ci .align 3 388c2ecf20Sopenharmony_cistxcpy_aligned: 398c2ecf20Sopenharmony_ci .frame sp, 0, t9 408c2ecf20Sopenharmony_ci .prologue 0 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci /* On entry to this basic block: 438c2ecf20Sopenharmony_ci t0 == the first destination word for masking back in 448c2ecf20Sopenharmony_ci t1 == the first source word. */ 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci /* Create the 1st output word and detect 0's in the 1st input word. */ 478c2ecf20Sopenharmony_ci lda t2, -1 # e1 : build a mask against false zero 488c2ecf20Sopenharmony_ci mskqh t2, a1, t2 # e0 : detection in the src word 498c2ecf20Sopenharmony_ci mskqh t1, a1, t3 # e0 : 508c2ecf20Sopenharmony_ci ornot t1, t2, t2 # .. e1 : 518c2ecf20Sopenharmony_ci mskql t0, a1, t0 # e0 : assemble the first output word 528c2ecf20Sopenharmony_ci cmpbge zero, t2, t8 # .. e1 : bits set iff null found 538c2ecf20Sopenharmony_ci or t0, t3, t1 # e0 : 548c2ecf20Sopenharmony_ci bne t8, $a_eos # .. e1 : 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci /* On entry to this basic block: 578c2ecf20Sopenharmony_ci t0 == the first destination word for masking back in 588c2ecf20Sopenharmony_ci t1 == a source word not containing a null. */ 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci$a_loop: 618c2ecf20Sopenharmony_ci stq_u t1, 0(a0) # e0 : 628c2ecf20Sopenharmony_ci addq a0, 8, a0 # .. e1 : 638c2ecf20Sopenharmony_ci ldq_u t1, 0(a1) # e0 : 648c2ecf20Sopenharmony_ci addq a1, 8, a1 # .. e1 : 658c2ecf20Sopenharmony_ci cmpbge zero, t1, t8 # e0 (stall) 668c2ecf20Sopenharmony_ci beq t8, $a_loop # .. e1 (zdb) 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci /* Take care of the final (partial) word store. 698c2ecf20Sopenharmony_ci On entry to this basic block we have: 708c2ecf20Sopenharmony_ci t1 == the source word containing the null 718c2ecf20Sopenharmony_ci t8 == the cmpbge mask that found it. */ 728c2ecf20Sopenharmony_ci$a_eos: 738c2ecf20Sopenharmony_ci negq t8, t6 # e0 : find low bit set 748c2ecf20Sopenharmony_ci and t8, t6, t12 # e1 (stall) 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* For the sake of the cache, don't read a destination word 778c2ecf20Sopenharmony_ci if we're not going to need it. */ 788c2ecf20Sopenharmony_ci and t12, 0x80, t6 # e0 : 798c2ecf20Sopenharmony_ci bne t6, 1f # .. e1 (zdb) 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* We're doing a partial word store and so need to combine 828c2ecf20Sopenharmony_ci our source and original destination words. */ 838c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 848c2ecf20Sopenharmony_ci subq t12, 1, t6 # .. e1 : 858c2ecf20Sopenharmony_ci zapnot t1, t6, t1 # e0 : clear src bytes >= null 868c2ecf20Sopenharmony_ci or t12, t6, t8 # .. e1 : 878c2ecf20Sopenharmony_ci zap t0, t8, t0 # e0 : clear dst bytes <= null 888c2ecf20Sopenharmony_ci or t0, t1, t1 # e1 : 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci1: stq_u t1, 0(a0) # e0 : 918c2ecf20Sopenharmony_ci ret (t9) # .. e1 : 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci .end stxcpy_aligned 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci .align 3 968c2ecf20Sopenharmony_ci .ent __stxcpy 978c2ecf20Sopenharmony_ci .globl __stxcpy 988c2ecf20Sopenharmony_ci__stxcpy: 998c2ecf20Sopenharmony_ci .frame sp, 0, t9 1008c2ecf20Sopenharmony_ci .prologue 0 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* Are source and destination co-aligned? */ 1038c2ecf20Sopenharmony_ci xor a0, a1, t0 # e0 : 1048c2ecf20Sopenharmony_ci unop # : 1058c2ecf20Sopenharmony_ci and t0, 7, t0 # e0 : 1068c2ecf20Sopenharmony_ci bne t0, $unaligned # .. e1 : 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci /* We are co-aligned; take care of a partial first word. */ 1098c2ecf20Sopenharmony_ci ldq_u t1, 0(a1) # e0 : load first src word 1108c2ecf20Sopenharmony_ci and a0, 7, t0 # .. e1 : take care not to load a word ... 1118c2ecf20Sopenharmony_ci addq a1, 8, a1 # e0 : 1128c2ecf20Sopenharmony_ci beq t0, stxcpy_aligned # .. e1 : ... if we wont need it 1138c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 1148c2ecf20Sopenharmony_ci br stxcpy_aligned # .. e1 : 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci/* The source and destination are not co-aligned. Align the destination 1188c2ecf20Sopenharmony_ci and cope. We have to be very careful about not reading too much and 1198c2ecf20Sopenharmony_ci causing a SEGV. */ 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci .align 3 1228c2ecf20Sopenharmony_ci$u_head: 1238c2ecf20Sopenharmony_ci /* We know just enough now to be able to assemble the first 1248c2ecf20Sopenharmony_ci full source word. We can still find a zero at the end of it 1258c2ecf20Sopenharmony_ci that prevents us from outputting the whole thing. 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci On entry to this basic block: 1288c2ecf20Sopenharmony_ci t0 == the first dest word, for masking back in, if needed else 0 1298c2ecf20Sopenharmony_ci t1 == the low bits of the first source word 1308c2ecf20Sopenharmony_ci t6 == bytemask that is -1 in dest word bytes */ 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci ldq_u t2, 8(a1) # e0 : 1338c2ecf20Sopenharmony_ci addq a1, 8, a1 # .. e1 : 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci extql t1, a1, t1 # e0 : 1368c2ecf20Sopenharmony_ci extqh t2, a1, t4 # e0 : 1378c2ecf20Sopenharmony_ci mskql t0, a0, t0 # e0 : 1388c2ecf20Sopenharmony_ci or t1, t4, t1 # .. e1 : 1398c2ecf20Sopenharmony_ci mskqh t1, a0, t1 # e0 : 1408c2ecf20Sopenharmony_ci or t0, t1, t1 # e1 : 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci or t1, t6, t6 # e0 : 1438c2ecf20Sopenharmony_ci cmpbge zero, t6, t8 # .. e1 : 1448c2ecf20Sopenharmony_ci lda t6, -1 # e0 : for masking just below 1458c2ecf20Sopenharmony_ci bne t8, $u_final # .. e1 : 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci mskql t6, a1, t6 # e0 : mask out the bits we have 1488c2ecf20Sopenharmony_ci or t6, t2, t2 # e1 : already extracted before 1498c2ecf20Sopenharmony_ci cmpbge zero, t2, t8 # e0 : testing eos 1508c2ecf20Sopenharmony_ci bne t8, $u_late_head_exit # .. e1 (zdb) 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci /* Finally, we've got all the stupid leading edge cases taken care 1538c2ecf20Sopenharmony_ci of and we can set up to enter the main loop. */ 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci stq_u t1, 0(a0) # e0 : store first output word 1568c2ecf20Sopenharmony_ci addq a0, 8, a0 # .. e1 : 1578c2ecf20Sopenharmony_ci extql t2, a1, t0 # e0 : position ho-bits of lo word 1588c2ecf20Sopenharmony_ci ldq_u t2, 8(a1) # .. e1 : read next high-order source word 1598c2ecf20Sopenharmony_ci addq a1, 8, a1 # e0 : 1608c2ecf20Sopenharmony_ci cmpbge zero, t2, t8 # .. e1 : 1618c2ecf20Sopenharmony_ci nop # e0 : 1628c2ecf20Sopenharmony_ci bne t8, $u_eos # .. e1 : 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci /* Unaligned copy main loop. In order to avoid reading too much, 1658c2ecf20Sopenharmony_ci the loop is structured to detect zeros in aligned source words. 1668c2ecf20Sopenharmony_ci This has, unfortunately, effectively pulled half of a loop 1678c2ecf20Sopenharmony_ci iteration out into the head and half into the tail, but it does 1688c2ecf20Sopenharmony_ci prevent nastiness from accumulating in the very thing we want 1698c2ecf20Sopenharmony_ci to run as fast as possible. 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci On entry to this basic block: 1728c2ecf20Sopenharmony_ci t0 == the shifted high-order bits from the previous source word 1738c2ecf20Sopenharmony_ci t2 == the unshifted current source word 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci We further know that t2 does not contain a null terminator. */ 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci .align 3 1788c2ecf20Sopenharmony_ci$u_loop: 1798c2ecf20Sopenharmony_ci extqh t2, a1, t1 # e0 : extract high bits for current word 1808c2ecf20Sopenharmony_ci addq a1, 8, a1 # .. e1 : 1818c2ecf20Sopenharmony_ci extql t2, a1, t3 # e0 : extract low bits for next time 1828c2ecf20Sopenharmony_ci addq a0, 8, a0 # .. e1 : 1838c2ecf20Sopenharmony_ci or t0, t1, t1 # e0 : current dst word now complete 1848c2ecf20Sopenharmony_ci ldq_u t2, 0(a1) # .. e1 : load high word for next time 1858c2ecf20Sopenharmony_ci stq_u t1, -8(a0) # e0 : save the current word 1868c2ecf20Sopenharmony_ci mov t3, t0 # .. e1 : 1878c2ecf20Sopenharmony_ci cmpbge zero, t2, t8 # e0 : test new word for eos 1888c2ecf20Sopenharmony_ci beq t8, $u_loop # .. e1 : 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci /* We've found a zero somewhere in the source word we just read. 1918c2ecf20Sopenharmony_ci If it resides in the lower half, we have one (probably partial) 1928c2ecf20Sopenharmony_ci word to write out, and if it resides in the upper half, we 1938c2ecf20Sopenharmony_ci have one full and one partial word left to write out. 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$u_eos: 1998c2ecf20Sopenharmony_ci extqh t2, a1, t1 # e0 : 2008c2ecf20Sopenharmony_ci or t0, t1, t1 # e1 : first (partial) source word complete 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci cmpbge zero, t1, t8 # e0 : is the null in this first bit? 2038c2ecf20Sopenharmony_ci bne t8, $u_final # .. e1 (zdb) 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci$u_late_head_exit: 2068c2ecf20Sopenharmony_ci stq_u t1, 0(a0) # e0 : the null was in the high-order bits 2078c2ecf20Sopenharmony_ci addq a0, 8, a0 # .. e1 : 2088c2ecf20Sopenharmony_ci extql t2, a1, t1 # e0 : 2098c2ecf20Sopenharmony_ci cmpbge zero, t1, t8 # .. e1 : 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci /* Take care of a final (probably partial) result word. 2128c2ecf20Sopenharmony_ci On entry to this basic block: 2138c2ecf20Sopenharmony_ci t1 == assembled source word 2148c2ecf20Sopenharmony_ci t8 == cmpbge mask that found the null. */ 2158c2ecf20Sopenharmony_ci$u_final: 2168c2ecf20Sopenharmony_ci negq t8, t6 # e0 : isolate low bit set 2178c2ecf20Sopenharmony_ci and t6, t8, t12 # e1 : 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci and t12, 0x80, t6 # e0 : avoid dest word load if we can 2208c2ecf20Sopenharmony_ci bne t6, 1f # .. e1 (zdb) 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 2238c2ecf20Sopenharmony_ci subq t12, 1, t6 # .. e1 : 2248c2ecf20Sopenharmony_ci or t6, t12, t8 # e0 : 2258c2ecf20Sopenharmony_ci zapnot t1, t6, t1 # .. e1 : kill source bytes >= null 2268c2ecf20Sopenharmony_ci zap t0, t8, t0 # e0 : kill dest bytes <= null 2278c2ecf20Sopenharmony_ci or t0, t1, t1 # e1 : 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci1: stq_u t1, 0(a0) # e0 : 2308c2ecf20Sopenharmony_ci ret (t9) # .. e1 : 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci /* Unaligned copy entry point. */ 2338c2ecf20Sopenharmony_ci .align 3 2348c2ecf20Sopenharmony_ci$unaligned: 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci ldq_u t1, 0(a1) # e0 : load first source word 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci and a0, 7, t4 # .. e1 : find dest misalignment 2398c2ecf20Sopenharmony_ci and a1, 7, t5 # e0 : find src misalignment 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci /* Conditionally load the first destination word and a bytemask 2428c2ecf20Sopenharmony_ci with 0xff indicating that the destination byte is sacrosanct. */ 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci mov zero, t0 # .. e1 : 2458c2ecf20Sopenharmony_ci mov zero, t6 # e0 : 2468c2ecf20Sopenharmony_ci beq t4, 1f # .. e1 : 2478c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 2488c2ecf20Sopenharmony_ci lda t6, -1 # .. e1 : 2498c2ecf20Sopenharmony_ci mskql t6, a0, t6 # e0 : 2508c2ecf20Sopenharmony_ci1: 2518c2ecf20Sopenharmony_ci subq a1, t4, a1 # .. e1 : sub dest misalignment from src addr 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci /* If source misalignment is larger than dest misalignment, we need 2548c2ecf20Sopenharmony_ci extra startup checks to avoid SEGV. */ 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci cmplt t4, t5, t12 # e0 : 2578c2ecf20Sopenharmony_ci beq t12, $u_head # .. e1 (zdb) 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci lda t2, -1 # e1 : mask out leading garbage in source 2608c2ecf20Sopenharmony_ci mskqh t2, t5, t2 # e0 : 2618c2ecf20Sopenharmony_ci nop # e0 : 2628c2ecf20Sopenharmony_ci ornot t1, t2, t3 # .. e1 : 2638c2ecf20Sopenharmony_ci cmpbge zero, t3, t8 # e0 : is there a zero? 2648c2ecf20Sopenharmony_ci beq t8, $u_head # .. e1 (zdb) 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci /* At this point we've found a zero in the first partial word of 2678c2ecf20Sopenharmony_ci the source. We need to isolate the valid source data and mask 2688c2ecf20Sopenharmony_ci it into the original destination data. (Incidentally, we know 2698c2ecf20Sopenharmony_ci that we'll need at least one byte of that original dest word.) */ 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci negq t8, t6 # .. e1 : build bitmask of bytes <= zero 2748c2ecf20Sopenharmony_ci and t6, t8, t12 # e0 : 2758c2ecf20Sopenharmony_ci and a1, 7, t5 # .. e1 : 2768c2ecf20Sopenharmony_ci subq t12, 1, t6 # e0 : 2778c2ecf20Sopenharmony_ci or t6, t12, t8 # e1 : 2788c2ecf20Sopenharmony_ci srl t12, t5, t12 # e0 : adjust final null return value 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci zapnot t2, t8, t2 # .. e1 : prepare source word; mirror changes 2818c2ecf20Sopenharmony_ci and t1, t2, t1 # e1 : to source validity mask 2828c2ecf20Sopenharmony_ci extql t2, a1, t2 # .. e0 : 2838c2ecf20Sopenharmony_ci extql t1, a1, t1 # e0 : 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci andnot t0, t2, t0 # .. e1 : zero place for source to reside 2868c2ecf20Sopenharmony_ci or t0, t1, t1 # e1 : and put it there 2878c2ecf20Sopenharmony_ci stq_u t1, 0(a0) # .. e0 : 2888c2ecf20Sopenharmony_ci ret (t9) # e1 : 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci .end __stxcpy 291