From nobody Sun May 5 10:18:41 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=1583317591; cv=none; d=zohomail.com; s=zohoarc; b=h3wFP1kEff+ujhn14Vtq9EPuEPjJQuZQOsPMfUwmsLQXAUnqzp3rN7pqIMWOtvNjNou38Q2v2oEQ8R9QBQJdxKZh85KqFKlBwIs16GVw+YFD4aMLzULPSTyVpHQcL70SU3aiRtjEKZyjUVqfQpKzRBPZk9Lpn+qjb8F28nB0CRI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1583317591; 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=jwiDWnfdmihF1mP2ru3dAwRB41GEQx6BC0pxuLy8RqQ=; b=er0kHUmGl5EXRyP6SnDBm4W9mslatRQo2Voevl8ju9eW+R4TlbkFz6mUF4VDMvcex1IVxmKEmc53yXxWnIWD8cUd7FBcty27WcUq/jdQywQHXQ5cr6khyvbIjPf3OOZZFZ5RkiifTQK2qIPsrP03WbiAaTRaELwDYvL6wmA7eWc= 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 1583317591207877.7635181233334; Wed, 4 Mar 2020 02:26:31 -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 1j9RDm-0005vf-6q; Wed, 04 Mar 2020 10:25:46 +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 1j9RDl-0005vX-HH for xen-devel@lists.xenproject.org; Wed, 04 Mar 2020 10:25:45 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 81476aa8-5e02-11ea-a37e-12813bfff9fa; Wed, 04 Mar 2020 10:25:44 +0000 (UTC) X-Inumbo-ID: 81476aa8-5e02-11ea-a37e-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1583317544; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=wVKtIWkWLCbtRg2wHYMZ2QJn1z7XVRpTANX0I+HLkbs=; b=iKy8w7VJ6ORtf1PDK+EA2n3DHRAuYjXBVu7prCyKzRsXmvDgvbar0H4/ RyW4xbRoguX53rfRgKvzgzfQG0ZLPxKWWnwUx9X4rjdn6aFJnCmIeRjSQ 2r6mpTE5JFJCo8ccXvoI7ewRbrZAukUjyMj80Gy48rlC79WHfxROl5VJS 8=; 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: sowDHIAz8HWRbgdmAvb/4ml+uCpKbtmyinY1p1IX1WXqfytbuIkIXmerQKYOqmjyBQs742ofkU OvydMlms1EQ8CBFD5s5fNRbVzWjMjNJV1rqa7Jo5fRYz+OmJSzAQuk22+UhpPaPPHEvbUqmfPW RYKd6i6lePLOF9tz9ivOb1tr2djHbI5LFYuHqgssGMaVpsSRG7fmISwYWnLjFZCjhbJLA+7qMG ZHQWSsmqRMhnVObR4v3npUUe5GxK6ttZxkmBd69q9pScOEShu1CBqX7Sj/fj7pJtBRyo0hssBx 8ZE= X-SBRS: 2.7 X-MesageID: 13366243 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,513,1574139600"; d="scan'208";a="13366243" From: Roger Pau Monne To: Date: Wed, 4 Mar 2020 11:25:23 +0100 Message-ID: <20200304102523.52454-1-roger.pau@citrix.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v3] 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 Reviewed-by: Jan Beulich --- Changes since v2: - Simplify checks since the kernel used memory must always be a subset of a RAM region. - Fix comment grammar. - Align the loaded kernel area to page boundaries. - For safety assert that all RAM regions in the memory map are page aligned. 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..cc719a0208 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 & PAGE_MASK; + paddr_t kernel_end =3D ROUNDUP((paddr_t)elf->dest_base + elf->dest_siz= e, + PAGE_SIZE); + unsigned int i; + + /* + * The memory map is sorted and all RAM regions starts and sizes are + * aligned to page boundaries. + */ + 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)); + + ASSERT(IS_ALIGNED(start, PAGE_SIZE) && IS_ALIGNED(end, PAGE_SIZE)); + + 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 - start > end - kernel_end ) + end =3D kernel_start; + else + start =3D kernel_end; + + 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 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