From patchwork Mon Mar 2 15:55:09 2020 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: 11415793 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 87FC992A for ; Mon, 2 Mar 2020 15:57:10 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6438B21D56 for ; Mon, 2 Mar 2020 15:57:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="SI3YmTN6" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6438B21D56 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j8nPu-0004EA-K0; Mon, 02 Mar 2020 15:55:38 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j8nPt-0004E5-A0 for xen-devel@lists.xenproject.org; Mon, 02 Mar 2020 15:55:37 +0000 X-Inumbo-ID: 41691edc-5c9e-11ea-932e-bc764e2007e4 Received: from esa4.hc3370-68.iphmx.com (unknown [216.71.155.144]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 41691edc-5c9e-11ea-932e-bc764e2007e4; Mon, 02 Mar 2020 15:55:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1583164536; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=RgqdBUX+c9cqV970cHWW+GqlPnZxNAo1T22Uo5zE7go=; b=SI3YmTN6Eqc5i4vmE1aqYGfkIaw4K190wGBQQKl2Ph7+4HwO9oNePvaX d5RIKGkwJOZgmCEg6yAyMJnnZ/pqYu8lWrZM/hgQp/q+qoDxCV8dmpbPW lM963jNEJ1u0kHgpLpoqNnJnJrevXP+YuTFWHBQS1a9leG5auV08WrCxf U=; Authentication-Results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=roger.pau@citrix.com; spf=Pass smtp.mailfrom=roger.pau@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of roger.pau@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="roger.pau@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa4.hc3370-68.iphmx.com: domain of roger.pau@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="roger.pau@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ip4:168.245.78.127 ~all" Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: az7A6hgZRkR99j9p/uu5cmmtVOrECAr7YKWsFJoERmip0bU/A2mqZRf6ifTSTmSJ+RTk1hyOSx Yt5Pe+0EjZgrRLaOsPDPMrGraZTrhvmeL9FSFukpOSpRKfDSpaTSNQMr+721T2FnNyP9cMVH0I B+wGczorq1DYOPH+80HxtS8aMhcpLayRR49cn4XarfLdnGoZNL9NL7LsMKi6XdqNNf8X30GTTg KpN2j992zxyRcNSPL+EEa0vaXvKH8Z8xqfVlH78tyTXlfz1b92qLa0oe8t9JVXOiNZ1yGeS7Zs Hog= X-SBRS: 2.7 X-MesageID: 13904351 X-Ironport-Server: esa4.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.70,507,1574139600"; d="scan'208";a="13904351" From: Roger Pau Monne To: Date: Mon, 2 Mar 2020 16:55:09 +0100 Message-ID: <20200302155509.44753-1-roger.pau@citrix.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH] x86/dom0: improve PVH initrd and metadata placement X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Wei Liu , Jan Beulich , Roger Pau Monne Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Don't assume there's going to be enough space at the tail of the loaded kernel and instead try to find a suitable memory area where the initrd and metadata can be loaded. Reported-by: Andrew Cooper Signed-off-by: Roger Pau Monné --- xen/arch/x86/hvm/dom0_build.c | 51 ++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/hvm/dom0_build.c b/xen/arch/x86/hvm/dom0_build.c index eded87eaf5..a03bf2e663 100644 --- a/xen/arch/x86/hvm/dom0_build.c +++ b/xen/arch/x86/hvm/dom0_build.c @@ -490,6 +490,44 @@ static int __init pvh_populate_p2m(struct domain *d) #undef MB1_PAGES } +static paddr_t find_memory(const struct domain *d, const struct elf_binary *elf, + size_t size) +{ + paddr_t kernel_start = (paddr_t)elf->dest_base; + paddr_t kernel_end = (paddr_t)(elf->dest_base + elf->dest_size); + unsigned int i; + + for ( i = 0; i < d->arch.nr_e820; i++ ) + { + paddr_t start, end; + + if ( d->arch.e820[i].addr < MB(1) && + d->arch.e820[i].addr + d->arch.e820[i].size < MB(1) ) + continue; + + start = MAX(ROUNDUP(d->arch.e820[i].addr, PAGE_SIZE), MB(1)); + end = d->arch.e820[i].addr + d->arch.e820[i].size; + + /* Deal with the kernel being loaded in the region. */ + if ( kernel_start <= start && kernel_end >= start ) + /* Truncate the start of the region */ + start = ROUNDUP(kernel_end, PAGE_SIZE); + else if ( kernel_start <= end && kernel_end >= end ) + /* Truncate the end of the region */ + end = kernel_start; + /* Pick the biggest of the split regions */ + else if ( kernel_start - start > end - kernel_end ) + end = kernel_start; + else + start = ROUNDUP(kernel_end, PAGE_SIZE); + + if ( end - start >= size ) + return start; + } + + return INVALID_PADDR; +} + static int __init pvh_load_kernel(struct domain *d, const module_t *image, unsigned long image_headroom, module_t *initrd, void *image_base, @@ -546,7 +584,18 @@ static int __init pvh_load_kernel(struct domain *d, const module_t *image, return rc; } - last_addr = ROUNDUP(parms.virt_kend - parms.virt_base, PAGE_SIZE); + last_addr = find_memory(d, &elf, sizeof(start_info) + + initrd ? ROUNDUP(initrd->mod_end, PAGE_SIZE) + + sizeof(mod) + : 0 + + cmdline ? ROUNDUP(strlen(cmdline) + 1, + elf_64bit(&elf) ? 8 : 4) + : 0); + if ( last_addr == INVALID_PADDR ) + { + printk("Unable to find a memory region to load initrd and metadata\n"); + return -ENOMEM; + } if ( initrd != NULL ) {