Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp3018887rdb; Tue, 13 Feb 2024 04:43:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVKdO6dtZazpfD87yl9sNoTXn2EFIm1ZTlT0RXo3oS6OO5kw5uU91gk04sAbIH/Sju6w6B2oDuLcNLBBypKG+FpgoOETgeDTl4Q3VGgIA== X-Google-Smtp-Source: AGHT+IFD9b8OcRiWgsa6BDZJzWYHB5zj+1+q3haL2EMORQvJSWG6zYzvLF3n5Oy6AXh2Zhh7fkCI X-Received: by 2002:a0c:f153:0:b0:68e:d1da:5e37 with SMTP id y19-20020a0cf153000000b0068ed1da5e37mr3086244qvl.16.1707828223344; Tue, 13 Feb 2024 04:43:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707828223; cv=pass; d=google.com; s=arc-20160816; b=OLW3+vKR5Sae4/CDd8qZDSnDtfLKr/ZOMrKePo+kqcWZZGKnLqDRCPw+QjCZKxJPPm Rv7iP0NDdI3j0Vh88wBnwR/KpjUMr3OrV/TfiQPzDuIDXO8/jT23Spx/gHYDMvICrfI1 hytquKBe0+XNPCRN8yfW4pTiOiTQuYEjdQfLhr+p++bsC4SGUzAQEeim2BMgw1LY3hb3 ljDVT5w8lmjOli2KXvl5CjH0fqc2ZJrtVYXg9vQJPhb/CtGEM9BV85vohmsO428OxuM3 wf7WpUrIhv4vjGWdeck6IdNWs5Z4pc0VtiMGuy2DXDQN4z4i+3AXaraKRIMmI66utiJQ VHVw== 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=eklaQMdtNqfp+JT1Cu4F0dtN+dys2QKIDEY8as5i074=; fh=f0jHjZ7rq4lOW1W6d5Yy2Mw//qkGR324pUru3RE0SVw=; b=FT2+vl+VkMSbACVow4MbznxOH/eOE2fKiAoxDBDSkZ2G2eDgbZs6ZRbV5EPQOumvO8 nc7wLr8A/JzxipmJ37a5BL83bMs5w91Gu+UPicAyL2t6Ms7LZXdb+fu0tGvffollAlEA nGT5rH+SBEE/hLA00eBciT8JFtXdqQOEhApoP85fyOps3qoEwB7fiSO72YWRKD+JgoXB 8jdFyKzhEmwjxAdgYV6+Lkd7rQ8GXdRICADmcpGG8+qR3LN0x6v3QE0MrNJm53JbgLE3 2OIk5kv5suI4z8/2pVnl2+rakwM7xzcGf0lho/ySIxrtxza9Jc80oxYSrbN6a87sksm2 8eeA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gEDkLskT; 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-63525-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63525-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCU+gEGXha1iWkJhQxJFxIAelWLk974OCB3hOHQdzpewugep1OvqY2x+CV+RtodTj1SDp6FRUG/O7TdIO5VyF54kVV/HADPtpQgqVsZT1A== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id iw6-20020a0562140f2600b0068ee3ac0160si1563012qvb.351.2024.02.13.04.43.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 04:43:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63525-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gEDkLskT; 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-63525-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63525-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 1111C1C2625A for ; Tue, 13 Feb 2024 12:43:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BABC552F9D; Tue, 13 Feb 2024 12:42:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gEDkLskT" Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (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 1625B52F65 for ; Tue, 13 Feb 2024 12:42:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707828136; cv=none; b=sovywcGt6q6WqromMYkAkn0HZYh0VnhhmzAaM0osDCL5v2x9fg69gdVBdrorzESoE6Yb1Hdt663p5XwCFADUflXJlaTsrSnI2ASvRPw00a07q8ssnWDtiiYkCUZyS2wSGzW+gqkYh11zf9KhMaNfP21CA4akJvUNKKrJjTD4oxQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707828136; c=relaxed/simple; bh=wDmKde0tt5a+Yu2oaRGJYKH5vQlM4NHmT0UJ0NId53U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SPd0Cs33FlB2NhkK+8BJgDTIksOLSyq/V5SjDn3Uhi3KD1K/3MTqLj/e4aAizKIChYLoy+Xj/oJssv01A5QlOSljALNXI4UjMhQNeG4s5mbPSXGdjSOesKflUZEaA9Ly/OYpQpC4luDGzUSbDzRzcJr+rFPWs70q8fNnfambPk0= 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=gEDkLskT; arc=none smtp.client-ip=209.85.221.73 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-f73.google.com with SMTP id ffacd0b85a97d-33b2875eaa9so1649141f8f.3 for ; Tue, 13 Feb 2024 04:42:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707828133; x=1708432933; 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=eklaQMdtNqfp+JT1Cu4F0dtN+dys2QKIDEY8as5i074=; b=gEDkLskTpAQmj210Vf1KkahynByQ/4C45xu+UjvPmjzxo2Z83CVEhxAGG5KtyblACz vqWvMYg57EG3tiHW2l7nK2D1yJSzCz0QI2vNP5wYiK4r6KBjJG0qpXXlFuSN0riBLjv2 Om4ZgURY5mLyt9aYT5Ag8aMlGF8VR2ltJaztm8ePydrw5EHVat4SLp7QtVkOVEihJY+t o/rUWZ3MoElQauVL2O+eXtKwutLVF+0RK1MkEevSTy7PbWeF2NTBzly6rheehrPYgVSM oNOjfBjXQni9rGnvgj94CIWeWA0JdP10OMquLl1PWMoauZ1/JILziYv9tC30YnfhRknd fJpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707828133; x=1708432933; 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=eklaQMdtNqfp+JT1Cu4F0dtN+dys2QKIDEY8as5i074=; b=cFXbNm5hM9cIQdo9WUdA6DNsJc505QvVFPfbnQwKj4tVhoUhoRZniYu9nF5X5mN2tA HTPLUjJdhedBKX+fsatt+f+DheJVh1gjE9fhqvi0r5hUr8TjChFG8dNMmWErMr7/9xvX oy/q+VxksyM5rCHA4TSxKnMiBHzNGAudNLnzXVtV8cPPdxYcdXggHByBM0SrHo2SoqSj MC2DDlBCJ+ohiGnceL+olyuPsxDYd51DrZD4VXBIK2L4o+tuO29eeQbZ269Djhk8IuSS 3hb3/9p+BKab2OGRns5n568Me2yxgAtZ9/bYEUp697bGNQf5VZtmzbro0ca74YxVDcNM Kjlw== X-Gm-Message-State: AOJu0YzKd6C2ilDj+a4jZ1L9Nri/VXcwobvReb3oyRGeEXogtOPd6N+X Iba/ebFp8jHhqrLO3Ve2uIalTUWaouORgR88JrhlzEYhyvuKen7Me5jmLzBpCK+qhUz3EulZ34i kMI0NM60pnAyDKXHkiK8QUNbH65dupd4pRbyxdKmewnJUZOLun+zWqoN3CC8T26GbMEet61D5Kj d0Pf2y4FJLQd1gjV40UKNzDjhGeuwbPA== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:600c:3f87:b0:40f:c77e:55d with SMTP id fs7-20020a05600c3f8700b0040fc77e055dmr50227wmb.6.1707828133083; Tue, 13 Feb 2024 04:42:13 -0800 (PST) Date: Tue, 13 Feb 2024 13:41:49 +0100 In-Reply-To: <20240213124143.1484862-13-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: <20240213124143.1484862-13-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2705; i=ardb@kernel.org; h=from:subject; bh=y4u3TSGDXd+Oc4OLRl0ssIJXn6VCieIfI1jk/RL3A6A=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIfV0cu+FuNtRM/c+mCxqoVS+ZmblR6mdWd1rg+e7MTeYT b8pnhfYUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACYyZwojQ6N35pVJP01mHeos +nPkABd/pusnc7vnqy/8vs/fJL/paRHDf7dWvRWzNq1zN994durenpnXXGO9TzcsSPmX7xXPIDF 7Pi8A X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240213124143.1484862-18-ardb+git@google.com> Subject: [PATCH v4 05/11] 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 , Kees Cook , Brian Gerst , linux-arch@vger.kernel.org, llvm@lists.linux.dev Content-Type: text/plain; charset="UTF-8" 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 implemented in a slightly confusing way, so 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 7e76cc0b442a..6dcc2f7f4108 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -111,13 +111,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 @@ -126,12 +124,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 @@ -171,18 +163,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: /* Create a mask of CR4 bits to preserve */ @@ -202,9 +194,6 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) btsl $X86_CR4_PSE_BIT, %ecx movq %rcx, %cr4 - /* Setup early boot stage 4-/5-level pagetables. */ - addq phys_base(%rip), %rax - /* * Switch to new page-table * -- 2.43.0.687.g38aa6559b0-goog