Skip to contentSkip to conversation

drop writes to read-only ram device & vfio regions

PATCH Superseded Yan Zhao 2 participants 10 messages Apr 13, 2020 – Apr 15, 2020

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Yan Zhao

author
#0 lore raw

v3 cover drop writes to read-only ram device & vfio regions

patch 1 modifies handler of ram device memory regions to drop guest writes to read-only ram device memory regions

patch 2 modifies handler of non-mmap'd read-only vfio regions to drop guest writes to those regions

patch 3 set read-only flag to mmap'd read-only vfio regions, so that guest writes to those regions would be trapped.
without patch 1, host qemu would then crash on guest write to those read-only regions.
with patch 1, host qemu would drop the writes.

Changelog:
v3:
-refreshed and Cc Stefan for reviewing of tracing part

v2:
-split one big patches into smaller ones (Philippe)
-modify existing trace to record guest writes to read-only memory (Alex) -modify vfio_region_write() to drop guest writes to non-mmap'd read-only
region (Alex)

Yan Zhao (3):
memory: drop guest writes to read-only ram device regions hw/vfio: drop guest writes to ro regions hw/vfio: let read-only flag take effect for mmap'd regions

 hw/vfio/common.c     | 12 +++++++++++-
 hw/vfio/trace-events |  2 +-
 memory.c             |  6 +++++-
 trace-events         |  2 +-
 4 files changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v3 1/3 memory: drop guest writes to read-only ram device regions

for ram device regions, drop guest writes if the regions is read-only.

---
 memory.c     | 6 +++++-
 trace-events | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
memory.c +5 -1
index 601b749906..a380b59980 100644
@@ -1312,7 +1312,11 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
 {
     MemoryRegion *mr = opaque;
 
-    trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
+    trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data,
+                                         size, mr->readonly);
+    if (mr->readonly) {
+        return;
+    }
 
     switch (size) {
     case 1:
trace-events +1 -1
index 42107ebc69..e1de662973 100644
@@ -61,7 +61,7 @@ memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t value,
 memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
 memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
 memory_region_ram_device_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
-memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
+memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size, bool readonly) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u" " is_readonly_region=%d"
 flatview_new(void *view, void *root) "%p (root %p)"
 flatview_destroy(void *view, void *root) "%p (root %p)"
 flatview_destroy_rcu(void *view, void *root) "%p (root %p)"
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v3 1/3] memory: drop guest writes to read-only ram device regions

Quoted 14 lines · Yan Zhao

for ram device regions, drop guest writes if the regions is read-only.

---
  memory.c     | 6 +++++-
  trace-events | 2 +-
  2 files changed, 6 insertions(+), 2 deletions(-)
memory.c +0 -0
index 601b749906..a380b59980 100644

memory.c static void memory_region_ram_device_write(void *opaque, hwaddr addr, · line 1317

Quoted 7 lines
memory.c +3 -1
@@ -1312,7 +1312,11 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
  {
      MemoryRegion *mr = opaque;
 
-    trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
+    trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data,
+                                         size, mr->readonly);
+    if (mr->readonly) {
           qemu_log_mask(LOG_GUEST_ERROR, ...)?
Quoted 19 lines
memory.c +2 -0
+        return;
+    }
 
      switch (size) {
      case 1:
trace-events +1 -1
index 42107ebc69..e1de662973 100644
@@ -61,7 +61,7 @@ memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t value,
  memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
  memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
  memory_region_ram_device_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
-memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
+memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size, bool readonly) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u" " is_readonly_region=%d"
  flatview_new(void *view, void *root) "%p (root %p)"
  flatview_destroy(void *view, void *root) "%p (root %p)"
  flatview_destroy_rcu(void *view, void *root) "%p (root %p)"
author
#3 lore raw

Re: [PATCH v3 1/3] memory: drop guest writes to read-only ram device regions

Quoted 18 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Yan Zhao

for ram device regions, drop guest writes if the regions is read-only.

---
  memory.c     | 6 +++++-
  trace-events | 2 +-
  2 files changed, 6 insertions(+), 2 deletions(-)
memory.c +0 -0
index 601b749906..a380b59980 100644
@@ -1312,7 +1312,11 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
  {
      MemoryRegion *mr = opaque;
 
Quoted 7 lines

memory.c static void memory_region_ram_device_write(void *opaque, hwaddr addr, · line 1317

Quoted 4 lines
memory.c +3 -1
-    trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
+    trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data,
+                                         size, mr->readonly);
+    if (mr->readonly) {
            qemu_log_mask(LOG_GUEST_ERROR, ...)?

ok. will add it in next version.

Thanks
Yan

Quoted 20 lines
Quoted 19 lines
memory.c +2 -0
+        return;
+    }
 
      switch (size) {
      case 1:
trace-events +1 -1
index 42107ebc69..e1de662973 100644
@@ -61,7 +61,7 @@ memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t value,
  memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
  memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
  memory_region_ram_device_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
-memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
+memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size, bool readonly) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u" " is_readonly_region=%d"
  flatview_new(void *view, void *root) "%p (root %p)"
  flatview_destroy(void *view, void *root) "%p (root %p)"
  flatview_destroy_rcu(void *view, void *root) "%p (root %p)"
author
#4 lore raw

PATCH v3 2/3 hw/vfio: drop guest writes to ro regions

In reply to Yan Zhao

for vfio regions that are without write permission, drop guest writes to those regions.

---
 hw/vfio/common.c     | 8 +++++++-
 hw/vfio/trace-events | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)
hw/vfio/common.c +7 -1
index 0b3593b3c0..fd6ee1fe3e 100644
@@ -190,6 +190,11 @@ void vfio_region_write(void *opaque, hwaddr addr,
         uint64_t qword;
     } buf;
 
+    if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+        trace_vfio_region_write(vbasedev->name, region->nr,
+                                   addr, data, size, true);
+        return;
+    }
     switch (size) {
     case 1:
         buf.byte = data;
@@ -215,7 +220,8 @@ void vfio_region_write(void *opaque, hwaddr addr,
                      addr, data, size);
     }
 
-    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
+    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size,
+                            false);
 
     /*
      * A read or write to a BAR always signals an INTx EOI.  This will
hw/vfio/trace-events +1 -1
index b1ef55a33f..fb9ff604e6 100644
@@ -91,7 +91,7 @@ vfio_pci_nvlink2_setup_quirk_ssatgt(const char *name, uint64_t tgt, uint64_t siz
 vfio_pci_nvlink2_setup_quirk_lnkspd(const char *name, uint32_t link_speed) "%s link_speed=0x%x"
 
 # common.c
-vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)"
+vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size, bool readonly) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)" " is_readonly_region=%d."
 vfio_region_read(char *name, int index, uint64_t addr, unsigned size, uint64_t data) " (%s:region%d+0x%"PRIx64", %d) = 0x%"PRIx64
 vfio_iommu_map_notify(const char *op, uint64_t iova_start, uint64_t iova_end) "iommu %s @ 0x%"PRIx64" - 0x%"PRIx64
 vfio_listener_region_add_skip(uint64_t start, uint64_t end) "SKIPPING region_add 0x%"PRIx64" - 0x%"PRIx64
Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH v3 2/3] hw/vfio: drop guest writes to ro regions

Hi Yan,

Quoted 15 lines · Yan Zhao

for vfio regions that are without write permission, drop guest writes to those regions.

---
  hw/vfio/common.c     | 8 +++++++-
  hw/vfio/trace-events | 2 +-
  2 files changed, 8 insertions(+), 2 deletions(-)
hw/vfio/common.c +0 -0
index 0b3593b3c0..fd6ee1fe3e 100644

hw/vfio/common.c void vfio_region_write(void *opaque, hwaddr addr, · line 192

Quoted 3 lines
hw/vfio/common.c +0 -0
@@ -190,6 +190,11 @@ void vfio_region_write(void *opaque, hwaddr addr,
          uint64_t qword;
      } buf;
 

I'd move the trace here (trace always):

       trace_vfio_region_write(vbasedev->name, region->nr, addr, data, 

size);

hw/vfio/common.c void vfio_region_write(void *opaque, hwaddr addr, · line 195

Quoted 3 lines
hw/vfio/common.c +3 -0
+    if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+        trace_vfio_region_write(vbasedev->name, region->nr,
+                                   addr, data, size, true);

And use qemu_log_mask(LOG_GUEST_ERROR, ...) here instead.

Quoted 29 lines
hw/vfio/common.c +4 -1
+        return;
+    }
      switch (size) {
      case 1:
          buf.byte = data;
@@ -215,7 +220,8 @@ void vfio_region_write(void *opaque, hwaddr addr,
                       addr, data, size);
      }
 
-    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
+    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size,
+                            false);
 
      /*
       * A read or write to a BAR always signals an INTx EOI.  This will
hw/vfio/trace-events +1 -1
index b1ef55a33f..fb9ff604e6 100644
@@ -91,7 +91,7 @@ vfio_pci_nvlink2_setup_quirk_ssatgt(const char *name, uint64_t tgt, uint64_t siz
  vfio_pci_nvlink2_setup_quirk_lnkspd(const char *name, uint32_t link_speed) "%s link_speed=0x%x"
 
  # common.c
-vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)"
+vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size, bool readonly) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)" " is_readonly_region=%d."
  vfio_region_read(char *name, int index, uint64_t addr, unsigned size, uint64_t data) " (%s:region%d+0x%"PRIx64", %d) = 0x%"PRIx64
  vfio_iommu_map_notify(const char *op, uint64_t iova_start, uint64_t iova_end) "iommu %s @ 0x%"PRIx64" - 0x%"PRIx64
  vfio_listener_region_add_skip(uint64_t start, uint64_t end) "SKIPPING region_add 0x%"PRIx64" - 0x%"PRIx64
author
#6 lore raw

Re: [PATCH v3 2/3] hw/vfio: drop guest writes to ro regions

Quoted 24 lines · Philippe Mathieu-Daudé

Hi Yan,

Quoted 15 lines · Yan Zhao

for vfio regions that are without write permission, drop guest writes to those regions.

---
  hw/vfio/common.c     | 8 +++++++-
  hw/vfio/trace-events | 2 +-
  2 files changed, 8 insertions(+), 2 deletions(-)
hw/vfio/common.c +0 -0
index 0b3593b3c0..fd6ee1fe3e 100644

hw/vfio/common.c void vfio_region_write(void *opaque, hwaddr addr, · line 192

Quoted 3 lines
hw/vfio/common.c +0 -0
@@ -190,6 +190,11 @@ void vfio_region_write(void *opaque, hwaddr addr,
          uint64_t qword;
      } buf;
 

I'd move the trace here (trace always):

Quoted 9 lines
        trace_vfio_region_write(vbasedev->name, region->nr, addr, data, 

size);

hw/vfio/common.c void vfio_region_write(void *opaque, hwaddr addr, · line 195

Quoted 3 lines
hw/vfio/common.c +3 -0
+    if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+        trace_vfio_region_write(vbasedev->name, region->nr,
+                                   addr, data, size, true);

And use qemu_log_mask(LOG_GUEST_ERROR, ...) here instead.

ok. will change it. Thanks!

Yan

Quoted 30 lines
Quoted 29 lines
hw/vfio/common.c +4 -1
+        return;
+    }
      switch (size) {
      case 1:
          buf.byte = data;
@@ -215,7 +220,8 @@ void vfio_region_write(void *opaque, hwaddr addr,
                       addr, data, size);
      }
 
-    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
+    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size,
+                            false);
 
      /*
       * A read or write to a BAR always signals an INTx EOI.  This will
hw/vfio/trace-events +1 -1
index b1ef55a33f..fb9ff604e6 100644
@@ -91,7 +91,7 @@ vfio_pci_nvlink2_setup_quirk_ssatgt(const char *name, uint64_t tgt, uint64_t siz
  vfio_pci_nvlink2_setup_quirk_lnkspd(const char *name, uint32_t link_speed) "%s link_speed=0x%x"
 
  # common.c
-vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)"
+vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size, bool readonly) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)" " is_readonly_region=%d."
  vfio_region_read(char *name, int index, uint64_t addr, unsigned size, uint64_t data) " (%s:region%d+0x%"PRIx64", %d) = 0x%"PRIx64
  vfio_iommu_map_notify(const char *op, uint64_t iova_start, uint64_t iova_end) "iommu %s @ 0x%"PRIx64" - 0x%"PRIx64
  vfio_listener_region_add_skip(uint64_t start, uint64_t end) "SKIPPING region_add 0x%"PRIx64" - 0x%"PRIx64
author
#7 lore raw

PATCH v3 3/3 hw/vfio: let read-only flag take effect for mmap'd regions

In reply to Yan Zhao

along side setting host page table to be read-only, the memory regions are also required to be read-only, so that when guest writes to the read-only & mmap'd regions, vmexits would happen and region write handlers are called.

---
 hw/vfio/common.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/vfio/common.c +4 -0
index fd6ee1fe3e..fc7618e041 100644
@@ -977,6 +977,10 @@ int vfio_region_mmap(VFIORegion *region)
                                           name, region->mmaps[i].size,
                                           region->mmaps[i].mmap);
         g_free(name);
+
+        if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+            memory_region_set_readonly(&region->mmaps[i].mem, true);
+        }
         memory_region_add_subregion(region->mem, region->mmaps[i].offset,
                                     &region->mmaps[i].mem);
 
Signature · 1 line
2.17.1
Reviewed-by on v3 3/3
#8 lore raw

Re: [PATCH v3 3/3] hw/vfio: let read-only flag take effect for mmap'd regions

Quoted 17 lines · Yan Zhao

along side setting host page table to be read-only, the memory regions are also required to be read-only, so that when guest writes to the read-only & mmap'd regions, vmexits would happen and region write handlers are called.

---
  hw/vfio/common.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/vfio/common.c +0 -0
index fd6ee1fe3e..fc7618e041 100644

hw/vfio/common.c int vfio_region_mmap(VFIORegion *region) · line 986

Quoted 10 lines
hw/vfio/common.c +4 -0
@@ -977,6 +977,10 @@ int vfio_region_mmap(VFIORegion *region)
                                            name, region->mmaps[i].size,
                                            region->mmaps[i].mmap);
          g_free(name);
+
+        if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+            memory_region_set_readonly(&region->mmaps[i].mem, true);
+        }
          memory_region_add_subregion(region->mem, region->mmaps[i].offset,
                                      &region->mmaps[i].mem);
 
author
#9 lore raw

Re: [PATCH v3 3/3] hw/vfio: let read-only flag take effect for mmap'd regions

Quoted 22 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Yan Zhao

along side setting host page table to be read-only, the memory regions are also required to be read-only, so that when guest writes to the read-only & mmap'd regions, vmexits would happen and region write handlers are called.

---
  hw/vfio/common.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/vfio/common.c +2 -0
index fd6ee1fe3e..fc7618e041 100644
@@ -977,6 +977,10 @@ int vfio_region_mmap(VFIORegion *region)
                                            name, region->mmaps[i].size,
                                            region->mmaps[i].mmap);
          g_free(name);
+
+        if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) {
Quoted 8 lines

hw/vfio/common.c int vfio_region_mmap(VFIORegion *region) · line 986

Quoted 6 lines
hw/vfio/common.c +2 -0
+            memory_region_set_readonly(&region->mmaps[i].mem, true);
+        }
          memory_region_add_subregion(region->mem, region->mmaps[i].offset,
                                      &region->mmaps[i].mem);
 

Thanks!