qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Janosch Frank <frankja@linux.ibm.com>
To: qemu-devel@nongnu.org
Cc: marcandre.lureau@redhat.com, pbonzini@redhat.com,
	mhartmay@linux.ibm.com,  borntraeger@linux.ibm.com,
	imbrenda@linux.ibm.com, pasic@linux.ibm.com, cohuck@redhat.com,
	thuth@redhat.com, qemu-s390x@nongnu.org, seiden@linux.ibm.com,
	scgl@linux.ibm.com
Subject: [PATCH v5 13/18] dump/dump: Add arch section support
Date: Thu, 11 Aug 2022 12:11:06 +0000	[thread overview]
Message-ID: <20220811121111.9878-14-frankja@linux.ibm.com> (raw)
In-Reply-To: <20220811121111.9878-1-frankja@linux.ibm.com>

Add hooks which architectures can use to add arbitrary data to custom
sections.

Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
---
 dump/dump.c                | 120 ++++++++++++++++++++++++++++++-------
 include/sysemu/dump-arch.h |   3 +
 2 files changed, 100 insertions(+), 23 deletions(-)

diff --git a/dump/dump.c b/dump/dump.c
index 0d6dbf453a..65b18fc602 100644
--- a/dump/dump.c
+++ b/dump/dump.c
@@ -430,7 +430,7 @@ static void prepare_elf_section_hdr_string(DumpState *s, void *buff)
     memcpy(buff, shdr, shdr_size);
 }
 
-static void prepare_elf_section_hdrs(DumpState *s)
+static void prepare_elf_section_hdrs(DumpState *s, Error **errp)
 {
     size_t len, sizeof_shdr;
     void *buff_hdr;
@@ -438,6 +438,7 @@ static void prepare_elf_section_hdrs(DumpState *s)
     /*
      * Section ordering:
      * - HDR zero
+     * - Arch section hdrs
      * - String table hdr
      */
     sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
@@ -465,6 +466,16 @@ static void prepare_elf_section_hdrs(DumpState *s)
         return;
     }
 
+    /* Add architecture defined section headers */
+    if (s->dump_info.arch_sections_write_hdr_fn) {
+        buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr);
+
+        if (s->shdr_num >= SHN_LORESERVE) {
+            error_setg_errno(errp, EINVAL, "dump: too many architecture defined sections");
+            return;
+        }
+    }
+
     /*
      * String table is the last section since strings are added via
      * arch_sections_write_hdr().
@@ -477,7 +488,10 @@ static void write_elf_section_headers(DumpState *s, Error **errp)
     size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
     int ret;
 
-    prepare_elf_section_hdrs(s);
+    prepare_elf_section_hdrs(s, errp);
+    if (*errp) {
+        return;
+    }
 
     ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
     if (ret < 0) {
@@ -485,6 +499,30 @@ static void write_elf_section_headers(DumpState *s, Error **errp)
     }
 }
 
+static void write_elf_sections(DumpState *s, Error **errp)
+{
+    int ret;
+
+    if (!s->elf_section_data_size) {
+        return;
+    }
+
+    /* Write architecture section data */
+    ret = fd_write_vmcore(s->elf_section_data,
+                          s->elf_section_data_size, s);
+    if (ret < 0) {
+        error_setg_errno(errp, -ret, "dump: failed to write architecture section  data");
+        return;
+    }
+
+    /* Write string table */
+    ret = fd_write_vmcore(s->string_table_buf->data,
+                          s->string_table_buf->len, s);
+    if (ret < 0) {
+        error_setg_errno(errp, -ret, "dump: failed to write string table data");
+    }
+}
+
 static void write_data(DumpState *s, void *buf, int length, Error **errp)
 {
     int ret;
@@ -744,6 +782,24 @@ static void dump_iterate(DumpState *s, Error **errp)
     }
 }
 
+static void dump_end(DumpState *s, Error **errp)
+{
+    ERRP_GUARD();
+
+    if (!s->elf_section_data_size) {
+        return;
+    }
+    s->elf_section_data = g_malloc0(s->elf_section_data_size);
+
+    /* Adds the architecture defined section data to s->elf_section_data  */
+    if (s->dump_info.arch_sections_write_fn) {
+        s->dump_info.arch_sections_write_fn(s, s->elf_section_data);
+    }
+
+    /* write sections to vmcore */
+    write_elf_sections(s, errp);
+}
+
 static void create_vmcore(DumpState *s, Error **errp)
 {
     ERRP_GUARD();
@@ -758,6 +814,9 @@ static void create_vmcore(DumpState *s, Error **errp)
     if (*errp) {
         return;
     }
+
+    /* Write the section data */
+    dump_end(s, errp);
 }
 
 static int write_start_flat_header(int fd)
@@ -1883,38 +1942,53 @@ static void dump_init(DumpState *s, int fd, bool has_format,
     }
 
     /*
-     * calculate phdr_num
-     *
-     * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
+     * Adds the number of architecture sections to shdr_num, sets
+     * string_table_usage and sets elf_section_data_size so we know
+     * the offsets and sizes of all parts.
      */
-    s->phdr_num = 1; /* PT_NOTE */
-    if (s->list.num < UINT16_MAX - 2) {
-        s->shdr_num = 0;
-        s->phdr_num += s->list.num;
-    } else {
-        /* sh_info of section 0 holds the real number of phdrs */
-        s->shdr_num = 1;
+    if (s->dump_info.arch_sections_add_fn) {
+        s->dump_info.arch_sections_add_fn(s);
 
-        /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
-        if (s->list.num <= UINT32_MAX - 1) {
-            s->phdr_num += s->list.num;
-        } else {
-            s->phdr_num = UINT32_MAX;
+        if (s->shdr_num) {
+            s->string_table_usage = true;
         }
     }
 
     /*
-     * calculate shdr_num and elf_section_data_size so we know the offsets and
-     * sizes of all parts.
+     * Calculate phdr_num
      *
-     * If phdr_num overflowed we have at least one section header
-     * More sections/hdrs can be added by the architectures
+     * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
+     * sh_info is 32 bit. There's special handling once we go over
+     * UINT16_MAX - 1 but that is handled in the ehdr and section
+     * code.
      */
-    if (s->shdr_num > 1) {
-        /* Reserve the string table */
+    s->phdr_num = 1; /* Reserve PT_NOTE */
+    if (s->list.num <= UINT32_MAX - 1) {
+        s->phdr_num += s->list.num;
+    } else {
+        s->phdr_num = UINT32_MAX;
+    }
+
+    /*
+     * The first section header is always a special one in which most
+     * fields are 0.
+     *
+     * We need it if we have custom sections and if phdr_num >=
+     * PN_XNUM so we can write the real phdr_num into sh_info.
+     */
+    if (s->shdr_num || s->phdr_num >= PN_XNUM) {
         s->shdr_num += 1;
     }
 
+    /* Reserve the string table for the arch sections if needed */
+    if (s->string_table_usage) {
+        s->shdr_num += 1;
+    }
+
+    /*
+     * Now that the number of section and program headers is known we
+     * can calculate the offsets of the headers and data.
+     */
     if (dump_is_64bit(s)) {
         s->shdr_offset = sizeof(Elf64_Ehdr);
         s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
diff --git a/include/sysemu/dump-arch.h b/include/sysemu/dump-arch.h
index e25b02e990..824c0c1152 100644
--- a/include/sysemu/dump-arch.h
+++ b/include/sysemu/dump-arch.h
@@ -21,6 +21,9 @@ typedef struct ArchDumpInfo {
     uint32_t page_size;      /* The target's page size. If it's variable and
                               * unknown, then this should be the maximum. */
     uint64_t phys_base;      /* The target's physmem base. */
+    void (*arch_sections_add_fn)(DumpState *s);
+    uint64_t (*arch_sections_write_hdr_fn)(DumpState *s, uint8_t *buff);
+    void (*arch_sections_write_fn)(DumpState *s, uint8_t *buff);
 } ArchDumpInfo;
 
 struct GuestPhysBlockList; /* memory_mapping.h */
-- 
2.34.1



  parent reply	other threads:[~2022-08-11 12:32 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-11 12:10 [PATCH v5 00/18] dump: Add arch section and s390x PV dump Janosch Frank
2022-08-11 12:10 ` [PATCH v5 01/18] dump: Replace opaque DumpState pointer with a typed one Janosch Frank
2022-08-11 16:51   ` Daniel Henrique Barboza
2022-08-16  7:58   ` Marc-André Lureau
2022-08-11 12:10 ` [PATCH v5 02/18] dump: Rename write_elf_loads to write_elf_phdr_loads Janosch Frank
2022-08-11 12:10 ` [PATCH v5 03/18] dump: Refactor dump_iterate and introduce dump_filter_memblock_*() Janosch Frank
2022-08-16  8:12   ` Marc-André Lureau
2022-08-11 12:10 ` [PATCH v5 04/18] dump: Rework get_start_block Janosch Frank
2022-08-29 20:17   ` Janis Schoetterl-Glausch
2022-09-26 14:48     ` Janosch Frank
2022-08-11 12:10 ` [PATCH v5 05/18] dump: Rework filter area variables Janosch Frank
2022-08-16  8:19   ` Marc-André Lureau
2022-08-11 12:10 ` [PATCH v5 06/18] dump: Rework dump_calculate_size function Janosch Frank
2022-09-01  9:24   ` Janis Schoetterl-Glausch
2022-08-11 12:11 ` [PATCH v5 07/18] dump: Split elf header functions into prepare and write Janosch Frank
2022-08-16  8:26   ` Marc-André Lureau
2022-09-01  9:24   ` Janis Schoetterl-Glausch
2022-08-11 12:11 ` [PATCH v5 08/18] dump: Rename write_elf*_phdr_note to prepare_elf*_phdr_note Janosch Frank
2022-08-16  8:28   ` Marc-André Lureau
2022-09-01  9:24   ` Janis Schoetterl-Glausch
2022-08-11 12:11 ` [PATCH v5 09/18] dump: Use a buffer for ELF section data and headers Janosch Frank
2022-08-16  8:43   ` Marc-André Lureau
2022-08-29 20:43   ` Janis Schoetterl-Glausch
2022-08-11 12:11 ` [PATCH v5 10/18] dump: Reorder struct DumpState Janosch Frank
2022-08-11 12:11 ` [PATCH v5 11/18] dump: Swap segment and section header locations Janosch Frank
2022-08-11 12:11 ` [PATCH v5 12/18] dump/dump: Add section string table support Janosch Frank
2022-08-30 11:35   ` Steffen Eiden
2022-08-30 14:02     ` Janosch Frank
2022-09-01  9:25   ` Janis Schoetterl-Glausch
2022-08-11 12:11 ` Janosch Frank [this message]
2022-09-01  9:26   ` [PATCH v5 13/18] dump/dump: Add arch section support Janis Schoetterl-Glausch
2022-08-11 12:11 ` [PATCH v5 14/18] DRAFT: linux header sync Janosch Frank
2022-08-11 12:11 ` [PATCH v5 15/18] s390x: Add protected dump cap Janosch Frank
2022-08-29 11:29   ` Thomas Huth
2022-09-01  9:26   ` Janis Schoetterl-Glausch
2022-08-11 12:11 ` [PATCH v5 16/18] s390x: Introduce PV query interface Janosch Frank
2022-08-29 11:30   ` Thomas Huth
2022-09-01  9:26   ` Janis Schoetterl-Glausch
2022-08-11 12:11 ` [PATCH v5 17/18] s390x: Add KVM PV dump interface Janosch Frank
2022-08-23 15:25   ` Steffen Eiden
2022-09-01  9:26   ` Janis Schoetterl-Glausch
2022-08-11 12:11 ` [PATCH v5 18/18] s390x: pv: Add dump support Janosch Frank
2022-08-11 13:03   ` Janosch Frank
2022-08-23 15:26   ` Steffen Eiden
2022-08-29 11:57   ` Thomas Huth
2022-09-01  9:31   ` Janis Schoetterl-Glausch

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=20220811121111.9878-14-frankja@linux.ibm.com \
    --to=frankja@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=cohuck@redhat.com \
    --cc=imbrenda@linux.ibm.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mhartmay@linux.ibm.com \
    --cc=pasic@linux.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    --cc=scgl@linux.ibm.com \
    --cc=seiden@linux.ibm.com \
    --cc=thuth@redhat.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).