thebeanogamer / rpms / qemu-kvm

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