Skip to contentSkip to conversation

target/i386/mshv: fix read and write memory across the page boundary

PATCH In review Doru Blânzeanu 3 participants 6 messages Apr 9 – Oct 12

lore
Share & tools
lore
#0 lore raw

cover target/i386/mshv: fix read and write memory across the page boundary

This fixes the guest memory reading and writing spanning multiple pages for mshv Port I/O.

Currently, the read_memory and write_memory functions only translate GVA-to-GPA once per buffer. In case the buffer is bigger than a page, the translation is only valid for the first page.

Doru Blânzeanu (1):
target/i386/mshv: fix read/write memory across the page boundary

 target/i386/mshv/mshv-cpu.c | 71 +++++++++++++++++++++++++++----------
 1 file changed, 52 insertions(+), 19 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/1 target/i386/mshv: fix read/write memory across the page boundary

Previously, read_memory and write_memory performed a single GVA-to-GPA translation for the entire buffer. If the buffer spanned a page boundary, the translated GPA was only valid for the first page, causing incorrect reads/writes for the remainder.

Fix both functions to loop over pages, translating and accessing each page-aligned chunk separately.

---
 target/i386/mshv/mshv-cpu.c | 71 +++++++++++++++++++++++++++----------
 1 file changed, 52 insertions(+), 19 deletions(-)
target/i386/mshv/mshv-cpu.c +52 -19
index 2bc978deb2..afdb6b6e29 100644
@@ -1316,21 +1316,38 @@ static int read_memory(const CPUState *cpu, uint64_t initial_gva,
 {
     int ret;
     uint64_t gpa, flags;
-
-    if (gva == initial_gva) {
-        gpa = initial_gpa;
-    } else {
-        flags = HV_TRANSLATE_GVA_VALIDATE_READ;
-        ret = translate_gva(cpu, gva, &gpa, flags);
-        if (ret < 0) {
-            return -1;
+    uint64_t cur_gva = gva;
+    size_t page_left, chunk;
+    uint8_t *cur_data = data;
+
+    /*
+     * If the read spans multiple pages,
+     * we need to translate and read each page separately
+     */
+    while (len > 0) {
+        page_left = HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1));
+        chunk = MIN(len, page_left);
+
+        if (cur_gva == initial_gva) {
+            gpa = initial_gpa;
+        } else {
+            flags = HV_TRANSLATE_GVA_VALIDATE_READ;
+            ret = translate_gva(cpu, cur_gva, &gpa, flags);
+            if (ret < 0) {
+                return -1;
+            }
         }
 
-        ret = mshv_guest_mem_read(gpa, data, len, false, false);
+        ret = mshv_guest_mem_read(gpa, cur_data, chunk,
+                                  false, false);
         if (ret < 0) {
             error_report("failed to read guest mem");
             return -1;
         }
+
+        cur_gva += chunk;
+        cur_data += chunk;
+        len -= chunk;
     }
 
     return 0;
@@ -1341,18 +1358,34 @@ static int write_memory(const CPUState *cpu, uint64_t gva, const uint8_t *data,
 {
     int ret;
     uint64_t gpa, flags;
+    uint64_t cur_gva = gva;
+    size_t page_left, chunk;
+    const uint8_t *cur_data = data;
+
+    /*
+     * If the write spans multiple pages,
+     * we need to translate and write each page separately
+     */
+    while (len > 0) {
+        page_left = HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1));
+        chunk = MIN(len, page_left);
+
+        flags = HV_TRANSLATE_GVA_VALIDATE_WRITE;
+        ret = translate_gva(cpu, cur_gva, &gpa, flags);
+        if (ret < 0) {
+            error_report("failed to translate gva to gpa");
+            return -1;
+        }
 
-    flags = HV_TRANSLATE_GVA_VALIDATE_WRITE;
-    ret = translate_gva(cpu, gva, &gpa, flags);
-    if (ret < 0) {
-        error_report("failed to translate gva to gpa");
-        return -1;
-    }
+        ret = mshv_guest_mem_write(gpa, cur_data, chunk, false);
+        if (ret != MEMTX_OK) {
+            error_report("failed to write to mmio");
+            return -1;
+        }
 
-    ret = mshv_guest_mem_write(gpa, data, len, false);
-    if (ret != MEMTX_OK) {
-        error_report("failed to write to mmio");
-        return -1;
+        cur_gva += chunk;
+        cur_data += chunk;
+        len -= chunk;
     }
 
     return 0;
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH 1/1] target/i386/mshv: fix read/write memory across the page boundary

Quoted 9 lines · Doru Blânzeanu

Previously, read_memory and write_memory performed a single GVA-to-GPA translation for the entire buffer. If the buffer spanned a page boundary, the translated GPA was only valid for the first page, causing incorrect reads/writes for the remainder.

Quoted 4 lines

Fix both functions to loop over pages, translating and accessing each page-aligned chunk separately.

Hi,

Quoted 105 lines

---
target/i386/mshv/mshv-cpu.c | 71 +++++++++++++++++++++++++++----------

1 file changed, 52 insertions(+), 19 deletions(-)
target/i386/mshv/mshv-cpu.c +52 -19
index 2bc978deb2..afdb6b6e29 100644
@@ -1316,21 +1316,38 @@ static int read_memory(const CPUState *cpu, uint64_t initial_gva,
 {
    int ret;
    uint64_t gpa, flags;
-
-    if (gva == initial_gva) {
-        gpa = initial_gpa;
-    } else {
-        flags = HV_TRANSLATE_GVA_VALIDATE_READ;
-        ret = translate_gva(cpu, gva, &gpa, flags);
-        if (ret < 0) {
-            return -1;
+    uint64_t cur_gva = gva;
+    size_t page_left, chunk;
+    uint8_t *cur_data = data;
+
+    /*
+     * If the read spans multiple pages,
+     * we need to translate and read each page separately
+     */
+    while (len > 0) {
+        page_left = HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1));
+        chunk = MIN(len, page_left);
+
+        if (cur_gva == initial_gva) {
+            gpa = initial_gpa;
+        } else {
+            flags = HV_TRANSLATE_GVA_VALIDATE_READ;
+            ret = translate_gva(cpu, cur_gva, &gpa, flags);
+            if (ret < 0) {
+                return -1;
+            }
        }
 
-        ret = mshv_guest_mem_read(gpa, data, len, false, false);
+        ret = mshv_guest_mem_read(gpa, cur_data, chunk,
+                                  false, false);
        if (ret < 0) {
            error_report("failed to read guest mem");
            return -1;
        }
+
+        cur_gva += chunk;
+        cur_data += chunk;
+        len -= chunk;
    }
 
    return 0;
@@ -1341,18 +1358,34 @@ static int write_memory(const CPUState *cpu, uint64_t gva, const uint8_t *data,
 {
    int ret;
    uint64_t gpa, flags;
+    uint64_t cur_gva = gva;
+    size_t page_left, chunk;
+    const uint8_t *cur_data = data;
+
+    /*
+     * If the write spans multiple pages,
+     * we need to translate and write each page separately
+     */
+    while (len > 0) {
+        page_left = HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1));
+        chunk = MIN(len, page_left);
+
+        flags = HV_TRANSLATE_GVA_VALIDATE_WRITE;
+        ret = translate_gva(cpu, cur_gva, &gpa, flags);
+        if (ret < 0) {
+            error_report("failed to translate gva to gpa");
+            return -1;
+        }
 
-    flags = HV_TRANSLATE_GVA_VALIDATE_WRITE;
-    ret = translate_gva(cpu, gva, &gpa, flags);
-    if (ret < 0) {
-        error_report("failed to translate gva to gpa");
-        return -1;
-    }
+        ret = mshv_guest_mem_write(gpa, cur_data, chunk, false);
+        if (ret != MEMTX_OK) {
+            error_report("failed to write to mmio");
+            return -1;
+        }
 
-    ret = mshv_guest_mem_write(gpa, data, len, false);
-    if (ret != MEMTX_OK) {
-        error_report("failed to write to mmio");
-        return -1;
+        cur_gva += chunk;
+        cur_data += chunk;
+        len -= chunk;
    }
 
    return 0;
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#3 lore raw

Re: [PATCH 1/1] target/i386/mshv: fix read/write memory across the page boundary

In reply to Doru Blânzeanu
Quoted 9 lines · Doru Blânzeanu

Previously, read_memory and write_memory performed a single GVA-to-GPA translation for the entire buffer. If the buffer spanned a page boundary, the translated GPA was only valid for the first page, causing incorrect reads/writes for the remainder.

Fix both functions to loop over pages, translating and accessing each page-aligned chunk separately.

Thanks, looks good. Confirmed with tests that the issue is resolved.

#4 lore raw

Re: [PATCH 1/1] target/i386/mshv: fix read/write memory across the page boundary

In reply to Doru Blânzeanu
Quoted 112 lines · Doru Blânzeanu

Previously, read_memory and write_memory performed a single GVA-to-GPA translation for the entire buffer. If the buffer spanned a page boundary, the translated GPA was only valid for the first page, causing incorrect reads/writes for the remainder.

Fix both functions to loop over pages, translating and accessing each page-aligned chunk separately.

---
 target/i386/mshv/mshv-cpu.c | 71 +++++++++++++++++++++++++++----------
 1 file changed, 52 insertions(+), 19 deletions(-)
target/i386/mshv/mshv-cpu.c +52 -19
index 2bc978deb2..afdb6b6e29 100644
@@ -1316,21 +1316,38 @@ static int read_memory(const CPUState *cpu, uint64_t initial_gva,
 {
     int ret;
     uint64_t gpa, flags;
-
-    if (gva == initial_gva) {
-        gpa = initial_gpa;
-    } else {
-        flags = HV_TRANSLATE_GVA_VALIDATE_READ;
-        ret = translate_gva(cpu, gva, &gpa, flags);
-        if (ret < 0) {
-            return -1;
+    uint64_t cur_gva = gva;
+    size_t page_left, chunk;
+    uint8_t *cur_data = data;
+
+    /*
+     * If the read spans multiple pages,
+     * we need to translate and read each page separately
+     */
+    while (len > 0) {
+        page_left = HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1));
+        chunk = MIN(len, page_left);
+
+        if (cur_gva == initial_gva) {
+            gpa = initial_gpa;
+        } else {
+            flags = HV_TRANSLATE_GVA_VALIDATE_READ;
+            ret = translate_gva(cpu, cur_gva, &gpa, flags);
+            if (ret < 0) {
+                return -1;
+            }
         }
 
-        ret = mshv_guest_mem_read(gpa, data, len, false, false);
+        ret = mshv_guest_mem_read(gpa, cur_data, chunk,
+                                  false, false);
         if (ret < 0) {
             error_report("failed to read guest mem");
             return -1;
         }
+
+        cur_gva += chunk;
+        cur_data += chunk;
+        len -= chunk;
     }
 
     return 0;
@@ -1341,18 +1358,34 @@ static int write_memory(const CPUState *cpu, uint64_t gva, const uint8_t *data,
 {
     int ret;
     uint64_t gpa, flags;
+    uint64_t cur_gva = gva;
+    size_t page_left, chunk;
+    const uint8_t *cur_data = data;
+
+    /*
+     * If the write spans multiple pages,
+     * we need to translate and write each page separately
+     */
+    while (len > 0) {
+        page_left = HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1));
+        chunk = MIN(len, page_left);
+
+        flags = HV_TRANSLATE_GVA_VALIDATE_WRITE;
+        ret = translate_gva(cpu, cur_gva, &gpa, flags);
+        if (ret < 0) {
+            error_report("failed to translate gva to gpa");
+            return -1;
+        }
 
-    flags = HV_TRANSLATE_GVA_VALIDATE_WRITE;
-    ret = translate_gva(cpu, gva, &gpa, flags);
-    if (ret < 0) {
-        error_report("failed to translate gva to gpa");
-        return -1;
-    }
+        ret = mshv_guest_mem_write(gpa, cur_data, chunk, false);
+        if (ret != MEMTX_OK) {
+            error_report("failed to write to mmio");
+            return -1;
+        }
 
-    ret = mshv_guest_mem_write(gpa, data, len, false);
-    if (ret != MEMTX_OK) {
-        error_report("failed to write to mmio");
-        return -1;
+        cur_gva += chunk;
+        cur_data += chunk;
+        len -= chunk;
     }
 
     return 0;
Signature · 1 line
2.53.0
Fixes 6dec60528c (target/i386/mshv: Implement mshv_vcpu_run())
#5 lore raw
In reply to Doru Blânzeanu
Quoted 13 lines · Doru Blânzeanu

This fixes the guest memory reading and writing spanning multiple pages for mshv Port I/O.

Currently, the read_memory and write_memory functions only translate GVA-to-GPA once per buffer. In case the buffer is bigger than a page, the translation is only valid for the first page.

Doru Blânzeanu (1):
target/i386/mshv: fix read/write memory across the page boundary

 target/i386/mshv/mshv-cpu.c | 71 +++++++++++++++++++++++++++----------
 1 file changed, 52 insertions(+), 19 deletions(-)
Signature · 1 line
2.53.0

included in mkulke-pull-20261006