From patchwork Wed Sep 20 09:22:32 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: 9961167 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 84CD9601D5 for ; Wed, 20 Sep 2017 09:25:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 75D0629047 for ; Wed, 20 Sep 2017 09:25:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6A89829049; Wed, 20 Sep 2017 09:25:02 +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 89CD529048 for ; Wed, 20 Sep 2017 09:25:01 +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 1dubDV-0007eZ-Ql; Wed, 20 Sep 2017 09:22:49 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dubDU-0007dg-Jg for xen-devel@lists.xen.org; Wed, 20 Sep 2017 09:22:48 +0000 Received: from [85.158.137.68] by server-1.bemta-3.messagelabs.com id A7/2E-02048-76332C95; Wed, 20 Sep 2017 09:22:47 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprPIsWRWlGSWpSXmKPExsUSfTyjVTfd+FC kwZa7HBZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa0bThYdsBUstKjbMPsTSwNis3sXIycEsYC3R +6+ZsYuRi4NFYBaLxK6ji5kgnAksEvtvzmcHqRIS8JCYdmkDO0hCSGABo0T35uMsMInrXztYI BLLGCVOtdwDS7AJGEi8+vqNEcQWEZCWuPb5MtgOZoGvTBKbGtcwgySEBbwlfr29CLSPA2ifqs Spg+YgYV4Bd4kl80+DbZYQkJO4ea4TrJwTaNmLl4+ZQcqFgGrWXUmfwAh0DcMqRvXi1KKy1CJ dU72kosz0jJLcxMwcXUMDY73c1OLixPTUnMSkYr3k/NxNjMDAqmdgYNzBePmr0yFGSQ4mJVFe FZuDkUJ8SfkplRmJxRnxRaU5qcWHGGU4OJQkeE0cgHKCRanpqRVpmTnAEIdJS3DwKInwKoKke YsLEnOLM9MhUqcYdTk6bt79wyTEkpeflyolzssCUiQAUpRRmgc3AhZvlxhlpYR5GRkYGIR4Cl KLcjNLUOVfMYpzMCoJQ1zCk5lXArfpFdARTEBHZG84AHJESSJCSqqB8ciKY4t8lApe3ZacmlC zzlgpaprqqcwpB+5YXfYsmfejUXjh/dSFu/cm9fqzbuNRNyy54yK76MWstEtn1980mnhHViil UOUcw7+5O2sm/Lsf4sZ9OfH5ltWzk697xf7ZemtvwVbjy9/nfBWYvPuhyJfr4dVHjCes3a/zX IzHP0bIc/6bFB2rZVeUWIozEg21mIuKEwFCiq4JsgIAAA== X-Env-Sender: aisaila@bitdefender.com X-Msg-Ref: server-7.tower-31.messagelabs.com!1505899366!108101702!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 20247 invoked from network); 20 Sep 2017 09:22:47 -0000 Received: from mx02.bbu.dsd.mx.bitdefender.com (HELO mx02.buh.bitdefender.com) (91.199.104.133) by server-7.tower-31.messagelabs.com with DHE-RSA-AES128-GCM-SHA256 encrypted SMTP; 20 Sep 2017 09:22:47 -0000 Comment: DomainKeys? See http://domainkeys.sourceforge.net/ DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=bitdefender.com; b=zAbXUG5kSrwgEC/LtY0b3yq5hzOLWKJX3Uq4iSSSeJm1ixHacrgbuoh86SzLGAXjz4LVLBhDUczbbmwlfdY65YprkQdXIAvldzc9fB+CTu9Zqa3Tnl2EuxHyPaxViybrLocmhN/ETjLMVo3w0xMY8iNOFzdhnquMsa8CqrKMqQ3lL9DP5RxuvIrbd9Gt4G7RhJM9w/LBotGTMQTI9JtPnNn/Zqj/aKBhO+z/cXuGebpsCSN9Or7eoCZzHuheZL6RzbktYjEOBaIYJUrieck7EF2gz9VMGQYTgq7pHYvXvCfa4qM5jkiTx4Sjc4og9TTkQMBx1w6xLTPHW+8ofc73/Q==; 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=AmeFPFcKlQb0AY0ZSg1lV+CYsQ8=; b=k3ijF468rjC1ldziXWL/Ekrk/Tzw HJR2ppKZyyltqeS9JG+r+cygJhHoSA45U3ZoVQBt+wbm/Oz/uE/ARqyWJdMVAkqM ooFQlMLlN9ca5CIw7u0TWCgafJXjJKoPhwC07oNv7EkN8TufgD7YGyl5AV9We2in euGZexJjg4b7iwAfGMnSr9YbqhZzZPcGnYJrcES6rQ0DfoCLUPzqCKdYzlfMh2Sc PObeG6qQghENibFOGrEFSHkuZD0L9FIVKUhOc1qhEhT2nDA0uPH8U7rMiWAhLcea oN6gMRbI7hNYKOmKvnIPKki6SyBN6hI1I8F7XwrYoHT/j5r3vuazxAx75Q== Received: (qmail 31975 invoked from network); 20 Sep 2017 12:22:42 +0300 Received: from mx01robo.bbu.dsd.mx.bitdefender.com (10.17.80.60) by mx02.buh.bitdefender.com with AES128-GCM-SHA256 encrypted SMTP; 20 Sep 2017 12:22:41 +0300 Received: (qmail 11738 invoked from network); 20 Sep 2017 12:22:42 +0300 Received: from unknown (HELO aisaila-Latitude-E5570.dsd.bitdefender.biz) (10.10.195.54) by mx01robo.bbu.dsd.mx.bitdefender.com with SMTP; 20 Sep 2017 12:22:42 +0300 From: Alexandru Isaila To: xen-devel@lists.xen.org Date: Wed, 20 Sep 2017 12:22:32 +0300 Message-Id: <1505899353-13554-3-git-send-email-aisaila@bitdefender.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1505899353-13554-1-git-send-email-aisaila@bitdefender.com> References: <1505899353-13554-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, jbeulich@suse.com, Alexandru Isaila , boris.ostrovsky@oracle.com, ian.jackson@eu.citrix.com Subject: [Xen-devel] [PATCH v4 2/3] 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 Reviewed-by: Paul Durrant Acked-by: Jan Beulich --- 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..93394c1 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 = gaddr_to_gfn(addr); + 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_to_gaddr(gfn)) ) + 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);