xref: /titanic_52/usr/src/lib/libc/sparcv9/gen/strlcpy.s (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31/*
32 * The strlcpy() function copies at most dstsize-1 characters
33 * (dstsize being the size of the string buffer dst) from src
34 * to dst, truncating src if necessary. The result is always
35 * null-terminated.  The function returns strlen(src). Buffer
36 * overflow can be checked as follows:
37 *
38 *   if (strlcpy(dst, src, dstsize) >= dstsize)
39 *           return -1;
40 */
41
42#include <sys/asm_linkage.h>
43
44	! strlcpy implementation is similar to that of strcpy, except
45	! in this case, the maximum size of the detination must be
46	! tracked since it bounds our maximum copy size.  However,
47	! we must still continue to check for zero since the routine
48	! is expected to null-terminate any string that is within
49	! the dest size bound.
50	!
51	! this method starts by checking for and arranging source alignment.
52	! Once this has occurred, we copy based upon destination alignment.
53	! This is either by xword, word, halfword, or byte.  As this occurs, we
54	! check for a zero-byte.  If one is found, we branch to a method
55	! which checks for the exact location of a zero-byte within a
56	! larger xword/word/half-word quantity.
57
58
59	ENTRY(strlcpy)
60
61	.align 32
62
63	save	%sp, -SA(WINDOWSIZE), %sp
64	subcc	%g0, %i2, %g4		! n = -n, n == 0 ?
65	bz,pn	%ncc, .getstrlen	! n == 0, must determine strlen
66	add	%i1, %i2, %i3		! src = src + n
67	andcc	%i1, 7, %i4		! src dword aligned ?
68	bz,pn	%ncc, .dwordaligned	! yup
69	add	%i0, %i2, %i2		! dst = dst + n
70	sub	%i4, 8, %i4		! bytes until src aligned
71
72.alignsrc:
73	ldub	[%i3 + %g4], %l1	! src[]
74	andcc	%l1, 0xff, %g0		! end of src reached (null byte) ?
75	stub	%l1, [%i2 + %g4]	! dst[] = src[]
76	bz,a	%ncc, .done		! yes, done
77	add 	%i2, %g4, %i2		! need single dest pointer for strlen
78	addcc	%g4, 1, %g4		! src++, dst++, n--
79	bz,pn	%ncc, .forcenullunalign	! n == 0, force null byte, compute len
80	addcc	%i4, 1, %i4		! src aligned now?
81	bnz,a	%ncc, .alignsrc		! no, copy another byte
82	nop				! pad
83
84.dwordaligned:
85	sethi	%hi(0x01010101), %i4	! Alan Mycroft's magic1
86	add	%i2, %g4, %l0		! dst
87	or	%i4, %lo(0x01010101),%i4!  finish loading magic1
88	and	%l0, 3, %g1		! dst<1:0> to examine offset
89	sllx	%i4, 32, %l1		! spread magic1
90	cmp	%g1, 1			! dst offset of 1 or 5
91	or	%i4, %l1, %i4		!   to all 64 bits
92	sub	%i2, 8, %i2		! adjust for dest pre-incr in cpy loops
93	be,pn	%ncc, .storebyte1241	! store 1, 2, 4, 1 bytes
94	sllx	%i4, 7, %i5		!  Alan Mycroft's magic2
95	cmp	%g1, 3			! dst offset of 3 or 7
96	be,pn	%ncc, .storebyte1421	! store 1, 4, 2, 1 bytes
97	cmp	%g1, 2			! dst halfword aligned ?
98	be,pn	%ncc, .storehalfword	! yup, store half-word wise
99	andcc	%l0, 7, %g0		! dst word aligned ?
100	bnz,pn	%ncc, .storeword2	! yup, store word wise
101	nop				! ensure loop is 16-byte aligned
102	nop				! ensure loop is 16-byte aligned
103
104.storedword:
105	ldx	[%i3 + %g4], %l1	! src dword
106	addcc	%g4, 8, %g4		! n += 8, src += 8, dst += 8
107	bcs,pn	%ncc, .lastword		! if counter wraps, last word
108	andn	%i5, %l1, %g1		! ~dword & 0x8080808080808080
109	sub	%l1, %i4, %l0		! dword - 0x0101010101010101
110	andcc	%l0, %g1, %g0		! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080)
111	bz,a,pt	%ncc, .storedword	! no zero byte if magic expression == 0
112	stx	%l1, [%i2 + %g4]	! store word to dst (address pre-incremented)
113
114	! n has not expired, but src is at the end. we need to push out the
115	! remaining src bytes. Since strlen(dts) == strlen(src), we can
116	! compute the return value as the difference of final dst pointer
117	! and the pointer to the start of dst
118
119.zerobyte:
120	add	%i2, %g4, %i2		! pointer to dest string
121	srlx	%l1, 56, %g1		! first byte
122	andcc	%g1, 0xff, %g0		! end of string ?
123	bz,pn	%ncc, .done		! yup, copy done, return length
124	stb	%g1, [%i2]		! store it
125	add	%i2, 1, %i2		! dst++
126	srlx	%l1, 48, %g1		! second byte
127	andcc	%g1, 0xff, %g0		! end of string ?
128	bz,pn	%ncc, .done		! yup, copy done, return length
129	stb	%g1, [%i2]		! store it
130	add	%i2, 1, %i2		! dst++
131	srlx	%l1, 40, %g1		! third byte
132	andcc	%g1, 0xff, %g0		! end of string ?
133	bz,pn	%ncc, .done		! yup, copy done, return length
134	stb	%g1, [%i2]		! store it
135	add	%i2, 1, %i2		! dst++
136	srlx	%l1, 32, %g1		! fourth byte
137	andcc	%g1, 0xff, %g0		! end of string ?
138	bz,pn	%ncc, .done		! yup, copy done, return length
139	stb	%g1, [%i2]		! store it
140	add	%i2, 1, %i2		! dst++
141	srlx	%l1, 24, %g1		! fifth byte
142	andcc	%g1, 0xff, %g0		! end of string ?
143	bz,pn	%ncc, .done		! yup, copy done, return length
144	stb	%g1, [%i2]		! store it
145	add	%i2, 1, %i2		! dst++
146	srlx	%l1, 16, %g1		! sixth byte
147	andcc	%g1, 0xff, %g0		! end of string ?
148	bz,pn	%ncc, .done		! yup, copy done, return length
149	stb	%g1, [%i2]		! store it
150	add	%i2, 1, %i2		! dst++
151	srlx	%l1, 8, %g1		! seventh byte
152	andcc	%g1, 0xff, %g0		! end of string ?
153	bz,pn	%ncc, .done		! yup, copy done, return length
154	stb	%g1, [%i2]		! store it
155	stb	%l1, [%i2 + 1]		! store eigth byte
156	add	%i2, 1, %i2		! dst++
157
158.done:
159	sub	%i2, %i0, %i0		! len = dst - orig dst
160	ret				! subroutine done
161	restore	%i0, %g0, %o0		! restore register window, return len
162
163	! n expired, so this is the last word. It may contain null bytes.
164	! Store bytes until n == 0. If a null byte is encountered during
165	! processing of this last src word, we are done. Otherwise continue
166	! to scan src until we hit the end, and compute strlen from the
167	! difference between the pointer past the last byte of src and the
168	! original pointer to the start of src
169
170.lastword:
171	add	%i2, %g4, %i2		! we want a single dst pointer here
172	sub	%g4, 8, %g4		! undo counter pre-increment
173	add	%i3, %g4, %i3		! we want a single src pointer here
174
175	srlx	%l1, 56, %g1		! first byte
176	andcc	%g1, 0xff, %g0		! end of src reached ?
177	bz,pn	%ncc, .done		! yup
178	stb	%g1, [%i2]		! store it
179	inccc	%g4			! n--
180	bz	.forcenull		! if n == 0, force null byte, compute len
181	srlx	%l1, 48, %g1		! second byte
182	add	%i2, 1, %i2		! dst++
183	andcc	%g1, 0xff, %g0		! end of src reached ?
184	bz,pn	%ncc, .done		! yup
185	stb	%g1, [%i2]		! store it
186	inccc	%g4			! n--
187	bz	.forcenull		! if n == 0, force null byte, compute len
188	srlx	%l1, 40, %g1		! third byte
189	add	%i2, 1, %i2		! dst++
190	andcc	%g1, 0xff, %g0		! end of src reached ?
191	bz,pn	%ncc, .done		! yup
192	stb	%g1, [%i2]		! store it
193	inccc	%g4			! n--
194	bz	.forcenull		! if n == 0, force null byte, compute strlen
195	srlx	%l1, 32, %g1		! fourth byte
196	add	%i2, 1, %i2		! dst++
197	andcc	%g1, 0xff, %g0		! end of src reached ?
198	bz,pn	%ncc, .done		! yup
199	stb	%g1, [%i2]		! store it
200	inccc	%g4			! n--
201	bz	.forcenull		! if n == 0, force null byte, compute strlen
202	srlx	%l1, 24, %g1		! fifth byte
203	add	%i2, 1, %i2		! dst++
204	andcc	%g1, 0xff, %g0		! end of src reached ?
205	bz,pn	%ncc, .done		! yup
206	stb	%g1, [%i2]		! store it
207	inccc	%g4			! n--
208	bz	.forcenull		! if n == 0, force null byte, compute strlen
209	srlx	%l1, 16, %g1		! sixth byte
210	add	%i2, 1, %i2		! dst++
211	andcc	%g1, 0xff, %g0		! end of src reached ?
212	bz,pn	%ncc, .done		! yup
213	stb	%g1, [%i2]		! store it
214	inccc	%g4			! n--
215	bz	.forcenull		! if n == 0, force null byte, compute strlen
216	srlx	%l1, 8, %g1		! seventh byte
217	add	%i2, 1, %i2		! dst++
218	andcc	%g1, 0xff, %g0		! end of src reached ?
219	bz,pn	%ncc, .done		! yup
220	stb	%g1, [%i2]		! store it
221	inccc	%g4			! n--
222	bz	.forcenull		! if n == 0, force null byte, compute strlen
223	andcc	%l1, 0xff, %g0		! end of src reached ?
224	add	%i2, 1, %i2		! dst++
225	bz,pn	%ncc, .done		! yup
226	stb	%l1, [%i2]		! store eigth byte
227
228	! we need to force a null byte in the last position of dst
229	! %i2 points to the location
230
231.forcenull:
232	stb	%g0, [%i2]		! force string terminating null byte
233
234	! here: %i1 points to src start
235	!	%i3 points is current src ptr (8-byte aligned)
236
237.searchword:
238	ldx	[%i3], %l1		! src dword
239.searchword2:
240	andn	%i5, %l1, %g1		! ~dword & 0x8080808080808080
241	sub	%l1, %i4, %l0		! dword - 0x0101010101010101
242	andcc	%l0, %g1, %g0		! ((dword - 0x0101010101010101) & ~dword & 0x80808080
243	bz,a,pt	%ncc, .searchword	! no null byte if expression is 0
244	add	%i3, 8, %i3		! src += 8
245
246	mov	0xff, %i5		! create byte mask for null byte scanning
247	sllx	%i5, 56, %i5		! mask for 1st byte = 0xff0000000000000000
248.searchbyte:
249	andcc	%l1, %i5, %g0		! current byte zero?
250	srlx	%i5, 8, %i5		! byte mask for next byte
251	bnz,a	%ncc, .searchbyte	! current byte != zero, continue search
252	add	%i3, 1, %i3		! src++
253
254.endfound:
255	sub	%i3, %i1, %i0		! len = src - orig src
256	ret				! done
257	restore	%i0, %g0, %o0		! restore register window, return len
258	nop				! align loop on 16-byte
259
260.storebyte1421:
261	ldx	[%i3 + %g4], %l1	! x = src[]
262	addcc	%g4, 8, %g4		! src += 8, dst += 8
263	bcs,pn	%ncc, .lastword		! if counter wraps, last word
264	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
265	sub	%l1, %i4, %l0		! x - 0x0101010101010101
266	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
267	bnz,pn	%ncc, .zerobyte		! end of src found, may need to pad
268	add	%i2, %g4, %l0		! dst (in pointer form)
269	srlx	%l1, 56, %g1		! %g1<7:0> = first byte; word aligned now
270	stb	%g1, [%l0]		! store first byte
271	srlx	%l1, 24, %g1		! %g1<31:0> = bytes 2, 3, 4, 5
272	stw	%g1, [%l0 + 1]		! store bytes 2, 3, 4, 5
273	srlx	%l1, 8, %g1		! %g1<15:0> = bytes 6, 7
274	sth	%g1, [%l0 + 5]		! store bytes 6, 7
275	ba	.storebyte1421		! next dword
276	stb	%l1, [%l0 + 7]		! store eigth byte
277
278.storebyte1241:
279	ldx	[%i3 + %g4], %l1	! x = src[]
280	addcc	%g4, 8, %g4		! src += 8, dst += 8
281	bcs,pn	%ncc, .lastword		! if counter wraps, last word
282	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
283	sub	%l1, %i4, %l0		! x - 0x0101010101010101
284	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
285	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
286	add	%i2, %g4, %l0		! dst (in pointer form)
287	srlx	%l1, 56, %g1		! %g1<7:0> = first byte; half-word aligned now
288	stb	%g1, [%l0]		! store first byte
289	srlx	%l1, 40, %g1		! %g1<15:0> = bytes 2, 3
290	sth	%g1, [%l0 + 1]		! store bytes 2, 3
291	srlx	%l1, 8, %g1		! %g1<31:0> = bytes 4, 5, 6, 7
292	stw	%g1, [%l0 + 3]		! store bytes 4, 5, 6, 7
293	ba	.storebyte1241		! next dword
294	stb	%l1, [%l0 + 7]		! store eigth byte
295
296.storehalfword:
297	ldx	[%i3 + %g4], %l1	! x = src[]
298	addcc	%g4, 8, %g4		! src += 8, dst += 8
299	bcs,pn	%ncc, .lastword		! if counter wraps, last word
300	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
301	sub	%l1, %i4, %l0		! x - 0x0101010101010101
302	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
303	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
304	add	%i2, %g4, %l0		! dst (in pointer form)
305	srlx	%l1, 48, %g1		! %g1<15:0> = bytes 1, 2; word aligned now
306	sth	%g1, [%l0]		! store bytes 1, 2
307	srlx	%l1, 16, %g1		! %g1<31:0> = bytes 3, 4, 5, 6
308	stw	%g1, [%l0 + 2]		! store bytes 3, 4, 5, 6
309	ba	.storehalfword		! next dword
310	sth	%l1, [%l0 + 6]		! store bytes 7, 8
311	nop				! align next loop to 16-byte boundary
312	nop				! align next loop to 16-byte boundary
313
314.storeword2:
315	ldx	[%i3 + %g4], %l1	! x = src[]
316	addcc	%g4, 8, %g4		! src += 8, dst += 8
317	bcs,pn	%ncc, .lastword		! if counter wraps, last word
318	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
319	sub	%l1, %i4, %l0		! x - 0x0101010101010101
320	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
321	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
322	add	%i2, %g4, %l0		! dst (in pointer form)
323	srlx	%l1, 32, %g1		! %g1<31:0> = bytes 1, 2, 3, 4
324	stw	%g1, [%l0]		! store bytes 1, 2, 3, 4
325	ba	.storeword2		! next dword
326	stw	%l1, [%l0 + 4]		! store bytes 5, 6, 7, 8
327
328	! n expired, i.e. end of destination buffer reached. Force null
329	! null termination of dst, then scan src until end foudn for
330	! determination of strlen(src)
331	!
332	! here: %i3 points to current src byte
333	!       %i2 points one byte past end of dst
334	! magic constants not loaded
335
336.forcenullunalign:
337	add	%i2, %g4, %i2		! we need a single dst ptr
338	stb	%g0, [%i2 - 1]		! force string terminating null byte
339
340.getstrlen:
341	sethi	%hi(0x01010101), %i4	! Alan Mycroft's magic1
342	or	%i4, %lo(0x01010101),%i4!  finish loading magic1
343	sllx	%i4, 32, %i2		! spread magic1
344	or	%i4, %i2, %i4		!   to all 64 bits
345	sllx	%i4, 7, %i5		!  Alan Mycroft's magic2
346	nop				! align loop to 16-byte boundary
347
348.getstrlenloop:
349	andcc	%i3, 7, %g0		! src dword aligned?
350	bz,a,pn	%ncc, .searchword2	! yup, now search a dword at a time
351	ldx	[%i3], %l1		! src dword
352	ldub	[%i3], %l1		! load src byte
353	andcc	%l1, 0xff, %g0		! end of src reached?
354	bnz,a	%ncc, .getstrlenloop	! yup, return length
355	add	%i3, 1, %i3		! src++
356	sub	%i3, %i1, %i0		! len = src - orig src
357	ret				! done
358	restore	%i0, %g0, %o0		! restore register window, return len
359
360	nop				! pad tp 16-byte boundary
361	nop				! pad tp 16-byte boundary
362	SET_SIZE(strlcpy)
363