From patchwork Tue Feb 13 12:41:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 200414 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp507039dyb; Tue, 13 Feb 2024 04:43:23 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWleunWe5o0KcEsuzLt6ukslJ1YMd9QgCqvgPMoCPApH/H2mV1wHKZbtF9vBz8VxbigjX9qbJWItIjE4ypbuRin8Ezk5w== X-Google-Smtp-Source: AGHT+IErRgzw5ExbEQKu18xTODECAMnM3sjBlVFbNW275wrQX/FqJptPcKWCOKDrEopxrFPlDuRn X-Received: by 2002:a05:6a20:c792:b0:19e:4e41:7ae0 with SMTP id hk18-20020a056a20c79200b0019e4e417ae0mr3171650pzb.20.1707828203022; Tue, 13 Feb 2024 04:43:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707828203; cv=pass; d=google.com; s=arc-20160816; b=LhorKTgrUJwOoXq2bIyUVb1yozXcYMviEF3MI/CvcBhBZXZUX8EGF17E55CHDAwoxG TAEQqXquLMoH5EqU8LXDfIyXrPNacbwQLl9sDktysD2Y0WT8nVTMc9eJ564zW3J3O3yg BTVFzOca5aH4XjlPW8A21hWJWp5EqdOnf1GdX1i3YBvDC4kWC/pXUY9y5JPpsZtXpANL nTRnnLbv5YozTFjasClq89zEwC2vtUzIHRck2FuF++1A2sb0XmBIKa0NWGgCp2VYrXV+ y3E39t94kTPVC9oXwnkxtJn70W/N6ZuNkX4kN4PxYY9G29LtdFVX7Sh9U+evRtP21o7e A+WQ== 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=R8PzBwStBQmNo4IaP5da+Ydr5S+nPOaVv6NiNBRmMEI=; fh=53uUiUcD6b+VxcaUX5H0rX8jbUzS/Fzb7T+QgZy+5PA=; b=1Bn+1GKWv+4wAOoVODWBAL+Jnl+YnFhCEUy3Klt/InarWv1ZH8dyJSIXCeczzsPYss FkYXjuzRYXx4ZADm69gQHxKHwm6GFkXW59DJ5qKgRJo5shBZDWPn/qJ+EA3YOZJFffqu CQdjohAC2oiLKkP0on3yzLbdxeo5S/7qwyP9pZWpR20RxBiaZ70EM491Nlz8BX9hXEOG XnhncetbLjfUElbdHo6nx+0UPtqZfi7mfMXP0yBB7ms4+C/NTfiwom+QqxLnPTgqUz+w qSEeVXx9ovfv1NneTUjjZ86fTZeT00tud1PMchCcrGLLlx0U1FMlejJcYXbgFf+ddhxl OObQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ulj1QaRz; 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-63523-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63523-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVBgBvNXsk3MJ+wUgk7/Qjla+YHewTKXtNt0OzsympdLMZ04CdRsVhwYANsdx1cgQHUjhd8Wi9OB0Lt3/wjgsSY0W/mLA== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id l28-20020a63ba5c000000b005d8b7ca8ffdsi1913788pgu.327.2024.02.13.04.43.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 04:43:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63523-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ulj1QaRz; 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-63523-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63523-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id BE3C7281FBD for ; Tue, 13 Feb 2024 12:43:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 52584524B8; Tue, 13 Feb 2024 12:42:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ulj1QaRz" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 D037C50A7F for ; Tue, 13 Feb 2024 12:42:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707828131; cv=none; b=QfqKn314PJzzYHflP51gR2wEnOopq0vT7jlNgM9SF+84vD5IRVV0m6sdeMP10oezSxj/8VFZKEpyamw49mGZSVoLJV+98DgSSZZtZhbPz3T3Krwlw2j5IzNJP0/DG8fgRLZAmw5mvgzcxjgVvBSoodwkbtQoJzqrbOVSidQ1CXI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707828131; c=relaxed/simple; bh=jPHNmEDl0ryiAggUWuXvb8LuTJ6Z4Zpf6+3+QYXDVB4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XjFo8aYcn5T+HQHmqUSxeBQ1ciQ7n08ZxExpQO9ZWHqDRYytQpLQL2Hxkd2v9IwCV7ctEBLpHllh7bNHhq+KijreSjVuMAkNd9zqCQ4OAKqCZOTQCkX8BCf/SpnaBQlS0t7hfySNN4PLjHWFLHIZNcQ13bXH5RiylygyZVzWl2U= 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=Ulj1QaRz; arc=none smtp.client-ip=209.85.219.202 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-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcc0bcf9256so1704171276.3 for ; Tue, 13 Feb 2024 04:42:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707828129; x=1708432929; 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=R8PzBwStBQmNo4IaP5da+Ydr5S+nPOaVv6NiNBRmMEI=; b=Ulj1QaRzhF/2z0kHbIl0qORR2B/5JZX1j9bvKaguuYs0KnvmnLFoA5G8B8vyRMTTX2 aNjqRPen6MrgyFLbYrFpnK7PbEmvI3OpkyKmxG8AoBL7zY2RYzYyYCL9LzZK2QbT6qi/ D+2KAkUtuW4eCR9a4dbHSylD7EH5GgkG9LFnSIEF4oCu/f63FQSzqfIJJlzSMDeeirTN KsF9Q0gmZeftSDy0VgUbZ2BKIJzr4B+EGoNEwlARhLEpbX1oG0sbW5RC/LAMCEY098j6 65DEqwagXx65SCaCFhL9Bjupvc+EXJgFdpDS/HoZdqQHhC8nk0oqJdX/4GyscKuYAAZM gm+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707828129; x=1708432929; 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=R8PzBwStBQmNo4IaP5da+Ydr5S+nPOaVv6NiNBRmMEI=; b=evbW2X0QK/9P6gUvtIQMzemR5M9B7bGsvkSVrshYXUk1FfAISa4KPxmMtkPk1nVYtZ CbQnuO+P4Z1dDJUneEEWXbnmiir3fTcMeYGKGhwQd12q8cnk6zGL9LhbKlcOq1vPGFg9 MG8mBfEDDWveOc5wKHCoUNx941zHGS+erY9ODzW93uCYRLchOZ6yOnpBXJSmlYMh+5U2 c7gLJKIYDF5UX4g9Y6T7mnd32EJyLCiIydwkZaxYq2pTdav9MGlRXyn/u3t7GCgndFtg xkWJxCQN+IMvBGSl1K3EW8Oc4W7MdviD7G1Nnx1jkjKdkj0nJc6YZDd6DAG5DhkYYR+G UoYg== X-Gm-Message-State: AOJu0Yx7vbLIwLyRuidhhtxZNxesRtf8GqILc61eAFfzHdr0/j5NHCyj 1zig+r7r7LW/K8yA+1LspN8MXEmxlKL6Pbd3C0sl8WcyONigUc+6NdTW0wgt7/WKPLWGu/oyXf1 NBVONBSEAM+BNboPo1TxZDlnY8+9Gf0IUIkJwOouSGmTnLj5Pgf+QRh4bQQ4IrgIiseShl0UG40 DAW651YxacwQCMP7oYgqwi9QQvkyRDwQ== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:6902:1028:b0:dc7:3189:4e75 with SMTP id x8-20020a056902102800b00dc731894e75mr346289ybt.3.1707828128483; Tue, 13 Feb 2024 04:42:08 -0800 (PST) Date: Tue, 13 Feb 2024 13:41:47 +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=2373; i=ardb@kernel.org; h=from:subject; bh=4k+FBUPb8cUOIJZ1D8SGO5j46VqaiuS2qc/hfQO7nqA=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIfV0cnf5JtewZYpJvdc2m4UXi63ZycLU4s3Y8/T5sZxrm bOVH1h1lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgImsYWX4K/p4j/cbl04206aX nas2ySWs8X9we3KI9D3lOXJvToscymJkmOA068FH/ZhrTr+kemP/3SvaKS3atlwuVc9F9fTLNa6 7+AA= X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240213124143.1484862-16-ardb+git@google.com> Subject: [PATCH v4 03/11] x86/startup_64: Simplify CR4 handling in startup code 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790787665607066014 X-GMAIL-MSGID: 1790787665607066014 From: Ard Biesheuvel When paging is enabled, the CR4.PAE and CR4.LA57 control bits cannot be changed, and so they can simply be preserved rather than reason about whether or not they need to be set. CR4.MCE should be preserved unless the kernel was built without CONFIG_X86_MCE, in which case it must be cleared. CR4.PSE should be set explicitly, regardless of whether or not it was set before. CR4.PGE is set explicitly, and then cleared and set again after programming CR3 in order to flush TLB entries based on global translations. This makes the first assignment redundant, and can therefore be omitted. Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/head_64.S | 24 ++++++-------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 3cac98c61066..7e76cc0b442a 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -185,6 +185,8 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) addq $(init_top_pgt - __START_KERNEL_map), %rax 1: + /* Create a mask of CR4 bits to preserve */ + movl $(X86_CR4_PAE | X86_CR4_LA57), %edx #ifdef CONFIG_X86_MCE /* * Preserve CR4.MCE if the kernel will enable #MC support. @@ -193,20 +195,11 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) * configured will crash the system regardless of the CR4.MCE value set * here. */ - movq %cr4, %rcx - andl $X86_CR4_MCE, %ecx -#else - movl $0, %ecx -#endif - - /* Enable PAE mode, PSE, PGE and LA57 */ - orl $(X86_CR4_PAE | X86_CR4_PSE | X86_CR4_PGE), %ecx -#ifdef CONFIG_X86_5LEVEL - testb $1, __pgtable_l5_enabled(%rip) - jz 1f - orl $X86_CR4_LA57, %ecx -1: + orl $X86_CR4_MCE, %edx #endif + movq %cr4, %rcx + andl %edx, %ecx + btsl $X86_CR4_PSE_BIT, %ecx movq %rcx, %cr4 /* Setup early boot stage 4-/5-level pagetables. */ @@ -226,11 +219,8 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) * Do a global TLB flush after the CR3 switch to make sure the TLB * entries from the identity mapping are flushed. */ - movq %cr4, %rcx - movq %rcx, %rax - xorq $X86_CR4_PGE, %rcx + btsl $X86_CR4_PGE_BIT, %ecx movq %rcx, %cr4 - movq %rax, %cr4 /* Ensure I am executing from virtual addresses */ movq $1f, %rax