Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp552181rdb; Mon, 29 Jan 2024 10:08:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IH1XrIK2XE5lMGcW1Ai43HfmabQyXKX3UUHCOmjatc3Z54XLr3HtpSCEvJ4M/AdzwTphHWx X-Received: by 2002:a17:903:5ce:b0:1d7:836d:7b3f with SMTP id kf14-20020a17090305ce00b001d7836d7b3fmr4996921plb.9.1706551682880; Mon, 29 Jan 2024 10:08:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706551682; cv=pass; d=google.com; s=arc-20160816; b=ckXClSTZk+o+8NCU7JLp3HKHngVmDAY8otMWobhsbyR6xYAJo9NaP2tK8XJjTtlE5J pmPRJu1zHwpljHieNNBssKQ8JrlEaV2ggerHMDGtpMxyIlOHLEa1ODqWlPrFaC3EEr4S 9/FthXPFKHsPOfeyjypqM2o/LHodwhm8FKgB9uMtkXgR12cM6m/6WhxRc8Xlh4GUesay SK396N4E1qUgaaB951Yjt+Bq3zQbq3jVzAokmFXwjbEgVpM/kf2VRaAWIpbIqqdtUczd 5srEe+Z6Ae2J8yYNoorWKQMwf4yPE+fG8DNSA8IO2PrKADIy7EzK8vY4FQCeDM1k4Nbe Td5w== 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=fMhMPvo8duafGkM4xZHdH7fULbau3etefTGsKa+Qy7I=; b=Czc6CzovKsPnmgnSEqoCLTi5HMtGVgwvtejrXo1azLvjX1n6hP6Dsghfic66T6U/9O GHAMC51S+DAdV16uE3JyY40ToL1YjcHmR3Dfc5WQjA2CtiK2mwgWZYSopAZTtpZTPCZJ 5JY1FY06kYpCBdFFVIMM9J4BtYMh7x0o2y8b3SCKTj+OrNLyGax7XGYPMUhko/ME0Ec5 /4AdY/cimf8s8qFQD2itdfvI0Kr0CW56AMEtJGZ8oLMeSAT2NN8te+rtVSaAaE4Kawa7 PpT/gAd2pbooKbqZ7c80nx1UythNpikLPXpHJdZv9vvT7y1/zreGSImT/HETOBtjTboy 88xQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MX9uMOEP; 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-43271-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43271-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id e2-20020a170902ed8200b001d78c9a13b7si5802215plj.528.2024.01.29.10.08.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 10:08:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-43271-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MX9uMOEP; 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-43271-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43271-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 61231284813 for ; Mon, 29 Jan 2024 18:06:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BB5267605F; Mon, 29 Jan 2024 18:05:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MX9uMOEP" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 EBA3F76042 for ; Mon, 29 Jan 2024 18:05:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706551541; cv=none; b=f3g24PxsWqbQ66xxYKsojs9r+E+VxEgzdSaIBU0dqjkWoj52yZwvnHuCPq/nYsM+pefajZDR5adtezCM9HJo+OraO9dthv+ItwZvkAdOFwKUtjzSUKbt+2HAbI5R4GzeYvXYuSq8HZWupzya35+/qMFP0URRcS2BBV14/kfw+FM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706551541; c=relaxed/simple; bh=NfXCrvSrRHu5/o/a38jIuQQEOM5CDJx3GD3fZgnkl0s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=S7elWN3q9JG90DNhaU04iQC46g9MC0O9bWFbX+K1vYNP9kGVK1JLv5pD2G7ScL3zgtvIy6S0hzBbVuZLWIvv6O1nx/1N36f59fMIuDZ1mXoehrigM7llZ3BlTj5EMJAMwuS5ML2Mr7RQDe9mFjoaSLx8JLd+O/UqRwTLd41z2mk= 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=MX9uMOEP; arc=none smtp.client-ip=209.85.128.201 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-yw1-f201.google.com with SMTP id 00721157ae682-5f874219ff9so39678257b3.0 for ; Mon, 29 Jan 2024 10:05:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706551538; x=1707156338; 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=MX9uMOEPvqCEzuFwVNq0sK6GsmH4zreaoZnLFIPuSq2JEuMNrYywE6eiti9DFDJQAd +ZDzDoAg54m6NW5y5DLrHlQZryjay+LdBDLUGUWP5VdV3C6ey4LELYpkUs9fZ2wqrGY9 pO7Gf/q2oKFFAd1ANNHlPDPXik++WcaTbMZbdQVb5fb7aqvH8a8eIiwDCzmCWKMbdSMg /Nz4gpIVvHvJUaGP9wCky3ed4DoR2qxK7Bbu6WrCkIcL2tfJdHwthY1WSP/13NPtSl/1 kZhTHVGYF15Xawdl8H0Q5a9b3mpYupt45KPnfQBDzu+zjcgIqNFWscDaawS6MLEPyBjE sdeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706551538; x=1707156338; 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=EGsm2xPcUoWRKQj+yIFN3HaDQQbQJbBpNHPxefzlv+W0u2mnNypjSJo7GWpQDxSW78 lYBgjkJjhmfPVbNshCO2UFHQU3zJPMQkznXSc/nmLmevpXySEfldjqgOUW6zjgklPKeg 1J7Xmx3a0iEGjAfvAubEK2AipHez23QohMlDdVxzMYoZw3XKXuJdIdxwAOEXp7IOIdyX QI7vrW5PErI7wDycORu3pOWbbDUhfJd+LHtEAhRA78xaHI2EdTpoxOTxWphIC4Zt/mer 3RgItdLtPb3Xyn+6Gfu/SKWA1MoGnV86t4RHRqSVNo9JMx6/Q3p2K299OZlmxFiUwzhd Zqww== X-Gm-Message-State: AOJu0Yw53PNALJUHP2wrcnId0vE72RKULLeIf3DIYri4HaxCwI3IEO0h 7WZlFscnKnl1kzIMg4XwA5PK5zuG+9BbXuPbFOu287O5bHqUoAi7xglYvm4jyM4x5ZtMUYdxtRR aXOWvjs/k5rU/w/O7w+JhBh4eQXuBj67MfH4hv5OchXfjW+xHmEj6I7OP4L/OISr1BDav2dGVkj 99fZiBNIu1Gn6/fc6c1DC+93R3UoFU6g== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a81:9991:0:b0:5ff:6ec3:b8da with SMTP id q139-20020a819991000000b005ff6ec3b8damr1836537ywg.1.1706551538650; Mon, 29 Jan 2024 10:05:38 -0800 (PST) Date: Mon, 29 Jan 2024 19:05:07 +0100 In-Reply-To: <20240129180502.4069817-21-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: <20240129180502.4069817-21-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=owGbwMvMwCFmkMcZplerG8N4Wi2JIXX7i8s8r3v1rr9Z/GV7jFFs1hRBhh+bbvyp+1p6tmPOX ONr6Zvfd5SyMIhxMMiKKbIIzP77bufpiVK1zrNkYeawMoEMYeDiFICJfDjF8Feyh+FfQIWc0h5F mWnGdoteLW5T478Teqbpf/okTp82pz5GhoaqDJOa37f3eH7suSHndEf1SL5VeaCY8gMVjT/qvzf rcQAA X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240129180502.4069817-25-ardb+git@google.com> Subject: [PATCH v3 04/19] 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 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 * -- 2.43.0.429.g432eaa2c6b-goog