[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v6 4/7] x86: re-work memcpy()


  • To: Jan Beulich <jbeulich@xxxxxxxx>, "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Jason Andryuk <jason.andryuk@xxxxxxx>
  • Date: Tue, 24 Jun 2025 16:58:59 -0400
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=suse.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=6RO44iSbQZHsEat2MA7ef/QCZoLfnm7fnQZeluYpsxw=; b=B8KxRWFZfdhWV3nWWm6k99WL4zgtTBsCqbthzXL8yE+ngvfgLty/yyI+Q9s+iEuBR1xJw8RpF9yGcCpma7ewiEl9Z6uNRB+bT4oGQKa41EHvfGZTOQ88Eqx4HcRKHFrliV7RJ40vh8NwOep9u8QNbOS7R+ndTKawb29ic0JlhjGND2/TLPagzmTaQLyHyPPtAc07WXv/DyKeac3i50cnznjLQaelbmI0jzdKAROry/hdUgZf6Ovgh/0Jmh/uNnvN4fNOZ0fpU3OSOteEt0WD37ZW8w4jEhVNWviSdz2CpQXeJBGMQSCZTScYrsPxXQ70N/NgUEYxrZsf4OIBPNdDxQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=mYezYZ5i7PGC+U98A5ZXgF6HR4uEFaYMrRhc5+E0t1vy5mccOFjj/7DiJiMYpmxMpcu53mbFOimtnSj2b8E/1b6Gr57WUfXWwZAPPzDuWOCU/BbtcWXf5K/jvPul5AyBuq1K//Lu+bRlptxtsv8a1LF3Q2VZ4BQapNfVa6Bdewc0Sh+ZSvpuj2R6i3/zq6l8ZZ9D6z2O0E0cONOta3B58ivF81FaVtfaFWpeazP9YFSc+erSrRfGVU16nVBE8kiPFHjY98nWWexXL3ZbDH+SiyLD4mOWqIF5V7NhWmOvNE8GL3ZMosfnpiAcPhhT6zmiIKNYjK3nXawOxquTuIS1eA==
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>
  • Delivery-date: Tue, 24 Jun 2025 20:59:20 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 2025-06-16 09:00, Jan Beulich wrote:
Move the function to its own assembly file. Having it in C just for the
entire body to be an asm() isn't really helpful. Then have two flavors:
A "basic" version using qword steps for the bulk of the operation, and an
ERMS version for modern hardware, to be substituted in via alternatives
patching.

Alternatives patching, however, requires an extra precaution: It uses
memcpy() itself, and hence the function may patch itself. Luckily the
patched-in code only replaces the prolog of the original function. Make
sure this remains this way.

Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
Reviewed-by: Teddy Astie <teddy.astie@xxxxxxxxxx>

--- /dev/null
+++ b/xen/arch/x86/memcpy.S
@@ -0,0 +1,20 @@
+#include <asm/asm_defns.h>
+
+FUNC(memcpy)
+        mov     %rdx, %rcx
+        mov     %rdi, %rax
+        /*
+         * We need to be careful here: memcpy() is involved in alternatives
+         * patching, so the code doing the actual copying (i.e. past setting
+         * up registers) may not be subject to patching (unless further
+         * precautions were taken).
+         */

I think this phrasing is a little clearer:

We need to be careful here: memcpy() is involved in alternatives patching. Define the original code as only the register setup. The code doing the actual copying (i.e. past setting up registers) is not subject to patching, which avoids it changing underneath the processor.

Your comment is okay if you prefer not to change it:

Reviewed-by: Jason Andryuk <jason.andryuk@xxxxxxx>

Regards,
Jason

+        ALTERNATIVE "and $7, %edx; shr $3, %rcx", \
+                    STR(rep movsb; RET), X86_FEATURE_ERMS
+        rep movsq
+        or      %edx, %ecx
+        jz      1f
+        rep movsb
+1:
+        RET
+END(memcpy)



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.