From patchwork Thu Feb 23 09:33:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Dyasli X-Patchwork-Id: 9587979 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 0F10C601AE for ; Thu, 23 Feb 2017 09:36:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E5F0B28621 for ; Thu, 23 Feb 2017 09:36:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DA459286EC; Thu, 23 Feb 2017 09:36:10 +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 7B36128621 for ; Thu, 23 Feb 2017 09:36:09 +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 1cgpmL-0007as-QX; Thu, 23 Feb 2017 09:33:37 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cgpmK-0007aG-HJ for xen-devel@lists.xen.org; Thu, 23 Feb 2017 09:33:36 +0000 Received: from [85.158.143.35] by server-11.bemta-6.messagelabs.com id 4D/7F-04971-F6CAEA85; Thu, 23 Feb 2017 09:33:35 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmplkeJIrShJLcpLzFFi42JxWrohUjd/zbo Ig2M/ZS2WfFzM4sDocXT3b6YAxijWzLyk/IoE1ozbTx6yFzyXrDi48DRrA+NpkS5GDg4JAX+J U22eXYycHGwCehIbZ79iArFFBGQlVnfNYe9i5OJgFjjCKDF11RF2kISwgKXEkkl/WUBsFgFVi X0TT7CC2LwCthK/WiaBNUsIyElc2vaFGcTmFLCTeLXuEhuILQRU83nJayhbVeL1i10sEL2CEi dnPgGzmQUkJA6+eME8gZF3FpLULCSpBYxMqxg1ilOLylKLdA3N9JKKMtMzSnITM3N0DQ3M9HJ Ti4sT01NzEpOK9ZLzczcxAoOHAQh2MN7fGHCIUZKDSUmUl23lugghvqT8lMqMxOKM+KLSnNTi Q4wyHBxKErwzVgHlBItS01Mr0jJzgGEMk5bg4FES4e0CaeUtLkjMLc5Mh0idYlSUEudVWg2UE ABJZJTmwbXBYucSo6yUMC8j0CFCPAWpRbmZJajyrxjFORiVhHltQabwZOaVwE1/BbSYCWixpf NakMUliQgpqQbGOw98z3i4hzGd5pj57tiXD366R3/98rXYciD8xafAOQZ7yozNFzO8n83InXJ iiXfWIZ82AU/Pswt5+awfnrPrYfSyeKhXscj4f7K+xzEtPhmx3JjpnMayQeqfzZd4z3+SXnjE c/Vky0mr+zKsd2tOSzD5XSh2R3250i+JJxKnRYLKZPddTV6uxFKckWioxVxUnAgARhU4aJgCA AA= X-Env-Sender: prvs=22001adc4=sergey.dyasli@citrix.com X-Msg-Ref: server-9.tower-21.messagelabs.com!1487842413!58998462!2 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 39887 invoked from network); 23 Feb 2017 09:33:35 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-9.tower-21.messagelabs.com with RC4-SHA encrypted SMTP; 23 Feb 2017 09:33:35 -0000 X-IronPort-AV: E=Sophos;i="5.35,197,1484006400"; d="scan'208";a="409109704" From: Sergey Dyasli To: Date: Thu, 23 Feb 2017 09:33:25 +0000 Message-ID: <20170223093327.23493-2-sergey.dyasli@citrix.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170223093327.23493-1-sergey.dyasli@citrix.com> References: <20170223093327.23493-1-sergey.dyasli@citrix.com> MIME-Version: 1.0 Cc: Andrew Cooper , Kevin Tian , Jan Beulich , Jun Nakajima , Sergey Dyasli Subject: [Xen-devel] [PATCH v3 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 Acked-by: Kevin Tian --- v2 --> v3: - Now "idx < *msr_count" is compared before "(*msr_area)[idx].index" in order to eliminate possible out-of-bounds access 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 5306a48..977106f 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -1320,6 +1320,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; @@ -1388,14 +1426,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; idx < *msr_count && (*msr_area)[idx].index <= msr; 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 4ee01da..4945b78 100644 --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -530,6 +530,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);