17c478bd9Sstevel@tonic-gate/* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57257d1b4Sraf * Common Development and Distribution License (the "License"). 67257d1b4Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217257d1b4Sraf 227c478bd9Sstevel@tonic-gate/* 237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 27*9a70fc3bSMark J. Nelson .file "strcpy.s" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate/* 307c478bd9Sstevel@tonic-gate * strcpy(s1, s2) 317c478bd9Sstevel@tonic-gate * 327c478bd9Sstevel@tonic-gate * Copy string s2 to s1. s1 must be large enough. Return s1. 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * Fast assembler language version of the following C-program strcpy 357c478bd9Sstevel@tonic-gate * which represents the `standard' for the C-library. 367c478bd9Sstevel@tonic-gate * 377c478bd9Sstevel@tonic-gate * char * 387c478bd9Sstevel@tonic-gate * strcpy(s1, s2) 397c478bd9Sstevel@tonic-gate * register char *s1; 407c478bd9Sstevel@tonic-gate * register const char *s2; 417c478bd9Sstevel@tonic-gate * { 427c478bd9Sstevel@tonic-gate * char *os1 = s1; 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * while(*s1++ = *s2++) 457c478bd9Sstevel@tonic-gate * ; 467c478bd9Sstevel@tonic-gate * return(os1); 477c478bd9Sstevel@tonic-gate * } 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate ! This implementation of strcpy works by first checking the 547c478bd9Sstevel@tonic-gate ! source alignment and copying byte, half byte, or word 557c478bd9Sstevel@tonic-gate ! quantities until the source ptr is aligned at an extended 567c478bd9Sstevel@tonic-gate ! word boundary. Once this has occurred, the string is copied, 577c478bd9Sstevel@tonic-gate ! checking for zero bytes, depending upon its dst ptr alignment. 587c478bd9Sstevel@tonic-gate ! (methods for xword, word, half-word, and byte copies are present) 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate ENTRY(strcpy) 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate .align 32 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate sub %o1, %o0, %o3 ! src - dst 657c478bd9Sstevel@tonic-gate andcc %o1, 7, %o4 ! dword aligned ? 667c478bd9Sstevel@tonic-gate bz,pn %ncc, .srcaligned ! yup 677c478bd9Sstevel@tonic-gate mov %o0, %o2 ! save dst 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate.chkbyte: 707c478bd9Sstevel@tonic-gate andcc %o1, 1, %g0 ! need to copy byte ? 717c478bd9Sstevel@tonic-gate bz,pn %ncc, .chkhalfword ! nope, maybe halfword 727c478bd9Sstevel@tonic-gate sub %g0, %o1, %g1 ! %g1<2:0> = # of unaligned bytes 737c478bd9Sstevel@tonic-gate ldub [%o2 + %o3], %o5 ! src[0] 747c478bd9Sstevel@tonic-gate tst %o5 ! src[0] == 0 ? 757c478bd9Sstevel@tonic-gate stb %o5, [%o2] ! dst[0] = src[0] 767c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 777c478bd9Sstevel@tonic-gate inc %o2 ! src++, dst++ 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate.chkhalfword: 807c478bd9Sstevel@tonic-gate andcc %g1, 2, %g0 ! need to copy half-word ? 817c478bd9Sstevel@tonic-gate bz,pn %ncc, .chkword ! nope, maybe word 827c478bd9Sstevel@tonic-gate nop ! 837c478bd9Sstevel@tonic-gate lduh [%o2 + %o3], %o5 ! load src halfword 847c478bd9Sstevel@tonic-gate srl %o5, 8, %o4 ! extract first byte 857c478bd9Sstevel@tonic-gate tst %o4 ! first byte == 0 ? 867c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 877c478bd9Sstevel@tonic-gate stb %o4, [%o2] ! store first byte 887c478bd9Sstevel@tonic-gate andcc %o5, 0xff, %g0 ! extract second byte 897c478bd9Sstevel@tonic-gate stb %o5, [%o2 + 1] ! store second byte 907c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, 2nd byte zero, done 917c478bd9Sstevel@tonic-gate add %o2, 2, %o2 ! src += 2 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate.chkword: 947c478bd9Sstevel@tonic-gate andcc %g1, 4, %g0 ! need to copy word ? 957c478bd9Sstevel@tonic-gate bz,pn %ncc, .srcaligned ! nope 967c478bd9Sstevel@tonic-gate nop ! 977c478bd9Sstevel@tonic-gate lduw [%o2 + %o3], %o5 ! load src word 987c478bd9Sstevel@tonic-gate srl %o5, 24, %o4 ! extract first byte 997c478bd9Sstevel@tonic-gate tst %o4 ! is first byte zero ? 1007c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1017c478bd9Sstevel@tonic-gate stb %o4, [%o2] ! store first byte 1027c478bd9Sstevel@tonic-gate srl %o5, 16, %o4 ! extract second byte 1037c478bd9Sstevel@tonic-gate andcc %o4, 0xff, %g0 ! is second byte zero ? 1047c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1057c478bd9Sstevel@tonic-gate stb %o4, [%o2 + 1] ! store second byte 1067c478bd9Sstevel@tonic-gate srl %o5, 8, %o4 ! extract third byte 1077c478bd9Sstevel@tonic-gate andcc %o4, 0xff, %g0 ! third byte zero ? 1087c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1097c478bd9Sstevel@tonic-gate stb %o4, [%o2 + 2] ! store third byte 1107c478bd9Sstevel@tonic-gate andcc %o5, 0xff, %g0 ! fourth byte zero ? 1117c478bd9Sstevel@tonic-gate stb %o5, [%o2 + 3] ! store fourth byte 1127c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, fourth byte zero, done 1137c478bd9Sstevel@tonic-gate add %o2, 4, %o2 ! src += 2 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate.srcaligned: 1167c478bd9Sstevel@tonic-gate sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1 1177c478bd9Sstevel@tonic-gate or %o4, %lo(0x01010101),%o4! finish loading magic1 1187c478bd9Sstevel@tonic-gate sllx %o4, 32, %o1 ! spread magic1 1197c478bd9Sstevel@tonic-gate and %o2, 3, %g4 ! dst<1:0> to examine offset 1207c478bd9Sstevel@tonic-gate or %o4, %o1, %o4 ! to all 64 bits 1217c478bd9Sstevel@tonic-gate cmp %g4, 1 ! dst offset of 1 or 5 1227c478bd9Sstevel@tonic-gate sllx %o4, 7, %o5 ! Alan Mycroft's magic2 1237c478bd9Sstevel@tonic-gate be,pn %ncc, .storebyte1241 ! store 1, 2, 4, 1 bytes 1247c478bd9Sstevel@tonic-gate cmp %g4, 3 ! dst offset of 3 or 7 1257c478bd9Sstevel@tonic-gate be,pn %ncc, .storebyte1421 ! store 1, 4, 2, 1 bytes 1267c478bd9Sstevel@tonic-gate cmp %g4, 2 ! dst halfword aligned ? 1277c478bd9Sstevel@tonic-gate be,pn %ncc, .storehalfword ! yup, store half-word wise 1287c478bd9Sstevel@tonic-gate andcc %o2, 7, %g0 ! dst word aligned ? 1297c478bd9Sstevel@tonic-gate bnz,pn %ncc, .storeword2 ! yup, store word wise 1307c478bd9Sstevel@tonic-gate .empty 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate.storedword: 1337c478bd9Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! src dword 1347c478bd9Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 1357c478bd9Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~dword & 0x8080808080808080 1367c478bd9Sstevel@tonic-gate sub %o1, %o4, %g4 ! dword - 0x0101010101010101 1377c478bd9Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080) 1387c478bd9Sstevel@tonic-gate bz,a,pt %ncc, .storedword ! no zero byte if magic expression == 0 1397c478bd9Sstevel@tonic-gate stx %o1, [%o2 - 8] ! store word to dst (address pre-incremented) 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate.zerobyte: 1427c478bd9Sstevel@tonic-gate orn %o4, %g0, %o4 ! 0xffffffffffffffff 1437c478bd9Sstevel@tonic-gate sllx %o4, 56, %o4 ! 0xff00000000000000 1447c478bd9Sstevel@tonic-gate srlx %o1, 56, %o3 ! %o3<7:0> = first byte 1457c478bd9Sstevel@tonic-gate andcc %o1, %o4, %g0 ! first byte zero? 1467c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1477c478bd9Sstevel@tonic-gate stb %o3, [%o2 - 8] ! store first byte 1487c478bd9Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x00ff000000000000 1497c478bd9Sstevel@tonic-gate srlx %o1, 48, %o3 ! %o3<7:0> = second byte 1507c478bd9Sstevel@tonic-gate andcc %o1, %o4, %g0 ! second byte zero? 1517c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1527c478bd9Sstevel@tonic-gate stb %o3, [%o2 - 7] ! store second byte 1537c478bd9Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x0000ff0000000000 1547c478bd9Sstevel@tonic-gate srlx %o1, 40, %o3 ! %o3<7:0> = third byte 1557c478bd9Sstevel@tonic-gate andcc %o1, %o4, %g0 ! third byte zero? 1567c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1577c478bd9Sstevel@tonic-gate stb %o3, [%o2 - 6] ! store third byte 1587c478bd9Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x000000ff00000000 1597c478bd9Sstevel@tonic-gate srlx %o1, 32, %o3 ! %o3<7:0> = fourth byte 1607c478bd9Sstevel@tonic-gate andcc %o1, %o4, %g0 ! fourth byte zero? 1617c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1627c478bd9Sstevel@tonic-gate stb %o3, [%o2 - 5] ! store fourth byte 1637c478bd9Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x00000000ff000000 1647c478bd9Sstevel@tonic-gate srlx %o1, 24, %o3 ! %o3<7:0> = fifth byte 1657c478bd9Sstevel@tonic-gate andcc %o1, %o4, %g0 ! fifth byte zero? 1667c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1677c478bd9Sstevel@tonic-gate stb %o3, [%o2 - 4] ! store fifth byte 1687c478bd9Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x0000000000ff0000 1697c478bd9Sstevel@tonic-gate srlx %o1, 16, %o3 ! %o3<7:0> = sixth byte 1707c478bd9Sstevel@tonic-gate andcc %o1, %o4, %g0 ! sixth byte zero? 1717c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1727c478bd9Sstevel@tonic-gate stb %o3, [%o2 - 3] ! store sixth byte 1737c478bd9Sstevel@tonic-gate srlx %o4, 8, %o4 ! 0x000000000000ff00 1747c478bd9Sstevel@tonic-gate andcc %o1, %o4, %g0 ! seventh byte zero? 1757c478bd9Sstevel@tonic-gate srlx %o1, 8, %o3 ! %o3<7:0> = seventh byte 1767c478bd9Sstevel@tonic-gate bz,pn %ncc, .done ! yup, done 1777c478bd9Sstevel@tonic-gate stb %o3, [%o2 - 2] ! store seventh byte 1787c478bd9Sstevel@tonic-gate stb %o1, [%o2 - 1] ! store eigth byte 1797c478bd9Sstevel@tonic-gate.done: 1807c478bd9Sstevel@tonic-gate retl ! done with leaf function 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate.storebyte1421: 1857c478bd9Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! x = src[] 1867c478bd9Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 1877c478bd9Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 1887c478bd9Sstevel@tonic-gate sub %o1, %o4, %g4 ! x - 0x0101010101010101 1897c478bd9Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 1907c478bd9Sstevel@tonic-gate bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 1917c478bd9Sstevel@tonic-gate srlx %o1, 56, %g1 ! %g1<7:0> = first byte; word aligned now 1927c478bd9Sstevel@tonic-gate stb %g1, [%o2 - 8] ! store first byte 1937c478bd9Sstevel@tonic-gate srlx %o1, 24, %g1 ! %g1<31:0> = bytes 2, 3, 4, 5 1947c478bd9Sstevel@tonic-gate stw %g1, [%o2 - 7] ! store bytes 2, 3, 4, 5 1957c478bd9Sstevel@tonic-gate srlx %o1, 8, %g1 ! %g1<15:0> = bytes 6, 7 1967c478bd9Sstevel@tonic-gate sth %g1, [%o2 - 3] ! store bytes 6, 7 1977c478bd9Sstevel@tonic-gate ba .storebyte1421 ! next dword 1987c478bd9Sstevel@tonic-gate stb %o1, [%o2 - 1] ! store eigth byte 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 2017c478bd9Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate.storebyte1241: 2047c478bd9Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! x = src[] 2057c478bd9Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 2067c478bd9Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 2077c478bd9Sstevel@tonic-gate sub %o1, %o4, %g4 ! x - 0x0101010101010101 2087c478bd9Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 2097c478bd9Sstevel@tonic-gate bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 2107c478bd9Sstevel@tonic-gate srlx %o1, 56, %g1 ! %g1<7:0> = first byte; word aligned now 2117c478bd9Sstevel@tonic-gate stb %g1, [%o2 - 8] ! store first byte 2127c478bd9Sstevel@tonic-gate srlx %o1, 40, %g1 ! %g1<15:0> = bytes 2, 3 2137c478bd9Sstevel@tonic-gate sth %g1, [%o2 - 7] ! store bytes 2, 3 2147c478bd9Sstevel@tonic-gate srlx %o1, 8, %g1 ! %g1<31:0> = bytes 4, 5, 6, 7 2157c478bd9Sstevel@tonic-gate stw %g1, [%o2 - 5] ! store bytes 4, 5, 6, 7 2167c478bd9Sstevel@tonic-gate ba .storebyte1241 ! next dword 2177c478bd9Sstevel@tonic-gate stb %o1, [%o2 - 1] ! store eigth byte 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 2207c478bd9Sstevel@tonic-gate nop ! ensure following loop 16-byte aligned 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate.storehalfword: 2237c478bd9Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! x = src[] 2247c478bd9Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 2257c478bd9Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 2267c478bd9Sstevel@tonic-gate sub %o1, %o4, %g4 ! x - 0x0101010101010101 2277c478bd9Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 2287c478bd9Sstevel@tonic-gate bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 2297c478bd9Sstevel@tonic-gate srlx %o1, 48, %g1 ! get first and second byte 2307c478bd9Sstevel@tonic-gate sth %g1, [%o2 - 8] ! store first and second byte; word aligned now 2317c478bd9Sstevel@tonic-gate srlx %o1, 16, %g1 ! %g1<31:0> = bytes 3, 4, 5, 6 2327c478bd9Sstevel@tonic-gate stw %g1, [%o2 - 6] ! store bytes 3, 4, 5, 6 2337c478bd9Sstevel@tonic-gate ba .storehalfword ! next word 2347c478bd9Sstevel@tonic-gate sth %o1, [%o2 - 2] ! store seventh and eigth byte 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate.storeword: 2377c478bd9Sstevel@tonic-gate ldx [%o2 + %o3], %o1 ! x = src[] 2387c478bd9Sstevel@tonic-gate.storeword2: 2397c478bd9Sstevel@tonic-gate add %o2, 8, %o2 ! src += 8, dst += 8 2407c478bd9Sstevel@tonic-gate andn %o5, %o1, %g1 ! ~x & 0x0x8080808080808080 2417c478bd9Sstevel@tonic-gate sub %o1, %o4, %g4 ! x - 0x0101010101010101 2427c478bd9Sstevel@tonic-gate andcc %g4, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 2437c478bd9Sstevel@tonic-gate bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 2447c478bd9Sstevel@tonic-gate srlx %o1, 32, %g1 ! get bytes 1,2,3,4 2457c478bd9Sstevel@tonic-gate stw %g1, [%o2 - 8] ! store bytes 1,2,3,4 (address is pre-incremented) 2467c478bd9Sstevel@tonic-gate ba .storeword ! no zero byte if magic expression == 0 2477c478bd9Sstevel@tonic-gate stw %o1, [%o2 - 4] ! store bytes 5,6,7,8 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate nop ! padding, do not remove!!! 2507c478bd9Sstevel@tonic-gate nop ! padding, do not remove!!! 2517c478bd9Sstevel@tonic-gate SET_SIZE(strcpy) 2527c478bd9Sstevel@tonic-gate 253