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 "strcpy.s" 280Sstevel@tonic-gate 290Sstevel@tonic-gate/* 300Sstevel@tonic-gate * strcpy(s1, s2) 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * Copy string s2 to s1. s1 must be large enough. Return s1. 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * Fast assembler language version of the following C-program strcpy 350Sstevel@tonic-gate * which represents the `standard' for the C-library. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * char * 380Sstevel@tonic-gate * strcpy(s1, s2) 390Sstevel@tonic-gate * register char *s1; 400Sstevel@tonic-gate * register const char *s2; 410Sstevel@tonic-gate * { 420Sstevel@tonic-gate * char *os1 = s1; 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * while(*s1++ = *s2++) 450Sstevel@tonic-gate * ; 460Sstevel@tonic-gate * return(os1); 470Sstevel@tonic-gate * } 480Sstevel@tonic-gate * 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate 510Sstevel@tonic-gate#include <sys/asm_linkage.h> 520Sstevel@tonic-gate 530Sstevel@tonic-gate ! This implementation of strcpy works by first checking the 540Sstevel@tonic-gate ! source alignment and copying byte, half byte, or word 550Sstevel@tonic-gate ! quantities until the source ptr is aligned at an extended 560Sstevel@tonic-gate ! word boundary. Once this has occurred, the string is copied, 570Sstevel@tonic-gate ! checking for zero bytes, depending upon its dst ptr alignment. 580Sstevel@tonic-gate ! (methods for xword, word, half-word, and byte copies are present) 590Sstevel@tonic-gate 600Sstevel@tonic-gate ENTRY(strcpy) 610Sstevel@tonic-gate 620Sstevel@tonic-gate .align 32 630Sstevel@tonic-gate 640Sstevel@tonic-gate sub %o1, %o0, %o3 ! src - dst 650Sstevel@tonic-gate andcc %o1, 7, %o4 ! dword aligned ? 660Sstevel@tonic-gate bz,pn %ncc, .srcaligned ! yup 670Sstevel@tonic-gate mov %o0, %o2 ! save dst 680Sstevel@tonic-gate 690Sstevel@tonic-gate.chkbyte: 700Sstevel@tonic-gate andcc %o1, 1, %g0 ! need to copy byte ? 710Sstevel@tonic-gate bz,pn %ncc, .chkhalfword ! nope, maybe halfword 720Sstevel@tonic-gate sub %g0, %o1, %g1 ! %g1<2:0> = # of unaligned bytes 730Sstevel@tonic-gate ldub [%o2 + %o3], %o5 ! src[0] 740Sstevel@tonic-gate tst %o5 ! src[0] == 0 ? 750Sstevel@tonic-gate stb %o5, [%o2] ! dst[0] = src[0] 760Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 770Sstevel@tonic-gate inc %o2 ! src++, dst++ 780Sstevel@tonic-gate 790Sstevel@tonic-gate.chkhalfword: 800Sstevel@tonic-gate andcc %g1, 2, %g0 ! need to copy half-word ? 810Sstevel@tonic-gate bz,pn %ncc, .chkword ! nope, maybe word 820Sstevel@tonic-gate nop ! 830Sstevel@tonic-gate lduh [%o2 + %o3], %o5 ! load src halfword 840Sstevel@tonic-gate srl %o5, 8, %o4 ! extract first byte 850Sstevel@tonic-gate tst %o4 ! first byte == 0 ? 860Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 870Sstevel@tonic-gate stb %o4, [%o2] ! store first byte 880Sstevel@tonic-gate andcc %o5, 0xff, %g0 ! extract second byte 890Sstevel@tonic-gate stb %o5, [%o2 + 1] ! store second byte 900Sstevel@tonic-gate bz,pn %ncc, .done ! yup, 2nd byte zero, done 910Sstevel@tonic-gate add %o2, 2, %o2 ! src += 2 920Sstevel@tonic-gate 930Sstevel@tonic-gate.chkword: 940Sstevel@tonic-gate andcc %g1, 4, %g0 ! need to copy word ? 950Sstevel@tonic-gate bz,pn %ncc, .srcaligned ! nope 960Sstevel@tonic-gate nop ! 970Sstevel@tonic-gate lduw [%o2 + %o3], %o5 ! load src word 980Sstevel@tonic-gate srl %o5, 24, %o4 ! extract first byte 990Sstevel@tonic-gate tst %o4 ! is first byte zero ? 1000Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1010Sstevel@tonic-gate stb %o4, [%o2] ! store first byte 1020Sstevel@tonic-gate srl %o5, 16, %o4 ! extract second byte 1030Sstevel@tonic-gate andcc %o4, 0xff, %g0 ! is second byte zero ? 1040Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1050Sstevel@tonic-gate stb %o4, [%o2 + 1] ! store second byte 1060Sstevel@tonic-gate srl %o5, 8, %o4 ! extract third byte 1070Sstevel@tonic-gate andcc %o4, 0xff, %g0 ! third byte zero ? 1080Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1090Sstevel@tonic-gate stb %o4, [%o2 + 2] ! store third byte 1100Sstevel@tonic-gate andcc %o5, 0xff, %g0 ! fourth byte zero ? 1110Sstevel@tonic-gate stb %o5, [%o2 + 3] ! store fourth byte 1120Sstevel@tonic-gate bz,pn %ncc, .done ! yup, fourth byte zero, done 1130Sstevel@tonic-gate add %o2, 4, %o2 ! src += 2 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate.srcaligned: 1160Sstevel@tonic-gate sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1 1170Sstevel@tonic-gate or %o4, %lo(0x01010101),%o4! finish loading magic1 1180Sstevel@tonic-gate sllx %o4, 32, %o1 ! spread magic1 1190Sstevel@tonic-gate and %o2, 3, %g4 ! dst<1:0> to examine offset 1200Sstevel@tonic-gate or %o4, %o1, %o4 ! to all 64 bits 1210Sstevel@tonic-gate cmp %g4, 1 ! dst offset of 1 or 5 1220Sstevel@tonic-gate sllx %o4, 7, %o5 ! Alan Mycroft's magic2 1230Sstevel@tonic-gate be,pn %ncc, .storebyte1241 ! store 1, 2, 4, 1 bytes 1240Sstevel@tonic-gate cmp %g4, 3 ! dst offset of 3 or 7 1250Sstevel@tonic-gate be,pn %ncc, .storebyte1421 ! store 1, 4, 2, 1 bytes 1260Sstevel@tonic-gate cmp %g4, 2 ! dst halfword aligned ? 1270Sstevel@tonic-gate be,pn %ncc, .storehalfword ! yup, store half-word wise 1280Sstevel@tonic-gate andcc %o2, 7, %g0 ! dst word aligned ? 1290Sstevel@tonic-gate bnz,pn %ncc, .storeword2 ! yup, store word wise 1300Sstevel@tonic-gate .empty 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate.storedword: 1330Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! src dword 1340Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 1350Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~dword & 0x8080808080808080 1360Sstevel@tonic-gate sub %o1, %o4, %g4 ! dword - 0x0101010101010101 1370Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080) 1380Sstevel@tonic-gate bz,a,pt %ncc, .storedword ! no zero byte if magic expression == 0 1390Sstevel@tonic-gate stx %o1, [%o2 - 8] ! store word to dst (address pre-incremented) 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate.zerobyte: 1420Sstevel@tonic-gate orn %o4, %g0, %o4 ! 0xffffffffffffffff 1430Sstevel@tonic-gate sllx %o4, 56, %o4 ! 0xff00000000000000 1440Sstevel@tonic-gate srlx %o1, 56, %o3 ! %o3<7:0> = first byte 1450Sstevel@tonic-gate andcc %o1, %o4, %g0 ! first byte zero? 1460Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1470Sstevel@tonic-gate stb %o3, [%o2 - 8] ! store first byte 1480Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x00ff000000000000 1490Sstevel@tonic-gate srlx %o1, 48, %o3 ! %o3<7:0> = second byte 1500Sstevel@tonic-gate andcc %o1, %o4, %g0 ! second byte zero? 1510Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1520Sstevel@tonic-gate stb %o3, [%o2 - 7] ! store second byte 1530Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x0000ff0000000000 1540Sstevel@tonic-gate srlx %o1, 40, %o3 ! %o3<7:0> = third byte 1550Sstevel@tonic-gate andcc %o1, %o4, %g0 ! third byte zero? 1560Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1570Sstevel@tonic-gate stb %o3, [%o2 - 6] ! store third byte 1580Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x000000ff00000000 1590Sstevel@tonic-gate srlx %o1, 32, %o3 ! %o3<7:0> = fourth byte 1600Sstevel@tonic-gate andcc %o1, %o4, %g0 ! fourth byte zero? 1610Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1620Sstevel@tonic-gate stb %o3, [%o2 - 5] ! store fourth byte 1630Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x00000000ff000000 1640Sstevel@tonic-gate srlx %o1, 24, %o3 ! %o3<7:0> = fifth byte 1650Sstevel@tonic-gate andcc %o1, %o4, %g0 ! fifth byte zero? 1660Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1670Sstevel@tonic-gate stb %o3, [%o2 - 4] ! store fifth byte 1680Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x0000000000ff0000 1690Sstevel@tonic-gate srlx %o1, 16, %o3 ! %o3<7:0> = sixth byte 1700Sstevel@tonic-gate andcc %o1, %o4, %g0 ! sixth byte zero? 1710Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1720Sstevel@tonic-gate stb %o3, [%o2 - 3] ! store sixth byte 1730Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x000000000000ff00 1740Sstevel@tonic-gate andcc %o1, %o4, %g0 ! seventh byte zero? 1750Sstevel@tonic-gate srlx %o1, 8, %o3 ! %o3<7:0> = seventh byte 1760Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1770Sstevel@tonic-gate stb %o3, [%o2 - 2] ! store seventh byte 1780Sstevel@tonic-gate stb %o1, [%o2 - 1] ! store eigth byte 1790Sstevel@tonic-gate.done: 1800Sstevel@tonic-gate retl ! done with leaf function 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate.storebyte1421: 1850Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! x = src[] 1860Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 1870Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 1880Sstevel@tonic-gate sub %o1, %o4, %g4 ! x - 0x0101010101010101 1890Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 1900Sstevel@tonic-gate bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 1910Sstevel@tonic-gate srlx %o1, 56, %g1 ! %g1<7:0> = first byte; word aligned now 1920Sstevel@tonic-gate stb %g1, [%o2 - 8] ! store first byte 1930Sstevel@tonic-gate srlx %o1, 24, %g1 ! %g1<31:0> = bytes 2, 3, 4, 5 1940Sstevel@tonic-gate stw %g1, [%o2 - 7] ! store bytes 2, 3, 4, 5 1950Sstevel@tonic-gate srlx %o1, 8, %g1 ! %g1<15:0> = bytes 6, 7 1960Sstevel@tonic-gate sth %g1, [%o2 - 3] ! store bytes 6, 7 1970Sstevel@tonic-gate ba .storebyte1421 ! next dword 1980Sstevel@tonic-gate stb %o1, [%o2 - 1] ! store eigth byte 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 2010Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate.storebyte1241: 2040Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! x = src[] 2050Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 2060Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 2070Sstevel@tonic-gate sub %o1, %o4, %g4 ! x - 0x0101010101010101 2080Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 2090Sstevel@tonic-gate bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 2100Sstevel@tonic-gate srlx %o1, 56, %g1 ! %g1<7:0> = first byte; word aligned now 2110Sstevel@tonic-gate stb %g1, [%o2 - 8] ! store first byte 2120Sstevel@tonic-gate srlx %o1, 40, %g1 ! %g1<15:0> = bytes 2, 3 2130Sstevel@tonic-gate sth %g1, [%o2 - 7] ! store bytes 2, 3 2140Sstevel@tonic-gate srlx %o1, 8, %g1 ! %g1<31:0> = bytes 4, 5, 6, 7 2150Sstevel@tonic-gate stw %g1, [%o2 - 5] ! store bytes 4, 5, 6, 7 2160Sstevel@tonic-gate ba .storebyte1241 ! next dword 2170Sstevel@tonic-gate stb %o1, [%o2 - 1] ! store eigth byte 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 2200Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate.storehalfword: 2230Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! x = src[] 2240Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 2250Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 2260Sstevel@tonic-gate sub %o1, %o4, %g4 ! x - 0x0101010101010101 2270Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 2280Sstevel@tonic-gate bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 2290Sstevel@tonic-gate srlx %o1, 48, %g1 ! get first and second byte 2300Sstevel@tonic-gate sth %g1, [%o2 - 8] ! store first and second byte; word aligned now 2310Sstevel@tonic-gate srlx %o1, 16, %g1 ! %g1<31:0> = bytes 3, 4, 5, 6 2320Sstevel@tonic-gate stw %g1, [%o2 - 6] ! store bytes 3, 4, 5, 6 2330Sstevel@tonic-gate ba .storehalfword ! next word 2340Sstevel@tonic-gate sth %o1, [%o2 - 2] ! store seventh and eigth byte 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate.storeword: 2370Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! x = src[] 2380Sstevel@tonic-gate.storeword2: 2390Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 2400Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~x & 0x0x8080808080808080 2410Sstevel@tonic-gate sub %o1, %o4, %g4 ! x - 0x0101010101010101 2420Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 2430Sstevel@tonic-gate bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 2440Sstevel@tonic-gate srlx %o1, 32, %g1 ! get bytes 1,2,3,4 2450Sstevel@tonic-gate stw %g1, [%o2 - 8] ! store bytes 1,2,3,4 (address is pre-incremented) 2460Sstevel@tonic-gate ba .storeword ! no zero byte if magic expression == 0 2470Sstevel@tonic-gate stw %o1, [%o2 - 4] ! store bytes 5,6,7,8 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate nop ! padding, do not remove!!! 2500Sstevel@tonic-gate nop ! padding, do not remove!!! 2510Sstevel@tonic-gate SET_SIZE(strcpy) 2520Sstevel@tonic-gate 253