Skip to contentSkip to conversation

hvf: Avoid mapping regions < PAGE_SIZE as ram

PATCH Applied Alexander Graf 3 participants 5 messages Oct 25, 2021 – Oct 26, 2021

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hvf: Avoid mapping regions < PAGE_SIZE as ram

HVF has generic memory listener code that adds all RAM regions as HVF RAM regions. However, HVF can only handle page aligned, page granule regions.

So let's ignore regions that are not page aligned and sized. They will be trapped as MMIO instead.

---
 accel/hvf/hvf-accel-ops.c | 6 ++++++
 1 file changed, 6 insertions(+)
accel/hvf/hvf-accel-ops.c +6 -0
index 6bf319d34c..090155853a 100644
@@ -135,6 +135,12 @@ static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
         }
     }
 
+    if (int128_get64(section->size) & (qemu_real_host_page_size - 1) ||
+        section->offset_within_address_space & (qemu_real_host_page_size - 1)) {
+        /* Not page aligned, so we can not map as RAM */
+        add = false;
+    }
+
     mem = hvf_find_overlap_slot(
             section->offset_within_address_space,
             int128_get64(section->size));
Signature · 1 line
2.30.1 (Apple Git-130)
#1 lore raw

Hi Alex,

Quoted 16 lines · Alexander Graf

HVF has generic memory listener code that adds all RAM regions as HVF RAM regions. However, HVF can only handle page aligned, page granule regions.

So let's ignore regions that are not page aligned and sized. They will be trapped as MMIO instead.

---
 accel/hvf/hvf-accel-ops.c | 6 ++++++
 1 file changed, 6 insertions(+)
accel/hvf/hvf-accel-ops.c +0 -0
index 6bf319d34c..090155853a 100644

accel/hvf/hvf-accel-ops.c static void hvf_set_phys_mem(MemoryRegionSection *section, bool add) · line 139

Quoted 5 lines
accel/hvf/hvf-accel-ops.c +2 -0
@@ -135,6 +135,12 @@ static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
         }
     }
 
+    if (int128_get64(section->size) & (qemu_real_host_page_size - 1) ||
+        section->offset_within_address_space & (qemu_real_host_page_size - 1)) {

Could we use QEMU_IS_ALIGNED() instead?

Quoted 8 lines
accel/hvf/hvf-accel-ops.c +4 -0
+        /* Not page aligned, so we can not map as RAM */
+        add = false;
+    }
+
     mem = hvf_find_overlap_slot(
             section->offset_within_address_space,
             int128_get64(section->size));
#2 lore raw
In reply to Alexander Graf
Quoted 19 lines · Alexander Graf

HVF has generic memory listener code that adds all RAM regions as HVF RAM regions. However, HVF can only handle page aligned, page granule regions.

So let's ignore regions that are not page aligned and sized. They will be trapped as MMIO instead.

---
  accel/hvf/hvf-accel-ops.c | 6 ++++++
  1 file changed, 6 insertions(+)
accel/hvf/hvf-accel-ops.c +0 -0
index 6bf319d34c..090155853a 100644
@@ -135,6 +135,12 @@ static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
          }
      }

accel/hvf/hvf-accel-ops.c static void hvf_set_phys_mem(MemoryRegionSection *section, bool add) · line 146

Quoted 10 lines
accel/hvf/hvf-accel-ops.c +6 -0
 
+    if (int128_get64(section->size) & (qemu_real_host_page_size - 1) ||
+        section->offset_within_address_space & (qemu_real_host_page_size - 1)) {
+        /* Not page aligned, so we can not map as RAM */
+        add = false;
+    }
+
      mem = hvf_find_overlap_slot(
              section->offset_within_address_space,
              int128_get64(section->size));

Queued, thanks.

Paolo

#3 lore raw
Quoted 25 lines · Paolo Bonzini
Quoted 24 lines · Alexander Graf

HVF has generic memory listener code that adds all RAM regions as HVF RAM regions. However, HVF can only handle page aligned, page granule regions.

So let's ignore regions that are not page aligned and sized. They will be trapped as MMIO instead.

---
  accel/hvf/hvf-accel-ops.c | 6 ++++++

  1 file changed, 6 insertions(+)
accel/hvf/hvf-accel-ops.c +0 -0
index 6bf319d34c..090155853a 100644
@@ -135,6 +135,12 @@ static void hvf_set_phys_mem(MemoryRegionSection 
 *section, bool add)
           }
       }

  +    if (int128_get64(section->size) & (qemu_real_host_page_size - 1) || +        section->offset_within_address_space & (qemu_real_host_page_size - 1)) {
+        /* Not page aligned, so we can not map as RAM */
+        add = false;

Quoted 8 lines
Quoted 6 lines

+    }
+
      mem = hvf_find_overlap_slot( section->offset_within_address_space, int128_get64(section->size));

Queued, thanks.

You probably want v2 instead :)

Alex

#4 lore raw
Quoted 14 lines · Alexander Graf
Quoted 14 lines
Quoted 13 lines

          }
      }
  +    if (int128_get64(section->size) & (qemu_real_host_page_size - 1) || +        section->offset_within_address_space & (qemu_real_host_page_size - 1)) {
+        /* Not page aligned, so we can not map as RAM */
+        add = false;
+    }
+
      mem = hvf_find_overlap_slot( section->offset_within_address_space, int128_get64(section->size));

Quoted 4 lines
Quoted 1 line

Queued, thanks.

You probably want v2 instead :)

That's actually what I had applied. :)

Paolo