thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-dump-Add-architecture-section-and-section-string-tab.patch

62547e
From f2f3efff83dddd38a97699cd2701f46f61a732e3 Mon Sep 17 00:00:00 2001
62547e
From: Janosch Frank <frankja@linux.ibm.com>
62547e
Date: Mon, 17 Oct 2022 11:32:10 +0000
62547e
Subject: [PATCH 36/42] dump: Add architecture section and section string table
62547e
 support
62547e
MIME-Version: 1.0
62547e
Content-Type: text/plain; charset=UTF-8
62547e
Content-Transfer-Encoding: 8bit
62547e
62547e
RH-Author: Cédric Le Goater <clg@redhat.com>
62547e
RH-MergeRequest: 226: s390: Enhanced Interpretation for PCI Functions and Secure Execution guest dump
62547e
RH-Bugzilla: 1664378 2043909
62547e
RH-Acked-by: Thomas Huth <thuth@redhat.com>
62547e
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
62547e
RH-Acked-by: Jon Maloy <jmaloy@redhat.com>
62547e
RH-Commit: [36/41] 83b98ff185e93e62703f686b65546d60c783d783
62547e
62547e
Add hooks which architectures can use to add arbitrary data to custom
62547e
sections.
62547e
62547e
Also add a section name string table in order to identify section
62547e
contents
62547e
62547e
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
62547e
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
62547e
Message-Id: <20221017113210.41674-1-frankja@linux.ibm.com>
62547e
(cherry picked from commit 9b72224f44612ddd5b434a1bccf79346946d11da)
62547e
Signed-off-by: Cédric Le Goater <clg@redhat.com>
62547e
---
62547e
 dump/dump.c                | 186 +++++++++++++++++++++++++++++++------
62547e
 include/sysemu/dump-arch.h |   3 +
62547e
 include/sysemu/dump.h      |   3 +
62547e
 3 files changed, 166 insertions(+), 26 deletions(-)
62547e
62547e
diff --git a/dump/dump.c b/dump/dump.c
62547e
index 7a42401790..4aa8fb64d2 100644
62547e
--- a/dump/dump.c
62547e
+++ b/dump/dump.c
62547e
@@ -104,6 +104,7 @@ static int dump_cleanup(DumpState *s)
62547e
     memory_mapping_list_free(&s->list);
62547e
     close(s->fd);
62547e
     g_free(s->guest_note);
62547e
+    g_array_unref(s->string_table_buf);
62547e
     s->guest_note = NULL;
62547e
     if (s->resume) {
62547e
         if (s->detached) {
62547e
@@ -153,11 +154,10 @@ static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
62547e
     elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
62547e
     elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
62547e
     elf_header->e_phnum = cpu_to_dump16(s, phnum);
62547e
-    if (s->shdr_num) {
62547e
-        elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
62547e
-        elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
62547e
-        elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
62547e
-    }
62547e
+    elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
62547e
+    elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
62547e
+    elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
62547e
+    elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
62547e
 }
62547e
 
62547e
 static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
62547e
@@ -181,11 +181,10 @@ static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
62547e
     elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
62547e
     elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
62547e
     elf_header->e_phnum = cpu_to_dump16(s, phnum);
62547e
-    if (s->shdr_num) {
62547e
-        elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
62547e
-        elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
62547e
-        elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
62547e
-    }
62547e
+    elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
62547e
+    elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
62547e
+    elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
62547e
+    elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
62547e
 }
62547e
 
62547e
 static void write_elf_header(DumpState *s, Error **errp)
62547e
@@ -196,6 +195,8 @@ static void write_elf_header(DumpState *s, Error **errp)
62547e
     void *header_ptr;
62547e
     int ret;
62547e
 
62547e
+    /* The NULL header and the shstrtab are always defined */
62547e
+    assert(s->shdr_num >= 2);
62547e
     if (dump_is_64bit(s)) {
62547e
         prepare_elf64_header(s, &elf64_header);
62547e
         header_size = sizeof(elf64_header);
62547e
@@ -394,17 +395,49 @@ static void prepare_elf_section_hdr_zero(DumpState *s)
62547e
     }
62547e
 }
62547e
 
62547e
-static void prepare_elf_section_hdrs(DumpState *s)
62547e
+static void prepare_elf_section_hdr_string(DumpState *s, void *buff)
62547e
+{
62547e
+    uint64_t index = s->string_table_buf->len;
62547e
+    const char strtab[] = ".shstrtab";
62547e
+    Elf32_Shdr shdr32 = {};
62547e
+    Elf64_Shdr shdr64 = {};
62547e
+    int shdr_size;
62547e
+    void *shdr;
62547e
+
62547e
+    g_array_append_vals(s->string_table_buf, strtab, sizeof(strtab));
62547e
+    if (dump_is_64bit(s)) {
62547e
+        shdr_size = sizeof(Elf64_Shdr);
62547e
+        shdr64.sh_type = SHT_STRTAB;
62547e
+        shdr64.sh_offset = s->section_offset + s->elf_section_data_size;
62547e
+        shdr64.sh_name = index;
62547e
+        shdr64.sh_size = s->string_table_buf->len;
62547e
+        shdr = &shdr64;
62547e
+    } else {
62547e
+        shdr_size = sizeof(Elf32_Shdr);
62547e
+        shdr32.sh_type = SHT_STRTAB;
62547e
+        shdr32.sh_offset = s->section_offset + s->elf_section_data_size;
62547e
+        shdr32.sh_name = index;
62547e
+        shdr32.sh_size = s->string_table_buf->len;
62547e
+        shdr = &shdr32;
62547e
+    }
62547e
+    memcpy(buff, shdr, shdr_size);
62547e
+}
62547e
+
62547e
+static bool prepare_elf_section_hdrs(DumpState *s, Error **errp)
62547e
 {
62547e
     size_t len, sizeof_shdr;
62547e
+    void *buff_hdr;
62547e
 
62547e
     /*
62547e
      * Section ordering:
62547e
      * - HDR zero
62547e
+     * - Arch section hdrs
62547e
+     * - String table hdr
62547e
      */
62547e
     sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
62547e
     len = sizeof_shdr * s->shdr_num;
62547e
     s->elf_section_hdrs = g_malloc0(len);
62547e
+    buff_hdr = s->elf_section_hdrs;
62547e
 
62547e
     /*
62547e
      * The first section header is ALWAYS a special initial section
62547e
@@ -420,6 +453,26 @@ static void prepare_elf_section_hdrs(DumpState *s)
62547e
     if (s->phdr_num >= PN_XNUM) {
62547e
         prepare_elf_section_hdr_zero(s);
62547e
     }
62547e
+    buff_hdr += sizeof_shdr;
62547e
+
62547e
+    /* Add architecture defined section headers */
62547e
+    if (s->dump_info.arch_sections_write_hdr_fn
62547e
+        && s->shdr_num > 2) {
62547e
+        buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr);
62547e
+
62547e
+        if (s->shdr_num >= SHN_LORESERVE) {
62547e
+            error_setg_errno(errp, EINVAL,
62547e
+                             "dump: too many architecture defined sections");
62547e
+            return false;
62547e
+        }
62547e
+    }
62547e
+
62547e
+    /*
62547e
+     * String table is the last section since strings are added via
62547e
+     * arch_sections_write_hdr().
62547e
+     */
62547e
+    prepare_elf_section_hdr_string(s, buff_hdr);
62547e
+    return true;
62547e
 }
62547e
 
62547e
 static void write_elf_section_headers(DumpState *s, Error **errp)
62547e
@@ -427,7 +480,9 @@ static void write_elf_section_headers(DumpState *s, Error **errp)
62547e
     size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
62547e
     int ret;
62547e
 
62547e
-    prepare_elf_section_hdrs(s);
62547e
+    if (!prepare_elf_section_hdrs(s, errp)) {
62547e
+        return;
62547e
+    }
62547e
 
62547e
     ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
62547e
     if (ret < 0) {
62547e
@@ -437,6 +492,29 @@ static void write_elf_section_headers(DumpState *s, Error **errp)
62547e
     g_free(s->elf_section_hdrs);
62547e
 }
62547e
 
62547e
+static void write_elf_sections(DumpState *s, Error **errp)
62547e
+{
62547e
+    int ret;
62547e
+
62547e
+    if (s->elf_section_data_size) {
62547e
+        /* Write architecture section data */
62547e
+        ret = fd_write_vmcore(s->elf_section_data,
62547e
+                              s->elf_section_data_size, s);
62547e
+        if (ret < 0) {
62547e
+            error_setg_errno(errp, -ret,
62547e
+                             "dump: failed to write architecture section data");
62547e
+            return;
62547e
+        }
62547e
+    }
62547e
+
62547e
+    /* Write string table */
62547e
+    ret = fd_write_vmcore(s->string_table_buf->data,
62547e
+                          s->string_table_buf->len, s);
62547e
+    if (ret < 0) {
62547e
+        error_setg_errno(errp, -ret, "dump: failed to write string table data");
62547e
+    }
62547e
+}
62547e
+
62547e
 static void write_data(DumpState *s, void *buf, int length, Error **errp)
62547e
 {
62547e
     int ret;
62547e
@@ -693,6 +771,31 @@ static void dump_iterate(DumpState *s, Error **errp)
62547e
     }
62547e
 }
62547e
 
62547e
+static void dump_end(DumpState *s, Error **errp)
62547e
+{
62547e
+    int rc;
62547e
+    ERRP_GUARD();
62547e
+
62547e
+    if (s->elf_section_data_size) {
62547e
+        s->elf_section_data = g_malloc0(s->elf_section_data_size);
62547e
+    }
62547e
+
62547e
+    /* Adds the architecture defined section data to s->elf_section_data  */
62547e
+    if (s->dump_info.arch_sections_write_fn &&
62547e
+        s->elf_section_data_size) {
62547e
+        rc = s->dump_info.arch_sections_write_fn(s, s->elf_section_data);
62547e
+        if (rc) {
62547e
+            error_setg_errno(errp, rc,
62547e
+                             "dump: failed to get arch section data");
62547e
+            g_free(s->elf_section_data);
62547e
+            return;
62547e
+        }
62547e
+    }
62547e
+
62547e
+    /* write sections to vmcore */
62547e
+    write_elf_sections(s, errp);
62547e
+}
62547e
+
62547e
 static void create_vmcore(DumpState *s, Error **errp)
62547e
 {
62547e
     ERRP_GUARD();
62547e
@@ -702,7 +805,14 @@ static void create_vmcore(DumpState *s, Error **errp)
62547e
         return;
62547e
     }
62547e
 
62547e
+    /* Iterate over memory and dump it to file */
62547e
     dump_iterate(s, errp);
62547e
+    if (*errp) {
62547e
+        return;
62547e
+    }
62547e
+
62547e
+    /* Write the section data */
62547e
+    dump_end(s, errp);
62547e
 }
62547e
 
62547e
 static int write_start_flat_header(int fd)
62547e
@@ -1720,6 +1830,14 @@ static void dump_init(DumpState *s, int fd, bool has_format,
62547e
     s->filter_area_begin = begin;
62547e
     s->filter_area_length = length;
62547e
 
62547e
+    /* First index is 0, it's the special null name */
62547e
+    s->string_table_buf = g_array_new(FALSE, TRUE, 1);
62547e
+    /*
62547e
+     * Allocate the null name, due to the clearing option set to true
62547e
+     * it will be 0.
62547e
+     */
62547e
+    g_array_set_size(s->string_table_buf, 1);
62547e
+
62547e
     memory_mapping_list_init(&s->list);
62547e
 
62547e
     guest_phys_blocks_init(&s->guest_phys_blocks);
62547e
@@ -1856,26 +1974,42 @@ static void dump_init(DumpState *s, int fd, bool has_format,
62547e
     }
62547e
 
62547e
     /*
62547e
-     * calculate phdr_num
62547e
+     * The first section header is always a special one in which most
62547e
+     * fields are 0. The section header string table is also always
62547e
+     * set.
62547e
+     */
62547e
+    s->shdr_num = 2;
62547e
+
62547e
+    /*
62547e
+     * Adds the number of architecture sections to shdr_num and sets
62547e
+     * elf_section_data_size so we know the offsets and sizes of all
62547e
+     * parts.
62547e
+     */
62547e
+    if (s->dump_info.arch_sections_add_fn) {
62547e
+        s->dump_info.arch_sections_add_fn(s);
62547e
+    }
62547e
+
62547e
+    /*
62547e
+     * calculate shdr_num so we know the offsets and sizes of all
62547e
+     * parts.
62547e
+     * Calculate phdr_num
62547e
      *
62547e
-     * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
62547e
+     * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
62547e
+     * sh_info is 32 bit. There's special handling once we go over
62547e
+     * UINT16_MAX - 1 but that is handled in the ehdr and section
62547e
+     * code.
62547e
      */
62547e
-    s->phdr_num = 1; /* PT_NOTE */
62547e
-    if (s->list.num < UINT16_MAX - 2) {
62547e
-        s->shdr_num = 0;
62547e
+    s->phdr_num = 1; /* Reserve PT_NOTE */
62547e
+    if (s->list.num <= UINT32_MAX - 1) {
62547e
         s->phdr_num += s->list.num;
62547e
     } else {
62547e
-        /* sh_info of section 0 holds the real number of phdrs */
62547e
-        s->shdr_num = 1;
62547e
-
62547e
-        /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
62547e
-        if (s->list.num <= UINT32_MAX - 1) {
62547e
-            s->phdr_num += s->list.num;
62547e
-        } else {
62547e
-            s->phdr_num = UINT32_MAX;
62547e
-        }
62547e
+        s->phdr_num = UINT32_MAX;
62547e
     }
62547e
 
62547e
+    /*
62547e
+     * Now that the number of section and program headers is known we
62547e
+     * can calculate the offsets of the headers and data.
62547e
+     */
62547e
     if (dump_is_64bit(s)) {
62547e
         s->shdr_offset = sizeof(Elf64_Ehdr);
62547e
         s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
62547e
diff --git a/include/sysemu/dump-arch.h b/include/sysemu/dump-arch.h
62547e
index e25b02e990..59bbc9be38 100644
62547e
--- a/include/sysemu/dump-arch.h
62547e
+++ b/include/sysemu/dump-arch.h
62547e
@@ -21,6 +21,9 @@ typedef struct ArchDumpInfo {
62547e
     uint32_t page_size;      /* The target's page size. If it's variable and
62547e
                               * unknown, then this should be the maximum. */
62547e
     uint64_t phys_base;      /* The target's physmem base. */
62547e
+    void (*arch_sections_add_fn)(DumpState *s);
62547e
+    uint64_t (*arch_sections_write_hdr_fn)(DumpState *s, uint8_t *buff);
62547e
+    int (*arch_sections_write_fn)(DumpState *s, uint8_t *buff);
62547e
 } ArchDumpInfo;
62547e
 
62547e
 struct GuestPhysBlockList; /* memory_mapping.h */
62547e
diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
62547e
index 9ed811b313..38ccac7190 100644
62547e
--- a/include/sysemu/dump.h
62547e
+++ b/include/sysemu/dump.h
62547e
@@ -180,6 +180,9 @@ typedef struct DumpState {
62547e
     hwaddr note_offset;
62547e
 
62547e
     void *elf_section_hdrs;     /* Pointer to section header buffer */
62547e
+    void *elf_section_data;     /* Pointer to section data buffer */
62547e
+    uint64_t elf_section_data_size; /* Size of section data */
62547e
+    GArray *string_table_buf;   /* String table data buffer */
62547e
 
62547e
     uint8_t *note_buf;          /* buffer for notes */
62547e
     size_t note_buf_offset;     /* the writing place in note_buf */
62547e
-- 
62547e
2.37.3
62547e