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

From: David Laight

Date: Sat Nov 14 2026 - 17:45:21 EST


On Fri, 13 Nov 2026 18:21:23 -0800
"H. Peter Anvin" <hpa@xxxxxxxxx> wrote:

> On November 13, 2026 3:37:03 PM PST, Borislav Petkov <bp@xxxxxxxxx> wrote:
> >On Fri, Nov 13, 2026 at 10:36:30AM -0800, H. Peter Anvin wrote:
> >> Well, we can put a request in for compiler support. This equally applies to
> >> other compiler runtime functions, for that matter...
> >
> >I was going to suggest that too. Lemme know if I should ping my gcc folks...
> >
>
> Seems like a good idea to me. Presumably it would be using the rep movsb convention.

You probably want at least two scratch registers and to leave any value in
rdi, rsi and rcx.
Otherwise you'll need to spill to stack for even a simple byte copy loop.
But yes, you want to be able to patch 'rep movsb; nop; nop' over the call.

Two registers might even be enough, one to hold a 'remainder' length and
one for the value being copied.
I think this loop should run at 1 write per clock (two clocks per
iteration):
void _memcpy(void *dst, const void *src, u64 len)
{
u64 val, rem = len & 15;
len &= ~15;
if (len == 0)
goto short_copy;
dst += len;
src += len;
len = -len;
do {
val = src[len];
dst[len] = val;
val = src[len + 8];
dst[len + 8] = val;
} while (len += 16);
/* copy trailing bytes */
val = src[rem - 16];
dst[rem - 16] = val;
val = src[rem - 8];
dst[rem - 8] = val;
return;
short_copy:
...
}

I really need to dig out my memcpy() cycle counting code again.

David