xref: /aosp_15_r20/external/coreboot/src/arch/x86/memcpy.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <string.h>
4 #include <stdbool.h>
5 #include <asan.h>
6 
memcpy(void * dest,const void * src,size_t n)7 void *memcpy(void *dest, const void *src, size_t n)
8 {
9 	unsigned long d0, d1, d2;
10 
11 #if (ENV_SEPARATE_ROMSTAGE && CONFIG(ASAN_IN_ROMSTAGE)) || \
12 		(ENV_RAMSTAGE && CONFIG(ASAN_IN_RAMSTAGE))
13 	check_memory_region((unsigned long)src, n, false, _RET_IP_);
14 	check_memory_region((unsigned long)dest, n, true, _RET_IP_);
15 #endif
16 
17 #if ENV_X86_64
18 	asm volatile(
19 		"rep ; movsq\n\t"
20 		"mov %4,%%rcx\n\t"
21 		"rep ; movsb\n\t"
22 		: "=&c" (d0), "=&D" (d1), "=&S" (d2)
23 		: "0" (n >> 3), "g" (n & 7), "1" (dest), "2" (src)
24 		: "memory"
25 	);
26 #else
27 	asm volatile(
28 		"rep ; movsl\n\t"
29 		"movl %4,%%ecx\n\t"
30 		"rep ; movsb\n\t"
31 		: "=&c" (d0), "=&D" (d1), "=&S" (d2)
32 		: "0" (n >> 2), "g" (n & 3), "1" (dest), "2" (src)
33 		: "memory"
34 	);
35 #endif
36 
37 
38 	return dest;
39 }
40