1 /* SPDX-License-Identifier: GPL-2.0 */
3 * arch/alpha/lib/stxncpy.S
4 * Contributed by Richard Henderson (rth@tamu.edu)
6 * Copy no more than COUNT bytes of the null-terminated string from
9 * This is an internal routine used by strncpy, stpncpy, and strncat.
10 * As such, it uses special linkage conventions to make implementation
11 * of these public functions more efficient.
19 * Furthermore, COUNT may not be zero.
22 * t0 = last word written
23 * t10 = bitmask (with one bit set) indicating the byte position of
24 * the end of the range specified by COUNT
25 * t12 = bitmask (with one bit set) indicating the last byte written
26 * a0 = unaligned address of the last *word* written
27 * a2 = the number of full words left in COUNT
29 * Furthermore, v0, a3-a5, t11, and $at are untouched.
32 #include <asm/regdef.h>
39 /* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that
40 doesn't like putting the entry point for a procedure somewhere in the
41 middle of the procedure descriptor. Work around this by putting the
42 aligned copy in its own procedure descriptor */
50 /* On entry to this basic block:
51 t0 == the first destination word for masking back in
52 t1 == the first source word. */
54 /* Create the 1st output word and detect 0's in the 1st input word. */
55 lda t2, -1 # e1 : build a mask against false zero
56 mskqh t2, a1, t2 # e0 : detection in the src word
57 mskqh t1, a1, t3 # e0 :
58 ornot t1, t2, t2 # .. e1 :
59 mskql t0, a1, t0 # e0 : assemble the first output word
60 cmpbge zero, t2, t8 # .. e1 : bits set iff null found
62 beq a2, $a_eoc # .. e1 :
63 bne t8, $a_eos # .. e1 :
65 /* On entry to this basic block:
66 t0 == a source word not containing a null. */
69 stq_u t0, 0(a0) # e0 :
70 addq a0, 8, a0 # .. e1 :
71 ldq_u t0, 0(a1) # e0 :
72 addq a1, 8, a1 # .. e1 :
74 cmpbge zero, t0, t8 # .. e1 (stall)
76 beq t8, $a_loop # e1 :
78 /* Take care of the final (partial) word store. At this point
79 the end-of-count bit is set in t8 iff it applies.
81 On entry to this basic block we have:
82 t0 == the source word containing the null
83 t8 == the cmpbge mask that found it. */
86 negq t8, t12 # e0 : find low bit set
87 and t8, t12, t12 # e1 (stall)
89 /* For the sake of the cache, don't read a destination word
90 if we're not going to need it. */
91 and t12, 0x80, t6 # e0 :
92 bne t6, 1f # .. e1 (zdb)
94 /* We're doing a partial word store and so need to combine
95 our source and original destination words. */
96 ldq_u t1, 0(a0) # e0 :
97 subq t12, 1, t6 # .. e1 :
100 zapnot t0, t8, t0 # e0 : clear src bytes > null
101 zap t1, t8, t1 # .. e1 : clear dst bytes <= null
104 1: stq_u t0, 0(a0) # e0 :
107 /* Add the end-of-count bit to the eos detection bitmask. */
121 /* Are source and destination co-aligned? */
122 xor a0, a1, t1 # e0 :
123 and a0, 7, t0 # .. e1 : find dest misalignment
125 addq a2, t0, a2 # .. e1 : bias count by dest misalignment
126 subq a2, 1, a2 # e0 :
128 srl a2, 3, a2 # e0 : a2 = loop counter = (count - 1)/8
129 addq zero, 1, t10 # .. e1 :
130 sll t10, t2, t10 # e0 : t10 = bitmask of last count byte
131 bne t1, $unaligned # .. e1 :
133 /* We are co-aligned; take care of a partial first word. */
135 ldq_u t1, 0(a1) # e0 : load first src word
136 addq a1, 8, a1 # .. e1 :
138 beq t0, stxncpy_aligned # avoid loading dest word if not needed
139 ldq_u t0, 0(a0) # e0 :
140 br stxncpy_aligned # .. e1 :
143 /* The source and destination are not co-aligned. Align the destination
144 and cope. We have to be very careful about not reading too much and
149 /* We know just enough now to be able to assemble the first
150 full source word. We can still find a zero at the end of it
151 that prevents us from outputting the whole thing.
153 On entry to this basic block:
154 t0 == the first dest word, unmasked
155 t1 == the shifted low bits of the first source word
156 t6 == bytemask that is -1 in dest word bytes */
158 ldq_u t2, 8(a1) # e0 : load second src word
159 addq a1, 8, a1 # .. e1 :
160 mskql t0, a0, t0 # e0 : mask trailing garbage in dst
161 extqh t2, a1, t4 # e0 :
162 or t1, t4, t1 # e1 : first aligned src word complete
163 mskqh t1, a0, t1 # e0 : mask leading garbage in src
164 or t0, t1, t0 # e0 : first output word complete
165 or t0, t6, t6 # e1 : mask original data for zero test
166 cmpbge zero, t6, t8 # e0 :
167 beq a2, $u_eocfin # .. e1 :
169 bne t8, $u_final # .. e1 :
171 mskql t6, a1, t6 # e0 : mask out bits already seen
173 stq_u t0, 0(a0) # e0 : store first output word
174 or t6, t2, t2 # .. e1 :
175 cmpbge zero, t2, t8 # e0 : find nulls in second partial
176 addq a0, 8, a0 # .. e1 :
177 subq a2, 1, a2 # e0 :
178 bne t8, $u_late_head_exit # .. e1 :
180 /* Finally, we've got all the stupid leading edge cases taken care
181 of and we can set up to enter the main loop. */
183 extql t2, a1, t1 # e0 : position hi-bits of lo word
184 beq a2, $u_eoc # .. e1 :
185 ldq_u t2, 8(a1) # e0 : read next high-order source word
186 addq a1, 8, a1 # .. e1 :
187 extqh t2, a1, t0 # e0 : position lo-bits of hi word (stall)
188 cmpbge zero, t2, t8 # .. e1 :
190 bne t8, $u_eos # .. e1 :
192 /* Unaligned copy main loop. In order to avoid reading too much,
193 the loop is structured to detect zeros in aligned source words.
194 This has, unfortunately, effectively pulled half of a loop
195 iteration out into the head and half into the tail, but it does
196 prevent nastiness from accumulating in the very thing we want
197 to run as fast as possible.
199 On entry to this basic block:
200 t0 == the shifted low-order bits from the current source word
201 t1 == the shifted high-order bits from the previous source word
202 t2 == the unshifted current source word
204 We further know that t2 does not contain a null terminator. */
208 or t0, t1, t0 # e0 : current dst word now complete
209 subq a2, 1, a2 # .. e1 : decrement word count
210 stq_u t0, 0(a0) # e0 : save the current word
211 addq a0, 8, a0 # .. e1 :
212 extql t2, a1, t1 # e0 : extract high bits for next time
213 beq a2, $u_eoc # .. e1 :
214 ldq_u t2, 8(a1) # e0 : load high word for next time
215 addq a1, 8, a1 # .. e1 :
217 cmpbge zero, t2, t8 # e1 : test new word for eos (stall)
218 extqh t2, a1, t0 # e0 : extract low bits for current word
219 beq t8, $u_loop # .. e1 :
221 /* We've found a zero somewhere in the source word we just read.
222 If it resides in the lower half, we have one (probably partial)
223 word to write out, and if it resides in the upper half, we
224 have one full and one partial word left to write out.
226 On entry to this basic block:
227 t0 == the shifted low-order bits from the current source word
228 t1 == the shifted high-order bits from the previous source word
229 t2 == the unshifted current source word. */
231 or t0, t1, t0 # e0 : first (partial) source word complete
233 cmpbge zero, t0, t8 # e0 : is the null in this first bit?
234 bne t8, $u_final # .. e1 (zdb)
236 stq_u t0, 0(a0) # e0 : the null was in the high-order bits
237 addq a0, 8, a0 # .. e1 :
238 subq a2, 1, a2 # e1 :
241 extql t2, a1, t0 # .. e0 :
242 cmpbge zero, t0, t8 # e0 :
243 or t8, t10, t6 # e1 :
244 cmoveq a2, t6, t8 # e0 :
247 /* Take care of a final (probably partial) result word.
248 On entry to this basic block:
249 t0 == assembled source word
250 t8 == cmpbge mask that found the null. */
252 negq t8, t6 # e0 : isolate low bit set
253 and t6, t8, t12 # e1 :
255 and t12, 0x80, t6 # e0 : avoid dest word load if we can
256 bne t6, 1f # .. e1 (zdb)
258 ldq_u t1, 0(a0) # e0 :
259 subq t12, 1, t6 # .. e1 :
260 or t6, t12, t8 # e0 :
261 zapnot t0, t8, t0 # .. e1 : kill source bytes > null
262 zap t1, t8, t1 # e0 : kill dest bytes <= null
265 1: stq_u t0, 0(a0) # e0 :
268 /* Got to end-of-count before end of string.
269 On entry to this basic block:
270 t1 == the shifted high-order bits from the previous source word */
273 sll t10, t6, t6 # e0 :
274 and t6, 0xff, t6 # e0 :
277 ldq_u t2, 8(a1) # e0 : load final src word
279 extqh t2, a1, t0 # e0 : extract low bits for last word
282 1: cmpbge zero, t1, t8
285 $u_eocfin: # end-of-count, final word
289 /* Unaligned copy entry point. */
293 ldq_u t1, 0(a1) # e0 : load first source word
295 and a0, 7, t4 # .. e1 : find dest misalignment
296 and a1, 7, t5 # e0 : find src misalignment
298 /* Conditionally load the first destination word and a bytemask
299 with 0xff indicating that the destination byte is sacrosanct. */
301 mov zero, t0 # .. e1 :
304 ldq_u t0, 0(a0) # e0 :
306 mskql t6, a0, t6 # e0 :
307 subq a1, t4, a1 # .. e1 : sub dest misalignment from src addr
309 /* If source misalignment is larger than dest misalignment, we need
310 extra startup checks to avoid SEGV. */
312 1: cmplt t4, t5, t12 # e1 :
313 extql t1, a1, t1 # .. e0 : shift src into place
314 lda t2, -1 # e0 : for creating masks later
315 beq t12, $u_head # .. e1 :
317 extql t2, a1, t2 # e0 :
318 cmpbge zero, t1, t8 # .. e1 : is there a zero?
319 andnot t2, t6, t2 # e0 : dest mask for a single word copy
320 or t8, t10, t5 # .. e1 : test for end-of-count too
321 cmpbge zero, t2, t3 # e0 :
322 cmoveq a2, t5, t8 # .. e1 :
323 andnot t8, t3, t8 # e0 :
324 beq t8, $u_head # .. e1 (zdb)
326 /* At this point we've found a zero in the first partial word of
327 the source. We need to isolate the valid source data and mask
328 it into the original destination data. (Incidentally, we know
329 that we'll need at least one byte of that original dest word.) */
331 ldq_u t0, 0(a0) # e0 :
332 negq t8, t6 # .. e1 : build bitmask of bytes <= zero
333 mskqh t1, t4, t1 # e0 :
334 and t6, t8, t12 # .. e1 :
335 subq t12, 1, t6 # e0 :
336 or t6, t12, t8 # e1 :
338 zapnot t2, t8, t2 # e0 : prepare source word; mirror changes
339 zapnot t1, t8, t1 # .. e1 : to source validity mask
341 andnot t0, t2, t0 # e0 : zero place for source to reside
342 or t0, t1, t0 # e1 : and put it there
343 stq_u t0, 0(a0) # e0 :