[RFC PATCH 1/2] exec: Let memory_access_size() consider minimum valid access size

Philippe Mathieu-Daudé posted 2 patches 5 years, 6 months ago
[RFC PATCH 1/2] exec: Let memory_access_size() consider minimum valid access size
Posted by Philippe Mathieu-Daudé 5 years, 6 months ago
As it is illegal to access a device with less that its
minimum valid size, also check for access_size_min.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 exec.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/exec.c b/exec.c
index 5162f0d12f..d3ec30f995 100644
--- a/exec.c
+++ b/exec.c
@@ -3066,10 +3066,14 @@ void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size)
 
 static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
 {
+    unsigned access_size_min = mr->ops->valid.min_access_size;
     unsigned access_size_max = mr->ops->valid.max_access_size;
 
     /* Regions are assumed to support 1-4 byte accesses unless
        otherwise specified.  */
+    if (access_size_min == 0) {
+        access_size_min = 1;
+    }
     if (access_size_max == 0) {
         access_size_max = 4;
     }
@@ -3082,11 +3086,14 @@ static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
         }
     }
 
-    /* Don't attempt accesses larger than the maximum.  */
-    if (l > access_size_max) {
+    /* Don't attempt accesses not in the minimum/maximum range.  */
+    if (l < access_size_min) {
+        l = access_size_min;
+    } else if (l > access_size_max) {
         l = access_size_max;
+    } else {
+        l = pow2floor(l);
     }
-    l = pow2floor(l);
 
     return l;
 }
-- 
2.21.3