From patchwork Fri Jul 29 16:29:02 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: 9252675 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 456F560757 for ; Fri, 29 Jul 2016 16:31:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3695B283E1 for ; Fri, 29 Jul 2016 16:31:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B628283E3; Fri, 29 Jul 2016 16:31:39 +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 C2C06283E1 for ; Fri, 29 Jul 2016 16:31:37 +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 1bTAfC-0003X2-92; Fri, 29 Jul 2016 16:29:30 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bTAfB-0003VC-39 for xen-devel@lists.xenproject.org; Fri, 29 Jul 2016 16:29:29 +0000 Received: from [193.109.254.147] by server-13.bemta-14.messagelabs.com id 7C/B7-01541-8648B975; Fri, 29 Jul 2016 16:29:28 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrDIsWRWlGSWpSXmKPExsXitHSDvW56y+x wg6kL9Cy+b5nM5MDocfjDFZYAxijWzLyk/IoE1ozmk91MBQ91Kn5Ne8/UwHhAoYuRk0NCwF9i 0rPHjCA2m4COxMW5O9m6GDk4RARUJG7vNQAxmQXKJWbciAepEBawl3jzdDdYNYuAqsSMBctZQ GxeAReJ0309zBATdSUenvvNCtLKKeAq8X9VNYgpBFTS0BQKUS0ocXLmE7BOZgFNidbtv9khbH mJ5q2zwaYICShK9M97AHaLhAC3xN9u+wmM/LOQdM9C0j0LSfcCRuZVjBrFqUVlqUW6hoZ6SUW Z6RkluYmZOUCeiV5uanFxYnpqTmJSsV5yfu4mRmDoMQDBDsYzK5wPMUpyMCmJ8i5YMStciC8p P6UyI7E4I76oNCe1+BCjBgeHwOa1qy8wSrHk5eelKknwJjTPDhcSLEpNT61Iy8wBRgdMqQQHj 5II7yyQNG9xQWJucWY6ROoUoy7HlgU31jIJgc2QEufVBykSACnKKM2DGwGL1EuMslLCvIxABw rxFKQW5WaWoMq/YhTnYFQS5q0EmcKTmVcCt+kV0BFMQEcUx84AOaIkESEl1cAo031lSeSliBu Zm80KTzPLP2qcadPU5OLqqvr05/e9nw/3/Jm4SCQoIFj+tlBuyOSVH/VfzhPwfLXa4MyFifFm hgZxUU6HLwbEJfRu1HrQkvM/87/aEsndE07K1n5wstgY/W/9F+PrKcui2aaG/nbJPeub3JP36 WmHceQCET7j6e2ZNuuscoWUWIozEg21mIuKEwHevsPszwIAAA== X-Env-Sender: prvs=011b0443a=roger.pau@citrix.com X-Msg-Ref: server-15.tower-27.messagelabs.com!1469809760!1527603!4 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 12941 invoked from network); 29 Jul 2016 16:29:27 -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:27 -0000 X-IronPort-AV: E=Sophos;i="5.28,440,1464652800"; d="scan'208";a="376340868" From: Roger Pau Monne To: Date: Fri, 29 Jul 2016 18:29:02 +0200 Message-ID: <1469809747-11176-8-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 07/12] xen/x86: parse Dom0 kernel for PVHv2 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 Introduce a helper to parse the Dom0 kernel. Signed-off-by: Roger Pau Monné --- Cc: Jan Beulich Cc: Andrew Cooper --- xen/arch/x86/domain_build.c | 139 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/xen/arch/x86/domain_build.c b/xen/arch/x86/domain_build.c index cb8ecbd..df6354a 100644 --- a/xen/arch/x86/domain_build.c +++ b/xen/arch/x86/domain_build.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,7 @@ #include #include +#include static long __initdata dom0_nrpages; static long __initdata dom0_min_nrpages; @@ -1818,12 +1820,141 @@ static int __init hvm_setup_p2m(struct domain *d) return 0; } +static int __init hvm_load_kernel(struct domain *d, const module_t *image, + unsigned long image_headroom, + module_t *initrd, char *image_base, + char *cmdline, paddr_t *entry, + paddr_t *start_info_addr) +{ + char *image_start = image_base + image_headroom; + unsigned long image_len = image->mod_end; + struct elf_binary elf; + struct elf_dom_parms parms; + paddr_t last_addr; + struct hvm_start_info start_info; + struct hvm_modlist_entry mod; + struct vcpu *saved_current, *v = d->vcpu[0]; + int rc; + + printk("** Parsing Dom0 kernel **\n"); + + if ( (rc = bzimage_parse(image_base, &image_start, &image_len)) != 0 ) + { + printk("Error trying to detect bz compressed kernel\n"); + return rc; + } + + if ( (rc = elf_init(&elf, image_start, image_len)) != 0 ) + { + printk("Unable to init ELF\n"); + return rc; + } +#ifdef VERBOSE + elf_set_verbose(&elf); +#endif + elf_parse_binary(&elf); + if ( (rc = elf_xen_parse(&elf, &parms)) != 0 ) + { + printk("Unable to parse kernel for ELFNOTES\n"); + return rc; + } + + if ( parms.phys_entry == UNSET_ADDR32 ) { + printk("Unable to find kernel entry point, aborting\n"); + return -EINVAL; + } + + printk("OS: %s version: %s loader: %s bitness: %s\n", parms.guest_os, + parms.guest_ver, parms.loader, + elf_64bit(&elf) ? "64-bit" : "32-bit"); + + printk("** Loading Dom0 kernel **\n"); + /* Copy the OS image and free temporary buffer. */ + elf.dest_base = (void *)(parms.virt_kstart - parms.virt_base); + elf.dest_size = parms.virt_kend - parms.virt_kstart; + + saved_current = current; + set_current(v); + + rc = elf_load_binary(&elf); + if ( rc < 0 ) + { + printk("Failed to load kernel: %d\n", rc); + printk("Xen dom0 kernel broken ELF: %s\n", elf_check_broken(&elf)); + goto out; + } + + last_addr = ROUNDUP(parms.virt_kend - parms.virt_base, PAGE_SIZE); + printk("** Copying Dom0 modules **\n"); + + rc = hvm_copy_to_guest_phys(last_addr, mfn_to_virt(initrd->mod_start), + initrd->mod_end); + if ( rc != HVMCOPY_okay ) + { + printk("Unable to copy initrd to guest\n"); + rc = -EFAULT; + goto out; + } + + mod.paddr = last_addr; + mod.size = initrd->mod_end; + last_addr += ROUNDUP(initrd->mod_end, PAGE_SIZE); + + /* Free temporary buffers. */ + discard_initial_images(); + + printk("** Setting up start-of-day info **\n"); + + memset(&start_info, 0, sizeof(start_info)); + if ( cmdline != NULL ) + { + rc = hvm_copy_to_guest_phys(last_addr, cmdline, strlen(cmdline) + 1); + if ( rc != HVMCOPY_okay ) + { + printk("Unable to copy guest command line\n"); + rc = -EFAULT; + goto out; + } + start_info.cmdline_paddr = last_addr; + last_addr += ROUNDUP(strlen(cmdline) + 1, 8); + } + rc = hvm_copy_to_guest_phys(last_addr, &mod, sizeof(mod)); + if ( rc != HVMCOPY_okay ) + { + printk("Unable to copy guest modules\n"); + rc = -EFAULT; + goto out; + } + + start_info.modlist_paddr = last_addr; + start_info.nr_modules = 1; + last_addr += sizeof(mod); + start_info.magic = XEN_HVM_START_MAGIC_VALUE; + start_info.flags = SIF_PRIVILEGED | SIF_INITDOMAIN; + rc = hvm_copy_to_guest_phys(last_addr, &start_info, sizeof(start_info)); + if ( rc != HVMCOPY_okay ) + { + printk("Unable to copy start info to guest\n"); + rc = -EFAULT; + goto out; + } + + *entry = parms.phys_entry; + *start_info_addr = last_addr; + rc = 0; + +out: + set_current(saved_current); + return rc; +} + 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) { + paddr_t entry, start_info; int rc; printk("** Building a PVH Dom0 **\n"); @@ -1843,6 +1974,14 @@ static int __init construct_dom0_hvm(struct domain *d, const module_t *image, return rc; } + rc = hvm_load_kernel(d, image, image_headroom, initrd, bootstrap_map(image), + cmdline, &entry, &start_info); + if ( rc ) + { + printk("Failed to load Dom0 kernel\n"); + return rc; + } + return 0; }