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>
Subject: [RFCv2 33/38] x86/hyperlaunch: move kernel extraction under domain builder
Date: Thu, 15 May 2025 09:19:45 -0400	[thread overview]
Message-ID: <20250515131951.5594-4-dpsmith@apertussolutions.com> (raw)
In-Reply-To: <20250515131951.5594-1-dpsmith@apertussolutions.com>

The function bzimage_parse attempted to prepare the kernel image for copying
into the guest 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 kernel preparation for the three kernel types
to the domain builder. It descopes bziamge_parse to only handling bzImages.

Signed-off-by: Daniel P. Smith <dpsmith@apertussolutions.com>
---
 xen/arch/x86/bzimage.c                    | 34 ++++++++++-------------
 xen/arch/x86/domain-builder/domain.c      | 30 ++++++++++++++++++++
 xen/arch/x86/hvm/dom_build.c              |  3 +-
 xen/arch/x86/include/asm/bzimage.h        |  5 ++--
 xen/arch/x86/include/asm/domain-builder.h |  3 ++
 xen/arch/x86/pv/dom0_build.c              |  4 ++-
 6 files changed, 55 insertions(+), 24 deletions(-)

diff --git a/xen/arch/x86/bzimage.c b/xen/arch/x86/bzimage.c
index ba090b3ef9d6..65ee0eef029a 100644
--- a/xen/arch/x86/bzimage.c
+++ b/xen/arch/x86/bzimage.c
@@ -66,8 +66,6 @@ int __init bzimage_check(void *image, unsigned long len)
     return 1;
 }
 
-static unsigned long __initdata orig_image_len;
-
 unsigned long __init bzimage_headroom(void *image_start,
                                       unsigned long image_length)
 {
@@ -77,7 +75,6 @@ unsigned long __init bzimage_headroom(void *image_start,
     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);
     /*
@@ -96,31 +93,28 @@ unsigned long __init bzimage_headroom(void *image_start,
     return ROUNDUP(headroom, PAGE_SIZE);
 }
 
-int __init bzimage_parse(void *image_base, void **image_start,
-                         unsigned long *image_len)
+int __init bzimage_parse(
+    void *image_base, void **image_start, unsigned long headroom,
+    unsigned long *image_len)
 {
     struct setup_header *hdr = (struct setup_header *)(*image_start);
-    int err = bzimage_check(hdr, *image_len);
+    int err = 0;
     unsigned long output_len;
 
-    if ( err < 0 )
-        return err;
-
-    if ( err > 0 )
-    {
-        *image_start += (hdr->setup_sects + 1) * 512 + hdr->payload_offset;
-        *image_len = hdr->payload_length;
-    }
+    *image_start += (hdr->setup_sects + 1) * 512 + hdr->payload_offset;
+    *image_len = hdr->payload_length;
 
     if ( elf_is_elfbinary(*image_start, *image_len) )
-        return 0;
+        return err;
 
-    BUG_ON(!(image_base < *image_start));
+    output_len = gzip_isize(*image_start, *image_len);
 
-    output_len = gzip_isize(*image_start, orig_image_len);
+    BUG_ON(!(image_base < *image_start));
 
-    if ( (err = perform_gunzip(image_base, *image_start, orig_image_len)) > 0 )
-        err = decompress(*image_start, orig_image_len, image_base);
+    if ( gzip_check(*image_start, *image_len) )
+        err = perform_gunzip(image_base, *image_start, *image_len);
+    else
+        err = decompress(*image_start, *image_len, image_base);
 
     if ( !err )
     {
@@ -128,7 +122,7 @@ int __init bzimage_parse(void *image_base, void **image_start,
         *image_len = output_len;
     }
 
-    return err > 0 ? 0 : err;
+    return err;
 }
 
 /*
diff --git a/xen/arch/x86/domain-builder/domain.c b/xen/arch/x86/domain-builder/domain.c
index 9c2f30eee8bd..deff6c8efaf1 100644
--- a/xen/arch/x86/domain-builder/domain.c
+++ b/xen/arch/x86/domain-builder/domain.c
@@ -271,6 +271,36 @@ void __init arch_builder_headroom(struct boot_domain *bd)
     bootstrap_unmap();
 }
 
+int __init arch_builder_prepare_kernel(
+    void *image_base, void **image_start, unsigned long headroom,
+    unsigned long *image_len)
+{
+    int rc = 0;
+
+    *image_start = image_base + headroom;
+    *image_len -= headroom;
+
+    if ( bzimage_check(*image_start, *image_len) )
+        rc = bzimage_parse(image_base, image_start, headroom, image_len);
+    else if ( gzip_check(*image_start, *image_len) )
+    {
+        unsigned long len = gzip_isize(*image_start, *image_len);
+        rc = perform_gunzip(image_base, *image_start, *image_len);
+        if ( !rc )
+        {
+            *image_start = image_base;
+            *image_len = len;
+        }
+    }
+    else if ( elf_is_elfbinary(*image_start, *image_len) )
+        rc = 0;
+    else
+        rc = -EINVAL;
+
+    return rc;
+}
+
+
 struct domain *__init arch_create_dom(
     struct boot_info *bi, struct boot_domain *bd)
 {
diff --git a/xen/arch/x86/hvm/dom_build.c b/xen/arch/x86/hvm/dom_build.c
index 3eab97b5288b..170caac6716e 100644
--- a/xen/arch/x86/hvm/dom_build.c
+++ b/xen/arch/x86/hvm/dom_build.c
@@ -745,7 +745,8 @@ static int __init pvh_load_kernel(
     struct vcpu *v = d->vcpu[0];
     int rc;
 
-    if ( (rc = bzimage_parse(image_base, &image_start, &image_len)) != 0 )
+    if ( (rc = arch_builder_prepare_kernel(image_base, &image_start,
+                                           image->headroom, &image_len)) != 0 )
     {
         printk("Error trying to detect bz compressed kernel\n");
         return rc;
diff --git a/xen/arch/x86/include/asm/bzimage.h b/xen/arch/x86/include/asm/bzimage.h
index c3a042f177ac..ebe4f9325c4f 100644
--- a/xen/arch/x86/include/asm/bzimage.h
+++ b/xen/arch/x86/include/asm/bzimage.h
@@ -6,7 +6,8 @@
 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,
-                  unsigned long *image_len);
+int bzimage_parse(
+    void *image_base, void **image_start, unsigned long headroom,
+    unsigned long *image_len);
 
 #endif /* __X86_BZIMAGE_H__ */
diff --git a/xen/arch/x86/include/asm/domain-builder.h b/xen/arch/x86/include/asm/domain-builder.h
index f1749d2786c6..3adeadc15423 100644
--- a/xen/arch/x86/include/asm/domain-builder.h
+++ b/xen/arch/x86/include/asm/domain-builder.h
@@ -21,6 +21,9 @@ unsigned long dom_paging_pages(
     const struct boot_domain *d, unsigned long nr_pages);
 
 void arch_builder_headroom(struct boot_domain *bd);
+int arch_builder_prepare_kernel(
+    void *image_base, void **image_start, unsigned long headroom,
+    unsigned long *image_len);
 
 unsigned long dom_compute_nr_pages(
     struct boot_domain *bd, struct elf_dom_parms *parms);
diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c
index 21cb0b11748e..72319c31fa0e 100644
--- a/xen/arch/x86/pv/dom0_build.c
+++ b/xen/arch/x86/pv/dom0_build.c
@@ -428,7 +428,9 @@ static int __init dom0_construct(struct boot_domain *bd)
 
     d->max_pages = ~0U;
 
-    if ( (rc = bzimage_parse(image_base, &image_start, &image_len)) != 0 )
+    if ( (rc = arch_builder_prepare_kernel(image_base, &image_start,
+                                           bd->kernel->headroom,
+                                           &image_len)) != 0 )
         return rc;
 
     if ( (rc = elf_init(&elf, image_start, image_len)) != 0 )
-- 
2.30.2



  parent reply	other threads:[~2025-05-15 13:38 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 ` [RFCv2 32/38] x86/hyperlaunch: move headroom under domain builder Daniel P. Smith
2025-05-15 13:19 ` Daniel P. Smith [this message]
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-4-dpsmith@apertussolutions.com \
    --to=dpsmith@apertussolutions.com \
    --cc=agarciav@amd.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jason.andryuk@amd.com \
    --cc=jbeulich@suse.com \
    --cc=roger.pau@citrix.com \
    --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.