From patchwork Fri Feb 17 15:42:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Dyasli X-Patchwork-Id: 9580183 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BCE56600C5 for ; Fri, 17 Feb 2017 15:44:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AF7152871F for ; Fri, 17 Feb 2017 15:44:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A470A28722; Fri, 17 Feb 2017 15:44:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 621D92871F for ; Fri, 17 Feb 2017 15:44:58 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cekgM-0001lc-2S; Fri, 17 Feb 2017 15:42:50 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cekgL-0001lR-FW for xen-devel@lists.xen.org; Fri, 17 Feb 2017 15:42:49 +0000 Received: from [193.109.254.147] by server-4.bemta-6.messagelabs.com id 64/A5-25093-8F917A85; Fri, 17 Feb 2017 15:42:48 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmplkeJIrShJLcpLzFFi42JxWrrBXveH5PI Ig8M7ZSyWfFzM4sDocXT3b6YAxijWzLyk/IoE1owd29cyFvyQrNj/WLuB8bJIFyMnh4SAv0TX yblMIDabgJ7ExtmvwGwRAVmJ1V1z2LsYuTiYBY4wSkxddYQdJCEsYCnRMHU3WBGLgKpE88s7L CA2r4CtRNPhRmaIoXISl7Z9AbM5Bewktv7+D2YLAdX8evyYBcJWlXj9YhdUr6DEyZlPwGxmAQ mJgy9eME9g5J2FJDULSWoBI9MqRo3i1KKy1CJdIwu9pKLM9IyS3MTMHF1DAzO93NTi4sT01Jz EpGK95PzcTYzA4GEAgh2M59cGHmKU5GBSEuV9xLU8QogvKT+lMiOxOCO+qDQntfgQowwHh5IE 70MJoJxgUWp6akVaZg4wjGHSEhw8SiK8fsBQFuItLkjMLc5Mh0idYlSUEuf9B9InAJLIKM2Da 4PFziVGWSlhXkagQ4R4ClKLcjNLUOVfMYpzMCoJ874EmcKTmVcCN/0V0GImoMWdEUtBFpckIq SkGhgLpuV2bdtnNllvxUNezYI/uU+Un3xa1St12thX/1x3wZNAz2mPHQIrtcs9+6wM98lZz50 p5XV+mWnV6cCH04xMuq8cfSbue8G9Rtjh9HPV60FrFUze+mmULi2cbbTsyERvJZc//HbXj21o 2Pid8XuxmUVX9/oP/8LjbYvubsnelvZ7R6nHbSYlluKMREMt5qLiRADmKesamAIAAA== X-Env-Sender: prvs=214538380=sergey.dyasli@citrix.com X-Msg-Ref: server-8.tower-27.messagelabs.com!1487346166!77354806!1 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 38242 invoked from network); 17 Feb 2017 15:42:48 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-8.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 17 Feb 2017 15:42:48 -0000 X-IronPort-AV: E=Sophos;i="5.35,172,1484006400"; d="scan'208";a="416657562" From: Sergey Dyasli To: Date: Fri, 17 Feb 2017 15:42:31 +0000 Message-ID: <20170217154233.28514-2-sergey.dyasli@citrix.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170217154233.28514-1-sergey.dyasli@citrix.com> References: <20170217154233.28514-1-sergey.dyasli@citrix.com> MIME-Version: 1.0 Cc: Andrew Cooper , Kevin Tian , Jan Beulich , Jun Nakajima , Sergey Dyasli Subject: [Xen-devel] [PATCH v2 1/3] x86/vmx: introduce vmx_find_msr() X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Modify vmx_add_msr() to use a variation of insertion sort algorithm: find a place for the new entry and shift all subsequent elements before insertion. The new vmx_find_msr() exploits the fact that MSR list is now sorted and reuses the existing code for binary search. Signed-off-by: Sergey Dyasli Reviewed-by: Jan Beulich --- v1 --> v2: - qsort (heap sort) is replaced with a variant of insertion sort - both host and guest MSR lists are now kept sorted - vmx_find_guest_msr() is replaced with more generic vmx_find_msr() xen/arch/x86/hvm/vmx/vmcs.c | 45 ++++++++++++++++++++++++++++++++++++-- xen/include/asm-x86/hvm/vmx/vmcs.h | 1 + 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 454d444..49587d6 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -1307,6 +1307,44 @@ static int construct_vmcs(struct vcpu *v) return 0; } +static int vmx_msr_entry_key_cmp(const void *key, const void *elt) +{ + const u32 *msr = key; + const struct vmx_msr_entry *entry = elt; + + if ( *msr > entry->index ) + return 1; + if ( *msr < entry->index ) + return -1; + + return 0; +} + +struct vmx_msr_entry *vmx_find_msr(u32 msr, int type) +{ + struct vcpu *curr = current; + unsigned int msr_count; + struct vmx_msr_entry *msr_area; + + if ( type == VMX_GUEST_MSR ) + { + msr_count = curr->arch.hvm_vmx.msr_count; + msr_area = curr->arch.hvm_vmx.msr_area; + } + else + { + ASSERT(type == VMX_HOST_MSR); + msr_count = curr->arch.hvm_vmx.host_msr_count; + msr_area = curr->arch.hvm_vmx.host_msr_area; + } + + if ( msr_area == NULL ) + return NULL; + + return bsearch(&msr, msr_area, msr_count, sizeof(struct vmx_msr_entry), + vmx_msr_entry_key_cmp); +} + int vmx_read_guest_msr(u32 msr, u64 *val) { struct vcpu *curr = current; @@ -1375,14 +1413,17 @@ int vmx_add_msr(u32 msr, int type) __vmwrite(VM_EXIT_MSR_LOAD_ADDR, virt_to_maddr(*msr_area)); } - for ( idx = 0; idx < *msr_count; idx++ ) + for ( idx = 0; (*msr_area)[idx].index <= msr && idx < *msr_count; idx++ ) if ( (*msr_area)[idx].index == msr ) return 0; if ( *msr_count == (PAGE_SIZE / sizeof(struct vmx_msr_entry)) ) return -ENOSPC; - msr_area_elem = *msr_area + *msr_count; + memmove(*msr_area + idx + 1, *msr_area + idx, + sizeof(*msr_area_elem) * (*msr_count - idx)); + + msr_area_elem = *msr_area + idx; msr_area_elem->index = msr; msr_area_elem->mbz = 0; diff --git a/xen/include/asm-x86/hvm/vmx/vmcs.h b/xen/include/asm-x86/hvm/vmx/vmcs.h index c30aab6..903af51 100644 --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -529,6 +529,7 @@ void vmx_disable_intercept_for_msr(struct vcpu *v, u32 msr, int type); void vmx_enable_intercept_for_msr(struct vcpu *v, u32 msr, int type); int vmx_read_guest_msr(u32 msr, u64 *val); int vmx_write_guest_msr(u32 msr, u64 val); +struct vmx_msr_entry *vmx_find_msr(u32 msr, int type); int vmx_add_msr(u32 msr, int type); void vmx_vmcs_switch(paddr_t from, paddr_t to); void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector);