Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp771690pxp; Wed, 16 Mar 2022 16:48:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy6qFpXgyyUkIWoQ+EfrAk8mIRJmdpCI5Q1L8Ab84ddVsOQeLgkiWq71kp7Ijqi9fTLpjyD X-Received: by 2002:a63:101:0:b0:36c:6d40:5688 with SMTP id 1-20020a630101000000b0036c6d405688mr1489601pgb.554.1647474481061; Wed, 16 Mar 2022 16:48:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647474481; cv=none; d=google.com; s=arc-20160816; b=DJTeLDFC2Vgzh1LQhutVRU3NhRg97YN3jnqVO6a8gjrFd1Gq1q01Iass2mOelLJ3if nf34Zc3AizuOL7BY1Bzx/bmA5TWXa+BUCBpPQgBX6r04qqXqa0SyUPVrPHzKf/050mPp e26rbtHBC5ChbmNo+/SExy3Bo/nSZjFoFT/wv7zhOqtGrhXHVTQLajYUmmco7dz4SI+h 9QA0x+ZedX/uFvAhyOkPWytIVcoah3uKOq68tbM6hK3wMreBQXCmVfMebEMCj7dfYb9Q elmE7O7xBuHL4VgcaLWekqFhPc8AOqCRJxoW7H5NFZB9yQjirzevU8wAnZj7vOzWKDlX 2wsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5fCTmHFXou9EXcp+ESkxBnlA64G4oU02VJ4xIQfBPxI=; b=mauRjW44tyojhviV0mBHenBvdepk3zxxtO+53/bMHlBYn0RPvjFC0n5quI66Sa6+pf 87QR3ZDMQeAp3KlaVRDaKCkr8j0cUqUG3IbLHDetoqX8BzoD7NBhM6+NK/jdZxm6TR3D wkPfYBLBwbPBLXOa7AIVpKYoQzRomxqU7RoNZqy2Sj+xGQ55+cGdtQpN2qnFY5JU/H6+ TIAl6pZfHYITpjdHm68mQjWTD1YoUCCF9B4fVxmps3paquPL+LVSxicUgxZs/sehc7xa +LrZM0lBJlO6Mw4KCkq+CX09jiRHTUMQRKZ/u7rqbPWeQF2cZApWZuo2wax3pb4qBCV4 klnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="NgGMMp/r"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q9-20020a170902bd8900b0014fa3a3ad86si2945891pls.272.2022.03.16.16.47.48; Wed, 16 Mar 2022 16:48:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="NgGMMp/r"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353199AbiCPCM6 (ORCPT + 99 others); Tue, 15 Mar 2022 22:12:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352482AbiCPCLa (ORCPT ); Tue, 15 Mar 2022 22:11:30 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F1675E75A for ; Tue, 15 Mar 2022 19:10:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647396614; x=1678932614; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=piyvgIvcVOBe83P9ahK/zUHRvKZIbLrME/ZzGKzUuwo=; b=NgGMMp/rZoMd/TOjsengmF+9YA7bOdt4vDJPiQh/cRwRtzSv1tpAZCC6 kGOq+M0a3Tq2et3n2zju3JzoUYh7E3l+FiP4dSxzAq0x2dth1pb8cHvPm 3YLJ6Bn2GgHsF2+e2WW4E5Ruah52IpocDUza96/IiqywV84Pk5kJJ5UiW hzFBJtzfTMyiCCevNIpDPHch4UrjAke9LItyh+tO6juW8z7+bPEWDgegg 0V1B9XEHkZoanCzdctE/B3DHHqBTlENfqUTblwoEpFLX4RojVVzinhIBT Bz8kFUb/RtIrX5ito/6PN2KlqqNu+JAkBbZgafRLwjK5NDWEcMHdK6NPC Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10286"; a="236415691" X-IronPort-AV: E=Sophos;i="5.90,185,1643702400"; d="scan'208";a="236415691" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Mar 2022 19:10:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,185,1643702400"; d="scan'208";a="714416793" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga005.jf.intel.com with ESMTP; 15 Mar 2022 19:10:05 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id A620BA9D; Wed, 16 Mar 2022 04:10:10 +0200 (EET) From: "Kirill A. Shutemov" To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@intel.com, luto@kernel.org, peterz@infradead.org Cc: sathyanarayanan.kuppuswamy@linux.intel.com, aarcange@redhat.com, ak@linux.intel.com, dan.j.williams@intel.com, david@redhat.com, hpa@zytor.com, jgross@suse.com, jmattson@google.com, joro@8bytes.org, jpoimboe@redhat.com, knsathya@kernel.org, pbonzini@redhat.com, sdeep@vmware.com, seanjc@google.com, tony.luck@intel.com, vkuznets@redhat.com, wanpengli@tencent.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Dave Hansen Subject: [PATCHv6 22/30] x86/boot: Set CR0.NE early and keep it set during the boot Date: Wed, 16 Mar 2022 05:08:48 +0300 Message-Id: <20220316020856.24435-23-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220316020856.24435-1-kirill.shutemov@linux.intel.com> References: <20220316020856.24435-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org TDX guest requires CR0.NE to be set. Clearing the bit triggers #GP(0). If CR0.NE is 0, the MS-DOS compatibility mode for handling floating-point exceptions is selected. In this mode, the software exception handler for floating-point exceptions is invoked externally using the processor’s FERR#, INTR, and IGNNE# pins. Using FERR# and IGNNE# to handle floating-point exception is deprecated. CR0.NE=0 also limits newer processors to operate with one logical processor active. Kernel uses CR0_STATE constant to initialize CR0. It has NE bit set. But during early boot kernel has more ad-hoc approach to setting bit in the register. During some of this ad-hoc manipulation, CR0.NE is cleared. This causes a #GP in TDX guests and makes it die in early boot. Make CR0 initialization consistent, deriving the initial value of CR0 from CR0_STATE. Since CR0_STATE always has CR0.NE=1, this ensures that CR0.NE is never 0 and avoids the #GP. Signed-off-by: Kirill A. Shutemov Reviewed-by: Dave Hansen --- arch/x86/boot/compressed/head_64.S | 7 ++++--- arch/x86/realmode/rm/trampoline_64.S | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index fd9441f40457..d0c3d33f3542 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -289,7 +289,7 @@ SYM_FUNC_START(startup_32) pushl %eax /* Enter paged protected Mode, activating Long Mode */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax /* Enable Paging and Protected mode */ + movl $CR0_STATE, %eax movl %eax, %cr0 /* Jump from 32bit compatibility mode into 64bit mode. */ @@ -662,8 +662,9 @@ SYM_CODE_START(trampoline_32bit_src) pushl $__KERNEL_CS pushl %eax - /* Enable paging again */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax + /* Enable paging again. */ + movl %cr0, %eax + btsl $X86_CR0_PG_BIT, %eax movl %eax, %cr0 lret diff --git a/arch/x86/realmode/rm/trampoline_64.S b/arch/x86/realmode/rm/trampoline_64.S index ae112a91592f..d380f2d1fd23 100644 --- a/arch/x86/realmode/rm/trampoline_64.S +++ b/arch/x86/realmode/rm/trampoline_64.S @@ -70,7 +70,7 @@ SYM_CODE_START(trampoline_start) movw $__KERNEL_DS, %dx # Data segment descriptor # Enable protected mode - movl $X86_CR0_PE, %eax # protected mode (PE) bit + movl $(CR0_STATE & ~X86_CR0_PG), %eax movl %eax, %cr0 # into protected mode # flush prefetch and jump to startup_32 @@ -148,8 +148,8 @@ SYM_CODE_START(startup_32) movl $MSR_EFER, %ecx wrmsr - # Enable paging and in turn activate Long Mode - movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax + # Enable paging and in turn activate Long Mode. + movl $CR0_STATE, %eax movl %eax, %cr0 /* @@ -169,7 +169,7 @@ SYM_CODE_START(pa_trampoline_compat) movl $rm_stack_end, %esp movw $__KERNEL_DS, %dx - movl $X86_CR0_PE, %eax + movl $(CR0_STATE & ~X86_CR0_PG), %eax movl %eax, %cr0 ljmpl $__KERNEL32_CS, $pa_startup_32 SYM_CODE_END(pa_trampoline_compat) -- 2.34.1