Skip to contentSkip to conversation

exec.c: Allow memory region start_addr and region_offset to vary in low bits

PATCH In review Peter Maydell 2 participants 4 messages Dec 5, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 exec.c: Allow memory region start_addr and region_offset to vary in low bits

Fix a long-standing deficiency of cpu_register_physical_memory_log() where the start address and region offset had to have the same low bits (otherwise the IO functions would be passed an incorrect address offset). This was most likely to bite when registering memory regions which started at a non-page-boundary.

---
This is such a small change to correct this issue that I'm kind of suspicious of it :-)

 exec.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
exec.c +6 -5
index 6b92198..7030cea 100644
@@ -2655,10 +2655,7 @@ static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
    For RAM, 'size' must be a multiple of the target page size.
    If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
    io memory page.  The address used when calling the IO function is
-   the offset from the start of the region, plus region_offset.  Both
-   start_addr and region_offset are rounded down to a page boundary
-   before calculating this offset.  This should not be a problem unless
-   the low bits of start_addr and region_offset differ.  */
+   the offset from the start of the region, plus region_offset. */
 void cpu_register_physical_memory_log(target_phys_addr_t start_addr,
                                          ram_addr_t size,
                                          ram_addr_t phys_offset,
@@ -2677,7 +2674,11 @@ void cpu_register_physical_memory_log(target_phys_addr_t start_addr,
     if (phys_offset == IO_MEM_UNASSIGNED) {
         region_offset = start_addr;
     }
-    region_offset &= TARGET_PAGE_MASK;
+    /* Adjust the region offset to account for the start_addr possibly
+     * not being page aligned, so we end up passing the IO functions
+     * the true offset from the start of the region.
+     */
+    region_offset -= (start_addr & ~TARGET_PAGE_MASK);
     size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
     end_addr = start_addr + (target_phys_addr_t)size;
 
Signature · 1 line
1.7.1
#1 lore raw
Quoted 10 lines · Peter Maydell

Fix a long-standing deficiency of cpu_register_physical_memory_log() where the start address and region offset had to have the same low bits (otherwise the IO functions would be passed an incorrect address offset). This was most likely to bite when registering memory regions which started at a non-page-boundary.

---
This is such a small change to correct this issue that I'm kind of suspicious of it :-)

Your instincts are correct, unfortunately.

exec.c void cpu_register_physical_memory_log(target_phys_addr_t start_addr, · line 2684

Quoted 13 lines
exec.c +5 -1
@@ -2677,7 +2674,11 @@ void cpu_register_physical_memory_log(target_phys_addr_t start_addr,
     if (phys_offset == IO_MEM_UNASSIGNED) {
         region_offset = start_addr;
     }
-    region_offset &= TARGET_PAGE_MASK;
+    /* Adjust the region offset to account for the start_addr possibly
+     * not being page aligned, so we end up passing the IO functions
+     * the true offset from the start of the region.
+     */
+    region_offset -= (start_addr & ~TARGET_PAGE_MASK);
     size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
     end_addr = start_addr + (target_phys_addr_t)size;
 

region_offset is added to iotlb in tlb_set_page(), smashing the low bits with your change. It's safe in subpage, since that doesn't happen there.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 17 lines · Avi Kivity

exec.c void cpu_register_physical_memory_log(target_phys_addr_t start_addr, · line 2684

Quoted 13 lines · Peter Maydell
exec.c +5 -1
@@ -2677,7 +2674,11 @@ void cpu_register_physical_memory_log(target_phys_addr_t start_addr,
      if (phys_offset == IO_MEM_UNASSIGNED) {
          region_offset = start_addr;
      }
-    region_offset &= TARGET_PAGE_MASK;
+    /* Adjust the region offset to account for the start_addr possibly
+     * not being page aligned, so we end up passing the IO functions
+     * the true offset from the start of the region.
+     */
+    region_offset -= (start_addr & ~TARGET_PAGE_MASK);
      size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
      end_addr = start_addr + (target_phys_addr_t)size;
 

region_offset is added to iotlb in tlb_set_page(), smashing the low bits with your change.  It's safe in subpage, since that doesn't happen there.

OK, but we only need to avoid trashing the bottom 5 bits, right? So we could do

    region_offset -= (start_addr & ~TARGET_PAGE_MASK);
    if (size >= TARGET_PAGE_SIZE) {
        region_offset &= ~0x1F; /* can make this a #define IO_MEM_MASK */
    }

which would allow regions to start on 0x20 granularity, or byte granularity if they're less than a page in size (and so guaranteed to be subpages only).

-- PMM

#3 lore raw
Quoted 19 lines · Peter Maydell
Quoted 17 lines · Avi Kivity

exec.c void cpu_register_physical_memory_log(target_phys_addr_t start_addr, · line 2684

Quoted 13 lines · Peter Maydell
exec.c +5 -1
@@ -2677,7 +2674,11 @@ void cpu_register_physical_memory_log(target_phys_addr_t start_addr,
     if (phys_offset == IO_MEM_UNASSIGNED) {
         region_offset = start_addr;
     }
-    region_offset &= TARGET_PAGE_MASK;
+    /* Adjust the region offset to account for the start_addr possibly
+     * not being page aligned, so we end up passing the IO functions
+     * the true offset from the start of the region.
+     */
+    region_offset -= (start_addr & ~TARGET_PAGE_MASK);
     size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
     end_addr = start_addr + (target_phys_addr_t)size;
 

region_offset is added to iotlb in tlb_set_page(), smashing the low bits with your change. It's safe in subpage, since that doesn't happen there.

Quoted 1 line

OK, but we only need to avoid trashing the bottom 5 bits, right?

All TARGET_PAGE_BITS of them.

Quoted 9 lines

So we could do

    region_offset -= (start_addr & ~TARGET_PAGE_MASK);
    if (size >= TARGET_PAGE_SIZE) {
        region_offset &= ~0x1F; /* can make this a #define IO_MEM_MASK */
    }

which would allow regions to start on 0x20 granularity, or byte granularity if they're less than a page in size (and so guaranteed to be subpages only).

An alternative is to stash region_offset somewhere else. There's CPUTLBEntry::addend, see comment above its definition.

Signature · 1 line
error compiling committee.c: too many arguments to function