qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hw/vfio: let readonly flag take effect for mmaped regions
@ 2020-03-27 11:19 yan.y.zhao
  2020-03-27 10:51 ` Philippe Mathieu-Daudé
  2020-03-27 17:25 ` Alex Williamson
  0 siblings, 2 replies; 12+ messages in thread
From: yan.y.zhao @ 2020-03-27 11:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, alex.williamson, Yan Zhao, Xin Zeng

From: Yan Zhao <yan.y.zhao@intel.com>

currently, vfio regions without VFIO_REGION_INFO_FLAG_WRITE are only
read-only when VFIO_REGION_INFO_FLAG_MMAP is not set.

regions with flag VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_MMAP
are only read-only in host page table for qemu.

This patch sets corresponding ept page entries read-only for regions
with flag VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_MMAP.

accordingly, it ignores guest write when guest writes to the read-only
regions are trapped.

Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
Signed-off-by: Xin Zeng <xin.zeng@intel.com>
---
 hw/vfio/common.c | 4 ++++
 memory.c         | 3 +++
 2 files changed, 7 insertions(+)

diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index 0b3593b3c0..e901621ca0 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -971,6 +971,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);
 
diff --git a/memory.c b/memory.c
index 601b749906..4b1071dc74 100644
--- a/memory.c
+++ b/memory.c
@@ -1313,6 +1313,9 @@ 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);
+    if (mr->readonly) {
+        return;
+    }
 
     switch (size) {
     case 1:
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-04-01  6:57 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-27 11:19 [PATCH] hw/vfio: let readonly flag take effect for mmaped regions yan.y.zhao
2020-03-27 10:51 ` Philippe Mathieu-Daudé
2020-03-27 16:17   ` Paolo Bonzini
2020-03-31  7:59     ` Philippe Mathieu-Daudé
2020-04-01  6:47       ` Yan Zhao
2020-03-27 17:25 ` Alex Williamson
2020-03-30  1:35   ` Yan Zhao
2020-03-30  6:34     ` Yan Zhao
2020-03-30 14:59       ` Alex Williamson
2020-03-31  1:59         ` Yan Zhao
2020-03-31 19:28           ` Alex Williamson
2020-04-01  6:45             ` Yan Zhao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).