From nobody Fri Oct 31 11:38:32 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750078882; cv=none; d=zohomail.com; s=zohoarc; b=R3QXnh178D+f4bg8Rr3+L7dzxtdu3TiX5kEZWEiYKPFi0mr5SzhrhUXb+ghaJACJGS2EsAo/DveacG02a02UZBqxzz9GrKyc5W6F8UDz1gXYW4v1j6nUKtU829FzgfsAF9dNb/MKMWXry/LNKIip1c7EoHNv6NMrvgOB3sZ6NjE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750078882; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=WQcA0jk9C0hbnGwgMgyk6/kBDd3xLT1I9c+U8NiS7QA=; b=WyzTrvArse0qVGKaPooqUjHnKd8Wkzf4hB03oFx72niAmMYpyvZReBj7vbMz/MK2B62JU2rkpS1vn6X4rSKdaypGj+WCD//IvQHhOhvzuoEs+QXl9LeM86MSPpH9XVe8o/10Y9M/nhppYTMfAYV6fPtYJA15XSDxL67DxwKhKe4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750078882773598.4924079914747; Mon, 16 Jun 2025 06:01:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1017228.1394200 (Exim 4.92) (envelope-from ) id 1uR9SU-0006PK-LN; Mon, 16 Jun 2025 13:01:06 +0000 Received: by outflank-mailman (output) from mailman id 1017228.1394200; Mon, 16 Jun 2025 13:01:06 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR9SU-0006PD-Gf; Mon, 16 Jun 2025 13:01:06 +0000 Received: by outflank-mailman (input) for mailman id 1017228; Mon, 16 Jun 2025 13:01:06 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR9SU-0003pW-7t for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 13:01:06 +0000 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [2a00:1450:4864:20::429]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id f5f27c6f-4ab1-11f0-b894-0df219b8e170; Mon, 16 Jun 2025 15:01:04 +0200 (CEST) Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-3a365a6804eso3189285f8f.3 for ; Mon, 16 Jun 2025 06:01:04 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-748900d7e0asm6939967b3a.174.2025.06.16.06.01.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Jun 2025 06:01:03 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f5f27c6f-4ab1-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1750078864; x=1750683664; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :references:cc:to:from:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=WQcA0jk9C0hbnGwgMgyk6/kBDd3xLT1I9c+U8NiS7QA=; b=EnFl/Lo0Dsq2zXASpFbbOcqQE7FWxD3vEprjfbu910qv3pzyDrXPENfEQg025sbWME dw/7C5hmV/a8630+Et0F162h2zMoOl11xp8v5DwR/cjYA8oly+wbKndBrFaGu7U1EjvH HeTud35xSWx+zHvPW47mBC9ukQFsMw2P29Iolwacxd+B26vFTR5T1DqoswaNnNRXtk0c 3Pf0bAi/ATh1gOEjegBOLXq7NaY8z3NNalzIAI2nsHMKC4hMwk81fMY8FdRdB3QBdDLT x2Dkh6Z36fJw0cwupXA8qejtwFdMapuibYslTzjv6SIa2EqAsgeqsYClblKMKG/geuri ibDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750078864; x=1750683664; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :references:cc:to:from:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WQcA0jk9C0hbnGwgMgyk6/kBDd3xLT1I9c+U8NiS7QA=; b=Na4jqUljAxMDCdy5lNlieASPD89SIghyYMzTTzSiiwbgtGE3pDDiag26ESyP1g7Fj+ TwZTNA7rORDC13mnIWnD5ZMyABSjgMpsHUui/vGyuBJQCu96NoaszZr8oSbja/s8A440 fpTKLrlx4N7//eEYaWNAB2dz4xcJ1bJX0RRnO+ef81bok+QJDjev9PVLcsedydNl2yIO dA62ZZVrUAnHW2c0Tz8tWXZw0he7EBH0t9s3htisebBthToJvA/pW0gN4HlGZGTQGdvr yCyIIXU5zvEt7u0G6aeff1F/BZsipewavs4QpnG/5egs4o6qLjv2vBSvsW5IGf70hX44 mEzQ== X-Gm-Message-State: AOJu0Yy2uAlgtVmAGZtuGHggx72MztRt8k0+0NTs5lcnkWKSg+RAmOmy vl2OAOzrwT8X7Xt+hRuji62I0OLETyqtdo/U8/8AC0IzFcd2faKPyoYRgnN00815AoyTYRSRanR ikQ4= X-Gm-Gg: ASbGnctQcwgFJvVdt3cyKgYzrwjbCxuujbedFllGjpUd3qJhmxicWUhebcII0Xi2ajL RZs1MqOhA4I8QDFYf55mazbzsC62KDugdgzPLVtbevgzZXO+DhJkcEGxqafu5a2jE5zazXkL7BM OIGQG6WM7tzdhZUGyRv5ScfBvDf1et+ffuxV5e83JNM5KtWDKiZwXzkuC4Lb4Zr7NbfELlLi+zI WUI8wJk66w7Wwy8hmGFAehz9qROJGUmC1oVf9VUbfSL1ds7JZtgQVL1BARlvlU+QLGR61S5AXAi fuvQVdvN7Imf/AojWd3l7mbs9wHLgQSkBjVvFdQ92Y6tWNbM9ioTZUepZWHE6vGif0cEyrvCnGK CGRcXccz8xiyXbvl8vSJsxmSxhbhxY82vc7uKmhhNpUxuBVY= X-Google-Smtp-Source: AGHT+IFf29AbfGPH26yutrwPeNXyRBv8TOMvSaOgP0uOAz18ppyA5lsUOZBFuNdhP7jBbzTRijBxzQ== X-Received: by 2002:a05:6000:290a:b0:3a4:f520:8bfc with SMTP id ffacd0b85a97d-3a5723a3b03mr6780273f8f.36.1750078863555; Mon, 16 Jun 2025 06:01:03 -0700 (PDT) Message-ID: <991c9caf-c7b6-4e1f-9855-5b2930b7989c@suse.com> Date: Mon, 16 Jun 2025 15:00:56 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v6 4/7] x86: re-work memcpy() From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= References: Content-Language: en-US Autocrypt: addr=jbeulich@su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IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1750078886512116600 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Teddy Astie Reviewed-by: Jason Andryuk --- We may want to consider branching over the REP MOVSQ as well, if the number of qwords turns out to be zero. We may also want to consider using non-REP MOVS{L,W,B} for the tail. TBD: We may further need a workaround similar to Linux'es 8ca97812c3c8 ("x86/mce: Work around an erratum on fast string copy instructions"). TBD: Some older AMD CPUs have an issue with REP MOVS when source and destination are misaligned with one another (modulo 32?), which may require a separate memcpy() flavor. --- v6: Move alternatives.c change to a separate patch. v5: Re-base. v4: Use CR2 write as serializing insn, and limit its use to boot time. v3: Re-base. --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -47,6 +47,7 @@ obj-$(CONFIG_RETURN_THUNK) +=3D indirect-t obj-$(CONFIG_PV) +=3D ioport_emulate.o obj-y +=3D irq.o obj-$(CONFIG_KEXEC) +=3D machine_kexec.o +obj-y +=3D memcpy.o obj-y +=3D memset.o obj-y +=3D mm.o x86_64/mm.o obj-$(CONFIG_VM_EVENT) +=3D monitor.o --- /dev/null +++ b/xen/arch/x86/memcpy.S @@ -0,0 +1,20 @@ +#include + +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 setti= ng + * up registers) may not be subject to patching (unless further + * precautions were taken). + */ + 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) --- a/xen/arch/x86/string.c +++ b/xen/arch/x86/string.c @@ -7,21 +7,6 @@ =20 #include =20 -void *(memcpy)(void *dest, const void *src, size_t n) -{ - long d0, d1, d2; - - asm volatile ( - " rep ; movs"__OS" ; " - " mov %k4,%k3 ; " - " rep ; movsb " - : "=3D&c" (d0), "=3D&D" (d1), "=3D&S" (d2) - : "0" (n/BYTES_PER_LONG), "r" (n%BYTES_PER_LONG), "1" (dest), "2" = (src) - : "memory" ); - - return dest; -} - void *(memmove)(void *dest, const void *src, size_t n) { long d0, d1, d2;