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 * strncpy(s1, s2) 33 * 34 * Copy string s2 to s1, truncating or null-padding to always copy n bytes 35 * return s1. 36 * 37 * Fast assembler language version of the following C-program for strncpy 38 * which represents the `standard' for the C-library. 39 * 40 * char * 41 * strncpy(char *s1, const char *s2, size_t n) 42 * { 43 * char *os1 = s1; 44 * 45 * n++; 46 * while ((--n != 0) && ((*s1++ = *s2++) != '\0')) 47 * ; 48 * if (n != 0) 49 * while (--n != 0) 50 * *s1++ = '\0'; 51 * return (os1); 52 * } 53 */ 54 55#include <sys/asm_linkage.h> 56 57 ! strncpy works similarly to strcpy, except that n bytes of s2 58 ! are copied to s1. If a null character is reached in s2 yet more 59 ! bytes remain to be copied, strncpy will copy null bytes into 60 ! the destination string. 61 ! 62 ! This implementation works by first aligning the src ptr and 63 ! performing small copies until it is aligned. Then, the string 64 ! is copied based upon destination alignment. (byte, half-word, 65 ! word, etc.) 66 67 ENTRY(strncpy) 68 69 .align 32 70 nop ! pad to align loop on 16-byte boundary 71 subcc %g0, %o2, %g4 ! n = -n, n == 0 ? 72 bz,pn %ncc, .done ! n == 0, done 73 add %o1, %o2, %o3 ! src = src + n 74 andcc %o1, 7, %o4 ! dword aligned ? 75 bz,pn %ncc, .dwordaligned ! yup 76 add %o0, %o2, %o2 ! dst = dst + n 77 sub %o4, 8, %o4 ! bytes until src aligned 78 79.alignsrc: 80 ldub [%o3 + %g4], %o1 ! src[] 81 stb %o1, [%o2 + %g4] ! dst[] = src[] 82 addcc %g4, 1, %g4 ! src++, dst++, n-- 83 bz,pn %ncc, .done ! n == 0, done 84 tst %o1 ! end of src reached (null byte) ? 85 bz,a %ncc, .bytepad ! yes, at least one byte to pad here 86 add %o2, %g4, %o3 ! need single dest pointer for fill 87 addcc %o4, 1, %o4 ! src aligned now? 88 bnz,a %ncc, .alignsrc ! no, copy another byte 89 nop ! pad 90 nop ! pad 91 92.dwordaligned: 93 sethi %hi(0x01010101), %o4 ! Alan Mycroft's magic1 94 add %o2, %g4, %g5 ! dst 95 or %o4, %lo(0x01010101),%o4! finish loading magic1 96 and %g5, 3, %g1 ! dst<1:0> to examine offset 97 sllx %o4, 32, %o1 ! spread magic1 98 cmp %g1, 1 ! dst offset of 1 or 5 99 or %o4, %o1, %o4 ! to all 64 bits 100 sub %o2, 8, %o2 ! adjust for dest pre-incr in cpy loops 101 be,pn %ncc, .storebyte1241 ! store 1, 2, 4, 1 bytes 102 sllx %o4, 7, %o5 ! Alan Mycroft's magic2 103 cmp %g1, 3 ! dst offset of 3 or 7 104 be,pn %ncc, .storebyte1421 ! store 1, 4, 2, 1 bytes 105 cmp %g1, 2 ! dst halfword aligned ? 106 be,pn %ncc, .storehalfword ! yup, store half-word wise 107 andcc %g5, 7, %g0 ! dst word aligned ? 108 bnz,pn %ncc, .storeword2 ! yup, store word wise 109 nop ! ensure loop is 16-byte aligned 110 111.storedword: 112 ldx [%o3 + %g4], %o1 ! src dword 113 addcc %g4, 8, %g4 ! n += 8, src += 8, dst += 8 114 bcs,pn %ncc,.lastword ! if counter wraps, last word 115 andn %o5, %o1, %g1 ! ~dword & 0x8080808080808080 116 sub %o1, %o4, %g5 ! dword - 0x0101010101010101 117 andcc %g5, %g1, %g0 ! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080) 118 bz,a,pt %ncc, .storedword ! no zero byte if magic expression == 0 119 stx %o1, [%o2 + %g4] ! store word to dst (address pre-incremented) 120 121 ! n has not expired, but src is at the end. we need to push out the 122 ! remaining src bytes and then start padding with null bytes 123 124.zerobyte: 125 add %o2, %g4, %o3 ! pointer to dest string 126 srlx %o1, 56, %g1 ! first byte 127 stb %g1, [%o3] ! store it 128 andcc %g1, 0xff, %g0 ! end of string ? 129 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 130 srlx %o1, 48, %g1 ! second byte 131 stb %g1, [%o3 + 1] ! store it 132 andcc %g1, 0xff, %g0 ! end of string ? 133 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 134 srlx %o1, 40, %g1 ! third byte 135 stb %g1, [%o3 + 2] ! store it 136 andcc %g1, 0xff, %g0 ! end of string ? 137 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 138 srlx %o1, 32, %g1 ! fourth byte 139 stb %g1, [%o3 + 3] ! store it 140 andcc %g1, 0xff, %g0 ! end of string ? 141 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 142 srlx %o1, 24, %g1 ! fifth byte 143 stb %g1, [%o3 + 4] ! store it 144 andcc %g1, 0xff, %g0 ! end of string ? 145 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 146 srlx %o1, 16, %g1 ! sixth byte 147 stb %g1, [%o3 + 5] ! store it 148 andcc %g1, 0xff, %g0 ! end of string ? 149 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 150 srlx %o1, 8, %g1 ! seventh byte 151 stb %g1, [%o3 + 6] ! store it 152 andcc %g1, 0xff, %g0 ! end of string ? 153 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 154 stb %o1, [%o3 + 7] ! store eighth byte 155 addcc %g4, 16, %g0 ! number of pad bytes < 16 ? 156 bcs,pn %ncc, .bytepad ! yes, do simple byte wise fill 157 add %o3, 8, %o3 ! dst += 8 158 andcc %o3, 7, %o4 ! dst offset relative to dword boundary 159 bz,pn %ncc, .fillaligned ! dst already dword aligned 160 161 ! here there is a least one more byte to zero out: otherwise we would 162 ! have exited through label .lastword 163 164 sub %o4, 8, %o4 ! bytes to align dst to dword boundary 165.makealigned: 166 stb %g0, [%o3] ! dst[] = 0 167 addcc %g4, 1, %g4 ! n-- 168 bz,pt %ncc, .done ! n == 0, we are done 169 addcc %o4, 1, %o4 ! any more byte needed to align 170 bnz,pt %ncc, .makealigned ! yup, pad another byte 171 add %o3, 1, %o3 ! dst++ 172 nop ! pad to align copy loop below 173 nop ! pad to align copy loop below 174 175 ! here we know that there at least another 8 bytes to pad, since 176 ! we don't get here unless there were >= 16 bytes to pad to begin 177 ! with, and we have padded at most 7 bytes suring dst aligning 178 179.fillaligned: 180 add %g4, 7, %o2 ! round up to next dword boundary 181 and %o2, -8, %o4 ! pointer to next dword boundary 182 and %o2, 8, %o2 ! dword count odd ? 8 : 0 183 stx %g0, [%o3] ! store first dword 184 addcc %o4, %o2, %o4 ! dword count == 1 ? 185 add %g4, %o2, %g4 ! if dword count odd, n -= 8 186 bz,pt %ncc, .bytepad ! if dword count == 1, pad leftover bytes 187 add %o3, %o2, %o3 ! bump dst if dword count odd 188 189.filldword: 190 addcc %o4, 16, %o4 ! count -= 16 191 stx %g0, [%o3] ! dst[n] = 0 192 stx %g0, [%o3 + 8] ! dst[n+8] = 0 193 add %o3, 16, %o3 ! dst += 16 194 bcc,pt %ncc, .filldword ! fill dwords until count == 0 195 addcc %g4, 16, %g4 ! n -= 16 196 bz,pn %ncc, .done ! if n == 0, we are done 197 198.bytepad: 199 and %g4, 1, %o2 ! byte count odd ? 1 : 0 200 stb %g0, [%o3] ! store first byte 201 addcc %g4, %o2, %g4 ! byte count == 1 ? 202 bz,pt %ncc, .done ! yup, we are done 203 add %o3, %o2, %o3 ! bump pointer if odd 204 205.fillbyte: 206 addcc %g4, 2, %g4 ! n -= 2 207 stb %g0, [%o3] ! dst[n] = 0 208 stb %g0, [%o3 + 1] ! dst[n+1] = 0 209 bnz,pt %ncc, .fillbyte ! fill until n == 0 210 add %o3, 2, %o3 ! dst += 2 211 212.done: 213 retl ! done 214 nop ! pad to align loops below 215 nop ! pad to align loops below 216 217 ! this is the last word. It may contain null bytes. store bytes 218 ! until n == 0. if null byte encountered, continue 219 220.lastword: 221 sub %g4, 8, %g4 ! undo counter pre-increment 222 add %o2, 8, %o2 ! adjust dst for counter un-bumping 223 224 srlx %o1, 56, %g1 ! first byte 225 stb %g1, [%o2 + %g4] ! store it 226 inccc %g4 ! n-- 227 bz .done ! if n == 0, we're done 228 andcc %g1, 0xff, %g0 ! end of src reached ? 229 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 230 srlx %o1, 48, %g1 ! second byte 231 stb %g1, [%o2 + %g4] ! store it 232 inccc %g4 ! n-- 233 bz .done ! if n == 0, we're done 234 andcc %g1, 0xff, %g0 ! end of src reached ? 235 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 236 srlx %o1, 40, %g1 ! third byte 237 stb %g1, [%o2 + %g4] ! store it 238 inccc %g4 ! n-- 239 bz .done ! if n == 0, we're done 240 andcc %g1, 0xff, %g0 ! end of src reached ? 241 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 242 srlx %o1, 32, %g1 ! fourth byte 243 stb %g1, [%o2 + %g4] ! store it 244 inccc %g4 ! n-- 245 bz .done ! if n == 0, we're done 246 andcc %g1, 0xff, %g0 ! end of src reached ? 247 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 248 srlx %o1, 24, %g1 ! fifth byte 249 stb %g1, [%o2 + %g4] ! store it 250 inccc %g4 ! n-- 251 bz .done ! if n == 0, we're done 252 andcc %g1, 0xff, %g0 ! end of src reached ? 253 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 254 srlx %o1, 16, %g1 ! sixth byte 255 stb %g1, [%o2 + %g4] ! store it 256 inccc %g4 ! n-- 257 bz .done ! if n == 0, we're done 258 andcc %g1, 0xff, %g0 ! end of src reached ? 259 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 260 srlx %o1, 8, %g1 ! seventh byte 261 stb %g1, [%o2 + %g4] ! store it 262 inccc %g4 ! n-- 263 bz .done ! if n == 0, we're done 264 andcc %g1, 0xff, %g0 ! end of src reached ? 265 movz %ncc, %g0, %o1 ! if so, start padding with null bytes 266 ba .done ! here n must be zero, we are done 267 stb %o1, [%o2 + %g4] ! store eigth byte 268 nop ! pad to align loops below 269 nop ! pad to align loops below 270 271.storebyte1421: 272 ldx [%o3 + %g4], %o1 ! x = src[] 273 addcc %g4, 8, %g4 ! src += 8, dst += 8 274 bcs,pn %ncc,.lastword ! if counter wraps, last word 275 andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 276 sub %o1, %o4, %g5 ! x - 0x0101010101010101 277 andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 278 bnz,pn %ncc, .zerobyte ! end of src found, may need to pad 279 add %o2, %g4, %g5 ! dst (in pointer form) 280 srlx %o1, 56, %g1 ! %g1<7:0> = first byte; word aligned now 281 stb %g1, [%g5] ! store first byte 282 srlx %o1, 24, %g1 ! %g1<31:0> = bytes 2, 3, 4, 5 283 stw %g1, [%g5 + 1] ! store bytes 2, 3, 4, 5 284 srlx %o1, 8, %g1 ! %g1<15:0> = bytes 6, 7 285 sth %g1, [%g5 + 5] ! store bytes 6, 7 286 ba .storebyte1421 ! next dword 287 stb %o1, [%g5 + 7] ! store eigth byte 288 289.storebyte1241: 290 ldx [%o3 + %g4], %o1 ! x = src[] 291 addcc %g4, 8, %g4 ! src += 8, dst += 8 292 bcs,pn %ncc,.lastword ! if counter wraps, last word 293 andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 294 sub %o1, %o4, %g5 ! x - 0x0101010101010101 295 andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 296 bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 297 add %o2, %g4, %g5 ! dst (in pointer form) 298 srlx %o1, 56, %g1 ! %g1<7:0> = first byte; half-word aligned now 299 stb %g1, [%g5] ! store first byte 300 srlx %o1, 40, %g1 ! %g1<15:0> = bytes 2, 3 301 sth %g1, [%g5 + 1] ! store bytes 2, 3 302 srlx %o1, 8, %g1 ! %g1<31:0> = bytes 4, 5, 6, 7 303 stw %g1, [%g5 + 3] ! store bytes 4, 5, 6, 7 304 ba .storebyte1241 ! next dword 305 stb %o1, [%g5 + 7] ! store eigth byte 306 307.storehalfword: 308 ldx [%o3 + %g4], %o1 ! x = src[] 309 addcc %g4, 8, %g4 ! src += 8, dst += 8 310 bcs,pn %ncc,.lastword ! if counter wraps, last word 311 andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 312 sub %o1, %o4, %g5 ! x - 0x0101010101010101 313 andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 314 bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 315 add %o2, %g4, %g5 ! dst (in pointer form) 316 srlx %o1, 48, %g1 ! %g1<15:0> = bytes 1, 2; word aligned now 317 sth %g1, [%g5] ! store bytes 1, 2 318 srlx %o1, 16, %g1 ! %g1<31:0> = bytes 3, 4, 5, 6 319 stw %g1, [%g5 + 2] ! store bytes 3, 4, 5, 6 320 ba .storehalfword ! next dword 321 sth %o1, [%g5 + 6] ! store bytes 7, 8 322 nop ! align next loop to 16-byte boundary 323 nop ! align next loop to 16-byte boundary 324 325.storeword2: 326 ldx [%o3 + %g4], %o1 ! x = src[] 327 addcc %g4, 8, %g4 ! src += 8, dst += 8 328 bcs,pn %ncc,.lastword ! if counter wraps, last word 329 andn %o5, %o1, %g1 ! ~x & 0x8080808080808080 330 sub %o1, %o4, %g5 ! x - 0x0101010101010101 331 andcc %g5, %g1, %g0 ! ((x - 0x0101010101010101) & ~x & 0x8080808080808080) 332 bnz,pn %ncc, .zerobyte ! x has zero byte, handle end cases 333 add %o2, %g4, %g5 ! dst (in pointer form) 334 srlx %o1, 32, %g1 ! %g1<31:0> = bytes 1, 2, 3, 4 335 stw %g1, [%g5] ! store bytes 1, 2, 3, 4 336 ba .storeword2 ! next dword 337 stw %o1, [%g5 + 4] ! store bytes 5, 6, 7, 8 338 339 ! do not remove these pads, loop above may slow down otherwise 340 341 nop ! pad 342 nop ! pad 343 344 SET_SIZE(strncpy) 345