Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp251516img; Mon, 25 Feb 2019 22:34:02 -0800 (PST) X-Google-Smtp-Source: AHgI3IYhO/aWSm11hGGD1N9ePe2ot6m1CehzojrQU6kTivI5DT2wnycpRkxFVVp6Tfzh5SPxuXOu X-Received: by 2002:a62:503:: with SMTP id 3mr24040059pff.176.1551162842578; Mon, 25 Feb 2019 22:34:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551162842; cv=none; d=google.com; s=arc-20160816; b=YAXUNrK04KZ0d4mIOUTTpFAGIXgDKDvCvWN58dKClPgCRalJBkHyjCbDty0YFDh70v IGy4E6QsYQbPp6bAiFez5NfLG00Obro8tzuocaaTQ5rnLCkezJzJ2VfHWs87LsETqq8/ pm2rD2yYkhzSG2OhfYRq/1/jH2vj3I36bzPrsAux9QIhCFEbSHEo9dY5j2lBqiQ7/n3d UyN8mZsrmO3gH3rpBYz0f8q8EMcD5FLWvgztkklyKCW3dBxY6XuBm351pw5XPW7+YQ7J HreAtWmoe/mu8r8qoXseCO1z3u4+a1x1AvfUHxcu/5qbv5LGpg9q3t613ErtLfr+k9Mu qSgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=5CkMbOEE1FbdXCSnMgKazVoX3pOjCFiXW24XA6oYVPw=; b=RQWG0XTK0daeBRgzSeq2579pRhvPmLDW2BS/bBjb1t+cdYg/59hx6dG/3fmi58Lzm1 DZzQuOLr78wB5NJFYxU08/28GVjk2otLyXblUaB+ZrlflmbPQGOX722LdYuLom7k5luU pXR4FHKGlxcWInF/n7hOQv1zMiyxm/wZZhJyomzKnMZpN+mOZl5eyWHe9uTuhfH5Yl1V 7wG/zco1JwQOjuGLfzqnUgZEXy9aUvQKwaUlXDCpxAzYX2CIADw5DPV7/n2weqL6n3qK yIm8EsHtk0CJGybtWW6kuTs2boybWF8N7SnJ42azv0aqXFcZn2mSTnWyIydQyzP2tdqZ jctg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t23si3046400pfa.64.2019.02.25.22.33.47; Mon, 25 Feb 2019 22:34:02 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726867AbfBZGdE (ORCPT + 99 others); Tue, 26 Feb 2019 01:33:04 -0500 Received: from mga17.intel.com ([192.55.52.151]:18116 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726781AbfBZGdC (ORCPT ); Tue, 26 Feb 2019 01:33:02 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Feb 2019 22:33:01 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,414,1544515200"; d="scan'208";a="141666222" Received: from local-michael-cet-test.sh.intel.com ([10.239.159.128]) by orsmga001.jf.intel.com with ESMTP; 25 Feb 2019 22:32:59 -0800 From: Yang Weijiang To: pbonzini@redhat.com, rkrcmar@redhat.com, sean.j.christopherson@intel.com, jmattson@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, mst@redhat.com, yu-cheng.yu@intel.com Cc: Yang Weijiang Subject: [PATCH v3 8/8] KVM:X86: Add user-space read/write interface for CET MSRs. Date: Mon, 25 Feb 2019 21:27:16 +0800 Message-Id: <20190225132716.6982-9-weijiang.yang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190225132716.6982-1-weijiang.yang@intel.com> References: <20190225132716.6982-1-weijiang.yang@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The Guest MSRs are stored in fpu storage area, they are operated by XSAVES/XRSTORS, so use kvm_load_guest_fpu to restore them is a convenient way to let KVM access them. After finish operation, need to restore Host MSR contents by kvm_put_guest_fpu. Signed-off-by: Yang Weijiang --- arch/x86/kvm/x86.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a0f8b71b2132..a4bdbef3a712 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -75,6 +75,8 @@ #define MAX_IO_MSRS 256 #define KVM_MAX_MCE_BANKS 32 +#define MAX_GUEST_CET_MSRS 5 + u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P; EXPORT_SYMBOL_GPL(kvm_mce_cap_supported); @@ -214,6 +216,8 @@ struct kvm_stats_debugfs_item debugfs_entries[] = { u64 __read_mostly host_xcr0; static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt); +static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu); +static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu); static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu) { @@ -2889,21 +2893,57 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) } EXPORT_SYMBOL_GPL(kvm_get_msr_common); +static int do_cet_msrs(struct kvm_vcpu *vcpu, int entry_num, + struct kvm_msr_entry *entries, bool read) +{ + int i = entry_num; + int j = MAX_GUEST_CET_MSRS; + bool has_cet; + + has_cet = guest_cpuid_has(vcpu, X86_FEATURE_SHSTK) | + guest_cpuid_has(vcpu, X86_FEATURE_IBT); + /* + * Guest CET MSRs are saved by XSAVES, so need to restore + * them first, then read out or update the contents and + * restore Host ones. + */ + if (has_cet) { + kvm_load_guest_fpu(vcpu); + + if (read) { + for (j = 0; j < MAX_GUEST_CET_MSRS; j++, i++) + rdmsrl(entries[i].index, entries[i].data); + } else { + for (j = 0; j < MAX_GUEST_CET_MSRS; j++, i++) + wrmsrl(entries[i].index, entries[i].data); + } + + kvm_put_guest_fpu(vcpu); + } + return j; +} /* * Read or write a bunch of msrs. All parameters are kernel addresses. * * @return number of msrs set successfully. */ static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, - struct kvm_msr_entry *entries, + struct kvm_msr_entry *entries, bool read, int (*do_msr)(struct kvm_vcpu *vcpu, unsigned index, u64 *data)) { int i; - for (i = 0; i < msrs->nmsrs; ++i) + for (i = 0; i < msrs->nmsrs; ++i) { + /* If it comes to CET related MSRs, read them together. */ + if (entries[i].index == MSR_IA32_U_CET) { + i += do_cet_msrs(vcpu, i, entries, read) - 1; + continue; + } + if (do_msr(vcpu, entries[i].index, &entries[i].data)) break; + } return i; } @@ -2938,7 +2978,7 @@ static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, goto out; } - r = n = __msr_io(vcpu, &msrs, entries, do_msr); + r = n = __msr_io(vcpu, &msrs, entries, !!writeback, do_msr); if (r < 0) goto out_free; -- 2.17.1