From patchwork Wed Jan 24 13:15:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 191680 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp1141824dyi; Wed, 24 Jan 2024 09:31:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IHtX+AgpjXlHV90orHhfEDh/3HCRV58LNCP0vxC+Hnyw4WtLUgXXFGDoOpVkjHNHISfr8jF X-Received: by 2002:ae9:ee12:0:b0:783:af65:c821 with SMTP id i18-20020ae9ee12000000b00783af65c821mr2018101qkg.43.1706117475085; Wed, 24 Jan 2024 09:31:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706117475; cv=pass; d=google.com; s=arc-20160816; b=t5UKp++hCErxQ7Me+q4/ueBcXl1Woj3W9v39P/LSkqqHKVy1JBm+3WSxcpz6Wy7yaz KXBv4L3aw0eNVWsspQtHGV7yqRhycKDwG8OuBtuzmWu6SYRTsny+WBrDo/FmWLo/nT8H vO7qQnBBO8TWCm4azgEJZ0ZJqz782duBhm4RD4tt8odSLOSWsn4VCNq8DnuaYj50d7Y/ Qz82WZF+cdAOy9N4z+2nM9KCCDDkgL87uVvwkrx82JTnDHpP30eyz9kIvs+gi39yMn5z m035H2nYd0A5JIse8knw6Q/znw0TS2iDOJGLTibtDRp7Xp4SSlivsLRuXwGRegM34Mcd E9ww== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=3fH3Bwsh2BRIZlWopPdKunx/uUYtHMLC4A8Nv3V03mU=; fh=FkUL5BGAbRsQQ8uzsNq22fL2kRnrHEbsH7Qgym7v+RE=; b=Cp8JE+yPWFZ8B1BcdwfMsCHnKee+3NvGlf9A+y+GakGn1aNgrRObhX22FakVLoCfUe +f6DA+I1UfnL64uLFzo87zMq5J5rwsGSHD5N7Jpm1QabQPx6RGsUuJtEgNe2qX7vidnj y8r5KOi/OQE6N3QfoBZQwUfLuDdnZn8QV/XHEk3PUfrolJF/Bg5241b2VPZFq803CesP jHyZ5+bwAKSsO43Fsh6YzXZ7c7E3pLWJCxjYIoJixF4jK6z90/yvVveQCEqokI4F/KGr kLzXh6sJiQGACxfp9chPrG46J/Z98I8F6Sfk1+yoPAGZu3TwJ3thWGMTxiF52bCOI8N6 uhhw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ajuBn643; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-37057-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37057-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id wg16-20020a05620a569000b007834e326495si10522533qkn.355.2024.01.24.09.31.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 09:31:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-37057-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ajuBn643; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-37057-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37057-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 7B1931C28FCC for ; Wed, 24 Jan 2024 13:15:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7AC5377F1E; Wed, 24 Jan 2024 13:15:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ajuBn643" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35B3963116 for ; Wed, 24 Jan 2024 13:15:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706102120; cv=none; b=qBQOizrHQ7RAVuFBiLWSBZVx7HaBrp8to2UmDzqte8J+PYYuB4zUQdTlpPd8AZUOBtu+RS7nwxL/epuolCBBAOEjdQkxGDn4qa7Fi8Sv/w+PfNcEnIDEXjmIVa0cOzVoA5XTqV2F63JDmgCjmMsRah/EM9sBdiDY3CDe9Zw1kVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706102120; c=relaxed/simple; bh=1aG00z85N8OdrjfdrwaynQrPMyFIqlBHlOtO7kt7xqQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=e+FUHIRwmrHow6OzkaoKGGh227nzlNgaBgl/lzDkHg1n8ivJUhFJl50+UC66suVbRsoiGK0CtUcuPjHLMQHQW53qHcPiftCPF6QHWhdpqGh2/CtSOWgvCsBqoZvHVm7HAx2SSHEc/0d8+Z3gSPn3QVTfnOeSbsp6xPUjEiHtDNw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ajuBn643; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706102119; x=1737638119; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=1aG00z85N8OdrjfdrwaynQrPMyFIqlBHlOtO7kt7xqQ=; b=ajuBn6433q8RWgg0rUWS+UBneu8LTJBL5TtRO8ZBlYzqS6GFyvfloLBd cstRP9fN1HpFa28DXKmSj0JRBYKy5XukXz9YiplKTEb3yIFrUuVeki4k7 WCkbi2jmNbj0YKKlcPsMlCummVjLkEXMpJggePMNZOirsDb/86Ujzf6h1 0N/VYbQOdEGA3qMKKguVMEu+s03yQgz+hHyc87/gXM7BPJfyflHN3DQp2 MwMxY1B7v4d2ax5DOeiB+fP/gKeqNt7PeaVXBIY3QspOs90/7cH/av8pO /O98+vR45G/toJdTc/RH6PcCEThcLbzBTxTQ8sYzKsteXu1kUWVX8fzE1 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="8599487" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="8599487" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2024 05:15:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="735926693" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="735926693" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga003.jf.intel.com with ESMTP; 24 Jan 2024 05:15:13 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id BCC909F; Wed, 24 Jan 2024 15:15:12 +0200 (EET) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen Cc: x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Andi Kleen , Kai Huang , Sean Christopherson Subject: [PATCHv3, RESEND] x86/trampoline: Bypass compat mode in trampoline_start64() if not needed Date: Wed, 24 Jan 2024 15:15:10 +0200 Message-ID: <20240124131510.496803-1-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788993837497283441 X-GMAIL-MSGID: 1788993837497283441 The trampoline_start64() vector is used when a secondary CPU starts in 64-bit mode. The current implementation directly enters compatibility mode. It is necessary to disable paging and re-enable it in the correct paging mode: either 4- or 5-level, depending on the configuration. The X86S[1] ISA does not support compatibility mode in ring 0, and paging cannot be disabled. The trampoline_start64() function is reworked to only enter compatibility mode if it is necessary to change the paging mode. If the CPU is already in the desired paging mode, it will proceed in long mode. This change will allow a secondary CPU to boot on an X86S machine as long as the CPU is already in the correct paging mode. In the future, there will be a mechanism to switch between paging modes without disabling paging. [1] https://www.intel.com/content/www/us/en/developer/articles/technical/envisioning-future-simplified-architecture.html Signed-off-by: Kirill A. Shutemov Reviewed-by: Andi Kleen Reviewed-by: Kai Huang Cc: Sean Christopherson --- v3: - tr_cr4 is 32-bit, use 32-bit XOR to access it (Sean). - Use 32-bit TEST instead of AND to check if LA57 different between CR4 and tr_cr4 (Sean). v2: - Fix build with GCC; --- arch/x86/realmode/rm/trampoline_64.S | 31 +++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/arch/x86/realmode/rm/trampoline_64.S b/arch/x86/realmode/rm/trampoline_64.S index c9f76fae902e..608f108cba7d 100644 --- a/arch/x86/realmode/rm/trampoline_64.S +++ b/arch/x86/realmode/rm/trampoline_64.S @@ -37,13 +37,15 @@ .text .code16 -.macro LOCK_AND_LOAD_REALMODE_ESP lock_pa=0 +.macro LOCK_AND_LOAD_REALMODE_ESP lock_pa=0 lock_rip=0 /* * Make sure only one CPU fiddles with the realmode stack */ .Llock_rm\@: .if \lock_pa lock btsl $0, pa_tr_lock + .elseif \lock_rip + lock btsl $0, tr_lock(%rip) .else lock btsl $0, tr_lock .endif @@ -220,6 +222,33 @@ SYM_CODE_START(trampoline_start64) lidt tr_idt(%rip) lgdt tr_gdt64(%rip) + /* Check if paging mode has to be changed */ + movq %cr4, %rax + xorl tr_cr4(%rip), %eax + testl $X86_CR4_LA57, %eax + jnz .L_switch_paging + + /* Paging mode is correct proceed in 64-bit mode */ + + LOCK_AND_LOAD_REALMODE_ESP lock_rip=1 + + movw $__KERNEL_DS, %dx + movl %edx, %ss + addl $pa_real_mode_base, %esp + movl %edx, %ds + movl %edx, %es + movl %edx, %fs + movl %edx, %gs + + movl $pa_trampoline_pgd, %eax + movq %rax, %cr3 + + jmpq *tr_start(%rip) +.L_switch_paging: + /* + * To switch between 4- and 5-level paging modes, it is necessary + * to disable paging. This must be done in the compatibility mode. + */ ljmpl *tr_compat(%rip) SYM_CODE_END(trampoline_start64)