1/* $OpenBSD: memmove.S,v 1.4 2013/06/13 15:03:08 deraadt Exp $ */ 2/*- 3 * Copyright (c) 1991, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Ralph Campbell. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include "DEFS.h" 35 36 37/* 38 * memcpy(to, from, len) 39 always copy forward 40 * 41 * memmove(to, from, len), bcopy(from, to, len) 42 * both handle overlap 43 */ 44LEAF(memcpy, 0) 45 .set noreorder 46 move v0, a0 # swap from and to 47 move a0, a1 48 move a1, v0 49 PTR_ADDU t0, a0, a2 # t0 = end of s1 region 50 sltu t1, a1, t0 51 sltu t2, a0, a1 52 j forward # do forward copy 53 slt t2, a2, 12 # check for small copy 54 55ALEAF(memmove) 56 .set noreorder 57 move v0, a0 # swap from and to 58 move a0, a1 59 move a1, v0 60ALEAF(bcopy) 61 .set noreorder 62 PTR_ADDU t0, a0, a2 # t0 = end of s1 region 63 sltu t1, a1, t0 64 sltu t2, a0, a1 65 and t1, t1, t2 # t1 = true if from < to < (from+len) 66 beq t1, zero, forward # non overlapping, do forward copy 67 slt t2, a2, 12 # check for small copy 68 69 ble a2, zero, 2f 70 PTR_ADDU t1, a1, a2 # t1 = end of to region 711: 72 lb v1, -1(t0) # copy bytes backwards, 73 PTR_SUBU t0, t0, 1 # doesnt happen often so do slow way 74 PTR_SUBU t1, t1, 1 75 bne t0, a0, 1b 76 sb v1, 0(t1) 772: 78 j ra 79 nop 80forward: 81 bne t2, zero, smallcpy # do a small bcopy 82 xor v1, a0, a1 # compare low two bits of addresses 83 and v1, v1, 3 84 PTR_SUBU a3, zero, a1 # compute # bytes to word align address 85 beq v1, zero, aligned # addresses can be word aligned 86 and a3, a3, 3 87 88 beq a3, zero, 1f 89 PTR_SUBU a2, a2, a3 # subtract from remaining count 90 LWHI v1, 0(a0) # get next 4 bytes (unaligned) 91 LWLO v1, 3(a0) 92 PTR_ADDU a0, a0, a3 93 SWHI v1, 0(a1) # store 1, 2, or 3 bytes to align a1 94 PTR_ADDU a1, a1, a3 951: 96 and v1, a2, 3 # compute number of words left 97 PTR_SUBU a3, a2, v1 98 move a2, v1 99 PTR_ADDU a3, a3, a0 # compute ending address 1002: 101 LWHI v1, 0(a0) # copy words a0 unaligned, a1 aligned 102 LWLO v1, 3(a0) 103 PTR_ADDU a0, a0, 4 104 sw v1, 0(a1) 105 PTR_ADDU a1, a1, 4 106 bne a0, a3, 2b 107 nop # We have to do this mmu-bug. 108 b smallcpy 109 nop 110aligned: 111 beq a3, zero, 1f 112 PTR_SUBU a2, a2, a3 # subtract from remaining count 113 LWHI v1, 0(a0) # copy 1, 2, or 3 bytes to align 114 PTR_ADDU a0, a0, a3 115 SWHI v1, 0(a1) 116 PTR_ADDU a1, a1, a3 1171: 118 and v1, a2, 3 # compute number of whole words left 119 PTR_SUBU a3, a2, v1 120 move a2, v1 121 PTR_ADDU a3, a3, a0 # compute ending address 1222: 123 lw v1, 0(a0) # copy words 124 PTR_ADDU a0, a0, 4 125 sw v1, 0(a1) 126 bne a0, a3, 2b 127 PTR_ADDU a1, a1, 4 128smallcpy: 129 ble a2, zero, 2f 130 PTR_ADDU a3, a2, a0 # compute ending address 1311: 132 lbu v1, 0(a0) # copy bytes 133 PTR_ADDU a0, a0, 1 134 sb v1, 0(a1) 135 bne a0, a3, 1b 136 PTR_ADDU a1, a1, 1 # MMU BUG ? can not do -1(a1) at 0x80000000!! 1372: 138 j ra 139 nop 140END(memcpy) 141