162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * arch/alpha/lib/stxcpy.S 462306a36Sopenharmony_ci * Contributed by Richard Henderson (rth@tamu.edu) 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Copy a null-terminated string from SRC to DST. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * This is an internal routine used by strcpy, stpcpy, and strcat. 962306a36Sopenharmony_ci * As such, it uses special linkage conventions to make implementation 1062306a36Sopenharmony_ci * of these public functions more efficient. 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * On input: 1362306a36Sopenharmony_ci * t9 = return address 1462306a36Sopenharmony_ci * a0 = DST 1562306a36Sopenharmony_ci * a1 = SRC 1662306a36Sopenharmony_ci * 1762306a36Sopenharmony_ci * On output: 1862306a36Sopenharmony_ci * t12 = bitmask (with one bit set) indicating the last byte written 1962306a36Sopenharmony_ci * a0 = unaligned address of the last *word* written 2062306a36Sopenharmony_ci * 2162306a36Sopenharmony_ci * Furthermore, v0, a3-a5, t11, and t12 are untouched. 2262306a36Sopenharmony_ci */ 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#include <asm/regdef.h> 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci .set noat 2762306a36Sopenharmony_ci .set noreorder 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci .text 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci/* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that 3262306a36Sopenharmony_ci doesn't like putting the entry point for a procedure somewhere in the 3362306a36Sopenharmony_ci middle of the procedure descriptor. Work around this by putting the 3462306a36Sopenharmony_ci aligned copy in its own procedure descriptor */ 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci .ent stxcpy_aligned 3762306a36Sopenharmony_ci .align 3 3862306a36Sopenharmony_cistxcpy_aligned: 3962306a36Sopenharmony_ci .frame sp, 0, t9 4062306a36Sopenharmony_ci .prologue 0 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci /* On entry to this basic block: 4362306a36Sopenharmony_ci t0 == the first destination word for masking back in 4462306a36Sopenharmony_ci t1 == the first source word. */ 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci /* Create the 1st output word and detect 0's in the 1st input word. */ 4762306a36Sopenharmony_ci lda t2, -1 # e1 : build a mask against false zero 4862306a36Sopenharmony_ci mskqh t2, a1, t2 # e0 : detection in the src word 4962306a36Sopenharmony_ci mskqh t1, a1, t3 # e0 : 5062306a36Sopenharmony_ci ornot t1, t2, t2 # .. e1 : 5162306a36Sopenharmony_ci mskql t0, a1, t0 # e0 : assemble the first output word 5262306a36Sopenharmony_ci cmpbge zero, t2, t8 # .. e1 : bits set iff null found 5362306a36Sopenharmony_ci or t0, t3, t1 # e0 : 5462306a36Sopenharmony_ci bne t8, $a_eos # .. e1 : 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci /* On entry to this basic block: 5762306a36Sopenharmony_ci t0 == the first destination word for masking back in 5862306a36Sopenharmony_ci t1 == a source word not containing a null. */ 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci$a_loop: 6162306a36Sopenharmony_ci stq_u t1, 0(a0) # e0 : 6262306a36Sopenharmony_ci addq a0, 8, a0 # .. e1 : 6362306a36Sopenharmony_ci ldq_u t1, 0(a1) # e0 : 6462306a36Sopenharmony_ci addq a1, 8, a1 # .. e1 : 6562306a36Sopenharmony_ci cmpbge zero, t1, t8 # e0 (stall) 6662306a36Sopenharmony_ci beq t8, $a_loop # .. e1 (zdb) 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci /* Take care of the final (partial) word store. 6962306a36Sopenharmony_ci On entry to this basic block we have: 7062306a36Sopenharmony_ci t1 == the source word containing the null 7162306a36Sopenharmony_ci t8 == the cmpbge mask that found it. */ 7262306a36Sopenharmony_ci$a_eos: 7362306a36Sopenharmony_ci negq t8, t6 # e0 : find low bit set 7462306a36Sopenharmony_ci and t8, t6, t12 # e1 (stall) 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci /* For the sake of the cache, don't read a destination word 7762306a36Sopenharmony_ci if we're not going to need it. */ 7862306a36Sopenharmony_ci and t12, 0x80, t6 # e0 : 7962306a36Sopenharmony_ci bne t6, 1f # .. e1 (zdb) 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci /* We're doing a partial word store and so need to combine 8262306a36Sopenharmony_ci our source and original destination words. */ 8362306a36Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 8462306a36Sopenharmony_ci subq t12, 1, t6 # .. e1 : 8562306a36Sopenharmony_ci zapnot t1, t6, t1 # e0 : clear src bytes >= null 8662306a36Sopenharmony_ci or t12, t6, t8 # .. e1 : 8762306a36Sopenharmony_ci zap t0, t8, t0 # e0 : clear dst bytes <= null 8862306a36Sopenharmony_ci or t0, t1, t1 # e1 : 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci1: stq_u t1, 0(a0) # e0 : 9162306a36Sopenharmony_ci ret (t9) # .. e1 : 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci .end stxcpy_aligned 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci .align 3 9662306a36Sopenharmony_ci .ent __stxcpy 9762306a36Sopenharmony_ci .globl __stxcpy 9862306a36Sopenharmony_ci__stxcpy: 9962306a36Sopenharmony_ci .frame sp, 0, t9 10062306a36Sopenharmony_ci .prologue 0 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci /* Are source and destination co-aligned? */ 10362306a36Sopenharmony_ci xor a0, a1, t0 # e0 : 10462306a36Sopenharmony_ci unop # : 10562306a36Sopenharmony_ci and t0, 7, t0 # e0 : 10662306a36Sopenharmony_ci bne t0, $unaligned # .. e1 : 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci /* We are co-aligned; take care of a partial first word. */ 10962306a36Sopenharmony_ci ldq_u t1, 0(a1) # e0 : load first src word 11062306a36Sopenharmony_ci and a0, 7, t0 # .. e1 : take care not to load a word ... 11162306a36Sopenharmony_ci addq a1, 8, a1 # e0 : 11262306a36Sopenharmony_ci beq t0, stxcpy_aligned # .. e1 : ... if we wont need it 11362306a36Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 11462306a36Sopenharmony_ci br stxcpy_aligned # .. e1 : 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci/* The source and destination are not co-aligned. Align the destination 11862306a36Sopenharmony_ci and cope. We have to be very careful about not reading too much and 11962306a36Sopenharmony_ci causing a SEGV. */ 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci .align 3 12262306a36Sopenharmony_ci$u_head: 12362306a36Sopenharmony_ci /* We know just enough now to be able to assemble the first 12462306a36Sopenharmony_ci full source word. We can still find a zero at the end of it 12562306a36Sopenharmony_ci that prevents us from outputting the whole thing. 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci On entry to this basic block: 12862306a36Sopenharmony_ci t0 == the first dest word, for masking back in, if needed else 0 12962306a36Sopenharmony_ci t1 == the low bits of the first source word 13062306a36Sopenharmony_ci t6 == bytemask that is -1 in dest word bytes */ 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci ldq_u t2, 8(a1) # e0 : 13362306a36Sopenharmony_ci addq a1, 8, a1 # .. e1 : 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci extql t1, a1, t1 # e0 : 13662306a36Sopenharmony_ci extqh t2, a1, t4 # e0 : 13762306a36Sopenharmony_ci mskql t0, a0, t0 # e0 : 13862306a36Sopenharmony_ci or t1, t4, t1 # .. e1 : 13962306a36Sopenharmony_ci mskqh t1, a0, t1 # e0 : 14062306a36Sopenharmony_ci or t0, t1, t1 # e1 : 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci or t1, t6, t6 # e0 : 14362306a36Sopenharmony_ci cmpbge zero, t6, t8 # .. e1 : 14462306a36Sopenharmony_ci lda t6, -1 # e0 : for masking just below 14562306a36Sopenharmony_ci bne t8, $u_final # .. e1 : 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci mskql t6, a1, t6 # e0 : mask out the bits we have 14862306a36Sopenharmony_ci or t6, t2, t2 # e1 : already extracted before 14962306a36Sopenharmony_ci cmpbge zero, t2, t8 # e0 : testing eos 15062306a36Sopenharmony_ci bne t8, $u_late_head_exit # .. e1 (zdb) 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci /* Finally, we've got all the stupid leading edge cases taken care 15362306a36Sopenharmony_ci of and we can set up to enter the main loop. */ 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci stq_u t1, 0(a0) # e0 : store first output word 15662306a36Sopenharmony_ci addq a0, 8, a0 # .. e1 : 15762306a36Sopenharmony_ci extql t2, a1, t0 # e0 : position ho-bits of lo word 15862306a36Sopenharmony_ci ldq_u t2, 8(a1) # .. e1 : read next high-order source word 15962306a36Sopenharmony_ci addq a1, 8, a1 # e0 : 16062306a36Sopenharmony_ci cmpbge zero, t2, t8 # .. e1 : 16162306a36Sopenharmony_ci nop # e0 : 16262306a36Sopenharmony_ci bne t8, $u_eos # .. e1 : 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci /* Unaligned copy main loop. In order to avoid reading too much, 16562306a36Sopenharmony_ci the loop is structured to detect zeros in aligned source words. 16662306a36Sopenharmony_ci This has, unfortunately, effectively pulled half of a loop 16762306a36Sopenharmony_ci iteration out into the head and half into the tail, but it does 16862306a36Sopenharmony_ci prevent nastiness from accumulating in the very thing we want 16962306a36Sopenharmony_ci to run as fast as possible. 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci On entry to this basic block: 17262306a36Sopenharmony_ci t0 == the shifted high-order bits from the previous source word 17362306a36Sopenharmony_ci t2 == the unshifted current source word 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci We further know that t2 does not contain a null terminator. */ 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci .align 3 17862306a36Sopenharmony_ci$u_loop: 17962306a36Sopenharmony_ci extqh t2, a1, t1 # e0 : extract high bits for current word 18062306a36Sopenharmony_ci addq a1, 8, a1 # .. e1 : 18162306a36Sopenharmony_ci extql t2, a1, t3 # e0 : extract low bits for next time 18262306a36Sopenharmony_ci addq a0, 8, a0 # .. e1 : 18362306a36Sopenharmony_ci or t0, t1, t1 # e0 : current dst word now complete 18462306a36Sopenharmony_ci ldq_u t2, 0(a1) # .. e1 : load high word for next time 18562306a36Sopenharmony_ci stq_u t1, -8(a0) # e0 : save the current word 18662306a36Sopenharmony_ci mov t3, t0 # .. e1 : 18762306a36Sopenharmony_ci cmpbge zero, t2, t8 # e0 : test new word for eos 18862306a36Sopenharmony_ci beq t8, $u_loop # .. e1 : 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci /* We've found a zero somewhere in the source word we just read. 19162306a36Sopenharmony_ci If it resides in the lower half, we have one (probably partial) 19262306a36Sopenharmony_ci word to write out, and if it resides in the upper half, we 19362306a36Sopenharmony_ci have one full and one partial word left to write out. 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci On entry to this basic block: 19662306a36Sopenharmony_ci t0 == the shifted high-order bits from the previous source word 19762306a36Sopenharmony_ci t2 == the unshifted current source word. */ 19862306a36Sopenharmony_ci$u_eos: 19962306a36Sopenharmony_ci extqh t2, a1, t1 # e0 : 20062306a36Sopenharmony_ci or t0, t1, t1 # e1 : first (partial) source word complete 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci cmpbge zero, t1, t8 # e0 : is the null in this first bit? 20362306a36Sopenharmony_ci bne t8, $u_final # .. e1 (zdb) 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci$u_late_head_exit: 20662306a36Sopenharmony_ci stq_u t1, 0(a0) # e0 : the null was in the high-order bits 20762306a36Sopenharmony_ci addq a0, 8, a0 # .. e1 : 20862306a36Sopenharmony_ci extql t2, a1, t1 # e0 : 20962306a36Sopenharmony_ci cmpbge zero, t1, t8 # .. e1 : 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci /* Take care of a final (probably partial) result word. 21262306a36Sopenharmony_ci On entry to this basic block: 21362306a36Sopenharmony_ci t1 == assembled source word 21462306a36Sopenharmony_ci t8 == cmpbge mask that found the null. */ 21562306a36Sopenharmony_ci$u_final: 21662306a36Sopenharmony_ci negq t8, t6 # e0 : isolate low bit set 21762306a36Sopenharmony_ci and t6, t8, t12 # e1 : 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci and t12, 0x80, t6 # e0 : avoid dest word load if we can 22062306a36Sopenharmony_ci bne t6, 1f # .. e1 (zdb) 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 22362306a36Sopenharmony_ci subq t12, 1, t6 # .. e1 : 22462306a36Sopenharmony_ci or t6, t12, t8 # e0 : 22562306a36Sopenharmony_ci zapnot t1, t6, t1 # .. e1 : kill source bytes >= null 22662306a36Sopenharmony_ci zap t0, t8, t0 # e0 : kill dest bytes <= null 22762306a36Sopenharmony_ci or t0, t1, t1 # e1 : 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci1: stq_u t1, 0(a0) # e0 : 23062306a36Sopenharmony_ci ret (t9) # .. e1 : 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci /* Unaligned copy entry point. */ 23362306a36Sopenharmony_ci .align 3 23462306a36Sopenharmony_ci$unaligned: 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci ldq_u t1, 0(a1) # e0 : load first source word 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci and a0, 7, t4 # .. e1 : find dest misalignment 23962306a36Sopenharmony_ci and a1, 7, t5 # e0 : find src misalignment 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci /* Conditionally load the first destination word and a bytemask 24262306a36Sopenharmony_ci with 0xff indicating that the destination byte is sacrosanct. */ 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci mov zero, t0 # .. e1 : 24562306a36Sopenharmony_ci mov zero, t6 # e0 : 24662306a36Sopenharmony_ci beq t4, 1f # .. e1 : 24762306a36Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 24862306a36Sopenharmony_ci lda t6, -1 # .. e1 : 24962306a36Sopenharmony_ci mskql t6, a0, t6 # e0 : 25062306a36Sopenharmony_ci1: 25162306a36Sopenharmony_ci subq a1, t4, a1 # .. e1 : sub dest misalignment from src addr 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci /* If source misalignment is larger than dest misalignment, we need 25462306a36Sopenharmony_ci extra startup checks to avoid SEGV. */ 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci cmplt t4, t5, t12 # e0 : 25762306a36Sopenharmony_ci beq t12, $u_head # .. e1 (zdb) 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci lda t2, -1 # e1 : mask out leading garbage in source 26062306a36Sopenharmony_ci mskqh t2, t5, t2 # e0 : 26162306a36Sopenharmony_ci nop # e0 : 26262306a36Sopenharmony_ci ornot t1, t2, t3 # .. e1 : 26362306a36Sopenharmony_ci cmpbge zero, t3, t8 # e0 : is there a zero? 26462306a36Sopenharmony_ci beq t8, $u_head # .. e1 (zdb) 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci /* At this point we've found a zero in the first partial word of 26762306a36Sopenharmony_ci the source. We need to isolate the valid source data and mask 26862306a36Sopenharmony_ci it into the original destination data. (Incidentally, we know 26962306a36Sopenharmony_ci that we'll need at least one byte of that original dest word.) */ 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci ldq_u t0, 0(a0) # e0 : 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci negq t8, t6 # .. e1 : build bitmask of bytes <= zero 27462306a36Sopenharmony_ci and t6, t8, t12 # e0 : 27562306a36Sopenharmony_ci and a1, 7, t5 # .. e1 : 27662306a36Sopenharmony_ci subq t12, 1, t6 # e0 : 27762306a36Sopenharmony_ci or t6, t12, t8 # e1 : 27862306a36Sopenharmony_ci srl t12, t5, t12 # e0 : adjust final null return value 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci zapnot t2, t8, t2 # .. e1 : prepare source word; mirror changes 28162306a36Sopenharmony_ci and t1, t2, t1 # e1 : to source validity mask 28262306a36Sopenharmony_ci extql t2, a1, t2 # .. e0 : 28362306a36Sopenharmony_ci extql t1, a1, t1 # e0 : 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci andnot t0, t2, t0 # .. e1 : zero place for source to reside 28662306a36Sopenharmony_ci or t0, t1, t1 # e1 : and put it there 28762306a36Sopenharmony_ci stq_u t1, 0(a0) # .. e0 : 28862306a36Sopenharmony_ci ret (t9) # e1 : 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci .end __stxcpy 291