From patchwork Wed Feb 7 17:26:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Xin3" X-Patchwork-Id: 198005 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2406924dyb; Wed, 7 Feb 2024 10:03:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IF7jTAu6rHo+EnLr9qrfbXp2Wiz6RT0gqOwY7J9QA/4qYKzymopWSwAAqem+NBbg6uRCvJl X-Received: by 2002:a05:622a:d2:b0:42c:4826:7f04 with SMTP id p18-20020a05622a00d200b0042c48267f04mr379900qtw.29.1707329010356; Wed, 07 Feb 2024 10:03:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707329010; cv=pass; d=google.com; s=arc-20160816; b=rQvnNmFobUdZeUVPxPR1U93j6tikm5bIFDuFiUxl0eynUyZB6KLTvXgWkdmsKc5Lv6 2EW1qnqVxJ3cFPauKXMnNtLcZBf3SUT9BpBemFDYgWJ0S/BLZxXOZ0Oa4nuJX95V5LO1 qqZOfEuoZMOrYaH7l3oXJzXCKQ2lqkKlmHQFWsqUm1EmJrj4nTsB4oWU2SahLJ7KGKo2 NcmUgVrfRqR8fOgA0r8Zk5w3ARSG7Qj53i/mBiLD/xBU+7gWAeIjzEx71efBeMMnV0sU 7jslIhaqTpMRJ/AW8F5gs5xBFRu8yNi/nHoSW0qfga5606Ls7yN1XWXqGGOshGUeAMOp q2vA== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=O4FIUlTl/IfiVjXPyzJ/QW0ouKqLlA6XYYo/BTcTNPQ=; fh=6bhtLln5xTiTRGJQ+YgzHUPW5lgf8a3+fgxnudGBNbA=; b=brjR0YrIyNTdR+u0WLn4AsTK7NfQDSNawwFI9TpvrrEC76Edz+dDzOygBl0mGTQGhR F9HvCpOQEPL6aU4Pg8r6NGY/JgnLzaZ0H0i/PDkxeSfli6ywTft4WShTHdKRnCp7C6V6 l8NinqqqJ+kgXmz90D5hnXTLOK4/r3ZeeI56HB2aTsf/bgj2xPkJOo763CqNKNUoqcFp R2qmatPi+XPfSJ9eVAYzC14wZFZhKEOYzcIiksqNuOk6uYkqiODvTPd1274pXjfsUWp0 Ad+OZjisgsNHAMIsZp5KQYDaOQzBULzZALajGI6DErmhDPp3H+vT/e/ukTRj4zhKjY10 XXuA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="XUXI/ocY"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-56863-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56863-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCUi19H7SggFJQN+UG3VLS7H+ALc8bYM/7y4l/Ui/LiAqls98bRM3HSxD5J9n3wYjT4s7ooWegARYENGguC/F7TulKPmEg== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id y10-20020a05622a164a00b0042c48226303si216369qtj.789.2024.02.07.10.03.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 10:03:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56863-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="XUXI/ocY"; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-56863-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56863-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 1F1521C22108 for ; Wed, 7 Feb 2024 18:03:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 776D912BEA6; Wed, 7 Feb 2024 17:59:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="XUXI/ocY" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) (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 524F7126F11; Wed, 7 Feb 2024 17:59:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707328750; cv=none; b=Zq9bX+sJQv3uDnmtPSUxXj/lZBscefVE/JuccvRVF/LTO6lXH2gAisaBvxyvMSf0KA0OC2QL/02cSbiMUC2H/W4rNAWYZhhJx0xYO1Hevxz+t/EGNRWJwMN3WQY79flyqscPLZq2xV9bdQfV2jlE22vBW8rBD7MnT5PSv0IRHfY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707328750; c=relaxed/simple; bh=GjnwgyBkd3KBWam4oCzszCoyfvTgjRR1j1p0QWV6WrM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tk2+dHkEF9p9yzj9B3z0BzSxRaNz+hCibqjKpkHFnk9Qb3Lr4/qkoLs8vRRe5vwlzw//aXPw9f3uxBa7s13AJ5XXazUyFG+WS8dZgRMYebjeqEcOZnELw35nDbGBwfv6wkLxT5g8pgqdMy/+lQBn8IqgGn1cpirYdPpEeyGzosc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=XUXI/ocY; arc=none smtp.client-ip=198.175.65.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707328749; x=1738864749; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GjnwgyBkd3KBWam4oCzszCoyfvTgjRR1j1p0QWV6WrM=; b=XUXI/ocYcY/xOo4Dsm41ycUIIOVqJEY+5OPjRfc4q1sdOrxggzSSA/Kr viPiXoYX1wj8+4duCntrLrwSp1v+H6VQ1mH+iD+YZZtciaaWlCOWf499F F2fSp/B1BehqSPSEEtNDaCCDU7ndrzI71wfmrqkfa4pDmBKvRVM+pdtbu oHLCvxDgCTPmJsWqbfNiLhk+7D1OqYQjCZU9mTYGoJX8awZsslOT5LNVk Ll3mm86A5dlcWX2AOMcsUpNTU/gyRvpo/XACdi04zwIYgg3gcuRDrkdHd kT5wZZ699GibQJkPAD4eNrAeBCCmqbdrBF9Hawo8EgNpqqplv5SfuLJE8 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10977"; a="11622554" X-IronPort-AV: E=Sophos;i="6.05,251,1701158400"; d="scan'208";a="11622554" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2024 09:58:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,251,1701158400"; d="scan'208";a="6020721" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmviesa004.fm.intel.com with ESMTP; 07 Feb 2024 09:58:55 -0800 From: Xin Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, corbet@lwn.net, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, shuah@kernel.org, vkuznets@redhat.com, peterz@infradead.org, ravi.v.shankar@intel.com, xin@zytor.com Subject: [PATCH v2 09/25] KVM: VMX: Switch FRED RSP0 between host and guest Date: Wed, 7 Feb 2024 09:26:29 -0800 Message-ID: <20240207172646.3981-10-xin3.li@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240207172646.3981-1-xin3.li@intel.com> References: <20240207172646.3981-1-xin3.li@intel.com> 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: 1790264224574958350 X-GMAIL-MSGID: 1790264224574958350 Switch MSR_IA32_FRED_RSP0 between host and guest in vmx_prepare_switch_to_{host,guest}(). MSR_IA32_FRED_RSP0 is used during ring 3 event delivery only, thus KVM, running on ring 0, can run safely with guest FRED RSP0, i.e., no need to switch between host/guest FRED RSP0 during VM entry and exit. KVM should switch to host FRED RSP0 before returning to user level, and switch to guest FRED RSP0 before entering guest mode. Signed-off-by: Xin Li Tested-by: Shan Kang --- Changes since v1: * Don't use guest_cpuid_has() in vmx_prepare_switch_to_{host,guest}(), which are called from IRQ-disabled context (Chao Gao). * Reset msr_guest_fred_rsp0 in __vmx_vcpu_reset() (Chao Gao). --- arch/x86/kvm/vmx/vmx.c | 17 +++++++++++++++++ arch/x86/kvm/vmx/vmx.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index b7b772183ee4..264378c3b784 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1337,6 +1337,16 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) } wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); + + if (guest_can_use(vcpu, X86_FEATURE_FRED)) { + /* + * MSR_IA32_FRED_RSP0 is top of task stack, which never changes. + * Thus it should be initialized only once. + */ + if (unlikely(vmx->msr_host_fred_rsp0 == 0)) + vmx->msr_host_fred_rsp0 = read_msr(MSR_IA32_FRED_RSP0); + wrmsrl(MSR_IA32_FRED_RSP0, vmx->msr_guest_fred_rsp0); + } #else savesegment(fs, fs_sel); savesegment(gs, gs_sel); @@ -1381,6 +1391,11 @@ static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx) invalidate_tss_limit(); #ifdef CONFIG_X86_64 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); + + if (guest_can_use(&vmx->vcpu, X86_FEATURE_FRED)) { + vmx->msr_guest_fred_rsp0 = read_msr(MSR_IA32_FRED_RSP0); + wrmsrl(MSR_IA32_FRED_RSP0, vmx->msr_host_fred_rsp0); + } #endif load_fixmap_gdt(raw_smp_processor_id()); vmx->guest_state_loaded = false; @@ -4889,6 +4904,8 @@ static void __vmx_vcpu_reset(struct kvm_vcpu *vcpu) #ifdef CONFIG_X86_64 if (kvm_cpu_cap_has(X86_FEATURE_FRED)) { + vmx->msr_guest_fred_rsp0 = 0; + vmcs_write64(GUEST_IA32_FRED_CONFIG, 0); vmcs_write64(GUEST_IA32_FRED_RSP1, 0); vmcs_write64(GUEST_IA32_FRED_RSP2, 0); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 3ad52437f426..176ad39be406 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -278,6 +278,8 @@ struct vcpu_vmx { #ifdef CONFIG_X86_64 u64 msr_host_kernel_gs_base; u64 msr_guest_kernel_gs_base; + u64 msr_host_fred_rsp0; + u64 msr_guest_fred_rsp0; #endif u64 spec_ctrl;