From patchwork Wed Jan 24 02:41:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Yang, Weijiang" X-Patchwork-Id: 191297 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp739727dyi; Tue, 23 Jan 2024 18:49:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IFQRqkZdWuacQkndEsC22JZeRnl/H/yHANRr3nioxEJBNXTKrh5ckSlVd5mOuBFlI22n0A9 X-Received: by 2002:a0c:e3d1:0:b0:681:14c1:b300 with SMTP id e17-20020a0ce3d1000000b0068114c1b300mr1475931qvl.121.1706064570940; Tue, 23 Jan 2024 18:49:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706064570; cv=pass; d=google.com; s=arc-20160816; b=gfKRbg2iph1YzJwH0gJWFHf7iosWLbyp1jfeJpNAdMYzk+EglV5cG0oGPrTMCDRomb s8T0xpPde9Vk8mHQsWtneBe9SziHspPBg88EJMGnD8hGDQsWFSgH7ip4pv4Y1HQOHeTE encCtIgPse7DWviLen6YWS+h7YafPv1iq9301AVMHAHOzg5fCBy4JHjnBa3SpTc0qnQ3 Y+r1cqtYolJYeP+fIMcpt+1VhI+/S/BbSL9bE8iJeeOM5QfoA1Lb8P2gCYlvys/pDx52 jGNM38vFn0Skk6abKsEAqzASQ6AmffRemdagTwImF9eD3Xn83qHLN+C8iPtICPKSVuuO PkKQ== 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36336-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. [147.75.199.223]) by mx.google.com with ESMTPS id z14-20020a0cf00e000000b00683696e7e41si9233526qvk.299.2024.01.23.18.49.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 18:49:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36336-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36336-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 A76971C211D4 for ; Wed, 24 Jan 2024 02:49:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E62F1B28D; Wed, 24 Jan 2024 02:42:48 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788938363760850065 X-GMAIL-MSGID: 1788938363760850065 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