Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp990456rdb; Sun, 18 Feb 2024 23:55:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV6FVdsvzpfrQ8d+W91GW7nA0g+eksyvD88O4HffZb9yuZ4zmgqY1WgdlRls3d+p8seAy3yYuQC/Q3oTLlhFfB62mjWfsq32cg61IAARQ== X-Google-Smtp-Source: AGHT+IFqXOdoPIPlEepeIM3w1pmNJcZqyguDPB3U6OJHkYLabOTdUB8Z0RDY89h0VNccS2Q6+6Zm X-Received: by 2002:a05:6402:1a50:b0:564:8adf:9a7e with SMTP id bf16-20020a0564021a5000b005648adf9a7emr508694edb.8.1708329317508; Sun, 18 Feb 2024 23:55:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708329317; cv=pass; d=google.com; s=arc-20160816; b=cJoKO2k5YTr6W7GdWO6YbSlKqirwhafod0Fa0YBDsmh5HLMJPkS6up7sC4G2xpymEw JpcIGQtAtDgVSKMy20+bQgQRaQPPb9DZNfVdRFm92+5ZWWkpxvVu+EOjELvQBx0ipftz m0bDYX1mjOo6ZP9Tn/BT6vRQP8nF2I5+q8DpGSeHR9P7GwaQO6p1qjnUg7znLoFmpeYR B6pPgjooJJFqY94WlF1IkQw1CFFxRAxjGHNVWSVmJcgarlr0o/41VhwhVy4XQ+pyivFP ydf7o29Otz2j6gSVTWblh2ykIIHxHqsZVtFH3IoTEe0ReBzKY+OKn8LtIoh1SNCeyWx4 PCew== 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=Hpord2r4lwH1PSd8Vb2F9FkgvcH3RK29YRANf8LhAMA=; fh=6bYtQoKQxNJSYrJA1a9vseXH6qHZpRYO7L/7krtpXA0=; b=R9C/2RCOhe/xochnqDHoOxlkjUlGDQj6S6CLHa34MHgh4xaFX3y1laKBc3bAvBqPTW VyTc4UYRBh36ENR1k1KCAcIE6Lnnxt5pm47wtfRej0zmdssRyVSE9i7wCS0uJwZiuwpX tzzKJCaHbpfjCWtaRYZOyj9TsZvsOYMe6JwFWNZyujgz3WSw+LP3Zz7obbpuw3TLzXWl 70gNL+c7lQpO/Up05zBOTPX57mXWnSvCRZfnxx/e7bK0tbbuRSOYreSnowO7Y4qAR/uB C+IcRnhAhzyvDRbOZB7qgbmn5ZHtbZZtltMxADd1FB1jOZnUTRJZpwIuti/czzPv3BQE DIKg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="CqYsBM9/"; 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-70881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70881-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id d17-20020aa7d691000000b005615be7a300si2156024edr.545.2024.02.18.23.55.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 23:55:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="CqYsBM9/"; 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-70881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70881-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 1AFEA1F20F8D for ; Mon, 19 Feb 2024 07:55:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B32ED38390; Mon, 19 Feb 2024 07:47:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CqYsBM9/" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 4A44833080; Mon, 19 Feb 2024 07:47:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708328875; cv=none; b=J2bq1f5561WaC02gjc8FHouZ8iRNoxUL/y1aT9nYGXcHPgz9qMoUVJpZFUx6Vg+13CE1Ad+77upsnoOPY6veYzouQUrEY7HkfxshzrsD7dM2bMDfwuancns4gtRmEtDmYzzfJlZsl1LDzbaf0e7W561B67qzJQZRI9tBjeD7K/o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708328875; c=relaxed/simple; bh=jwP3gUL56k+hGNBaB4Q0U23pwBgasARA/lYMleLy96s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RuRx6t6JYsWJtHHAqElSXgDDhmqeqoltQHUI6YrSAWSaeY1iZWZM9w2jURABAoEHa1nin5n7zGb/kOST4VOpQObaE5PDdMbv7Z8DEhUgtEbxxy4j1z4079Wvq7VuwDS3JNi5kQYfSgCPk9ZpAyfZbkhwyE8Hxwwp56Zq59EI3aI= 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=CqYsBM9/; arc=none smtp.client-ip=198.175.65.16 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=1708328873; x=1739864873; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jwP3gUL56k+hGNBaB4Q0U23pwBgasARA/lYMleLy96s=; b=CqYsBM9/HOuZmKcuRCptjTdlC31YM+6Lqn6CwAjtE2P0v7ilH4a1OJge HQeJsK+feBbCabKaH34kifo1pmL4+956qa+OALD5zwnkQeATb7TQ7AMCo /15BaeVlcikHRzBUQbbYmzafStbRJeEtfeu7qgshsXhA3L8XSTFkwle8Y KC7NQovDdgxX4oTGRqtsIjmmI9+nF14AuK8NvaFP1jQW0AatHWq0BD4FS TVDKxPCd3Feh27MFF/ONSrUWcf2aikUHlgICNjrC61nJUkDY+XRVNQh7D fzNAbbJoAro42f6nCQUl3O+Oq/x9AChrRfoX5rRs/TFNZgbUlShClASQ9 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10988"; a="2535114" X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="2535114" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2024 23:47:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10988"; a="826966102" X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="826966102" Received: from jf.jf.intel.com (HELO jf.intel.com) ([10.165.9.183]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2024 23:47:43 -0800 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org 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 v10 15/27] KVM: x86: Load guest FPU state when access XSAVE-managed MSRs Date: Sun, 18 Feb 2024 23:47:21 -0800 Message-ID: <20240219074733.122080-16-weijiang.yang@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240219074733.122080-1-weijiang.yang@intel.com> References: <20240219074733.122080-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 60b574fc04d1..906307757159 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.43.0