All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Smith" <dpsmith@apertussolutions.com>
To: xen-devel@lists.xenproject.org
Cc: "Daniel P. Smith" <dpsmith@apertussolutions.com>,
	jason.andryuk@amd.com, stefano.stabellini@amd.com,
	agarciav@amd.com, "Jan Beulich" <jbeulich@suse.com>,
	"Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>,
	"Anthony PERARD" <anthony.perard@vates.tech>,
	"Michal Orzel" <michal.orzel@amd.com>,
	"Julien Grall" <julien@xen.org>,
	"Stefano Stabellini" <sstabellini@kernel.org>
Subject: [RFCv2 32/38] x86/hyperlaunch: move headroom under domain builder
Date: Thu, 15 May 2025 09:19:44 -0400	[thread overview]
Message-ID: <20250515131951.5594-3-dpsmith@apertussolutions.com> (raw)
In-Reply-To: <20250515131951.5594-1-dpsmith@apertussolutions.com>

The function bzimage_headroom attempted to determine the necessary headroom for
all supported kernel types, not just bzImage. The result was convoluted logic
to handle three kernel image types, and then within the bzImage type, also
handle three types of payloads.

This commit moves the generalized headroom determination for the three kernel
types to the domain builder and scopes bziamge_headroom to only doing headroom
calculations for bzimage payload types.

Signed-off-by: Daniel P. Smith <dpsmith@apertussolutions.com>
---
 xen/arch/x86/bzimage.c                    | 43 +++++++++++------------
 xen/arch/x86/domain-builder/domain.c      | 24 +++++++++++++
 xen/arch/x86/include/asm/bzimage.h        |  1 +
 xen/arch/x86/include/asm/domain-builder.h |  2 ++
 xen/arch/x86/setup.c                      |  6 ++--
 xen/include/xen/gunzip.h                  |  9 +++++
 6 files changed, 58 insertions(+), 27 deletions(-)

diff --git a/xen/arch/x86/bzimage.c b/xen/arch/x86/bzimage.c
index eaea64b9c014..ba090b3ef9d6 100644
--- a/xen/arch/x86/bzimage.c
+++ b/xen/arch/x86/bzimage.c
@@ -47,8 +47,10 @@ struct __packed setup_header {
         uint32_t        payload_length;
     };
 
-static __init int bzimage_check(struct setup_header *hdr, unsigned long len)
+int __init bzimage_check(void *image, unsigned long len)
 {
+    struct setup_header *hdr = (struct setup_header *)image;
+
     if ( len < sizeof(struct setup_header) )
         return 0;
 
@@ -56,9 +58,10 @@ static __init int bzimage_check(struct setup_header *hdr, unsigned long len)
         return 0;
 
     if ( hdr->version < VERSION(2,8) ) {
-        printk("Cannot load bzImage v%d.%02d at least v2.08 is required\n",
-           hdr->version >> 8, hdr->version & 0xff);
-        return -EINVAL;
+        printk(XENLOG_ERR
+               "Cannot load bzImage v%d.%02d at least v2.08 is required\n",
+               hdr->version >> 8, hdr->version & 0xff);
+        return 0;
     }
     return 1;
 }
@@ -69,34 +72,28 @@ unsigned long __init bzimage_headroom(void *image_start,
                                       unsigned long image_length)
 {
     struct setup_header *hdr = (struct setup_header *)image_start;
-    int err;
     unsigned long headroom;
 
-    err = bzimage_check(hdr, image_length);
-    if ( err < 0 )
-        return 0;
-
-    if ( err > 0 )
-    {
-        image_start += (hdr->setup_sects + 1) * 512 + hdr->payload_offset;
-        image_length = hdr->payload_length;
-    }
-
-    if ( elf_is_elfbinary(image_start, image_length) )
-        return 0;
+    image_start += (hdr->setup_sects + 1) * 512 + hdr->payload_offset;
+    image_length = hdr->payload_length;
 
     orig_image_len = image_length;
     /* Linux build system mimics gzip isize field for bzip2/lzma algos */
     headroom = gzip_isize(image_start, image_length);
+    /*
+     * Final headroom:
+     *  - gzip: original size + heuristically derived value
+     *  - uncompressed kernel: zero the headroom
+     *  - bzip2 & lzma: original size + payload size
+     */
     if (gzip_check(image_start, image_length))
-    {
-        headroom += headroom >> 12; /* Add 8 bytes for every 32K input block */
-        headroom += (32768 + 18); /* Add 32K + 18 bytes of extra headroom */
-    } else
+        headroom = gzip_headroom(headroom);
+    else if ( elf_is_elfbinary(image_start, image_length) )
+        headroom = 0;
+    else
         headroom += image_length;
-    headroom = (headroom + 4095) & ~4095;
 
-    return headroom;
+    return ROUNDUP(headroom, PAGE_SIZE);
 }
 
 int __init bzimage_parse(void *image_base, void **image_start,
diff --git a/xen/arch/x86/domain-builder/domain.c b/xen/arch/x86/domain-builder/domain.c
index 673fc5b16ed3..9c2f30eee8bd 100644
--- a/xen/arch/x86/domain-builder/domain.c
+++ b/xen/arch/x86/domain-builder/domain.c
@@ -9,6 +9,7 @@
 #include <xen/err.h>
 #include <xen/event.h>
 #include <xen/grant_table.h>
+#include <xen/gunzip.h>
 #include <xen/init.h>
 #include <xen/lib.h> /* get types.h for libefl.h */
 #include <xen/libelf.h>
@@ -20,6 +21,7 @@
 #include <public/hvm/params.h>
 
 #include <asm/bootinfo.h>
+#include <asm/bzimage.h>
 #include <asm/cpu-policy.h>
 #include <asm/dom0_build.h>
 #include <asm/domain-builder.h>
@@ -247,6 +249,28 @@ static size_t __init domain_cmdline_size(const struct boot_info *bi,
     return s;
 }
 
+/* Xen x86 supports two kernel types that requires headroom:
+ *  - Linux bzImage
+ *  - Linux vmlinuz aka vmlinux.gz
+ */
+void __init arch_builder_headroom(struct boot_domain *bd)
+{
+    void *image = bootstrap_map_bm(bd->kernel);
+    unsigned long length = bd->kernel->size;
+
+    if ( bzimage_check(image, length) )
+        bd->kernel->headroom = bzimage_headroom(image, length);
+    else if ( gzip_check(image, length) )
+    {
+        unsigned long headroom = gzip_isize(image, length);
+        bd->kernel->headroom = gzip_headroom(headroom);
+    }
+    else
+        bd->kernel->headroom = 0;
+
+    bootstrap_unmap();
+}
+
 struct domain *__init arch_create_dom(
     struct boot_info *bi, struct boot_domain *bd)
 {
diff --git a/xen/arch/x86/include/asm/bzimage.h b/xen/arch/x86/include/asm/bzimage.h
index 7ed69d39103d..c3a042f177ac 100644
--- a/xen/arch/x86/include/asm/bzimage.h
+++ b/xen/arch/x86/include/asm/bzimage.h
@@ -3,6 +3,7 @@
 
 #include <xen/init.h>
 
+int bzimage_check(void *image, unsigned long len);
 unsigned long bzimage_headroom(void *image_start, unsigned long image_length);
 
 int bzimage_parse(void *image_base, void **image_start,
diff --git a/xen/arch/x86/include/asm/domain-builder.h b/xen/arch/x86/include/asm/domain-builder.h
index 6ef4776faf9a..f1749d2786c6 100644
--- a/xen/arch/x86/include/asm/domain-builder.h
+++ b/xen/arch/x86/include/asm/domain-builder.h
@@ -20,6 +20,8 @@ int hvm_alloc_xenstore_page(struct boot_domain *bd, xen_pfn_t *xs_gfn);
 unsigned long dom_paging_pages(
     const struct boot_domain *d, unsigned long nr_pages);
 
+void arch_builder_headroom(struct boot_domain *bd);
+
 unsigned long dom_compute_nr_pages(
     struct boot_domain *bd, struct elf_dom_parms *parms);
 
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index b2c7846be18f..36e6ba11ddcd 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -38,6 +38,7 @@
 #include <asm/bzimage.h>
 #include <asm/cpu-policy.h>
 #include <asm/desc.h>
+#include <asm/domain-builder.h>
 #include <asm/e820.h>
 #include <asm/edd.h>
 #include <asm/genapic.h>
@@ -1330,10 +1331,7 @@ void asmlinkage __init noreturn __start_xen(void)
         xen->size  = __2M_rwdata_end - _stext;
     }
 
-    bi->domains[0].kernel->headroom =
-        bzimage_headroom(bootstrap_map_bm(bi->domains[0].kernel),
-                         bi->domains[0].kernel->size);
-    bootstrap_unmap();
+    arch_builder_headroom(&bi->domains[0]);
 
 #ifndef highmem_start
     /* Don't allow split below 4Gb. */
diff --git a/xen/include/xen/gunzip.h b/xen/include/xen/gunzip.h
index 1b45350c195f..c8a0d59eef9d 100644
--- a/xen/include/xen/gunzip.h
+++ b/xen/include/xen/gunzip.h
@@ -1,10 +1,19 @@
 #ifndef __XEN_GUNZIP_H
 #define __XEN_GUNZIP_H
 
+#include <xen/macros.h>
+#include <xen/page-size.h>
 #include <xen/types.h>
 
 int gzip_check(char *image, unsigned long image_len);
 uint32_t gzip_isize(char *image, unsigned long image_len);
 int perform_gunzip(char *output, char *image, unsigned long image_len);
 
+static inline unsigned long gzip_headroom(unsigned long headroom)
+{
+    headroom += headroom >> 12; /* Add 8 bytes for every 32K input block */
+    headroom += (32768 + 18); /* Add 32K + 18 bytes of extra headroom */
+
+    return ROUNDUP(headroom, PAGE_SIZE);
+}
 #endif
-- 
2.30.2



  parent reply	other threads:[~2025-05-15 13:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-15 13:19 [RFCv2 30/38] x86/hyperlaunch: introduce concept of core domains Daniel P. Smith
2025-05-15 13:19 ` [RFCv2 31/38] common/gzip: add function to read isize field Daniel P. Smith
2025-05-15 13:19 ` Daniel P. Smith [this message]
2025-05-15 13:19 ` [RFCv2 33/38] x86/hyperlaunch: move kernel extraction under domain builder Daniel P. Smith
2025-05-15 13:19 ` [RFCv2 34/38] x86/hyperlaunch: introduce multidomain kconfig option Daniel P. Smith
2025-05-15 13:19 ` [RFCv2 35/38] x86/hyperlaunch: add multidomain construction logic Daniel P. Smith
2025-05-15 13:19 ` [RFCv2 36/38] x86/hyperlaunch: enable unpausing mulitple domains Daniel P. Smith
2025-05-15 13:19 ` [RFCv2 37/38] x86/hyperlaunch: generalize domid assignment Daniel P. Smith
2025-05-15 13:19 ` [RFCv2 38/38] tools: introduce hyperlaunch domain late init Daniel P. Smith

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250515131951.5594-3-dpsmith@apertussolutions.com \
    --to=dpsmith@apertussolutions.com \
    --cc=agarciav@amd.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=anthony.perard@vates.tech \
    --cc=jason.andryuk@amd.com \
    --cc=jbeulich@suse.com \
    --cc=julien@xen.org \
    --cc=michal.orzel@amd.com \
    --cc=roger.pau@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=stefano.stabellini@amd.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.