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