Re: [PATCH v3 2/2] x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM

From: David Laight

Date: Fri Nov 13 2026 - 11:24:00 EST


On Fri, 13 Nov 2026 06:11:36 -0800
Usama Arif <usama.arif@xxxxxxxxx> wrote:

> How about something like what I have added at the end, would that be more
> acceptable in terms of maintainability?

I was thinking more of something like:
(written in the email so untested)

static always_inline void *memcpy(void *dst, const void *src, size_t len)
{
void *orig_dst = dst;

asm volatile( ".pushsection ".memcpy_calls\n"
"\t.long . - 1f\n"
".popsection\n"
"1:\trep movsb\n"
"\tnop; nop"
: "+c (len), "+D" (dst), "+S" (src)
:: ASM_CALL_CONSTRAINT);

return orig_dst;
}

An unused return value will be optimised away at all the call sites.

That will work (maybe slowly) on all cpu.
Then some code reads all the addresses from .memcpy_calls and, if necessary,
patches the 5 bytes to a call to the appropriate function.

It might be worth giving the code one or two scratch registers so that
a short copy loop doesn't need to spill to stack (perhaps %rax and %rdx).
It'll matter less for a longer copy loop.

So you might have:
_memcpy_erms:
cmp $nnnn, %rcx
jb _memcpy_words
rep movsb
RET

_memcpy_words:
cmpb $nnnn %rcx
jb _memcpy_bytes
mov %rax,%rcx
or %rcx,%rdi
or %rcx,%rsi
test $7,%rcx
jnz _memcpy_misaligned
sub $8,%rax
1: mov %rcx,[%rsi]
mov %rdx,8[%rsi]
mov [%rdi],%rcx
mov 8[%rdi],%rdx
add $16,%rdi
add $16,%rsi
sub $16,%rax
jnc 1b:
jz 2f
mov %rax,[%si]
mov [%rdi]%rax]
2: RET

That isn't the best 2-word loop by any means.
And you may not care about misaligned pointers.

etc

David