From patchwork Fri Jul 29 16:29:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 9252681 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 0BF7F60757 for ; Fri, 29 Jul 2016 16:31:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F0264283E1 for ; Fri, 29 Jul 2016 16:31:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E4F0F283E3; Fri, 29 Jul 2016 16:31: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.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 207AA283E1 for ; Fri, 29 Jul 2016 16:31:51 +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 1bTAfA-0003VI-St; Fri, 29 Jul 2016 16:29:28 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bTAf8-0003UA-Vg for xen-devel@lists.xenproject.org; Fri, 29 Jul 2016 16:29:27 +0000 Received: from [193.109.254.147] by server-4.bemta-14.messagelabs.com id 4E/9C-10431-6648B975; Fri, 29 Jul 2016 16:29:26 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrPIsWRWlGSWpSXmKPExsXitHSDvW5qy+x wg0NtShbft0xmcmD0OPzhCksAYxRrZl5SfkUCa8ba1uyCBveK/a8/sTcw3jDsYuTkkBDwl7i0 9AsriM0moCNxce5Oti5GDg4RARWJ23sNQExmgXKJGTfiQSqEBbwlTry9zQJiswioShxfsZ8Nx OYVcJE42NDFBjFRV+Lhud+sIK2cAq4S/1dVg5hCQCUNTaEQ1YISJ2c+AZvCLKAp0br9NzuELS /RvHU2M4gtJKAo0T/vAdgtEgLcEn+77Scw8s9C0j0LSfcsJN0LGJlXMWoUpxaVpRbpGhrqJRV lpmeU5CZm5gB5Jnq5qcXFiempOYlJxXrJ+bmbGIGBxwAEOxjPrHA+xCjJwaQkyrtgxaxwIb6k /JTKjMTijPii0pzU4kOMGhwcApvXrr7AKMWSl5+XqiTBm9A8O1xIsCg1PbUiLTMHGBswpRIcP EoivLNA0rzFBYm5xZnpEKlTjIpS4rz6IAkBkERGaR5cGyweLzHKSgnzMgIdJcRTkFqUm1mCKv +KUZyDUUmYtxJkCk9mXgnc9FdAi5mAFhfHzgBZXJKIkJJqYFy8dO0Oy3UXv4VXHvEqD7kdELT 6V4rSrTnyPxletH/mkLeTVLQv+bs/g5G5Xi7K5KnopspP8dopr7wXuhl09Lyu0Hwsz5rE2RrP s2fq1fslc/8fP2195cC6Dzclj0RzXKz41X3C+UbSia75D+4e6dr9+k39jASv1HbjxlrbA8994 gt5DjSGmimxFGckGmoxFxUnAgAfbMJ/wgIAAA== X-Env-Sender: prvs=011b0443a=roger.pau@citrix.com X-Msg-Ref: server-15.tower-27.messagelabs.com!1469809760!1527603!3 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: 8.77; banners=-,-,- X-VirusChecked: Checked Received: (qmail 12495 invoked from network); 29 Jul 2016 16:29:25 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-15.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 29 Jul 2016 16:29:25 -0000 X-IronPort-AV: E=Sophos;i="5.28,440,1464652800"; d="scan'208";a="376340863" From: Roger Pau Monne To: Date: Fri, 29 Jul 2016 18:29:01 +0200 Message-ID: <1469809747-11176-7-git-send-email-roger.pau@citrix.com> X-Mailer: git-send-email 2.7.4 (Apple Git-66) In-Reply-To: <1469809747-11176-1-git-send-email-roger.pau@citrix.com> References: <1469809747-11176-1-git-send-email-roger.pau@citrix.com> MIME-Version: 1.0 X-DLP: MIA1 Cc: Andrew Cooper , Jan Beulich , Roger Pau Monne Subject: [Xen-devel] [PATCH RFC 06/12] xen/x86: populate PVHv2 Dom0 physical memory map 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 Craft the Dom0 e820 memory map and populate it. Signed-off-by: Roger Pau Monné --- Cc: Jan Beulich Cc: Andrew Cooper --- xen/arch/x86/domain_build.c | 199 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 193 insertions(+), 6 deletions(-) diff --git a/xen/arch/x86/domain_build.c b/xen/arch/x86/domain_build.c index c0ef40f..cb8ecbd 100644 --- a/xen/arch/x86/domain_build.c +++ b/xen/arch/x86/domain_build.c @@ -43,6 +43,11 @@ static long __initdata dom0_nrpages; static long __initdata dom0_min_nrpages; static long __initdata dom0_max_nrpages = LONG_MAX; +/* GFN of the identity map for EPT. */ +#define HVM_IDENT_PT_GFN 0xfeffeu + +static unsigned int __initdata hvm_mem_stats[MAX_ORDER + 1]; + /* * dom0_mem=[min:,][max:,][] * @@ -304,7 +309,8 @@ static unsigned long __init compute_dom0_nr_pages( avail -= max_pdx >> s; } - need_paging = opt_dom0_shadow || (is_pvh_domain(d) && !iommu_hap_pt_share); + need_paging = opt_dom0_shadow || (has_hvm_container_domain(d) && + (!iommu_hap_pt_share || !paging_mode_hap(d))); for ( ; ; need_paging = 0 ) { nr_pages = dom0_nrpages; @@ -336,7 +342,8 @@ static unsigned long __init compute_dom0_nr_pages( avail -= dom0_paging_pages(d, nr_pages); } - if ( (parms->p2m_base == UNSET_ADDR) && (dom0_nrpages <= 0) && + if ( is_pv_domain(d) && + (parms->p2m_base == UNSET_ADDR) && (dom0_nrpages <= 0) && ((dom0_min_nrpages <= 0) || (nr_pages > min_pages)) ) { /* @@ -547,11 +554,12 @@ static __init void pvh_map_all_iomem(struct domain *d, unsigned long nr_pages) ASSERT(nr_holes == 0); } -static __init void pvh_setup_e820(struct domain *d, unsigned long nr_pages) +static __init void hvm_setup_e820(struct domain *d, unsigned long nr_pages) { struct e820entry *entry, *entry_guest; unsigned int i; unsigned long pages, cur_pages = 0; + uint64_t start, end; /* * Craft the e820 memory map for Dom0 based on the hardware e820 map. @@ -579,8 +587,19 @@ static __init void pvh_setup_e820(struct domain *d, unsigned long nr_pages) continue; } - *entry_guest = *entry; - pages = PFN_UP(entry_guest->size); + /* + * Make sure the start and length are aligned to PAGE_SIZE, because + * that's the minimum granularity of the 2nd stage translation. + */ + start = ROUNDUP(entry->addr, PAGE_SIZE); + end = (entry->addr + entry->size) & PAGE_MASK; + if ( start >= end ) + continue; + + entry_guest->type = E820_RAM; + entry_guest->addr = start; + entry_guest->size = end - start; + pages = PFN_DOWN(entry_guest->size); if ( (cur_pages + pages) > nr_pages ) { /* Truncate region */ @@ -591,6 +610,8 @@ static __init void pvh_setup_e820(struct domain *d, unsigned long nr_pages) { cur_pages += pages; } + ASSERT((entry_guest->addr & ~PAGE_MASK) == 0 && + (entry_guest->size & ~PAGE_MASK) == 0); next: d->arch.nr_e820++; entry_guest++; @@ -1631,7 +1652,7 @@ static int __init construct_dom0_pv( dom0_update_physmap(d, pfn, mfn, 0); pvh_map_all_iomem(d, nr_pages); - pvh_setup_e820(d, nr_pages); + hvm_setup_e820(d, nr_pages); } if ( d->domain_id == hardware_domid ) @@ -1647,15 +1668,181 @@ out: return rc; } +/* Helper to convert from bytes into human-readable form. */ +static void __init pretty_print_bytes(uint64_t size) +{ + const char* units[] = {"B", "KB", "MB", "GB", "TB"}; + int i = 0; + + while ( ++i < sizeof(units) && size >= 1024 ) + size >>= 10; /* size /= 1024 */ + + printk("%4" PRIu64 "%2s", size, units[i-1]); +} + +/* Calculate the biggest usable order given a size in bytes. */ +static inline unsigned int get_order(uint64_t size) +{ + unsigned int order; + uint64_t pg; + + ASSERT((size & ~PAGE_MASK) == 0); + pg = PFN_DOWN(size); + for ( order = 0; pg >= (1 << (order + 1)); order++ ); + + return order; +} + +/* Populate an HVM memory range using the biggest possible order. */ +static void __init hvm_populate_memory_range(struct domain *d, uint64_t start, + uint64_t size) +{ + static unsigned int __initdata memflags = MEMF_no_dma|MEMF_exact_node; + unsigned int order; + struct page_info *page; + int rc; + + ASSERT((size & ~PAGE_MASK) == 0 && (start & ~PAGE_MASK) == 0); + + order = MAX_ORDER; + while ( size != 0 ) + { + order = min(get_order(size), order); + page = alloc_domheap_pages(d, order, memflags); + if ( page == NULL ) + { + if ( order == 0 && memflags ) + { + /* Try again without any memflags. */ + memflags = 0; + order = MAX_ORDER; + continue; + } + if ( order == 0 ) + panic("Unable to allocate memory with order 0!\n"); + order--; + continue; + } + + hvm_mem_stats[order]++; + rc = guest_physmap_add_page(d, _gfn(PFN_DOWN(start)), + _mfn(page_to_mfn(page)), order); + if ( rc != 0 ) + panic("Failed to populate memory: [%" PRIx64 " - %" PRIx64 "] %d\n", + start, start + (((uint64_t)1) << (order + PAGE_SHIFT)), rc); + start += ((uint64_t)1) << (order + PAGE_SHIFT); + size -= ((uint64_t)1) << (order + PAGE_SHIFT); + if ( (size & 0xffffffff) == 0 ) + process_pending_softirqs(); + } + +} + +static int __init hvm_setup_p2m(struct domain *d) +{ + struct vcpu *v = d->vcpu[0]; + unsigned long nr_pages; + int i; + + printk("** Preparing memory map **\n"); + + /* + * Subtract one page for the EPT identity page table and two pages + * for the MADT replacement. + */ + nr_pages = compute_dom0_nr_pages(d, NULL, 0) - 3; + + hvm_setup_e820(d, nr_pages); + paging_set_allocation(d, dom0_paging_pages(d, nr_pages)); + + printk("Dom0 memory map:\n"); + print_e820_memory_map(d->arch.e820, d->arch.nr_e820); + + printk("** Populating memory map **\n"); + /* Populate memory map. */ + for ( i = 0; i < d->arch.nr_e820; i++ ) + { + if ( d->arch.e820[i].type != E820_RAM ) + continue; + + hvm_populate_memory_range(d, d->arch.e820[i].addr, + d->arch.e820[i].size); + } + + printk("Memory allocation stats:\n"); + for ( i = 0; i <= MAX_ORDER; i++ ) + { + if ( hvm_mem_stats[MAX_ORDER - i] != 0 ) + { + printk("Order %2u: ", MAX_ORDER - i); + pretty_print_bytes(((uint64_t)1 << (MAX_ORDER - i + PAGE_SHIFT)) * + hvm_mem_stats[MAX_ORDER - i]); + printk("\n"); + } + } + + if ( cpu_has_vmx && paging_mode_hap(d) && !vmx_unrestricted_guest(v) ) + { + struct vcpu *saved_current; + struct page_info *page; + uint32_t *ident_pt; + + /* + * Identity-map page table is required for running with CR0.PG=0 + * when using Intel EPT. Create a 32-bit non-PAE page directory of + * superpages. + */ + page = alloc_domheap_pages(d, 0, 0); + if ( unlikely(!page) ) + { + printk("Unable to allocate page for identity map\n"); + return -ENOMEM; + } + + saved_current = current; + set_current(v); + + ident_pt = __map_domain_page(page); + for ( i = 0; i < PAGE_SIZE / sizeof(*ident_pt); i++ ) + ident_pt[i] = ((i << 22) | _PAGE_PRESENT | _PAGE_RW | _PAGE_USER | + _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); + unmap_domain_page(ident_pt); + + guest_physmap_add_page(d, _gfn(HVM_IDENT_PT_GFN), + _mfn(page_to_mfn(page)), 0); + d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT] = + HVM_IDENT_PT_GFN << PAGE_SHIFT; + set_current(saved_current); + } + + return 0; +} + static int __init construct_dom0_hvm(struct domain *d, const module_t *image, unsigned long image_headroom, module_t *initrd, void *(*bootstrap_map)(const module_t *), char *cmdline) { + int rc; printk("** Building a PVH Dom0 **\n"); + /* Sanity! */ + BUG_ON(d->domain_id != 0); + BUG_ON(d->vcpu[0] == NULL); + + process_pending_softirqs(); + + iommu_hwdom_init(d); + + rc = hvm_setup_p2m(d); + if ( rc ) + { + printk("Failed to setup Dom0 physical memory map\n"); + return rc; + } + return 0; }