xref: /openbmc/linux/arch/x86/lib/memmove_64.S (revision bb916ff7)
19599ec04SFenghua Yu/*
29599ec04SFenghua Yu * Normally compiler builtins are used, but sometimes the compiler calls out
39599ec04SFenghua Yu * of line code. Based on asm-i386/string.h.
49599ec04SFenghua Yu *
59599ec04SFenghua Yu * This assembly file is re-written from memmove_64.c file.
69599ec04SFenghua Yu *	- Copyright 2011 Fenghua Yu <fenghua.yu@intel.com>
79599ec04SFenghua Yu */
89599ec04SFenghua Yu#define _STRING_C
99599ec04SFenghua Yu#include <linux/linkage.h>
109599ec04SFenghua Yu#include <asm/dwarf2.h>
11057e05c1SFenghua Yu#include <asm/cpufeature.h>
1259e97e4dSAndy Lutomirski#include <asm/alternative-asm.h>
139599ec04SFenghua Yu
149599ec04SFenghua Yu#undef memmove
159599ec04SFenghua Yu
169599ec04SFenghua Yu/*
179599ec04SFenghua Yu * Implement memmove(). This can handle overlap between src and dst.
189599ec04SFenghua Yu *
199599ec04SFenghua Yu * Input:
209599ec04SFenghua Yu * rdi: dest
219599ec04SFenghua Yu * rsi: src
229599ec04SFenghua Yu * rdx: count
239599ec04SFenghua Yu *
249599ec04SFenghua Yu * Output:
259599ec04SFenghua Yu * rax: dest
269599ec04SFenghua Yu */
279599ec04SFenghua YuENTRY(memmove)
289599ec04SFenghua Yu	CFI_STARTPROC
29057e05c1SFenghua Yu
309599ec04SFenghua Yu	/* Handle more 32bytes in loop */
319599ec04SFenghua Yu	mov %rdi, %rax
329599ec04SFenghua Yu	cmp $0x20, %rdx
339599ec04SFenghua Yu	jb	1f
349599ec04SFenghua Yu
359599ec04SFenghua Yu	/* Decide forward/backward copy mode */
369599ec04SFenghua Yu	cmp %rdi, %rsi
37057e05c1SFenghua Yu	jge .Lmemmove_begin_forward
38057e05c1SFenghua Yu	mov %rsi, %r8
39057e05c1SFenghua Yu	add %rdx, %r8
40057e05c1SFenghua Yu	cmp %rdi, %r8
41057e05c1SFenghua Yu	jg 2f
429599ec04SFenghua Yu
43057e05c1SFenghua Yu.Lmemmove_begin_forward:
449599ec04SFenghua Yu	/*
459599ec04SFenghua Yu	 * movsq instruction have many startup latency
469599ec04SFenghua Yu	 * so we handle small size by general register.
479599ec04SFenghua Yu	 */
489599ec04SFenghua Yu	cmp  $680, %rdx
499599ec04SFenghua Yu	jb	3f
509599ec04SFenghua Yu	/*
519599ec04SFenghua Yu	 * movsq instruction is only good for aligned case.
529599ec04SFenghua Yu	 */
539599ec04SFenghua Yu
549599ec04SFenghua Yu	cmpb %dil, %sil
559599ec04SFenghua Yu	je 4f
569599ec04SFenghua Yu3:
579599ec04SFenghua Yu	sub $0x20, %rdx
589599ec04SFenghua Yu	/*
59bb916ff7SAndy Shevchenko	 * We gobble 32 bytes forward in each loop.
609599ec04SFenghua Yu	 */
619599ec04SFenghua Yu5:
629599ec04SFenghua Yu	sub $0x20, %rdx
639599ec04SFenghua Yu	movq 0*8(%rsi), %r11
649599ec04SFenghua Yu	movq 1*8(%rsi), %r10
659599ec04SFenghua Yu	movq 2*8(%rsi), %r9
669599ec04SFenghua Yu	movq 3*8(%rsi), %r8
679599ec04SFenghua Yu	leaq 4*8(%rsi), %rsi
689599ec04SFenghua Yu
699599ec04SFenghua Yu	movq %r11, 0*8(%rdi)
709599ec04SFenghua Yu	movq %r10, 1*8(%rdi)
719599ec04SFenghua Yu	movq %r9, 2*8(%rdi)
729599ec04SFenghua Yu	movq %r8, 3*8(%rdi)
739599ec04SFenghua Yu	leaq 4*8(%rdi), %rdi
749599ec04SFenghua Yu	jae 5b
759599ec04SFenghua Yu	addq $0x20, %rdx
769599ec04SFenghua Yu	jmp 1f
779599ec04SFenghua Yu	/*
789599ec04SFenghua Yu	 * Handle data forward by movsq.
799599ec04SFenghua Yu	 */
809599ec04SFenghua Yu	.p2align 4
819599ec04SFenghua Yu4:
829599ec04SFenghua Yu	movq %rdx, %rcx
839599ec04SFenghua Yu	movq -8(%rsi, %rdx), %r11
849599ec04SFenghua Yu	lea -8(%rdi, %rdx), %r10
859599ec04SFenghua Yu	shrq $3, %rcx
869599ec04SFenghua Yu	rep movsq
879599ec04SFenghua Yu	movq %r11, (%r10)
889599ec04SFenghua Yu	jmp 13f
89057e05c1SFenghua Yu.Lmemmove_end_forward:
90057e05c1SFenghua Yu
919599ec04SFenghua Yu	/*
929599ec04SFenghua Yu	 * Handle data backward by movsq.
939599ec04SFenghua Yu	 */
949599ec04SFenghua Yu	.p2align 4
959599ec04SFenghua Yu7:
969599ec04SFenghua Yu	movq %rdx, %rcx
979599ec04SFenghua Yu	movq (%rsi), %r11
989599ec04SFenghua Yu	movq %rdi, %r10
999599ec04SFenghua Yu	leaq -8(%rsi, %rdx), %rsi
1009599ec04SFenghua Yu	leaq -8(%rdi, %rdx), %rdi
1019599ec04SFenghua Yu	shrq $3, %rcx
1029599ec04SFenghua Yu	std
1039599ec04SFenghua Yu	rep movsq
1049599ec04SFenghua Yu	cld
1059599ec04SFenghua Yu	movq %r11, (%r10)
1069599ec04SFenghua Yu	jmp 13f
1079599ec04SFenghua Yu
1089599ec04SFenghua Yu	/*
1099599ec04SFenghua Yu	 * Start to prepare for backward copy.
1109599ec04SFenghua Yu	 */
1119599ec04SFenghua Yu	.p2align 4
1129599ec04SFenghua Yu2:
1139599ec04SFenghua Yu	cmp $680, %rdx
1149599ec04SFenghua Yu	jb 6f
1159599ec04SFenghua Yu	cmp %dil, %sil
1169599ec04SFenghua Yu	je 7b
1179599ec04SFenghua Yu6:
1189599ec04SFenghua Yu	/*
1199599ec04SFenghua Yu	 * Calculate copy position to tail.
1209599ec04SFenghua Yu	 */
1219599ec04SFenghua Yu	addq %rdx, %rsi
1229599ec04SFenghua Yu	addq %rdx, %rdi
1239599ec04SFenghua Yu	subq $0x20, %rdx
1249599ec04SFenghua Yu	/*
125bb916ff7SAndy Shevchenko	 * We gobble 32 bytes backward in each loop.
1269599ec04SFenghua Yu	 */
1279599ec04SFenghua Yu8:
1289599ec04SFenghua Yu	subq $0x20, %rdx
1299599ec04SFenghua Yu	movq -1*8(%rsi), %r11
1309599ec04SFenghua Yu	movq -2*8(%rsi), %r10
1319599ec04SFenghua Yu	movq -3*8(%rsi), %r9
1329599ec04SFenghua Yu	movq -4*8(%rsi), %r8
1339599ec04SFenghua Yu	leaq -4*8(%rsi), %rsi
1349599ec04SFenghua Yu
1359599ec04SFenghua Yu	movq %r11, -1*8(%rdi)
1369599ec04SFenghua Yu	movq %r10, -2*8(%rdi)
1379599ec04SFenghua Yu	movq %r9, -3*8(%rdi)
1389599ec04SFenghua Yu	movq %r8, -4*8(%rdi)
1399599ec04SFenghua Yu	leaq -4*8(%rdi), %rdi
1409599ec04SFenghua Yu	jae 8b
1419599ec04SFenghua Yu	/*
1429599ec04SFenghua Yu	 * Calculate copy position to head.
1439599ec04SFenghua Yu	 */
1449599ec04SFenghua Yu	addq $0x20, %rdx
1459599ec04SFenghua Yu	subq %rdx, %rsi
1469599ec04SFenghua Yu	subq %rdx, %rdi
1479599ec04SFenghua Yu1:
1489599ec04SFenghua Yu	cmpq $16, %rdx
1499599ec04SFenghua Yu	jb 9f
1509599ec04SFenghua Yu	/*
1519599ec04SFenghua Yu	 * Move data from 16 bytes to 31 bytes.
1529599ec04SFenghua Yu	 */
1539599ec04SFenghua Yu	movq 0*8(%rsi), %r11
1549599ec04SFenghua Yu	movq 1*8(%rsi), %r10
1559599ec04SFenghua Yu	movq -2*8(%rsi, %rdx), %r9
1569599ec04SFenghua Yu	movq -1*8(%rsi, %rdx), %r8
1579599ec04SFenghua Yu	movq %r11, 0*8(%rdi)
1589599ec04SFenghua Yu	movq %r10, 1*8(%rdi)
1599599ec04SFenghua Yu	movq %r9, -2*8(%rdi, %rdx)
1609599ec04SFenghua Yu	movq %r8, -1*8(%rdi, %rdx)
1619599ec04SFenghua Yu	jmp 13f
1629599ec04SFenghua Yu	.p2align 4
1639599ec04SFenghua Yu9:
1649599ec04SFenghua Yu	cmpq $8, %rdx
1659599ec04SFenghua Yu	jb 10f
1669599ec04SFenghua Yu	/*
1679599ec04SFenghua Yu	 * Move data from 8 bytes to 15 bytes.
1689599ec04SFenghua Yu	 */
1699599ec04SFenghua Yu	movq 0*8(%rsi), %r11
1709599ec04SFenghua Yu	movq -1*8(%rsi, %rdx), %r10
1719599ec04SFenghua Yu	movq %r11, 0*8(%rdi)
1729599ec04SFenghua Yu	movq %r10, -1*8(%rdi, %rdx)
1739599ec04SFenghua Yu	jmp 13f
1749599ec04SFenghua Yu10:
1759599ec04SFenghua Yu	cmpq $4, %rdx
1769599ec04SFenghua Yu	jb 11f
1779599ec04SFenghua Yu	/*
1789599ec04SFenghua Yu	 * Move data from 4 bytes to 7 bytes.
1799599ec04SFenghua Yu	 */
1809599ec04SFenghua Yu	movl (%rsi), %r11d
1819599ec04SFenghua Yu	movl -4(%rsi, %rdx), %r10d
1829599ec04SFenghua Yu	movl %r11d, (%rdi)
1839599ec04SFenghua Yu	movl %r10d, -4(%rdi, %rdx)
1849599ec04SFenghua Yu	jmp 13f
1859599ec04SFenghua Yu11:
1869599ec04SFenghua Yu	cmp $2, %rdx
1879599ec04SFenghua Yu	jb 12f
1889599ec04SFenghua Yu	/*
1899599ec04SFenghua Yu	 * Move data from 2 bytes to 3 bytes.
1909599ec04SFenghua Yu	 */
1919599ec04SFenghua Yu	movw (%rsi), %r11w
1929599ec04SFenghua Yu	movw -2(%rsi, %rdx), %r10w
1939599ec04SFenghua Yu	movw %r11w, (%rdi)
1949599ec04SFenghua Yu	movw %r10w, -2(%rdi, %rdx)
1959599ec04SFenghua Yu	jmp 13f
1969599ec04SFenghua Yu12:
1979599ec04SFenghua Yu	cmp $1, %rdx
1989599ec04SFenghua Yu	jb 13f
1999599ec04SFenghua Yu	/*
2009599ec04SFenghua Yu	 * Move data for 1 byte.
2019599ec04SFenghua Yu	 */
2029599ec04SFenghua Yu	movb (%rsi), %r11b
2039599ec04SFenghua Yu	movb %r11b, (%rdi)
2049599ec04SFenghua Yu13:
2059599ec04SFenghua Yu	retq
2069599ec04SFenghua Yu	CFI_ENDPROC
207057e05c1SFenghua Yu
208057e05c1SFenghua Yu	.section .altinstr_replacement,"ax"
209057e05c1SFenghua Yu.Lmemmove_begin_forward_efs:
210057e05c1SFenghua Yu	/* Forward moving data. */
211057e05c1SFenghua Yu	movq %rdx, %rcx
212057e05c1SFenghua Yu	rep movsb
213057e05c1SFenghua Yu	retq
214057e05c1SFenghua Yu.Lmemmove_end_forward_efs:
215057e05c1SFenghua Yu	.previous
216057e05c1SFenghua Yu
217057e05c1SFenghua Yu	.section .altinstructions,"a"
21859e97e4dSAndy Lutomirski	altinstruction_entry .Lmemmove_begin_forward,		\
21959e97e4dSAndy Lutomirski		.Lmemmove_begin_forward_efs,X86_FEATURE_ERMS,	\
22059e97e4dSAndy Lutomirski		.Lmemmove_end_forward-.Lmemmove_begin_forward,	\
22159e97e4dSAndy Lutomirski		.Lmemmove_end_forward_efs-.Lmemmove_begin_forward_efs
222057e05c1SFenghua Yu	.previous
2239599ec04SFenghua YuENDPROC(memmove)
224