1*4882a593Smuzhiyun/* SPDX-License-Identifier: GPL-2.0 */ 2*4882a593Smuzhiyun/* 3*4882a593Smuzhiyun * arch/alpha/lib/stxncpy.S 4*4882a593Smuzhiyun * Contributed by Richard Henderson (rth@tamu.edu) 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Copy no more than COUNT bytes of the null-terminated string from 7*4882a593Smuzhiyun * SRC to DST. 8*4882a593Smuzhiyun * 9*4882a593Smuzhiyun * This is an internal routine used by strncpy, stpncpy, and strncat. 10*4882a593Smuzhiyun * As such, it uses special linkage conventions to make implementation 11*4882a593Smuzhiyun * of these public functions more efficient. 12*4882a593Smuzhiyun * 13*4882a593Smuzhiyun * On input: 14*4882a593Smuzhiyun * t9 = return address 15*4882a593Smuzhiyun * a0 = DST 16*4882a593Smuzhiyun * a1 = SRC 17*4882a593Smuzhiyun * a2 = COUNT 18*4882a593Smuzhiyun * 19*4882a593Smuzhiyun * Furthermore, COUNT may not be zero. 20*4882a593Smuzhiyun * 21*4882a593Smuzhiyun * On output: 22*4882a593Smuzhiyun * t0 = last word written 23*4882a593Smuzhiyun * t10 = bitmask (with one bit set) indicating the byte position of 24*4882a593Smuzhiyun * the end of the range specified by COUNT 25*4882a593Smuzhiyun * t12 = bitmask (with one bit set) indicating the last byte written 26*4882a593Smuzhiyun * a0 = unaligned address of the last *word* written 27*4882a593Smuzhiyun * a2 = the number of full words left in COUNT 28*4882a593Smuzhiyun * 29*4882a593Smuzhiyun * Furthermore, v0, a3-a5, t11, and $at are untouched. 30*4882a593Smuzhiyun */ 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun#include <asm/regdef.h> 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun .set noat 35*4882a593Smuzhiyun .set noreorder 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun .text 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun/* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that 40*4882a593Smuzhiyun doesn't like putting the entry point for a procedure somewhere in the 41*4882a593Smuzhiyun middle of the procedure descriptor. Work around this by putting the 42*4882a593Smuzhiyun aligned copy in its own procedure descriptor */ 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun .ent stxncpy_aligned 45*4882a593Smuzhiyun .align 3 46*4882a593Smuzhiyunstxncpy_aligned: 47*4882a593Smuzhiyun .frame sp, 0, t9, 0 48*4882a593Smuzhiyun .prologue 0 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun /* On entry to this basic block: 51*4882a593Smuzhiyun t0 == the first destination word for masking back in 52*4882a593Smuzhiyun t1 == the first source word. */ 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun /* Create the 1st output word and detect 0's in the 1st input word. */ 55*4882a593Smuzhiyun lda t2, -1 # e1 : build a mask against false zero 56*4882a593Smuzhiyun mskqh t2, a1, t2 # e0 : detection in the src word 57*4882a593Smuzhiyun mskqh t1, a1, t3 # e0 : 58*4882a593Smuzhiyun ornot t1, t2, t2 # .. e1 : 59*4882a593Smuzhiyun mskql t0, a1, t0 # e0 : assemble the first output word 60*4882a593Smuzhiyun cmpbge zero, t2, t8 # .. e1 : bits set iff null found 61*4882a593Smuzhiyun or t0, t3, t0 # e0 : 62*4882a593Smuzhiyun beq a2, $a_eoc # .. e1 : 63*4882a593Smuzhiyun bne t8, $a_eos # .. e1 : 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun /* On entry to this basic block: 66*4882a593Smuzhiyun t0 == a source word not containing a null. */ 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun$a_loop: 69*4882a593Smuzhiyun stq_u t0, 0(a0) # e0 : 70*4882a593Smuzhiyun addq a0, 8, a0 # .. e1 : 71*4882a593Smuzhiyun ldq_u t0, 0(a1) # e0 : 72*4882a593Smuzhiyun addq a1, 8, a1 # .. e1 : 73*4882a593Smuzhiyun subq a2, 1, a2 # e0 : 74*4882a593Smuzhiyun cmpbge zero, t0, t8 # .. e1 (stall) 75*4882a593Smuzhiyun beq a2, $a_eoc # e1 : 76*4882a593Smuzhiyun beq t8, $a_loop # e1 : 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun /* Take care of the final (partial) word store. At this point 79*4882a593Smuzhiyun the end-of-count bit is set in t8 iff it applies. 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun On entry to this basic block we have: 82*4882a593Smuzhiyun t0 == the source word containing the null 83*4882a593Smuzhiyun t8 == the cmpbge mask that found it. */ 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun$a_eos: 86*4882a593Smuzhiyun negq t8, t12 # e0 : find low bit set 87*4882a593Smuzhiyun and t8, t12, t12 # e1 (stall) 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun /* For the sake of the cache, don't read a destination word 90*4882a593Smuzhiyun if we're not going to need it. */ 91*4882a593Smuzhiyun and t12, 0x80, t6 # e0 : 92*4882a593Smuzhiyun bne t6, 1f # .. e1 (zdb) 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun /* We're doing a partial word store and so need to combine 95*4882a593Smuzhiyun our source and original destination words. */ 96*4882a593Smuzhiyun ldq_u t1, 0(a0) # e0 : 97*4882a593Smuzhiyun subq t12, 1, t6 # .. e1 : 98*4882a593Smuzhiyun or t12, t6, t8 # e0 : 99*4882a593Smuzhiyun unop # 100*4882a593Smuzhiyun zapnot t0, t8, t0 # e0 : clear src bytes > null 101*4882a593Smuzhiyun zap t1, t8, t1 # .. e1 : clear dst bytes <= null 102*4882a593Smuzhiyun or t0, t1, t0 # e1 : 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun1: stq_u t0, 0(a0) # e0 : 105*4882a593Smuzhiyun ret (t9) # e1 : 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun /* Add the end-of-count bit to the eos detection bitmask. */ 108*4882a593Smuzhiyun$a_eoc: 109*4882a593Smuzhiyun or t10, t8, t8 110*4882a593Smuzhiyun br $a_eos 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun .end stxncpy_aligned 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun .align 3 115*4882a593Smuzhiyun .ent __stxncpy 116*4882a593Smuzhiyun .globl __stxncpy 117*4882a593Smuzhiyun__stxncpy: 118*4882a593Smuzhiyun .frame sp, 0, t9, 0 119*4882a593Smuzhiyun .prologue 0 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun /* Are source and destination co-aligned? */ 122*4882a593Smuzhiyun xor a0, a1, t1 # e0 : 123*4882a593Smuzhiyun and a0, 7, t0 # .. e1 : find dest misalignment 124*4882a593Smuzhiyun and t1, 7, t1 # e0 : 125*4882a593Smuzhiyun addq a2, t0, a2 # .. e1 : bias count by dest misalignment 126*4882a593Smuzhiyun subq a2, 1, a2 # e0 : 127*4882a593Smuzhiyun and a2, 7, t2 # e1 : 128*4882a593Smuzhiyun srl a2, 3, a2 # e0 : a2 = loop counter = (count - 1)/8 129*4882a593Smuzhiyun addq zero, 1, t10 # .. e1 : 130*4882a593Smuzhiyun sll t10, t2, t10 # e0 : t10 = bitmask of last count byte 131*4882a593Smuzhiyun bne t1, $unaligned # .. e1 : 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun /* We are co-aligned; take care of a partial first word. */ 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun ldq_u t1, 0(a1) # e0 : load first src word 136*4882a593Smuzhiyun addq a1, 8, a1 # .. e1 : 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun beq t0, stxncpy_aligned # avoid loading dest word if not needed 139*4882a593Smuzhiyun ldq_u t0, 0(a0) # e0 : 140*4882a593Smuzhiyun br stxncpy_aligned # .. e1 : 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun/* The source and destination are not co-aligned. Align the destination 144*4882a593Smuzhiyun and cope. We have to be very careful about not reading too much and 145*4882a593Smuzhiyun causing a SEGV. */ 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun .align 3 148*4882a593Smuzhiyun$u_head: 149*4882a593Smuzhiyun /* We know just enough now to be able to assemble the first 150*4882a593Smuzhiyun full source word. We can still find a zero at the end of it 151*4882a593Smuzhiyun that prevents us from outputting the whole thing. 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun On entry to this basic block: 154*4882a593Smuzhiyun t0 == the first dest word, unmasked 155*4882a593Smuzhiyun t1 == the shifted low bits of the first source word 156*4882a593Smuzhiyun t6 == bytemask that is -1 in dest word bytes */ 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun ldq_u t2, 8(a1) # e0 : load second src word 159*4882a593Smuzhiyun addq a1, 8, a1 # .. e1 : 160*4882a593Smuzhiyun mskql t0, a0, t0 # e0 : mask trailing garbage in dst 161*4882a593Smuzhiyun extqh t2, a1, t4 # e0 : 162*4882a593Smuzhiyun or t1, t4, t1 # e1 : first aligned src word complete 163*4882a593Smuzhiyun mskqh t1, a0, t1 # e0 : mask leading garbage in src 164*4882a593Smuzhiyun or t0, t1, t0 # e0 : first output word complete 165*4882a593Smuzhiyun or t0, t6, t6 # e1 : mask original data for zero test 166*4882a593Smuzhiyun cmpbge zero, t6, t8 # e0 : 167*4882a593Smuzhiyun beq a2, $u_eocfin # .. e1 : 168*4882a593Smuzhiyun lda t6, -1 # e0 : 169*4882a593Smuzhiyun bne t8, $u_final # .. e1 : 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun mskql t6, a1, t6 # e0 : mask out bits already seen 172*4882a593Smuzhiyun nop # .. e1 : 173*4882a593Smuzhiyun stq_u t0, 0(a0) # e0 : store first output word 174*4882a593Smuzhiyun or t6, t2, t2 # .. e1 : 175*4882a593Smuzhiyun cmpbge zero, t2, t8 # e0 : find nulls in second partial 176*4882a593Smuzhiyun addq a0, 8, a0 # .. e1 : 177*4882a593Smuzhiyun subq a2, 1, a2 # e0 : 178*4882a593Smuzhiyun bne t8, $u_late_head_exit # .. e1 : 179*4882a593Smuzhiyun 180*4882a593Smuzhiyun /* Finally, we've got all the stupid leading edge cases taken care 181*4882a593Smuzhiyun of and we can set up to enter the main loop. */ 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun extql t2, a1, t1 # e0 : position hi-bits of lo word 184*4882a593Smuzhiyun beq a2, $u_eoc # .. e1 : 185*4882a593Smuzhiyun ldq_u t2, 8(a1) # e0 : read next high-order source word 186*4882a593Smuzhiyun addq a1, 8, a1 # .. e1 : 187*4882a593Smuzhiyun extqh t2, a1, t0 # e0 : position lo-bits of hi word (stall) 188*4882a593Smuzhiyun cmpbge zero, t2, t8 # .. e1 : 189*4882a593Smuzhiyun nop # e0 : 190*4882a593Smuzhiyun bne t8, $u_eos # .. e1 : 191*4882a593Smuzhiyun 192*4882a593Smuzhiyun /* Unaligned copy main loop. In order to avoid reading too much, 193*4882a593Smuzhiyun the loop is structured to detect zeros in aligned source words. 194*4882a593Smuzhiyun This has, unfortunately, effectively pulled half of a loop 195*4882a593Smuzhiyun iteration out into the head and half into the tail, but it does 196*4882a593Smuzhiyun prevent nastiness from accumulating in the very thing we want 197*4882a593Smuzhiyun to run as fast as possible. 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun On entry to this basic block: 200*4882a593Smuzhiyun t0 == the shifted low-order bits from the current source word 201*4882a593Smuzhiyun t1 == the shifted high-order bits from the previous source word 202*4882a593Smuzhiyun t2 == the unshifted current source word 203*4882a593Smuzhiyun 204*4882a593Smuzhiyun We further know that t2 does not contain a null terminator. */ 205*4882a593Smuzhiyun 206*4882a593Smuzhiyun .align 3 207*4882a593Smuzhiyun$u_loop: 208*4882a593Smuzhiyun or t0, t1, t0 # e0 : current dst word now complete 209*4882a593Smuzhiyun subq a2, 1, a2 # .. e1 : decrement word count 210*4882a593Smuzhiyun stq_u t0, 0(a0) # e0 : save the current word 211*4882a593Smuzhiyun addq a0, 8, a0 # .. e1 : 212*4882a593Smuzhiyun extql t2, a1, t1 # e0 : extract high bits for next time 213*4882a593Smuzhiyun beq a2, $u_eoc # .. e1 : 214*4882a593Smuzhiyun ldq_u t2, 8(a1) # e0 : load high word for next time 215*4882a593Smuzhiyun addq a1, 8, a1 # .. e1 : 216*4882a593Smuzhiyun nop # e0 : 217*4882a593Smuzhiyun cmpbge zero, t2, t8 # e1 : test new word for eos (stall) 218*4882a593Smuzhiyun extqh t2, a1, t0 # e0 : extract low bits for current word 219*4882a593Smuzhiyun beq t8, $u_loop # .. e1 : 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun /* We've found a zero somewhere in the source word we just read. 222*4882a593Smuzhiyun If it resides in the lower half, we have one (probably partial) 223*4882a593Smuzhiyun word to write out, and if it resides in the upper half, we 224*4882a593Smuzhiyun have one full and one partial word left to write out. 225*4882a593Smuzhiyun 226*4882a593Smuzhiyun On entry to this basic block: 227*4882a593Smuzhiyun t0 == the shifted low-order bits from the current source word 228*4882a593Smuzhiyun t1 == the shifted high-order bits from the previous source word 229*4882a593Smuzhiyun t2 == the unshifted current source word. */ 230*4882a593Smuzhiyun$u_eos: 231*4882a593Smuzhiyun or t0, t1, t0 # e0 : first (partial) source word complete 232*4882a593Smuzhiyun nop # .. e1 : 233*4882a593Smuzhiyun cmpbge zero, t0, t8 # e0 : is the null in this first bit? 234*4882a593Smuzhiyun bne t8, $u_final # .. e1 (zdb) 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun stq_u t0, 0(a0) # e0 : the null was in the high-order bits 237*4882a593Smuzhiyun addq a0, 8, a0 # .. e1 : 238*4882a593Smuzhiyun subq a2, 1, a2 # e1 : 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun$u_late_head_exit: 241*4882a593Smuzhiyun extql t2, a1, t0 # .. e0 : 242*4882a593Smuzhiyun cmpbge zero, t0, t8 # e0 : 243*4882a593Smuzhiyun or t8, t10, t6 # e1 : 244*4882a593Smuzhiyun cmoveq a2, t6, t8 # e0 : 245*4882a593Smuzhiyun nop # .. e1 : 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun /* Take care of a final (probably partial) result word. 248*4882a593Smuzhiyun On entry to this basic block: 249*4882a593Smuzhiyun t0 == assembled source word 250*4882a593Smuzhiyun t8 == cmpbge mask that found the null. */ 251*4882a593Smuzhiyun$u_final: 252*4882a593Smuzhiyun negq t8, t6 # e0 : isolate low bit set 253*4882a593Smuzhiyun and t6, t8, t12 # e1 : 254*4882a593Smuzhiyun 255*4882a593Smuzhiyun and t12, 0x80, t6 # e0 : avoid dest word load if we can 256*4882a593Smuzhiyun bne t6, 1f # .. e1 (zdb) 257*4882a593Smuzhiyun 258*4882a593Smuzhiyun ldq_u t1, 0(a0) # e0 : 259*4882a593Smuzhiyun subq t12, 1, t6 # .. e1 : 260*4882a593Smuzhiyun or t6, t12, t8 # e0 : 261*4882a593Smuzhiyun zapnot t0, t8, t0 # .. e1 : kill source bytes > null 262*4882a593Smuzhiyun zap t1, t8, t1 # e0 : kill dest bytes <= null 263*4882a593Smuzhiyun or t0, t1, t0 # e1 : 264*4882a593Smuzhiyun 265*4882a593Smuzhiyun1: stq_u t0, 0(a0) # e0 : 266*4882a593Smuzhiyun ret (t9) # .. e1 : 267*4882a593Smuzhiyun 268*4882a593Smuzhiyun /* Got to end-of-count before end of string. 269*4882a593Smuzhiyun On entry to this basic block: 270*4882a593Smuzhiyun t1 == the shifted high-order bits from the previous source word */ 271*4882a593Smuzhiyun$u_eoc: 272*4882a593Smuzhiyun and a1, 7, t6 # e1 : 273*4882a593Smuzhiyun sll t10, t6, t6 # e0 : 274*4882a593Smuzhiyun and t6, 0xff, t6 # e0 : 275*4882a593Smuzhiyun bne t6, 1f # .. e1 : 276*4882a593Smuzhiyun 277*4882a593Smuzhiyun ldq_u t2, 8(a1) # e0 : load final src word 278*4882a593Smuzhiyun nop # .. e1 : 279*4882a593Smuzhiyun extqh t2, a1, t0 # e0 : extract low bits for last word 280*4882a593Smuzhiyun or t1, t0, t1 # e1 : 281*4882a593Smuzhiyun 282*4882a593Smuzhiyun1: cmpbge zero, t1, t8 283*4882a593Smuzhiyun mov t1, t0 284*4882a593Smuzhiyun 285*4882a593Smuzhiyun$u_eocfin: # end-of-count, final word 286*4882a593Smuzhiyun or t10, t8, t8 287*4882a593Smuzhiyun br $u_final 288*4882a593Smuzhiyun 289*4882a593Smuzhiyun /* Unaligned copy entry point. */ 290*4882a593Smuzhiyun .align 3 291*4882a593Smuzhiyun$unaligned: 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun ldq_u t1, 0(a1) # e0 : load first source word 294*4882a593Smuzhiyun 295*4882a593Smuzhiyun and a0, 7, t4 # .. e1 : find dest misalignment 296*4882a593Smuzhiyun and a1, 7, t5 # e0 : find src misalignment 297*4882a593Smuzhiyun 298*4882a593Smuzhiyun /* Conditionally load the first destination word and a bytemask 299*4882a593Smuzhiyun with 0xff indicating that the destination byte is sacrosanct. */ 300*4882a593Smuzhiyun 301*4882a593Smuzhiyun mov zero, t0 # .. e1 : 302*4882a593Smuzhiyun mov zero, t6 # e0 : 303*4882a593Smuzhiyun beq t4, 1f # .. e1 : 304*4882a593Smuzhiyun ldq_u t0, 0(a0) # e0 : 305*4882a593Smuzhiyun lda t6, -1 # .. e1 : 306*4882a593Smuzhiyun mskql t6, a0, t6 # e0 : 307*4882a593Smuzhiyun subq a1, t4, a1 # .. e1 : sub dest misalignment from src addr 308*4882a593Smuzhiyun 309*4882a593Smuzhiyun /* If source misalignment is larger than dest misalignment, we need 310*4882a593Smuzhiyun extra startup checks to avoid SEGV. */ 311*4882a593Smuzhiyun 312*4882a593Smuzhiyun1: cmplt t4, t5, t12 # e1 : 313*4882a593Smuzhiyun extql t1, a1, t1 # .. e0 : shift src into place 314*4882a593Smuzhiyun lda t2, -1 # e0 : for creating masks later 315*4882a593Smuzhiyun beq t12, $u_head # .. e1 : 316*4882a593Smuzhiyun 317*4882a593Smuzhiyun extql t2, a1, t2 # e0 : 318*4882a593Smuzhiyun cmpbge zero, t1, t8 # .. e1 : is there a zero? 319*4882a593Smuzhiyun andnot t2, t6, t2 # e0 : dest mask for a single word copy 320*4882a593Smuzhiyun or t8, t10, t5 # .. e1 : test for end-of-count too 321*4882a593Smuzhiyun cmpbge zero, t2, t3 # e0 : 322*4882a593Smuzhiyun cmoveq a2, t5, t8 # .. e1 : 323*4882a593Smuzhiyun andnot t8, t3, t8 # e0 : 324*4882a593Smuzhiyun beq t8, $u_head # .. e1 (zdb) 325*4882a593Smuzhiyun 326*4882a593Smuzhiyun /* At this point we've found a zero in the first partial word of 327*4882a593Smuzhiyun the source. We need to isolate the valid source data and mask 328*4882a593Smuzhiyun it into the original destination data. (Incidentally, we know 329*4882a593Smuzhiyun that we'll need at least one byte of that original dest word.) */ 330*4882a593Smuzhiyun 331*4882a593Smuzhiyun ldq_u t0, 0(a0) # e0 : 332*4882a593Smuzhiyun negq t8, t6 # .. e1 : build bitmask of bytes <= zero 333*4882a593Smuzhiyun mskqh t1, t4, t1 # e0 : 334*4882a593Smuzhiyun and t6, t8, t12 # .. e1 : 335*4882a593Smuzhiyun subq t12, 1, t6 # e0 : 336*4882a593Smuzhiyun or t6, t12, t8 # e1 : 337*4882a593Smuzhiyun 338*4882a593Smuzhiyun zapnot t2, t8, t2 # e0 : prepare source word; mirror changes 339*4882a593Smuzhiyun zapnot t1, t8, t1 # .. e1 : to source validity mask 340*4882a593Smuzhiyun 341*4882a593Smuzhiyun andnot t0, t2, t0 # e0 : zero place for source to reside 342*4882a593Smuzhiyun or t0, t1, t0 # e1 : and put it there 343*4882a593Smuzhiyun stq_u t0, 0(a0) # e0 : 344*4882a593Smuzhiyun ret (t9) # .. e1 : 345*4882a593Smuzhiyun 346*4882a593Smuzhiyun .end __stxncpy 347