memcpy_64.S 3.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/* Copyright 2002 Andi Kleen */
2

3
#include <linux/linkage.h>
I
Ingo Molnar 已提交
4

5
#include <asm/cpufeature.h>
I
Ingo Molnar 已提交
6
#include <asm/dwarf2.h>
7

L
Linus Torvalds 已提交
8 9 10
/*
 * memcpy - Copy a memory block.
 *
I
Ingo Molnar 已提交
11 12 13 14 15
 * Input:
 *  rdi destination
 *  rsi source
 *  rdx count
 *
L
Linus Torvalds 已提交
16 17
 * Output:
 * rax original destination
I
Ingo Molnar 已提交
18
 */
L
Linus Torvalds 已提交
19

I
Ingo Molnar 已提交
20 21 22
/*
 * memcpy_c() - fast string ops (REP MOVSQ) based variant.
 *
23
 * This gets patched over the unrolled variant (below) via the
I
Ingo Molnar 已提交
24 25
 * alternative instructions framework:
 */
26 27
	.section .altinstr_replacement, "ax", @progbits
.Lmemcpy_c:
I
Ingo Molnar 已提交
28 29 30 31 32
	movq %rdi, %rax

	movl %edx, %ecx
	shrl $3, %ecx
	andl $7, %edx
33
	rep movsq
I
Ingo Molnar 已提交
34
	movl %edx, %ecx
35 36
	rep movsb
	ret
37 38
.Lmemcpy_e:
	.previous
39 40 41 42

ENTRY(__memcpy)
ENTRY(memcpy)
	CFI_STARTPROC
43
	movq %rdi, %rax
44

I
Ingo Molnar 已提交
45
	/*
46
	 * Use 32bit CMP here to avoid long NOP padding.
I
Ingo Molnar 已提交
47
	 */
48 49
	cmp  $0x20, %edx
	jb .Lhandle_tail
50

I
Ingo Molnar 已提交
51
	/*
52 53
	 * We check whether memory false dependece could occur,
	 * then jump to corresponding copy mode.
I
Ingo Molnar 已提交
54
	 */
55 56 57 58 59
	cmp  %dil, %sil
	jl .Lcopy_backward
	subl $0x20, %edx
.Lcopy_forward_loop:
	subq $0x20,	%rdx
60

I
Ingo Molnar 已提交
61
	/*
62
	 * Move in blocks of 4x8 bytes:
I
Ingo Molnar 已提交
63
	 */
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	movq 0*8(%rsi),	%r8
	movq 1*8(%rsi),	%r9
	movq 2*8(%rsi),	%r10
	movq 3*8(%rsi),	%r11
	leaq 4*8(%rsi),	%rsi

	movq %r8,	0*8(%rdi)
	movq %r9,	1*8(%rdi)
	movq %r10,	2*8(%rdi)
	movq %r11,	3*8(%rdi)
	leaq 4*8(%rdi),	%rdi
	jae  .Lcopy_forward_loop
	addq $0x20,	%rdx
	jmp  .Lhandle_tail

.Lcopy_backward:
	/*
	 * Calculate copy position to tail.
	 */
	addq %rdx,	%rsi
	addq %rdx,	%rdi
	subq $0x20,	%rdx
	/*
	 * At most 3 ALU operations in one cycle,
	 * so append NOPS in the same 16bytes trunk.
	 */
	.p2align 4
.Lcopy_backward_loop:
	subq $0x20,	%rdx
	movq -1*8(%rsi),	%r8
	movq -2*8(%rsi),	%r9
	movq -3*8(%rsi),	%r10
	movq -4*8(%rsi),	%r11
	leaq -4*8(%rsi),	%rsi
	movq %r8,		-1*8(%rdi)
	movq %r9,		-2*8(%rdi)
	movq %r10,		-3*8(%rdi)
	movq %r11,		-4*8(%rdi)
	leaq -4*8(%rdi),	%rdi
	jae  .Lcopy_backward_loop
104

105 106 107 108 109 110
	/*
	 * Calculate copy position to head.
	 */
	addq $0x20,	%rdx
	subq %rdx,	%rsi
	subq %rdx,	%rdi
111
.Lhandle_tail:
112 113
	cmpq $16,	%rdx
	jb   .Lless_16bytes
I
Ingo Molnar 已提交
114

115 116 117 118 119 120 121 122 123 124 125 126
	/*
	 * Move data from 16 bytes to 31 bytes.
	 */
	movq 0*8(%rsi), %r8
	movq 1*8(%rsi),	%r9
	movq -2*8(%rsi, %rdx),	%r10
	movq -1*8(%rsi, %rdx),	%r11
	movq %r8,	0*8(%rdi)
	movq %r9,	1*8(%rdi)
	movq %r10,	-2*8(%rdi, %rdx)
	movq %r11,	-1*8(%rdi, %rdx)
	retq
127
	.p2align 4
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
.Lless_16bytes:
	cmpq $8,	%rdx
	jb   .Lless_8bytes
	/*
	 * Move data from 8 bytes to 15 bytes.
	 */
	movq 0*8(%rsi),	%r8
	movq -1*8(%rsi, %rdx),	%r9
	movq %r8,	0*8(%rdi)
	movq %r9,	-1*8(%rdi, %rdx)
	retq
	.p2align 4
.Lless_8bytes:
	cmpq $4,	%rdx
	jb   .Lless_3bytes
I
Ingo Molnar 已提交
143

144 145 146 147 148 149 150 151
	/*
	 * Move data from 4 bytes to 7 bytes.
	 */
	movl (%rsi), %ecx
	movl -4(%rsi, %rdx), %r8d
	movl %ecx, (%rdi)
	movl %r8d, -4(%rdi, %rdx)
	retq
152
	.p2align 4
153 154 155 156 157 158
.Lless_3bytes:
	cmpl $0, %edx
	je .Lend
	/*
	 * Move data from 1 bytes to 3 bytes.
	 */
159
.Lloop_1:
I
Ingo Molnar 已提交
160 161
	movb (%rsi), %r8b
	movb %r8b, (%rdi)
162 163
	incq %rdi
	incq %rsi
164
	decl %edx
165 166
	jnz .Lloop_1

I
Ingo Molnar 已提交
167
.Lend:
168
	retq
169 170 171
	CFI_ENDPROC
ENDPROC(memcpy)
ENDPROC(__memcpy)
172

I
Ingo Molnar 已提交
173 174 175 176
	/*
	 * Some CPUs run faster using the string copy instructions.
	 * It is also a lot simpler. Use this when possible:
	 */
177

I
Ingo Molnar 已提交
178
	.section .altinstructions, "a"
179
	.align 8
180
	.quad memcpy
181
	.quad .Lmemcpy_c
182
	.word X86_FEATURE_REP_GOOD
I
Ingo Molnar 已提交
183 184 185 186 187 188

	/*
	 * Replace only beginning, memcpy is used to apply alternatives,
	 * so it is silly to overwrite itself with nops - reboot is the
	 * only outcome...
	 */
189 190
	.byte .Lmemcpy_e - .Lmemcpy_c
	.byte .Lmemcpy_e - .Lmemcpy_c
191
	.previous