From patchwork Thu Jan 25 11:28:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 192015 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp1572919dyi; Thu, 25 Jan 2024 03:33:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IGN4dQ8+l0FJLJ0qOzvOOe/KhgVu8eKU2Nk3wngm81QJ3sh4SyoYQoYCeub11ND4wqciiTF X-Received: by 2002:a2e:a40c:0:b0:2ce:fa57:4dd0 with SMTP id p12-20020a2ea40c000000b002cefa574dd0mr476471ljn.11.1706182418084; Thu, 25 Jan 2024 03:33:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706182418; cv=pass; d=google.com; s=arc-20160816; b=K9j+6VcLhvdWFDAoKVy8FA5lpcJlMpwyCVbWgV7Fd4Xedgna/UCzFfghy2aDLMjIB4 LhoXBIXQBJbfKXCquvPmvjQSj+LvcTK+u6v+EbwDSXg0JDGiPFFhgTXnb6zVCSVyjsqF F5dvRQku3hoDK6Vah/o0sOKvN1F9vE02tJLDbUCcK0iFikvTH/GfghLzfWSj65DXLoBp DVyc1DEulR1w9yA6O9oGoqv7HRVV2hFCVLNViOEFIBAmgDantVizQPVEsIgNDh5bTHz9 Munkc0pCdN4ddfHebf8WPeVjCNNgQl3n9tYQjZy8QW0cE1wJ7BlymEUOUpCXJ0bmpZa2 H7xw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=iHVLq9Wa6m8PRjV68aukFNmIoFWxPV4eVUMjwp/oWNk=; fh=Mjd69IxHltS/Jba8SYEPy4aDMrScq2KBDxG2XjETtWk=; b=nbzg1Yr+E+6AsZi+1H+2AS8u9u40NnDLPj6hVZUEdNMyWXjSJL1S95vIvovTOZkIDl gqbWGtucmFiXG9YUHArbIt12Ordw/p0sBX1PRGVlkMBYMMIDT3eO7+N3vxUVj34yZER4 3osX2ltZb1FpD4qnPsbpofzs2BP1NBzJ99t7oKUb8GtTPMuuj0QpFT7Hu4EjNOUaTaua SZrAHCh8FC2LOo/ADO5N60h/bq5ujfkdt74qD7SzoQc/5tqw6IljjbpONoUMKzusC+Xa qqI2+Hpnz/duh+i/aSDq+UzVCokwLsZOcK0Zcu548HmbFZaJSyG7JyaYxX7QVw3Nw4qx 1jLQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=le7noE1q; arc=pass (i=1 spf=pass spfdomain=flex--ardb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-38506-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38506-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id h9-20020a056402280900b0055c5ca0f656si3661527ede.503.2024.01.25.03.33.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 03:33:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-38506-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=le7noE1q; arc=pass (i=1 spf=pass spfdomain=flex--ardb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-38506-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38506-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 83A5C1F23D62 for ; Thu, 25 Jan 2024 11:33:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B37DD4594C; Thu, 25 Jan 2024 11:32:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="le7noE1q" Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 152D833CC9 for ; Thu, 25 Jan 2024 11:32:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706182365; cv=none; b=idXcCyHPX2eCNwbSIp1uP17KCif0FqBa9C5bsiXZBdua4P0ookbyb1ibPsYoWFUNMLYcD63JxhiEcqrk1zdaBCulPQURDh4xFrGYdisOY/sVhpZkjuraOrSsNfxKM+Q9EzGERACUouJpDm4JWqkCSQp531gdIW0HcB7dzyqWfxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706182365; c=relaxed/simple; bh=NfXCrvSrRHu5/o/a38jIuQQEOM5CDJx3GD3fZgnkl0s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gmQZqiSXxKi01UiCHOsti2wYkpuH7wmPkEYaOGFOyz9YyA6u92asm08MPWw1upeDz7IF/JOGRR3TX5+m63VAOFbhN6wBIzmmOo40V+6bkwBjfEsUQZKH+zLGHZ8WyRcuOOugmhqpsQTC9UK0BNoYKUjqC7O16fPbtlLK6lzlte0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=le7noE1q; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3392c5e6dcdso249030f8f.0 for ; Thu, 25 Jan 2024 03:32:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706182362; x=1706787162; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iHVLq9Wa6m8PRjV68aukFNmIoFWxPV4eVUMjwp/oWNk=; b=le7noE1qf7mBa1wSBSLwLBSR76Pa9kx7N7YBkZGuTxppl23kHAp6gMLMWmAOvoeKEL DEVQTKZt21rJCv5Sp4O8o02+tEGPAawBRpOaLN2GIXyArDf4H0e5HuIIuUgDy9kSOnyx oTg81+DcEv3iwigMp7lBwpKw1gO7koP+yxrCC5205dutFJRoYqK41P3zR563vQdpaRAC emzZM9lCrfvEgFQd+hFPEJu4Fl+fDLmrNLRR3ap6t19ILXLptkxVRE13cMCv6YhQMlm3 YcFj5tzSdUjk7frSunXiFfx7VXm2G+xalp+5nkwllU95ZvJ8VA/n7APcnkJADVOQE6Zb SmPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706182362; x=1706787162; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iHVLq9Wa6m8PRjV68aukFNmIoFWxPV4eVUMjwp/oWNk=; b=p76YVR31MrdAZWDZL2cygahOq877wu7eA/+WBdWa3ZZbJQRqV6B09MsF/Bhf2Z5p9X FQppvIvMC9HB2kksOuYRHIru9qOzYFZb43d7VLQ0FNK9yS96VdgEM7DkALp6/3AQaYGt lWHh3PSOL/1pmJyl3luGGrYBjD+BgNTFRTBFk6+PWZGtMOnK/Ud/QpgSCnh1SB+rZlpQ mI3Ie38f0a+NFFusK104LxFFyQBgBPPgDKPtPutFxXMFCECrryMMHOFOZgk4eq/X6Y1Y Qd0wUdraYROjPbf0mzxr1dUtFk85jeQ+CN8pR21y1cwMOeGh76liQCTlNqD/iKw6zk3x NS8w== X-Gm-Message-State: AOJu0YwsK7084vSiZ0KtiGXK2c6eHUyY8wDT5YIOVyj6CTfxMhiCmw6r djad5Va6UA+qGZ9hLSYXKd7BZNZpnWU2e5ISHAVXGi7zk7kf/+lzafm9KzLGLiZWOvVa0cjKGxM CUe4Ula3Jjw9FWEXrbL9EnGZ9jNsut11yZRsBklGlaCVBIxtxY+tKZx2l2iB540bdXxBb7e4PQE bCIYLGH5aInsl+O7etZCTLt7JH0biy8Q== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:600c:3489:b0:40e:d20f:6e43 with SMTP id a9-20020a05600c348900b0040ed20f6e43mr21711wmq.1.1706182362257; Thu, 25 Jan 2024 03:32:42 -0800 (PST) Date: Thu, 25 Jan 2024 12:28:21 +0100 In-Reply-To: <20240125112818.2016733-19-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240125112818.2016733-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2687; i=ardb@kernel.org; h=from:subject; bh=XmOltJN4+bmiY4MWHeNLYuJPJ6TOu7DKSx2rFcdng6s=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIXWT63We1716198s/rI9xig2a4ogw49NN/7UfS092zFnr vG19M3vO0pZGMQ4GGTFFFkEZv99t/P0RKla51myMHNYmUCGMHBxCsBE5M0YGfpWObw/1+Pu4DHj O3NQwTOG9GOlO/nK+iwWHXXX36D7O43hn7nK/YsyjduO3U2/XPBoM3+7+M7T4tcKePreLt7y9RX 3D0YA X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240125112818.2016733-21-ardb+git@google.com> Subject: [PATCH v2 02/17] x86/startup_64: Simplify calculation of initial page table address From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Kevin Loughlin , Tom Lendacky , Dionna Glaze , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Andy Lutomirski , Arnd Bergmann , Nathan Chancellor , Nick Desaulniers , Justin Stitt , Brian Gerst , linux-arch@vger.kernel.org, llvm@lists.linux.dev X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789061935272076112 X-GMAIL-MSGID: 1789061935272076112 From: Ard Biesheuvel Determining the address of the initial page table to program into CR3 involves: - taking the physical address - adding the SME encryption mask On the primary entry path, the code is mapped using a 1:1 virtual to physical translation, so the physical address can be taken directly using a RIP-relative LEA instruction. On the secondary entry path, the address can be obtained by taking the offset from the virtual kernel base (__START_kernel_map) and adding the physical kernel base. This is all very straight-forward, but the current code makes a mess of this. Clean this up. Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/head_64.S | 25 ++++++-------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 4017a49d7b76..6d24c2014759 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -113,13 +113,11 @@ SYM_CODE_START_NOALIGN(startup_64) call __startup_64 /* Form the CR3 value being sure to include the CR3 modifier */ - addq $(early_top_pgt - __START_KERNEL_map), %rax + leaq early_top_pgt(%rip), %rcx + addq %rcx, %rax #ifdef CONFIG_AMD_MEM_ENCRYPT mov %rax, %rdi - mov %rax, %r14 - - addq phys_base(%rip), %rdi /* * For SEV guests: Verify that the C-bit is correct. A malicious @@ -128,12 +126,6 @@ SYM_CODE_START_NOALIGN(startup_64) * the next RET instruction. */ call sev_verify_cbit - - /* - * Restore CR3 value without the phys_base which will be added - * below, before writing %cr3. - */ - mov %r14, %rax #endif jmp 1f @@ -173,18 +165,18 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Clear %R15 which holds the boot_params pointer on the boot CPU */ xorq %r15, %r15 + /* Derive the runtime physical address of init_top_pgt[] */ + movq phys_base(%rip), %rax + addq $(init_top_pgt - __START_KERNEL_map), %rax + /* * Retrieve the modifier (SME encryption mask if SME is active) to be * added to the initial pgdir entry that will be programmed into CR3. */ #ifdef CONFIG_AMD_MEM_ENCRYPT - movq sme_me_mask, %rax -#else - xorq %rax, %rax + addq sme_me_mask(%rip), %rax #endif - /* Form the CR3 value being sure to include the CR3 modifier */ - addq $(init_top_pgt - __START_KERNEL_map), %rax 1: #ifdef CONFIG_X86_MCE @@ -211,9 +203,6 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) #endif movq %rcx, %cr4 - /* Setup early boot stage 4-/5-level pagetables. */ - addq phys_base(%rip), %rax - /* * Switch to new page-table *