Add the following helpers:
 * section_access_allowed()
   - used to check access in GuestMemory::try_access().

 * section_covers_region_addr()
   - used to implement GuestMemoryRegion::check_address().

 * section_get_host_addr()
   - used to implement GuestMemoryRegion::get_host_address().

 * section_fuzz_dma_read()
   - used to insert fuzz hook before read/load.

Signed-off-by: Zhao Liu <zhao1....@intel.com>
---
 include/system/memory.h | 56 +++++++++++++++++++++++++++++++++++++++++
 system/physmem.c        | 30 ++++++++++++++++++++++
 2 files changed, 86 insertions(+)

diff --git a/include/system/memory.h b/include/system/memory.h
index eab69e15e10f..110ad0a3b590 100644
--- a/include/system/memory.h
+++ b/include/system/memory.h
@@ -3357,6 +3357,62 @@ address_space_write_cached(MemoryRegionCache *cache, 
hwaddr addr,
 MemTxResult address_space_set(AddressSpace *as, hwaddr addr,
                               uint8_t c, hwaddr len, MemTxAttrs attrs);
 
+/**
+ * section_access_allowed
+ *
+ * @section: #MemoryRegionSection to be accessed.
+ * @attrs: memory transaction attributes.
+ * @addr: address within that memory region.
+ * @len: the number of bytes to access.
+ *
+ * Check if a memory transaction is allowed.
+ *
+ * Returns: true if transaction is allowed, false if denied.
+ */
+bool section_access_allowed(MemoryRegionSection *section,
+                            MemTxAttrs attrs, hwaddr addr,
+                            hwaddr len);
+
+/**
+ * section_covers_region_addr
+ *
+ * @section: #MemoryRegionSection to be accessed.
+ * @region_addr: memory region address within the region, which is
+ *               pointed by #MemoryRegionSection.
+ *
+ * Check if a region address is coverd by #MemoryRegionSection.
+ *
+ * Returns: true if transaction is allowed, false if denied.
+ */
+bool section_covers_region_addr(const MemoryRegionSection *section,
+                                hwaddr region_addr);
+
+/**
+ * section_get_host_addr
+ *
+ * @section: #MemoryRegionSection to be accessed.
+ * @region_addr: memory region address within the region, which is
+ *               pointed by #MemoryRegionSection.
+ *
+ * Get the pointer to the host address.
+ *
+ * Returns: pointer to the host address.
+ */
+uint8_t *section_get_host_addr(const MemoryRegionSection *section,
+                               hwaddr region_addr);
+
+/**
+ * section_fuzz_dma_read
+ *
+ * @section: #MemoryRegionSection to be accessed.
+ * @addr: memory address to be fuzzed.
+ * @len: length of the memory
+ *
+ * This function is wrapper of fuzz_dma_read_cb().
+ */
+void section_fuzz_dma_read(MemoryRegionSection *section,
+                           hwaddr addr, hwaddr len);
+
 /*
  * Inhibit technologies that require discarding of pages in RAM blocks, e.g.,
  * to manage the actual amount of memory consumed by the VM (then, the memory
diff --git a/system/physmem.c b/system/physmem.c
index 8aaaab4d3a74..e06633f4d8a2 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -320,6 +320,29 @@ static inline bool section_covers_addr(const 
MemoryRegionSection *section,
                              int128_getlo(section->size), addr);
 }
 
+bool section_covers_region_addr(const MemoryRegionSection *section,
+                                hwaddr region_addr)
+{
+    return section->offset_within_region <= region_addr &&
+           section->offset_within_region + int128_get64(section->size) >= 
region_addr;
+}
+
+uint8_t *section_get_host_addr(const MemoryRegionSection *section,
+                               hwaddr region_addr)
+{
+    MemoryRegion *mr = section->mr;
+    assert(mr && mr->ram_block);
+
+    return qemu_map_ram_ptr(mr->ram_block,
+                            section->offset_within_region + region_addr);
+}
+
+void section_fuzz_dma_read(MemoryRegionSection *section,
+                           hwaddr addr, hwaddr len)
+{
+    fuzz_dma_read_cb(addr, len, section->mr);
+}
+
 static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr 
addr)
 {
     PhysPageEntry lp = d->phys_map, *p;
@@ -2947,6 +2970,13 @@ static bool memory_region_access_allowed(MemoryRegion 
*mr, MemTxAttrs attrs,
     return false;
 }
 
+bool section_access_allowed(MemoryRegionSection *section,
+                            MemTxAttrs attrs, hwaddr addr,
+                            hwaddr len)
+{
+    return memory_region_access_allowed(section->mr, attrs, addr, len);
+}
+
 static MemTxResult flatview_write_continue_step(MemTxAttrs attrs,
                                                 const uint8_t *buf,
                                                 hwaddr len, hwaddr mr_addr,
-- 
2.34.1


Reply via email to