All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xenproject.org
Cc: Juergen Gross <jgross@suse.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	George Dunlap <george.dunlap@citrix.com>,
	Jan Beulich <jbeulich@suse.com>, Julien Grall <julien@xen.org>,
	Stefano Stabellini <sstabellini@kernel.org>, Wei Liu <wl@xen.org>
Subject: [PATCH] xen: consider alloc-only segments when loading PV dom0 kernel
Date: Thu, 23 Jun 2022 10:02:08 +0200	[thread overview]
Message-ID: <20220623080208.2214-1-jgross@suse.com> (raw)

When loading the dom0 kernel for PV mode, the first free usable memory
location after the kernel needs to take segments into account, which
have only the ALLOC flag set, but are not specified to be loaded in
the program headers of the ELF file.

This is e.g. a problem for Linux kernels from 5.19 onwards, as those
can have a final NOLOAD section at the end, which must not be used by
e.g. the start_info structure or the initial page tables allocated by
the hypervisor.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 xen/common/libelf/libelf-loader.c | 33 +++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/xen/common/libelf/libelf-loader.c b/xen/common/libelf/libelf-loader.c
index 629cc0d3e6..4b0e3ced55 100644
--- a/xen/common/libelf/libelf-loader.c
+++ b/xen/common/libelf/libelf-loader.c
@@ -467,7 +467,9 @@ do {                                                                \
 void elf_parse_binary(struct elf_binary *elf)
 {
     ELF_HANDLE_DECL(elf_phdr) phdr;
+    ELF_HANDLE_DECL(elf_shdr) shdr;
     uint64_t low = -1, high = 0, paddr, memsz;
+    uint64_t vlow = -1, vhigh = 0, vaddr, voff;
     unsigned i, count;
 
     count = elf_phdr_count(elf);
@@ -480,6 +482,7 @@ void elf_parse_binary(struct elf_binary *elf)
         if ( !elf_phdr_is_loadable(elf, phdr) )
             continue;
         paddr = elf_uval(elf, phdr, p_paddr);
+        vaddr = elf_uval(elf, phdr, p_vaddr);
         memsz = elf_uval(elf, phdr, p_memsz);
         elf_msg(elf, "ELF: phdr: paddr=%#" PRIx64 " memsz=%#" PRIx64 "\n",
                 paddr, memsz);
@@ -487,7 +490,37 @@ void elf_parse_binary(struct elf_binary *elf)
             low = paddr;
         if ( high < paddr + memsz )
             high = paddr + memsz;
+        if ( vlow > vaddr )
+            vlow = vaddr;
+        if ( vhigh < vaddr + memsz )
+            vhigh = vaddr + memsz;
     }
+
+    voff = vhigh - high;
+
+    count = elf_shdr_count(elf);
+    for ( i = 0; i < count; i++ )
+    {
+        shdr = elf_shdr_by_index(elf, i);
+        if ( !elf_access_ok(elf, ELF_HANDLE_PTRVAL(shdr), 1) )
+            /* input has an insane section header count field */
+            break;
+        if ( !(elf_uval(elf, shdr, sh_flags) & SHF_ALLOC) )
+            continue;
+        vaddr = elf_uval(elf, shdr, sh_addr);
+        memsz = elf_uval(elf, shdr, sh_size);
+        if ( vlow > vaddr )
+        {
+            vlow = vaddr;
+            low = vaddr - voff;
+        }
+        if ( vhigh < vaddr + memsz )
+        {
+            vhigh = vaddr + memsz;
+            high = vaddr + memsz - voff;
+        }
+    }
+
     elf->pstart = low;
     elf->pend = high;
     elf_msg(elf, "ELF: memory: %#" PRIx64 " -> %#" PRIx64 "\n",
-- 
2.35.3



             reply	other threads:[~2022-06-23  8:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-23  8:02 Juergen Gross [this message]
2022-06-23  9:04 ` [PATCH] xen: consider alloc-only segments when loading PV dom0 kernel Jan Beulich
2022-06-23  9:08   ` Juergen Gross
2022-06-23  9:14     ` Jan Beulich

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=20220623080208.2214-1-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=george.dunlap@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=julien@xen.org \
    --cc=sstabellini@kernel.org \
    --cc=wl@xen.org \
    --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.