1// Copyright 2016 The Fuchsia Authors
2//
3// Use of this source code is governed by a MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT
6
7#include <asm.h>
8#include <lib/code_patching.h>
9
10.text
11
12// memcpy implementation relying on Intel's Enhanced REP MOVSB optimization
13// %rax = memcpy_erms(%rdi, %rsi, %rdx)
14FUNCTION(memcpy_erms)
15    // Save return value.
16    mov %rdi, %rax
17
18    mov %rdx, %rcx
19    rep movsb // while (rcx-- > 0) *rdi++ = *rsi++; /* rdi, rsi are uint8_t* */
20    ret
21END_FUNCTION(memcpy_erms)
22
23// memcpy implementation that copies 8 bytes at a time when possible
24// %rax = memcpy_quad(%rdi, %rsi, %rdx)
25FUNCTION(memcpy_quad)
26    // Save return value.
27    mov %rdi, %rax
28
29    // Copy all of the 8 byte chunks we can
30    mov %rdx, %rcx
31    shr $3, %rcx
32    rep movsq // while (rcx-- > 0) { *rdi++ = *rsi++; /* rdi, rsi are uint64_t* */ }
33
34    // Copy the rest
35    mov %rdx, %rcx
36    and $0x7, %rcx
37    rep movsb
38    ret
39END_FUNCTION(memcpy_quad)
40
41FUNCTION(memcpy)
42    jmp memcpy_erms
43    APPLY_CODE_PATCH_FUNC_WITH_DEFAULT(x86_memcpy_select, memcpy, 2)
44END_FUNCTION(memcpy)
45