xref: /openbsd/sys/lib/libkern/arch/amd64/memmove.S (revision bf39093a)
127c40355Sderaadt/*-
227c40355Sderaadt * Copyright (c) 1990 The Regents of the University of California.
327c40355Sderaadt * All rights reserved.
427c40355Sderaadt *
527c40355Sderaadt * This code is derived from locore.s.
627c40355Sderaadt *
727c40355Sderaadt * Redistribution and use in source and binary forms, with or without
827c40355Sderaadt * modification, are permitted provided that the following conditions
927c40355Sderaadt * are met:
1027c40355Sderaadt * 1. Redistributions of source code must retain the above copyright
1127c40355Sderaadt *    notice, this list of conditions and the following disclaimer.
1227c40355Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
1327c40355Sderaadt *    notice, this list of conditions and the following disclaimer in the
1427c40355Sderaadt *    documentation and/or other materials provided with the distribution.
1527c40355Sderaadt * 3. All advertising materials mentioning features or use of this software
1627c40355Sderaadt *    must display the following acknowledgement:
1727c40355Sderaadt *	This product includes software developed by the University of
1827c40355Sderaadt *	California, Berkeley and its contributors.
1927c40355Sderaadt * 4. Neither the name of the University nor the names of its contributors
2027c40355Sderaadt *    may be used to endorse or promote products derived from this software
2127c40355Sderaadt *    without specific prior written permission.
2227c40355Sderaadt *
2327c40355Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2427c40355Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2527c40355Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2627c40355Sderaadt * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2727c40355Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2827c40355Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2927c40355Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3027c40355Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3127c40355Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3227c40355Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3327c40355Sderaadt * SUCH DAMAGE.
3427c40355Sderaadt */
35f5df1827Smickey
3627c40355Sderaadt#include <machine/asm.h>
3727c40355Sderaadt
3827c40355Sderaadt	/*
3927c40355Sderaadt	 * memmove (dst,src,cnt)
4027c40355Sderaadt	 *  ws@tools.de     (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
4127c40355Sderaadt	 */
4227c40355Sderaadt
4327c40355SderaadtENTRY(bcopy)
4427c40355Sderaadt	xchgq	%rdi,%rsi
4527c40355Sderaadt	/* fall into memmove */
4627c40355Sderaadt
47a5910b4fSderaadtNENTRY(memmove)
48*bf39093aSguenther	endbr64
491d66f0a0Smortimer	RETGUARD_SETUP(memmove, r10)
5027c40355Sderaadt	movq	%rdi,%r11	/* save dest */
5127c40355Sderaadt	movq	%rdx,%rcx
5227c40355Sderaadt	movq	%rdi,%rax
5327c40355Sderaadt	subq	%rsi,%rax
5427c40355Sderaadt	cmpq	%rcx,%rax	/* overlapping? */
5527c40355Sderaadt	jb	1f
56c067621fStedu	jmp	2f		/* nope */
57c067621fStedu
58c067621fSteduENTRY(memcpy)
591d66f0a0Smortimer	RETGUARD_SETUP(memmove, r10)
60c067621fStedu	movq	%rdi,%r11	/* save dest */
61c067621fStedu	movq	%rdx,%rcx
62c067621fStedu2:
6327c40355Sderaadt	shrq	$3,%rcx		/* copy by words */
6427c40355Sderaadt	rep
6527c40355Sderaadt	movsq
6627c40355Sderaadt	movq	%rdx,%rcx
6727c40355Sderaadt	andq	$7,%rcx		/* any bytes left? */
6827c40355Sderaadt	rep
6927c40355Sderaadt	movsb
7027c40355Sderaadt	movq	%r11,%rax
711d66f0a0Smortimer	jmp 3f
7227c40355Sderaadt1:
7327c40355Sderaadt	addq	%rcx,%rdi	/* copy backwards. */
7427c40355Sderaadt	addq	%rcx,%rsi
7527c40355Sderaadt	std
7627c40355Sderaadt	andq	$7,%rcx		/* any fractional bytes? */
7727c40355Sderaadt	decq	%rdi
7827c40355Sderaadt	decq	%rsi
7927c40355Sderaadt	rep
8027c40355Sderaadt	movsb
8127c40355Sderaadt	movq	%rdx,%rcx	/* copy remainder by words */
8227c40355Sderaadt	shrq	$3,%rcx
8327c40355Sderaadt	subq	$7,%rsi
8427c40355Sderaadt	subq	$7,%rdi
8527c40355Sderaadt	rep
8627c40355Sderaadt	movsq
8727c40355Sderaadt	movq	%r11,%rax
8827c40355Sderaadt	cld
891d66f0a0Smortimer3:	RETGUARD_CHECK(memmove, r10)
9027c40355Sderaadt	ret
91fc541c5dSguenther	lfence
9228c67577SguentherEND(bcopy)
9328c67577SguentherEND(memmove)
9428c67577SguentherEND(memcpy)
95