From patchwork Tue Sep 27 15:57:11 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: 9352209 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 2E4BE6077B for ; Tue, 27 Sep 2016 16:01:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1FAA02927A for ; Tue, 27 Sep 2016 16:01:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1430F2928E; Tue, 27 Sep 2016 16:01:01 +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 984E52928A for ; Tue, 27 Sep 2016 16:01:00 +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 1boum5-0007qi-SV; Tue, 27 Sep 2016 15:58:29 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1boum5-0007oC-2p for xen-devel@lists.xenproject.org; Tue, 27 Sep 2016 15:58:29 +0000 Received: from [85.158.143.35] by server-7.bemta-6.messagelabs.com id 09/88-15404-4279AE75; Tue, 27 Sep 2016 15:58:28 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrAIsWRWlGSWpSXmKPExsXitHSDva7K9Ff hBheuMFp83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBkXFlYVLNauOPnvEGsD4yz5LkZODgkBf4lZ C2cyg9hsAjoSF+fuZOti5OAQEVCRuL3XoIuRi4NZYAOjxPTHt1lBaoQF7CTW317PDmKzCKhK7 DqykAnE5hVwlbjb1c4MMVNX4uG532D1nEDx3rYFYDVCAi4Sd2d+ZIWoF5Q4OfMJC4jNLKAp0b r9NzuELS/RvHU2M0S9okT/vAdsEDO5JW6fnso8gZF/FpL2WUjaZyFpX8DIvIpRozi1qCy1SNf ITC+pKDM9oyQ3MTNH19DATC83tbg4MT01JzGpWC85P3cTIzAEGYBgB+OZBYGHGCU5mJREeTXa X4UL8SXlp1RmJBZnxBeV5qQWH2LU4OAQ2Lx29QVGKZa8/LxUJQlej6lAdYJFqempFWmZOcAog SmV4OBREuHlmQaU5i0uSMwtzkyHSJ1i1OXYsuDGWiYhsBlS4rwNIDMEQIoySvPgRsAi9hKjrJ QwLyPQgUI8BalFuZklqPKvGMU5GJWEeRVAVvFk5pXAbXoFdAQT0BFLT7wAOaIkESEl1cDIvFl JcuvXfKM5ztGbrwg9C/L3m/ZL36W8n//y1L7wqowPl2zviZzf+fV48/fHO6UmtQR68vi2fH9y WWShygaBW0derfywcWXVSn4z9/2s/hHaDS89Nd7HN8x+IHTUkfn0m8YM28pw1u/H9BdIJZm9+ Pz32b5AY6X8ku/zpCpXuv5cxpkXn2uqxFKckWioxVxUnAgAIVnmt9MCAAA= X-Env-Sender: prvs=071b8e69e=roger.pau@citrix.com X-Msg-Ref: server-3.tower-21.messagelabs.com!1474991906!34711275!1 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.84; banners=-,-,- X-VirusChecked: Checked Received: (qmail 56784 invoked from network); 27 Sep 2016 15:58:27 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-3.tower-21.messagelabs.com with RC4-SHA encrypted SMTP; 27 Sep 2016 15:58:27 -0000 X-IronPort-AV: E=Sophos;i="5.30,405,1470700800"; d="scan'208";a="389169661" From: Roger Pau Monne To: Date: Tue, 27 Sep 2016 17:57:11 +0200 Message-ID: <1474991845-27962-17-git-send-email-roger.pau@citrix.com> X-Mailer: git-send-email 2.7.4 (Apple Git-66) In-Reply-To: <1474991845-27962-1-git-send-email-roger.pau@citrix.com> References: <1474991845-27962-1-git-send-email-roger.pau@citrix.com> MIME-Version: 1.0 X-DLP: MIA2 Cc: Andrew Cooper , boris.ostrovsky@oracle.com, Roger Pau Monne , Jan Beulich Subject: [Xen-devel] [PATCH v2 16/30] 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 | 138 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/xen/arch/x86/domain_build.c b/xen/arch/x86/domain_build.c index c590c58..effebf1 100644 --- a/xen/arch/x86/domain_build.c +++ b/xen/arch/x86/domain_build.c @@ -39,6 +39,7 @@ #include #include +#include static long __initdata dom0_nrpages; static long __initdata dom0_min_nrpages; @@ -1886,12 +1887,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"); @@ -1911,6 +2041,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; }