Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp2303564rdb; Wed, 21 Feb 2024 03:38:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUkmY46A9RnelpRplq0JZZRhJHmDkUBnjVcTKffIeom95hVwT2iukfWHQ5oC12RZPk/dJPLIohMm85PTD9Qr7carVfezsIyBYZsYY7HPw== X-Google-Smtp-Source: AGHT+IGsr8N6U8MsvWSkGnXU2HU8og8owmxCNFhlp8NOjsWIMt/BYBD3S5HzRyecZqb49Dx+nKLi X-Received: by 2002:a92:da03:0:b0:365:4c03:763c with SMTP id z3-20020a92da03000000b003654c03763cmr3305951ilm.2.1708515488356; Wed, 21 Feb 2024 03:38:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708515488; cv=pass; d=google.com; s=arc-20160816; b=R3qlAM3YWSQ2SCIKxG/OOBfOIrvsygwvWNNthHBW0poxVdj5ZqOXlOBD+zMvmq7D1o KzgQx5UvEt+7BaFW2U9DQosLh4DFldFw2pI5i89B1iuoK11SaIU4zQNDzYxBLUOmbTcM QXQN5xH+62O5Lu1gYvg4bImiThaYg+TNnuMpDtGhflTh525L4HSkbh3QPRaHxjiyoK5e H2ON0mfoc6Wl42yWFOP3PRwqRpvVIwbUC3z/h0e5AyJepCs3ZdZ+PGjNWdLgCW1T84i9 R5mCDf9BZZH0QRR1FVJby697EMM2PjrQ1l9Y5Xone6G4FDw8kMQvINQbgjgQEBmOUQSo OgXw== 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=EkxcLENiSlDOPoQNco4VFPfOwd/QcirP1cMQb5AjsLI=; fh=fMhMPvo8duafGkM4xZHdH7fULbau3etefTGsKa+Qy7I=; b=V0+/EttialycaLrzGeCumgd8w0EjqIC0X0srG/VFycHKzgat6DETw22mWAuC+YeYdq zbg8I8dVSlYWZmTwgm29cMnU7WeSAQAz1KD3a6anod0fx+wa+a4x2PhIbXCd+9Pb1RxT NbBw+xSCRctoIH0x5NMdEae9tYi90csWnaJ9MJ91uUQ7t7Tl8StYjsQUyQr/Zv9N0PGz GiARTzi6/Jm/bOov16mBwkpKBa1AaCrEvX68lVrtlMo5QW0ljAGRBW41+eHnPBy7fuYD UmzToZYldLNh2yFRpkTyt6xERYgW7de73yAPdxf0UlUgPIQhdSf9YdNHA0cHzWSbgF4n 4uDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=HUdSHzvF; 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-74637-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74637-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. [139.178.88.99]) by mx.google.com with ESMTPS id x62-20020a638641000000b005ceb4a70188si7838369pgd.375.2024.02.21.03.38.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 03:38:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74637-linux.lists.archive=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=HUdSHzvF; 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-74637-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74637-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 F01B7284227 for ; Wed, 21 Feb 2024 11:38:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 07E624D9E5; Wed, 21 Feb 2024 11:35:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HUdSHzvF" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.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 A4ADE4D134 for ; Wed, 21 Feb 2024 11:35:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708515354; cv=none; b=qPQFGOF9Pd/edIXIWN4IAhciO4gv49m09I9UXz7pIv08Km9zun1C7L/FCm+tIV1VohpM3dVNxtqW4bW+Do22cCQH/MvTmAkynZhkNUMVF0r6hulcobwpZAfCqMg1ZpaV7DhHb9Rk/mtB5O2BGqQmutl5wpYHSuPXwHOdkHInBEw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708515354; c=relaxed/simple; bh=+vNI1MmLFGS1GivHerZyjgoY4sNAT6wDfnGDYRsl6Po=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=L0fId5mne7m7chEKaNQwpMETmLPp/GY1oOKf3t3bPRJKUUbyWSYevRTw6UWgqDXK7Jk5sr6Vu3Ah4PG7gxKgnFksJqiWjyIa1OwTDHGZecS2TDxJMRDFLwhryap47dDTapmONJ287n3c8VA2vQ17FkNcoK72WTxU7SfakRlNjkI= 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=HUdSHzvF; arc=none smtp.client-ip=209.85.128.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-yw1-f202.google.com with SMTP id 00721157ae682-60832a48684so43910207b3.1 for ; Wed, 21 Feb 2024 03:35:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708515351; x=1709120151; 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=EkxcLENiSlDOPoQNco4VFPfOwd/QcirP1cMQb5AjsLI=; b=HUdSHzvFKPPDstZtNL3AuROXQglCVOQW91cKv2q4pk+2jsrpvNUxc6qdc4t1/mPRX5 AlB+Cr5BCSgREX05P0N5nOYVKEFQm8RfZXG1ya6ffkcRoNiOXpTJTrNQClXDp6lI7D2D k8KDOgLGC6L3M4pZgtaEpkKet2jWRduZXsl/Ybzf0ggM4OGkJdQ4ghu+idGoug8xLJsu KEbCSokgZjWd5j4CJEctAhPwwHBPsn6R+/0hew/rxEPd425BhMyKaBe6gsK0sqyx1gZG 0bz3mrfsKf/x3lT/Z/+3nBIePxWybSuo+78weNarM2CHbv6sLEIbpzTXW2VbJ4s0q8AY Q7wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708515351; x=1709120151; 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=EkxcLENiSlDOPoQNco4VFPfOwd/QcirP1cMQb5AjsLI=; b=prNmuSEhFM/Dc6F9KrBn7FMJ/9COoRkJ1wvOiQWAMX8Cj4C7q7ddXkSdi8Wdj2XK9J eanOmAQQ36H9kG5HPeypMTVUos/vNcajEDsRI4ZZp/XkCXQEqdue3OdtyMPZpNpcO6VH OalYwWC/luiAVf6Qg0NnYARWuv9SAcwoy4orTMLohWC/3V8bOYzL9X/Lo0ayybtDEuF0 8w0zX46IH2xnkUNg9yDB3MLrC2Jjegqx37ibEHkXQXnIO2WRO8H90oeLVe+rBQhzPspw 0HsjDsQ4xbXE5AScmsEJY0Ru4NBNdlVlpyU6aOC0MAk+o31CzLactyxUAK7j0NmGrHgM 41rA== X-Gm-Message-State: AOJu0YwFakY13X16OFmxke1hm4GCmkmkGfYr/Qjz9+s2YydG0jQYvUOv UI3F217ptvt/+KylHlU0pudJI5yIYB8FRLn6i+bYqgb0LaxjDXaUjq1l+WRUGp3lfgAkn0deIu3 sNSpyjM1g0xmuxm11GITvvCMs9mglT2ZERZ6K9TOI3+wESYFlt1uR/BLkzC/VkVMPgGCPgLr+ej t7Io5OWHMFCc8zNOxmEZTW4NboPyenzA== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a0d:d790:0:b0:608:1fec:1dd8 with SMTP id z138-20020a0dd790000000b006081fec1dd8mr1741929ywd.6.1708515351508; Wed, 21 Feb 2024 03:35:51 -0800 (PST) Date: Wed, 21 Feb 2024 12:35:14 +0100 In-Reply-To: <20240221113506.2565718-18-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: <20240221113506.2565718-18-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2455; i=ardb@kernel.org; h=from:subject; bh=yaS/9ovMSw8F0QV6XZaoG3EypsAj0QA2cGCCi/EyQdM=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIfXq/Q+6elITfjUkXFxSXNX0UuVCU/Pf1rdHz2ydK3dXK 7z/yoWTHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAij18wMvQIb7OtV6/2qrBx Cv98yNPx7toM9qoPhkYHVx1mi2xTXs7I8NctSEVKctq3g9P4dr+z/LD31RzWlr/pAolrNTynt09 3ZgAA X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240221113506.2565718-25-ardb+git@google.com> Subject: [PATCH v5 07/16] 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 Content-Type: text/plain; charset="UTF-8" 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, 8 insertions(+), 16 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index fb2a98c29094..426f6fdc0075 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,13 @@ 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 + orl $X86_CR4_MCE, %edx #endif + movq %cr4, %rcx + andl %edx, %ecx - /* 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: -#endif + /* Even if ignored in long mode, set PSE uniformly on all logical CPUs. */ + btsl $X86_CR4_PSE_BIT, %ecx movq %rcx, %cr4 /* Setup early boot stage 4-/5-level pagetables. */ @@ -226,11 +221,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 -- 2.44.0.rc0.258.g7320e95886-goog