Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp865766rdb; Tue, 23 Jan 2024 18:55:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IF+O0YRohI4EUY4n6HRdR9EpyChErmNkAXpChkPQ3Mg8GpyokD63U0eDoZuL/m2vkpIu/xa X-Received: by 2002:a17:902:ecc4:b0:1d6:f1b5:b799 with SMTP id a4-20020a170902ecc400b001d6f1b5b799mr156962plh.73.1706064910526; Tue, 23 Jan 2024 18:55:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706064910; cv=pass; d=google.com; s=arc-20160816; b=qtLd7afloPlC5FqGkFaVwW8rRtYczMArD10/BrMCUGI8yqqdGrzvEca9k4uV5dgVZW THMVxfvQAJUL7PV6tbv6k4xS94PXMJ995RpiBICucC9EQE44iviXgCa1HeLv5vJBIA6q P7s5lXYk895njsWrZAE1a8CkxBMZMCScjDGiXyzfkRo8DH8VN8oYIiR0SnLwVV9dZVEG v07AK//DsQry5RZOQCE1EHAu1dDEhbUn0yGpOVe1TzNhe8/mSsdWHxwp/mOAxSSR7Mkb +goCF3KVk1gLgXkxFlq009n2zHxfA4duu2H8VkCDxk7HYrZjPOwMUAmlOCbtRKWEahLK kI6w== 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=VL3riO4wl1z8S8zC0ovJqRLywPdpK9/6DEgyMNBRMEk=; fh=D+u40KSnYgjUYLGDRdXdD62xsMHg3nFDmpf3sEnyojA=; b=Ne9x05rg4euDpxyImre4Wu7hgC+G/nIwDnVk8ASikdIV5lGJ3ChjqO3XEoT3LZEHMk s3Remc29UpZs096rLuvVbV0vVuYVQfwpJQgIvH+YcY8WuA3OvqWysT3DnFlg95K6Kfa5 QpCzGi5wyip/sfz17byWGBENv/9wzph0xB2hVuWs5ggmPE5bBYYnvnBQNAqT8iVISV0y Ez2WbI+t0orP5QpzUKmd+Nib53/nmGkTtY/ucVQB+jT2CehfnxK90srg6fiSBFcLql1n kOnn6y7K+ZHZuSl8mqdsDDepDPA7gmFj6XbRDbcdQmlo9p1AU4bMwyxfgiGvjcHdyai5 un5w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CPHRO5Wv; 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-36336-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36336-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id k17-20020a170902c41100b001d726c1022asi8105915plk.118.2024.01.23.18.55.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 18:55:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36336-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CPHRO5Wv; 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-36336-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36336-linux.lists.archive=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id C8A35B297C9 for ; Wed, 24 Jan 2024 02:48:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C79C418633; Wed, 24 Jan 2024 02:42:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CPHRO5Wv" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) (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 CC3C01798E; Wed, 24 Jan 2024 02:42:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.55.52.120 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706064164; cv=none; b=rXiejwmHfEhlIP29fBWrCi2GuG/xMxFb7ga26onJT9DNnSr1gbUK7MuXzcfTeZW3OFfasl9QmfkqvoIJXkO6LUw1A150AtpyYrSpsf13lwPr6EU46OJP9KDUxXJk2HOmVfOVf5nNTr4LsssrXQCyoicMGPhI0eCdlpMCPFjH4tM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706064164; c=relaxed/simple; bh=LMZ9WqVy19AK6b6saLJ6FfZQNW3F536pCoX8lK5esR8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hPuhgSlUBUg2Ab2RzIvPeJHV93NGyGZYMnNi1cH/wBXDzAZ+rTJQj0f45uiSkYRIPfPeg4AwS/U8vCxujceczJsj71j+GKSN9URJN+H0q9Y8vHUK6WaTGEBZAufXjdxDW2Fh6c3SJcH6zhvrmHHqBNTXo/udrC1WRWMfV/ZUTYs= 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=CPHRO5Wv; arc=none smtp.client-ip=192.55.52.120 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=1706064162; x=1737600162; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LMZ9WqVy19AK6b6saLJ6FfZQNW3F536pCoX8lK5esR8=; b=CPHRO5WvugX0UgPSLKGKQzhDYzP2c43ti5PqOlxTkNsEc2wQJxt64GPy 0s7NanC1PIWwhWB/U/uTRkaKfzVCTt1YXLZvkyPnKW7QEr/Hhb5GSrxFP BuNGxMAlYWQaSt90O/M2xBJeYuw6MY+8E9qCqouIh8QhF+qDURW8Oe8xQ d7gyVS5ApatinBdWugUeTJX2fRwB7uPwaUrbMBHEfIEMNQJryIVDRDz19 /oLTi/+qt+yEtjkzQT+R5EP9jyMmz2g86f29fths4JPVXiwRJzjWtfprJ YhB1GowUUmHIUe5VeakF1T266mrJMy8EhYF6I+4L53mi53AZbO3ia3AXT g==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="400586519" X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="400586519" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 18:42:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="1825879" Received: from 984fee00a5ca.jf.intel.com ([10.165.9.183]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 18:42:38 -0800 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, yuan.yao@linux.intel.com Cc: peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, mlevitsk@redhat.com, john.allen@amd.com, weijiang.yang@intel.com Subject: [PATCH v9 15/27] KVM: x86: Load guest FPU state when access XSAVE-managed MSRs Date: Tue, 23 Jan 2024 18:41:48 -0800 Message-Id: <20240124024200.102792-16-weijiang.yang@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240124024200.102792-1-weijiang.yang@intel.com> References: <20240124024200.102792-1-weijiang.yang@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sean Christopherson Load the guest's FPU state if userspace is accessing MSRs whose values are managed by XSAVES. Introduce two helpers, kvm_{get,set}_xstate_msr(), to facilitate access to such kind of MSRs. If MSRs supported in kvm_caps.supported_xss are passed through to guest, the guest MSRs are swapped with host's before vCPU exits to userspace and after it reenters kernel before next VM-entry. Because the modified code is also used for the KVM_GET_MSRS device ioctl(), explicitly check @vcpu is non-null before attempting to load guest state. The XSAVE-managed MSRs cannot be retrieved via the device ioctl() without loading guest FPU state (which doesn't exist). Note that guest_cpuid_has() is not queried as host userspace is allowed to access MSRs that have not been exposed to the guest, e.g. it might do KVM_SET_MSRS prior to KVM_SET_CPUID2. The two helpers are put here in order to manifest accessing xsave-managed MSRs requires special check and handling to guarantee the correctness of read/write to the MSRs. Signed-off-by: Sean Christopherson Co-developed-by: Yang Weijiang Signed-off-by: Yang Weijiang Reviewed-by: Maxim Levitsky --- arch/x86/kvm/x86.c | 35 ++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.h | 24 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f50c5a523b92..bde780ae69bf 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -133,6 +133,9 @@ static int __set_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2); static void __get_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2); static DEFINE_MUTEX(vendor_module_lock); +static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu); +static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu); + struct kvm_x86_ops kvm_x86_ops __read_mostly; #define KVM_X86_OP(func) \ @@ -4509,6 +4512,21 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) } EXPORT_SYMBOL_GPL(kvm_get_msr_common); +/* + * Returns true if the MSR in question is managed via XSTATE, i.e. is context + * switched with the rest of guest FPU state. + */ +static bool is_xstate_managed_msr(u32 index) +{ + switch (index) { + case MSR_IA32_U_CET: + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: + return true; + default: + return false; + } +} + /* * Read or write a bunch of msrs. All parameters are kernel addresses. * @@ -4519,11 +4537,26 @@ static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, int (*do_msr)(struct kvm_vcpu *vcpu, unsigned index, u64 *data)) { + bool fpu_loaded = false; int i; - for (i = 0; i < msrs->nmsrs; ++i) + for (i = 0; i < msrs->nmsrs; ++i) { + /* + * If userspace is accessing one or more XSTATE-managed MSRs, + * temporarily load the guest's FPU state so that the guest's + * MSR value(s) is resident in hardware, i.e. so that KVM can + * get/set the MSR via RDMSR/WRMSR. + */ + if (vcpu && !fpu_loaded && kvm_caps.supported_xss && + is_xstate_managed_msr(entries[i].index)) { + kvm_load_guest_fpu(vcpu); + fpu_loaded = true; + } if (do_msr(vcpu, entries[i].index, &entries[i].data)) break; + } + if (fpu_loaded) + kvm_put_guest_fpu(vcpu); return i; } diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 2f7e19166658..9c19dfb5011d 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -543,4 +543,28 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, unsigned int port, void *data, unsigned int count, int in); +/* + * Lock and/or reload guest FPU and access xstate MSRs. For accesses initiated + * by host, guest FPU is loaded in __msr_io(). For accesses initiated by guest, + * guest FPU should have been loaded already. + */ + +static inline void kvm_get_xstate_msr(struct kvm_vcpu *vcpu, + struct msr_data *msr_info) +{ + KVM_BUG_ON(!vcpu->arch.guest_fpu.fpstate->in_use, vcpu->kvm); + kvm_fpu_get(); + rdmsrl(msr_info->index, msr_info->data); + kvm_fpu_put(); +} + +static inline void kvm_set_xstate_msr(struct kvm_vcpu *vcpu, + struct msr_data *msr_info) +{ + KVM_BUG_ON(!vcpu->arch.guest_fpu.fpstate->in_use, vcpu->kvm); + kvm_fpu_get(); + wrmsrl(msr_info->index, msr_info->data); + kvm_fpu_put(); +} + #endif -- 2.39.3