From nobody Mon Feb 9 19:08:27 2026 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1638874460; cv=none; d=zohomail.com; s=zohoarc; b=MkGEyCgXVniVjSyxjAPtUafn3C/9RUNAWWj8Zbah8vHIftCbwPzM7Rd0D6jAJeqngc6YSblWd714w+5/S749zmeNWLrwOQqQahAJbPC2kIvj2m9SlO1VMLihOQaj4feIIvrE4HNINu7Ky7pMWVjnymhLL/rHAwB0ecSCMPSPNjY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1638874460; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=eh5kclOP2Fig+qgY1g+ClqOgJrbhcCABejmPXedKPv4=; b=L3M1FYbynyseXGkGV0bwkMPyKSK06841/IzBpDcdoo5F/32A6GbVJ8K4UgadNsvDS0Um5UVNhhcC5dnL3VVzV0BZPz0Jel3jjTzrm2A0VaPXYf3gDlxByW7WLELGK5lIqVey2tQur0G7DZxwifA59MtqZ+q8sqGaAIzRnf56r9I= 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=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1638874460121889.5399654647996; Tue, 7 Dec 2021 02:54:20 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.240875.417659 (Exim 4.92) (envelope-from ) id 1muY6o-0000Bo-L2; Tue, 07 Dec 2021 10:54:06 +0000 Received: by outflank-mailman (output) from mailman id 240875.417659; Tue, 07 Dec 2021 10:54: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 1muY6o-0000B6-Gb; Tue, 07 Dec 2021 10:54:06 +0000 Received: by outflank-mailman (input) for mailman id 240875; Tue, 07 Dec 2021 10:54:05 +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 1muY6n-0007ZY-IQ for xen-devel@lists.xenproject.org; Tue, 07 Dec 2021 10:54:05 +0000 Received: from esa5.hc3370-68.iphmx.com (esa5.hc3370-68.iphmx.com [216.71.155.168]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id fd4fab96-574b-11ec-a831-37629979565c; Tue, 07 Dec 2021 11:54:04 +0100 (CET) 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: fd4fab96-574b-11ec-a831-37629979565c DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1638874444; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Fbig8ZrshkFGK596jPWQZGyoDDWpC3fZiJfWDk+IDGU=; b=KIcx92tjufdGfKmXRg09ye9cDaVbXR4t9euHnMx3Km4sUGIBh0rlgDdi adKEEK8etJZXxl9H9IkaLeSb1rqBSDpSUAEF98nM4H5zXE8J2Rstxc5/D NuE2ssFeTMEO2SO7epIeWoRAk8cJOD4d50owB4aJp8o5ApP1ZcS2KkX0R Y=; Authentication-Results: esa5.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: OWEdGLEuOdn8Ms4IOSKYbFVQOki0Azv0nsLgY5gdxuAnngZ4mmE92Lj2m/dnqSkp6smVumcKEK /Uafh8pze9xYxI0w8wK6pG8Y1v0uoTmF6vM7iWTBATByQI6grGG4dYBaNZb8/d5EB3HAjVtgjD 5kaauncSMaQi93aHjUhQda55xmhcz60GJfkKQLYi0oDLYDWhnbi/rFwQHy4tr6MvI3uYOXPHqK CqMTlBGFX8oYFJFyYIPfB3lqSl5VEwlKzhnaLTArd5D6TNmW+1aruXIxQCNYjXpOGEAGaY+4R6 vF2Tsm/DV4MPjFqcPJTjOrpC X-SBRS: 5.1 X-MesageID: 58948462 X-Ironport-Server: esa5.hc3370-68.iphmx.com X-Remote-IP: 162.221.156.83 X-Policy: $RELAYED IronPort-Data: A9a23:t2zT2qMjybxlMUfvrR2dkMFynXyQoLVcMsEvi/4bfWQNrUom0zcDx 2YeDWqBaKmDMWf1ctglboS09BhU6JfSx9c1QQto+SlhQUwRpJueD7x1DKtR0wB+jCHnZBg6h ynLQoCYdKjYdpJYz/uUGuCJQUNUjMlkfZKhTr6bUsxNbVU8En5400o8w7RRbrNA2rBVPSvc4 bsenOWHULOV82Yc3rU8sv/rRLtH5ZweiRtA1rAMTakjUGz2zhH5OKk3N6CpR0YUd6EPdgKMq 0Qv+5nilo/R109F5tpICd8XeGVSKlLZFVDmZna7x8FOK/WNz8A/+v9TCRYSVatYozGooPVIl uxEiYCtVV87PqOVxeEQdzANRkmSPYUekFPGCX22sMjVxEzaaXr8hf5pCSnaP6VBpLwxWzsXs 6VFdnZdNXhvhMrvqF6/YsBqit4uM4/AO4QHt2s75TrYEewnUdbIRKCiCdpwgWdh2pASQ6y2i 8wxSQFfcxH6Oxt0JmgaNbRhlcOvpFWmWmgNwL6SjfVuuDWCpOBr65DyNPLF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Sj+f xG+85rrCMc6QxhPk3I9urhBEDtex/hHNtOkOws1NSZLW7bUQmTXeJfBOLZqlWKcUDDH6xmpM NdmsBFeaXN5DNB7PoSjjPWLz9Z+qjkzJyV X-IronPort-AV: E=Sophos;i="5.87,293,1631592000"; d="scan'208";a="58948462" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu Subject: [PATCH 3/3] x86/boot: Don't double-copy the stack during physical relocation Date: Tue, 7 Dec 2021 10:53:39 +0000 Message-ID: <20211207105339.28440-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20211207105339.28440-1-andrew.cooper3@citrix.com> References: <20211207105339.28440-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1638874462301100001 cpu0_stack is contained within .data, which means the memcpy() already take= s a snapshot at the start of the critical region. Later, when we switch to the relocated Xen, we do end up losing updates to = the local variables, but that's fine because the only variables we've modified = go out of scope after the printk(). Use this properly to avoid copying the wh= ole stack (32k) a second time. Signed-off-by: Andrew Cooper --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu Ever so slightly RFC, as it has only had light testing, but I'm confident in the reasoning. --- xen/arch/x86/setup.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index a6ff450daab7..c04c68a09b47 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -1183,6 +1183,7 @@ void __init noreturn __start_xen(unsigned long mbi_p) l3_pgentry_t *pl3e; l2_pgentry_t *pl2e; int i, j, k; + unsigned long tmp; =20 /* Select relocation address. */ xen_phys_start =3D end - reloc_size; @@ -1193,7 +1194,8 @@ void __init noreturn __start_xen(unsigned long mbi_p) * Perform relocation to new physical address. * Before doing so we must sync static/global data with main m= emory * with a barrier(). After this we must *not* modify static/gl= obal - * data until after we have switched to the relocated pagetabl= es! + * data, or locals that need to survive, until after we have + * switched to the relocated pagetables! */ barrier(); memcpy(__va(__pa(_start)), _start, _end - _start); @@ -1239,18 +1241,21 @@ void __init noreturn __start_xen(unsigned long mbi_= p) *pl2e =3D l2e_from_intpte(l2e_get_intpte(*pl2e) + xen_phys= _start); } =20 - /* Re-sync the stack and then switch to relocated pagetables. = */ + /* + * Switch to relocated pagetables. This also discards updates= to + * any local variables since the memmove() call above, but tha= t's + * fine because we don't use any of them again. + */ asm volatile ( - "rep movsq ; " /* re-sync the stack */ - "movq %%cr4,%%rsi ; " - "andb $0x7f,%%sil ; " - "movq %%rsi,%%cr4 ; " /* CR4.PGE =3D=3D 0 */ - "movq %[pg],%%cr3 ; " /* CR3 =3D=3D new pagetables */ - "orb $0x80,%%sil ; " - "movq %%rsi,%%cr4 " /* CR4.PGE =3D=3D 1 */ - : "=3D&S" (i), "=3D&D" (i), "=3D&c" (i) /* All outputs dis= carded. */ - : [pg] "r" (__pa(idle_pg_table)), "0" (cpu0_stack), - "1" (__va(__pa(cpu0_stack))), "2" (STACK_SIZE / 8) + "mov %%cr4, %[cr4]\n\t" + "andb $~%c[pge], %b[cr4]\n\t" + "mov %[cr4], %%cr4\n\t" /* CR4.PGE =3D 0 */ + "mov %[cr3], %%cr3\n\t" /* CR3 =3D new pagetables */ + "orb $%c[pge], %b[cr4]\n\t" + "mov %[cr4], %%cr4\n\t" /* CR4.PGE =3D 1 */ + : [cr4] "=3D&a" (tmp) /* Could be "r", but "a" makes bette= r asm */ + : [cr3] "r" (__pa(idle_pg_table)), + [pge] "i" (X86_CR4_PGE) : "memory" ); =20 printk("New Xen image base address: %#lx\n", xen_phys_start); --=20 2.11.0