From nobody Sat May 4 21:22:59 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1583236452; cv=none; d=zohomail.com; s=zohoarc; b=jhw8IJT8MTz1GANIrEA0BTdmxX8dGe0qcJd4htsH4rHPy1IiK9PL0FGyUHvOlLtbGMNhDRkgOUNoVzzxL/+9504YJMwHxXjXI7rIxpm/3kDUkyWdkfD6IFXwGWT94ym6GeOhHAtgJH1kb0wJLwx3ZhdmlyQmTFDbfOcwQgHm+g4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1583236452; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=kBJQ3gxXeSg7NhhuQUEwW/nzCTlzcbzUlRYAur6rFik=; b=ZraBoB3o/jB+kNBLB0zIbtee4cO2IMgsufkZucCd7Z4Rquvirj0hrPBShSpH6gAu0TuiyKPR2unwsrxuXq8IqJWUFDLZXt9LmG7bTh0k6eH0P98UrES55pOi8bIBpUcLkyM6KaJCtLroUJfzw8j1eqkZy+IHa4upF+Uj/88n32Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1583236452331404.3354011175845; Tue, 3 Mar 2020 03:54:12 -0800 (PST) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j9673-0001NJ-Ai; Tue, 03 Mar 2020 11:53:25 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j9672-0001NE-34 for xen-devel@lists.xenproject.org; Tue, 03 Mar 2020 11:53:24 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 951a7d44-5d45-11ea-a132-12813bfff9fa; Tue, 03 Mar 2020 11:53:22 +0000 (UTC) X-Inumbo-ID: 951a7d44-5d45-11ea-a132-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1583236403; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=EJ2Y4K1/MLP14q3XX9JMCQKA3bXUY1R9HuMcyEsM/RY=; b=hCuJQ1JVBDOGugfDozZMdYa1knINMLHyZDHH/vRBLl4oeY19w+vzfoNS ruKSoblVJhjQ+HNwwEIDoHqVsqWiXvOiE7X+QFamMJ4WNDCuvZ1yuXIS/ jT+/+daBCuiB5IDSIT+Noitq6yGTmNEJfagGZZ7kakJSLv5Fs/vktgZu3 o=; Authentication-Results: esa3.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 (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa3.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=esa3.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="roger.pau@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa3.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=esa3.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 (esa3.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=esa3.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: m2rlbu+djAEFOD7LwoLXS1cuzbQhkfhIL0cPHxNFmIyPIjFYvrAz8L1ZAHPWhdDmio6+YDkCRp pA3iB52xIgNYy8dBp516EkoDTrEaXzOtMVbkdYJZoYmexE6ruxy8tY5YbehuRLJ4ok5XFwXwqN rOEk6rcTvuKIzdMldw/juS3mJP/0b17n6jir/pQNqJX+gP4zAq+o0aonzSr0q9aVvnawOT8I42 lyvubj4DiR/9IRN02otspuRQxIh6UqB9gYmOBTrpLXKwwfyUTL5FduO2BZt1yjHQ+WS5PdGyre dW0= X-SBRS: 2.7 X-MesageID: 13305173 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.70,511,1574139600"; d="scan'208";a="13305173" From: Roger Pau Monne To: Date: Tue, 3 Mar 2020 12:52:53 +0100 Message-ID: <20200303115253.47449-1-roger.pau@citrix.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v2] 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) 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=C3=A9 Tested-by: Andrew Cooper --- Changes since v1: - Calculate end of e820 entry earlier. - Only check if the end of the region is < 1MB. - Check for range overlaps with the kernel region. - Check the region is of type RAM. - Fix off-by-one checks in range overlaps. - Add a comment about why initrd and metadata is placed together. - Add parentheses around size calculations. --- xen/arch/x86/hvm/dom0_build.c | 58 ++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/hvm/dom0_build.c b/xen/arch/x86/hvm/dom0_build.c index eded87eaf5..33520ec1bc 100644 --- a/xen/arch/x86/hvm/dom0_build.c +++ b/xen/arch/x86/hvm/dom0_build.c @@ -490,6 +490,45 @@ static int __init pvh_populate_p2m(struct domain *d) #undef MB1_PAGES } =20 +static paddr_t find_memory(const struct domain *d, const struct elf_binary= *elf, + size_t size) +{ + paddr_t kernel_start =3D (paddr_t)elf->dest_base; + paddr_t kernel_end =3D (paddr_t)(elf->dest_base + elf->dest_size); + unsigned int i; + + for ( i =3D 0; i < d->arch.nr_e820; i++ ) + { + paddr_t start, end =3D d->arch.e820[i].addr + d->arch.e820[i].size; + + /* Don't use memory below 1MB, as it could overwrite the BDA/EBDA.= */ + if ( end <=3D MB(1) || d->arch.e820[i].type !=3D E820_RAM ) + continue; + + start =3D MAX(ROUNDUP(d->arch.e820[i].addr, PAGE_SIZE), MB(1)); + + if ( end <=3D kernel_start || start >=3D kernel_end ) + ; /* No overlap, nothing to do. */ + /* Deal with the kernel already being loaded in the region. */ + else if ( kernel_start <=3D start && kernel_end > start ) + /* Truncate the start of the region. */ + start =3D ROUNDUP(kernel_end, PAGE_SIZE); + else if ( kernel_start <=3D end && kernel_end > end ) + /* Truncate the end of the region. */ + end =3D kernel_start; + /* Pick the biggest of the split regions. */ + else if ( kernel_start - start > end - kernel_end ) + end =3D kernel_start; + else + start =3D ROUNDUP(kernel_end, PAGE_SIZE); + + if ( end - start >=3D 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 +585,24 @@ static int __init pvh_load_kernel(struct domain *d, co= nst module_t *image, return rc; } =20 - last_addr =3D ROUNDUP(parms.virt_kend - parms.virt_base, PAGE_SIZE); + /* + * Find a RAM region big enough (and that doesn't overlap with the loa= ded + * kernel) in order to load the initrd and the metadata. Note it could= be + * split into smaller allocations, done it as a single region in order= to + * simplify it. + */ + last_addr =3D 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 =3D=3D INVALID_PADDR ) + { + printk("Unable to find a memory region to load initrd and metadata= \n"); + return -ENOMEM; + } =20 if ( initrd !=3D NULL ) { --=20 2.25.0 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel