62547e
From 411f5354b809f6b783946e58d7655135814fb809 Mon Sep 17 00:00:00 2001
62547e
From: Janosch Frank <frankja@linux.ibm.com>
62547e
Date: Thu, 11 Aug 2022 12:10:58 +0000
62547e
Subject: [PATCH 26/42] dump: Rework filter area variables
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: [26/41] f10a5523dfd2724f7a8637fca3ed68ba6df659a5
62547e
62547e
While the DumpState begin and length variables directly mirror the API
62547e
variable names they are not very descriptive. So let's add a
62547e
"filter_area_" prefix and make has_filter a function checking length > 0.
62547e
62547e
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
62547e
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
62547e
Message-Id: <20220811121111.9878-6-frankja@linux.ibm.com>
62547e
(cherry picked from commit dddf725f70bfe7f5adb41fa31dbd06e767271bda)
62547e
Signed-off-by: Cédric Le Goater <clg@redhat.com>
62547e
---
62547e
 dump/dump.c           | 53 +++++++++++++++++++++++++------------------
62547e
 include/sysemu/dump.h | 13 ++++++++---
62547e
 2 files changed, 41 insertions(+), 25 deletions(-)
62547e
62547e
diff --git a/dump/dump.c b/dump/dump.c
62547e
index e6aa037f59..f6fe13e258 100644
62547e
--- a/dump/dump.c
62547e
+++ b/dump/dump.c
62547e
@@ -60,6 +60,11 @@ static inline bool dump_is_64bit(DumpState *s)
62547e
     return s->dump_info.d_class == ELFCLASS64;
62547e
 }
62547e
 
62547e
+static inline bool dump_has_filter(DumpState *s)
62547e
+{
62547e
+    return s->filter_area_length > 0;
62547e
+}
62547e
+
62547e
 uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
62547e
 {
62547e
     if (s->dump_info.d_endian == ELFDATA2LSB) {
62547e
@@ -444,29 +449,30 @@ static void get_offset_range(hwaddr phys_addr,
62547e
     *p_offset = -1;
62547e
     *p_filesz = 0;
62547e
 
62547e
-    if (s->has_filter) {
62547e
-        if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
62547e
+    if (dump_has_filter(s)) {
62547e
+        if (phys_addr < s->filter_area_begin ||
62547e
+            phys_addr >= s->filter_area_begin + s->filter_area_length) {
62547e
             return;
62547e
         }
62547e
     }
62547e
 
62547e
     QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
62547e
-        if (s->has_filter) {
62547e
-            if (block->target_start >= s->begin + s->length ||
62547e
-                block->target_end <= s->begin) {
62547e
+        if (dump_has_filter(s)) {
62547e
+            if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
62547e
+                block->target_end <= s->filter_area_begin) {
62547e
                 /* This block is out of the range */
62547e
                 continue;
62547e
             }
62547e
 
62547e
-            if (s->begin <= block->target_start) {
62547e
+            if (s->filter_area_begin <= block->target_start) {
62547e
                 start = block->target_start;
62547e
             } else {
62547e
-                start = s->begin;
62547e
+                start = s->filter_area_begin;
62547e
             }
62547e
 
62547e
             size_in_block = block->target_end - start;
62547e
-            if (s->begin + s->length < block->target_end) {
62547e
-                size_in_block -= block->target_end - (s->begin + s->length);
62547e
+            if (s->filter_area_begin + s->filter_area_length < block->target_end) {
62547e
+                size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
62547e
             }
62547e
         } else {
62547e
             start = block->target_start;
62547e
@@ -639,12 +645,12 @@ static void dump_iterate(DumpState *s, Error **errp)
62547e
     int64_t memblock_size, memblock_start;
62547e
 
62547e
     QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
62547e
-        memblock_start = dump_filtered_memblock_start(block, s->begin, s->length);
62547e
+        memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
62547e
         if (memblock_start == -1) {
62547e
             continue;
62547e
         }
62547e
 
62547e
-        memblock_size = dump_filtered_memblock_size(block, s->begin, s->length);
62547e
+        memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
62547e
 
62547e
         /* Write the memory to file */
62547e
         write_memory(s, block, memblock_start, memblock_size, errp);
62547e
@@ -1513,14 +1519,14 @@ static int validate_start_block(DumpState *s)
62547e
 {
62547e
     GuestPhysBlock *block;
62547e
 
62547e
-    if (!s->has_filter) {
62547e
+    if (!dump_has_filter(s)) {
62547e
         return 0;
62547e
     }
62547e
 
62547e
     QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
62547e
         /* This block is out of the range */
62547e
-        if (block->target_start >= s->begin + s->length ||
62547e
-            block->target_end <= s->begin) {
62547e
+        if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
62547e
+            block->target_end <= s->filter_area_begin) {
62547e
             continue;
62547e
         }
62547e
         return 0;
62547e
@@ -1559,10 +1565,10 @@ static int64_t dump_calculate_size(DumpState *s)
62547e
     int64_t size = 0, total = 0, left = 0, right = 0;
62547e
 
62547e
     QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
62547e
-        if (s->has_filter) {
62547e
+        if (dump_has_filter(s)) {
62547e
             /* calculate the overlapped region. */
62547e
-            left = MAX(s->begin, block->target_start);
62547e
-            right = MIN(s->begin + s->length, block->target_end);
62547e
+            left = MAX(s->filter_area_begin, block->target_start);
62547e
+            right = MIN(s->filter_area_begin + s->filter_area_length, block->target_end);
62547e
             size = right - left;
62547e
             size = size > 0 ? size : 0;
62547e
         } else {
62547e
@@ -1652,9 +1658,12 @@ static void dump_init(DumpState *s, int fd, bool has_format,
62547e
     }
62547e
 
62547e
     s->fd = fd;
62547e
-    s->has_filter = has_filter;
62547e
-    s->begin = begin;
62547e
-    s->length = length;
62547e
+    if (has_filter && !length) {
62547e
+        error_setg(errp, QERR_INVALID_PARAMETER, "length");
62547e
+        goto cleanup;
62547e
+    }
62547e
+    s->filter_area_begin = begin;
62547e
+    s->filter_area_length = length;
62547e
 
62547e
     memory_mapping_list_init(&s->list);
62547e
 
62547e
@@ -1787,8 +1796,8 @@ static void dump_init(DumpState *s, int fd, bool has_format,
62547e
         return;
62547e
     }
62547e
 
62547e
-    if (s->has_filter) {
62547e
-        memory_mapping_filter(&s->list, s->begin, s->length);
62547e
+    if (dump_has_filter(s)) {
62547e
+        memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
62547e
     }
62547e
 
62547e
     /*
62547e
diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
62547e
index 7fce1d4af6..b62513d87d 100644
62547e
--- a/include/sysemu/dump.h
62547e
+++ b/include/sysemu/dump.h
62547e
@@ -166,9 +166,16 @@ typedef struct DumpState {
62547e
     hwaddr memory_offset;
62547e
     int fd;
62547e
 
62547e
-    bool has_filter;
62547e
-    int64_t begin;
62547e
-    int64_t length;
62547e
+    /*
62547e
+     * Dump filter area variables
62547e
+     *
62547e
+     * A filtered dump only contains the guest memory designated by
62547e
+     * the start address and length variables defined below.
62547e
+     *
62547e
+     * If length is 0, no filtering is applied.
62547e
+     */
62547e
+    int64_t filter_area_begin;  /* Start address of partial guest memory area */
62547e
+    int64_t filter_area_length; /* Length of partial guest memory area */
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