From patchwork Fri Sep 8 16:05:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Stefan ISAILA X-Patchwork-Id: 9944635 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 946F2604D4 for ; Fri, 8 Sep 2017 16:08:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7488A2881C for ; Fri, 8 Sep 2017 16:08:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 68F4728821; Fri, 8 Sep 2017 16:08:51 +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.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID 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 D3A8B2881C for ; Fri, 8 Sep 2017 16:08:50 +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 1dqLnm-00061X-Pp; Fri, 08 Sep 2017 16:06:42 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dqLnk-00060E-VX for xen-devel@lists.xen.org; Fri, 08 Sep 2017 16:06:41 +0000 Received: from [85.158.137.68] by server-8.bemta-3.messagelabs.com id CF/5B-02224-010C2B95; Fri, 08 Sep 2017 16:06:40 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprPIsWRWlGSWpSXmKPExsUSfTyjVZf/wKZ Ig8unJSyWfFzM4sDocXT3b6YAxijWzLyk/IoE1oynU3IKtllUTNn5ga2BsVW9i5GTg1nAWqL3 XzNjFyMXB4vALBaJC/dmMkM4E1gkJqw4xgxSJSTgLnFo3SewKiGB+YwSH7uaGGESR659Z4ZIL GWUmL1vAxNIgk3AQOLV129gRSIC0hLXPl8G62YWmM4scfd3AztIQljAW2LxzztADRxA+1Ql9h 3NBgnzCrhJzGp7DTZHQkBO4ua5TrArOIGWrb7wnh2kXAioZn6D2gRGgQWMDKsYNYpTi8pSi3S NzPWSijLTM0pyEzNzdA0NjPVyU4uLE9NTcxKTivWS83M3MQIDq56BgXEHY8tev0OMkhxMSqK8 Mj2bIoX4kvJTKjMSizPii0pzUosPMcpwcChJ8B7cB5QTLEpNT61Iy8wBhjhMWoKDR0mEdxtIm re4IDG3ODMdInWKUZej4+bdP0xCLHn5ealS4rzpIEUCIEUZpXlwI2DxdolRVkqYl5GBgUGIpy C1KDezBFX+FaM4B6OSMO8pkCk8mXklcJteAR3BBHREyfMNIEeUJCKkpBoYI4LvPe/yOHZQ24P NOE3J7Wzf7Htpz45Fbd9alLBa5qZ7844j1xY7qTw5rjJLuWjD9O1dNiFyyvfFbv2Vltz05Q2b /W/f0n9b0z1ySh/nn4zYlDD5W8nnmritVWGCMyYe2bNi/hIJCV+FarGieQ4SL/ZGpYYs8mZ// pG5x3bz/otXr669mBwRosRSnJFoqMVcVJwIAIrNjzCyAgAA X-Env-Sender: aisaila@bitdefender.com X-Msg-Ref: server-10.tower-31.messagelabs.com!1504886798!113181872!1 X-Originating-IP: [91.199.104.133] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 58874 invoked from network); 8 Sep 2017 16:06:39 -0000 Received: from mx02.bbu.dsd.mx.bitdefender.com (HELO mx02.buh.bitdefender.com) (91.199.104.133) by server-10.tower-31.messagelabs.com with DHE-RSA-AES128-GCM-SHA256 encrypted SMTP; 8 Sep 2017 16:06:39 -0000 Comment: DomainKeys? See http://domainkeys.sourceforge.net/ DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=bitdefender.com; b=mStXs0bq+s0hMzQb7x1Qr5SgpbiS58fc5aVvF6456ptBoR+zq49ryybcuc/f45Zdvit9XsxqxMsy2voOdiq1NjJkKJdHQrMJD2/W0MlQSblA7Dcttkk7M4tli66j/mE9CQXyozuNnpv63bprClA/vObAsck7pmVSZ7OGaNuEAKXvAgtuNam0+s8PCcnLQbs//sKUU81eyJAotcrPJO8tUrYCaQcG8LpfT7stDPblZ5r/23aFd1Ei0Row1KBiUB31AbLVfaHx76rIHeBzTucNAxTy45s/1HFd17SDFuWhtmzORMfN116W2xigJTPMqlKKl000hlXaNFe0O0CLCmw+Sg==; h=Received:Received:Received:Received:From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References; DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=bitdefender.com; h=from:to :cc:subject:date:message-id:in-reply-to:references; s=default; bh=R5KUAZb/lcA2Fp4p8lEY+gUfE4Y=; b=N5GdMESnkCRyXz/+qP9oYLe/AnuE WgHrVH9dsb9HLXP7Id+G03NvEwDqt6ZpCAuzA9/k2LoW3ARV9/ncDQz4PaueaaKh 9oE174zR4OMpXDOgrYlyjBm2yQTpQJhX3j4Vt6wqxHyqXrxqmwQ11nAFqVzRp2W1 FqWuPPaORSr4Ga87lmZRXpvDC6ancQqrdttnItKdaRxUiH4B2fkoltcSf07PcYhg 8+O66Rv2WVO8vkhNPF3Cyq0F0ODXUmEGC/t8FkNgE9T+6rFLWY59mBHBhwQfZfxi EbWFwJJeVENS6bJG8qtb5jQshJLpI8+0CmiU+aZ1wnIQXaZNVml+Bb9/1A== Received: (qmail 24913 invoked from network); 8 Sep 2017 19:06:37 +0300 Received: from mx01robo.bbu.dsd.mx.bitdefender.com (10.17.80.60) by mx02.buh.bitdefender.com with AES128-GCM-SHA256 encrypted SMTP; 8 Sep 2017 19:06:37 +0300 Received: (qmail 10188 invoked from network); 8 Sep 2017 19:06:37 +0300 Received: from unknown (HELO aisaila-Latitude-E5570.dsd.bitdefender.biz) (10.10.195.54) by mx01robo.bbu.dsd.mx.bitdefender.com with SMTP; 8 Sep 2017 19:06:37 +0300 From: Alexandru Isaila To: xen-devel@lists.xen.org Date: Fri, 8 Sep 2017 19:05:35 +0300 Message-Id: <1504886736-1823-4-git-send-email-aisaila@bitdefender.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1504886736-1823-1-git-send-email-aisaila@bitdefender.com> References: <1504886736-1823-1-git-send-email-aisaila@bitdefender.com> Cc: jun.nakajima@intel.com, kevin.tian@intel.com, sstabellini@kernel.org, wei.liu2@citrix.com, suravee.suthikulpanit@amd.com, george.dunlap@eu.citrix.com, andrew.cooper3@citrix.com, tim@xen.org, paul.durrant@citrix.com, Jan Beulich , Alexandru Isaila , boris.ostrovsky@oracle.com, ian.jackson@eu.citrix.com Subject: [Xen-devel] [PATCH v2 3/4] x86/hvm: Break out __hvm_copy()'s translation logic 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: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Andrew Cooper It will be reused by later changes. Signed-off-by: Andrew Cooper Signed-off-by: Alexandru Isaila --- CC: Jan Beulich Changes since V1: - Changed param name - Added ASSERT --- xen/arch/x86/hvm/hvm.c | 144 +++++++++++++++++++++++--------------- xen/include/asm-x86/hvm/support.h | 12 ++++ 2 files changed, 98 insertions(+), 58 deletions(-) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 488acbf..a60149a 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -3069,6 +3069,83 @@ void hvm_task_switch( hvm_unmap_entry(nptss_desc); } +enum hvm_translation_result hvm_translate_get_page( + struct vcpu *v, unsigned long addr, bool linear, uint32_t pfec, + pagefault_info_t *pfinfo, struct page_info **page_p, + gfn_t *gfn_p, p2m_type_t *p2mt_p) +{ + struct page_info *page; + p2m_type_t p2mt; + gfn_t gfn; + + if ( linear ) + { + gfn = _gfn(paging_gva_to_gfn(v, addr, &pfec)); + + if ( gfn_eq(gfn, INVALID_GFN) ) + { + if ( pfec & PFEC_page_paged ) + return HVMTRANS_gfn_paged_out; + + if ( pfec & PFEC_page_shared ) + return HVMTRANS_gfn_shared; + + if ( pfinfo ) + { + pfinfo->linear = addr; + pfinfo->ec = pfec & ~PFEC_implicit; + } + + return HVMTRANS_bad_linear_to_gfn; + } + } + else + { + gfn = _gfn(addr >> PAGE_SHIFT); + ASSERT(!pfinfo); + } + + /* + * No need to do the P2M lookup for internally handled MMIO, benefiting + * - 32-bit WinXP (& older Windows) on AMD CPUs for LAPIC accesses, + * - newer Windows (like Server 2012) for HPET accesses. + */ + if ( v == current + && !nestedhvm_vcpu_in_guestmode(v) + && hvm_mmio_internal(gfn_x(gfn) << PAGE_SHIFT) ) + return HVMTRANS_bad_gfn_to_mfn; + + page = get_page_from_gfn(v->domain, gfn_x(gfn), &p2mt, P2M_UNSHARE); + + if ( !page ) + return HVMTRANS_bad_gfn_to_mfn; + + if ( p2m_is_paging(p2mt) ) + { + put_page(page); + p2m_mem_paging_populate(v->domain, gfn_x(gfn)); + return HVMTRANS_gfn_paged_out; + } + if ( p2m_is_shared(p2mt) ) + { + put_page(page); + return HVMTRANS_gfn_shared; + } + if ( p2m_is_grant(p2mt) ) + { + put_page(page); + return HVMTRANS_unhandleable; + } + + *page_p = page; + if ( gfn_p ) + *gfn_p = gfn; + if ( p2mt_p ) + *p2mt_p = p2mt; + + return HVMTRANS_okay; +} + #define HVMCOPY_from_guest (0u<<0) #define HVMCOPY_to_guest (1u<<0) #define HVMCOPY_phys (0u<<2) @@ -3077,7 +3154,7 @@ static enum hvm_translation_result __hvm_copy( void *buf, paddr_t addr, int size, struct vcpu *v, unsigned int flags, uint32_t pfec, pagefault_info_t *pfinfo) { - unsigned long gfn; + gfn_t gfn; struct page_info *page; p2m_type_t p2mt; char *p; @@ -3103,65 +3180,15 @@ static enum hvm_translation_result __hvm_copy( while ( todo > 0 ) { + enum hvm_translation_result res; paddr_t gpa = addr & ~PAGE_MASK; count = min_t(int, PAGE_SIZE - gpa, todo); - if ( flags & HVMCOPY_linear ) - { - gfn = paging_gva_to_gfn(v, addr, &pfec); - if ( gfn == gfn_x(INVALID_GFN) ) - { - if ( pfec & PFEC_page_paged ) - return HVMTRANS_gfn_paged_out; - if ( pfec & PFEC_page_shared ) - return HVMTRANS_gfn_shared; - if ( pfinfo ) - { - pfinfo->linear = addr; - pfinfo->ec = pfec & ~PFEC_implicit; - } - return HVMTRANS_bad_linear_to_gfn; - } - gpa |= (paddr_t)gfn << PAGE_SHIFT; - } - else - { - gfn = addr >> PAGE_SHIFT; - gpa = addr; - } - - /* - * No need to do the P2M lookup for internally handled MMIO, benefiting - * - 32-bit WinXP (& older Windows) on AMD CPUs for LAPIC accesses, - * - newer Windows (like Server 2012) for HPET accesses. - */ - if ( v == current - && !nestedhvm_vcpu_in_guestmode(v) - && hvm_mmio_internal(gpa) ) - return HVMTRANS_bad_gfn_to_mfn; - - page = get_page_from_gfn(v->domain, gfn, &p2mt, P2M_UNSHARE); - - if ( !page ) - return HVMTRANS_bad_gfn_to_mfn; - - if ( p2m_is_paging(p2mt) ) - { - put_page(page); - p2m_mem_paging_populate(v->domain, gfn); - return HVMTRANS_gfn_paged_out; - } - if ( p2m_is_shared(p2mt) ) - { - put_page(page); - return HVMTRANS_gfn_shared; - } - if ( p2m_is_grant(p2mt) ) - { - put_page(page); - return HVMTRANS_unhandleable; - } + res = hvm_translate_get_page(v, addr, flags & HVMCOPY_linear, + pfec, pfinfo, &page, &gfn, &p2mt); + if ( res != HVMTRANS_okay ) + return res; p = (char *)__map_domain_page(page) + (addr & ~PAGE_MASK); @@ -3170,10 +3197,11 @@ static enum hvm_translation_result __hvm_copy( if ( p2m_is_discard_write(p2mt) ) { static unsigned long lastpage; - if ( xchg(&lastpage, gfn) != gfn ) + + if ( xchg(&lastpage, gfn_x(gfn)) != gfn_x(gfn) ) dprintk(XENLOG_G_DEBUG, "%pv attempted write to read-only gfn %#lx (mfn=%#lx)\n", - v, gfn, page_to_mfn(page)); + v, gfn_x(gfn), page_to_mfn(page)); } else { diff --git a/xen/include/asm-x86/hvm/support.h b/xen/include/asm-x86/hvm/support.h index e3b035d..d784fc1 100644 --- a/xen/include/asm-x86/hvm/support.h +++ b/xen/include/asm-x86/hvm/support.h @@ -24,6 +24,7 @@ #include #include #include +#include #ifndef NDEBUG #define DBG_LEVEL_0 (1 << 0) @@ -103,6 +104,17 @@ enum hvm_translation_result hvm_fetch_from_guest_linear( void *buf, unsigned long addr, int size, uint32_t pfec, pagefault_info_t *pfinfo); +/* + * Get a reference on the page under an HVM physical or linear address. If + * linear, a pagewalk is performed using pfec (fault details optionally in + * pfinfo). + * On success, returns HVMTRANS_okay with a reference taken on **_page. + */ +enum hvm_translation_result hvm_translate_get_page( + struct vcpu *v, unsigned long addr, bool linear, uint32_t pfec, + pagefault_info_t *pfinfo, struct page_info **page_p, + gfn_t *gfn_p, p2m_type_t *p2mt_p); + #define HVM_HCALL_completed 0 /* hypercall completed - no further action */ #define HVM_HCALL_preempted 1 /* hypercall preempted - re-execute VMCALL */ int hvm_hypercall(struct cpu_user_regs *regs);