10Sstevel@tonic-gate/* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 56812Sraf * Common Development and Distribution License (the "License"). 66812Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 216812Sraf 220Sstevel@tonic-gate/* 236812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 27*7298SMark.J.Nelson@Sun.COM .file "strncpy.s" 280Sstevel@tonic-gate 290Sstevel@tonic-gate/* 300Sstevel@tonic-gate * strncpy(s1, s2) 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * Copy string s2 to s1, truncating or null-padding to always copy n bytes 330Sstevel@tonic-gate * return s1. 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * Fast assembler language version of the following C-program for strncpy 360Sstevel@tonic-gate * which represents the `standard' for the C-library. 370Sstevel@tonic-gate * 380Sstevel@tonic-gate * char * 390Sstevel@tonic-gate * strncpy(char *s1, const char *s2, size_t n) 400Sstevel@tonic-gate * { 410Sstevel@tonic-gate * char *os1 = s1; 420Sstevel@tonic-gate * 430Sstevel@tonic-gate * n++; 440Sstevel@tonic-gate * while ((--n != 0) && ((*s1++ = *s2++) != '\0')) 450Sstevel@tonic-gate * ; 460Sstevel@tonic-gate * if (n != 0) 470Sstevel@tonic-gate * while (--n != 0) 480Sstevel@tonic-gate * *s1++ = '\0'; 490Sstevel@tonic-gate * return (os1); 500Sstevel@tonic-gate * } 510Sstevel@tonic-gate */ 520Sstevel@tonic-gate 530Sstevel@tonic-gate#include <sys/asm_linkage.h> 540Sstevel@tonic-gate 550Sstevel@tonic-gate ! strncpy works similarly to strcpy, except that n bytes of s2 560Sstevel@tonic-gate ! are copied to s1. If a null character is reached in s2 yet more 570Sstevel@tonic-gate ! bytes remain to be copied, strncpy will copy null bytes into 580Sstevel@tonic-gate ! the destination string. 590Sstevel@tonic-gate ! 600Sstevel@tonic-gate ! This implementation works by first aligning the src ptr and 610Sstevel@tonic-gate ! performing small copies until it is aligned. Then, the string 620Sstevel@tonic-gate ! is copied based upon destination alignment. (byte, half-word, 630Sstevel@tonic-gate ! word, etc.) 640Sstevel@tonic-gate 650Sstevel@tonic-gate ENTRY(strncpy) 660Sstevel@tonic-gate 670Sstevel@tonic-gate .align 32 680Sstevel@tonic-gate subcc %g0, %o2, %o4 ! n = -n 690Sstevel@tonic-gate bz .doneshort ! if n == 0, done 700Sstevel@tonic-gate cmp %o2, 7 ! n < 7 ? 710Sstevel@tonic-gate add %o1, %o2, %o3 ! src = src + n 720Sstevel@tonic-gate blu .shortcpy ! n < 7, use byte-wise copy 730Sstevel@tonic-gate add %o0, %o2, %o2 ! dst = dst + n 740Sstevel@tonic-gate andcc %o1, 3, %o5 ! src word aligned ? 750Sstevel@tonic-gate bz .wordaligned ! yup 760Sstevel@tonic-gate save %sp, -0x40, %sp ! create new register window 770Sstevel@tonic-gate sub %i5, 4, %i5 ! bytes until src aligned 780Sstevel@tonic-gate nop ! align loop on 16-byte boundary 790Sstevel@tonic-gate nop ! align loop on 16-byte boundary 800Sstevel@tonic-gate 810Sstevel@tonic-gate.alignsrc: 820Sstevel@tonic-gate ldub [%i3 + %i4], %i1 ! src[] 830Sstevel@tonic-gate stb %i1, [%i2 + %i4] ! dst[] = src[] 840Sstevel@tonic-gate inccc %i4 ! src++, dst++, n-- 850Sstevel@tonic-gate bz .done ! n == 0, done 860Sstevel@tonic-gate tst %i1 ! end of src reached (null byte) ? 870Sstevel@tonic-gate bz,a .bytepad ! yes, at least one byte to pad here 880Sstevel@tonic-gate add %i2, %i4, %l0 ! need single dest pointer for fill 890Sstevel@tonic-gate inccc %i5 ! src aligned now? 900Sstevel@tonic-gate bnz .alignsrc ! no, copy another byte 910Sstevel@tonic-gate .empty 920Sstevel@tonic-gate 930Sstevel@tonic-gate.wordaligned: 940Sstevel@tonic-gate add %i2, %i4, %l0 ! dst 950Sstevel@tonic-gate sethi %hi(0x01010101), %l1 ! Alan Mycroft's magic1 960Sstevel@tonic-gate sub %i2, 4, %i2 ! adjust for dest pre-incr in cpy loops 970Sstevel@tonic-gate or %l1, %lo(0x01010101),%l1! finish loading magic1 980Sstevel@tonic-gate andcc %l0, 3, %g1 ! destination word aligned ? 990Sstevel@tonic-gate bnz .dstnotaligned ! nope 1000Sstevel@tonic-gate sll %l1, 7, %i5 ! create Alan Mycroft's magic2 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate.storeword: 1030Sstevel@tonic-gate lduw [%i3 + %i4], %i1 ! src dword 1040Sstevel@tonic-gate addcc %i4, 4, %i4 ! n += 4, src += 4, dst += 4 1050Sstevel@tonic-gate bcs .lastword ! if counter wraps, last word 1060Sstevel@tonic-gate andn %i5, %i1, %g1 ! ~dword & 0x80808080 1070Sstevel@tonic-gate sub %i1, %l1, %l0 ! dword - 0x01010101 1080Sstevel@tonic-gate andcc %l0, %g1, %g0 ! ((dword - 0x01010101) & ~dword & 0x80808080) 1090Sstevel@tonic-gate bz,a .storeword ! no zero byte if magic expression == 0 1100Sstevel@tonic-gate stw %i1, [%i2 + %i4] ! store word to dst (address pre-incremented) 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate ! n has not expired, but src is at the end. we need to push out the 1130Sstevel@tonic-gate ! remaining src bytes and then start padding with null bytes 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate.zerobyte: 1160Sstevel@tonic-gate add %i2, %i4, %l0 ! pointer to dest string 1170Sstevel@tonic-gate srl %i1, 24, %g1 ! first byte 1180Sstevel@tonic-gate stb %g1, [%l0] ! store it 1190Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 1200Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 1210Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null bytes 1220Sstevel@tonic-gate srl %i1, 16, %g1 ! second byte 1230Sstevel@tonic-gate stb %g1, [%l0 + 1] ! store it 1240Sstevel@tonic-gate and %g1, 0xff, %g1 ! isolate byte 1250Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 1260Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 1270Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null bytes 1280Sstevel@tonic-gate srl %i1, 8, %g1 ! third byte 1290Sstevel@tonic-gate stb %g1, [%l0 + 2] ! store it 1300Sstevel@tonic-gate and %g1, 0xff, %g1 ! isolate byte 1310Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 1320Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 1330Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null bytes 1340Sstevel@tonic-gate stb %i1, [%l0 + 3] ! store fourth byte 1350Sstevel@tonic-gate addcc %i4, 8, %g0 ! number of pad bytes < 8 ? 1360Sstevel@tonic-gate bcs .bytepad ! yes, do simple byte wise fill 1370Sstevel@tonic-gate add %l0, 4, %l0 ! dst += 4 1380Sstevel@tonic-gate andcc %l0, 3, %l1 ! dst offset relative to word boundary 1390Sstevel@tonic-gate bz .fillaligned ! dst already word aligned 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate ! here there is a least one more byte to zero out: otherwise we would 1420Sstevel@tonic-gate ! have exited through label .lastword 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate sub %l1, 4, %l1 ! bytes to align dst to word boundary 1450Sstevel@tonic-gate.makealigned: 1460Sstevel@tonic-gate stb %g0, [%l0] ! dst[] = 0 1470Sstevel@tonic-gate addcc %i4, 1, %i4 ! n-- 1480Sstevel@tonic-gate bz .done ! n == 0, we are done 1490Sstevel@tonic-gate addcc %l1, 1, %l1 ! any more byte needed to align 1500Sstevel@tonic-gate bnz .makealigned ! yup, pad another byte 1510Sstevel@tonic-gate add %l0, 1, %l0 ! dst++ 1520Sstevel@tonic-gate nop ! pad to align copy loop below 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate ! here we know that there at least another 4 bytes to pad, since 1550Sstevel@tonic-gate ! we don't get here unless there were >= 8 bytes to pad to begin 1560Sstevel@tonic-gate ! with, and we have padded at most 3 bytes suring dst aligning 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate.fillaligned: 1590Sstevel@tonic-gate add %i4, 3, %i2 ! round up to next word boundary 1600Sstevel@tonic-gate and %i2, -4, %l1 ! pointer to next word boundary 1610Sstevel@tonic-gate and %i2, 4, %i2 ! word count odd ? 4 : 0 1620Sstevel@tonic-gate stw %g0, [%l0] ! store first word 1630Sstevel@tonic-gate addcc %l1, %i2, %l1 ! dword count == 1 ? 1640Sstevel@tonic-gate add %i4, %i2, %i4 ! if word count odd, n -= 4 1650Sstevel@tonic-gate bz .bytepad ! if word count == 1, pad bytes left 1660Sstevel@tonic-gate add %l0, %i2, %l0 ! bump dst if word count odd 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate.fillword: 1690Sstevel@tonic-gate addcc %l1, 8, %l1 ! count -= 8 1700Sstevel@tonic-gate stw %g0, [%l0] ! dst[n] = 0 1710Sstevel@tonic-gate stw %g0, [%l0 + 4] ! dst[n+4] = 0 1720Sstevel@tonic-gate add %l0, 8, %l0 ! dst += 8 1730Sstevel@tonic-gate bcc .fillword ! fill words until count == 0 1740Sstevel@tonic-gate addcc %i4, 8, %i4 ! n -= 8 1750Sstevel@tonic-gate bz .done ! if n == 0, we are done 1760Sstevel@tonic-gate .empty 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate.bytepad: 1790Sstevel@tonic-gate and %i4, 1, %i2 ! byte count odd ? 1 : 0 1800Sstevel@tonic-gate stb %g0, [%l0] ! store first byte 1810Sstevel@tonic-gate addcc %i4, %i2, %i4 ! byte count == 1 ? 1820Sstevel@tonic-gate bz .done ! yup, we are done 1830Sstevel@tonic-gate add %l0, %i2, %l0 ! bump pointer if odd 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate.fillbyte: 1860Sstevel@tonic-gate addcc %i4, 2, %i4 ! n -= 2 1870Sstevel@tonic-gate stb %g0, [%l0] ! dst[n] = 0 1880Sstevel@tonic-gate stb %g0, [%l0 + 1] ! dst[n+1] = 0 1890Sstevel@tonic-gate bnz .fillbyte ! fill until n == 0 1900Sstevel@tonic-gate add %l0, 2, %l0 ! dst += 2 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate.done: 1930Sstevel@tonic-gate ret ! done 1940Sstevel@tonic-gate restore %i0, %g0, %o0 ! restore reg window, return dst 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate ! this is the last word. It may contain null bytes. store bytes 1970Sstevel@tonic-gate ! until n == 0. if null byte encountered, continue 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate.lastword: 2000Sstevel@tonic-gate sub %i4, 4, %i4 ! undo counter pre-increment 2010Sstevel@tonic-gate add %i2, 4, %i2 ! adjust dst for counter un-bumping 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate srl %i1, 24, %g1 ! first byte 2040Sstevel@tonic-gate stb %g1, [%i2 + %i4] ! store it 2050Sstevel@tonic-gate inccc %i4 ! n-- 2060Sstevel@tonic-gate bz .done ! if n == 0, we're done 2070Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 2080Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 2090Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null 2100Sstevel@tonic-gate srl %i1, 16, %g1 ! second byte 2110Sstevel@tonic-gate stb %g1, [%i2 + %i4] ! store it 2120Sstevel@tonic-gate inccc %i4 ! n-- 2130Sstevel@tonic-gate bz .done ! if n == 0, we're done 2140Sstevel@tonic-gate and %g1, 0xff, %g1 ! isolate byte 2150Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 2160Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 2170Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null 2180Sstevel@tonic-gate srl %i1, 8, %g1 ! third byte 2190Sstevel@tonic-gate stb %g1, [%i2 + %i4] ! store it 2200Sstevel@tonic-gate inccc %i4 ! n-- 2210Sstevel@tonic-gate bz .done ! if n == 0, we're done 2220Sstevel@tonic-gate and %g1, 0xff, %g1 ! isolate byte 2230Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 2240Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 2250Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null 2260Sstevel@tonic-gate ba .done ! here n must be zero, we are done 2270Sstevel@tonic-gate stb %i1, [%i2 + %i4] ! store fourth byte 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate.dstnotaligned: 2300Sstevel@tonic-gate cmp %g1, 2 ! dst half word aligned? 2310Sstevel@tonic-gate be .storehalfword2 ! yup, store half word at a time 2320Sstevel@tonic-gate .empty 2330Sstevel@tonic-gate.storebyte: 2340Sstevel@tonic-gate lduw [%i3 + %i4], %i1 ! x = src[] 2350Sstevel@tonic-gate addcc %i4, 4, %i4 ! src += 4, dst += 4, n -= 4 2360Sstevel@tonic-gate bcs .lastword ! if counter wraps, last word 2370Sstevel@tonic-gate andn %i5, %i1, %g1 ! ~x & 0x80808080 2380Sstevel@tonic-gate sub %i1, %l1, %l0 ! x - 0x01010101 2390Sstevel@tonic-gate andcc %l0, %g1, %g0 ! ((x - 0x01010101) & ~x & 0x80808080) 2400Sstevel@tonic-gate bnz .zerobyte ! end of src found, may need to pad 2410Sstevel@tonic-gate add %i2, %i4, %l0 ! dst (in pointer form) 2420Sstevel@tonic-gate srl %i1, 24, %g1 ! %g1<7:0> = 1st byte; half-word aligned now 2430Sstevel@tonic-gate stb %g1, [%l0] ! store first byte 2440Sstevel@tonic-gate srl %i1, 8, %g1 ! %g1<15:0> = bytes 2, 3 2450Sstevel@tonic-gate sth %g1, [%l0 + 1] ! store bytes 2, 3 2460Sstevel@tonic-gate ba .storebyte ! next word 2470Sstevel@tonic-gate stb %i1, [%l0 + 3] ! store fourth byte 2480Sstevel@tonic-gate nop 2490Sstevel@tonic-gate nop 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate.storehalfword: 2520Sstevel@tonic-gate lduw [%i3 + %i4], %i1 ! x = src[] 2530Sstevel@tonic-gate.storehalfword2: 2540Sstevel@tonic-gate addcc %i4, 4, %i4 ! src += 4, dst += 4, n -= 4 2550Sstevel@tonic-gate bcs .lastword ! if counter wraps, last word 2560Sstevel@tonic-gate andn %i5, %i1, %g1 ! ~x & 0x80808080 2570Sstevel@tonic-gate sub %i1, %l1, %l0 ! x - 0x01010101 2580Sstevel@tonic-gate andcc %l0, %g1, %g0 ! ((x -0x01010101) & ~x & 0x8080808080) 2590Sstevel@tonic-gate bnz .zerobyte ! x has zero byte, handle end cases 2600Sstevel@tonic-gate add %i2, %i4, %l0 ! dst (in pointer form) 2610Sstevel@tonic-gate srl %i1, 16, %g1 ! %g1<15:0> = bytes 1, 2 2620Sstevel@tonic-gate sth %g1, [%l0] ! store bytes 1, 2 2630Sstevel@tonic-gate ba .storehalfword ! next dword 2640Sstevel@tonic-gate sth %i1, [%l0 + 2] ! store bytes 3, 4 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate.shortcpy: 2670Sstevel@tonic-gate ldub [%o3 + %o4], %o5 ! src[] 2680Sstevel@tonic-gate stb %o5, [%o2 + %o4] ! dst[] = src[] 2690Sstevel@tonic-gate inccc %o4 ! src++, dst++, n-- 2700Sstevel@tonic-gate bz .doneshort ! if n == 0, done 2710Sstevel@tonic-gate tst %o5 ! src[] == 0 ? 2720Sstevel@tonic-gate bnz,a .shortcpy ! nope, next byte 2730Sstevel@tonic-gate nop ! empty delay slot 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate.padbyte: 2760Sstevel@tonic-gate stb %g0, [%o2 + %o4] ! dst[] = 0 2770Sstevel@tonic-gate.padbyte2: 2780Sstevel@tonic-gate addcc %o4, 1, %o4 ! dst++, n-- 2790Sstevel@tonic-gate bnz,a .padbyte2 ! if n != 0, next byte 2800Sstevel@tonic-gate stb %g0, [%o2 + %o4] ! dst[] = 0 2810Sstevel@tonic-gate nop ! align label below to 16-byte boundary 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate.doneshort: 2840Sstevel@tonic-gate retl ! return from leaf 2850Sstevel@tonic-gate nop ! empty delay slot 2860Sstevel@tonic-gate SET_SIZE(strncpy) 287